src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 04 Aug 2003 00:52:42 +0000
changeset 663 8bedd6d61642
parent 571 8e3ce997621c
child 692 04dd6c6d7c30
permissions -rw-r--r--
Date: Sat, 2 Aug 2003 16:22:51 +0300
From: "Mike Gorchak"
Subject: New patches for QNX6

Here my patches for the SDL/QNX:

QNXSDL.diff - diff to non-QNX related sources:

- updated BUGS file, I think QNX6 is now will be officially supported
- configure.in - added shared library support for QNX, and removed dependency between the ALSA and QNX6.
- SDL_audio.c - added QNX NTO sound bootstrap insted of ALSA's.
- SDL_sysaudio.h - the same.
- SDL_nto_audio.c - the same.
- SDL_video.c - right now, QNX doesn't offer any method to obtain pointers to the OpenGL functions by function name, so they must be hardcoded in library, otherwise OpenGL will not be supported.
- testsprite.c - fixed: do not draw vertical red line if we are in non-double-buffered mode.

sdlqnxph.tar.gz - archive of the ./src/video/photon/* . Too many changes in code to make diffs :) :

+ Added stub for support hide/unhide window event
+ Added full YUV overlays support.
+ Added window maximize support.
+ Added mouse wheel events.
+ Added support for some specific key codes in Unicode mode (like ESC).
+ Added more checks to the all memory allocation code.
+ Added SDL_DOUBLEBUF support in all fullscreen modes.
+ Added fallback to window mode, if desired fullscreen mode is not supported.
+ Added stub support for the GL_LoadLibrary and GL_GetProcAddress functions.
+ Added resizable window support without caption.
! Fixed bug in the Ph_EV_EXPOSE event handler, when rectangles to update is 0 and when width or height of the rectangle is 0.
! Fixed bug in the event handler code. Events has not been passed to the window widget handler.
! Fixed codes for Win keys (Super/Hyper/Menu).
! Fixed memory leak, when deallocation palette.
! Fixed palette emulation code bugs.
! Fixed fullscreen and hwsurface handling.
! Fixed CLOSE button bug. First event was passed to the handler, but second terminated the application. Now all events passed to the application correctly.
- Removed all printfs in code, now SDL_SetError used instead of them.
- Disabled ToggleFullScreen function.

README.QNX - updated README.QNX file. Added much more issues.
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@571
   197
slouken@571
   198
    PtSetArg(&args[nargs++], Pt_ARG_DIM, &dim, 0);
slouken@571
   199
    PtSetArg(&args[nargs++], Pt_ARG_FILL_COLOR, Pg_BLACK, 0);
slouken@571
   200
slouken@571
   201
    if ((flags & SDL_RESIZABLE) == SDL_RESIZABLE)
slouken@571
   202
    {
slouken@663
   203
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_CLOSE);
slouken@663
   204
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_MAX | Ph_WM_RESTORE);
slouken@663
   205
        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
   206
        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
   207
    }
slouken@571
   208
    else
slouken@571
   209
    {
slouken@663
   210
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX | Ph_WM_RESTORE | Ph_WM_CLOSE);
slouken@663
   211
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX | Ph_WM_RESTORE);
slouken@663
   212
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_MOVE | Ph_WM_CLOSE);
slouken@663
   213
        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
   214
    }
slouken@571
   215
slouken@571
   216
    if (((flags & SDL_NOFRAME)==SDL_NOFRAME) || ((flags & SDL_FULLSCREEN)==SDL_FULLSCREEN))
slouken@571
   217
    {
slouken@663
   218
       if ((flags & SDL_RESIZABLE) != SDL_RESIZABLE)
slouken@663
   219
       {
slouken@663
   220
           PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Pt_TRUE);
slouken@663
   221
       }
slouken@663
   222
       else
slouken@663
   223
       {
slouken@663
   224
           PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Pt_TRUE);
slouken@663
   225
           PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_BORDER);
slouken@663
   226
       }
slouken@571
   227
    }
slouken@571
   228
    else
slouken@571
   229
    {
slouken@571
   230
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_BORDER | Ph_WM_RENDER_TITLE |
slouken@663
   231
                                 Ph_WM_RENDER_CLOSE | Ph_WM_RENDER_MENU | Ph_WM_RENDER_MIN);
slouken@571
   232
    }
slouken@571
   233
slouken@663
   234
    if ((flags & SDL_FULLSCREEN) == SDL_FULLSCREEN)
slouken@571
   235
    {
slouken@571
   236
        PtSetArg(&args[nargs++], Pt_ARG_POS, &pos, 0);
slouken@571
   237
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_FFRONT | Ph_WM_MAX);
slouken@571
   238
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISMAX |
slouken@571
   239
                                                               Ph_WM_STATE_ISFOCUS | Ph_WM_STATE_ISALTKEY);
slouken@571
   240
    }
slouken@571
   241
    else
slouken@571
   242
    {
slouken@571
   243
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_FALSE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISMAX | Ph_WM_STATE_ISALTKEY);
slouken@571
   244
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_HIDE);
slouken@663
   245
        PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_HIDE);
slouken@571
   246
        PtSetArg(&args[nargs++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@571
   247
    }
slouken@571
   248
slouken@571
   249
    PtSetResources(window, nargs, args);
slouken@571
   250
    PtRealizeWidget(window);
slouken@571
   251
slouken@571
   252
    return 0;
slouken@571
   253
}
slouken@571
   254
slouken@663
   255
static const struct ColourMasks* ph_GetColourMasks(int bpp)
slouken@663
   256
{
slouken@663
   257
    /* The alpha mask doesn't appears to be needed */
slouken@663
   258
    static const struct ColourMasks phColorMasks[5] = {
slouken@663
   259
        /*  8 bit      */  {0, 0, 0, 0, 8},
slouken@663
   260
        /* 15 bit ARGB */  {0x7C00, 0x03E0, 0x001F, 0x8000, 15},
slouken@663
   261
        /* 16 bit  RGB */  {0xF800, 0x07E0, 0x001F, 0x0000, 16},
slouken@663
   262
        /* 24 bit  RGB */  {0xFF0000, 0x00FF00, 0x0000FF, 0x000000, 24},
slouken@663
   263
        /* 32 bit ARGB */  {0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000, 32},
slouken@663
   264
    };
slouken@663
   265
slouken@663
   266
    switch (bpp)
slouken@663
   267
    {
slouken@663
   268
        case 8:
slouken@663
   269
             return &phColorMasks[0];
slouken@663
   270
        case 15:
slouken@663
   271
             return &phColorMasks[1];
slouken@663
   272
        case 16:
slouken@663
   273
             return &phColorMasks[2];
slouken@663
   274
        case 24:
slouken@663
   275
             return &phColorMasks[3];
slouken@663
   276
        case 32:
slouken@663
   277
             return &phColorMasks[4];
slouken@663
   278
    }
slouken@663
   279
    return NULL;
slouken@663
   280
}
slouken@663
   281
slouken@0
   282
static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   283
{
slouken@309
   284
    PgVideoModeInfo_t my_mode_info;
slouken@309
   285
    PgHWCaps_t my_hwcaps;
slouken@663
   286
    int i;
slouken@309
   287
slouken@309
   288
    window=NULL;
slouken@320
   289
    desktoppal=SDLPH_PAL_NONE;
slouken@320
   290
#ifdef HAVE_OPENGL
slouken@309
   291
    oglctx=NULL;
slouken@320
   292
#endif /* HAVE_OPENGL */
slouken@315
   293
    
slouken@315
   294
    old_video_mode=-1;
slouken@315
   295
    old_refresh_rate=-1;
slouken@0
   296
	
slouken@309
   297
    if (NULL == (event = malloc(EVENT_SIZE)))
slouken@309
   298
    {
slouken@571
   299
        SDL_OutOfMemory();
slouken@571
   300
        return -1;
slouken@309
   301
    }
slouken@380
   302
    memset(event, 0x00, EVENT_SIZE);
slouken@0
   303
slouken@571
   304
    window = ph_CreateWindow(this);
slouken@571
   305
    if (window == NULL)
slouken@571
   306
    {
slouken@663
   307
        SDL_SetError("ph_VideoInit(): Couldn't create video window !\n");
slouken@571
   308
        return -1;
slouken@571
   309
    }
slouken@571
   310
slouken@309
   311
    /* Create the blank cursor */
slouken@309
   312
    SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
slouken@309
   313
                                          (int)BLANK_CWIDTH, (int)BLANK_CHEIGHT,
slouken@309
   314
                                          (int)BLANK_CHOTX, (int)BLANK_CHOTY);
slouken@0
   315
slouken@309
   316
    if (SDL_BlankCursor == NULL)
slouken@309
   317
    {
slouken@663
   318
        return -1;
slouken@309
   319
    }
slouken@0
   320
slouken@309
   321
    if (PgGetGraphicsHWCaps(&my_hwcaps) < 0)
slouken@309
   322
    {
slouken@663
   323
        SDL_SetError("ph_VideoInit(): GetGraphicsHWCaps function failed !\n");
slouken@663
   324
        this->FreeWMCursor(this, SDL_BlankCursor);
slouken@663
   325
        return -1;
slouken@309
   326
    }
slouken@0
   327
slouken@309
   328
    if (PgGetVideoModeInfo(my_hwcaps.current_video_mode, &my_mode_info) < 0)
slouken@309
   329
    {
slouken@663
   330
        SDL_SetError("ph_VideoInit(): PgGetVideoModeInfo function failed !\n");
slouken@663
   331
        this->FreeWMCursor(this, SDL_BlankCursor);
slouken@663
   332
        return -1;
slouken@309
   333
    }
slouken@0
   334
slouken@309
   335
    /* We need to return BytesPerPixel as it in used by CreateRGBsurface */
slouken@309
   336
    vformat->BitsPerPixel = my_mode_info.bits_per_pixel;
slouken@309
   337
    vformat->BytesPerPixel = my_mode_info.bytes_per_scanline/my_mode_info.width;
slouken@315
   338
    desktopbpp = my_mode_info.bits_per_pixel;
slouken@320
   339
    
slouken@320
   340
    /* save current palette */
slouken@320
   341
    if (desktopbpp==8)
slouken@320
   342
    {
slouken@571
   343
        PgGetPalette(savedpal);
slouken@571
   344
        PgGetPalette(syspalph);
slouken@320
   345
    }
slouken@663
   346
    else
slouken@663
   347
    {
slouken@663
   348
        for(i=0; i<_Pg_MAX_PALETTE; i++)
slouken@663
   349
        {
slouken@663
   350
            savedpal[i]=PgRGB(0, 0, 0);
slouken@663
   351
            syspalph[i]=PgRGB(0, 0, 0);
slouken@663
   352
        }
slouken@663
   353
    }
slouken@309
   354
         
slouken@19
   355
    currently_fullscreen = 0;
slouken@663
   356
    currently_hided = 0;
slouken@663
   357
    current_overlay = NULL;
slouken@663
   358
slouken@663
   359
    OCImage.direct_context = NULL;
slouken@663
   360
    OCImage.offscreen_context = NULL;
slouken@663
   361
    OCImage.offscreen_backcontext = NULL;
slouken@663
   362
    OCImage.oldDC = NULL;
slouken@663
   363
    OCImage.CurrentFrameData = NULL;
slouken@663
   364
    OCImage.FrameData0 = NULL;
slouken@663
   365
    OCImage.FrameData1 = NULL;
slouken@663
   366
slouken@19
   367
    
slouken@19
   368
    this->info.wm_available = 1;
slouken@19
   369
    
slouken@0
   370
    return 0;
slouken@0
   371
}
slouken@0
   372
slouken@0
   373
static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
   374
                int width, int height, int bpp, Uint32 flags)
slouken@0
   375
{
slouken@663
   376
    const struct ColourMasks* mask;
slouken@0
   377
slouken@0
   378
    /* Lock the event thread, in multi-threading environments */
slouken@0
   379
    SDL_Lock_EventThread();
slouken@0
   380
slouken@320
   381
    current->flags = flags;
slouken@320
   382
slouken@663
   383
    /* if we do not have desired fullscreen mode, then fallback into window mode */
slouken@663
   384
    if (((current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) && (ph_GetVideoMode(width, height, bpp)==0))
slouken@663
   385
    {
slouken@663
   386
       current->flags &= ~SDL_FULLSCREEN;
slouken@663
   387
       current->flags &= ~SDL_NOFRAME;
slouken@663
   388
       current->flags &= ~SDL_RESIZABLE;
slouken@663
   389
    }
slouken@663
   390
slouken@663
   391
    ph_SetupWindow(this, width, height, current->flags);
slouken@663
   392
slouken@663
   393
    mask = ph_GetColourMasks(bpp);
slouken@663
   394
    if (mask != NULL)
slouken@663
   395
    {
slouken@663
   396
        SDL_ReallocFormat(current, mask->bpp, mask->red, mask->green, mask->blue, 0);
slouken@663
   397
    }
slouken@663
   398
    else
slouken@663
   399
    {
slouken@663
   400
        SDL_SetError("ph_SetVideoMode(): desired bpp is not supported by photon !\n");
slouken@663
   401
        return NULL;
slouken@663
   402
    }
slouken@309
   403
slouken@309
   404
#ifdef HAVE_OPENGL
slouken@571
   405
    if (current->flags & SDL_OPENGL)
slouken@309
   406
    {
slouken@309
   407
        /* ph_SetupOpenGLContext creates also window as need */
slouken@309
   408
        if (ph_SetupOpenGLContext(this, width, height, bpp, flags)==0)
slouken@309
   409
        {
slouken@571
   410
            ph_SetupUpdateFunction(this, current, flags); 
slouken@0
   411
        }
slouken@0
   412
        else
slouken@0
   413
        {
slouken@309
   414
            /* if context creation fail, report no OpenGL to high level */
slouken@571
   415
            current->flags &= ~SDL_OPENGL;
slouken@663
   416
            return NULL;
slouken@309
   417
        }
slouken@309
   418
#else
slouken@571
   419
    if (current->flags & SDL_OPENGL) /* if no built-in OpenGL support */
slouken@309
   420
    {
slouken@663
   421
        SDL_SetError("ph_SetVideoMode(): no OpenGL support, try to recompile library.\n");
slouken@571
   422
        current->flags &= ~SDL_OPENGL;
slouken@309
   423
        return NULL;
slouken@309
   424
#endif /* HAVE_OPENGL */
slouken@309
   425
    }
slouken@309
   426
    else
slouken@309
   427
    {
slouken@663
   428
        /* Initialize internal variables */
slouken@663
   429
        if ((current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN)
slouken@309
   430
        {
slouken@315
   431
            if (bpp==8)
slouken@315
   432
            {
slouken@315
   433
               desktoppal=SDLPH_PAL_SYSTEM;
slouken@315
   434
            }
slouken@309
   435
slouken@571
   436
            current->flags &= ~SDL_RESIZABLE; /* no resize for Direct Context */
slouken@663
   437
        }
slouken@309
   438
        else
slouken@309
   439
        {
slouken@663
   440
            /* remove this if we'll support non-fullscreen sw/hw+doublebuf */
slouken@663
   441
            current->flags &= ~SDL_DOUBLEBUF;
slouken@663
   442
slouken@663
   443
            /* Use offscreen memory if SDL_HWSURFACE flag is set */
slouken@663
   444
            if ((current->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
slouken@309
   445
            {
slouken@663
   446
slouken@663
   447
                if (desktopbpp!=bpp)
slouken@663
   448
                {
slouken@663
   449
                   current->flags &= ~SDL_HWSURFACE;
slouken@663
   450
                }
slouken@309
   451
            }
slouken@320
   452
slouken@315
   453
            /* using palette emulation code in window mode */
slouken@315
   454
            if (bpp==8)
slouken@315
   455
            {
slouken@315
   456
                if (desktopbpp>=15)
slouken@315
   457
                {
slouken@663
   458
                    desktoppal = SDLPH_PAL_EMULATE;
slouken@315
   459
                }
slouken@315
   460
                else
slouken@315
   461
                {
slouken@663
   462
                    desktoppal = SDLPH_PAL_SYSTEM;
slouken@571
   463
                }
slouken@315
   464
            }
slouken@315
   465
            else
slouken@315
   466
            {
slouken@663
   467
               desktoppal = SDLPH_PAL_NONE;
slouken@315
   468
            }
slouken@0
   469
        }
slouken@0
   470
    }
slouken@0
   471
slouken@309
   472
    current->w = width;
slouken@309
   473
    current->h = height;
slouken@571
   474
slouken@663
   475
    if (desktoppal==SDLPH_PAL_SYSTEM)
slouken@663
   476
    {
slouken@663
   477
       current->flags|=SDL_HWPALETTE;
slouken@663
   478
    }
slouken@370
   479
slouken@663
   480
    /* Must call at least once for setup image planes */
slouken@663
   481
    if (ph_SetupUpdateFunction(this, current, current->flags)==-1)
slouken@370
   482
    {
slouken@370
   483
        return NULL;
slouken@370
   484
    }
slouken@0
   485
slouken@663
   486
    /* finish window drawing, if we are not in fullscreen, of course */
slouken@663
   487
    if ((current->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
slouken@663
   488
    {
slouken@663
   489
       PtFlush();
slouken@663
   490
    }
slouken@320
   491
slouken@0
   492
    SDL_Unlock_EventThread();
slouken@0
   493
slouken@0
   494
    /* We're done! */
slouken@315
   495
    return (current);
slouken@0
   496
}
slouken@0
   497
slouken@0
   498
static void ph_VideoQuit(_THIS)
slouken@0
   499
{
slouken@320
   500
#ifdef HAVE_OPENGL
slouken@309
   501
    PhRegion_t region_info;
slouken@320
   502
#endif /* HAVE_OPENGL */
slouken@309
   503
slouken@663
   504
    /* restore palette */
slouken@663
   505
    if (desktopbpp==8)
slouken@663
   506
    {
slouken@663
   507
        PgSetPalette(syspalph, 0, -1, 0, 0, 0);
slouken@663
   508
        PgSetPalette(savedpal, 0, 0, _Pg_MAX_PALETTE, Pg_PALSET_GLOBAL | Pg_PALSET_FORCE_EXPOSE, 0);
slouken@663
   509
        PgFlush();
slouken@663
   510
    }
slouken@663
   511
slouken@283
   512
    ph_DestroyImage(this, SDL_VideoSurface); 
slouken@0
   513
slouken@309
   514
#ifdef HAVE_OPENGL
slouken@320
   515
    /* prevent double SEGFAULT during parachute mode */
slouken@315
   516
    if (this->screen)
slouken@309
   517
    {
slouken@315
   518
        if (((this->screen->flags & SDL_FULLSCREEN)==SDL_FULLSCREEN) &&
slouken@315
   519
            ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
slouken@315
   520
        {
slouken@315
   521
            region_info.cursor_type=Ph_CURSOR_POINTER;
slouken@315
   522
            region_info.rid=PtWidgetRid(window);
slouken@315
   523
            PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@315
   524
        }
slouken@309
   525
    }
slouken@309
   526
slouken@309
   527
    PtFlush();
slouken@309
   528
#endif /* HAVE_OPENGL */
slouken@309
   529
    
slouken@309
   530
    if (window)
slouken@309
   531
    {
slouken@309
   532
        PtUnrealizeWidget(window);
slouken@309
   533
        PtDestroyWidget(window);
slouken@309
   534
        window=NULL;
slouken@309
   535
    }
slouken@309
   536
slouken@309
   537
#ifdef HAVE_OPENGL
slouken@309
   538
    if (oglctx)
slouken@309
   539
    {
slouken@309
   540
        PhDCSetCurrent(NULL);
slouken@309
   541
        PhDCRelease(oglctx);
slouken@309
   542
        oglctx=NULL;
slouken@309
   543
    }
slouken@309
   544
#endif /* HAVE_OPENGL */
slouken@571
   545
slouken@571
   546
    if (event!=NULL)
slouken@571
   547
    {
slouken@571
   548
        free(event);
slouken@571
   549
        event=NULL;
slouken@571
   550
    }
slouken@0
   551
}
slouken@0
   552
slouken@0
   553
static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   554
{
slouken@315
   555
    int i;
slouken@571
   556
    SDL_Rect updaterect;
slouken@571
   557
slouken@571
   558
    updaterect.x = updaterect.y = 0;
slouken@571
   559
    updaterect.w = this->screen->w;
slouken@571
   560
    updaterect.h = this->screen->h;
slouken@0
   561
slouken@315
   562
    /* palette emulation code, using palette of the PhImage_t struct */
slouken@315
   563
    if (desktoppal==SDLPH_PAL_EMULATE)
slouken@315
   564
    {
slouken@315
   565
        if ((SDL_Image) && (SDL_Image->palette))
slouken@315
   566
        {
slouken@315
   567
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   568
            {
slouken@571
   569
                syspalph[i] = PgRGB(colors[i-firstcolor].r, colors[i-firstcolor].g, colors[i-firstcolor].b);
slouken@571
   570
                SDL_Image->palette[i] = syspalph[i];
slouken@315
   571
            }
slouken@663
   572
slouken@571
   573
            /* image needs to be redrawn */
slouken@571
   574
            this->UpdateRects(this, 1, &updaterect);
slouken@315
   575
        }
slouken@315
   576
    }
slouken@315
   577
    else
slouken@315
   578
    {
slouken@315
   579
        if (desktoppal==SDLPH_PAL_SYSTEM)
slouken@315
   580
        {
slouken@315
   581
            for (i=firstcolor; i<firstcolor+ncolors; i++)
slouken@315
   582
            {
slouken@571
   583
                syspalph[i] = PgRGB(colors[i-firstcolor].r, colors[i-firstcolor].g, colors[i-firstcolor].b);
slouken@315
   584
            }
slouken@0
   585
slouken@315
   586
            if ((this->screen->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
slouken@315
   587
            {
slouken@571
   588
                 /* window mode must use soft palette */
slouken@663
   589
                PgSetPalette(&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
slouken@571
   590
                /* image needs to be redrawn */
slouken@571
   591
                this->UpdateRects(this, 1, &updaterect);
slouken@315
   592
            }
slouken@315
   593
            else
slouken@315
   594
            {
slouken@315
   595
                /* fullscreen mode must use hardware palette */
slouken@663
   596
                PgSetPalette(&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
slouken@315
   597
            }
slouken@315
   598
        }
slouken@315
   599
        else
slouken@315
   600
        {
slouken@315
   601
            /* SDLPH_PAL_NONE do nothing */
slouken@315
   602
        }
slouken@0
   603
    }
slouken@315
   604
    
slouken@315
   605
    return 1;
slouken@0
   606
}
slouken@0
   607
slouken@279
   608
#ifdef HAVE_OPENGL
slouken@309
   609
slouken@309
   610
int ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags)
slouken@309
   611
{
slouken@309
   612
    PhDim_t dim;
slouken@309
   613
    uint64_t OGLAttrib[PH_OGL_MAX_ATTRIBS];
slouken@309
   614
    int OGLargc;
slouken@309
   615
slouken@309
   616
    dim.w=width;
slouken@309
   617
    dim.h=height;
slouken@309
   618
    
slouken@309
   619
    if (oglctx!=NULL)
slouken@309
   620
    {
slouken@309
   621
       PhDCSetCurrent(NULL);
slouken@309
   622
       PhDCRelease(oglctx);
slouken@309
   623
       oglctx=NULL;
slouken@309
   624
    }
slouken@309
   625
slouken@309
   626
    OGLargc=0;
slouken@309
   627
    if (this->gl_config.depth_size)
slouken@309
   628
    {
slouken@309
   629
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DEPTH_BITS;
slouken@309
   630
        OGLAttrib[OGLargc++]=this->gl_config.depth_size;
slouken@309
   631
    }
slouken@309
   632
    if (this->gl_config.stencil_size)
slouken@309
   633
    {
slouken@309
   634
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_STENCIL_BITS;
slouken@309
   635
        OGLAttrib[OGLargc++]=this->gl_config.stencil_size;
slouken@309
   636
    }
slouken@309
   637
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FORCE_SW;
slouken@309
   638
    if (flags & SDL_FULLSCREEN)
slouken@309
   639
    {
slouken@309
   640
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN;
slouken@309
   641
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DIRECT;
slouken@309
   642
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_BEST;
slouken@309
   643
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_CENTER;
slouken@309
   644
    }
slouken@309
   645
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_NONE;
slouken@309
   646
slouken@309
   647
    if (this->gl_config.double_buffer)
slouken@309
   648
    {
slouken@309
   649
        oglctx=PdCreateOpenGLContext(2, &dim, 0, OGLAttrib);
slouken@309
   650
    }
slouken@309
   651
    else
slouken@309
   652
    {
slouken@309
   653
        oglctx=PdCreateOpenGLContext(1, &dim, 0, OGLAttrib);
slouken@309
   654
    }
slouken@320
   655
slouken@309
   656
    if (oglctx==NULL)
slouken@309
   657
    {
slouken@663
   658
        SDL_SetError("ph_SetupOpenGLContext(): cannot create OpenGL context !\n");
slouken@309
   659
        return (-1);
slouken@309
   660
    }
slouken@309
   661
slouken@309
   662
    PhDCSetCurrent(oglctx);
slouken@309
   663
slouken@309
   664
    /* disable mouse for fullscreen */
slouken@309
   665
    if (flags & SDL_FULLSCREEN)
slouken@309
   666
    {
slouken@309
   667
        PhRegion_t region_info;
slouken@309
   668
slouken@309
   669
        region_info.cursor_type=Ph_CURSOR_NONE;
slouken@309
   670
        region_info.rid=PtWidgetRid(window);
slouken@309
   671
        PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@309
   672
    }
slouken@309
   673
slouken@309
   674
    PtFlush();
slouken@309
   675
slouken@309
   676
    return 0;
slouken@309
   677
}
slouken@309
   678
slouken@266
   679
void ph_GL_SwapBuffers(_THIS)
slouken@266
   680
{
slouken@291
   681
    PgSetRegion(PtWidgetRid(window));
slouken@309
   682
    PdOpenGLContextSwapBuffers(oglctx);
slouken@266
   683
}
slouken@266
   684
slouken@291
   685
int ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   686
{
slouken@291
   687
    switch (attrib)
slouken@291
   688
    {
slouken@291
   689
        case SDL_GL_DOUBLEBUFFER:
slouken@291
   690
             *value=this->gl_config.double_buffer;
slouken@291
   691
             break;
slouken@291
   692
        case SDL_GL_STENCIL_SIZE:
slouken@291
   693
             *value=this->gl_config.stencil_size;
slouken@291
   694
             break;
slouken@291
   695
        case SDL_GL_DEPTH_SIZE:
slouken@291
   696
             *value=this->gl_config.depth_size;
slouken@291
   697
             break;
slouken@291
   698
        default:
slouken@291
   699
             *value=0;
slouken@291
   700
             return(-1);
slouken@291
   701
    }
slouken@291
   702
    return 0;
slouken@291
   703
}
slouken@0
   704
slouken@663
   705
int ph_GL_LoadLibrary(_THIS, const char* path)
slouken@663
   706
{
slouken@663
   707
   /* if code compiled with HAVE_OPENGL, the library already linked */
slouken@663
   708
   this->gl_config.driver_loaded = 1;
slouken@663
   709
slouken@663
   710
   return 0;
slouken@663
   711
}
slouken@663
   712
slouken@663
   713
void* ph_GL_GetProcAddress(_THIS, const char* proc)
slouken@663
   714
{
slouken@663
   715
   return NULL;
slouken@663
   716
}
slouken@663
   717
slouken@291
   718
#endif /* HAVE_OPENGL */
slouken@571
   719
slouken@571
   720
static void ph_UpdateMouse(_THIS)
slouken@571
   721
{
slouken@571
   722
    PhCursorInfo_t phcursor;
slouken@571
   723
    short abs_x;
slouken@571
   724
    short abs_y;
slouken@571
   725
slouken@571
   726
    /* Lock the event thread, in multi-threading environments */
slouken@571
   727
    SDL_Lock_EventThread();
slouken@571
   728
slouken@571
   729
    /* synchronizing photon mouse cursor position and SDL mouse position, if cursor appears over window. */
slouken@571
   730
    PtGetAbsPosition(window, &abs_x, &abs_y);
slouken@571
   731
    PhQueryCursor(PhInputGroup(NULL), &phcursor);
slouken@571
   732
    if (((phcursor.pos.x >= abs_x) && (phcursor.pos.x <= abs_x + this->screen->w)) &&
slouken@571
   733
        ((phcursor.pos.y >= abs_y) && (phcursor.pos.y <= abs_y + this->screen->h)))
slouken@571
   734
    {
slouken@571
   735
        SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@571
   736
        SDL_PrivateMouseMotion(0, 0, phcursor.pos.x-abs_x, phcursor.pos.y-abs_y);
slouken@571
   737
    }
slouken@571
   738
    else
slouken@571
   739
    {
slouken@571
   740
        SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@571
   741
    }
slouken@571
   742
slouken@571
   743
    /* Unlock the event thread, in multi-threading environments */
slouken@571
   744
    SDL_Unlock_EventThread();
slouken@571
   745
}