erkeyb-client.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00041 #include <glib.h>
00042 #include <gtk/gtk.h>
00043
00044
00045 #include "config.h"
00046 #include "erkeyb-client.h"
00047
00048
00049 #include "logging.h"
00050
00051
00052
00053
00054
00055 #define HOST "127.0.0.1"
00056 #define PORT "60666"
00057
00058
00059
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
00073
00074
00075 static keyb_client g_keyb_client;
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 int erkeyb_client_init(void)
00088 {
00089 DBG_ENTRY;
00090 struct addrinfo *result, *rp;
00091 int s;
00092
00093
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;
00099 g_keyb_client.hints.ai_socktype = SOCK_DGRAM;
00100 g_keyb_client.hints.ai_flags = 0;
00101 g_keyb_client.hints.ai_protocol = 0;
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;
00118
00119 close(g_keyb_client.sfd);
00120 }
00121
00122 if (rp == NULL) {
00123 ERR("Could not connect\n");
00124 freeaddrinfo(result);
00125 return 1;
00126 }
00127
00128 freeaddrinfo(result);
00129
00130
00131
00132
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