2001-11-13 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / test / simple-at.c
index 2a8fc24..deeb3f3 100644 (file)
 #include <sys/un.h>
 #include "spi.h"
 
-void report_focus_event (void *fp);
-
-void report_button_press (void *fp);
+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);
+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_write (const char *buff, int fd);
-static void _send_to_magnifier (int x, int y, int w, int h);
 
 static boolean use_magnifier = FALSE;
 static boolean use_festival = FALSE;
 static boolean festival_chatty = FALSE;
 
-static struct sockaddr_un mag_server = { AF_UNIX , "/tmp/magnifier_socket" };
-static struct sockaddr_un client = { AF_UNIX, "/tmp/mag_client"};
+static SpiAccessibleEventListener *focus_listener;
+static SpiAccessibleEventListener *property_listener;
+static SpiAccessibleEventListener *button_listener;
+static KeystrokeListener *key_listener;
 
 int
 main(int argc, char **argv)
@@ -47,10 +50,8 @@ main(int argc, char **argv)
   int i, j;
   int n_desktops;
   int n_apps;
-  Accessible *desktop;
-  Accessible *application;
-  AccessibleEventListener *focus_listener;
-  AccessibleEventListener *button_listener;
+  SpiAccessible *desktop;
+  SpiAccessible *application;
 
   if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
   {
@@ -62,25 +63,43 @@ main(int argc, char **argv)
   SPI_init();
 
   focus_listener = createEventListener (report_focus_event);
+  property_listener = createEventListener (check_property_change); 
   button_listener = createEventListener (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));
-      n_apps = Accessible_getChildCount (desktop);
+      fprintf (stderr, "desktop %d name: %s\n", i, SpiAccessible_getName (desktop));
+      n_apps = SpiAccessible_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));
+          application = SpiAccessible_getChildAtIndex (desktop, j);
+          fprintf (stderr, "app %d name: %s\n", j, SpiAccessible_getName (application));
+         SpiAccessible_unref (application);
         }
     }
-  
+
+  /* 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);
+}
+
+static void
+get_environment_vars()
+{
   if (getenv ("FESTIVAL"))
   {
     use_festival = TRUE;
@@ -93,46 +112,133 @@ main(int argc, char **argv)
   {
     use_magnifier = TRUE;
   }  
-
-  SPI_event_main(FALSE);
 }
 
 void
-report_focus_event (void *p)
+report_focussed_accessible (SpiAccessible *obj, boolean shutup_previous_speech)
 {
-  AccessibleEvent *ev = (AccessibleEvent *) p;
-  fprintf (stderr, "%s event from %s\n", ev->type,
-           Accessible_getName (&ev->source));
-  
   if (use_festival)
     {
     if (festival_chatty)           
       {
-        _festival_say (Accessible_getRole (&ev->source), "voice_don_diphone", TRUE);
+        _festival_say (SpiAccessible_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");
+      _festival_say (SpiAccessible_getName (obj), "voice_kal_diphone",
+                    shutup_previous_speech || festival_chatty);
     }
   
-  if (Accessible_isComponent (&ev->source))
+  if (SpiAccessible_isComponent (obj))
     {
       long x, y, width, height;
-      AccessibleComponent *component = Accessible_getComponent (&ev->source);
-      fprintf (stderr, "Source implements IDL:Accessibility/Component:1.0\n");
-      AccessibleComponent_getExtents (component, &x, &y, &width, &height,
+      SpiAccessibleComponent *component = SpiAccessible_getComponent (obj);
+      SpiAccessibleComponent_getExtents (component, &x, &y, &width, &height,
                                       COORD_TYPE_SCREEN);
       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
                x, y, x+width, y+height);
-      if (use_magnifier)
-             _send_to_magnifier (x, y, width, height);
+      if (use_magnifier) {
+             magnifier_set_roi ((short) 0, x, y, width, height);
+      }
     }
+  /* if this is a text object, speak the first sentence. */
+  if (SpiAccessible_isText(obj))
+  {
+     SpiAccessibleText *spi_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");
+  }
+}
+
+void
+report_focus_event (void *p)
+{
+  SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
+  fprintf (stderr, "%s event from %s\n", ev->type,
+           SpiAccessible_getName (&ev->source));
+  report_focussed_accessible (&ev->source, TRUE);
 }
 
 void
 report_button_press (void *p)
 {
-  AccessibleEvent *ev = (AccessibleEvent *) p;
+  SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
   fprintf (stderr, "%s event from %s\n", ev->type,
-           Accessible_getName (&ev->source));
+           SpiAccessible_getName (&ev->source));
+}
+
+
+void
+check_property_change (void *p)
+{
+  SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
+  SpiAccessibleSelection *selection = SpiAccessible_getSelection (&ev->source);
+  int n_selections;
+  int i;
+  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);
+  /* 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);
+    }
+  }
+}
+
+static void
+simple_at_exit()
+{
+  deregisterGlobalEventListenerAll (focus_listener);
+  deregisterGlobalEventListenerAll (property_listener);
+  deregisterGlobalEventListenerAll (button_listener);
+  deregisterKeystrokeListener (key_listener, KEYMASK_ALT );
+  
+  SPI_exit ();
+}
+
+static boolean
+is_command_key (KeyStroke *key)
+{
+  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)
+{
+  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))?
+         (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
+         (int) key->keycode);
+  return is_command_key (key);
 }
 
 static int _festival_init ()
@@ -155,7 +261,7 @@ static int _festival_init ()
 
   _festival_write ("(audio_mode'async)\n", fd);
   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
-  _festival_write ("(Parameter.set 'Duration_Stretch 0.5)\n", fd);
+  _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
   return fd;
 }
 
@@ -196,7 +302,7 @@ static void _festival_say (const char *text, const char *voice, boolean shutup)
       snprintf (voice_spec, 32, "(%s)\n", voice); 
       _festival_write (voice_spec, fd);
       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
-      _festival_write ("(Parameter.set 'Duration_Stretch 0.5)\n", fd);
+      _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
     }
 
   _festival_write (quoted, fd);
@@ -214,37 +320,3 @@ static void _festival_write (const gchar *command_string, int fd)
   write(fd, command_string, strlen(command_string));
 }
 
-static void
-_send_to_magnifier(int x, int y, int w, int h)
-{
-  int desc, length_msg;
-  gchar buff[100];
-
-  sprintf (buff, "~5:%d,%d:", x+w/2, y+h/2);
-
-#ifdef MAG_DEBUG
-  g_print ("sending magnifier: %s\n", buff);
-#endif
-
-  if((desc=socket(AF_UNIX,SOCK_STREAM,0)) == -1){
-    perror("socket");
-    return;
-  }
-  unlink("/tmp/mag_client");
-
-  if (bind(desc, (struct sockaddr*)&client, sizeof(client)) == -1)
-    {
-      perror("bind");
-      return;
-    }
-
-  if (connect(desc,(struct sockaddr*)&mag_server,sizeof(mag_server)) == -1)
-    {
-      perror("connect");
-      return;
-    }
-
-  length_msg = write(desc,buff,strlen(buff));
-  unlink("/tmp/mag_client");
-  return;
-}