| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* X-Windows touch module. |
| 12 |
* |
| 13 |
* By Ryan Gumbs. |
| 14 |
* |
| 15 |
* See readme.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
#define ALLEGRO_NO_COMPATIBILITY |
| 19 |
|
| 20 |
#include "allegro5/allegro.h" |
| 21 |
#include "allegro5/internal/aintern.h" |
| 22 |
#include "allegro5/internal/aintern_mouse.h" |
| 23 |
#include "allegro5/internal/aintern_x.h" |
| 24 |
#include "allegro5/internal/aintern_xdisplay.h" |
| 25 |
#include "allegro5/internal/aintern_xmouse.h" |
| 26 |
#include "allegro5/internal/aintern_xsystem.h" |
| 27 |
#include "allegro5/internal/aintern_xtouch.h" |
| 28 |
|
| 29 |
#ifdef ALLEGRO_XWINDOWS_WITH_XINPUT2 |
| 30 |
|
| 31 |
#include <X11/Xlib.h> |
| 32 |
#include <X11/extensions/XInput2.h> |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
ALLEGRO_DEBUG_CHANNEL("touch") |
| 37 |
|
| 38 |
|
| 39 |
/* the one and only touch object */ |
| 40 |
static ALLEGRO_TOUCH_INPUT_STATE touch_input_state; |
| 41 |
static ALLEGRO_TOUCH_INPUT touch_input; |
| 42 |
static ALLEGRO_MOUSE_STATE mouse_state; |
| 43 |
static int touch_ids[ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT]; /* the XInput id for touches */ |
| 44 |
static int primary_touch_id = -1; |
| 45 |
static bool installed = false; |
| 46 |
static size_t initiali_time_stamp = ~0; |
| 47 |
static int opcode; |
| 48 |
|
| 49 |
|
| 50 |
static void reset_touch_input_state(void) |
| 51 |
{ |
| 52 |
int i; |
| 53 |
|
| 54 |
for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; i++) { |
| 55 |
touch_input_state.touches[i].id = -1; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
static void generate_touch_input_event(unsigned int type, double timestamp, |
| 61 |
int id, float x, float y, float dx, float dy, bool primary, |
| 62 |
ALLEGRO_DISPLAY *disp) |
| 63 |
{ |
| 64 |
ALLEGRO_EVENT event; |
| 65 |
|
| 66 |
bool want_touch_event = _al_event_source_needs_to_generate_event(&touch_input.es); |
| 67 |
bool want_mouse_emulation_event; |
| 68 |
|
| 69 |
if (touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_5_0_x) { |
| 70 |
want_mouse_emulation_event = _al_event_source_needs_to_generate_event(&touch_input.mouse_emulation_es) && al_is_mouse_installed(); |
| 71 |
} |
| 72 |
else { |
| 73 |
want_mouse_emulation_event = _al_event_source_needs_to_generate_event(&touch_input.mouse_emulation_es) && primary && al_is_mouse_installed(); |
| 74 |
} |
| 75 |
|
| 76 |
if (touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_NONE) |
| 77 |
want_mouse_emulation_event = false; |
| 78 |
else if (touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_INCLUSIVE) |
| 79 |
want_touch_event = al_is_mouse_installed() ? (want_touch_event && !primary) : want_touch_event; |
| 80 |
else if (touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_EXCLUSIVE) |
| 81 |
want_touch_event = al_is_mouse_installed() ? false : want_touch_event; |
| 82 |
|
| 83 |
|
| 84 |
if (!want_touch_event && !want_mouse_emulation_event) |
| 85 |
return; |
| 86 |
|
| 87 |
if (want_touch_event) { |
| 88 |
event.touch.type = type; |
| 89 |
event.touch.display = (ALLEGRO_DISPLAY*)disp; |
| 90 |
event.touch.timestamp = timestamp; |
| 91 |
event.touch.id = id; |
| 92 |
event.touch.x = x; |
| 93 |
event.touch.y = y; |
| 94 |
event.touch.dx = dx; |
| 95 |
event.touch.dy = dy; |
| 96 |
event.touch.primary = primary; |
| 97 |
|
| 98 |
_al_event_source_lock(&touch_input.es); |
| 99 |
_al_event_source_emit_event(&touch_input.es, &event); |
| 100 |
_al_event_source_unlock(&touch_input.es); |
| 101 |
} |
| 102 |
|
| 103 |
if (touch_input.mouse_emulation_mode != ALLEGRO_MOUSE_EMULATION_NONE) { |
| 104 |
mouse_state.x = (int)x; |
| 105 |
mouse_state.y = (int)y; |
| 106 |
if (type == ALLEGRO_EVENT_TOUCH_BEGIN) |
| 107 |
mouse_state.buttons++; |
| 108 |
else if (type == ALLEGRO_EVENT_TOUCH_END) |
| 109 |
mouse_state.buttons--; |
| 110 |
|
| 111 |
mouse_state.pressure = mouse_state.buttons ? 1.0 : 0.0; /* TODO */ |
| 112 |
|
| 113 |
_al_event_source_lock(&touch_input.mouse_emulation_es); |
| 114 |
if (want_mouse_emulation_event) { |
| 115 |
|
| 116 |
switch (type) { |
| 117 |
case ALLEGRO_EVENT_TOUCH_BEGIN: type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN; break; |
| 118 |
case ALLEGRO_EVENT_TOUCH_CANCEL: |
| 119 |
case ALLEGRO_EVENT_TOUCH_END: type = ALLEGRO_EVENT_MOUSE_BUTTON_UP; break; |
| 120 |
case ALLEGRO_EVENT_TOUCH_MOVE: type = ALLEGRO_EVENT_MOUSE_AXES; break; |
| 121 |
} |
| 122 |
|
| 123 |
event.mouse.type = type; |
| 124 |
event.mouse.timestamp = timestamp; |
| 125 |
event.mouse.display = (ALLEGRO_DISPLAY*)disp; |
| 126 |
event.mouse.x = (int)x; |
| 127 |
event.mouse.y = (int)y; |
| 128 |
event.mouse.dx = (int)dx; |
| 129 |
event.mouse.dy = (int)dy; |
| 130 |
event.mouse.dz = 0; |
| 131 |
event.mouse.dw = 0; |
| 132 |
if (touch_input.mouse_emulation_mode != ALLEGRO_MOUSE_EMULATION_5_0_x) { |
| 133 |
event.mouse.button = 1; |
| 134 |
} |
| 135 |
else { |
| 136 |
event.mouse.button = id; |
| 137 |
} |
| 138 |
event.mouse.pressure = mouse_state.pressure; |
| 139 |
|
| 140 |
if (touch_input.mouse_emulation_mode != ALLEGRO_MOUSE_EMULATION_5_0_x) { |
| 141 |
al_set_mouse_xy(event.mouse.display, event.mouse.x, event.mouse.y); |
| 142 |
} |
| 143 |
|
| 144 |
_al_event_source_emit_event(&touch_input.mouse_emulation_es, &event); |
| 145 |
} |
| 146 |
_al_event_source_unlock(&touch_input.mouse_emulation_es); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
static int find_free_touch_state_index(void) |
| 152 |
{ |
| 153 |
int i; |
| 154 |
|
| 155 |
for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; i++) |
| 156 |
if (touch_input_state.touches[i].id < 0) |
| 157 |
return i; |
| 158 |
|
| 159 |
return -1; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
static int find_touch_state_index_with_id(int id) |
| 164 |
{ |
| 165 |
int i; |
| 166 |
|
| 167 |
for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; i++) |
| 168 |
if (touch_ids[i] == id) |
| 169 |
return i; |
| 170 |
|
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static void touch_input_handle_begin(int id, double timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY *disp) |
| 176 |
{ |
| 177 |
int index= find_free_touch_state_index(); |
| 178 |
if (index < 0) |
| 179 |
return; |
| 180 |
|
| 181 |
ALLEGRO_TOUCH_STATE* state = touch_input_state.touches + index; |
| 182 |
(void)primary; |
| 183 |
|
| 184 |
if (NULL == state) |
| 185 |
return; |
| 186 |
|
| 187 |
_al_event_source_lock(&touch_input.es); |
| 188 |
state->id = index; |
| 189 |
state->x = x; |
| 190 |
state->y = y; |
| 191 |
state->dx = 0.0f; |
| 192 |
state->dy = 0.0f; |
| 193 |
state->primary = primary; |
| 194 |
state->display = disp; |
| 195 |
_al_event_source_unlock(&touch_input.es); |
| 196 |
|
| 197 |
generate_touch_input_event(ALLEGRO_EVENT_TOUCH_BEGIN, timestamp, |
| 198 |
state->id, state->x, state->y, state->dx, state->dy, state->primary, |
| 199 |
disp); |
| 200 |
|
| 201 |
touch_ids[index]= id; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
static void touch_input_handle_end(int id, double timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY *disp) |
| 206 |
{ |
| 207 |
int index= find_touch_state_index_with_id(id); |
| 208 |
if (index < 0) |
| 209 |
return; |
| 210 |
|
| 211 |
ALLEGRO_TOUCH_STATE* state = touch_input_state.touches + index; |
| 212 |
(void)primary; |
| 213 |
|
| 214 |
if (NULL == state) |
| 215 |
return; |
| 216 |
|
| 217 |
_al_event_source_lock(&touch_input.es); |
| 218 |
state->dx = x - state->x; |
| 219 |
state->dy = y - state->y; |
| 220 |
state->x = x; |
| 221 |
state->y = y; |
| 222 |
_al_event_source_unlock(&touch_input.es); |
| 223 |
|
| 224 |
generate_touch_input_event(ALLEGRO_EVENT_TOUCH_END, timestamp, |
| 225 |
state->id, state->x, state->y, state->dx, state->dy, state->primary, |
| 226 |
disp); |
| 227 |
|
| 228 |
_al_event_source_lock(&touch_input.es); |
| 229 |
state->id = -1; |
| 230 |
touch_ids[index]= -1; |
| 231 |
_al_event_source_unlock(&touch_input.es); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
static void touch_input_handle_move(int id, double timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY *disp) |
| 236 |
{ |
| 237 |
int index= find_touch_state_index_with_id(id); |
| 238 |
if (index < 0) |
| 239 |
return; |
| 240 |
|
| 241 |
ALLEGRO_TOUCH_STATE* state = touch_input_state.touches + index; |
| 242 |
(void)primary; |
| 243 |
|
| 244 |
if (NULL == state) |
| 245 |
return; |
| 246 |
|
| 247 |
if (x == state->x && y == state->y) |
| 248 |
return; /* coordinates haven't changed */ |
| 249 |
|
| 250 |
_al_event_source_lock(&touch_input.es); |
| 251 |
state->dx = x - state->x; |
| 252 |
state->dy = y - state->y; |
| 253 |
state->x = x; |
| 254 |
state->y = y; |
| 255 |
_al_event_source_unlock(&touch_input.es); |
| 256 |
|
| 257 |
generate_touch_input_event(ALLEGRO_EVENT_TOUCH_MOVE, timestamp, |
| 258 |
state->id, state->x, state->y, state->dx, state->dy, state->primary, |
| 259 |
disp); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
static void touch_input_handle_cancel(int id, double timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY *disp) |
| 264 |
{ |
| 265 |
int index= find_touch_state_index_with_id(id); |
| 266 |
if (index < 0) |
| 267 |
return; |
| 268 |
|
| 269 |
ALLEGRO_TOUCH_STATE* state = touch_input_state.touches + index; |
| 270 |
(void)primary; |
| 271 |
|
| 272 |
if (NULL == state) |
| 273 |
return; |
| 274 |
|
| 275 |
_al_event_source_lock(&touch_input.es); |
| 276 |
state->dx = x - state->x; |
| 277 |
state->dy = y - state->y; |
| 278 |
state->x = x; |
| 279 |
state->y = y; |
| 280 |
_al_event_source_unlock(&touch_input.es); |
| 281 |
|
| 282 |
generate_touch_input_event(ALLEGRO_EVENT_TOUCH_CANCEL, timestamp, |
| 283 |
state->id, state->x, state->y, state->dx, state->dy, state->primary, disp); |
| 284 |
|
| 285 |
_al_event_source_lock(&touch_input.es); |
| 286 |
state->id = -1; |
| 287 |
_al_event_source_unlock(&touch_input.es); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
static bool init_touch_input_api(void) |
| 292 |
{ |
| 293 |
ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver(); |
| 294 |
Display *dpy = system->x11display; |
| 295 |
|
| 296 |
/* check XInput extension */ |
| 297 |
int ev; |
| 298 |
int err; |
| 299 |
if (!XQueryExtension(dpy, "XInputExtension", &opcode, &ev, &err)) { |
| 300 |
ALLEGRO_DEBUG("XInput extension not available. Touch input unavailable.\n"); |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
/* check the version of XInput */ |
| 305 |
int major = 2; |
| 306 |
int minor = 2; |
| 307 |
int rc = XIQueryVersion(dpy, &major, &minor); |
| 308 |
if (rc != Success) { |
| 309 |
ALLEGRO_DEBUG("XInput version is too old (%d.%d): Needs 2.2. Touch input unavailable.\n", major, minor); |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
/* select device */ |
| 314 |
int cnt; |
| 315 |
int i, j; |
| 316 |
int touch_devid; |
| 317 |
XIDeviceInfo *di = XIQueryDevice(dpy, XIAllDevices, &cnt); |
| 318 |
for (i = 0; i < cnt; i++) { |
| 319 |
XIDeviceInfo *dev = &di[i]; |
| 320 |
for (j = 0; j < dev->num_classes; j++) { |
| 321 |
XITouchClassInfo *class = (XITouchClassInfo*)(dev->classes[j]); |
| 322 |
if (class->type == XITouchClass) { |
| 323 |
touch_devid = dev->deviceid; |
| 324 |
ALLEGRO_DEBUG("Found touchscreen deviceid: %i\n", touch_devid); |
| 325 |
goto STOP_SEARCH_DEVICE; |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
STOP_SEARCH_DEVICE: |
| 330 |
XIFreeDeviceInfo(di); |
| 331 |
|
| 332 |
if (i >= cnt) { |
| 333 |
/* no device found */ |
| 334 |
ALLEGRO_DEBUG("No touchscreen device found.\n"); |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
static bool xtouch_init(void) |
| 343 |
{ |
| 344 |
if (installed) |
| 345 |
return false; |
| 346 |
|
| 347 |
if (!init_touch_input_api()) |
| 348 |
return false; |
| 349 |
|
| 350 |
ALLEGRO_DEBUG("XInput2 touch input initialized.\n"); |
| 351 |
|
| 352 |
memset(&touch_input, 0, sizeof(touch_input)); |
| 353 |
reset_touch_input_state(); |
| 354 |
|
| 355 |
/* Initialise the touch object for use as an event source. */ |
| 356 |
_al_event_source_init(&touch_input.es); |
| 357 |
|
| 358 |
_al_event_source_init(&touch_input.mouse_emulation_es); |
| 359 |
touch_input.mouse_emulation_mode = ALLEGRO_MOUSE_EMULATION_TRANSPARENT; |
| 360 |
|
| 361 |
initiali_time_stamp = al_get_time(); |
| 362 |
|
| 363 |
installed = true; |
| 364 |
|
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
static void xtouch_exit(void) |
| 370 |
{ |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
static ALLEGRO_TOUCH_INPUT* get_touch_input(void) |
| 375 |
{ |
| 376 |
return &touch_input; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
static void get_touch_input_state(ALLEGRO_TOUCH_INPUT_STATE *ret_state) |
| 381 |
{ |
| 382 |
_al_event_source_lock(&touch_input.es); |
| 383 |
*ret_state = touch_input_state; |
| 384 |
_al_event_source_unlock(&touch_input.es); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
static void set_mouse_emulation_mode(int mode) |
| 389 |
{ |
| 390 |
if (touch_input.mouse_emulation_mode != mode) { |
| 391 |
int i; |
| 392 |
|
| 393 |
for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; i++) { |
| 394 |
ALLEGRO_TOUCH_STATE* touch = touch_input_state.touches + i; |
| 395 |
|
| 396 |
if (touch->id > 0) { |
| 397 |
touch_input_handle_cancel(touch_ids[i], initiali_time_stamp, |
| 398 |
touch->x, touch->y, touch->primary, touch->display); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
touch_input.mouse_emulation_mode = mode; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
/* the driver vtable */ |
| 408 |
#define TOUCHDRV_XWIN AL_ID('X','W','I','N') |
| 409 |
|
| 410 |
static ALLEGRO_TOUCH_INPUT_DRIVER touchdrv_xwin = |
| 411 |
{ |
| 412 |
TOUCHDRV_XWIN, |
| 413 |
xtouch_init, |
| 414 |
xtouch_exit, |
| 415 |
get_touch_input, |
| 416 |
get_touch_input_state, |
| 417 |
set_mouse_emulation_mode, |
| 418 |
NULL |
| 419 |
}; |
| 420 |
|
| 421 |
#endif |
| 422 |
|
| 423 |
_AL_DRIVER_INFO _al_touch_input_driver_list[] = |
| 424 |
{ |
| 425 |
#ifdef ALLEGRO_XWINDOWS_WITH_XINPUT2 |
| 426 |
{TOUCHDRV_XWIN, &touchdrv_xwin, true}, |
| 427 |
#endif |
| 428 |
{0, NULL, 0} |
| 429 |
}; |
| 430 |
|
| 431 |
void _al_x_handle_touch_event(ALLEGRO_SYSTEM_XGLX *s, ALLEGRO_DISPLAY_XGLX *d, XEvent *e) |
| 432 |
{ |
| 433 |
#ifdef ALLEGRO_XWINDOWS_WITH_XINPUT2 |
| 434 |
Display *x11display = s->x11display; |
| 435 |
XGenericEventCookie *cookie = &e->xcookie; |
| 436 |
if (!installed) |
| 437 |
return; |
| 438 |
/* extended event */ |
| 439 |
if (XGetEventData(x11display, cookie)) { |
| 440 |
/* check if this belongs to XInput */ |
| 441 |
if (cookie->type == GenericEvent && cookie->extension == opcode) { |
| 442 |
XIDeviceEvent *devev; |
| 443 |
|
| 444 |
devev = cookie->data; |
| 445 |
|
| 446 |
/* ignore events from a different display */ |
| 447 |
if (devev->display != x11display) |
| 448 |
return; |
| 449 |
|
| 450 |
switch (devev->evtype) { |
| 451 |
case XI_TouchBegin: |
| 452 |
/* the next new touch gets primary flag if it's not set */ |
| 453 |
if (primary_touch_id < 0) |
| 454 |
primary_touch_id= devev->detail; |
| 455 |
|
| 456 |
touch_input_handle_begin(devev->detail, al_get_time(), |
| 457 |
devev->event_x, devev->event_y, primary_touch_id == devev->detail, &d->display); |
| 458 |
break; |
| 459 |
case XI_TouchUpdate: |
| 460 |
touch_input_handle_move(devev->detail, al_get_time(), |
| 461 |
devev->event_x, devev->event_y, primary_touch_id == devev->detail, &d->display); |
| 462 |
break; |
| 463 |
case XI_TouchEnd: |
| 464 |
touch_input_handle_end(devev->detail, al_get_time(), |
| 465 |
devev->event_x, devev->event_y, primary_touch_id == devev->detail, &d->display); |
| 466 |
|
| 467 |
if (primary_touch_id == devev->detail) |
| 468 |
primary_touch_id = -1; |
| 469 |
break; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
#else |
| 474 |
(void)s; |
| 475 |
(void)d; |
| 476 |
(void)e; |
| 477 |
#endif |
| 478 |
} |