forked from flit/MidiKeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiKeyMap.m
280 lines (243 loc) · 6.35 KB
/
MidiKeyMap.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// MidiKeyMap.mm
// MidiKeys
//
// Created by Chris Reed on Sat Oct 26 2002.
// Copyright (c) 2002 Chris Reed. All rights reserved.
//
#import "MidiKeyMap.h"
#import <Carbon/Carbon.h>
@interface MidiKeyMap (InternalMethods)
- (void)constructMaps;
@end
@implementation MidiKeyMap
- initWithDefinition:(id)def
{
self = [super init];
if (self)
{
mDefinition = [def retain];
mRanges = [[mDefinition objectForKey:@"Ranges"] retain];
[self constructMaps];
}
return self;
}
- (void)dealloc
{
// can't leave hotkeys sitting around without a way to remove them
if (mHotKeysAreRegistered)
{
[self unregisterHotKeys];
}
[mRegisteredHotKeys release];
[mRanges release];
[mDefinition release];
[super dealloc];
}
- (void)constructMaps
{
int i;
for (i=0; i < MAX_KEY_CODES; ++i)
{
mKeyCodeToNoteMap[i] = -1;
}
for (i=0; i < MAX_MIDI_NOTES; ++i)
{
mNoteToKeyCodeMap[i] = -1;
}
id range;
for (range in mRanges)
{
@try
{
NSArray *rangeKeys = [range objectForKey:@"KeyCodes"];
int rangeStartNote = [[range objectForKey:@"FirstMidiNote"] intValue];
int numKeyCodes = [rangeKeys count];
for (i=0; i < numKeyCodes; ++i)
{
id thisKey = [rangeKeys objectAtIndex:i];
int keycode = [thisKey intValue];
int note = rangeStartNote + i;
mKeyCodeToNoteMap[keycode] = note;
mNoteToKeyCodeMap[note] = keycode;
}
}
@catch (NSException * e)
{
// a key was missing from the range dictionary (or something)
NSLog(@"invalid range definition");
// move on to the next range
// @todo remove the offending range from the key map
}
}
}
//! @return The MIDI note number that corresponds to the given key code.
//! @retval -1 There is no mapping for the given key code.
- (int)midiNoteForKeyCode:(int)keycode
{
if (keycode < 0 || keycode >= MAX_KEY_CODES)
{
return -1;
}
else
{
return mKeyCodeToNoteMap[keycode];
}
}
// look up the hot key in our dictionary
- (int)midiNoteForHotKeyWithIdentifier:(UInt32)identifier
{
// return -1 if there are no hotkeys installed
if (!mHotKeysAreRegistered || [mRegisteredHotKeys count]==0)
return -1;
int note = [[mRegisteredHotKeys objectForKey:[NSValue valueWithPointer:(void *)identifier]] intValue];
return note;
}
- (int)keyCodeForMidiNote:(int)note
{
if (note < 0 || note >= MAX_MIDI_NOTES)
{
return -1;
}
else
{
return mNoteToKeyCodeMap[note];
}
}
//! @return An NSString containing the character value. Will be an empty string if
//! the key map doesn't have an entry for the given note value.
- (NSString *)characterForMidiNote:(int)midiNote
{
int keyCode = [self keyCodeForMidiNote:midiNote];
// Get current key layout selected in Keyboard menu.
KeyboardLayoutRef layout;
// TISInputSourceRef inputSource;
OSStatus status;
// inputSource = TISCopyCurrentKeyboardInputSource();
status = KLGetCurrentKeyboardLayout(&layout);
if (status)
{
return nil;
}
// Get the type of available layout data.
KeyboardLayoutKind layoutKind;
status = KLGetKeyboardLayoutProperty(layout, kKLKind, (const void **)&layoutKind);
if (status)
{
return nil;
}
UInt32 deadKeyState = 0;
switch (layoutKind)
{
// Use the Unicode layout if available.
case kKLKCHRuchrKind:
case kKLuchrKind:
{
// Get the Unicode 'uchr' key layout data.
UCKeyboardLayout * uchrData;
status = KLGetKeyboardLayoutProperty(layout, kKLuchrData, (const void **)&uchrData);
if (status)
{
return nil;
}
// Get Unicode characters for this keycode.
UniChar keyChars[16];
UniCharCount keyCharsCount;
status = UCKeyTranslate(uchrData, keyCode, kUCKeyActionDisplay, 0, LMGetKbdType(), kUCKeyTranslateNoDeadKeysMask, &deadKeyState, sizeof(keyChars), &keyCharsCount, keyChars);
if (status)
{
return nil;
}
// Return NSString with the Unicode characters.
return [NSString stringWithCharacters:(const unichar *)&keyChars length:keyCharsCount];
}
// Otherwise fall back to the old format.
case kKLKCHRKind:
{
// Get the 'KCHR' resource data.
void * kchrData;
status = KLGetKeyboardLayoutProperty(layout, kKLKCHRData, (const void **)&kchrData);
if (status)
{
return nil;
}
// Put a 1 in bit 7 of the keycode param to indicate a down stroke.
UInt32 resultChars = KeyTranslate(kchrData, (keyCode & 0x3f) | (1 << 7), &deadKeyState);
if (status)
{
return nil;
}
// Return the key char as a string.
char keyCharString[2] = {0};
keyCharString[0] = resultChars & 0xff;
return [NSString stringWithUTF8String:keyCharString];
}
}
return nil;
}
- (void)registerHotKeysWithModifiers:(int)modifiers;
{
// must have a modifier set to install hotkeys
if (modifiers == 0)
{
return;
}
OSStatus err;
EventHotKeyID hotKeyID = { 0, 0 };
EventHotKeyRef hotKeyRef;
[mRegisteredHotKeys release];
mRegisteredHotKeys = [[NSMutableDictionary dictionary] retain];
id range;
for (range in mRanges)
{
@try
{
NSArray *rangeKeys = [range objectForKey:@"KeyCodes"];
int rangeStartNote = [[range objectForKey:@"FirstMidiNote"] intValue];
int numKeyCodes = [rangeKeys count];
int i;
for (i=0; i < numKeyCodes; ++i)
{
id thisKey = [rangeKeys objectAtIndex:i];
int midiNote = rangeStartNote + i;
err = RegisterEventHotKey([thisKey intValue], modifiers, hotKeyID, GetApplicationEventTarget(), 0, &hotKeyRef);
if (err)
{
NSLog(@"error %ld installing hotkey (keycode = %ld)", err, [thisKey intValue]);
continue;
}
mHotKeysAreRegistered = YES;
// save the hotKeyRef as the key in a dictionary, with its keycode as the value
[mRegisteredHotKeys setObject:[NSNumber numberWithInt:midiNote] forKey:[NSValue valueWithPointer:hotKeyRef]];
}
}
@catch (NSException * e)
{
// a key was missing from the range dictionary (or something)
NSLog(@"invalid range definition");
// move on to the next range
// XXX remove the offending range from the key map
}
}
}
- (void)unregisterHotKeys
{
if (!mHotKeysAreRegistered || [mRegisteredHotKeys count]==0)
{
return;
}
id iterator;
for (iterator in mRegisteredHotKeys)
{
EventHotKeyRef hotKeyRef = (EventHotKeyRef)[iterator pointerValue];
OSStatus err = UnregisterEventHotKey(hotKeyRef);
if (err)
{
NSLog(@"err %ld unregistering hot key %p", err, hotKeyRef);
}
}
// clear hot keys dictionary
[mRegisteredHotKeys removeAllObjects];
mHotKeysAreRegistered = NO;
}
@end