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

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