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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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_event(const void *event)
  138. {
  139. NSEvent *nsevent;
  140. nsevent = (NSEvent*)event;
  141. switch ([nsevent type]) {
  142. case NSKeyDown:
  143. case NSKeyUp:
  144. case NSFlagsChanged:
  145. return 1;
  146. default:
  147. return 0;
  148. }
  149. }
  150. int cocoa_is_key_press(const void *event)
  151. {
  152. NSEvent *nsevent;
  153. nsevent = (NSEvent*)event;
  154. if ([nsevent type] == NSKeyDown)
  155. return 1;
  156. if ([nsevent type] == NSFlagsChanged) {
  157. UInt32 mask;
  158. // We don't see any event on release of CapsLock
  159. if ([nsevent keyCode] == kVK_CapsLock)
  160. return 1;
  161. // These are entirely undocumented, but I cannot find any other way
  162. // of differentiating between left and right keys
  163. switch ([nsevent keyCode]) {
  164. case kVK_RightCommand:
  165. mask = 0x0010;
  166. break;
  167. case kVK_Command:
  168. mask = 0x0008;
  169. break;
  170. case kVK_Shift:
  171. mask = 0x0002;
  172. break;
  173. case kVK_CapsLock:
  174. // We don't see any event on release of CapsLock
  175. return 1;
  176. case kVK_Option:
  177. mask = 0x0020;
  178. break;
  179. case kVK_Control:
  180. mask = 0x0001;
  181. break;
  182. case kVK_RightShift:
  183. mask = 0x0004;
  184. break;
  185. case kVK_RightOption:
  186. mask = 0x0040;
  187. break;
  188. case kVK_RightControl:
  189. mask = 0x2000;
  190. break;
  191. default:
  192. return 0;
  193. }
  194. if ([nsevent modifierFlags] & mask)
  195. return 1;
  196. else
  197. return 0;
  198. }
  199. return 0;
  200. }
  201. int cocoa_event_keycode(const void *event)
  202. {
  203. NSEvent *nsevent;
  204. nsevent = (NSEvent*)event;
  205. return [nsevent keyCode];
  206. }
  207. static NSString *key_translate(UInt16 keyCode, UInt32 modifierFlags)
  208. {
  209. const UCKeyboardLayout *layout;
  210. OSStatus err;
  211. layout = NULL;
  212. TISInputSourceRef keyboard;
  213. CFDataRef uchr;
  214. keyboard = TISCopyCurrentKeyboardLayoutInputSource();
  215. uchr = (CFDataRef)TISGetInputSourceProperty(keyboard,
  216. kTISPropertyUnicodeKeyLayoutData);
  217. if (uchr == NULL)
  218. return nil;
  219. layout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
  220. if (layout == NULL)
  221. return nil;
  222. UInt32 dead_state;
  223. UniCharCount max_len, actual_len;
  224. UniChar string[255];
  225. dead_state = 0;
  226. max_len = sizeof(string)/sizeof(*string);
  227. modifierFlags = (modifierFlags >> 8) & 0xff;
  228. err = UCKeyTranslate(layout, keyCode, kUCKeyActionDown, modifierFlags,
  229. LMGetKbdType(), 0, &dead_state, max_len, &actual_len,
  230. string);
  231. if (err != noErr)
  232. return nil;
  233. // Dead key?
  234. if (dead_state != 0) {
  235. // We have no fool proof way of asking what dead key this is.
  236. // Assume we get a spacing equivalent if we press the
  237. // same key again, and try to deduce something from that.
  238. err = UCKeyTranslate(layout, keyCode, kUCKeyActionDown, modifierFlags,
  239. LMGetKbdType(), 0, &dead_state, max_len, &actual_len,
  240. string);
  241. if (err != noErr)
  242. return nil;
  243. }
  244. return [NSString stringWithCharacters:string length:actual_len];
  245. }
  246. static const int kvk_map[][2] = {
  247. { kVK_Return, XK_Return },
  248. { kVK_Tab, XK_Tab },
  249. { kVK_Space, XK_space },
  250. { kVK_Delete, XK_BackSpace },
  251. { kVK_Escape, XK_Escape },
  252. { kVK_RightCommand, XK_Super_R },
  253. { kVK_Command, XK_Super_L },
  254. { kVK_Shift, XK_Shift_L },
  255. { kVK_CapsLock, XK_Caps_Lock },
  256. { kVK_Option, XK_Alt_L },
  257. { kVK_Control, XK_Control_L },
  258. { kVK_RightShift, XK_Shift_R },
  259. { kVK_RightOption, XK_Alt_R },
  260. { kVK_RightControl, XK_Control_R },
  261. { kVK_F17, XK_F17 },
  262. { kVK_VolumeUp, XF86XK_AudioRaiseVolume },
  263. { kVK_VolumeDown, XF86XK_AudioLowerVolume },
  264. { kVK_Mute, XF86XK_AudioMute },
  265. { kVK_F18, XK_F18 },
  266. { kVK_F19, XK_F19 },
  267. { kVK_F20, XK_F20 },
  268. { kVK_F5, XK_F5 },
  269. { kVK_F6, XK_F6 },
  270. { kVK_F7, XK_F7 },
  271. { kVK_F3, XK_F3 },
  272. { kVK_F8, XK_F8 },
  273. { kVK_F9, XK_F9 },
  274. { kVK_F11, XK_F11 },
  275. { kVK_F13, XK_F13 },
  276. { kVK_F16, XK_F16 },
  277. { kVK_F14, XK_F14 },
  278. { kVK_F10, XK_F10 },
  279. { kVK_Menu, XK_Menu },
  280. { kVK_F12, XK_F12 },
  281. { kVK_F15, XK_F15 },
  282. // Should we send Insert here?
  283. { kVK_Help, XK_Help },
  284. { kVK_Home, XK_Home },
  285. { kVK_PageUp, XK_Page_Up },
  286. { kVK_ForwardDelete, XK_Delete },
  287. { kVK_F4, XK_F4 },
  288. { kVK_End, XK_End },
  289. { kVK_F2, XK_F2 },
  290. { kVK_PageDown, XK_Page_Down },
  291. { kVK_F1, XK_F1 },
  292. { kVK_LeftArrow, XK_Left },
  293. { kVK_RightArrow, XK_Right },
  294. { kVK_DownArrow, XK_Down },
  295. { kVK_UpArrow, XK_Up },
  296. // The OS X headers claim these keys are not layout independent.
  297. // Could it be because of the state of the decimal key?
  298. /* { kVK_ANSI_KeypadDecimal, XK_KP_Decimal }, */ // see below
  299. { kVK_ANSI_KeypadMultiply, XK_KP_Multiply },
  300. { kVK_ANSI_KeypadPlus, XK_KP_Add },
  301. // OS X doesn't have NumLock, so is this really correct?
  302. { kVK_ANSI_KeypadClear, XK_Num_Lock },
  303. { kVK_ANSI_KeypadDivide, XK_KP_Divide },
  304. { kVK_ANSI_KeypadEnter, XK_KP_Enter },
  305. { kVK_ANSI_KeypadMinus, XK_KP_Subtract },
  306. { kVK_ANSI_KeypadEquals, XK_KP_Equal },
  307. { kVK_ANSI_Keypad0, XK_KP_0 },
  308. { kVK_ANSI_Keypad1, XK_KP_1 },
  309. { kVK_ANSI_Keypad2, XK_KP_2 },
  310. { kVK_ANSI_Keypad3, XK_KP_3 },
  311. { kVK_ANSI_Keypad4, XK_KP_4 },
  312. { kVK_ANSI_Keypad5, XK_KP_5 },
  313. { kVK_ANSI_Keypad6, XK_KP_6 },
  314. { kVK_ANSI_Keypad7, XK_KP_7 },
  315. { kVK_ANSI_Keypad8, XK_KP_8 },
  316. { kVK_ANSI_Keypad9, XK_KP_9 },
  317. // Japanese Keyboard Support
  318. { kVK_JIS_Eisu, XK_Eisu_toggle },
  319. { kVK_JIS_Kana, XK_Hiragana_Katakana },
  320. };
  321. int cocoa_event_keysym(const void *event)
  322. {
  323. NSEvent *nsevent;
  324. UInt16 key_code;
  325. size_t i;
  326. NSString *chars;
  327. UInt32 modifiers;
  328. nsevent = (NSEvent*)event;
  329. key_code = [nsevent keyCode];
  330. // Start with keys that either don't generate a symbol, or
  331. // generate the same symbol as some other key.
  332. for (i = 0;i < sizeof(kvk_map)/sizeof(kvk_map[0]);i++) {
  333. if (key_code == kvk_map[i][0])
  334. return kvk_map[i][1];
  335. }
  336. // OS X always sends the same key code for the decimal key on the
  337. // num pad, but X11 wants different keysyms depending on if it should
  338. // be a comma or full stop.
  339. if (key_code == 0x41) {
  340. switch ([[nsevent charactersIgnoringModifiers] UTF8String][0]) {
  341. case ',':
  342. return XK_KP_Separator;
  343. case '.':
  344. return XK_KP_Decimal;
  345. default:
  346. return NoSymbol;
  347. }
  348. }
  349. // We want a "normal" symbol out of the event, which basically means
  350. // we only respect the shift and alt/altgr modifiers. Cocoa can help
  351. // us if we only wanted shift, but as we also want alt/altgr, we'll
  352. // have to do some lookup ourselves. This matches our behaviour on
  353. // other platforms.
  354. modifiers = 0;
  355. if ([nsevent modifierFlags] & NSAlphaShiftKeyMask)
  356. modifiers |= alphaLock;
  357. if ([nsevent modifierFlags] & NSShiftKeyMask)
  358. modifiers |= shiftKey;
  359. if ([nsevent modifierFlags] & NSAlternateKeyMask)
  360. modifiers |= optionKey;
  361. chars = key_translate(key_code, modifiers);
  362. if (chars == nil)
  363. return NoSymbol;
  364. // FIXME: Some dead keys are given as NBSP + combining character
  365. if ([chars length] != 1)
  366. return NoSymbol;
  367. // Dead key?
  368. if ([[nsevent characters] length] == 0)
  369. return ucs2keysym(ucs2combining([chars characterAtIndex:0]));
  370. return ucs2keysym([chars characterAtIndex:0]);
  371. }
  372. static int cocoa_open_hid(io_connect_t *ioc)
  373. {
  374. kern_return_t ret;
  375. io_service_t ios;
  376. CFMutableDictionaryRef mdict;
  377. mdict = IOServiceMatching(kIOHIDSystemClass);
  378. ios = IOServiceGetMatchingService(kIOMasterPortDefault,
  379. (CFDictionaryRef) mdict);
  380. if (!ios)
  381. return KERN_FAILURE;
  382. ret = IOServiceOpen(ios, mach_task_self(), kIOHIDParamConnectType, ioc);
  383. IOObjectRelease(ios);
  384. if (ret != KERN_SUCCESS)
  385. return ret;
  386. return KERN_SUCCESS;
  387. }
  388. static int cocoa_set_modifier_lock_state(int modifier, bool on)
  389. {
  390. kern_return_t ret;
  391. io_connect_t ioc;
  392. ret = cocoa_open_hid(&ioc);
  393. if (ret != KERN_SUCCESS)
  394. return ret;
  395. ret = IOHIDSetModifierLockState(ioc, modifier, on);
  396. IOServiceClose(ioc);
  397. if (ret != KERN_SUCCESS)
  398. return ret;
  399. return KERN_SUCCESS;
  400. }
  401. static int cocoa_get_modifier_lock_state(int modifier, bool *on)
  402. {
  403. kern_return_t ret;
  404. io_connect_t ioc;
  405. ret = cocoa_open_hid(&ioc);
  406. if (ret != KERN_SUCCESS)
  407. return ret;
  408. ret = IOHIDGetModifierLockState(ioc, modifier, on);
  409. IOServiceClose(ioc);
  410. if (ret != KERN_SUCCESS)
  411. return ret;
  412. return KERN_SUCCESS;
  413. }
  414. int cocoa_set_caps_lock_state(bool on)
  415. {
  416. return cocoa_set_modifier_lock_state(kIOHIDCapsLockState, on);
  417. }
  418. int cocoa_set_num_lock_state(bool on)
  419. {
  420. return cocoa_set_modifier_lock_state(kIOHIDNumLockState, on);
  421. }
  422. int cocoa_get_caps_lock_state(bool *on)
  423. {
  424. return cocoa_get_modifier_lock_state(kIOHIDCapsLockState, on);
  425. }
  426. int cocoa_get_num_lock_state(bool *on)
  427. {
  428. return cocoa_get_modifier_lock_state(kIOHIDNumLockState, on);
  429. }