From 427670cc638d02f84897ae7fa174de4ac995e7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 2 Oct 2019 22:19:34 +0200 Subject: [PATCH] device-manager-x11: Ignore events if no source device is set There might be some inconsistent event for which we don't have a known source device. In the current state we don't handle them and we could crash when getting the current device tool. So, add an utility function that retrieves the source device for an event that warns if no device is found, and use this for Motion, Key and Button events. In case we don't have a valid source in such case, just return early instead of trying to generate invalid clutter events. https://gitlab.gnome.org/GNOME/mutter/merge_requests/823 --- src/backends/x11/meta-device-manager-x11.c | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/backends/x11/meta-device-manager-x11.c b/src/backends/x11/meta-device-manager-x11.c index c2a0ec112..c6ddb7d6f 100644 --- a/src/backends/x11/meta-device-manager-x11.c +++ b/src/backends/x11/meta-device-manager-x11.c @@ -1291,6 +1291,22 @@ handle_raw_event (MetaDeviceManagerX11 *manager_xi2, } } +static ClutterInputDevice * +get_source_device_checked (MetaDeviceManagerX11 *manager_xi2, + XIDeviceEvent *xev) +{ + ClutterInputDevice *source_device; + + source_device = g_hash_table_lookup (manager_xi2->devices_by_id, + GINT_TO_POINTER (xev->sourceid)); + + if (!source_device) + g_warning ("Impossible to get the source device with id %d for event of " + "type %d", xev->sourceid, xev->evtype); + + return source_device; +} + gboolean meta_device_manager_x11_translate_event (MetaDeviceManagerX11 *manager_xi2, XEvent *xevent, @@ -1379,6 +1395,10 @@ meta_device_manager_x11_translate_event (MetaDeviceManagerX11 *manager_xi2, char buffer[7] = { 0, }; gunichar n; + source_device = get_source_device_checked (manager_xi2, xev); + if (!source_device) + return FALSE; + event->key.type = event->type = (xev->evtype == XI_KeyPress) ? CLUTTER_KEY_PRESS : CLUTTER_KEY_RELEASE; @@ -1413,8 +1433,6 @@ meta_device_manager_x11_translate_event (MetaDeviceManagerX11 *manager_xi2, event_x11->caps_lock_set = clutter_keymap_get_caps_lock_state (CLUTTER_KEYMAP (keymap_x11)); - source_device = g_hash_table_lookup (manager_xi2->devices_by_id, - GINT_TO_POINTER (xev->sourceid)); clutter_event_set_source_device (event, source_device); device = g_hash_table_lookup (manager_xi2->devices_by_id, @@ -1458,8 +1476,10 @@ meta_device_manager_x11_translate_event (MetaDeviceManagerX11 *manager_xi2, { XIDeviceEvent *xev = (XIDeviceEvent *) xi_event; - source_device = g_hash_table_lookup (manager_xi2->devices_by_id, - GINT_TO_POINTER (xev->sourceid)); + source_device = get_source_device_checked (manager_xi2, xev); + if (!source_device) + return FALSE; + device = g_hash_table_lookup (manager_xi2->devices_by_id, GINT_TO_POINTER (xev->deviceid)); @@ -1626,7 +1646,7 @@ meta_device_manager_x11_translate_event (MetaDeviceManagerX11 *manager_xi2, break; } - if (source_device != NULL && device->stage != NULL) + if (device->stage != NULL) _clutter_input_device_set_stage (source_device, device->stage); if (xev->flags & XIPointerEmulated) @@ -1644,8 +1664,10 @@ meta_device_manager_x11_translate_event (MetaDeviceManagerX11 *manager_xi2, XIDeviceEvent *xev = (XIDeviceEvent *) xi_event; double delta_x, delta_y; - source_device = g_hash_table_lookup (manager_xi2->devices_by_id, - GINT_TO_POINTER (xev->sourceid)); + source_device = get_source_device_checked (manager_xi2, xev); + if (!source_device) + return FALSE; + device = g_hash_table_lookup (manager_xi2->devices_by_id, GINT_TO_POINTER (xev->deviceid)); @@ -1716,7 +1738,7 @@ meta_device_manager_x11_translate_event (MetaDeviceManagerX11 *manager_xi2, event->motion.y, &xev->valuators); - if (source_device != NULL && device->stage != NULL) + if (device->stage != NULL) _clutter_input_device_set_stage (source_device, device->stage); if (xev->flags & XIPointerEmulated)