| 1 |
/* |
| 2 |
* Copyright (C) 2012-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "WinEventsOSXImpl.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "application/AppInboundProtocol.h" |
| 13 |
#include "input/actions/Action.h" |
| 14 |
#include "input/actions/ActionIDs.h" |
| 15 |
#include "input/mouse/MouseStat.h" |
| 16 |
#include "messaging/ApplicationMessenger.h" |
| 17 |
#include "utils/log.h" |
| 18 |
|
| 19 |
#include <mutex> |
| 20 |
#include <optional> |
| 21 |
#include <queue> |
| 22 |
|
| 23 |
#import <AppKit/AppKit.h> |
| 24 |
#import <Foundation/Foundation.h> |
| 25 |
|
| 26 |
#pragma mark - objc implementation |
| 27 |
|
| 28 |
@implementation CWinEventsOSXImpl |
| 29 |
{ |
| 30 |
std::queue<XBMC_Event> events; |
| 31 |
CCriticalSection m_inputlock; |
| 32 |
bool m_inputEnabled; |
| 33 |
|
| 34 |
//! macOS requires the calls the NSCursor hide/unhide to be balanced |
| 35 |
enum class NSCursorVisibilityBalancer |
| 36 |
{ |
| 37 |
NONE, |
| 38 |
HIDE, |
| 39 |
UNHIDE |
| 40 |
}; |
| 41 |
NSCursorVisibilityBalancer m_lastAppCursorVisibilityAction; |
| 42 |
} |
| 43 |
|
| 44 |
#pragma mark - init |
| 45 |
|
| 46 |
- (instancetype)init |
| 47 |
{ |
| 48 |
self = [super init]; |
| 49 |
|
| 50 |
m_lastAppCursorVisibilityAction = NSCursorVisibilityBalancer::NONE; |
| 51 |
[self enableInputEvents]; |
| 52 |
return self; |
| 53 |
} |
| 54 |
|
| 55 |
- (void)MessagePush:(XBMC_Event*)newEvent |
| 56 |
{ |
| 57 |
std::unique_lock<CCriticalSection> lock(m_inputlock); |
| 58 |
events.emplace(*newEvent); |
| 59 |
} |
| 60 |
|
| 61 |
- (bool)MessagePump |
| 62 |
{ |
| 63 |
|
| 64 |
bool ret = false; |
| 65 |
|
| 66 |
// Do not always loop, only pump the initial queued count events. else if ui keep pushing |
| 67 |
// events the loop won't finish then it will block xbmc main message loop. |
| 68 |
for (size_t pumpEventCount = [self GetQueueSize]; pumpEventCount > 0; --pumpEventCount) |
| 69 |
{ |
| 70 |
// Pop up only one event per time since in App::OnEvent it may init modal dialog which init |
| 71 |
// deeper message loop and call the deeper MessagePump from there. |
| 72 |
XBMC_Event pumpEvent = {}; |
| 73 |
{ |
| 74 |
std::unique_lock<CCriticalSection> lock(m_inputlock); |
| 75 |
if (events.size() == 0) |
| 76 |
return ret; |
| 77 |
pumpEvent = events.front(); |
| 78 |
events.pop(); |
| 79 |
} |
| 80 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 81 |
if (appPort) |
| 82 |
ret |= appPort->OnEvent(pumpEvent); |
| 83 |
} |
| 84 |
return ret; |
| 85 |
} |
| 86 |
|
| 87 |
- (size_t)GetQueueSize |
| 88 |
{ |
| 89 |
std::unique_lock<CCriticalSection> lock(m_inputlock); |
| 90 |
return events.size(); |
| 91 |
} |
| 92 |
|
| 93 |
- (unichar)OsxKey2XbmcKey:(unichar)character |
| 94 |
{ |
| 95 |
switch (character) |
| 96 |
{ |
| 97 |
case NSLeftArrowFunctionKey: |
| 98 |
return XBMCK_LEFT; |
| 99 |
case NSRightArrowFunctionKey: |
| 100 |
return XBMCK_RIGHT; |
| 101 |
case NSUpArrowFunctionKey: |
| 102 |
return XBMCK_UP; |
| 103 |
case NSDownArrowFunctionKey: |
| 104 |
return XBMCK_DOWN; |
| 105 |
case NSBackspaceCharacter: |
| 106 |
case NSDeleteCharacter: |
| 107 |
return XBMCK_BACKSPACE; |
| 108 |
case NSCarriageReturnCharacter: |
| 109 |
case NSEnterCharacter: |
| 110 |
return XBMCK_RETURN; |
| 111 |
case NSF1FunctionKey: |
| 112 |
return XBMCK_F1; |
| 113 |
case NSF2FunctionKey: |
| 114 |
return XBMCK_F2; |
| 115 |
case NSF3FunctionKey: |
| 116 |
return XBMCK_F3; |
| 117 |
case NSF4FunctionKey: |
| 118 |
return XBMCK_F4; |
| 119 |
case NSF5FunctionKey: |
| 120 |
return XBMCK_F5; |
| 121 |
case NSF6FunctionKey: |
| 122 |
return XBMCK_F6; |
| 123 |
case NSF7FunctionKey: |
| 124 |
return XBMCK_F7; |
| 125 |
case NSF8FunctionKey: |
| 126 |
return XBMCK_F8; |
| 127 |
case NSF9FunctionKey: |
| 128 |
return XBMCK_F9; |
| 129 |
case NSF10FunctionKey: |
| 130 |
return XBMCK_F10; |
| 131 |
case NSF11FunctionKey: |
| 132 |
return XBMCK_F11; |
| 133 |
case NSF12FunctionKey: |
| 134 |
return XBMCK_F12; |
| 135 |
case NSF13FunctionKey: |
| 136 |
return XBMCK_F13; |
| 137 |
case NSF14FunctionKey: |
| 138 |
return XBMCK_F14; |
| 139 |
case NSF15FunctionKey: |
| 140 |
return XBMCK_F15; |
| 141 |
case NSHomeFunctionKey: |
| 142 |
return XBMCK_HOME; |
| 143 |
case NSEndFunctionKey: |
| 144 |
return XBMCK_END; |
| 145 |
case NSPageDownFunctionKey: |
| 146 |
return XBMCK_PAGEDOWN; |
| 147 |
case NSPageUpFunctionKey: |
| 148 |
return XBMCK_PAGEUP; |
| 149 |
case NSPauseFunctionKey: |
| 150 |
return XBMCK_PAUSE; |
| 151 |
case NSInsertFunctionKey: |
| 152 |
case NSInsertCharFunctionKey: |
| 153 |
return XBMCK_INSERT; |
| 154 |
case NSDeleteFunctionKey: |
| 155 |
case NSDeleteCharFunctionKey: |
| 156 |
return XBMCK_DELETE; |
| 157 |
default: |
| 158 |
return character; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
- (XBMCMod)OsxMod2XbmcMod:(NSEventModifierFlags)appleModifier |
| 163 |
{ |
| 164 |
unsigned int xbmcModifier = XBMCKMOD_NONE; |
| 165 |
// shift |
| 166 |
if (appleModifier & NSEventModifierFlagShift) |
| 167 |
xbmcModifier |= XBMCKMOD_SHIFT; |
| 168 |
// left ctrl |
| 169 |
if (appleModifier & NSEventModifierFlagControl) |
| 170 |
xbmcModifier |= XBMCKMOD_CTRL; |
| 171 |
// left alt/option |
| 172 |
if (appleModifier & NSEventModifierFlagOption) |
| 173 |
xbmcModifier |= XBMCKMOD_ALT; |
| 174 |
// left command |
| 175 |
if (appleModifier & NSEventModifierFlagCommand) |
| 176 |
xbmcModifier |= XBMCKMOD_LMETA; |
| 177 |
|
| 178 |
return static_cast<XBMCMod>(xbmcModifier); |
| 179 |
} |
| 180 |
|
| 181 |
- (bool)ProcessOSXShortcuts:(XBMC_Event&)event |
| 182 |
{ |
| 183 |
const auto cmd = (event.key.keysym.mod & (XBMCKMOD_LMETA | XBMCKMOD_RMETA)) != 0; |
| 184 |
if (cmd && event.type == XBMC_KEYDOWN) |
| 185 |
{ |
| 186 |
switch (event.key.keysym.sym) |
| 187 |
{ |
| 188 |
case XBMCK_s: // CMD-s to take a screenshot |
| 189 |
{ |
| 190 |
CAction* action = new CAction(ACTION_TAKE_SCREENSHOT); |
| 191 |
CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_ACTION, WINDOW_INVALID, -1, |
| 192 |
static_cast<void*>(action)); |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
default: |
| 197 |
return false; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
- (void)enableInputEvents |
| 205 |
{ |
| 206 |
m_inputEnabled = true; |
| 207 |
} |
| 208 |
|
| 209 |
- (void)disableInputEvents |
| 210 |
{ |
| 211 |
m_inputEnabled = false; |
| 212 |
} |
| 213 |
|
| 214 |
- (void)signalMouseEntered |
| 215 |
{ |
| 216 |
if (m_lastAppCursorVisibilityAction != NSCursorVisibilityBalancer::HIDE) |
| 217 |
{ |
| 218 |
m_lastAppCursorVisibilityAction = NSCursorVisibilityBalancer::HIDE; |
| 219 |
[NSCursor hide]; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
- (void)signalMouseExited |
| 224 |
{ |
| 225 |
if (m_lastAppCursorVisibilityAction != NSCursorVisibilityBalancer::UNHIDE) |
| 226 |
{ |
| 227 |
m_lastAppCursorVisibilityAction = NSCursorVisibilityBalancer::UNHIDE; |
| 228 |
[NSCursor unhide]; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
- (void)ProcessInputEvent:(NSEvent*)nsEvent |
| 233 |
{ |
| 234 |
if (m_inputEnabled) |
| 235 |
{ |
| 236 |
[self InputEventHandler:nsEvent]; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
- (NSEvent*)InputEventHandler:(NSEvent*)nsEvent |
| 241 |
{ |
| 242 |
bool passEvent = true; |
| 243 |
XBMC_Event newEvent = {}; |
| 244 |
|
| 245 |
switch (nsEvent.type) |
| 246 |
{ |
| 247 |
// handle mouse events and transform them into the xbmc event world |
| 248 |
case NSEventTypeLeftMouseUp: |
| 249 |
{ |
| 250 |
[self SendXBMCMouseButtonEvent:nsEvent |
| 251 |
xbmcEvent:newEvent |
| 252 |
mouseEventType:XBMC_MOUSEBUTTONUP |
| 253 |
buttonCode:XBMC_BUTTON_LEFT]; |
| 254 |
break; |
| 255 |
} |
| 256 |
case NSEventTypeLeftMouseDown: |
| 257 |
{ |
| 258 |
[self SendXBMCMouseButtonEvent:nsEvent |
| 259 |
xbmcEvent:newEvent |
| 260 |
mouseEventType:XBMC_MOUSEBUTTONDOWN |
| 261 |
buttonCode:XBMC_BUTTON_LEFT]; |
| 262 |
break; |
| 263 |
} |
| 264 |
case NSEventTypeRightMouseUp: |
| 265 |
{ |
| 266 |
[self SendXBMCMouseButtonEvent:nsEvent |
| 267 |
xbmcEvent:newEvent |
| 268 |
mouseEventType:XBMC_MOUSEBUTTONUP |
| 269 |
buttonCode:XBMC_BUTTON_RIGHT]; |
| 270 |
break; |
| 271 |
} |
| 272 |
case NSEventTypeRightMouseDown: |
| 273 |
{ |
| 274 |
[self SendXBMCMouseButtonEvent:nsEvent |
| 275 |
xbmcEvent:newEvent |
| 276 |
mouseEventType:XBMC_MOUSEBUTTONDOWN |
| 277 |
buttonCode:XBMC_BUTTON_RIGHT]; |
| 278 |
break; |
| 279 |
} |
| 280 |
case NSEventTypeOtherMouseUp: |
| 281 |
{ |
| 282 |
[self SendXBMCMouseButtonEvent:nsEvent |
| 283 |
xbmcEvent:newEvent |
| 284 |
mouseEventType:XBMC_MOUSEBUTTONUP |
| 285 |
buttonCode:XBMC_BUTTON_MIDDLE]; |
| 286 |
break; |
| 287 |
} |
| 288 |
case NSEventTypeOtherMouseDown: |
| 289 |
{ |
| 290 |
[self SendXBMCMouseButtonEvent:nsEvent |
| 291 |
xbmcEvent:newEvent |
| 292 |
mouseEventType:XBMC_MOUSEBUTTONDOWN |
| 293 |
buttonCode:XBMC_BUTTON_MIDDLE]; |
| 294 |
break; |
| 295 |
} |
| 296 |
case NSEventTypeMouseMoved: |
| 297 |
case NSEventTypeLeftMouseDragged: |
| 298 |
case NSEventTypeRightMouseDragged: |
| 299 |
case NSEventTypeOtherMouseDragged: |
| 300 |
{ |
| 301 |
auto location = [self TranslateMouseLocation:nsEvent]; |
| 302 |
if (location.has_value()) |
| 303 |
{ |
| 304 |
NSPoint locationCoordinates = location.value(); |
| 305 |
newEvent.type = XBMC_MOUSEMOTION; |
| 306 |
newEvent.motion.x = locationCoordinates.x; |
| 307 |
newEvent.motion.y = locationCoordinates.y; |
| 308 |
[self MessagePush:&newEvent]; |
| 309 |
} |
| 310 |
break; |
| 311 |
} |
| 312 |
case NSEventTypeScrollWheel: |
| 313 |
{ |
| 314 |
// very strange, real scrolls have non-zero deltaY followed by same number of events |
| 315 |
// with a zero deltaY. This reverses our scroll which is WTF? anoying. Trap them out here. |
| 316 |
if (nsEvent.deltaY != 0.0) |
| 317 |
{ |
| 318 |
auto button = nsEvent.scrollingDeltaY > 0 ? XBMC_BUTTON_WHEELUP : XBMC_BUTTON_WHEELDOWN; |
| 319 |
if ([self SendXBMCMouseButtonEvent:nsEvent |
| 320 |
xbmcEvent:newEvent |
| 321 |
mouseEventType:XBMC_MOUSEBUTTONDOWN |
| 322 |
buttonCode:button]) |
| 323 |
{ |
| 324 |
// scrollwhell need a subsquent button press with no button code |
| 325 |
[self SendXBMCMouseButtonEvent:nsEvent |
| 326 |
xbmcEvent:newEvent |
| 327 |
mouseEventType:XBMC_MOUSEBUTTONUP |
| 328 |
buttonCode:std::nullopt]; |
| 329 |
} |
| 330 |
} |
| 331 |
break; |
| 332 |
} |
| 333 |
|
| 334 |
// handle keyboard events and transform them into the xbmc event world |
| 335 |
case NSEventTypeKeyUp: |
| 336 |
{ |
| 337 |
newEvent = [self keyPressEvent:nsEvent]; |
| 338 |
newEvent.type = XBMC_KEYUP; |
| 339 |
|
| 340 |
[self MessagePush:&newEvent]; |
| 341 |
passEvent = false; |
| 342 |
break; |
| 343 |
} |
| 344 |
case NSEventTypeKeyDown: |
| 345 |
{ |
| 346 |
newEvent = [self keyPressEvent:nsEvent]; |
| 347 |
newEvent.type = XBMC_KEYDOWN; |
| 348 |
|
| 349 |
if (![self ProcessOSXShortcuts:newEvent]) |
| 350 |
[self MessagePush:&newEvent]; |
| 351 |
passEvent = false; |
| 352 |
|
| 353 |
break; |
| 354 |
} |
| 355 |
default: |
| 356 |
return nsEvent; |
| 357 |
} |
| 358 |
// We must return the event for it to be useful if not already handled |
| 359 |
if (passEvent) |
| 360 |
return nsEvent; |
| 361 |
else |
| 362 |
return nullptr; |
| 363 |
} |
| 364 |
|
| 365 |
- (XBMC_Event)keyPressEvent:(NSEvent*)nsEvent |
| 366 |
{ |
| 367 |
XBMC_Event newEvent = {}; |
| 368 |
|
| 369 |
// use characters to propagate the actual unicode character |
| 370 |
NSString* unicode = nsEvent.characters; |
| 371 |
// use charactersIgnoringModifiers to get the corresponding char without modifiers. This will |
| 372 |
// keep shift so, lower case it as kodi shortcuts might depend on it. modifiers are propagated |
| 373 |
// anyway in keysym.mod |
| 374 |
NSString* unicodeWithoutModifiers = [nsEvent.charactersIgnoringModifiers lowercaseString]; |
| 375 |
|
| 376 |
// May be possible for actualStringLength > 1. Havent been able to replicate anything |
| 377 |
// larger than 1, but keep in mind for any regressions |
| 378 |
if (!unicode || unicode.length == 0 || !unicodeWithoutModifiers || |
| 379 |
unicodeWithoutModifiers.length == 0) |
| 380 |
{ |
| 381 |
return newEvent; |
| 382 |
} |
| 383 |
else if (unicode.length > 1) |
| 384 |
{ |
| 385 |
CLog::Log(LOGERROR, "CWinEventsOSXImpl::keyPressEvent - event string > 1 - size: {}", |
| 386 |
unicode.length); |
| 387 |
return newEvent; |
| 388 |
} |
| 389 |
|
| 390 |
newEvent.key.keysym.scancode = nsEvent.keyCode; |
| 391 |
newEvent.key.keysym.sym = |
| 392 |
static_cast<XBMCKey>([self OsxKey2XbmcKey:[unicodeWithoutModifiers characterAtIndex:0]]); |
| 393 |
newEvent.key.keysym.unicode = [unicode characterAtIndex:0]; |
| 394 |
newEvent.key.keysym.mod = [self OsxMod2XbmcMod:nsEvent.modifierFlags]; |
| 395 |
|
| 396 |
return newEvent; |
| 397 |
} |
| 398 |
|
| 399 |
- (BOOL)SendXBMCMouseButtonEvent:(NSEvent*)nsEvent |
| 400 |
xbmcEvent:(XBMC_Event&)xbmcEvent |
| 401 |
mouseEventType:(uint8_t)mouseEventType |
| 402 |
buttonCode:(std::optional<uint8_t>)buttonCode |
| 403 |
{ |
| 404 |
auto location = [self TranslateMouseLocation:nsEvent]; |
| 405 |
if (location.has_value()) |
| 406 |
{ |
| 407 |
NSPoint locationCoordinates = location.value(); |
| 408 |
xbmcEvent.type = mouseEventType; |
| 409 |
if (buttonCode.has_value()) |
| 410 |
{ |
| 411 |
xbmcEvent.button.button = buttonCode.value(); |
| 412 |
} |
| 413 |
xbmcEvent.button.x = locationCoordinates.x; |
| 414 |
xbmcEvent.button.y = locationCoordinates.y; |
| 415 |
[self MessagePush:&xbmcEvent]; |
| 416 |
return true; |
| 417 |
} |
| 418 |
return false; |
| 419 |
} |
| 420 |
|
| 421 |
- (std::optional<NSPoint>)TranslateMouseLocation:(NSEvent*)nsEvent |
| 422 |
{ |
| 423 |
NSPoint location = nsEvent.locationInWindow; |
| 424 |
// ignore events if outside the view bounds |
| 425 |
if (!nsEvent.window || !NSPointInRect(location, nsEvent.window.contentView.frame)) |
| 426 |
{ |
| 427 |
return std::nullopt; |
| 428 |
} |
| 429 |
// translate the location to backing units |
| 430 |
location = [nsEvent.window convertPointToBacking:location]; |
| 431 |
NSRect frame = [nsEvent.window convertRectToBacking:nsEvent.window.contentView.frame]; |
| 432 |
// cocoa world is upside down ... |
| 433 |
location.y = frame.size.height - location.y; |
| 434 |
return location; |
| 435 |
} |
| 436 |
|
| 437 |
@end |