src/video/maccommon/SDL_macevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include <stdio.h>
slouken@0
    25
icculus@1133
    26
#if defined(__APPLE__) && defined(__MACH__)
icculus@1133
    27
#include <Carbon/Carbon.h>
icculus@1133
    28
#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
slouken@0
    29
#include <Carbon.h>
slouken@0
    30
#else
slouken@0
    31
#include <Script.h>
slouken@0
    32
#include <LowMem.h>
slouken@0
    33
#include <Devices.h>
slouken@0
    34
#include <DiskInit.h>
slouken@0
    35
#include <ToolUtils.h>
slouken@0
    36
#endif
slouken@0
    37
slouken@0
    38
#include "SDL_events.h"
slouken@0
    39
#include "SDL_video.h"
slouken@0
    40
#include "SDL_syswm.h"
slouken@1361
    41
#include "../../events/SDL_events_c.h"
slouken@1361
    42
#include "../../events/SDL_sysevents.h"
slouken@1361
    43
#include "../SDL_cursor_c.h"
slouken@0
    44
#include "SDL_macevents_c.h"
slouken@0
    45
#include "SDL_mackeys.h"
slouken@0
    46
#include "SDL_macmouse_c.h"
slouken@0
    47
slouken@0
    48
/* Define this to be able to collapse SDL windows.
slouken@0
    49
#define USE_APPEARANCE_MANAGER
slouken@0
    50
 */
slouken@0
    51
slouken@0
    52
/* Macintosh resource constants */
slouken@1662
    53
#define mApple	128             /* Apple menu resource */
slouken@1662
    54
#define iAbout	1               /* About menu item */
slouken@0
    55
slouken@0
    56
/* Functions to handle the About menu */
slouken@1668
    57
static void Mac_DoAppleMenu(_THIS, long item);
slouken@0
    58
slouken@0
    59
/* The translation table from a macintosh key scancode to a SDL keysym */
slouken@0
    60
static SDLKey MAC_keymap[256];
slouken@1668
    61
static SDL_keysym *TranslateKey(int scancode, int modifiers,
slouken@1668
    62
                                SDL_keysym * keysym, int pressed);
slouken@0
    63
slouken@0
    64
/* Handle activation and deactivation  -- returns whether an event was posted */
slouken@1662
    65
static int
slouken@1668
    66
Mac_HandleActivate(int activate)
slouken@0
    67
{
slouken@1662
    68
    if (activate) {
slouken@1662
    69
        /* Show the current SDL application cursor */
slouken@1668
    70
        SDL_SetCursor(NULL);
slouken@0
    71
slouken@1662
    72
        /* put our mask back case it changed during context switch */
slouken@1668
    73
        SetEventMask(everyEvent & ~autoKeyMask);
slouken@1662
    74
    } else {
slouken@0
    75
#if TARGET_API_MAC_CARBON
slouken@1662
    76
        {
slouken@1662
    77
            Cursor cursor;
slouken@1668
    78
            SetCursor(GetQDGlobalsArrow(&cursor));
slouken@1662
    79
        }
slouken@0
    80
#else
slouken@1668
    81
        SetCursor(&theQD->arrow);
slouken@0
    82
#endif
slouken@1662
    83
        if (!Mac_cursor_showing) {
slouken@1668
    84
            ShowCursor();
slouken@1662
    85
            Mac_cursor_showing = 1;
slouken@1662
    86
        }
slouken@1662
    87
    }
slouken@1668
    88
    return (SDL_PrivateAppActive(activate, SDL_APPINPUTFOCUS));
slouken@0
    89
}
slouken@0
    90
slouken@1662
    91
static void
slouken@1668
    92
myGlobalToLocal(_THIS, Point * pt)
slouken@0
    93
{
slouken@1662
    94
    if (SDL_VideoSurface && !(SDL_VideoSurface->flags & SDL_FULLSCREEN)) {
slouken@1662
    95
        GrafPtr saveport;
slouken@1668
    96
        GetPort(&saveport);
slouken@0
    97
#if TARGET_API_MAC_CARBON
slouken@1668
    98
        SetPort(GetWindowPort(SDL_Window));
slouken@0
    99
#else
slouken@1668
   100
        SetPort(SDL_Window);
slouken@0
   101
#endif
slouken@1668
   102
        GlobalToLocal(pt);
slouken@1668
   103
        SetPort(saveport);
slouken@1662
   104
    }
slouken@0
   105
}
slouken@0
   106
slouken@0
   107
/* The main MacOS event handler */
slouken@1662
   108
static int
slouken@1668
   109
Mac_HandleEvents(_THIS, int wait4it)
slouken@0
   110
{
slouken@1662
   111
    static int mouse_button = 1;
slouken@1662
   112
    int i;
slouken@1662
   113
    EventRecord event;
slouken@0
   114
slouken@0
   115
#if TARGET_API_MAC_CARBON
slouken@1662
   116
    /* There's no GetOSEvent() in the Carbon API. *sigh* */
slouken@0
   117
#define cooperative_multitasking 1
slouken@0
   118
#else
slouken@1662
   119
    int cooperative_multitasking;
slouken@1662
   120
    /* If we're running fullscreen, we can hog the MacOS events,
slouken@1662
   121
       otherwise we had better play nicely with the other apps.
slouken@1662
   122
     */
slouken@1662
   123
    if (this->screen && (this->screen->flags & SDL_FULLSCREEN)) {
slouken@1662
   124
        cooperative_multitasking = 0;
slouken@1662
   125
    } else {
slouken@1662
   126
        cooperative_multitasking = 1;
slouken@1662
   127
    }
slouken@0
   128
#endif
slouken@0
   129
slouken@1662
   130
    /* If we call WaitNextEvent(), MacOS will check other processes
slouken@1662
   131
     * and allow them to run, and perform other high-level processing.
slouken@1662
   132
     */
slouken@1662
   133
    if (cooperative_multitasking || wait4it) {
slouken@1662
   134
        UInt32 wait_time;
slouken@0
   135
slouken@1662
   136
        /* Are we polling or not? */
slouken@1662
   137
        if (wait4it) {
slouken@1662
   138
            wait_time = 2147483647;
slouken@1662
   139
        } else {
slouken@1662
   140
            wait_time = 0;
slouken@1662
   141
        }
slouken@1668
   142
        WaitNextEvent(everyEvent, &event, wait_time, nil);
slouken@1662
   143
    } else {
slouken@0
   144
#if ! TARGET_API_MAC_CARBON
slouken@1668
   145
        GetOSEvent(everyEvent, &event);
slouken@0
   146
#endif
slouken@1662
   147
    }
slouken@0
   148
slouken@0
   149
#if TARGET_API_MAC_CARBON
slouken@1662
   150
    /* for some reason, event.where isn't set ? */
slouken@1668
   151
    GetGlobalMouse(&event.where);
slouken@0
   152
#endif
slouken@155
   153
slouken@1662
   154
    /* Check for mouse motion */
slouken@1662
   155
    if ((event.where.h != last_where.h) || (event.where.v != last_where.v)) {
slouken@1662
   156
        Point pt;
slouken@1662
   157
        pt = last_where = event.where;
slouken@1668
   158
        myGlobalToLocal(this, &pt);
slouken@1668
   159
        SDL_PrivateMouseMotion(0, 0, pt.h, pt.v);
slouken@1662
   160
    }
slouken@0
   161
slouken@1662
   162
    /* Check the current state of the keyboard */
slouken@1668
   163
    if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
slouken@1662
   164
        KeyMap keys;
slouken@0
   165
slouken@1662
   166
        /* Check for special non-event keys */
slouken@1662
   167
        if (event.modifiers != last_mods) {
slouken@1662
   168
            static struct
slouken@1662
   169
            {
slouken@1662
   170
                EventModifiers mask;
slouken@1662
   171
                SDLKey key;
slouken@1662
   172
            } mods[] = {
slouken@1662
   173
                {
slouken@1662
   174
                alphaLock, SDLK_CAPSLOCK},
slouken@1662
   175
#if 0                           /* These are handled below in the GetKeys() code */
slouken@1662
   176
                {
slouken@1662
   177
                cmdKey, SDLK_LMETA}, {
slouken@1662
   178
                shiftKey, SDLK_LSHIFT}, {
slouken@1662
   179
                rightShiftKey, SDLK_RSHIFT}, {
slouken@1662
   180
                optionKey, SDLK_LALT}, {
slouken@1662
   181
                rightOptionKey, SDLK_RALT}, {
slouken@1662
   182
                controlKey, SDLK_LCTRL}, {
slouken@1662
   183
                rightControlKey, SDLK_RCTRL},
slouken@0
   184
#endif /* 0 */
slouken@1662
   185
                {
slouken@1662
   186
                0, 0}
slouken@1662
   187
            };
slouken@1662
   188
            SDL_keysym keysym;
slouken@1662
   189
            Uint8 mode;
slouken@1662
   190
            EventModifiers mod, mask;
slouken@0
   191
slouken@0
   192
slouken@1662
   193
            /* Set up the keyboard event */
slouken@1662
   194
            keysym.scancode = 0;
slouken@1662
   195
            keysym.sym = SDLK_UNKNOWN;
slouken@1662
   196
            keysym.mod = KMOD_NONE;
slouken@1662
   197
            keysym.unicode = 0;
slouken@0
   198
slouken@1662
   199
            /* See what has changed, and generate events */
slouken@1662
   200
            mod = event.modifiers;
slouken@1662
   201
            for (i = 0; mods[i].mask; ++i) {
slouken@1662
   202
                mask = mods[i].mask;
slouken@1662
   203
                if ((mod & mask) != (last_mods & mask)) {
slouken@1662
   204
                    keysym.sym = mods[i].key;
slouken@1662
   205
                    if ((mod & mask) || (mods[i].key == SDLK_CAPSLOCK)) {
slouken@1662
   206
                        mode = SDL_PRESSED;
slouken@1662
   207
                    } else {
slouken@1662
   208
                        mode = SDL_RELEASED;
slouken@1662
   209
                    }
slouken@1668
   210
                    SDL_PrivateKeyboard(mode, &keysym);
slouken@1662
   211
                }
slouken@1662
   212
            }
slouken@0
   213
slouken@1662
   214
            /* Save state for next time */
slouken@1662
   215
            last_mods = mod;
slouken@1662
   216
        }
slouken@1662
   217
slouken@1662
   218
        /* Check for normal event keys, but we have to scan the
slouken@1662
   219
           actual keyboard state because on Mac OS X a keydown event
slouken@1662
   220
           is immediately followed by a keyup event.
slouken@1662
   221
         */
slouken@1668
   222
        GetKeys(keys);
slouken@1662
   223
        if ((keys[0] != last_keys[0]) || (keys[1] != last_keys[1]) ||
slouken@1662
   224
            (keys[2] != last_keys[2]) || (keys[3] != last_keys[3])) {
slouken@1662
   225
            SDL_keysym keysym;
slouken@1662
   226
            int old_bit, new_bit;
slouken@0
   227
slouken@0
   228
#ifdef DEBUG_KEYBOARD
slouken@1668
   229
            fprintf(sterr, "New keys: 0x%x 0x%x 0x%x 0x%x\n",
slouken@1668
   230
                    new_keys[0], new_keys[1], new_keys[2], new_keys[3]);
slouken@0
   231
#endif
slouken@1662
   232
            for (i = 0; i < 128; ++i) {
slouken@1662
   233
                old_bit = (((Uint8 *) last_keys)[i / 8] >> (i % 8)) & 0x01;
slouken@1662
   234
                new_bit = (((Uint8 *) keys)[i / 8] >> (i % 8)) & 0x01;
slouken@1662
   235
                if (old_bit != new_bit) {
slouken@1662
   236
                    /* Post the keyboard event */
slouken@0
   237
#ifdef DEBUG_KEYBOARD
slouken@1668
   238
                    fprintf(stderr, "Scancode: 0x%2.2X\n", i);
slouken@0
   239
#endif
slouken@1668
   240
                    SDL_PrivateKeyboard(new_bit,
slouken@1668
   241
                                        TranslateKey(i,
slouken@1668
   242
                                                     event.
slouken@1668
   243
                                                     modifiers,
slouken@1668
   244
                                                     &keysym, new_bit));
slouken@1662
   245
                }
slouken@1662
   246
            }
slouken@0
   247
slouken@1662
   248
            /* Save state for next time */
slouken@1662
   249
            last_keys[0] = keys[0];
slouken@1662
   250
            last_keys[1] = keys[1];
slouken@1662
   251
            last_keys[2] = keys[2];
slouken@1662
   252
            last_keys[3] = keys[3];
slouken@1662
   253
        }
slouken@1662
   254
    }
slouken@0
   255
slouken@1662
   256
    /* Handle normal events */
slouken@1662
   257
    switch (event.what) {
slouken@1662
   258
    case mouseDown:
slouken@1662
   259
        {
slouken@1662
   260
            WindowRef win;
slouken@1662
   261
            short area;
slouken@1662
   262
slouken@1668
   263
            area = FindWindow(event.where, &win);
slouken@1662
   264
            /* Support switching between the SIOUX console
slouken@1662
   265
               and SDL_Window by clicking in the window.
slouken@1662
   266
             */
slouken@1668
   267
            if (win && (win != FrontWindow())) {
slouken@1668
   268
                SelectWindow(win);
slouken@1662
   269
            }
slouken@1662
   270
            switch (area) {
slouken@1662
   271
            case inMenuBar:    /* Only the apple menu exists */
slouken@1668
   272
                Mac_DoAppleMenu(this, MenuSelect(event.where));
slouken@1668
   273
                HiliteMenu(0);
slouken@1662
   274
                break;
slouken@1662
   275
            case inDrag:
slouken@0
   276
#if TARGET_API_MAC_CARBON
slouken@1668
   277
                DragWindow(win, event.where, NULL);
slouken@0
   278
#else
slouken@1668
   279
                DragWindow(win, event.where, &theQD->screenBits.bounds);
slouken@0
   280
#endif
slouken@1662
   281
                break;
slouken@1662
   282
            case inGoAway:
slouken@1668
   283
                if (TrackGoAway(win, event.where)) {
slouken@1668
   284
                    SDL_PrivateQuit();
slouken@1662
   285
                }
slouken@1662
   286
                break;
slouken@1662
   287
            case inContent:
slouken@1668
   288
                myGlobalToLocal(this, &event.where);
slouken@1662
   289
                /* Treat command-click as right mouse button */
slouken@1662
   290
                if (event.modifiers & optionKey) {
slouken@1662
   291
                    mouse_button = 2;
slouken@1662
   292
                } else if (event.modifiers & cmdKey) {
slouken@1662
   293
                    mouse_button = 3;
slouken@1662
   294
                } else {
slouken@1662
   295
                    mouse_button = 1;
slouken@1662
   296
                }
slouken@1668
   297
                SDL_PrivateMouseButton(SDL_PRESSED,
slouken@1668
   298
                                       mouse_button, event.where.h,
slouken@1668
   299
                                       event.where.v);
slouken@1662
   300
                break;
slouken@1662
   301
            case inGrow:
slouken@1662
   302
                {
slouken@1662
   303
                    int newSize;
slouken@0
   304
slouken@1662
   305
                    /* Don't allow resize if video mode isn't resizable */
slouken@1662
   306
                    if (!SDL_PublicSurface ||
slouken@1662
   307
                        !(SDL_PublicSurface->flags & SDL_RESIZABLE)) {
slouken@1662
   308
                        break;
slouken@1662
   309
                    }
slouken@0
   310
#if TARGET_API_MAC_CARBON
slouken@1668
   311
                    newSize = GrowWindow(win, event.where, NULL);
slouken@0
   312
#else
slouken@1662
   313
                    newSize =
slouken@1668
   314
                        GrowWindow(win, event.where,
slouken@1668
   315
                                   &theQD->screenBits.bounds);
slouken@0
   316
#endif
slouken@1662
   317
                    if (newSize) {
slouken@0
   318
#if !TARGET_API_MAC_CARBON
slouken@1668
   319
                        EraseRect(&theQD->screenBits.bounds);
slouken@0
   320
#endif
slouken@1668
   321
                        SizeWindow(win, LoWord(newSize), HiWord(newSize), 1);
slouken@1668
   322
                        SDL_PrivateResize(LoWord(newSize), HiWord(newSize));
slouken@1662
   323
                    }
slouken@1662
   324
                }
slouken@1662
   325
                break;
slouken@1662
   326
            case inZoomIn:
slouken@1662
   327
            case inZoomOut:
slouken@1668
   328
                if (TrackBox(win, event.where, area)) {
slouken@1662
   329
                    Rect rect;
slouken@0
   330
#if !TARGET_API_MAC_CARBON
slouken@1668
   331
                    EraseRect(&theQD->screenBits.bounds);
slouken@0
   332
#endif
slouken@1668
   333
                    ZoomWindow(win, area, 0);
slouken@1662
   334
                    if (area == inZoomIn) {
slouken@1668
   335
                        GetWindowUserState(SDL_Window, &rect);
slouken@1662
   336
                    } else {
slouken@1668
   337
                        GetWindowStandardState(SDL_Window, &rect);
slouken@1662
   338
                    }
slouken@1668
   339
                    SDL_PrivateResize(rect.right - rect.left,
slouken@1668
   340
                                      rect.bottom - rect.top);
slouken@1662
   341
                }
slouken@1662
   342
                break;
slouken@0
   343
#if TARGET_API_MAC_CARBON
slouken@1662
   344
            case inCollapseBox:
slouken@1668
   345
                if (TrackBox(win, event.where, area)) {
slouken@1668
   346
                    if (IsWindowCollapsable(win)) {
slouken@1668
   347
                        CollapseWindow(win, !IsWindowCollapsed(win));
slouken@1662
   348
                        /* There should be something done like in inGrow case, but... */
slouken@1662
   349
                    }
slouken@1662
   350
                }
slouken@1662
   351
                break;
slouken@0
   352
#endif /* TARGET_API_MAC_CARBON */
slouken@1662
   353
            case inSysWindow:
slouken@0
   354
#if TARGET_API_MAC_CARBON
slouken@1662
   355
                /* Never happens in Carbon? */
slouken@0
   356
#else
slouken@1668
   357
                SystemClick(&event, win);
slouken@0
   358
#endif
slouken@1662
   359
                break;
slouken@1662
   360
            default:
slouken@1662
   361
                break;
slouken@1662
   362
            }
slouken@1662
   363
        }
slouken@1662
   364
        break;
slouken@1662
   365
    case mouseUp:
slouken@1662
   366
        {
slouken@1668
   367
            myGlobalToLocal(this, &event.where);
slouken@1662
   368
            /* Release the mouse button we simulated in the last press.
slouken@1662
   369
               The drawback of this methos is we cannot press more than
slouken@1662
   370
               one button. However, this doesn't matter, since there is
slouken@1662
   371
               only a single logical mouse button, even if you have a
slouken@1662
   372
               multi-button mouse, this doesn't matter at all.
slouken@1662
   373
             */
slouken@1668
   374
            SDL_PrivateMouseButton(SDL_RELEASED,
slouken@1668
   375
                                   mouse_button, event.where.h,
slouken@1668
   376
                                   event.where.v);
slouken@1662
   377
        }
slouken@1662
   378
        break;
slouken@1662
   379
#if 0                           /* Handled above the switch statement */
slouken@1662
   380
    case keyDown:
slouken@1662
   381
        {
slouken@1662
   382
            SDL_keysym keysym;
slouken@0
   383
slouken@1668
   384
            SDL_PrivateKeyboard(SDL_PRESSED,
slouken@1668
   385
                                TranslateKey((event.
slouken@1668
   386
                                              message & keyCodeMask) >> 8
slouken@1668
   387
                                             event.modifiers, &keysym, 1));
slouken@1662
   388
        }
slouken@1662
   389
        break;
slouken@1662
   390
    case keyUp:
slouken@1662
   391
        {
slouken@1662
   392
            SDL_keysym keysym;
slouken@0
   393
slouken@1668
   394
            SDL_PrivateKeyboard(SDL_RELEASED,
slouken@1668
   395
                                TranslateKey((event.
slouken@1668
   396
                                              message & keyCodeMask) >> 8
slouken@1668
   397
                                             event.modifiers, &keysym, 0));
slouken@1662
   398
        }
slouken@1662
   399
        break;
slouken@0
   400
#endif
slouken@1662
   401
    case updateEvt:
slouken@1662
   402
        {
slouken@1668
   403
            BeginUpdate(SDL_Window);
slouken@1662
   404
#if SDL_VIDEO_OPENGL
slouken@1662
   405
            if (SDL_VideoSurface->flags & SDL_INTERNALOPENGL)
slouken@1668
   406
                SDL_GL_SwapBuffers();
slouken@1662
   407
            else
slouken@1662
   408
#endif
slouken@1662
   409
            if ((SDL_VideoSurface->flags & SDL_HWSURFACE) == SDL_SWSURFACE) {
slouken@1668
   410
                SDL_UpdateRect(SDL_VideoSurface, 0, 0, 0, 0);
slouken@1662
   411
            }
slouken@1668
   412
            EndUpdate(SDL_Window);
slouken@1662
   413
        }
slouken@1662
   414
        /* If this was an update event for the SIOUX console, we return 0
slouken@1662
   415
           in order to stop an endless series of updates being triggered.
slouken@1662
   416
         */
slouken@1662
   417
        if ((WindowRef) event.message != SDL_Window) {
slouken@1662
   418
            return 0;
slouken@1662
   419
        }
slouken@1662
   420
        break;
slouken@1662
   421
    case activateEvt:
slouken@1662
   422
        {
slouken@1668
   423
            Mac_HandleActivate(!!(event.modifiers & activeFlag));
slouken@1662
   424
        }
slouken@1662
   425
        break;
slouken@1662
   426
    case diskEvt:
slouken@1662
   427
        {
slouken@0
   428
#if TARGET_API_MAC_CARBON
slouken@1662
   429
            /* What are we supposed to do? */ ;
slouken@0
   430
#else
slouken@1662
   431
            if (((event.message >> 16) & 0xFFFF) != noErr) {
slouken@1662
   432
                Point spot;
slouken@1668
   433
                SetPt(&spot, 0x0070, 0x0050);
slouken@1668
   434
                DIBadMount(spot, event.message);
slouken@1662
   435
            }
slouken@0
   436
#endif
slouken@1662
   437
        }
slouken@1662
   438
        break;
slouken@1662
   439
    case osEvt:
slouken@1662
   440
        {
slouken@1662
   441
            switch ((event.message >> 24) & 0xFF) {
slouken@1662
   442
#if 0                           /* Handled above the switch statement */
slouken@1662
   443
            case mouseMovedMessage:
slouken@1662
   444
                {
slouken@1668
   445
                    myGlobalToLocal(this, &event.where);
slouken@1668
   446
                    SDL_PrivateMouseMotion(0, 0,
slouken@1668
   447
                                           event.where.h, event.where.v);
slouken@1662
   448
                }
slouken@1662
   449
                break;
slouken@0
   450
#endif /* 0 */
slouken@1662
   451
            case suspendResumeMessage:
slouken@1662
   452
                {
slouken@1668
   453
                    Mac_HandleActivate(!!(event.message & resumeFlag));
slouken@1662
   454
                }
slouken@1662
   455
                break;
slouken@1662
   456
            }
slouken@1662
   457
        }
slouken@1662
   458
        break;
slouken@1662
   459
    default:
slouken@1662
   460
        {
slouken@1662
   461
            ;
slouken@1662
   462
        }
slouken@1662
   463
        break;
slouken@1662
   464
    }
slouken@1662
   465
    return (event.what != nullEvent);
slouken@0
   466
}
slouken@0
   467
slouken@0
   468
slouken@1662
   469
void
slouken@1668
   470
Mac_PumpEvents(_THIS)
slouken@0
   471
{
slouken@1662
   472
    /* Process pending MacOS events */
slouken@1668
   473
    while (Mac_HandleEvents(this, 0)) {
slouken@1662
   474
        /* Loop and check again */ ;
slouken@1662
   475
    }
slouken@0
   476
}
slouken@0
   477
slouken@1662
   478
void
slouken@1668
   479
Mac_InitOSKeymap(_THIS)
slouken@0
   480
{
slouken@1662
   481
    const void *KCHRPtr;
slouken@1662
   482
    UInt32 state;
slouken@1662
   483
    UInt32 value;
slouken@1662
   484
    int i;
slouken@1662
   485
    int world = SDLK_WORLD_0;
slouken@0
   486
slouken@1662
   487
    /* Map the MAC keysyms */
slouken@1668
   488
    for (i = 0; i < SDL_arraysize(MAC_keymap); ++i)
slouken@1662
   489
        MAC_keymap[i] = SDLK_UNKNOWN;
slouken@0
   490
slouken@1662
   491
    /* Defined MAC_* constants */
slouken@1662
   492
    MAC_keymap[MK_ESCAPE] = SDLK_ESCAPE;
slouken@1662
   493
    MAC_keymap[MK_F1] = SDLK_F1;
slouken@1662
   494
    MAC_keymap[MK_F2] = SDLK_F2;
slouken@1662
   495
    MAC_keymap[MK_F3] = SDLK_F3;
slouken@1662
   496
    MAC_keymap[MK_F4] = SDLK_F4;
slouken@1662
   497
    MAC_keymap[MK_F5] = SDLK_F5;
slouken@1662
   498
    MAC_keymap[MK_F6] = SDLK_F6;
slouken@1662
   499
    MAC_keymap[MK_F7] = SDLK_F7;
slouken@1662
   500
    MAC_keymap[MK_F8] = SDLK_F8;
slouken@1662
   501
    MAC_keymap[MK_F9] = SDLK_F9;
slouken@1662
   502
    MAC_keymap[MK_F10] = SDLK_F10;
slouken@1662
   503
    MAC_keymap[MK_F11] = SDLK_F11;
slouken@1662
   504
    MAC_keymap[MK_F12] = SDLK_F12;
slouken@1662
   505
    MAC_keymap[MK_PRINT] = SDLK_PRINT;
slouken@1662
   506
    MAC_keymap[MK_SCROLLOCK] = SDLK_SCROLLOCK;
slouken@1662
   507
    MAC_keymap[MK_PAUSE] = SDLK_PAUSE;
slouken@1662
   508
    MAC_keymap[MK_POWER] = SDLK_POWER;
slouken@1662
   509
    MAC_keymap[MK_BACKQUOTE] = SDLK_BACKQUOTE;
slouken@1662
   510
    MAC_keymap[MK_1] = SDLK_1;
slouken@1662
   511
    MAC_keymap[MK_2] = SDLK_2;
slouken@1662
   512
    MAC_keymap[MK_3] = SDLK_3;
slouken@1662
   513
    MAC_keymap[MK_4] = SDLK_4;
slouken@1662
   514
    MAC_keymap[MK_5] = SDLK_5;
slouken@1662
   515
    MAC_keymap[MK_6] = SDLK_6;
slouken@1662
   516
    MAC_keymap[MK_7] = SDLK_7;
slouken@1662
   517
    MAC_keymap[MK_8] = SDLK_8;
slouken@1662
   518
    MAC_keymap[MK_9] = SDLK_9;
slouken@1662
   519
    MAC_keymap[MK_0] = SDLK_0;
slouken@1662
   520
    MAC_keymap[MK_MINUS] = SDLK_MINUS;
slouken@1662
   521
    MAC_keymap[MK_EQUALS] = SDLK_EQUALS;
slouken@1662
   522
    MAC_keymap[MK_BACKSPACE] = SDLK_BACKSPACE;
slouken@1662
   523
    MAC_keymap[MK_INSERT] = SDLK_INSERT;
slouken@1662
   524
    MAC_keymap[MK_HOME] = SDLK_HOME;
slouken@1662
   525
    MAC_keymap[MK_PAGEUP] = SDLK_PAGEUP;
slouken@1662
   526
    MAC_keymap[MK_NUMLOCK] = SDLK_NUMLOCK;
slouken@1662
   527
    MAC_keymap[MK_KP_EQUALS] = SDLK_KP_EQUALS;
slouken@1662
   528
    MAC_keymap[MK_KP_DIVIDE] = SDLK_KP_DIVIDE;
slouken@1662
   529
    MAC_keymap[MK_KP_MULTIPLY] = SDLK_KP_MULTIPLY;
slouken@1662
   530
    MAC_keymap[MK_TAB] = SDLK_TAB;
slouken@1662
   531
    MAC_keymap[MK_q] = SDLK_q;
slouken@1662
   532
    MAC_keymap[MK_w] = SDLK_w;
slouken@1662
   533
    MAC_keymap[MK_e] = SDLK_e;
slouken@1662
   534
    MAC_keymap[MK_r] = SDLK_r;
slouken@1662
   535
    MAC_keymap[MK_t] = SDLK_t;
slouken@1662
   536
    MAC_keymap[MK_y] = SDLK_y;
slouken@1662
   537
    MAC_keymap[MK_u] = SDLK_u;
slouken@1662
   538
    MAC_keymap[MK_i] = SDLK_i;
slouken@1662
   539
    MAC_keymap[MK_o] = SDLK_o;
slouken@1662
   540
    MAC_keymap[MK_p] = SDLK_p;
slouken@1662
   541
    MAC_keymap[MK_LEFTBRACKET] = SDLK_LEFTBRACKET;
slouken@1662
   542
    MAC_keymap[MK_RIGHTBRACKET] = SDLK_RIGHTBRACKET;
slouken@1662
   543
    MAC_keymap[MK_BACKSLASH] = SDLK_BACKSLASH;
slouken@1662
   544
    MAC_keymap[MK_DELETE] = SDLK_DELETE;
slouken@1662
   545
    MAC_keymap[MK_END] = SDLK_END;
slouken@1662
   546
    MAC_keymap[MK_PAGEDOWN] = SDLK_PAGEDOWN;
slouken@1662
   547
    MAC_keymap[MK_KP7] = SDLK_KP7;
slouken@1662
   548
    MAC_keymap[MK_KP8] = SDLK_KP8;
slouken@1662
   549
    MAC_keymap[MK_KP9] = SDLK_KP9;
slouken@1662
   550
    MAC_keymap[MK_KP_MINUS] = SDLK_KP_MINUS;
slouken@1662
   551
    MAC_keymap[MK_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@1662
   552
    MAC_keymap[MK_a] = SDLK_a;
slouken@1662
   553
    MAC_keymap[MK_s] = SDLK_s;
slouken@1662
   554
    MAC_keymap[MK_d] = SDLK_d;
slouken@1662
   555
    MAC_keymap[MK_f] = SDLK_f;
slouken@1662
   556
    MAC_keymap[MK_g] = SDLK_g;
slouken@1662
   557
    MAC_keymap[MK_h] = SDLK_h;
slouken@1662
   558
    MAC_keymap[MK_j] = SDLK_j;
slouken@1662
   559
    MAC_keymap[MK_k] = SDLK_k;
slouken@1662
   560
    MAC_keymap[MK_l] = SDLK_l;
slouken@1662
   561
    MAC_keymap[MK_SEMICOLON] = SDLK_SEMICOLON;
slouken@1662
   562
    MAC_keymap[MK_QUOTE] = SDLK_QUOTE;
slouken@1662
   563
    MAC_keymap[MK_RETURN] = SDLK_RETURN;
slouken@1662
   564
    MAC_keymap[MK_KP4] = SDLK_KP4;
slouken@1662
   565
    MAC_keymap[MK_KP5] = SDLK_KP5;
slouken@1662
   566
    MAC_keymap[MK_KP6] = SDLK_KP6;
slouken@1662
   567
    MAC_keymap[MK_KP_PLUS] = SDLK_KP_PLUS;
slouken@1662
   568
    MAC_keymap[MK_LSHIFT] = SDLK_LSHIFT;
slouken@1662
   569
    MAC_keymap[MK_z] = SDLK_z;
slouken@1662
   570
    MAC_keymap[MK_x] = SDLK_x;
slouken@1662
   571
    MAC_keymap[MK_c] = SDLK_c;
slouken@1662
   572
    MAC_keymap[MK_v] = SDLK_v;
slouken@1662
   573
    MAC_keymap[MK_b] = SDLK_b;
slouken@1662
   574
    MAC_keymap[MK_n] = SDLK_n;
slouken@1662
   575
    MAC_keymap[MK_m] = SDLK_m;
slouken@1662
   576
    MAC_keymap[MK_COMMA] = SDLK_COMMA;
slouken@1662
   577
    MAC_keymap[MK_PERIOD] = SDLK_PERIOD;
slouken@1662
   578
    MAC_keymap[MK_SLASH] = SDLK_SLASH;
slouken@1662
   579
#if 0                           /* These are the same as the left versions - use left by default */
slouken@1662
   580
    MAC_keymap[MK_RSHIFT] = SDLK_RSHIFT;
slouken@0
   581
#endif
slouken@1662
   582
    MAC_keymap[MK_UP] = SDLK_UP;
slouken@1662
   583
    MAC_keymap[MK_KP1] = SDLK_KP1;
slouken@1662
   584
    MAC_keymap[MK_KP2] = SDLK_KP2;
slouken@1662
   585
    MAC_keymap[MK_KP3] = SDLK_KP3;
slouken@1662
   586
    MAC_keymap[MK_KP_ENTER] = SDLK_KP_ENTER;
slouken@1662
   587
    MAC_keymap[MK_LCTRL] = SDLK_LCTRL;
slouken@1662
   588
    MAC_keymap[MK_LALT] = SDLK_LALT;
slouken@1662
   589
    MAC_keymap[MK_LMETA] = SDLK_LMETA;
slouken@1662
   590
    MAC_keymap[MK_SPACE] = SDLK_SPACE;
slouken@1662
   591
#if 0                           /* These are the same as the left versions - use left by default */
slouken@1662
   592
    MAC_keymap[MK_RMETA] = SDLK_RMETA;
slouken@1662
   593
    MAC_keymap[MK_RALT] = SDLK_RALT;
slouken@1662
   594
    MAC_keymap[MK_RCTRL] = SDLK_RCTRL;
slouken@0
   595
#endif
slouken@1662
   596
    MAC_keymap[MK_LEFT] = SDLK_LEFT;
slouken@1662
   597
    MAC_keymap[MK_DOWN] = SDLK_DOWN;
slouken@1662
   598
    MAC_keymap[MK_RIGHT] = SDLK_RIGHT;
slouken@1662
   599
    MAC_keymap[MK_KP0] = SDLK_KP0;
slouken@1662
   600
    MAC_keymap[MK_KP_PERIOD] = SDLK_KP_PERIOD;
slouken@10
   601
slouken@10
   602
#if defined(__APPLE__) && defined(__MACH__)
slouken@1662
   603
    /* Wierd, these keys are on my iBook under Mac OS X
slouken@1662
   604
       Note that the left arrow keysym is the same as left ctrl!?
slouken@1662
   605
     */
slouken@1662
   606
    MAC_keymap[MK_IBOOK_ENTER] = SDLK_KP_ENTER;
slouken@1662
   607
    MAC_keymap[MK_IBOOK_RIGHT] = SDLK_RIGHT;
slouken@1662
   608
    MAC_keymap[MK_IBOOK_DOWN] = SDLK_DOWN;
slouken@1662
   609
    MAC_keymap[MK_IBOOK_UP] = SDLK_UP;
slouken@1662
   610
    MAC_keymap[MK_IBOOK_LEFT] = SDLK_LEFT;
slouken@1621
   611
#endif /* Mac OS X */
slouken@198
   612
slouken@1662
   613
    /* Up there we setup a static scancode->keysym map. However, it will not
slouken@1662
   614
     * work very well on international keyboard. Hence we now query MacOS
slouken@1662
   615
     * for its own keymap to adjust our own mapping table. However, this is
slouken@1662
   616
     * bascially only useful for ascii char keys. This is also the reason
slouken@1662
   617
     * why we keep the static table, too.
slouken@1662
   618
     */
slouken@1662
   619
slouken@1662
   620
    /* Get a pointer to the systems cached KCHR */
slouken@1668
   621
    KCHRPtr = (void *) GetScriptManagerVariable(smKCHRCache);
slouken@1662
   622
    if (KCHRPtr) {
slouken@1662
   623
        /* Loop over all 127 possible scan codes */
slouken@1662
   624
        for (i = 0; i < 0x7F; i++) {
slouken@1662
   625
            /* We pretend a clean start to begin with (i.e. no dead keys active */
slouken@1662
   626
            state = 0;
slouken@1662
   627
slouken@1662
   628
            /* Now translate the key code to a key value */
slouken@1668
   629
            value = KeyTranslate(KCHRPtr, i, &state) & 0xff;
slouken@1662
   630
slouken@1662
   631
            /* If the state become 0, it was a dead key. We need to translate again,
slouken@1662
   632
               passing in the new state, to get the actual key value */
slouken@1662
   633
            if (state != 0)
slouken@1668
   634
                value = KeyTranslate(KCHRPtr, i, &state) & 0xff;
slouken@1662
   635
slouken@1662
   636
            /* Now we should have an ascii value, or 0. Try to figure out to which SDL symbol it maps */
slouken@1662
   637
            if (value >= 128)   /* Some non-ASCII char, map it to SDLK_WORLD_* */
slouken@1662
   638
                MAC_keymap[i] = world++;
slouken@1662
   639
            else if (value >= 32)       /* non-control ASCII char */
slouken@1662
   640
                MAC_keymap[i] = value;
slouken@1662
   641
        }
slouken@1662
   642
    }
slouken@1662
   643
slouken@1662
   644
    /* The keypad codes are re-setup here, because the loop above cannot
slouken@1662
   645
     * distinguish between a key on the keypad and a regular key. We maybe
slouken@1662
   646
     * could get around this problem in another fashion: NSEvent's flags
slouken@1662
   647
     * include a "NSNumericPadKeyMask" bit; we could check that and modify
slouken@1662
   648
     * the symbol we return on the fly. However, this flag seems to exhibit
slouken@1662
   649
     * some weird behaviour related to the num lock key
slouken@1662
   650
     */
slouken@1662
   651
    MAC_keymap[MK_KP0] = SDLK_KP0;
slouken@1662
   652
    MAC_keymap[MK_KP1] = SDLK_KP1;
slouken@1662
   653
    MAC_keymap[MK_KP2] = SDLK_KP2;
slouken@1662
   654
    MAC_keymap[MK_KP3] = SDLK_KP3;
slouken@1662
   655
    MAC_keymap[MK_KP4] = SDLK_KP4;
slouken@1662
   656
    MAC_keymap[MK_KP5] = SDLK_KP5;
slouken@1662
   657
    MAC_keymap[MK_KP6] = SDLK_KP6;
slouken@1662
   658
    MAC_keymap[MK_KP7] = SDLK_KP7;
slouken@1662
   659
    MAC_keymap[MK_KP8] = SDLK_KP8;
slouken@1662
   660
    MAC_keymap[MK_KP9] = SDLK_KP9;
slouken@1662
   661
    MAC_keymap[MK_KP_MINUS] = SDLK_KP_MINUS;
slouken@1662
   662
    MAC_keymap[MK_KP_PLUS] = SDLK_KP_PLUS;
slouken@1662
   663
    MAC_keymap[MK_KP_PERIOD] = SDLK_KP_PERIOD;
slouken@1662
   664
    MAC_keymap[MK_KP_EQUALS] = SDLK_KP_EQUALS;
slouken@1662
   665
    MAC_keymap[MK_KP_DIVIDE] = SDLK_KP_DIVIDE;
slouken@1662
   666
    MAC_keymap[MK_KP_MULTIPLY] = SDLK_KP_MULTIPLY;
slouken@1662
   667
    MAC_keymap[MK_KP_ENTER] = SDLK_KP_ENTER;
slouken@0
   668
}
slouken@0
   669
slouken@1662
   670
static SDL_keysym *
slouken@1668
   671
TranslateKey(int scancode, int modifiers, SDL_keysym * keysym, int pressed)
slouken@0
   672
{
slouken@1662
   673
    /* Set the keysym information */
slouken@1662
   674
    keysym->scancode = scancode;
slouken@1662
   675
    keysym->sym = MAC_keymap[keysym->scancode];
slouken@1662
   676
    keysym->mod = KMOD_NONE;
slouken@1662
   677
    keysym->unicode = 0;
slouken@1662
   678
    if (pressed && SDL_TranslateUNICODE) {
slouken@1662
   679
        static unsigned long state = 0;
slouken@1662
   680
        static Ptr keymap = nil;
slouken@1662
   681
        Ptr new_keymap;
slouken@0
   682
slouken@1662
   683
        /* Get the current keyboard map resource */
slouken@1668
   684
        new_keymap = (Ptr) GetScriptManagerVariable(smKCHRCache);
slouken@1662
   685
        if (new_keymap != keymap) {
slouken@1662
   686
            keymap = new_keymap;
slouken@1662
   687
            state = 0;
slouken@1662
   688
        }
slouken@1668
   689
        keysym->unicode = KeyTranslate(keymap,
slouken@1668
   690
                                       keysym->scancode | modifiers,
slouken@1668
   691
                                       &state) & 0xFFFF;
slouken@1662
   692
    }
slouken@1662
   693
    return (keysym);
slouken@0
   694
}
slouken@0
   695
slouken@1662
   696
void
slouken@1668
   697
Mac_InitEvents(_THIS)
slouken@0
   698
{
slouken@1662
   699
    /* Create apple menu bar */
slouken@1668
   700
    apple_menu = GetMenu(mApple);
slouken@1662
   701
    if (apple_menu != nil) {
slouken@1668
   702
        AppendResMenu(apple_menu, 'DRVR');
slouken@1668
   703
        InsertMenu(apple_menu, 0);
slouken@1662
   704
    }
slouken@1668
   705
    DrawMenuBar();
slouken@0
   706
slouken@1662
   707
    /* Get rid of spurious events at startup */
slouken@1668
   708
    FlushEvents(everyEvent, 0);
slouken@1662
   709
slouken@1662
   710
    /* Allow every event but keyrepeat */
slouken@1668
   711
    SetEventMask(everyEvent & ~autoKeyMask);
slouken@0
   712
}
slouken@0
   713
slouken@1662
   714
void
slouken@1668
   715
Mac_QuitEvents(_THIS)
slouken@0
   716
{
slouken@1668
   717
    ClearMenuBar();
slouken@1662
   718
    if (apple_menu != nil) {
slouken@1668
   719
        ReleaseResource((char **) apple_menu);
slouken@1662
   720
    }
slouken@0
   721
slouken@1662
   722
    /* Clean up pending events */
slouken@1668
   723
    FlushEvents(everyEvent, 0);
slouken@0
   724
}
slouken@0
   725
slouken@1662
   726
static void
slouken@1668
   727
Mac_DoAppleMenu(_THIS, long choice)
slouken@0
   728
{
slouken@1662
   729
#if !TARGET_API_MAC_CARBON      /* No Apple menu in OS X */
slouken@1662
   730
    short menu, item;
slouken@0
   731
slouken@1662
   732
    item = (choice & 0xFFFF);
slouken@1662
   733
    choice >>= 16;
slouken@1662
   734
    menu = (choice & 0xFFFF);
slouken@1662
   735
slouken@1662
   736
    switch (menu) {
slouken@1662
   737
    case mApple:
slouken@1662
   738
        {
slouken@1662
   739
            switch (item) {
slouken@1662
   740
            case iAbout:
slouken@1662
   741
                {
slouken@1662
   742
                    /* Run the about box */ ;
slouken@1662
   743
                }
slouken@1662
   744
                break;
slouken@1662
   745
            default:
slouken@1662
   746
                {
slouken@1662
   747
                    Str255 name;
slouken@1662
   748
slouken@1668
   749
                    GetMenuItemText(apple_menu, item, name);
slouken@1668
   750
                    OpenDeskAcc(name);
slouken@1662
   751
                }
slouken@1662
   752
                break;
slouken@1662
   753
            }
slouken@1662
   754
        }
slouken@1662
   755
        break;
slouken@1662
   756
    default:
slouken@1662
   757
        {
slouken@1662
   758
            /* Ignore other menus */ ;
slouken@1662
   759
        }
slouken@1662
   760
    }
slouken@0
   761
#endif /* !TARGET_API_MAC_CARBON */
slouken@0
   762
}
slouken@0
   763
slouken@0
   764
#if !TARGET_API_MAC_CARBON
slouken@0
   765
/* Since we don't initialize QuickDraw, we need to get a pointer to qd */
slouken@0
   766
QDGlobals *theQD = NULL;
icculus@1133
   767
#endif
slouken@0
   768
slouken@0
   769
/* Exported to the macmain code */
slouken@1662
   770
void
slouken@1668
   771
SDL_InitQuickDraw(struct QDGlobals *the_qd)
slouken@0
   772
{
icculus@1133
   773
#if !TARGET_API_MAC_CARBON
slouken@1662
   774
    theQD = the_qd;
icculus@1133
   775
#endif
slouken@0
   776
}
slouken@1662
   777
slouken@1662
   778
/* vi: set ts=4 sw=4 expandtab: */