Added methods for Component Layer and MDI Z-Order information (see
[platform/core/uifw/at-spi2-atk.git] / test / simple-at.c
index c06b527..a7baca9 100644 (file)
  */
 
 #include <stdlib.h>
+#include <ctype.h>
+#include <unistd.h>
 #include <sys/socket.h>
 #include <sys/un.h>
-#include "spi.h"
+#include <cspi/spi.h>
+#include "../util/mag_client.h"
 
-static void report_focus_event (void *fp);
-static void report_button_press (void *fp);
-static boolean report_key_event (void *fp);
+static void report_focus_event    (AccessibleEvent *event);
+static void report_button_press   (AccessibleEvent *event);
+static void check_property_change (AccessibleEvent *event);
+static SPIBoolean report_command_key_event  (AccessibleKeystroke *stroke);
+static SPIBoolean report_ordinary_key_event (AccessibleKeystroke *stroke);
 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 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)
@@ -46,9 +57,7 @@ main(int argc, char **argv)
   int n_apps;
   Accessible *desktop;
   Accessible *application;
-  AccessibleEventListener *focus_listener;
-  AccessibleEventListener *button_listener;
-  KeystrokeListener *key_listener;
+  char *s;
 
   if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
   {
@@ -59,33 +68,60 @@ main(int argc, char **argv)
 
   SPI_init();
 
-  focus_listener = createEventListener (report_focus_event);
-  button_listener = createEventListener (report_button_press);
-
+  focus_listener = createAccessibleEventListener (report_focus_event);
+  property_listener = createAccessibleEventListener (check_property_change); 
+  button_listener = createAccessibleEventListener (report_button_press);
   registerGlobalEventListener (focus_listener, "focus:");
+  registerGlobalEventListener (property_listener, "object:property-change:accessible-selection"); 
   registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
-
   n_desktops = getDesktopCount ();
 
   for (i=0; i<n_desktops; ++i)
     {
       desktop = getDesktop (i);
-      fprintf (stderr, "desktop %d name: %s\n", i, Accessible_getName (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 = Accessible_getChildAtIndex (desktop, j);
-          fprintf (stderr, "app %d name: %s\n", j, Accessible_getName (application));
+         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);
-     registerKeystrokeListener(key_listener, KEYMASK_SHIFT); */
+  command_key_listener = createAccessibleKeystrokeListener (report_command_key_event);
+  ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event);
+  
+  /* 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(FALSE);
+  SPI_event_main(TRUE);
 }
 
 static void
@@ -106,59 +142,163 @@ get_environment_vars()
 }
 
 void
-report_focus_event (void *p)
+report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
 {
-  AccessibleEvent *ev = (AccessibleEvent *) p;
-  fprintf (stderr, "%s event from %s\n", ev->type,
-           Accessible_getName (&ev->source));
-  
+  char *s;
+  int len;
   if (use_festival)
     {
     if (festival_chatty)           
       {
-        _festival_say (Accessible_getRole (&ev->source), "voice_don_diphone", TRUE);
+        _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
       }
-      _festival_say (Accessible_getName (&ev->source), "voice_kal_diphone", festival_chatty==FALSE);
+      fprintf (stderr, "getting Name\n");
+      s = Accessible_getName (obj);
+      _festival_say (s, "voice_kal_diphone",
+                    shutup_previous_speech || festival_chatty);
+      SPI_freeString (s);
     }
   
-  if (Accessible_isComponent (&ev->source))
+  if (Accessible_isComponent (obj))
     {
       long x, y, width, height;
-      AccessibleComponent *component = Accessible_getComponent (&ev->source);
+      AccessibleComponent *component = Accessible_getComponent (obj);
       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
-                                      COORD_TYPE_SCREEN);
+                                      SPI_COORD_TYPE_SCREEN);
       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
                x, y, x+width, y+height);
       if (use_magnifier) {
-             magnifier_set_roi (x, y, width, height);        
+             magnifier_set_roi ((short) 0, x, y, width, height);
       }
     }
-  if (Accessible_isText(&ev->source))
-    /* if this is a text object, speak the first sentence. */
+  /* if this is a text object, speak the first sentence. */
+
+  if (Accessible_isText(obj))
+
   {
-     AccessibleText *text_interface = Accessible_getText (&ev->source);
+     AccessibleText *text_interface;
      long start_offset, end_offset;
-     char *first_sentence = "";
+     char *first_sentence = "empty";
+     text_interface = Accessible_getText (obj);
      first_sentence = AccessibleText_getTextAtOffset (
-            text_interface, (long) 0, TEXT_BOUNDARY_WORD_END,
-            &start_offset, &end_offset); 
-     _festival_say(first_sentence, "voice_don_diphone", festival_chatty==FALSE);
+              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_button_press (void *p)
+report_focus_event (AccessibleEvent *event)
+{
+  char *s = Accessible_getName (event->source);
+  fprintf (stderr, "%s event from %s\n", event->type, s);
+  SPI_freeString (s);
+  report_focussed_accessible (event->source, TRUE);
+}
+
+void
+report_button_press (AccessibleEvent *event)
+{
+  char *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 (AccessibleEvent *event)
+{
+  AccessibleSelection *selection = Accessible_getSelection (event->source);
+  int n_selections;
+  int i;
+  char *s;
+  if (selection)
+  {
+    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)
+      {
+        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()
+{
+  deregisterGlobalEventListenerAll (focus_listener);
+  deregisterGlobalEventListenerAll (property_listener);
+  deregisterGlobalEventListenerAll (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 ();
+}
+
+static SPIBoolean
+is_command_key (AccessibleKeystroke *key)
 {
-  AccessibleEvent *ev = (AccessibleEvent *) p;
-  fprintf (stderr, "%s event from %s\n", ev->type,
-           Accessible_getName (&ev->source));
+  switch (key->keyID)
+    {
+    case 'Q':
+    case 'q':
+           simple_at_exit(); 
+           return TRUE; /* not reached */
+    case 'M':
+    case 'm':
+           use_magnifier = ! use_magnifier;
+           return TRUE;
+    case 'F':
+    case 'f':
+           use_festival = ! use_festival;
+           return TRUE;
+    default:
+           return FALSE;
+    }
 }
 
-static boolean
-report_key_event (void *p)
+static SPIBoolean
+report_command_key_event (AccessibleKeystroke *key)
+{
+  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 SPIBoolean
+report_ordinary_key_event (AccessibleKeystroke *key)
 {
-  KeyStroke *key = (KeyStroke *) p;
-  fprintf (stderr, ".");
+  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;
 }
 
@@ -186,7 +326,7 @@ 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;