2001-12-08 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / test / simple-at.c
index deeb3f3..1e66ef1 100644 (file)
  */
 
 #include <stdlib.h>
+#include <ctype.h>
+#include <unistd.h>
 #include <sys/socket.h>
 #include <sys/un.h>
-#include "spi.h"
-
-static void report_focus_event (void *fp);
-static void report_button_press (void *fp);
-static boolean report_key_event (void *fp);
-static void check_property_change (void *fp);
+#include <cspi/spi.h>
+#include "../util/mag_client.h"
+#include "../cspi/spi-private.h" /* A hack for now */
+
+static void report_focus_event    (AccessibleEvent *event, void *user_data);
+static void report_button_press   (AccessibleEvent *event, void *user_data);
+static void check_property_change (AccessibleEvent *event, void *user_data);
+static SPIBoolean report_command_key_event  (AccessibleKeystroke *stroke, void *user_data);
+static SPIBoolean report_ordinary_key_event (AccessibleKeystroke *stroke, void *user_data);
 static void get_environment_vars (void);
 
 static int _festival_init ();
-static void _festival_say (const char *text, const char *voice, boolean shutup);
+static void _festival_say (const char *text, const char *voice, SPIBoolean shutup);
 static void _festival_write (const char *buff, int fd);
 
-static boolean use_magnifier = FALSE;
-static boolean use_festival = FALSE;
-static boolean festival_chatty = FALSE;
+static SPIBoolean use_magnifier = FALSE;
+static SPIBoolean use_festival = FALSE;
+static SPIBoolean festival_chatty = FALSE;
 
-static SpiAccessibleEventListener *focus_listener;
-static SpiAccessibleEventListener *property_listener;
-static SpiAccessibleEventListener *button_listener;
-static KeystrokeListener *key_listener;
+static AccessibleEventListener *focus_listener;
+static AccessibleEventListener *property_listener;
+static AccessibleEventListener *button_listener;
+static AccessibleKeystrokeListener *command_key_listener;
+static AccessibleKeystrokeListener *ordinary_key_listener;
 
 int
-main(int argc, char **argv)
+main (int argc, char **argv)
 {
   int i, j;
   int n_desktops;
   int n_apps;
-  SpiAccessible *desktop;
-  SpiAccessible *application;
+  Accessible *desktop;
+  Accessible *application;
+  char *s;
 
-  if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
+  if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
   {
     printf ("Usage: simple-at\n");
     printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
-    exit(0);
+    exit (0);
   }
 
-  SPI_init();
+  SPI_init ();
 
-  focus_listener = createEventListener (report_focus_event);
-  property_listener = createEventListener (check_property_change); 
-  button_listener = createEventListener (report_button_press);
+  focus_listener = createAccessibleEventListener (report_focus_event, NULL);
+  property_listener = createAccessibleEventListener (check_property_change, NULL); 
+  button_listener = createAccessibleEventListener (report_button_press, NULL);
   registerGlobalEventListener (focus_listener, "focus:");
   registerGlobalEventListener (property_listener, "object:property-change:accessible-selection"); 
   registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
@@ -73,67 +80,101 @@ main(int argc, char **argv)
   for (i=0; i<n_desktops; ++i)
     {
       desktop = getDesktop (i);
-      fprintf (stderr, "desktop %d name: %s\n", i, SpiAccessible_getName (desktop));
-      n_apps = SpiAccessible_getChildCount (desktop);
+      s = Accessible_getName (desktop);
+      fprintf (stderr, "desktop %d name: %s\n", i, s);
+      SPI_freeString (s);
+      n_apps = Accessible_getChildCount (desktop);
       for (j=0; j<n_apps; ++j)
         {
-          application = SpiAccessible_getChildAtIndex (desktop, j);
-          fprintf (stderr, "app %d name: %s\n", j, SpiAccessible_getName (application));
-         SpiAccessible_unref (application);
+          application = Accessible_getChildAtIndex (desktop, j);
+         s = Accessible_getName (application);
+          fprintf (stderr, "app %d name: %s\n", j, s);
+          SPI_freeString (s);
+          Accessible_unref (application);
         }
+      Accessible_unref (desktop);
     }
 
   /* prepare the keyboard snoopers */
-  key_listener = createKeystrokeListener(report_key_event);
-  /* will listen only to Alt-key combinations */
-  registerKeystrokeListener(key_listener,
-                           (KeySet *) ALL_KEYS,
-                           KEYMASK_ALT,
-                           (unsigned long) ( KeyPress | KeyRelease),
-                           KEYSPI_LISTENER_CANCONSUME);
-
-  get_environment_vars();
-
-  SPI_event_main(TRUE);
+  command_key_listener = createAccessibleKeystrokeListener (report_command_key_event, NULL);
+  ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
+  
+  /* will listen only to Alt-key combinations, and only to KeyPress events */
+  registerAccessibleKeystrokeListener(command_key_listener,
+                                     (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
+                                     SPI_KEYMASK_ALT,
+                                     (unsigned long) ( KeyPress ),
+                                     SPI_KEYLISTENER_ALL_WINDOWS);
+  
+  /* will listen only to unshifted key events, both press and release */
+  registerAccessibleKeystrokeListener(ordinary_key_listener,
+                                     (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
+                                     SPI_KEYMASK_UNMODIFIED,
+                                     (unsigned long) ( KeyPress | KeyRelease),
+                                     SPI_KEYLISTENER_NOSYNC);
+                                     
+  /* will listen only to shifted key events, both press and release */
+  registerAccessibleKeystrokeListener(ordinary_key_listener,
+                                     (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
+                                     SPI_KEYMASK_SHIFT,
+                                     (unsigned long) ( KeyPress | KeyRelease),
+                                     SPI_KEYLISTENER_NOSYNC);
+
+  get_environment_vars ();
+
+  SPI_event_main ();
+
+  setenv ("AT_BRIDGE_SHUTDOWN", "1", TRUE);
+
+  return SPI_exit ();
 }
 
 static void
-get_environment_vars()
+get_environment_vars (void)
 {
-  if (getenv ("FESTIVAL"))
-  {
-    use_festival = TRUE;
-    if (getenv ("FESTIVAL_CHATTY"))
+  if (g_getenv ("FESTIVAL"))
     {
-      festival_chatty = TRUE;
+      fprintf (stderr, "Using festival\n");
+      use_festival = TRUE;
+      if (g_getenv ("FESTIVAL_CHATTY"))
+        {
+          festival_chatty = TRUE;
+       }
     }
-  }
-  if (getenv("MAGNIFIER"))
-  {
-    use_magnifier = TRUE;
-  }  
+  if (g_getenv ("MAGNIFIER"))
+    {
+      fprintf (stderr, "Using magnifier\n");
+      use_magnifier = TRUE;
+    }  
 }
 
 void
-report_focussed_accessible (SpiAccessible *obj, boolean shutup_previous_speech)
+report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
 {
+  char *s;
+  int len;
+
+  g_warning ("Report focused !");
+
   if (use_festival)
     {
     if (festival_chatty)           
       {
-        _festival_say (SpiAccessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
+        _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
       }
       fprintf (stderr, "getting Name\n");
-      _festival_say (SpiAccessible_getName (obj), "voice_kal_diphone",
+      s = Accessible_getName (obj);
+      _festival_say (s, "voice_kal_diphone",
                     shutup_previous_speech || festival_chatty);
+      SPI_freeString (s);
     }
   
-  if (SpiAccessible_isComponent (obj))
+  if (Accessible_isComponent (obj))
     {
       long x, y, width, height;
-      SpiAccessibleComponent *component = SpiAccessible_getComponent (obj);
-      SpiAccessibleComponent_getExtents (component, &x, &y, &width, &height,
-                                      COORD_TYPE_SCREEN);
+      AccessibleComponent *component = Accessible_getComponent (obj);
+      AccessibleComponent_getExtents (component, &x, &y, &width, &height,
+                                      SPI_COORD_TYPE_SCREEN);
       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
                x, y, x+width, y+height);
       if (use_magnifier) {
@@ -141,74 +182,112 @@ report_focussed_accessible (SpiAccessible *obj, boolean shutup_previous_speech)
       }
     }
   /* if this is a text object, speak the first sentence. */
-  if (SpiAccessible_isText(obj))
+
+  if (Accessible_isText(obj))
+
   {
-     SpiAccessibleText *spi_text_interface;
+     AccessibleText *text_interface;
      long start_offset, end_offset;
      char *first_sentence = "empty";
-     spi_text_interface = SpiAccessible_getText (obj);
-     fprintf (stderr, "isText...%p %p\n", spi_text_interface, (void *)*spi_text_interface);
-     first_sentence = SpiAccessibleText_getTextAtOffset (
-              spi_text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
-     if (first_sentence) _festival_say(first_sentence, "voice_don_diphone", FALSE);
-     fprintf (stderr, "done reporting on focussed object\n");
+     text_interface = Accessible_getText (obj);
+     first_sentence = AccessibleText_getTextAtOffset (
+              text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
+     if (first_sentence)
+       {
+        _festival_say(first_sentence, "voice_don_diphone", FALSE);
+        SPI_freeString (first_sentence);
+       }
+     len = AccessibleText_getCharacterCount (text_interface);
+     s = AccessibleText_getText (text_interface, 0, len);
+     fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
   }
 }
 
 void
-report_focus_event (void *p)
+report_focus_event (AccessibleEvent *event, void *user_data)
 {
-  SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
-  fprintf (stderr, "%s event from %s\n", ev->type,
-           SpiAccessible_getName (&ev->source));
-  report_focussed_accessible (&ev->source, TRUE);
+  char *s;
+
+  g_warning ("report focus event");
+
+  g_return_if_fail (event->source != NULL);
+
+  s = Accessible_getName (event->source);
+  if (cspi_warn_ev (cspi_ev (), "Foobar"))
+    {
+      fprintf (stderr, "%s event from %s\n", event->type, s);
+      SPI_freeString (s);
+      report_focussed_accessible (event->source, TRUE);
+    }
+  Accessible_getParent (event->source);
 }
 
 void
-report_button_press (void *p)
+report_button_press (AccessibleEvent *event, void *user_data)
 {
-  SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
-  fprintf (stderr, "%s event from %s\n", ev->type,
-           SpiAccessible_getName (&ev->source));
-}
+  char *s;
+
+  g_return_if_fail (event->source != NULL);
 
+  s = Accessible_getName (event->source);
+
+  fprintf (stderr, "%s event from %s\n", event->type, s);
+  SPI_freeString (s);
+  s = Accessible_getDescription (event->source);
+  fprintf (stderr, "Object description %s\n", s);
+  SPI_freeString (s);
+}
 
 void
-check_property_change (void *p)
+check_property_change (AccessibleEvent *event, void *user_data)
 {
-  SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
-  SpiAccessibleSelection *selection = SpiAccessible_getSelection (&ev->source);
+  AccessibleSelection *selection = Accessible_getSelection (event->source);
   int n_selections;
   int i;
+  char *s;
   if (selection)
   {
-    n_selections = (int) SpiAccessibleSelection_getNSelectedChildren (selection);
-    fprintf (stderr, "(Property) %s event from %s, %d selected children\n", ev->type,
-           SpiAccessible_getName (&ev->source), n_selections);
+    n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
+    s = Accessible_getName (event->source);
+    fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
+            event->type, s, n_selections);
+    SPI_freeString (s);
   /* for now, speak entire selection set */
     for (i=0; i<n_selections; ++i)
-    {
-         SpiAccessible *obj = SpiAccessibleSelection_getSelectedChild (selection, (long) i);
-         g_return_if_fail (obj);
-          fprintf (stderr, "Child %d, name=%s\n", i, SpiAccessible_getName (obj));
-         report_focussed_accessible (obj, i==0);
+      {
+        Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
+       g_return_if_fail (obj);
+       s = Accessible_getName (obj);
+       fprintf (stderr, "Child %d, name=%s\n", i, s);
+       SPI_freeString (s);
+       report_focussed_accessible (obj, i==0);
     }
   }
 }
 
 static void
-simple_at_exit()
+simple_at_exit ()
 {
   deregisterGlobalEventListenerAll (focus_listener);
+  AccessibleEventListener_unref    (focus_listener);
+
   deregisterGlobalEventListenerAll (property_listener);
+  AccessibleEventListener_unref    (property_listener);
+
   deregisterGlobalEventListenerAll (button_listener);
-  deregisterKeystrokeListener (key_listener, KEYMASK_ALT );
+  AccessibleEventListener_unref    (button_listener);
+
+  deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT);
+  deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED);
+  deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT);
+  AccessibleKeystrokeListener_unref (command_key_listener);
+  AccessibleKeystrokeListener_unref (ordinary_key_listener);
   
-  SPI_exit ();
+  SPI_event_quit ();
 }
 
-static boolean
-is_command_key (KeyStroke *key)
+static SPIBoolean
+is_command_key (AccessibleKeystroke *key)
 {
   switch (key->keyID)
     {
@@ -229,19 +308,31 @@ is_command_key (KeyStroke *key)
     }
 }
 
-static boolean
-report_key_event (void *p)
+static SPIBoolean
+report_command_key_event (AccessibleKeystroke *key, void *user_data)
 {
-  KeyStroke *key = (KeyStroke *) p;
-  fprintf(stderr, "KeyEvent %s%c (keycode %d)\n",
-         (key->modifiers & KEYMASK_ALT)?"Alt-":"",
-         ((key->modifiers & KEYMASK_SHIFT)^(key->modifiers & KEYMASK_SHIFTLOCK))?
+  fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
+         (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
+         ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
          (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
          (int) key->keycode);
   return is_command_key (key);
 }
 
-static int _festival_init ()
+
+static SPIBoolean
+report_ordinary_key_event (AccessibleKeystroke *key, void *user_data)
+{
+  fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
+          (long) key->keyID,
+          (unsigned int) key->modifiers,
+          (int) key->keycode,
+          (long int) key->timestamp);
+  return FALSE;
+}
+
+static int
+_festival_init ()
 {
   int fd;
   struct sockaddr_in name;
@@ -265,7 +356,8 @@ static int _festival_init ()
   return fd;
 }
 
-static void _festival_say (const char *text, const char *voice, boolean shutup)
+static void 
+_festival_say (const char *text, const char *voice, SPIBoolean shutup)
 {
   static int fd = 0;
   gchar *quoted;
@@ -310,7 +402,8 @@ static void _festival_say (const char *text, const char *voice, boolean shutup)
   g_free(quoted);
 }
 
-static void _festival_write (const gchar *command_string, int fd)
+static void
+_festival_write (const gchar *command_string, int fd)
 {
   fprintf(stderr, command_string);
   if (fd < 0) {