Added methods for Component Layer and MDI Z-Order information (see
[platform/core/uifw/at-spi2-atk.git] / test / keysynth-demo.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <gtk/gtk.h>
27 #include <gdk/gdkx.h>
28 #include "spi.h"
29
30 #define LABELMAXLEN 20
31 #define MIN_KEYCODE 9
32 #define CAPSLOCK_KEYCODE 66
33 #define SHIFT_L_KEYCODE 50
34 #define SHIFT_R_KEYCODE 62
35
36 /* these can be increased to access more keys */
37 #define MAX_ROWS 6 /* The last row only holds Quit and ShiftLatch, special-purpose keys */
38 #define MAX_COLUMNS 14
39
40 static AccessibleKeystrokeListener *key_listener;
41 static AccessibleKeystrokeListener *switch_listener;
42
43 static gboolean shift_latched = False;
44 static gboolean caps_lock = False;
45 static GtkButton **buttons[MAX_ROWS];
46
47 typedef enum {
48         SCAN_IDLE,
49         SCAN_LINES,
50         SCAN_LINES_DONE,
51         SCAN_KEYS,
52         SCAN_KEYS_DONE
53 } ScanTimerState;
54
55 typedef struct {
56         ScanTimerState timer_state;
57         gint scan_row;
58         gint scan_column;
59 } ScanState;
60
61 GdkColor *
62 button_default_bg_color (GtkButton *button)
63 {
64   static GdkColor bg_color;
65   static gboolean initialized = FALSE;
66   if (!initialized)
67   {
68     bg_color = gtk_widget_get_style (GTK_WIDGET (button))->bg[GTK_STATE_NORMAL];
69     initialized = TRUE;
70   }
71   return &bg_color;
72 }
73
74 GdkColor *
75 button_default_selected_color (GtkButton *button)
76 {
77   static GdkColor selected_color;
78   static gboolean initialized = FALSE;
79   if (!initialized)
80   {
81     selected_color = gtk_widget_get_style (GTK_WIDGET (button))->bg[GTK_STATE_SELECTED];
82     initialized = TRUE;
83   }
84   return &selected_color;
85 }
86
87 void
88 select_key (gint lineno, gint keyno)
89 {
90   /*
91    * Before we do this, we need to make sure we've saved the default normal bg.
92    * The button_default_bg_color() call caches this for us (as a side-effect).
93    * Probably we should do this a cleaner way...
94    */
95   button_default_bg_color (buttons [lineno][keyno]);    
96   gtk_widget_modify_bg (GTK_WIDGET (buttons [lineno][keyno]),
97                         GTK_STATE_NORMAL,
98                         button_default_selected_color (buttons [lineno][keyno]));
99 }
100
101 void
102 deselect_key (gint lineno, gint keyno)
103 {
104   gtk_widget_modify_bg (GTK_WIDGET (buttons [lineno][keyno]),
105                         GTK_STATE_NORMAL,
106                         button_default_bg_color (buttons [lineno][keyno]));
107 }
108
109 void
110 deselect_line (gint lineno)
111 {
112   int i;
113   int max_columns = MAX_COLUMNS;
114   if (lineno == MAX_ROWS-1) max_columns = 2;
115   for (i=0; i<max_columns; ++i)
116           deselect_key (lineno, i);
117 }
118
119 void
120 select_line (gint lineno)
121 {
122   int i;
123   int max_columns = MAX_COLUMNS;
124   if (lineno == MAX_ROWS-1) max_columns = 2;
125   for (i=0; i<max_columns; ++i)
126           select_key (lineno, i);
127 }
128
129
130 static ScanState*
131 scan_state ()
132 {
133   static ScanState state = {SCAN_IDLE, 0, 0};
134   return &state;
135 }
136
137 static gboolean
138 timeout_scan (gpointer data)
139 {
140   ScanState *state = (ScanState *) data;
141   state->timer_state = SCAN_IDLE;
142   deselect_key (state->scan_row, state->scan_column);
143   return FALSE;
144 }
145
146 static gboolean
147 increment_scan (gpointer data)
148 {
149   ScanState *state = (ScanState *) data;
150   int max_columns;
151   switch (state->timer_state)
152     {  
153       case SCAN_IDLE: 
154 /* happens if switch-break occurs before the timer fires, after SCAN_KEYS_DONE*/
155           return FALSE;
156       case SCAN_LINES:
157           deselect_line (state->scan_row);
158           state->scan_row = (++state->scan_row < MAX_ROWS) ? state->scan_row : 0;
159           select_line (state->scan_row);
160           g_print ("line %d\n", state->scan_row);
161           break;
162       case SCAN_KEYS:
163           deselect_key (state->scan_row, state->scan_column);
164           if (state->scan_row == MAX_ROWS-1) max_columns = 2;
165           else max_columns = MAX_COLUMNS; /* last row has only two keys */
166           state->scan_column = (++state->scan_column < max_columns) ? state->scan_column : 0;
167           select_key (state->scan_row, state->scan_column);
168           g_print ("row %d\n", state->scan_column);
169           break;
170       case SCAN_LINES_DONE:
171       case SCAN_KEYS_DONE:
172           return FALSE;
173       default:
174     }
175   return TRUE;
176 }
177
178 static void
179 scan_start (unsigned int timestamp)
180 {
181   ScanState *state = scan_state();
182   switch (state->timer_state)
183     {
184     case SCAN_IDLE:
185       state->timer_state = SCAN_LINES;
186       state->scan_column = 0;
187       state->scan_row = 0;
188       g_timeout_add_full (G_PRIORITY_HIGH_IDLE, 600, increment_scan, state, NULL);
189       select_line (state->scan_row);
190       break;
191     case SCAN_LINES_DONE:
192       state->timer_state = SCAN_KEYS;
193       g_timeout_add_full (G_PRIORITY_HIGH_IDLE, 600, increment_scan, state, NULL);
194       deselect_line (state->scan_row);
195       select_key (state->scan_row, state->scan_column);
196       break;
197     case SCAN_KEYS_DONE:
198       gtk_button_clicked (buttons[state->scan_row][state->scan_column]);
199       deselect_key (state->scan_row, state->scan_column);
200       state->timer_state = SCAN_IDLE;
201       break;
202     default:
203       g_print("unexpected state for 'scan start'\n");
204     }
205 }
206
207 static void
208 scan_stop (unsigned int timestamp)
209 {
210   /* find the element correspondin to this event's timestamp */
211   ScanState *state = scan_state();
212   switch (state->timer_state)
213     {
214     case SCAN_LINES:
215       state->timer_state = SCAN_LINES_DONE;
216       break;
217     case SCAN_KEYS:
218       state->timer_state = SCAN_KEYS_DONE;
219       g_timeout_add_full (G_PRIORITY_HIGH_IDLE, 1200, timeout_scan, state, NULL);
220       break;
221     case SCAN_IDLE:
222       break;
223     default:
224       g_print("unexpected state for 'scan stop'\n");
225     }
226 }
227
228 static void
229 label_buttons(gboolean shifted)
230 {
231   int i, j;
232   KeySym keysym;
233   KeyCode keycode = MIN_KEYCODE;
234   char label[LABELMAXLEN] = " ";
235   char *button_label;
236   char *keysymstring;
237   
238   for (i=0; i<MAX_ROWS-1; ++i) /* last row doesn't change */
239     {
240       for (j=0; j<MAX_COLUMNS; ++j, ++keycode)
241         {
242           keysym = (KeySym) XKeycodeToKeysym (GDK_DISPLAY(), keycode, shifted ? 1 : 0);
243           /* Note: these routines are not i18n-savvy,  we need to use XIM, other methods here */
244           if (keysym && g_ascii_isprint((int)keysym))
245             {
246               snprintf (label, 2, "%c", (int) keysym); 
247             }
248           else
249             {
250               keysymstring = XKeysymToString (keysym);
251               if (keysymstring)
252                 { 
253                   /* KP_ means keypad... we won't expose this difference */
254                   if (!strncmp (keysymstring, "KP_", 3))
255                        strncpy (label, (char *)(keysymstring+3), LABELMAXLEN);
256                   else strncpy (label, keysymstring, LABELMAXLEN);
257                 }
258               else *label = 0;
259             }
260           button_label =        
261           *label==' ' ? "   space   " : label;
262           gtk_button_set_label (buttons[i][j], button_label);    
263         }
264     }
265 }
266
267 static void
268 show_shift (GtkButton *button, gboolean *is_shifted)
269 {
270  label_buttons (*is_shifted ^ caps_lock);       
271 }
272
273 static void
274 toggle_shift_latch (GtkButton *button) 
275
276   shift_latched = !shift_latched;
277   if (buttons) label_buttons (caps_lock || shift_latched);
278 }
279
280 static void
281 keysynth_exit()
282 {
283   deregisterAccessibleKeystrokeListener (key_listener, SPI_KEYMASK_ALT );
284   deregisterAccessibleKeystrokeListener (switch_listener, SPI_KEYMASK_UNMODIFIED );
285   SPI_exit ();
286 }
287
288 static void
289 keysynth_realize (GtkWidget *widget)
290 {
291   XWMHints wm_hints;
292   Atom wm_window_protocols[2];
293   static gboolean initialized = FALSE;
294   
295   if (!initialized)
296     {
297       wm_window_protocols[0] = gdk_x11_get_xatom_by_name ("WM_DELETE_WINDOW");
298       wm_window_protocols[1] = gdk_x11_get_xatom_by_name ("_NET_WM_PING");
299     }
300   
301   wm_hints.flags = InputHint;
302   wm_hints.input = False;
303   
304   XSetWMHints (GDK_WINDOW_XDISPLAY (widget->window),
305                GDK_WINDOW_XWINDOW (widget->window), &wm_hints);
306   
307   XSetWMProtocols (GDK_WINDOW_XDISPLAY (widget->window),
308                    GDK_WINDOW_XWINDOW (widget->window), wm_window_protocols, 2);
309 }
310
311 static void
312 button_exit(GtkButton *notused, void *alsonotused)
313 {
314   keysynth_exit();
315 }
316
317 static SPIBoolean
318 is_command_key (AccessibleKeystroke *key)
319 {
320   switch (key->keyID)
321     {
322     case 'Q':
323     case 'q':
324             keysynth_exit(); 
325             return TRUE; /* not reached */
326     }
327   return FALSE;
328 }
329
330 static SPIBoolean
331 switch_callback (AccessibleKeystroke *key)
332 {
333   static gboolean is_down = FALSE;
334   if (key->type == SPI_KEY_RELEASED)
335     {
336       g_print ("spacebar up\n");
337       is_down = FALSE;
338       scan_stop (key->timestamp);
339     }
340   else 
341   if (!is_down)
342     {
343       g_print ("spacebar down\n");
344       is_down = TRUE;
345       scan_start (key->timestamp);
346     }
347   /* catch the first, ignore the rest (will repeat) until keyrelease */
348   return FALSE;
349 }
350
351 static void
352 synth_keycode (GtkButton *button, KeyCode *keycode)
353 {
354   static KeyCode shift_keycode = 0;
355   if (!shift_keycode) shift_keycode = XKeysymToKeycode(GDK_DISPLAY(), (KeySym) 0xFFE1);
356   /* Note: in a real onscreen keyboard shift keycode should not be hard-coded! */
357   if (*keycode)
358     {
359       if (*keycode == CAPSLOCK_KEYCODE)
360         {
361           caps_lock = !caps_lock;            
362           label_buttons (caps_lock || shift_latched);
363         }
364       if (shift_latched)
365               generateKeyEvent (shift_keycode, SPI_KEY_PRESS);
366       
367       generateKeyEvent ((long) *keycode, SPI_KEY_PRESSRELEASE);
368
369       if (shift_latched)
370         {
371           generateKeyEvent (shift_keycode, SPI_KEY_RELEASE);
372           toggle_shift_latch (button);
373         }
374     }
375 }
376
377 static void
378 create_vkbd()
379 {
380   GtkWidget *window, *container, *hbox;
381   int i, j;
382   KeyCode *keycodeptr, keycode = MIN_KEYCODE;
383   static gboolean true_val = True;
384   static gboolean false_val = False;
385
386   window = g_object_connect (gtk_widget_new (gtk_window_get_type (),
387                                              "user_data", NULL,
388                                              "can_focus", FALSE,
389                                              "type", GTK_WINDOW_TOPLEVEL,
390                                              "window-position", GTK_WIN_POS_CENTER,
391                                              "title", "test",
392                                              "allow_grow", FALSE,
393                                              "allow_shrink", FALSE,
394                                              "border_width", 10,
395                                              NULL),
396                              "signal::realize", keysynth_realize, NULL,
397                              "signal::destroy", keysynth_exit, NULL,
398                              NULL);
399   
400   container = gtk_widget_new (GTK_TYPE_VBOX,
401                               "GtkWidget::parent", window,
402                               "GtkWidget::visible", TRUE,
403                               NULL);
404   for (i=0; i<MAX_ROWS-1; ++i)
405     {
406       hbox = gtk_widget_new (gtk_hbox_get_type(),
407                              "GtkWidget::parent", container,
408                              "GtkWidget::visible", TRUE,
409                              NULL);
410       buttons[i] = g_new0 (GtkButton*, MAX_COLUMNS);
411       for (j=0; j<MAX_COLUMNS; ++j)
412         {
413           keycodeptr = (KeyCode *) g_new0 (KeyCode, 1);
414           *keycodeptr = keycode;
415           buttons[i][j] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
416                                                      "GtkWidget::parent", hbox,
417                                                      "GtkWidget::visible", TRUE,
418                                                      NULL),
419                                      "signal::clicked",
420                                      synth_keycode, keycodeptr,
421                                      NULL);
422           if (keycode == SHIFT_L_KEYCODE || keycode == SHIFT_R_KEYCODE)
423             {
424               g_object_connect (buttons[i][j], "signal::pressed", show_shift,
425                                 &true_val, NULL);  
426               g_object_connect (buttons[i][j], "signal::released", show_shift,
427                                 &false_val, NULL);  
428             }
429           ++keycode;
430         }
431     } 
432   hbox = gtk_widget_new (gtk_hbox_get_type(),
433                          "GtkWidget::parent", container,
434                          "GtkWidget::visible", TRUE,
435                          NULL);
436   buttons[i] = g_new0 (GtkButton*, 2);
437   buttons[i][0] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
438                                                              "GtkButton::label", "Quit",
439                                                              "GtkWidget::parent", hbox,
440                                                              "GtkWidget::visible", TRUE,
441                                                              NULL),
442                                              "signal::clicked",
443                                              button_exit, NULL,
444                                              NULL);
445   buttons[i][1] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
446                                                     "GtkButton::label", "ShiftLatch",
447                                                     "GtkWidget::parent", hbox,
448                                                     "GtkWidget::visible", TRUE,
449                                                     NULL),
450                                     "signal::clicked",
451                                     toggle_shift_latch, NULL,
452                                     NULL);
453   label_buttons (caps_lock || shift_latched);
454   gtk_widget_show (window);
455 }
456
457 int
458 main(int argc, char **argv)
459 {
460   AccessibleKeySet switch_set;
461   
462   if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
463     {
464       printf ("Usage: keysynth-demo\n");
465       exit (1);
466     }
467
468   gtk_init (&argc, &argv); /* must call, because this program uses GTK+ */
469
470   SPI_init ();
471
472   key_listener = createAccessibleKeystrokeListener (is_command_key);
473   /* will listen only to Alt-key combinations */
474   registerAccessibleKeystrokeListener (key_listener,
475                                        (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
476                                        SPI_KEYMASK_ALT,
477                                        (unsigned long) ( KeyPress | KeyRelease),
478                                        SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS);
479   create_vkbd ();  
480
481   /*
482    * Register a listener on an 'unused' key, to serve as a 'single switch'.
483    * On most Intel boxes there is at least one 'special' system key that does not
484    * have a non-zero keycode assigned in the Xserver, so we will intercept any keycode
485    * that is 'zero'.  Often these the are the "windows" or the "menu" keys.
486    */
487   switch_set.keysyms = g_new0 (unsigned long, 1);
488   switch_set.keycodes = g_new0 (unsigned short, 1);
489   switch_set.len = 1;
490   switch_set.keysyms[0] = (unsigned long) 0;
491   switch_set.keycodes[0] = (unsigned short) 0;
492   switch_listener = createAccessibleKeystrokeListener (switch_callback);
493   registerAccessibleKeystrokeListener (switch_listener,
494                                        &switch_set,
495                                        SPI_KEYMASK_UNMODIFIED,
496                                        (unsigned long) ( KeyPress | KeyRelease),
497                                        SPI_KEYLISTENER_CANCONSUME);
498   
499   SPI_event_main (TRUE);
500
501   return 0;
502 }