From f90c154472cc9f3901644ccb49e944aca40200ca Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Thu, 14 Jul 2011 17:43:59 +0200 Subject: [PATCH] gdk/x11: Add gdk_x11_device_get_id() This function can be used to find out the XInput2 device ID behind a GdkDevice, mostly useful when you need to interact with say Clutter, or raw libXi calls. --- docs/reference/gdk/gdk3-sections.txt | 1 + gdk/gdk.symbols | 1 + gdk/x11/Makefile.am | 1 + gdk/x11/gdkdevice-xi2.c | 8 +++++ gdk/x11/gdkdevicemanager-x11.c | 45 ++++++++++++++++++++++++++++ gdk/x11/gdkprivate-x11.h | 3 ++ gdk/x11/gdkx.h | 1 + gdk/x11/gdkx11device.h | 35 ++++++++++++++++++++++ 8 files changed, 95 insertions(+) create mode 100644 gdk/x11/gdkx11device.h diff --git a/docs/reference/gdk/gdk3-sections.txt b/docs/reference/gdk/gdk3-sections.txt index 287599b54b..ab5cf98931 100644 --- a/docs/reference/gdk/gdk3-sections.txt +++ b/docs/reference/gdk/gdk3-sections.txt @@ -928,6 +928,7 @@ GDK_POINTER_TO_XID GDK_XID_TO_POINTER gdk_x11_lookup_xdisplay gdk_x11_get_server_time +gdk_x11_device_get_id gdk_x11_display_get_user_time gdk_x11_display_broadcast_startup_message gdk_x11_display_get_startup_notification_id diff --git a/gdk/gdk.symbols b/gdk/gdk.symbols index 239ca4e4ab..8386bb95e4 100644 --- a/gdk/gdk.symbols +++ b/gdk/gdk.symbols @@ -513,6 +513,7 @@ gdk_x11_cursor_get_type gdk_x11_cursor_get_xcursor gdk_x11_cursor_get_xdisplay gdk_x11_device_core_get_type +gdk_x11_device_get_id gdk_x11_device_manager_core_get_type gdk_x11_device_manager_xi2_get_type gdk_x11_device_manager_xi_get_type diff --git a/gdk/x11/Makefile.am b/gdk/x11/Makefile.am index 6e10b494c2..bd01d9389e 100644 --- a/gdk/x11/Makefile.am +++ b/gdk/x11/Makefile.am @@ -66,6 +66,7 @@ libgdkinclude_HEADERS = \ libgdkx11include_HEADERS = \ gdkx11applaunchcontext.h \ gdkx11cursor.h \ + gdkx11device.h \ gdkx11device-core.h \ gdkx11device-xi.h \ gdkx11device-xi2.h \ diff --git a/gdk/x11/gdkdevice-xi2.c b/gdk/x11/gdkdevice-xi2.c index d56f8fa4b9..b86d80221a 100644 --- a/gdk/x11/gdkdevice-xi2.c +++ b/gdk/x11/gdkdevice-xi2.c @@ -750,6 +750,14 @@ _gdk_x11_device_xi2_translate_state (XIModifierState *mods_state, return state; } +gint +_gdk_x11_device_xi2_get_id (GdkX11DeviceXI2 *device) +{ + g_return_val_if_fail (GDK_IS_X11_DEVICE_XI2 (device), 0); + + return device->device_id; +} + #else /* XINPUT_2 */ static void diff --git a/gdk/x11/gdkdevicemanager-x11.c b/gdk/x11/gdkdevicemanager-x11.c index a19a05e065..4d2ebfc2d6 100644 --- a/gdk/x11/gdkdevicemanager-x11.c +++ b/gdk/x11/gdkdevicemanager-x11.c @@ -29,6 +29,12 @@ #include "gdkinternals.h" #include "gdkprivate-x11.h" +/* Defines for VCP/VCK, to be used too + * for the core protocol device manager + */ +#define VIRTUAL_CORE_POINTER_ID 2 +#define VIRTUAL_CORE_KEYBOARD_ID 3 + GdkDeviceManager * _gdk_x11_device_manager_new (GdkDisplay *display) { @@ -83,3 +89,42 @@ _gdk_x11_device_manager_new (GdkDisplay *display) "display", display, NULL); } + +/** + * gdk_x11_device_get_id: + * @device: a #GdkDevice + * + * Returns the device ID as seen by XInput2. + * + * + * If gdk_disable_multidevice() has been called, this function + * will respectively return 2/3 for the core pointer and keyboard, + * (matching the IDs for the Virtual Core Pointer and Keyboard in + * XInput 2), but calling this function on any slave devices (i.e. + * those managed via XInput 1.x), will return 0. + * + * + * Returns: the XInput2 device ID. + **/ +gint +gdk_x11_device_get_id (GdkDevice *device) +{ + gint device_id = 0; + + g_return_val_if_fail (GDK_IS_DEVICE (device), 0); + +#ifdef XINPUT_2 + if (GDK_IS_X11_DEVICE_XI2 (device)) + device_id = _gdk_x11_device_xi2_get_id (GDK_X11_DEVICE_XI2 (device)); + else +#endif /* XINPUT_2 */ + if (GDK_IS_X11_DEVICE_CORE (device)) + { + if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD) + device_id = VIRTUAL_CORE_KEYBOARD_ID; + else + device_id = VIRTUAL_CORE_POINTER_ID; + } + + return device_id; +} diff --git a/gdk/x11/gdkprivate-x11.h b/gdk/x11/gdkprivate-x11.h index 52ff852d58..cb9b231479 100644 --- a/gdk/x11/gdkprivate-x11.h +++ b/gdk/x11/gdkprivate-x11.h @@ -252,6 +252,9 @@ guchar * _gdk_x11_device_xi2_translate_event_mask (GdkEventMask event_mask, guint _gdk_x11_device_xi2_translate_state (XIModifierState *mods_state, XIButtonState *buttons_state, XIGroupState *group_state); +gint _gdk_x11_device_xi2_get_id (GdkX11DeviceXI2 *device); + + #endif void _gdk_x11_event_translate_keyboard_string (GdkEventKey *event); diff --git a/gdk/x11/gdkx.h b/gdk/x11/gdkx.h index 48207b585d..66425c92bc 100644 --- a/gdk/x11/gdkx.h +++ b/gdk/x11/gdkx.h @@ -36,6 +36,7 @@ #include #include +#include #include #include #include diff --git a/gdk/x11/gdkx11device.h b/gdk/x11/gdkx11device.h new file mode 100644 index 0000000000..4a55540f5c --- /dev/null +++ b/gdk/x11/gdkx11device.h @@ -0,0 +1,35 @@ +/* GDK - The GIMP Drawing Kit + * Copyright (C) 2011 Carlos Garnacho + * + * 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 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., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#if !defined (__GDKX_H_INSIDE__) && !defined (GDK_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __GDK_X11_DEVICE_H__ +#define __GDK_X11_DEVICE_H__ + +#include + +G_BEGIN_DECLS + +gint gdk_x11_device_get_id (GdkDevice *device); + +G_END_DECLS + +#endif /* __GDK_X11_DEVICE_H__ */