TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qglobalshortcut_mac.cpp
Go to the documentation of this file.
1 #include <Carbon/Carbon.h>
2 #include "qglobalshortcut_p.h"
3 #include <QMap>
4 #include <QApplication>
5 
6 typedef QPair<uint, uint> Identifier;
7 static QMap<quint32, EventHotKeyRef> keyRefs;
8 static QHash<Identifier, quint32> keyIDs;
9 static quint32 hotKeySerial = 0;
10 static bool q_mac_handler_installed = false;
11 
12 OSStatus q_mac_handle_hot_key(EventHandlerCallRef nextHandler, EventRef event, void *data)
13 {
14  // pass event to the app event filter
15  Q_UNUSED(data);
16  qApp->macEventFilter(nextHandler, event);
17  return noErr;
18 }
19 
20 #if !TTK_QT_VERSION_CHECK(5,0,0)
22 #else
23 bool QGlobalShortcutPrivate::nativeEventFilter(const QByteArray &, void *message, qint *)
24 #endif
25 {
26  EventRef event = (EventRef) message;
27  if(GetEventClass(event) == kEventClassKeyboard && GetEventKind(event) == kEventHotKeyPressed)
28  {
29  EventHotKeyID keyID;
30  GetEventParameter(event, kEventParamDirectObject, typeEventHotKeyID, nullptr, sizeof(keyID), nullptr, &keyID);
31  Identifier id = keyIDs.key(keyID.id);
32  activateShortcut(id.second, id.first);
33  }
34  return false;
35 }
36 
37 quint32 QGlobalShortcutPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
38 {
39  quint32 native = 0;
40  if(modifiers & Qt::ShiftModifier)
41  native |= shiftKey;
42  if(modifiers & Qt::ControlModifier)
43  native |= cmdKey;
44  if(modifiers & Qt::AltModifier)
45  native |= optionKey;
46  if(modifiers & Qt::MetaModifier)
47  native |= controlKey;
48  if(modifiers & Qt::KeypadModifier)
49  native |= kEventKeyModifierNumLockMask;
50  return native;
51 }
52 
54 {
55  UTF16Char ch;
56  // Constants found in NSEvent.h from AppKit.framework
57  if(key == Qt::Key_Up) ch = 0xF700;
58  else if(key == Qt::Key_Down) ch = 0xF701;
59  else if(key == Qt::Key_Left) ch = 0xF702;
60  else if(key == Qt::Key_Right) ch = 0xF703;
61  else if(key >= Qt::Key_F1 && key <= Qt::Key_F35)
62  ch = key - Qt::Key_F1 + 0xF704;
63  else if(key == Qt::Key_Insert) ch = 0xF727;
64  else if(key == Qt::Key_Delete) ch = 0xF728;
65  else if(key == Qt::Key_Home) ch = 0xF729;
66  else if(key == Qt::Key_End) ch = 0xF72B;
67  else if(key == Qt::Key_PageUp) ch = 0xF72C;
68  else if(key == Qt::Key_PageDown) ch = 0xF72D;
69  else if(key == Qt::Key_Print) ch = 0xF72E;
70  else if(key == Qt::Key_ScrollLock) ch = 0xF72F;
71  else if(key == Qt::Key_Pause) ch = 0xF730;
72  else if(key == Qt::Key_SysReq) ch = 0xF731;
73  else if(key == Qt::Key_Stop) ch = 0xF734;
74  else if(key == Qt::Key_Menu) ch = 0xF735;
75  else if(key == Qt::Key_Select) ch = 0xF741;
76  else if(key == Qt::Key_Execute) ch = 0xF742;
77  else if(key == Qt::Key_Help) ch = 0xF746;
78  else if(key == Qt::Key_Mode_switch) ch = 0xF747;
79  else if(key == Qt::Key_Escape) ch = 27;
80  else if(key == Qt::Key_Return) ch = 13;
81  else if(key == Qt::Key_Enter) ch = 3;
82  else if(key == Qt::Key_Tab) ch = 9;
83  else ch = key;
84 
85  KeyboardLayoutRef layout;
86  KeyboardLayoutKind layoutKind;
87  KLGetCurrentKeyboardLayout(&layout);
88  KLGetKeyboardLayoutProperty(layout, kKLKind, TTKConstCast(const void**, TTKReinterpretCast(void**, &layoutKind)));
89 
90  if(layoutKind == kKLKCHRKind)
91  { // no Unicode available
92  if(ch > 255) return 0;
93 
94  char *data;
95  KLGetKeyboardLayoutProperty(layout, kKLKCHRData, TTKConstCast(const void**, TTKReinterpretCast(void**, &data)));
96  int ct = *TTKReinterpretCast(short*, data + 258);
97  for(int i = 0; i < ct; ++i)
98  {
99  char *keyTable = data + 260 + 128 * i;
100  for(int j = 0; j < 128; ++j)
101  {
102  if(keyTable[j] == ch) return j;
103  }
104  }
105 
106  return 0;
107  }
108 
109  char *data;
110  KLGetKeyboardLayoutProperty(layout, kKLuchrData, TTKConstCast(const void**, TTKReinterpretCast(void**, &data)));
111  UCKeyboardLayout *header = TTKReinterpretCast(UCKeyboardLayout*, data);
112  UCKeyboardTypeHeader *table = header->keyboardTypeList;
113 
114  for(quint32 i=0; i < header->keyboardTypeCount; i++)
115  {
116  UCKeyStateRecordsIndex *stateRec = 0;
117  if(table[i].keyStateRecordsIndexOffset != 0)
118  {
119  stateRec = TTKReinterpretCast(UCKeyStateRecordsIndex*, data + table[i].keyStateRecordsIndexOffset);
120  if(stateRec->keyStateRecordsIndexFormat != kUCKeyStateRecordsIndexFormat) stateRec = 0;
121  }
122 
123  UCKeyToCharTableIndex *charTable = TTKReinterpretCast(UCKeyToCharTableIndex*, data + table[i].keyToCharTableIndexOffset);
124  if(charTable->keyToCharTableIndexFormat != kUCKeyToCharTableIndexFormat)
125  {
126  continue;
127  }
128 
129  for(quint32 j=0; j < charTable->keyToCharTableCount; j++)
130  {
131  UCKeyOutput *keyToChar = TTKReinterpretCast(UCKeyOutput*, data + charTable->keyToCharTableOffsets[j]);
132  for(quint32 k=0; k < charTable->keyToCharTableSize; k++)
133  {
134  if(keyToChar[k] & kUCKeyOutputTestForIndexMask)
135  {
136  long idx = keyToChar[k] & kUCKeyOutputGetIndexMask;
137  if(stateRec && idx < stateRec->keyStateRecordCount)
138  {
139  UCKeyStateRecord *rec = TTKReinterpretCast(UCKeyStateRecord*, data + stateRec->keyStateRecordOffsets[idx]);
140  if(rec->stateZeroCharData == ch)
141  {
142  return k;
143  }
144  }
145  }
146  else if(!(keyToChar[k] & kUCKeyOutputSequenceIndexMask) && keyToChar[k] < 0xFFFE)
147  {
148  if(keyToChar[k] == ch)
149  {
150  return k;
151  }
152  }
153  } // for k
154  } // for j
155  } // for i
156 
157  return 0;
158 }
159 
160 bool QGlobalShortcutPrivate::registerShortcut(quint32 nativeKey, quint32 nativeMods)
161 {
163  {
164  EventTypeSpec t;
165  t.eventClass = kEventClassKeyboard;
166  t.eventKind = kEventHotKeyPressed;
167  InstallApplicationEventHandler(&q_mac_handle_hot_key, 1, &t, nullptr, nullptr);
168  }
169 
170  EventHotKeyID keyID;
171  keyID.signature = 'cute';
172  keyID.id = ++hotKeySerial;
173 
174  EventHotKeyRef ref = 0;
175  const bool rv = !RegisterEventHotKey(nativeKey, nativeMods, keyID, GetApplicationEventTarget(), 0, &ref);
176  if(rv)
177  {
178  keyIDs.insert(Identifier(nativeMods, nativeKey), keyID.id);
179  keyRefs.insert(keyID.id, ref);
180  }
181 
182  return rv;
183 }
184 
185 bool QGlobalShortcutPrivate::unregisterShortcut(quint32 nativeKey, quint32 nativeMods)
186 {
187  Identifier id(nativeMods, nativeKey);
188  if(!keyIDs.contains(id))
189  return false;
190 
191  EventHotKeyRef ref = keyRefs.take(keyIDs[id]);
192  keyIDs.remove(id);
193  return !UnregisterEventHotKey(ref);
194 }
static quint32 nativeKeycode(Qt::Key keycode)
#define TTKConstCast(x, y)
Definition: ttkglobal.h:213
static bool q_mac_handler_installed
static QMap< quint32, EventHotKeyRef > keyRefs
static QHash< Identifier, quint32 > keyIDs
static quint32 nativeModifiers(Qt::KeyboardModifiers modifiers)
static constexpr wchar_t key[]
OSStatus q_mac_handle_hot_key(EventHandlerCallRef nextHandler, EventRef event, void *data)
#define qint
Definition: ttkqtglobal.h:180
static bool unregisterShortcut(quint32 nativeKey, quint32 nativeMods)
static bool eventFilter(void *message)
static bool registerShortcut(quint32 nativeKey, quint32 nativeMods)
QPair< uint, uint > Identifier
static quint32 hotKeySerial
#define TTKReinterpretCast(x, y)
Definition: ttkglobal.h:225