"Unfixed" some things :-). cleaned up and made some of the namespace changes more...
[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 /*
24  * listener.c: test for accessibility implementation
25  *
26  */
27
28 #ifdef SPI_DEBUG
29 #include <stdio.h>
30 #endif
31
32 #include <config.h>
33 #include <bonobo/Bonobo.h>
34 #include <libspi/Accessibility.h>
35
36 /*
37  * This pulls the definition for the BonoboObject (GType)
38  */
39 #include "keystrokelistener.h"
40
41 /*
42  * Our parent Gtk object type
43  */
44 #define PARENT_TYPE BONOBO_OBJECT_TYPE
45
46 /*
47  * A pointer to our parent object class
48  */
49 static GObjectClass *keystroke_listener_parent_class;
50
51 /*
52  * Implemented GObject::finalize
53  */
54 static void
55 keystroke_listener_object_finalize (GObject *object)
56 {
57
58 #ifdef SPI_DEBUG
59         fprintf(stderr, "keystroke_listener_object_finalize called\n");
60 #endif
61         keystroke_listener_parent_class->finalize (object);
62 }
63
64 void   spi_keystroke_listener_add_callback (SpiKeystrokeListener *listener,
65                                             BooleanKeystrokeListenerCB callback)
66 {
67   listener->callbacks = g_list_append (listener->callbacks, callback);
68 #ifdef SPI_DEBUG
69         fprintf(stderr, "keystroke_listener_add_callback (%p) called\n",
70                 (gpointer) callback);
71 #endif
72 }
73
74 void   keystroke_listener_remove_callback (SpiKeystrokeListener *listener,
75                                            BooleanKeystrokeListenerCB callback)
76 {
77   listener->callbacks = g_list_remove (listener->callbacks, callback);
78 }
79
80 /*
81  * CORBA Accessibility::KeystrokeListener::keyEvent method implementation
82  */
83 static CORBA_boolean
84 impl_key_event (PortableServer_Servant     servant,
85                 const Accessibility_KeyStroke *key,
86                 CORBA_Environment         *ev)
87 {
88   SpiKeystrokeListener *listener = SPI_KEYSTROKE_LISTENER (bonobo_object_from_servant (servant));
89   GList *callbacks = listener->callbacks;
90   gboolean was_consumed = FALSE;
91 #ifdef SPI_KEYEVENT_DEBUG
92   if (ev->_major != CORBA_NO_EXCEPTION) {
93     fprintf(stderr,
94             ("Accessibility app error: exception during keystroke notification: %s\n"),
95             CORBA_exception_id(ev));
96     exit(-1);
97   }
98   else {
99     fprintf(stderr, "%s%c",
100             (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
101             ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
102             (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID));
103   }
104 #endif
105   while (callbacks)
106   {
107           BooleanKeystrokeListenerCB cb = (BooleanKeystrokeListenerCB) callbacks->data;
108           was_consumed = (*cb) (key) || was_consumed;
109           callbacks = g_list_next (callbacks);
110   }
111   return was_consumed;
112 }
113
114 static void
115 keystroke_listener_class_init (SpiKeystrokeListenerClass *klass)
116 {
117         GObjectClass * object_class = (GObjectClass *) klass;
118         POA_Accessibility_KeystrokeListener__epv *epv = &klass->epv;
119         keystroke_listener_parent_class = g_type_class_ref (BONOBO_OBJECT_TYPE);
120
121         object_class->finalize = keystroke_listener_object_finalize;
122
123         epv->keyEvent = impl_key_event;
124 }
125
126 static void
127 keystroke_listener_init (SpiKeystrokeListener *keystroke_listener)
128 {
129         keystroke_listener->callbacks = NULL;
130 }
131
132 GType
133 spi_keystroke_listener_get_type (void)
134 {
135         static GType type = 0;
136
137         if (!type) {
138                 static const GTypeInfo tinfo = {
139                         sizeof (SpiKeystrokeListenerClass),
140                         (GBaseInitFunc) NULL,
141                         (GBaseFinalizeFunc) NULL,
142                         (GClassInitFunc) keystroke_listener_class_init,
143                         (GClassFinalizeFunc) NULL,
144                         NULL, /* class data */
145                         sizeof (SpiKeystrokeListener),
146                         0, /* n preallocs */
147                         (GInstanceInitFunc) keystroke_listener_init,
148                         NULL /* value table */
149                 };
150                 /*
151                  *   Here we use bonobo_type_unique instead of
152                  * gtk_type_unique, this auto-generates a load of
153                  * CORBA structures for us. All derived types must
154                  * use bonobo_type_unique.
155                  */
156                 type = bonobo_type_unique (
157                         PARENT_TYPE,
158                         POA_Accessibility_KeystrokeListener__init,
159                         NULL,
160                         G_STRUCT_OFFSET (SpiKeystrokeListenerClass, epv),
161                         &tinfo,
162                         "SpiKeystrokeListener");
163         }
164
165         return type;
166 }
167
168 SpiKeystrokeListener *
169 spi_keystroke_listener_new (void)
170 {
171     SpiKeystrokeListener *retval =
172                SPI_KEYSTROKE_LISTENER (g_object_new (spi_keystroke_listener_get_type (), NULL));
173     return retval;
174 }