23 #ifdef SDL_INPUT_LINUXEV
34 #define _THIS SDL_EVDEV_PrivateData *_this
40 #include <sys/ioctl.h>
47 #include "../../core/linux/SDL_udev.h"
49 #include "../../events/SDL_events_c.h"
56 static SDL_Scancode SDL_EVDEV_translate_keycode(
int keycode);
57 static void SDL_EVDEV_sync_device(SDL_evdevlist_item *item);
58 static int SDL_EVDEV_device_removed(
const char *devpath);
61 static int SDL_EVDEV_device_added(
const SDL_UDEV_deviceclass devclass,
const char *devpath);
62 void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class,
const char *devpath);
317 static Uint8 EVDEV_MouseButtons[] = {
340 if (SDL_UDEV_Init() < 0) {
347 if ( SDL_UDEV_AddCallback(SDL_EVDEV_udev_callback) < 0) {
360 _this->ref_count += 1;
372 _this->ref_count -= 1;
374 if (
_this->ref_count < 1) {
377 SDL_UDEV_DelCallback(SDL_EVDEV_udev_callback);
383 SDL_EVDEV_device_removed(
_this->first->path);
396 void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class,
const char *devpath)
398 SDL_EVDEV_deviceclass devclass;
400 if (devpath ==
NULL) {
406 case SDL_UDEV_DEVICE_MOUSE:
407 devclass = SDL_EVDEV_DEVICE_MOUSE;
410 case SDL_UDEV_DEVICE_KEYBOARD:
411 devclass = SDL_EVDEV_DEVICE_KEYBOARD;
420 case SDL_UDEV_DEVICEADDED:
421 SDL_EVDEV_device_added(devclass, devpath);
424 case SDL_UDEV_DEVICEREMOVED:
425 SDL_EVDEV_device_removed(devpath);
440 struct input_event events[32];
442 SDL_evdevlist_item *item;
451 for (item =
_this->first; item !=
NULL; item = item->next) {
452 while ((len = read(item->fd, events, (
sizeof events))) > 0) {
453 len /=
sizeof(events[0]);
454 for (i = 0; i <
len; ++
i) {
455 switch(item->devclass) {
456 case SDL_EVDEV_DEVICE_KEYBOARD:
457 switch (events[i].
type) {
459 scan_code = SDL_EVDEV_translate_keycode(events[i].
code);
461 if (events[i].
value == 0) {
464 else if (events[i].
value == 1) {
467 else if (events[i].
value == 2) {
479 case SDL_EVDEV_DEVICE_MOUSE:
481 switch (events[i].type) {
483 mouse_button = events[
i].code - BTN_MOUSE;
484 if (mouse_button >= 0 && mouse_button <
SDL_arraysize(EVDEV_MouseButtons)) {
485 if (events[i].
value == 0) {
488 else if (events[i].
value == 1) {
494 switch(events[i].
code) {
506 switch(events[i].code) {
534 switch (events[i].type) {
536 switch (events[i].
code) {
538 SDL_EVDEV_sync_device(item);
551 SDL_EVDEV_translate_keycode(
int keycode)
556 scancode = EVDEV_Keycodes[keycode];
559 SDL_Log(
"The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> EVDEV KeyCode %d \n", keycode);
565 SDL_EVDEV_sync_device(SDL_evdevlist_item *item)
572 SDL_EVDEV_device_added(
const SDL_UDEV_deviceclass devclass,
const char *devpath)
574 SDL_evdevlist_item *item;
577 for (item =
_this->first; item !=
NULL; item = item->next) {
578 if (strcmp(devpath, item->path) == 0) {
583 item = (SDL_evdevlist_item *)
SDL_calloc(1,
sizeof (SDL_evdevlist_item));
588 item->devclass = devclass;
591 item->fd = open(devpath, O_RDONLY, 0);
598 if (item->path ==
NULL) {
605 fcntl(item->fd, F_SETFL, O_NONBLOCK);
610 _this->last->next = item;
614 SDL_EVDEV_sync_device(item);
616 return _this->numdevices++;
621 SDL_EVDEV_device_removed(
const char *devpath)
623 SDL_evdevlist_item *item;
624 SDL_evdevlist_item *prev =
NULL;
626 for (item =
_this->first; item !=
NULL; item = item->next) {
628 if ( strcmp(devpath, item->path) ==0 ) {
630 prev->next = item->next;
633 _this->first = item->next;
635 if (item ==
_this->last) {
SDL_Mouse * SDL_GetMouse(void)
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size)
DECLSPEC void SDLCALL SDL_free(void *mem)
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
static SDL_VideoDevice * _this
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
DECLSPEC char *SDLCALL SDL_strdup(const char *str)
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
#define SDL_BUTTON_MIDDLE
#define SDL_assert(condition)
#define SDL_OutOfMemory()
#define SDL_arraysize(array)
DECLSPEC void SDLCALL SDL_Log(const char *fmt,...)
Log a message with SDL_LOG_CATEGORY_APPLICATION and SDL_LOG_PRIORITY_INFO.
EGLSurface EGLint void ** value
uint8_t Uint8
An unsigned 8-bit integer type.
int SDL_SendMouseWheel(SDL_Window *window, SDL_MouseID mouseID, int x, int y)
int SDL_SendMouseButton(SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
SDL_Scancode
The SDL keyboard scancode representation.