Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm

raw
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h"
5
6 #include <map>
7 #include <mutex>
8
9 #include <Carbon/Carbon.h>
10 #include <Cocoa/Cocoa.h>
11
12 #include "Core/Host.h"
13
14 #include "InputCommon/ControllerInterface/ControllerInterface.h"
15 #include "InputCommon/ControllerInterface/Quartz/Quartz.h"
16
17 /// Helper class to get window position data from threads other than the main thread
18 @interface DolWindowPositionObserver : NSObject
19
20 - (instancetype)initWithView:(NSView*)view;
21 @property(readonly) NSRect frame;
22
23 @end
24
25 @implementation DolWindowPositionObserver
26 {
27 NSView* _view;
28 NSWindow* _window;
29 NSRect _frame;
30 std::mutex _mtx;
31 }
32
33 - (NSRect)calcFrame
34 {
35 return [_window convertRectToScreen:[_view frame]];
36 }
37
38 - (instancetype)initWithView:(NSView*)view
39 {
40 self = [super init];
41 if (self)
42 {
43 _view = view;
44 _window = [view window];
45 _frame = [self calcFrame];
46 [_window addObserver:self forKeyPath:@"frame" options:0 context:nil];
47 }
48 return self;
49 }
50
51 - (NSRect)frame
52 {
53 std::lock_guard<std::mutex> guard(_mtx);
54 return _frame;
55 }
56
57 - (void)observeValueForKeyPath:(NSString*)keyPath
58 ofObject:(id)object
59 change:(NSDictionary<NSKeyValueChangeKey, id>*)change
60 context:(void*)context
61 {
62 if (object == _window)
63 {
64 NSRect new_frame = [self calcFrame];
65 std::lock_guard<std::mutex> guard(_mtx);
66 _frame = new_frame;
67 }
68 }
69
70 - (void)dealloc
71 {
72 [_window removeObserver:self forKeyPath:@"frame"];
73 }
74
75 @end
76
77 namespace ciface::Quartz
78 {
79 std::string KeycodeToName(const CGKeyCode keycode)
80 {
81 static const std::map<CGKeyCode, std::string> named_keys = {
82 {kVK_ANSI_A, "A"},
83 {kVK_ANSI_B, "B"},
84 {kVK_ANSI_C, "C"},
85 {kVK_ANSI_D, "D"},
86 {kVK_ANSI_E, "E"},
87 {kVK_ANSI_F, "F"},
88 {kVK_ANSI_G, "G"},
89 {kVK_ANSI_H, "H"},
90 {kVK_ANSI_I, "I"},
91 {kVK_ANSI_J, "J"},
92 {kVK_ANSI_K, "K"},
93 {kVK_ANSI_L, "L"},
94 {kVK_ANSI_M, "M"},
95 {kVK_ANSI_N, "N"},
96 {kVK_ANSI_O, "O"},
97 {kVK_ANSI_P, "P"},
98 {kVK_ANSI_Q, "Q"},
99 {kVK_ANSI_R, "R"},
100 {kVK_ANSI_S, "S"},
101 {kVK_ANSI_T, "T"},
102 {kVK_ANSI_U, "U"},
103 {kVK_ANSI_V, "V"},
104 {kVK_ANSI_W, "W"},
105 {kVK_ANSI_X, "X"},
106 {kVK_ANSI_Y, "Y"},
107 {kVK_ANSI_Z, "Z"},
108 {kVK_ANSI_1, "1"},
109 {kVK_ANSI_2, "2"},
110 {kVK_ANSI_3, "3"},
111 {kVK_ANSI_4, "4"},
112 {kVK_ANSI_5, "5"},
113 {kVK_ANSI_6, "6"},
114 {kVK_ANSI_7, "7"},
115 {kVK_ANSI_8, "8"},
116 {kVK_ANSI_9, "9"},
117 {kVK_ANSI_0, "0"},
118 {kVK_Return, "Return"},
119 {kVK_Escape, "Escape"},
120 {kVK_Delete, "Backspace"},
121 {kVK_Tab, "Tab"},
122 {kVK_Space, "Space"},
123 {kVK_ANSI_Minus, "-"},
124 {kVK_ANSI_Equal, "="},
125 {kVK_ANSI_LeftBracket, "["},
126 {kVK_ANSI_RightBracket, "]"},
127 {kVK_ANSI_Backslash, "\\"},
128 {kVK_ANSI_Semicolon, ";"},
129 {kVK_ANSI_Quote, "'"},
130 {kVK_ANSI_Grave, "Tilde"},
131 {kVK_ANSI_Comma, ","},
132 {kVK_ANSI_Period, "."},
133 {kVK_ANSI_Slash, "/"},
134 {kVK_CapsLock, "Caps Lock"},
135 {kVK_F1, "F1"},
136 {kVK_F2, "F2"},
137 {kVK_F3, "F3"},
138 {kVK_F4, "F4"},
139 {kVK_F5, "F5"},
140 {kVK_F6, "F6"},
141 {kVK_F7, "F7"},
142 {kVK_F8, "F8"},
143 {kVK_F9, "F9"},
144 {kVK_F10, "F10"},
145 {kVK_F11, "F11"},
146 {kVK_F12, "F12"},
147 {kVK_Home, "Home"},
148 {kVK_PageUp, "Page Up"},
149 {kVK_ForwardDelete, "Delete"},
150 {kVK_End, "End"},
151 {kVK_PageDown, "Page Down"},
152 {kVK_RightArrow, "Right Arrow"},
153 {kVK_LeftArrow, "Left Arrow"},
154 {kVK_DownArrow, "Down Arrow"},
155 {kVK_UpArrow, "Up Arrow"},
156 {kVK_ANSI_KeypadDivide, "Keypad /"},
157 {kVK_ANSI_KeypadMultiply, "Keypad *"},
158 {kVK_ANSI_KeypadMinus, "Keypad -"},
159 {kVK_ANSI_KeypadPlus, "Keypad +"},
160 {kVK_ANSI_KeypadEnter, "Keypad Enter"},
161 {kVK_ANSI_Keypad1, "Keypad 1"},
162 {kVK_ANSI_Keypad2, "Keypad 2"},
163 {kVK_ANSI_Keypad3, "Keypad 3"},
164 {kVK_ANSI_Keypad4, "Keypad 4"},
165 {kVK_ANSI_Keypad5, "Keypad 5"},
166 {kVK_ANSI_Keypad6, "Keypad 6"},
167 {kVK_ANSI_Keypad7, "Keypad 7"},
168 {kVK_ANSI_Keypad8, "Keypad 8"},
169 {kVK_ANSI_Keypad9, "Keypad 9"},
170 {kVK_ANSI_Keypad0, "Keypad 0"},
171 {kVK_ANSI_KeypadDecimal, "Keypad ."},
172 {kVK_ANSI_KeypadEquals, "Keypad ="},
173 {kVK_Control, "Left Control"},
174 {kVK_Shift, "Left Shift"},
175 {kVK_Option, "Left Alt"},
176 {kVK_Command, "Command"},
177 {kVK_RightControl, "Right Control"},
178 {kVK_RightShift, "Right Shift"},
179 {kVK_RightOption, "Right Alt"},
180 };
181
182 if (named_keys.contains(keycode))
183 return named_keys.at(keycode);
184 else
185 return "Key " + std::to_string(keycode);
186 }
187
188 KeyboardAndMouse::Key::Key(CGKeyCode keycode) : m_keycode(keycode), m_name(KeycodeToName(keycode))
189 {
190 }
191
192 ControlState KeyboardAndMouse::Key::GetState() const
193 {
194 return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, m_keycode) != 0;
195 }
196
197 std::string KeyboardAndMouse::Key::GetName() const
198 {
199 return m_name;
200 }
201
202 KeyboardAndMouse::KeyboardAndMouse(void* view)
203 {
204 // All keycodes in <HIToolbox/Events.h> are 0x7e or lower. If you notice
205 // keys that aren't being recognized, bump this number up!
206 for (int keycode = 0; keycode < 0x80; ++keycode)
207 AddInput(new Key(keycode));
208
209 // Add combined left/right modifiers with consistent naming across platforms.
210 AddCombinedInput("Alt", {"Left Alt", "Right Alt"});
211 AddCombinedInput("Shift", {"Left Shift", "Right Shift"});
212 AddCombinedInput("Ctrl", {"Left Control", "Right Control"});
213
214 // PopulateDevices may be called on the Emuthread, so we need to ensure that
215 // these UI APIs are only ever called on the main thread.
216 if ([NSThread isMainThread])
217 MainThreadInitialization(view);
218 else
219 dispatch_sync(dispatch_get_main_queue(), [this, view] { MainThreadInitialization(view); });
220
221 // cursor, with a hax for-loop
222 for (unsigned int i = 0; i < 4; ++i)
223 AddInput(new Cursor(!!(i & 2), (&m_cursor.x)[i / 2], !!(i & 1)));
224
225 AddInput(new Button(kCGMouseButtonLeft));
226 AddInput(new Button(kCGMouseButtonRight));
227 AddInput(new Button(kCGMouseButtonCenter));
228 }
229
230 // Very important that this is here
231 // C++ and ObjC++ have different views of the header, and only ObjC++'s will deallocate properly
232 KeyboardAndMouse::~KeyboardAndMouse() = default;
233
234 void KeyboardAndMouse::MainThreadInitialization(void* view)
235 {
236 NSView* cocoa_view = (__bridge NSView*)view;
237 m_window_pos_observer = [[DolWindowPositionObserver alloc] initWithView:cocoa_view];
238 }
239
240 Core::DeviceRemoval KeyboardAndMouse::UpdateInput()
241 {
242 NSRect bounds = [m_window_pos_observer frame];
243
244 const double window_width = std::max(bounds.size.width, 1.0);
245 const double window_height = std::max(bounds.size.height, 1.0);
246
247 if (g_controller_interface.IsMouseCenteringRequested() &&
248 (Host_RendererHasFocus() || Host_TASInputHasFocus()))
249 {
250 m_cursor.x = 0;
251 m_cursor.y = 0;
252
253 const CGPoint window_center_global_coordinates =
254 CGPointMake(bounds.origin.x + window_width / 2.0, bounds.origin.y + window_height / 2.0);
255 CGWarpMouseCursorPosition(window_center_global_coordinates);
256 // Without this line there is a short but obvious delay after centering the cursor before it can
257 // be moved again
258 CGAssociateMouseAndMouseCursorPosition(true);
259
260 g_controller_interface.SetMouseCenteringRequested(false);
261 }
262 else if (!Host_TASInputHasFocus())
263 {
264 // When a TAS Input window has focus and "Enable Controller Input" is checked most types of
265 // input should be read normally as if the render window had focus instead. The cursor is an
266 // exception, as otherwise using the mouse to set any control in the TAS Input window will also
267 // update the Wii IR value (or any other input controlled by the cursor).
268
269 NSPoint loc = [NSEvent mouseLocation];
270
271 const auto window_scale = g_controller_interface.GetWindowInputScale();
272
273 loc.x -= bounds.origin.x;
274 loc.y -= bounds.origin.y;
275 m_cursor.x = (loc.x / window_width * 2 - 1.0) * window_scale.x;
276 m_cursor.y = (loc.y / window_height * 2 - 1.0) * -window_scale.y;
277 }
278
279 return Core::DeviceRemoval::Keep;
280 }
281
282 std::string KeyboardAndMouse::GetName() const
283 {
284 return "Keyboard & Mouse";
285 }
286
287 std::string KeyboardAndMouse::GetSource() const
288 {
289 return Quartz::GetSourceName();
290 }
291
292 int KeyboardAndMouse::GetSortPriority() const
293 {
294 return DEFAULT_DEVICE_SORT_PRIORITY;
295 }
296
297 ControlState KeyboardAndMouse::Cursor::GetState() const
298 {
299 return std::max(0.0, ControlState(m_axis) / (m_positive ? 1.0 : -1.0));
300 }
301
302 ControlState KeyboardAndMouse::Button::GetState() const
303 {
304 return CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, m_button) != 0;
305 }
306
307 std::string KeyboardAndMouse::Cursor::GetName() const
308 {
309 char tmpstr[] = "Cursor ..";
310 tmpstr[7] = (char)('X' + m_index);
311 tmpstr[8] = (m_positive ? '+' : '-');
312 return tmpstr;
313 }
314
315 std::string KeyboardAndMouse::Button::GetName() const
316 {
317 if (m_button == kCGMouseButtonLeft)
318 return "Left Click";
319 if (m_button == kCGMouseButtonCenter)
320 return "Middle Click";
321 if (m_button == kCGMouseButtonRight)
322 return "Right Click";
323 return std::string("Click ") + char('0' + m_button);
324 }
325 } // namespace ciface::Quartz