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

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