2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001 Sun Microsystems Inc.
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.
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.
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.
30 #define LABELMAXLEN 20
32 #define CAPSLOCK_KEYCODE 66
33 #define SHIFT_L_KEYCODE 50
34 #define SHIFT_R_KEYCODE 62
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
40 static AccessibleKeystrokeListener *key_listener;
41 static AccessibleKeystrokeListener *switch_listener;
43 static SPIBoolean shift_latched = False;
44 static SPIBoolean caps_lock = False;
45 static GtkButton **buttons[MAX_ROWS];
56 ScanTimerState timer_state;
62 button_default_bg_color (GtkButton *button)
64 static GdkColor bg_color;
65 static gboolean initialized = FALSE;
68 bg_color = gtk_widget_get_style (GTK_WIDGET (button))->bg[GTK_STATE_NORMAL];
75 button_default_selected_color (GtkButton *button)
77 static GdkColor selected_color;
78 static gboolean initialized = FALSE;
81 selected_color = gtk_widget_get_style (GTK_WIDGET (button))->bg[GTK_STATE_SELECTED];
84 return &selected_color;
88 select_key (gint lineno, gint keyno)
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...
95 button_default_bg_color (buttons [lineno][keyno]);
96 gtk_widget_modify_bg (GTK_WIDGET (buttons [lineno][keyno]),
98 button_default_selected_color (buttons [lineno][keyno]));
102 deselect_key (gint lineno, gint keyno)
104 gtk_widget_modify_bg (GTK_WIDGET (buttons [lineno][keyno]),
106 button_default_bg_color (buttons [lineno][keyno]));
110 deselect_line (gint lineno)
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);
120 select_line (gint lineno)
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);
133 static ScanState state = {SCAN_IDLE, 0, 0};
138 timeout_scan (gpointer data)
140 ScanState *state = (ScanState *) data;
141 state->timer_state = SCAN_IDLE;
142 deselect_key (state->scan_row, state->scan_column);
147 increment_scan (gpointer data)
149 ScanState *state = (ScanState *) data;
151 switch (state->timer_state)
154 /* happens if switch-break occurs before the timer fires, after SCAN_KEYS_DONE*/
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);
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);
170 case SCAN_LINES_DONE:
179 scan_start (unsigned int timestamp)
181 ScanState *state = scan_state();
182 switch (state->timer_state)
185 state->timer_state = SCAN_LINES;
186 state->scan_column = 0;
188 g_timeout_add_full (G_PRIORITY_HIGH_IDLE, 600, increment_scan, state, NULL);
189 select_line (state->scan_row);
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);
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;
203 g_print("unexpected state for 'scan start'\n");
208 scan_stop (unsigned int timestamp)
210 /* find the element correspondin to this event's timestamp */
211 ScanState *state = scan_state();
212 switch (state->timer_state)
215 state->timer_state = SCAN_LINES_DONE;
218 state->timer_state = SCAN_KEYS_DONE;
219 g_timeout_add_full (G_PRIORITY_HIGH_IDLE, 1200, timeout_scan, state, NULL);
224 g_print("unexpected state for 'scan stop'\n");
229 label_buttons(SPIBoolean shifted)
233 KeyCode keycode = MIN_KEYCODE;
234 char label[LABELMAXLEN] = " ";
238 for (i=0; i<MAX_ROWS-1; ++i) /* last row doesn't change */
240 for (j=0; j<MAX_COLUMNS; ++j, ++keycode)
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))
246 snprintf (label, 2, "%c", (int) keysym);
250 keysymstring = XKeysymToString (keysym);
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);
261 *label==' ' ? " space " : label;
262 gtk_button_set_label (buttons[i][j], button_label);
268 show_shift (GtkButton *button, SPIBoolean *is_shifted)
270 label_buttons (*is_shifted ^ caps_lock);
274 toggle_shift_latch (GtkButton *button)
276 shift_latched = !shift_latched;
277 if (buttons) label_buttons (caps_lock || shift_latched);
283 SPI_deregisterAccessibleKeystrokeListener (key_listener, SPI_KEYMASK_ALT);
284 AccessibleKeystrokeListener_unref (key_listener);
286 SPI_deregisterAccessibleKeystrokeListener (switch_listener, SPI_KEYMASK_UNMODIFIED);
287 AccessibleKeystrokeListener_unref (switch_listener);
293 keysynth_realize (GtkWidget *widget)
296 Atom wm_window_protocols[2];
297 static gboolean initialized = FALSE;
301 wm_window_protocols[0] = gdk_x11_get_xatom_by_name ("WM_DELETE_WINDOW");
302 wm_window_protocols[1] = gdk_x11_get_xatom_by_name ("_NET_WM_PING");
305 wm_hints.flags = InputHint;
306 wm_hints.input = False;
308 XSetWMHints (GDK_WINDOW_XDISPLAY (widget->window),
309 GDK_WINDOW_XWINDOW (widget->window), &wm_hints);
311 XSetWMProtocols (GDK_WINDOW_XDISPLAY (widget->window),
312 GDK_WINDOW_XWINDOW (widget->window), wm_window_protocols, 2);
316 button_exit (GtkButton *notused, void *alsonotused)
322 is_command_key (AccessibleKeystroke *key, void *user_data)
329 return TRUE; /* not reached */
335 switch_callback (AccessibleKeystroke *key, void *user_data)
337 static SPIBoolean is_down = FALSE;
339 if (key->type == SPI_KEY_RELEASED)
341 g_print ("spacebar up\n");
343 scan_stop (key->timestamp);
348 g_print ("spacebar down\n");
350 scan_start (key->timestamp);
352 /* catch the first, ignore the rest (will repeat) until keyrelease */
357 synth_keycode (GtkButton *button, KeyCode *keycode)
359 static KeyCode shift_keycode = 0;
360 if (!shift_keycode) shift_keycode = XKeysymToKeycode(GDK_DISPLAY(), (KeySym) 0xFFE1);
361 /* Note: in a real onscreen keyboard shift keycode should not be hard-coded! */
364 if (*keycode == CAPSLOCK_KEYCODE)
366 caps_lock = !caps_lock;
367 label_buttons (caps_lock || shift_latched);
370 SPI_generateKeyEvent (shift_keycode, SPI_KEY_PRESS);
372 SPI_generateKeyEvent ((long) *keycode, SPI_KEY_PRESSRELEASE);
376 SPI_generateKeyEvent (shift_keycode, SPI_KEY_RELEASE);
377 toggle_shift_latch (button);
385 GtkWidget *window, *container, *hbox;
387 KeyCode *keycodeptr, keycode = MIN_KEYCODE;
388 static SPIBoolean true_val = True;
389 static SPIBoolean false_val = False;
391 window = g_object_connect (gtk_widget_new (gtk_window_get_type (),
394 "type", GTK_WINDOW_TOPLEVEL,
395 "window-position", GTK_WIN_POS_CENTER,
398 "allow_shrink", FALSE,
401 "signal::realize", keysynth_realize, NULL,
402 "signal::destroy", keysynth_exit, NULL,
405 container = gtk_widget_new (GTK_TYPE_VBOX,
406 "GtkWidget::parent", window,
407 "GtkWidget::visible", TRUE,
409 for (i=0; i<MAX_ROWS-1; ++i)
411 hbox = gtk_widget_new (gtk_hbox_get_type(),
412 "GtkWidget::parent", container,
413 "GtkWidget::visible", TRUE,
415 buttons[i] = g_new0 (GtkButton*, MAX_COLUMNS);
416 for (j=0; j<MAX_COLUMNS; ++j)
418 keycodeptr = (KeyCode *) g_new0 (KeyCode, 1);
419 *keycodeptr = keycode;
420 buttons[i][j] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
421 "GtkWidget::parent", hbox,
422 "GtkWidget::visible", TRUE,
425 synth_keycode, keycodeptr,
427 if (keycode == SHIFT_L_KEYCODE || keycode == SHIFT_R_KEYCODE)
429 g_object_connect (buttons[i][j], "signal::pressed", show_shift,
431 g_object_connect (buttons[i][j], "signal::released", show_shift,
437 hbox = gtk_widget_new (gtk_hbox_get_type(),
438 "GtkWidget::parent", container,
439 "GtkWidget::visible", TRUE,
441 buttons[i] = g_new0 (GtkButton*, 2);
442 buttons[i][0] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
443 "GtkButton::label", "Quit",
444 "GtkWidget::parent", hbox,
445 "GtkWidget::visible", TRUE,
450 buttons[i][1] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
451 "GtkButton::label", "ShiftLatch",
452 "GtkWidget::parent", hbox,
453 "GtkWidget::visible", TRUE,
456 toggle_shift_latch, NULL,
458 label_buttons (caps_lock || shift_latched);
459 gtk_widget_show (window);
463 main (int argc, char **argv)
465 AccessibleKeySet switch_set;
467 if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
469 printf ("Usage: keysynth-demo\n");
473 gtk_init (&argc, &argv); /* must call, because this program uses GTK+ */
477 key_listener = SPI_createAccessibleKeystrokeListener (is_command_key, NULL);
478 /* will listen only to Alt-key combinations */
479 SPI_registerAccessibleKeystrokeListener (key_listener,
480 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
482 (unsigned long) ( KeyPress | KeyRelease),
483 SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS);
487 * Register a listener on an 'unused' key, to serve as a 'single switch'.
488 * On most Intel boxes there is at least one 'special' system key that does not
489 * have a non-zero keycode assigned in the Xserver, so we will intercept any keycode
490 * that is 'zero'. Often these the are the "windows" or the "menu" keys.
492 switch_set.keysyms = g_new0 (unsigned long, 1);
493 switch_set.keycodes = g_new0 (unsigned short, 1);
495 switch_set.keysyms[0] = (unsigned long) 0;
496 switch_set.keycodes[0] = (unsigned short) 0;
497 switch_listener = SPI_createAccessibleKeystrokeListener (switch_callback, NULL);
498 SPI_registerAccessibleKeystrokeListener (switch_listener,
500 SPI_KEYMASK_UNMODIFIED,
501 (unsigned long) ( KeyPress | KeyRelease),
502 SPI_KEYLISTENER_CANCONSUME);