82
82
83
83
#define DEFAULT_EVDEV_PATH "/dev/input/" // Path to the linux input events
84
84
85
+ // So actually the biggest key is KEY_CNT but we only really map the keys up to
86
+ // KEY_ALS_TOGGLE
87
+ #define KEYMAP_SIZE KEY_ALS_TOGGLE
88
+
85
89
//----------------------------------------------------------------------------------
86
90
// Types and Structures Definition
87
91
//----------------------------------------------------------------------------------
@@ -135,38 +139,81 @@ static PlatformData platform = { 0 }; // Platform specific data
135
139
//----------------------------------------------------------------------------------
136
140
// Local Variables Definition
137
141
//----------------------------------------------------------------------------------
138
- // Scancode to keycode mapping for US keyboards
142
+
143
+ // NOTE: The complete evdev EV_KEY list can be found at /usr/include/linux/input-event-codes.h
144
+ // TODO: Complete the LUT with all unicode decimal values
139
145
// TODO: Replace this with a keymap from the X11 to get the correct regional map for the keyboard:
140
146
// Currently non US keyboards will have the wrong mapping for some keys
141
147
// NOTE: Replacing this with the keymap from X11 would probably be useless, as people use the drm
142
148
// backend to *avoid* X11
143
- static const int keymapUS [] = {
144
- 0 , 256 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 48 , 45 , 61 , 259 , 258 , 81 , 87 , 69 , 82 , 84 ,
145
- 89 , 85 , 73 , 79 , 80 , 91 , 93 , 257 , 341 , 65 , 83 , 68 , 70 , 71 , 72 , 74 , 75 , 76 , 59 , 39 , 96 ,
146
- 340 , 92 , 90 , 88 , 67 , 86 , 66 , 78 , 77 , 44 , 46 , 47 , 344 , 332 , 342 , 32 , 280 , 290 , 291 ,
147
- 292 , 293 , 294 , 295 , 296 , 297 , 298 , 299 , 282 , 281 , 327 , 328 , 329 , 333 , 324 , 325 ,
148
- 326 , 334 , 321 , 322 , 323 , 320 , 330 , 0 , 85 , 86 , 300 , 301 , 89 , 90 , 91 , 92 , 93 , 94 , 95 ,
149
- 335 , 345 , 331 , 283 , 346 , 101 , 268 , 265 , 266 , 263 , 262 , 269 , 264 , 267 , 260 , 261 ,
150
- 112 , 113 , 114 , 115 , 116 , 117 , 118 , 119 , 120 , 121 , 122 , 123 , 124 , 125 , 347 , 127 ,
151
- 128 , 129 , 130 , 131 , 132 , 133 , 134 , 135 , 136 , 137 , 138 , 139 , 140 , 141 , 142 , 143 ,
152
- 144 , 145 , 146 , 147 , 148 , 149 , 150 , 151 , 152 , 153 , 154 , 155 , 156 , 157 , 158 , 159 ,
153
- 160 , 161 , 162 , 163 , 164 , 165 , 166 , 167 , 168 , 169 , 170 , 171 , 172 , 173 , 174 , 175 ,
154
- 176 , 177 , 178 , 179 , 180 , 181 , 182 , 183 , 184 , 185 , 186 , 187 , 188 , 189 , 190 , 191 ,
155
- 192 , 193 , 194 , 0 , 0 , 0 , 0 , 0 , 200 , 201 , 202 , 203 , 204 , 205 , 206 , 207 , 208 , 209 , 210 ,
156
- 211 , 212 , 213 , 214 , 215 , 216 , 217 , 218 , 219 , 220 , 221 , 222 , 223 , 224 , 225 , 226 ,
157
- 227 , 228 , 229 , 230 , 231 , 232 , 233 , 234 , 235 , 236 , 237 , 238 , 239 , 240 , 241 , 242 ,
158
- 243 , 244 , 245 , 246 , 247 , 248 , 0 , 0 , 0 , 0 , 0 , 0 , 0
159
- };
160
-
161
- // NOTE: The complete evdev EV_KEY list can be found at /usr/include/linux/input-event-codes.h
162
- // TODO: Complete the LUT with all unicode decimal values
163
149
static const int evkeyToUnicodeLUT [] = {
164
150
0 , 27 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 48 , 45 , 61 , 8 , 0 , 113 , 119 , 101 , 114 ,
165
151
116 , 121 , 117 , 105 , 111 , 112 , 0 , 0 , 13 , 0 , 97 , 115 , 100 , 102 , 103 , 104 , 106 , 107 , 108 , 59 ,
166
152
39 , 96 , 0 , 92 , 122 , 120 , 99 , 118 , 98 , 110 , 109 , 44 , 46 , 47 , 0 , 0 , 0 , 32
167
153
// LUT currently incomplete, just mapped the most essential keys
168
154
};
169
155
156
+ // This is the map used to map any keycode returned from linux to a raylib code from 'raylib.h'
157
+ // NOTE: Use short here to save a little memory
158
+ static const short linuxToRaylibMap [KEYMAP_SIZE ] = {
159
+ // We don't map those with designated initialization, because we would getting
160
+ // into loads of naming conflicts
161
+ 0 , 256 , 49 , 50 , 51 , 52 , 53 , 54 ,
162
+ 55 , 56 , 57 , 48 , 45 , 61 , 259 , 258 ,
163
+ 81 , 87 , 69 , 82 , 84 , 89 , 85 , 73 ,
164
+ 79 , 80 , 91 , 93 , 257 , 341 , 65 , 83 ,
165
+ 68 , 70 , 71 , 72 , 74 , 75 , 76 , 59 ,
166
+ 39 , 96 , 340 , 92 , 90 , 88 , 67 , 86 ,
167
+ 66 , 78 , 77 , 44 , 46 , 47 , 344 , 332 ,
168
+ 342 , 32 , 280 , 290 , 291 , 292 , 293 , 294 ,
169
+ 295 , 296 , 297 , 298 , 299 , 282 , 281 , 327 ,
170
+ 328 , 329 , 333 , 324 , 325 , 326 , 334 , 321 ,
171
+ 322 , 323 , 320 , 330 , 0 , 85 , 86 , 300 ,
172
+ 301 , 89 , 90 , 91 , 92 , 93 , 94 , 95 ,
173
+ 335 , 345 , 331 , 283 , 346 , 101 , 268 , 265 ,
174
+ 266 , 263 , 262 , 269 , 264 , 267 , 260 , 261 ,
175
+ 112 , 113 , 114 , 115 , 116 , 117 , 118 , 119 ,
176
+ 120 , 121 , 122 , 123 , 124 , 125 , 347 , 127 ,
177
+ 128 , 129 , 130 , 131 , 132 , 133 , 134 , 135 ,
178
+ 136 , 137 , 138 , 139 , 140 , 141 , 142 , 143 ,
179
+ 144 , 145 , 146 , 147 , 148 , 149 , 150 , 151 ,
180
+ 152 , 153 , 154 , 155 , 156 , 157 , 158 , 159 ,
181
+ 160 , 161 , 162 , 163 , 164 , 165 , 166 , 167 ,
182
+ 168 , 169 , 170 , 171 , 172 , 173 , 174 , 175 ,
183
+ 176 , 177 , 178 , 179 , 180 , 181 , 182 , 183 ,
184
+ 184 , 185 , 186 , 187 , 188 , 189 , 190 , 191 ,
185
+ 192 , 193 , 194 , 0 , 0 , 0 , 0 , 0 ,
186
+ 200 , 201 , 202 , 203 , 204 , 205 , 206 , 207 ,
187
+ 208 , 209 , 210 , 211 , 212 , 213 , 214 , 215 ,
188
+ 216 , 217 , 218 , 219 , 220 , 221 , 222 , 223 ,
189
+ 224 , 225 , 226 , 227 , 228 , 229 , 230 , 231 ,
190
+ 232 , 233 , 234 , 235 , 236 , 237 , 238 , 239 ,
191
+ 240 , 241 , 242 , 243 , 244 , 245 , 246 , 247 ,
192
+ 248 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
193
+
194
+ // Gamepads are mapped according to:
195
+ // https://www.kernel.org/doc/html/next/input/gamepad.html
196
+ // Those mappings are standardized, but that doesn't mean people follow
197
+ // the standards, so this is more of an approximation
198
+ [BTN_DPAD_UP ] = GAMEPAD_BUTTON_LEFT_FACE_UP ,
199
+ [BTN_DPAD_RIGHT ] = GAMEPAD_BUTTON_LEFT_FACE_RIGHT ,
200
+ [BTN_DPAD_DOWN ] = GAMEPAD_BUTTON_LEFT_FACE_DOWN ,
201
+ [BTN_DPAD_LEFT ] = GAMEPAD_BUTTON_LEFT_FACE_LEFT ,
202
+ [BTN_Y ] = GAMEPAD_BUTTON_RIGHT_FACE_UP ,
203
+ [BTN_B ] = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT ,
204
+ [BTN_A ] = GAMEPAD_BUTTON_RIGHT_FACE_DOWN ,
205
+ [BTN_X ] = GAMEPAD_BUTTON_RIGHT_FACE_LEFT ,
206
+ [BTN_TL ] = GAMEPAD_BUTTON_LEFT_TRIGGER_1 ,
207
+ [BTN_TL2 ] = GAMEPAD_BUTTON_LEFT_TRIGGER_2 ,
208
+ [BTN_TR ] = GAMEPAD_BUTTON_RIGHT_TRIGGER_1 ,
209
+ [BTN_TR2 ] GAMEPAD_BUTTON_RIGHT_TRIGGER_2 ,
210
+ [BTN_SELECT ] = GAMEPAD_BUTTON_MIDDLE_LEFT ,
211
+ [BTN_MODE ] = GAMEPAD_BUTTON_MIDDLE ,
212
+ [BTN_START ] = GAMEPAD_BUTTON_MIDDLE_RIGHT ,
213
+ [BTN_THUMBL ] = GAMEPAD_BUTTON_LEFT_THUMB ,
214
+ [BTN_THUMBR ] = GAMEPAD_BUTTON_RIGHT_THUMB ,
215
+ };
216
+
170
217
//----------------------------------------------------------------------------------
171
218
// Module Internal Functions Declaration
172
219
//----------------------------------------------------------------------------------
@@ -574,7 +621,7 @@ void PollInputEvents(void)
574
621
CORE .Input .Mouse .previousWheelMove = CORE .Input .Mouse .currentWheelMove ;
575
622
CORE .Input .Mouse .currentWheelMove = platform .eventWheelMove ;
576
623
platform .eventWheelMove = (Vector2 ){ 0.0f , 0.0f };
577
-
624
+
578
625
for (int i = 0 ; i < MAX_MOUSE_BUTTONS ; i ++ )
579
626
{
580
627
CORE .Input .Mouse .previousButtonState [i ] = CORE .Input .Mouse .currentButtonState [i ];
@@ -1293,7 +1340,10 @@ static void ConfigureEvdevDevice(char *device)
1293
1340
1294
1341
// Identify the device
1295
1342
//-------------------------------------------------------------------------------------------------------
1296
- struct input_absinfo absinfo [ABS_CNT ] = { 0 };
1343
+ struct {
1344
+ bool exist ;
1345
+ struct input_absinfo info ;
1346
+ } absinfo [ABS_CNT ] = { 0 };
1297
1347
1298
1348
// These flags aren't really a one of
1299
1349
// Some devices could have properties we assosciate with keyboards as well as properties
@@ -1318,8 +1368,12 @@ static void ConfigureEvdevDevice(char *device)
1318
1368
if (hasAbsXY )
1319
1369
{
1320
1370
absAxisCount += 2 ;
1321
- ioctl (fd , EVIOCGABS (ABS_X ), & absinfo [ABS_X ]);
1322
- ioctl (fd , EVIOCGABS (ABS_Y ), & absinfo [ABS_Y ]);
1371
+
1372
+ absinfo [ABS_X ].exist = true;
1373
+ absinfo [ABS_Y ].exist = true;
1374
+
1375
+ ioctl (fd , EVIOCGABS (ABS_X ), & absinfo [ABS_X ].info );
1376
+ ioctl (fd , EVIOCGABS (ABS_Y ), & absinfo [ABS_Y ].info );
1323
1377
}
1324
1378
1325
1379
// If it has any of these buttons it's a touch device
@@ -1340,10 +1394,11 @@ static void ConfigureEvdevDevice(char *device)
1340
1394
{
1341
1395
if (TEST_BIT (absBits , axis ))
1342
1396
{
1397
+ absinfo [axis ].exist = true;
1343
1398
isGamepad = true;
1344
1399
absAxisCount ++ ;
1345
1400
1346
- ioctl (fd , EVIOCGABS (axis ), & absinfo [axis ]);
1401
+ ioctl (fd , EVIOCGABS (axis ), & absinfo [axis ]. info );
1347
1402
}
1348
1403
}
1349
1404
}
@@ -1398,11 +1453,11 @@ static void ConfigureEvdevDevice(char *device)
1398
1453
1399
1454
if (absAxisCount > 0 )
1400
1455
{
1401
- platform .absRange .x = absinfo [ABS_X ].minimum ;
1402
- platform .absRange .width = absinfo [ABS_X ].maximum - absinfo [ABS_X ].minimum ;
1456
+ platform .absRange .x = absinfo [ABS_X ].info . minimum ;
1457
+ platform .absRange .width = absinfo [ABS_X ].info . maximum - absinfo [ABS_X ]. info .minimum ;
1403
1458
1404
- platform .absRange .y = absinfo [ABS_Y ].minimum ;
1405
- platform .absRange .height = absinfo [ABS_Y ].maximum - absinfo [ABS_Y ].minimum ;
1459
+ platform .absRange .y = absinfo [ABS_Y ].info . minimum ;
1460
+ platform .absRange .height = absinfo [ABS_Y ].info . maximum - absinfo [ABS_Y ]. info .minimum ;
1406
1461
}
1407
1462
}
1408
1463
else if (isGamepad && !isMouse && !isKeyboard && platform .gamepadCount < MAX_GAMEPADS )
@@ -1429,10 +1484,14 @@ static void ConfigureEvdevDevice(char *device)
1429
1484
int axisIndex = 0 ;
1430
1485
for (int axis = ABS_X ; axis < ABS_PRESSURE ; axis ++ )
1431
1486
{
1432
- platform .gamepadAbsAxisRange [index ][axisIndex ][0 ] = absinfo [axisIndex ].minimum ;
1433
- platform .gamepadAbsAxisRange [index ][axisIndex ][1 ] = absinfo [axisIndex ].maximum - absinfo [axisIndex ].minimum ;
1487
+ if (absinfo [axis ].exist )
1488
+ {
1489
+ platform .gamepadAbsAxisRange [index ][axisIndex ][0 ] = absinfo [axisIndex ].info .minimum ;
1490
+ platform .gamepadAbsAxisRange [index ][axisIndex ][1 ] = absinfo [axisIndex ].info .maximum - absinfo [axisIndex ].info .minimum ;
1434
1491
1435
- platform .gamepadAbsAxisMap [index ][axis ] = axisIndex ++ ;
1492
+ platform .gamepadAbsAxisMap [index ][axis ] = axisIndex ;
1493
+ axisIndex ++ ;
1494
+ }
1436
1495
}
1437
1496
}
1438
1497
}
@@ -1475,7 +1534,7 @@ static void PollKeyboardEvents(void)
1475
1534
{
1476
1535
1477
1536
// Lookup the scancode in the keymap to get a keycode
1478
- keycode = keymapUS [event .code ];
1537
+ keycode = linuxToRaylibMap [event .code ];
1479
1538
1480
1539
// Make sure we got a valid keycode
1481
1540
if ((keycode > 0 ) && (keycode < MAX_KEYBOARD_KEYS ))
@@ -1527,27 +1586,38 @@ static void PollGamepadEvents(void)
1527
1586
{
1528
1587
if (event .type == EV_KEY )
1529
1588
{
1530
- if (event .code >= MAX_GAMEPAD_BUTTONS ) continue ;
1589
+ if (event .code < KEYMAP_SIZE )
1590
+ {
1591
+ short keycodeRaylib = linuxToRaylibMap [event .code ];
1531
1592
1532
- TRACELOG (LOG_DEBUG , "INPUT: Gamepad %i button : %i, value : %i " , i , event .code , event .value );
1593
+ TRACELOG (LOG_DEBUG , "INPUT: Gamepad %2i: KEY_%s Keycode(linux) : %4i Keycode(raylib) : %4i " , i , ( event .value == 0 ) ? "UP " : "DOWN" , event .code , keycodeRaylib );
1533
1594
1534
- // 1 - button pressed, 0 - button released
1535
- CORE .Input .Gamepad .currentButtonState [i ][event .code ] = event .value ;
1595
+ if ((keycodeRaylib != 0 ) && (keycodeRaylib < MAX_GAMEPAD_BUTTONS ))
1596
+ {
1597
+ // 1 - button pressed, 0 - button released
1598
+ CORE .Input .Gamepad .currentButtonState [i ][keycodeRaylib ] = event .value ;
1536
1599
1537
- if (event .value == 1 ) CORE .Input .Gamepad .lastButtonPressed = event .code ;
1538
- else CORE .Input .Gamepad .lastButtonPressed = 0 ; // GAMEPAD_BUTTON_UNKNOWN
1600
+ CORE .Input .Gamepad .lastButtonPressed = (event .value == 1 )? keycodeRaylib : GAMEPAD_BUTTON_UNKNOWN ;
1601
+ }
1602
+ }
1539
1603
}
1540
1604
else if (event .type == EV_ABS )
1541
1605
{
1542
- if (event .code >= MAX_GAMEPAD_AXIS ) continue ;
1606
+ if (event .code < ABS_CNT )
1607
+ {
1608
+ int axisRaylib = platform .gamepadAbsAxisMap [i ][event .code ];
1543
1609
1544
- TRACELOG (LOG_DEBUG , "INPUT: Gamepad %i axis : %i, value : %i" , i , platform . gamepadAbsAxisMap [ i ][ event . code ] , event .value );
1610
+ TRACELOG (LOG_DEBUG , "INPUT: Gamepad %2i: Axis : %2i Value : %i" , i , axisRaylib , event .value );
1545
1611
1546
- int min = platform .gamepadAbsAxisRange [i ][event .code ][0 ];
1547
- int range = platform .gamepadAbsAxisRange [i ][event .code ][1 ];
1612
+ if (axisRaylib < MAX_GAMEPAD_AXIS )
1613
+ {
1614
+ int min = platform .gamepadAbsAxisRange [i ][event .code ][0 ];
1615
+ int range = platform .gamepadAbsAxisRange [i ][event .code ][1 ];
1548
1616
1549
- // NOTE: Scaling of event.value to get values between -1..1
1550
- CORE .Input .Gamepad .axisState [i ][platform .gamepadAbsAxisMap [i ][event .code ]] = (2 * (float )(event .value - min ) / range ) - 1 ;
1617
+ // NOTE: Scaling of event.value to get values between -1..1
1618
+ CORE .Input .Gamepad .axisState [i ][axisRaylib ] = (2 * (float )(event .value - min ) / range ) - 1 ;
1619
+ }
1620
+ }
1551
1621
}
1552
1622
}
1553
1623
}
@@ -1576,7 +1646,7 @@ static void PollMouseEvents(void)
1576
1646
CORE .Input .Mouse .previousPosition .x = 0.0f ;
1577
1647
}
1578
1648
else CORE .Input .Mouse .currentPosition .x += event .value ;
1579
-
1649
+
1580
1650
CORE .Input .Touch .position [0 ].x = CORE .Input .Mouse .currentPosition .x ;
1581
1651
touchAction = 2 ; // TOUCH_ACTION_MOVE
1582
1652
}
@@ -1589,7 +1659,7 @@ static void PollMouseEvents(void)
1589
1659
CORE .Input .Mouse .previousPosition .y = 0.0f ;
1590
1660
}
1591
1661
else CORE .Input .Mouse .currentPosition .y += event .value ;
1592
-
1662
+
1593
1663
CORE .Input .Touch .position [0 ].y = CORE .Input .Mouse .currentPosition .y ;
1594
1664
touchAction = 2 ; // TOUCH_ACTION_MOVE
1595
1665
}
0 commit comments