src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 23 Aug 2003 23:20:21 +0000
changeset 692 04dd6c6d7c30
parent 663 8bedd6d61642
child 701 aaf3b8af6616
permissions -rw-r--r--
Date: Fri, 15 Aug 2003 09:13:59 +0300
From: "Mike Gorchak"
Subject: Patches for tests and QNX6

Here more fixes for the QNX6 in sdlqnx.diff file:

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