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