4
* Copyright (C) 2013 Intel Corporation
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License as
8
* published by the Free Software Foundation; either version 2 of the
9
* License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23
* Copyright © 2010-2011 Intel Corporation
24
* Copyright © 2008-2011 Kristian Høgsberg
25
* Copyright © 2012 Collabora, Ltd.
27
* Permission to use, copy, modify, distribute, and sell this software and
28
* its documentation for any purpose is hereby granted without fee, provided
29
* that the above copyright notice appear in all copies and that both that
30
* copyright notice and this permission notice appear in supporting
31
* documentation, and that the name of the copyright holders not be used in
32
* advertising or publicity pertaining to distribution of the software
33
* without specific, written prior permission. The copyright holders make
34
* no representations about the suitability of this software for any
35
* purpose. It is provided "as is" without express or implied warranty.
37
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
38
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
39
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
40
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
41
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
42
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
43
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46
/* The file is based on src/input.c from Weston */
57
#include <clutter/evdev/clutter-evdev.h>
59
#include "display-private.h"
60
#include "backends/meta-backend-private.h"
62
#include "meta-wayland-private.h"
64
#ifdef HAVE_NATIVE_BACKEND
65
#include "backends/native/meta-backend-native.h"
68
#define GSD_KEYBOARD_SCHEMA "org.gnome.settings-daemon.peripherals.keyboard"
71
GSD_KEYBOARD_NUM_LOCK_STATE_UNKNOWN,
72
GSD_KEYBOARD_NUM_LOCK_STATE_ON,
73
GSD_KEYBOARD_NUM_LOCK_STATE_OFF
74
} GsdKeyboardNumLockState;
76
G_DEFINE_TYPE (MetaWaylandKeyboard, meta_wayland_keyboard,
77
META_TYPE_WAYLAND_INPUT_DEVICE)
79
static void meta_wayland_keyboard_update_xkb_state (MetaWaylandKeyboard *keyboard);
80
static void meta_wayland_keyboard_set_numlock (MetaWaylandKeyboard *keyboard,
81
gboolean numlock_state);
82
static void notify_modifiers (MetaWaylandKeyboard *keyboard);
83
static guint evdev_code (const ClutterKeyEvent *event);
86
unbind_resource (struct wl_resource *resource)
88
wl_list_remove (wl_resource_get_link (resource));
92
create_anonymous_file (off_t size,
95
static const char template[] = "mutter-shared-XXXXXX";
99
fd = g_file_open_tmp (template, &path, error);
107
flags = fcntl (fd, F_GETFD);
111
if (fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
114
if (ftruncate (fd, size) < 0)
120
g_set_error_literal (error,
122
g_file_error_from_errno (errno),
130
inform_clients_of_new_keymap (MetaWaylandKeyboard *keyboard)
132
struct wl_resource *keyboard_resource;
134
wl_resource_for_each (keyboard_resource, &keyboard->resource_list)
136
wl_keyboard_send_keymap (keyboard_resource,
137
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
138
keyboard->xkb_info.keymap_fd,
139
keyboard->xkb_info.keymap_size);
141
wl_resource_for_each (keyboard_resource, &keyboard->focus_resource_list)
143
wl_keyboard_send_keymap (keyboard_resource,
144
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
145
keyboard->xkb_info.keymap_fd,
146
keyboard->xkb_info.keymap_size);
151
meta_wayland_keyboard_take_keymap (MetaWaylandKeyboard *keyboard,
152
struct xkb_keymap *keymap)
154
MetaWaylandXkbInfo *xkb_info = &keyboard->xkb_info;
155
GError *error = NULL;
157
size_t previous_size;
161
g_warning ("Attempting to set null keymap (compilation probably failed)");
165
xkb_keymap_unref (xkb_info->keymap);
166
xkb_info->keymap = xkb_keymap_ref (keymap);
168
meta_wayland_keyboard_update_xkb_state (keyboard);
170
keymap_str = xkb_map_get_as_string (xkb_info->keymap);
171
if (keymap_str == NULL)
173
g_warning ("failed to get string version of keymap");
176
previous_size = xkb_info->keymap_size;
177
xkb_info->keymap_size = strlen (keymap_str) + 1;
179
if (xkb_info->keymap_fd >= 0)
180
close (xkb_info->keymap_fd);
182
xkb_info->keymap_fd = create_anonymous_file (xkb_info->keymap_size, &error);
183
if (xkb_info->keymap_fd < 0)
185
g_warning ("creating a keymap file for %lu bytes failed: %s",
186
(unsigned long) xkb_info->keymap_size,
188
g_clear_error (&error);
192
if (xkb_info->keymap_area)
193
munmap (xkb_info->keymap_area, previous_size);
195
xkb_info->keymap_area = mmap (NULL, xkb_info->keymap_size,
196
PROT_READ | PROT_WRITE,
197
MAP_SHARED, xkb_info->keymap_fd, 0);
198
if (xkb_info->keymap_area == MAP_FAILED)
200
g_warning ("failed to mmap() %lu bytes\n",
201
(unsigned long) xkb_info->keymap_size);
204
strcpy (xkb_info->keymap_area, keymap_str);
207
inform_clients_of_new_keymap (keyboard);
209
notify_modifiers (keyboard);
214
close (xkb_info->keymap_fd);
215
xkb_info->keymap_fd = -1;
221
static xkb_mod_mask_t
222
kbd_a11y_apply_mask (MetaWaylandKeyboard *keyboard)
224
xkb_mod_mask_t latched, locked, depressed, group;
225
xkb_mod_mask_t update_mask = 0;
227
depressed = xkb_state_serialize_mods(keyboard->xkb_info.state, XKB_STATE_DEPRESSED);
228
latched = xkb_state_serialize_mods (keyboard->xkb_info.state, XKB_STATE_MODS_LATCHED);
229
locked = xkb_state_serialize_mods (keyboard->xkb_info.state, XKB_STATE_MODS_LOCKED);
230
group = xkb_state_serialize_layout (keyboard->xkb_info.state, XKB_STATE_LAYOUT_EFFECTIVE);
232
if ((latched & keyboard->kbd_a11y_latched_mods) != keyboard->kbd_a11y_latched_mods)
233
update_mask |= XKB_STATE_MODS_LATCHED;
235
if ((locked & keyboard->kbd_a11y_locked_mods) != keyboard->kbd_a11y_locked_mods)
236
update_mask |= XKB_STATE_MODS_LOCKED;
240
latched |= keyboard->kbd_a11y_latched_mods;
241
locked |= keyboard->kbd_a11y_locked_mods;
242
xkb_state_update_mask (keyboard->xkb_info.state, depressed, latched, locked, 0, 0, group);
249
on_keymap_changed (MetaBackend *backend,
252
MetaWaylandKeyboard *keyboard = data;
254
meta_wayland_keyboard_take_keymap (keyboard, meta_backend_get_keymap (backend));
258
on_keymap_layout_group_changed (MetaBackend *backend,
262
MetaWaylandKeyboard *keyboard = data;
263
xkb_mod_mask_t depressed_mods;
264
xkb_mod_mask_t latched_mods;
265
xkb_mod_mask_t locked_mods;
266
struct xkb_state *state;
268
state = keyboard->xkb_info.state;
270
depressed_mods = xkb_state_serialize_mods (state, XKB_STATE_MODS_DEPRESSED);
271
latched_mods = xkb_state_serialize_mods (state, XKB_STATE_MODS_LATCHED);
272
locked_mods = xkb_state_serialize_mods (state, XKB_STATE_MODS_LOCKED);
274
xkb_state_update_mask (state, depressed_mods, latched_mods, locked_mods, 0, 0, idx);
275
kbd_a11y_apply_mask (keyboard);
277
notify_modifiers (keyboard);
281
keyboard_handle_focus_surface_destroy (struct wl_listener *listener, void *data)
283
MetaWaylandKeyboard *keyboard = wl_container_of (listener, keyboard,
284
focus_surface_listener);
286
meta_wayland_keyboard_set_focus (keyboard, NULL);
290
meta_wayland_keyboard_broadcast_key (MetaWaylandKeyboard *keyboard,
295
struct wl_resource *resource;
297
if (!wl_list_empty (&keyboard->focus_resource_list))
299
MetaWaylandInputDevice *input_device =
300
META_WAYLAND_INPUT_DEVICE (keyboard);
302
keyboard->key_serial =
303
meta_wayland_input_device_next_serial (input_device);
305
wl_resource_for_each (resource, &keyboard->focus_resource_list)
307
wl_keyboard_send_key (resource, keyboard->key_serial, time, key, state);
311
/* Eat the key events if we have a focused surface. */
312
return (keyboard->focus_surface != NULL);
316
notify_key (MetaWaylandKeyboard *keyboard,
317
const ClutterEvent *event)
319
return keyboard->grab->interface->key (keyboard->grab, event);
322
static xkb_mod_mask_t
323
add_vmod (xkb_mod_mask_t mask,
326
xkb_mod_mask_t *added)
328
if ((mask & mod) && !(mod & *added))
336
static xkb_mod_mask_t
337
add_virtual_mods (xkb_mod_mask_t mask)
339
MetaKeyBindingManager *keys = &(meta_get_display ()->key_binding_manager);
340
xkb_mod_mask_t added;
342
/* Order is important here: if multiple vmods share the same real
343
modifier we only want to add the first. */
348
{ keys->super_mask, keys->virtual_super_mask },
349
{ keys->hyper_mask, keys->virtual_hyper_mask },
350
{ keys->meta_mask, keys->virtual_meta_mask },
354
for (i = 0; i < G_N_ELEMENTS (mods); ++i)
355
mask = add_vmod (mask, mods[i].mod, mods[i].vmod, &added);
361
keyboard_send_modifiers (MetaWaylandKeyboard *keyboard,
362
struct wl_resource *resource,
365
struct xkb_state *state = keyboard->xkb_info.state;
366
xkb_mod_mask_t depressed, latched, locked;
368
depressed = add_virtual_mods (xkb_state_serialize_mods (state, XKB_STATE_MODS_DEPRESSED));
369
latched = add_virtual_mods (xkb_state_serialize_mods (state, XKB_STATE_MODS_LATCHED));
370
locked = add_virtual_mods (xkb_state_serialize_mods (state, XKB_STATE_MODS_LOCKED));
372
wl_keyboard_send_modifiers (resource, serial, depressed, latched, locked,
373
xkb_state_serialize_layout (state, XKB_STATE_LAYOUT_EFFECTIVE));
377
meta_wayland_keyboard_broadcast_modifiers (MetaWaylandKeyboard *keyboard)
379
struct wl_resource *resource;
381
if (!wl_list_empty (&keyboard->focus_resource_list))
383
MetaWaylandInputDevice *input_device =
384
META_WAYLAND_INPUT_DEVICE (keyboard);
387
serial = meta_wayland_input_device_next_serial (input_device);
389
wl_resource_for_each (resource, &keyboard->focus_resource_list)
390
keyboard_send_modifiers (keyboard, resource, serial);
395
notify_modifiers (MetaWaylandKeyboard *keyboard)
397
struct xkb_state *state;
399
state = keyboard->xkb_info.state;
400
keyboard->grab->interface->modifiers (keyboard->grab,
401
xkb_state_serialize_mods (state, XKB_STATE_MODS_EFFECTIVE));
405
numlock_set_xkb_state (MetaWaylandKeyboard *keyboard,
406
GsdKeyboardNumLockState state)
408
MetaBackend *backend = meta_get_backend ();
409
gboolean numlock_state;
411
if (state != GSD_KEYBOARD_NUM_LOCK_STATE_ON &&
412
state != GSD_KEYBOARD_NUM_LOCK_STATE_OFF)
415
numlock_state = (state == GSD_KEYBOARD_NUM_LOCK_STATE_ON);
416
meta_verbose ("set numlock state %s\n", (numlock_state ? "ON" : "OFF"));
417
meta_backend_set_numlock (backend, numlock_state);
418
meta_wayland_keyboard_set_numlock (keyboard, numlock_state);
422
maybe_restore_numlock_state (MetaWaylandKeyboard *keyboard)
424
gboolean remember_numlock;
426
if (!keyboard->gsd_settings)
429
/* We are cheating for now, we use g-s-d settings... */
430
remember_numlock = g_settings_get_boolean (keyboard->gsd_settings,
431
"remember-numlock-state");
433
if (remember_numlock)
435
GsdKeyboardNumLockState state;
437
state = g_settings_get_enum (keyboard->gsd_settings, "numlock-state");
438
numlock_set_xkb_state (keyboard, state);
443
maybe_save_numlock_state (MetaWaylandKeyboard *keyboard)
445
#ifdef HAVE_NATIVE_BACKEND
446
MetaWaylandXkbInfo *xkb_info = &keyboard->xkb_info;
447
GsdKeyboardNumLockState numlock_state;
450
if (!META_IS_BACKEND_NATIVE (meta_get_backend ()))
453
if (!xkb_info->state)
456
if (!keyboard->gsd_settings)
459
if (!g_settings_get_boolean (keyboard->gsd_settings, "remember-numlock-state"))
462
numlock_active = xkb_state_mod_name_is_active(xkb_info->state,
464
XKB_STATE_MODS_LOCKED);
465
switch (numlock_active)
468
numlock_state = GSD_KEYBOARD_NUM_LOCK_STATE_UNKNOWN;
471
numlock_state = GSD_KEYBOARD_NUM_LOCK_STATE_OFF;
474
numlock_state = GSD_KEYBOARD_NUM_LOCK_STATE_ON;
477
g_settings_set_enum (keyboard->gsd_settings, "numlock-state", numlock_state);
482
meta_wayland_keyboard_set_numlock (MetaWaylandKeyboard *keyboard,
483
gboolean numlock_state)
485
MetaWaylandXkbInfo *xkb_info = &keyboard->xkb_info;
486
xkb_mod_mask_t latched, locked, group, depressed;
487
xkb_mod_mask_t numlock;
489
meta_verbose ("backend numlock state %s\n", (numlock_state ? "ON" : "OFF"));
491
latched = xkb_state_serialize_mods (xkb_info->state, XKB_STATE_MODS_LATCHED);
492
locked = xkb_state_serialize_mods (xkb_info->state, XKB_STATE_MODS_LOCKED);
493
group = xkb_state_serialize_layout (xkb_info->state, XKB_STATE_LAYOUT_EFFECTIVE);
494
depressed = xkb_state_serialize_mods(xkb_info->state, XKB_STATE_DEPRESSED);
495
numlock = (1 << xkb_keymap_mod_get_index(xkb_info->keymap, "Mod2"));
497
if (numlock_state == TRUE)
502
xkb_state_update_mask (xkb_info->state, depressed, latched, locked, 0, 0, group);
503
kbd_a11y_apply_mask (keyboard);
505
notify_modifiers (keyboard);
509
meta_wayland_keyboard_update_xkb_state (MetaWaylandKeyboard *keyboard)
511
MetaWaylandXkbInfo *xkb_info = &keyboard->xkb_info;
512
xkb_mod_mask_t latched, locked;
513
MetaBackend *backend = meta_get_backend ();
514
xkb_layout_index_t layout_idx;
516
/* Preserve latched/locked modifiers state */
519
latched = xkb_state_serialize_mods (xkb_info->state, XKB_STATE_MODS_LATCHED);
520
locked = xkb_state_serialize_mods (xkb_info->state, XKB_STATE_MODS_LOCKED);
521
xkb_state_unref (xkb_info->state);
525
latched = locked = 0;
528
xkb_info->state = xkb_state_new (xkb_info->keymap);
530
layout_idx = meta_backend_get_keymap_layout_group (backend);
531
xkb_state_update_mask (xkb_info->state, 0, latched, locked, 0, 0, layout_idx);
533
kbd_a11y_apply_mask (keyboard);
537
on_kbd_a11y_mask_changed (ClutterDeviceManager *device_manager,
538
xkb_mod_mask_t new_latched_mods,
539
xkb_mod_mask_t new_locked_mods,
540
MetaWaylandKeyboard *keyboard)
542
xkb_mod_mask_t latched, locked, depressed, group;
544
if (keyboard->xkb_info.state == NULL)
547
depressed = xkb_state_serialize_mods(keyboard->xkb_info.state, XKB_STATE_DEPRESSED);
548
latched = xkb_state_serialize_mods (keyboard->xkb_info.state, XKB_STATE_MODS_LATCHED);
549
locked = xkb_state_serialize_mods (keyboard->xkb_info.state, XKB_STATE_MODS_LOCKED);
550
group = xkb_state_serialize_layout (keyboard->xkb_info.state, XKB_STATE_LAYOUT_EFFECTIVE);
552
/* Clear previous masks */
553
latched &= ~keyboard->kbd_a11y_latched_mods;
554
locked &= ~keyboard->kbd_a11y_locked_mods;
555
xkb_state_update_mask (keyboard->xkb_info.state, depressed, latched, locked, 0, 0, group);
557
/* Apply new masks */
558
keyboard->kbd_a11y_latched_mods = new_latched_mods;
559
keyboard->kbd_a11y_locked_mods = new_locked_mods;
560
kbd_a11y_apply_mask (keyboard);
562
notify_modifiers (keyboard);
566
notify_key_repeat_for_resource (MetaWaylandKeyboard *keyboard,
567
struct wl_resource *keyboard_resource)
569
if (wl_resource_get_version (keyboard_resource) >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION)
572
unsigned int delay, rate;
574
repeat = g_settings_get_boolean (keyboard->settings, "repeat");
578
unsigned int interval;
579
interval = g_settings_get_uint (keyboard->settings, "repeat-interval");
580
/* Our setting is in the milliseconds between keys. "rate" is the number
581
* of keys per second. */
583
rate = (1000 / interval);
587
delay = g_settings_get_uint (keyboard->settings, "delay");
595
wl_keyboard_send_repeat_info (keyboard_resource, rate, delay);
600
notify_key_repeat (MetaWaylandKeyboard *keyboard)
602
struct wl_resource *keyboard_resource;
604
wl_resource_for_each (keyboard_resource, &keyboard->resource_list)
606
notify_key_repeat_for_resource (keyboard, keyboard_resource);
609
wl_resource_for_each (keyboard_resource, &keyboard->focus_resource_list)
611
notify_key_repeat_for_resource (keyboard, keyboard_resource);
616
remember_numlock_state_changed (GSettings *settings,
620
MetaWaylandKeyboard *keyboard = data;
622
maybe_save_numlock_state (keyboard);
626
settings_changed (GSettings *settings,
630
MetaWaylandKeyboard *keyboard = data;
632
notify_key_repeat (keyboard);
636
default_grab_key (MetaWaylandKeyboardGrab *grab,
637
const ClutterEvent *event)
639
MetaWaylandKeyboard *keyboard = grab->keyboard;
640
gboolean is_press = event->type == CLUTTER_KEY_PRESS;
642
#ifdef HAVE_NATIVE_BACKEND
643
MetaBackend *backend = meta_get_backend ();
646
/* Synthetic key events are for autorepeat. Ignore those, as
647
* autorepeat in Wayland is done on the client side. */
648
if ((event->key.flags & CLUTTER_EVENT_FLAG_SYNTHETIC) &&
649
!(event->key.flags & CLUTTER_EVENT_FLAG_INPUT_METHOD))
652
#ifdef HAVE_NATIVE_BACKEND
653
if (META_IS_BACKEND_NATIVE (backend))
654
code = clutter_evdev_event_get_event_code (event);
657
code = evdev_code (&event->key);
659
return meta_wayland_keyboard_broadcast_key (keyboard, event->key.time,
664
default_grab_modifiers (MetaWaylandKeyboardGrab *grab,
665
ClutterModifierType modifiers)
667
meta_wayland_keyboard_broadcast_modifiers (grab->keyboard);
670
static const MetaWaylandKeyboardGrabInterface default_keyboard_grab_interface = {
672
default_grab_modifiers
676
meta_wayland_keyboard_enable (MetaWaylandKeyboard *keyboard)
678
MetaBackend *backend = meta_get_backend ();
679
GSettingsSchema *schema;
681
keyboard->settings = g_settings_new ("org.gnome.desktop.peripherals.keyboard");
682
g_signal_connect (keyboard->settings, "changed",
683
G_CALLBACK (settings_changed), keyboard);
685
/* We are cheating for now, we use g-s-d settings... Check if available */
686
schema = g_settings_schema_source_lookup (g_settings_schema_source_get_default (),
691
keyboard->gsd_settings = g_settings_new_full (schema, NULL, NULL);
692
g_settings_schema_unref (schema);
693
g_signal_connect (keyboard->gsd_settings, "changed::remember-numlock-state",
694
G_CALLBACK (remember_numlock_state_changed), keyboard);
697
g_signal_connect (backend, "keymap-changed",
698
G_CALLBACK (on_keymap_changed), keyboard);
699
g_signal_connect (backend, "keymap-layout-group-changed",
700
G_CALLBACK (on_keymap_layout_group_changed), keyboard);
702
g_signal_connect (clutter_device_manager_get_default (), "kbd-a11y-mods-state-changed",
703
G_CALLBACK (on_kbd_a11y_mask_changed), keyboard);
705
meta_wayland_keyboard_take_keymap (keyboard, meta_backend_get_keymap (backend));
707
maybe_restore_numlock_state (keyboard);
711
meta_wayland_xkb_info_init (MetaWaylandXkbInfo *xkb_info)
713
xkb_info->keymap_fd = -1;
717
meta_wayland_xkb_info_destroy (MetaWaylandXkbInfo *xkb_info)
719
g_clear_pointer (&xkb_info->keymap, xkb_keymap_unref);
720
g_clear_pointer (&xkb_info->state, xkb_state_unref);
722
if (xkb_info->keymap_area)
724
munmap (xkb_info->keymap_area, xkb_info->keymap_size);
725
xkb_info->keymap_area = NULL;
727
if (xkb_info->keymap_fd >= 0)
729
close (xkb_info->keymap_fd);
730
xkb_info->keymap_fd = -1;
735
meta_wayland_keyboard_disable (MetaWaylandKeyboard *keyboard)
737
MetaBackend *backend = meta_get_backend ();
739
g_signal_handlers_disconnect_by_func (backend, on_keymap_changed, keyboard);
740
g_signal_handlers_disconnect_by_func (backend, on_keymap_layout_group_changed, keyboard);
742
meta_wayland_keyboard_end_grab (keyboard);
743
meta_wayland_keyboard_set_focus (keyboard, NULL);
744
meta_wayland_xkb_info_destroy (&keyboard->xkb_info);
746
wl_list_remove (&keyboard->resource_list);
747
wl_list_init (&keyboard->resource_list);
748
wl_list_remove (&keyboard->focus_resource_list);
749
wl_list_init (&keyboard->focus_resource_list);
751
g_clear_object (&keyboard->settings);
752
if (keyboard->gsd_settings)
753
g_object_unref (keyboard->gsd_settings);
757
evdev_code (const ClutterKeyEvent *event)
759
/* clutter-xkb-utils.c adds a fixed offset of 8 to go into XKB's
760
* range, so we do the reverse here. */
761
return event->hardware_keycode - 8;
765
meta_wayland_keyboard_update (MetaWaylandKeyboard *keyboard,
766
const ClutterKeyEvent *event)
768
gboolean is_press = event->type == CLUTTER_KEY_PRESS;
770
/* If we get a key event but still have pending modifier state
771
* changes from a previous event that didn't get cleared, we need to
772
* send that state right away so that the new key event can be
773
* interpreted by clients correctly modified. */
774
if (keyboard->mods_changed)
775
notify_modifiers (keyboard);
777
keyboard->mods_changed = xkb_state_update_key (keyboard->xkb_info.state,
778
event->hardware_keycode,
779
is_press ? XKB_KEY_DOWN : XKB_KEY_UP);
780
keyboard->mods_changed |= kbd_a11y_apply_mask (keyboard);
784
meta_wayland_keyboard_handle_event (MetaWaylandKeyboard *keyboard,
785
const ClutterKeyEvent *event)
787
gboolean is_press = event->type == CLUTTER_KEY_PRESS;
790
/* Synthetic key events are for autorepeat. Ignore those, as
791
* autorepeat in Wayland is done on the client side. */
792
if ((event->flags & CLUTTER_EVENT_FLAG_SYNTHETIC) &&
793
!(event->flags & CLUTTER_EVENT_FLAG_INPUT_METHOD))
796
meta_verbose ("Handling key %s event code %d\n",
797
is_press ? "press" : "release",
798
event->hardware_keycode);
800
handled = notify_key (keyboard, (const ClutterEvent *) event);
803
meta_verbose ("Sent event to wayland client\n");
805
meta_verbose ("No wayland surface is focused, continuing normal operation\n");
807
if (keyboard->mods_changed != 0)
809
if (keyboard->mods_changed & XKB_STATE_MODS_LOCKED)
810
maybe_save_numlock_state (keyboard);
811
notify_modifiers (keyboard);
812
keyboard->mods_changed = 0;
819
meta_wayland_keyboard_update_key_state (MetaWaylandKeyboard *keyboard,
824
gboolean mods_changed = FALSE;
827
for (i = offset; i < key_vector_len * 8; i++)
829
gboolean set = (key_vector[i/8] & (1 << (i % 8))) != 0;
831
/* The 'offset' parameter allows the caller to have the indices
832
* into key_vector to either be X-style (base 8) or evdev (base 0), or
833
* something else (unlikely). We subtract 'offset' to convert to evdev
834
* style, then add 8 to convert the "evdev" style keycode back to
835
* the X-style that xkbcommon expects.
837
mods_changed |= xkb_state_update_key (keyboard->xkb_info.state,
839
set ? XKB_KEY_DOWN : XKB_KEY_UP);
842
mods_changed |= kbd_a11y_apply_mask (keyboard);
844
notify_modifiers (keyboard);
848
move_resources (struct wl_list *destination, struct wl_list *source)
850
wl_list_insert_list (destination, source);
851
wl_list_init (source);
855
move_resources_for_client (struct wl_list *destination,
856
struct wl_list *source,
857
struct wl_client *client)
859
struct wl_resource *resource, *tmp;
860
wl_resource_for_each_safe (resource, tmp, source)
862
if (wl_resource_get_client (resource) == client)
864
wl_list_remove (wl_resource_get_link (resource));
865
wl_list_insert (destination, wl_resource_get_link (resource));
871
broadcast_focus (MetaWaylandKeyboard *keyboard,
872
struct wl_resource *resource)
874
struct wl_array fake_keys;
876
/* We never want to send pressed keys to wayland clients on
877
* enter. The protocol says that we should send them, presumably so
878
* that clients can trigger their own key repeat routine in case
879
* they are given focus and a key is physically pressed.
881
* Unfortunately this causes some clients, in particular Xwayland,
882
* to register key events that they really shouldn't handle,
883
* e.g. on an Alt+Tab keybinding, where Alt is released before Tab,
884
* clients would see Tab being pressed on enter followed by a key
885
* release event for Tab, meaning that Tab would be processed by
886
* the client when it really shouldn't.
888
* Since the use case for the pressed keys array on enter seems weak
889
* to us, we'll just fake that there are no pressed keys instead
890
* which should be spec compliant even if it might not be true.
892
wl_array_init (&fake_keys);
894
keyboard_send_modifiers (keyboard, resource, keyboard->focus_serial);
895
wl_keyboard_send_enter (resource, keyboard->focus_serial,
896
keyboard->focus_surface->resource,
901
meta_wayland_keyboard_set_focus (MetaWaylandKeyboard *keyboard,
902
MetaWaylandSurface *surface)
904
MetaWaylandInputDevice *input_device = META_WAYLAND_INPUT_DEVICE (keyboard);
906
if (keyboard->focus_surface == surface)
909
if (keyboard->focus_surface != NULL)
911
if (!wl_list_empty (&keyboard->focus_resource_list))
913
struct wl_resource *resource;
916
serial = meta_wayland_input_device_next_serial (input_device);
918
wl_resource_for_each (resource, &keyboard->focus_resource_list)
920
wl_keyboard_send_leave (resource, serial,
921
keyboard->focus_surface->resource);
924
move_resources (&keyboard->resource_list,
925
&keyboard->focus_resource_list);
928
wl_list_remove (&keyboard->focus_surface_listener.link);
929
keyboard->focus_surface = NULL;
934
struct wl_resource *focus_surface_resource;
936
keyboard->focus_surface = surface;
937
focus_surface_resource = keyboard->focus_surface->resource;
938
wl_resource_add_destroy_listener (focus_surface_resource,
939
&keyboard->focus_surface_listener);
941
move_resources_for_client (&keyboard->focus_resource_list,
942
&keyboard->resource_list,
943
wl_resource_get_client (focus_surface_resource));
945
/* Make sure a11y masks are applied before braodcasting modifiers */
946
kbd_a11y_apply_mask (keyboard);
948
if (!wl_list_empty (&keyboard->focus_resource_list))
950
struct wl_resource *resource;
952
keyboard->focus_serial =
953
meta_wayland_input_device_next_serial (input_device);
955
wl_resource_for_each (resource, &keyboard->focus_resource_list)
957
broadcast_focus (keyboard, resource);
964
meta_wayland_keyboard_get_focus_client (MetaWaylandKeyboard *keyboard)
966
if (keyboard->focus_surface)
967
return wl_resource_get_client (keyboard->focus_surface->resource);
973
keyboard_release (struct wl_client *client,
974
struct wl_resource *resource)
976
wl_resource_destroy (resource);
979
static const struct wl_keyboard_interface keyboard_interface = {
984
meta_wayland_keyboard_create_new_resource (MetaWaylandKeyboard *keyboard,
985
struct wl_client *client,
986
struct wl_resource *seat_resource,
989
struct wl_resource *resource;
991
resource = wl_resource_create (client, &wl_keyboard_interface,
992
wl_resource_get_version (seat_resource), id);
993
wl_resource_set_implementation (resource, &keyboard_interface,
994
keyboard, unbind_resource);
996
wl_keyboard_send_keymap (resource,
997
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
998
keyboard->xkb_info.keymap_fd,
999
keyboard->xkb_info.keymap_size);
1001
notify_key_repeat_for_resource (keyboard, resource);
1003
if (keyboard->focus_surface &&
1004
wl_resource_get_client (keyboard->focus_surface->resource) == client)
1006
wl_list_insert (&keyboard->focus_resource_list,
1007
wl_resource_get_link (resource));
1008
broadcast_focus (keyboard, resource);
1012
wl_list_insert (&keyboard->resource_list,
1013
wl_resource_get_link (resource));
1018
meta_wayland_keyboard_can_popup (MetaWaylandKeyboard *keyboard,
1021
return keyboard->key_serial == serial;
1025
meta_wayland_keyboard_start_grab (MetaWaylandKeyboard *keyboard,
1026
MetaWaylandKeyboardGrab *grab)
1028
meta_wayland_keyboard_set_focus (keyboard, NULL);
1029
keyboard->grab = grab;
1030
grab->keyboard = keyboard;
1034
meta_wayland_keyboard_end_grab (MetaWaylandKeyboard *keyboard)
1036
keyboard->grab = &keyboard->default_grab;
1040
meta_wayland_keyboard_init (MetaWaylandKeyboard *keyboard)
1042
wl_list_init (&keyboard->resource_list);
1043
wl_list_init (&keyboard->focus_resource_list);
1045
meta_wayland_xkb_info_init (&keyboard->xkb_info);
1047
keyboard->default_grab.interface = &default_keyboard_grab_interface;
1048
keyboard->default_grab.keyboard = keyboard;
1049
keyboard->grab = &keyboard->default_grab;
1051
keyboard->focus_surface_listener.notify =
1052
keyboard_handle_focus_surface_destroy;
1056
meta_wayland_keyboard_class_init (MetaWaylandKeyboardClass *klass)