Mercurial > sdl-ios-xcode
annotate src/video/photon/SDL_ph_video.c @ 156:7064032cbe77
Is this right??
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 20 Aug 2001 00:10:35 +0000 |
parents | 8cc4dbfab9ab |
children | 62bad9a82022 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 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 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@devolution.com | |
21 */ | |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 #include <stdlib.h> | |
29 #include <stdio.h> | |
30 #include <unistd.h> | |
31 #include <string.h> | |
32 #include <sys/ioctl.h> | |
33 | |
34 #include "SDL.h" | |
35 #include "SDL_error.h" | |
36 #include "SDL_timer.h" | |
37 #include "SDL_thread.h" | |
38 #include "SDL_video.h" | |
39 #include "SDL_mouse.h" | |
40 #include "SDL_endian.h" | |
41 #include "SDL_sysvideo.h" | |
42 #include "SDL_pixels_c.h" | |
43 #include "SDL_events_c.h" | |
44 #include "SDL_ph_video.h" | |
45 #include "SDL_ph_modes_c.h" | |
46 #include "SDL_ph_image_c.h" | |
47 #include "SDL_ph_events_c.h" | |
48 #include "SDL_ph_mouse_c.h" | |
19
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
49 #include "SDL_ph_wm_c.h" |
0 | 50 #include "SDL_phyuv_c.h" |
51 #include "blank_cursor.h" | |
52 | |
53 static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat); | |
54 static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current, | |
55 int width, int height, int bpp, Uint32 flags); | |
56 static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors); | |
57 static void ph_VideoQuit(_THIS); | |
58 static void ph_DeleteDevice(SDL_VideoDevice *device); | |
59 | |
60 static int ph_Available(void) | |
61 { | |
62 | |
63 return 1; | |
64 } | |
65 | |
66 static SDL_VideoDevice *ph_CreateDevice(int devindex) | |
67 { | |
68 SDL_VideoDevice *device; | |
69 | |
70 /* Initialize all variables that we clean on shutdown */ | |
71 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice)); | |
72 if ( device ) { | |
73 memset(device, 0, (sizeof *device)); | |
74 device->hidden = (struct SDL_PrivateVideoData *) | |
75 malloc((sizeof *device->hidden)); | |
76 device->gl_data = NULL; | |
77 } | |
78 if ( (device == NULL) || (device->hidden == NULL) ) { | |
79 SDL_OutOfMemory(); | |
80 ph_DeleteDevice(device); | |
81 return(0); | |
82 } | |
83 memset(device->hidden, 0, (sizeof *device->hidden)); | |
84 | |
85 /* Set the driver flags */ | |
86 device->handles_any_size = 1; //JB not true for fullscreen | |
87 | |
88 /* Set the function pointers */ | |
89 device->CreateYUVOverlay = ph_CreateYUVOverlay; | |
90 device->VideoInit = ph_VideoInit; | |
91 device->ListModes = ph_ListModes; | |
92 device->SetVideoMode = ph_SetVideoMode; | |
93 device->ToggleFullScreen = ph_ToggleFullScreen; | |
94 device->UpdateMouse = NULL; | |
95 device->SetColors = ph_SetColors; | |
96 device->UpdateRects = NULL; //set in ph_ResizeImage | |
97 device->VideoQuit = ph_VideoQuit; | |
98 device->AllocHWSurface = ph_AllocHWSurface; | |
99 device->CheckHWBlit = NULL; | |
100 device->FillHWRect = NULL; | |
101 device->SetHWColorKey = NULL; | |
102 device->SetHWAlpha = NULL; | |
103 device->LockHWSurface = ph_LockHWSurface; | |
104 device->UnlockHWSurface = ph_UnlockHWSurface; | |
105 device->FlipHWSurface = ph_FlipHWSurface; | |
106 device->FreeHWSurface = ph_FreeHWSurface; | |
19
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
107 device->SetCaption = ph_SetCaption; |
0 | 108 device->SetIcon = NULL; |
19
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
109 device->IconifyWindow = ph_IconifyWindow; |
0 | 110 device->GrabInput = NULL; |
111 device->GetWMInfo = NULL; | |
112 device->FreeWMCursor = ph_FreeWMCursor; | |
113 device->CreateWMCursor = ph_CreateWMCursor; | |
114 device->ShowWMCursor = ph_ShowWMCursor; | |
115 device->WarpWMCursor = ph_WarpWMCursor; | |
116 device->CheckMouseMode = ph_CheckMouseMode; | |
117 device->InitOSKeymap = ph_InitOSKeymap; | |
118 device->PumpEvents = ph_PumpEvents; | |
119 | |
120 device->free = ph_DeleteDevice; | |
121 | |
122 return device; | |
123 } | |
124 | |
19
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
125 VideoBootStrap ph_bootstrap = { |
0 | 126 "photon", "QNX Photon video output", |
127 ph_Available, ph_CreateDevice | |
128 }; | |
129 | |
130 static void ph_DeleteDevice(SDL_VideoDevice *device) | |
131 { | |
132 | |
133 if ( device ) { | |
134 if ( device->hidden ) { | |
135 free(device->hidden); | |
136 device->hidden = NULL; | |
137 } | |
138 if ( device->gl_data ) { | |
139 free(device->gl_data); | |
140 device->gl_data = NULL; | |
141 } | |
142 free(device); | |
143 device = NULL; | |
144 } | |
145 } | |
146 | |
147 static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat) | |
148 { | |
149 PtArg_t arg[1]; | |
150 PhDim_t dim; | |
151 PgColor_t ph_palette[_Pg_MAX_PALETTE]; | |
152 int i; | |
153 unsigned long *tempptr; | |
154 int rtnval; | |
155 PgDisplaySettings_t mysettings; | |
156 PgVideoModeInfo_t my_mode_info; | |
157 | |
158 if( NULL == ( event = malloc( EVENT_SIZE ) ) ) | |
159 exit( EXIT_FAILURE ); | |
160 | |
161 /* Create a widget 'window' to capture events */ | |
162 dim.w=0; //JB test320; | |
163 dim.h=0; //JB test240; | |
164 //We need to return BytesPerPixel as it in used by CreateRGBsurface | |
165 | |
166 PtSetArg(&arg[0], Pt_ARG_DIM, &dim,0); | |
167 | |
168 /* | |
169 PtSetArg(&arg[1], Pt_ARG_RESIZE_FLAGS, Pt_TRUE, Pt_RESIZE_XY_AS_REQUIRED); | |
170 PtSetArg(&arg[2], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFRONT | | |
171 Ph_WM_STATE_ISMAX | | |
172 Ph_WM_STATE_ISFOCUS); | |
173 | |
174 PtSetArg(&arg[3], Pt_ARG_WINDOW_RENDER_FLAGS,Pt_FALSE,~0); | |
175 PtSetArg(&arg[4], Pt_ARG_WINDOW_MANAGED_FLAGS,Pt_TRUE, | |
176 Ph_WM_FFRONT | | |
177 Ph_WM_CLOSE | | |
178 Ph_WM_TOFRONT | | |
179 Ph_WM_CONSWITCH); | |
180 */ | |
181 | |
182 | |
183 window=PtAppInit(NULL, NULL, NULL, 1, arg); | |
184 | |
185 if(window == NULL) | |
186 { | |
19
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
187 printf("Photon application init failed, probably Photon is not running.\n"); |
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
188 exit( EXIT_FAILURE ); |
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
189 // PtExit(EXIT_FAILURE); // Got SEGFAULT. |
0 | 190 } |
191 | |
192 //PgSetDrawBufferSize(16 *1024); | |
193 PgSetRegion(PtWidgetRid(window)); | |
194 PgSetClipping(0,NULL); | |
195 PtRealizeWidget(window); | |
196 | |
197 | |
198 /* Get the available video modes */ | |
199 // if(ph_GetVideoModes(this) < 0) | |
200 // return -1; | |
201 | |
202 /* Create the blank cursor */ | |
203 SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask, | |
204 (int)BLANK_CWIDTH, (int)BLANK_CHEIGHT, | |
205 (int)BLANK_CHOTX, (int)BLANK_CHOTY); | |
206 | |
207 if(SDL_BlankCursor == NULL) | |
208 printf("could not create blank cursor\n"); | |
209 /* Get the video mode */ | |
210 if (PgGetVideoMode( &mysettings ) < 0) | |
211 { | |
212 fprintf(stderr,"ph_VideoInit: PgGetVideoMode failed\n"); | |
213 //QNX6/Patch A always fails return code even though call succeeds. fixed Patch B | |
214 } | |
215 | |
216 if (PgGetVideoModeInfo(mysettings.mode, &my_mode_info) < 0) | |
217 { | |
218 fprintf(stderr,"ph_VideoInit: PgGetVideoModeInfo failed\n"); | |
219 } | |
220 //We need to return BytesPerPixel as it in used by CreateRGBsurface | |
221 vformat->BitsPerPixel = my_mode_info.bits_per_pixel; | |
222 vformat->BytesPerPixel = vformat->BitsPerPixel/8; | |
223 | |
224 //return a palette if we are in 256 color mode | |
225 if(vformat->BitsPerPixel == 8) | |
226 { | |
227 vformat->palette = malloc(sizeof(SDL_Palette)); | |
228 memset(vformat->palette, 0, sizeof(SDL_Palette)); | |
229 vformat->palette->ncolors = 256; | |
230 vformat->palette->colors = (SDL_Color *)malloc(256 *sizeof(SDL_Color)); | |
231 | |
232 //fill the palette | |
233 rtnval = PgGetPalette(ph_palette); | |
234 if(rtnval < 0) | |
235 printf("ph_VideoInit: PgGetPalette failed\n"); | |
236 | |
237 tempptr = (unsigned long *)vformat->palette->colors; | |
238 | |
239 for(i=0;i<256; i++) | |
240 { | |
241 *tempptr = (((unsigned long)ph_palette[i]) << 8); | |
242 tempptr++; | |
243 | |
244 } | |
245 | |
246 } | |
247 | |
248 | |
19
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
249 currently_fullscreen = 0; |
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
250 |
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
251 this->info.wm_available = 1; |
8cc4dbfab9ab
Date: Thu, 19 Apr 2001 08:36:54 +0300
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
252 |
0 | 253 return 0; |
254 } | |
255 | |
256 static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current, | |
257 int width, int height, int bpp, Uint32 flags) | |
258 { | |
259 PhRegion_t region_info; | |
260 PgDisplaySettings_t settings; | |
261 PgVideoModeInfo_t mode_info; | |
262 int mode, actual_width, actual_height; | |
263 PtArg_t arg[5]; | |
264 PhDim_t dim; | |
265 int rtnval; | |
266 SDL_Rect ** mymodelist; | |
267 SDL_PixelFormat myformat; | |
268 PgColor_t ph_palette[_Pg_MAX_PALETTE]; | |
269 int i; | |
270 unsigned long *tempptr; | |
271 | |
272 actual_width = width; | |
273 actual_height = height; | |
274 | |
275 | |
276 /* Lock the event thread, in multi-threading environments */ | |
277 SDL_Lock_EventThread(); | |
278 | |
279 | |
280 /* Initialize the window */ | |
281 if (flags & SDL_FULLSCREEN) //Direct Context , assume SDL_HWSURFACE also set | |
282 { | |
283 | |
284 /* | |
285 if (old_video_mode==-1) | |
286 { | |
287 PgGetGraphicsHWCaps(&graphics_card_caps); | |
288 old_video_mode=graphics_card_caps.current_video_mode; | |
289 old_refresh_rate=graphics_card_caps.current_rrate; | |
290 } | |
291 */ | |
292 | |
293 | |
294 | |
295 /* Get the video mode and set it */ | |
296 if (bpp == 0) | |
297 { | |
298 if (PgGetVideoMode( &settings ) < 0) | |
299 { | |
300 fprintf(stderr,"error: PgGetVideoMode failed\n"); | |
301 } | |
302 if (PgGetVideoModeInfo(settings.mode, &mode_info) < 0) | |
303 { | |
304 fprintf(stderr,"error: PgGetVideoModeInfo failed\n"); | |
305 } | |
306 bpp = mode_info.bits_per_pixel; | |
307 } | |
308 if (flags & SDL_ANYFORMAT) | |
309 { | |
310 if ((mode = get_mode_any_format(width, height, bpp)) == 0) | |
311 { | |
312 fprintf(stderr,"error: get_mode_any_format failed\n"); | |
313 exit(1); | |
314 } | |
315 } | |
316 else | |
317 { | |
318 if ((mode = get_mode(width, height, bpp)) == 0) | |
319 { | |
320 fprintf(stderr,"error: get_mode failed\n"); | |
321 exit(1); | |
322 } | |
323 | |
324 | |
325 } | |
326 settings.mode = mode; | |
327 settings.refresh = 0; | |
328 settings.flags = 0; | |
329 | |
330 | |
331 if (PgSetVideoMode( &settings ) < 0) | |
332 { | |
333 fprintf(stderr,"error: PgSetVideoMode failed\n"); | |
334 } | |
335 | |
336 /* Get the true height and width */ | |
337 | |
338 current->flags = (flags|(~SDL_RESIZABLE)); //no resize for Direct Context | |
339 | |
340 /* Begin direct mode */ | |
341 ph_EnterFullScreen(this); | |
342 | |
343 | |
344 | |
345 } //end fullscreen flag | |
346 else if (flags & SDL_HWSURFACE) /* Use offscreen memory iff SDL_HWSURFACE flag is set */ | |
347 { | |
348 // Hardware surface is Offsceen Context. ph_ResizeImage handles the switch | |
349 current->flags = (flags|(~SDL_RESIZABLE)); //no stretch blit in offscreen context | |
350 } | |
351 else // must be SDL_SWSURFACE | |
352 { | |
353 current->flags = (flags|SDL_RESIZABLE); //yes we can resize as this is a software surface | |
354 } | |
355 | |
356 | |
357 //If we are setting video to use the palette make sure we have allocated memory for it | |
358 if(bpp == 8) | |
359 { | |
360 current->format->palette = malloc(sizeof(SDL_Palette)); | |
361 memset(current->format->palette, 0, sizeof(SDL_Palette)); | |
362 current->format->palette->ncolors = 256; | |
363 current->format->palette->colors = (SDL_Color *)malloc(256 *sizeof(SDL_Color)); | |
364 //fill the palette | |
365 rtnval = PgGetPalette(ph_palette); | |
366 | |
367 tempptr = (unsigned long *)current->format->palette->colors; | |
368 | |
369 for(i=0;i<256; i++) | |
370 { | |
371 *tempptr = (((unsigned long)ph_palette[i]) << 8); | |
372 tempptr++; | |
373 | |
374 } | |
375 } | |
376 | |
377 | |
378 //Current window dimensions | |
379 PtGetResource( window, Pt_ARG_DIM, &dim, 0 ); | |
380 | |
381 //If we need to resize the window | |
382 if((dim.w != width)||(dim.h != height)) | |
383 { | |
384 dim.w=width; | |
385 dim.h=height; | |
386 PtSetArg(&arg[0], Pt_ARG_DIM, &dim,0); | |
387 PtSetResources( window, 1, arg ); | |
388 current->w = width; | |
389 current->h = height; | |
390 current->format->BitsPerPixel = bpp; | |
391 current->format->BytesPerPixel = bpp/8; | |
392 current->pitch = SDL_CalculatePitch(current); | |
393 //Must call at least once it setup image planes | |
394 ph_ResizeImage(this, current, flags); | |
395 } | |
396 | |
397 | |
398 SDL_Unlock_EventThread(); | |
399 | |
400 /* We're done! */ | |
401 return(current); | |
402 } | |
403 | |
404 static void ph_VideoQuit(_THIS) | |
405 { | |
406 | |
407 if(SDL_Image != NULL) | |
408 { | |
409 ph_DestroyImage(this, SDL_VideoSurface); | |
410 | |
411 } | |
412 | |
413 if (currently_fullscreen) | |
414 { | |
415 PdDirectStop( directContext ); | |
416 PdReleaseDirectContext( directContext ); | |
417 directContext=0; | |
418 currently_fullscreen = 0; | |
419 } | |
420 | |
421 } | |
422 | |
423 | |
424 static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) | |
425 { | |
426 PgColor_t *in, *out; | |
427 int i, j; | |
428 int alloct_all = 1; | |
429 | |
430 colors = this->screen->format->palette->colors; | |
431 | |
432 in = alloca( ncolors*sizeof(PgColor_t) ); | |
433 if ( in == NULL ) { | |
434 return 0; | |
435 } | |
436 memset(in,0,ncolors*sizeof(PgColor_t)); | |
437 | |
438 out = alloca( ncolors*sizeof(PgColor_t) ); | |
439 if ( out == NULL ) { | |
440 return 0; | |
441 } | |
442 | |
443 for (i=0,j=firstcolor;i<ncolors;i++,j++) | |
444 { | |
445 in[i] |= colors[j].r<<16 ; | |
446 in[i] |= colors[j].g<<8 ; | |
447 in[i] |= colors[j].b ; | |
448 } | |
449 | |
450 if ( (this->screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) | |
451 { | |
452 if( PgSetPalette( in, 0, 0, ncolors, Pg_PALSET_HARD, 0) < 0 ) | |
453 { | |
454 fprintf(stderr,"error: PgSetPalette(..,Pg_PALSET_HARD) failed\n"); | |
455 return 0; | |
456 } | |
457 } | |
458 else | |
459 { | |
460 if ( PgColorMatch(ncolors, in, out) < 0 ) | |
461 { | |
462 fprintf(stderr,"error: PgColorMatch failed\n"); | |
463 return 0; | |
464 } | |
465 for (i=0;i<ncolors;i++) | |
466 { | |
467 if (memcmp(&in[i],&out[i],sizeof(PgColor_t))) | |
468 { | |
469 alloct_all = 0; | |
470 break; | |
471 } | |
472 } | |
473 if( PgSetPalette( out, 0, 0, ncolors, Pg_PALSET_SOFT, 0) < 0 ) | |
474 { | |
475 fprintf(stderr,"error: PgSetPalette(..,Pg_PALSET_SOFT) failed\n"); | |
476 return 0; | |
477 } | |
478 } | |
479 return alloct_all; | |
480 } | |
481 | |
482 static int ph_ResizeWindow(_THIS, | |
483 SDL_Surface *screen, int w, int h, Uint32 flags) | |
484 { | |
485 PhWindowEvent_t winevent; | |
486 | |
487 memset( &winevent, 0, sizeof(winevent) ); | |
488 winevent.event_f = Ph_WM_RESIZE; | |
489 winevent.size.w = w; | |
490 winevent.size.h = h; | |
491 winevent.rid = PtWidgetRid( window ); | |
492 if (PtForwardWindowEvent( &winevent ) < 0) | |
493 { | |
494 fprintf(stderr,"error: PtForwardWindowEvent failed.\n"); | |
495 } | |
496 current_w = w; | |
497 current_h = h; | |
498 return(0); | |
499 } | |
500 | |
501 |