src/input/evdev/SDL_evdev.c
author Gabriel Jacobo <gabomdq@gmail.com>
Sat, 28 Sep 2013 15:48:32 -0300
changeset 7755 f06772125578
parent 7753 e4c38f17bfad
child 7767 0d425d6327d8
permissions -rw-r--r--
Do not use UDEV references in EVDEV if UDEV has not been detected
gabomdq@7753
     1
/*
gabomdq@7753
     2
  Simple DirectMedia Layer
gabomdq@7753
     3
  Copyright (C) 1997-2013 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
*/
gabomdq@7753
    21
#include "SDL_config.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
gabomdq@7753
    33
#include "SDL_evdev.h"
gabomdq@7753
    34
#define _THIS SDL_EVDEV_PrivateData *_this
gabomdq@7753
    35
static _THIS = NULL;
gabomdq@7753
    36
gabomdq@7753
    37
#include <sys/stat.h>
gabomdq@7753
    38
#include <unistd.h>
gabomdq@7753
    39
#include <fcntl.h>
gabomdq@7753
    40
#include <sys/ioctl.h>
gabomdq@7753
    41
#include <limits.h>             /* For the definition of PATH_MAX */
gabomdq@7753
    42
gabomdq@7753
    43
gabomdq@7753
    44
#include "SDL.h"
gabomdq@7753
    45
#include "SDL_assert.h"
gabomdq@7753
    46
#include "SDL_endian.h"
gabomdq@7753
    47
#include "../../core/linux/SDL_udev.h"
gabomdq@7753
    48
#include "SDL_scancode.h"
gabomdq@7753
    49
#include "../../events/SDL_events_c.h"
gabomdq@7753
    50
gabomdq@7753
    51
/* This isn't defined in older Linux kernel headers */
gabomdq@7753
    52
#ifndef SYN_DROPPED
gabomdq@7753
    53
#define SYN_DROPPED 3
gabomdq@7753
    54
#endif
gabomdq@7753
    55
gabomdq@7755
    56
static SDL_Scancode SDL_EVDEV_translate_keycode(int keycode);
gabomdq@7755
    57
static void SDL_EVDEV_sync_device(SDL_evdevlist_item *item);
gabomdq@7753
    58
gabomdq@7755
    59
#if SDL_USE_LIBUDEV
gabomdq@7753
    60
static int SDL_EVDEV_device_removed(const char *devpath);
gabomdq@7753
    61
static int SDL_EVDEV_device_added(const SDL_UDEV_deviceclass devclass, const char *devpath);
gabomdq@7753
    62
void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath);
gabomdq@7755
    63
#endif /* SDL_USE_LIBUDEV */
gabomdq@7753
    64
gabomdq@7753
    65
static SDL_Scancode EVDEV_Keycodes[] = {
gabomdq@7753
    66
    SDL_SCANCODE_UNKNOWN,       /*  KEY_RESERVED        0 */
gabomdq@7753
    67
    SDL_SCANCODE_ESCAPE,        /*  KEY_ESC         1 */
gabomdq@7753
    68
    SDL_SCANCODE_1,             /*  KEY_1           2 */
gabomdq@7753
    69
    SDL_SCANCODE_2,             /*  KEY_2           3 */
gabomdq@7753
    70
    SDL_SCANCODE_3,             /*  KEY_3           4 */
gabomdq@7753
    71
    SDL_SCANCODE_4,             /*  KEY_4           5 */
gabomdq@7753
    72
    SDL_SCANCODE_5,             /*  KEY_5           6 */
gabomdq@7753
    73
    SDL_SCANCODE_6,             /*  KEY_6           7 */
gabomdq@7753
    74
    SDL_SCANCODE_7,             /*  KEY_7           8 */
gabomdq@7753
    75
    SDL_SCANCODE_8,             /*  KEY_8           9 */
gabomdq@7753
    76
    SDL_SCANCODE_9,             /*  KEY_9           10 */
gabomdq@7753
    77
    SDL_SCANCODE_0,             /*  KEY_0           11 */
gabomdq@7753
    78
    SDL_SCANCODE_MINUS,         /*  KEY_MINUS       12 */
gabomdq@7753
    79
    SDL_SCANCODE_EQUALS,        /*  KEY_EQUAL       13 */
gabomdq@7753
    80
    SDL_SCANCODE_BACKSPACE,     /*  KEY_BACKSPACE       14 */
gabomdq@7753
    81
    SDL_SCANCODE_TAB,           /*  KEY_TAB         15 */
gabomdq@7753
    82
    SDL_SCANCODE_Q,             /*  KEY_Q           16 */
gabomdq@7753
    83
    SDL_SCANCODE_W,             /*  KEY_W           17 */
gabomdq@7753
    84
    SDL_SCANCODE_E,             /*  KEY_E           18 */
gabomdq@7753
    85
    SDL_SCANCODE_R,             /*  KEY_R           19 */
gabomdq@7753
    86
    SDL_SCANCODE_T,             /*  KEY_T           20 */
gabomdq@7753
    87
    SDL_SCANCODE_Y,             /*  KEY_Y           21 */
gabomdq@7753
    88
    SDL_SCANCODE_U,             /*  KEY_U           22 */
gabomdq@7753
    89
    SDL_SCANCODE_I,             /*  KEY_I           23 */
gabomdq@7753
    90
    SDL_SCANCODE_O,             /*  KEY_O           24 */
gabomdq@7753
    91
    SDL_SCANCODE_P,             /*  KEY_P           25 */
gabomdq@7753
    92
    SDL_SCANCODE_LEFTBRACKET,   /*  KEY_LEFTBRACE       26 */
gabomdq@7753
    93
    SDL_SCANCODE_RIGHTBRACKET,  /*  KEY_RIGHTBRACE      27 */
gabomdq@7753
    94
    SDL_SCANCODE_RETURN,        /*  KEY_ENTER       28 */
gabomdq@7753
    95
    SDL_SCANCODE_LCTRL,         /*  KEY_LEFTCTRL        29 */
gabomdq@7753
    96
    SDL_SCANCODE_A,             /*  KEY_A           30 */
gabomdq@7753
    97
    SDL_SCANCODE_S,             /*  KEY_S           31 */
gabomdq@7753
    98
    SDL_SCANCODE_D,             /*  KEY_D           32 */
gabomdq@7753
    99
    SDL_SCANCODE_F,             /*  KEY_F           33 */
gabomdq@7753
   100
    SDL_SCANCODE_G,             /*  KEY_G           34 */
gabomdq@7753
   101
    SDL_SCANCODE_H,             /*  KEY_H           35 */
gabomdq@7753
   102
    SDL_SCANCODE_J,             /*  KEY_J           36 */
gabomdq@7753
   103
    SDL_SCANCODE_K,             /*  KEY_K           37 */
gabomdq@7753
   104
    SDL_SCANCODE_L,             /*  KEY_L           38 */
gabomdq@7753
   105
    SDL_SCANCODE_SEMICOLON,     /*  KEY_SEMICOLON       39 */
gabomdq@7753
   106
    SDL_SCANCODE_APOSTROPHE,    /*  KEY_APOSTROPHE      40 */
gabomdq@7753
   107
    SDL_SCANCODE_GRAVE,         /*  KEY_GRAVE       41 */
gabomdq@7753
   108
    SDL_SCANCODE_LSHIFT,        /*  KEY_LEFTSHIFT       42 */
gabomdq@7753
   109
    SDL_SCANCODE_BACKSLASH,     /*  KEY_BACKSLASH       43 */
gabomdq@7753
   110
    SDL_SCANCODE_Z,             /*  KEY_Z           44 */
gabomdq@7753
   111
    SDL_SCANCODE_X,             /*  KEY_X           45 */
gabomdq@7753
   112
    SDL_SCANCODE_C,             /*  KEY_C           46 */
gabomdq@7753
   113
    SDL_SCANCODE_V,             /*  KEY_V           47 */
gabomdq@7753
   114
    SDL_SCANCODE_B,             /*  KEY_B           48 */
gabomdq@7753
   115
    SDL_SCANCODE_N,             /*  KEY_N           49 */
gabomdq@7753
   116
    SDL_SCANCODE_M,             /*  KEY_M           50 */
gabomdq@7753
   117
    SDL_SCANCODE_COMMA,         /*  KEY_COMMA       51 */
gabomdq@7753
   118
    SDL_SCANCODE_PERIOD,        /*  KEY_DOT         52 */
gabomdq@7753
   119
    SDL_SCANCODE_SLASH,         /*  KEY_SLASH       53 */
gabomdq@7753
   120
    SDL_SCANCODE_RSHIFT,        /*  KEY_RIGHTSHIFT      54 */
gabomdq@7753
   121
    SDL_SCANCODE_KP_MULTIPLY,   /*  KEY_KPASTERISK      55 */
gabomdq@7753
   122
    SDL_SCANCODE_LALT,          /*  KEY_LEFTALT     56 */
gabomdq@7753
   123
    SDL_SCANCODE_SPACE,         /*  KEY_SPACE       57 */
gabomdq@7753
   124
    SDL_SCANCODE_CAPSLOCK,      /*  KEY_CAPSLOCK        58 */
gabomdq@7753
   125
    SDL_SCANCODE_F1,            /*  KEY_F1          59 */
gabomdq@7753
   126
    SDL_SCANCODE_F2,            /*  KEY_F2          60 */
gabomdq@7753
   127
    SDL_SCANCODE_F3,            /*  KEY_F3          61 */
gabomdq@7753
   128
    SDL_SCANCODE_F4,            /*  KEY_F4          62 */
gabomdq@7753
   129
    SDL_SCANCODE_F5,            /*  KEY_F5          63 */
gabomdq@7753
   130
    SDL_SCANCODE_F6,            /*  KEY_F6          64 */
gabomdq@7753
   131
    SDL_SCANCODE_F7,            /*  KEY_F7          65 */
gabomdq@7753
   132
    SDL_SCANCODE_F8,            /*  KEY_F8          66 */
gabomdq@7753
   133
    SDL_SCANCODE_F9,            /*  KEY_F9          67 */
gabomdq@7753
   134
    SDL_SCANCODE_F10,           /*  KEY_F10         68 */
gabomdq@7753
   135
    SDL_SCANCODE_NUMLOCKCLEAR,  /*  KEY_NUMLOCK     69 */
gabomdq@7753
   136
    SDL_SCANCODE_SCROLLLOCK,    /*  KEY_SCROLLLOCK      70 */
gabomdq@7753
   137
    SDL_SCANCODE_KP_7,          /*  KEY_KP7         71 */
gabomdq@7753
   138
    SDL_SCANCODE_KP_8,          /*  KEY_KP8         72 */
gabomdq@7753
   139
    SDL_SCANCODE_KP_9,          /*  KEY_KP9         73 */
gabomdq@7753
   140
    SDL_SCANCODE_KP_MINUS,      /*  KEY_KPMINUS     74 */
gabomdq@7753
   141
    SDL_SCANCODE_KP_4,          /*  KEY_KP4         75 */
gabomdq@7753
   142
    SDL_SCANCODE_KP_5,          /*  KEY_KP5         76 */
gabomdq@7753
   143
    SDL_SCANCODE_KP_6,          /*  KEY_KP6         77 */
gabomdq@7753
   144
    SDL_SCANCODE_KP_PLUS,       /*  KEY_KPPLUS      78 */
gabomdq@7753
   145
    SDL_SCANCODE_KP_1,          /*  KEY_KP1         79 */
gabomdq@7753
   146
    SDL_SCANCODE_KP_2,          /*  KEY_KP2         80 */
gabomdq@7753
   147
    SDL_SCANCODE_KP_3,          /*  KEY_KP3         81 */
gabomdq@7753
   148
    SDL_SCANCODE_KP_0,          /*  KEY_KP0         82 */
gabomdq@7753
   149
    SDL_SCANCODE_KP_PERIOD,     /*  KEY_KPDOT       83 */
gabomdq@7753
   150
    SDL_SCANCODE_UNKNOWN,       /*  84 */
gabomdq@7753
   151
    SDL_SCANCODE_LANG5,         /*  KEY_ZENKAKUHANKAKU  85 */
gabomdq@7753
   152
    SDL_SCANCODE_UNKNOWN,       /*  KEY_102ND       86 */
gabomdq@7753
   153
    SDL_SCANCODE_F11,           /*  KEY_F11         87 */
gabomdq@7753
   154
    SDL_SCANCODE_F12,           /*  KEY_F12         88 */
gabomdq@7753
   155
    SDL_SCANCODE_UNKNOWN,       /*  KEY_RO          89 */
gabomdq@7753
   156
    SDL_SCANCODE_LANG3,         /*  KEY_KATAKANA        90 */
gabomdq@7753
   157
    SDL_SCANCODE_LANG4,         /*  KEY_HIRAGANA        91 */
gabomdq@7753
   158
    SDL_SCANCODE_UNKNOWN,       /*  KEY_HENKAN      92 */
gabomdq@7753
   159
    SDL_SCANCODE_LANG3,         /*  KEY_KATAKANAHIRAGANA    93 */
gabomdq@7753
   160
    SDL_SCANCODE_UNKNOWN,       /*  KEY_MUHENKAN        94 */
gabomdq@7753
   161
    SDL_SCANCODE_KP_COMMA,      /*  KEY_KPJPCOMMA       95 */
gabomdq@7753
   162
    SDL_SCANCODE_KP_ENTER,      /*  KEY_KPENTER     96 */
gabomdq@7753
   163
    SDL_SCANCODE_RCTRL,         /*  KEY_RIGHTCTRL       97 */
gabomdq@7753
   164
    SDL_SCANCODE_KP_DIVIDE,     /*  KEY_KPSLASH     98 */
gabomdq@7753
   165
    SDL_SCANCODE_SYSREQ,        /*  KEY_SYSRQ       99 */
gabomdq@7753
   166
    SDL_SCANCODE_RALT,          /*  KEY_RIGHTALT        100 */
gabomdq@7753
   167
    SDL_SCANCODE_UNKNOWN,       /*  KEY_LINEFEED        101 */
gabomdq@7753
   168
    SDL_SCANCODE_HOME,          /*  KEY_HOME        102 */
gabomdq@7753
   169
    SDL_SCANCODE_UP,            /*  KEY_UP          103 */
gabomdq@7753
   170
    SDL_SCANCODE_PAGEUP,        /*  KEY_PAGEUP      104 */
gabomdq@7753
   171
    SDL_SCANCODE_LEFT,          /*  KEY_LEFT        105 */
gabomdq@7753
   172
    SDL_SCANCODE_RIGHT,         /*  KEY_RIGHT       106 */
gabomdq@7753
   173
    SDL_SCANCODE_END,           /*  KEY_END         107 */
gabomdq@7753
   174
    SDL_SCANCODE_DOWN,          /*  KEY_DOWN        108 */
gabomdq@7753
   175
    SDL_SCANCODE_PAGEDOWN,      /*  KEY_PAGEDOWN        109 */
gabomdq@7753
   176
    SDL_SCANCODE_INSERT,        /*  KEY_INSERT      110 */
gabomdq@7753
   177
    SDL_SCANCODE_DELETE,        /*  KEY_DELETE      111 */
gabomdq@7753
   178
    SDL_SCANCODE_UNKNOWN,       /*  KEY_MACRO       112 */
gabomdq@7753
   179
    SDL_SCANCODE_MUTE,          /*  KEY_MUTE        113 */
gabomdq@7753
   180
    SDL_SCANCODE_VOLUMEDOWN,    /*  KEY_VOLUMEDOWN      114 */
gabomdq@7753
   181
    SDL_SCANCODE_VOLUMEUP,      /*  KEY_VOLUMEUP        115 */
gabomdq@7753
   182
    SDL_SCANCODE_POWER,         /*  KEY_POWER       116 SC System Power Down */
gabomdq@7753
   183
    SDL_SCANCODE_KP_EQUALS,     /*  KEY_KPEQUAL     117 */
gabomdq@7753
   184
    SDL_SCANCODE_KP_MINUS,      /*  KEY_KPPLUSMINUS     118 */
gabomdq@7753
   185
    SDL_SCANCODE_PAUSE,         /*  KEY_PAUSE       119 */
gabomdq@7753
   186
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SCALE       120 AL Compiz Scale (Expose) */
gabomdq@7753
   187
    SDL_SCANCODE_KP_COMMA,      /*  KEY_KPCOMMA     121 */
gabomdq@7753
   188
    SDL_SCANCODE_LANG1,         /*  KEY_HANGEUL,KEY_HANGUEL 122 */
gabomdq@7753
   189
    SDL_SCANCODE_LANG2,         /*  KEY_HANJA       123 */
gabomdq@7753
   190
    SDL_SCANCODE_INTERNATIONAL3,/*  KEY_YEN         124 */
gabomdq@7753
   191
    SDL_SCANCODE_LGUI,          /*  KEY_LEFTMETA        125 */
gabomdq@7753
   192
    SDL_SCANCODE_RGUI,          /*  KEY_RIGHTMETA       126 */
gabomdq@7753
   193
    SDL_SCANCODE_APPLICATION,   /*  KEY_COMPOSE     127 */
gabomdq@7753
   194
    SDL_SCANCODE_STOP,          /*  KEY_STOP        128 AC Stop */
gabomdq@7753
   195
    SDL_SCANCODE_AGAIN,         /*  KEY_AGAIN       129 */
gabomdq@7753
   196
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PROPS       130 AC Properties */
gabomdq@7753
   197
    SDL_SCANCODE_UNDO,          /*  KEY_UNDO        131 AC Undo */
gabomdq@7753
   198
    SDL_SCANCODE_UNKNOWN,       /*  KEY_FRONT       132 */
gabomdq@7753
   199
    SDL_SCANCODE_COPY,          /*  KEY_COPY        133 AC Copy */
gabomdq@7753
   200
    SDL_SCANCODE_UNKNOWN,       /*  KEY_OPEN        134 AC Open */
gabomdq@7753
   201
    SDL_SCANCODE_PASTE,         /*  KEY_PASTE       135 AC Paste */
gabomdq@7753
   202
    SDL_SCANCODE_FIND,          /*  KEY_FIND        136 AC Search */
gabomdq@7753
   203
    SDL_SCANCODE_CUT,           /*  KEY_CUT         137 AC Cut */
gabomdq@7753
   204
    SDL_SCANCODE_HELP,          /*  KEY_HELP        138 AL Integrated Help Center */
gabomdq@7753
   205
    SDL_SCANCODE_MENU,          /*  KEY_MENU        139 Menu (show menu) */
gabomdq@7753
   206
    SDL_SCANCODE_CALCULATOR,    /*  KEY_CALC        140 AL Calculator */
gabomdq@7753
   207
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SETUP       141 */
gabomdq@7753
   208
    SDL_SCANCODE_SLEEP,         /*  KEY_SLEEP       142 SC System Sleep */
gabomdq@7753
   209
    SDL_SCANCODE_UNKNOWN,       /*  KEY_WAKEUP      143 System Wake Up */
gabomdq@7753
   210
    SDL_SCANCODE_UNKNOWN,       /*  KEY_FILE        144 AL Local Machine Browser */
gabomdq@7753
   211
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SENDFILE        145 */
gabomdq@7753
   212
    SDL_SCANCODE_UNKNOWN,       /*  KEY_DELETEFILE      146 */
gabomdq@7753
   213
    SDL_SCANCODE_UNKNOWN,       /*  KEY_XFER        147 */
gabomdq@7753
   214
    SDL_SCANCODE_APP1,          /*  KEY_PROG1       148 */
gabomdq@7753
   215
    SDL_SCANCODE_APP1,          /*  KEY_PROG2       149 */
gabomdq@7753
   216
    SDL_SCANCODE_WWW,           /*  KEY_WWW         150 AL Internet Browser */
gabomdq@7753
   217
    SDL_SCANCODE_UNKNOWN,       /*  KEY_MSDOS       151 */
gabomdq@7753
   218
    SDL_SCANCODE_UNKNOWN,       /*  KEY_COFFEE,KEY_SCREENLOCK      152 AL Terminal Lock/Screensaver */
gabomdq@7753
   219
    SDL_SCANCODE_UNKNOWN,       /*  KEY_DIRECTION       153 */
gabomdq@7753
   220
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CYCLEWINDOWS    154 */
gabomdq@7753
   221
    SDL_SCANCODE_MAIL,          /*  KEY_MAIL        155 */
gabomdq@7753
   222
    SDL_SCANCODE_AC_BOOKMARKS,  /*  KEY_BOOKMARKS       156 AC Bookmarks */
gabomdq@7753
   223
    SDL_SCANCODE_COMPUTER,      /*  KEY_COMPUTER        157 */
gabomdq@7753
   224
    SDL_SCANCODE_AC_BACK,       /*  KEY_BACK        158 AC Back */
gabomdq@7753
   225
    SDL_SCANCODE_AC_FORWARD,    /*  KEY_FORWARD     159 AC Forward */
gabomdq@7753
   226
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CLOSECD     160 */
gabomdq@7753
   227
    SDL_SCANCODE_EJECT,         /*  KEY_EJECTCD     161 */
gabomdq@7753
   228
    SDL_SCANCODE_UNKNOWN,       /*  KEY_EJECTCLOSECD    162 */
gabomdq@7753
   229
    SDL_SCANCODE_AUDIONEXT,     /*  KEY_NEXTSONG        163 */
gabomdq@7753
   230
    SDL_SCANCODE_AUDIOPLAY,     /*  KEY_PLAYPAUSE       164 */
gabomdq@7753
   231
    SDL_SCANCODE_AUDIOPREV,     /*  KEY_PREVIOUSSONG    165 */
gabomdq@7753
   232
    SDL_SCANCODE_AUDIOSTOP,     /*  KEY_STOPCD      166 */
gabomdq@7753
   233
    SDL_SCANCODE_UNKNOWN,       /*  KEY_RECORD      167 */
gabomdq@7753
   234
    SDL_SCANCODE_UNKNOWN,       /*  KEY_REWIND      168 */
gabomdq@7753
   235
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PHONE       169 Media Select Telephone */
gabomdq@7753
   236
    SDL_SCANCODE_UNKNOWN,       /*  KEY_ISO         170 */
gabomdq@7753
   237
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CONFIG      171 AL Consumer Control Configuration */
gabomdq@7753
   238
    SDL_SCANCODE_AC_HOME,       /*  KEY_HOMEPAGE        172 AC Home */
gabomdq@7753
   239
    SDL_SCANCODE_AC_REFRESH,    /*  KEY_REFRESH     173 AC Refresh */
gabomdq@7753
   240
    SDL_SCANCODE_UNKNOWN,       /*  KEY_EXIT        174 AC Exit */
gabomdq@7753
   241
    SDL_SCANCODE_UNKNOWN,       /*  KEY_MOVE        175 */
gabomdq@7753
   242
    SDL_SCANCODE_UNKNOWN,       /*  KEY_EDIT        176 */
gabomdq@7753
   243
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SCROLLUP        177 */
gabomdq@7753
   244
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SCROLLDOWN      178 */
gabomdq@7753
   245
    SDL_SCANCODE_KP_LEFTPAREN,  /*  KEY_KPLEFTPAREN     179 */
gabomdq@7753
   246
    SDL_SCANCODE_KP_RIGHTPAREN, /*  KEY_KPRIGHTPAREN    180 */
gabomdq@7753
   247
    SDL_SCANCODE_UNKNOWN,       /*  KEY_NEW         181 AC New */
gabomdq@7753
   248
    SDL_SCANCODE_AGAIN,         /*  KEY_REDO        182 AC Redo/Repeat */
gabomdq@7753
   249
    SDL_SCANCODE_F13,           /*  KEY_F13         183 */
gabomdq@7753
   250
    SDL_SCANCODE_F14,           /*  KEY_F14         184 */
gabomdq@7753
   251
    SDL_SCANCODE_F15,           /*  KEY_F15         185 */
gabomdq@7753
   252
    SDL_SCANCODE_F16,           /*  KEY_F16         186 */
gabomdq@7753
   253
    SDL_SCANCODE_F17,           /*  KEY_F17         187 */
gabomdq@7753
   254
    SDL_SCANCODE_F18,           /*  KEY_F18         188 */
gabomdq@7753
   255
    SDL_SCANCODE_F19,           /*  KEY_F19         189 */
gabomdq@7753
   256
    SDL_SCANCODE_F20,           /*  KEY_F20         190 */
gabomdq@7753
   257
    SDL_SCANCODE_F21,           /*  KEY_F21         191 */
gabomdq@7753
   258
    SDL_SCANCODE_F22,           /*  KEY_F22         192 */
gabomdq@7753
   259
    SDL_SCANCODE_F23,           /*  KEY_F23         193 */
gabomdq@7753
   260
    SDL_SCANCODE_F24,           /*  KEY_F24         194 */
gabomdq@7753
   261
    SDL_SCANCODE_UNKNOWN,       /*  195 */
gabomdq@7753
   262
    SDL_SCANCODE_UNKNOWN,       /*  196 */
gabomdq@7753
   263
    SDL_SCANCODE_UNKNOWN,       /*  197 */
gabomdq@7753
   264
    SDL_SCANCODE_UNKNOWN,       /*  198 */
gabomdq@7753
   265
    SDL_SCANCODE_UNKNOWN,       /*  199 */
gabomdq@7753
   266
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PLAYCD      200 */
gabomdq@7753
   267
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PAUSECD     201 */
gabomdq@7753
   268
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PROG3       202 */
gabomdq@7753
   269
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PROG4       203 */
gabomdq@7753
   270
    SDL_SCANCODE_UNKNOWN,       /*  KEY_DASHBOARD       204 AL Dashboard */
gabomdq@7753
   271
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SUSPEND     205 */
gabomdq@7753
   272
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CLOSE       206 AC Close */
gabomdq@7753
   273
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PLAY        207 */
gabomdq@7753
   274
    SDL_SCANCODE_UNKNOWN,       /*  KEY_FASTFORWARD     208 */
gabomdq@7753
   275
    SDL_SCANCODE_UNKNOWN,       /*  KEY_BASSBOOST       209 */
gabomdq@7753
   276
    SDL_SCANCODE_UNKNOWN,       /*  KEY_PRINT       210 AC Print */
gabomdq@7753
   277
    SDL_SCANCODE_UNKNOWN,       /*  KEY_HP          211 */
gabomdq@7753
   278
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CAMERA      212 */
gabomdq@7753
   279
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SOUND       213 */
gabomdq@7753
   280
    SDL_SCANCODE_UNKNOWN,       /*  KEY_QUESTION        214 */
gabomdq@7753
   281
    SDL_SCANCODE_UNKNOWN,       /*  KEY_EMAIL       215 */
gabomdq@7753
   282
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CHAT        216 */
gabomdq@7753
   283
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SEARCH      217 */
gabomdq@7753
   284
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CONNECT     218 */
gabomdq@7753
   285
    SDL_SCANCODE_UNKNOWN,       /*  KEY_FINANCE     219 AL Checkbook/Finance */
gabomdq@7753
   286
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SPORT       220 */
gabomdq@7753
   287
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SHOP        221 */
gabomdq@7753
   288
    SDL_SCANCODE_UNKNOWN,       /*  KEY_ALTERASE        222 */
gabomdq@7753
   289
    SDL_SCANCODE_UNKNOWN,       /*  KEY_CANCEL      223 AC Cancel */
gabomdq@7753
   290
    SDL_SCANCODE_UNKNOWN,       /*  KEY_BRIGHTNESSDOWN  224 */
gabomdq@7753
   291
    SDL_SCANCODE_UNKNOWN,       /*  KEY_BRIGHTNESSUP    225 */
gabomdq@7753
   292
    SDL_SCANCODE_UNKNOWN,       /*  KEY_MEDIA       226 */
gabomdq@7753
   293
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SWITCHVIDEOMODE 227 Cycle between available video outputs (Monitor/LCD/TV-out/etc) */
gabomdq@7753
   294
    SDL_SCANCODE_UNKNOWN,       /*  KEY_KBDILLUMTOGGLE  228 */
gabomdq@7753
   295
    SDL_SCANCODE_UNKNOWN,       /*  KEY_KBDILLUMDOWN    229 */
gabomdq@7753
   296
    SDL_SCANCODE_UNKNOWN,       /*  KEY_KBDILLUMUP      230 */
gabomdq@7753
   297
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SEND        231 AC Send */
gabomdq@7753
   298
    SDL_SCANCODE_UNKNOWN,       /*  KEY_REPLY       232 AC Reply */
gabomdq@7753
   299
    SDL_SCANCODE_UNKNOWN,       /*  KEY_FORWARDMAIL     233 AC Forward Msg */
gabomdq@7753
   300
    SDL_SCANCODE_UNKNOWN,       /*  KEY_SAVE        234 AC Save */
gabomdq@7753
   301
    SDL_SCANCODE_UNKNOWN,       /*  KEY_DOCUMENTS       235 */
gabomdq@7753
   302
    SDL_SCANCODE_UNKNOWN,       /*  KEY_BATTERY     236  */
gabomdq@7753
   303
    SDL_SCANCODE_UNKNOWN,       /*  KEY_BLUETOOTH       237 */
gabomdq@7753
   304
    SDL_SCANCODE_UNKNOWN,       /*  KEY_WLAN        238 */
gabomdq@7753
   305
    SDL_SCANCODE_UNKNOWN,       /*  KEY_UWB         239 */
gabomdq@7753
   306
    SDL_SCANCODE_UNKNOWN,       /*  KEY_UNKNOWN     240 */
gabomdq@7753
   307
    SDL_SCANCODE_UNKNOWN,       /*  KEY_VIDEO_NEXT      241 drive next video source */
gabomdq@7753
   308
    SDL_SCANCODE_UNKNOWN,       /*  KEY_VIDEO_PREV      242 drive previous video source */
gabomdq@7753
   309
    SDL_SCANCODE_UNKNOWN,       /*  KEY_BRIGHTNESS_CYCLE    243 brightness up, after max is min */
gabomdq@7753
   310
    SDL_SCANCODE_UNKNOWN,       /*  KEY_BRIGHTNESS_ZERO 244 brightness off, use ambient */
gabomdq@7753
   311
    SDL_SCANCODE_UNKNOWN,       /*  KEY_DISPLAY_OFF     245 display device to off state */
gabomdq@7753
   312
    SDL_SCANCODE_UNKNOWN,       /*  KEY_WIMAX       246 */
gabomdq@7753
   313
    SDL_SCANCODE_UNKNOWN,       /*  KEY_RFKILL      247 Key that controls all radios */
gabomdq@7753
   314
    SDL_SCANCODE_UNKNOWN,       /*  KEY_MICMUTE     248 Mute / unmute the microphone */
gabomdq@7753
   315
};
gabomdq@7753
   316
gabomdq@7753
   317
static Uint8 EVDEV_MouseButtons[] = {
gabomdq@7753
   318
    SDL_BUTTON_LEFT,            /*  BTN_LEFT        0x110 */
gabomdq@7753
   319
    SDL_BUTTON_RIGHT,           /*  BTN_RIGHT       0x111 */
gabomdq@7753
   320
    SDL_BUTTON_MIDDLE,          /*  BTN_MIDDLE      0x112 */
gabomdq@7753
   321
    SDL_BUTTON_X1,              /*  BTN_SIDE        0x113 */
gabomdq@7753
   322
    SDL_BUTTON_X2,              /*  BTN_EXTRA       0x114 */
gabomdq@7753
   323
    SDL_BUTTON_X2 + 1,          /*  BTN_FORWARD     0x115 */
gabomdq@7753
   324
    SDL_BUTTON_X2 + 2,          /*  BTN_BACK        0x116 */
gabomdq@7753
   325
    SDL_BUTTON_X2 + 3           /*  BTN_TASK        0x117 */
gabomdq@7753
   326
};
gabomdq@7753
   327
gabomdq@7753
   328
int
gabomdq@7753
   329
SDL_EVDEV_Init(void)
gabomdq@7753
   330
{
gabomdq@7753
   331
    int retval = 0;
gabomdq@7753
   332
    
gabomdq@7753
   333
    if (_this == NULL) {
gabomdq@7753
   334
        _this = (SDL_EVDEV_PrivateData *) SDL_calloc(1, sizeof(*_this));
gabomdq@7753
   335
        if(_this == NULL) {
gabomdq@7753
   336
            return SDL_OutOfMemory();
gabomdq@7753
   337
        }
gabomdq@7753
   338
gabomdq@7753
   339
#if SDL_USE_LIBUDEV
gabomdq@7753
   340
        if (SDL_UDEV_Init() < 0) {
gabomdq@7753
   341
            SDL_free(_this);
gabomdq@7753
   342
            _this = NULL;
gabomdq@7753
   343
            return -1;
gabomdq@7753
   344
        }
gabomdq@7753
   345
gabomdq@7753
   346
        /* Set up the udev callback */
gabomdq@7753
   347
        if ( SDL_UDEV_AddCallback(SDL_EVDEV_udev_callback) < 0) {
gabomdq@7753
   348
            SDL_EVDEV_Quit();
gabomdq@7753
   349
            return -1;
gabomdq@7753
   350
        }
gabomdq@7753
   351
        
gabomdq@7753
   352
        /* Force a scan to build the initial device list */
gabomdq@7753
   353
        SDL_UDEV_Scan();
gabomdq@7753
   354
#else
gabomdq@7753
   355
        /* TODO: Scan the devices manually, like a caveman */
gabomdq@7753
   356
#endif /* SDL_USE_LIBUDEV */
gabomdq@7753
   357
gabomdq@7753
   358
    }
gabomdq@7753
   359
    
gabomdq@7753
   360
    _this->ref_count += 1;
gabomdq@7753
   361
    
gabomdq@7753
   362
    return retval;
gabomdq@7753
   363
}
gabomdq@7753
   364
gabomdq@7753
   365
void
gabomdq@7753
   366
SDL_EVDEV_Quit(void)
gabomdq@7753
   367
{
gabomdq@7753
   368
    if (_this == NULL) {
gabomdq@7753
   369
        return;
gabomdq@7753
   370
    }
gabomdq@7753
   371
    
gabomdq@7753
   372
    _this->ref_count -= 1;
gabomdq@7753
   373
    
gabomdq@7753
   374
    if (_this->ref_count < 1) {
gabomdq@7753
   375
        
gabomdq@7753
   376
#if SDL_USE_LIBUDEV
gabomdq@7753
   377
        SDL_UDEV_DelCallback(SDL_EVDEV_udev_callback);
gabomdq@7753
   378
        SDL_UDEV_Quit();
gabomdq@7753
   379
#endif /* SDL_USE_LIBUDEV */
gabomdq@7753
   380
       
gabomdq@7753
   381
        /* Remove existing devices */
gabomdq@7753
   382
        while(_this->first != NULL) {
gabomdq@7753
   383
            SDL_EVDEV_device_removed(_this->first->path);
gabomdq@7753
   384
        }
gabomdq@7753
   385
        
gabomdq@7753
   386
        SDL_assert(_this->first == NULL);
gabomdq@7753
   387
        SDL_assert(_this->last == NULL);
gabomdq@7753
   388
        SDL_assert(_this->numdevices == 0);
gabomdq@7753
   389
        
gabomdq@7753
   390
        SDL_free(_this);
gabomdq@7753
   391
        _this = NULL;
gabomdq@7753
   392
    }
gabomdq@7753
   393
}
gabomdq@7753
   394
gabomdq@7755
   395
#if SDL_USE_LIBUDEV
gabomdq@7753
   396
void SDL_EVDEV_udev_callback(SDL_UDEV_deviceevent udev_type, SDL_UDEV_deviceclass udev_class, const char *devpath)
gabomdq@7753
   397
{
gabomdq@7753
   398
    SDL_EVDEV_deviceclass devclass;
gabomdq@7753
   399
    
gabomdq@7753
   400
    if (devpath == NULL) {
gabomdq@7753
   401
        return;
gabomdq@7753
   402
    }
gabomdq@7753
   403
    
gabomdq@7753
   404
    switch( udev_class )
gabomdq@7753
   405
    {
gabomdq@7753
   406
        case SDL_UDEV_DEVICE_MOUSE:
gabomdq@7753
   407
            devclass = SDL_EVDEV_DEVICE_MOUSE;
gabomdq@7753
   408
            break;
gabomdq@7753
   409
            
gabomdq@7753
   410
        case SDL_UDEV_DEVICE_KEYBOARD:
gabomdq@7753
   411
            devclass = SDL_EVDEV_DEVICE_KEYBOARD;
gabomdq@7753
   412
            break;
gabomdq@7753
   413
            
gabomdq@7753
   414
        default:
gabomdq@7753
   415
            return;
gabomdq@7753
   416
    }
gabomdq@7753
   417
    
gabomdq@7753
   418
    switch( udev_type )
gabomdq@7753
   419
    {
gabomdq@7753
   420
        case SDL_UDEV_DEVICEADDED:
gabomdq@7753
   421
            SDL_EVDEV_device_added(devclass, devpath);
gabomdq@7753
   422
            break;
gabomdq@7753
   423
            
gabomdq@7753
   424
        case SDL_UDEV_DEVICEREMOVED:
gabomdq@7753
   425
            SDL_EVDEV_device_removed(devpath);
gabomdq@7753
   426
            break;
gabomdq@7753
   427
            
gabomdq@7753
   428
        default:
gabomdq@7753
   429
            break;
gabomdq@7753
   430
            
gabomdq@7753
   431
    }
gabomdq@7753
   432
    
gabomdq@7753
   433
}
gabomdq@7753
   434
gabomdq@7755
   435
#endif /* SDL_USE_LIBUDEV */
gabomdq@7755
   436
gabomdq@7753
   437
void 
gabomdq@7753
   438
SDL_EVDEV_Poll(void)
gabomdq@7753
   439
{
gabomdq@7753
   440
    struct input_event events[32];
gabomdq@7753
   441
    int i, len;
gabomdq@7753
   442
    SDL_evdevlist_item *item;
gabomdq@7753
   443
    SDL_Scancode scan_code;
gabomdq@7753
   444
    int mouse_button;
gabomdq@7753
   445
    SDL_Mouse *mouse;
gabomdq@7753
   446
    
gabomdq@7753
   447
#if SDL_USE_LIBUDEV
gabomdq@7753
   448
    SDL_UDEV_Poll();
gabomdq@7753
   449
#endif
gabomdq@7753
   450
    
gabomdq@7753
   451
    for (item = _this->first; item != NULL; item = item->next) {
gabomdq@7753
   452
        while ((len = read(item->fd, events, (sizeof events))) > 0) {
gabomdq@7753
   453
            len /= sizeof(events[0]);
gabomdq@7753
   454
            for (i = 0; i < len; ++i) {
gabomdq@7753
   455
                switch(item->devclass) {
gabomdq@7753
   456
                    case SDL_EVDEV_DEVICE_KEYBOARD:
gabomdq@7753
   457
                        switch (events[i].type) {
gabomdq@7753
   458
                            case EV_KEY:
gabomdq@7753
   459
                                scan_code = SDL_EVDEV_translate_keycode(events[i].code);
gabomdq@7753
   460
                                if (scan_code != SDL_SCANCODE_UNKNOWN) {
gabomdq@7753
   461
                                    if (events[i].value == 0) {
gabomdq@7753
   462
                                        SDL_SendKeyboardKey(SDL_RELEASED, scan_code);
gabomdq@7753
   463
                                    }
gabomdq@7753
   464
                                    else if (events[i].value == 1) {
gabomdq@7753
   465
                                        SDL_SendKeyboardKey(SDL_PRESSED, scan_code);
gabomdq@7753
   466
                                    }
gabomdq@7753
   467
                                    else if (events[i].value == 2) {
gabomdq@7753
   468
                                        /* Key repeated */
gabomdq@7753
   469
                                        SDL_SendKeyboardKey(SDL_PRESSED, scan_code);
gabomdq@7753
   470
                                    }
gabomdq@7753
   471
                                }
gabomdq@7753
   472
                                break;
gabomdq@7753
   473
gabomdq@7753
   474
                            default:
gabomdq@7753
   475
                                break;
gabomdq@7753
   476
                        }
gabomdq@7753
   477
                        break; /* SDL_EVDEV_DEVICE_KEYBOARD */
gabomdq@7753
   478
                        
gabomdq@7753
   479
                    case SDL_EVDEV_DEVICE_MOUSE:
gabomdq@7753
   480
                        mouse = SDL_GetMouse();
gabomdq@7753
   481
                        switch (events[i].type) {
gabomdq@7753
   482
                            case EV_KEY:
gabomdq@7753
   483
                                mouse_button = events[i].code - BTN_MOUSE;
gabomdq@7753
   484
                                if (mouse_button >= 0 && mouse_button < SDL_arraysize(EVDEV_MouseButtons)) {
gabomdq@7753
   485
                                    if (events[i].value == 0) {
gabomdq@7753
   486
                                        SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, EVDEV_MouseButtons[mouse_button]);
gabomdq@7753
   487
                                    }
gabomdq@7753
   488
                                    else if (events[i].value == 1) {
gabomdq@7753
   489
                                        SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_PRESSED, EVDEV_MouseButtons[mouse_button]);
gabomdq@7753
   490
                                    } 
gabomdq@7753
   491
                                }
gabomdq@7753
   492
                                break;
gabomdq@7753
   493
                            case EV_ABS:
gabomdq@7753
   494
                                 switch(events[i].code) {
gabomdq@7753
   495
                                    case ABS_X:
gabomdq@7753
   496
                                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_FALSE, events[i].value, mouse->y);
gabomdq@7753
   497
                                        break;
gabomdq@7753
   498
                                    case ABS_Y:
gabomdq@7753
   499
                                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_FALSE, mouse->x, events[i].value);
gabomdq@7753
   500
                                        break;
gabomdq@7753
   501
                                    default:
gabomdq@7753
   502
                                        break;
gabomdq@7753
   503
                                 }
gabomdq@7753
   504
                                break;
gabomdq@7753
   505
                            case EV_REL:
gabomdq@7753
   506
                                switch(events[i].code) {
gabomdq@7753
   507
                                    case REL_X:
gabomdq@7753
   508
                                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_TRUE, events[i].value, 0);
gabomdq@7753
   509
                                        break;
gabomdq@7753
   510
                                    case REL_Y:
gabomdq@7753
   511
                                        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_TRUE, 0, events[i].value);
gabomdq@7753
   512
                                        break;
gabomdq@7753
   513
                                    case REL_WHEEL:
gabomdq@7753
   514
                                        SDL_SendMouseWheel(mouse->focus, mouse->mouseID, 0, events[i].value);
gabomdq@7753
   515
                                        break;
gabomdq@7753
   516
                                    case REL_HWHEEL:
gabomdq@7753
   517
                                        SDL_SendMouseWheel(mouse->focus, mouse->mouseID, events[i].value, 0);
gabomdq@7753
   518
                                        break;
gabomdq@7753
   519
                                    default:
gabomdq@7753
   520
                                        break;
gabomdq@7753
   521
                                }
gabomdq@7753
   522
                                break;
gabomdq@7753
   523
                            default:
gabomdq@7753
   524
                                break;
gabomdq@7753
   525
                        }
gabomdq@7753
   526
                        break; /* SDL_EVDEV_DEVICE_MOUSE */                    
gabomdq@7753
   527
gabomdq@7753
   528
                    default:
gabomdq@7753
   529
                        break;
gabomdq@7753
   530
                }
gabomdq@7753
   531
                
gabomdq@7753
   532
                
gabomdq@7753
   533
                /* Handle events not specific to any type of device */
gabomdq@7753
   534
                switch (events[i].type) {
gabomdq@7753
   535
                    case EV_SYN:
gabomdq@7753
   536
                        switch (events[i].code) {
gabomdq@7753
   537
                        case SYN_DROPPED :
gabomdq@7753
   538
                            SDL_EVDEV_sync_device(item);
gabomdq@7753
   539
                            break;
gabomdq@7753
   540
                        default:
gabomdq@7753
   541
                            break;
gabomdq@7753
   542
                        }
gabomdq@7753
   543
                }
gabomdq@7753
   544
            
gabomdq@7753
   545
            }
gabomdq@7753
   546
        }    
gabomdq@7753
   547
    }
gabomdq@7753
   548
}
gabomdq@7753
   549
gabomdq@7753
   550
static SDL_Scancode
gabomdq@7753
   551
SDL_EVDEV_translate_keycode(int keycode)
gabomdq@7753
   552
{
gabomdq@7753
   553
    SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
gabomdq@7753
   554
gabomdq@7753
   555
    if (keycode < SDL_arraysize(EVDEV_Keycodes)) {
gabomdq@7753
   556
        scancode = EVDEV_Keycodes[keycode];
gabomdq@7753
   557
    }
gabomdq@7753
   558
    if (scancode == SDL_SCANCODE_UNKNOWN) {
gabomdq@7753
   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);
gabomdq@7753
   560
    }
gabomdq@7753
   561
    return scancode;
gabomdq@7753
   562
}
gabomdq@7753
   563
gabomdq@7753
   564
static void
gabomdq@7753
   565
SDL_EVDEV_sync_device(SDL_evdevlist_item *item) 
gabomdq@7753
   566
{
gabomdq@7753
   567
    /* TODO: get full state of device and report whatever is required */
gabomdq@7753
   568
}
gabomdq@7753
   569
gabomdq@7755
   570
#if SDL_USE_LIBUDEV
gabomdq@7753
   571
static int
gabomdq@7753
   572
SDL_EVDEV_device_added(const SDL_UDEV_deviceclass devclass, const char *devpath)
gabomdq@7753
   573
{
gabomdq@7753
   574
    SDL_evdevlist_item *item;
gabomdq@7753
   575
gabomdq@7753
   576
    /* Check to make sure it's not already in list. */
gabomdq@7753
   577
    for (item = _this->first; item != NULL; item = item->next) {
gabomdq@7753
   578
        if (strcmp(devpath, item->path) == 0) {
gabomdq@7753
   579
            return -1;  /* already have this one */
gabomdq@7753
   580
        }
gabomdq@7753
   581
    }
gabomdq@7753
   582
    
gabomdq@7753
   583
    item = (SDL_evdevlist_item *) SDL_calloc(1, sizeof (SDL_evdevlist_item));
gabomdq@7753
   584
    if (item == NULL) {
gabomdq@7753
   585
        return SDL_OutOfMemory();
gabomdq@7753
   586
    }
gabomdq@7753
   587
gabomdq@7753
   588
    item->devclass = devclass;
gabomdq@7753
   589
    
gabomdq@7753
   590
   
gabomdq@7753
   591
    item->fd = open(devpath, O_RDONLY, 0);
gabomdq@7753
   592
    if (item->fd < 0) {
gabomdq@7753
   593
        SDL_free(item);
gabomdq@7753
   594
        return SDL_SetError("Unable to open %s", devpath);
gabomdq@7753
   595
    }
gabomdq@7753
   596
    
gabomdq@7753
   597
    item->path = SDL_strdup(devpath);
gabomdq@7753
   598
    if (item->path == NULL) {
gabomdq@7753
   599
        close(item->fd);
gabomdq@7753
   600
        SDL_free(item);
gabomdq@7753
   601
        return SDL_OutOfMemory();
gabomdq@7753
   602
    }
gabomdq@7753
   603
    
gabomdq@7753
   604
    /* Non blocking read mode */
gabomdq@7753
   605
    fcntl(item->fd, F_SETFL, O_NONBLOCK);
gabomdq@7753
   606
    
gabomdq@7753
   607
    if (_this->last == NULL) {
gabomdq@7753
   608
        _this->first = _this->last = item;
gabomdq@7753
   609
    } else {
gabomdq@7753
   610
        _this->last->next = item;
gabomdq@7753
   611
        _this->last = item;
gabomdq@7753
   612
    }
gabomdq@7753
   613
    
gabomdq@7753
   614
    SDL_EVDEV_sync_device(item);
gabomdq@7753
   615
    
gabomdq@7753
   616
    return _this->numdevices++;
gabomdq@7753
   617
}
gabomdq@7753
   618
gabomdq@7753
   619
gabomdq@7753
   620
static int
gabomdq@7753
   621
SDL_EVDEV_device_removed(const char *devpath)
gabomdq@7753
   622
{
gabomdq@7753
   623
    SDL_evdevlist_item *item;
gabomdq@7753
   624
    SDL_evdevlist_item *prev = NULL;
gabomdq@7753
   625
gabomdq@7753
   626
    for (item = _this->first; item != NULL; item = item->next) {
gabomdq@7753
   627
        /* found it, remove it. */
gabomdq@7753
   628
        if ( strcmp(devpath, item->path) ==0 ) {
gabomdq@7753
   629
            if (prev != NULL) {
gabomdq@7753
   630
                prev->next = item->next;
gabomdq@7753
   631
            } else {
gabomdq@7753
   632
                SDL_assert(_this->first == item);
gabomdq@7753
   633
                _this->first = item->next;
gabomdq@7753
   634
            }
gabomdq@7753
   635
            if (item == _this->last) {
gabomdq@7753
   636
                _this->last = prev;
gabomdq@7753
   637
            }
gabomdq@7753
   638
            close(item->fd);
gabomdq@7753
   639
            SDL_free(item->path);
gabomdq@7753
   640
            SDL_free(item);
gabomdq@7753
   641
            _this->numdevices--;
gabomdq@7753
   642
            return 0;
gabomdq@7753
   643
        }
gabomdq@7753
   644
        prev = item;
gabomdq@7753
   645
    }
gabomdq@7753
   646
gabomdq@7753
   647
    return -1;
gabomdq@7753
   648
}
gabomdq@7755
   649
#endif /* SDL_USE_LIBUDEV */
gabomdq@7753
   650
gabomdq@7753
   651
#endif /* SDL_INPUT_LINUXEV */
gabomdq@7753
   652
gabomdq@7753
   653
/* vi: set ts=4 sw=4 expandtab: */