Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
737 lines (641 loc) · 24.1 KB

SDL_cocoakeyboard.m

File metadata and controls

737 lines (641 loc) · 24.1 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Jul 8, 2010
Jul 8, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_cocoavideo.h"
#include "../../events/SDL_keyboard_c.h"
Feb 7, 2008
Feb 7, 2008
27
#include "../../events/scancodes_darwin.h"
Aug 19, 2007
Aug 19, 2007
29
30
#include <Carbon/Carbon.h>
Sep 19, 2009
Sep 19, 2009
31
32
//#define DEBUG_IME NSLog
#define DEBUG_IME
Jul 30, 2006
Jul 30, 2006
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
#ifndef NX_DEVICERCTLKEYMASK
#define NX_DEVICELCTLKEYMASK 0x00000001
#endif
#ifndef NX_DEVICELSHIFTKEYMASK
#define NX_DEVICELSHIFTKEYMASK 0x00000002
#endif
#ifndef NX_DEVICERSHIFTKEYMASK
#define NX_DEVICERSHIFTKEYMASK 0x00000004
#endif
#ifndef NX_DEVICELCMDKEYMASK
#define NX_DEVICELCMDKEYMASK 0x00000008
#endif
#ifndef NX_DEVICERCMDKEYMASK
#define NX_DEVICERCMDKEYMASK 0x00000010
#endif
#ifndef NX_DEVICELALTKEYMASK
#define NX_DEVICELALTKEYMASK 0x00000020
#endif
#ifndef NX_DEVICERALTKEYMASK
#define NX_DEVICERALTKEYMASK 0x00000040
#endif
#ifndef NX_DEVICERCTLKEYMASK
#define NX_DEVICERCTLKEYMASK 0x00002000
#endif
Sep 19, 2009
Sep 19, 2009
59
@interface SDLTranslatorResponder : NSView <NSTextInput>
Dec 29, 2007
Dec 29, 2007
60
{
Sep 19, 2009
Sep 19, 2009
61
62
63
64
NSString *_markedText;
NSRange _markedRange;
NSRange _selectedRange;
SDL_Rect _inputRect;
Dec 29, 2007
Dec 29, 2007
65
66
}
- (void) doCommandBySelector:(SEL)myselector;
Sep 19, 2009
Sep 19, 2009
67
- (void) setInputRect:(SDL_Rect *) rect;
Dec 29, 2007
Dec 29, 2007
68
69
70
@end
@implementation SDLTranslatorResponder
Sep 19, 2009
Sep 19, 2009
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
- (void) setInputRect:(SDL_Rect *) rect
{
_inputRect = *rect;
}
- (void) insertText:(id) aString
{
const char *str;
DEBUG_IME(@"insertText: %@", aString);
/* Could be NSString or NSAttributedString, so we have
* to test and convert it before return as SDL event */
if ([aString isKindOfClass: [NSAttributedString class]])
str = [[aString string] UTF8String];
else
str = [aString UTF8String];
May 10, 2010
May 10, 2010
90
SDL_SendKeyboardText(str);
Sep 19, 2009
Sep 19, 2009
91
92
93
94
}
- (void) doCommandBySelector:(SEL) myselector
{
Jan 19, 2010
Jan 19, 2010
95
96
97
// No need to do anything since we are not using Cocoa
// selectors to handle special keys, instead we use SDL
// key events to do the same job.
Sep 19, 2009
Sep 19, 2009
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
}
- (BOOL) hasMarkedText
{
return _markedText != nil;
}
- (NSRange) markedRange
{
return _markedRange;
}
- (NSRange) selectedRange
{
return _selectedRange;
}
- (void) setMarkedText:(id) aString
selectedRange:(NSRange) selRange
{
if ([aString isKindOfClass: [NSAttributedString class]])
aString = [aString string];
if ([aString length] == 0)
{
[self unmarkText];
return;
}
if (_markedText != aString)
{
[_markedText release];
_markedText = [aString retain];
}
_selectedRange = selRange;
_markedRange = NSMakeRange(0, [aString length]);
May 10, 2010
May 10, 2010
136
SDL_SendEditingText([aString UTF8String],
Apr 16, 2010
Apr 16, 2010
137
selRange.location, selRange.length);
Sep 19, 2009
Sep 19, 2009
138
139
140
141
142
143
144
145
146
DEBUG_IME(@"setMarkedText: %@, (%d, %d)", _markedText,
selRange.location, selRange.length);
}
- (void) unmarkText
{
[_markedText release];
_markedText = nil;
Aug 12, 2010
Aug 12, 2010
147
148
SDL_SendEditingText("", 0, 0);
Sep 19, 2009
Sep 19, 2009
149
150
151
152
}
- (NSRect) firstRectForCharacterRange: (NSRange) theRange
{
Apr 16, 2010
Apr 16, 2010
153
154
155
NSWindow *window = [self window];
NSRect contentRect = [window contentRectForFrameRect: [window frame]];
float windowHeight = contentRect.size.height;
Sep 19, 2009
Sep 19, 2009
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y - _inputRect.h,
_inputRect.w, _inputRect.h);
DEBUG_IME(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
theRange.location, theRange.length, windowHeight,
NSStringFromRect(rect));
rect.origin = [[self window] convertBaseToScreen: rect.origin];
return rect;
}
- (NSAttributedString *) attributedSubstringFromRange: (NSRange) theRange
{
DEBUG_IME(@"attributedSubstringFromRange: (%d, %d)", theRange.location, theRange.length);
return nil;
}
Dec 5, 2009
Dec 5, 2009
173
/* Needs long instead of NSInteger for compilation on Mac OS X 10.4 */
Dec 1, 2010
Dec 1, 2010
174
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
Dec 5, 2009
Dec 5, 2009
175
- (long) conversationIdentifier
Dec 1, 2010
Dec 1, 2010
176
177
178
#else
- (NSInteger) conversationIdentifier
#endif
Sep 19, 2009
Sep 19, 2009
179
{
Dec 5, 2009
Dec 5, 2009
180
return (long) self;
Sep 19, 2009
Sep 19, 2009
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}
// This method returns the index for character that is
// nearest to thePoint. thPoint is in screen coordinate system.
- (NSUInteger) characterIndexForPoint:(NSPoint) thePoint
{
DEBUG_IME(@"characterIndexForPoint: (%g, %g)", thePoint.x, thePoint.y);
return 0;
}
// This method is the key to attribute extension.
// We could add new attributes through this method.
// NSInputServer examines the return value of this
// method & constructs appropriate attributed string.
- (NSArray *) validAttributesForMarkedText
{
return [NSArray array];
}
Dec 29, 2007
Dec 29, 2007
200
201
@end
Jul 30, 2006
Jul 30, 2006
202
203
204
205
/* This is the original behavior, before support was added for
* differentiating between left and right versions of the keys.
*/
static void
May 10, 2010
May 10, 2010
206
DoUnsidedModifiers(unsigned short scancode,
Jul 30, 2006
Jul 30, 2006
207
208
unsigned int oldMods, unsigned int newMods)
{
Feb 5, 2008
Feb 5, 2008
209
210
211
212
213
214
215
const int mapping[] = {
SDL_SCANCODE_CAPSLOCK,
SDL_SCANCODE_LSHIFT,
SDL_SCANCODE_LCTRL,
SDL_SCANCODE_LALT,
SDL_SCANCODE_LGUI
};
Jul 30, 2006
Jul 30, 2006
216
217
218
219
220
221
222
223
224
225
226
227
unsigned int i, bit;
/* Iterate through the bits, testing each against the current modifiers */
for (i = 0, bit = NSAlphaShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) {
unsigned int oldMask, newMask;
oldMask = oldMods & bit;
newMask = newMods & bit;
if (oldMask && oldMask != newMask) { /* modifier up event */
/* If this was Caps Lock, we need some additional voodoo to make SDL happy */
if (bit == NSAlphaShiftKeyMask) {
Jul 22, 2010
Jul 22, 2010
228
SDL_SendKeyboardKey(SDL_PRESSED, mapping[i]);
Jul 30, 2006
Jul 30, 2006
229
}
Jul 22, 2010
Jul 22, 2010
230
SDL_SendKeyboardKey(SDL_RELEASED, mapping[i]);
Jul 30, 2006
Jul 30, 2006
231
} else if (newMask && oldMask != newMask) { /* modifier down event */
Jul 22, 2010
Jul 22, 2010
232
SDL_SendKeyboardKey(SDL_PRESSED, mapping[i]);
Jul 30, 2006
Jul 30, 2006
233
234
/* If this was Caps Lock, we need some additional voodoo to make SDL happy */
if (bit == NSAlphaShiftKeyMask) {
Jul 22, 2010
Jul 22, 2010
235
SDL_SendKeyboardKey(SDL_RELEASED, mapping[i]);
Jul 30, 2006
Jul 30, 2006
236
237
238
239
240
241
242
243
244
245
}
}
}
}
/* This is a helper function for HandleModifierSide. This
* function reverts back to behavior before the distinction between
* sides was made.
*/
static void
May 10, 2010
May 10, 2010
246
HandleNonDeviceModifier(unsigned int device_independent_mask,
Jul 30, 2006
Jul 30, 2006
247
248
unsigned int oldMods,
unsigned int newMods,
Feb 7, 2011
Feb 7, 2011
249
SDL_Scancode scancode)
Jul 30, 2006
Jul 30, 2006
250
251
252
253
254
255
256
257
258
259
{
unsigned int oldMask, newMask;
/* Isolate just the bits we care about in the depedent bits so we can
* figure out what changed
*/
oldMask = oldMods & device_independent_mask;
newMask = newMods & device_independent_mask;
if (oldMask && oldMask != newMask) {
Jul 22, 2010
Jul 22, 2010
260
SDL_SendKeyboardKey(SDL_RELEASED, scancode);
Jul 30, 2006
Jul 30, 2006
261
} else if (newMask && oldMask != newMask) {
Jul 22, 2010
Jul 22, 2010
262
SDL_SendKeyboardKey(SDL_PRESSED, scancode);
Jul 30, 2006
Jul 30, 2006
263
264
265
266
267
268
269
}
}
/* This is a helper function for HandleModifierSide.
* This function sets the actual SDL_PrivateKeyboard event.
*/
static void
May 10, 2010
May 10, 2010
270
HandleModifierOneSide(unsigned int oldMods, unsigned int newMods,
Feb 7, 2011
Feb 7, 2011
271
SDL_Scancode scancode,
Jul 30, 2006
Jul 30, 2006
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
unsigned int sided_device_dependent_mask)
{
unsigned int old_dep_mask, new_dep_mask;
/* Isolate just the bits we care about in the depedent bits so we can
* figure out what changed
*/
old_dep_mask = oldMods & sided_device_dependent_mask;
new_dep_mask = newMods & sided_device_dependent_mask;
/* We now know that this side bit flipped. But we don't know if
* it went pressed to released or released to pressed, so we must
* find out which it is.
*/
if (new_dep_mask && old_dep_mask != new_dep_mask) {
Jul 22, 2010
Jul 22, 2010
287
SDL_SendKeyboardKey(SDL_PRESSED, scancode);
Jul 30, 2006
Jul 30, 2006
288
} else {
Jul 22, 2010
Jul 22, 2010
289
SDL_SendKeyboardKey(SDL_RELEASED, scancode);
Jul 30, 2006
Jul 30, 2006
290
291
292
293
294
295
296
297
}
}
/* This is a helper function for DoSidedModifiers.
* This function will figure out if the modifier key is the left or right side,
* e.g. left-shift vs right-shift.
*/
static void
May 10, 2010
May 10, 2010
298
HandleModifierSide(int device_independent_mask,
Jul 30, 2006
Jul 30, 2006
299
unsigned int oldMods, unsigned int newMods,
Feb 7, 2011
Feb 7, 2011
300
301
SDL_Scancode left_scancode,
SDL_Scancode right_scancode,
Jul 30, 2006
Jul 30, 2006
302
303
304
305
306
307
308
309
310
311
312
313
314
unsigned int left_device_dependent_mask,
unsigned int right_device_dependent_mask)
{
unsigned int device_dependent_mask = (left_device_dependent_mask |
right_device_dependent_mask);
unsigned int diff_mod;
/* On the basis that the device independent mask is set, but there are
* no device dependent flags set, we'll assume that we can't detect this
* keyboard and revert to the unsided behavior.
*/
if ((device_dependent_mask & newMods) == 0) {
/* Revert to the old behavior */
May 10, 2010
May 10, 2010
315
HandleNonDeviceModifier(device_independent_mask, oldMods, newMods, left_scancode);
Jul 30, 2006
Jul 30, 2006
316
317
318
319
320
321
322
323
324
325
326
327
return;
}
/* XOR the previous state against the new state to see if there's a change */
diff_mod = (device_dependent_mask & oldMods) ^
(device_dependent_mask & newMods);
if (diff_mod) {
/* A change in state was found. Isolate the left and right bits
* to handle them separately just in case the values can simulataneously
* change or if the bits don't both exist.
*/
if (left_device_dependent_mask & diff_mod) {
May 10, 2010
May 10, 2010
328
HandleModifierOneSide(oldMods, newMods, left_scancode, left_device_dependent_mask);
Jul 30, 2006
Jul 30, 2006
329
330
}
if (right_device_dependent_mask & diff_mod) {
May 10, 2010
May 10, 2010
331
HandleModifierOneSide(oldMods, newMods, right_scancode, right_device_dependent_mask);
Jul 30, 2006
Jul 30, 2006
332
333
334
335
336
337
338
339
340
341
}
}
}
/* This is a helper function for DoSidedModifiers.
* This function will release a key press in the case that
* it is clear that the modifier has been released (i.e. one side
* can't still be down).
*/
static void
May 10, 2010
May 10, 2010
342
ReleaseModifierSide(unsigned int device_independent_mask,
Jul 30, 2006
Jul 30, 2006
343
unsigned int oldMods, unsigned int newMods,
Feb 7, 2011
Feb 7, 2011
344
345
SDL_Scancode left_scancode,
SDL_Scancode right_scancode,
Jul 30, 2006
Jul 30, 2006
346
347
348
349
350
351
352
353
354
355
356
357
358
359
unsigned int left_device_dependent_mask,
unsigned int right_device_dependent_mask)
{
unsigned int device_dependent_mask = (left_device_dependent_mask |
right_device_dependent_mask);
/* On the basis that the device independent mask is set, but there are
* no device dependent flags set, we'll assume that we can't detect this
* keyboard and revert to the unsided behavior.
*/
if ((device_dependent_mask & oldMods) == 0) {
/* In this case, we can't detect the keyboard, so use the left side
* to represent both, and release it.
*/
Jul 22, 2010
Jul 22, 2010
360
SDL_SendKeyboardKey(SDL_RELEASED, left_scancode);
Jul 30, 2006
Jul 30, 2006
361
362
363
364
365
366
367
368
369
370
return;
}
/*
* This could have been done in an if-else case because at this point,
* we know that all keys have been released when calling this function.
* But I'm being paranoid so I want to handle each separately,
* so I hope this doesn't cause other problems.
*/
if ( left_device_dependent_mask & oldMods ) {
Jul 22, 2010
Jul 22, 2010
371
SDL_SendKeyboardKey(SDL_RELEASED, left_scancode);
Jul 30, 2006
Jul 30, 2006
372
373
}
if ( right_device_dependent_mask & oldMods ) {
Jul 22, 2010
Jul 22, 2010
374
SDL_SendKeyboardKey(SDL_RELEASED, right_scancode);
Jul 30, 2006
Jul 30, 2006
375
376
377
378
379
380
381
}
}
/* This is a helper function for DoSidedModifiers.
* This function handles the CapsLock case.
*/
static void
May 10, 2010
May 10, 2010
382
HandleCapsLock(unsigned short scancode,
Jul 30, 2006
Jul 30, 2006
383
384
385
386
387
388
389
390
unsigned int oldMods, unsigned int newMods)
{
unsigned int oldMask, newMask;
oldMask = oldMods & NSAlphaShiftKeyMask;
newMask = newMods & NSAlphaShiftKeyMask;
if (oldMask != newMask) {
Jul 22, 2010
Jul 22, 2010
391
392
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
Jul 30, 2006
Jul 30, 2006
393
}
Jul 30, 2006
Jul 30, 2006
394
395
396
397
398
oldMask = oldMods & NSNumericPadKeyMask;
newMask = newMods & NSNumericPadKeyMask;
if (oldMask != newMask) {
Jul 22, 2010
Jul 22, 2010
399
400
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
Jul 30, 2006
Jul 30, 2006
401
}
Jul 30, 2006
Jul 30, 2006
402
403
404
405
406
407
}
/* This function will handle the modifier keys and also determine the
* correct side of the key.
*/
static void
May 10, 2010
May 10, 2010
408
DoSidedModifiers(unsigned short scancode,
Jul 30, 2006
Jul 30, 2006
409
410
411
unsigned int oldMods, unsigned int newMods)
{
/* Set up arrays for the key syms for the left and right side. */
Feb 7, 2011
Feb 7, 2011
412
const SDL_Scancode left_mapping[] = {
Feb 5, 2008
Feb 5, 2008
413
414
415
416
417
SDL_SCANCODE_LSHIFT,
SDL_SCANCODE_LCTRL,
SDL_SCANCODE_LALT,
SDL_SCANCODE_LGUI
};
Feb 7, 2011
Feb 7, 2011
418
const SDL_Scancode right_mapping[] = {
Feb 5, 2008
Feb 5, 2008
419
420
421
422
423
SDL_SCANCODE_RSHIFT,
SDL_SCANCODE_RCTRL,
SDL_SCANCODE_RALT,
SDL_SCANCODE_RGUI
};
Jul 30, 2006
Jul 30, 2006
424
425
426
427
428
429
430
431
432
/* Set up arrays for the device dependent masks with indices that
* correspond to the _mapping arrays
*/
const unsigned int left_device_mapping[] = { NX_DEVICELSHIFTKEYMASK, NX_DEVICELCTLKEYMASK, NX_DEVICELALTKEYMASK, NX_DEVICELCMDKEYMASK };
const unsigned int right_device_mapping[] = { NX_DEVICERSHIFTKEYMASK, NX_DEVICERCTLKEYMASK, NX_DEVICERALTKEYMASK, NX_DEVICERCMDKEYMASK };
unsigned int i, bit;
/* Handle CAPSLOCK separately because it doesn't have a left/right side */
May 10, 2010
May 10, 2010
433
HandleCapsLock(scancode, oldMods, newMods);
Jul 30, 2006
Jul 30, 2006
434
435
436
437
438
439
440
441
442
443
444
445
/* Iterate through the bits, testing each against the old modifiers */
for (i = 0, bit = NSShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) {
unsigned int oldMask, newMask;
oldMask = oldMods & bit;
newMask = newMods & bit;
/* If the bit is set, we must always examine it because the left
* and right side keys may alternate or both may be pressed.
*/
if (newMask) {
May 10, 2010
May 10, 2010
446
HandleModifierSide(bit, oldMods, newMods,
Jul 30, 2006
Jul 30, 2006
447
448
449
450
451
452
453
left_mapping[i], right_mapping[i],
left_device_mapping[i], right_device_mapping[i]);
}
/* If the state changed from pressed to unpressed, we must examine
* the device dependent bits to release the correct keys.
*/
else if (oldMask && oldMask != newMask) {
May 10, 2010
May 10, 2010
454
ReleaseModifierSide(bit, oldMods, newMods,
Jul 30, 2006
Jul 30, 2006
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
left_mapping[i], right_mapping[i],
left_device_mapping[i], right_device_mapping[i]);
}
}
}
static void
HandleModifiers(_THIS, unsigned short scancode, unsigned int modifierFlags)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
if (modifierFlags == data->modifierFlags) {
return;
}
/*
* Starting with Panther (10.3.0), the ability to distinguish between
* left side and right side modifiers is available.
*/
if (data->osversion >= 0x1030) {
May 10, 2010
May 10, 2010
475
DoSidedModifiers(scancode, data->modifierFlags, modifierFlags);
Jul 30, 2006
Jul 30, 2006
476
} else {
May 10, 2010
May 10, 2010
477
DoUnsidedModifiers(scancode, data->modifierFlags, modifierFlags);
Jul 30, 2006
Jul 30, 2006
478
479
480
481
}
data->modifierFlags = modifierFlags;
}
Feb 5, 2008
Feb 5, 2008
482
483
484
static void
UpdateKeymap(SDL_VideoData *data)
{
Dec 1, 2010
Dec 1, 2010
485
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
Sep 5, 2009
Sep 5, 2009
486
487
TISInputSourceRef key_layout;
#else
Feb 5, 2008
Feb 5, 2008
488
KeyboardLayoutRef key_layout;
Sep 5, 2009
Sep 5, 2009
489
#endif
Feb 5, 2008
Feb 5, 2008
490
491
const void *chr_data;
int i;
Feb 7, 2011
Feb 7, 2011
492
SDL_Scancode scancode;
Feb 7, 2011
Feb 7, 2011
493
SDL_Keycode keymap[SDL_NUM_SCANCODES];
Feb 5, 2008
Feb 5, 2008
494
495
/* See if the keymap needs to be updated */
Dec 1, 2010
Dec 1, 2010
496
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
Sep 5, 2009
Sep 5, 2009
497
498
key_layout = TISCopyCurrentKeyboardLayoutInputSource();
#else
Feb 5, 2008
Feb 5, 2008
499
KLGetCurrentKeyboardLayout(&key_layout);
Sep 5, 2009
Sep 5, 2009
500
#endif
Feb 5, 2008
Feb 5, 2008
501
502
503
504
505
506
507
508
if (key_layout == data->key_layout) {
return;
}
data->key_layout = key_layout;
SDL_GetDefaultKeymap(keymap);
/* Try Unicode data first (preferred as of Mac OS X 10.5) */
Dec 1, 2010
Dec 1, 2010
509
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
Sep 5, 2009
Sep 5, 2009
510
511
512
513
514
515
CFDataRef uchrDataRef = TISGetInputSourceProperty(key_layout, kTISPropertyUnicodeKeyLayoutData);
if (uchrDataRef)
chr_data = CFDataGetBytePtr(uchrDataRef);
else
goto cleanup;
#else
Feb 5, 2008
Feb 5, 2008
516
KLGetKeyboardLayoutProperty(key_layout, kKLuchrData, &chr_data);
Sep 5, 2009
Sep 5, 2009
517
#endif
Feb 5, 2008
Feb 5, 2008
518
519
520
521
if (chr_data) {
UInt32 keyboard_type = LMGetKbdType();
OSStatus err;
Feb 7, 2008
Feb 7, 2008
522
for (i = 0; i < SDL_arraysize(darwin_scancode_table); i++) {
Feb 5, 2008
Feb 5, 2008
523
524
525
526
527
UniChar s[8];
UniCharCount len;
UInt32 dead_key_state;
/* Make sure this scancode is a valid character scancode */
Feb 7, 2008
Feb 7, 2008
528
scancode = darwin_scancode_table[i];
Feb 5, 2008
Feb 5, 2008
529
530
531
532
533
534
if (scancode == SDL_SCANCODE_UNKNOWN ||
(keymap[scancode] & SDLK_SCANCODE_MASK)) {
continue;
}
dead_key_state = 0;
Sep 5, 2009
Sep 5, 2009
535
536
err = UCKeyTranslate ((UCKeyboardLayout *) chr_data,
i, kUCKeyActionDown,
Feb 5, 2008
Feb 5, 2008
537
538
539
540
541
542
543
544
545
546
0, keyboard_type,
kUCKeyTranslateNoDeadKeysMask,
&dead_key_state, 8, &len, s);
if (err != noErr)
continue;
if (len > 0 && s[0] != 0x10) {
keymap[scancode] = s[0];
}
}
May 10, 2010
May 10, 2010
547
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
Feb 5, 2008
Feb 5, 2008
548
549
550
return;
}
Dec 1, 2010
Dec 1, 2010
551
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
Sep 5, 2009
Sep 5, 2009
552
553
554
cleanup:
CFRelease(key_layout);
#else
Feb 5, 2008
Feb 5, 2008
555
556
557
558
559
560
561
/* Fall back to older style key map data */
KLGetKeyboardLayoutProperty(key_layout, kKLKCHRData, &chr_data);
if (chr_data) {
for (i = 0; i < 128; i++) {
UInt32 c, state = 0;
/* Make sure this scancode is a valid character scancode */
Feb 7, 2008
Feb 7, 2008
562
scancode = darwin_scancode_table[i];
Feb 5, 2008
Feb 5, 2008
563
564
565
566
567
if (scancode == SDL_SCANCODE_UNKNOWN ||
(keymap[scancode] & SDLK_SCANCODE_MASK)) {
continue;
}
Feb 5, 2008
Feb 5, 2008
568
c = KeyTranslate (chr_data, i, &state) & 255;
Feb 5, 2008
Feb 5, 2008
569
570
if (state) {
/* Dead key, process key up */
Feb 5, 2008
Feb 5, 2008
571
c = KeyTranslate (chr_data, i | 128, &state) & 255;
Feb 5, 2008
Feb 5, 2008
572
573
}
Feb 5, 2008
Feb 5, 2008
574
if (c != 0 && c != 0x10) {
Feb 5, 2008
Feb 5, 2008
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/* MacRoman to Unicode table, taken from X.org sources */
static const unsigned short macroman_table[128] = {
0xc4, 0xc5, 0xc7, 0xc9, 0xd1, 0xd6, 0xdc, 0xe1,
0xe0, 0xe2, 0xe4, 0xe3, 0xe5, 0xe7, 0xe9, 0xe8,
0xea, 0xeb, 0xed, 0xec, 0xee, 0xef, 0xf1, 0xf3,
0xf2, 0xf4, 0xf6, 0xf5, 0xfa, 0xf9, 0xfb, 0xfc,
0x2020, 0xb0, 0xa2, 0xa3, 0xa7, 0x2022, 0xb6, 0xdf,
0xae, 0xa9, 0x2122, 0xb4, 0xa8, 0x2260, 0xc6, 0xd8,
0x221e, 0xb1, 0x2264, 0x2265, 0xa5, 0xb5, 0x2202, 0x2211,
0x220f, 0x3c0, 0x222b, 0xaa, 0xba, 0x3a9, 0xe6, 0xf8,
0xbf, 0xa1, 0xac, 0x221a, 0x192, 0x2248, 0x2206, 0xab,
0xbb, 0x2026, 0xa0, 0xc0, 0xc3, 0xd5, 0x152, 0x153,
0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0xf7, 0x25ca,
0xff, 0x178, 0x2044, 0x20ac, 0x2039, 0x203a, 0xfb01, 0xfb02,
0x2021, 0xb7, 0x201a, 0x201e, 0x2030, 0xc2, 0xca, 0xc1,
0xcb, 0xc8, 0xcd, 0xce, 0xcf, 0xcc, 0xd3, 0xd4,
0xf8ff, 0xd2, 0xda, 0xdb, 0xd9, 0x131, 0x2c6, 0x2dc,
0xaf, 0x2d8, 0x2d9, 0x2da, 0xb8, 0x2dd, 0x2db, 0x2c7,
};
if (c >= 128) {
c = macroman_table[c - 128];
}
keymap[scancode] = c;
}
}
May 10, 2010
May 10, 2010
601
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
Feb 5, 2008
Feb 5, 2008
602
603
return;
}
Sep 5, 2009
Sep 5, 2009
604
#endif
Feb 5, 2008
Feb 5, 2008
605
606
}
607
608
609
610
611
void
Cocoa_InitKeyboard(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Feb 5, 2008
Feb 5, 2008
612
UpdateKeymap(data);
Aug 19, 2007
Aug 19, 2007
613
614
/* Set our own names for the platform-dependent but layout-independent keys */
Feb 5, 2008
Feb 5, 2008
615
616
617
618
619
620
/* This key is NumLock on the MacBook keyboard. :) */
/*SDL_SetScancodeName(SDL_SCANCODE_NUMLOCKCLEAR, "Clear");*/
SDL_SetScancodeName(SDL_SCANCODE_LALT, "Left Option");
SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Command");
SDL_SetScancodeName(SDL_SCANCODE_RALT, "Right Option");
SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Command");
Sep 19, 2009
Sep 19, 2009
623
624
625
626
627
628
629
void
Cocoa_StartTextInput(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSView *parentView = [[NSApp keyWindow] contentView];
Apr 16, 2010
Apr 16, 2010
630
631
632
633
634
/* We only keep one field editor per process, since only the front most
* window can receive text input events, so it make no sense to keep more
* than one copy. When we switched to another window and requesting for
* text input, simply remove the field editor from its superview then add
* it to the front most window's content view */
Apr 16, 2010
Apr 16, 2010
635
if (!data->fieldEdit) {
Apr 16, 2010
Apr 16, 2010
636
637
data->fieldEdit =
[[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
Apr 16, 2010
Apr 16, 2010
638
}
Apr 16, 2010
Apr 16, 2010
639
Apr 16, 2010
Apr 16, 2010
640
if (![[data->fieldEdit superview] isEqual: parentView])
Sep 19, 2009
Sep 19, 2009
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
{
// DEBUG_IME(@"add fieldEdit to window contentView");
[data->fieldEdit removeFromSuperview];
[parentView addSubview: data->fieldEdit];
[[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
}
[pool release];
}
void
Cocoa_StopTextInput(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Jan 19, 2010
Jan 19, 2010
656
657
658
659
660
661
662
if (data && data->fieldEdit) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[data->fieldEdit removeFromSuperview];
[data->fieldEdit release];
data->fieldEdit = nil;
[pool release];
}
Sep 19, 2009
Sep 19, 2009
663
664
665
666
667
668
669
670
671
672
}
void
Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
[data->fieldEdit setInputRect: rect];
}
Jul 30, 2006
Jul 30, 2006
673
674
675
676
677
void
Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
unsigned short scancode = [event keyCode];
Feb 7, 2011
Feb 7, 2011
678
SDL_Scancode code;
May 10, 2010
May 10, 2010
679
#if 0
Jul 30, 2006
Jul 30, 2006
680
const char *text;
May 10, 2010
May 10, 2010
681
#endif
Jul 30, 2006
Jul 30, 2006
682
Aug 19, 2007
Aug 19, 2007
683
684
685
686
if ((scancode == 10 || scancode == 50) && KBGetLayoutType(LMGetKbdType()) == kKeyboardISO) {
/* see comments in SDL_cocoakeys.h */
scancode = 60 - scancode;
}
Feb 7, 2008
Feb 7, 2008
687
688
if (scancode < SDL_arraysize(darwin_scancode_table)) {
code = darwin_scancode_table[scancode];
Aug 19, 2007
Aug 19, 2007
689
690
}
else {
Jul 30, 2006
Jul 30, 2006
691
/* Hmm, does this ever happen? If so, need to extend the keymap... */
Feb 5, 2008
Feb 5, 2008
692
code = SDL_SCANCODE_UNKNOWN;
Jul 30, 2006
Jul 30, 2006
693
694
695
696
}
switch ([event type]) {
case NSKeyDown:
Jul 22, 2010
Jul 22, 2010
697
if (![event isARepeat]) {
Feb 5, 2008
Feb 5, 2008
698
699
/* See if we need to rebuild the keyboard layout */
UpdateKeymap(data);
Jul 21, 2010
Jul 21, 2010
700
}
Feb 5, 2008
Feb 5, 2008
701
Jul 22, 2010
Jul 22, 2010
702
SDL_SendKeyboardKey(SDL_PRESSED, code);
Aug 19, 2007
Aug 19, 2007
703
#if 1
Jul 21, 2010
Jul 21, 2010
704
705
if (code == SDL_SCANCODE_UNKNOWN) {
fprintf(stderr, "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode);
Jul 30, 2006
Jul 30, 2006
706
}
Jul 21, 2010
Jul 21, 2010
707
#endif
Jun 16, 2007
Jun 16, 2007
708
if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
Aug 19, 2007
Aug 19, 2007
709
/* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */
Jul 11, 2007
Jul 11, 2007
710
[data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
Sep 19, 2009
Sep 19, 2009
711
#if 0
Jun 16, 2007
Jun 16, 2007
712
713
text = [[event characters] UTF8String];
if(text && *text) {
May 10, 2010
May 10, 2010
714
SDL_SendKeyboardText(text);
Aug 21, 2007
Aug 21, 2007
715
[data->fieldEdit setString:@""];
Jun 16, 2007
Jun 16, 2007
716
}
Sep 19, 2009
Sep 19, 2009
717
#endif
Jul 30, 2006
Jul 30, 2006
718
719
720
}
break;
case NSKeyUp:
Jul 22, 2010
Jul 22, 2010
721
SDL_SendKeyboardKey(SDL_RELEASED, code);
Jul 30, 2006
Jul 30, 2006
722
723
break;
case NSFlagsChanged:
Aug 19, 2007
Aug 19, 2007
724
/* FIXME CW 2007-08-14: check if this whole mess that takes up half of this file is really necessary */
Jul 30, 2006
Jul 30, 2006
725
726
HandleModifiers(_this, scancode, [event modifierFlags]);
break;
Aug 19, 2007
Aug 19, 2007
727
728
729
730
731
default: /* just to avoid compiler warnings */
break;
}
}
732
733
734
735
736
737
void
Cocoa_QuitKeyboard(_THIS)
{
}
/* vi: set ts=4 sw=4 expandtab: */