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-xi2.h"
21 #include "gdkinternals.h"
22 #include "gdkprivate-x11.h"
23 #include "gdkdisplay-x11.h"
24 
25 /* Defines for VCP/VCK, to be used too
26  * for the core protocol device manager
27  */
28 #define VIRTUAL_CORE_POINTER_ID 2
29 #define VIRTUAL_CORE_KEYBOARD_ID 3
30 
31 GdkX11DeviceManagerXI2 *
_gdk_x11_device_manager_new(GdkDisplay * display)32 _gdk_x11_device_manager_new (GdkDisplay *display)
33 {
34   int opcode, firstevent, firsterror;
35   Display *xdisplay;
36 
37   xdisplay = GDK_DISPLAY_XDISPLAY (display);
38 
39   if (XQueryExtension (xdisplay, "XInputExtension",
40                        &opcode, &firstevent, &firsterror))
41     {
42       int major, minor;
43 
44       major = 2;
45       minor = 3;
46 
47       if (XIQueryVersion (xdisplay, &major, &minor) != BadRequest)
48         {
49           GdkX11DeviceManagerXI2 *device_manager_xi2;
50 
51           GDK_DISPLAY_NOTE (display, INPUT, g_message ("Creating XI2 device manager"));
52 
53           device_manager_xi2 = g_object_new (GDK_TYPE_X11_DEVICE_MANAGER_XI2,
54                                              "display", display,
55                                              "opcode", opcode,
56                                              "major", major,
57                                              "minor", minor,
58                                              NULL);
59 
60           return device_manager_xi2;
61         }
62     }
63 
64   g_error ("XInput2 support not found on display");
65 }
66 
67 /**
68  * gdk_x11_device_manager_lookup:
69  * @device_manager: (type GdkX11DeviceManagerXI2): a `GdkDeviceManager`
70  * @device_id: a device ID, as understood by the XInput2 protocol
71  *
72  * Returns the `GdkDevice` that wraps the given device ID.
73  *
74  * Returns: (transfer none) (nullable) (type GdkX11DeviceXI2): The
75  *   `GdkDevice` wrapping the device ID, or %NULL if the given ID
76  *   doesn’t currently represent a device.
77  */
78 GdkDevice *
gdk_x11_device_manager_lookup(GdkX11DeviceManagerXI2 * device_manager,int device_id)79 gdk_x11_device_manager_lookup (GdkX11DeviceManagerXI2 *device_manager,
80 			       int                     device_id)
81 {
82   g_return_val_if_fail (GDK_IS_X11_DEVICE_MANAGER_XI2 (device_manager), NULL);
83 
84   return _gdk_x11_device_manager_xi2_lookup (GDK_X11_DEVICE_MANAGER_XI2 (device_manager),
85                                              device_id);
86 }
87 
88 /**
89  * gdk_x11_device_get_id:
90  * @device: (type GdkX11DeviceXI2): a `GdkDevice`
91  *
92  * Returns the device ID as seen by XInput2.
93  *
94  * Returns: the XInput2 device ID
95  */
96 int
gdk_x11_device_get_id(GdkDevice * device)97 gdk_x11_device_get_id (GdkDevice *device)
98 {
99   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
100 
101   return _gdk_x11_device_xi2_get_id (GDK_X11_DEVICE_XI2 (device));
102 }
103