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