comparison src/video/nanox/SDL_nxvideo.c @ 30:57bf11a5efd7

Added initial support for Nano-X (thanks Hsieh-Fu!)
author Sam Lantinga <slouken@lokigames.com>
date Fri, 11 May 2001 01:13:35 +0000
parents
children e8157fcb3114
comparison
equal deleted inserted replaced
29:a8360daed17d 30:57bf11a5efd7
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
4 Copyright (C) 2001 Hsieh-Fu Tsai
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the Free
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 Sam Lantinga
21 slouken@devolution.com
22
23 Hsieh-Fu Tsai
24 clare@setabox.com
25 */
26
27 #include <stdlib.h>
28
29 #include "SDL_video.h"
30 #include "SDL_pixels_c.h"
31 #include "SDL_events_c.h"
32 #include "SDL_thread.h"
33
34 #define MWINCLUDECOLORS
35 #include "SDL_nxvideo.h"
36 #include "SDL_nxmodes_c.h"
37 #include "SDL_nxwm_c.h"
38 #include "SDL_nxmouse_c.h"
39 #include "SDL_nximage_c.h"
40 #include "SDL_nxevents_c.h"
41
42 // Initialization/Query functions
43 static int NX_VideoInit (_THIS, SDL_PixelFormat * vformat) ;
44 static SDL_Surface * NX_SetVideoMode (_THIS, SDL_Surface * current, int width, int height, int bpp, Uint32 flags) ;
45 static int NX_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color * colors) ;
46 static void NX_VideoQuit (_THIS) ;
47 static void NX_DestroyWindow (_THIS, SDL_Surface * screen) ;
48 static int NX_ToggleFullScreen (_THIS, int on) ;
49 static void NX_UpdateMouse (_THIS) ;
50 static int NX_SetGammaRamp (_THIS, Uint16 * ramp) ;
51 static int NX_GetGammaRamp (_THIS, Uint16 * ramp) ;
52
53 // Microwin driver bootstrap functions
54 static int NX_Available ()
55 {
56 Dprintf ("enter NX_Available\n") ;
57
58 if (GrOpen () < 0) return 0 ;
59 GrClose () ;
60
61 Dprintf ("leave NX_Available\n") ;
62 return 1 ;
63 }
64
65 static void NX_DeleteDevice (SDL_VideoDevice * device)
66 {
67 Dprintf ("enter NX_DeleteDevice\n") ;
68
69 if (device) {
70 if (device -> hidden) free (device -> hidden) ;
71 if (device -> gl_data) free (device -> gl_data) ;
72 free (device) ;
73 }
74
75 Dprintf ("leave NX_DeleteDevice\n") ;
76 }
77
78 static SDL_VideoDevice * NX_CreateDevice (int devindex)
79 {
80 SDL_VideoDevice * device ;
81
82 Dprintf ("enter NX_CreateDevice\n") ;
83
84 // Initialize all variables that we clean on shutdown
85 device = (SDL_VideoDevice *) malloc (sizeof (SDL_VideoDevice)) ;
86 if (device) {
87 memset (device, 0, (sizeof * device)) ;
88 device -> hidden = (struct SDL_PrivateVideoData *)
89 malloc ((sizeof * device -> hidden)) ;
90 device -> gl_data = NULL ;
91 }
92 if ((device == NULL) || (device -> hidden == NULL)) {
93 SDL_OutOfMemory () ;
94 NX_DeleteDevice (device) ;
95 return 0 ;
96 }
97 memset (device -> hidden, 0, (sizeof * device -> hidden)) ;
98
99 // Set the function pointers
100 device -> VideoInit = NX_VideoInit ;
101 device -> ListModes = NX_ListModes ;
102 device -> SetVideoMode = NX_SetVideoMode ;
103 device -> ToggleFullScreen = NX_ToggleFullScreen ;
104 device -> UpdateMouse = NX_UpdateMouse ;
105 device -> CreateYUVOverlay = NULL ;
106 device -> SetColors = NX_SetColors ;
107 device -> UpdateRects = NULL ;
108 device -> VideoQuit = NX_VideoQuit;
109 device -> AllocHWSurface = NULL ;
110 device -> CheckHWBlit = NULL ;
111 device -> FillHWRect = NULL ;
112 device -> SetHWColorKey = NULL ;
113 device -> SetHWAlpha = NULL ;
114 device -> LockHWSurface = NULL ;
115 device -> UnlockHWSurface = NULL ;
116 device -> FlipHWSurface = NULL ;
117 device -> FreeHWSurface = NULL ;
118 device -> SetGamma = NULL ;
119 device -> GetGamma = NULL ;
120 device -> SetGammaRamp = NX_SetGammaRamp ;
121 device -> GetGammaRamp = NX_GetGammaRamp ;
122
123 #ifdef HAVE_OPENGL
124 device -> GL_LoadLibrary = NULL ;
125 device -> GL_GetProcAddress = NULL ;
126 device -> GL_GetAttribute = NULL ;
127 device -> GL_MakeCurrent = NULL ;
128 device -> GL_SwapBuffers = NULL ;
129 #endif
130
131 device -> SetIcon = NULL ;
132 device -> SetCaption = NX_SetCaption;
133 device -> IconifyWindow = NULL ;
134 device -> GrabInput = NULL ;
135 device -> GetWMInfo = NX_GetWMInfo ;
136 device -> FreeWMCursor = NX_FreeWMCursor ;
137 device -> CreateWMCursor = NX_CreateWMCursor ;
138 device -> ShowWMCursor = NX_ShowWMCursor ;
139 device -> WarpWMCursor = NX_WarpWMCursor ;
140 device -> CheckMouseMode = NULL ;
141 device -> InitOSKeymap = NX_InitOSKeymap ;
142 device -> PumpEvents = NX_PumpEvents ;
143
144 device -> free = NX_DeleteDevice ;
145
146 Dprintf ("leave NX_CreateDevice\n") ;
147 return device ;
148 }
149
150 VideoBootStrap NX_bootstrap = {
151 "nanox", "nanox", NX_Available, NX_CreateDevice
152 } ;
153
154 static void create_aux_windows (_THIS)
155 {
156 GR_WM_PROPERTIES props ;
157
158 Dprintf ("enter create_aux_windows\n") ;
159
160 // Don't create any extra windows if we are being managed
161 if (SDL_windowid) {
162 FSwindow = 0 ;
163 return ;
164 }
165
166 if (FSwindow && FSwindow != GR_ROOT_WINDOW_ID) {
167 GrDestroyWindow (FSwindow) ;
168 }
169
170 FSwindow = GrNewWindow (GR_ROOT_WINDOW_ID, 0, 0, 1, 1, 0, BLACK, BLACK) ;
171 props.flags = GR_WM_FLAGS_PROPS ;
172 props.props = GR_WM_PROPS_NODECORATE ;
173 GrSetWMProperties (FSwindow, & props) ;
174
175 GrSelectEvents (FSwindow, (GR_EVENT_MASK_EXPOSURE |
176 GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP |
177 GR_EVENT_MASK_FOCUS_IN | GR_EVENT_MASK_FOCUS_OUT |
178 GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_KEY_UP |
179 GR_EVENT_MASK_MOUSE_ENTER | GR_EVENT_MASK_MOUSE_EXIT |
180 GR_EVENT_MASK_MOUSE_MOTION | GR_EVENT_MASK_UPDATE |
181 GR_EVENT_MASK_CLOSE_REQ)) ;
182
183 Dprintf ("leave create_aux_windows\n") ;
184 }
185
186 int NX_VideoInit (_THIS, SDL_PixelFormat * vformat)
187 {
188 GR_SCREEN_INFO si ;
189
190 Dprintf ("enter NX_VideoInit\n") ;
191
192 if (GrOpen () < 0) {
193 SDL_SetError ("GrOpen() fail") ;
194 return -1 ;
195 }
196
197 // use share memory to speed up
198 #ifdef NANOX_SHARE_MEMORY
199 GrReqShmCmds (0xFFFF);
200 #endif
201
202 SDL_Window = 0 ;
203 FSwindow = 0 ;
204
205 GammaRamp_R = NULL ;
206 GammaRamp_G = NULL ;
207 GammaRamp_B = NULL ;
208
209 GrGetScreenInfo (& si) ;
210 SDL_Visual.bpp = si.bpp ;
211
212 // GetVideoMode
213 SDL_modelist = (SDL_Rect **) malloc (sizeof (SDL_Rect *) * 2) ;
214 if (SDL_modelist) {
215 SDL_modelist [0] = (SDL_Rect *) malloc (sizeof(SDL_Rect)) ;
216 if (SDL_modelist [0]) {
217 SDL_modelist [0] -> x = 0 ;
218 SDL_modelist [0] -> y = 0 ;
219 SDL_modelist [0] -> w = si.cols ;
220 SDL_modelist [0] -> h = si.rows ;
221 }
222 SDL_modelist [1] = NULL ;
223 }
224
225 #ifdef NANOX_PIXEL_RGB
226 pixel_type = MWPF_RGB ;
227 SDL_Visual.red_mask = 0x000000FF ;
228 SDL_Visual.green_mask = 0x0000FF00 ;
229 SDL_Visual.blue_mask = 0x00FF0000 ;
230 #endif
231 #ifdef NANOX_PIXEL_0888
232 pixel_type = MWPF_TRUECOLOR0888 ;
233 SDL_Visual.red_mask = 0x00FF0000 ;
234 SDL_Visual.green_mask = 0x0000FF00 ;
235 SDL_Visual.blue_mask = 0x000000FF ;
236 #endif
237 #ifdef NANOX_PIXEL_888
238 pixel_type = MWPF_TRUECOLOR888 ;
239 SDL_Visual.red_mask = 0xFF0000 ;
240 SDL_Visual.green_mask = 0x00FF00 ;
241 SDL_Visual.blue_mask = 0x0000FF ;
242 #endif
243 #ifdef NANOX_PIXEL_565
244 pixel_type = MWPF_TRUECOLOR565 ;
245 SDL_Visual.red_mask = 0xF800 ;
246 SDL_Visual.green_mask = 0x07E0 ;
247 SDL_Visual.blue_mask = 0x001F ;
248 #endif
249 #ifdef NANOX_PIXEL_555
250 pixel_type = MWPF_TRUECOLOR555 ;
251 SDL_Visual.red_mask = 0x7C00 ;
252 SDL_Visual.green_mask = 0x03E0 ;
253 SDL_Visual.blue_mask = 0x001F ;
254 #endif
255 #ifdef NANOX_PIXEL_332
256 pixel_type = MWPF_TRUECOLOR332 ;
257 #endif
258 #ifdef NANOX_PIXEL_PAL
259 pixel_type = MWPF_PALETTE ;
260 #endif
261
262 vformat -> BitsPerPixel = SDL_Visual.bpp ;
263 if (vformat -> BitsPerPixel > 8) {
264 vformat -> Rmask = SDL_Visual.red_mask ;
265 vformat -> Gmask = SDL_Visual.green_mask ;
266 vformat -> Bmask = SDL_Visual.blue_mask ;
267 }
268
269 // See if we have been passed a window to use
270 SDL_windowid = getenv ("SDL_WINDOWID") ;
271
272 // Create the fullscreen (and managed windows : no implement)
273 create_aux_windows (this) ;
274
275 Dprintf ("leave NX_VideoInit\n") ;
276 return 0 ;
277 }
278
279 void NX_VideoQuit (_THIS)
280 {
281 Dprintf ("enter NX_VideoQuit\n") ;
282
283 // Start shutting down the windows
284 NX_DestroyImage (this, this -> screen) ;
285 NX_DestroyWindow (this, this -> screen) ;
286 if (FSwindow && FSwindow != GR_ROOT_WINDOW_ID) {
287 GrDestroyWindow (FSwindow) ;
288 }
289 NX_FreeVideoModes (this) ;
290 free (GammaRamp_R) ;
291 free (GammaRamp_G) ;
292 free (GammaRamp_B) ;
293 GrClose () ;
294
295 Dprintf ("leave NX_VideoQuit\n") ;
296 }
297
298 static void NX_DestroyWindow (_THIS, SDL_Surface * screen)
299 {
300 Dprintf ("enter NX_DestroyWindow\n") ;
301
302 if (! SDL_windowid) {
303 if (screen && (screen -> flags & SDL_FULLSCREEN)) {
304 screen -> flags &= ~ SDL_FULLSCREEN ;
305 NX_LeaveFullScreen (this) ;
306 }
307
308 // Destroy the output window
309 if (SDL_Window && SDL_Window != GR_ROOT_WINDOW_ID) {
310 GrDestroyWindow (SDL_Window) ;
311 }
312 }
313
314 // Free the graphics context
315 if (! SDL_GC) {
316 GrDestroyGC (SDL_GC) ;
317 SDL_GC = 0;
318 }
319
320 Dprintf ("leave NX_DestroyWindow\n") ;
321 }
322
323 static int NX_CreateWindow (_THIS, SDL_Surface * screen,
324 int w, int h, int bpp, Uint32 flags)
325 {
326 Dprintf ("enter NX_CreateWindow\n") ;
327
328 // If a window is already present, destroy it and start fresh
329 if (SDL_Window && SDL_Window != GR_ROOT_WINDOW_ID) {
330 NX_DestroyWindow (this, screen) ;
331 }
332
333 // See if we have been given a window id
334 if (SDL_windowid) {
335 SDL_Window = strtol (SDL_windowid, NULL, 0) ;
336 } else {
337 SDL_Window = 0 ;
338 }
339
340 if ( ! SDL_ReallocFormat (screen, bpp, SDL_Visual.red_mask,
341 SDL_Visual.green_mask, SDL_Visual.blue_mask, 0))
342 return -1;
343
344 // Create (or use) the nanox display window
345 if (! SDL_windowid) {
346
347 SDL_Window = GrNewWindow (GR_ROOT_WINDOW_ID, 0, 0, w, h, 0, BLACK, WHITE) ;
348
349 GrSelectEvents (SDL_Window, (GR_EVENT_MASK_EXPOSURE |
350 GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP |
351 GR_EVENT_MASK_FOCUS_IN | GR_EVENT_MASK_FOCUS_OUT |
352 GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_KEY_UP |
353 GR_EVENT_MASK_MOUSE_ENTER | GR_EVENT_MASK_MOUSE_EXIT |
354 GR_EVENT_MASK_MOUSE_MOTION | GR_EVENT_MASK_UPDATE |
355 GR_EVENT_MASK_CLOSE_REQ)) ;
356 }
357
358 /* Create the graphics context here, once we have a window */
359 SDL_GC = GrNewGC () ;
360 if (SDL_GC == 0) {
361 SDL_SetError("Couldn't create graphics context");
362 return(-1);
363 }
364
365 // Map them both and go fullscreen, if requested
366 if (! SDL_windowid) {
367 GrMapWindow (SDL_Window) ;
368 if (flags & SDL_FULLSCREEN) {
369 screen -> flags |= SDL_FULLSCREEN ;
370 NX_EnterFullScreen (this) ;
371 } else {
372 screen -> flags &= ~ SDL_FULLSCREEN ;
373 }
374 }
375
376 Dprintf ("leave NX_CreateWindow\n") ;
377 return 0 ;
378 }
379
380 SDL_Surface * NX_SetVideoMode (_THIS, SDL_Surface * current,
381 int width, int height, int bpp, Uint32 flags)
382 {
383 Dprintf ("enter NX_SetVideoMode\n") ;
384
385 // Lock the event thread, in multi-threading environments
386 SDL_Lock_EventThread () ;
387
388 bpp = SDL_Visual.bpp ;
389 if (NX_CreateWindow (this, current, width, height, bpp, flags) < 0) {
390 current = NULL;
391 goto done;
392 }
393
394 if (current -> w != width || current -> h != height) {
395 current -> w = width ;
396 current -> h = height ;
397 current -> pitch = SDL_CalculatePitch (current) ;
398 NX_ResizeImage (this, current, flags) ;
399 }
400 current -> flags |= (flags & (SDL_RESIZABLE | SDL_NOFRAME)) ;
401
402 done:
403 SDL_Unlock_EventThread () ;
404
405 Dprintf ("leave NX_SetVideoMode\n") ;
406
407 // We're done!
408 return current ;
409 }
410
411 // ncolors <= 256
412 int NX_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color * colors)
413 {
414 int i ;
415 GR_PALETTE pal ;
416
417 Dprintf ("enter NX_SetColors\n") ;
418
419 if (ncolors > 256) return 0 ;
420
421 pal.count = ncolors ;
422 for (i = 0; i < ncolors; ++ i) {
423 pal.palette [i].r = colors [i].r ;
424 pal.palette [i].g = colors [i].g ;
425 pal.palette [i].b = colors [i].b ;
426 }
427 GrSetSystemPalette (firstcolor, & pal) ;
428
429 Dprintf ("leave NX_SetColors\n") ;
430 return 1 ;
431 }
432
433 static int NX_ToggleFullScreen (_THIS, int on)
434 {
435 SDL_Rect rect ;
436 Uint32 event_thread ;
437
438 Dprintf ("enter NX_ToggleFullScreen\n") ;
439
440 // Don't switch if we don't own the window
441 if (SDL_windowid) return 0 ;
442
443 // Don't lock if we are the event thread
444 event_thread = SDL_EventThreadID () ;
445 if (event_thread && (SDL_ThreadID () == event_thread)) {
446 event_thread = 0 ;
447 }
448 if (event_thread) {
449 SDL_Lock_EventThread() ;
450 }
451
452 if (on) {
453 NX_EnterFullScreen (this) ;
454 } else {
455 this -> screen -> flags &= ~ SDL_FULLSCREEN ;
456 NX_LeaveFullScreen (this) ;
457 }
458
459 rect.x = rect.y = 0 ;
460 rect.w = this -> screen -> w, rect.h = this -> screen -> h ;
461 NX_NormalUpdate (this, 1, & rect) ;
462
463 if (event_thread) {
464 SDL_Unlock_EventThread () ;
465 }
466
467 Dprintf ("leave NX_ToggleFullScreen\n") ;
468 return 1 ;
469 }
470
471 // Update the current mouse state and position
472 static void NX_UpdateMouse (_THIS)
473 {
474 int x, y ;
475 GR_WINDOW_INFO info ;
476 GR_SCREEN_INFO si ;
477
478
479 Dprintf ("enter NX_UpdateMouse\n") ;
480
481 // Lock the event thread, in multi-threading environments
482 SDL_Lock_EventThread () ;
483
484 GrGetScreenInfo (& si) ;
485 GrGetWindowInfo (SDL_Window, & info) ;
486 x = si.xpos - info.x ;
487 y = si.ypos - info.y ;
488 if (x >= 0 && x <= info.width && y >= 0 && y <= info.height) {
489 SDL_PrivateAppActive (1, SDL_APPMOUSEFOCUS) ;
490 SDL_PrivateMouseMotion (0, 0, x, y);
491 } else {
492 SDL_PrivateAppActive (0, SDL_APPMOUSEFOCUS) ;
493 }
494
495 SDL_Unlock_EventThread () ;
496 Dprintf ("leave NX_UpdateMouse\n") ;
497 }
498
499 static int NX_SetGammaRamp (_THIS, Uint16 * ramp)
500 {
501 int i ;
502 Uint16 * red, * green, * blue ;
503
504 Dprintf ("enter NX_SetGammaRamp\n") ;
505
506 if (SDL_Visual.bpp != 32 && SDL_Visual.bpp != 24) return -1 ;
507
508 if (! GammaRamp_R) GammaRamp_R = (Uint16 *) malloc (sizeof (Uint16) * CI_SIZE) ;
509 if (! GammaRamp_G) GammaRamp_G = (Uint16 *) malloc (sizeof (Uint16) * CI_SIZE) ;
510 if (! GammaRamp_B) GammaRamp_B = (Uint16 *) malloc (sizeof (Uint16) * CI_SIZE) ;
511 if ((! GammaRamp_R) || (! GammaRamp_G) || (! GammaRamp_B)) {
512 SDL_OutOfMemory () ;
513 return -1 ;
514 }
515
516 for (i = 0; i < CI_SIZE; ++ i)
517 GammaRamp_R [i] = GammaRamp_G [i] = GammaRamp_B [i] = i ;
518
519 red = ramp ;
520 green = ramp + CI_SIZE ;
521 blue = green + CI_SIZE ;
522
523 for (i = 0; i < CI_SIZE; ++ i) {
524 GammaRamp_R [i] = red [i] ;
525 GammaRamp_G [i] = green [i] ;
526 GammaRamp_B [i] = blue [i] ;
527 }
528
529 Dprintf ("leave NX_SetGammaRamp\n") ;
530 return 0 ;
531 }
532
533 static int NX_GetGammaRamp (_THIS, Uint16 * ramp)
534 {
535 int i ;
536 Uint16 * red, * green, * blue ;
537
538 Dprintf ("enter NX_GetGammaRamp\n") ;
539
540 if (SDL_Visual.bpp != 32 && SDL_Visual.bpp != 24) return -1 ;
541 red = ramp ;
542 green = ramp + CI_SIZE ;
543 blue = green + CI_SIZE ;
544 if (GammaRamp_R && GammaRamp_G && GammaRamp_B) {
545 for (i = 0; i < CI_SIZE; ++ i) {
546 red [i] = GammaRamp_R [i] ;
547 green [i] = GammaRamp_G [i] ;
548 blue [i] = GammaRamp_B [i] ;
549 }
550 } else {
551 for (i = 0; i < CI_SIZE; ++ i)
552 red [i] = green [i] = blue [i] = i ;
553 }
554
555 Dprintf ("leave NX_GetGammaRamp\n") ;
556 return 0 ;
557 }