1 #pragma once
2 
3 // For more detailed and configurable input, implement NativeTouch, NativeKey and NativeAxis and do your
4 // own mapping. Might later move the mapping system from PPSSPP to native.
5 
6 #include <map>
7 #include <mutex>
8 #include <unordered_map>
9 #include <vector>
10 
11 #include "Common/Math/lin/vec3.h"
12 #include "Common/Input/KeyCodes.h"
13 
14 // Default device IDs
15 
16 enum {
17 	DEVICE_ID_ANY = -1,  // Represents any device ID
18 	DEVICE_ID_DEFAULT = 0,  // Old Android
19 	DEVICE_ID_KEYBOARD = 1,  // PC keyboard, android keyboards
20 	DEVICE_ID_MOUSE = 2,  // PC mouse only (not touchscreen!)
21 	DEVICE_ID_PAD_0 = 10,  // Generic joypads
22 	DEVICE_ID_PAD_1 = 11,  // these should stay as contiguous numbers
23 	DEVICE_ID_PAD_2 = 12,
24 	DEVICE_ID_PAD_3 = 13,
25 	DEVICE_ID_PAD_4 = 14,
26 	DEVICE_ID_PAD_5 = 15,
27 	DEVICE_ID_PAD_6 = 16,
28 	DEVICE_ID_PAD_7 = 17,
29 	DEVICE_ID_PAD_8 = 18,
30 	DEVICE_ID_PAD_9 = 19,
31 	DEVICE_ID_XINPUT_0 = 20,  // XInput joypads
32 	DEVICE_ID_XINPUT_1 = 21,
33 	DEVICE_ID_XINPUT_2 = 22,
34 	DEVICE_ID_XINPUT_3 = 23,
35 	DEVICE_ID_ACCELEROMETER = 30,
36 };
37 
38 //number of contiguous generic joypad IDs
39 const int MAX_NUM_PADS = 10;
40 
41 const char *GetDeviceName(int deviceId);
42 
43 enum {
44 	PAD_BUTTON_A = 1,
45 	PAD_BUTTON_B = 2,
46 	PAD_BUTTON_X = 4,
47 	PAD_BUTTON_Y = 8,
48 	PAD_BUTTON_LBUMPER = 16,
49 	PAD_BUTTON_RBUMPER = 32,
50 	PAD_BUTTON_START = 64,
51 	PAD_BUTTON_SELECT = 128,
52 	PAD_BUTTON_UP = 256,
53 	PAD_BUTTON_DOWN = 512,
54 	PAD_BUTTON_LEFT = 1024,
55 	PAD_BUTTON_RIGHT = 2048,
56 
57 	PAD_BUTTON_MENU = 4096,
58 	PAD_BUTTON_BACK = 8192,
59 
60 	// For Qt
61 	PAD_BUTTON_JOY_UP = 1<<14,
62 	PAD_BUTTON_JOY_DOWN = 1<<15,
63 	PAD_BUTTON_JOY_LEFT = 1<<16,
64 	PAD_BUTTON_JOY_RIGHT = 1<<17,
65 
66 	PAD_BUTTON_LEFT_THUMB = 1 << 18,   // Click left thumb stick on X360
67 	PAD_BUTTON_RIGHT_THUMB = 1 << 19,   // Click right thumb stick on X360
68 
69 	PAD_BUTTON_LEFT_TRIGGER = 1 << 21,   // Click left thumb stick on X360
70 	PAD_BUTTON_RIGHT_TRIGGER = 1 << 22,   // Click left thumb stick on X360
71 
72 	PAD_BUTTON_FASTFORWARD = 1 << 20, // Click Tab to unthrottle
73 };
74 
75 #ifndef MAX_KEYQUEUESIZE
76 #define MAX_KEYQUEUESIZE 20
77 #endif
78 
79 // Represents a single bindable key
80 class KeyDef {
81 public:
KeyDef()82 	KeyDef() : deviceId(0), keyCode(0) {}
KeyDef(int devId,int k)83 	KeyDef(int devId, int k) : deviceId(devId), keyCode(k) {}
84 	int deviceId;
85 	int keyCode;
86 
87 	// If you want to use std::find and match ANY, you need to perform an explicit search for that.
88 	bool operator < (const KeyDef &other) const {
89 		if (deviceId < other.deviceId) return true;
90 		if (deviceId > other.deviceId) return false;
91 		if (keyCode < other.keyCode) return true;
92 		return false;
93 	}
94 	bool operator == (const KeyDef &other) const {
95 		if (deviceId != other.deviceId && deviceId != DEVICE_ID_ANY && other.deviceId != DEVICE_ID_ANY) return false;
96 		if (keyCode != other.keyCode) return false;
97 		return true;
98 	}
99 };
100 
101 enum {
102 	TOUCH_MOVE = 1 << 0,
103 	TOUCH_DOWN = 1 << 1,
104 	TOUCH_UP = 1 << 2,
105 	TOUCH_CANCEL = 1 << 3,  // Sent by scrollviews to their children when they detect a scroll
106 	TOUCH_WHEEL = 1 << 4,  // Scrollwheel event. Usually only affects Y but can potentially affect X.
107 	TOUCH_MOUSE = 1 << 5,  // Identifies that this touch event came from a mouse
108 	TOUCH_RELEASE_ALL = 1 << 6,  // Useful for app focus switches when events may be lost.
109 
110 	// These are the Android getToolType() codes, shifted by 10.
111 	TOUCH_TOOL_MASK = 7 << 10,
112 	TOUCH_TOOL_UNKNOWN = 0 << 10,
113 	TOUCH_TOOL_FINGER = 1 << 10,
114 	TOUCH_TOOL_STYLUS = 2 << 10,
115 	TOUCH_TOOL_MOUSE = 3 << 10,
116 	TOUCH_TOOL_ERASER = 4 << 10,
117 };
118 
119 // Used for asynchronous touch input.
120 // DOWN is always on its own.
121 // MOVE and UP can be combined.
122 struct TouchInput {
123 	float x;
124 	float y;
125 	int id; // Needs to be <= GestureDetector::MAX_PTRS (10.)
126 	int flags;
127 	double timestamp;
128 };
129 
130 #undef KEY_DOWN
131 #undef KEY_UP
132 
133 enum {
134 	KEY_DOWN = 1 << 0,
135 	KEY_UP = 1 << 1,
136 	KEY_HASWHEELDELTA = 1 << 2,
137 	KEY_IS_REPEAT = 1 << 3,
138 	KEY_CHAR = 1 << 4,  // Unicode character input. Cannot detect keyups of these so KEY_DOWN and KEY_UP are zero when this is set.
139 };
140 
141 struct KeyInput {
KeyInputKeyInput142 	KeyInput() {}
KeyInputKeyInput143 	KeyInput(int devId, int code, int fl) : deviceId(devId), keyCode(code), flags(fl) {}
144 	int deviceId;
145 	int keyCode;  // Android keycodes are the canonical keycodes, everyone else map to them.
146 	int flags;
147 };
148 
149 struct AxisInput {
150 	int deviceId;
151 	int axisId;  // Android axis Ids are the canonical ones.
152 	float value;
153 	int flags;
154 };
155 
156 // Is there a nicer place for this stuff? It's here to avoid dozens of linking errors in UnitTest..
157 extern std::vector<KeyDef> dpadKeys;
158 extern std::vector<KeyDef> confirmKeys;
159 extern std::vector<KeyDef> cancelKeys;
160 extern std::vector<KeyDef> tabLeftKeys;
161 extern std::vector<KeyDef> tabRightKeys;
162 void SetDPadKeys(const std::vector<KeyDef> &leftKey, const std::vector<KeyDef> &rightKey,
163 		const std::vector<KeyDef> &upKey, const std::vector<KeyDef> &downKey);
164 void SetConfirmCancelKeys(const std::vector<KeyDef> &confirm, const std::vector<KeyDef> &cancel);
165 void SetTabLeftRightKeys(const std::vector<KeyDef> &tabLeft, const std::vector<KeyDef> &tabRight);
166 
167 // 0 means unknown (attempt autodetect), -1 means flip, 1 means original direction.
168 void SetAnalogFlipY(std::unordered_map<int, int> flipYByDeviceId);
169 int GetAnalogYDirection(int deviceId);
170