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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2020 Colomban Wendling <cwendling@hypra.fr>
* Copyright (C) 2012-2021 MATE Developers
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "msd-a11y-keyboard-atspi.h"
#include <glib-object.h>
#include <gdk/gdk.h>
#include <atspi/atspi.h>
struct _MsdA11yKeyboardAtspi
{
GObject parent;
AtspiDeviceListener *listener;
gboolean listening;
};
G_DEFINE_TYPE (MsdA11yKeyboardAtspi, msd_a11y_keyboard_atspi, G_TYPE_OBJECT)
static void
msd_a11y_keyboard_atspi_finalize (GObject *obj)
{
MsdA11yKeyboardAtspi *self = MSD_A11Y_KEYBOARD_ATSPI (obj);
g_clear_object (&self->listener);
self->listening = FALSE;
G_OBJECT_CLASS (msd_a11y_keyboard_atspi_parent_class)->finalize (obj);
}
static void
msd_a11y_keyboard_atspi_class_init (MsdA11yKeyboardAtspiClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = msd_a11y_keyboard_atspi_finalize;
}
static gboolean
on_key_press_event (AtspiDeviceEvent *event,
void *user_data G_GNUC_UNUSED)
{
/* don't ring on capslock itself, that's taken care of by togglekeys
* if the user want it. */
if (event->id != GDK_KEY_Caps_Lock)
gdk_display_beep (gdk_display_get_default ());
/* cast the possible erroneous const away with atspi < 2.40 */
g_boxed_free (ATSPI_TYPE_DEVICE_EVENT, (gpointer) event);
return FALSE;
}
static void
msd_a11y_keyboard_atspi_init (MsdA11yKeyboardAtspi *self)
{
self->listener = NULL;
self->listening = FALSE;
#ifndef DESTROYING_ATSPI_LISTENER_DOES_NOT_CRASH
/* init AT-SPI if needed */
atspi_init ();
self->listener = atspi_device_listener_new (on_key_press_event,
self, NULL);
/* leak a reference so that this listener is *never* destroyed, to
* prevent the crash even if our object gets destroyed.
* See https://gitlab.gnome.org/GNOME/at-spi2-core/-/issues/22 */
g_object_ref (self->listener);
#endif
}
static void
register_deregister_events (MsdA11yKeyboardAtspi *self,
gboolean do_register)
{
g_return_if_fail (MSD_IS_A11Y_KEYBOARD_ATSPI (self));
g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (self->listener));
/* register listeners for all keys with CAPS_LOCK modifier */
for (AtspiKeyMaskType mod_mask = 0; mod_mask < 256; mod_mask++)
{
if (! (mod_mask & (1 << ATSPI_MODIFIER_SHIFTLOCK)))
continue;
if (do_register)
atspi_register_keystroke_listener (self->listener,
NULL,
mod_mask,
1 << ATSPI_KEY_PRESSED_EVENT,
ATSPI_KEYLISTENER_NOSYNC,
NULL);
else
atspi_deregister_keystroke_listener (self->listener,
NULL,
mod_mask,
1 << ATSPI_KEY_PRESSED_EVENT,
NULL);
}
}
void
msd_a11y_keyboard_atspi_start (MsdA11yKeyboardAtspi *self)
{
g_return_if_fail (MSD_IS_A11Y_KEYBOARD_ATSPI (self));
if (self->listening)
return;
#ifdef DESTROYING_ATSPI_LISTENER_DOES_NOT_CRASH
/* init AT-SPI if needed */
atspi_init ();
self->listener = atspi_device_listener_new (on_key_press_event,
self, NULL);
#endif
register_deregister_events (self, TRUE);
self->listening = TRUE;
}
void
msd_a11y_keyboard_atspi_stop (MsdA11yKeyboardAtspi *self)
{
g_return_if_fail (MSD_IS_A11Y_KEYBOARD_ATSPI (self));
if (! self->listening)
return;
#ifdef DESTROYING_ATSPI_LISTENER_DOES_NOT_CRASH
g_clear_object (&self->listener);
#else
register_deregister_events (self, FALSE);
#endif
self->listening = FALSE;
}
MsdA11yKeyboardAtspi *
msd_a11y_keyboard_atspi_new ()
{
return g_object_new (MSD_TYPE_A11Y_KEYBOARD_ATSPI, NULL);
}
|