src/video/quartz/SDL_QuartzEvents.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Aug 2001 23:57:39 +0000
changeset 155 2d162219f433
parent 117 aac75d5f7869
child 158 4382c38dfbee
permissions -rw-r--r--
Date: Thu, 16 Aug 2001 21:50:51 -0500 (EST)
From: Darrell Walisser <dwaliss1@purdue.edu>
Subject: Patch for video bugs + Max's additions

I've attached a patch for today's CVS that includes Max's virtual
mouse button fix as well as some other changes:

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