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

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