src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 28 May 2002 19:31:32 +0000
changeset 380 bce7171e7a85
parent 370 ba72f259bc88
child 571 8e3ce997621c
permissions -rw-r--r--
Date: Wed, 22 May 2002 22:30:58 +0300
From: "Mike Gorchak" <mike@malva.com.ua>
Subject: One more QNX patch

Hi !

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