]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkdevicemanager-x11.c
75470636a9e595efa83a888635597f1650ccc539
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gdkx11devicemanager-core.h"
21 #include "gdkdevicemanagerprivate-core.h"
22 #ifdef XINPUT_XFREE
23 #include "gdkx11devicemanager-xi.h"
24 #ifdef XINPUT_2
25 #include "gdkx11devicemanager-xi2.h"
26 #endif
27 #endif
28 #include "gdkinternals.h"
29 #include "gdkprivate-x11.h"
30
31 /* Defines for VCP/VCK, to be used too
32  * for the core protocol device manager
33  */
34 #define VIRTUAL_CORE_POINTER_ID 2
35 #define VIRTUAL_CORE_KEYBOARD_ID 3
36
37 GdkDeviceManager *
38 _gdk_x11_device_manager_new (GdkDisplay *display)
39 {
40   if (!g_getenv ("GDK_CORE_DEVICE_EVENTS"))
41     {
42 #if defined (XINPUT_2) || defined (XINPUT_XFREE)
43       int opcode, firstevent, firsterror;
44       Display *xdisplay;
45
46       xdisplay = GDK_DISPLAY_XDISPLAY (display);
47
48       if (XQueryExtension (xdisplay, "XInputExtension",
49                            &opcode, &firstevent, &firsterror))
50         {
51 #ifdef XINPUT_2
52           int major, minor;
53
54           major = 2;
55 #ifdef XINPUT_2_2
56           minor = 2;
57 #else
58           minor = 0;
59 #endif /* XINPUT_2_2 */
60
61           if (!_gdk_disable_multidevice &&
62               XIQueryVersion (xdisplay, &major, &minor) != BadRequest)
63             {
64               GdkX11DeviceManagerXI2 *device_manager_xi2;
65
66               GDK_NOTE (INPUT, g_print ("Creating XI2 device manager\n"));
67
68               device_manager_xi2 = g_object_new (GDK_TYPE_X11_DEVICE_MANAGER_XI2,
69                                                  "display", display,
70                                                  "opcode", opcode,
71                                                  "major", major,
72                                                  "minor", minor,
73                                                  NULL);
74
75               return GDK_DEVICE_MANAGER (device_manager_xi2);
76             }
77           else
78 #endif /* XINPUT_2 */
79             {
80               GDK_NOTE (INPUT, g_print ("Creating XI device manager\n"));
81
82               return g_object_new (GDK_TYPE_X11_DEVICE_MANAGER_XI,
83                                    "display", display,
84                                    "event-base", firstevent,
85                                    NULL);
86             }
87         }
88 #endif /* XINPUT_2 || XINPUT_XFREE */
89     }
90
91   GDK_NOTE (INPUT, g_print ("Creating core device manager\n"));
92
93   return g_object_new (GDK_TYPE_X11_DEVICE_MANAGER_CORE,
94                        "display", display,
95                        NULL);
96 }
97
98 /**
99  * gdk_x11_device_manager_lookup:
100  * @device_manager: a #GdkDeviceManager
101  * @device_id: a device ID, as understood by the XInput2 protocol
102  *
103  * Returns the #GdkDevice that wraps the given device ID.
104  *
105  * Returns: (transfer none): (allow-none): The #GdkDevice wrapping the device ID,
106  *          or %NULL if the given ID doesn't currently represent a device.
107  *
108  * Since: 3.2
109  **/
110 GdkDevice *
111 gdk_x11_device_manager_lookup (GdkDeviceManager *device_manager,
112                                gint              device_id)
113 {
114   GdkDevice *device = NULL;
115
116   g_return_val_if_fail (GDK_IS_DEVICE_MANAGER (device_manager), NULL);
117
118 #ifdef XINPUT_2
119   if (GDK_IS_X11_DEVICE_MANAGER_XI2 (device_manager))
120     device = _gdk_x11_device_manager_xi2_lookup (GDK_X11_DEVICE_MANAGER_XI2 (device_manager),
121                                                  device_id);
122   else
123 #endif /* XINPUT_2 */
124     if (GDK_IS_X11_DEVICE_MANAGER_CORE (device_manager))
125       {
126         /* It is a core/xi1 device manager, we only map
127          * IDs 2 and 3, matching XI2's Virtual Core Pointer
128          * and Keyboard.
129          */
130         if (device_id == VIRTUAL_CORE_POINTER_ID)
131           device = GDK_X11_DEVICE_MANAGER_CORE (device_manager)->core_pointer;
132         else if (device_id == VIRTUAL_CORE_KEYBOARD_ID)
133           device = GDK_X11_DEVICE_MANAGER_CORE (device_manager)->core_keyboard;
134       }
135
136   return device;
137 }
138
139 /**
140  * gdk_x11_device_get_id:
141  * @device: a #GdkDevice
142  *
143  * Returns the device ID as seen by XInput2.
144  *
145  * <note>
146  *   If gdk_disable_multidevice() has been called, this function
147  *   will respectively return 2/3 for the core pointer and keyboard,
148  *   (matching the IDs for the Virtual Core Pointer and Keyboard in
149  *   XInput 2), but calling this function on any slave devices (i.e.
150  *   those managed via XInput 1.x), will return 0.
151  * </note>
152  *
153  * Returns: the XInput2 device ID.
154  *
155  * Since: 3.2
156  **/
157 gint
158 gdk_x11_device_get_id (GdkDevice *device)
159 {
160   gint device_id = 0;
161
162   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
163
164 #ifdef XINPUT_2
165   if (GDK_IS_X11_DEVICE_XI2 (device))
166     device_id = _gdk_x11_device_xi2_get_id (GDK_X11_DEVICE_XI2 (device));
167   else
168 #endif /* XINPUT_2 */
169     if (GDK_IS_X11_DEVICE_CORE (device))
170       {
171         if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
172           device_id = VIRTUAL_CORE_KEYBOARD_ID;
173         else
174           device_id = VIRTUAL_CORE_POINTER_ID;
175       }
176
177   return device_id;
178 }