| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <linux/input.h> | 5 #include <linux/input.h> |
| 6 #include <cstdint> | 6 #include <cstdint> |
| 7 #include <list> |
| 7 #include <map> | 8 #include <map> |
| 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "ui/events/ozone/gamepad/gamepad_mapping.h" | 12 #include "ui/events/ozone/evdev/event_device_info.h" |
| 13 #include "ui/events/ozone/gamepad/static_gamepad_mapping.h" |
| 11 #include "ui/events/ozone/gamepad/webgamepad_constants.h" | 14 #include "ui/events/ozone/gamepad/webgamepad_constants.h" |
| 12 | 15 |
| 13 namespace ui { | 16 namespace ui { |
| 14 | 17 |
| 15 // KeyMap maps evdev key code to web gamepad code. | 18 typedef bool (*GamepadMapperFunction)(uint16_t key, |
| 16 struct KeyMapEntry { | 19 uint16_t code, |
| 17 uint16_t evdev_code; | 20 GamepadEventType* mapped_type, |
| 18 uint16_t mapped_code; | 21 uint16_t* mapped_code); |
| 19 }; | |
| 20 | |
| 21 // AbsMap maps evdev abs code to web gamepad (type, code). | |
| 22 struct AbsMapEntry { | |
| 23 uint16_t evdev_code; | |
| 24 GamepadEventType mapped_type; | |
| 25 uint16_t mapped_code; | |
| 26 }; | |
| 27 | |
| 28 using KeyMapType = const KeyMapEntry[]; | |
| 29 using AbsMapType = const AbsMapEntry[]; | |
| 30 | |
| 31 #define TO_BTN(code, mapped_code) \ | |
| 32 { code, GamepadEventType::BUTTON, mapped_code } | |
| 33 | |
| 34 #define TO_ABS(code, mapped_code) \ | |
| 35 { code, GamepadEventType::AXIS, mapped_code } | |
| 36 | 22 |
| 37 #define DO_MAPPING \ | 23 #define DO_MAPPING \ |
| 38 DoGamepadMapping(key_mapping, arraysize(key_mapping), abs_mapping, \ | 24 DoGamepadMapping(key_mapping, arraysize(key_mapping), abs_mapping, \ |
| 39 arraysize(abs_mapping), type, code, mapped_type, \ | 25 arraysize(abs_mapping), type, code, mapped_type, \ |
| 40 mapped_code) | 26 mapped_code) |
| 41 | 27 |
| 42 bool DoGamepadMapping(const KeyMapEntry* key_mapping, | 28 bool DoGamepadMapping(const KeyMapEntry* key_mapping, |
| 43 size_t key_map_size, | 29 size_t key_map_size, |
| 44 const AbsMapEntry* abs_mapping, | 30 const AbsMapEntry* abs_mapping, |
| 45 size_t abs_map_size, | 31 size_t abs_map_size, |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 181 |
| 196 bool ADT1Mapper(uint16_t type, | 182 bool ADT1Mapper(uint16_t type, |
| 197 uint16_t code, | 183 uint16_t code, |
| 198 GamepadEventType* mapped_type, | 184 GamepadEventType* mapped_type, |
| 199 uint16_t* mapped_code) { | 185 uint16_t* mapped_code) { |
| 200 static const KeyMapType key_mapping = { | 186 static const KeyMapType key_mapping = { |
| 201 {BTN_A, WG_BUTTON_A}, {BTN_B, WG_BUTTON_B}, | 187 {BTN_A, WG_BUTTON_A}, {BTN_B, WG_BUTTON_B}, |
| 202 {BTN_X, WG_BUTTON_X}, {BTN_Y, WG_BUTTON_Y}, | 188 {BTN_X, WG_BUTTON_X}, {BTN_Y, WG_BUTTON_Y}, |
| 203 {BTN_TL, WG_BUTTON_L1}, {BTN_TR, WG_BUTTON_R1}, | 189 {BTN_TL, WG_BUTTON_L1}, {BTN_TR, WG_BUTTON_R1}, |
| 204 {BTN_THUMBL, WG_BUTTON_THUMBL}, {BTN_THUMBR, WG_BUTTON_THUMBR}, | 190 {BTN_THUMBL, WG_BUTTON_THUMBL}, {BTN_THUMBR, WG_BUTTON_THUMBR}, |
| 205 {BTN_MODE, WG_BUTTON_START}, {KEY_BACK, WG_BUTTON_SELECT}, | 191 {BTN_MODE, WG_BUTTON_MODE}, {KEY_BACK, WG_BUTTON_SELECT}, |
| 206 {KEY_HOMEPAGE, WG_BUTTON_MODE}}; | 192 {KEY_HOMEPAGE, WG_BUTTON_START}}; |
| 207 | 193 |
| 208 static const AbsMapType abs_mapping = { | 194 static const AbsMapType abs_mapping = { |
| 209 TO_ABS(ABS_X, WG_ABS_X), TO_ABS(ABS_Y, WG_ABS_Y), | 195 TO_ABS(ABS_X, WG_ABS_X), TO_ABS(ABS_Y, WG_ABS_Y), |
| 210 TO_ABS(ABS_Z, WG_ABS_RX), TO_ABS(ABS_RZ, WG_ABS_RY), | 196 TO_ABS(ABS_Z, WG_ABS_RX), TO_ABS(ABS_RZ, WG_ABS_RY), |
| 211 TO_BTN(ABS_BRAKE, WG_BUTTON_LT), TO_BTN(ABS_GAS, WG_BUTTON_RT), | 197 TO_BTN(ABS_BRAKE, WG_BUTTON_LT), TO_BTN(ABS_GAS, WG_BUTTON_RT), |
| 212 TO_BTN(ABS_HAT0X, kHAT_X), TO_BTN(ABS_HAT0Y, kHAT_Y)}; | 198 TO_BTN(ABS_HAT0X, kHAT_X), TO_BTN(ABS_HAT0Y, kHAT_Y)}; |
| 213 return DO_MAPPING; | 199 return DO_MAPPING; |
| 214 } | 200 } |
| 215 | 201 |
| 216 bool Vendor_1d79Product_0009Mapper(uint16_t type, | 202 bool Vendor_1d79Product_0009Mapper(uint16_t type, |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 {BTN_SELECT, WG_BUTTON_SELECT}, | 334 {BTN_SELECT, WG_BUTTON_SELECT}, |
| 349 {BTN_MODE, WG_BUTTON_MODE}}; | 335 {BTN_MODE, WG_BUTTON_MODE}}; |
| 350 | 336 |
| 351 static const AbsMapType abs_mapping = { | 337 static const AbsMapType abs_mapping = { |
| 352 TO_ABS(ABS_X, WG_ABS_X), TO_ABS(ABS_Y, WG_ABS_Y), | 338 TO_ABS(ABS_X, WG_ABS_X), TO_ABS(ABS_Y, WG_ABS_Y), |
| 353 TO_ABS(ABS_RX, WG_ABS_RX), TO_ABS(ABS_RY, WG_ABS_RY), | 339 TO_ABS(ABS_RX, WG_ABS_RX), TO_ABS(ABS_RY, WG_ABS_RY), |
| 354 TO_BTN(ABS_Z, WG_BUTTON_LT), TO_BTN(ABS_RZ, WG_BUTTON_RT)}; | 340 TO_BTN(ABS_Z, WG_BUTTON_LT), TO_BTN(ABS_RZ, WG_BUTTON_RT)}; |
| 355 return DO_MAPPING; | 341 return DO_MAPPING; |
| 356 } | 342 } |
| 357 | 343 |
| 358 bool GenericMapper(uint16_t type, | 344 bool JoydevLikeMapper(uint16_t type, |
| 359 uint16_t code, | 345 uint16_t code, |
| 360 GamepadEventType* mapped_type, | 346 GamepadEventType* mapped_type, |
| 361 uint16_t* mapped_code) { | 347 uint16_t* mapped_code) { |
| 362 static const KeyMapType key_mapping = {{BTN_A, WG_BUTTON_A}, | 348 static const KeyMapType key_mapping = { |
| 363 {BTN_B, WG_BUTTON_B}, | 349 {BTN_A, WG_BUTTON_A}, {BTN_B, WG_BUTTON_B}, |
| 364 {BTN_X, WG_BUTTON_X}, | 350 {BTN_C, WG_BUTTON_X}, {BTN_X, WG_BUTTON_Y}, |
| 365 {BTN_Y, WG_BUTTON_Y}, | 351 {BTN_Y, WG_BUTTON_L1}, {BTN_Z, WG_BUTTON_R1}, |
| 366 {BTN_TL, WG_BUTTON_L1}, | 352 {BTN_TL, WG_BUTTON_LT}, {BTN_TR, WG_BUTTON_RT}, |
| 367 {BTN_TR, WG_BUTTON_R1}, | 353 {BTN_TL2, WG_BUTTON_SELECT}, {BTN_TR2, WG_BUTTON_START}, |
| 368 {BTN_TL2, WG_BUTTON_LT}, | 354 {BTN_SELECT, WG_BUTTON_THUMBL}, {BTN_START, WG_BUTTON_THUMBR}}; |
| 369 {BTN_TR2, WG_BUTTON_RT}, | |
| 370 {BTN_THUMBL, WG_BUTTON_THUMBL}, | |
| 371 {BTN_THUMBR, WG_BUTTON_THUMBR}, | |
| 372 {KEY_BACK, WG_BUTTON_SELECT}, | |
| 373 {KEY_HOMEPAGE, WG_BUTTON_MODE}, | |
| 374 {KEY_UP, WG_BUTTON_DPAD_UP}, | |
| 375 {KEY_DOWN, WG_BUTTON_DPAD_DOWN}, | |
| 376 {KEY_LEFT, WG_BUTTON_DPAD_LEFT}, | |
| 377 {KEY_RIGHT, WG_BUTTON_DPAD_RIGHT}, | |
| 378 {BTN_SELECT, WG_BUTTON_SELECT}, | |
| 379 {BTN_START, WG_BUTTON_START}, | |
| 380 {BTN_MODE, WG_BUTTON_MODE}}; | |
| 381 | 355 |
| 382 static const AbsMapType abs_mapping = { | 356 static const AbsMapType abs_mapping = { |
| 383 TO_ABS(ABS_X, WG_ABS_X), TO_ABS(ABS_Y, WG_ABS_Y), | 357 TO_ABS(ABS_X, WG_ABS_X), TO_ABS(ABS_Y, WG_ABS_Y), |
| 384 TO_ABS(ABS_RX, WG_ABS_RX), TO_ABS(ABS_RY, WG_ABS_RY), | 358 TO_ABS(ABS_Z, WG_ABS_RX), TO_ABS(ABS_RZ, WG_ABS_RY), |
| 385 TO_ABS(ABS_Z, WG_ABS_RX), TO_ABS(ABS_RZ, WG_ABS_RY), | 359 TO_BTN(ABS_HAT0X, kHAT_X), TO_BTN(ABS_HAT0Y, kHAT_Y)}; |
| 386 TO_BTN(ABS_BRAKE, WG_BUTTON_LT), TO_BTN(ABS_GAS, WG_BUTTON_RT), | 360 return DO_MAPPING; |
| 387 TO_BTN(ABS_HAT0X, kHAT_X), TO_BTN(ABS_HAT0Y, kHAT_Y)}; | 361 } |
| 362 |
| 363 bool DualShock4(uint16_t type, |
| 364 uint16_t code, |
| 365 GamepadEventType* mapped_type, |
| 366 uint16_t* mapped_code) { |
| 367 static const KeyMapType key_mapping = { |
| 368 {BTN_A, WG_BUTTON_A}, {BTN_B, WG_BUTTON_B}, |
| 369 {BTN_C, WG_BUTTON_X}, {BTN_X, WG_BUTTON_Y}, |
| 370 {BTN_Y, WG_BUTTON_L1}, {BTN_Z, WG_BUTTON_R1}, |
| 371 {BTN_TL, WG_BUTTON_LT}, {BTN_TR, WG_BUTTON_RT}, |
| 372 {BTN_TL2, WG_BUTTON_SELECT}, {BTN_TR2, WG_BUTTON_START}, |
| 373 {BTN_SELECT, WG_BUTTON_THUMBL}, {BTN_START, WG_BUTTON_THUMBR}}; |
| 374 |
| 375 static const AbsMapType abs_mapping = { |
| 376 TO_ABS(ABS_X, WG_ABS_X), TO_ABS(ABS_Y, WG_ABS_Y), |
| 377 TO_ABS(ABS_RX, WG_BUTTON_LT), TO_ABS(ABS_RY, WG_BUTTON_RT), |
| 378 TO_ABS(ABS_Z, WG_ABS_RX), TO_ABS(ABS_RZ, WG_ABS_RY), |
| 379 TO_BTN(ABS_HAT0X, kHAT_X), TO_BTN(ABS_HAT0Y, kHAT_Y)}; |
| 388 return DO_MAPPING; | 380 return DO_MAPPING; |
| 389 } | 381 } |
| 390 | 382 |
| 391 static const struct MappingData { | 383 static const struct MappingData { |
| 392 uint16_t vendor_id; | 384 uint16_t vendor_id; |
| 393 uint16_t product_id; | 385 uint16_t product_id; |
| 394 GamepadMapper mapper; | 386 GamepadMapperFunction mapper; |
| 395 } AvailableMappings[] = { | 387 } AvailableMappings[] = { |
| 396 // Xbox style gamepad. | 388 // Xbox style gamepad. |
| 397 {0x045e, 0x028e, XInputStyleMapper}, // Xbox 360 wired. | 389 {0x045e, 0x028e, XInputStyleMapper}, // Xbox 360 wired. |
| 398 {0x045e, 0x028f, XInputStyleMapper}, // Xbox 360 wireless. | 390 {0x045e, 0x028f, XInputStyleMapper}, // Xbox 360 wireless. |
| 399 {0x045e, 0x02a1, XInputStyleMapper}, // Xbox 360 wireless. | 391 {0x045e, 0x02a1, XInputStyleMapper}, // Xbox 360 wireless. |
| 400 {0x045e, 0x02d1, XInputStyleMapper}, // Xbox one wired. | 392 {0x045e, 0x02d1, XInputStyleMapper}, // Xbox one wired. |
| 401 {0x045e, 0x02dd, XInputStyleMapper}, // Xbox one wired (2015 fw). | 393 {0x045e, 0x02dd, XInputStyleMapper}, // Xbox one wired (2015 fw). |
| 402 {0x045e, 0x02e3, XInputStyleMapper}, // Xbox elite wired. | 394 {0x045e, 0x02e3, XInputStyleMapper}, // Xbox elite wired. |
| 403 {0x045e, 0x02ea, XInputStyleMapper}, // Xbox one s (usb). | 395 {0x045e, 0x02ea, XInputStyleMapper}, // Xbox one s (usb). |
| 404 {0x045e, 0x0719, XInputStyleMapper}, // Xbox 360 wireless. | 396 {0x045e, 0x0719, XInputStyleMapper}, // Xbox 360 wireless. |
| 405 {0x046d, 0xc21d, XInputStyleMapper}, // Logitech f310. | 397 {0x046d, 0xc21d, XInputStyleMapper}, // Logitech f310. |
| 406 {0x046d, 0xc21e, XInputStyleMapper}, // Logitech f510. | 398 {0x046d, 0xc21e, XInputStyleMapper}, // Logitech f510. |
| 407 {0x046d, 0xc21f, XInputStyleMapper}, // Logitech f710. | 399 {0x046d, 0xc21f, XInputStyleMapper}, // Logitech f710. |
| 408 {0x2378, 0x1008, XInputStyleMapper}, // Onlive controller (bluetooth). | 400 {0x2378, 0x1008, XInputStyleMapper}, // Onlive controller (bluetooth). |
| 409 {0x2378, 0x100a, XInputStyleMapper}, // Onlive controller (wired). | 401 {0x2378, 0x100a, XInputStyleMapper}, // Onlive controller (wired). |
| 410 {0x1bad, 0xf016, XInputStyleMapper}, // Mad catz gamepad. | 402 {0x1bad, 0xf016, XInputStyleMapper}, // Mad catz gamepad. |
| 411 {0x1bad, 0xf023, XInputStyleMapper}, // Mad catz mlg gamepad for Xbox360. | 403 {0x1bad, 0xf023, XInputStyleMapper}, // Mad catz mlg gamepad for Xbox360. |
| 412 {0x1bad, 0xf027, XInputStyleMapper}, // Mad catz fps pro. | 404 {0x1bad, 0xf027, XInputStyleMapper}, // Mad catz fps pro. |
| 413 {0x1bad, 0xf036, XInputStyleMapper}, // Mad catz generic Xbox controller. | 405 {0x1bad, 0xf036, XInputStyleMapper}, // Mad catz generic Xbox controller. |
| 414 {0x1689, 0xfd01, XInputStyleMapper}, // Razer Xbox 360 gamepad. | 406 {0x1689, 0xfd01, XInputStyleMapper}, // Razer Xbox 360 gamepad. |
| 415 {0x1689, 0xfe00, XInputStyleMapper}, // Razer sabertooth elite. | 407 {0x1689, 0xfe00, XInputStyleMapper}, // Razer sabertooth elite. |
| 416 // Sony gamepads. | 408 // Sony gamepads. |
| 417 {0x054c, 0x0268, PlaystationSixAxisMapper}, // Playstation 3. | 409 {0x054c, 0x0268, PlaystationSixAxisMapper}, // Playstation 3. |
| 410 {0x054c, 0x05c4, DualShock4}, // Dualshock 4. |
| 418 // NES style gamepad. | 411 // NES style gamepad. |
| 419 {0x0583, 0x2060, IBuffalocClassicMapper}, // iBuffalo Classic. | 412 {0x0583, 0x2060, IBuffalocClassicMapper}, // iBuffalo Classic. |
| 420 {0x0079, 0x0011, ClassicNESMapper}, // Classic NES controller. | 413 {0x0079, 0x0011, ClassicNESMapper}, // Classic NES controller. |
| 421 {0x12bd, 0xd015, SNesRetroMapper}, // Hitgaming SNES retro. | 414 {0x12bd, 0xd015, SNesRetroMapper}, // Hitgaming SNES retro. |
| 422 // Android gamepad. | 415 // Android gamepad. |
| 423 {0x0b05, 0x4500, ADT1Mapper}, // Nexus player controller (asus gamepad). | 416 {0x0b05, 0x4500, ADT1Mapper}, // Nexus player controller (asus gamepad). |
| 424 {0x1532, 0x0900, ADT1Mapper}, // Razer serval. | 417 {0x1532, 0x0900, ADT1Mapper}, // Razer serval. |
| 425 {0x18d1, 0x2c40, ADT1Mapper}, // ADT-1 controller (odie). | 418 {0x18d1, 0x2c40, ADT1Mapper}, // ADT-1 controller (odie). |
| 426 // Other gamepads. | 419 // Other gamepads. |
| 427 {0x1d79, 0x0009, | 420 {0x1d79, 0x0009, |
| 428 Vendor_1d79Product_0009Mapper}, // Nyko playpad / Playpad pro. | 421 Vendor_1d79Product_0009Mapper}, // Nyko playpad / Playpad pro. |
| 429 {0x046d, 0xb501, Vendor_046dProduct_b501Mapper}, // Logitech redhawk. | 422 {0x046d, 0xb501, Vendor_046dProduct_b501Mapper}, // Logitech redhawk. |
| 430 // Logitech dual action controller. | 423 // Logitech dual action controller. |
| 431 {0x046d, 0xc216, Vendor_046dProduct_c216Mapper}, | 424 {0x046d, 0xc216, Vendor_046dProduct_c216Mapper}, |
| 432 // Logitech cordless rumblepad2. | 425 // Logitech cordless rumblepad2. |
| 433 {0x046d, 0xc219, Vendor_046dProduct_c219Mapper}, | 426 {0x046d, 0xc219, Vendor_046dProduct_c219Mapper}, |
| 434 {0x1038, 0x1412, Vendor_1038Product_1412Mapper}, // Steelseries free. | 427 {0x1038, 0x1412, Vendor_1038Product_1412Mapper}, // Steelseries free. |
| 435 // Razer onza tournment edition. | 428 // Razer onza tournment edition. |
| 436 {0x1689, 0xfd00, Vendor_1689Product_fd00Mapper}}; | 429 {0x1689, 0xfd00, Vendor_1689Product_fd00Mapper}, |
| 430 {0x11c5, 0x5506, JoydevLikeMapper} // HJC Game ZD-V |
| 431 }; |
| 437 | 432 |
| 438 GamepadMapper GetGamepadMapper(uint16_t vendor_id, uint16_t product_id) { | 433 class StaticGamepadMapper : public GamepadMapper { |
| 434 public: |
| 435 StaticGamepadMapper(GamepadMapperFunction fp) : mapper_fp_(fp) {} |
| 436 |
| 437 bool Map(uint16_t type, |
| 438 uint16_t code, |
| 439 GamepadEventType* mapped_type, |
| 440 uint16_t* mapped_code) const override { |
| 441 return mapper_fp_(type, code, mapped_type, mapped_code); |
| 442 }; |
| 443 |
| 444 private: |
| 445 GamepadMapperFunction mapper_fp_; |
| 446 }; |
| 447 |
| 448 GamepadMapper* GetStaticGamepadMapper(uint16_t vendor_id, uint16_t product_id) { |
| 439 for (size_t i = 0; i < arraysize(AvailableMappings); i++) { | 449 for (size_t i = 0; i < arraysize(AvailableMappings); i++) { |
| 440 if (AvailableMappings[i].vendor_id == vendor_id && | 450 if (AvailableMappings[i].vendor_id == vendor_id && |
| 441 AvailableMappings[i].product_id == product_id) { | 451 AvailableMappings[i].product_id == product_id) { |
| 442 return AvailableMappings[i].mapper; | 452 return new StaticGamepadMapper(AvailableMappings[i].mapper); |
| 443 } | 453 } |
| 444 } | 454 } |
| 445 return GenericMapper; | 455 return nullptr; |
| 446 } | 456 } |
| 447 | 457 |
| 448 } // namespace ui | 458 } // namespace ui |
| OLD | NEW |