src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 Sep 2003 18:13:48 +0000
changeset 718 cbc0f7fabd1c
parent 701 aaf3b8af6616
child 753 b14fdadd8311
permissions -rw-r--r--
Date: Sat, 13 Sep 2003 15:50:43 +0300
From: "Mike Gorchak"
Subject: QNX fixes

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