21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_X11
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;
43 static int xinput2_opcode;
45 static void parse_valuators(
const double *input_values,
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;
64 query_xinput2_version(Display *
display,
int major,
int minor)
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
80 double in_x,
double in_y,
float *out_x,
float *out_y)
86 if (
d->window->w == 1) {
89 *out_x = in_x / (
d->window->w - 1);
91 if (
d->window->h == 1) {
94 *out_y = in_y / (
d->window->h - 1);
110 #if SDL_VIDEO_DRIVER_X11_XINPUT2
114 XIEventMask eventmask;
115 unsigned char mask[3] = { 0,0,0 };
127 if (!SDL_X11_HAVE_XINPUT2 ||
128 !X11_XQueryExtension(
data->display,
"XInputExtension", &xinput2_opcode, &
event, &err)) {
133 version = query_xinput2_version(
data->display, 2, 2);
134 if (!xinput2_version_atleast(version, 2, 0)) {
138 xinput2_initialized = 1;
140 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
141 xinput2_multitouch_supported = xinput2_version_atleast(version, 2, 2);
145 eventmask.deviceid = XIAllMasterDevices;
146 eventmask.mask_len =
sizeof(
mask);
147 eventmask.mask =
mask;
149 XISetMask(
mask, XI_RawMotion);
150 XISetMask(
mask, XI_RawButtonPress);
151 XISetMask(
mask, XI_RawButtonRelease);
153 if (X11_XISelectEvents(
data->display,DefaultRootWindow(
data->display),&eventmask,1) != Success) {
162 #if SDL_VIDEO_DRIVER_X11_XINPUT2
163 if(cookie->extension != xinput2_opcode) {
166 switch(cookie->evtype) {
168 const XIRawEvent *rawev = (
const XIRawEvent*)cookie->data;
170 double relative_coords[2];
171 static Time prev_time = 0;
172 static double prev_rel_coords[2];
176 if (!mouse->relative_mode || mouse->relative_mode_warp) {
180 parse_valuators(rawev->raw_values,rawev->valuators.mask,
181 rawev->valuators.mask_len,relative_coords,2);
183 if ((rawev->time == prev_time) && (relative_coords[0] == prev_rel_coords[0]) && (relative_coords[1] == prev_rel_coords[1])) {
187 SDL_SendMouseMotion(mouse->focus,mouse->mouseID,1,(
int)relative_coords[0],(
int)relative_coords[1]);
188 prev_rel_coords[0] = relative_coords[0];
189 prev_rel_coords[1] = relative_coords[1];
190 prev_time = rawev->time;
195 case XI_RawButtonPress:
196 case XI_RawButtonRelease:
200 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
201 case XI_TouchBegin: {
202 const XIDeviceEvent *xev = (
const XIDeviceEvent *) cookie->data;
204 xinput2_normalize_touch_coordinates(videodata, xev->event,
205 xev->event_x, xev->event_y, &
x, &
y);
211 const XIDeviceEvent *xev = (
const XIDeviceEvent *) cookie->data;
213 xinput2_normalize_touch_coordinates(videodata, xev->event,
214 xev->event_x, xev->event_y, &
x, &
y);
219 case XI_TouchUpdate: {
220 const XIDeviceEvent *xev = (
const XIDeviceEvent *) cookie->data;
222 xinput2_normalize_touch_coordinates(videodata, xev->event,
223 xev->event_x, xev->event_y, &
x, &
y);
237 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
241 info = X11_XIQueryDevice(
data->display, XIAllDevices, &ndevices);
243 for (
i = 0;
i < ndevices;
i++) {
244 XIDeviceInfo *dev = &info[
i];
245 for (
j = 0;
j < dev->num_classes;
j++) {
247 XIAnyClassInfo *
class = dev->classes[
j];
248 XITouchClassInfo *
t = (XITouchClassInfo*)
class;
251 if (class->type != XITouchClass)
254 touchId =
t->sourceid;
258 X11_XIFreeDeviceInfo(info);
265 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
267 XIEventMask eventmask;
268 unsigned char mask[3] = { 0,0,0 };
278 eventmask.deviceid = XIAllMasterDevices;
279 eventmask.mask_len =
sizeof(
mask);
280 eventmask.mask =
mask;
282 XISetMask(
mask, XI_TouchBegin);
283 XISetMask(
mask, XI_TouchUpdate);
284 XISetMask(
mask, XI_TouchEnd);
286 X11_XISelectEvents(
data->display,window_data->
xwindow,&eventmask,1);
294 #if SDL_VIDEO_DRIVER_X11_XINPUT2
295 return xinput2_initialized;
304 #if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
305 return xinput2_initialized && xinput2_multitouch_supported;