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 deregisterAccessibleKeystrokeListener (key_listener, SPI_KEYMASK_ALT );
284 deregisterAccessibleKeystrokeListener (switch_listener, SPI_KEYMASK_UNMODIFIED );
289 keysynth_realize (GtkWidget *widget)
292 Atom wm_window_protocols[2];
293 static gboolean initialized = FALSE;
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");
301 wm_hints.flags = InputHint;
302 wm_hints.input = False;
304 XSetWMHints (GDK_WINDOW_XDISPLAY (widget->window),
305 GDK_WINDOW_XWINDOW (widget->window), &wm_hints);
307 XSetWMProtocols (GDK_WINDOW_XDISPLAY (widget->window),
308 GDK_WINDOW_XWINDOW (widget->window), wm_window_protocols, 2);
312 button_exit(GtkButton *notused, void *alsonotused)
318 is_command_key (AccessibleKeystroke *key, void *user_data)
325 return TRUE; /* not reached */
331 switch_callback (AccessibleKeystroke *key, void *user_data)
333 static SPIBoolean is_down = FALSE;
335 if (key->type == SPI_KEY_RELEASED)
337 g_print ("spacebar up\n");
339 scan_stop (key->timestamp);
344 g_print ("spacebar down\n");
346 scan_start (key->timestamp);
348 /* catch the first, ignore the rest (will repeat) until keyrelease */
353 synth_keycode (GtkButton *button, KeyCode *keycode)
355 static KeyCode shift_keycode = 0;
356 if (!shift_keycode) shift_keycode = XKeysymToKeycode(GDK_DISPLAY(), (KeySym) 0xFFE1);
357 /* Note: in a real onscreen keyboard shift keycode should not be hard-coded! */
360 if (*keycode == CAPSLOCK_KEYCODE)
362 caps_lock = !caps_lock;
363 label_buttons (caps_lock || shift_latched);
366 generateKeyEvent (shift_keycode, SPI_KEY_PRESS);
368 generateKeyEvent ((long) *keycode, SPI_KEY_PRESSRELEASE);
372 generateKeyEvent (shift_keycode, SPI_KEY_RELEASE);
373 toggle_shift_latch (button);
381 GtkWidget *window, *container, *hbox;
383 KeyCode *keycodeptr, keycode = MIN_KEYCODE;
384 static SPIBoolean true_val = True;
385 static SPIBoolean false_val = False;
387 window = g_object_connect (gtk_widget_new (gtk_window_get_type (),
390 "type", GTK_WINDOW_TOPLEVEL,
391 "window-position", GTK_WIN_POS_CENTER,
394 "allow_shrink", FALSE,
397 "signal::realize", keysynth_realize, NULL,
398 "signal::destroy", keysynth_exit, NULL,
401 container = gtk_widget_new (GTK_TYPE_VBOX,
402 "GtkWidget::parent", window,
403 "GtkWidget::visible", TRUE,
405 for (i=0; i<MAX_ROWS-1; ++i)
407 hbox = gtk_widget_new (gtk_hbox_get_type(),
408 "GtkWidget::parent", container,
409 "GtkWidget::visible", TRUE,
411 buttons[i] = g_new0 (GtkButton*, MAX_COLUMNS);
412 for (j=0; j<MAX_COLUMNS; ++j)
414 keycodeptr = (KeyCode *) g_new0 (KeyCode, 1);
415 *keycodeptr = keycode;
416 buttons[i][j] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
417 "GtkWidget::parent", hbox,
418 "GtkWidget::visible", TRUE,
421 synth_keycode, keycodeptr,
423 if (keycode == SHIFT_L_KEYCODE || keycode == SHIFT_R_KEYCODE)
425 g_object_connect (buttons[i][j], "signal::pressed", show_shift,
427 g_object_connect (buttons[i][j], "signal::released", show_shift,
433 hbox = gtk_widget_new (gtk_hbox_get_type(),
434 "GtkWidget::parent", container,
435 "GtkWidget::visible", TRUE,
437 buttons[i] = g_new0 (GtkButton*, 2);
438 buttons[i][0] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
439 "GtkButton::label", "Quit",
440 "GtkWidget::parent", hbox,
441 "GtkWidget::visible", TRUE,
446 buttons[i][1] = g_object_connect (gtk_widget_new (gtk_button_get_type (),
447 "GtkButton::label", "ShiftLatch",
448 "GtkWidget::parent", hbox,
449 "GtkWidget::visible", TRUE,
452 toggle_shift_latch, NULL,
454 label_buttons (caps_lock || shift_latched);
455 gtk_widget_show (window);
459 main(int argc, char **argv)
461 AccessibleKeySet switch_set;
463 if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
465 printf ("Usage: keysynth-demo\n");
469 gtk_init (&argc, &argv); /* must call, because this program uses GTK+ */
473 key_listener = createAccessibleKeystrokeListener (is_command_key, NULL);
474 /* will listen only to Alt-key combinations */
475 registerAccessibleKeystrokeListener (key_listener,
476 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
478 (unsigned long) ( KeyPress | KeyRelease),
479 SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS);
483 * Register a listener on an 'unused' key, to serve as a 'single switch'.
484 * On most Intel boxes there is at least one 'special' system key that does not
485 * have a non-zero keycode assigned in the Xserver, so we will intercept any keycode
486 * that is 'zero'. Often these the are the "windows" or the "menu" keys.
488 switch_set.keysyms = g_new0 (unsigned long, 1);
489 switch_set.keycodes = g_new0 (unsigned short, 1);
491 switch_set.keysyms[0] = (unsigned long) 0;
492 switch_set.keycodes[0] = (unsigned short) 0;
493 switch_listener = createAccessibleKeystrokeListener (switch_callback, NULL);
494 registerAccessibleKeystrokeListener (switch_listener,
496 SPI_KEYMASK_UNMODIFIED,
497 (unsigned long) ( KeyPress | KeyRelease),
498 SPI_KEYLISTENER_CANCONSUME);