hook into SDL
[sdl_omap.git] / src / video / SDL_video.c
CommitLineData
e14743d1 1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
4
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.
9
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.
14
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
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* The high-level video driver subsystem */
25
26#include "SDL.h"
27#include "SDL_sysvideo.h"
28#include "SDL_blit.h"
29#include "SDL_pixels_c.h"
30#include "SDL_cursor_c.h"
31#include "../events/SDL_sysevents.h"
32#include "../events/SDL_events_c.h"
33
34/* Available video drivers */
35static VideoBootStrap *bootstrap[] = {
36#if SDL_VIDEO_DRIVER_QUARTZ
37 &QZ_bootstrap,
38#endif
39#if SDL_VIDEO_DRIVER_X11
40 &X11_bootstrap,
41#endif
42#if SDL_VIDEO_DRIVER_DGA
43 &DGA_bootstrap,
44#endif
45#if SDL_VIDEO_DRIVER_NANOX
46 &NX_bootstrap,
47#endif
48#if SDL_VIDEO_DRIVER_IPOD
49 &iPod_bootstrap,
50#endif
51#if SDL_VIDEO_DRIVER_QTOPIA
52 &Qtopia_bootstrap,
53#endif
54#if SDL_VIDEO_DRIVER_WSCONS
55 &WSCONS_bootstrap,
56#endif
57#if SDL_VIDEO_DRIVER_FBCON
58 &FBCON_bootstrap,
59#endif
60#if SDL_VIDEO_DRIVER_DIRECTFB
61 &DirectFB_bootstrap,
62#endif
63#if SDL_VIDEO_DRIVER_PS2GS
64 &PS2GS_bootstrap,
65#endif
66#if SDL_VIDEO_DRIVER_PS3
67 &PS3_bootstrap,
68#endif
69#if SDL_VIDEO_DRIVER_GGI
70 &GGI_bootstrap,
71#endif
72#if SDL_VIDEO_DRIVER_VGL
73 &VGL_bootstrap,
74#endif
75#if SDL_VIDEO_DRIVER_SVGALIB
76 &SVGALIB_bootstrap,
77#endif
78#if SDL_VIDEO_DRIVER_GAPI
79 &GAPI_bootstrap,
80#endif
81#if SDL_VIDEO_DRIVER_WINDIB
82 &WINDIB_bootstrap,
83#endif
84#if SDL_VIDEO_DRIVER_DDRAW
85 &DIRECTX_bootstrap,
86#endif
87#if SDL_VIDEO_DRIVER_BWINDOW
88 &BWINDOW_bootstrap,
89#endif
90#if SDL_VIDEO_DRIVER_TOOLBOX
91 &TOOLBOX_bootstrap,
92#endif
93#if SDL_VIDEO_DRIVER_DRAWSPROCKET
94 &DSp_bootstrap,
95#endif
96#if SDL_VIDEO_DRIVER_PHOTON
97 &ph_bootstrap,
98#endif
99#if SDL_VIDEO_DRIVER_EPOC
100 &EPOC_bootstrap,
101#endif
102#if SDL_VIDEO_DRIVER_XBIOS
103 &XBIOS_bootstrap,
104#endif
105#if SDL_VIDEO_DRIVER_GEM
106 &GEM_bootstrap,
107#endif
108#if SDL_VIDEO_DRIVER_PICOGUI
109 &PG_bootstrap,
110#endif
111#if SDL_VIDEO_DRIVER_DC
112 &DC_bootstrap,
113#endif
114#if SDL_VIDEO_DRIVER_NDS
115 &NDS_bootstrap,
116#endif
117#if SDL_VIDEO_DRIVER_RISCOS
118 &RISCOS_bootstrap,
119#endif
120#if SDL_VIDEO_DRIVER_OS2FS
121 &OS2FSLib_bootstrap,
122#endif
123#if SDL_VIDEO_DRIVER_AALIB
124 &AALIB_bootstrap,
125#endif
126#if SDL_VIDEO_DRIVER_CACA
127 &CACA_bootstrap,
128#endif
9a8e84f8 129#if SDL_VIDEO_DRIVER_OMAPDSS
130 &omapdss_bootstrap,
131#endif
e14743d1 132#if SDL_VIDEO_DRIVER_DUMMY
133 &DUMMY_bootstrap,
134#endif
135 NULL
136};
137
138SDL_VideoDevice *current_video = NULL;
139
140/* Various local functions */
141int SDL_VideoInit(const char *driver_name, Uint32 flags);
142void SDL_VideoQuit(void);
143void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect* rects);
144
145static SDL_GrabMode SDL_WM_GrabInputOff(void);
146#if SDL_VIDEO_OPENGL
147static int lock_count = 0;
148#endif
149
150
151/*
152 * Initialize the video and event subsystems -- determine native pixel format
153 */
154int SDL_VideoInit (const char *driver_name, Uint32 flags)
155{
156 SDL_VideoDevice *video;
157 int index;
158 int i;
159 SDL_PixelFormat vformat;
160 Uint32 video_flags;
161
162 /* Toggle the event thread flags, based on OS requirements */
163#if defined(MUST_THREAD_EVENTS)
164 flags |= SDL_INIT_EVENTTHREAD;
165#elif defined(CANT_THREAD_EVENTS)
166 if ( (flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) {
167 SDL_SetError("OS doesn't support threaded events");
168 return(-1);
169 }
170#endif
171
172 /* Check to make sure we don't overwrite 'current_video' */
173 if ( current_video != NULL ) {
174 SDL_VideoQuit();
175 }
176
177 /* Select the proper video driver */
178 index = 0;
179 video = NULL;
180 if ( driver_name != NULL ) {
181#if 0 /* This will be replaced with a better driver selection API */
182 if ( SDL_strrchr(driver_name, ':') != NULL ) {
183 index = atoi(SDL_strrchr(driver_name, ':')+1);
184 }
185#endif
186 for ( i=0; bootstrap[i]; ++i ) {
187 if ( SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
188 if ( bootstrap[i]->available() ) {
189 video = bootstrap[i]->create(index);
190 break;
191 }
192 }
193 }
194 } else {
195 for ( i=0; bootstrap[i]; ++i ) {
196 if ( bootstrap[i]->available() ) {
197 video = bootstrap[i]->create(index);
198 if ( video != NULL ) {
199 break;
200 }
201 }
202 }
203 }
204 if ( video == NULL ) {
205 SDL_SetError("No available video device");
206 return(-1);
207 }
208 current_video = video;
209 current_video->name = bootstrap[i]->name;
210
211 /* Do some basic variable initialization */
212 video->screen = NULL;
213 video->shadow = NULL;
214 video->visible = NULL;
215 video->physpal = NULL;
216 video->gammacols = NULL;
217 video->gamma = NULL;
218 video->wm_title = NULL;
219 video->wm_icon = NULL;
220 video->offset_x = 0;
221 video->offset_y = 0;
222 SDL_memset(&video->info, 0, (sizeof video->info));
223
224 video->displayformatalphapixel = NULL;
225
226 /* Set some very sane GL defaults */
227 video->gl_config.driver_loaded = 0;
228 video->gl_config.dll_handle = NULL;
229 video->gl_config.red_size = 3;
230 video->gl_config.green_size = 3;
231 video->gl_config.blue_size = 2;
232 video->gl_config.alpha_size = 0;
233 video->gl_config.buffer_size = 0;
234 video->gl_config.depth_size = 16;
235 video->gl_config.stencil_size = 0;
236 video->gl_config.double_buffer = 1;
237 video->gl_config.accum_red_size = 0;
238 video->gl_config.accum_green_size = 0;
239 video->gl_config.accum_blue_size = 0;
240 video->gl_config.accum_alpha_size = 0;
241 video->gl_config.stereo = 0;
242 video->gl_config.multisamplebuffers = 0;
243 video->gl_config.multisamplesamples = 0;
244 video->gl_config.accelerated = -1; /* not known, don't set */
245 video->gl_config.swap_control = -1; /* not known, don't set */
246
247 /* Initialize the video subsystem */
248 SDL_memset(&vformat, 0, sizeof(vformat));
249 if ( video->VideoInit(video, &vformat) < 0 ) {
250 SDL_VideoQuit();
251 return(-1);
252 }
253
254 /* Create a zero sized video surface of the appropriate format */
255 video_flags = SDL_SWSURFACE;
256 SDL_VideoSurface = SDL_CreateRGBSurface(video_flags, 0, 0,
257 vformat.BitsPerPixel,
258 vformat.Rmask, vformat.Gmask, vformat.Bmask, 0);
259 if ( SDL_VideoSurface == NULL ) {
260 SDL_VideoQuit();
261 return(-1);
262 }
263 SDL_PublicSurface = NULL; /* Until SDL_SetVideoMode() */
264
265#if 0 /* Don't change the current palette - may be used by other programs.
266 * The application can't do anything with the display surface until
267 * a video mode has been set anyway. :)
268 */
269 /* If we have a palettized surface, create a default palette */
270 if ( SDL_VideoSurface->format->palette ) {
271 SDL_PixelFormat *vf = SDL_VideoSurface->format;
272 SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
273 video->SetColors(video,
274 0, vf->palette->ncolors, vf->palette->colors);
275 }
276#endif
277 video->info.vfmt = SDL_VideoSurface->format;
278
279 /* Start the event loop */
280 if ( SDL_StartEventLoop(flags) < 0 ) {
281 SDL_VideoQuit();
282 return(-1);
283 }
284 SDL_CursorInit(flags & SDL_INIT_EVENTTHREAD);
285
286 /* We're ready to go! */
287 return(0);
288}
289
290char *SDL_VideoDriverName(char *namebuf, int maxlen)
291{
292 if ( current_video != NULL ) {
293 SDL_strlcpy(namebuf, current_video->name, maxlen);
294 return(namebuf);
295 }
296 return(NULL);
297}
298
299/*
300 * Get the current display surface
301 */
302SDL_Surface *SDL_GetVideoSurface(void)
303{
304 SDL_Surface *visible;
305
306 visible = NULL;
307 if ( current_video ) {
308 visible = current_video->visible;
309 }
310 return(visible);
311}
312
313/*
314 * Get the current information about the video hardware
315 */
316const SDL_VideoInfo *SDL_GetVideoInfo(void)
317{
318 const SDL_VideoInfo *info;
319
320 info = NULL;
321 if ( current_video ) {
322 info = &current_video->info;
323 }
324 return(info);
325}
326
327/*
328 * Return a pointer to an array of available screen dimensions for the
329 * given format, sorted largest to smallest. Returns NULL if there are
330 * no dimensions available for a particular format, or (SDL_Rect **)-1
331 * if any dimension is okay for the given format. If 'format' is NULL,
332 * the mode list will be for the format given by SDL_GetVideoInfo()->vfmt
333 */
334SDL_Rect ** SDL_ListModes (SDL_PixelFormat *format, Uint32 flags)
335{
336 SDL_VideoDevice *video = current_video;
337 SDL_VideoDevice *this = current_video;
338 SDL_Rect **modes;
339
340 modes = NULL;
341 if ( SDL_VideoSurface ) {
342 if ( format == NULL ) {
343 format = SDL_VideoSurface->format;
344 }
345 modes = video->ListModes(this, format, flags);
346 }
347 return(modes);
348}
349
350/*
351 * Check to see if a particular video mode is supported.
352 * It returns 0 if the requested mode is not supported under any bit depth,
353 * or returns the bits-per-pixel of the closest available mode with the
354 * given width and height. If this bits-per-pixel is different from the
355 * one used when setting the video mode, SDL_SetVideoMode() will succeed,
356 * but will emulate the requested bits-per-pixel with a shadow surface.
357 */
358static Uint8 SDL_closest_depths[4][8] = {
359 /* 8 bit closest depth ordering */
360 { 0, 8, 16, 15, 32, 24, 0, 0 },
361 /* 15,16 bit closest depth ordering */
362 { 0, 16, 15, 32, 24, 8, 0, 0 },
363 /* 24 bit closest depth ordering */
364 { 0, 24, 32, 16, 15, 8, 0, 0 },
365 /* 32 bit closest depth ordering */
366 { 0, 32, 16, 15, 24, 8, 0, 0 }
367};
368
369
370#ifdef __MACOS__ /* MPW optimization bug? */
371#define NEGATIVE_ONE 0xFFFFFFFF
372#else
373#define NEGATIVE_ONE -1
374#endif
375
376int SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags)
377{
378 int table, b, i;
379 int supported;
380 SDL_PixelFormat format;
381 SDL_Rect **sizes;
382
383 /* Currently 1 and 4 bpp are not supported */
384 if ( bpp < 8 || bpp > 32 ) {
385 return(0);
386 }
387 if ( (width <= 0) || (height <= 0) ) {
388 return(0);
389 }
390
391 /* Search through the list valid of modes */
392 SDL_memset(&format, 0, sizeof(format));
393 supported = 0;
394 table = ((bpp+7)/8)-1;
395 SDL_closest_depths[table][0] = bpp;
396 SDL_closest_depths[table][7] = 0;
397 for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
398 format.BitsPerPixel = SDL_closest_depths[table][b];
399 sizes = SDL_ListModes(&format, flags);
400 if ( sizes == (SDL_Rect **)0 ) {
401 /* No sizes supported at this bit-depth */
402 continue;
403 } else
404 if (sizes == (SDL_Rect **)NEGATIVE_ONE) {
405 /* Any size supported at this bit-depth */
406 supported = 1;
407 continue;
408 } else if (current_video->handles_any_size) {
409 /* Driver can center a smaller surface to simulate fullscreen */
410 for ( i=0; sizes[i]; ++i ) {
411 if ((sizes[i]->w >= width) && (sizes[i]->h >= height)) {
412 supported = 1; /* this mode can fit the centered window. */
413 break;
414 }
415 }
416 } else
417 for ( i=0; sizes[i]; ++i ) {
418 if ((sizes[i]->w == width) && (sizes[i]->h == height)) {
419 supported = 1;
420 break;
421 }
422 }
423 }
424 if ( supported ) {
425 --b;
426 return(SDL_closest_depths[table][b]);
427 } else {
428 return(0);
429 }
430}
431
432/*
433 * Get the closest non-emulated video mode to the one requested
434 */
435static int SDL_GetVideoMode (int *w, int *h, int *BitsPerPixel, Uint32 flags)
436{
437 int table, b, i;
438 int supported;
439 int native_bpp;
440 SDL_PixelFormat format;
441 SDL_Rect **sizes;
442
443 /* Check parameters */
444 if ( *BitsPerPixel < 8 || *BitsPerPixel > 32 ) {
445 SDL_SetError("Invalid bits per pixel (range is {8...32})");
446 return(0);
447 }
448 if ((*w <= 0) || (*h <= 0)) {
449 SDL_SetError("Invalid width or height");
450 return(0);
451 }
452
453 /* Try the original video mode, get the closest depth */
454 native_bpp = SDL_VideoModeOK(*w, *h, *BitsPerPixel, flags);
455 if ( native_bpp == *BitsPerPixel ) {
456 return(1);
457 }
458 if ( native_bpp > 0 ) {
459 *BitsPerPixel = native_bpp;
460 return(1);
461 }
462
463 /* No exact size match at any depth, look for closest match */
464 SDL_memset(&format, 0, sizeof(format));
465 supported = 0;
466 table = ((*BitsPerPixel+7)/8)-1;
467 SDL_closest_depths[table][0] = *BitsPerPixel;
468 SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
469 for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
470 int best;
471
472 format.BitsPerPixel = SDL_closest_depths[table][b];
473 sizes = SDL_ListModes(&format, flags);
474 if ( sizes == (SDL_Rect **)0 ) {
475 /* No sizes supported at this bit-depth */
476 continue;
477 }
478 best=0;
479 for ( i=0; sizes[i]; ++i ) {
480 /* Mode with both dimensions bigger or equal than asked ? */
481 if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) {
482 /* Mode with any dimension smaller or equal than current best ? */
483 if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) {
484 /* Now choose the mode that has less pixels */
485 if ((sizes[i]->w * sizes[i]->h) <= (sizes[best]->w * sizes[best]->h)) {
486 best=i;
487 supported = 1;
488 }
489 }
490 }
491 }
492 if (supported) {
493 *w=sizes[best]->w;
494 *h=sizes[best]->h;
495 *BitsPerPixel = SDL_closest_depths[table][b];
496 }
497 }
498 if ( ! supported ) {
499 SDL_SetError("No video mode large enough for %dx%d", *w, *h);
500 }
501 return(supported);
502}
503
504/* This should probably go somewhere else -- like SDL_surface.c */
505static void SDL_ClearSurface(SDL_Surface *surface)
506{
507 Uint32 black;
508
509 black = SDL_MapRGB(surface->format, 0, 0, 0);
510 SDL_FillRect(surface, NULL, black);
511 if ((surface->flags&SDL_HWSURFACE) && (surface->flags&SDL_DOUBLEBUF)) {
512 SDL_Flip(surface);
513 SDL_FillRect(surface, NULL, black);
514 }
515 if (surface->flags&SDL_FULLSCREEN) {
516 SDL_Flip(surface);
517 }
518}
519
520/*
521 * Create a shadow surface suitable for fooling the app. :-)
522 */
523static void SDL_CreateShadowSurface(int depth)
524{
525 Uint32 Rmask, Gmask, Bmask;
526
527 /* Allocate the shadow surface */
528 if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
529 Rmask = (SDL_VideoSurface->format)->Rmask;
530 Gmask = (SDL_VideoSurface->format)->Gmask;
531 Bmask = (SDL_VideoSurface->format)->Bmask;
532 } else {
533 Rmask = Gmask = Bmask = 0;
534 }
535 SDL_ShadowSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
536 SDL_VideoSurface->w, SDL_VideoSurface->h,
537 depth, Rmask, Gmask, Bmask, 0);
538 if ( SDL_ShadowSurface == NULL ) {
539 return;
540 }
541
542 /* 8-bit shadow surfaces report that they have exclusive palette */
543 if ( SDL_ShadowSurface->format->palette ) {
544 SDL_ShadowSurface->flags |= SDL_HWPALETTE;
545 if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
546 SDL_memcpy(SDL_ShadowSurface->format->palette->colors,
547 SDL_VideoSurface->format->palette->colors,
548 SDL_VideoSurface->format->palette->ncolors*
549 sizeof(SDL_Color));
550 } else {
551 SDL_DitherColors(
552 SDL_ShadowSurface->format->palette->colors, depth);
553 }
554 }
555
556 /* If the video surface is resizable, the shadow should say so */
557 if ( (SDL_VideoSurface->flags & SDL_RESIZABLE) == SDL_RESIZABLE ) {
558 SDL_ShadowSurface->flags |= SDL_RESIZABLE;
559 }
560 /* If the video surface has no frame, the shadow should say so */
561 if ( (SDL_VideoSurface->flags & SDL_NOFRAME) == SDL_NOFRAME ) {
562 SDL_ShadowSurface->flags |= SDL_NOFRAME;
563 }
564 /* If the video surface is fullscreen, the shadow should say so */
565 if ( (SDL_VideoSurface->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
566 SDL_ShadowSurface->flags |= SDL_FULLSCREEN;
567 }
568 /* If the video surface is flippable, the shadow should say so */
569 if ( (SDL_VideoSurface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
570 SDL_ShadowSurface->flags |= SDL_DOUBLEBUF;
571 }
572 return;
573}
574
575#ifdef __QNXNTO__
576 #include <sys/neutrino.h>
577#endif /* __QNXNTO__ */
578
579/*
580 * Set the requested video mode, allocating a shadow buffer if necessary.
581 */
582SDL_Surface * SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags)
583{
584 SDL_VideoDevice *video, *this;
585 SDL_Surface *prev_mode, *mode;
586 int video_w;
587 int video_h;
588 int video_bpp;
589 int is_opengl;
590 SDL_GrabMode saved_grab;
591
592 /* Start up the video driver, if necessary..
593 WARNING: This is the only function protected this way!
594 */
595 if ( ! current_video ) {
596 if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0 ) {
597 return(NULL);
598 }
599 }
600 this = video = current_video;
601
602 /* Default to the current width and height */
603 if ( width == 0 ) {
604 width = video->info.current_w;
605 }
606 if ( height == 0 ) {
607 height = video->info.current_h;
608 }
609 /* Default to the current video bpp */
610 if ( bpp == 0 ) {
611 flags |= SDL_ANYFORMAT;
612 bpp = SDL_VideoSurface->format->BitsPerPixel;
613 }
614
615 /* Get a good video mode, the closest one possible */
616 video_w = width;
617 video_h = height;
618 video_bpp = bpp;
619 if ( ! SDL_GetVideoMode(&video_w, &video_h, &video_bpp, flags) ) {
620 return(NULL);
621 }
622
623 /* Check the requested flags */
624 /* There's no palette in > 8 bits-per-pixel mode */
625 if ( video_bpp > 8 ) {
626 flags &= ~SDL_HWPALETTE;
627 }
628#if 0
629 if ( (flags&SDL_FULLSCREEN) != SDL_FULLSCREEN ) {
630 /* There's no windowed double-buffering */
631 flags &= ~SDL_DOUBLEBUF;
632 }
633#endif
634 if ( (flags&SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
635 /* Use hardware surfaces when double-buffering */
636 flags |= SDL_HWSURFACE;
637 }
638
639 is_opengl = ( ( flags & SDL_OPENGL ) == SDL_OPENGL );
640 if ( is_opengl ) {
641 /* These flags are for 2D video modes only */
642 flags &= ~(SDL_HWSURFACE|SDL_DOUBLEBUF);
643 }
644
645 /* Reset the keyboard here so event callbacks can run */
646 SDL_ResetKeyboard();
647 SDL_ResetMouse();
648 SDL_SetMouseRange(width, height);
649 SDL_cursorstate &= ~CURSOR_USINGSW;
650
651 /* Clean up any previous video mode */
652 if ( SDL_PublicSurface != NULL ) {
653 SDL_PublicSurface = NULL;
654 }
655 if ( SDL_ShadowSurface != NULL ) {
656 SDL_Surface *ready_to_go;
657 ready_to_go = SDL_ShadowSurface;
658 SDL_ShadowSurface = NULL;
659 SDL_FreeSurface(ready_to_go);
660 }
661 if ( video->physpal ) {
662 SDL_free(video->physpal->colors);
663 SDL_free(video->physpal);
664 video->physpal = NULL;
665 }
666 if( video->gammacols) {
667 SDL_free(video->gammacols);
668 video->gammacols = NULL;
669 }
670
671 /* Save the previous grab state and turn off grab for mode switch */
672 saved_grab = SDL_WM_GrabInputOff();
673
674 /* Try to set the video mode, along with offset and clipping */
675 prev_mode = SDL_VideoSurface;
676 SDL_LockCursor();
677 SDL_VideoSurface = NULL; /* In case it's freed by driver */
678 mode = video->SetVideoMode(this, prev_mode,video_w,video_h,video_bpp,flags);
679 if ( mode ) { /* Prevent resize events from mode change */
680 /* But not on OS/2 */
681#ifndef __OS2__
682 SDL_PrivateResize(mode->w, mode->h);
683#endif
684
685 /* Sam - If we asked for OpenGL mode, and didn't get it, fail */
686 if ( is_opengl && !(mode->flags & SDL_OPENGL) ) {
687 mode = NULL;
688 SDL_SetError("OpenGL not available");
689 }
690 }
691 /*
692 * rcg11292000
693 * If you try to set an SDL_OPENGL surface, and fail to find a
694 * matching visual, then the next call to SDL_SetVideoMode()
695 * will segfault, since we no longer point to a dummy surface,
696 * but rather NULL.
697 * Sam 11/29/00
698 * WARNING, we need to make sure that the previous mode hasn't
699 * already been freed by the video driver. What do we do in
700 * that case? Should we call SDL_VideoInit() again?
701 */
702 SDL_VideoSurface = (mode != NULL) ? mode : prev_mode;
703
704 if ( (mode != NULL) && (!is_opengl) ) {
705 /* Sanity check */
706 if ( (mode->w < width) || (mode->h < height) ) {
707 SDL_SetError("Video mode smaller than requested");
708 return(NULL);
709 }
710
711 /* If we have a palettized surface, create a default palette */
712 if ( mode->format->palette ) {
713 SDL_PixelFormat *vf = mode->format;
714 SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
715 video->SetColors(this, 0, vf->palette->ncolors,
716 vf->palette->colors);
717 }
718
719 /* Clear the surface to black */
720 video->offset_x = 0;
721 video->offset_y = 0;
722 mode->offset = 0;
723 SDL_SetClipRect(mode, NULL);
724 SDL_ClearSurface(mode);
725
726 /* Now adjust the offsets to match the desired mode */
727 video->offset_x = (mode->w-width)/2;
728 video->offset_y = (mode->h-height)/2;
729 mode->offset = video->offset_y*mode->pitch +
730 video->offset_x*mode->format->BytesPerPixel;
731#ifdef DEBUG_VIDEO
732 fprintf(stderr,
733 "Requested mode: %dx%dx%d, obtained mode %dx%dx%d (offset %d)\n",
734 width, height, bpp,
735 mode->w, mode->h, mode->format->BitsPerPixel, mode->offset);
736#endif
737 mode->w = width;
738 mode->h = height;
739 SDL_SetClipRect(mode, NULL);
740 }
741 SDL_ResetCursor();
742 SDL_UnlockCursor();
743
744 /* If we failed setting a video mode, return NULL... (Uh Oh!) */
745 if ( mode == NULL ) {
746 return(NULL);
747 }
748
749 /* If there is no window manager, set the SDL_NOFRAME flag */
750 if ( ! video->info.wm_available ) {
751 mode->flags |= SDL_NOFRAME;
752 }
753
754 /* Reset the mouse cursor and grab for new video mode */
755 SDL_SetCursor(NULL);
756 if ( video->UpdateMouse ) {
757 video->UpdateMouse(this);
758 }
759 SDL_WM_GrabInput(saved_grab);
760 SDL_GetRelativeMouseState(NULL, NULL); /* Clear first large delta */
761
762#if SDL_VIDEO_OPENGL
763 /* Load GL symbols (before MakeCurrent, where we need glGetString). */
764 if ( flags & (SDL_OPENGL | SDL_OPENGLBLIT) ) {
765
766#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
767#define __SDL_NOGETPROCADDR__
768#elif defined(__MINT__)
769#define __SDL_NOGETPROCADDR__
770#endif
771#ifdef __SDL_NOGETPROCADDR__
772 #define SDL_PROC(ret,func,params) video->func=func;
773#else
774 #define SDL_PROC(ret,func,params) \
775 do { \
776 video->func = SDL_GL_GetProcAddress(#func); \
777 if ( ! video->func ) { \
778 SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
779 return(NULL); \
780 } \
781 } while ( 0 );
782
783#endif /* __SDL_NOGETPROCADDR__ */
784
785#include "SDL_glfuncs.h"
786#undef SDL_PROC
787 }
788#endif /* SDL_VIDEO_OPENGL */
789
790 /* If we're running OpenGL, make the context current */
791 if ( (video->screen->flags & SDL_OPENGL) &&
792 video->GL_MakeCurrent ) {
793 if ( video->GL_MakeCurrent(this) < 0 ) {
794 return(NULL);
795 }
796 }
797
798 /* Set up a fake SDL surface for OpenGL "blitting" */
799 if ( (flags & SDL_OPENGLBLIT) == SDL_OPENGLBLIT ) {
800 /* Load GL functions for performing the texture updates */
801#if SDL_VIDEO_OPENGL
802
803 /* Create a software surface for blitting */
804#ifdef GL_VERSION_1_2
805 /* If the implementation either supports the packed pixels
806 extension, or implements the core OpenGL 1.2 API, it will
807 support the GL_UNSIGNED_SHORT_5_6_5 texture format.
808 */
809 if ( (bpp == 16) &&
810 (SDL_strstr((const char *)video->glGetString(GL_EXTENSIONS), "GL_EXT_packed_pixels") ||
811 (SDL_atof((const char *)video->glGetString(GL_VERSION)) >= 1.2f))
812 ) {
813 video->is_32bit = 0;
814 SDL_VideoSurface = SDL_CreateRGBSurface(
815 flags,
816 width,
817 height,
818 16,
819 31 << 11,
820 63 << 5,
821 31,
822 0
823 );
824 }
825 else
826#endif /* OpenGL 1.2 */
827 {
828 video->is_32bit = 1;
829 SDL_VideoSurface = SDL_CreateRGBSurface(
830 flags,
831 width,
832 height,
833 32,
834#if SDL_BYTEORDER == SDL_LIL_ENDIAN
835 0x000000FF,
836 0x0000FF00,
837 0x00FF0000,
838 0xFF000000
839#else
840 0xFF000000,
841 0x00FF0000,
842 0x0000FF00,
843 0x000000FF
844#endif
845 );
846 }
847 if ( ! SDL_VideoSurface ) {
848 return(NULL);
849 }
850 SDL_VideoSurface->flags = mode->flags | SDL_OPENGLBLIT;
851
852 /* Free the original video mode surface (is this safe?) */
853 SDL_FreeSurface(mode);
854
855 /* Set the surface completely opaque & white by default */
856 SDL_memset( SDL_VideoSurface->pixels, 255, SDL_VideoSurface->h * SDL_VideoSurface->pitch );
857 video->glGenTextures( 1, &video->texture );
858 video->glBindTexture( GL_TEXTURE_2D, video->texture );
859 video->glTexImage2D(
860 GL_TEXTURE_2D,
861 0,
862 video->is_32bit ? GL_RGBA : GL_RGB,
863 256,
864 256,
865 0,
866 video->is_32bit ? GL_RGBA : GL_RGB,
867#ifdef GL_VERSION_1_2
868 video->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
869#else
870 GL_UNSIGNED_BYTE,
871#endif
872 NULL);
873
874 video->UpdateRects = SDL_GL_UpdateRectsLock;
875#else
876 SDL_SetError("Somebody forgot to #define SDL_VIDEO_OPENGL");
877 return(NULL);
878#endif
879 }
880
881 /* Create a shadow surface if necessary */
882 /* There are three conditions under which we create a shadow surface:
883 1. We need a particular bits-per-pixel that we didn't get.
884 2. We need a hardware palette and didn't get one.
885 3. We need a software surface and got a hardware surface.
886 */
887 if ( !(SDL_VideoSurface->flags & SDL_OPENGL) &&
888 (
889 ( !(flags&SDL_ANYFORMAT) &&
890 (SDL_VideoSurface->format->BitsPerPixel != bpp)) ||
891 ( (flags&SDL_HWPALETTE) &&
892 !(SDL_VideoSurface->flags&SDL_HWPALETTE)) ||
893 /* If the surface is in hardware, video writes are visible
894 as soon as they are performed, so we need to buffer them
895 */
896 ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) &&
897 (SDL_VideoSurface->flags&SDL_HWSURFACE)) ||
898 ( (flags&SDL_DOUBLEBUF) &&
899 (SDL_VideoSurface->flags&SDL_HWSURFACE) &&
900 !(SDL_VideoSurface->flags&SDL_DOUBLEBUF))
901 ) ) {
902 SDL_CreateShadowSurface(bpp);
903 if ( SDL_ShadowSurface == NULL ) {
904 SDL_SetError("Couldn't create shadow surface");
905 return(NULL);
906 }
907 SDL_PublicSurface = SDL_ShadowSurface;
908 } else {
909 SDL_PublicSurface = SDL_VideoSurface;
910 }
911 video->info.vfmt = SDL_VideoSurface->format;
912 video->info.current_w = SDL_VideoSurface->w;
913 video->info.current_h = SDL_VideoSurface->h;
914
915 /* We're done! */
916 return(SDL_PublicSurface);
917}
918
919/*
920 * Convert a surface into the video pixel format.
921 */
922SDL_Surface * SDL_DisplayFormat (SDL_Surface *surface)
923{
924 Uint32 flags;
925
926 if ( ! SDL_PublicSurface ) {
927 SDL_SetError("No video mode has been set");
928 return(NULL);
929 }
930 /* Set the flags appropriate for copying to display surface */
931 if (((SDL_PublicSurface->flags&SDL_HWSURFACE) == SDL_HWSURFACE) && current_video->info.blit_hw)
932 flags = SDL_HWSURFACE;
933 else
934 flags = SDL_SWSURFACE;
935#ifdef AUTORLE_DISPLAYFORMAT
936 flags |= (surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA));
937 flags |= SDL_RLEACCELOK;
938#else
939 flags |= surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA|SDL_RLEACCELOK);
940#endif
941 return(SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
942}
943
944/*
945 * Convert a surface into a format that's suitable for blitting to
946 * the screen, but including an alpha channel.
947 */
948SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface)
949{
950 SDL_PixelFormat *vf;
951 SDL_PixelFormat *format;
952 SDL_Surface *converted;
953 Uint32 flags;
954 /* default to ARGB8888 */
955 Uint32 amask = 0xff000000;
956 Uint32 rmask = 0x00ff0000;
957 Uint32 gmask = 0x0000ff00;
958 Uint32 bmask = 0x000000ff;
959
960 if ( ! SDL_PublicSurface ) {
961 SDL_SetError("No video mode has been set");
962 return(NULL);
963 }
964 vf = SDL_PublicSurface->format;
965
966 switch(vf->BytesPerPixel) {
967 case 2:
968 /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
969 For anything else (like ARGB4444) it doesn't matter
970 since we have no special code for it anyway */
971 if ( (vf->Rmask == 0x1f) &&
972 (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
973 rmask = 0xff;
974 bmask = 0xff0000;
975 }
976 break;
977
978 case 3:
979 case 4:
980 /* Keep the video format, as long as the high 8 bits are
981 unused or alpha */
982 if ( (vf->Rmask == 0xff) && (vf->Bmask == 0xff0000) ) {
983 rmask = 0xff;
984 bmask = 0xff0000;
985 }
986 break;
987
988 default:
989 /* We have no other optimised formats right now. When/if a new
990 optimised alpha format is written, add the converter here */
991 break;
992 }
993 format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
994 flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
995 flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
996 converted = SDL_ConvertSurface(surface, format, flags);
997 SDL_FreeFormat(format);
998 return(converted);
999}
1000
1001/*
1002 * Update a specific portion of the physical screen
1003 */
1004void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
1005{
1006 if ( screen ) {
1007 SDL_Rect rect;
1008
1009 /* Perform some checking */
1010 if ( w == 0 )
1011 w = screen->w;
1012 if ( h == 0 )
1013 h = screen->h;
1014 if ( (int)(x+w) > screen->w )
1015 return;
1016 if ( (int)(y+h) > screen->h )
1017 return;
1018
1019 /* Fill the rectangle */
1020 rect.x = (Sint16)x;
1021 rect.y = (Sint16)y;
1022 rect.w = (Uint16)w;
1023 rect.h = (Uint16)h;
1024 SDL_UpdateRects(screen, 1, &rect);
1025 }
1026}
1027void SDL_UpdateRects (SDL_Surface *screen, int numrects, SDL_Rect *rects)
1028{
1029 int i;
1030 SDL_VideoDevice *video = current_video;
1031 SDL_VideoDevice *this = current_video;
1032
1033 if ( (screen->flags & (SDL_OPENGL | SDL_OPENGLBLIT)) == SDL_OPENGL ) {
1034 SDL_SetError("OpenGL active, use SDL_GL_SwapBuffers()");
1035 return;
1036 }
1037 if ( screen == SDL_ShadowSurface ) {
1038 /* Blit the shadow surface using saved mapping */
1039 SDL_Palette *pal = screen->format->palette;
1040 SDL_Color *saved_colors = NULL;
1041 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1042 /* simulated 8bpp, use correct physical palette */
1043 saved_colors = pal->colors;
1044 if ( video->gammacols ) {
1045 /* gamma-corrected palette */
1046 pal->colors = video->gammacols;
1047 } else if ( video->physpal ) {
1048 /* physical palette different from logical */
1049 pal->colors = video->physpal->colors;
1050 }
1051 }
1052 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1053 SDL_LockCursor();
1054 SDL_DrawCursor(SDL_ShadowSurface);
1055 for ( i=0; i<numrects; ++i ) {
1056 SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
1057 SDL_VideoSurface, &rects[i]);
1058 }
1059 SDL_EraseCursor(SDL_ShadowSurface);
1060 SDL_UnlockCursor();
1061 } else {
1062 for ( i=0; i<numrects; ++i ) {
1063 SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
1064 SDL_VideoSurface, &rects[i]);
1065 }
1066 }
1067 if ( saved_colors ) {
1068 pal->colors = saved_colors;
1069 }
1070
1071 /* Fall through to video surface update */
1072 screen = SDL_VideoSurface;
1073 }
1074 if ( screen == SDL_VideoSurface ) {
1075 /* Update the video surface */
1076 if ( screen->offset ) {
1077 for ( i=0; i<numrects; ++i ) {
1078 rects[i].x += video->offset_x;
1079 rects[i].y += video->offset_y;
1080 }
1081 video->UpdateRects(this, numrects, rects);
1082 for ( i=0; i<numrects; ++i ) {
1083 rects[i].x -= video->offset_x;
1084 rects[i].y -= video->offset_y;
1085 }
1086 } else {
1087 video->UpdateRects(this, numrects, rects);
1088 }
1089 }
1090}
1091
1092/*
1093 * Performs hardware double buffering, if possible, or a full update if not.
1094 */
1095int SDL_Flip(SDL_Surface *screen)
1096{
1097 SDL_VideoDevice *video = current_video;
1098 /* Copy the shadow surface to the video surface */
1099 if ( screen == SDL_ShadowSurface ) {
1100 SDL_Rect rect;
1101 SDL_Palette *pal = screen->format->palette;
1102 SDL_Color *saved_colors = NULL;
1103 if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
1104 /* simulated 8bpp, use correct physical palette */
1105 saved_colors = pal->colors;
1106 if ( video->gammacols ) {
1107 /* gamma-corrected palette */
1108 pal->colors = video->gammacols;
1109 } else if ( video->physpal ) {
1110 /* physical palette different from logical */
1111 pal->colors = video->physpal->colors;
1112 }
1113 }
1114
1115 rect.x = 0;
1116 rect.y = 0;
1117 rect.w = screen->w;
1118 rect.h = screen->h;
1119 if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
1120 SDL_LockCursor();
1121 SDL_DrawCursor(SDL_ShadowSurface);
1122 SDL_LowerBlit(SDL_ShadowSurface, &rect,
1123 SDL_VideoSurface, &rect);
1124 SDL_EraseCursor(SDL_ShadowSurface);
1125 SDL_UnlockCursor();
1126 } else {
1127 SDL_LowerBlit(SDL_ShadowSurface, &rect,
1128 SDL_VideoSurface, &rect);
1129 }
1130 if ( saved_colors ) {
1131 pal->colors = saved_colors;
1132 }
1133
1134 /* Fall through to video surface update */
1135 screen = SDL_VideoSurface;
1136 }
1137 if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
1138 SDL_VideoDevice *this = current_video;
1139 return(video->FlipHWSurface(this, SDL_VideoSurface));
1140 } else {
1141 SDL_UpdateRect(screen, 0, 0, 0, 0);
1142 }
1143 return(0);
1144}
1145
1146static void SetPalette_logical(SDL_Surface *screen, SDL_Color *colors,
1147 int firstcolor, int ncolors)
1148{
1149 SDL_Palette *pal = screen->format->palette;
1150 SDL_Palette *vidpal;
1151
1152 if ( colors != (pal->colors + firstcolor) ) {
1153 SDL_memcpy(pal->colors + firstcolor, colors,
1154 ncolors * sizeof(*colors));
1155 }
1156
1157 if ( current_video && SDL_VideoSurface ) {
1158 vidpal = SDL_VideoSurface->format->palette;
1159 if ( (screen == SDL_ShadowSurface) && vidpal ) {
1160 /*
1161 * This is a shadow surface, and the physical
1162 * framebuffer is also indexed. Propagate the
1163 * changes to its logical palette so that
1164 * updates are always identity blits
1165 */
1166 SDL_memcpy(vidpal->colors + firstcolor, colors,
1167 ncolors * sizeof(*colors));
1168 }
1169 }
1170 SDL_FormatChanged(screen);
1171}
1172
1173static int SetPalette_physical(SDL_Surface *screen,
1174 SDL_Color *colors, int firstcolor, int ncolors)
1175{
1176 SDL_VideoDevice *video = current_video;
1177 int gotall = 1;
1178
1179 if ( video->physpal ) {
1180 /* We need to copy the new colors, since we haven't
1181 * already done the copy in the logical set above.
1182 */
1183 SDL_memcpy(video->physpal->colors + firstcolor,
1184 colors, ncolors * sizeof(*colors));
1185 }
1186 if ( screen == SDL_ShadowSurface ) {
1187 if ( SDL_VideoSurface->flags & SDL_HWPALETTE ) {
1188 /*
1189 * The real screen is also indexed - set its physical
1190 * palette. The physical palette does not include the
1191 * gamma modification, we apply it directly instead,
1192 * but this only happens if we have hardware palette.
1193 */
1194 screen = SDL_VideoSurface;
1195 } else {
1196 /*
1197 * The video surface is not indexed - invalidate any
1198 * active shadow-to-video blit mappings.
1199 */
1200 if ( screen->map->dst == SDL_VideoSurface ) {
1201 SDL_InvalidateMap(screen->map);
1202 }
1203 if ( video->gamma ) {
1204 if( ! video->gammacols ) {
1205 SDL_Palette *pp = video->physpal;
1206 if(!pp)
1207 pp = screen->format->palette;
1208 video->gammacols = SDL_malloc(pp->ncolors
1209 * sizeof(SDL_Color));
1210 SDL_ApplyGamma(video->gamma,
1211 pp->colors,
1212 video->gammacols,
1213 pp->ncolors);
1214 } else {
1215 SDL_ApplyGamma(video->gamma, colors,
1216 video->gammacols
1217 + firstcolor,
1218 ncolors);
1219 }
1220 }
1221 SDL_UpdateRect(screen, 0, 0, 0, 0);
1222 }
1223 }
1224
1225 if ( screen == SDL_VideoSurface ) {
1226 SDL_Color gcolors[256];
1227
1228 if ( video->gamma ) {
1229 SDL_ApplyGamma(video->gamma, colors, gcolors, ncolors);
1230 colors = gcolors;
1231 }
1232 gotall = video->SetColors(video, firstcolor, ncolors, colors);
1233 if ( ! gotall ) {
1234 /* The video flags shouldn't have SDL_HWPALETTE, and
1235 the video driver is responsible for copying back the
1236 correct colors into the video surface palette.
1237 */
1238 ;
1239 }
1240 SDL_CursorPaletteChanged();
1241 }
1242 return gotall;
1243}
1244
1245/*
1246 * Set the physical and/or logical colormap of a surface:
1247 * Only the screen has a physical colormap. It determines what is actually
1248 * sent to the display.
1249 * The logical colormap is used to map blits to/from the surface.
1250 * 'which' is one or both of SDL_LOGPAL, SDL_PHYSPAL
1251 *
1252 * Return nonzero if all colours were set as requested, or 0 otherwise.
1253 */
1254int SDL_SetPalette(SDL_Surface *screen, int which,
1255 SDL_Color *colors, int firstcolor, int ncolors)
1256{
1257 SDL_Palette *pal;
1258 int gotall;
1259 int palsize;
1260
1261 if ( !screen ) {
1262 return 0;
1263 }
1264 if ( !current_video || screen != SDL_PublicSurface ) {
1265 /* only screens have physical palettes */
1266 which &= ~SDL_PHYSPAL;
1267 } else if ( (screen->flags & SDL_HWPALETTE) != SDL_HWPALETTE ) {
1268 /* hardware palettes required for split colormaps */
1269 which |= SDL_PHYSPAL | SDL_LOGPAL;
1270 }
1271
1272 /* Verify the parameters */
1273 pal = screen->format->palette;
1274 if( !pal ) {
1275 return 0; /* not a palettized surface */
1276 }
1277 gotall = 1;
1278 palsize = 1 << screen->format->BitsPerPixel;
1279 if ( ncolors > (palsize - firstcolor) ) {
1280 ncolors = (palsize - firstcolor);
1281 gotall = 0;
1282 }
1283
1284 if ( which & SDL_LOGPAL ) {
1285 /*
1286 * Logical palette change: The actual screen isn't affected,
1287 * but the internal colormap is altered so that the
1288 * interpretation of the pixel values (for blits etc) is
1289 * changed.
1290 */
1291 SetPalette_logical(screen, colors, firstcolor, ncolors);
1292 }
1293 if ( which & SDL_PHYSPAL ) {
1294 SDL_VideoDevice *video = current_video;
1295 /*
1296 * Physical palette change: This doesn't affect the
1297 * program's idea of what the screen looks like, but changes
1298 * its actual appearance.
1299 */
1300 if ( !video->physpal && !(which & SDL_LOGPAL) ) {
1301 /* Lazy physical palette allocation */
1302 int size;
1303 SDL_Palette *pp = SDL_malloc(sizeof(*pp));
1304 if ( !pp ) {
1305 return 0;
1306 }
1307 video->physpal = pp;
1308 pp->ncolors = pal->ncolors;
1309 size = pp->ncolors * sizeof(SDL_Color);
1310 pp->colors = SDL_malloc(size);
1311 if ( !pp->colors ) {
1312 return 0;
1313 }
1314 SDL_memcpy(pp->colors, pal->colors, size);
1315 }
1316 if ( ! SetPalette_physical(screen,
1317 colors, firstcolor, ncolors) ) {
1318 gotall = 0;
1319 }
1320 }
1321 return gotall;
1322}
1323
1324int SDL_SetColors(SDL_Surface *screen, SDL_Color *colors, int firstcolor,
1325 int ncolors)
1326{
1327 return SDL_SetPalette(screen, SDL_LOGPAL | SDL_PHYSPAL,
1328 colors, firstcolor, ncolors);
1329}
1330
1331/*
1332 * Clean up the video subsystem
1333 */
1334void SDL_VideoQuit (void)
1335{
1336 SDL_Surface *ready_to_go;
1337
1338 if ( current_video ) {
1339 SDL_VideoDevice *video = current_video;
1340 SDL_VideoDevice *this = current_video;
1341
1342 /* Halt event processing before doing anything else */
1343 SDL_StopEventLoop();
1344
1345 /* Clean up allocated window manager items */
1346 if ( SDL_PublicSurface ) {
1347 SDL_PublicSurface = NULL;
1348 }
1349 SDL_CursorQuit();
1350
1351 /* Just in case... */
1352 SDL_WM_GrabInputOff();
1353
1354 /* Clean up the system video */
1355 video->VideoQuit(this);
1356
1357 /* Free any lingering surfaces */
1358 ready_to_go = SDL_ShadowSurface;
1359 SDL_ShadowSurface = NULL;
1360 SDL_FreeSurface(ready_to_go);
1361 if ( SDL_VideoSurface != NULL ) {
1362 ready_to_go = SDL_VideoSurface;
1363 SDL_VideoSurface = NULL;
1364 SDL_FreeSurface(ready_to_go);
1365 }
1366 SDL_PublicSurface = NULL;
1367
1368 /* Clean up miscellaneous memory */
1369 if ( video->physpal ) {
1370 SDL_free(video->physpal->colors);
1371 SDL_free(video->physpal);
1372 video->physpal = NULL;
1373 }
1374 if ( video->gammacols ) {
1375 SDL_free(video->gammacols);
1376 video->gammacols = NULL;
1377 }
1378 if ( video->gamma ) {
1379 SDL_free(video->gamma);
1380 video->gamma = NULL;
1381 }
1382 if ( video->wm_title != NULL ) {
1383 SDL_free(video->wm_title);
1384 video->wm_title = NULL;
1385 }
1386 if ( video->wm_icon != NULL ) {
1387 SDL_free(video->wm_icon);
1388 video->wm_icon = NULL;
1389 }
1390
1391 /* Finish cleaning up video subsystem */
1392 video->free(this);
1393 current_video = NULL;
1394 }
1395 return;
1396}
1397
1398/* Load the GL driver library */
1399int SDL_GL_LoadLibrary(const char *path)
1400{
1401 SDL_VideoDevice *video = current_video;
1402 SDL_VideoDevice *this = current_video;
1403 int retval;
1404
1405 retval = -1;
1406 if ( video == NULL ) {
1407 SDL_SetError("Video subsystem has not been initialized");
1408 } else {
1409 if ( video->GL_LoadLibrary ) {
1410 retval = video->GL_LoadLibrary(this, path);
1411 } else {
1412 SDL_SetError("No dynamic GL support in video driver");
1413 }
1414 }
1415 return(retval);
1416}
1417
1418void *SDL_GL_GetProcAddress(const char* proc)
1419{
1420 SDL_VideoDevice *video = current_video;
1421 SDL_VideoDevice *this = current_video;
1422 void *func;
1423
1424 func = NULL;
1425 if ( video->GL_GetProcAddress ) {
1426 if ( video->gl_config.driver_loaded ) {
1427 func = video->GL_GetProcAddress(this, proc);
1428 } else {
1429 SDL_SetError("No GL driver has been loaded");
1430 }
1431 } else {
1432 SDL_SetError("No dynamic GL support in video driver");
1433 }
1434 return func;
1435}
1436
1437/* Set the specified GL attribute for setting up a GL video mode */
1438int SDL_GL_SetAttribute( SDL_GLattr attr, int value )
1439{
1440 int retval;
1441 SDL_VideoDevice *video = current_video;
1442
1443 retval = 0;
1444 switch (attr) {
1445 case SDL_GL_RED_SIZE:
1446 video->gl_config.red_size = value;
1447 break;
1448 case SDL_GL_GREEN_SIZE:
1449 video->gl_config.green_size = value;
1450 break;
1451 case SDL_GL_BLUE_SIZE:
1452 video->gl_config.blue_size = value;
1453 break;
1454 case SDL_GL_ALPHA_SIZE:
1455 video->gl_config.alpha_size = value;
1456 break;
1457 case SDL_GL_DOUBLEBUFFER:
1458 video->gl_config.double_buffer = value;
1459 break;
1460 case SDL_GL_BUFFER_SIZE:
1461 video->gl_config.buffer_size = value;
1462 break;
1463 case SDL_GL_DEPTH_SIZE:
1464 video->gl_config.depth_size = value;
1465 break;
1466 case SDL_GL_STENCIL_SIZE:
1467 video->gl_config.stencil_size = value;
1468 break;
1469 case SDL_GL_ACCUM_RED_SIZE:
1470 video->gl_config.accum_red_size = value;
1471 break;
1472 case SDL_GL_ACCUM_GREEN_SIZE:
1473 video->gl_config.accum_green_size = value;
1474 break;
1475 case SDL_GL_ACCUM_BLUE_SIZE:
1476 video->gl_config.accum_blue_size = value;
1477 break;
1478 case SDL_GL_ACCUM_ALPHA_SIZE:
1479 video->gl_config.accum_alpha_size = value;
1480 break;
1481 case SDL_GL_STEREO:
1482 video->gl_config.stereo = value;
1483 break;
1484 case SDL_GL_MULTISAMPLEBUFFERS:
1485 video->gl_config.multisamplebuffers = value;
1486 break;
1487 case SDL_GL_MULTISAMPLESAMPLES:
1488 video->gl_config.multisamplesamples = value;
1489 break;
1490 case SDL_GL_ACCELERATED_VISUAL:
1491 video->gl_config.accelerated = value;
1492 break;
1493 case SDL_GL_SWAP_CONTROL:
1494 video->gl_config.swap_control = value;
1495 break;
1496 default:
1497 SDL_SetError("Unknown OpenGL attribute");
1498 retval = -1;
1499 break;
1500 }
1501 return(retval);
1502}
1503
1504/* Retrieve an attribute value from the windowing system. */
1505int SDL_GL_GetAttribute(SDL_GLattr attr, int* value)
1506{
1507 int retval = -1;
1508 SDL_VideoDevice* video = current_video;
1509 SDL_VideoDevice* this = current_video;
1510
1511 if ( video->GL_GetAttribute ) {
1512 retval = this->GL_GetAttribute(this, attr, value);
1513 } else {
1514 *value = 0;
1515 SDL_SetError("GL_GetAttribute not supported");
1516 }
1517 return retval;
1518}
1519
1520/* Perform a GL buffer swap on the current GL context */
1521void SDL_GL_SwapBuffers(void)
1522{
1523 SDL_VideoDevice *video = current_video;
1524 SDL_VideoDevice *this = current_video;
1525
1526 if ( video->screen->flags & SDL_OPENGL ) {
1527 video->GL_SwapBuffers(this);
1528 } else {
1529 SDL_SetError("OpenGL video mode has not been set");
1530 }
1531}
1532
1533/* Update rects with locking */
1534void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect *rects)
1535{
1536 SDL_GL_Lock();
1537 SDL_GL_UpdateRects(numrects, rects);
1538 SDL_GL_Unlock();
1539}
1540
1541/* Update rects without state setting and changing (the caller is responsible for it) */
1542void SDL_GL_UpdateRects(int numrects, SDL_Rect *rects)
1543{
1544#if SDL_VIDEO_OPENGL
1545 SDL_VideoDevice *this = current_video;
1546 SDL_Rect update, tmp;
1547 int x, y, i;
1548
1549 for ( i = 0; i < numrects; i++ )
1550 {
1551 tmp.y = rects[i].y;
1552 tmp.h = rects[i].h;
1553 for ( y = 0; y <= rects[i].h / 256; y++ )
1554 {
1555 tmp.x = rects[i].x;
1556 tmp.w = rects[i].w;
1557 for ( x = 0; x <= rects[i].w / 256; x++ )
1558 {
1559 update.x = tmp.x;
1560 update.y = tmp.y;
1561 update.w = tmp.w;
1562 update.h = tmp.h;
1563
1564 if ( update.w > 256 )
1565 update.w = 256;
1566
1567 if ( update.h > 256 )
1568 update.h = 256;
1569
1570 this->glFlush();
1571 this->glTexSubImage2D(
1572 GL_TEXTURE_2D,
1573 0,
1574 0,
1575 0,
1576 update.w,
1577 update.h,
1578 this->is_32bit? GL_RGBA : GL_RGB,
1579#ifdef GL_VERSION_1_2
1580 this->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5,
1581#else
1582 GL_UNSIGNED_BYTE,
1583#endif
1584 (Uint8 *)this->screen->pixels +
1585 this->screen->format->BytesPerPixel * update.x +
1586 update.y * this->screen->pitch );
1587
1588 this->glFlush();
1589 /*
1590 * Note the parens around the function name:
1591 * This is because some OpenGL implementations define glTexCoord etc
1592 * as macros, and we don't want them expanded here.
1593 */
1594 this->glBegin(GL_TRIANGLE_STRIP);
1595 (this->glTexCoord2f)( 0.0, 0.0 );
1596 (this->glVertex2i)( update.x, update.y );
1597 (this->glTexCoord2f)( (float)(update.w / 256.0), 0.0 );
1598 (this->glVertex2i)( update.x + update.w, update.y );
1599 (this->glTexCoord2f)( 0.0, (float)(update.h / 256.0) );
1600 (this->glVertex2i)( update.x, update.y + update.h );
1601 (this->glTexCoord2f)( (float)(update.w / 256.0), (float)(update.h / 256.0) );
1602 (this->glVertex2i)( update.x + update.w , update.y + update.h );
1603 this->glEnd();
1604
1605 tmp.x += 256;
1606 tmp.w -= 256;
1607 }
1608 tmp.y += 256;
1609 tmp.h -= 256;
1610 }
1611 }
1612#endif
1613}
1614
1615/* Lock == save current state */
1616void SDL_GL_Lock()
1617{
1618#if SDL_VIDEO_OPENGL
1619 lock_count--;
1620 if (lock_count==-1)
1621 {
1622 SDL_VideoDevice *this = current_video;
1623
1624 this->glPushAttrib( GL_ALL_ATTRIB_BITS ); /* TODO: narrow range of what is saved */
1625#ifdef GL_CLIENT_PIXEL_STORE_BIT
1626 this->glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
1627#endif
1628
1629 this->glEnable(GL_TEXTURE_2D);
1630 this->glEnable(GL_BLEND);
1631 this->glDisable(GL_FOG);
1632 this->glDisable(GL_ALPHA_TEST);
1633 this->glDisable(GL_DEPTH_TEST);
1634 this->glDisable(GL_SCISSOR_TEST);
1635 this->glDisable(GL_STENCIL_TEST);
1636 this->glDisable(GL_CULL_FACE);
1637
1638 this->glBindTexture( GL_TEXTURE_2D, this->texture );
1639 this->glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
1640 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
1641 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
1642 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
1643 this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
1644
1645 this->glPixelStorei( GL_UNPACK_ROW_LENGTH, this->screen->pitch / this->screen->format->BytesPerPixel );
1646 this->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1647 (this->glColor4f)(1.0, 1.0, 1.0, 1.0); /* Solaris workaround */
1648
1649 this->glViewport(0, 0, this->screen->w, this->screen->h);
1650 this->glMatrixMode(GL_PROJECTION);
1651 this->glPushMatrix();
1652 this->glLoadIdentity();
1653
1654 this->glOrtho(0.0, (GLdouble) this->screen->w, (GLdouble) this->screen->h, 0.0, 0.0, 1.0);
1655
1656 this->glMatrixMode(GL_MODELVIEW);
1657 this->glPushMatrix();
1658 this->glLoadIdentity();
1659 }
1660#endif
1661}
1662
1663/* Unlock == restore saved state */
1664void SDL_GL_Unlock()
1665{
1666#if SDL_VIDEO_OPENGL
1667 lock_count++;
1668 if (lock_count==0)
1669 {
1670 SDL_VideoDevice *this = current_video;
1671
1672 this->glPopMatrix();
1673 this->glMatrixMode(GL_PROJECTION);
1674 this->glPopMatrix();
1675
1676 this->glPopClientAttrib();
1677 this->glPopAttrib();
1678 }
1679#endif
1680}
1681
1682/*
1683 * Sets/Gets the title and icon text of the display window, if any.
1684 */
1685void SDL_WM_SetCaption (const char *title, const char *icon)
1686{
1687 SDL_VideoDevice *video = current_video;
1688 SDL_VideoDevice *this = current_video;
1689
1690 if ( video ) {
1691 if ( title ) {
1692 if ( video->wm_title ) {
1693 SDL_free(video->wm_title);
1694 }
1695 video->wm_title = SDL_strdup(title);
1696 }
1697 if ( icon ) {
1698 if ( video->wm_icon ) {
1699 SDL_free(video->wm_icon);
1700 }
1701 video->wm_icon = SDL_strdup(icon);
1702 }
1703 if ( (title || icon) && (video->SetCaption != NULL) ) {
1704 video->SetCaption(this, video->wm_title,video->wm_icon);
1705 }
1706 }
1707}
1708void SDL_WM_GetCaption (char **title, char **icon)
1709{
1710 SDL_VideoDevice *video = current_video;
1711
1712 if ( video ) {
1713 if ( title ) {
1714 *title = video->wm_title;
1715 }
1716 if ( icon ) {
1717 *icon = video->wm_icon;
1718 }
1719 }
1720}
1721
1722/* Utility function used by SDL_WM_SetIcon();
1723 * flags & 1 for color key, flags & 2 for alpha channel. */
1724static void CreateMaskFromColorKeyOrAlpha(SDL_Surface *icon, Uint8 *mask, int flags)
1725{
1726 int x, y;
1727 Uint32 colorkey;
1728#define SET_MASKBIT(icon, x, y, mask) \
1729 mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8)))
1730
1731 colorkey = icon->format->colorkey;
1732 switch (icon->format->BytesPerPixel) {
1733 case 1: { Uint8 *pixels;
1734 for ( y=0; y<icon->h; ++y ) {
1735 pixels = (Uint8 *)icon->pixels + y*icon->pitch;
1736 for ( x=0; x<icon->w; ++x ) {
1737 if ( *pixels++ == colorkey ) {
1738 SET_MASKBIT(icon, x, y, mask);
1739 }
1740 }
1741 }
1742 }
1743 break;
1744
1745 case 2: { Uint16 *pixels;
1746 for ( y=0; y<icon->h; ++y ) {
1747 pixels = (Uint16 *)icon->pixels +
1748 y*icon->pitch/2;
1749 for ( x=0; x<icon->w; ++x ) {
1750 if ( (flags & 1) && *pixels == colorkey ) {
1751 SET_MASKBIT(icon, x, y, mask);
1752 } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1753 SET_MASKBIT(icon, x, y, mask);
1754 }
1755 pixels++;
1756 }
1757 }
1758 }
1759 break;
1760
1761 case 4: { Uint32 *pixels;
1762 for ( y=0; y<icon->h; ++y ) {
1763 pixels = (Uint32 *)icon->pixels +
1764 y*icon->pitch/4;
1765 for ( x=0; x<icon->w; ++x ) {
1766 if ( (flags & 1) && *pixels == colorkey ) {
1767 SET_MASKBIT(icon, x, y, mask);
1768 } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) {
1769 SET_MASKBIT(icon, x, y, mask);
1770 }
1771 pixels++;
1772 }
1773 }
1774 }
1775 break;
1776 }
1777}
1778
1779/*
1780 * Sets the window manager icon for the display window.
1781 */
1782void SDL_WM_SetIcon (SDL_Surface *icon, Uint8 *mask)
1783{
1784 SDL_VideoDevice *video = current_video;
1785 SDL_VideoDevice *this = current_video;
1786
1787 if ( icon && video->SetIcon ) {
1788 /* Generate a mask if necessary, and create the icon! */
1789 if ( mask == NULL ) {
1790 int mask_len = icon->h*(icon->w+7)/8;
1791 int flags = 0;
1792 mask = (Uint8 *)SDL_malloc(mask_len);
1793 if ( mask == NULL ) {
1794 return;
1795 }
1796 SDL_memset(mask, ~0, mask_len);
1797 if ( icon->flags & SDL_SRCCOLORKEY ) flags |= 1;
1798 if ( icon->flags & SDL_SRCALPHA ) flags |= 2;
1799 if( flags ) {
1800 CreateMaskFromColorKeyOrAlpha(icon, mask, flags);
1801 }
1802 video->SetIcon(video, icon, mask);
1803 SDL_free(mask);
1804 } else {
1805 video->SetIcon(this, icon, mask);
1806 }
1807 }
1808}
1809
1810/*
1811 * Grab or ungrab the keyboard and mouse input.
1812 * This function returns the final grab mode after calling the
1813 * driver dependent function.
1814 */
1815static SDL_GrabMode SDL_WM_GrabInputRaw(SDL_GrabMode mode)
1816{
1817 SDL_VideoDevice *video = current_video;
1818 SDL_VideoDevice *this = current_video;
1819
1820 /* Only do something if we have support for grabs */
1821 if ( video->GrabInput == NULL ) {
1822 return(video->input_grab);
1823 }
1824
1825 /* If the final grab mode if off, only then do we actually grab */
1826#ifdef DEBUG_GRAB
1827 printf("SDL_WM_GrabInputRaw(%d) ... ", mode);
1828#endif
1829 if ( mode == SDL_GRAB_OFF ) {
1830 if ( video->input_grab != SDL_GRAB_OFF ) {
1831 mode = video->GrabInput(this, mode);
1832 }
1833 } else {
1834 if ( video->input_grab == SDL_GRAB_OFF ) {
1835 mode = video->GrabInput(this, mode);
1836 }
1837 }
1838 if ( mode != video->input_grab ) {
1839 video->input_grab = mode;
1840 if ( video->CheckMouseMode ) {
1841 video->CheckMouseMode(this);
1842 }
1843 }
1844#ifdef DEBUG_GRAB
1845 printf("Final mode %d\n", video->input_grab);
1846#endif
1847
1848 /* Return the final grab state */
1849 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1850 mode -= SDL_GRAB_FULLSCREEN;
1851 }
1852 return(mode);
1853}
1854SDL_GrabMode SDL_WM_GrabInput(SDL_GrabMode mode)
1855{
1856 SDL_VideoDevice *video = current_video;
1857
1858 /* If the video isn't initialized yet, we can't do anything */
1859 if ( ! video ) {
1860 return SDL_GRAB_OFF;
1861 }
1862
1863 /* Return the current mode on query */
1864 if ( mode == SDL_GRAB_QUERY ) {
1865 mode = video->input_grab;
1866 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1867 mode -= SDL_GRAB_FULLSCREEN;
1868 }
1869 return(mode);
1870 }
1871
1872#ifdef DEBUG_GRAB
1873 printf("SDL_WM_GrabInput(%d) ... ", mode);
1874#endif
1875 /* If the video surface is fullscreen, we always grab */
1876 if ( mode >= SDL_GRAB_FULLSCREEN ) {
1877 mode -= SDL_GRAB_FULLSCREEN;
1878 }
1879 if ( SDL_VideoSurface && (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) {
1880 mode += SDL_GRAB_FULLSCREEN;
1881 }
1882 return(SDL_WM_GrabInputRaw(mode));
1883}
1884static SDL_GrabMode SDL_WM_GrabInputOff(void)
1885{
1886 SDL_GrabMode mode;
1887
1888 /* First query the current grab state */
1889 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
1890
1891 /* Now explicitly turn off input grab */
1892 SDL_WM_GrabInputRaw(SDL_GRAB_OFF);
1893
1894 /* Return the old state */
1895 return(mode);
1896}
1897
1898/*
1899 * Iconify the window in window managed environments.
1900 * A successful iconification will result in an SDL_APPACTIVE loss event.
1901 */
1902int SDL_WM_IconifyWindow(void)
1903{
1904 SDL_VideoDevice *video = current_video;
1905 SDL_VideoDevice *this = current_video;
1906 int retval;
1907
1908 retval = 0;
1909 if ( video->IconifyWindow ) {
1910 retval = video->IconifyWindow(this);
1911 }
1912 return(retval);
1913}
1914
1915/*
1916 * Toggle fullscreen mode
1917 */
1918int SDL_WM_ToggleFullScreen(SDL_Surface *surface)
1919{
1920 SDL_VideoDevice *video = current_video;
1921 SDL_VideoDevice *this = current_video;
1922 int toggled;
1923
1924 toggled = 0;
1925 if ( SDL_PublicSurface && (surface == SDL_PublicSurface) &&
1926 video->ToggleFullScreen ) {
1927 if ( surface->flags & SDL_FULLSCREEN ) {
1928 toggled = video->ToggleFullScreen(this, 0);
1929 if ( toggled ) {
1930 SDL_VideoSurface->flags &= ~SDL_FULLSCREEN;
1931 SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
1932 }
1933 } else {
1934 toggled = video->ToggleFullScreen(this, 1);
1935 if ( toggled ) {
1936 SDL_VideoSurface->flags |= SDL_FULLSCREEN;
1937 SDL_PublicSurface->flags |= SDL_FULLSCREEN;
1938 }
1939 }
1940 /* Double-check the grab state inside SDL_WM_GrabInput() */
1941 if ( toggled ) {
1942 SDL_WM_GrabInput(video->input_grab);
1943 }
1944 }
1945 return(toggled);
1946}
1947
1948/*
1949 * Get some platform dependent window manager information
1950 */
1951int SDL_GetWMInfo (SDL_SysWMinfo *info)
1952{
1953 SDL_VideoDevice *video = current_video;
1954 SDL_VideoDevice *this = current_video;
1955
1956 if ( video && video->GetWMInfo ) {
1957 return(video->GetWMInfo(this, info));
1958 } else {
1959 return(0);
1960 }
1961}