Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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