erkeybd.c

Go to the documentation of this file.
00001 /*
00002  * File Name: erkeybd.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 include files, between < >
00032 #include <glib.h>
00033 #include <gtk/gtk.h>
00034 /* local include files */
00035 #include "ergtk-keyb.h"
00036 
00037 /* logging */
00038 #include "logging.h"
00039 
00040 #include <errno.h>
00041 #include <string.h>
00042 
00043 #include <sys/types.h>
00044 #include <sys/socket.h>
00045 #include <netdb.h>
00046 #include <netinet/in.h>
00047 #include <arpa/inet.h>
00048 
00049 #include <unistd.h>
00050 #include <stdlib.h>
00051 #include <stdio.h>
00052 #include <strings.h>
00053 #include <errno.h>
00054 #include <signal.h>
00055 
00056 //----------------------------------------------------------------------------
00057 //  MACRO definitions
00058 //----------------------------------------------------------------------------
00059 
00060   
00061 //----------------------------------------------------------------------------
00062 // Type Declarations
00063 //----------------------------------------------------------------------------
00064 
00065 typedef struct _KeybUI KeybUI;
00066 
00067 struct _KeybUI {
00068     gboolean is_visible;
00069     GtkWidget* window;
00070     GtkWidget* alignment;
00071     GtkWidget* keyb;
00072     GIOChannel* sock_io_chan;
00073 };
00074 
00075 
00076 //----------------------------------------------------------------------------
00077 // Static Variables
00078 //----------------------------------------------------------------------------
00079 
00080 static KeybUI g_keybui;
00081 
00082 //----------------------------------------------------------------------------
00083 // Function Prototypes
00084 //----------------------------------------------------------------------------
00085 static void on_sigterm (int signo);
00086 static gboolean my_io_callback(GIOChannel* source, GIOCondition condition, gpointer data );
00087 
00088 static gint realize_keyboard(KeybUI* ui );
00089 static gint keyboard_show(KeybUI* ui );
00090 static gint keyboard_hide(KeybUI* ui );
00091 static gint destroy_keyboard(KeybUI* ui);
00092 
00093 static gboolean finish_app_cb(GtkWidget* widget, gpointer data );
00094 
00095 //----------------------------------------------------------------------------
00096 // Function Implementations
00097 //----------------------------------------------------------------------------
00098 
00099 static void on_sigterm (int signo)
00100 {
00101     DBG_ENTRY;
00102 
00103     DBG("erkeybd: Caught SIGNAL (%d), exiting...  my pid [%d]", getpid(), signo);
00104 
00105     gtk_main_quit();
00106 
00107     DBG_EXIT;
00108 }
00109 
00110 static gboolean my_io_callback(GIOChannel* source, GIOCondition condition, gpointer data )
00111 {
00112     DBG_ENTRY;
00113 
00114     char* tmp = NULL ; 
00115 
00116     GString* buf = g_string_new(NULL);
00117 
00118     g_io_channel_read_line_string( source, buf, NULL, NULL);
00119     tmp = (char*) buf->str; 
00120 
00121     DBG(" Received -- %s", tmp );
00122 
00123     if (strcasecmp(tmp, "Keyboard-Show\n") == 0)
00124     {
00125         DBG("Showing Keyboard!\n");
00126         keyboard_show(&g_keybui);
00127     }
00128     else if (strcasecmp(tmp, "Keyboard-Hide\n") == 0)
00129     {
00130         DBG("Hiding Keyboard!\n");
00131         keyboard_hide(&g_keybui);
00132     }
00133     else if (strcasecmp(tmp, "Keyboard-Destroy\n") == 0)
00134     {
00135         DBG("Destroying Keyboard!\n");
00136         destroy_keyboard(&g_keybui);
00137     }
00138 
00139 
00140     tmp = NULL ;
00141     tmp = (char*) g_string_free(buf, TRUE);
00142     DBG_ASSERT( tmp == NULL );
00143 
00144     DBG_EXIT;
00145 
00146     return TRUE;
00147 }
00148 
00149 
00150 int main ( int argc, char** argv )
00151 {
00152     DBG_ENTRY;
00153 
00154     struct sigaction on_term;
00155     int        sockfd = -1;
00156     int        error = 0;
00157     struct     sockaddr_in server_addr;
00158 
00159     gchar      buffer[512];
00160     memset(buffer, 0x0, sizeof(buffer));
00161 
00162     memset(&on_term, 0x00, sizeof(on_term));
00163     on_term.sa_handler = on_sigterm;
00164     sigaction(SIGTERM, &on_term, NULL);
00165 #if LOGGING_ON
00166     sigaction(SIGINT,  &on_term, NULL);
00167 #endif
00168 
00169     sockfd = socket(PF_INET, SOCK_DGRAM, 0);
00170     server_addr.sin_family = AF_INET;
00171     server_addr.sin_port = htons(60666);
00172     server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
00173     memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
00174 
00175     error = bind(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr));
00176 
00177     if (error)
00178     {
00179         perror("Error invoking bind");
00180         return 0;
00181     }
00182 
00183     gtk_init(&argc, &argv);
00184 
00185     g_keybui.sock_io_chan = NULL ; 
00186     g_keybui.sock_io_chan = g_io_channel_unix_new(sockfd) ;
00187     DBG_ASSERT( g_keybui.sock_io_chan != NULL  ) ;
00188 
00189     realize_keyboard(&g_keybui);
00190 
00191     g_io_channel_set_encoding(g_keybui.sock_io_chan, NULL, NULL  );
00192     g_io_add_watch( g_keybui.sock_io_chan, G_IO_IN, my_io_callback, NULL );
00193 
00194     gtk_main();
00195 
00196     close(sockfd);
00197 
00198     DBG_EXIT;
00199     return 0;
00200 }
00201 
00202 
00203 static gint realize_keyboard(KeybUI* ui)
00204 {
00205     DBG_ENTRY;
00206 
00207     DBG_ASSERT( ui != NULL ) ;
00208 
00209     ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00210     
00211     gtk_signal_connect(
00212             GTK_OBJECT(ui->window),
00213             "destroy", 
00214             GTK_SIGNAL_FUNC(gtk_widget_destroyed), 
00215             (gpointer)&ui->window
00216             );
00217     
00218 
00219     ui->alignment = gtk_alignment_new(0.5, 0.0, 0.0, 0.0);
00220     gtk_signal_connect(
00221             GTK_OBJECT(ui->alignment),
00222             "destroy", 
00223             GTK_SIGNAL_FUNC(gtk_widget_destroyed), 
00224             (gpointer)&ui->alignment
00225             );
00226 
00227     // ui->vbox = gtk_vbox_new(3,FALSE);
00228     ui->keyb = ergtk_keyb_new();
00229     gtk_signal_connect(
00230             GTK_OBJECT(ui->keyb),
00231             "destroy", 
00232             GTK_SIGNAL_FUNC(gtk_widget_destroyed), 
00233             (gpointer)&ui->keyb
00234             );
00235     g_signal_connect(
00236             G_OBJECT(ui->keyb), 
00237             "destroy", 
00238             G_CALLBACK(finish_app_cb), 
00239             (gpointer) &g_keybui 
00240             );
00241 
00242     gtk_container_add(GTK_CONTAINER(ui->alignment), ui->keyb);
00243     gtk_widget_show(ui->keyb);
00244 
00245     gtk_container_add(GTK_CONTAINER(ui->window), ui->alignment);
00246     gtk_widget_show(ui->alignment);
00247 
00248     gtk_window_set_title(GTK_WINDOW(ui->window), "erkeyb");
00249     gtk_container_set_border_width(GTK_CONTAINER(ui->window), 10);
00250     gtk_window_set_resizable(GTK_WINDOW(ui->window),FALSE);
00251 
00252     // use hint DOCK so it is hidden for fullscreen windows
00253     gtk_window_set_type_hint(GTK_WINDOW(ui->window), GDK_WINDOW_TYPE_HINT_DOCK);
00254     gtk_window_set_decorated(GTK_WINDOW(ui->window), FALSE);
00255     gtk_window_set_accept_focus(GTK_WINDOW(ui->window), FALSE);
00256 
00257     // resize and move to dock at the botton of the screen
00258     gtk_window_move(GTK_WINDOW(ui->window), 0, gdk_screen_height());
00259     gtk_window_set_resizable(GTK_WINDOW(ui->window), FALSE);
00260 
00261     ui->is_visible = FALSE;
00262 
00263     DBG_EXIT;
00264     return 0;
00265 }
00266 
00267 static gint keyboard_show(KeybUI* ui)
00268 {
00269     DBG_ENTRY;
00270     DBG_ASSERT( ui != NULL ) ;
00271     DBG_ASSERT( ui->window != NULL ) ;
00272 
00273     ergtk_keyb_reset_keymap(ERGTK_KEYB(ui->keyb));
00274 
00275     if ( ui->is_visible == FALSE  )
00276     {
00277         gtk_widget_show_all(ui->window);
00278         ui->is_visible = TRUE;
00279     }
00280     else {
00281         DBG("Keyboard is already visible!\n");
00282     }
00283 
00284     DBG_EXIT;
00285     return 0;
00286 }
00287 
00288 static gint keyboard_hide(KeybUI* ui)
00289 {
00290     DBG_ENTRY;
00291     DBG_ASSERT( ui != NULL ) ;
00292     DBG_ASSERT( ui->window != NULL ) ;
00293 
00294     if ( ui->is_visible == TRUE  )
00295     {
00296         gtk_widget_hide_all(ui->window);
00297         ui->is_visible = FALSE;
00298     }
00299     else {
00300         DBG("Keyboard is already hidden!\n");
00301     }
00302 
00303     DBG_EXIT;
00304     return 0;
00305 }
00306 
00307 static gint destroy_keyboard(KeybUI* ui)
00308 {
00309     DBG_ENTRY;
00310     DBG_ASSERT( ui != NULL ) ;
00311     DBG_ASSERT(ui->window != NULL ) ;
00312     
00313     gtk_widget_hide_all(ui->window);
00314     ui->is_visible = FALSE;
00315     gtk_widget_destroy(ui->window);
00316 
00317     DBG_EXIT;
00318     return 0;
00319 }
00320 
00321 static gboolean finish_app_cb(GtkWidget* widget, gpointer data )
00322 {
00323     DBG_ENTRY;
00324     KeybUI* k = (KeybUI*) data;
00325 
00326     DBG_ASSERT( k->window     == NULL ) ;
00327     DBG_ASSERT( k->keyb       == NULL ) ;
00328     DBG_ASSERT( k->alignment  == NULL ) ;
00329     DBG_ASSERT( k->is_visible == FALSE ) ;
00330 
00331     /* remove the watch from the main event loop and close the GIOChannel */
00332     g_io_channel_shutdown(k->sock_io_chan, FALSE, NULL);
00333     g_io_channel_unref(k->sock_io_chan);
00334     k->sock_io_chan = NULL ; 
00335 
00336     gtk_main_quit();
00337     DBG_EXIT;
00338     return FALSE;
00339 }
Generated by  doxygen 1.6.2-20100208