src/core/linux/SDL_evdev.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 11094 093b58f5de3c
child 12160 c63903ab7150
permissions -rw-r--r--
Updated copyright for 2018
gabomdq@7753
     1
/*
gabomdq@7753
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
gabomdq@7753
     4
gabomdq@7753
     5
  This software is provided 'as-is', without any express or implied
gabomdq@7753
     6
  warranty.  In no event will the authors be held liable for any damages
gabomdq@7753
     7
  arising from the use of this software.
gabomdq@7753
     8
gabomdq@7753
     9
  Permission is granted to anyone to use this software for any purpose,
gabomdq@7753
    10
  including commercial applications, and to alter it and redistribute it
gabomdq@7753
    11
  freely, subject to the following restrictions:
gabomdq@7753
    12
gabomdq@7753
    13
  1. The origin of this software must not be misrepresented; you must not
gabomdq@7753
    14
     claim that you wrote the original software. If you use this software
gabomdq@7753
    15
     in a product, an acknowledgment in the product documentation would be
gabomdq@7753
    16
     appreciated but is not required.
gabomdq@7753
    17
  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@7753
    18
     misrepresented as being the original software.
gabomdq@7753
    19
  3. This notice may not be removed or altered from any source distribution.
gabomdq@7753
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
gabomdq@7753
    22
gabomdq@7753
    23
#ifdef SDL_INPUT_LINUXEV
gabomdq@7753
    24
gabomdq@7753
    25
/* This is based on the linux joystick driver */
gabomdq@7753
    26
/* References: https://www.kernel.org/doc/Documentation/input/input.txt 
gabomdq@7753
    27
 *             https://www.kernel.org/doc/Documentation/input/event-codes.txt
gabomdq@7753
    28
 *             /usr/include/linux/input.h
gabomdq@7753
    29
 *             The evtest application is also useful to debug the protocol
gabomdq@7753
    30
 */
gabomdq@7753
    31
gabomdq@7753
    32
#include "SDL_evdev.h"
slouken@10797
    33
#include "SDL_evdev_kbd.h"
gabomdq@7753
    34
gabomdq@7753
    35
#include <sys/stat.h>
gabomdq@7753
    36
#include <unistd.h>
gabomdq@7753
    37
#include <fcntl.h>
gabomdq@7753
    38
#include <sys/ioctl.h>
gabomdq@7778
    39
#include <linux/input.h>
gabomdq@7809
    40
gabomdq@7753
    41
#include "SDL.h"
gabomdq@7753
    42
#include "SDL_assert.h"
gabomdq@7753
    43
#include "SDL_endian.h"
gabomdq@7753
    44
#include "SDL_scancode.h"
gabomdq@7753
    45
#include "../../events/SDL_events_c.h"
slouken@10433
    46
#include "../../events/scancodes_linux.h" /* adds linux_scancode_table */
slouken@10797
    47
#include "../../core/linux/SDL_udev.h"
gabomdq@7753
    48
slouken@10785
    49
/* These are not defined in older Linux kernel headers */
gabomdq@7753
    50
#ifndef SYN_DROPPED
gabomdq@7753
    51
#define SYN_DROPPED 3
gabomdq@7753
    52
#endif
slouken@10566
    53
#ifndef ABS_MT_SLOT
slouken@10566
    54
#define ABS_MT_SLOT         0x2f
slouken@10566
    55
#define ABS_MT_POSITION_X   0x35
slouken@10566
    56
#define ABS_MT_POSITION_Y   0x36
slouken@10566
    57
#define ABS_MT_TRACKING_ID  0x39
slouken@10566
    58
#endif
slouken@10566
    59
slouken@10433
    60
typedef struct SDL_evdevlist_item
slouken@10433
    61
{
slouken@10433
    62
    char *path;
slouken@10433
    63
    int fd;
slouken@10797
    64
slouken@10433
    65
    /* TODO: use this for every device, not just touchscreen */
slouken@10433
    66
    int out_of_sync;
slouken@10797
    67
slouken@10433
    68
    /* TODO: expand on this to have data for every possible class (mouse,
slouken@10433
    69
       keyboard, touchpad, etc.). Also there's probably some things in here we
slouken@10433
    70
       can pull out to the SDL_evdevlist_item i.e. name */
slouken@10433
    71
    int is_touchscreen;
slouken@10433
    72
    struct {
slouken@10433
    73
        char* name;
slouken@10797
    74
slouken@10433
    75
        int min_x, max_x, range_x;
slouken@10433
    76
        int min_y, max_y, range_y;
slouken@10797
    77
slouken@10433
    78
        int max_slots;
slouken@10433
    79
        int current_slot;
slouken@10433
    80
        struct {
slouken@10433
    81
            enum {
slouken@10433
    82
                EVDEV_TOUCH_SLOTDELTA_NONE = 0,
slouken@10433
    83
                EVDEV_TOUCH_SLOTDELTA_DOWN,
slouken@10433
    84
                EVDEV_TOUCH_SLOTDELTA_UP,
slouken@10433
    85
                EVDEV_TOUCH_SLOTDELTA_MOVE
slouken@10433
    86
            } delta;
slouken@10433
    87
            int tracking_id;
slouken@10433
    88
            int x, y;
slouken@10433
    89
        } * slots;
slouken@10433
    90
    } * touchscreen_data;
slouken@10797
    91
slouken@10433
    92
    struct SDL_evdevlist_item *next;
slouken@10433
    93
} SDL_evdevlist_item;
slouken@10433
    94
slouken@10433
    95
typedef struct SDL_EVDEV_PrivateData
slouken@10433
    96
{
slouken@10797
    97
    int ref_count;
slouken@10797
    98
    int num_devices;
slouken@10433
    99
    SDL_evdevlist_item *first;
slouken@10433
   100
    SDL_evdevlist_item *last;
slouken@10797
   101
    SDL_EVDEV_keyboard_state *kbd;
slouken@10433
   102
} SDL_EVDEV_PrivateData;
slouken@10433
   103
slouken@10433
   104
#define _THIS SDL_EVDEV_PrivateData *_this
slouken@10433
   105
static _THIS = NULL;
slouken@10433
   106
gabomdq@7755
   107
static SDL_Scancode SDL_EVDEV_translate_keycode(int keycode);
gabomdq@7755
   108
static void SDL_EVDEV_sync_device(SDL_evdevlist_item *item);
slouken@10433
   109
static int SDL_EVDEV_device_removed(const char *dev_path);
gabomdq@7753
   110
gabomdq@7755
   111
#if SDL_USE_LIBUDEV
slouken@10445
   112
static int SDL_EVDEV_device_added(const char *dev_path, int udev_class);
philipp@11094
   113
static void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class,
slouken@10445
   114
    const char *dev_path);
gabomdq@7755
   115
#endif /* SDL_USE_LIBUDEV */
gabomdq@7753
   116
gabomdq@7753
   117
static Uint8 EVDEV_MouseButtons[] = {
gabomdq@7753
   118
    SDL_BUTTON_LEFT,            /*  BTN_LEFT        0x110 */
gabomdq@7753
   119
    SDL_BUTTON_RIGHT,           /*  BTN_RIGHT       0x111 */
gabomdq@7753
   120
    SDL_BUTTON_MIDDLE,          /*  BTN_MIDDLE      0x112 */
gabomdq@7753
   121
    SDL_BUTTON_X1,              /*  BTN_SIDE        0x113 */
gabomdq@7753
   122
    SDL_BUTTON_X2,              /*  BTN_EXTRA       0x114 */
gabomdq@7753
   123
    SDL_BUTTON_X2 + 1,          /*  BTN_FORWARD     0x115 */
gabomdq@7753
   124
    SDL_BUTTON_X2 + 2,          /*  BTN_BACK        0x116 */
gabomdq@7753
   125
    SDL_BUTTON_X2 + 3           /*  BTN_TASK        0x117 */
gabomdq@7753
   126
};
gabomdq@7753
   127
gabomdq@7753
   128
int
gabomdq@7753
   129
SDL_EVDEV_Init(void)
gabomdq@7753
   130
{
slouken@10445
   131
    if (_this == NULL) {
slouken@10445
   132
        _this = (SDL_EVDEV_PrivateData*)SDL_calloc(1, sizeof(*_this));
slouken@10445
   133
        if (_this == NULL) {
gabomdq@7753
   134
            return SDL_OutOfMemory();
gabomdq@7753
   135
        }
gabomdq@7753
   136
gabomdq@7753
   137
#if SDL_USE_LIBUDEV
slouken@10445
   138
        if (SDL_UDEV_Init() < 0) {
slouken@10445
   139
            SDL_free(_this);
gabomdq@7753
   140
            _this = NULL;
gabomdq@7753
   141
            return -1;
gabomdq@7753
   142
        }
gabomdq@7753
   143
gabomdq@7753
   144
        /* Set up the udev callback */
slouken@9780
   145
        if (SDL_UDEV_AddCallback(SDL_EVDEV_udev_callback) < 0) {
slouken@10433
   146
            SDL_UDEV_Quit();
slouken@10445
   147
            SDL_free(_this);
slouken@10433
   148
            _this = NULL;
gabomdq@7753
   149
            return -1;
gabomdq@7753
   150
        }
slouken@10797
   151
gabomdq@7753
   152
        /* Force a scan to build the initial device list */
gabomdq@7753
   153
        SDL_UDEV_Scan();
gabomdq@7753
   154
#else
gabomdq@7753
   155
        /* TODO: Scan the devices manually, like a caveman */
gabomdq@7753
   156
#endif /* SDL_USE_LIBUDEV */
slouken@10784
   157
slouken@10797
   158
        _this->kbd = SDL_EVDEV_kbd_init();
slouken@10797
   159
    }
slouken@10784
   160
gabomdq@7753
   161
    _this->ref_count += 1;
slouken@10797
   162
slouken@10433
   163
    return 0;
gabomdq@7753
   164
}
gabomdq@7753
   165
gabomdq@7753
   166
void
gabomdq@7753
   167
SDL_EVDEV_Quit(void)
gabomdq@7753
   168
{
slouken@10445
   169
    if (_this == NULL) {
gabomdq@7753
   170
        return;
gabomdq@7753
   171
    }
slouken@10797
   172
gabomdq@7753
   173
    _this->ref_count -= 1;
slouken@10797
   174
slouken@10445
   175
    if (_this->ref_count < 1) {
gabomdq@7753
   176
#if SDL_USE_LIBUDEV
slouken@10445
   177
        SDL_UDEV_DelCallback(SDL_EVDEV_udev_callback);
gabomdq@7753
   178
        SDL_UDEV_Quit();
gabomdq@7753
   179
#endif /* SDL_USE_LIBUDEV */
slouken@10797
   180
slouken@10797
   181
        SDL_EVDEV_kbd_quit(_this->kbd);
slouken@10797
   182
gabomdq@7753
   183
        /* Remove existing devices */
gabomdq@7753
   184
        while(_this->first != NULL) {
slouken@10445
   185
            SDL_EVDEV_device_removed(_this->first->path);
gabomdq@7753
   186
        }
slouken@10797
   187
slouken@10445
   188
        SDL_assert(_this->first == NULL);
slouken@10445
   189
        SDL_assert(_this->last == NULL);
slouken@10445
   190
        SDL_assert(_this->num_devices == 0);
slouken@10797
   191
slouken@10445
   192
        SDL_free(_this);
gabomdq@7753
   193
        _this = NULL;
gabomdq@7753
   194
    }
gabomdq@7753
   195
}
gabomdq@7753
   196
gabomdq@7755
   197
#if SDL_USE_LIBUDEV
philipp@11094
   198
static void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_event, int udev_class,
slouken@10445
   199
    const char* dev_path)
gabomdq@7753
   200
{
slouken@10445
   201
    if (dev_path == NULL) {
gabomdq@7753
   202
        return;
gabomdq@7753
   203
    }
slouken@10797
   204
slouken@10445
   205
    switch(udev_event) {
slouken@7788
   206
    case SDL_UDEV_DEVICEADDED:
slouken@10445
   207
        if (!(udev_class & (SDL_UDEV_DEVICE_MOUSE | SDL_UDEV_DEVICE_KEYBOARD |
slouken@10445
   208
            SDL_UDEV_DEVICE_TOUCHSCREEN)))
slouken@9779
   209
            return;
slouken@10433
   210
slouken@10445
   211
        SDL_EVDEV_device_added(dev_path, udev_class);
slouken@10433
   212
        break;  
slouken@10433
   213
    case SDL_UDEV_DEVICEREMOVED:
slouken@10445
   214
        SDL_EVDEV_device_removed(dev_path);
slouken@7788
   215
        break;
slouken@7788
   216
    default:
slouken@7788
   217
        break;
gabomdq@7753
   218
    }
gabomdq@7753
   219
}
slouken@10433
   220
#endif /* SDL_USE_LIBUDEV */
gabomdq@7753
   221
gabomdq@7753
   222
void 
gabomdq@7753
   223
SDL_EVDEV_Poll(void)
gabomdq@7753
   224
{
gabomdq@7753
   225
    struct input_event events[32];
slouken@10433
   226
    int i, j, len;
gabomdq@7753
   227
    SDL_evdevlist_item *item;
gabomdq@7753
   228
    SDL_Scancode scan_code;
gabomdq@7753
   229
    int mouse_button;
gabomdq@7753
   230
    SDL_Mouse *mouse;
slouken@10433
   231
    float norm_x, norm_y;
gabomdq@7778
   232
slouken@9159
   233
    if (!_this) {
slouken@9159
   234
        return;
slouken@9159
   235
    }
slouken@9159
   236
gabomdq@7753
   237
#if SDL_USE_LIBUDEV
gabomdq@7753
   238
    SDL_UDEV_Poll();
gabomdq@7753
   239
#endif
gabomdq@7778
   240
slouken@7788
   241
    mouse = SDL_GetMouse();
slouken@7788
   242
gabomdq@7753
   243
    for (item = _this->first; item != NULL; item = item->next) {
gabomdq@7753
   244
        while ((len = read(item->fd, events, (sizeof events))) > 0) {
gabomdq@7753
   245
            len /= sizeof(events[0]);
gabomdq@7753
   246
            for (i = 0; i < len; ++i) {
slouken@10433
   247
                /* special handling for touchscreen, that should eventually be
slouken@10433
   248
                   used for all devices */
slouken@10445
   249
                if (item->out_of_sync && item->is_touchscreen &&
slouken@10445
   250
                    events[i].type == EV_SYN && events[i].code != SYN_REPORT) {
slouken@10433
   251
                    break;
slouken@10433
   252
                }
slouken@10797
   253
slouken@7788
   254
                switch (events[i].type) {
slouken@7788
   255
                case EV_KEY:
slouken@7788
   256
                    if (events[i].code >= BTN_MOUSE && events[i].code < BTN_MOUSE + SDL_arraysize(EVDEV_MouseButtons)) {
slouken@7788
   257
                        mouse_button = events[i].code - BTN_MOUSE;
slouken@7788
   258
                        if (events[i].value == 0) {
slouken@7788
   259
                            SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, EVDEV_MouseButtons[mouse_button]);
slouken@7788
   260
                        } else if (events[i].value == 1) {
slouken@7788
   261
                            SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_PRESSED, EVDEV_MouseButtons[mouse_button]);
slouken@7788
   262
                        }
slouken@7788
   263
                        break;
slouken@7788
   264
                    }
slouken@7788
   265
slouken@7788
   266
                    /* Probably keyboard */
slouken@7788
   267
                    scan_code = SDL_EVDEV_translate_keycode(events[i].code);
slouken@7788
   268
                    if (scan_code != SDL_SCANCODE_UNKNOWN) {
slouken@7788
   269
                        if (events[i].value == 0) {
slouken@7788
   270
                            SDL_SendKeyboardKey(SDL_RELEASED, scan_code);
slouken@10445
   271
                        } else if (events[i].value == 1 || events[i].value == 2 /* key repeated */) {
slouken@7788
   272
                            SDL_SendKeyboardKey(SDL_PRESSED, scan_code);
gabomdq@7753
   273
                        }
slouken@7788
   274
                    }
slouken@10797
   275
                    SDL_EVDEV_kbd_keycode(_this->kbd, events[i].code, events[i].value);
slouken@7788
   276
                    break;
slouken@7788
   277
                case EV_ABS:
slouken@7788
   278
                    switch(events[i].code) {
slouken@10433
   279
                    case ABS_MT_SLOT:
slouken@10445
   280
                        if (!item->is_touchscreen) /* FIXME: temp hack */
slouken@10433
   281
                            break;
slouken@10433
   282
                        item->touchscreen_data->current_slot = events[i].value;
slouken@10433
   283
                        break;
slouken@10433
   284
                    case ABS_MT_TRACKING_ID:
slouken@10445
   285
                        if (!item->is_touchscreen) /* FIXME: temp hack */
slouken@10433
   286
                            break;
slouken@10445
   287
                        if (events[i].value >= 0) {
slouken@10433
   288
                            item->touchscreen_data->slots[item->touchscreen_data->current_slot].tracking_id = events[i].value;
slouken@10433
   289
                            item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_DOWN;
slouken@10433
   290
                        } else {
slouken@10433
   291
                            item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_UP;
slouken@10433
   292
                        }
slouken@10433
   293
                        break;
slouken@10433
   294
                    case ABS_MT_POSITION_X:
slouken@10445
   295
                        if (!item->is_touchscreen) /* FIXME: temp hack */
slouken@10433
   296
                            break;
slouken@10433
   297
                        item->touchscreen_data->slots[item->touchscreen_data->current_slot].x = events[i].value;
slouken@10445
   298
                        if (item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta == EVDEV_TOUCH_SLOTDELTA_NONE) {
slouken@10433
   299
                            item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_MOVE;
slouken@10433
   300
                        }
slouken@10433
   301
                        break;
slouken@10433
   302
                    case ABS_MT_POSITION_Y:
slouken@10445
   303
                        if (!item->is_touchscreen) /* FIXME: temp hack */
slouken@10433
   304
                            break;
slouken@10433
   305
                        item->touchscreen_data->slots[item->touchscreen_data->current_slot].y = events[i].value;
slouken@10445
   306
                        if (item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta == EVDEV_TOUCH_SLOTDELTA_NONE) {
slouken@10433
   307
                            item->touchscreen_data->slots[item->touchscreen_data->current_slot].delta = EVDEV_TOUCH_SLOTDELTA_MOVE;
slouken@10433
   308
                        }
slouken@10433
   309
                        break;
slouken@7788
   310
                    case ABS_X:
slouken@10445
   311
                        if (item->is_touchscreen) /* FIXME: temp hack */
slouken@10433
   312
                            break;
slouken@7788
   313
                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_FALSE, events[i].value, mouse->y);
slouken@7788
   314
                        break;
slouken@7788
   315
                    case ABS_Y:
slouken@10445
   316
                        if (item->is_touchscreen) /* FIXME: temp hack */
slouken@10433
   317
                            break;
slouken@7788
   318
                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_FALSE, mouse->x, events[i].value);
slouken@7788
   319
                        break;
gabomdq@7753
   320
                    default:
gabomdq@7753
   321
                        break;
slouken@7788
   322
                    }
slouken@7788
   323
                    break;
slouken@7788
   324
                case EV_REL:
slouken@7788
   325
                    switch(events[i].code) {
slouken@7788
   326
                    case REL_X:
slouken@7788
   327
                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_TRUE, events[i].value, 0);
slouken@7788
   328
                        break;
slouken@7788
   329
                    case REL_Y:
slouken@7788
   330
                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_TRUE, 0, events[i].value);
slouken@7788
   331
                        break;
slouken@7788
   332
                    case REL_WHEEL:
urkle@9257
   333
                        SDL_SendMouseWheel(mouse->focus, mouse->mouseID, 0, events[i].value, SDL_MOUSEWHEEL_NORMAL);
slouken@7788
   334
                        break;
slouken@7788
   335
                    case REL_HWHEEL:
urkle@9257
   336
                        SDL_SendMouseWheel(mouse->focus, mouse->mouseID, events[i].value, 0, SDL_MOUSEWHEEL_NORMAL);
slouken@7788
   337
                        break;
slouken@7788
   338
                    default:
slouken@7788
   339
                        break;
slouken@7788
   340
                    }
slouken@7788
   341
                    break;
slouken@7788
   342
                case EV_SYN:
slouken@7788
   343
                    switch (events[i].code) {
slouken@10433
   344
                    case SYN_REPORT:
slouken@10445
   345
                        if (!item->is_touchscreen) /* FIXME: temp hack */
slouken@10433
   346
                            break;
slouken@10797
   347
slouken@10445
   348
                        for(j = 0; j < item->touchscreen_data->max_slots; j++) {
slouken@10433
   349
                            norm_x = (float)(item->touchscreen_data->slots[j].x - item->touchscreen_data->min_x) /
slouken@10433
   350
                                (float)item->touchscreen_data->range_x;
slouken@10433
   351
                            norm_y = (float)(item->touchscreen_data->slots[j].y - item->touchscreen_data->min_y) /
slouken@10433
   352
                                (float)item->touchscreen_data->range_y;
slouken@10797
   353
slouken@10445
   354
                            switch(item->touchscreen_data->slots[j].delta) {
slouken@10433
   355
                            case EVDEV_TOUCH_SLOTDELTA_DOWN:
slouken@10445
   356
                                SDL_SendTouch(item->fd, item->touchscreen_data->slots[j].tracking_id, SDL_TRUE, norm_x, norm_y, 1.0f);
slouken@10433
   357
                                item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
slouken@10433
   358
                                break;
slouken@10433
   359
                            case EVDEV_TOUCH_SLOTDELTA_UP:
slouken@10445
   360
                                SDL_SendTouch(item->fd, item->touchscreen_data->slots[j].tracking_id, SDL_FALSE, norm_x, norm_y, 1.0f);
slouken@10433
   361
                                item->touchscreen_data->slots[j].tracking_id = -1;
slouken@10433
   362
                                item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
slouken@10433
   363
                                break;
slouken@10433
   364
                            case EVDEV_TOUCH_SLOTDELTA_MOVE:
slouken@10445
   365
                                SDL_SendTouchMotion(item->fd, item->touchscreen_data->slots[j].tracking_id, norm_x, norm_y, 1.0f);
slouken@10433
   366
                                item->touchscreen_data->slots[j].delta = EVDEV_TOUCH_SLOTDELTA_NONE;
slouken@10433
   367
                                break;
slouken@10433
   368
                            default:
slouken@10433
   369
                                break;
slouken@10433
   370
                            }
slouken@10433
   371
                        }
slouken@10797
   372
slouken@10445
   373
                        if (item->out_of_sync)
slouken@10433
   374
                            item->out_of_sync = 0;
slouken@10433
   375
                        break;
slouken@7788
   376
                    case SYN_DROPPED:
slouken@10445
   377
                        if (item->is_touchscreen)
slouken@10433
   378
                            item->out_of_sync = 1;
slouken@7788
   379
                        SDL_EVDEV_sync_device(item);
slouken@7788
   380
                        break;
slouken@7788
   381
                    default:
slouken@7788
   382
                        break;
slouken@7788
   383
                    }
slouken@7788
   384
                    break;
gabomdq@7753
   385
                }
gabomdq@7753
   386
            }
gabomdq@7753
   387
        }    
gabomdq@7753
   388
    }
gabomdq@7753
   389
}
gabomdq@7753
   390
gabomdq@7753
   391
static SDL_Scancode
slouken@10445
   392
SDL_EVDEV_translate_keycode(int keycode)
gabomdq@7753
   393
{
gabomdq@7753
   394
    SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
gabomdq@7753
   395
slouken@10445
   396
    if (keycode < SDL_arraysize(linux_scancode_table))
slouken@10433
   397
        scancode = linux_scancode_table[keycode];
slouken@10433
   398
slouken@10445
   399
    if (scancode == SDL_SCANCODE_UNKNOWN) {
slouken@10445
   400
        SDL_Log("The key you just pressed is not recognized by SDL. To help "
philipp@11035
   401
            "get this fixed, please report this to the SDL forums/mailing list "
philipp@11035
   402
            "<https://discourse.libsdl.org/> EVDEV KeyCode %d", keycode);
gabomdq@7753
   403
    }
slouken@10433
   404
slouken@10433
   405
    return scancode;
slouken@10433
   406
}
slouken@10433
   407
slouken@10445
   408
#ifdef SDL_USE_LIBUDEV
slouken@10433
   409
static int
slouken@10445
   410
SDL_EVDEV_init_touchscreen(SDL_evdevlist_item* item)
slouken@10445
   411
{
slouken@10433
   412
    int ret, i;
slouken@10433
   413
    char name[64];
slouken@10433
   414
    struct input_absinfo abs_info;
slouken@10797
   415
slouken@10445
   416
    if (!item->is_touchscreen)
slouken@10433
   417
        return 0;
slouken@10797
   418
slouken@10445
   419
    item->touchscreen_data = SDL_calloc(1, sizeof(*item->touchscreen_data));
slouken@10445
   420
    if (item->touchscreen_data == NULL)
slouken@10433
   421
        return SDL_OutOfMemory();
slouken@10797
   422
slouken@10445
   423
    ret = ioctl(item->fd, EVIOCGNAME(sizeof(name)), name);
slouken@10445
   424
    if (ret < 0) {
slouken@10445
   425
        SDL_free(item->touchscreen_data);
slouken@10445
   426
        return SDL_SetError("Failed to get evdev touchscreen name");
gabomdq@7753
   427
    }
slouken@10797
   428
slouken@10445
   429
    item->touchscreen_data->name = SDL_strdup(name);
slouken@10445
   430
    if (item->touchscreen_data->name == NULL) {
slouken@10445
   431
        SDL_free(item->touchscreen_data);
slouken@10433
   432
        return SDL_OutOfMemory();
slouken@10433
   433
    }
slouken@10797
   434
slouken@10445
   435
    ret = ioctl(item->fd, EVIOCGABS(ABS_MT_POSITION_X), &abs_info);
slouken@10445
   436
    if (ret < 0) {
slouken@10445
   437
        SDL_free(item->touchscreen_data->name);
slouken@10445
   438
        SDL_free(item->touchscreen_data);
slouken@10445
   439
        return SDL_SetError("Failed to get evdev touchscreen limits");
slouken@10433
   440
    }
slouken@10433
   441
    item->touchscreen_data->min_x = abs_info.minimum;
slouken@10433
   442
    item->touchscreen_data->max_x = abs_info.maximum;
slouken@10433
   443
    item->touchscreen_data->range_x = abs_info.maximum - abs_info.minimum;
slouken@10797
   444
slouken@10445
   445
    ret = ioctl(item->fd, EVIOCGABS(ABS_MT_POSITION_Y), &abs_info);
slouken@10445
   446
    if (ret < 0) {
slouken@10445
   447
        SDL_free(item->touchscreen_data->name);
slouken@10445
   448
        SDL_free(item->touchscreen_data);
slouken@10445
   449
        return SDL_SetError("Failed to get evdev touchscreen limits");
slouken@10433
   450
    }
slouken@10433
   451
    item->touchscreen_data->min_y = abs_info.minimum;
slouken@10433
   452
    item->touchscreen_data->max_y = abs_info.maximum;
slouken@10433
   453
    item->touchscreen_data->range_y = abs_info.maximum - abs_info.minimum;
slouken@10797
   454
slouken@10445
   455
    ret = ioctl(item->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
slouken@10445
   456
    if (ret < 0) {
slouken@10445
   457
        SDL_free(item->touchscreen_data->name);
slouken@10445
   458
        SDL_free(item->touchscreen_data);
slouken@10445
   459
        return SDL_SetError("Failed to get evdev touchscreen limits");
slouken@10433
   460
    }
slouken@10433
   461
    item->touchscreen_data->max_slots = abs_info.maximum + 1;
slouken@10797
   462
slouken@10433
   463
    item->touchscreen_data->slots = SDL_calloc(
slouken@10433
   464
        item->touchscreen_data->max_slots,
slouken@10445
   465
        sizeof(*item->touchscreen_data->slots));
slouken@10445
   466
    if (item->touchscreen_data->slots == NULL) {
slouken@10445
   467
        SDL_free(item->touchscreen_data->name);
slouken@10445
   468
        SDL_free(item->touchscreen_data);
slouken@10433
   469
        return SDL_OutOfMemory();
slouken@10433
   470
    }
slouken@10797
   471
slouken@10445
   472
    for(i = 0; i < item->touchscreen_data->max_slots; i++) {
slouken@10433
   473
        item->touchscreen_data->slots[i].tracking_id = -1;
slouken@10433
   474
    }
slouken@10797
   475
slouken@10445
   476
    ret = SDL_AddTouch(item->fd, /* I guess our fd is unique enough */
slouken@10445
   477
        item->touchscreen_data->name);
slouken@10445
   478
    if (ret < 0) {
slouken@10445
   479
        SDL_free(item->touchscreen_data->slots);
slouken@10445
   480
        SDL_free(item->touchscreen_data->name);
slouken@10445
   481
        SDL_free(item->touchscreen_data);
slouken@10433
   482
        return ret;
slouken@10433
   483
    }
slouken@10797
   484
slouken@10433
   485
    return 0;
slouken@10433
   486
}
slouken@10445
   487
#endif /* SDL_USE_LIBUDEV */
slouken@10433
   488
slouken@10433
   489
static void
slouken@10445
   490
SDL_EVDEV_destroy_touchscreen(SDL_evdevlist_item* item) {
slouken@10445
   491
    if (!item->is_touchscreen)
slouken@10433
   492
        return;
slouken@10797
   493
slouken@10445
   494
    SDL_DelTouch(item->fd);
slouken@10445
   495
    SDL_free(item->touchscreen_data->slots);
slouken@10445
   496
    SDL_free(item->touchscreen_data->name);
slouken@10445
   497
    SDL_free(item->touchscreen_data);
gabomdq@7753
   498
}
gabomdq@7753
   499
gabomdq@7753
   500
static void
gabomdq@7753
   501
SDL_EVDEV_sync_device(SDL_evdevlist_item *item) 
gabomdq@7753
   502
{
slouken@10446
   503
#ifdef EVIOCGMTSLOTS
slouken@10433
   504
    int i, ret;
slouken@10433
   505
    struct input_absinfo abs_info;
slouken@10433
   506
    /*
slouken@10433
   507
     * struct input_mt_request_layout {
slouken@10433
   508
     *     __u32 code;
slouken@10433
   509
     *     __s32 values[num_slots];
slouken@10433
   510
     * };
slouken@10433
   511
     *
slouken@10433
   512
     * this is the structure we're trying to emulate
slouken@10433
   513
     */
slouken@10433
   514
    __u32* mt_req_code;
slouken@10433
   515
    __s32* mt_req_values;
slouken@10433
   516
    size_t mt_req_size;
slouken@10797
   517
slouken@10433
   518
    /* TODO: sync devices other than touchscreen */
slouken@10445
   519
    if (!item->is_touchscreen)
slouken@10433
   520
        return;
slouken@10797
   521
slouken@10445
   522
    mt_req_size = sizeof(*mt_req_code) +
slouken@10445
   523
        sizeof(*mt_req_values) * item->touchscreen_data->max_slots;
slouken@10797
   524
slouken@10445
   525
    mt_req_code = SDL_calloc(1, mt_req_size);
slouken@10445
   526
    if (mt_req_code == NULL) {
slouken@10433
   527
        return;
slouken@10433
   528
    }
slouken@10797
   529
slouken@10433
   530
    mt_req_values = (__s32*)mt_req_code + 1;
slouken@10797
   531
slouken@10433
   532
    *mt_req_code = ABS_MT_TRACKING_ID;
slouken@10445
   533
    ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
slouken@10445
   534
    if (ret < 0) {
slouken@10445
   535
        SDL_free(mt_req_code);
slouken@10433
   536
        return;
slouken@10433
   537
    }
slouken@10445
   538
    for(i = 0; i < item->touchscreen_data->max_slots; i++) {
slouken@10433
   539
        /*
slouken@10433
   540
         * This doesn't account for the very edge case of the user removing their
slouken@10433
   541
         * finger and replacing it on the screen during the time we're out of sync,
slouken@10433
   542
         * which'll mean that we're not going from down -> up or up -> down, we're
slouken@10433
   543
         * going from down -> down but with a different tracking id, meaning we'd
slouken@10433
   544
         * have to tell SDL of the two events, but since we wait till SYN_REPORT in
slouken@10433
   545
         * SDL_EVDEV_Poll to tell SDL, the current structure of this code doesn't
slouken@10433
   546
         * allow it. Lets just pray to God it doesn't happen.
slouken@10433
   547
         */
slouken@10445
   548
        if (item->touchscreen_data->slots[i].tracking_id < 0 &&
slouken@10445
   549
            mt_req_values[i] >= 0) {
slouken@10433
   550
            item->touchscreen_data->slots[i].tracking_id = mt_req_values[i];
slouken@10433
   551
            item->touchscreen_data->slots[i].delta = EVDEV_TOUCH_SLOTDELTA_DOWN;
slouken@10445
   552
        } else if (item->touchscreen_data->slots[i].tracking_id >= 0 &&
slouken@10445
   553
            mt_req_values[i] < 0) {
slouken@10433
   554
            item->touchscreen_data->slots[i].tracking_id = -1;
slouken@10433
   555
            item->touchscreen_data->slots[i].delta = EVDEV_TOUCH_SLOTDELTA_UP;
slouken@10433
   556
        }
slouken@10433
   557
    }
slouken@10797
   558
slouken@10433
   559
    *mt_req_code = ABS_MT_POSITION_X;
slouken@10445
   560
    ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
slouken@10445
   561
    if (ret < 0) {
slouken@10445
   562
        SDL_free(mt_req_code);
slouken@10433
   563
        return;
slouken@10433
   564
    }
slouken@10445
   565
    for(i = 0; i < item->touchscreen_data->max_slots; i++) {
slouken@10445
   566
        if (item->touchscreen_data->slots[i].tracking_id >= 0 &&
slouken@10445
   567
            item->touchscreen_data->slots[i].x != mt_req_values[i]) {
slouken@10433
   568
            item->touchscreen_data->slots[i].x = mt_req_values[i];
slouken@10445
   569
            if (item->touchscreen_data->slots[i].delta ==
slouken@10445
   570
                EVDEV_TOUCH_SLOTDELTA_NONE) {
slouken@10433
   571
                item->touchscreen_data->slots[i].delta =
slouken@10433
   572
                    EVDEV_TOUCH_SLOTDELTA_MOVE;
slouken@10433
   573
            }
slouken@10433
   574
        }
slouken@10433
   575
    }
slouken@10797
   576
slouken@10433
   577
    *mt_req_code = ABS_MT_POSITION_Y;
slouken@10445
   578
    ret = ioctl(item->fd, EVIOCGMTSLOTS(mt_req_size), mt_req_code);
slouken@10445
   579
    if (ret < 0) {
slouken@10445
   580
        SDL_free(mt_req_code);
slouken@10433
   581
        return;
slouken@10433
   582
    }
slouken@10445
   583
    for(i = 0; i < item->touchscreen_data->max_slots; i++) {
slouken@10445
   584
        if (item->touchscreen_data->slots[i].tracking_id >= 0 &&
slouken@10445
   585
            item->touchscreen_data->slots[i].y != mt_req_values[i]) {
slouken@10433
   586
            item->touchscreen_data->slots[i].y = mt_req_values[i];
slouken@10445
   587
            if (item->touchscreen_data->slots[i].delta ==
slouken@10445
   588
                EVDEV_TOUCH_SLOTDELTA_NONE) {
slouken@10433
   589
                item->touchscreen_data->slots[i].delta =
slouken@10433
   590
                    EVDEV_TOUCH_SLOTDELTA_MOVE;
slouken@10433
   591
            }
slouken@10433
   592
        }
slouken@10433
   593
    }
slouken@10797
   594
slouken@10445
   595
    ret = ioctl(item->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
slouken@10445
   596
    if (ret < 0) {
slouken@10445
   597
        SDL_free(mt_req_code);
slouken@10433
   598
        return;
slouken@10433
   599
    }
slouken@10433
   600
    item->touchscreen_data->current_slot = abs_info.value;
slouken@10797
   601
slouken@10445
   602
    SDL_free(mt_req_code);
slouken@10446
   603
slouken@10446
   604
#endif /* EVIOCGMTSLOTS */
gabomdq@7753
   605
}
gabomdq@7753
   606
gabomdq@7755
   607
#if SDL_USE_LIBUDEV
gabomdq@7753
   608
static int
slouken@10433
   609
SDL_EVDEV_device_added(const char *dev_path, int udev_class)
gabomdq@7753
   610
{
slouken@10433
   611
    int ret;
gabomdq@7753
   612
    SDL_evdevlist_item *item;
gabomdq@7753
   613
gabomdq@7753
   614
    /* Check to make sure it's not already in list. */
gabomdq@7753
   615
    for (item = _this->first; item != NULL; item = item->next) {
slouken@10433
   616
        if (SDL_strcmp(dev_path, item->path) == 0) {
gabomdq@7753
   617
            return -1;  /* already have this one */
gabomdq@7753
   618
        }
gabomdq@7753
   619
    }
slouken@10797
   620
gabomdq@7753
   621
    item = (SDL_evdevlist_item *) SDL_calloc(1, sizeof (SDL_evdevlist_item));
gabomdq@7753
   622
    if (item == NULL) {
gabomdq@7753
   623
        return SDL_OutOfMemory();
gabomdq@7753
   624
    }
gabomdq@7753
   625
slouken@10445
   626
    item->fd = open(dev_path, O_RDONLY | O_NONBLOCK);
gabomdq@7753
   627
    if (item->fd < 0) {
gabomdq@7753
   628
        SDL_free(item);
slouken@10433
   629
        return SDL_SetError("Unable to open %s", dev_path);
gabomdq@7753
   630
    }
slouken@10797
   631
slouken@10433
   632
    item->path = SDL_strdup(dev_path);
gabomdq@7753
   633
    if (item->path == NULL) {
gabomdq@7753
   634
        close(item->fd);
gabomdq@7753
   635
        SDL_free(item);
gabomdq@7753
   636
        return SDL_OutOfMemory();
gabomdq@7753
   637
    }
slouken@10797
   638
slouken@10445
   639
    if (udev_class & SDL_UDEV_DEVICE_TOUCHSCREEN) {
slouken@10433
   640
        item->is_touchscreen = 1;
slouken@10797
   641
slouken@10445
   642
        if ((ret = SDL_EVDEV_init_touchscreen(item)) < 0) {
slouken@10433
   643
            close(item->fd);
slouken@10433
   644
            SDL_free(item);
slouken@10433
   645
            return ret;
slouken@10433
   646
        }
slouken@10433
   647
    }
slouken@10797
   648
gabomdq@7753
   649
    if (_this->last == NULL) {
gabomdq@7753
   650
        _this->first = _this->last = item;
gabomdq@7753
   651
    } else {
gabomdq@7753
   652
        _this->last->next = item;
gabomdq@7753
   653
        _this->last = item;
gabomdq@7753
   654
    }
slouken@10797
   655
gabomdq@7753
   656
    SDL_EVDEV_sync_device(item);
slouken@10797
   657
slouken@10433
   658
    return _this->num_devices++;
gabomdq@7753
   659
}
gabomdq@7767
   660
#endif /* SDL_USE_LIBUDEV */
gabomdq@7753
   661
gabomdq@7753
   662
static int
slouken@10433
   663
SDL_EVDEV_device_removed(const char *dev_path)
gabomdq@7753
   664
{
gabomdq@7753
   665
    SDL_evdevlist_item *item;
gabomdq@7753
   666
    SDL_evdevlist_item *prev = NULL;
gabomdq@7753
   667
gabomdq@7753
   668
    for (item = _this->first; item != NULL; item = item->next) {
gabomdq@7753
   669
        /* found it, remove it. */
slouken@10433
   670
        if (SDL_strcmp(dev_path, item->path) == 0) {
gabomdq@7753
   671
            if (prev != NULL) {
gabomdq@7753
   672
                prev->next = item->next;
gabomdq@7753
   673
            } else {
gabomdq@7753
   674
                SDL_assert(_this->first == item);
gabomdq@7753
   675
                _this->first = item->next;
gabomdq@7753
   676
            }
gabomdq@7753
   677
            if (item == _this->last) {
gabomdq@7753
   678
                _this->last = prev;
gabomdq@7753
   679
            }
slouken@10445
   680
            if (item->is_touchscreen) {
slouken@10445
   681
                SDL_EVDEV_destroy_touchscreen(item);
slouken@10433
   682
            }
gabomdq@7753
   683
            close(item->fd);
gabomdq@7753
   684
            SDL_free(item->path);
gabomdq@7753
   685
            SDL_free(item);
slouken@10433
   686
            _this->num_devices--;
gabomdq@7753
   687
            return 0;
gabomdq@7753
   688
        }
gabomdq@7753
   689
        prev = item;
gabomdq@7753
   690
    }
gabomdq@7753
   691
gabomdq@7753
   692
    return -1;
gabomdq@7753
   693
}
gabomdq@7767
   694
gabomdq@7753
   695
gabomdq@7753
   696
#endif /* SDL_INPUT_LINUXEV */
gabomdq@7753
   697
gabomdq@7753
   698
/* vi: set ts=4 sw=4 expandtab: */