File: QuartzKeyboardAndMouse.mm

package info (click to toggle)
dolphin-emu 2503%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 111,624 kB
  • sloc: cpp: 787,747; ansic: 217,914; xml: 31,400; python: 4,226; yacc: 3,985; javascript: 2,430; makefile: 777; asm: 726; sh: 281; pascal: 257; perl: 97; objc: 75
file content (325 lines) | stat: -rw-r--r-- 9,321 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2016 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h"

#include <map>
#include <mutex>

#include <Carbon/Carbon.h>
#include <Cocoa/Cocoa.h>

#include "Core/Host.h"

#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/Quartz/Quartz.h"

/// Helper class to get window position data from threads other than the main thread
@interface DolWindowPositionObserver : NSObject

- (instancetype)initWithView:(NSView*)view;
@property(readonly) NSRect frame;

@end

@implementation DolWindowPositionObserver
{
  NSView* _view;
  NSWindow* _window;
  NSRect _frame;
  std::mutex _mtx;
}

- (NSRect)calcFrame
{
  return [_window convertRectToScreen:[_view frame]];
}

- (instancetype)initWithView:(NSView*)view
{
  self = [super init];
  if (self)
  {
    _view = view;
    _window = [view window];
    _frame = [self calcFrame];
    [_window addObserver:self forKeyPath:@"frame" options:0 context:nil];
  }
  return self;
}

- (NSRect)frame
{
  std::lock_guard<std::mutex> guard(_mtx);
  return _frame;
}

- (void)observeValueForKeyPath:(NSString*)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey, id>*)change
                       context:(void*)context
{
  if (object == _window)
  {
    NSRect new_frame = [self calcFrame];
    std::lock_guard<std::mutex> guard(_mtx);
    _frame = new_frame;
  }
}

- (void)dealloc
{
  [_window removeObserver:self forKeyPath:@"frame"];
}

@end

namespace ciface::Quartz
{
std::string KeycodeToName(const CGKeyCode keycode)
{
  static const std::map<CGKeyCode, std::string> named_keys = {
      {kVK_ANSI_A, "A"},
      {kVK_ANSI_B, "B"},
      {kVK_ANSI_C, "C"},
      {kVK_ANSI_D, "D"},
      {kVK_ANSI_E, "E"},
      {kVK_ANSI_F, "F"},
      {kVK_ANSI_G, "G"},
      {kVK_ANSI_H, "H"},
      {kVK_ANSI_I, "I"},
      {kVK_ANSI_J, "J"},
      {kVK_ANSI_K, "K"},
      {kVK_ANSI_L, "L"},
      {kVK_ANSI_M, "M"},
      {kVK_ANSI_N, "N"},
      {kVK_ANSI_O, "O"},
      {kVK_ANSI_P, "P"},
      {kVK_ANSI_Q, "Q"},
      {kVK_ANSI_R, "R"},
      {kVK_ANSI_S, "S"},
      {kVK_ANSI_T, "T"},
      {kVK_ANSI_U, "U"},
      {kVK_ANSI_V, "V"},
      {kVK_ANSI_W, "W"},
      {kVK_ANSI_X, "X"},
      {kVK_ANSI_Y, "Y"},
      {kVK_ANSI_Z, "Z"},
      {kVK_ANSI_1, "1"},
      {kVK_ANSI_2, "2"},
      {kVK_ANSI_3, "3"},
      {kVK_ANSI_4, "4"},
      {kVK_ANSI_5, "5"},
      {kVK_ANSI_6, "6"},
      {kVK_ANSI_7, "7"},
      {kVK_ANSI_8, "8"},
      {kVK_ANSI_9, "9"},
      {kVK_ANSI_0, "0"},
      {kVK_Return, "Return"},
      {kVK_Escape, "Escape"},
      {kVK_Delete, "Backspace"},
      {kVK_Tab, "Tab"},
      {kVK_Space, "Space"},
      {kVK_ANSI_Minus, "-"},
      {kVK_ANSI_Equal, "="},
      {kVK_ANSI_LeftBracket, "["},
      {kVK_ANSI_RightBracket, "]"},
      {kVK_ANSI_Backslash, "\\"},
      {kVK_ANSI_Semicolon, ";"},
      {kVK_ANSI_Quote, "'"},
      {kVK_ANSI_Grave, "Tilde"},
      {kVK_ANSI_Comma, ","},
      {kVK_ANSI_Period, "."},
      {kVK_ANSI_Slash, "/"},
      {kVK_CapsLock, "Caps Lock"},
      {kVK_F1, "F1"},
      {kVK_F2, "F2"},
      {kVK_F3, "F3"},
      {kVK_F4, "F4"},
      {kVK_F5, "F5"},
      {kVK_F6, "F6"},
      {kVK_F7, "F7"},
      {kVK_F8, "F8"},
      {kVK_F9, "F9"},
      {kVK_F10, "F10"},
      {kVK_F11, "F11"},
      {kVK_F12, "F12"},
      {kVK_Home, "Home"},
      {kVK_PageUp, "Page Up"},
      {kVK_ForwardDelete, "Delete"},
      {kVK_End, "End"},
      {kVK_PageDown, "Page Down"},
      {kVK_RightArrow, "Right Arrow"},
      {kVK_LeftArrow, "Left Arrow"},
      {kVK_DownArrow, "Down Arrow"},
      {kVK_UpArrow, "Up Arrow"},
      {kVK_ANSI_KeypadDivide, "Keypad /"},
      {kVK_ANSI_KeypadMultiply, "Keypad *"},
      {kVK_ANSI_KeypadMinus, "Keypad -"},
      {kVK_ANSI_KeypadPlus, "Keypad +"},
      {kVK_ANSI_KeypadEnter, "Keypad Enter"},
      {kVK_ANSI_Keypad1, "Keypad 1"},
      {kVK_ANSI_Keypad2, "Keypad 2"},
      {kVK_ANSI_Keypad3, "Keypad 3"},
      {kVK_ANSI_Keypad4, "Keypad 4"},
      {kVK_ANSI_Keypad5, "Keypad 5"},
      {kVK_ANSI_Keypad6, "Keypad 6"},
      {kVK_ANSI_Keypad7, "Keypad 7"},
      {kVK_ANSI_Keypad8, "Keypad 8"},
      {kVK_ANSI_Keypad9, "Keypad 9"},
      {kVK_ANSI_Keypad0, "Keypad 0"},
      {kVK_ANSI_KeypadDecimal, "Keypad ."},
      {kVK_ANSI_KeypadEquals, "Keypad ="},
      {kVK_Control, "Left Control"},
      {kVK_Shift, "Left Shift"},
      {kVK_Option, "Left Alt"},
      {kVK_Command, "Command"},
      {kVK_RightControl, "Right Control"},
      {kVK_RightShift, "Right Shift"},
      {kVK_RightOption, "Right Alt"},
  };

  if (named_keys.contains(keycode))
    return named_keys.at(keycode);
  else
    return "Key " + std::to_string(keycode);
}

KeyboardAndMouse::Key::Key(CGKeyCode keycode) : m_keycode(keycode), m_name(KeycodeToName(keycode))
{
}

ControlState KeyboardAndMouse::Key::GetState() const
{
  return CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, m_keycode) != 0;
}

std::string KeyboardAndMouse::Key::GetName() const
{
  return m_name;
}

KeyboardAndMouse::KeyboardAndMouse(void* view)
{
  // All keycodes in <HIToolbox/Events.h> are 0x7e or lower. If you notice
  // keys that aren't being recognized, bump this number up!
  for (int keycode = 0; keycode < 0x80; ++keycode)
    AddInput(new Key(keycode));

  // Add combined left/right modifiers with consistent naming across platforms.
  AddCombinedInput("Alt", {"Left Alt", "Right Alt"});
  AddCombinedInput("Shift", {"Left Shift", "Right Shift"});
  AddCombinedInput("Ctrl", {"Left Control", "Right Control"});

  // PopulateDevices may be called on the Emuthread, so we need to ensure that
  // these UI APIs are only ever called on the main thread.
  if ([NSThread isMainThread])
    MainThreadInitialization(view);
  else
    dispatch_sync(dispatch_get_main_queue(), [this, view] { MainThreadInitialization(view); });

  // cursor, with a hax for-loop
  for (unsigned int i = 0; i < 4; ++i)
    AddInput(new Cursor(!!(i & 2), (&m_cursor.x)[i / 2], !!(i & 1)));

  AddInput(new Button(kCGMouseButtonLeft));
  AddInput(new Button(kCGMouseButtonRight));
  AddInput(new Button(kCGMouseButtonCenter));
}

// Very important that this is here
// C++ and ObjC++ have different views of the header, and only ObjC++'s will deallocate properly
KeyboardAndMouse::~KeyboardAndMouse() = default;

void KeyboardAndMouse::MainThreadInitialization(void* view)
{
  NSView* cocoa_view = (__bridge NSView*)view;
  m_window_pos_observer = [[DolWindowPositionObserver alloc] initWithView:cocoa_view];
}

Core::DeviceRemoval KeyboardAndMouse::UpdateInput()
{
  NSRect bounds = [m_window_pos_observer frame];

  const double window_width = std::max(bounds.size.width, 1.0);
  const double window_height = std::max(bounds.size.height, 1.0);

  if (g_controller_interface.IsMouseCenteringRequested() &&
      (Host_RendererHasFocus() || Host_TASInputHasFocus()))
  {
    m_cursor.x = 0;
    m_cursor.y = 0;

    const CGPoint window_center_global_coordinates =
        CGPointMake(bounds.origin.x + window_width / 2.0, bounds.origin.y + window_height / 2.0);
    CGWarpMouseCursorPosition(window_center_global_coordinates);
    // Without this line there is a short but obvious delay after centering the cursor before it can
    // be moved again
    CGAssociateMouseAndMouseCursorPosition(true);

    g_controller_interface.SetMouseCenteringRequested(false);
  }
  else if (!Host_TASInputHasFocus())
  {
    // When a TAS Input window has focus and "Enable Controller Input" is checked most types of
    // input should be read normally as if the render window had focus instead. The cursor is an
    // exception, as otherwise using the mouse to set any control in the TAS Input window will also
    // update the Wii IR value (or any other input controlled by the cursor).

    NSPoint loc = [NSEvent mouseLocation];

    const auto window_scale = g_controller_interface.GetWindowInputScale();

    loc.x -= bounds.origin.x;
    loc.y -= bounds.origin.y;
    m_cursor.x = (loc.x / window_width * 2 - 1.0) * window_scale.x;
    m_cursor.y = (loc.y / window_height * 2 - 1.0) * -window_scale.y;
  }

  return Core::DeviceRemoval::Keep;
}

std::string KeyboardAndMouse::GetName() const
{
  return "Keyboard & Mouse";
}

std::string KeyboardAndMouse::GetSource() const
{
  return Quartz::GetSourceName();
}

int KeyboardAndMouse::GetSortPriority() const
{
  return DEFAULT_DEVICE_SORT_PRIORITY;
}

ControlState KeyboardAndMouse::Cursor::GetState() const
{
  return std::max(0.0, ControlState(m_axis) / (m_positive ? 1.0 : -1.0));
}

ControlState KeyboardAndMouse::Button::GetState() const
{
  return CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, m_button) != 0;
}

std::string KeyboardAndMouse::Cursor::GetName() const
{
  char tmpstr[] = "Cursor ..";
  tmpstr[7] = (char)('X' + m_index);
  tmpstr[8] = (m_positive ? '+' : '-');
  return tmpstr;
}

std::string KeyboardAndMouse::Button::GetName() const
{
  if (m_button == kCGMouseButtonLeft)
    return "Left Click";
  if (m_button == kCGMouseButtonCenter)
    return "Middle Click";
  if (m_button == kCGMouseButtonRight)
    return "Right Click";
  return std::string("Click ") + char('0' + m_button);
}
}  // namespace ciface::Quartz