23 #if SDL_VIDEO_DRIVER_COCOA
27 #include "../../events/SDL_keyboard_c.h"
28 #include "../../events/scancodes_darwin.h"
30 #include <Carbon/Carbon.h>
33 #define DEBUG_IME(...)
35 #ifndef NX_DEVICERCTLKEYMASK
36 #define NX_DEVICELCTLKEYMASK 0x00000001
38 #ifndef NX_DEVICELSHIFTKEYMASK
39 #define NX_DEVICELSHIFTKEYMASK 0x00000002
41 #ifndef NX_DEVICERSHIFTKEYMASK
42 #define NX_DEVICERSHIFTKEYMASK 0x00000004
44 #ifndef NX_DEVICELCMDKEYMASK
45 #define NX_DEVICELCMDKEYMASK 0x00000008
47 #ifndef NX_DEVICERCMDKEYMASK
48 #define NX_DEVICERCMDKEYMASK 0x00000010
50 #ifndef NX_DEVICELALTKEYMASK
51 #define NX_DEVICELALTKEYMASK 0x00000020
53 #ifndef NX_DEVICERALTKEYMASK
54 #define NX_DEVICERALTKEYMASK 0x00000040
56 #ifndef NX_DEVICERCTLKEYMASK
57 #define NX_DEVICERCTLKEYMASK 0x00002000
60 @interface SDLTranslatorResponder : NSView <NSTextInput>
62 NSString *_markedText;
64 NSRange _selectedRange;
67 - (
void) doCommandBySelector:(
SEL)myselector;
71 @implementation SDLTranslatorResponder
78 - (
void) insertText:(
id) aString
82 DEBUG_IME(
@"insertText: %@", aString);
86 if ([aString isKindOfClass: [NSAttributedString
class]])
87 str = [[aString string] UTF8String];
89 str = [aString UTF8String];
94 - (
void) doCommandBySelector:(
SEL) myselector
102 - (
BOOL) hasMarkedText
104 return _markedText != nil;
107 - (NSRange) markedRange
112 - (NSRange) selectedRange
114 return _selectedRange;
117 - (
void) setMarkedText:(
id) aString
118 selectedRange:(NSRange) selRange
120 if ([aString isKindOfClass: [NSAttributedString
class]])
121 aString = [aString string];
123 if ([aString
length] == 0)
129 if (_markedText != aString)
131 [_markedText release];
132 _markedText = [aString retain];
135 _selectedRange = selRange;
136 _markedRange = NSMakeRange(0, [aString
length]);
139 selRange.location, selRange.length);
141 DEBUG_IME(
@"setMarkedText: %@, (%d, %d)", _markedText,
142 selRange.location, selRange.length);
147 [_markedText release];
153 - (NSRect) firstRectForCharacterRange: (NSRange) theRange
155 NSWindow *window = [
self window];
156 NSRect contentRect = [window contentRectForFrameRect: [window frame]];
157 float windowHeight = contentRect.size.height;
158 NSRect rect = NSMakeRect(_inputRect.x, windowHeight - _inputRect.y - _inputRect.h,
159 _inputRect.w, _inputRect.h);
161 DEBUG_IME(
@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
162 theRange.location, theRange.length, windowHeight,
163 NSStringFromRect(rect));
164 rect.origin = [[
self window] convertBaseToScreen: rect.origin];
169 - (NSAttributedString *) attributedSubstringFromRange: (NSRange) theRange
171 DEBUG_IME(
@"attributedSubstringFromRange: (%d, %d)", theRange.location, theRange.length);
175 - (NSInteger) conversationIdentifier
177 return (NSInteger)
self;
183 - (NSUInteger) characterIndexForPoint:(NSPoint) thePoint
185 DEBUG_IME(
@"characterIndexForPoint: (%g, %g)", thePoint.x, thePoint.y);
194 - (NSArray *) validAttributesForMarkedText
196 return [NSArray array];
205 DoUnsidedModifiers(
unsigned short scancode,
206 unsigned int oldMods,
unsigned int newMods)
218 for (i = 0, bit = NSAlphaShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) {
219 unsigned int oldMask, newMask;
221 oldMask = oldMods & bit;
222 newMask = newMods & bit;
224 if (oldMask && oldMask != newMask) {
226 if (bit == NSAlphaShiftKeyMask) {
230 }
else if (newMask && oldMask != newMask) {
233 if (bit == NSAlphaShiftKeyMask) {
245 HandleNonDeviceModifier(
unsigned int device_independent_mask,
246 unsigned int oldMods,
247 unsigned int newMods,
250 unsigned int oldMask, newMask;
255 oldMask = oldMods & device_independent_mask;
256 newMask = newMods & device_independent_mask;
258 if (oldMask && oldMask != newMask) {
260 }
else if (newMask && oldMask != newMask) {
269 HandleModifierOneSide(
unsigned int oldMods,
unsigned int newMods,
271 unsigned int sided_device_dependent_mask)
273 unsigned int old_dep_mask, new_dep_mask;
278 old_dep_mask = oldMods & sided_device_dependent_mask;
279 new_dep_mask = newMods & sided_device_dependent_mask;
285 if (new_dep_mask && old_dep_mask != new_dep_mask) {
297 HandleModifierSide(
int device_independent_mask,
298 unsigned int oldMods,
unsigned int newMods,
301 unsigned int left_device_dependent_mask,
302 unsigned int right_device_dependent_mask)
304 unsigned int device_dependent_mask = (left_device_dependent_mask |
305 right_device_dependent_mask);
306 unsigned int diff_mod;
312 if ((device_dependent_mask & newMods) == 0) {
314 HandleNonDeviceModifier(device_independent_mask, oldMods, newMods, left_scancode);
319 diff_mod = (device_dependent_mask & oldMods) ^
320 (device_dependent_mask & newMods);
326 if (left_device_dependent_mask & diff_mod) {
327 HandleModifierOneSide(oldMods, newMods, left_scancode, left_device_dependent_mask);
329 if (right_device_dependent_mask & diff_mod) {
330 HandleModifierOneSide(oldMods, newMods, right_scancode, right_device_dependent_mask);
341 ReleaseModifierSide(
unsigned int device_independent_mask,
342 unsigned int oldMods,
unsigned int newMods,
345 unsigned int left_device_dependent_mask,
346 unsigned int right_device_dependent_mask)
348 unsigned int device_dependent_mask = (left_device_dependent_mask |
349 right_device_dependent_mask);
355 if ((device_dependent_mask & oldMods) == 0) {
369 if ( left_device_dependent_mask & oldMods ) {
372 if ( right_device_dependent_mask & oldMods ) {
381 HandleCapsLock(
unsigned short scancode,
382 unsigned int oldMods,
unsigned int newMods)
384 unsigned int oldMask, newMask;
386 oldMask = oldMods & NSAlphaShiftKeyMask;
387 newMask = newMods & NSAlphaShiftKeyMask;
389 if (oldMask != newMask) {
399 DoSidedModifiers(
unsigned short scancode,
400 unsigned int oldMods,
unsigned int newMods)
418 const unsigned int left_device_mapping[] = { NX_DEVICELSHIFTKEYMASK, NX_DEVICELCTLKEYMASK, NX_DEVICELALTKEYMASK, NX_DEVICELCMDKEYMASK };
419 const unsigned int right_device_mapping[] = { NX_DEVICERSHIFTKEYMASK, NX_DEVICERCTLKEYMASK, NX_DEVICERALTKEYMASK, NX_DEVICERCMDKEYMASK };
424 HandleCapsLock(scancode, oldMods, newMods);
427 for (i = 0, bit = NSShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) {
428 unsigned int oldMask, newMask;
430 oldMask = oldMods & bit;
431 newMask = newMods & bit;
437 HandleModifierSide(bit, oldMods, newMods,
438 left_mapping[i], right_mapping[i],
439 left_device_mapping[i], right_device_mapping[i]);
444 else if (oldMask && oldMask != newMask) {
445 ReleaseModifierSide(bit, oldMods, newMods,
446 left_mapping[i], right_mapping[i],
447 left_device_mapping[i], right_device_mapping[i]);
453 HandleModifiers(
_THIS,
unsigned short scancode,
unsigned int modifierFlags)
466 DoSidedModifiers(scancode, data->
modifierFlags, modifierFlags);
468 DoUnsidedModifiers(scancode, data->
modifierFlags, modifierFlags);
476 TISInputSourceRef key_layout;
477 const void *chr_data;
483 key_layout = TISCopyCurrentKeyboardLayoutInputSource();
492 CFDataRef uchrDataRef = TISGetInputSourceProperty(key_layout, kTISPropertyUnicodeKeyLayoutData);
494 chr_data = CFDataGetBytePtr(uchrDataRef);
499 UInt32 keyboard_type = LMGetKbdType();
505 UInt32 dead_key_state;
515 err = UCKeyTranslate ((UCKeyboardLayout *) chr_data,
518 kUCKeyTranslateNoDeadKeysMask,
519 &dead_key_state, 8, &len, s);
523 if (len > 0 && s[0] != 0x10) {
524 keymap[scancode] = s[0];
532 CFRelease(key_layout);
555 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
init];
556 NSView *parentView = [[NSApp keyWindow] contentView];
565 [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
568 if (![[data->
fieldEdit superview] isEqual: parentView])
571 [data->fieldEdit removeFromSuperview];
572 [parentView addSubview: data->fieldEdit];
573 [[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
585 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
586 [data->fieldEdit removeFromSuperview];
587 [data->fieldEdit release];
603 [data->fieldEdit setInputRect: rect];
610 unsigned short scancode = [event keyCode];
616 if ((scancode == 10 || scancode == 50) && KBGetLayoutType(LMGetKbdType()) == kKeyboardISO) {
618 scancode = 60 - scancode;
628 switch ([event
type]) {
630 if (![event isARepeat]) {
638 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);
643 [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
645 text = [[event characters] UTF8String];
648 [data->fieldEdit setString:@""];
658 HandleModifiers(
_this, scancode, [event modifierFlags]);
void SDL_GetDefaultKeymap(SDL_Keycode *keymap)
void Cocoa_SetTextInputRect(_THIS, SDL_Rect *rect)
void Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
SDLTranslatorResponder * fieldEdit
static void init(struct bs2b *bs2b)
#define SDLK_SCANCODE_MASK
DECLSPEC Uint8 SDLCALL SDL_EventState(Uint32 type, int state)
#define SDL_InvalidParamError(param)
unsigned int modifierFlags
void Cocoa_StartTextInput(_THIS)
Sint32 SDL_Keycode
The SDL virtual key representation.
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
void Cocoa_QuitKeyboard(_THIS)
static SDL_VideoDevice * _this
void SDL_SetKeymap(int start, SDL_Keycode *keys, int length)
static const SDL_Scancode darwin_scancode_table[]
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
int SDL_SendKeyboardText(const char *text)
void SDL_SetScancodeName(SDL_Scancode scancode, const char *name)
void cleanup(int exitcode)
#define SDL_arraysize(array)
void Cocoa_InitKeyboard(_THIS)
void Cocoa_StopTextInput(_THIS)
GLenum GLenum GLenum GLenum mapping
SDL_Scancode
The SDL keyboard scancode representation.
int SDL_SendEditingText(const char *text, int start, int length)
A rectangle, with the origin at the upper left.
typedef BOOL(WINAPI *PFNWGLSETSTEREOEMITTERSTATE3DLPROC)(HDC hDC