9ec6a10275bf4915374a48564a443b985d21e7cb
[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   spi_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   /* TODO: convert from the CORBA-based struct to a c-type-based one ? */
92     fprintf (stderr, "Key:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
93            (long) key->keyID,
94            (unsigned int) key->modifiers,
95            (int) key->keycode,
96            (long int) key->timestamp);
97
98   while (callbacks)
99   {
100           BooleanKeystrokeListenerCB cb = (BooleanKeystrokeListenerCB) callbacks->data;
101           was_consumed = (*cb) (key) || was_consumed;
102           callbacks = g_list_next (callbacks);
103   }
104   return was_consumed;
105 }
106
107 static void
108 spi_keystroke_listener_class_init (SpiKeystrokeListenerClass *klass)
109 {
110         GObjectClass * object_class = (GObjectClass *) klass;
111         POA_Accessibility_KeystrokeListener__epv *epv = &klass->epv;
112         keystroke_listener_parent_class = g_type_class_peek_parent (klass);
113
114         object_class->finalize = keystroke_listener_object_finalize;
115
116         epv->keyEvent = impl_key_event;
117 }
118
119 static void
120 spi_keystroke_listener_init (SpiKeystrokeListener *keystroke_listener)
121 {
122         keystroke_listener->callbacks = NULL;
123 }
124
125 BONOBO_TYPE_FUNC_FULL (SpiKeystrokeListener,
126                        Accessibility_KeystrokeListener,
127                        BONOBO_TYPE_OBJECT,
128                        spi_keystroke_listener);
129
130 SpiKeystrokeListener *
131 spi_keystroke_listener_new (void)
132 {
133     SpiKeystrokeListener *retval = g_object_new (
134             SPI_KEYSTROKE_LISTENER_TYPE, NULL);
135     return retval;
136 }