src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 28 Mar 2002 16:20:10 +0000
changeset 320 66f815c147ed
parent 315 3333b6e68289
child 370 ba72f259bc88
permissions -rw-r--r--
Date: Thu, 28 Mar 2002 09:20:03 +0200
From: "Mike Gorchak" <mike@malva.ua>
Subject: New QNX patch.

Hi !

1. Removed warning (possible bug) with invalid type, passing to the function
in ph_WarpedMotion.
2. Rewritten handler of Ph_WM_RESIZE message, now works, but buggy (old
handler doesn't work at all).
3. Added stub handler for Ph_WM_MAX (maximize) message.
4. Added more #ifdef HAVE_OPENGL to disable OpenGL stuff when it not needed.
5. Added support for SDL_NOFRAME and SDL_RESIZABLE flags (in OpenGL windows
too).
6. Added cosmetic changes, if no SDL_RESIZABLE flag defined, disable resize
handlers in window border and maximize button at caption.
7. Fixed my bug with invalid arguments count passed to PtCreateWidget call.
8. Fixed some palette problems.
9. Updated README.QNX file.

And I changed testgl.c test application:

10. Added in testgl.c application support for SDL_NOFRAME flag and
option -noframe.
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@309
   258
slouken@309
   259
        PtSetArg(&arg[pargc++], Pt_ARG_DIM, &dim, 0);
slouken@309
   260
        PtSetArg(&arg[pargc++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@309
   261
slouken@320
   262
        /* enable window minimizing */
slouken@320
   263
        PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_HIDE);
slouken@320
   264
slouken@320
   265
        /* remove border and caption if no frame flag selected */
slouken@320
   266
        if ((flags & SDL_NOFRAME) == SDL_NOFRAME)
slouken@320
   267
        {
slouken@320
   268
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Ph_WM_RENDER_TITLE | Ph_WM_RENDER_BORDER);
slouken@320
   269
        }
slouken@320
   270
        else
slouken@320
   271
        {
slouken@320
   272
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_TITLE | Ph_WM_RENDER_BORDER);
slouken@320
   273
        }
slouken@320
   274
slouken@320
   275
        /* if window is not resizable then remove resize handles and maximize button */
slouken@320
   276
        if ((flags & SDL_RESIZABLE) != SDL_RESIZABLE)
slouken@320
   277
        {
slouken@320
   278
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_MAX);
slouken@320
   279
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   280
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   281
        }
slouken@320
   282
        else
slouken@320
   283
        {
slouken@320
   284
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_MAX);
slouken@320
   285
            /* it is need to be Pt_FALSE to allow the application to process the resize callback */
slouken@320
   286
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   287
            PtSetArg(&arg[pargc++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_RESIZE | Ph_WM_MAX);
slouken@320
   288
        }
slouken@320
   289
slouken@309
   290
        if (window!=NULL)
slouken@0
   291
        {
slouken@309
   292
            PtUnrealizeWidget(window);
slouken@309
   293
            PtDestroyWidget(window);
slouken@309
   294
            window=NULL;
slouken@309
   295
        }
slouken@309
   296
slouken@320
   297
        window=PtCreateWidget(PtWindow, NULL, pargc, arg);
slouken@309
   298
        PtRealizeWidget(window);
slouken@315
   299
        
slouken@309
   300
        PtFlush();
slouken@309
   301
    }
slouken@309
   302
slouken@309
   303
#ifdef HAVE_OPENGL
slouken@309
   304
    if (flags & SDL_OPENGL)
slouken@309
   305
    {
slouken@309
   306
        /* ph_SetupOpenGLContext creates also window as need */
slouken@309
   307
        if (ph_SetupOpenGLContext(this, width, height, bpp, flags)==0)
slouken@309
   308
        {
slouken@309
   309
            /* setup OGL update function ... ugly method */
slouken@309
   310
            ph_ResizeImage(this, current, flags); 
slouken@0
   311
        }
slouken@0
   312
        else
slouken@0
   313
        {
slouken@309
   314
            /* if context creation fail, report no OpenGL to high level */
slouken@309
   315
            current->flags=(flags & (~SDL_OPENGL));
slouken@309
   316
        }
slouken@309
   317
#else
slouken@309
   318
    if (flags & SDL_OPENGL) /* if no built-in OpenGL support */
slouken@309
   319
    {
slouken@309
   320
        fprintf(stderr, "error: no OpenGL support, try to recompile library.\n");
slouken@309
   321
        current->flags=(flags & (~SDL_OPENGL));
slouken@309
   322
        return NULL;
slouken@309
   323
#endif /* HAVE_OPENGL */
slouken@309
   324
    }
slouken@309
   325
    else
slouken@309
   326
    {
slouken@309
   327
        /* Initialize the window */
slouken@309
   328
        if (flags & SDL_FULLSCREEN) /* Direct Context , assume SDL_HWSURFACE also set */
slouken@309
   329
        {
slouken@309
   330
            /* Get the video mode and set it */
slouken@309
   331
            if (flags & SDL_ANYFORMAT)
slouken@0
   332
            {
slouken@309
   333
                if ((mode = get_mode_any_format(width, height, bpp)) == 0)
slouken@309
   334
                {
slouken@309
   335
                    fprintf(stderr,"error: get_mode_any_format failed\n");
slouken@309
   336
                    exit(1);
slouken@309
   337
                }
slouken@309
   338
            }
slouken@309
   339
            else
slouken@309
   340
            {
slouken@309
   341
                if ((mode = get_mode(width, height, bpp)) == 0)
slouken@309
   342
                {
slouken@309
   343
                    fprintf(stderr,"error: get_mode failed\n");
slouken@309
   344
                    exit(1);
slouken@309
   345
                }
slouken@309
   346
            }
slouken@315
   347
            
slouken@315
   348
            if (bpp==8)
slouken@315
   349
            {
slouken@315
   350
               desktoppal=SDLPH_PAL_SYSTEM;
slouken@315
   351
            }
slouken@315
   352
            
slouken@315
   353
            /* save old video mode caps */
slouken@315
   354
            PgGetVideoMode(&settings);
slouken@315
   355
            old_video_mode=settings.mode;
slouken@315
   356
            old_refresh_rate=settings.refresh;
slouken@315
   357
slouken@315
   358
            /* setup new video mode */
slouken@309
   359
            settings.mode = mode;
slouken@309
   360
            settings.refresh = 0;
slouken@315
   361
            settings.flags = 0;
slouken@309
   362
slouken@315
   363
            if (PgSetVideoMode(&settings) < 0)
slouken@309
   364
            {
slouken@309
   365
                fprintf(stderr,"error: PgSetVideoMode failed\n");
slouken@309
   366
            }
slouken@309
   367
slouken@309
   368
            current->flags = (flags & (~SDL_RESIZABLE)); /* no resize for Direct Context */
slouken@309
   369
slouken@309
   370
            /* Begin direct mode */
slouken@309
   371
            ph_EnterFullScreen(this);
slouken@309
   372
slouken@309
   373
        } /* end fullscreen flag */
slouken@309
   374
        else
slouken@309
   375
        {
slouken@320
   376
            /* Use offscreen memory iff SDL_HWSURFACE flag is set */
slouken@320
   377
            if (flags & SDL_HWSURFACE)
slouken@309
   378
            {
slouken@320
   379
                /* no stretch blit in offscreen context */
slouken@320
   380
                current->flags = (flags & (~SDL_RESIZABLE));
slouken@309
   381
            }
slouken@320
   382
slouken@315
   383
            /* using palette emulation code in window mode */
slouken@315
   384
            if (bpp==8)
slouken@315
   385
            {
slouken@315
   386
                if (desktopbpp>=15)
slouken@315
   387
                {
slouken@315
   388
                    desktoppal=SDLPH_PAL_EMULATE;
slouken@315
   389
                }
slouken@315
   390
                else
slouken@315
   391
                {
slouken@315
   392
                    desktoppal=SDLPH_PAL_SYSTEM;
slouken@315
   393
                }
slouken@315
   394
            }
slouken@315
   395
            else
slouken@315
   396
            {
slouken@315
   397
               desktoppal=SDLPH_PAL_NONE;
slouken@315
   398
            }
slouken@0
   399
        }
slouken@266
   400
slouken@309
   401
	/* If we are setting video to use the palette make sure we have allocated memory for it */
slouken@309
   402
	if (bpp==8)
slouken@309
   403
	{
slouken@309
   404
            current->format->palette = malloc(sizeof(SDL_Palette));
slouken@309
   405
            memset(current->format->palette, 0, sizeof(SDL_Palette));
slouken@309
   406
            current->format->palette->ncolors = 256;
slouken@309
   407
            current->format->palette->colors = (SDL_Color *)malloc(256 *sizeof(SDL_Color));
slouken@309
   408
            /* fill the palette */
slouken@309
   409
            rtnval = PgGetPalette(ph_palette);
slouken@309
   410
slouken@309
   411
            tempptr = (unsigned long *)current->format->palette->colors;
slouken@309
   412
slouken@309
   413
            for(i=0; i<256; i++)
slouken@309
   414
            {
slouken@309
   415
                *tempptr = (((unsigned long)ph_palette[i]) << 8);
slouken@309
   416
                tempptr++;
slouken@309
   417
            }
slouken@0
   418
        }
slouken@0
   419
slouken@0
   420
    }
slouken@0
   421
slouken@309
   422
    current->w = width;
slouken@309
   423
    current->h = height;
slouken@309
   424
    current->format->BitsPerPixel = bpp;
slouken@309
   425
    current->format->BytesPerPixel = (bpp+7)/8;
slouken@309
   426
    current->pitch = SDL_CalculatePitch(current);
slouken@309
   427
    /* Must call at least once it setup image planes */
slouken@309
   428
    ph_ResizeImage(this, current, flags);
slouken@0
   429
slouken@315
   430
    /* delayed set caption call */
slouken@315
   431
    if (captionflag)
slouken@315
   432
    {
slouken@315
   433
        ph_SetCaption(this, this->wm_title, NULL);
slouken@315
   434
    }
slouken@315
   435
slouken@320
   436
    /* finish window drawing */
slouken@320
   437
    PtFlush();
slouken@320
   438
slouken@0
   439
    SDL_Unlock_EventThread();
slouken@0
   440
slouken@0
   441
    /* We're done! */
slouken@315
   442
    return (current);
slouken@0
   443
}
slouken@0
   444
slouken@0
   445
static void ph_VideoQuit(_THIS)
slouken@0
   446
{
slouken@320
   447
#ifdef HAVE_OPENGL
slouken@309
   448
    PhRegion_t region_info;
slouken@320
   449
#endif /* HAVE_OPENGL */
slouken@309
   450
slouken@283
   451
    ph_DestroyImage(this, SDL_VideoSurface); 
slouken@0
   452
slouken@279
   453
    if (currently_fullscreen)
slouken@279
   454
    {
slouken@315
   455
        ph_LeaveFullScreen(this);
slouken@279
   456
    }
slouken@309
   457
slouken@309
   458
#ifdef HAVE_OPENGL
slouken@320
   459
    /* prevent double SEGFAULT during parachute mode */
slouken@315
   460
    if (this->screen)
slouken@309
   461
    {
slouken@315
   462
        if (((this->screen->flags & SDL_FULLSCREEN)==SDL_FULLSCREEN) &&
slouken@315
   463
            ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
slouken@315
   464
        {
slouken@315
   465
            region_info.cursor_type=Ph_CURSOR_POINTER;
slouken@315
   466
            region_info.rid=PtWidgetRid(window);
slouken@315
   467
            PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@315
   468
        }
slouken@309
   469
    }
slouken@309
   470
slouken@309
   471
    PtFlush();
slouken@309
   472
#endif /* HAVE_OPENGL */
slouken@309
   473
    
slouken@309
   474
    if (window)
slouken@309
   475
    {
slouken@309
   476
        PtUnrealizeWidget(window);
slouken@309
   477
        PtDestroyWidget(window);
slouken@309
   478
        window=NULL;
slouken@309
   479
    }
slouken@315
   480
    
slouken@315
   481
    /* restore palette */
slouken@315
   482
    if (desktoppal!=SDLPH_PAL_NONE)
slouken@315
   483
    {
slouken@315
   484
        PgSetPalette(ph_palette, 0, 0, _Pg_MAX_PALETTE, Pg_PALSET_GLOBAL | Pg_PALSET_FORCE_EXPOSE, 0);
slouken@315
   485
    }
slouken@309
   486
slouken@309
   487
#ifdef HAVE_OPENGL
slouken@309
   488
    if (oglctx)
slouken@309
   489
    {
slouken@309
   490
        PhDCSetCurrent(NULL);
slouken@309
   491
        PhDCRelease(oglctx);
slouken@309
   492
        oglctx=NULL;
slouken@309
   493
    }
slouken@309
   494
#endif /* HAVE_OPENGL */
slouken@0
   495
}
slouken@0
   496
slouken@0
   497
static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   498
{
slouken@315
   499
    int i;
slouken@315
   500
    PhPoint_t point={0, 0};
slouken@315
   501
    PgColor_t syspalph[_Pg_MAX_PALETTE];
slouken@0
   502
slouken@315
   503
    /* palette emulation code, using palette of the PhImage_t struct */
slouken@315
   504
    if (desktoppal==SDLPH_PAL_EMULATE)
slouken@315
   505
    {
slouken@315
   506
        if ((SDL_Image) && (SDL_Image->palette))
slouken@315
   507
        {
slouken@315
   508
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   509
            {
slouken@315
   510
                SDL_Image->palette[i]  = 0x00000000UL;
slouken@315
   511
                SDL_Image->palette[i] |= colors[i-firstcolor].r<<16;
slouken@315
   512
                SDL_Image->palette[i] |= colors[i-firstcolor].g<<8;
slouken@315
   513
                SDL_Image->palette[i] |= colors[i-firstcolor].b;
slouken@315
   514
            }
slouken@315
   515
        }
slouken@315
   516
        /* image needs to be redrawed, very slow method */
slouken@315
   517
        PgDrawPhImage(&point, SDL_Image, 0);
slouken@315
   518
    }
slouken@315
   519
    else
slouken@315
   520
    {
slouken@315
   521
        if (desktoppal==SDLPH_PAL_SYSTEM)
slouken@315
   522
        {
slouken@315
   523
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   524
            {
slouken@315
   525
                syspalph[i]  = 0x00000000UL;
slouken@315
   526
                syspalph[i] |= colors[i-firstcolor].r<<16;
slouken@315
   527
                syspalph[i] |= colors[i-firstcolor].g<<8;
slouken@315
   528
                syspalph[i] |= colors[i-firstcolor].b;
slouken@315
   529
            }
slouken@0
   530
slouken@315
   531
            if ((this->screen->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
slouken@315
   532
            {
slouken@315
   533
                /* window mode must use soft palette */
slouken@320
   534
                PgSetPalette((PgColor_t*)&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_SOFT, 0);
slouken@315
   535
                /* image needs to be redrawed, very slow method */
slouken@315
   536
                PgDrawPhImage(&point, SDL_Image, 0);
slouken@315
   537
            }
slouken@315
   538
            else
slouken@315
   539
            {
slouken@315
   540
                /* fullscreen mode must use hardware palette */
slouken@320
   541
                PgSetPalette((PgColor_t*)&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
slouken@315
   542
            }
slouken@315
   543
        }
slouken@315
   544
        else
slouken@315
   545
        {
slouken@315
   546
            /* SDLPH_PAL_NONE do nothing */
slouken@315
   547
        }
slouken@0
   548
    }
slouken@315
   549
    
slouken@315
   550
    return 1;
slouken@0
   551
}
slouken@0
   552
slouken@279
   553
#ifdef HAVE_OPENGL
slouken@309
   554
slouken@309
   555
int ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags)
slouken@309
   556
{
slouken@309
   557
    PtArg_t args[8];
slouken@309
   558
    PhDim_t dim;
slouken@309
   559
    uint64_t OGLAttrib[PH_OGL_MAX_ATTRIBS];
slouken@309
   560
    int OGLargc;
slouken@309
   561
    int pargc;
slouken@309
   562
slouken@309
   563
    dim.w=width;
slouken@309
   564
    dim.h=height;
slouken@309
   565
    
slouken@309
   566
    if (oglctx!=NULL)
slouken@309
   567
    {
slouken@309
   568
       PhDCSetCurrent(NULL);
slouken@309
   569
       PhDCRelease(oglctx);
slouken@309
   570
       oglctx=NULL;
slouken@309
   571
    }
slouken@309
   572
slouken@309
   573
    OGLargc=0;
slouken@309
   574
    if (this->gl_config.depth_size)
slouken@309
   575
    {
slouken@309
   576
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DEPTH_BITS;
slouken@309
   577
        OGLAttrib[OGLargc++]=this->gl_config.depth_size;
slouken@309
   578
    }
slouken@309
   579
    if (this->gl_config.stencil_size)
slouken@309
   580
    {
slouken@309
   581
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_STENCIL_BITS;
slouken@309
   582
        OGLAttrib[OGLargc++]=this->gl_config.stencil_size;
slouken@309
   583
    }
slouken@309
   584
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FORCE_SW;
slouken@309
   585
    if (flags & SDL_FULLSCREEN)
slouken@309
   586
    {
slouken@309
   587
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN;
slouken@309
   588
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DIRECT;
slouken@309
   589
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_BEST;
slouken@309
   590
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_CENTER;
slouken@309
   591
    }
slouken@309
   592
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_NONE;
slouken@309
   593
slouken@309
   594
    if (this->gl_config.double_buffer)
slouken@309
   595
    {
slouken@309
   596
        oglctx=PdCreateOpenGLContext(2, &dim, 0, OGLAttrib);
slouken@309
   597
    }
slouken@309
   598
    else
slouken@309
   599
    {
slouken@309
   600
        oglctx=PdCreateOpenGLContext(1, &dim, 0, OGLAttrib);
slouken@309
   601
    }
slouken@320
   602
slouken@309
   603
    if (oglctx==NULL)
slouken@309
   604
    {
slouken@320
   605
        fprintf(stderr,"ph_SetupOpenGLContext: cannot create OpenGL context.\n");
slouken@309
   606
        return (-1);
slouken@309
   607
    }
slouken@309
   608
slouken@309
   609
    PhDCSetCurrent(oglctx);
slouken@309
   610
slouken@309
   611
    pargc=0;
slouken@309
   612
slouken@309
   613
    PtSetArg(&args[pargc++], Pt_ARG_DIM, &dim, 0);
slouken@309
   614
    PtSetArg(&args[pargc++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@309
   615
    PtSetArg(&args[pargc++], Pt_ARG_FILL_COLOR, Pg_BLACK, 0);
slouken@309
   616
slouken@309
   617
    if (flags & SDL_FULLSCREEN)
slouken@309
   618
    {
slouken@309
   619
        PhPoint_t pos;
slouken@309
   620
slouken@309
   621
        pos.x=0;
slouken@309
   622
        pos.y=0;
slouken@309
   623
slouken@309
   624
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, ~0);
slouken@320
   625
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_FFRONT | Ph_WM_CLOSE | Ph_WM_TOFRONT | Ph_WM_CONSWITCH);
slouken@309
   626
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISFOCUS);
slouken@309
   627
        PtSetArg(&args[pargc++], Pt_ARG_POS, &pos, 0);
slouken@309
   628
    }
slouken@320
   629
    else
slouken@320
   630
    {
slouken@320
   631
        /* remove border and caption if no frame flag selected */
slouken@320
   632
        if ((flags & SDL_NOFRAME) == SDL_NOFRAME)
slouken@320
   633
        {
slouken@320
   634
            PtSetArg(&args[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, 0, Ph_WM_RENDER_TITLE | Ph_WM_RENDER_BORDER);
slouken@320
   635
        }
slouken@320
   636
        else
slouken@320
   637
        {
slouken@320
   638
           /* if window is not resizable then remove resize handles */
slouken@320
   639
           if ((flags & SDL_RESIZABLE) != SDL_RESIZABLE)
slouken@320
   640
           {
slouken@320
   641
               PtSetArg(&args[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, 0, Ph_WM_RENDER_RESIZE);
slouken@320
   642
           }
slouken@320
   643
        }
slouken@320
   644
    }
slouken@309
   645
slouken@309
   646
    if (window!=NULL)
slouken@309
   647
    {
slouken@309
   648
        PtUnrealizeWidget(window);
slouken@309
   649
        PtDestroyWidget(window);
slouken@309
   650
        window=NULL;
slouken@309
   651
    }
slouken@309
   652
slouken@320
   653
    window=PtCreateWidget(PtWindow, NULL, pargc, args);
slouken@309
   654
    PtRealizeWidget(window);
slouken@309
   655
slouken@309
   656
    /* disable mouse for fullscreen */
slouken@309
   657
    if (flags & SDL_FULLSCREEN)
slouken@309
   658
    {
slouken@309
   659
        PhRegion_t region_info;
slouken@309
   660
slouken@309
   661
        region_info.cursor_type=Ph_CURSOR_NONE;
slouken@309
   662
        region_info.rid=PtWidgetRid(window);
slouken@309
   663
        PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@309
   664
    }
slouken@309
   665
slouken@309
   666
    PtFlush();
slouken@309
   667
slouken@309
   668
    return 0;
slouken@309
   669
}
slouken@309
   670
slouken@266
   671
void ph_GL_SwapBuffers(_THIS)
slouken@266
   672
{
slouken@291
   673
    PgSetRegion(PtWidgetRid(window));
slouken@309
   674
    PdOpenGLContextSwapBuffers(oglctx);
slouken@266
   675
}
slouken@266
   676
slouken@291
   677
int ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   678
{
slouken@291
   679
    switch (attrib)
slouken@291
   680
    {
slouken@291
   681
        case SDL_GL_DOUBLEBUFFER:
slouken@291
   682
             *value=this->gl_config.double_buffer;
slouken@291
   683
             break;
slouken@291
   684
        case SDL_GL_STENCIL_SIZE:
slouken@291
   685
             *value=this->gl_config.stencil_size;
slouken@291
   686
             break;
slouken@291
   687
        case SDL_GL_DEPTH_SIZE:
slouken@291
   688
             *value=this->gl_config.depth_size;
slouken@291
   689
             break;
slouken@291
   690
        default:
slouken@291
   691
             *value=0;
slouken@291
   692
             return(-1);
slouken@291
   693
    }
slouken@291
   694
    return 0;
slouken@291
   695
}
slouken@0
   696
slouken@291
   697
#endif /* HAVE_OPENGL */