qxtglobalshortcut_mac.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <Carbon/Carbon.h>
  2. /****************************************************************************
  3. ** Copyright (c) 2006 - 2011, the LibQxt project.
  4. ** See the Qxt AUTHORS file for a list of authors and copyright holders.
  5. ** All rights reserved.
  6. **
  7. ** Redistribution and use in source and binary forms, with or without
  8. ** modification, are permitted provided that the following conditions are met:
  9. ** * Redistributions of source code must retain the above copyright
  10. ** notice, this list of conditions and the following disclaimer.
  11. ** * Redistributions in binary form must reproduce the above copyright
  12. ** notice, this list of conditions and the following disclaimer in the
  13. ** documentation and/or other materials provided with the distribution.
  14. ** * Neither the name of the LibQxt project nor the
  15. ** names of its contributors may be used to endorse or promote products
  16. ** derived from this software without specific prior written permission.
  17. **
  18. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. ** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. **
  29. ** <http://libqxt.org> <foundation@libqxt.org>
  30. *****************************************************************************/
  31. #include "qxtglobalshortcut_p.h"
  32. #include <QMap>
  33. #include <QHash>
  34. #include <QtDebug>
  35. #include <QApplication>
  36. typedef QPair<uint, uint> Identifier;
  37. static QMap<quint32, EventHotKeyRef> keyRefs;
  38. static QHash<Identifier, quint32> keyIDs;
  39. static quint32 hotKeySerial = 0;
  40. static bool qxt_mac_handler_installed = false;
  41. OSStatus qxt_mac_handle_hot_key(EventHandlerCallRef nextHandler, EventRef event, void* data)
  42. {
  43. Q_UNUSED(nextHandler);
  44. Q_UNUSED(data);
  45. if (GetEventClass(event) == kEventClassKeyboard && GetEventKind(event) == kEventHotKeyPressed)
  46. {
  47. EventHotKeyID keyID;
  48. GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(keyID), NULL, &keyID);
  49. Identifier id = keyIDs.key(keyID.id);
  50. QxtGlobalShortcutPrivate::activateShortcut(id.second, id.first);
  51. }
  52. return noErr;
  53. }
  54. quint32 QxtGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
  55. {
  56. quint32 native = 0;
  57. if (modifiers & Qt::ShiftModifier)
  58. native |= shiftKey;
  59. if (modifiers & Qt::ControlModifier)
  60. native |= cmdKey;
  61. if (modifiers & Qt::AltModifier)
  62. native |= optionKey;
  63. if (modifiers & Qt::MetaModifier)
  64. native |= controlKey;
  65. if (modifiers & Qt::KeypadModifier)
  66. native |= kEventKeyModifierNumLockMask;
  67. return native;
  68. }
  69. quint32 QxtGlobalShortcutPrivate::nativeKeycode(Qt::Key key)
  70. {
  71. UTF16Char ch;
  72. // Constants found in NSEvent.h from AppKit.framework
  73. switch (key)
  74. {
  75. case Qt::Key_Return:
  76. return kVK_Return;
  77. case Qt::Key_Enter:
  78. return kVK_ANSI_KeypadEnter;
  79. case Qt::Key_Tab:
  80. return kVK_Tab;
  81. case Qt::Key_Space:
  82. return kVK_Space;
  83. case Qt::Key_Backspace:
  84. return kVK_Delete;
  85. case Qt::Key_Control:
  86. return kVK_Command;
  87. case Qt::Key_Shift:
  88. return kVK_Shift;
  89. case Qt::Key_CapsLock:
  90. return kVK_CapsLock;
  91. case Qt::Key_Option:
  92. return kVK_Option;
  93. case Qt::Key_Meta:
  94. return kVK_Control;
  95. case Qt::Key_F17:
  96. return kVK_F17;
  97. case Qt::Key_VolumeUp:
  98. return kVK_VolumeUp;
  99. case Qt::Key_VolumeDown:
  100. return kVK_VolumeDown;
  101. case Qt::Key_F18:
  102. return kVK_F18;
  103. case Qt::Key_F19:
  104. return kVK_F19;
  105. case Qt::Key_F20:
  106. return kVK_F20;
  107. case Qt::Key_F5:
  108. return kVK_F5;
  109. case Qt::Key_F6:
  110. return kVK_F6;
  111. case Qt::Key_F7:
  112. return kVK_F7;
  113. case Qt::Key_F3:
  114. return kVK_F3;
  115. case Qt::Key_F8:
  116. return kVK_F8;
  117. case Qt::Key_F9:
  118. return kVK_F9;
  119. case Qt::Key_F11:
  120. return kVK_F11;
  121. case Qt::Key_F13:
  122. return kVK_F13;
  123. case Qt::Key_F16:
  124. return kVK_F16;
  125. case Qt::Key_F14:
  126. return kVK_F14;
  127. case Qt::Key_F10:
  128. return kVK_F10;
  129. case Qt::Key_F12:
  130. return kVK_F12;
  131. case Qt::Key_F15:
  132. return kVK_F15;
  133. case Qt::Key_Help:
  134. return kVK_Help;
  135. case Qt::Key_Home:
  136. return kVK_Home;
  137. case Qt::Key_PageUp:
  138. return kVK_PageUp;
  139. case Qt::Key_Delete:
  140. return kVK_ForwardDelete;
  141. case Qt::Key_F4:
  142. return kVK_F4;
  143. case Qt::Key_End:
  144. return kVK_End;
  145. case Qt::Key_F2:
  146. return kVK_F2;
  147. case Qt::Key_PageDown:
  148. return kVK_PageDown;
  149. case Qt::Key_F1:
  150. return kVK_F1;
  151. case Qt::Key_Left:
  152. return kVK_LeftArrow;
  153. case Qt::Key_Right:
  154. return kVK_RightArrow;
  155. case Qt::Key_Down:
  156. return kVK_DownArrow;
  157. case Qt::Key_Up:
  158. return kVK_UpArrow;
  159. default:
  160. ;
  161. }
  162. if (key == Qt::Key_Escape) ch = 27;
  163. else if (key == Qt::Key_Return) ch = 13;
  164. else if (key == Qt::Key_Enter) ch = 3;
  165. else if (key == Qt::Key_Tab) ch = 9;
  166. else ch = key;
  167. CFDataRef currentLayoutData;
  168. TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
  169. if (currentKeyboard == NULL)
  170. return 0;
  171. currentLayoutData = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
  172. CFRelease(currentKeyboard);
  173. if (currentLayoutData == NULL)
  174. return 0;
  175. UCKeyboardLayout* header = (UCKeyboardLayout*)CFDataGetBytePtr(currentLayoutData);
  176. UCKeyboardTypeHeader* table = header->keyboardTypeList;
  177. uint8_t *data = (uint8_t*)header;
  178. // God, would a little documentation for this shit kill you...
  179. for (quint32 i=0; i < header->keyboardTypeCount; i++)
  180. {
  181. UCKeyStateRecordsIndex* stateRec = 0;
  182. if (table[i].keyStateRecordsIndexOffset != 0)
  183. {
  184. stateRec = reinterpret_cast<UCKeyStateRecordsIndex*>(data + table[i].keyStateRecordsIndexOffset);
  185. if (stateRec->keyStateRecordsIndexFormat != kUCKeyStateRecordsIndexFormat) stateRec = 0;
  186. }
  187. UCKeyToCharTableIndex* charTable = reinterpret_cast<UCKeyToCharTableIndex*>(data + table[i].keyToCharTableIndexOffset);
  188. if (charTable->keyToCharTableIndexFormat != kUCKeyToCharTableIndexFormat) continue;
  189. for (quint32 j=0; j < charTable->keyToCharTableCount; j++)
  190. {
  191. UCKeyOutput* keyToChar = reinterpret_cast<UCKeyOutput*>(data + charTable->keyToCharTableOffsets[j]);
  192. for (quint32 k=0; k < charTable->keyToCharTableSize; k++)
  193. {
  194. if (keyToChar[k] & kUCKeyOutputTestForIndexMask)
  195. {
  196. long idx = keyToChar[k] & kUCKeyOutputGetIndexMask;
  197. if (stateRec && idx < stateRec->keyStateRecordCount)
  198. {
  199. UCKeyStateRecord* rec = reinterpret_cast<UCKeyStateRecord*>(data + stateRec->keyStateRecordOffsets[idx]);
  200. if (rec->stateZeroCharData == ch) return k;
  201. }
  202. }
  203. else if (!(keyToChar[k] & kUCKeyOutputSequenceIndexMask) && keyToChar[k] < 0xFFFE)
  204. {
  205. if (keyToChar[k] == ch) return k;
  206. }
  207. } // for k
  208. } // for j
  209. } // for i
  210. return 0;
  211. }
  212. bool QxtGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods)
  213. {
  214. if (!qxt_mac_handler_installed)
  215. {
  216. EventTypeSpec t;
  217. t.eventClass = kEventClassKeyboard;
  218. t.eventKind = kEventHotKeyPressed;
  219. InstallApplicationEventHandler(&qxt_mac_handle_hot_key, 1, &t, NULL, NULL);
  220. }
  221. EventHotKeyID keyID;
  222. keyID.signature = 'cute';
  223. keyID.id = ++hotKeySerial;
  224. EventHotKeyRef ref = 0;
  225. bool rv = !RegisterEventHotKey(nativeKey, nativeMods, keyID, GetApplicationEventTarget(), 0, &ref);
  226. if (rv)
  227. {
  228. keyIDs.insert(Identifier(nativeMods, nativeKey), keyID.id);
  229. keyRefs.insert(keyID.id, ref);
  230. }
  231. return rv;
  232. }
  233. bool QxtGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods)
  234. {
  235. Identifier id(nativeMods, nativeKey);
  236. if (!keyIDs.contains(id)) return false;
  237. EventHotKeyRef ref = keyRefs.take(keyIDs[id]);
  238. keyIDs.remove(id);
  239. return !UnregisterEventHotKey(ref);
  240. }