erkeyb-client.c

Go to the documentation of this file.
00001 /*
00002  * File Name: erkeyb-client.c
00003  */
00004 
00005 /*
00006  * This file is part of erkeyb.
00007  *
00008  * erkeyb is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * erkeyb is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2009 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system includes
00032 #include <sys/types.h>
00033 #include <sys/socket.h>
00034 #include <netdb.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <unistd.h>
00038 #include <string.h>
00039 
00040 // gtk
00041 #include <glib.h>
00042 #include <gtk/gtk.h>
00043 
00044 // local include files, between " "
00045 #include "config.h"
00046 #include "erkeyb-client.h"
00047 
00048 // logging
00049 #include "logging.h"
00050 
00051 //----------------------------------------------------------------------------
00052 // MACROS and definitions
00053 //----------------------------------------------------------------------------
00054 
00055 #define HOST "127.0.0.1"
00056 #define PORT "60666"
00057 
00058 //----------------------------------------------------------------------------
00059 // Type Declarations
00060 //----------------------------------------------------------------------------
00061 
00062 typedef struct _keyb_client keyb_client;
00063 
00064 struct _keyb_client {
00065     struct addrinfo hints;
00066     int sfd;
00067     GIOChannel* chan;
00068     gboolean is_closed;
00069 };
00070 
00071 //----------------------------------------------------------------------------
00072 // Static Variables
00073 //----------------------------------------------------------------------------
00074 
00075 static keyb_client g_keyb_client;
00076 
00077 //----------------------------------------------------------------------------
00078 // Functions Prototypes  (keyboard)
00079 //----------------------------------------------------------------------------
00080 
00081 
00082 //============================================================================
00083 // Functions Implementation (keyboard)
00084 //============================================================================
00085 
00086 
00087 int erkeyb_client_init(void) 
00088 {
00089     DBG_ENTRY;
00090     struct addrinfo *result, *rp;
00091     int s;
00092 
00093     /* Obtain address(es) matching host/port */
00094 
00095     memset(&g_keyb_client, 0x0, sizeof(keyb_client));
00096     memset(&g_keyb_client.hints, 0x0, sizeof(struct addrinfo));
00097 
00098     g_keyb_client.hints.ai_family = AF_INET;    /* Allow IPv4 */
00099     g_keyb_client.hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
00100     g_keyb_client.hints.ai_flags = 0;
00101     g_keyb_client.hints.ai_protocol = 0;          /* Any protocol */
00102 
00103     s = getaddrinfo(HOST, PORT, &g_keyb_client.hints, &result);
00104 
00105     if (s != 0) {
00106         ERR("getaddrinfo: %s\n", gai_strerror(s));
00107         return 1;
00108     }
00109 
00110     for (rp = result; rp != NULL; rp = rp->ai_next) {
00111         g_keyb_client.sfd = socket(rp->ai_family, rp->ai_socktype,
00112                 rp->ai_protocol);
00113         if (g_keyb_client.sfd == -1)
00114             continue;
00115 
00116         if (connect(g_keyb_client.sfd, rp->ai_addr, rp->ai_addrlen) != -1)
00117             break;                  /* Success */
00118 
00119         close(g_keyb_client.sfd);
00120     }
00121 
00122     if (rp == NULL) {               /* No address succeeded */
00123         ERR("Could not connect\n");
00124         freeaddrinfo(result);           /* No longer needed */
00125         return 1;
00126     }
00127 
00128     freeaddrinfo(result);           /* No longer needed */
00129 
00130 
00131     /* we have a connection */
00132     /* so we can setup a GIOChannel */
00133 
00134     g_keyb_client.chan = NULL ;
00135     g_keyb_client.chan = g_io_channel_unix_new( g_keyb_client.sfd);
00136     DBG_ASSERT( g_keyb_client.chan != NULL  ) ;
00137 
00138     g_io_channel_set_encoding(g_keyb_client.chan, NULL, NULL  );
00139 
00140     g_keyb_client.is_closed = FALSE;
00141 
00142     DBG_EXIT;
00143     return 0;
00144 }
00145 
00146 void erkeyb_client_show(void) 
00147 {
00148     DBG_ENTRY;
00149     
00150     if ( g_keyb_client.is_closed == TRUE )
00151     {
00152         erkeyb_client_init();
00153     }
00154 
00155     g_io_channel_write_chars(g_keyb_client.chan, "keyboard-show\n", -1, NULL, NULL );
00156     g_io_channel_flush(g_keyb_client.chan, NULL );
00157 
00158     DBG_EXIT;
00159 }
00160 
00161 void erkeyb_client_hide(void)
00162 {
00163     DBG_ENTRY;
00164 
00165     if ( g_keyb_client.is_closed == TRUE )
00166     {
00167         erkeyb_client_init();
00168     }
00169 
00170     g_io_channel_write_chars(g_keyb_client.chan, "keyboard-hide\n", -1, NULL, NULL );
00171     g_io_channel_flush(g_keyb_client.chan, NULL );
00172 
00173     DBG_EXIT;
00174 }
00175 
00176 void erkeyb_client_term(void) 
00177 {
00178     DBG_ENTRY;
00179 
00180     if ( g_keyb_client.is_closed == FALSE )
00181     {
00182         g_io_channel_write_chars(g_keyb_client.chan, "keyboard-hide\n", -1, NULL, NULL );
00183         g_io_channel_flush(g_keyb_client.chan, NULL );
00184 
00185         g_io_channel_shutdown(g_keyb_client.chan, FALSE, NULL);
00186         g_io_channel_unref(g_keyb_client.chan);
00187         g_keyb_client.chan = NULL ;
00188         close(g_keyb_client.sfd);
00189         g_keyb_client.sfd = -1;
00190         g_keyb_client.is_closed = TRUE;
00191     }
00192 
00193     DBG_EXIT;
00194 }
00195 
Generated by  doxygen 1.6.2-20100208