Hash :
2490166d
Author :
Date :
2013-08-21T10:12:16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#if SDL_VIDEO_DRIVER_X11
#include "SDL_x11video.h"
#include "SDL_x11xinput2.h"
#include "../../events/SDL_mouse_c.h"
#include "../../events/SDL_touch_c.h"
#define MAX_AXIS 16
#if SDL_VIDEO_DRIVER_X11_XINPUT2
static int xinput2_initialized = 0;
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
static int xinput2_multitouch_supported = 0;
#endif
/* Opcode returned XQueryExtension
* It will be used in event processing
* to know that the event came from
* this extension */
static int xinput2_opcode;
static void parse_valuators(const double *input_values,unsigned char *mask,int mask_len,
double *output_values,int output_values_len) {
int i = 0,z = 0;
int top = mask_len * 8;
if (top > MAX_AXIS)
top = MAX_AXIS;
SDL_memset(output_values,0,output_values_len * sizeof(double));
for (; i < top && z < output_values_len; i++) {
if (XIMaskIsSet(mask, i)) {
const int value = (int) *input_values;
output_values[z] = value;
input_values++;
}
z++;
}
}
#endif /* SDL_VIDEO_DRIVER_X11_XINPUT2 */
void
X11_InitXinput2(_THIS)
{
#if SDL_VIDEO_DRIVER_X11_XINPUT2
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
XIEventMask eventmask;
unsigned char mask[3] = { 0,0,0 };
int event, err;
int major = 2, minor = 0;
int outmajor,outminor;
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
minor = 2;
#endif
/*
* Initialize XInput 2
* According to http://who-t.blogspot.com/2009/05/xi2-recipes-part-1.html its better
* to inform Xserver what version of Xinput we support.The server will store the version we support.
* "As XI2 progresses it becomes important that you use this call as the server may treat the client
* differently depending on the supported version".
*
* FIXME:event and err are not needed but if not passed XQueryExtension returns SegmentationFault
*/
if (!SDL_X11_HAVE_XINPUT2 ||
!XQueryExtension(data->display, "XInputExtension", &xinput2_opcode, &event, &err)) {
return;
}
outmajor = major;
outminor = minor;
if (XIQueryVersion(data->display, &outmajor, &outminor) != Success) {
return;
}
/* Check supported version */
if(outmajor * 1000 + outminor < major * 1000 + minor) {
/* X server does not support the version we want */
return;
}
xinput2_initialized = 1;
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
/* XInput 2.2 */
if(outmajor * 1000 + outminor >= major * 1000 + minor) {
xinput2_multitouch_supported = 1;
}
#endif
/* Enable Raw motion events for this display */
eventmask.deviceid = XIAllMasterDevices;
eventmask.mask_len = sizeof(mask);
eventmask.mask = mask;
XISetMask(mask, XI_RawMotion);
if (XISelectEvents(data->display,DefaultRootWindow(data->display),&eventmask,1) != Success) {
return;
}
#endif
}
int
X11_HandleXinput2Event(SDL_VideoData *videodata,XGenericEventCookie *cookie)
{
#if SDL_VIDEO_DRIVER_X11_XINPUT2
if(cookie->extension != xinput2_opcode) {
return 0;
}
switch(cookie->evtype) {
case XI_RawMotion: {
const XIRawEvent *rawev = (const XIRawEvent*)cookie->data;
SDL_Mouse *mouse = SDL_GetMouse();
double relative_cords[2];
if (!mouse->relative_mode) {
return 0;
}
parse_valuators(rawev->raw_values,rawev->valuators.mask,
rawev->valuators.mask_len,relative_cords,2);
SDL_SendMouseMotion(mouse->focus,mouse->mouseID,1,(int)relative_cords[0],(int)relative_cords[1]);
return 1;
}
break;
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
case XI_TouchBegin: {
const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
SDL_SendTouch(xev->sourceid,xev->detail,
SDL_TRUE, xev->event_x, xev->event_y, 1.0);
return 1;
}
break;
case XI_TouchEnd: {
const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
SDL_SendTouch(xev->sourceid,xev->detail,
SDL_FALSE, xev->event_x, xev->event_y, 1.0);
return 1;
}
break;
case XI_TouchUpdate: {
const XIDeviceEvent *xev = (const XIDeviceEvent *) cookie->data;
SDL_SendTouchMotion(xev->sourceid,xev->detail,
xev->event_x, xev->event_y, 1.0);
return 1;
}
break;
#endif
}
#endif
return 0;
}
void
X11_InitXinput2Multitouch(_THIS)
{
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
XIDeviceInfo *info;
int ndevices,i,j;
info = XIQueryDevice(data->display, XIAllMasterDevices, &ndevices);
for (i = 0; i < ndevices; i++) {
XIDeviceInfo *dev = &info[i];
for (j = 0; j < dev->num_classes; j++) {
SDL_TouchID touchId;
XIAnyClassInfo *class = dev->classes[j];
XITouchClassInfo *t = (XITouchClassInfo*)class;
/* Only touch devices */
if (class->type != XITouchClass)
continue;
touchId = t->sourceid;
if (!SDL_GetTouch(touchId)) {
SDL_AddTouch(touchId, dev->name);
}
}
}
XIFreeDeviceInfo(info);
#endif
}
void
X11_Xinput2SelectTouch(_THIS, SDL_Window *window)
{
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
SDL_VideoData *data = NULL;
XIEventMask eventmask;
unsigned char mask[3] = { 0,0,0 };
SDL_WindowData *window_data = NULL;
if (!X11_Xinput2IsMultitouchSupported()) {
return;
}
data = (SDL_VideoData *) _this->driverdata;
window_data = (SDL_WindowData*)window->driverdata;
eventmask.deviceid = XIAllMasterDevices;
eventmask.mask_len = sizeof(mask);
eventmask.mask = mask;
XISetMask(mask, XI_TouchBegin);
XISetMask(mask, XI_TouchUpdate);
XISetMask(mask, XI_TouchEnd);
XISelectEvents(data->display,window_data->xwindow,&eventmask,1);
#endif
}
int
X11_Xinput2IsInitialized()
{
#if SDL_VIDEO_DRIVER_X11_XINPUT2
return xinput2_initialized;
#else
return 0;
#endif
}
int
X11_Xinput2IsMultitouchSupported()
{
#if SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH
return xinput2_initialized && xinput2_multitouch_supported;
#else
return 0;
#endif
}
#endif /* SDL_VIDEO_DRIVER_X11 */
/* vi: set ts=4 sw=4 expandtab: */