src/video/macrom/SDL_romvideo.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
icculus@1133
    24
#if defined(__APPLE__) && defined(__MACH__)
icculus@1133
    25
#include <Carbon/Carbon.h>
icculus@1133
    26
#if USE_QUICKTIME
icculus@1133
    27
#include <QuickTime/Movies.h>
icculus@1133
    28
#endif
icculus@1133
    29
#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
slouken@0
    30
#include <Carbon.h>
slouken@0
    31
/* The fullscreen code requires the QuickTime framework, and the window
slouken@1621
    32
   is still at the back on Mac OS X, which is where this code is needed.
slouken@0
    33
 */
slouken@0
    34
#if USE_QUICKTIME
icculus@1133
    35
#include <Movies.h>
slouken@0
    36
#endif
slouken@0
    37
#else
slouken@1659
    38
#include <Quickdraw.h>
slouken@0
    39
#include <LowMem.h>
slouken@0
    40
#include <Gestalt.h>
slouken@0
    41
#include <Devices.h>
slouken@0
    42
#include <DiskInit.h>
slouken@0
    43
#include <QDOffscreen.h>
slouken@0
    44
#endif
slouken@0
    45
slouken@0
    46
#include "SDL_video.h"
slouken@0
    47
#include "SDL_syswm.h"
slouken@1361
    48
#include "../SDL_sysvideo.h"
slouken@0
    49
#include "SDL_romvideo.h"
slouken@1424
    50
#include "../maccommon/SDL_macgl_c.h"
slouken@1424
    51
#include "../maccommon/SDL_macwm_c.h"
slouken@1424
    52
#include "../maccommon/SDL_macmouse_c.h"
slouken@1424
    53
#include "../maccommon/SDL_macevents_c.h"
slouken@0
    54
slouken@0
    55
/* Initialization/Query functions */
slouken@1668
    56
static int ROM_VideoInit(_THIS, SDL_PixelFormat * vformat);
slouken@1668
    57
static SDL_Rect **ROM_ListModes(_THIS, SDL_PixelFormat * format,
slouken@1668
    58
                                Uint32 flags);
slouken@1668
    59
static SDL_Surface *ROM_SetVideoMode(_THIS, SDL_Surface * current, int width,
slouken@1668
    60
                                     int height, int bpp, Uint32 flags);
slouken@1668
    61
static int ROM_SetColors(_THIS, int firstcolor, int ncolors,
slouken@1668
    62
                         SDL_Color * colors);
slouken@1668
    63
static void ROM_VideoQuit(_THIS);
slouken@0
    64
slouken@0
    65
/* Hardware surface functions */
slouken@1668
    66
static int ROM_AllocHWSurface(_THIS, SDL_Surface * surface);
slouken@1668
    67
static int ROM_LockHWSurface(_THIS, SDL_Surface * surface);
slouken@1668
    68
static void ROM_UnlockHWSurface(_THIS, SDL_Surface * surface);
slouken@1668
    69
static void ROM_FreeHWSurface(_THIS, SDL_Surface * surface);
slouken@0
    70
slouken@1662
    71
#if !TARGET_API_MAC_CARBON      /* This seems not to be available? -sts Aug 2000 */
slouken@0
    72
/* Saved state for the menu bar */
slouken@1662
    73
static RgnHandle gSaveGrayRgn = nil;
slouken@1662
    74
static short gSaveMenuBar = 0;
slouken@1662
    75
static Boolean gSaveCSVis = true;
slouken@0
    76
slouken@0
    77
#if powerc
slouken@0
    78
/* Mixed mode glue to activate the 68K emulator and twiddle a register */
slouken@0
    79
#define ONEWORDSTUB(p1) \
slouken@0
    80
		{ 0x41FA, 0x0010, 0x209F, (p1), 0x41FA, \
slouken@0
    81
		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
slouken@0
    82
slouken@0
    83
#define TWOWORDSTUB(p1,p2) \
slouken@0
    84
		{ 0x41FA, 0x0012, 0x209F, (p1), (p2), 0x41FA, \
slouken@0
    85
		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
slouken@0
    86
slouken@0
    87
#define THREEWORDSTUB(p1,p2,p3) \
slouken@0
    88
		{ 0x41FA, 0x0014, 0x209F, (p1), (p2), (p3), 0x41FA, \
slouken@0
    89
		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
slouken@0
    90
slouken@0
    91
/* ControlStrip inline glue for PowerPC */
slouken@1662
    92
static pascal Boolean
slouken@1668
    93
SBIsControlStripVisible(void)
slouken@0
    94
{
slouken@1668
    95
    static short procData[] = TWOWORDSTUB(0x7000, 0xAAF2);
slouken@1662
    96
    ProcInfoType procInfo = kD0DispatchedPascalStackBased
slouken@1668
    97
        | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
slouken@1668
    98
        | DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode);
slouken@1662
    99
slouken@1662
   100
    return ((Boolean)
slouken@1668
   101
            CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x00));
slouken@0
   102
}
slouken@0
   103
slouken@1662
   104
static pascal void
slouken@1668
   105
SBShowHideControlStrip(Boolean showIt)
slouken@0
   106
{
slouken@1668
   107
    static short procData[] = THREEWORDSTUB(0x303C, 0x0101, 0xAAF2);
slouken@1662
   108
    ProcInfoType procInfo = kD0DispatchedPascalStackBased
slouken@1668
   109
        | DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode)
slouken@1668
   110
        | DISPATCHED_STACK_ROUTINE_PARAMETER(1,
slouken@1668
   111
                                             SIZE_CODE(sizeof(Boolean)));
slouken@0
   112
slouken@1668
   113
    CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x01, showIt);
slouken@0
   114
}
slouken@0
   115
#endif /* powerc */
slouken@0
   116
#endif /* !TARGET_API_MAC_CARBON */
slouken@0
   117
slouken@0
   118
/* Macintosh toolbox driver bootstrap functions */
slouken@0
   119
slouken@1662
   120
static int
slouken@1668
   121
ROM_Available(void)
slouken@0
   122
{
slouken@1662
   123
    return (1);
slouken@0
   124
}
slouken@0
   125
slouken@1662
   126
static void
slouken@1668
   127
ROM_DeleteDevice(SDL_VideoDevice * device)
slouken@0
   128
{
slouken@1668
   129
    SDL_free(device->hidden);
slouken@1668
   130
    SDL_free(device);
slouken@0
   131
}
slouken@0
   132
slouken@1662
   133
static SDL_VideoDevice *
slouken@1668
   134
ROM_CreateDevice(int devindex)
slouken@0
   135
{
slouken@1662
   136
    SDL_VideoDevice *device;
slouken@0
   137
slouken@1662
   138
    /* Initialize all variables that we clean on shutdown */
slouken@1668
   139
    device = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice));
slouken@1662
   140
    if (device) {
slouken@1668
   141
        SDL_memset(device, 0, (sizeof *device));
slouken@1662
   142
        device->hidden = (struct SDL_PrivateVideoData *)
slouken@1668
   143
            SDL_malloc((sizeof *device->hidden));
slouken@1662
   144
    }
slouken@1662
   145
    if ((device == NULL) || (device->hidden == NULL)) {
slouken@1668
   146
        SDL_OutOfMemory();
slouken@1662
   147
        if (device) {
slouken@1668
   148
            SDL_free(device);
slouken@1662
   149
        }
slouken@1662
   150
        return (0);
slouken@1662
   151
    }
slouken@1668
   152
    SDL_memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
   153
slouken@1662
   154
    /* Set the function pointers */
slouken@1662
   155
    device->VideoInit = ROM_VideoInit;
slouken@1662
   156
    device->ListModes = ROM_ListModes;
slouken@1662
   157
    device->SetVideoMode = ROM_SetVideoMode;
slouken@1662
   158
    device->SetColors = ROM_SetColors;
slouken@1662
   159
    device->UpdateRects = NULL;
slouken@1662
   160
    device->VideoQuit = ROM_VideoQuit;
slouken@1662
   161
    device->AllocHWSurface = ROM_AllocHWSurface;
slouken@1662
   162
    device->CheckHWBlit = NULL;
slouken@1662
   163
    device->FillHWRect = NULL;
slouken@1662
   164
    device->SetHWColorKey = NULL;
slouken@1662
   165
    device->SetHWAlpha = NULL;
slouken@1662
   166
    device->LockHWSurface = ROM_LockHWSurface;
slouken@1662
   167
    device->UnlockHWSurface = ROM_UnlockHWSurface;
slouken@1662
   168
    device->FlipHWSurface = NULL;
slouken@1662
   169
    device->FreeHWSurface = ROM_FreeHWSurface;
slouken@1361
   170
#if SDL_VIDEO_OPENGL
slouken@1662
   171
    device->GL_MakeCurrent = Mac_GL_MakeCurrent;
slouken@1662
   172
    device->GL_SwapBuffers = Mac_GL_SwapBuffers;
slouken@1662
   173
    device->GL_LoadLibrary = Mac_GL_LoadLibrary;
slouken@1662
   174
    device->GL_GetProcAddress = Mac_GL_GetProcAddress;
slouken@1662
   175
#endif // Have OpenGL
slouken@1662
   176
    device->SetCaption = Mac_SetCaption;
slouken@1662
   177
    device->SetIcon = NULL;
slouken@1662
   178
    device->IconifyWindow = NULL;
slouken@1662
   179
    device->GrabInput = NULL;
slouken@1662
   180
    device->GetWMInfo = NULL;
slouken@1662
   181
    device->FreeWMCursor = Mac_FreeWMCursor;
slouken@1662
   182
    device->CreateWMCursor = Mac_CreateWMCursor;
slouken@1662
   183
    device->ShowWMCursor = Mac_ShowWMCursor;
slouken@1662
   184
    device->WarpWMCursor = Mac_WarpWMCursor;
slouken@1662
   185
    device->InitOSKeymap = Mac_InitOSKeymap;
slouken@1662
   186
    device->PumpEvents = Mac_PumpEvents;
slouken@0
   187
slouken@1662
   188
    device->free = ROM_DeleteDevice;
slouken@0
   189
slouken@1662
   190
    return device;
slouken@0
   191
}
slouken@0
   192
slouken@0
   193
VideoBootStrap TOOLBOX_bootstrap = {
slouken@1662
   194
    "toolbox", "MacOS ROM Toolbox",
slouken@1662
   195
    ROM_Available, ROM_CreateDevice
slouken@0
   196
};
slouken@0
   197
slouken@0
   198
slouken@1662
   199
static int
slouken@1668
   200
ROM_VideoInit(_THIS, SDL_PixelFormat * vformat)
slouken@0
   201
{
slouken@1662
   202
    long info;
slouken@0
   203
slouken@1662
   204
    /* Check out some things about the system */
slouken@1668
   205
    Gestalt(gestaltQuickdrawVersion, &info);
slouken@1662
   206
    if (info == gestaltOriginalQD) {
slouken@1668
   207
        SDL_SetError("Color Quickdraw not available");
slouken@1662
   208
        return (-1);
slouken@1662
   209
    }
slouken@0
   210
slouken@1662
   211
    /* Start ROMintosh events */
slouken@1668
   212
    Mac_InitEvents(this);
slouken@0
   213
slouken@1662
   214
    /* Get a handle to the main monitor */
slouken@1668
   215
    SDL_Display = GetMainDevice();
slouken@1545
   216
slouken@1662
   217
    /* Determine the current screen size */
slouken@1662
   218
    this->info.current_w = (**SDL_Display).gdRect.right;
slouken@1662
   219
    this->info.current_h = (**SDL_Display).gdRect.bottom;
slouken@0
   220
slouken@1662
   221
    /* Determine pixel format */
slouken@1662
   222
    vformat->BitsPerPixel = (**(**SDL_Display).gdPMap).pixelSize;
slouken@1662
   223
    switch (vformat->BitsPerPixel) {
slouken@1662
   224
    case 16:                   /* 5-5-5 RGB */
slouken@1662
   225
        vformat->Rmask = 0x00007c00;
slouken@1662
   226
        vformat->Gmask = 0x000003e0;
slouken@1662
   227
        vformat->Bmask = 0x0000001f;
slouken@1662
   228
        break;
slouken@1662
   229
    default:
slouken@1662
   230
        break;
slouken@1662
   231
    }
slouken@0
   232
slouken@1662
   233
    /* Create our palette */
slouken@1668
   234
    SDL_CTab = (CTabHandle) NewHandle(sizeof(ColorSpec) * 256 + 8);
slouken@1662
   235
    if (SDL_CTab == nil) {
slouken@1668
   236
        SDL_OutOfMemory();
slouken@1662
   237
        return (-1);
slouken@1662
   238
    }
slouken@1668
   239
    (**SDL_CTab).ctSeed = GetCTSeed();
slouken@1662
   240
    (**SDL_CTab).ctFlags = 0;
slouken@1662
   241
    (**SDL_CTab).ctSize = 255;
slouken@1668
   242
    CTabChanged(SDL_CTab);
slouken@1668
   243
    SDL_CPal = NewPalette(256, SDL_CTab, pmExplicit + pmTolerant, 0);
slouken@0
   244
slouken@1662
   245
    /* Get a list of available fullscreen modes */
slouken@1668
   246
    SDL_modelist = (SDL_Rect **) SDL_malloc((1 + 1) * sizeof(SDL_Rect *));
slouken@1662
   247
    if (SDL_modelist) {
slouken@1668
   248
        SDL_modelist[0] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
slouken@1662
   249
        if (SDL_modelist[0]) {
slouken@1662
   250
            SDL_modelist[0]->x = 0;
slouken@1662
   251
            SDL_modelist[0]->y = 0;
slouken@1662
   252
            SDL_modelist[0]->w = (**SDL_Display).gdRect.right;
slouken@1662
   253
            SDL_modelist[0]->h = (**SDL_Display).gdRect.bottom;
slouken@1662
   254
        }
slouken@1662
   255
        SDL_modelist[1] = NULL;
slouken@1662
   256
    }
slouken@0
   257
slouken@1662
   258
    /* Fill in some window manager capabilities */
slouken@1662
   259
    this->info.wm_available = 1;
slouken@1662
   260
slouken@1662
   261
    /* We're done! */
slouken@1662
   262
    return (0);
slouken@0
   263
}
slouken@0
   264
slouken@1662
   265
static SDL_Rect **
slouken@1668
   266
ROM_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
slouken@0
   267
{
slouken@1662
   268
    if (this->screen->format->BitsPerPixel == format->BitsPerPixel) {
slouken@1662
   269
        if ((flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
slouken@1662
   270
            return (SDL_modelist);
slouken@1662
   271
        } else {
slouken@1662
   272
            return ((SDL_Rect **) - 1);
slouken@1662
   273
        }
slouken@1662
   274
    } else {
slouken@1662
   275
        return ((SDL_Rect **) 0);
slouken@1662
   276
    }
slouken@0
   277
}
slouken@0
   278
slouken@1662
   279
static void
slouken@1668
   280
ROM_HideMenuBar(_THIS)
slouken@0
   281
{
slouken@1662
   282
#if !TARGET_API_MAC_CARBON      /* This seems not to be available? -sts Aug 2000 */
slouken@1662
   283
    RgnHandle drawRgn = nil;
slouken@1662
   284
    RgnHandle tempRgn = nil;
slouken@1662
   285
    RgnHandle grayRgn = nil;
slouken@1662
   286
    WindowPtr window = nil;
slouken@1662
   287
    GDHandle gd = nil;
slouken@1662
   288
    GrafPtr savePort;
slouken@1662
   289
    long response;
slouken@1662
   290
    short height;
slouken@1662
   291
    EventRecord theEvent;
slouken@0
   292
slouken@1668
   293
    height = GetMBarHeight();
slouken@0
   294
slouken@1662
   295
    if (height > 0) {
slouken@1668
   296
        tempRgn = NewRgn();
slouken@1668
   297
        drawRgn = NewRgn();
slouken@1668
   298
        gSaveGrayRgn = NewRgn();
slouken@1662
   299
        if (!tempRgn || !drawRgn || !gSaveGrayRgn) {
slouken@1662
   300
            goto CLEANUP;
slouken@1662
   301
        }
slouken@1668
   302
        grayRgn = GetGrayRgn(); /* No need to check for this */
slouken@0
   303
slouken@1668
   304
        GetPort(&savePort);
slouken@0
   305
slouken@1662
   306
        /* Hide the control strip if it's present, and record its 
slouken@1662
   307
           previous position into the dirty region for redrawing. 
slouken@1662
   308
           This isn't necessary, but may help catch stray bits. */
slouken@1668
   309
        CopyRgn(grayRgn, tempRgn);
slouken@1668
   310
        if (!Gestalt(gestaltControlStripAttr, &response) &&
slouken@1662
   311
            (response & (1L << gestaltControlStripExists))) {
slouken@1668
   312
            gSaveCSVis = SBIsControlStripVisible();
slouken@1662
   313
            if (gSaveCSVis)
slouken@1668
   314
                SBShowHideControlStrip(false);
slouken@1662
   315
        }
slouken@1668
   316
        DiffRgn(grayRgn, tempRgn, drawRgn);
slouken@0
   317
slouken@1662
   318
        /* Save the gray region once the control strip is hidden */
slouken@1668
   319
        CopyRgn(grayRgn, gSaveGrayRgn);
slouken@0
   320
slouken@1662
   321
        /* Change the menu height in lowmem */
slouken@1662
   322
        gSaveMenuBar = height;
slouken@1668
   323
        LMSetMBarHeight(0);
slouken@0
   324
slouken@1662
   325
        /* Walk the monitor rectangles, and combine any pieces that
slouken@1662
   326
           aren't in GrayRgn: menubar, round corners, fake floaters. */
slouken@1668
   327
        for (gd = GetDeviceList(); gd; gd = GetNextDevice(gd)) {
slouken@1668
   328
            if (!TestDeviceAttribute(gd, screenDevice))
slouken@1662
   329
                continue;
slouken@1668
   330
            if (!TestDeviceAttribute(gd, screenActive))
slouken@1662
   331
                continue;
slouken@0
   332
slouken@1668
   333
            RectRgn(tempRgn, &(*gd)->gdRect);   /* Get the whole screen */
slouken@1668
   334
            DiffRgn(tempRgn, grayRgn, tempRgn); /* Subtract out GrayRgn */
slouken@1668
   335
            UnionRgn(tempRgn, drawRgn, drawRgn);        /* Combine all the bits */
slouken@1662
   336
        }
slouken@0
   337
slouken@1662
   338
        /* Add the bits into the GrayRgn */
slouken@1668
   339
        UnionRgn(drawRgn, grayRgn, grayRgn);
slouken@1662
   340
slouken@1662
   341
        /* Modify the vis regions of exposed windows */
slouken@1668
   342
        window = (FrontWindow())? FrontWindow() : (WindowPtr) - 1L;
slouken@1668
   343
        PaintBehind(window, drawRgn);
slouken@1668
   344
        CalcVisBehind(window, drawRgn);
slouken@1662
   345
slouken@1668
   346
        SetPort(savePort);
slouken@1662
   347
slouken@1662
   348
        /* Yield time so that floaters can catch up */
slouken@1668
   349
        EventAvail(0, &theEvent);
slouken@1668
   350
        EventAvail(0, &theEvent);
slouken@1668
   351
        EventAvail(0, &theEvent);
slouken@1668
   352
        EventAvail(0, &theEvent);
slouken@1662
   353
    }
slouken@1662
   354
slouken@1662
   355
  CLEANUP:
slouken@1662
   356
slouken@1662
   357
    if (tempRgn)
slouken@1668
   358
        DisposeRgn(tempRgn);
slouken@1662
   359
    if (drawRgn)
slouken@1668
   360
        DisposeRgn(drawRgn);
slouken@0
   361
#endif /* !TARGET_API_MAC_CARBON */
slouken@0
   362
}
slouken@1662
   363
slouken@1662
   364
static void
slouken@1668
   365
ROM_ShowMenuBar(_THIS)
slouken@0
   366
{
slouken@1662
   367
#if !TARGET_API_MAC_CARBON      /* This seems not to be available? -sts Aug 2000 */
slouken@1662
   368
    RgnHandle drawRgn = nil;
slouken@1662
   369
    RgnHandle menuRgn = nil;
slouken@1662
   370
    RgnHandle tempRgn = nil;
slouken@1662
   371
    RgnHandle grayRgn = nil;
slouken@1662
   372
    WindowPtr window = nil;
slouken@1662
   373
    GrafPtr wMgrPort;
slouken@1662
   374
    GrafPtr savePort;
slouken@1662
   375
    Rect menuRect;
slouken@1662
   376
    long response;
slouken@1662
   377
    short height;
slouken@1662
   378
    EventRecord theEvent;
slouken@1662
   379
    RGBColor saveRGB;
slouken@1662
   380
    RGBColor blackRGB = { 0, 0, 0 };
slouken@0
   381
slouken@1668
   382
    height = GetMBarHeight();
slouken@0
   383
slouken@1662
   384
    if ((height <= 0) && (gSaveMenuBar > 0)) {
slouken@1668
   385
        drawRgn = NewRgn();
slouken@1668
   386
        menuRgn = NewRgn();
slouken@1668
   387
        tempRgn = NewRgn();
slouken@1662
   388
        if (!tempRgn || !drawRgn || !gSaveGrayRgn) {
slouken@1662
   389
            goto CLEANUP;
slouken@1662
   390
        }
slouken@1668
   391
        grayRgn = GetGrayRgn(); /* No need to check for this */
slouken@0
   392
slouken@1668
   393
        GetPort(&savePort);
slouken@1668
   394
        GetWMgrPort(&wMgrPort);
slouken@0
   395
slouken@1662
   396
        /* Set the height properly */
slouken@1668
   397
        LMSetMBarHeight(gSaveMenuBar);
slouken@0
   398
slouken@1662
   399
        /* Restore the old GrayRgn: rounded corners, etc, but not
slouken@1662
   400
           the menubar -- subtract that out first! */
slouken@1662
   401
        if (gSaveGrayRgn) {
slouken@1668
   402
            menuRect = (*GetMainDevice())->gdRect;
slouken@1662
   403
            menuRect.bottom = menuRect.top + gSaveMenuBar;
slouken@1668
   404
            RectRgn(menuRgn, &menuRect);
slouken@0
   405
slouken@1668
   406
            DiffRgn(grayRgn, gSaveGrayRgn, drawRgn);    /* What do we inval? */
slouken@1668
   407
            DiffRgn(drawRgn, menuRgn, drawRgn); /* Clip out the menu */
slouken@0
   408
slouken@1662
   409
            /* Now redraw the corners and other bits black */
slouken@1668
   410
            SetPort(wMgrPort);
slouken@1668
   411
            GetClip(tempRgn);
slouken@1668
   412
            SetClip(drawRgn);
slouken@1668
   413
            GetForeColor(&saveRGB);
slouken@1668
   414
            RGBForeColor(&blackRGB);
slouken@1668
   415
            PaintRgn(drawRgn);
slouken@1668
   416
            RGBForeColor(&saveRGB);
slouken@1668
   417
            SetClip(tempRgn);
slouken@1668
   418
            SetPort(savePort);
slouken@0
   419
slouken@1668
   420
            UnionRgn(drawRgn, menuRgn, drawRgn);        /* Put back the menu */
slouken@0
   421
slouken@1662
   422
            /* Now actually restore the GrayRgn */
slouken@1668
   423
            CopyRgn(gSaveGrayRgn, grayRgn);
slouken@1668
   424
            DisposeRgn(gSaveGrayRgn);
slouken@1662
   425
            gSaveGrayRgn = nil;
slouken@1662
   426
        }
slouken@0
   427
slouken@1662
   428
        /* Modify the vis regions of exposed windows and draw menubar */
slouken@1668
   429
        window = (FrontWindow())? FrontWindow() : (WindowPtr) - 1L;
slouken@1668
   430
        PaintBehind(window, drawRgn);
slouken@1668
   431
        CalcVisBehind(window, drawRgn);
slouken@1668
   432
        DrawMenuBar();
slouken@0
   433
slouken@1668
   434
        SetPort(savePort);
slouken@1662
   435
        gSaveMenuBar = 0;
slouken@1662
   436
slouken@1662
   437
        /* Now show the control strip if it's present */
slouken@1668
   438
        if (!Gestalt(gestaltControlStripAttr, &response) &&
slouken@1662
   439
            (response & (1L << gestaltControlStripExists))) {
slouken@1668
   440
            if (gSaveCSVis && !SBIsControlStripVisible())
slouken@1668
   441
                SBShowHideControlStrip(true);
slouken@1662
   442
            gSaveCSVis = true;
slouken@1662
   443
        }
slouken@1662
   444
slouken@1662
   445
        /* Yield time so that floaters can catch up */
slouken@1668
   446
        EventAvail(0, &theEvent);
slouken@1668
   447
        EventAvail(0, &theEvent);
slouken@1668
   448
        EventAvail(0, &theEvent);
slouken@1668
   449
        EventAvail(0, &theEvent);
slouken@1662
   450
    }
slouken@1662
   451
slouken@1662
   452
  CLEANUP:
slouken@1662
   453
slouken@1662
   454
    if (drawRgn)
slouken@1668
   455
        DisposeRgn(drawRgn);
slouken@1662
   456
    if (menuRgn)
slouken@1668
   457
        DisposeRgn(menuRgn);
slouken@1662
   458
    if (tempRgn)
slouken@1668
   459
        DisposeRgn(tempRgn);
slouken@0
   460
#endif /* !TARGET_API_MAC_CARBON */
slouken@0
   461
}
slouken@0
   462
slouken@0
   463
/* Various screen update functions available */
slouken@1668
   464
static void ROM_DirectUpdate(_THIS, int numrects, SDL_Rect * rects);
slouken@1668
   465
static void ROM_WindowUpdate(_THIS, int numrects, SDL_Rect * rects);
slouken@0
   466
slouken@1662
   467
static void
slouken@1668
   468
ROM_UnsetVideoMode(_THIS, SDL_Surface * current)
slouken@0
   469
{
slouken@1662
   470
    /* Free the current window, if any */
slouken@1662
   471
    if (SDL_Window != nil) {
slouken@1662
   472
        GWorldPtr memworld;
slouken@0
   473
slouken@1662
   474
        /* Handle OpenGL support */
slouken@1668
   475
        Mac_GL_Quit(this);
slouken@1662
   476
slouken@1668
   477
        memworld = (GWorldPtr) GetWRefCon(SDL_Window);
slouken@1662
   478
        if (memworld != nil) {
slouken@1668
   479
            UnlockPixels(GetGWorldPixMap(memworld));
slouken@1668
   480
            DisposeGWorld(memworld);
slouken@1662
   481
        }
slouken@1662
   482
        if ((current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
slouken@0
   483
#if USE_QUICKTIME
slouken@1668
   484
            EndFullScreen(fullscreen_ctx, nil);
slouken@1662
   485
            SDL_Window = nil;
slouken@0
   486
#else
slouken@1668
   487
            ROM_ShowMenuBar(this);
slouken@0
   488
#endif
slouken@1662
   489
        }
slouken@1662
   490
    }
slouken@1662
   491
    current->pixels = NULL;
slouken@1662
   492
    current->flags &= ~(SDL_HWSURFACE | SDL_FULLSCREEN);
slouken@0
   493
}
slouken@0
   494
slouken@1662
   495
static SDL_Surface *
slouken@1668
   496
ROM_SetVideoMode(_THIS, SDL_Surface * current,
slouken@1668
   497
                 int width, int height, int bpp, Uint32 flags)
slouken@0
   498
{
slouken@1662
   499
    Rect wrect, orect;
slouken@0
   500
#if TARGET_API_MAC_CARBON
slouken@1662
   501
    Rect tmprect;
slouken@0
   502
#endif
slouken@0
   503
slouken@1662
   504
    /* Free any previous video mode */
slouken@1668
   505
    ROM_UnsetVideoMode(this, current);
slouken@0
   506
slouken@1662
   507
    /* Create the ROM window and SDL video surface */
slouken@1662
   508
    current->flags = 0;         /* Clear flags */
slouken@1662
   509
    current->w = width;
slouken@1662
   510
    current->h = height;
slouken@1668
   511
    SetRect(&wrect, 0, 0, width, height);
slouken@1662
   512
    if (SDL_Window) {
slouken@1662
   513
        /* If we recreate the window, don't move it around */
slouken@0
   514
#if TARGET_API_MAC_CARBON
slouken@1668
   515
        orect = *GetWindowPortBounds(SDL_Window, &tmprect);
slouken@0
   516
#else
slouken@1662
   517
        orect = SDL_Window->portRect;
slouken@0
   518
#endif
slouken@1668
   519
        OffsetRect(&wrect, orect.left, orect.top);
slouken@1662
   520
    } else {
slouken@1662
   521
        /* Center the window the first time we show it */
slouken@1668
   522
        OffsetRect(&wrect,
slouken@1668
   523
                   (SDL_modelist[0]->w - width) / 2,
slouken@1668
   524
                   (SDL_modelist[0]->h - height) / 2);
slouken@1662
   525
    }
slouken@0
   526
slouken@1402
   527
#if defined(__MACOSX__) && !USE_QUICKTIME
slouken@1662
   528
    /* Hum.. fullscreen mode is broken */
slouken@1662
   529
    flags &= ~SDL_FULLSCREEN;
slouken@0
   530
#endif
slouken@1662
   531
    if ((flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
slouken@1662
   532
        /* Create the fullscreen window and use screen bits */
slouken@1662
   533
        current->flags |= SDL_HWSURFACE | SDL_FULLSCREEN;
slouken@1662
   534
        if (SDL_Window) {
slouken@1668
   535
            DisposeWindow(SDL_Window);
slouken@1662
   536
        }
slouken@0
   537
#if USE_QUICKTIME
slouken@1668
   538
        BeginFullScreen(&fullscreen_ctx, nil, 0, 0, &SDL_Window, nil, 0);
slouken@0
   539
#else
slouken@1668
   540
        SDL_Window = NewCWindow(nil, &wrect, "\p", true, plainDBox,
slouken@1668
   541
                                (WindowPtr) - 1, false, 0);
slouken@1668
   542
        ROM_HideMenuBar(this);
slouken@0
   543
#endif
slouken@1662
   544
        current->pitch = (**(**SDL_Display).gdPMap).rowBytes & 0x3FFF;
slouken@1662
   545
        current->pixels = (**(**SDL_Display).gdPMap).baseAddr;
slouken@1662
   546
        this->UpdateRects = ROM_DirectUpdate;
slouken@1662
   547
    } else {
slouken@1662
   548
        GWorldPtr memworld;
slouken@1662
   549
        PixMapHandle pixmap;
slouken@1662
   550
        int style;
slouken@0
   551
slouken@1662
   552
        style = noGrowDocProc;
slouken@1662
   553
        if (flags & SDL_NOFRAME) {
slouken@1662
   554
            style = plainDBox;
slouken@1662
   555
            current->flags |= SDL_NOFRAME;
slouken@1662
   556
        } else if (flags & SDL_RESIZABLE) {
slouken@1662
   557
            style = zoomDocProc;
slouken@1662
   558
            current->flags |= SDL_RESIZABLE;
slouken@1662
   559
        }
slouken@1662
   560
        if (SDL_Window && (style == current_style)) {
slouken@1662
   561
            /* Resize existing window, if necessary */
slouken@1662
   562
            if (((orect.right - orect.left) != width) ||
slouken@1662
   563
                ((orect.bottom - orect.top) != height)) {
slouken@1668
   564
                SizeWindow(SDL_Window, width, height, false);
slouken@1662
   565
            }
slouken@1662
   566
        } else {
slouken@1662
   567
            /* Recreate the window in the new style */
slouken@1662
   568
            if (SDL_Window) {
slouken@1668
   569
                DisposeWindow(SDL_Window);
slouken@1662
   570
            }
slouken@1668
   571
            SDL_Window = NewCWindow(nil, &wrect, "\p", true,
slouken@1668
   572
                                    style, (WindowPtr) - 1, true, 0);
slouken@0
   573
slouken@1662
   574
            /* Set the window title, if any */
slouken@1662
   575
            {
slouken@1662
   576
                char *title;
slouken@1668
   577
                SDL_WM_GetCaption(&title, NULL);
slouken@1662
   578
                if (title) {
slouken@1668
   579
                    Mac_SetCaption(this, title, NULL);
slouken@1662
   580
                }
slouken@1662
   581
            }
slouken@1662
   582
        }
slouken@1662
   583
        current_style = style;
slouken@1668
   584
        SetPalette(SDL_Window, SDL_CPal, false);
slouken@1668
   585
        ActivatePalette(SDL_Window);
slouken@1668
   586
        if (NewGWorld(&memworld, 0,
slouken@0
   587
#if TARGET_API_MAC_CARBON
slouken@1668
   588
                      GetWindowPortBounds(SDL_Window, &tmprect),
slouken@0
   589
#else
slouken@1668
   590
                      &SDL_Window->portRect,
slouken@0
   591
#endif
slouken@1668
   592
                      SDL_CTab, nil, 0) != noErr) {
slouken@1668
   593
            SDL_SetError("NewGWorld() failed");
slouken@1662
   594
            return (NULL);
slouken@1662
   595
        }
slouken@1668
   596
        SetWRefCon(SDL_Window, (long) memworld);
slouken@1668
   597
        pixmap = GetGWorldPixMap(memworld);
slouken@1668
   598
        LockPixels(pixmap);
slouken@1662
   599
        current->pitch = (**pixmap).rowBytes & 0x3FFF;
slouken@1668
   600
        current->pixels = GetPixBaseAddr(pixmap);
slouken@1662
   601
        this->UpdateRects = ROM_WindowUpdate;
slouken@1662
   602
    }
slouken@1668
   603
    SetPortWindowPort(SDL_Window);
slouken@1668
   604
    SelectWindow(SDL_Window);
slouken@0
   605
slouken@1662
   606
    /* Handle OpenGL support */
slouken@1662
   607
    if (flags & SDL_INTERNALOPENGL) {
slouken@1668
   608
        if (Mac_GL_Init(this) == 0) {
slouken@1662
   609
            current->flags |= SDL_INTERNALOPENGL;
slouken@1662
   610
        } else {
slouken@1662
   611
            current = NULL;
slouken@1662
   612
        }
slouken@1662
   613
    }
slouken@1662
   614
slouken@1662
   615
    if ((flags & SDL_HWPALETTE) && (flags & SDL_FULLSCREEN))
slouken@1662
   616
        current->flags |= SDL_HWPALETTE;
slouken@1662
   617
slouken@1662
   618
    /* We're live! */
slouken@1662
   619
    return (current);
slouken@0
   620
}
slouken@0
   621
slouken@0
   622
/* We don't actually allow hardware surfaces other than the main one */
slouken@1662
   623
static int
slouken@1668
   624
ROM_AllocHWSurface(_THIS, SDL_Surface * surface)
slouken@0
   625
{
slouken@1662
   626
    return (-1);
slouken@0
   627
}
slouken@1662
   628
static void
slouken@1668
   629
ROM_FreeHWSurface(_THIS, SDL_Surface * surface)
slouken@0
   630
{
slouken@1662
   631
    return;
slouken@0
   632
}
slouken@1662
   633
static int
slouken@1668
   634
ROM_LockHWSurface(_THIS, SDL_Surface * surface)
slouken@0
   635
{
slouken@1662
   636
    return (0);
slouken@0
   637
}
slouken@1662
   638
static void
slouken@1668
   639
ROM_UnlockHWSurface(_THIS, SDL_Surface * surface)
slouken@0
   640
{
slouken@1662
   641
    return;
slouken@0
   642
}
slouken@0
   643
slouken@1662
   644
static void
slouken@1668
   645
ROM_DirectUpdate(_THIS, int numrects, SDL_Rect * rects)
slouken@0
   646
{
slouken@1662
   647
    /* The application is already updating the visible video memory */
slouken@1662
   648
    return;
slouken@0
   649
}
slouken@0
   650
slouken@1662
   651
static void
slouken@1668
   652
ROM_WindowUpdate(_THIS, int numrects, SDL_Rect * rects)
slouken@0
   653
{
slouken@1662
   654
    GWorldPtr memworld;
slouken@1662
   655
    GrafPtr saveport;
slouken@1662
   656
    CGrafPtr thePort;
slouken@1662
   657
    const BitMap *memBits;
slouken@1662
   658
    const BitMap *winBits;
slouken@1662
   659
    int i;
slouken@1662
   660
    Rect update;
slouken@1662
   661
slouken@1662
   662
    /* Copy from the offscreen GWorld to the window port */
slouken@1668
   663
    GetPort(&saveport);
slouken@1668
   664
    SetPortWindowPort(SDL_Window);
slouken@1668
   665
    thePort = GetWindowPort(SDL_Window);
slouken@1668
   666
    memworld = (GWorldPtr) GetWRefCon(SDL_Window);
slouken@1659
   667
#if TARGET_API_MAC_CARBON && ACCESSOR_CALLS_ARE_FUNCTIONS
slouken@1668
   668
    memBits = GetPortBitMapForCopyBits((CGrafPtr) memworld);
slouken@0
   669
#else
slouken@1662
   670
    memBits = &((GrafPtr) memworld)->portBits;
slouken@0
   671
#endif
slouken@1659
   672
#if TARGET_API_MAC_CARBON && ACCESSOR_CALLS_ARE_FUNCTIONS
slouken@1668
   673
    winBits = GetPortBitMapForCopyBits(thePort);
slouken@0
   674
#else
slouken@1662
   675
    winBits = &SDL_Window->portBits;
slouken@0
   676
#endif
slouken@1662
   677
    for (i = 0; i < numrects; ++i) {
slouken@1662
   678
        update.left = rects[i].x;
slouken@1662
   679
        update.right = rects[i].x + rects[i].w;
slouken@1662
   680
        update.top = rects[i].y;
slouken@1662
   681
        update.bottom = rects[i].y + rects[i].h;
slouken@1668
   682
        CopyBits(memBits, winBits, &update, &update, srcCopy, nil);
slouken@1662
   683
    }
slouken@0
   684
#if TARGET_API_MAC_CARBON
slouken@1668
   685
    if (QDIsPortBuffered(thePort)) {
slouken@1668
   686
        QDFlushPortBuffer(thePort, NULL);
slouken@1662
   687
    }
slouken@0
   688
#endif
slouken@1668
   689
    SetPort(saveport);
slouken@0
   690
}
slouken@0
   691
slouken@1662
   692
static int
slouken@1668
   693
ROM_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color * colors)
slouken@0
   694
{
slouken@1662
   695
    CTabHandle cTab;
slouken@1662
   696
    int i;
slouken@0
   697
slouken@1662
   698
    /* Get the colortable from the either the display or window */
slouken@1662
   699
    if ((this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
slouken@1662
   700
        cTab = (**(**SDL_Display).gdPMap).pmTable;
slouken@1662
   701
    } else {
slouken@1662
   702
        cTab = SDL_CTab;
slouken@1662
   703
    }
slouken@0
   704
slouken@1662
   705
    /* Verify the range of colors */
slouken@1662
   706
    if ((firstcolor + ncolors) > ((**cTab).ctSize + 1)) {
slouken@1662
   707
        return (0);
slouken@1662
   708
    }
slouken@1662
   709
slouken@1662
   710
    /* Set the screen palette and update the display */
slouken@1662
   711
    for (i = 0; i < ncolors; ++i) {
slouken@1662
   712
        int j = firstcolor + i;
slouken@1662
   713
        (**cTab).ctTable[j].value = j;
slouken@1662
   714
        (**cTab).ctTable[j].rgb.red = colors[i].r << 8 | colors[i].r;
slouken@1662
   715
        (**cTab).ctTable[j].rgb.green = colors[i].g << 8 | colors[i].g;
slouken@1662
   716
        (**cTab).ctTable[j].rgb.blue = colors[i].b << 8 | colors[i].b;
slouken@1662
   717
    }
slouken@1662
   718
//      if ( (this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) 
slouken@1662
   719
    {
slouken@1662
   720
        GDevice **odisplay;
slouken@1668
   721
        odisplay = GetGDevice();
slouken@1668
   722
        SetGDevice(SDL_Display);
slouken@1668
   723
        SetEntries(0, (**cTab).ctSize, (ColorSpec *) & (**cTab).ctTable);
slouken@1668
   724
        SetGDevice(odisplay);
slouken@1662
   725
    }
slouken@1662
   726
    return (1);
slouken@0
   727
}
slouken@0
   728
slouken@1662
   729
void
slouken@1668
   730
ROM_VideoQuit(_THIS)
slouken@0
   731
{
slouken@1662
   732
    int i;
slouken@0
   733
slouken@1662
   734
    /* Free current video mode */
slouken@1668
   735
    ROM_UnsetVideoMode(this, this->screen);
slouken@1662
   736
    if (SDL_Window) {
slouken@1668
   737
        DisposeWindow(SDL_Window);
slouken@1662
   738
        SDL_Window = nil;
slouken@1662
   739
    }
slouken@0
   740
slouken@1662
   741
    /* Free palette and restore original one */
slouken@1662
   742
    if (SDL_CTab != nil) {
slouken@1668
   743
        DisposeHandle((Handle) SDL_CTab);
slouken@1662
   744
        SDL_CTab = nil;
slouken@1662
   745
    }
slouken@1662
   746
    if (SDL_CPal != nil) {
slouken@1668
   747
        DisposePalette(SDL_CPal);
slouken@1662
   748
        SDL_CPal = nil;
slouken@1662
   749
    }
slouken@1668
   750
    RestoreDeviceClut(GetMainDevice());
slouken@0
   751
slouken@1662
   752
    /* Free list of video modes */
slouken@1662
   753
    if (SDL_modelist != NULL) {
slouken@1662
   754
        for (i = 0; SDL_modelist[i]; ++i) {
slouken@1668
   755
            SDL_free(SDL_modelist[i]);
slouken@1662
   756
        }
slouken@1668
   757
        SDL_free(SDL_modelist);
slouken@1662
   758
        SDL_modelist = NULL;
slouken@1662
   759
    }
slouken@0
   760
}
slouken@0
   761
slouken@1662
   762
/* vi: set ts=4 sw=4 expandtab: */