summaryrefslogtreecommitdiff
path: root/atspi/atspi-device-legacy.c
blob: 84cc2a29cca6f7ac498c9519ab452189487a3a69 (plain)
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
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2020 SUSE LLC.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "atspi-device-legacy.h"
#include "atspi-private.h"

#ifdef HAVE_X11
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XInput2.h>
#endif

typedef struct
{
  guint keycode;
  guint modifier;
} AtspiLegacyKeyModifier;

typedef struct _AtspiDeviceLegacyPrivate AtspiDeviceLegacyPrivate;
struct _AtspiDeviceLegacyPrivate
{
  AtspiDeviceListener *listener;
#ifdef HAVE_X11
  Display *display;
  Window window;
#endif
  GSList *modifiers;
  guint virtual_mods_enabled;
  gboolean keyboard_grabbed;
  unsigned int numlock_physical_mask;
};

GObjectClass *device_legacy_parent_class;

G_DEFINE_TYPE_WITH_CODE (AtspiDeviceLegacy, atspi_device_legacy, ATSPI_TYPE_DEVICE, G_ADD_PRIVATE (AtspiDeviceLegacy))

static guint
find_virtual_mapping (AtspiDeviceLegacy *legacy_device, gint keycode)
{
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  GSList *l;

  for (l = priv->modifiers; l; l = l->next)
    {
      AtspiLegacyKeyModifier *entry = l->data;
      if (entry->keycode == keycode)
        return entry->modifier;
    }

  return 0;
}

static void
set_virtual_modifier (AtspiDeviceLegacy *legacy_device, gint keycode, gboolean enabled)
{
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  guint modifier = find_virtual_mapping (legacy_device, keycode);

  if (enabled)
    priv->virtual_mods_enabled |= modifier;
  else
    priv->virtual_mods_enabled &= ~modifier;
}

static gboolean
key_cb (AtspiDeviceEvent *event, void *user_data)
{
  AtspiDeviceLegacy *legacy_device = ATSPI_DEVICE_LEGACY (user_data);
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  gboolean ret = priv->keyboard_grabbed;
  guint modifiers;

  g_object_ref (legacy_device);
  set_virtual_modifier (legacy_device, event->hw_code,
                        event->type == (AtspiEventType) ATSPI_KEY_PRESS);

  modifiers = event->modifiers | priv->virtual_mods_enabled;
  if (modifiers & (1 << ATSPI_MODIFIER_NUMLOCK))
    modifiers &= ~priv->numlock_physical_mask;

  ret |= atspi_device_notify_key (ATSPI_DEVICE (legacy_device),
                                  event->type == (AtspiEventType) ATSPI_KEY_PRESS,
                                  event->hw_code, event->id,
                                  modifiers,
                                  event->event_string);

  g_boxed_free (ATSPI_TYPE_DEVICE_EVENT, event);
  g_object_unref (legacy_device);
  return ret;
}

static guint
atspi_device_legacy_get_locked_modifiers (AtspiDevice *device)
{
#ifdef HAVE_X11
  AtspiDeviceLegacy *legacy_device = ATSPI_DEVICE_LEGACY (device);
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  XkbStateRec state_rec;

  memset (&state_rec, 0, sizeof (state_rec));
  XkbGetState (priv->display, XkbUseCoreKbd, &state_rec);
  return state_rec.locked_mods;
#else
  return 0;
#endif
}

static gboolean
check_virtual_modifier (AtspiDeviceLegacy *legacy_device, guint modifier)
{
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  GSList *l;

  if (modifier == (1 << ATSPI_MODIFIER_NUMLOCK))
    return TRUE;

  for (l = priv->modifiers; l; l = l->next)
    {
      AtspiLegacyKeyModifier *entry = l->data;
      if (entry->modifier == modifier)
        return TRUE;
    }

  return FALSE;
}

static guint
get_unused_virtual_modifier (AtspiDeviceLegacy *legacy_device)
{
  guint ret = 0x1000;

  while (ret < 0x10000)
    {
      if (!check_virtual_modifier (legacy_device, ret))
        return ret;
      ret <<= 1;
    }

  return 0;
}

static guint
atspi_device_legacy_map_modifier (AtspiDevice *device, gint keycode)
{
  AtspiDeviceLegacy *legacy_device = ATSPI_DEVICE_LEGACY (device);
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  guint ret;
  AtspiLegacyKeyModifier *entry;
#ifdef HAVE_X11
  XkbDescPtr desc;

  desc = XkbGetMap (priv->display, XkbModifierMapMask, XkbUseCoreKbd);

  if (keycode < desc->min_key_code || keycode >= desc->max_key_code)
    {
      XkbFreeKeyboard (desc, XkbModifierMapMask, TRUE);
      g_warning ("Passed invalid keycode %d", keycode);
      return 0;
    }

  ret = desc->map->modmap[keycode];
  XkbFreeKeyboard (desc, XkbModifierMapMask, TRUE);
  if (ret & (ShiftMask | ControlMask))
    return ret;
#endif

  ret = find_virtual_mapping (legacy_device, keycode);
  if (ret)
    return ret;

  ret = get_unused_virtual_modifier (legacy_device);

  entry = g_new (AtspiLegacyKeyModifier, 1);
  entry->keycode = keycode;
  entry->modifier = ret;
  priv->modifiers = g_slist_append (priv->modifiers, entry);

  return ret;
}

static void
atspi_device_legacy_unmap_modifier (AtspiDevice *device, gint keycode)
{
  AtspiDeviceLegacy *legacy_device = ATSPI_DEVICE_LEGACY (device);
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  GSList *l;

  for (l = priv->modifiers; l; l = l->next)
    {
      AtspiLegacyKeyModifier *entry = l->data;
      if (entry->keycode == keycode)
        {
          priv->modifiers = g_slist_remove (priv->modifiers, entry);
          g_free (entry);
          return;
        }
    }
}

static guint
atspi_device_legacy_get_modifier (AtspiDevice *device, gint keycode)
{
  AtspiDeviceLegacy *legacy_device = ATSPI_DEVICE_LEGACY (device);
#ifdef HAVE_X11
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);
  XkbDescPtr desc;
  guint ret;

  desc = XkbGetMap (priv->display, XkbModifierMapMask, XkbUseCoreKbd);

  if (keycode < desc->min_key_code || keycode >= desc->max_key_code)
    {
      XkbFreeKeyboard (desc, XkbModifierMapMask, TRUE);
      g_warning ("Passed invalid keycode %d", keycode);
      return 0;
    }

  ret = desc->map->modmap[keycode];
  XkbFreeKeyboard (desc, XkbModifierMapMask, TRUE);
  if (ret)
    return ret;
#endif

  return find_virtual_mapping (legacy_device, keycode);
}

static gboolean
atspi_device_legacy_grab_keyboard (AtspiDevice *device)
{
  AtspiDeviceLegacy *legacy_device = ATSPI_DEVICE_LEGACY (device);
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);

  priv->keyboard_grabbed = TRUE;
  return TRUE;
}

static void
atspi_device_legacy_ungrab_keyboard (AtspiDevice *device)
{
  AtspiDeviceLegacy *legacy_device = ATSPI_DEVICE_LEGACY (device);
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (legacy_device);

  priv->keyboard_grabbed = FALSE;
}

static void
atspi_device_legacy_init (AtspiDeviceLegacy *device)
{
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (device);
  gint i;

  priv->listener = atspi_device_listener_new (key_cb, device, NULL);
  for (i = 0; i < 256; i++)
    atspi_register_keystroke_listener (priv->listener, NULL, i, 3, ATSPI_KEYLISTENER_SYNCHRONOUS | ATSPI_KEYLISTENER_CANCONSUME, NULL);

#ifdef HAVE_X11
  priv->display = XOpenDisplay ("");
  if (priv->display)
    priv->window = DefaultRootWindow (priv->display);
  priv->numlock_physical_mask = XkbKeysymToModifiers (priv->display,
                                                      XK_Num_Lock);
#endif
}

static void
atspi_device_legacy_finalize (GObject *object)
{
  AtspiDeviceLegacy *device = ATSPI_DEVICE_LEGACY (object);
  AtspiDeviceLegacyPrivate *priv = atspi_device_legacy_get_instance_private (device);

  g_clear_object (&priv->listener);
  device_legacy_parent_class->finalize (object);
}

static void
atspi_device_legacy_class_init (AtspiDeviceLegacyClass *klass)
{
  GObjectClass *object_class = (GObjectClass *) klass;
  AtspiDeviceClass *device_class = ATSPI_DEVICE_CLASS (klass);

  device_legacy_parent_class = g_type_class_peek_parent (klass);
  object_class->finalize = atspi_device_legacy_finalize;
  device_class->map_modifier = atspi_device_legacy_map_modifier;
  device_class->unmap_modifier = atspi_device_legacy_unmap_modifier;
  device_class->get_modifier = atspi_device_legacy_get_modifier;
  device_class->get_locked_modifiers = atspi_device_legacy_get_locked_modifiers;
  device_class->grab_keyboard = atspi_device_legacy_grab_keyboard;
  device_class->ungrab_keyboard = atspi_device_legacy_ungrab_keyboard;
}

/**
 * atspi_device_legacy_new:
 *
 * Creates a new #AtspiDeviceLegacy.
 *
 * Returns: (transfer full): a pointer to a newly-created #AtspiDeviceLegacy.
 *
 **/
AtspiDeviceLegacy *
atspi_device_legacy_new ()
{
  AtspiDeviceLegacy *device = g_object_new (atspi_device_legacy_get_type (), NULL);

  return device;
}