Debian Codegraph @

clutter/cally/cally-util.c

raw
1 /* CALLY - The Clutter Accessibility Implementation Library
2 *
3 * Copyright (C) 2008 Igalia, S.L.
4 *
5 * Author: Alejandro PiƱeiro Iglesias <[email protected]>
6 *
7 * Based on GailUtil from GAIL
8 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 */
25
26 /**
27 * SECTION:cally-util
28 * @Title: CallyUtil
29 * @short_description: #AtkUtil implementation
30 * @see_also: #ClutterActor
31 *
32 * #CallyUtil implements #AtkUtil abstract methods. Although it
33 * includes the name "Util" it is in fact one of the most important
34 * interfaces to be implemented in any ATK toolkit implementation.
35
36 * For instance, it defines atk_get_root(), the method that returns
37 * the root object in the hierarchy. Without it, you don't have
38 * available any accessible object.
39 */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <stdlib.h>
46 #include <string.h>
47 #include <clutter/clutter.h>
48
49 #include "cally-util.h"
50 #include "cally-root.h"
51 #include "cally-stage.h"
52
53 #define DEFAULT_PASSWORD_CHAR '*'
54
55 /* atkutil.h */
56
57 static guint cally_util_add_key_event_listener (AtkKeySnoopFunc listener,
58 gpointer data);
59 static void cally_util_remove_key_event_listener (guint remove_listener);
60 static AtkObject* cally_util_get_root (void);
61 static const gchar * cally_util_get_toolkit_name (void);
62 static const gchar * cally_util_get_toolkit_version (void);
63
64 /* private */
65 static void cally_util_simulate_snooper_install (void);
66 static void cally_util_simulate_snooper_remove (void);
67 static gboolean cally_key_snooper (ClutterActor *actor,
68 ClutterEvent *event,
69 gpointer user_data);
70 static void cally_util_stage_added_cb (ClutterStageManager *stage_manager,
71 ClutterStage *stage,
72 gpointer data);
73 static void cally_util_stage_removed_cb (ClutterStageManager *stage_manager,
74 ClutterStage *stage,
75 gpointer data);
76 static gboolean notify_hf (gpointer key,
77 gpointer value,
78 gpointer data);
79 static void insert_hf (gpointer key,
80 gpointer value,
81 gpointer data);
82
83 /* This is just a copy of the Gail one, a shared library or place to
84 define it could be a good idea. */
85 typedef struct _CallyKeyEventInfo CallyKeyEventInfo;
86
87 struct _CallyKeyEventInfo
88 {
89 AtkKeySnoopFunc listener;
90 gpointer func_data;
91 };
92
93 static AtkObject* root = NULL;
94 static GHashTable *key_listener_list = NULL;
95
96
97 G_DEFINE_TYPE (CallyUtil, cally_util, ATK_TYPE_UTIL);
98
99 static void
100 cally_util_class_init (CallyUtilClass *klass)
101 {
102 AtkUtilClass *atk_class;
103 gpointer data;
104
105 data = g_type_class_peek (ATK_TYPE_UTIL);
106 atk_class = ATK_UTIL_CLASS (data);
107
108 atk_class->add_key_event_listener = cally_util_add_key_event_listener;
109 atk_class->remove_key_event_listener = cally_util_remove_key_event_listener;
110 atk_class->get_root = cally_util_get_root;
111 atk_class->get_toolkit_name = cally_util_get_toolkit_name;
112 atk_class->get_toolkit_version = cally_util_get_toolkit_version;
113
114 /* FIXME: Instead of create this on the class, I think that would
115 worth to implement CallyUtil as a singleton instance, so the
116 class methods will access this instance. This will be a good
117 future enhancement, meanwhile, just using the same *working*
118 implementation used on GailUtil */
119 }
120
121 static void
122 cally_util_init (CallyUtil *cally_util)
123 {
124 /* instance init: usually not required */
125 }
126
127 /* ------------------------------ ATK UTIL METHODS -------------------------- */
128
129 static AtkObject*
130 cally_util_get_root (void)
131 {
132 if (!root)
133 root = cally_root_new ();
134
135 return root;
136 }
137
138 static const gchar *
139 cally_util_get_toolkit_name (void)
140 {
141 return "clutter";
142 }
143
144 static const gchar *
145 cally_util_get_toolkit_version (void)
146 {
147 return CLUTTER_VERSION_S;
148 }
149
150 static guint
151 cally_util_add_key_event_listener (AtkKeySnoopFunc listener,
152 gpointer data)
153 {
154 static guint key = 1;
155 CallyKeyEventInfo *event_info = NULL;
156
157 if (!key_listener_list)
158 {
159 key_listener_list = g_hash_table_new_full (NULL, NULL, NULL, g_free);
160
161 cally_util_simulate_snooper_install ();
162 }
163
164 event_info = g_new (CallyKeyEventInfo, 1);
165 event_info->listener = listener;
166 event_info->func_data = data;
167
168 g_hash_table_insert (key_listener_list, GUINT_TO_POINTER (key++), event_info);
169 /* XXX: we don't check to see if n_listeners > MAXUINT */
170 return key - 1;
171 }
172
173 static void
174 cally_util_remove_key_event_listener (guint remove_listener)
175 {
176 if (!g_hash_table_remove (key_listener_list, GUINT_TO_POINTER (remove_listener))) {
177 g_warning ("Not able to remove listener with id %i", remove_listener);
178 }
179
180 if (g_hash_table_size (key_listener_list) == 0)
181 {
182 g_hash_table_destroy (key_listener_list);
183 key_listener_list = NULL;
184 cally_util_simulate_snooper_remove ();
185 }
186 }
187
188 /* ------------------------------ PRIVATE FUNCTIONS ------------------------- */
189
190 /* Trying to emulate gtk_key_snooper install (a kind of wrapper). This
191 could be implemented without it, but I will maintain it in this
192 way, so if in the future clutter implements it natively it would be
193 easier the transition */
194 static void
195 cally_util_simulate_snooper_install (void)
196 {
197 ClutterStageManager *stage_manager = NULL;
198 ClutterStage *stage = NULL;
199 GSList *stage_list = NULL;
200 GSList *iter = NULL;
201
202 stage_manager = clutter_stage_manager_get_default ();
203 stage_list = clutter_stage_manager_list_stages (stage_manager);
204
205 for (iter = stage_list; iter != NULL; iter = g_slist_next (iter))
206 {
207 stage = CLUTTER_STAGE (iter->data);
208
209 g_signal_connect (G_OBJECT (stage), "captured-event",
210 G_CALLBACK (cally_key_snooper), NULL);
211 }
212
213 g_signal_connect (G_OBJECT (stage_manager), "stage-added",
214 G_CALLBACK (cally_util_stage_added_cb), cally_key_snooper);
215 g_signal_connect (G_OBJECT (stage_manager), "stage-removed",
216 G_CALLBACK (cally_util_stage_removed_cb), cally_key_snooper);
217 }
218
219 static void
220 cally_util_simulate_snooper_remove (void)
221 {
222 ClutterStageManager *stage_manager = NULL;
223 ClutterStage *stage = NULL;
224 GSList *stage_list = NULL;
225 GSList *iter = NULL;
226 gint num = 0;
227
228 stage_manager = clutter_stage_manager_get_default ();
229 stage_list = clutter_stage_manager_list_stages (stage_manager);
230
231 for (iter = stage_list; iter != NULL; iter = g_slist_next (iter))
232 {
233 stage = CLUTTER_STAGE (iter->data);
234
235 num += g_signal_handlers_disconnect_by_func (stage, cally_key_snooper, NULL);
236 }
237
238 g_signal_handlers_disconnect_by_func (G_OBJECT (stage_manager),
239 G_CALLBACK (cally_util_stage_added_cb),
240 cally_key_snooper);
241
242 g_signal_handlers_disconnect_by_func (G_OBJECT (stage_manager),
243 G_CALLBACK (cally_util_stage_removed_cb),
244 cally_key_snooper);
245
246 #ifdef CALLY_DEBUG
247 g_print ("Number of snooper callbacks disconnected: %i\n", num);
248 #endif
249 }
250
251 static AtkKeyEventStruct *
252 atk_key_event_from_clutter_event_key (ClutterKeyEvent *clutter_event,
253 gunichar password_char)
254 {
255 AtkKeyEventStruct *atk_event = g_new0 (AtkKeyEventStruct, 1);
256 gunichar key_unichar;
257
258 switch (clutter_event->type)
259 {
260 case CLUTTER_KEY_PRESS:
261 atk_event->type = ATK_KEY_EVENT_PRESS;
262 break;
263 case CLUTTER_KEY_RELEASE:
264 atk_event->type = ATK_KEY_EVENT_RELEASE;
265 break;
266 default:
267 g_assert_not_reached ();
268 return NULL;
269 }
270
271 if (password_char)
272 atk_event->state = 0;
273 else
274 atk_event->state = clutter_event->modifier_state;
275
276 /* We emit the clutter keyval. This is not exactly the one expected
277 by AtkKeyEventStruct, as it expects a Gdk-like event, with the
278 modifiers applied. But to avoid a dependency to gdk, we delegate
279 that on the AT application.
280 More information: Bug 1952 and bug 2072
281 */
282 if (password_char)
283 atk_event->keyval = clutter_unicode_to_keysym (password_char);
284 else
285 atk_event->keyval = clutter_event->keyval;
286
287 /* It is expected to store a key defining string here (ie "Space" in
288 case you press a space). Anyway, there are no function on clutter
289 to obtain that, and we want to avoid a gdk dependency here, so we
290 delegate on the AT application to obtain that string using the
291 rest of the data on the ATK event struct.
292
293 More information: Bug 1952 and 2072
294 */
295
296 if (password_char)
297 key_unichar = password_char;
298 else
299 key_unichar = clutter_event_get_key_unicode ((ClutterEvent *) clutter_event);
300
301 if (g_unichar_validate (key_unichar) && !g_unichar_iscntrl (key_unichar))
302 {
303 GString *new = NULL;
304
305 new = g_string_new ("");
306 new = g_string_insert_unichar (new, 0, key_unichar);
307 atk_event->string = new->str;
308 g_string_free (new, FALSE);
309 }
310 else
311 atk_event->string = NULL;
312
313 atk_event->length = 0;
314
315 /* Computing the hardware keycode from the password-char is
316 difficult. But we are in a password situation. We are already a
317 unichar that it is not the original one. Providing a "almost
318 real" keycode is irrelevant */
319 if (password_char)
320 atk_event->keycode = 0;
321 else
322 atk_event->keycode = clutter_event->hardware_keycode;
323
324 atk_event->timestamp = clutter_event->time;
325
326 #ifdef CALLY_DEBUG
327
328 g_debug ("CallyKeyEvent:\tsym 0x%x\n\t\tmods %x\n\t\tcode %u\n\t\ttime %lx \n\t\tstring %s\n",
329 (unsigned int) atk_event->keyval,
330 (unsigned int) atk_event->state,
331 (unsigned int) atk_event->keycode,
332 (unsigned long int) atk_event->timestamp,
333 atk_event->string);
334 #endif
335
336 return atk_event;
337 }
338
339
340 static gboolean
341 notify_hf (gpointer key, gpointer value, gpointer data)
342 {
343 CallyKeyEventInfo *info = (CallyKeyEventInfo *) value;
344 AtkKeyEventStruct *key_event = (AtkKeyEventStruct *)data;
345
346 return (*(AtkKeySnoopFunc) info->listener) (key_event, info->func_data) ? TRUE : FALSE;
347 }
348
349 static void
350 insert_hf (gpointer key, gpointer value, gpointer data)
351 {
352 GHashTable *new_table = (GHashTable *) data;
353 g_hash_table_insert (new_table, key, value);
354 }
355
356
357 /*
358 * 0 if the key of that event is visible, in other case the password
359 * char
360 */
361 static gunichar
362 check_key_visibility (ClutterEvent *event)
363 {
364 ClutterKeyEvent *key_event = (ClutterKeyEvent *)event;
365 AtkObject *accessible = clutter_actor_get_accessible (key_event->source);
366
367 g_return_val_if_fail (accessible != NULL, 0);
368
369 if (atk_object_get_role (accessible) != ATK_ROLE_PASSWORD_TEXT)
370 return 0;
371
372 /* If it is a clutter text, we use his password char. Note that
373 although at Clutter toolkit itself, only ClutterText exposes a
374 password role, nothing prevents on any derived toolkit (like st)
375 to create a new actor that can behave like a password entry. And
376 the key event will still be emitted here. Although in that case
377 we would lose any password char from the derived toolkit, it is
378 still better fill this with a default unichar that the original
379 one */
380
381 if (CLUTTER_IS_TEXT (key_event->source))
382 return clutter_text_get_password_char (CLUTTER_TEXT (key_event->source));
383 else
384 return DEFAULT_PASSWORD_CHAR;
385 }
386
387 static gboolean
388 cally_key_snooper (ClutterActor *actor,
389 ClutterEvent *event,
390 gpointer user_data)
391 {
392 AtkKeyEventStruct *key_event = NULL;
393 gint consumed = 0;
394 gunichar password_char = 0;
395
396 /* filter key events */
397 if ((event->type != CLUTTER_KEY_PRESS) && (event->type != CLUTTER_KEY_RELEASE))
398 {
399 return FALSE;
400 }
401
402 password_char = check_key_visibility (event);
403
404 if (key_listener_list)
405 {
406 GHashTable *new_hash = g_hash_table_new (NULL, NULL);
407
408 g_hash_table_foreach (key_listener_list, insert_hf, new_hash);
409 key_event = atk_key_event_from_clutter_event_key ((ClutterKeyEvent *)event,
410 password_char);
411 /* func data is inside the hash table */
412 consumed = g_hash_table_foreach_steal (new_hash, notify_hf, key_event);
413 g_hash_table_destroy (new_hash);
414
415 g_free (key_event->string);
416 g_free (key_event);
417 }
418
419 return (consumed ? 1 : 0);
420 }
421
422 static void
423 cally_util_stage_added_cb (ClutterStageManager *stage_manager,
424 ClutterStage *stage,
425 gpointer data)
426 {
427 GCallback cally_key_snooper_cb = G_CALLBACK (data);
428
429 g_signal_connect (G_OBJECT (stage), "captured-event", cally_key_snooper_cb, NULL);
430 }
431
432 static void
433 cally_util_stage_removed_cb (ClutterStageManager *stage_manager,
434 ClutterStage *stage,
435 gpointer data)
436 {
437 GCallback cally_key_snooper_cb = G_CALLBACK (data);
438
439 g_signal_handlers_disconnect_by_func (stage, cally_key_snooper_cb, NULL);
440 }
441
442 void
443 _cally_util_override_atk_util (void)
444 {
445 AtkUtilClass *atk_class = ATK_UTIL_CLASS (g_type_class_ref (ATK_TYPE_UTIL));
446
447 atk_class->add_key_event_listener = cally_util_add_key_event_listener;
448 atk_class->remove_key_event_listener = cally_util_remove_key_event_listener;
449 atk_class->get_root = cally_util_get_root;
450 atk_class->get_toolkit_name = cally_util_get_toolkit_name;
451 atk_class->get_toolkit_version = cally_util_get_toolkit_version;
452 }