2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
25 #include <sys/ioctl.h>
27 #include "SDL_endian.h"
28 #include "SDL_timer.h"
29 #include "SDL_thread.h"
30 #include "SDL_video.h"
31 #include "SDL_mouse.h"
32 #include "../SDL_sysvideo.h"
33 #include "../SDL_pixels_c.h"
34 #include "../../events/SDL_events_c.h"
35 #include "SDL_ph_video.h"
36 #include "SDL_ph_modes_c.h"
37 #include "SDL_ph_image_c.h"
38 #include "SDL_ph_events_c.h"
39 #include "SDL_ph_mouse_c.h"
40 #include "SDL_ph_wm_c.h"
41 #include "SDL_ph_gl.h"
42 #include "SDL_phyuv_c.h"
43 #include "../blank_cursor.h"
45 static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat);
46 static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
47 static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
48 static void ph_VideoQuit(_THIS);
49 static void ph_DeleteDevice(SDL_VideoDevice *device);
51 static int phstatus=-1;
53 static int ph_Available(void)
57 phstatus=PtInit(NULL);
70 static SDL_VideoDevice* ph_CreateDevice(int devindex)
72 SDL_VideoDevice* device;
74 /* Initialize all variables that we clean on shutdown */
75 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
78 SDL_memset(device, 0, (sizeof *device));
79 device->hidden = (struct SDL_PrivateVideoData*)SDL_malloc((sizeof *device->hidden));
80 device->gl_data = NULL;
82 if ((device == NULL) || (device->hidden == NULL))
85 ph_DeleteDevice(device);
88 SDL_memset(device->hidden, 0, (sizeof *device->hidden));
90 /* Set the driver flags */
91 device->handles_any_size = 1;
93 /* Set the function pointers */
94 device->CreateYUVOverlay = ph_CreateYUVOverlay;
95 device->VideoInit = ph_VideoInit;
96 device->ListModes = ph_ListModes;
97 device->SetVideoMode = ph_SetVideoMode;
98 device->ToggleFullScreen = ph_ToggleFullScreen;
99 device->UpdateMouse = ph_UpdateMouse;
100 device->SetColors = ph_SetColors;
101 device->UpdateRects = NULL; /* set up in ph_SetupUpdateFunction */
102 device->VideoQuit = ph_VideoQuit;
103 device->AllocHWSurface = ph_AllocHWSurface;
104 device->CheckHWBlit = ph_CheckHWBlit;
105 device->FillHWRect = ph_FillHWRect;
106 device->SetHWColorKey = ph_SetHWColorKey;
107 device->SetHWAlpha = ph_SetHWAlpha;
108 device->LockHWSurface = ph_LockHWSurface;
109 device->UnlockHWSurface = ph_UnlockHWSurface;
110 device->FlipHWSurface = ph_FlipHWSurface;
111 device->FreeHWSurface = ph_FreeHWSurface;
112 device->SetCaption = ph_SetCaption;
113 device->SetIcon = NULL;
114 device->IconifyWindow = ph_IconifyWindow;
115 device->GrabInput = ph_GrabInput;
116 device->GetWMInfo = ph_GetWMInfo;
117 device->FreeWMCursor = ph_FreeWMCursor;
118 device->CreateWMCursor = ph_CreateWMCursor;
119 device->ShowWMCursor = ph_ShowWMCursor;
120 device->WarpWMCursor = ph_WarpWMCursor;
121 device->MoveWMCursor = NULL;
122 device->CheckMouseMode = ph_CheckMouseMode;
123 device->InitOSKeymap = ph_InitOSKeymap;
124 device->PumpEvents = ph_PumpEvents;
126 /* OpenGL support. */
128 device->GL_MakeCurrent = ph_GL_MakeCurrent;
129 device->GL_SwapBuffers = ph_GL_SwapBuffers;
130 device->GL_GetAttribute = ph_GL_GetAttribute;
131 device->GL_LoadLibrary = ph_GL_LoadLibrary;
132 device->GL_GetProcAddress = ph_GL_GetProcAddress;
133 #endif /* SDL_VIDEO_OPENGL */
135 device->free = ph_DeleteDevice;
140 VideoBootStrap ph_bootstrap = {
141 "photon", "QNX Photon video output",
142 ph_Available, ph_CreateDevice
145 static void ph_DeleteDevice(SDL_VideoDevice *device)
151 SDL_free(device->hidden);
152 device->hidden = NULL;
156 SDL_free(device->gl_data);
157 device->gl_data = NULL;
164 static PtWidget_t *ph_CreateWindow(_THIS)
168 widget = PtCreateWidget(PtWindow, NULL, 0, NULL);
173 static int ph_SetupWindow(_THIS, int w, int h, int flags)
176 PhPoint_t pos = {0, 0};
178 PhDim_t dim = {w, h};
179 PhRect_t desktopextent;
181 const char* windowpos;
182 const char* iscentered;
185 /* check if window size has been changed by Window Manager */
186 PtGetResource(window, Pt_ARG_DIM, &olddim, 0);
187 if ((olddim->w!=w) || (olddim->h!=h))
189 PtSetArg(&args[nargs++], Pt_ARG_DIM, &dim, 0);
192 if ((flags & SDL_RESIZABLE) == SDL_RESIZABLE)
194 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_CLOSE);
195 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_MAX | Ph_WM_RESTORE | Ph_WM_RESIZE);
196 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_RESIZE | Ph_WM_MOVE | Ph_WM_CLOSE | Ph_WM_MAX | Ph_WM_RESTORE);
197 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);
198 PtSetArg(&args[nargs++], Pt_ARG_RESIZE_FLAGS, Pt_TRUE, Pt_RESIZE_XY_AS_REQUIRED);
202 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX | Ph_WM_RESTORE | Ph_WM_CLOSE);
203 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_FALSE, Ph_WM_RESIZE | Ph_WM_MAX | Ph_WM_RESTORE);
204 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_MOVE | Ph_WM_CLOSE);
205 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);
206 PtSetArg(&args[nargs++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
209 if (((flags & SDL_NOFRAME)==SDL_NOFRAME) || ((flags & SDL_FULLSCREEN)==SDL_FULLSCREEN))
211 if ((flags & SDL_RESIZABLE) != SDL_RESIZABLE)
213 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Pt_TRUE);
217 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, Pt_TRUE);
218 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_RESIZE | Ph_WM_RENDER_BORDER);
223 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_TRUE, Ph_WM_RENDER_BORDER | Ph_WM_RENDER_TITLE |
224 Ph_WM_RENDER_CLOSE | Ph_WM_RENDER_MENU | Ph_WM_RENDER_MIN);
227 if ((flags & SDL_FULLSCREEN) == SDL_FULLSCREEN)
229 PtSetArg(&args[nargs++], Pt_ARG_POS, &pos, 0);
230 PtSetArg(&args[nargs++], Pt_ARG_BASIC_FLAGS, Pt_TRUE, Pt_BASIC_PREVENT_FILL);
231 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_FFRONT | Ph_WM_MAX | Ph_WM_TOFRONT | Ph_WM_CONSWITCH);
232 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISFOCUS | Ph_WM_STATE_ISALTKEY);
236 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_FALSE, Ph_WM_FFRONT | Ph_WM_CONSWITCH);
237 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_FALSE, Ph_WM_STATE_ISFRONT);
238 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISALTKEY);
240 if ((flags & SDL_HWSURFACE) == SDL_HWSURFACE)
242 PtSetArg(&args[nargs++], Pt_ARG_BASIC_FLAGS, Pt_TRUE, Pt_BASIC_PREVENT_FILL);
246 PtSetArg(&args[nargs++], Pt_ARG_FILL_COLOR, Pg_BLACK, 0);
248 if (!currently_maximized)
250 windowpos = SDL_getenv("SDL_VIDEO_WINDOW_POS");
251 iscentered = SDL_getenv("SDL_VIDEO_CENTERED");
253 if ((iscentered) || ((windowpos) && (SDL_strcmp(windowpos, "center")==0)))
255 PhWindowQueryVisible(Ph_QUERY_CONSOLE, 0, 0, &desktopextent);
256 if (desktop_mode.width>w)
258 pos.x = (desktop_mode.width - w)/2;
260 if (desktop_mode.height>h)
262 pos.y = (desktop_mode.height - h)/2;
265 pos.x+=desktopextent.ul.x;
266 pos.y+=desktopextent.ul.y;
267 PtSetArg(&args[nargs++], Pt_ARG_POS, &pos, 0);
273 if (SDL_sscanf(windowpos, "%d,%d", &x, &y) == 2)
275 if ((x<desktop_mode.width) && (y<desktop_mode.height))
277 PhWindowQueryVisible(Ph_QUERY_CONSOLE, 0, 0, &desktopextent);
278 pos.x=x+desktopextent.ul.x;
279 pos.y=y+desktopextent.ul.y;
281 PtSetArg(&args[nargs++], Pt_ARG_POS, &pos, 0);
287 /* if window is maximized render it as maximized */
288 if (currently_maximized)
290 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISMAX);
294 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_FALSE, Ph_WM_STATE_ISMAX);
297 /* do not grab the keyboard by default */
298 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_FALSE, Ph_WM_STATE_ISALTKEY);
300 /* bring the focus to the window */
301 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFOCUS);
303 /* allow to catch hide event */
304 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_MANAGED_FLAGS, Pt_TRUE, Ph_WM_HIDE);
305 PtSetArg(&args[nargs++], Pt_ARG_WINDOW_NOTIFY_FLAGS, Pt_TRUE, Ph_WM_HIDE);
308 PtSetResources(window, nargs, args);
309 PtRealizeWidget(window);
310 PtWindowToFront(window);
313 PtGetResource(window, Pt_ARG_POS, &olddim, 0);
314 fprintf(stderr, "POSITION: %d, %d\n", olddim->w, olddim->h);
320 static const struct ColourMasks* ph_GetColourMasks(int bpp)
322 /* The alpha mask doesn't appears to be needed */
323 static const struct ColourMasks phColorMasks[5] = {
324 /* 8 bit */ {0, 0, 0, 0, 8},
325 /* 15 bit ARGB */ {0x7C00, 0x03E0, 0x001F, 0x8000, 15},
326 /* 16 bit RGB */ {0xF800, 0x07E0, 0x001F, 0x0000, 16},
327 /* 24 bit RGB */ {0xFF0000, 0x00FF00, 0x0000FF, 0x000000, 24},
328 /* 32 bit ARGB */ {0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000, 32},
334 return &phColorMasks[0];
336 return &phColorMasks[1];
338 return &phColorMasks[2];
340 return &phColorMasks[3];
342 return &phColorMasks[4];
347 static int ph_VideoInit(_THIS, SDL_PixelFormat* vformat)
353 desktoppal=SDLPH_PAL_NONE;
365 if (NULL == (phevent = SDL_malloc(EVENT_SIZE)))
370 SDL_memset(phevent, 0x00, EVENT_SIZE);
372 window = ph_CreateWindow(this);
375 SDL_SetError("ph_VideoInit(): Couldn't create video window !\n");
379 /* Create the blank cursor */
380 SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
381 (int)BLANK_CWIDTH, (int)BLANK_CHEIGHT,
382 (int)BLANK_CHOTX, (int)BLANK_CHOTY);
384 if (SDL_BlankCursor == NULL)
389 if (PgGetGraphicsHWCaps(&hwcaps) < 0)
391 SDL_SetError("ph_VideoInit(): GetGraphicsHWCaps function failed !\n");
392 this->FreeWMCursor(this, SDL_BlankCursor);
396 if (PgGetVideoModeInfo(hwcaps.current_video_mode, &desktop_mode) < 0)
398 SDL_SetError("ph_VideoInit(): PgGetVideoModeInfo function failed !\n");
399 this->FreeWMCursor(this, SDL_BlankCursor);
403 /* Determine the current screen size */
404 this->info.current_w = desktop_mode.width;
405 this->info.current_h = desktop_mode.height;
407 /* We need to return BytesPerPixel as it in used by CreateRGBsurface */
408 vformat->BitsPerPixel = desktop_mode.bits_per_pixel;
409 vformat->BytesPerPixel = desktop_mode.bytes_per_scanline/desktop_mode.width;
410 desktopbpp = desktop_mode.bits_per_pixel;
412 /* save current palette */
415 PgGetPalette(savedpal);
416 PgGetPalette(syspalph);
420 for(i=0; i<_Pg_MAX_PALETTE; i++)
422 savedpal[i]=PgRGB(0, 0, 0);
423 syspalph[i]=PgRGB(0, 0, 0);
427 currently_fullscreen = 0;
429 currently_maximized = 0;
430 current_overlay = NULL;
432 OCImage.direct_context = NULL;
433 OCImage.offscreen_context = NULL;
434 OCImage.offscreen_backcontext = NULL;
435 OCImage.oldDC = NULL;
436 OCImage.CurrentFrameData = NULL;
437 OCImage.FrameData0 = NULL;
438 OCImage.FrameData1 = NULL;
439 videomode_emulatemode = 0;
441 this->info.wm_available = 1;
443 ph_UpdateHWInfo(this);
448 static SDL_Surface* ph_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
450 const struct ColourMasks* mask;
452 /* Lock the event thread, in multi-threading environments */
453 SDL_Lock_EventThread();
455 current->flags = flags;
457 /* if we do not have desired fullscreen mode, then fallback into window mode */
458 if (((current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) && (ph_GetVideoMode(width, height, bpp)==0))
460 current->flags &= ~SDL_FULLSCREEN;
461 current->flags &= ~SDL_NOFRAME;
462 current->flags &= ~SDL_RESIZABLE;
465 ph_SetupWindow(this, width, height, current->flags);
467 mask = ph_GetColourMasks(bpp);
470 SDL_ReallocFormat(current, mask->bpp, mask->red, mask->green, mask->blue, 0);
474 SDL_SetError("ph_SetVideoMode(): desired bpp is not supported by photon !\n");
478 if ((current->flags & SDL_OPENGL)==SDL_OPENGL)
480 #if !SDL_VIDEO_OPENGL
481 /* if no built-in OpenGL support */
482 SDL_SetError("ph_SetVideoMode(): no OpenGL support, you need to recompile SDL.\n");
483 current->flags &= ~SDL_OPENGL;
485 #endif /* SDL_VIDEO_OPENGL */
489 /* Initialize internal variables */
490 if ((current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN)
494 desktoppal=SDLPH_PAL_SYSTEM;
497 current->flags &= ~SDL_RESIZABLE; /* no resize for Direct Context */
498 current->flags |= SDL_HWSURFACE;
502 /* remove this if we'll have support for the non-fullscreen sw/hw+doublebuf one day */
503 current->flags &= ~SDL_DOUBLEBUF;
505 /* Use offscreen memory if SDL_HWSURFACE flag is set */
506 if ((current->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
510 current->flags &= ~SDL_HWSURFACE;
514 /* using palette emulation code in window mode */
519 desktoppal = SDLPH_PAL_EMULATE;
523 desktoppal = SDLPH_PAL_SYSTEM;
528 desktoppal = SDLPH_PAL_NONE;
536 if (desktoppal==SDLPH_PAL_SYSTEM)
538 current->flags|=SDL_HWPALETTE;
541 /* Must call at least once for setup image planes */
542 if (ph_SetupUpdateFunction(this, current, current->flags)==-1)
544 /* Error string was filled in the ph_SetupUpdateFunction() */
548 /* finish window drawing, if we are not in fullscreen, of course */
549 if ((current->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
560 ph_UpdateHWInfo(this);
562 SDL_Unlock_EventThread();
568 static void ph_VideoQuit(_THIS)
570 /* restore palette */
573 PgSetPalette(syspalph, 0, -1, 0, 0, 0);
574 PgSetPalette(savedpal, 0, 0, _Pg_MAX_PALETTE, Pg_PALSET_GLOBAL | Pg_PALSET_FORCE_EXPOSE, 0);
578 ph_DestroyImage(this, SDL_VideoSurface);
582 PtUnrealizeWidget(window);
583 PtDestroyWidget(window);
594 static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
599 updaterect.x = updaterect.y = 0;
600 updaterect.w = this->screen->w;
601 updaterect.h = this->screen->h;
603 /* palette emulation code, using palette of the PhImage_t struct */
604 if (desktoppal==SDLPH_PAL_EMULATE)
606 if ((SDL_Image) && (SDL_Image->palette))
608 for (i=firstcolor; i<firstcolor+ncolors; i++)
610 syspalph[i] = PgRGB(colors[i-firstcolor].r, colors[i-firstcolor].g, colors[i-firstcolor].b);
611 SDL_Image->palette[i] = syspalph[i];
614 /* image needs to be redrawn */
615 this->UpdateRects(this, 1, &updaterect);
620 if (desktoppal==SDLPH_PAL_SYSTEM)
622 for (i=firstcolor; i<firstcolor+ncolors; i++)
624 syspalph[i] = PgRGB(colors[i-firstcolor].r, colors[i-firstcolor].g, colors[i-firstcolor].b);
627 if ((this->screen->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
629 /* window mode must use soft palette */
630 PgSetPalette(&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
631 /* image needs to be redrawn */
632 this->UpdateRects(this, 1, &updaterect);
636 /* fullscreen mode must use hardware palette */
637 PgSetPalette(&syspalph[firstcolor], 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
642 /* SDLPH_PAL_NONE do nothing */