You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cocoa.mm 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <FL/Fl.H>
  22. #include <FL/Fl_Window.H>
  23. #include <FL/x.H>
  24. #import <Cocoa/Cocoa.h>
  25. #import <Carbon/Carbon.h>
  26. #include <IOKit/hidsystem/IOHIDLib.h>
  27. #include <IOKit/hidsystem/IOHIDParameter.h>
  28. #define XK_LATIN1
  29. #define XK_MISCELLANY
  30. #define XK_XKB_KEYS
  31. #include <rfb/keysymdef.h>
  32. #include <rfb/XF86keysym.h>
  33. #include <rfb/Rect.h>
  34. #include "keysym2ucs.h"
  35. #define NoSymbol 0
  36. // This wasn't added until 10.12
  37. #if !defined(MAC_OS_X_VERSION_10_12) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12
  38. const int kVK_RightCommand = 0x36;
  39. #endif
  40. // And this is still missing
  41. const int kVK_Menu = 0x6E;
  42. static bool captured = false;
  43. int cocoa_get_level(Fl_Window *win)
  44. {
  45. NSWindow *nsw;
  46. nsw = (NSWindow*)fl_xid(win);
  47. return [nsw level];
  48. }
  49. void cocoa_set_level(Fl_Window *win, int level)
  50. {
  51. NSWindow *nsw;
  52. nsw = (NSWindow*)fl_xid(win);
  53. [nsw setLevel:level];
  54. }
  55. int cocoa_capture_displays(Fl_Window *win)
  56. {
  57. NSWindow *nsw;
  58. nsw = (NSWindow*)fl_xid(win);
  59. CGDisplayCount count;
  60. CGDirectDisplayID displays[16];
  61. int sx, sy, sw, sh;
  62. rfb::Rect windows_rect, screen_rect;
  63. windows_rect.setXYWH(win->x(), win->y(), win->w(), win->h());
  64. if (CGGetActiveDisplayList(16, displays, &count) != kCGErrorSuccess)
  65. return 1;
  66. if (count != (unsigned)Fl::screen_count())
  67. return 1;
  68. for (int i = 0; i < Fl::screen_count(); i++) {
  69. Fl::screen_xywh(sx, sy, sw, sh, i);
  70. screen_rect.setXYWH(sx, sy, sw, sh);
  71. if (screen_rect.enclosed_by(windows_rect)) {
  72. if (CGDisplayCapture(displays[i]) != kCGErrorSuccess)
  73. return 1;
  74. } else {
  75. // A display might have been captured with the previous
  76. // monitor selection. In that case we don't want to keep
  77. // it when its no longer inside the window_rect.
  78. CGDisplayRelease(displays[i]);
  79. }
  80. }
  81. captured = true;
  82. if ([nsw level] == CGShieldingWindowLevel())
  83. return 0;
  84. [nsw setLevel:CGShieldingWindowLevel()];
  85. return 0;
  86. }
  87. void cocoa_release_displays(Fl_Window *win)
  88. {
  89. NSWindow *nsw;
  90. int newlevel;
  91. if (captured)
  92. CGReleaseAllDisplays();
  93. captured = false;
  94. nsw = (NSWindow*)fl_xid(win);
  95. // Someone else has already changed the level of this window
  96. if ([nsw level] != CGShieldingWindowLevel())
  97. return;
  98. // FIXME: Store the previous level somewhere so we don't have to hard
  99. // code a level here.
  100. if (win->fullscreen_active() && win->contains(Fl::focus()))
  101. newlevel = NSStatusWindowLevel;
  102. else
  103. newlevel = NSNormalWindowLevel;
  104. // Only change if different as the level change also moves the window
  105. // to the top of that level.
  106. if ([nsw level] != newlevel)
  107. [nsw setLevel:newlevel];
  108. }
  109. CGColorSpaceRef cocoa_win_color_space(Fl_Window *win)
  110. {
  111. NSWindow *nsw;
  112. NSColorSpace *nscs;
  113. nsw = (NSWindow*)fl_xid(win);
  114. nscs = [nsw colorSpace];
  115. if (nscs == nil) {
  116. // Offscreen, so return standard SRGB color space
  117. assert(false);
  118. return CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
  119. }
  120. CGColorSpaceRef lut = [nscs CGColorSpace];
  121. // We want a permanent reference, not an autorelease
  122. CGColorSpaceRetain(lut);
  123. return lut;
  124. }
  125. bool cocoa_win_is_zoomed(Fl_Window *win)
  126. {
  127. NSWindow *nsw;
  128. nsw = (NSWindow*)fl_xid(win);
  129. return [nsw isZoomed];
  130. }
  131. void cocoa_win_zoom(Fl_Window *win)
  132. {
  133. NSWindow *nsw;
  134. nsw = (NSWindow*)fl_xid(win);
  135. [nsw zoom:nsw];
  136. }
  137. int cocoa_is_keyboard_sync(const void *event)
  138. {
  139. const NSEvent* nsevent = (const NSEvent*)event;
  140. assert(event);
  141. // If we get a NSFlagsChanged event with key code 0 then this isn't
  142. // an actual keyboard event but rather the system trying to sync up
  143. // modifier state after it has stolen input for some reason (e.g.
  144. // Cmd+Tab)
  145. if ([nsevent type] != NSFlagsChanged)
  146. return 0;
  147. if ([nsevent keyCode] != 0)
  148. return 0;
  149. return 1;
  150. }
  151. int cocoa_is_keyboard_event(const void *event)
  152. {
  153. NSEvent *nsevent;
  154. nsevent = (NSEvent*)event;
  155. switch ([nsevent type]) {
  156. case NSKeyDown:
  157. case NSKeyUp:
  158. case NSFlagsChanged:
  159. if (cocoa_is_keyboard_sync(event))
  160. return 0;
  161. return 1;
  162. default:
  163. return 0;
  164. }
  165. }
  166. int cocoa_is_key_press(const void *event)
  167. {
  168. NSEvent *nsevent;
  169. nsevent = (NSEvent*)event;
  170. if ([nsevent type] == NSKeyDown)
  171. return 1;
  172. if ([nsevent type] == NSFlagsChanged) {
  173. UInt32 mask;
  174. // We don't see any event on release of CapsLock
  175. if ([nsevent keyCode] == kVK_CapsLock)
  176. return 1;
  177. // These are entirely undocumented, but I cannot find any other way
  178. // of differentiating between left and right keys
  179. switch ([nsevent keyCode]) {
  180. case kVK_RightCommand:
  181. mask = 0x0010;
  182. break;
  183. case kVK_Command:
  184. mask = 0x0008;
  185. break;
  186. case kVK_Shift:
  187. mask = 0x0002;
  188. break;
  189. case kVK_CapsLock:
  190. // We don't see any event on release of CapsLock
  191. return 1;
  192. case kVK_Option:
  193. mask = 0x0020;
  194. break;
  195. case kVK_Control:
  196. mask = 0x0001;
  197. break;
  198. case kVK_RightShift:
  199. mask = 0x0004;
  200. break;
  201. case kVK_RightOption:
  202. mask = 0x0040;
  203. break;
  204. case kVK_RightControl:
  205. mask = 0x2000;
  206. break;
  207. default:
  208. return 0;
  209. }
  210. if ([nsevent modifierFlags] & mask)
  211. return 1;
  212. else
  213. return 0;
  214. }
  215. return 0;
  216. }
  217. int cocoa_event_keycode(const void *event)
  218. {
  219. NSEvent *nsevent;
  220. int keycode;
  221. nsevent = (NSEvent*)event;
  222. keycode = [nsevent keyCode];
  223. // macOS swaps these two keys for unknown reasons for ISO layouts
  224. if (KBGetLayoutType(LMGetKbdType()) == kKeyboardISO) {
  225. if (keycode == kVK_ANSI_Grave)
  226. return kVK_ISO_Section;
  227. if (keycode == kVK_ISO_Section)
  228. return kVK_ANSI_Grave;
  229. }
  230. return keycode;
  231. }
  232. static NSString *key_translate(UInt16 keyCode, UInt32 modifierFlags)
  233. {
  234. const UCKeyboardLayout *layout;
  235. OSStatus err;
  236. layout = NULL;
  237. TISInputSourceRef keyboard;
  238. CFDataRef uchr;
  239. keyboard = TISCopyCurrentKeyboardLayoutInputSource();
  240. uchr = (CFDataRef)TISGetInputSourceProperty(keyboard,
  241. kTISPropertyUnicodeKeyLayoutData);
  242. if (uchr == NULL)
  243. return nil;
  244. layout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
  245. if (layout == NULL)
  246. return nil;
  247. UInt32 dead_state;
  248. UniCharCount max_len, actual_len;
  249. UniChar string[255];
  250. dead_state = 0;
  251. max_len = sizeof(string)/sizeof(*string);
  252. modifierFlags = (modifierFlags >> 8) & 0xff;
  253. err = UCKeyTranslate(layout, keyCode, kUCKeyActionDown, modifierFlags,
  254. LMGetKbdType(), 0, &dead_state, max_len, &actual_len,
  255. string);
  256. if (err != noErr)
  257. return nil;
  258. // Dead key?
  259. if (dead_state != 0) {
  260. // We have no fool proof way of asking what dead key this is.
  261. // Assume we get a spacing equivalent if we press the
  262. // same key again, and try to deduce something from that.
  263. err = UCKeyTranslate(layout, keyCode, kUCKeyActionDown, modifierFlags,
  264. LMGetKbdType(), 0, &dead_state, max_len, &actual_len,
  265. string);
  266. if (err != noErr)
  267. return nil;
  268. }
  269. return [NSString stringWithCharacters:string length:actual_len];
  270. }
  271. static const int kvk_map[][2] = {
  272. { kVK_Return, XK_Return },
  273. { kVK_Tab, XK_Tab },
  274. { kVK_Space, XK_space },
  275. { kVK_Delete, XK_BackSpace },
  276. { kVK_Escape, XK_Escape },
  277. { kVK_RightCommand, XK_Super_R },
  278. { kVK_Command, XK_Super_L },
  279. { kVK_Shift, XK_Shift_L },
  280. { kVK_CapsLock, XK_Caps_Lock },
  281. { kVK_Option, XK_Alt_L },
  282. { kVK_Control, XK_Control_L },
  283. { kVK_RightShift, XK_Shift_R },
  284. { kVK_RightOption, XK_Alt_R },
  285. { kVK_RightControl, XK_Control_R },
  286. { kVK_F17, XK_F17 },
  287. { kVK_VolumeUp, XF86XK_AudioRaiseVolume },
  288. { kVK_VolumeDown, XF86XK_AudioLowerVolume },
  289. { kVK_Mute, XF86XK_AudioMute },
  290. { kVK_F18, XK_F18 },
  291. { kVK_F19, XK_F19 },
  292. { kVK_F20, XK_F20 },
  293. { kVK_F5, XK_F5 },
  294. { kVK_F6, XK_F6 },
  295. { kVK_F7, XK_F7 },
  296. { kVK_F3, XK_F3 },
  297. { kVK_F8, XK_F8 },
  298. { kVK_F9, XK_F9 },
  299. { kVK_F11, XK_F11 },
  300. { kVK_F13, XK_F13 },
  301. { kVK_F16, XK_F16 },
  302. { kVK_F14, XK_F14 },
  303. { kVK_F10, XK_F10 },
  304. { kVK_Menu, XK_Menu },
  305. { kVK_F12, XK_F12 },
  306. { kVK_F15, XK_F15 },
  307. // Should we send Insert here?
  308. { kVK_Help, XK_Help },
  309. { kVK_Home, XK_Home },
  310. { kVK_PageUp, XK_Page_Up },
  311. { kVK_ForwardDelete, XK_Delete },
  312. { kVK_F4, XK_F4 },
  313. { kVK_End, XK_End },
  314. { kVK_F2, XK_F2 },
  315. { kVK_PageDown, XK_Page_Down },
  316. { kVK_F1, XK_F1 },
  317. { kVK_LeftArrow, XK_Left },
  318. { kVK_RightArrow, XK_Right },
  319. { kVK_DownArrow, XK_Down },
  320. { kVK_UpArrow, XK_Up },
  321. // The OS X headers claim these keys are not layout independent.
  322. // Could it be because of the state of the decimal key?
  323. /* { kVK_ANSI_KeypadDecimal, XK_KP_Decimal }, */ // see below
  324. { kVK_ANSI_KeypadMultiply, XK_KP_Multiply },
  325. { kVK_ANSI_KeypadPlus, XK_KP_Add },
  326. // OS X doesn't have NumLock, so is this really correct?
  327. { kVK_ANSI_KeypadClear, XK_Num_Lock },
  328. { kVK_ANSI_KeypadDivide, XK_KP_Divide },
  329. { kVK_ANSI_KeypadEnter, XK_KP_Enter },
  330. { kVK_ANSI_KeypadMinus, XK_KP_Subtract },
  331. { kVK_ANSI_KeypadEquals, XK_KP_Equal },
  332. { kVK_ANSI_Keypad0, XK_KP_0 },
  333. { kVK_ANSI_Keypad1, XK_KP_1 },
  334. { kVK_ANSI_Keypad2, XK_KP_2 },
  335. { kVK_ANSI_Keypad3, XK_KP_3 },
  336. { kVK_ANSI_Keypad4, XK_KP_4 },
  337. { kVK_ANSI_Keypad5, XK_KP_5 },
  338. { kVK_ANSI_Keypad6, XK_KP_6 },
  339. { kVK_ANSI_Keypad7, XK_KP_7 },
  340. { kVK_ANSI_Keypad8, XK_KP_8 },
  341. { kVK_ANSI_Keypad9, XK_KP_9 },
  342. // Japanese Keyboard Support
  343. { kVK_JIS_Eisu, XK_Eisu_toggle },
  344. { kVK_JIS_Kana, XK_Hiragana_Katakana },
  345. };
  346. int cocoa_event_keysym(const void *event)
  347. {
  348. NSEvent *nsevent;
  349. UInt16 key_code;
  350. size_t i;
  351. NSString *chars;
  352. UInt32 modifiers;
  353. nsevent = (NSEvent*)event;
  354. key_code = [nsevent keyCode];
  355. // Start with keys that either don't generate a symbol, or
  356. // generate the same symbol as some other key.
  357. for (i = 0;i < sizeof(kvk_map)/sizeof(kvk_map[0]);i++) {
  358. if (key_code == kvk_map[i][0])
  359. return kvk_map[i][1];
  360. }
  361. // OS X always sends the same key code for the decimal key on the
  362. // num pad, but X11 wants different keysyms depending on if it should
  363. // be a comma or full stop.
  364. if (key_code == 0x41) {
  365. switch ([[nsevent charactersIgnoringModifiers] UTF8String][0]) {
  366. case ',':
  367. return XK_KP_Separator;
  368. case '.':
  369. return XK_KP_Decimal;
  370. default:
  371. return NoSymbol;
  372. }
  373. }
  374. // We want a "normal" symbol out of the event, which basically means
  375. // we only respect the shift and alt/altgr modifiers. Cocoa can help
  376. // us if we only wanted shift, but as we also want alt/altgr, we'll
  377. // have to do some lookup ourselves. This matches our behaviour on
  378. // other platforms.
  379. modifiers = 0;
  380. if ([nsevent modifierFlags] & NSAlphaShiftKeyMask)
  381. modifiers |= alphaLock;
  382. if ([nsevent modifierFlags] & NSShiftKeyMask)
  383. modifiers |= shiftKey;
  384. if ([nsevent modifierFlags] & NSAlternateKeyMask)
  385. modifiers |= optionKey;
  386. chars = key_translate(key_code, modifiers);
  387. if (chars == nil)
  388. return NoSymbol;
  389. // FIXME: Some dead keys are given as NBSP + combining character
  390. if ([chars length] != 1)
  391. return NoSymbol;
  392. // Dead key?
  393. if ([[nsevent characters] length] == 0)
  394. return ucs2keysym(ucs2combining([chars characterAtIndex:0]));
  395. return ucs2keysym([chars characterAtIndex:0]);
  396. }
  397. static int cocoa_open_hid(io_connect_t *ioc)
  398. {
  399. kern_return_t ret;
  400. io_service_t ios;
  401. CFMutableDictionaryRef mdict;
  402. mdict = IOServiceMatching(kIOHIDSystemClass);
  403. ios = IOServiceGetMatchingService(kIOMasterPortDefault,
  404. (CFDictionaryRef) mdict);
  405. if (!ios)
  406. return KERN_FAILURE;
  407. ret = IOServiceOpen(ios, mach_task_self(), kIOHIDParamConnectType, ioc);
  408. IOObjectRelease(ios);
  409. if (ret != KERN_SUCCESS)
  410. return ret;
  411. return KERN_SUCCESS;
  412. }
  413. static int cocoa_set_modifier_lock_state(int modifier, bool on)
  414. {
  415. kern_return_t ret;
  416. io_connect_t ioc;
  417. ret = cocoa_open_hid(&ioc);
  418. if (ret != KERN_SUCCESS)
  419. return ret;
  420. ret = IOHIDSetModifierLockState(ioc, modifier, on);
  421. IOServiceClose(ioc);
  422. if (ret != KERN_SUCCESS)
  423. return ret;
  424. return KERN_SUCCESS;
  425. }
  426. static int cocoa_get_modifier_lock_state(int modifier, bool *on)
  427. {
  428. kern_return_t ret;
  429. io_connect_t ioc;
  430. ret = cocoa_open_hid(&ioc);
  431. if (ret != KERN_SUCCESS)
  432. return ret;
  433. ret = IOHIDGetModifierLockState(ioc, modifier, on);
  434. IOServiceClose(ioc);
  435. if (ret != KERN_SUCCESS)
  436. return ret;
  437. return KERN_SUCCESS;
  438. }
  439. int cocoa_set_caps_lock_state(bool on)
  440. {
  441. return cocoa_set_modifier_lock_state(kIOHIDCapsLockState, on);
  442. }
  443. int cocoa_set_num_lock_state(bool on)
  444. {
  445. return cocoa_set_modifier_lock_state(kIOHIDNumLockState, on);
  446. }
  447. int cocoa_get_caps_lock_state(bool *on)
  448. {
  449. return cocoa_get_modifier_lock_state(kIOHIDCapsLockState, on);
  450. }
  451. int cocoa_get_num_lock_state(bool *on)
  452. {
  453. return cocoa_get_modifier_lock_state(kIOHIDNumLockState, on);
  454. }