Started fixing IDL docs.
[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/keystrokelistener.h>
30
31 /* Our parent Gtk object type  */
32 #define PARENT_TYPE BONOBO_TYPE_OBJECT
33
34 /* A pointer to our parent object class */
35 static GObjectClass *keystroke_listener_parent_class;
36
37 /*
38  * Implemented GObject::finalize
39  */
40 static void
41 keystroke_listener_object_finalize (GObject *object)
42 {
43
44 #ifdef SPI_DEBUG
45         fprintf(stderr, "keystroke_listener_object_finalize called\n");
46 #endif
47         keystroke_listener_parent_class->finalize (object);
48 }
49
50 void   spi_keystroke_listener_add_callback (SpiKeystrokeListener *listener,
51                                             BooleanKeystrokeListenerCB callback)
52 {
53   listener->callbacks = g_list_append (listener->callbacks, callback);
54 #ifdef SPI_DEBUG
55         fprintf(stderr, "keystroke_listener_add_callback (%p) called\n",
56                 (gpointer) callback);
57 #endif
58 }
59
60 void   keystroke_listener_remove_callback (SpiKeystrokeListener *listener,
61                                            BooleanKeystrokeListenerCB callback)
62 {
63   listener->callbacks = g_list_remove (listener->callbacks, callback);
64 }
65
66 /*
67  * CORBA Accessibility::KeystrokeListener::keyEvent method implementation
68  */
69 static CORBA_boolean
70 impl_key_event (PortableServer_Servant     servant,
71                 const Accessibility_KeyStroke *key,
72                 CORBA_Environment         *ev)
73 {
74   SpiKeystrokeListener *listener = SPI_KEYSTROKE_LISTENER (bonobo_object_from_servant (servant));
75   GList *callbacks = listener->callbacks;
76   gboolean was_consumed = FALSE;
77 #ifdef SPI_KEYEVENT_DEBUG
78   if (ev->_major != CORBA_NO_EXCEPTION) {
79     fprintf(stderr,
80             ("Accessibility app error: exception during keystroke notification: %s\n"),
81             CORBA_exception_id(ev));
82     exit(-1);
83   }
84   else {
85     fprintf(stderr, "%s%c",
86             (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
87             ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
88             (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID));
89   }
90 #endif
91   while (callbacks)
92   {
93           BooleanKeystrokeListenerCB cb = (BooleanKeystrokeListenerCB) callbacks->data;
94           was_consumed = (*cb) (key) || was_consumed;
95           callbacks = g_list_next (callbacks);
96   }
97   return was_consumed;
98 }
99
100 static void
101 spi_keystroke_listener_class_init (SpiKeystrokeListenerClass *klass)
102 {
103         GObjectClass * object_class = (GObjectClass *) klass;
104         POA_Accessibility_KeystrokeListener__epv *epv = &klass->epv;
105         keystroke_listener_parent_class = g_type_class_peek_parent (klass);
106
107         object_class->finalize = keystroke_listener_object_finalize;
108
109         epv->keyEvent = impl_key_event;
110 }
111
112 static void
113 spi_keystroke_listener_init (SpiKeystrokeListener *keystroke_listener)
114 {
115         keystroke_listener->callbacks = NULL;
116 }
117
118 BONOBO_TYPE_FUNC_FULL (SpiKeystrokeListener,
119                        Accessibility_KeystrokeListener,
120                        BONOBO_TYPE_OBJECT,
121                        spi_keystroke_listener);
122
123 SpiKeystrokeListener *
124 spi_keystroke_listener_new (void)
125 {
126     SpiKeystrokeListener *retval = g_object_new (
127             SPI_KEYSTROKE_LISTENER_TYPE, NULL);
128     return retval;
129 }