src/video/quartz/SDL_QuartzEvents.m
author Sam Lantinga <slouken@lokigames.com>
Thu, 07 Jun 2001 14:28:11 +0000
changeset 47 45b1c4303f87
child 117 aac75d5f7869
permissions -rw-r--r--
Added initial support for Quartz video (thanks Darrell!)
slouken@47
     1
/*
slouken@47
     2
    SDL - Simple DirectMedia Layer
slouken@47
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@47
     4
slouken@47
     5
    This library is free software; you can redistribute it and/or
slouken@47
     6
    modify it under the terms of the GNU Library General Public
slouken@47
     7
    License as published by the Free Software Foundation; either
slouken@47
     8
    version 2 of the License, or (at your option) any later version.
slouken@47
     9
slouken@47
    10
    This library is distributed in the hope that it will be useful,
slouken@47
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@47
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@47
    13
    Library General Public License for more details.
slouken@47
    14
slouken@47
    15
    You should have received a copy of the GNU Library General Public
slouken@47
    16
    License along with this library; if not, write to the Free
slouken@47
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@47
    18
slouken@47
    19
    Sam Lantinga
slouken@47
    20
    slouken@devolution.com
slouken@47
    21
*/
slouken@47
    22
slouken@47
    23
#include "SDL_QuartzKeys.h"
slouken@47
    24
slouken@47
    25
static void  QZ_InitOSKeymap (_THIS) {
slouken@47
    26
	int i;
slouken@47
    27
slouken@47
    28
	for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
slouken@47
    29
		keymap[i] = SDLK_UNKNOWN;
slouken@47
    30
slouken@47
    31
	// This keymap is almost exactly the same as the OS 9 one
slouken@47
    32
        keymap[QZ_ESCAPE] = SDLK_ESCAPE;
slouken@47
    33
	keymap[QZ_F1] = SDLK_F1;
slouken@47
    34
	keymap[QZ_F2] = SDLK_F2;
slouken@47
    35
	keymap[QZ_F3] = SDLK_F3;
slouken@47
    36
	keymap[QZ_F4] = SDLK_F4;
slouken@47
    37
	keymap[QZ_F5] = SDLK_F5;
slouken@47
    38
	keymap[QZ_F6] = SDLK_F6;
slouken@47
    39
	keymap[QZ_F7] = SDLK_F7;
slouken@47
    40
	keymap[QZ_F8] = SDLK_F8;
slouken@47
    41
	keymap[QZ_F9] = SDLK_F9;
slouken@47
    42
	keymap[QZ_F10] = SDLK_F10;
slouken@47
    43
	keymap[QZ_F11] = SDLK_F11;
slouken@47
    44
	keymap[QZ_F12] = SDLK_F12;
slouken@47
    45
	keymap[QZ_PRINT] = SDLK_PRINT;
slouken@47
    46
	keymap[QZ_SCROLLOCK] = SDLK_SCROLLOCK;
slouken@47
    47
	keymap[QZ_PAUSE] = SDLK_PAUSE;
slouken@47
    48
	keymap[QZ_POWER] = SDLK_POWER;
slouken@47
    49
	keymap[QZ_BACKQUOTE] = SDLK_BACKQUOTE;
slouken@47
    50
	keymap[QZ_1] = SDLK_1;
slouken@47
    51
	keymap[QZ_2] = SDLK_2;
slouken@47
    52
	keymap[QZ_3] = SDLK_3;
slouken@47
    53
	keymap[QZ_4] = SDLK_4;
slouken@47
    54
	keymap[QZ_5] = SDLK_5;
slouken@47
    55
	keymap[QZ_6] = SDLK_6;
slouken@47
    56
	keymap[QZ_7] = SDLK_7;
slouken@47
    57
	keymap[QZ_8] = SDLK_8;
slouken@47
    58
	keymap[QZ_9] = SDLK_9;
slouken@47
    59
	keymap[QZ_0] = SDLK_0;
slouken@47
    60
	keymap[QZ_MINUS] = SDLK_MINUS;
slouken@47
    61
	keymap[QZ_EQUALS] = SDLK_EQUALS;
slouken@47
    62
	keymap[QZ_BACKSPACE] = SDLK_BACKSPACE;
slouken@47
    63
	keymap[QZ_INSERT] = SDLK_INSERT;
slouken@47
    64
	keymap[QZ_HOME] = SDLK_HOME;
slouken@47
    65
	keymap[QZ_PAGEUP] = SDLK_PAGEUP;
slouken@47
    66
	keymap[QZ_NUMLOCK] = SDLK_NUMLOCK;
slouken@47
    67
	keymap[QZ_KP_EQUALS] = SDLK_KP_EQUALS;
slouken@47
    68
	keymap[QZ_KP_DIVIDE] = SDLK_KP_DIVIDE;
slouken@47
    69
	keymap[QZ_KP_MULTIPLY] = SDLK_KP_MULTIPLY;
slouken@47
    70
	keymap[QZ_TAB] = SDLK_TAB;
slouken@47
    71
	keymap[QZ_q] = SDLK_q;
slouken@47
    72
	keymap[QZ_w] = SDLK_w;
slouken@47
    73
	keymap[QZ_e] = SDLK_e;
slouken@47
    74
	keymap[QZ_r] = SDLK_r;
slouken@47
    75
	keymap[QZ_t] = SDLK_t;
slouken@47
    76
	keymap[QZ_y] = SDLK_y;
slouken@47
    77
	keymap[QZ_u] = SDLK_u;
slouken@47
    78
	keymap[QZ_i] = SDLK_i;
slouken@47
    79
	keymap[QZ_o] = SDLK_o;
slouken@47
    80
	keymap[QZ_p] = SDLK_p;
slouken@47
    81
	keymap[QZ_LEFTBRACKET] = SDLK_LEFTBRACKET;
slouken@47
    82
	keymap[QZ_RIGHTBRACKET] = SDLK_RIGHTBRACKET;
slouken@47
    83
	keymap[QZ_BACKSLASH] = SDLK_BACKSLASH;
slouken@47
    84
	keymap[QZ_DELETE] = SDLK_DELETE;
slouken@47
    85
	keymap[QZ_END] = SDLK_END;
slouken@47
    86
	keymap[QZ_PAGEDOWN] = SDLK_PAGEDOWN;
slouken@47
    87
	keymap[QZ_KP7] = SDLK_KP7;
slouken@47
    88
	keymap[QZ_KP8] = SDLK_KP8;
slouken@47
    89
	keymap[QZ_KP9] = SDLK_KP9;
slouken@47
    90
	keymap[QZ_KP_MINUS] = SDLK_KP_MINUS;
slouken@47
    91
	keymap[QZ_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@47
    92
	keymap[QZ_a] = SDLK_a;
slouken@47
    93
	keymap[QZ_s] = SDLK_s;
slouken@47
    94
	keymap[QZ_d] = SDLK_d;
slouken@47
    95
	keymap[QZ_f] = SDLK_f;
slouken@47
    96
	keymap[QZ_g] = SDLK_g;
slouken@47
    97
	keymap[QZ_h] = SDLK_h;
slouken@47
    98
	keymap[QZ_j] = SDLK_j;
slouken@47
    99
	keymap[QZ_k] = SDLK_k;
slouken@47
   100
	keymap[QZ_l] = SDLK_l;
slouken@47
   101
	keymap[QZ_SEMICOLON] = SDLK_SEMICOLON;
slouken@47
   102
	keymap[QZ_QUOTE] = SDLK_QUOTE;
slouken@47
   103
	keymap[QZ_RETURN] = SDLK_RETURN;
slouken@47
   104
	keymap[QZ_KP4] = SDLK_KP4;
slouken@47
   105
	keymap[QZ_KP5] = SDLK_KP5;
slouken@47
   106
	keymap[QZ_KP6] = SDLK_KP6;
slouken@47
   107
	keymap[QZ_KP_PLUS] = SDLK_KP_PLUS;
slouken@47
   108
	keymap[QZ_LSHIFT] = SDLK_LSHIFT;
slouken@47
   109
	keymap[QZ_z] = SDLK_z;
slouken@47
   110
	keymap[QZ_x] = SDLK_x;
slouken@47
   111
	keymap[QZ_c] = SDLK_c;
slouken@47
   112
	keymap[QZ_v] = SDLK_v;
slouken@47
   113
	keymap[QZ_b] = SDLK_b;
slouken@47
   114
	keymap[QZ_n] = SDLK_n;
slouken@47
   115
	keymap[QZ_m] = SDLK_m;
slouken@47
   116
	keymap[QZ_COMMA] = SDLK_COMMA;
slouken@47
   117
	keymap[QZ_PERIOD] = SDLK_PERIOD;
slouken@47
   118
	keymap[QZ_SLASH] = SDLK_SLASH;
slouken@47
   119
	keymap[QZ_UP] = SDLK_UP;
slouken@47
   120
	keymap[QZ_KP1] = SDLK_KP1;
slouken@47
   121
	keymap[QZ_KP2] = SDLK_KP2;
slouken@47
   122
	keymap[QZ_KP3] = SDLK_KP3;
slouken@47
   123
	keymap[QZ_KP_ENTER] = SDLK_KP_ENTER;
slouken@47
   124
	keymap[QZ_LCTRL] = SDLK_LCTRL;
slouken@47
   125
	keymap[QZ_LALT] = SDLK_LALT;
slouken@47
   126
	keymap[QZ_LMETA] = SDLK_LMETA;
slouken@47
   127
	keymap[QZ_SPACE] = SDLK_SPACE;
slouken@47
   128
	keymap[QZ_LEFT] = SDLK_LEFT;
slouken@47
   129
	keymap[QZ_DOWN] = SDLK_DOWN;
slouken@47
   130
	keymap[QZ_RIGHT] = SDLK_RIGHT;
slouken@47
   131
	keymap[QZ_KP0] = SDLK_KP0;
slouken@47
   132
	keymap[QZ_KP_PERIOD] = SDLK_KP_PERIOD;
slouken@47
   133
	keymap[QZ_IBOOK_ENTER] = SDLK_KP_ENTER;
slouken@47
   134
	keymap[QZ_IBOOK_RIGHT] = SDLK_RIGHT;
slouken@47
   135
	keymap[QZ_IBOOK_DOWN] = SDLK_DOWN;
slouken@47
   136
	keymap[QZ_IBOOK_UP]   = SDLK_UP;
slouken@47
   137
	keymap[QZ_IBOOK_LEFT] = SDLK_LEFT;
slouken@47
   138
}
slouken@47
   139
slouken@47
   140
static void QZ_DoKey (int state, NSEvent *event) {
slouken@47
   141
slouken@47
   142
        NSString *chars;
slouken@47
   143
        int i;
slouken@47
   144
        SDL_keysym key;
slouken@47
   145
        
slouken@47
   146
        /* An event can contain multiple characters */
slouken@47
   147
        /* I'll ignore this fact for now, since there is only one virtual key code per event */
slouken@47
   148
        chars = [ event characters ];
slouken@47
   149
        for (i =0; i < 1 /*[ chars length ] */; i++) {
slouken@47
   150
                    
slouken@47
   151
            key.scancode = [ event keyCode ];
slouken@47
   152
            key.sym      = keymap [ key.scancode ];
slouken@47
   153
            key.unicode  = [ chars characterAtIndex:i];
slouken@47
   154
            key.mod      = KMOD_NONE;
slouken@47
   155
                        
slouken@47
   156
            SDL_PrivateKeyboard (state, &key);
slouken@47
   157
        }
slouken@47
   158
}
slouken@47
   159
slouken@47
   160
static void QZ_DoModifiers (unsigned int newMods) {
slouken@47
   161
slouken@47
   162
    const int offset = 18;
slouken@47
   163
    const int mapping[] = { SDLK_LSHIFT, SDLK_LCTRL, SDLK_LALT, 0, SDLK_LMETA } ;
slouken@47
   164
slouken@47
   165
    int bit;
slouken@47
   166
    SDL_keysym key;
slouken@47
   167
    key.scancode = 0;
slouken@47
   168
    key.sym      = SDLK_UNKNOWN;
slouken@47
   169
    key.unicode  = 0;
slouken@47
   170
    key.mod      = KMOD_NONE;
slouken@47
   171
    
slouken@47
   172
    /* Iterate through the bits, testing each against the current modifiers */
slouken@47
   173
    for (bit = NSShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1) {
slouken@47
   174
    
slouken@47
   175
        unsigned int currentMask, newMask;
slouken@47
   176
        
slouken@47
   177
        currentMask = currentMods & bit;
slouken@47
   178
        newMask     = newMods & bit;
slouken@47
   179
        
slouken@47
   180
        if ( currentMask && 
slouken@47
   181
             currentMask != newMask ) {  /* modifier up event */
slouken@47
   182
slouken@47
   183
            key.sym = mapping[ currentMask >> offset ];
slouken@47
   184
            SDL_PrivateKeyboard (SDL_RELEASED, &key);
slouken@47
   185
        }
slouken@47
   186
        else
slouken@47
   187
        if ( newMask &&
slouken@47
   188
             currentMask != newMask ) {  /* modifier down event */
slouken@47
   189
         
slouken@47
   190
            key.sym = mapping [ newMask >> offset ];
slouken@47
   191
            SDL_PrivateKeyboard (SDL_PRESSED, &key);
slouken@47
   192
        }
slouken@47
   193
    }
slouken@47
   194
    
slouken@47
   195
    currentMods = newMods;
slouken@47
   196
}
slouken@47
   197
slouken@47
   198
static void QZ_DoActivate (_THIS, NSPoint p) {
slouken@47
   199
    
slouken@47
   200
    inForeground = YES;
slouken@47
   201
slouken@47
   202
    /* Regrab the mouse */
slouken@47
   203
    if (currentGrabMode == SDL_GRAB_ON) {
slouken@47
   204
        QZ_WarpWMCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2);
slouken@47
   205
        CGAssociateMouseAndMouseCursorPosition (0);
slouken@47
   206
    }
slouken@47
   207
    
slouken@47
   208
    SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS);
slouken@47
   209
}
slouken@47
   210
slouken@47
   211
static void QZ_DoDeactivate (_THIS) {
slouken@47
   212
    
slouken@47
   213
    inForeground = NO;
slouken@47
   214
slouken@47
   215
    /* Ungrab mouse if it is grabbed */
slouken@47
   216
    if (currentGrabMode == SDL_GRAB_ON) {
slouken@47
   217
        CGAssociateMouseAndMouseCursorPosition (1);
slouken@47
   218
    }
slouken@47
   219
    
slouken@47
   220
    SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
slouken@47
   221
}
slouken@47
   222
slouken@47
   223
static void QZ_PumpEvents (_THIS) { 
slouken@47
   224
slouken@47
   225
    NSDate *distantPast = [ NSDate distantPast ];
slouken@47
   226
    
slouken@47
   227
    //NSAutoreleasePool *pool;
slouken@47
   228
    //pool = [ [ NSAutoreleasePool alloc ] init ];
slouken@47
   229
    
slouken@47
   230
    NSEvent *event;
slouken@47
   231
    NSRect winRect;
slouken@47
   232
    NSRect titleBarRect;
slouken@47
   233
            
slouken@47
   234
    winRect = NSMakeRect (0, 0, SDL_VideoSurface->w + 1, SDL_VideoSurface->h + 1);
slouken@47
   235
    titleBarRect = NSMakeRect ( 0, SDL_VideoSurface->h, SDL_VideoSurface->w,
slouken@47
   236
        SDL_VideoSurface->h + 22 );
slouken@47
   237
            
slouken@47
   238
    do {
slouken@47
   239
    
slouken@47
   240
        /* Poll for an event. This will not block */
slouken@47
   241
        event = [ NSApp nextEventMatchingMask:NSAnyEventMask
slouken@47
   242
                    untilDate:distantPast
slouken@47
   243
                    inMode: NSDefaultRunLoopMode dequeue:YES ];
slouken@47
   244
    
slouken@47
   245
        if (event != nil) {
slouken@47
   246
            unsigned int type;
slouken@47
   247
            NSPoint p;
slouken@47
   248
            
slouken@47
   249
            type = [ event type ];
slouken@47
   250
            p = [ event locationInWindow ];
slouken@47
   251
            
slouken@47
   252
            #define DO_MOUSE_DOWN(button, sendToWindow)                                                \
slouken@47
   253
                if ( inForeground ) {                                                                  \
slouken@47
   254
                    if ( NSPointInRect(p,winRect))                                                     \
slouken@47
   255
                        SDL_PrivateMouseButton (SDL_PRESSED, button, p.x, SDL_VideoSurface->h - p.y);  \
slouken@47
   256
                    else if (sendToWindow)                                                             \
slouken@47
   257
                            [ window sendEvent:event ];                                                   \
slouken@47
   258
                }                                                                                      \
slouken@47
   259
                else {                                                                                 \
slouken@47
   260
                    QZ_DoActivate (this, p);                                                           \
slouken@47
   261
                }       
slouken@47
   262
            
slouken@47
   263
            #define DO_MOUSE_UP(button, sendToWindow)                                          \
slouken@47
   264
                if ( ! NSPointInRect (p, titleBarRect) ) \
slouken@47
   265
                    SDL_PrivateMouseButton (SDL_RELEASED, button, p.x, SDL_VideoSurface->h - p.y); \
slouken@47
   266
                if (sendToWindow)                                                              \
slouken@47
   267
                    [ window sendEvent:event ]
slouken@47
   268
                    
slouken@47
   269
            switch ( type) {
slouken@47
   270
            
slouken@47
   271
            case NSLeftMouseDown:  
slouken@47
   272
                if ( NSCommandKeyMask & currentMods ) {
slouken@47
   273
                        DO_MOUSE_DOWN (3, 0);
slouken@47
   274
                } 
slouken@47
   275
                else if ( NSAlternateKeyMask & currentMods ) {
slouken@47
   276
                    DO_MOUSE_DOWN (2, 0);
slouken@47
   277
                } 
slouken@47
   278
                else {
slouken@47
   279
                    DO_MOUSE_DOWN (1, 1);
slouken@47
   280
                }
slouken@47
   281
                break;
slouken@47
   282
            case 25:               DO_MOUSE_DOWN (2, 0); break;
slouken@47
   283
            case NSRightMouseDown: DO_MOUSE_DOWN (3, 0); break;   
slouken@47
   284
            case NSLeftMouseUp:    
slouken@47
   285
            if ( NSCommandKeyMask & currentMods ) {
slouken@47
   286
                        DO_MOUSE_UP (3, 0);
slouken@47
   287
                } 
slouken@47
   288
                else if ( NSAlternateKeyMask & currentMods ) {
slouken@47
   289
                    DO_MOUSE_UP (2, 0);
slouken@47
   290
                } 
slouken@47
   291
                else
slouken@47
   292
                    DO_MOUSE_UP (1, 1);
slouken@47
   293
                break;
slouken@47
   294
            case 26:               DO_MOUSE_UP (2, 0);   break;
slouken@47
   295
            case NSRightMouseUp:   DO_MOUSE_UP (3, 0);   break;
slouken@47
   296
            case NSSystemDefined:
slouken@47
   297
                //if ([event subtype] == 7) {
slouken@47
   298
                //    unsigned int buttons;   // up to 32 mouse button states!
slouken@47
   299
                //    buttons = [ event data2 ];
slouken@47
   300
                //}
slouken@47
   301
                break;
slouken@47
   302
            case NSLeftMouseDragged:
slouken@47
   303
            case NSRightMouseDragged:
slouken@47
   304
            case 27:
slouken@47
   305
                SDL_PrivateMouseMotion (SDL_PRESSED, 0, p.x, SDL_VideoSurface->h - p.y);
slouken@47
   306
                break;
slouken@47
   307
            case NSMouseMoved:
slouken@47
   308
                {
slouken@47
   309
                   static int moves = 0;
slouken@47
   310
                    
slouken@47
   311
                   if ( (moves % 10) == 0 ) {
slouken@47
   312
                        SDL_PrivateMouseMotion (SDL_RELEASED, 0, p.x, SDL_VideoSurface->h - p.y);
slouken@47
   313
                        moves = 0;
slouken@47
   314
                   }
slouken@47
   315
                   else {
slouken@47
   316
                        CGMouseDelta dx, dy;
slouken@47
   317
                        CGGetLastMouseDelta (&dx, &dy);
slouken@47
   318
                        SDL_PrivateMouseMotion (SDL_RELEASED, 1, dx, dy);
slouken@47
   319
                        moves++;
slouken@47
   320
                   }
slouken@47
   321
                }
slouken@47
   322
                break;
slouken@47
   323
            case NSScrollWheel:
slouken@47
   324
                {
slouken@47
   325
                    if (NSPointInRect(p, winRect)) {
slouken@47
   326
                        float dy;
slouken@47
   327
                        dy = [ event deltaY ];
slouken@47
   328
                        if ( dy > 0.0 ) /* Scroll up */
slouken@47
   329
                            SDL_PrivateMouseButton (SDL_PRESSED, 4, p.x, SDL_VideoSurface->h - p.y);
slouken@47
   330
                        else /* Scroll down */
slouken@47
   331
                            SDL_PrivateMouseButton (SDL_PRESSED, 5, p.x, SDL_VideoSurface->h - p.y);
slouken@47
   332
                    }
slouken@47
   333
                }
slouken@47
   334
                break;
slouken@47
   335
            case NSKeyUp:
slouken@47
   336
                QZ_DoKey (SDL_RELEASED, event);
slouken@47
   337
                break;
slouken@47
   338
            case NSKeyDown:
slouken@47
   339
                QZ_DoKey (SDL_PRESSED, event);
slouken@47
   340
                break;
slouken@47
   341
            case NSFlagsChanged:
slouken@47
   342
                QZ_DoModifiers( [ event modifierFlags ] );
slouken@47
   343
                break;
slouken@47
   344
            case NSMouseEntered: break;
slouken@47
   345
            case NSMouseExited: break;
slouken@47
   346
            case NSAppKitDefined:
slouken@47
   347
                switch ( [ event subtype ] ) {
slouken@47
   348
                case NSApplicationActivatedEventType:
slouken@47
   349
                    QZ_DoActivate (this, p);
slouken@47
   350
                    break;
slouken@47
   351
                case NSApplicationDeactivatedEventType:
slouken@47
   352
                    QZ_DoDeactivate (this);
slouken@47
   353
                    break;
slouken@47
   354
                case NSWindowMovedEventType:
slouken@47
   355
                    [ window sendEvent:event ];
slouken@47
   356
                    break;
slouken@47
   357
                }
slouken@47
   358
                break;
slouken@47
   359
            case NSApplicationDefined: break;
slouken@47
   360
            case NSPeriodic: break;
slouken@47
   361
            case NSCursorUpdate: break;
slouken@47
   362
            }
slouken@47
   363
        }
slouken@47
   364
       // [ pool release ];
slouken@47
   365
slouken@47
   366
      } while (event != nil);
slouken@47
   367
}
slouken@47
   368