1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/*
* Copyright (C) 2011 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Alejandro PiƱeiro Iglesias <apinheiro@igalia.com>
* Rodrigo Moya <rodrigo.moya@canonical.com>
*/
#include <gdk/gdk.h>
#include <stdlib.h>
#include <string.h>
#include "unity-util-accessible.h"
#include "unity-root-accessible.h"
#include "nux-base-window-accessible.h"
static void unity_util_accessible_class_init(UnityUtilAccessibleClass* klass);
static void unity_util_accessible_init(UnityUtilAccessible* unity_util_accessible);
/* atkutil.h */
static guint unity_util_accessible_add_global_event_listener(GSignalEmissionHook listener,
const gchar* event_type);
static void unity_util_accessible_remove_global_event_listener(guint remove_listener);
static AtkObject* unity_util_accessible_get_root(void);
static const gchar* unity_util_accessible_get_toolkit_name(void);
static const gchar* unity_util_accessible_get_toolkit_version(void);
static guint unity_util_accessible_add_key_event_listener(AtkKeySnoopFunc listener,
gpointer data);
static void unity_util_accessible_remove_key_event_listener(guint remove_listener);
typedef struct
{
guint idx;
gulong hook_id;
guint signal_id;
} UnityUtilListenerInfo;
typedef struct
{
AtkKeySnoopFunc func;
gpointer data;
guint key;
} UnityKeyEventListener;
/* FIXME: move this to a private structure on UnityUtilAccessible? */
static GHashTable* listener_list = NULL;
static AtkObject* root = NULL;
static GSList* key_listener_list = NULL;
static guint event_inspector_id = 0;
static nux::WindowThread* unity_window_thread = NULL;
G_DEFINE_TYPE(UnityUtilAccessible, unity_util_accessible, ATK_TYPE_UTIL);
static void
unity_util_accessible_class_init(UnityUtilAccessibleClass* klass)
{
AtkUtilClass* atk_class;
gpointer data;
data = g_type_class_peek(ATK_TYPE_UTIL);
atk_class = ATK_UTIL_CLASS(data);
atk_class->add_global_event_listener = unity_util_accessible_add_global_event_listener;
atk_class->remove_global_event_listener = unity_util_accessible_remove_global_event_listener;
atk_class->add_key_event_listener = unity_util_accessible_add_key_event_listener;
atk_class->remove_key_event_listener = unity_util_accessible_remove_key_event_listener;
atk_class->get_root = unity_util_accessible_get_root;
atk_class->get_toolkit_name = unity_util_accessible_get_toolkit_name;
atk_class->get_toolkit_version = unity_util_accessible_get_toolkit_version;
}
static void
unity_util_accessible_init(UnityUtilAccessible* unity_util_accessible)
{
}
static AtkObject*
unity_util_accessible_get_root(void)
{
if (!root)
root = unity_root_accessible_new();
return root;
}
static const gchar*
unity_util_accessible_get_toolkit_name(void)
{
return "UNITY";
}
static const gchar*
unity_util_accessible_get_toolkit_version(void)
{
/*
* FIXME:
* Version is passed in as a -D flag when this file is
* compiled.
*/
return "0.1";
}
static guint
add_listener(GSignalEmissionHook listener,
const gchar* object_type,
const gchar* signal_name,
const gchar* hook_data)
{
GType type;
guint signal_id;
guint rc = 0;
static guint listener_idx = 1;
if (!listener_list)
listener_list = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, g_free);
type = g_type_from_name(object_type);
if (type)
{
signal_id = g_signal_lookup(signal_name, type);
if (signal_id > 0)
{
UnityUtilListenerInfo* listener_info;
rc = listener_idx;
listener_info = g_new0(UnityUtilListenerInfo, 1);
listener_info->idx = listener_idx;
listener_info->hook_id = g_signal_add_emission_hook(signal_id, 0, listener,
g_strdup(hook_data),
(GDestroyNotify) g_free);
listener_info->signal_id = signal_id;
g_hash_table_insert(listener_list, &(listener_info->idx), listener_info);
listener_idx++;
}
else
{
/* Mainly becase some "window::xxx" methods not implemented
on NuxBaseWindowAccessible */
g_debug("Signal type %s not supported\n", signal_name);
}
}
else
g_warning("Invalid object type %s\n", object_type);
return rc;
}
static void
do_window_event_initialization(void)
{
/*
* Ensure that NuxBaseWindowClass exists
*/
g_type_class_unref(g_type_class_ref(NUX_TYPE_BASE_WINDOW_ACCESSIBLE));
}
static guint
unity_util_accessible_add_global_event_listener(GSignalEmissionHook listener,
const gchar* event_type)
{
gchar** split_string;
guint rc = 0;
split_string = g_strsplit(event_type, ":", 3);
if (split_string)
{
if (g_str_equal("window", split_string[0]))
{
/* Using NuxBaseWindow as the toplevelwindow */
static gboolean initialized = FALSE;
if (initialized == FALSE)
{
do_window_event_initialization();
initialized = TRUE;
}
rc = add_listener(listener, "NuxBaseWindowAccessible", split_string [1], event_type);
}
else
{
rc = add_listener(listener, split_string[1], split_string[2], event_type);
}
g_strfreev(split_string);
}
return rc;
}
static void
unity_util_accessible_remove_global_event_listener(guint remove_listener)
{
if (remove_listener > 0)
{
UnityUtilListenerInfo* listener_info;
listener_info = (UnityUtilListenerInfo*) g_hash_table_lookup(listener_list, &remove_listener);
if (listener_info != NULL)
{
if (listener_info->hook_id != 0 && listener_info->signal_id != 0)
{
g_signal_remove_emission_hook(listener_info->signal_id,
listener_info->hook_id);
g_hash_table_remove(listener_list, &remove_listener);
}
else
{
g_warning("Invalid listener hook_id %ld or signal_id %d",
listener_info->hook_id, listener_info->signal_id);
}
}
else
g_warning("No listener with the specified ID: %d", remove_listener);
}
else
g_warning("Invalid listener_id: %d", remove_listener);
}
static guint
translate_nux_modifiers_to_gdk_state(unsigned long nux_modifier)
{
guint result = 0;
if (nux_modifier & nux::NUX_STATE_SHIFT)
result |= GDK_SHIFT_MASK;
if (nux_modifier & nux::NUX_STATE_CAPS_LOCK)
result |= GDK_LOCK_MASK;
if (nux_modifier & nux::NUX_STATE_CTRL)
result |= GDK_CONTROL_MASK;
/* From gdk documentation
GDK_MOD1_MASK : the fourth modifier key (it depends on the
modifier mapping of the X server which key is interpreted as this
modifier, but normally it is the Alt key).
*/
if (nux_modifier & nux::NUX_STATE_ALT)
result |= GDK_MOD1_MASK;
/* FIXME: not sure how to translate this ones */
// if (nux_modifier & NUX_STATE_SCROLLLOCK)
// if (nux_modifier & NUX_STATE_NUMLOCK)
return result;
}
static AtkKeyEventStruct*
atk_key_event_from_nux_event_key(nux::Event* event)
{
AtkKeyEventStruct* atk_event = g_new0(AtkKeyEventStruct, 1);
gunichar key_unichar;
static GdkDisplay* display = gdk_display_get_default();
static GdkKeymap* keymap = gdk_keymap_get_for_display(display);
GdkKeymapKey* keys = NULL;
gint n_keys = 0;
gboolean success = FALSE;
switch (event->type)
{
case nux::NUX_KEYDOWN:
atk_event->type = ATK_KEY_EVENT_PRESS;
break;
case nux::NUX_KEYUP:
atk_event->type = ATK_KEY_EVENT_RELEASE;
break;
default:
/* we don't call atk_key_event_from_nux_event_key if the event
is different to keydown or keyup */
g_assert_not_reached();
g_free(atk_event);
return NULL;
}
atk_event->state = translate_nux_modifiers_to_gdk_state(event->key_modifiers);
atk_event->keyval = event->x11_keysym;
atk_event->keycode = event->x11_keycode;
/* GDK applies the modifiers to the keyval, and ATK expects that, so
* we need to do this also here, as it is not done on the release */
success = gdk_keymap_get_entries_for_keyval(keymap, atk_event->keyval,
&keys, &n_keys);
success &= n_keys > 0;
if (success)
{
gint group;
guint new_keyval;
gint effective_group;
gint level;
GdkModifierType consumed;
group = keys [0].group;
success = gdk_keymap_translate_keyboard_state(keymap,
atk_event->keycode,
(GdkModifierType) atk_event->state,
group,
&new_keyval,
&effective_group,
&level,
&consumed);
if (success)
atk_event->keyval = new_keyval;
}
atk_event->string = NULL;
if (event->text && event->text[0])
{
key_unichar = g_utf8_get_char(event->text);
if (g_unichar_validate(key_unichar) && g_unichar_isgraph(key_unichar))
{
GString* new_string = NULL;
new_string = g_string_new("");
new_string = g_string_insert_unichar(new_string, 0, key_unichar);
atk_event->string = new_string->str;
g_string_free(new_string, FALSE);
}
}
/* If ->string is still NULL we compute it from the keyval*/
if (atk_event->string == NULL)
atk_event->string = g_strdup(gdk_keyval_name(atk_event->keyval));
/* e_x11_timestamp is zero, see bug LB#735645*/
atk_event->timestamp = g_get_real_time() / 1000;
#ifdef DEBUG_ANY_KEY_EVENT
g_debug("[a11y] AtkKeyEvent:\n\t\tsym 0x%x\n\t\tmods %x\n\t\tcode %u\n\t\ttime %lx \n\t\tstring %s\n",
(unsigned int) atk_event->keyval,
(unsigned int) atk_event->state,
(unsigned int) atk_event->keycode,
(unsigned long int) atk_event->timestamp,
atk_event->string);
#endif
return atk_event;
}
static int
unity_util_event_inspector(nux::Area* area,
nux::Event* event,
void* data)
{
GSList* list = NULL;
AtkKeyEventStruct* atk_key_event = NULL;
gint result = 0;
if ((event->type != nux::NUX_KEYDOWN) && (event->type != nux::NUX_KEYUP))
return 0;
atk_key_event = atk_key_event_from_nux_event_key(event);
for (list = key_listener_list; list; list = list->next)
{
UnityKeyEventListener* listener = (UnityKeyEventListener*) list->data;
result |= listener->func(atk_key_event, listener->data);
}
g_free(atk_key_event->string);
g_free(atk_key_event);
return result;
}
static guint
unity_util_accessible_add_key_event_listener(AtkKeySnoopFunc listener_func,
gpointer data)
{
static guint key = 0;
UnityKeyEventListener* listener;
if (event_inspector_id == 0)
{
if (unity_window_thread == NULL)
return 0;
event_inspector_id = unity_window_thread->InstallEventInspector(unity_util_event_inspector, NULL);
}
key++;
listener = g_slice_new0(UnityKeyEventListener);
listener->func = listener_func;
listener->data = data;
listener->key = key;
key_listener_list = g_slist_append(key_listener_list, listener);
return key;
}
static void
unity_util_accessible_remove_key_event_listener(guint remove_listener)
{
GSList* l;
for (l = key_listener_list; l; l = l->next)
{
UnityKeyEventListener* listener = (UnityKeyEventListener*) l->data;
if (listener->key == remove_listener)
{
g_slice_free(UnityKeyEventListener, listener);
key_listener_list = g_slist_delete_link(key_listener_list, l);
break;
}
}
if (key_listener_list == NULL)
{
if (unity_window_thread == NULL)
return;
unity_window_thread->RemoveEventInspector(event_inspector_id);
event_inspector_id = 0;
}
}
/* Public */
void
unity_util_accessible_set_window_thread(nux::WindowThread* wt)
{
unity_window_thread = wt;
}
/*
* FIXME: temporal solution
*
* Normally not all the accessible objects on the hierarchy are
* available from the beginning, and they are being created by demand
* due the request on the AT (ie: orca) side
*
* It usually follows a top-down approach. Top objects emits a signal
* of interest, so AT apps get interest on it, and request their
* children. One example is the signal "window::activate". AT receives
* a signal meaning that a top level object is activated, so request
* their children (and gran-children).
*
* Due technical reasons, right now it is hard to find a suitable way
* to emit the signal "activate" on the BaseWindow. That means that
* objects on the bottom of the hierarchy are not created, so Orca
* doesn't react to changes on sections like the Launcher.
*
* So in order to prevent that, we make a manual exploration of the
* hierarchy in order to ensure that those objects are there.
*
* NOTE: this manual exploration is not required with at-spi2, just
* with at-spi.
*
*/
void
explore_children(AtkObject* obj)
{
gint num = 0;
gint i = 0;
AtkObject* atk_child = NULL;
g_return_if_fail(ATK_IS_OBJECT(obj));
num = atk_object_get_n_accessible_children(obj);
for (i = 0; i < num; i++)
{
atk_child = atk_object_ref_accessible_child(obj, i);
explore_children(atk_child);
g_object_unref(atk_child);
}
}
|