src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 May 2002 19:54:01 +0000
changeset 370 ba72f259bc88
parent 320 66f815c147ed
child 380 bce7171e7a85
permissions -rw-r--r--
Date: Sat, 18 May 2002 17:40:53 +0300
From: Mike Gorchak <mike@malva.ua>
Subject: New QNX patches

- Added more error check to avoid SEGFAULTS;
- Fixed bug in update function for SDL_HWSURFACE. BTW: update is much
faster than before.
- Added checks for SDL_HWSURFACE flag, chosen bpp must be equal to
desktop bpp for SDL_HWSURFACE.
- Fixed overlay bug, no more SEGFAULTS.
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@0
    53
static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@0
    54
static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
    55
                int width, int height, int bpp, Uint32 flags);
slouken@0
    56
static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
slouken@0
    57
static void ph_VideoQuit(_THIS);
slouken@0
    58
static void ph_DeleteDevice(SDL_VideoDevice *device);
slouken@291
    59
slouken@291
    60
#ifdef HAVE_OPENGL
slouken@309
    61
int ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags);
slouken@266
    62
static void ph_GL_SwapBuffers(_THIS);
slouken@291
    63
static int ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value);
slouken@291
    64
#endif /* HAVE_OPENGL */
slouken@266
    65
slouken@0
    66
static int ph_Available(void)
slouken@0
    67
{
slouken@309
    68
    int phstat=-1;
slouken@309
    69
slouken@309
    70
    phstat=PtInit(0);
slouken@309
    71
    if (phstat==0)
slouken@309
    72
    {
slouken@309
    73
       return 1;
slouken@309
    74
    }
slouken@309
    75
    else
slouken@309
    76
    {
slouken@309
    77
       return 0;
slouken@309
    78
    }
slouken@0
    79
}
slouken@0
    80
slouken@0
    81
static SDL_VideoDevice *ph_CreateDevice(int devindex)
slouken@0
    82
{
slouken@0
    83
    SDL_VideoDevice *device;
slouken@0
    84
slouken@0
    85
    /* Initialize all variables that we clean on shutdown */
slouken@0
    86
    device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
slouken@309
    87
    if (device) {
slouken@0
    88
        memset(device, 0, (sizeof *device));
slouken@0
    89
        device->hidden = (struct SDL_PrivateVideoData *)
slouken@0
    90
                malloc((sizeof *device->hidden));
slouken@0
    91
        device->gl_data = NULL;
slouken@0
    92
    }
slouken@0
    93
    if ( (device == NULL) || (device->hidden == NULL) ) {
slouken@0
    94
        SDL_OutOfMemory();
slouken@0
    95
        ph_DeleteDevice(device);
slouken@0
    96
        return(0);
slouken@0
    97
    }
slouken@0
    98
    memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
    99
slouken@0
   100
    /* Set the driver flags */
slouken@279
   101
    device->handles_any_size = 1; /* JB not true for fullscreen */
slouken@0
   102
slouken@0
   103
    /* Set the function pointers */
slouken@266
   104
    device->CreateYUVOverlay = ph_CreateYUVOverlay;
slouken@0
   105
    device->VideoInit = ph_VideoInit;
slouken@0
   106
    device->ListModes = ph_ListModes;
slouken@0
   107
    device->SetVideoMode = ph_SetVideoMode;
slouken@309
   108
    device->ToggleFullScreen = ph_ToggleFullScreen;
slouken@0
   109
    device->UpdateMouse = NULL;	
slouken@0
   110
    device->SetColors = ph_SetColors;
slouken@309
   111
    device->UpdateRects = NULL;         /* ph_ResizeImage */
slouken@0
   112
    device->VideoQuit = ph_VideoQuit;
slouken@0
   113
    device->AllocHWSurface = ph_AllocHWSurface;
slouken@0
   114
    device->CheckHWBlit = NULL;
slouken@0
   115
    device->FillHWRect = NULL;
slouken@0
   116
    device->SetHWColorKey = NULL;
slouken@0
   117
    device->SetHWAlpha = NULL;
slouken@0
   118
    device->LockHWSurface = ph_LockHWSurface;
slouken@0
   119
    device->UnlockHWSurface = ph_UnlockHWSurface;
slouken@0
   120
    device->FlipHWSurface = ph_FlipHWSurface;
slouken@0
   121
    device->FreeHWSurface = ph_FreeHWSurface;
slouken@19
   122
    device->SetCaption = ph_SetCaption;
slouken@0
   123
    device->SetIcon = NULL;
slouken@19
   124
    device->IconifyWindow = ph_IconifyWindow;
slouken@283
   125
    device->GrabInput = ph_GrabInput;
slouken@291
   126
    device->GetWMInfo = ph_GetWMInfo;
slouken@0
   127
    device->FreeWMCursor = ph_FreeWMCursor;
slouken@0
   128
    device->CreateWMCursor = ph_CreateWMCursor;
slouken@0
   129
    device->ShowWMCursor = ph_ShowWMCursor;
slouken@0
   130
    device->WarpWMCursor = ph_WarpWMCursor;
slouken@0
   131
    device->CheckMouseMode = ph_CheckMouseMode;
slouken@0
   132
    device->InitOSKeymap = ph_InitOSKeymap;
slouken@0
   133
    device->PumpEvents = ph_PumpEvents;
slouken@0
   134
slouken@279
   135
    /* OpenGL support. */
slouken@266
   136
    device->GL_LoadLibrary = NULL;
slouken@266
   137
    device->GL_GetProcAddress = NULL;
slouken@266
   138
    device->GL_MakeCurrent = NULL;
slouken@279
   139
#ifdef HAVE_OPENGL
slouken@266
   140
    device->GL_SwapBuffers = ph_GL_SwapBuffers;
slouken@291
   141
    device->GL_GetAttribute = ph_GL_GetAttribute;
slouken@279
   142
#else
slouken@279
   143
    device->GL_SwapBuffers = NULL;
slouken@291
   144
    device->GL_GetAttribute = NULL;
slouken@279
   145
#endif /* HAVE_OPENGL */
slouken@266
   146
slouken@0
   147
    device->free = ph_DeleteDevice;
slouken@0
   148
slouken@0
   149
    return device;
slouken@0
   150
}
slouken@0
   151
slouken@19
   152
VideoBootStrap ph_bootstrap = {
slouken@315
   153
    "photon", "QNX Photon video output",
slouken@315
   154
    ph_Available, ph_CreateDevice
slouken@0
   155
};
slouken@0
   156
slouken@0
   157
static void ph_DeleteDevice(SDL_VideoDevice *device)
slouken@0
   158
{
slouken@309
   159
    if (device)
slouken@309
   160
    {
slouken@309
   161
        if (device->hidden)
slouken@309
   162
        {
slouken@0
   163
            free(device->hidden);
slouken@0
   164
            device->hidden = NULL;
slouken@0
   165
        }
slouken@309
   166
        if (device->gl_data)
slouken@309
   167
        {
slouken@0
   168
            free(device->gl_data);
slouken@0
   169
            device->gl_data = NULL;
slouken@0
   170
        }
slouken@0
   171
        free(device);
slouken@0
   172
        device = NULL;
slouken@0
   173
    }
slouken@0
   174
}
slouken@0
   175
slouken@0
   176
static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   177
{
slouken@309
   178
    PgVideoModeInfo_t my_mode_info;
slouken@309
   179
    PgHWCaps_t my_hwcaps;
slouken@309
   180
slouken@309
   181
    window=NULL;
slouken@320
   182
    desktoppal=SDLPH_PAL_NONE;
slouken@320
   183
#ifdef HAVE_OPENGL
slouken@309
   184
    oglctx=NULL;
slouken@320
   185
#endif /* HAVE_OPENGL */
slouken@315
   186
    
slouken@315
   187
    captionflag=0;
slouken@315
   188
    old_video_mode=-1;
slouken@315
   189
    old_refresh_rate=-1;
slouken@0
   190
	
slouken@309
   191
    if (NULL == (event = malloc(EVENT_SIZE)))
slouken@309
   192
    {
slouken@309
   193
        exit(EXIT_FAILURE);
slouken@309
   194
    }
slouken@0
   195
slouken@309
   196
    /* Create the blank cursor */
slouken@309
   197
    SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
slouken@309
   198
                                          (int)BLANK_CWIDTH, (int)BLANK_CHEIGHT,
slouken@309
   199
                                          (int)BLANK_CHOTX, (int)BLANK_CHOTY);
slouken@0
   200
slouken@309
   201
    if (SDL_BlankCursor == NULL)
slouken@309
   202
    {
slouken@309
   203
        printf("ph_VideoInit: could not create blank cursor\n");
slouken@309
   204
    }
slouken@0
   205
slouken@309
   206
    if (PgGetGraphicsHWCaps(&my_hwcaps) < 0)
slouken@309
   207
    {
slouken@309
   208
        fprintf(stderr,"ph_VideoInit: GetGraphicsHWCaps failed!! \n");
slouken@309
   209
    }
slouken@0
   210
slouken@309
   211
    if (PgGetVideoModeInfo(my_hwcaps.current_video_mode, &my_mode_info) < 0)
slouken@309
   212
    {
slouken@309
   213
        fprintf(stderr,"ph_VideoInit:  PgGetVideoModeInfo failed\n");
slouken@309
   214
    }
slouken@0
   215
slouken@309
   216
    /* We need to return BytesPerPixel as it in used by CreateRGBsurface */
slouken@309
   217
    vformat->BitsPerPixel = my_mode_info.bits_per_pixel;
slouken@309
   218
    vformat->BytesPerPixel = my_mode_info.bytes_per_scanline/my_mode_info.width;
slouken@315
   219
    desktopbpp = my_mode_info.bits_per_pixel;
slouken@320
   220
    
slouken@320
   221
    /* save current palette */
slouken@320
   222
    if (desktopbpp==8)
slouken@320
   223
    {
slouken@320
   224
        PgGetPalette(ph_palette);
slouken@320
   225
    }
slouken@309
   226
         
slouken@19
   227
    currently_fullscreen = 0;
slouken@19
   228
    
slouken@19
   229
    this->info.wm_available = 1;
slouken@19
   230
    
slouken@0
   231
    return 0;
slouken@0
   232
}
slouken@0
   233
slouken@0
   234
static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
   235
                int width, int height, int bpp, Uint32 flags)
slouken@0
   236
{
slouken@0
   237
    PgDisplaySettings_t settings;
slouken@309
   238
    int mode;
slouken@309
   239
    PtArg_t arg[32];
slouken@309
   240
    PhDim_t dim;
slouken@266
   241
    int rtnval;
slouken@266
   242
    int i;
slouken@266
   243
    unsigned long *tempptr;
slouken@309
   244
    int pargc;
slouken@0
   245
slouken@266
   246
    dim.w=width;
slouken@266
   247
    dim.h=height;
slouken@0
   248
slouken@0
   249
    /* Lock the event thread, in multi-threading environments */
slouken@0
   250
    SDL_Lock_EventThread();
slouken@0
   251
slouken@320
   252
    current->flags = flags;
slouken@320
   253
slouken@309
   254
    /* create window if no OpenGL support selected */
slouken@309
   255
    if ((flags & SDL_OPENGL)!=SDL_OPENGL)
slouken@0
   256
    {
slouken@309
   257
        pargc=0;
slouken@370
   258
        
slouken@370
   259
        // prevent using HWSURFACE in window mode if desktop bpp != chosen bpp
slouken@370
   260
        if ((flags & SDL_HWSURFACE) && (!(flags & SDL_FULLSCREEN)))
slouken@370
   261
        {
slouken@370
   262
           if (desktopbpp!=bpp)
slouken@370
   263
           {
slouken@370
   264
              fprintf(stderr, "ph_SetVideoMode(): SDL_HWSURFACE available only with chosen bpp equal desktop bpp !\n");
slouken@370
   265
              return NULL;
slouken@370
   266
           }
slouken@370
   267
        }
slouken@309
   268
slouken@309
   269
        PtSetArg(&arg[pargc++], Pt_ARG_DIM, &dim, 0);
slouken@309
   270
        PtSetArg(&arg[pargc++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@309
   271
slouken@320
   272
        /* enable window minimizing */
slouken@320
   273
        PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_HIDE);
slouken@320
   274
slouken@320
   275
        /* remove border and caption if no frame flag selected */
slouken@320
   276
        if ((flags & SDL_NOFRAME) == SDL_NOFRAME)
slouken@320
   277
        {
slouken@320
   278
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Ph_WM_RENDER_TITLE | Ph_WM_RENDER_BORDER);
slouken@320
   279
        }
slouken@320
   280
        else
slouken@320
   281
        {
slouken@320
   282
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_TITLE | Ph_WM_RENDER_BORDER);
slouken@320
   283
        }
slouken@320
   284
slouken@320
   285
        /* if window is not resizable then remove resize handles and maximize button */
slouken@320
   286
        if ((flags & SDL_RESIZABLE) != SDL_RESIZABLE)
slouken@320
   287
        {
slouken@320
   288
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_MAX);
slouken@320
   289
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   290
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   291
        }
slouken@320
   292
        else
slouken@320
   293
        {
slouken@320
   294
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_MAX);
slouken@320
   295
            /* it is need to be Pt_FALSE to allow the application to process the resize callback */
slouken@320
   296
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   297
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   298
        }
slouken@320
   299
slouken@309
   300
        if (window!=NULL)
slouken@0
   301
        {
slouken@309
   302
            PtUnrealizeWidget(window);
slouken@309
   303
            PtDestroyWidget(window);
slouken@309
   304
            window=NULL;
slouken@309
   305
        }
slouken@309
   306
slouken@320
   307
        window=PtCreateWidget(PtWindow, NULL, pargc, arg);
slouken@309
   308
        PtRealizeWidget(window);
slouken@315
   309
        
slouken@309
   310
        PtFlush();
slouken@309
   311
    }
slouken@309
   312
slouken@309
   313
#ifdef HAVE_OPENGL
slouken@309
   314
    if (flags & SDL_OPENGL)
slouken@309
   315
    {
slouken@309
   316
        /* ph_SetupOpenGLContext creates also window as need */
slouken@309
   317
        if (ph_SetupOpenGLContext(this, width, height, bpp, flags)==0)
slouken@309
   318
        {
slouken@309
   319
            /* setup OGL update function ... ugly method */
slouken@309
   320
            ph_ResizeImage(this, current, flags); 
slouken@0
   321
        }
slouken@0
   322
        else
slouken@0
   323
        {
slouken@309
   324
            /* if context creation fail, report no OpenGL to high level */
slouken@309
   325
            current->flags=(flags & (~SDL_OPENGL));
slouken@309
   326
        }
slouken@309
   327
#else
slouken@309
   328
    if (flags & SDL_OPENGL) /* if no built-in OpenGL support */
slouken@309
   329
    {
slouken@370
   330
        fprintf(stderr, "ph_SetVideoMode(): no OpenGL support, try to recompile library.\n");
slouken@309
   331
        current->flags=(flags & (~SDL_OPENGL));
slouken@309
   332
        return NULL;
slouken@309
   333
#endif /* HAVE_OPENGL */
slouken@309
   334
    }
slouken@309
   335
    else
slouken@309
   336
    {
slouken@309
   337
        /* Initialize the window */
slouken@309
   338
        if (flags & SDL_FULLSCREEN) /* Direct Context , assume SDL_HWSURFACE also set */
slouken@309
   339
        {
slouken@309
   340
            /* Get the video mode and set it */
slouken@309
   341
            if (flags & SDL_ANYFORMAT)
slouken@0
   342
            {
slouken@309
   343
                if ((mode = get_mode_any_format(width, height, bpp)) == 0)
slouken@309
   344
                {
slouken@370
   345
                    fprintf(stderr,"ph_SetVideoMode(): get_mode_any_format failed !\n");
slouken@309
   346
                    exit(1);
slouken@309
   347
                }
slouken@309
   348
            }
slouken@309
   349
            else
slouken@309
   350
            {
slouken@309
   351
                if ((mode = get_mode(width, height, bpp)) == 0)
slouken@309
   352
                {
slouken@370
   353
                    fprintf(stderr,"ph_SetVideoMode(): get_mode failed !\n");
slouken@309
   354
                    exit(1);
slouken@309
   355
                }
slouken@309
   356
            }
slouken@315
   357
            
slouken@315
   358
            if (bpp==8)
slouken@315
   359
            {
slouken@315
   360
               desktoppal=SDLPH_PAL_SYSTEM;
slouken@315
   361
            }
slouken@315
   362
            
slouken@315
   363
            /* save old video mode caps */
slouken@315
   364
            PgGetVideoMode(&settings);
slouken@315
   365
            old_video_mode=settings.mode;
slouken@315
   366
            old_refresh_rate=settings.refresh;
slouken@315
   367
slouken@315
   368
            /* setup new video mode */
slouken@309
   369
            settings.mode = mode;
slouken@309
   370
            settings.refresh = 0;
slouken@315
   371
            settings.flags = 0;
slouken@309
   372
slouken@315
   373
            if (PgSetVideoMode(&settings) < 0)
slouken@309
   374
            {
slouken@370
   375
                fprintf(stderr,"ph_SetVideoMode(): PgSetVideoMode failed !\n");
slouken@309
   376
            }
slouken@309
   377
slouken@309
   378
            current->flags = (flags & (~SDL_RESIZABLE)); /* no resize for Direct Context */
slouken@309
   379
slouken@309
   380
            /* Begin direct mode */
slouken@309
   381
            ph_EnterFullScreen(this);
slouken@309
   382
slouken@309
   383
        } /* end fullscreen flag */
slouken@309
   384
        else
slouken@309
   385
        {
slouken@320
   386
            /* Use offscreen memory iff SDL_HWSURFACE flag is set */
slouken@320
   387
            if (flags & SDL_HWSURFACE)
slouken@309
   388
            {
slouken@320
   389
                /* no stretch blit in offscreen context */
slouken@320
   390
                current->flags = (flags & (~SDL_RESIZABLE));
slouken@309
   391
            }
slouken@320
   392
slouken@315
   393
            /* using palette emulation code in window mode */
slouken@315
   394
            if (bpp==8)
slouken@315
   395
            {
slouken@315
   396
                if (desktopbpp>=15)
slouken@315
   397
                {
slouken@315
   398
                    desktoppal=SDLPH_PAL_EMULATE;
slouken@315
   399
                }
slouken@315
   400
                else
slouken@315
   401
                {
slouken@315
   402
                    desktoppal=SDLPH_PAL_SYSTEM;
slouken@315
   403
                }
slouken@315
   404
            }
slouken@315
   405
            else
slouken@315
   406
            {
slouken@315
   407
               desktoppal=SDLPH_PAL_NONE;
slouken@315
   408
            }
slouken@0
   409
        }
slouken@266
   410
slouken@309
   411
	/* If we are setting video to use the palette make sure we have allocated memory for it */
slouken@309
   412
	if (bpp==8)
slouken@309
   413
	{
slouken@309
   414
            current->format->palette = malloc(sizeof(SDL_Palette));
slouken@309
   415
            memset(current->format->palette, 0, sizeof(SDL_Palette));
slouken@309
   416
            current->format->palette->ncolors = 256;
slouken@309
   417
            current->format->palette->colors = (SDL_Color *)malloc(256 *sizeof(SDL_Color));
slouken@309
   418
            /* fill the palette */
slouken@309
   419
            rtnval = PgGetPalette(ph_palette);
slouken@309
   420
slouken@309
   421
            tempptr = (unsigned long *)current->format->palette->colors;
slouken@309
   422
slouken@309
   423
            for(i=0; i<256; i++)
slouken@309
   424
            {
slouken@309
   425
                *tempptr = (((unsigned long)ph_palette[i]) << 8);
slouken@309
   426
                tempptr++;
slouken@309
   427
            }
slouken@0
   428
        }
slouken@0
   429
slouken@0
   430
    }
slouken@0
   431
slouken@309
   432
    current->w = width;
slouken@309
   433
    current->h = height;
slouken@309
   434
    current->format->BitsPerPixel = bpp;
slouken@309
   435
    current->format->BytesPerPixel = (bpp+7)/8;
slouken@309
   436
    current->pitch = SDL_CalculatePitch(current);
slouken@370
   437
slouken@309
   438
    /* Must call at least once it setup image planes */
slouken@370
   439
    rtnval = ph_ResizeImage(this, current, flags);
slouken@370
   440
    
slouken@370
   441
    if (rtnval==-1)
slouken@370
   442
    {
slouken@370
   443
        fprintf(stderr,"ph_SetVideoMode(): ph_ResizeImage failed !\n");
slouken@370
   444
        return NULL;
slouken@370
   445
    }
slouken@0
   446
slouken@315
   447
    /* delayed set caption call */
slouken@315
   448
    if (captionflag)
slouken@315
   449
    {
slouken@315
   450
        ph_SetCaption(this, this->wm_title, NULL);
slouken@315
   451
    }
slouken@315
   452
slouken@320
   453
    /* finish window drawing */
slouken@320
   454
    PtFlush();
slouken@320
   455
slouken@0
   456
    SDL_Unlock_EventThread();
slouken@0
   457
slouken@0
   458
    /* We're done! */
slouken@315
   459
    return (current);
slouken@0
   460
}
slouken@0
   461
slouken@0
   462
static void ph_VideoQuit(_THIS)
slouken@0
   463
{
slouken@320
   464
#ifdef HAVE_OPENGL
slouken@309
   465
    PhRegion_t region_info;
slouken@320
   466
#endif /* HAVE_OPENGL */
slouken@309
   467
slouken@283
   468
    ph_DestroyImage(this, SDL_VideoSurface); 
slouken@0
   469
slouken@279
   470
    if (currently_fullscreen)
slouken@279
   471
    {
slouken@315
   472
        ph_LeaveFullScreen(this);
slouken@279
   473
    }
slouken@309
   474
slouken@309
   475
#ifdef HAVE_OPENGL
slouken@320
   476
    /* prevent double SEGFAULT during parachute mode */
slouken@315
   477
    if (this->screen)
slouken@309
   478
    {
slouken@315
   479
        if (((this->screen->flags & SDL_FULLSCREEN)==SDL_FULLSCREEN) &&
slouken@315
   480
            ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
slouken@315
   481
        {
slouken@315
   482
            region_info.cursor_type=Ph_CURSOR_POINTER;
slouken@315
   483
            region_info.rid=PtWidgetRid(window);
slouken@315
   484
            PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@315
   485
        }
slouken@309
   486
    }
slouken@309
   487
slouken@309
   488
    PtFlush();
slouken@309
   489
#endif /* HAVE_OPENGL */
slouken@309
   490
    
slouken@309
   491
    if (window)
slouken@309
   492
    {
slouken@309
   493
        PtUnrealizeWidget(window);
slouken@309
   494
        PtDestroyWidget(window);
slouken@309
   495
        window=NULL;
slouken@309
   496
    }
slouken@315
   497
    
slouken@315
   498
    /* restore palette */
slouken@315
   499
    if (desktoppal!=SDLPH_PAL_NONE)
slouken@315
   500
    {
slouken@315
   501
        PgSetPalette(ph_palette, 0, 0, _Pg_MAX_PALETTE, Pg_PALSET_GLOBAL | Pg_PALSET_FORCE_EXPOSE, 0);
slouken@315
   502
    }
slouken@309
   503
slouken@309
   504
#ifdef HAVE_OPENGL
slouken@309
   505
    if (oglctx)
slouken@309
   506
    {
slouken@309
   507
        PhDCSetCurrent(NULL);
slouken@309
   508
        PhDCRelease(oglctx);
slouken@309
   509
        oglctx=NULL;
slouken@309
   510
    }
slouken@309
   511
#endif /* HAVE_OPENGL */
slouken@0
   512
}
slouken@0
   513
slouken@0
   514
static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   515
{
slouken@315
   516
    int i;
slouken@315
   517
    PhPoint_t point={0, 0};
slouken@315
   518
    PgColor_t syspalph[_Pg_MAX_PALETTE];
slouken@0
   519
slouken@315
   520
    /* palette emulation code, using palette of the PhImage_t struct */
slouken@315
   521
    if (desktoppal==SDLPH_PAL_EMULATE)
slouken@315
   522
    {
slouken@315
   523
        if ((SDL_Image) && (SDL_Image->palette))
slouken@315
   524
        {
slouken@315
   525
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   526
            {
slouken@315
   527
                SDL_Image->palette[i]  = 0x00000000UL;
slouken@315
   528
                SDL_Image->palette[i] |= colors[i-firstcolor].r<<16;
slouken@315
   529
                SDL_Image->palette[i] |= colors[i-firstcolor].g<<8;
slouken@315
   530
                SDL_Image->palette[i] |= colors[i-firstcolor].b;
slouken@315
   531
            }
slouken@370
   532
slouken@370
   533
           /* image needs to be redrawed, very slow method */
slouken@370
   534
           PgDrawPhImage(&point, SDL_Image, 0);
slouken@315
   535
        }
slouken@315
   536
    }
slouken@315
   537
    else
slouken@315
   538
    {
slouken@315
   539
        if (desktoppal==SDLPH_PAL_SYSTEM)
slouken@315
   540
        {
slouken@315
   541
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   542
            {
slouken@315
   543
                syspalph[i]  = 0x00000000UL;
slouken@315
   544
                syspalph[i] |= colors[i-firstcolor].r<<16;
slouken@315
   545
                syspalph[i] |= colors[i-firstcolor].g<<8;
slouken@315
   546
                syspalph[i] |= colors[i-firstcolor].b;
slouken@315
   547
            }
slouken@0
   548
slouken@315
   549
            if ((this->screen->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
slouken@315
   550
            {
slouken@315
   551
                /* window mode must use soft palette */
slouken@320
   552
                PgSetPalette((PgColor_t*)&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_SOFT, 0);
slouken@315
   553
                /* image needs to be redrawed, very slow method */
slouken@370
   554
                if (SDL_Image)
slouken@370
   555
                {
slouken@370
   556
                   PgDrawPhImage(&point, SDL_Image, 0);
slouken@370
   557
                }
slouken@315
   558
            }
slouken@315
   559
            else
slouken@315
   560
            {
slouken@315
   561
                /* fullscreen mode must use hardware palette */
slouken@320
   562
                PgSetPalette((PgColor_t*)&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
slouken@315
   563
            }
slouken@315
   564
        }
slouken@315
   565
        else
slouken@315
   566
        {
slouken@315
   567
            /* SDLPH_PAL_NONE do nothing */
slouken@315
   568
        }
slouken@0
   569
    }
slouken@315
   570
    
slouken@315
   571
    return 1;
slouken@0
   572
}
slouken@0
   573
slouken@279
   574
#ifdef HAVE_OPENGL
slouken@309
   575
slouken@309
   576
int ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags)
slouken@309
   577
{
slouken@309
   578
    PtArg_t args[8];
slouken@309
   579
    PhDim_t dim;
slouken@309
   580
    uint64_t OGLAttrib[PH_OGL_MAX_ATTRIBS];
slouken@309
   581
    int OGLargc;
slouken@309
   582
    int pargc;
slouken@309
   583
slouken@309
   584
    dim.w=width;
slouken@309
   585
    dim.h=height;
slouken@309
   586
    
slouken@309
   587
    if (oglctx!=NULL)
slouken@309
   588
    {
slouken@309
   589
       PhDCSetCurrent(NULL);
slouken@309
   590
       PhDCRelease(oglctx);
slouken@309
   591
       oglctx=NULL;
slouken@309
   592
    }
slouken@309
   593
slouken@309
   594
    OGLargc=0;
slouken@309
   595
    if (this->gl_config.depth_size)
slouken@309
   596
    {
slouken@309
   597
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DEPTH_BITS;
slouken@309
   598
        OGLAttrib[OGLargc++]=this->gl_config.depth_size;
slouken@309
   599
    }
slouken@309
   600
    if (this->gl_config.stencil_size)
slouken@309
   601
    {
slouken@309
   602
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_STENCIL_BITS;
slouken@309
   603
        OGLAttrib[OGLargc++]=this->gl_config.stencil_size;
slouken@309
   604
    }
slouken@309
   605
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FORCE_SW;
slouken@309
   606
    if (flags & SDL_FULLSCREEN)
slouken@309
   607
    {
slouken@309
   608
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN;
slouken@309
   609
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DIRECT;
slouken@309
   610
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_BEST;
slouken@309
   611
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_CENTER;
slouken@309
   612
    }
slouken@309
   613
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_NONE;
slouken@309
   614
slouken@309
   615
    if (this->gl_config.double_buffer)
slouken@309
   616
    {
slouken@309
   617
        oglctx=PdCreateOpenGLContext(2, &dim, 0, OGLAttrib);
slouken@309
   618
    }
slouken@309
   619
    else
slouken@309
   620
    {
slouken@309
   621
        oglctx=PdCreateOpenGLContext(1, &dim, 0, OGLAttrib);
slouken@309
   622
    }
slouken@320
   623
slouken@309
   624
    if (oglctx==NULL)
slouken@309
   625
    {
slouken@370
   626
        fprintf(stderr,"ph_SetupOpenGLContext(): cannot create OpenGL context.\n");
slouken@309
   627
        return (-1);
slouken@309
   628
    }
slouken@309
   629
slouken@309
   630
    PhDCSetCurrent(oglctx);
slouken@309
   631
slouken@309
   632
    pargc=0;
slouken@309
   633
slouken@309
   634
    PtSetArg(&args[pargc++], Pt_ARG_DIM, &dim, 0);
slouken@309
   635
    PtSetArg(&args[pargc++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@309
   636
    PtSetArg(&args[pargc++], Pt_ARG_FILL_COLOR, Pg_BLACK, 0);
slouken@309
   637
slouken@309
   638
    if (flags & SDL_FULLSCREEN)
slouken@309
   639
    {
slouken@309
   640
        PhPoint_t pos;
slouken@309
   641
slouken@309
   642
        pos.x=0;
slouken@309
   643
        pos.y=0;
slouken@309
   644
slouken@309
   645
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, ~0);
slouken@320
   646
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_FFRONT | Ph_WM_CLOSE | Ph_WM_TOFRONT | Ph_WM_CONSWITCH);
slouken@309
   647
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISFOCUS);
slouken@309
   648
        PtSetArg(&args[pargc++], Pt_ARG_POS, &pos, 0);
slouken@309
   649
    }
slouken@320
   650
    else
slouken@320
   651
    {
slouken@320
   652
        /* remove border and caption if no frame flag selected */
slouken@320
   653
        if ((flags & SDL_NOFRAME) == SDL_NOFRAME)
slouken@320
   654
        {
slouken@320
   655
            PtSetArg(&args[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, 0, Ph_WM_RENDER_TITLE | Ph_WM_RENDER_BORDER);
slouken@320
   656
        }
slouken@320
   657
        else
slouken@320
   658
        {
slouken@320
   659
           /* if window is not resizable then remove resize handles */
slouken@320
   660
           if ((flags & SDL_RESIZABLE) != SDL_RESIZABLE)
slouken@320
   661
           {
slouken@320
   662
               PtSetArg(&args[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, 0, Ph_WM_RENDER_RESIZE);
slouken@320
   663
           }
slouken@320
   664
        }
slouken@320
   665
    }
slouken@309
   666
slouken@309
   667
    if (window!=NULL)
slouken@309
   668
    {
slouken@309
   669
        PtUnrealizeWidget(window);
slouken@309
   670
        PtDestroyWidget(window);
slouken@309
   671
        window=NULL;
slouken@309
   672
    }
slouken@309
   673
slouken@320
   674
    window=PtCreateWidget(PtWindow, NULL, pargc, args);
slouken@309
   675
    PtRealizeWidget(window);
slouken@309
   676
slouken@309
   677
    /* disable mouse for fullscreen */
slouken@309
   678
    if (flags & SDL_FULLSCREEN)
slouken@309
   679
    {
slouken@309
   680
        PhRegion_t region_info;
slouken@309
   681
slouken@309
   682
        region_info.cursor_type=Ph_CURSOR_NONE;
slouken@309
   683
        region_info.rid=PtWidgetRid(window);
slouken@309
   684
        PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@309
   685
    }
slouken@309
   686
slouken@309
   687
    PtFlush();
slouken@309
   688
slouken@309
   689
    return 0;
slouken@309
   690
}
slouken@309
   691
slouken@266
   692
void ph_GL_SwapBuffers(_THIS)
slouken@266
   693
{
slouken@291
   694
    PgSetRegion(PtWidgetRid(window));
slouken@309
   695
    PdOpenGLContextSwapBuffers(oglctx);
slouken@266
   696
}
slouken@266
   697
slouken@291
   698
int ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   699
{
slouken@291
   700
    switch (attrib)
slouken@291
   701
    {
slouken@291
   702
        case SDL_GL_DOUBLEBUFFER:
slouken@291
   703
             *value=this->gl_config.double_buffer;
slouken@291
   704
             break;
slouken@291
   705
        case SDL_GL_STENCIL_SIZE:
slouken@291
   706
             *value=this->gl_config.stencil_size;
slouken@291
   707
             break;
slouken@291
   708
        case SDL_GL_DEPTH_SIZE:
slouken@291
   709
             *value=this->gl_config.depth_size;
slouken@291
   710
             break;
slouken@291
   711
        default:
slouken@291
   712
             *value=0;
slouken@291
   713
             return(-1);
slouken@291
   714
    }
slouken@291
   715
    return 0;
slouken@291
   716
}
slouken@0
   717
slouken@291
   718
#endif /* HAVE_OPENGL */