*** empty log message ***
[platform/core/uifw/at-spi2-atk.git] / test / keysynth-demo.c
index 31df81d..9e7bb6a 100644 (file)
@@ -2,7 +2,8 @@
  * AT-SPI - Assistive Technology Service Provider Interface
  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
  *
- * Copyright 2001 Sun Microsystems Inc.
+ * Copyright 2001, 2002 Sun Microsystems Inc.,
+ * Copyright 2001, 2002 Ximian, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * Boston, MA 02111-1307, USA.
  */
 
+#include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkx.h>
-#include "spi.h"
+#include <cspi/spi.h>
 
 #define LABELMAXLEN 20
 #define MIN_KEYCODE 9
@@ -38,8 +41,8 @@
 static AccessibleKeystrokeListener *key_listener;
 static AccessibleKeystrokeListener *switch_listener;
 
-static boolean shift_latched = False;
-static boolean caps_lock = False;
+static SPIBoolean shift_latched = False;
+static SPIBoolean caps_lock = False;
 static GtkButton **buttons[MAX_ROWS];
 
 typedef enum {
@@ -168,7 +171,6 @@ increment_scan (gpointer data)
       case SCAN_LINES_DONE:
       case SCAN_KEYS_DONE:
          return FALSE;
-      default:
     }
   return TRUE;
 }
@@ -224,7 +226,7 @@ scan_stop (unsigned int timestamp)
 }
 
 static void
-label_buttons(boolean shifted)
+label_buttons(SPIBoolean shifted)
 {
   int i, j;
   KeySym keysym;
@@ -241,7 +243,7 @@ label_buttons(boolean shifted)
           /* Note: these routines are not i18n-savvy,  we need to use XIM, other methods here */
          if (keysym && g_ascii_isprint((int)keysym))
            {
-             snprintf (label, 2, "%c", (int) keysym); 
+             g_snprintf (label, 2, "%c", (int) keysym); 
            }
          else
            {
@@ -263,7 +265,7 @@ label_buttons(boolean shifted)
 }
 
 static void
-show_shift (GtkButton *button, boolean *is_shifted)
+show_shift (GtkButton *button, SPIBoolean *is_shifted)
 {
  label_buttons (*is_shifted ^ caps_lock);      
 }
@@ -276,11 +278,15 @@ toggle_shift_latch (GtkButton *button)
 }
 
 static void
-keysynth_exit()
+keysynth_exit (void)
 {
-  deregisterAccessibleKeystrokeListener (key_listener, SPI_KEYMASK_ALT );
-  deregisterAccessibleKeystrokeListener (switch_listener, SPI_KEYMASK_UNMODIFIED );
-  SPI_exit ();
+  SPI_deregisterAccessibleKeystrokeListener (key_listener, SPI_KEYMASK_ALT);
+  AccessibleKeystrokeListener_unref         (key_listener);
+
+  SPI_deregisterAccessibleKeystrokeListener (switch_listener, SPI_KEYMASK_UNMODIFIED);
+  AccessibleKeystrokeListener_unref         (switch_listener);
+
+  SPI_event_quit ();
 }
 
 static void
@@ -307,39 +313,39 @@ keysynth_realize (GtkWidget *widget)
 }
 
 static void
-button_exit(GtkButton *notused, void *alsonotused)
+button_exit (GtkButton *notused, void *alsonotused)
 {
-  keysynth_exit();
+  keysynth_exit ();
 }
 
-static boolean
-is_command_key (void *p)
+static SPIBoolean
+is_command_key (const AccessibleKeystroke *key, void *user_data)
 {
-  AccessibleKeyStroke *key = (AccessibleKeyStroke *)p;
   switch (key->keyID)
     {
     case 'Q':
     case 'q':
-           keysynth_exit(); 
+           keysynth_exit (); 
            return TRUE; /* not reached */
     }
+  return FALSE;
 }
 
-static boolean
-switch_callback (void *p)
+static SPIBoolean
+switch_callback (const AccessibleKeystroke *key, void *user_data)
 {
-  AccessibleKeyStroke *key = (AccessibleKeyStroke *)p;
-  static is_down = FALSE;
-  if (key->type == Accessibility_KEY_RELEASED)
+  static SPIBoolean is_down = FALSE;
+
+  if (key->type == SPI_KEY_RELEASED)
     {
-      g_print ("spacebar up\n");
+      g_print ("switch up\n");
       is_down = FALSE;
       scan_stop (key->timestamp);
     }
   else 
   if (!is_down)
     {
-      g_print ("spacebar down\n");
+      g_print ("switch down\n");
       is_down = TRUE;
       scan_start (key->timestamp);
     }
@@ -353,6 +359,7 @@ synth_keycode (GtkButton *button, KeyCode *keycode)
   static KeyCode shift_keycode = 0;
   if (!shift_keycode) shift_keycode = XKeysymToKeycode(GDK_DISPLAY(), (KeySym) 0xFFE1);
   /* Note: in a real onscreen keyboard shift keycode should not be hard-coded! */
+  
   if (*keycode)
     {
       if (*keycode == CAPSLOCK_KEYCODE)
@@ -361,13 +368,14 @@ synth_keycode (GtkButton *button, KeyCode *keycode)
           label_buttons (caps_lock || shift_latched);
         }
       if (shift_latched)
-             generateKeyEvent (shift_keycode, SPI_KEY_PRESS);
-      
-      generateKeyEvent ((long) *keycode, SPI_KEY_PRESSRELEASE);
+             SPI_generateKeyboardEvent (shift_keycode, NULL, SPI_KEY_PRESS);
+
+      g_print ("generating key %d\n", (int) *keycode);
+      SPI_generateKeyboardEvent ((long) *keycode, NULL, SPI_KEY_PRESSRELEASE);
 
       if (shift_latched)
         {
-         generateKeyEvent (shift_keycode, SPI_KEY_RELEASE);
+         SPI_generateKeyboardEvent (shift_keycode, NULL, SPI_KEY_RELEASE);
          toggle_shift_latch (button);
        }
     }
@@ -376,11 +384,11 @@ synth_keycode (GtkButton *button, KeyCode *keycode)
 static void
 create_vkbd()
 {
-  GtkWidget *window, *button, *container, *hbox;
+  GtkWidget *window, *container, *hbox;
   int i, j;
   KeyCode *keycodeptr, keycode = MIN_KEYCODE;
-  static boolean true_val = True;
-  static boolean false_val = False;
+  static SPIBoolean true_val = True;
+  static SPIBoolean false_val = False;
 
   window = g_object_connect (gtk_widget_new (gtk_window_get_type (),
                                             "user_data", NULL,
@@ -454,46 +462,50 @@ create_vkbd()
 }
 
 int
-main(int argc, char **argv)
+main (int argc, char **argv)
 {
   AccessibleKeySet switch_set;
   
-  if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
-  {
-    printf ("Usage: keysynth-demo\n");
-    exit(0);
-  }
+  if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
+    {
+      printf ("Usage: keysynth-demo\n");
+      exit (1);
+    }
 
   gtk_init (&argc, &argv); /* must call, because this program uses GTK+ */
 
-  SPI_init();
+  SPI_init ();
 
-  key_listener = createAccessibleKeystrokeListener(is_command_key);
+  key_listener = SPI_createAccessibleKeystrokeListener (is_command_key, NULL);
   /* will listen only to Alt-key combinations */
-  registerAccessibleKeystrokeListener(key_listener,
-                                     (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
-                                     SPI_KEYMASK_ALT,
-                                     (unsigned long) ( KeyPress | KeyRelease),
-                                     SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS);
-  create_vkbd();  
+  SPI_registerAccessibleKeystrokeListener (key_listener,
+                                          (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
+                                          SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL,
+                                          (unsigned long) ( KeyPress | KeyRelease),
+                                          SPI_KEYLISTENER_CANCONSUME | SPI_KEYLISTENER_ALL_WINDOWS);
+  create_vkbd ();  
 
   /*
    * Register a listener on an 'unused' key, to serve as a 'single switch'.
    * On most Intel boxes there is at least one 'special' system key that does not
    * have a non-zero keycode assigned in the Xserver, so we will intercept any keycode
-   * that is 'zero'.  Often these the "windows" key or the "menu" key.
+   * that is 'zero'.  Often these the are the "windows" or the "menu" keys.
    */
   switch_set.keysyms = g_new0 (unsigned long, 1);
   switch_set.keycodes = g_new0 (unsigned short, 1);
+  switch_set.keystrings = g_new0 (char *, 1);
   switch_set.len = 1;
   switch_set.keysyms[0] = (unsigned long) 0;
   switch_set.keycodes[0] = (unsigned short) 0;
-  switch_listener = createAccessibleKeystrokeListener(switch_callback);
-  registerAccessibleKeystrokeListener(switch_listener,
-                                     &switch_set,
-                                     SPI_KEYMASK_UNMODIFIED,
-                                     (unsigned long) ( KeyPress | KeyRelease),
-                                     SPI_KEYLISTENER_CANCONSUME);
+  switch_set.keystrings[0] = "";
+  switch_listener = SPI_createAccessibleKeystrokeListener (switch_callback, NULL);
+  SPI_registerAccessibleKeystrokeListener (switch_listener,
+                                          &switch_set,
+                                          SPI_KEYMASK_UNMODIFIED,
+                                          (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
+                                          SPI_KEYLISTENER_NOSYNC);
   
-  SPI_event_main(TRUE);
+  SPI_event_main ();
+
+  return SPI_exit ();
 }