| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Linux console mouse driver for event interface (EVDEV). |
| 12 |
* |
| 13 |
* By Annie Testes. |
| 14 |
* |
| 15 |
* Handles all inputs that fill evdev with informations |
| 16 |
* about movement and click. |
| 17 |
* |
| 18 |
* Hacked for new mouse API by Peter Wang. |
| 19 |
* |
| 20 |
* See readme.txt for copyright information. |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#define ALLEGRO_NO_COMPATIBILITY |
| 25 |
|
| 26 |
#include "allegro5/allegro.h" |
| 27 |
|
| 28 |
#ifdef ALLEGRO_HAVE_LINUX_INPUT_H |
| 29 |
|
| 30 |
ALLEGRO_DEBUG_CHANNEL("lmseev"); |
| 31 |
|
| 32 |
#include "allegro5/internal/aintern.h" |
| 33 |
#include "allegro5/internal/aintern_mouse.h" |
| 34 |
#include "allegro5/platform/aintunix.h" |
| 35 |
#include "allegro5/platform/aintlnx.h" |
| 36 |
|
| 37 |
#ifdef ALLEGRO_RASPBERRYPI |
| 38 |
#include "allegro5/internal/aintern_system.h" |
| 39 |
#include "allegro5/internal/aintern_display.h" |
| 40 |
#include "allegro5/internal/aintern_vector.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
#include <string.h> |
| 45 |
#include <sys/ioctl.h> |
| 46 |
#include <linux/input.h> |
| 47 |
|
| 48 |
#define PREFIX_I "al-evdev INFO: " |
| 49 |
#define PREFIX_W "al-evdev WARNING: " |
| 50 |
#define PREFIX_E "al-evdev ERROR: " |
| 51 |
|
| 52 |
|
| 53 |
typedef struct AL_MOUSE_EVDEV |
| 54 |
{ |
| 55 |
ALLEGRO_MOUSE parent; |
| 56 |
int fd; |
| 57 |
ALLEGRO_MOUSE_STATE state; |
| 58 |
} AL_MOUSE_EVDEV; |
| 59 |
|
| 60 |
|
| 61 |
/* the one and only mouse object */ |
| 62 |
static AL_MOUSE_EVDEV the_mouse; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
/* forward declarations */ |
| 67 |
static void process_new_data(void *unused); |
| 68 |
static void process_event(struct input_event *event); |
| 69 |
static void handle_button_event(unsigned int button, bool is_down); |
| 70 |
static void handle_axis_event(int dx, int dy, int dz); |
| 71 |
static void generate_mouse_event(unsigned int type, |
| 72 |
int x, int y, int z, |
| 73 |
int dx, int dy, int dz, |
| 74 |
unsigned int button); |
| 75 |
bool _al_evdev_set_mouse_range(int x1, int y1, int x2, int y2); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
/* |
| 80 |
* Tools data |
| 81 |
*/ |
| 82 |
|
| 83 |
/* Tool mode |
| 84 |
* This mode is the behavior the user sees, it is not the way the device |
| 85 |
* reports the position. |
| 86 |
* Tools that send relative informations can only be in relative mode, |
| 87 |
* tools that send absolute informations can be either in absolute mode or in |
| 88 |
* relative mode */ |
| 89 |
typedef enum { |
| 90 |
MODE_RELATIVE, |
| 91 |
MODE_ABSOLUTE |
| 92 |
} MODE; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
typedef struct TOOL { |
| 97 |
int tool_id; /* One of the BTN_TOOL_... from linux/input.h */ |
| 98 |
MODE mode; /* Relative or absolute */ |
| 99 |
} TOOL; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static TOOL tools[] = { |
| 104 |
{ BTN_TOOL_MOUSE, MODE_RELATIVE }, |
| 105 |
{ BTN_TOOL_PEN, MODE_ABSOLUTE }, |
| 106 |
{ BTN_TOOL_RUBBER, MODE_ABSOLUTE }, |
| 107 |
{ BTN_TOOL_BRUSH, MODE_ABSOLUTE }, |
| 108 |
{ BTN_TOOL_PENCIL, MODE_ABSOLUTE }, |
| 109 |
{ BTN_TOOL_AIRBRUSH, MODE_ABSOLUTE }, |
| 110 |
{ BTN_TOOL_FINGER, MODE_ABSOLUTE }, |
| 111 |
{ BTN_TOOL_LENS, MODE_ABSOLUTE }, |
| 112 |
|
| 113 |
{ -1, MODE_ABSOLUTE } /* No tool */ |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
/* Default tool, in case the device does not send a tool id */ |
| 119 |
static TOOL *default_tool = tools + 0; |
| 120 |
|
| 121 |
static TOOL *no_tool = tools + (sizeof(tools)/sizeof(tools[0]) - 1); |
| 122 |
|
| 123 |
static TOOL *current_tool; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
/* find_tool: |
| 128 |
* Returns the tool with id 'tool_id' which is one of the BTN_TOOL_... defined |
| 129 |
* in linux/input.h. Returns 'default_tool' if not found. |
| 130 |
*/ |
| 131 |
static TOOL *find_tool(int tool_id) |
| 132 |
{ |
| 133 |
TOOL *t; |
| 134 |
for (t=tools; t!=no_tool; t++) { |
| 135 |
if (t->tool_id == tool_id) |
| 136 |
return t; |
| 137 |
} |
| 138 |
return default_tool; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
/* |
| 145 |
* Pointer axis |
| 146 |
*/ |
| 147 |
typedef struct AXIS { |
| 148 |
int in_min; /* For absolute mode */ |
| 149 |
int in_max; /* For absolute mode */ |
| 150 |
int out_min; /* For absolute mode */ |
| 151 |
int out_max; /* For absolute mode */ |
| 152 |
|
| 153 |
float speed; /* For set_mouse_speed */ |
| 154 |
int mickeys; /* For get_mouse_mickeys */ |
| 155 |
float scale; /* Scale factor, because tablet mice generate more movement |
| 156 |
than common mice */ |
| 157 |
|
| 158 |
int in_abs; /* Current absolute position, used for absolute input, |
| 159 |
whether the output is absolute or relative */ |
| 160 |
int out_abs; /* Position on the screen */ |
| 161 |
} AXIS; |
| 162 |
|
| 163 |
|
| 164 |
#define IN_RANGE(axis) ( (axis).in_max-(axis).in_min+1 ) |
| 165 |
#define OUT_RANGE(axis) ( (axis).out_max-(axis).out_min+1 ) |
| 166 |
|
| 167 |
|
| 168 |
/* in_to_screen: |
| 169 |
* maps an input absolute position to a screen position |
| 170 |
*/ |
| 171 |
static int in_to_screen(const AXIS *axis, int v) |
| 172 |
{ |
| 173 |
return (((v-axis->in_min) * OUT_RANGE(*axis)) / IN_RANGE(*axis)) + axis->out_min; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
/* rel_event: |
| 179 |
* returns the new screen position, given the input relative one. |
| 180 |
* The tool mode is always relative |
| 181 |
*/ |
| 182 |
static int rel_event(AXIS *axis, int v) |
| 183 |
{ |
| 184 |
/* When input only send relative events, the mode is always relative */ |
| 185 |
int ret = axis->out_abs + v*axis->speed; |
| 186 |
axis->mickeys += v; |
| 187 |
axis->in_abs += v; |
| 188 |
return ret; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
/* abs_event: |
| 194 |
* returns the new screen position, given the input absolute one, |
| 195 |
* and depending on the tool mode |
| 196 |
*/ |
| 197 |
static int abs_event(AXIS *axis, MODE mode, int v) |
| 198 |
{ |
| 199 |
if (mode == MODE_ABSOLUTE) { |
| 200 |
axis->mickeys = 0; /* No mickeys in absolute mode */ |
| 201 |
axis->in_abs = v; |
| 202 |
return in_to_screen(axis, v); |
| 203 |
} |
| 204 |
else { /* Input is absolute, but tool is relative */ |
| 205 |
int value = (v-axis->in_abs)*axis->scale; |
| 206 |
axis->mickeys += value; |
| 207 |
axis->in_abs = v; |
| 208 |
return axis->out_abs + value*axis->speed; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
/* get_axis_value: |
| 215 |
* returns the input absolute position |
| 216 |
*/ |
| 217 |
static void get_axis_value(int fd, AXIS *axis, int type) |
| 218 |
{ |
| 219 |
int abs[5]; |
| 220 |
int ret = ioctl(fd, EVIOCGABS(type), abs); |
| 221 |
if (ret>=0) { |
| 222 |
axis->in_abs = abs[0]; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
/* has_event: |
| 229 |
* returns true if the device generates the event |
| 230 |
*/ |
| 231 |
static int has_event(int fd, unsigned short type, unsigned short code) |
| 232 |
{ |
| 233 |
const unsigned int len = sizeof(unsigned long)*8; |
| 234 |
const unsigned int max = _ALLEGRO_MAX(EV_MAX, _ALLEGRO_MAX(KEY_MAX, _ALLEGRO_MAX(REL_MAX, _ALLEGRO_MAX(ABS_MAX, _ALLEGRO_MAX(LED_MAX, _ALLEGRO_MAX(SND_MAX, FF_MAX)))))); |
| 235 |
unsigned long bits[(max+len-1)/len]; |
| 236 |
if (ioctl(fd, EVIOCGBIT(type, max), bits)) { |
| 237 |
return (bits[code/len] >> (code%len)) & 1; |
| 238 |
} |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
/* get_num_buttons: |
| 245 |
* return the number of buttons |
| 246 |
*/ |
| 247 |
static int get_num_buttons(int fd) |
| 248 |
{ |
| 249 |
if (has_event(fd, EV_KEY, BTN_MIDDLE)) |
| 250 |
return 3; |
| 251 |
if (has_event(fd, EV_KEY, BTN_RIGHT)) |
| 252 |
return 2; |
| 253 |
if (has_event(fd, EV_KEY, BTN_MOUSE)) |
| 254 |
return 1; |
| 255 |
return 0; /* Not a mouse */ |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
/* The three axis: horizontal, vertical and wheel */ |
| 261 |
static AXIS x_axis; |
| 262 |
static AXIS y_axis; |
| 263 |
static AXIS z_axis; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
/* |
| 268 |
* Initialization functions |
| 269 |
*/ |
| 270 |
|
| 271 |
/* init_axis: |
| 272 |
* initialize an AXIS structure, from the device and the config file |
| 273 |
*/ |
| 274 |
static void init_axis(int fd, AXIS *axis, const char *name, const char *section, int type) |
| 275 |
{ |
| 276 |
#if 0 |
| 277 |
char tmp1[256]; /* config string */ |
| 278 |
char tmp2[256]; /* format string */ |
| 279 |
char tmp3[256]; /* Converted 'name' */ |
| 280 |
#endif |
| 281 |
int abs[5]; /* values given by the input */ |
| 282 |
int config_speed; |
| 283 |
|
| 284 |
(void)name; |
| 285 |
(void)section; |
| 286 |
|
| 287 |
config_speed = 1; |
| 288 |
axis->scale = 1; |
| 289 |
|
| 290 |
/* Ask the input */ |
| 291 |
if (ioctl(fd, EVIOCGABS(type), abs)>=0) { |
| 292 |
if (axis->in_min==0) |
| 293 |
axis->in_min=abs[1]; |
| 294 |
if (axis->in_max==0) |
| 295 |
axis->in_max=abs[2]; |
| 296 |
axis->in_abs = abs[0]; |
| 297 |
axis->scale = 320.0*config_speed/IN_RANGE(*axis); |
| 298 |
} |
| 299 |
if (axis->in_min>axis->in_max) { |
| 300 |
axis->in_min = axis->in_max = 0; |
| 301 |
axis->scale = 1; |
| 302 |
} |
| 303 |
|
| 304 |
axis->out_min = INT_MIN; |
| 305 |
axis->out_max = INT_MAX; |
| 306 |
|
| 307 |
axis->speed = 1; |
| 308 |
axis->mickeys = 0; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
/* init_tablet: |
| 314 |
* initialize the tools and axis |
| 315 |
*/ |
| 316 |
static void init_tablet(int fd) |
| 317 |
{ |
| 318 |
int default_abs = default_tool->mode==MODE_ABSOLUTE; |
| 319 |
|
| 320 |
if (default_abs) { |
| 321 |
default_tool->mode = MODE_ABSOLUTE; |
| 322 |
} |
| 323 |
else { |
| 324 |
default_tool->mode = MODE_RELATIVE; |
| 325 |
} |
| 326 |
|
| 327 |
init_axis(fd, &x_axis, "x", "mouse", ABS_X); |
| 328 |
init_axis(fd, &y_axis, "y", "mouse", ABS_Y); |
| 329 |
init_axis(fd, &z_axis, "z", "mouse", ABS_Z); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
/* |
| 336 |
* Process input functions |
| 337 |
*/ |
| 338 |
|
| 339 |
/* process_key: [fdwatch thread] |
| 340 |
* process events of type key (button clicks and vicinity events are currently |
| 341 |
* supported) |
| 342 |
*/ |
| 343 |
static void process_key(const struct input_event *event) |
| 344 |
{ |
| 345 |
switch (event->code) { |
| 346 |
/* Buttons click |
| 347 |
* if event->value is 1 the button has just been pressed |
| 348 |
* if event->value is 0 the button has just been released */ |
| 349 |
case BTN_LEFT: /* Mouse */ |
| 350 |
case BTN_TOUCH: /* Stylus */ |
| 351 |
handle_button_event(1, event->value); |
| 352 |
break; |
| 353 |
|
| 354 |
case BTN_RIGHT: /* Mouse */ |
| 355 |
case BTN_STYLUS: /* Stylus */ |
| 356 |
handle_button_event(2, event->value); |
| 357 |
break; |
| 358 |
|
| 359 |
case BTN_MIDDLE: /* Mouse */ |
| 360 |
case BTN_STYLUS2: /* Stylus */ |
| 361 |
handle_button_event(3, event->value); |
| 362 |
break; |
| 363 |
|
| 364 |
/* Vicinity events |
| 365 |
* if event->value is 1, the tool enters the tablet vicinity |
| 366 |
* if event->value is 0, the tool leaves the tablet vicinity */ |
| 367 |
case BTN_TOOL_MOUSE: |
| 368 |
case BTN_TOOL_PEN: |
| 369 |
case BTN_TOOL_RUBBER: |
| 370 |
case BTN_TOOL_BRUSH: |
| 371 |
case BTN_TOOL_PENCIL: |
| 372 |
case BTN_TOOL_AIRBRUSH: |
| 373 |
case BTN_TOOL_FINGER: |
| 374 |
case BTN_TOOL_LENS: |
| 375 |
if (event->value) { |
| 376 |
current_tool = find_tool(event->code); |
| 377 |
get_axis_value(the_mouse.fd, &x_axis, ABS_X); |
| 378 |
get_axis_value(the_mouse.fd, &y_axis, ABS_Y); |
| 379 |
get_axis_value(the_mouse.fd, &z_axis, ABS_Z); |
| 380 |
#ifdef ABS_WHEEL /* absent in 2.2.x */ |
| 381 |
get_axis_value(the_mouse.fd, &z_axis, ABS_WHEEL); |
| 382 |
#endif |
| 383 |
} |
| 384 |
else { |
| 385 |
current_tool = no_tool; |
| 386 |
} |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
/* [fdwatch thread] */ |
| 393 |
static void handle_button_event(unsigned int button, bool is_down) |
| 394 |
{ |
| 395 |
unsigned int event_type; |
| 396 |
|
| 397 |
if (is_down) { |
| 398 |
the_mouse.state.buttons |= (1 << (button-1)); |
| 399 |
event_type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN; |
| 400 |
} |
| 401 |
else { |
| 402 |
the_mouse.state.buttons &=~ (1 << (button-1)); |
| 403 |
event_type = ALLEGRO_EVENT_MOUSE_BUTTON_UP; |
| 404 |
} |
| 405 |
|
| 406 |
generate_mouse_event( |
| 407 |
event_type, |
| 408 |
the_mouse.state.x, the_mouse.state.y, the_mouse.state.z, |
| 409 |
0, 0, 0, |
| 410 |
button); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
/* process_rel: [fdwatch thread] |
| 417 |
* process relative events (x, y and z movement are currently supported) |
| 418 |
*/ |
| 419 |
static void process_rel(const struct input_event *event) |
| 420 |
{ |
| 421 |
/* The device can send a report when there's no tool */ |
| 422 |
if (current_tool!=no_tool) { |
| 423 |
switch (event->code) { |
| 424 |
case REL_X: |
| 425 |
x_axis.out_abs = rel_event(&x_axis, event->value); |
| 426 |
handle_axis_event(x_axis.mickeys, 0, 0); |
| 427 |
x_axis.mickeys = 0; |
| 428 |
break; |
| 429 |
|
| 430 |
case REL_Y: |
| 431 |
y_axis.out_abs = rel_event(&y_axis, event->value); |
| 432 |
handle_axis_event(0, y_axis.mickeys, 0); |
| 433 |
y_axis.mickeys = 0; |
| 434 |
break; |
| 435 |
|
| 436 |
#ifdef REL_WHEEL /* absent in 2.2.x */ |
| 437 |
case REL_WHEEL: |
| 438 |
#endif |
| 439 |
case REL_Z: |
| 440 |
z_axis.out_abs = rel_event(&z_axis, event->value); |
| 441 |
handle_axis_event(0, 0, z_axis.mickeys); |
| 442 |
z_axis.mickeys = 0; |
| 443 |
break; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
/* process_abs: [fdwatch thread] |
| 451 |
* process absolute events (x, y and z movement are currently supported) |
| 452 |
* TODO: missing handle_axis_event calls |
| 453 |
*/ |
| 454 |
static void process_abs(const struct input_event *event) |
| 455 |
{ |
| 456 |
/* The device can send a report when there's no tool */ |
| 457 |
if (current_tool!=no_tool) { |
| 458 |
switch (event->code) { |
| 459 |
case ABS_X: |
| 460 |
x_axis.out_abs = abs_event(&x_axis, current_tool->mode, event->value); |
| 461 |
break; |
| 462 |
|
| 463 |
case ABS_Y: |
| 464 |
y_axis.out_abs = abs_event(&y_axis, current_tool->mode, event->value); |
| 465 |
break; |
| 466 |
|
| 467 |
#ifdef ABS_WHEEL /* absent in 2.2.x */ |
| 468 |
case ABS_WHEEL: |
| 469 |
#endif |
| 470 |
case ABS_Z: |
| 471 |
z_axis.out_abs = abs_event(&z_axis, current_tool->mode, event->value); |
| 472 |
break; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
/* [fdwatch thread] */ |
| 480 |
static void handle_axis_event(int dx, int dy, int dz) |
| 481 |
{ |
| 482 |
if (current_tool != no_tool) { |
| 483 |
x_axis.out_abs = _ALLEGRO_CLAMP(x_axis.out_min, x_axis.out_abs, x_axis.out_max); |
| 484 |
y_axis.out_abs = _ALLEGRO_CLAMP(y_axis.out_min, y_axis.out_abs, y_axis.out_max); |
| 485 |
/* There's no range for z */ |
| 486 |
|
| 487 |
the_mouse.state.x = x_axis.out_abs; |
| 488 |
the_mouse.state.y = y_axis.out_abs; |
| 489 |
the_mouse.state.z = z_axis.out_abs * al_get_mouse_wheel_precision(); |
| 490 |
|
| 491 |
dz *= al_get_mouse_wheel_precision(); |
| 492 |
|
| 493 |
generate_mouse_event( |
| 494 |
ALLEGRO_EVENT_MOUSE_AXES, |
| 495 |
the_mouse.state.x, the_mouse.state.y, the_mouse.state.z, |
| 496 |
dx, dy, dz, |
| 497 |
0); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
/* open_mouse_device: |
| 504 |
* Open the specified device file and check that it is a mouse device. |
| 505 |
* Returns the file descriptor if successful or a negative number on error. |
| 506 |
*/ |
| 507 |
static int open_mouse_device (const char *device_file) |
| 508 |
{ |
| 509 |
int fd; |
| 510 |
|
| 511 |
fd = open (device_file, O_RDONLY | O_NONBLOCK); |
| 512 |
if (fd >= 0) { |
| 513 |
ALLEGRO_DEBUG("Opened device %s\n", device_file); |
| 514 |
/* The device is a mouse if it has a BTN_MOUSE */ |
| 515 |
if (has_event(fd, EV_KEY, BTN_MOUSE)) { |
| 516 |
ALLEGRO_DEBUG("Device %s was a mouse.\n", device_file); |
| 517 |
} |
| 518 |
else { |
| 519 |
ALLEGRO_DEBUG("Device %s was not mouse, closing.\n", device_file); |
| 520 |
close(fd); |
| 521 |
fd = -1; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
return fd; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
/* mouse_init: |
| 531 |
* Here we open the mouse device, initialise anything that needs it, |
| 532 |
* and chain to the framework init routine. |
| 533 |
*/ |
| 534 |
static bool mouse_init (void) |
| 535 |
{ |
| 536 |
/* |
| 537 |
char tmp1[128], tmp2[128]; |
| 538 |
const char *udevice; |
| 539 |
*/ |
| 540 |
|
| 541 |
/* Set the current tool */ |
| 542 |
current_tool = default_tool; |
| 543 |
|
| 544 |
/* try several /dev/input/event<n> |
| 545 |
* devices. */ |
| 546 |
const char *device_name[] = { "/dev/input/event0", |
| 547 |
"/dev/input/event1", |
| 548 |
"/dev/input/event2", |
| 549 |
"/dev/input/event3", |
| 550 |
NULL }; |
| 551 |
int i; |
| 552 |
|
| 553 |
ALLEGRO_DEBUG("Trying /dev/input/event[0-3] devices\n"); |
| 554 |
|
| 555 |
for (i=0; device_name[i]; i++) { |
| 556 |
the_mouse.fd = open_mouse_device (device_name[i]); |
| 557 |
if (the_mouse.fd >= 0) |
| 558 |
break; |
| 559 |
} |
| 560 |
|
| 561 |
if (!device_name[i]) { |
| 562 |
return false; |
| 563 |
} |
| 564 |
|
| 565 |
/* Init the tablet data */ |
| 566 |
init_tablet(the_mouse.fd); |
| 567 |
|
| 568 |
/* Initialise the mouse object for use as an event source. */ |
| 569 |
_al_event_source_init(&the_mouse.parent.es); |
| 570 |
|
| 571 |
/* Start watching for data on the fd. */ |
| 572 |
_al_unix_start_watching_fd(the_mouse.fd, process_new_data, &the_mouse); |
| 573 |
|
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
/* mouse_exit: |
| 580 |
* Chain to the framework, then uninitialise things. |
| 581 |
*/ |
| 582 |
static void mouse_exit (void) |
| 583 |
{ |
| 584 |
_al_unix_stop_watching_fd(the_mouse.fd); |
| 585 |
|
| 586 |
_al_event_source_free(&the_mouse.parent.es); |
| 587 |
|
| 588 |
close(the_mouse.fd); |
| 589 |
|
| 590 |
/* This may help catch bugs in the user program, since the pointer |
| 591 |
* we return to the user is always the same. |
| 592 |
*/ |
| 593 |
memset(&the_mouse, 0, sizeof the_mouse); |
| 594 |
the_mouse.fd = -1; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
/* mouse_get_mouse: |
| 600 |
* Returns the address of a ALLEGRO_MOUSE structure representing the mouse. |
| 601 |
*/ |
| 602 |
static ALLEGRO_MOUSE *mouse_get_mouse(void) |
| 603 |
{ |
| 604 |
return (ALLEGRO_MOUSE *)&the_mouse; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
/* mouse_get_mouse_num_buttons: |
| 610 |
* Return the number of buttons on the mouse. |
| 611 |
*/ |
| 612 |
static unsigned int mouse_get_mouse_num_buttons(void) |
| 613 |
{ |
| 614 |
ASSERT(the_mouse.fd >= 0); |
| 615 |
|
| 616 |
return get_num_buttons(the_mouse.fd); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
/* mouse_get_mouse_num_axes: |
| 622 |
* Return the number of axes on the mouse. |
| 623 |
*/ |
| 624 |
static unsigned int mouse_get_mouse_num_axes(void) |
| 625 |
{ |
| 626 |
ASSERT(the_mouse.fd >= 0); |
| 627 |
|
| 628 |
/* XXX get the right number later */ |
| 629 |
return 3; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
/* mouse_set_mouse_xy: |
| 635 |
* |
| 636 |
*/ |
| 637 |
static bool mouse_set_mouse_xy(ALLEGRO_DISPLAY *display, int x, int y) |
| 638 |
{ |
| 639 |
(void)display; |
| 640 |
|
| 641 |
_al_event_source_lock(&the_mouse.parent.es); |
| 642 |
{ |
| 643 |
int dx, dy; |
| 644 |
|
| 645 |
x_axis.out_abs = _ALLEGRO_CLAMP(x_axis.out_min, x, x_axis.out_max); |
| 646 |
y_axis.out_abs = _ALLEGRO_CLAMP(y_axis.out_min, y, y_axis.out_max); |
| 647 |
x_axis.mickeys = 0; |
| 648 |
y_axis.mickeys = 0; |
| 649 |
|
| 650 |
dx = x_axis.out_abs - the_mouse.state.x; |
| 651 |
dy = y_axis.out_abs - the_mouse.state.y; |
| 652 |
|
| 653 |
if ((dx != 0) && (dy != 0)) { |
| 654 |
the_mouse.state.x = x_axis.out_abs; |
| 655 |
the_mouse.state.y = y_axis.out_abs; |
| 656 |
|
| 657 |
generate_mouse_event( |
| 658 |
ALLEGRO_EVENT_MOUSE_AXES, |
| 659 |
the_mouse.state.x, the_mouse.state.y, the_mouse.state.z, |
| 660 |
dx, dy, 0, |
| 661 |
0); |
| 662 |
} |
| 663 |
} |
| 664 |
_al_event_source_unlock(&the_mouse.parent.es); |
| 665 |
|
| 666 |
return true; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
/* mouse_set_mouse_axis: |
| 672 |
* |
| 673 |
* Number of mickeys to cross the screen horizontally: speed * 320. |
| 674 |
*/ |
| 675 |
static bool mouse_set_mouse_axis(int which, int z) |
| 676 |
{ |
| 677 |
if (which != 2) { |
| 678 |
return false; |
| 679 |
} |
| 680 |
|
| 681 |
_al_event_source_lock(&the_mouse.parent.es); |
| 682 |
{ |
| 683 |
int dz; |
| 684 |
|
| 685 |
z_axis.out_abs = z; |
| 686 |
|
| 687 |
dz = z_axis.out_abs - the_mouse.state.z; |
| 688 |
|
| 689 |
if (dz != 0) { |
| 690 |
the_mouse.state.z = z_axis.out_abs; |
| 691 |
|
| 692 |
generate_mouse_event( |
| 693 |
ALLEGRO_EVENT_MOUSE_AXES, |
| 694 |
the_mouse.state.x, the_mouse.state.y, the_mouse.state.z, |
| 695 |
0, 0, dz, |
| 696 |
0); |
| 697 |
} |
| 698 |
} |
| 699 |
_al_event_source_unlock(&the_mouse.parent.es); |
| 700 |
|
| 701 |
return true; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
/* mouse_set_mouse_range: |
| 707 |
* |
| 708 |
*/ |
| 709 |
bool _al_evdev_set_mouse_range(int x1, int y1, int x2, int y2) |
| 710 |
{ |
| 711 |
_al_event_source_lock(&the_mouse.parent.es); |
| 712 |
{ |
| 713 |
int dx, dy; |
| 714 |
|
| 715 |
x_axis.out_min = x1; |
| 716 |
y_axis.out_min = y1; |
| 717 |
x_axis.out_max = x2; |
| 718 |
y_axis.out_max = y2; |
| 719 |
|
| 720 |
x_axis.out_abs = _ALLEGRO_CLAMP(x_axis.out_min, x_axis.out_abs, x_axis.out_max); |
| 721 |
y_axis.out_abs = _ALLEGRO_CLAMP(y_axis.out_min, y_axis.out_abs, y_axis.out_max); |
| 722 |
|
| 723 |
dx = x_axis.out_abs - the_mouse.state.x; |
| 724 |
dy = y_axis.out_abs - the_mouse.state.y; |
| 725 |
|
| 726 |
if ((dx != 0) && (dy != 0)) { |
| 727 |
the_mouse.state.x = x_axis.out_abs; |
| 728 |
the_mouse.state.y = y_axis.out_abs; |
| 729 |
|
| 730 |
generate_mouse_event( |
| 731 |
ALLEGRO_EVENT_MOUSE_AXES, |
| 732 |
the_mouse.state.x, the_mouse.state.y, the_mouse.state.z, |
| 733 |
dx, dy, 0, |
| 734 |
0); |
| 735 |
} |
| 736 |
} |
| 737 |
_al_event_source_unlock(&the_mouse.parent.es); |
| 738 |
|
| 739 |
return true; |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
/* mouse_get_state: |
| 745 |
* Copy the current mouse state into RET_STATE, with any necessary locking. |
| 746 |
*/ |
| 747 |
static void mouse_get_state(ALLEGRO_MOUSE_STATE *ret_state) |
| 748 |
{ |
| 749 |
_al_event_source_lock(&the_mouse.parent.es); |
| 750 |
{ |
| 751 |
*ret_state = the_mouse.state; |
| 752 |
} |
| 753 |
_al_event_source_unlock(&the_mouse.parent.es); |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
/* process_new_data: [fdwatch thread] |
| 759 |
* Process new data arriving in the keyboard's fd. |
| 760 |
*/ |
| 761 |
static void process_new_data(void *data) |
| 762 |
{ |
| 763 |
ASSERT((AL_MOUSE_EVDEV *)data == &the_mouse); |
| 764 |
|
| 765 |
_al_event_source_lock(&the_mouse.parent.es); |
| 766 |
{ |
| 767 |
struct input_event events[32]; |
| 768 |
int bytes, nr, i; |
| 769 |
|
| 770 |
while ((bytes = read(the_mouse.fd, &events, sizeof events)) > 0) { |
| 771 |
nr = bytes / sizeof(events[0]); |
| 772 |
for (i = 0; i < nr; i++) { |
| 773 |
process_event(&events[i]); |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
_al_event_source_unlock(&the_mouse.parent.es); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
/* [fdwatch thread] */ |
| 783 |
static void process_event(struct input_event *event) |
| 784 |
{ |
| 785 |
switch (event->type) { |
| 786 |
case EV_KEY: |
| 787 |
process_key(event); |
| 788 |
break; |
| 789 |
case EV_REL: |
| 790 |
process_rel(event); |
| 791 |
break; |
| 792 |
case EV_ABS: |
| 793 |
process_abs(event); |
| 794 |
break; |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
/* [fdwatch thread] */ |
| 801 |
static void generate_mouse_event(unsigned int type, |
| 802 |
int x, int y, int z, |
| 803 |
int dx, int dy, int dz, |
| 804 |
unsigned int button) |
| 805 |
{ |
| 806 |
ALLEGRO_EVENT event; |
| 807 |
|
| 808 |
if (!_al_event_source_needs_to_generate_event(&the_mouse.parent.es)) |
| 809 |
return; |
| 810 |
|
| 811 |
event.mouse.type = type; |
| 812 |
event.mouse.timestamp = al_get_time(); |
| 813 |
event.mouse.display = NULL; |
| 814 |
event.mouse.x = x; |
| 815 |
event.mouse.y = y; |
| 816 |
event.mouse.z = z; |
| 817 |
event.mouse.dx = dx; |
| 818 |
event.mouse.dy = dy; |
| 819 |
event.mouse.dz = dz; |
| 820 |
event.mouse.button = button; |
| 821 |
event.mouse.pressure = 0.0; /* TODO */ |
| 822 |
_al_event_source_emit_event(&the_mouse.parent.es, &event); |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
/* the driver vtable */ |
| 828 |
ALLEGRO_MOUSE_DRIVER _al_mousedrv_linux_evdev = |
| 829 |
{ |
| 830 |
AL_MOUSEDRV_LINUX_EVDEV, |
| 831 |
"", |
| 832 |
"", |
| 833 |
"Linux EVDEV mouse (and tablet)", |
| 834 |
mouse_init, |
| 835 |
mouse_exit, |
| 836 |
mouse_get_mouse, |
| 837 |
mouse_get_mouse_num_buttons, |
| 838 |
mouse_get_mouse_num_axes, |
| 839 |
mouse_set_mouse_xy, |
| 840 |
mouse_set_mouse_axis, |
| 841 |
mouse_get_state |
| 842 |
}; |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
#endif /* ALLEGRO_HAVE_LINUX_INPUT_H */ |
| 847 |
|
| 848 |
/* |
| 849 |
* Local Variables: |
| 850 |
* c-basic-offset: 3 |
| 851 |
* indent-tabs-mode: nil |
| 852 |
* End: |
| 853 |
*/ |