Added a new test for our key notification API
[platform/core/uifw/at-spi2-atk.git] / test / key-listener-test.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <sys/un.h>
30 #include <spi.h>
31
32 static SPIBoolean report_command_key_event  (const AccessibleKeystroke *stroke, void *user_data);
33 static SPIBoolean report_ordinary_key_event (const AccessibleKeystroke *stroke, void *user_data);
34 static SPIBoolean report_synchronous_key_event (const AccessibleKeystroke *stroke, void *user_data);
35
36 static AccessibleKeystrokeListener *command_key_listener;
37 static AccessibleKeystrokeListener *ordinary_key_listener;
38 static AccessibleKeystrokeListener *synchronous_key_listener;
39 static AccessibleKeySet            *command_keyset;
40 static AccessibleKeySet            *async_keyset;
41 static AccessibleKeySet            *sync_keyset;
42
43 int
44 main (int argc, char **argv)
45 {
46
47   SPI_init ();
48
49   /* prepare the keyboard snoopers */
50   command_key_listener = SPI_createAccessibleKeystrokeListener (report_command_key_event, NULL);
51   ordinary_key_listener = SPI_createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
52   synchronous_key_listener = SPI_createAccessibleKeystrokeListener (report_synchronous_key_event, NULL);
53
54   command_keyset = SPI_createAccessibleKeySet (11, "q", NULL, NULL);
55   async_keyset = SPI_createAccessibleKeySet (11, "abc", NULL, NULL);
56   sync_keyset = SPI_createAccessibleKeySet (11, "def", NULL, NULL);
57   
58   SPI_registerAccessibleKeystrokeListener(command_key_listener,
59                                           command_keyset,
60                                           SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL,
61                                           (unsigned long) ( SPI_KEY_PRESSED ),
62                                           SPI_KEYLISTENER_ALL_WINDOWS);
63
64   SPI_registerAccessibleKeystrokeListener(ordinary_key_listener,
65                                           async_keyset,
66                                           SPI_KEYMASK_UNMODIFIED,
67                                           (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
68                                           SPI_KEYLISTENER_NOSYNC);
69
70   SPI_registerAccessibleKeystrokeListener(synchronous_key_listener,
71                                           sync_keyset,
72                                           SPI_KEYMASK_UNMODIFIED,
73                                           (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
74                                           SPI_KEYLISTENER_CANCONSUME);
75
76   SPI_event_main ();
77
78   putenv ("AT_BRIDGE_SHUTDOWN=1");
79
80   return SPI_exit ();
81 }
82
83 static void
84 simple_at_exit ()
85 {
86   SPI_deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL);
87   AccessibleKeystrokeListener_unref         (command_key_listener);
88   SPI_freeAccessibleKeySet                  (command_keyset);
89   
90   SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL);
91   AccessibleKeystrokeListener_unref         (ordinary_key_listener);
92   SPI_freeAccessibleKeySet                  (async_keyset);
93   
94   SPI_deregisterAccessibleKeystrokeListener (synchronous_key_listener, SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL);
95   AccessibleKeystrokeListener_unref         (synchronous_key_listener);
96   SPI_freeAccessibleKeySet                  (sync_keyset);
97
98   SPI_event_quit ();
99 }
100
101 static SPIBoolean
102 is_command_key (const AccessibleKeystroke *key)
103 {
104   switch (key->keyID)
105     {
106     case 'Q':
107     case 'q':
108             simple_at_exit(); 
109             return TRUE; /* not reached */
110     default:
111             return FALSE;
112     }
113 }
114
115 static void
116 print_key_event (const AccessibleKeystroke *key, char *prefix)
117 {
118   fprintf (stderr, "%s KeyEvent %s%c (keycode %d); string=%s; time=%lx\n",
119            prefix,
120           (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
121           ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
122           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
123           (int) key->keycode,
124           key->keystring,
125           (long int) key->timestamp);   
126 }
127
128 static SPIBoolean
129 report_command_key_event (const AccessibleKeystroke *key, void *user_data)
130 {
131   print_key_event (key, "command");
132   return is_command_key (key);
133 }
134
135 static SPIBoolean
136 report_ordinary_key_event (const AccessibleKeystroke *key, void *user_data)
137 {
138   print_key_event (key, "ordinary");    
139   return FALSE;
140 }
141
142 static SPIBoolean
143 report_synchronous_key_event (const AccessibleKeystroke *key, void *user_data)
144 {
145   /* consume 'd' key, let others pass through */        
146   print_key_event (key, "synchronous (consumable) ");   
147   return ( key->keyID == 'd' ) ? TRUE : FALSE;
148 }
149