key_map.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <tuple>
  7. #include "core/hle/service/hid/hid.h"
  8. class EmuWindow;
  9. namespace KeyMap {
  10. /**
  11. * Represents key mapping targets that are not real 3DS buttons.
  12. * They will be handled by KeyMap and translated to 3DS input.
  13. */
  14. enum class IndirectTarget {
  15. CirclePadUp,
  16. CirclePadDown,
  17. CirclePadLeft,
  18. CirclePadRight,
  19. CirclePadModifier,
  20. };
  21. /**
  22. * Represents a key mapping target. It can be a PadState that represents real 3DS buttons,
  23. * or an IndirectTarget.
  24. */
  25. struct KeyTarget {
  26. bool direct;
  27. union {
  28. u32 direct_target_hex;
  29. IndirectTarget indirect_target;
  30. } target;
  31. KeyTarget() : direct(true) {
  32. target.direct_target_hex = 0;
  33. }
  34. KeyTarget(Service::HID::PadState pad) : direct(true) {
  35. target.direct_target_hex = pad.hex;
  36. }
  37. KeyTarget(IndirectTarget i) : direct(false) {
  38. target.indirect_target = i;
  39. }
  40. };
  41. /**
  42. * Represents a key for a specific host device.
  43. */
  44. struct HostDeviceKey {
  45. int key_code;
  46. int device_id; ///< Uniquely identifies a host device
  47. bool operator<(const HostDeviceKey& other) const {
  48. return std::tie(key_code, device_id) < std::tie(other.key_code, other.device_id);
  49. }
  50. bool operator==(const HostDeviceKey& other) const {
  51. return std::tie(key_code, device_id) == std::tie(other.key_code, other.device_id);
  52. }
  53. };
  54. extern const std::array<KeyTarget, Settings::NativeInput::NUM_INPUTS> mapping_targets;
  55. /**
  56. * Generates a new device id, which uniquely identifies a host device within KeyMap.
  57. */
  58. int NewDeviceId();
  59. /**
  60. * Maps a device-specific key to a target (a PadState or an IndirectTarget).
  61. */
  62. void SetKeyMapping(HostDeviceKey key, KeyTarget target);
  63. /**
  64. * Clears all key mappings belonging to one device.
  65. */
  66. void ClearKeyMapping(int device_id);
  67. /**
  68. * Maps a key press action and call the corresponding function in EmuWindow
  69. */
  70. void PressKey(EmuWindow& emu_window, HostDeviceKey key);
  71. /**
  72. * Maps a key release action and call the corresponding function in EmuWindow
  73. */
  74. void ReleaseKey(EmuWindow& emu_window, HostDeviceKey key);
  75. }