]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdevicemanager-x11.c
efa3c20cb09db56cf43f7c5764ed9e13f1ea07ad
[~andy/gtk] / gdk / x11 / gdkdevicemanager-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gdkx11devicemanager-core.h"
23 #include "gdkdevicemanagerprivate-core.h"
24 #ifdef XINPUT_XFREE
25 #include "gdkx11devicemanager-xi.h"
26 #ifdef XINPUT_2
27 #include "gdkx11devicemanager-xi2.h"
28 #endif
29 #endif
30 #include "gdkinternals.h"
31 #include "gdkprivate-x11.h"
32
33 /* Defines for VCP/VCK, to be used too
34  * for the core protocol device manager
35  */
36 #define VIRTUAL_CORE_POINTER_ID 2
37 #define VIRTUAL_CORE_KEYBOARD_ID 3
38
39 GdkDeviceManager *
40 _gdk_x11_device_manager_new (GdkDisplay *display)
41 {
42   if (!g_getenv ("GDK_CORE_DEVICE_EVENTS"))
43     {
44 #if defined (XINPUT_2) || defined (XINPUT_XFREE)
45       int opcode, firstevent, firsterror;
46       Display *xdisplay;
47
48       xdisplay = GDK_DISPLAY_XDISPLAY (display);
49
50       if (XQueryExtension (xdisplay, "XInputExtension",
51                            &opcode, &firstevent, &firsterror))
52         {
53 #ifdef XINPUT_2
54           int major, minor;
55
56           major = 2;
57           minor = 0;
58
59           if (!_gdk_disable_multidevice &&
60               XIQueryVersion (xdisplay, &major, &minor) != BadRequest)
61             {
62               GdkX11DeviceManagerXI2 *device_manager_xi2;
63
64               GDK_NOTE (INPUT, g_print ("Creating XI2 device manager\n"));
65
66               device_manager_xi2 = g_object_new (GDK_TYPE_X11_DEVICE_MANAGER_XI2,
67                                                  "display", display,
68                                                  "opcode", opcode,
69                                                  NULL);
70
71               return GDK_DEVICE_MANAGER (device_manager_xi2);
72             }
73           else
74 #endif /* XINPUT_2 */
75             {
76               GDK_NOTE (INPUT, g_print ("Creating XI device manager\n"));
77
78               return g_object_new (GDK_TYPE_X11_DEVICE_MANAGER_XI,
79                                    "display", display,
80                                    "event-base", firstevent,
81                                    NULL);
82             }
83         }
84 #endif /* XINPUT_2 || XINPUT_XFREE */
85     }
86
87   GDK_NOTE (INPUT, g_print ("Creating core device manager\n"));
88
89   return g_object_new (GDK_TYPE_X11_DEVICE_MANAGER_CORE,
90                        "display", display,
91                        NULL);
92 }
93
94 /**
95  * gdk_x11_device_manager_lookup:
96  * @device_manager: a #GdkDeviceManager
97  * @device_id: a device ID, as understood by the XInput2 protocol
98  *
99  * Returns the #GdkDevice that wraps the given device ID.
100  *
101  * Returns: (transfer none): (allow-none): The #GdkDevice wrapping the device ID,
102  *          or %NULL if the given ID doesn't currently represent a device.
103  *
104  * Since: 3.2
105  **/
106 GdkDevice *
107 gdk_x11_device_manager_lookup (GdkDeviceManager *device_manager,
108                                gint              device_id)
109 {
110   GdkDevice *device = NULL;
111
112   g_return_val_if_fail (GDK_IS_DEVICE_MANAGER (device_manager), NULL);
113
114 #ifdef XINPUT_2
115   if (GDK_IS_X11_DEVICE_MANAGER_XI2 (device_manager))
116     device = _gdk_x11_device_manager_xi2_lookup (GDK_X11_DEVICE_MANAGER_XI2 (device_manager),
117                                                  device_id);
118   else
119 #endif /* XINPUT_2 */
120     if (GDK_IS_X11_DEVICE_MANAGER_CORE (device_manager))
121       {
122         /* It is a core/xi1 device manager, we only map
123          * IDs 2 and 3, matching XI2's Virtual Core Pointer
124          * and Keyboard.
125          */
126         if (device_id == VIRTUAL_CORE_POINTER_ID)
127           device = GDK_X11_DEVICE_MANAGER_CORE (device_manager)->core_pointer;
128         else if (device_id == VIRTUAL_CORE_KEYBOARD_ID)
129           device = GDK_X11_DEVICE_MANAGER_CORE (device_manager)->core_keyboard;
130       }
131
132   return device;
133 }
134
135 /**
136  * gdk_x11_device_get_id:
137  * @device: a #GdkDevice
138  *
139  * Returns the device ID as seen by XInput2.
140  *
141  * <note>
142  *   If gdk_disable_multidevice() has been called, this function
143  *   will respectively return 2/3 for the core pointer and keyboard,
144  *   (matching the IDs for the Virtual Core Pointer and Keyboard in
145  *   XInput 2), but calling this function on any slave devices (i.e.
146  *   those managed via XInput 1.x), will return 0.
147  * </note>
148  *
149  * Returns: the XInput2 device ID.
150  *
151  * Since: 3.2
152  **/
153 gint
154 gdk_x11_device_get_id (GdkDevice *device)
155 {
156   gint device_id = 0;
157
158   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
159
160 #ifdef XINPUT_2
161   if (GDK_IS_X11_DEVICE_XI2 (device))
162     device_id = _gdk_x11_device_xi2_get_id (GDK_X11_DEVICE_XI2 (device));
163   else
164 #endif /* XINPUT_2 */
165     if (GDK_IS_X11_DEVICE_CORE (device))
166       {
167         if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
168           device_id = VIRTUAL_CORE_KEYBOARD_ID;
169         else
170           device_id = VIRTUAL_CORE_POINTER_ID;
171       }
172
173   return device_id;
174 }