Added API to IDL and cspi (but not libspi, yet) for AccessibleComponent
[platform/core/uifw/at-spi2-atk.git] / libspi / keystrokelistener.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 /* keystrokelistener.c: implement the KeystrokeListener interface */
24
25 #include <config.h>
26 #ifdef SPI_DEBUG
27 #  include <stdio.h>
28 #endif
29 #include <libspi/listener.h>
30 #include <libspi/keystrokelistener.h>
31
32 /* Our parent Gtk object type  */
33 #define PARENT_TYPE BONOBO_TYPE_OBJECT
34
35 /* A pointer to our parent object class */
36 static BonoboObjectClass *keystroke_listener_parent_class;
37
38 enum {
39         KEY_EVENT,
40         LAST_SIGNAL
41 };
42 static guint signals [LAST_SIGNAL];
43
44 /* GObject::finalize */
45 static void
46 spi_keystroke_listener_object_finalize (GObject *object)
47 {
48   SpiKeystrokeListener *listener = SPI_KEYSTROKE_LISTENER (object);
49
50   g_list_free (listener->callbacks);
51 #ifdef SPI_DEBUG
52   fprintf(stderr, "keystroke_listener_object_finalize called\n");
53 #endif
54   ((GObjectClass *) keystroke_listener_parent_class)->finalize (object);
55 }
56
57 void   spi_keystroke_listener_add_callback (SpiKeystrokeListener *listener,
58                                             BooleanKeystrokeListenerCB callback)
59 {
60   listener->callbacks = g_list_append (listener->callbacks, callback);
61 #ifdef SPI_DEBUG
62         fprintf(stderr, "keystroke_listener_add_callback (%p) called\n",
63                 (gpointer) callback);
64 #endif
65 }
66
67 void
68 spi_keystroke_listener_remove_callback (SpiKeystrokeListener *listener,
69                                         BooleanKeystrokeListenerCB callback)
70 {
71   listener->callbacks = g_list_remove (listener->callbacks, callback);
72 }
73
74 /*
75  * CORBA Accessibility::KeystrokeListener::keyEvent method implementation
76  */
77 static CORBA_boolean
78 impl_key_event (PortableServer_Servant     servant,
79                 const Accessibility_KeyStroke *key,
80                 CORBA_Environment         *ev)
81 {
82   SpiKeystrokeListener *listener = SPI_KEYSTROKE_LISTENER (bonobo_object_from_servant (servant));
83   GList *callbacks = listener->callbacks;
84   gboolean was_consumed = FALSE;
85
86   g_signal_emit (G_OBJECT (listener), signals [KEY_EVENT], 0, key, &was_consumed);
87   if (was_consumed)
88     {
89       return TRUE;
90     }
91
92 #ifdef SPI_KEYEVENT_DEBUG
93   if (ev->_major != CORBA_NO_EXCEPTION) {
94     fprintf(stderr,
95             ("Accessibility app error: exception during keystroke notification: %s\n"),
96             CORBA_exception_id(ev));
97     exit(-1);
98   }
99   else {
100     fprintf(stderr, "%s%c",
101             (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
102             ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
103             (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID));
104   }
105 #endif
106   /* TODO: convert from the CORBA-based struct to a c-type-based one ? */
107 #ifdef SPI_KEYSTROKE_DEBUG  
108     fprintf (stderr, "Key:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
109            (long) key->keyID,
110            (unsigned int) key->modifiers,
111            (int) key->keycode,
112            (long int) key->timestamp);
113 #endif
114   while (callbacks)
115   {
116           BooleanKeystrokeListenerCB cb = (BooleanKeystrokeListenerCB) callbacks->data;
117           was_consumed = (*cb) (key) || was_consumed;
118           callbacks = g_list_next (callbacks);
119   }
120   return was_consumed;
121 }
122
123
124 static gboolean
125 boolean_handled_accumulator (GSignalInvocationHint *ihint,
126                              GValue                *return_accu,
127                              const GValue          *handler_return,
128                              gpointer               dummy)
129 {
130   gboolean continue_emission;
131   gboolean signal_handled;
132   
133   signal_handled = g_value_get_boolean (handler_return);
134   g_value_set_boolean (return_accu, signal_handled);
135   continue_emission = !signal_handled;
136   
137   return continue_emission;
138 }
139
140 void
141 marshal_BOOLEAN__POINTER (GClosure     *closure,
142                           GValue       *return_value,
143                           guint         n_param_values,
144                           const GValue *param_values,
145                           gpointer      invocation_hint,
146                           gpointer      marshal_data)
147 {
148   typedef gboolean (*GMarshalFunc_BOOLEAN__POINTER) (gpointer     data1,
149                                                      gpointer     arg_1,
150                                                      gpointer     data2);
151   register GMarshalFunc_BOOLEAN__POINTER callback;
152   register GCClosure *cc = (GCClosure*) closure;
153   register gpointer data1, data2;
154   gboolean v_return;
155
156   g_return_if_fail (return_value != NULL);
157   g_return_if_fail (n_param_values == 2);
158
159   if (G_CCLOSURE_SWAP_DATA (closure))
160     {
161       data1 = closure->data;
162       data2 = g_value_peek_pointer (param_values + 0);
163     }
164   else
165     {
166       data1 = g_value_peek_pointer (param_values + 0);
167       data2 = closure->data;
168     }
169   callback = (GMarshalFunc_BOOLEAN__POINTER) (marshal_data ? marshal_data : cc->callback);
170
171   v_return = callback (data1,
172                        g_value_get_pointer (param_values + 1),
173                        data2);
174
175   g_value_set_boolean (return_value, v_return);
176 }
177
178 static void
179 spi_keystroke_listener_class_init (SpiKeystrokeListenerClass *klass)
180 {
181   GObjectClass * object_class = (GObjectClass *) klass;
182   POA_Accessibility_KeystrokeListener__epv *epv = &klass->epv;
183   keystroke_listener_parent_class = g_type_class_peek_parent (klass);
184   
185   signals [KEY_EVENT] = g_signal_new (
186     "key_event",
187     G_TYPE_FROM_CLASS (klass),
188     G_SIGNAL_RUN_LAST,
189     G_STRUCT_OFFSET (SpiKeystrokeListenerClass, key_event),
190     boolean_handled_accumulator, NULL,
191     marshal_BOOLEAN__POINTER,
192     G_TYPE_BOOLEAN, 1, G_TYPE_POINTER);
193   
194   object_class->finalize = spi_keystroke_listener_object_finalize;
195   
196   epv->keyEvent = impl_key_event;
197 }
198
199 static void
200 spi_keystroke_listener_init (SpiKeystrokeListener *keystroke_listener)
201 {
202   keystroke_listener->callbacks = NULL;
203 }
204
205 BONOBO_TYPE_FUNC_FULL (SpiKeystrokeListener,
206                        Accessibility_KeystrokeListener,
207                        BONOBO_TYPE_OBJECT,
208                        spi_keystroke_listener);
209
210 SpiKeystrokeListener *
211 spi_keystroke_listener_new (void)
212 {
213     SpiKeystrokeListener *retval = g_object_new (
214             SPI_KEYSTROKE_LISTENER_TYPE, NULL);
215     return retval;
216 }