aboutsummaryrefslogtreecommitdiffstats
path: root/examples/matchbox-keyboard-gtk-embed.c
blob: 66f6a66b1ee2fd6b599cb03e9b2b4088069dc6e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* SPDX-License-Identifier: LGPL-2.1 */

#include <gtk/gtk.h>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

enum {
  KB_TYPE_NUMERIC,
  KB_TYPE_NUMERIC_EXTENDED,
  KB_TYPE_QWERTY,
  KB_TYPE_SYMBOL,
  KB_TYPE_LOCALE,
};

static GtkWidget *kb_widgets[4];
static GtkWidget *kb_window = NULL;
static GtkWidget *kb_bbox = NULL;

static gint dock_gravity = 0; /*  0 = No (default), 1 = bottom, 2 = left, 3 = top, 4 = right */
static gboolean show_expanded = FALSE;

void
cleanup_children(int s)
{
  kill(-getpid(), 15);  /* kill every one in our process group  */
  exit(0);
}

void 
install_signal_handlers(void)
{
  signal (SIGCHLD, SIG_IGN);  /* kernel can deal with zombies  */
  signal (SIGINT, cleanup_children);
  signal (SIGQUIT, cleanup_children);
  signal (SIGTERM, cleanup_children);
}

unsigned long
launch_keyboard (gint  type,
                 const char *locale)
{
  int    i = 0, fd[2];
  int    stdout_pipe[2];
  int    stdin_pipe[2];
  char   buf[256], c;
  size_t n;

  unsigned long result = 0;

  printf("Launching keyboard\n");

  pipe (stdout_pipe);
  pipe (stdin_pipe);

  switch (fork ())
    {
    case 0:
      {
	/* Close the Child process' STDOUT */
	close(1);
	dup(stdout_pipe[1]);
	close(stdout_pipe[0]);
	close(stdout_pipe[1]);
	
        switch (type)
          {
          case KB_TYPE_SYMBOL:
            execlp ("/bin/sh", "sh", "-c", "matchbox-keyboard --xid --fontfamily dejavu:sans --fontptsize 10 --fontvariant mono symbol", NULL);
            break;
          case KB_TYPE_QWERTY:
            execlp ("/bin/sh", "sh", "-c", "matchbox-keyboard --xid --fontfamily dejavu:sans --fontptsize 10 --fontvariant mono", NULL);
            break;
          case KB_TYPE_NUMERIC_EXTENDED:
            execlp ("/bin/sh", "sh", "-c", "matchbox-keyboard --xid --fontfamily dejavu:sans --fontptsize 10 --fontvariant mono numpad-extended", NULL);
            break;
          case KB_TYPE_LOCALE:
            {
              GString *string = g_string_new (NULL);
              
              g_string_printf (string, "matchbox-keyboard --xid --fontfamily dejavu:sans --fontptsize 10 --fontvariant mono %s", locale);

              execlp ("/bin/sh", "sh", "-c", string->str, NULL);

              g_string_free (string, TRUE);
            } break;
          default:
            execlp ("/bin/sh", "sh", "-c", "matchbox-keyboard --xid --fontfamily dejavu:sans --fontptsize 12 --fontvariant mono:bold numpad-small", NULL);
            break;
          }
      }
    case -1:
      perror ("### Failed to launch 'matchbox-keyboard --xid', is it installed? ### ");
      exit(1);
    }

  /* Parent */

  /* Close the write end of STDOUT */
  close(stdout_pipe[1]);

  /* FIXME: This could be a little safer... */
  do 
    {
      n = read(stdout_pipe[0], &c, 1);
      if (n == 0 || c == '\n')
	break;
      buf[i++] = c;
    } 
  while (i < 256);

  buf[i] = '\0';
  result = atol (buf);

  close(stdout_pipe[0]);

  return result;
}


void widget_destroy( GtkWidget *widget,
                     gpointer   data )
{
   gtk_widget_destroy(widget);
   gtk_main_quit ();
}

static GtkWidget*
add_keyboard_widget_to_body (GtkWidget *body,
                             unsigned long xid)
{
  GtkWidget *socket_box = gtk_hbox_new (TRUE, 1);
  GtkWidget *socket = gtk_socket_new ();
  GdkWindow *gdkwin;

  gtk_box_pack_start (GTK_BOX (socket_box), socket, TRUE, TRUE, 1);
  gtk_box_pack_start (GTK_BOX (body), socket_box, TRUE, TRUE, 1);

  gtk_socket_add_id(GTK_SOCKET(socket), xid);

  gdkwin = gtk_socket_get_plug_window (GTK_SOCKET (socket));
  gdk_window_show (gdkwin);

  return socket_box;
}

/* -1 hides all. */
static void
show_kb_widget_by_index (gint active_index)
{
  gint ix;
  for (ix = 0; ix < G_N_ELEMENTS (kb_widgets); ix++)
    {
      if (active_index != ix)
        {
          gtk_widget_hide (kb_widgets[ix]);
        }
      else
        {
          gtk_widget_show (kb_widgets[ix]);
        }
    }
}

static void
switch_clicked (GtkButton *button,
                gpointer   data)
{
  gint index = GPOINTER_TO_INT (data);

  g_return_if_fail (GTK_IS_WINDOW (kb_window));

  show_kb_widget_by_index (index);
  
  gtk_window_resize (GTK_WINDOW (kb_window), 1, 1);
}

static void
_usage ()
{
  g_print ("Usage: ngi [OPTIONS]\n");
  g_print ("Options:\n");
  g_print ("  -h, --help                          show this help message\n");
  g_print ("\n");
  g_print ("  -e, -expanded                       show the expanded/switchable keyboard, rather than the small numeric entry.\n");
  g_print ("\n");
  g_print ("  -d, --dock-gravity <gravity>        set docking parameter. 0 = No (default), 1 = bottom, 2 = left, 3 = top, 4 = right\n");
}

static gboolean
parse_args (int argc, char **argv)
{
  gint i;
  
  /* Initial params. */
  dock_gravity = 0;

  for (i = 1; i < argc; ++i)
    {
      if (strcmp (argv[i], "-h") == 0 || strcmp (argv[i], "--help") == 0)
        {
          _usage ();
          return TRUE; /* We need to exit because all they wanted was the help */
        }
      else if (strcmp (argv[i], "--dock-gravity") == 0 || strcmp (argv[i], "-d") == 0)
        {
          if (i < argc)
            {
              i++;
              dock_gravity = strtol (argv[i], NULL, 0);
            }
        }
      else if (strcmp (argv[i], "--expanded") == 0 || strcmp (argv[i], "-e") == 0)
        {
          show_expanded = TRUE;
        }
    }
  
  /* Everything went well, the program can continue. */
  return FALSE;
}

static gboolean
add_keyboard_widget (GtkWidget   *body,
                     GtkWidget   *bbox, 
                     gint         kb_type,
                     const gchar *locale,
                     const gchar *next_button_name)
{
  gint kb_xid;
  GList *children;
  gint index;
  GtkWidget *button;

  g_return_val_if_fail (GTK_IS_CONTAINER (body), FALSE);
  g_return_val_if_fail (GTK_IS_BOX (bbox), FALSE);

  kb_xid = launch_keyboard (kb_type, locale);
  if (!kb_xid)
    {
      return FALSE;
    }
  
  /* Create the widget and add it to the container. */
  children = gtk_container_get_children (GTK_CONTAINER (body));
  index = g_list_length (children) - 1;
  kb_widgets[index] = add_keyboard_widget_to_body (body, kb_xid);

  button = gtk_button_new_with_label (next_button_name);
  gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 1);
  g_signal_connect (G_OBJECT (button), "clicked",
                    G_CALLBACK (switch_clicked), 
                    GINT_TO_POINTER (index));

  g_list_free (children);

  return TRUE;
}

static gboolean
get_locale_strings (gchar **locale, gchar **locale_btn)
{
  gboolean return_value = TRUE;
  PangoLanguage *lang = pango_language_get_default ();  
  
  *locale = g_strdup (pango_language_to_string (lang));
  *locale_btn = g_strdup ("AБB");
  
  return return_value;
}

int main(int argc, char **argv)
{
  GtkWidget *body, *textview;
  unsigned long kb_xid;
  GdkWindow *gdkwin;
  PangoContext *pangocontext;
  char *fontdescription;
  GtkStyle *style;
  gint i = 0;

  /* If they just wanted help info we need to exit */
  if (parse_args (argc, argv))
    return 0;

  gtk_init (&argc, &argv);

  install_signal_handlers();

  /* Window */
  kb_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (kb_window), "destroy",
                    G_CALLBACK (widget_destroy), NULL);
  
  body = gtk_vbox_new (FALSE, 1);
  gtk_container_add (GTK_CONTAINER (kb_window), body);

  /* Define the behavior, size and look of the window */
  gtk_window_set_resizable (GTK_WINDOW (kb_window), FALSE);
  //gtk_window_set_default_size (GTK_WINDOW (kb_window), 125, 125);
  gtk_container_set_border_width (GTK_CONTAINER (kb_window), 3);
  
  textview = gtk_text_view_new ();
  gtk_box_pack_start (GTK_BOX (body), textview, TRUE, TRUE, 2);

  /* Get the font details that came through the rc files. */
  pangocontext = gtk_widget_get_pango_context (kb_window);
  fontdescription = pango_font_description_to_string (pango_context_get_font_description (pangocontext));
  style = gtk_widget_get_style (kb_window);
  //g_printf ("******** %s :::: %s ********\n", fontdescription, pango_font_description_to_string (style->font_desc));

  /* Add the buttons to their own box. */
  kb_bbox = gtk_hbox_new (TRUE, 1);
  gtk_box_pack_end (GTK_BOX (body), kb_bbox, FALSE, FALSE, 1);

  if (show_expanded)
    {
      gchar *locale;
      gchar *locale_btn;
  
      /* Add the local keyboard. */
      if (get_locale_strings (&locale, &locale_btn))
        {
          //g_printf ("******** %s %s *************\n", locale, locale_btn);
          if (!add_keyboard_widget (body, kb_bbox, KB_TYPE_LOCALE, locale, locale_btn))
            {
              perror ("### 'locale keyboard', failed to return valid window ID. ### ");
              exit(-1);
            }
        }

      /* Add the qwerty keyboard. */
      if (!add_keyboard_widget (body, kb_bbox, KB_TYPE_QWERTY, NULL, "ABC"))
        {
          perror ("### 'qwerty keyboard', failed to return valid window ID. ### ");
          exit(-1);
        }

      /* Add the extended numeric keyboard. */
      if (!add_keyboard_widget (body, kb_bbox, KB_TYPE_NUMERIC_EXTENDED, NULL, "123"))
        {
          perror ("### 'extended numeric keyboard', failed to return valid window ID. ### ");
          exit(-1);
        }
  
      /* Add the symbol keyboard. */
      if (!add_keyboard_widget (body, kb_bbox, KB_TYPE_SYMBOL, NULL, "#+="))
        {
          perror ("### 'qwerty keyboard', failed to return valid window ID. ### ");
          exit(-1);
        }

      if (NULL != locale)
        {
          g_free (locale);
        }
      if (NULL != locale_btn)
        {
          g_free (locale_btn);
        }
    }
  else
    {
      /* Add only the numeric keyboard. */
      gint kb_xid = launch_keyboard (KB_TYPE_NUMERIC, NULL);
      if (!kb_xid)
        {
          perror ("### 'numpad keyboard', failed to return valid window ID. ### ");
          exit(-1);
        }
  
      /* Create the widget and add it to the container. */
      kb_widgets[0] = add_keyboard_widget_to_body (body, kb_xid);      
    }

  /* FIXME: handle "plug-added" & "plug-removed" signals for socket */

  gtk_widget_show_all (kb_window);

  /* hide all but the numeric */
  if (show_expanded)
    show_kb_widget_by_index (0);

  gtk_main ();

  return 0;
}