src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 30 Aug 2003 17:07:59 +0000
changeset 701 aaf3b8af6616
parent 692 04dd6c6d7c30
child 718 cbc0f7fabd1c
permissions -rw-r--r--
Date: Sat, 30 Aug 2003 16:28:10 +0300
From: "Mike Gorchak"
Subject: Re: SDL 1.2.6

- minor changes about shared library building under QNX6 into README.QNX
- added forgotten libSDLmain.a into distribution, SDL.qpg.in
- added header guards to the all headers.
- fixed fullscreen double buffered mode.
- fixed Photon crashes after/during using fullscreen OpenGL modes.
- added GL_MakeCurrent function.
- added SDL_VIDEOEXPOSE event, when OpenGL window have been resized
- added more HAVE_OPENGL checks to avoid dead code compilation without using OpenGL
- finished code reorganization (began into previous patches).
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>
slouken@0
    29
#include <stdio.h>
slouken@0
    30
#include <unistd.h>
slouken@0
    31
#include <string.h>
slouken@0
    32
#include <sys/ioctl.h>
slouken@0
    33
slouken@0
    34
#include "SDL.h"
slouken@0
    35
#include "SDL_error.h"
slouken@0
    36
#include "SDL_timer.h"
slouken@0
    37
#include "SDL_thread.h"
slouken@0
    38
#include "SDL_video.h"
slouken@0
    39
#include "SDL_mouse.h"
slouken@0
    40
#include "SDL_endian.h"
slouken@0
    41
#include "SDL_sysvideo.h"
slouken@0
    42
#include "SDL_pixels_c.h"
slouken@0
    43
#include "SDL_events_c.h"
slouken@0
    44
#include "SDL_ph_video.h"
slouken@0
    45
#include "SDL_ph_modes_c.h"
slouken@0
    46
#include "SDL_ph_image_c.h"
slouken@0
    47
#include "SDL_ph_events_c.h"
slouken@0
    48
#include "SDL_ph_mouse_c.h"
slouken@19
    49
#include "SDL_ph_wm_c.h"
slouken@0
    50
#include "SDL_phyuv_c.h"
slouken@0
    51
#include "blank_cursor.h"
slouken@0
    52
slouken@701
    53
static int  ph_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@701
    54
static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
slouken@701
    55
static int  ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
slouken@0
    56
static void ph_VideoQuit(_THIS);
slouken@0
    57
static void ph_DeleteDevice(SDL_VideoDevice *device);
slouken@291
    58
slouken@291
    59
#ifdef HAVE_OPENGL
slouken@701
    60
static void  ph_GL_SwapBuffers(_THIS);
slouken@701
    61
static int   ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value);
slouken@701
    62
static int   ph_GL_LoadLibrary(_THIS, const char* path);
slouken@663
    63
static void* ph_GL_GetProcAddress(_THIS, const char* proc);
slouken@701
    64
static int   ph_GL_MakeCurrent(_THIS);
slouken@291
    65
#endif /* HAVE_OPENGL */
slouken@266
    66
slouken@0
    67
static int ph_Available(void)
slouken@0
    68
{
slouken@309
    69
    int phstat=-1;
slouken@309
    70
slouken@309
    71
    phstat=PtInit(0);
slouken@309
    72
    if (phstat==0)
slouken@309
    73
    {
slouken@309
    74
       return 1;
slouken@309
    75
    }
slouken@309
    76
    else
slouken@309
    77
    {
slouken@309
    78
       return 0;
slouken@309
    79
    }
slouken@0
    80
}
slouken@0
    81
slouken@0
    82
static SDL_VideoDevice *ph_CreateDevice(int devindex)
slouken@0
    83
{
slouken@0
    84
    SDL_VideoDevice *device;
slouken@0
    85
slouken@0
    86
    /* Initialize all variables that we clean on shutdown */
slouken@0
    87
    device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
slouken@309
    88
    if (device) {
slouken@0
    89
        memset(device, 0, (sizeof *device));
slouken@0
    90
        device->hidden = (struct SDL_PrivateVideoData *)
slouken@0
    91
                malloc((sizeof *device->hidden));
slouken@0
    92
        device->gl_data = NULL;
slouken@0
    93
    }
slouken@571
    94
    if ((device == NULL) || (device->hidden == NULL)) {
slouken@0
    95
        SDL_OutOfMemory();
slouken@0
    96
        ph_DeleteDevice(device);
slouken@0
    97
        return(0);
slouken@0
    98
    }
slouken@0
    99
    memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
   100
slouken@0
   101
    /* Set the driver flags */
slouken@279
   102
    device->handles_any_size = 1; /* JB not true for fullscreen */
slouken@0
   103
slouken@0
   104
    /* Set the function pointers */
slouken@266
   105
    device->CreateYUVOverlay = ph_CreateYUVOverlay;
slouken@0
   106
    device->VideoInit = ph_VideoInit;
slouken@0
   107
    device->ListModes = ph_ListModes;
slouken@0
   108
    device->SetVideoMode = ph_SetVideoMode;
slouken@309
   109
    device->ToggleFullScreen = ph_ToggleFullScreen;
slouken@571
   110
    device->UpdateMouse = ph_UpdateMouse;
slouken@0
   111
    device->SetColors = ph_SetColors;
slouken@663
   112
    device->UpdateRects = NULL;         /* set up in ph_SetupUpdateFunction */
slouken@0
   113
    device->VideoQuit = ph_VideoQuit;
slouken@0
   114
    device->AllocHWSurface = ph_AllocHWSurface;
slouken@0
   115
    device->CheckHWBlit = NULL;
slouken@0
   116
    device->FillHWRect = NULL;
slouken@0
   117
    device->SetHWColorKey = NULL;
slouken@0
   118
    device->SetHWAlpha = NULL;
slouken@0
   119
    device->LockHWSurface = ph_LockHWSurface;
slouken@0
   120
    device->UnlockHWSurface = ph_UnlockHWSurface;
slouken@0
   121
    device->FlipHWSurface = ph_FlipHWSurface;
slouken@0
   122
    device->FreeHWSurface = ph_FreeHWSurface;
slouken@19
   123
    device->SetCaption = ph_SetCaption;
slouken@0
   124
    device->SetIcon = NULL;
slouken@19
   125
    device->IconifyWindow = ph_IconifyWindow;
slouken@283
   126
    device->GrabInput = ph_GrabInput;
slouken@291
   127
    device->GetWMInfo = ph_GetWMInfo;
slouken@0
   128
    device->FreeWMCursor = ph_FreeWMCursor;
slouken@0
   129
    device->CreateWMCursor = ph_CreateWMCursor;
slouken@0
   130
    device->ShowWMCursor = ph_ShowWMCursor;
slouken@0
   131
    device->WarpWMCursor = ph_WarpWMCursor;
slouken@701
   132
    device->MoveWMCursor = NULL;
slouken@0
   133
    device->CheckMouseMode = ph_CheckMouseMode;
slouken@0
   134
    device->InitOSKeymap = ph_InitOSKeymap;
slouken@0
   135
    device->PumpEvents = ph_PumpEvents;
slouken@0
   136
slouken@279
   137
    /* OpenGL support. */
slouken@279
   138
#ifdef HAVE_OPENGL
slouken@701
   139
    device->GL_MakeCurrent = ph_GL_MakeCurrent;
slouken@266
   140
    device->GL_SwapBuffers = ph_GL_SwapBuffers;
slouken@291
   141
    device->GL_GetAttribute = ph_GL_GetAttribute;
slouken@663
   142
    device->GL_LoadLibrary = ph_GL_LoadLibrary;
slouken@663
   143
    device->GL_GetProcAddress = ph_GL_GetProcAddress;
slouken@279
   144
#else
slouken@701
   145
    device->GL_MakeCurrent = NULL;
slouken@279
   146
    device->GL_SwapBuffers = NULL;
slouken@291
   147
    device->GL_GetAttribute = NULL;
slouken@663
   148
    device->GL_LoadLibrary = NULL;
slouken@663
   149
    device->GL_GetProcAddress = NULL;
slouken@279
   150
#endif /* HAVE_OPENGL */
slouken@266
   151
slouken@0
   152
    device->free = ph_DeleteDevice;
slouken@663
   153
    
slouken@0
   154
    return device;
slouken@0
   155
}
slouken@0
   156
slouken@19
   157
VideoBootStrap ph_bootstrap = {
slouken@315
   158
    "photon", "QNX Photon video output",
slouken@315
   159
    ph_Available, ph_CreateDevice
slouken@0
   160
};
slouken@0
   161
slouken@0
   162
static void ph_DeleteDevice(SDL_VideoDevice *device)
slouken@0
   163
{
slouken@309
   164
    if (device)
slouken@309
   165
    {
slouken@309
   166
        if (device->hidden)
slouken@309
   167
        {
slouken@0
   168
            free(device->hidden);
slouken@0
   169
            device->hidden = NULL;
slouken@0
   170
        }
slouken@309
   171
        if (device->gl_data)
slouken@309
   172
        {
slouken@0
   173
            free(device->gl_data);
slouken@0
   174
            device->gl_data = NULL;
slouken@0
   175
        }
slouken@0
   176
        free(device);
slouken@0
   177
        device = NULL;
slouken@0
   178
    }
slouken@0
   179
}
slouken@0
   180
slouken@571
   181
static PtWidget_t *ph_CreateWindow(_THIS)
slouken@571
   182
{
slouken@571
   183
    PtWidget_t *widget;
slouken@571
   184
    
slouken@571
   185
    widget = PtCreateWidget(PtWindow, NULL, 0, 0);
slouken@571
   186
slouken@571
   187
    return widget;
slouken@571
   188
}
slouken@571
   189
slouken@571
   190
static int ph_SetupWindow(_THIS, int w, int h, int flags)
slouken@571
   191
{
slouken@571
   192
    PtArg_t     args[32];
slouken@571
   193
    PhPoint_t   pos = {0, 0};
slouken@571
   194
    PhDim_t     dim = {w, h};
slouken@571
   195
    int         nargs = 0;
slouken@692
   196
    const char* windowpos;
slouken@692
   197
    const char* iscentered;
slouken@692
   198
    int         x, y;
slouken@571
   199
slouken@571
   200
    PtSetArg(&args[nargs++], Pt_ARG_DIM, &dim, 0);
slouken@571
   201
slouken@571
   202
    if ((flags & SDL_RESIZABLE) == SDL_RESIZABLE)
slouken@571
   203
    {
slouken@663
   204
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_CLOSE);
slouken@663
   205
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_MAX | Ph_WM_RESTORE);
slouken@663
   206
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_RESIZE | Ph_WM_MOVE | Ph_WM_MAX | Ph_WM_RESTORE | Ph_WM_CLOSE);
slouken@663
   207
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_MAX | Ph_WM_RENDER_COLLAPSE | Ph_WM_RENDER_RETURN);
slouken@571
   208
    }
slouken@571
   209
    else
slouken@571
   210
    {
slouken@663
   211
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX | Ph_WM_RESTORE | Ph_WM_CLOSE);
slouken@663
   212
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX | Ph_WM_RESTORE);
slouken@663
   213
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_MOVE | Ph_WM_CLOSE);
slouken@663
   214
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_MAX | Ph_WM_RENDER_COLLAPSE | Ph_WM_RENDER_RETURN);
slouken@571
   215
    }
slouken@571
   216
slouken@571
   217
    if (((flags & SDL_NOFRAME)==SDL_NOFRAME) || ((flags & SDL_FULLSCREEN)==SDL_FULLSCREEN))
slouken@571
   218
    {
slouken@663
   219
       if ((flags & SDL_RESIZABLE) != SDL_RESIZABLE)
slouken@663
   220
       {
slouken@663
   221
           PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Pt_TRUE);
slouken@663
   222
       }
slouken@663
   223
       else
slouken@663
   224
       {
slouken@663
   225
           PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Pt_TRUE);
slouken@663
   226
           PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_BORDER);
slouken@663
   227
       }
slouken@571
   228
    }
slouken@571
   229
    else
slouken@571
   230
    {
slouken@571
   231
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_BORDER | Ph_WM_RENDER_TITLE |
slouken@663
   232
                                 Ph_WM_RENDER_CLOSE | Ph_WM_RENDER_MENU | Ph_WM_RENDER_MIN);
slouken@571
   233
    }
slouken@571
   234
slouken@663
   235
    if ((flags & SDL_FULLSCREEN) == SDL_FULLSCREEN)
slouken@571
   236
    {
slouken@571
   237
        PtSetArg(&args[nargs++], Pt_ARG_POS, &pos, 0);
slouken@692
   238
        PtSetArg(&args[nargs++], Pt_ARG_BASIC_FLAGS, Pt_TRUE, Pt_BASIC_PREVENT_FILL);
slouken@571
   239
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_FFRONT | Ph_WM_MAX);
slouken@692
   240
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISFOCUS | Ph_WM_STATE_ISALTKEY);
slouken@571
   241
    }
slouken@571
   242
    else
slouken@571
   243
    {
slouken@692
   244
        windowpos = getenv("SDL_VIDEO_WINDOW_POS");
slouken@692
   245
	iscentered = getenv("SDL_VIDEO_CENTERED");
slouken@692
   246
slouken@692
   247
        if ((iscentered) || ((windowpos) && (strcmp(windowpos, "center")==0)))
slouken@692
   248
        {
slouken@692
   249
            pos.x = (desktop_mode.width - w)/2;
slouken@692
   250
            pos.y = (desktop_mode.height - h)/2;
slouken@692
   251
            PtSetArg(&args[nargs++], Pt_ARG_POS, &pos, 0);
slouken@692
   252
	}
slouken@692
   253
        else
slouken@692
   254
        {
slouken@692
   255
            if (windowpos)
slouken@692
   256
            {
slouken@692
   257
                if (sscanf(windowpos, "%d,%d", &x, &y) == 2 )
slouken@692
   258
                {
slouken@692
   259
                    pos.x=x;
slouken@692
   260
                    pos.y=y;
slouken@692
   261
                    PtSetArg(&args[nargs++], Pt_ARG_POS, &pos, 0);
slouken@692
   262
                }
slouken@692
   263
	    }
slouken@692
   264
        }
slouken@692
   265
slouken@692
   266
slouken@692
   267
        PtSetArg(&args[nargs++], Pt_ARG_FILL_COLOR, Pg_BLACK, 0);
slouken@571
   268
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_FALSE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISMAX | Ph_WM_STATE_ISALTKEY);
slouken@571
   269
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_HIDE);
slouken@663
   270
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_HIDE);
slouken@571
   271
        PtSetArg(&args[nargs++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@571
   272
    }
slouken@571
   273
slouken@571
   274
    PtSetResources(window, nargs, args);
slouken@571
   275
    PtRealizeWidget(window);
slouken@692
   276
    PtWindowToFront(window);
slouken@571
   277
slouken@571
   278
    return 0;
slouken@571
   279
}
slouken@571
   280
slouken@663
   281
static const struct ColourMasks* ph_GetColourMasks(int bpp)
slouken@663
   282
{
slouken@663
   283
    /* The alpha mask doesn't appears to be needed */
slouken@663
   284
    static const struct ColourMasks phColorMasks[5] = {
slouken@663
   285
        /*  8 bit      */  {0, 0, 0, 0, 8},
slouken@663
   286
        /* 15 bit ARGB */  {0x7C00, 0x03E0, 0x001F, 0x8000, 15},
slouken@663
   287
        /* 16 bit  RGB */  {0xF800, 0x07E0, 0x001F, 0x0000, 16},
slouken@663
   288
        /* 24 bit  RGB */  {0xFF0000, 0x00FF00, 0x0000FF, 0x000000, 24},
slouken@663
   289
        /* 32 bit ARGB */  {0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000, 32},
slouken@663
   290
    };
slouken@663
   291
slouken@663
   292
    switch (bpp)
slouken@663
   293
    {
slouken@663
   294
        case 8:
slouken@663
   295
             return &phColorMasks[0];
slouken@663
   296
        case 15:
slouken@663
   297
             return &phColorMasks[1];
slouken@663
   298
        case 16:
slouken@663
   299
             return &phColorMasks[2];
slouken@663
   300
        case 24:
slouken@663
   301
             return &phColorMasks[3];
slouken@663
   302
        case 32:
slouken@663
   303
             return &phColorMasks[4];
slouken@663
   304
    }
slouken@663
   305
    return NULL;
slouken@663
   306
}
slouken@663
   307
slouken@0
   308
static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   309
{
slouken@309
   310
    PgHWCaps_t my_hwcaps;
slouken@663
   311
    int i;
slouken@309
   312
slouken@309
   313
    window=NULL;
slouken@320
   314
    desktoppal=SDLPH_PAL_NONE;
slouken@701
   315
slouken@320
   316
#ifdef HAVE_OPENGL
slouken@309
   317
    oglctx=NULL;
slouken@701
   318
    oglflags=0;
slouken@701
   319
    oglbpp=0;
slouken@320
   320
#endif /* HAVE_OPENGL */
slouken@315
   321
    
slouken@315
   322
    old_video_mode=-1;
slouken@315
   323
    old_refresh_rate=-1;
slouken@0
   324
	
slouken@309
   325
    if (NULL == (event = malloc(EVENT_SIZE)))
slouken@309
   326
    {
slouken@571
   327
        SDL_OutOfMemory();
slouken@571
   328
        return -1;
slouken@309
   329
    }
slouken@380
   330
    memset(event, 0x00, EVENT_SIZE);
slouken@0
   331
slouken@571
   332
    window = ph_CreateWindow(this);
slouken@571
   333
    if (window == NULL)
slouken@571
   334
    {
slouken@663
   335
        SDL_SetError("ph_VideoInit(): Couldn't create video window !\n");
slouken@571
   336
        return -1;
slouken@571
   337
    }
slouken@571
   338
slouken@309
   339
    /* Create the blank cursor */
slouken@309
   340
    SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
slouken@309
   341
                                          (int)BLANK_CWIDTH, (int)BLANK_CHEIGHT,
slouken@309
   342
                                          (int)BLANK_CHOTX, (int)BLANK_CHOTY);
slouken@0
   343
slouken@309
   344
    if (SDL_BlankCursor == NULL)
slouken@309
   345
    {
slouken@663
   346
        return -1;
slouken@309
   347
    }
slouken@0
   348
slouken@309
   349
    if (PgGetGraphicsHWCaps(&my_hwcaps) < 0)
slouken@309
   350
    {
slouken@663
   351
        SDL_SetError("ph_VideoInit(): GetGraphicsHWCaps function failed !\n");
slouken@663
   352
        this->FreeWMCursor(this, SDL_BlankCursor);
slouken@663
   353
        return -1;
slouken@309
   354
    }
slouken@0
   355
slouken@692
   356
    if (PgGetVideoModeInfo(my_hwcaps.current_video_mode, &desktop_mode) < 0)
slouken@309
   357
    {
slouken@663
   358
        SDL_SetError("ph_VideoInit(): PgGetVideoModeInfo function failed !\n");
slouken@663
   359
        this->FreeWMCursor(this, SDL_BlankCursor);
slouken@663
   360
        return -1;
slouken@309
   361
    }
slouken@0
   362
slouken@309
   363
    /* We need to return BytesPerPixel as it in used by CreateRGBsurface */
slouken@692
   364
    vformat->BitsPerPixel = desktop_mode.bits_per_pixel;
slouken@692
   365
    vformat->BytesPerPixel = desktop_mode.bytes_per_scanline/desktop_mode.width;
slouken@692
   366
    desktopbpp = desktop_mode.bits_per_pixel;
slouken@320
   367
    
slouken@320
   368
    /* save current palette */
slouken@320
   369
    if (desktopbpp==8)
slouken@320
   370
    {
slouken@571
   371
        PgGetPalette(savedpal);
slouken@571
   372
        PgGetPalette(syspalph);
slouken@320
   373
    }
slouken@663
   374
    else
slouken@663
   375
    {
slouken@663
   376
        for(i=0; i<_Pg_MAX_PALETTE; i++)
slouken@663
   377
        {
slouken@663
   378
            savedpal[i]=PgRGB(0, 0, 0);
slouken@663
   379
            syspalph[i]=PgRGB(0, 0, 0);
slouken@663
   380
        }
slouken@663
   381
    }
slouken@309
   382
         
slouken@19
   383
    currently_fullscreen = 0;
slouken@663
   384
    currently_hided = 0;
slouken@663
   385
    current_overlay = NULL;
slouken@663
   386
slouken@663
   387
    OCImage.direct_context = NULL;
slouken@663
   388
    OCImage.offscreen_context = NULL;
slouken@663
   389
    OCImage.offscreen_backcontext = NULL;
slouken@663
   390
    OCImage.oldDC = NULL;
slouken@663
   391
    OCImage.CurrentFrameData = NULL;
slouken@663
   392
    OCImage.FrameData0 = NULL;
slouken@663
   393
    OCImage.FrameData1 = NULL;
slouken@663
   394
slouken@19
   395
    
slouken@19
   396
    this->info.wm_available = 1;
slouken@19
   397
    
slouken@0
   398
    return 0;
slouken@0
   399
}
slouken@0
   400
slouken@0
   401
static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
   402
                int width, int height, int bpp, Uint32 flags)
slouken@0
   403
{
slouken@663
   404
    const struct ColourMasks* mask;
slouken@0
   405
slouken@0
   406
    /* Lock the event thread, in multi-threading environments */
slouken@0
   407
    SDL_Lock_EventThread();
slouken@0
   408
slouken@320
   409
    current->flags = flags;
slouken@320
   410
slouken@663
   411
    /* if we do not have desired fullscreen mode, then fallback into window mode */
slouken@663
   412
    if (((current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) && (ph_GetVideoMode(width, height, bpp)==0))
slouken@663
   413
    {
slouken@663
   414
       current->flags &= ~SDL_FULLSCREEN;
slouken@663
   415
       current->flags &= ~SDL_NOFRAME;
slouken@663
   416
       current->flags &= ~SDL_RESIZABLE;
slouken@663
   417
    }
slouken@663
   418
slouken@663
   419
    ph_SetupWindow(this, width, height, current->flags);
slouken@663
   420
slouken@663
   421
    mask = ph_GetColourMasks(bpp);
slouken@663
   422
    if (mask != NULL)
slouken@663
   423
    {
slouken@663
   424
        SDL_ReallocFormat(current, mask->bpp, mask->red, mask->green, mask->blue, 0);
slouken@663
   425
    }
slouken@663
   426
    else
slouken@663
   427
    {
slouken@663
   428
        SDL_SetError("ph_SetVideoMode(): desired bpp is not supported by photon !\n");
slouken@663
   429
        return NULL;
slouken@663
   430
    }
slouken@309
   431
slouken@309
   432
#ifdef HAVE_OPENGL
slouken@701
   433
    if ((current->flags & SDL_OPENGL)==SDL_OPENGL)
slouken@309
   434
    {
slouken@309
   435
#else
slouken@701
   436
    if ((current->flags & SDL_OPENGL)==SDL_OPENGL) /* if no built-in OpenGL support */
slouken@309
   437
    {
slouken@663
   438
        SDL_SetError("ph_SetVideoMode(): no OpenGL support, try to recompile library.\n");
slouken@571
   439
        current->flags &= ~SDL_OPENGL;
slouken@309
   440
        return NULL;
slouken@309
   441
#endif /* HAVE_OPENGL */
slouken@309
   442
    }
slouken@309
   443
    else
slouken@309
   444
    {
slouken@663
   445
        /* Initialize internal variables */
slouken@663
   446
        if ((current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN)
slouken@309
   447
        {
slouken@315
   448
            if (bpp==8)
slouken@315
   449
            {
slouken@315
   450
               desktoppal=SDLPH_PAL_SYSTEM;
slouken@315
   451
            }
slouken@309
   452
slouken@571
   453
            current->flags &= ~SDL_RESIZABLE; /* no resize for Direct Context */
slouken@692
   454
            current->flags |= SDL_HWSURFACE;
slouken@663
   455
        }
slouken@309
   456
        else
slouken@309
   457
        {
slouken@663
   458
            /* remove this if we'll support non-fullscreen sw/hw+doublebuf */
slouken@663
   459
            current->flags &= ~SDL_DOUBLEBUF;
slouken@663
   460
slouken@663
   461
            /* Use offscreen memory if SDL_HWSURFACE flag is set */
slouken@663
   462
            if ((current->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
slouken@309
   463
            {
slouken@663
   464
slouken@663
   465
                if (desktopbpp!=bpp)
slouken@663
   466
                {
slouken@663
   467
                   current->flags &= ~SDL_HWSURFACE;
slouken@663
   468
                }
slouken@309
   469
            }
slouken@320
   470
slouken@315
   471
            /* using palette emulation code in window mode */
slouken@315
   472
            if (bpp==8)
slouken@315
   473
            {
slouken@315
   474
                if (desktopbpp>=15)
slouken@315
   475
                {
slouken@663
   476
                    desktoppal = SDLPH_PAL_EMULATE;
slouken@315
   477
                }
slouken@315
   478
                else
slouken@315
   479
                {
slouken@663
   480
                    desktoppal = SDLPH_PAL_SYSTEM;
slouken@571
   481
                }
slouken@315
   482
            }
slouken@315
   483
            else
slouken@315
   484
            {
slouken@663
   485
               desktoppal = SDLPH_PAL_NONE;
slouken@315
   486
            }
slouken@0
   487
        }
slouken@0
   488
    }
slouken@0
   489
slouken@309
   490
    current->w = width;
slouken@309
   491
    current->h = height;
slouken@571
   492
slouken@663
   493
    if (desktoppal==SDLPH_PAL_SYSTEM)
slouken@663
   494
    {
slouken@663
   495
       current->flags|=SDL_HWPALETTE;
slouken@663
   496
    }
slouken@370
   497
slouken@663
   498
    /* Must call at least once for setup image planes */
slouken@663
   499
    if (ph_SetupUpdateFunction(this, current, current->flags)==-1)
slouken@370
   500
    {
slouken@370
   501
        return NULL;
slouken@370
   502
    }
slouken@0
   503
slouken@663
   504
    /* finish window drawing, if we are not in fullscreen, of course */
slouken@663
   505
    if ((current->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
slouken@663
   506
    {
slouken@663
   507
       PtFlush();
slouken@663
   508
    }
slouken@692
   509
    else
slouken@692
   510
    {
slouken@692
   511
       PgFlush();
slouken@692
   512
    }
slouken@320
   513
slouken@0
   514
    SDL_Unlock_EventThread();
slouken@0
   515
slouken@0
   516
    /* We're done! */
slouken@315
   517
    return (current);
slouken@0
   518
}
slouken@0
   519
slouken@0
   520
static void ph_VideoQuit(_THIS)
slouken@0
   521
{
slouken@663
   522
    /* restore palette */
slouken@663
   523
    if (desktopbpp==8)
slouken@663
   524
    {
slouken@663
   525
        PgSetPalette(syspalph, 0, -1, 0, 0, 0);
slouken@663
   526
        PgSetPalette(savedpal, 0, 0, _Pg_MAX_PALETTE, Pg_PALSET_GLOBAL | Pg_PALSET_FORCE_EXPOSE, 0);
slouken@663
   527
        PgFlush();
slouken@663
   528
    }
slouken@663
   529
slouken@283
   530
    ph_DestroyImage(this, SDL_VideoSurface); 
slouken@0
   531
slouken@309
   532
    if (window)
slouken@309
   533
    {
slouken@309
   534
        PtUnrealizeWidget(window);
slouken@309
   535
        PtDestroyWidget(window);
slouken@309
   536
        window=NULL;
slouken@309
   537
    }
slouken@309
   538
slouken@571
   539
    if (event!=NULL)
slouken@571
   540
    {
slouken@571
   541
        free(event);
slouken@571
   542
        event=NULL;
slouken@571
   543
    }
slouken@0
   544
}
slouken@0
   545
slouken@0
   546
static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   547
{
slouken@315
   548
    int i;
slouken@571
   549
    SDL_Rect updaterect;
slouken@571
   550
slouken@571
   551
    updaterect.x = updaterect.y = 0;
slouken@571
   552
    updaterect.w = this->screen->w;
slouken@571
   553
    updaterect.h = this->screen->h;
slouken@0
   554
slouken@315
   555
    /* palette emulation code, using palette of the PhImage_t struct */
slouken@315
   556
    if (desktoppal==SDLPH_PAL_EMULATE)
slouken@315
   557
    {
slouken@315
   558
        if ((SDL_Image) && (SDL_Image->palette))
slouken@315
   559
        {
slouken@315
   560
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   561
            {
slouken@571
   562
                syspalph[i] = PgRGB(colors[i-firstcolor].r, colors[i-firstcolor].g, colors[i-firstcolor].b);
slouken@571
   563
                SDL_Image->palette[i] = syspalph[i];
slouken@315
   564
            }
slouken@663
   565
slouken@571
   566
            /* image needs to be redrawn */
slouken@571
   567
            this->UpdateRects(this, 1, &updaterect);
slouken@315
   568
        }
slouken@315
   569
    }
slouken@315
   570
    else
slouken@315
   571
    {
slouken@315
   572
        if (desktoppal==SDLPH_PAL_SYSTEM)
slouken@315
   573
        {
slouken@315
   574
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   575
            {
slouken@571
   576
                syspalph[i] = PgRGB(colors[i-firstcolor].r, colors[i-firstcolor].g, colors[i-firstcolor].b);
slouken@315
   577
            }
slouken@0
   578
slouken@315
   579
            if ((this->screen->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
slouken@315
   580
            {
slouken@571
   581
                 /* window mode must use soft palette */
slouken@663
   582
                PgSetPalette(&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
slouken@571
   583
                /* image needs to be redrawn */
slouken@571
   584
                this->UpdateRects(this, 1, &updaterect);
slouken@315
   585
            }
slouken@315
   586
            else
slouken@315
   587
            {
slouken@315
   588
                /* fullscreen mode must use hardware palette */
slouken@663
   589
                PgSetPalette(&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
slouken@315
   590
            }
slouken@315
   591
        }
slouken@315
   592
        else
slouken@315
   593
        {
slouken@315
   594
            /* SDLPH_PAL_NONE do nothing */
slouken@315
   595
        }
slouken@0
   596
    }
slouken@315
   597
    
slouken@315
   598
    return 1;
slouken@0
   599
}
slouken@0
   600
slouken@279
   601
#ifdef HAVE_OPENGL
slouken@309
   602
slouken@701
   603
static void ph_GL_SwapBuffers(_THIS)
slouken@266
   604
{
slouken@291
   605
    PgSetRegion(PtWidgetRid(window));
slouken@309
   606
    PdOpenGLContextSwapBuffers(oglctx);
slouken@266
   607
}
slouken@266
   608
slouken@701
   609
static int ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   610
{
slouken@291
   611
    switch (attrib)
slouken@291
   612
    {
slouken@291
   613
        case SDL_GL_DOUBLEBUFFER:
slouken@291
   614
             *value=this->gl_config.double_buffer;
slouken@291
   615
             break;
slouken@291
   616
        case SDL_GL_STENCIL_SIZE:
slouken@291
   617
             *value=this->gl_config.stencil_size;
slouken@291
   618
             break;
slouken@291
   619
        case SDL_GL_DEPTH_SIZE:
slouken@291
   620
             *value=this->gl_config.depth_size;
slouken@291
   621
             break;
slouken@291
   622
        default:
slouken@291
   623
             *value=0;
slouken@291
   624
             return(-1);
slouken@291
   625
    }
slouken@291
   626
    return 0;
slouken@291
   627
}
slouken@0
   628
slouken@701
   629
static int ph_GL_LoadLibrary(_THIS, const char* path)
slouken@663
   630
{
slouken@701
   631
   /* if code compiled with HAVE_OPENGL, that mean that library already linked */
slouken@663
   632
   this->gl_config.driver_loaded = 1;
slouken@663
   633
slouken@663
   634
   return 0;
slouken@663
   635
}
slouken@663
   636
slouken@701
   637
static void* ph_GL_GetProcAddress(_THIS, const char* proc)
slouken@663
   638
{
slouken@663
   639
   return NULL;
slouken@663
   640
}
slouken@663
   641
slouken@701
   642
static int ph_GL_MakeCurrent(_THIS)
slouken@701
   643
{
slouken@701
   644
    PgSetRegion(PtWidgetRid(window));
slouken@571
   645
slouken@701
   646
    if (oglctx!=NULL)
slouken@571
   647
    {
slouken@701
   648
        PhDCSetCurrent(oglctx);
slouken@571
   649
    }
slouken@571
   650
slouken@701
   651
    return 0;
slouken@571
   652
}
slouken@701
   653
slouken@701
   654
#endif /* HAVE_OPENGL */