2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_X11
25 #include "SDL_x11video.h"
26 #include "SDL_x11xinput2.h"
27 #include "../../events/SDL_mouse_c.h"
28 #include "../../events/SDL_touch_c.h"
32 #if SDL_VIDEO_DRIVER_X11_XINPUT2
33 static int xinput2_initialized = 0;
35 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
36 static int xinput2_multitouch_supported = 0;
39 /* Opcode returned X11_XQueryExtension
40 * It will be used in event processing
41 * to know that the event came from
43 static int xinput2_opcode;
45 static void parse_valuators(const double *input_values, const unsigned char *mask,int mask_len,
46 double *output_values,int output_values_len) {
48 int top = mask_len * 8;
52 SDL_memset(output_values,0,output_values_len * sizeof(double));
53 for (; i < top && z < output_values_len; i++) {
54 if (XIMaskIsSet(mask, i)) {
55 const int value = (int) *input_values;
56 output_values[z] = value;
64 query_xinput2_version(Display *display, int major, int minor)
66 /* We don't care if this fails, so long as it sets major/minor on it's way out the door. */
67 X11_XIQueryVersion(display, &major, &minor);
68 return ((major * 1000) + minor);
72 xinput2_version_atleast(const int version, const int wantmajor, const int wantminor)
74 return ( version >= ((wantmajor * 1000) + wantminor) );
77 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
79 xinput2_get_sdlwindow(SDL_VideoData *videodata, Window window)
82 for (i = 0; i < videodata->numwindows; i++) {
83 SDL_WindowData *d = videodata->windowlist[i];
84 if (d->xwindow == window) {
92 xinput2_normalize_touch_coordinates(SDL_Window *window, double in_x, double in_y, float *out_x, float *out_y)
98 *out_x = in_x / (window->w - 1);
100 if (window->h == 1) {
103 *out_y = in_y / (window->h - 1);
106 // couldn't find the window...
111 #endif /* SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH */
113 #endif /* SDL_VIDEO_DRIVER_X11_XINPUT2 */
116 X11_InitXinput2(_THIS)
118 #if SDL_VIDEO_DRIVER_X11_XINPUT2
119 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
122 XIEventMask eventmask;
123 unsigned char mask[3] = { 0,0,0 };
127 * Initialize XInput 2
128 * According to http://who-t.blogspot.com/2009/05/xi2-recipes-part-1.html its better
129 * to inform Xserver what version of Xinput we support.The server will store the version we support.
130 * "As XI2 progresses it becomes important that you use this call as the server may treat the client
131 * differently depending on the supported version".
133 * FIXME:event and err are not needed but if not passed X11_XQueryExtension returns SegmentationFault
135 if (!SDL_X11_HAVE_XINPUT2 ||
136 !X11_XQueryExtension(data->display, "XInputExtension", &xinput2_opcode, &event, &err)) {
137 return; /* X server does not have XInput at all */
140 /* We need at least 2.2 for Multitouch, 2.0 otherwise. */
141 version = query_xinput2_version(data->display, 2, 2);
142 if (!xinput2_version_atleast(version, 2, 0)) {
143 return; /* X server does not support the version we want at all. */
146 xinput2_initialized = 1;
148 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH /* Multitouch needs XInput 2.2 */
149 xinput2_multitouch_supported = xinput2_version_atleast(version, 2, 2);
152 /* Enable Raw motion events for this display */
153 eventmask.deviceid = XIAllMasterDevices;
154 eventmask.mask_len = sizeof(mask);
155 eventmask.mask = mask;
157 XISetMask(mask, XI_RawMotion);
158 XISetMask(mask, XI_RawButtonPress);
159 XISetMask(mask, XI_RawButtonRelease);
161 if (X11_XISelectEvents(data->display,DefaultRootWindow(data->display),&eventmask,1) != Success) {
168 X11_HandleXinput2Event(SDL_VideoData *videodata,XGenericEventCookie *cookie)
170 #if SDL_VIDEO_DRIVER_X11_XINPUT2
171 if(cookie->extension != xinput2_opcode) {
174 switch(cookie->evtype) {
176 const XIRawEvent *rawev = (const XIRawEvent*)cookie->data;
177 SDL_Mouse *mouse = SDL_GetMouse();
178 double relative_coords[2];
179 static Time prev_time = 0;
180 static double prev_rel_coords[2];
182 videodata->global_mouse_changed = SDL_TRUE;
184 if (!mouse->relative_mode || mouse->relative_mode_warp) {
188 parse_valuators(rawev->raw_values,rawev->valuators.mask,
189 rawev->valuators.mask_len,relative_coords,2);
191 if ((rawev->time == prev_time) && (relative_coords[0] == prev_rel_coords[0]) && (relative_coords[1] == prev_rel_coords[1])) {
192 return 0; /* duplicate event, drop it. */
195 SDL_SendMouseMotion(mouse->focus,mouse->mouseID,1,(int)relative_coords[0],(int)relative_coords[1]);
196 prev_rel_coords[0] = relative_coords[0];
197 prev_rel_coords[1] = relative_coords[1];
198 prev_time = rawev->time;
203 case XI_RawButtonPress:
204 case XI_RawButtonRelease:
205 videodata->global_mouse_changed = SDL_TRUE;
208 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
209 /* With multitouch, register to receive XI_Motion (which desctivates MotionNotify),
210 * so that we can distinguish real mouse motions from synthetic one. */
212 const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
213 int pointer_emulated = (xev->flags & XIPointerEmulated);
215 if (! pointer_emulated) {
216 SDL_Mouse *mouse = SDL_GetMouse();
217 if(!mouse->relative_mode || mouse->relative_mode_warp) {
218 SDL_Window *window = xinput2_get_sdlwindow(videodata, xev->event);
220 SDL_SendMouseMotion(window, 0, 0, xev->event_x, xev->event_y);
228 case XI_TouchBegin: {
229 const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
231 SDL_Window *window = xinput2_get_sdlwindow(videodata, xev->event);
232 xinput2_normalize_touch_coordinates(window, xev->event_x, xev->event_y, &x, &y);
233 SDL_SendTouch(xev->sourceid, xev->detail, window, SDL_TRUE, x, y, 1.0);
238 const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
240 SDL_Window *window = xinput2_get_sdlwindow(videodata, xev->event);
241 xinput2_normalize_touch_coordinates(window, xev->event_x, xev->event_y, &x, &y);
242 SDL_SendTouch(xev->sourceid, xev->detail, window, SDL_FALSE, x, y, 1.0);
246 case XI_TouchUpdate: {
247 const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
249 SDL_Window *window = xinput2_get_sdlwindow(videodata, xev->event);
250 xinput2_normalize_touch_coordinates(window, xev->event_x, xev->event_y, &x, &y);
251 SDL_SendTouchMotion(xev->sourceid, xev->detail, window, x, y, 1.0);
262 X11_InitXinput2Multitouch(_THIS)
264 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
265 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
268 info = X11_XIQueryDevice(data->display, XIAllDevices, &ndevices);
270 for (i = 0; i < ndevices; i++) {
271 XIDeviceInfo *dev = &info[i];
272 for (j = 0; j < dev->num_classes; j++) {
274 SDL_TouchDeviceType touchType;
275 XIAnyClassInfo *class = dev->classes[j];
276 XITouchClassInfo *t = (XITouchClassInfo*)class;
278 /* Only touch devices */
279 if (class->type != XITouchClass)
282 if (t->mode == XIDependentTouch) {
283 touchType = SDL_TOUCH_DEVICE_INDIRECT_RELATIVE;
284 } else { /* XIDirectTouch */
285 touchType = SDL_TOUCH_DEVICE_DIRECT;
288 touchId = t->sourceid;
289 SDL_AddTouch(touchId, touchType, dev->name);
292 X11_XIFreeDeviceInfo(info);
297 X11_Xinput2SelectTouch(_THIS, SDL_Window *window)
299 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
300 SDL_VideoData *data = NULL;
301 XIEventMask eventmask;
302 unsigned char mask[4] = { 0, 0, 0, 0 };
303 SDL_WindowData *window_data = NULL;
305 if (!X11_Xinput2IsMultitouchSupported()) {
309 data = (SDL_VideoData *) _this->driverdata;
310 window_data = (SDL_WindowData*)window->driverdata;
312 eventmask.deviceid = XIAllMasterDevices;
313 eventmask.mask_len = sizeof(mask);
314 eventmask.mask = mask;
316 XISetMask(mask, XI_TouchBegin);
317 XISetMask(mask, XI_TouchUpdate);
318 XISetMask(mask, XI_TouchEnd);
319 XISetMask(mask, XI_Motion);
321 X11_XISelectEvents(data->display,window_data->xwindow,&eventmask,1);
327 X11_Xinput2IsInitialized()
329 #if SDL_VIDEO_DRIVER_X11_XINPUT2
330 return xinput2_initialized;
337 X11_Xinput2IsMultitouchSupported()
339 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
340 return xinput2_initialized && xinput2_multitouch_supported;
346 #endif /* SDL_VIDEO_DRIVER_X11 */
348 /* vi: set ts=4 sw=4 expandtab: */