comparison src/video/ggi/SDL_ggivideo.c @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children 4f22a992f5e9
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
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 /* GGI-based SDL video driver implementation.
29 */
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <sys/mman.h>
36
37 #include <ggi/ggi.h>
38 #include <ggi/gii.h>
39
40 #include "SDL.h"
41 #include "SDL_error.h"
42 #include "SDL_video.h"
43 #include "SDL_mouse.h"
44 #include "SDL_sysvideo.h"
45 #include "SDL_pixels_c.h"
46 #include "SDL_events_c.h"
47 #include "SDL_ggivideo.h"
48 #include "SDL_ggimouse_c.h"
49 #include "SDL_ggievents_c.h"
50
51
52 struct private_hwdata
53 {
54 ggi_visual_t vis;
55 };
56
57 ggi_visual_t VIS;
58
59 /* Initialization/Query functions */
60 static int GGI_VideoInit(_THIS, SDL_PixelFormat *vformat);
61 static SDL_Rect **GGI_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
62 static SDL_Surface *GGI_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
63 static int GGI_SetColors(_THIS, int firstcolor, int ncolors,
64 SDL_Color *colors);
65 static void GGI_VideoQuit(_THIS);
66
67 /* Hardware surface functions */
68 static int GGI_AllocHWSurface(_THIS, SDL_Surface *surface);
69 static int GGI_LockHWSurface(_THIS, SDL_Surface *surface);
70 static void GGI_UnlockHWSurface(_THIS, SDL_Surface *surface);
71 static void GGI_FreeHWSurface(_THIS, SDL_Surface *surface);
72
73 /* GGI driver bootstrap functions */
74
75 static int GGI_Available(void)
76 {
77 ggi_visual_t *vis;
78
79 vis = ggiOpen(NULL);
80 if (vis != NULL)
81 {
82 ggiClose(vis);
83 }
84 return (vis != NULL);
85 }
86
87 static void GGI_DeleteDevice(SDL_VideoDevice *device)
88 {
89 free(device->hidden);
90 free(device);
91 }
92
93 static SDL_VideoDevice *GGI_CreateDevice(int devindex)
94 {
95 SDL_VideoDevice *device;
96
97 /* Initialize all variables that we clean on shutdown */
98 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
99 if ( device ) {
100 memset(device, 0, (sizeof *device));
101 device->hidden = (struct SDL_PrivateVideoData *)
102 malloc((sizeof *device->hidden));
103 }
104 if ( (device == NULL) || (device->hidden == NULL) ) {
105 SDL_OutOfMemory();
106 if ( device ) {
107 free(device);
108 }
109 return(0);
110 }
111 memset(device->hidden, 0, (sizeof *device->hidden));
112
113 /* Set the function pointers */
114 device->VideoInit = GGI_VideoInit;
115 device->ListModes = GGI_ListModes;
116 device->SetVideoMode = GGI_SetVideoMode;
117 device->SetColors = GGI_SetColors;
118 device->UpdateRects = NULL;
119 device->VideoQuit = GGI_VideoQuit;
120 device->AllocHWSurface = GGI_AllocHWSurface;
121 device->CheckHWBlit = NULL;
122 device->FillHWRect = NULL;
123 device->SetHWColorKey = NULL;
124 device->SetHWAlpha = NULL;
125 device->LockHWSurface = GGI_LockHWSurface;
126 device->UnlockHWSurface = GGI_UnlockHWSurface;
127 device->FlipHWSurface = NULL;
128 device->FreeHWSurface = GGI_FreeHWSurface;
129 device->SetCaption = NULL;
130 device->SetIcon = NULL;
131 device->IconifyWindow = NULL;
132 device->GrabInput = NULL;
133 device->GetWMInfo = NULL;
134 device->InitOSKeymap = GGI_InitOSKeymap;
135 device->PumpEvents = GGI_PumpEvents;
136
137 device->free = GGI_DeleteDevice;
138
139 return device;
140 }
141
142 VideoBootStrap GGI_bootstrap = {
143 "ggi", "General Graphics Interface (GGI)",
144 GGI_Available, GGI_CreateDevice
145 };
146
147
148 static SDL_Rect video_mode;
149 static SDL_Rect *SDL_modelist[4] = { NULL, NULL, NULL, NULL };
150
151 int GGI_VideoInit(_THIS, SDL_PixelFormat *vformat)
152 {
153 ggi_mode mode =
154 {
155 1,
156 { GGI_AUTO, GGI_AUTO },
157 { GGI_AUTO, GGI_AUTO },
158 { 0, 0 },
159 GT_AUTO,
160 { GGI_AUTO, GGI_AUTO }
161 };
162 struct private_hwdata *priv;
163 ggi_color pal[256], map[256];
164 const ggi_directbuffer *db;
165 int err, num_bufs;
166 ggi_pixel white, black;
167
168 priv = malloc(sizeof(struct private_hwdata));
169 if (priv == NULL)
170 {
171 SDL_SetError("Unhandled GGI mode type!\n");
172 GGI_VideoQuit(NULL);
173 }
174
175 if (ggiInit() != 0)
176 {
177 SDL_SetError("Unable to initialize GGI!\n");
178 GGI_VideoQuit(NULL);
179 }
180
181 VIS = ggiOpen(NULL);
182 if (VIS == NULL)
183 {
184 SDL_SetError("Unable to open default GGI visual!\n");
185 ggiExit();
186 GGI_VideoQuit(NULL);
187 }
188
189 ggiSetFlags(VIS, GGIFLAG_ASYNC);
190
191 /* Validate mode, autodetecting any GGI_AUTO or GT_AUTO fields */
192 ggiCheckMode(VIS, &mode);
193
194 /* At this point we should have a valid mode - try to set it */
195 err = ggiSetMode(VIS, &mode);
196
197 /* If we couldn't set _any_ modes, something is very wrong */
198 if (err)
199 {
200 SDL_SetError("Can't set a mode!\n");
201 ggiClose(VIS);
202 ggiExit();
203 GGI_VideoQuit(NULL);
204 }
205
206 /* Set a palette for palletized modes */
207 if (GT_SCHEME(mode.graphtype) == GT_PALETTE)
208 {
209 ggiSetColorfulPalette(VIS);
210 ggiGetPalette(VIS, 0, 1 << vformat->BitsPerPixel, pal);
211 }
212
213 /* Now we try to get the DirectBuffer info, which determines whether
214 * SDL can access hardware surfaces directly. */
215
216 num_bufs = ggiDBGetNumBuffers(VIS);
217
218 if (num_bufs > 0)
219 {
220 db = ggiDBGetBuffer(VIS, 0); /* Only handle one DB for now */
221
222 vformat->BitsPerPixel = db->buffer.plb.pixelformat->depth;
223
224 vformat->Rmask = db->buffer.plb.pixelformat->red_mask;
225 vformat->Gmask = db->buffer.plb.pixelformat->green_mask;
226 vformat->Bmask = db->buffer.plb.pixelformat->blue_mask;
227
228 /* Fill in our hardware acceleration capabilities */
229
230 this->info.wm_available = 0;
231 this->info.hw_available = 1;
232 this->info.video_mem = db->buffer.plb.stride * mode.virt.y;
233 }
234
235 video_mode.x = 0;
236 video_mode.y = 0;
237 video_mode.w = mode.virt.x;
238 video_mode.h = mode.virt.y;
239 SDL_modelist[((vformat->BitsPerPixel + 7) / 8) - 1] = &video_mode;
240
241 /* We're done! */
242 return(0);
243 }
244
245 static SDL_Rect **GGI_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
246 {
247 return(&SDL_modelist[((format->BitsPerPixel + 7) / 8) - 1]);
248 }
249
250 /* Various screen update functions available */
251 static void GGI_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
252
253 SDL_Surface *GGI_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
254 {
255 ggi_mode mode =
256 {
257 1,
258 { GGI_AUTO, GGI_AUTO },
259 { GGI_AUTO, GGI_AUTO },
260 { 0, 0 },
261 GT_AUTO,
262 { GGI_AUTO, GGI_AUTO }
263 };
264 const ggi_directbuffer *db;
265 ggi_color pal[256];
266 int err;
267
268 fprintf(stderr, "GGI_SetVideoMode()\n");
269
270 mode.visible.x = mode.virt.x = width;
271 mode.visible.y = mode.virt.y = height;
272
273 /* Translate requested SDL bit depth into a GGI mode */
274 switch (bpp)
275 {
276 case 1: mode.graphtype = GT_1BIT; break;
277 case 2: mode.graphtype = GT_2BIT; break;
278 case 4: mode.graphtype = GT_4BIT; break;
279 case 8: mode.graphtype = GT_8BIT; break;
280 case 15: mode.graphtype = GT_15BIT; break;
281 case 16: mode.graphtype = GT_16BIT; break;
282 case 24: mode.graphtype = GT_24BIT; break;
283 case 32: mode.graphtype = GT_32BIT; break;
284 default:
285 SDL_SetError("Unknown SDL bit depth, using GT_AUTO....\n");
286 mode.graphtype = GT_AUTO;
287 }
288
289 /* Validate mode, autodetecting any GGI_AUTO or GT_AUTO fields */
290 ggiCheckMode(VIS, &mode);
291
292 /* At this point we should have a valid mode - try to set it */
293 err = ggiSetMode(VIS, &mode);
294
295 /* If we couldn't set _any_ modes, something is very wrong */
296 if (err)
297 {
298 SDL_SetError("Can't set a mode!\n");
299 ggiClose(VIS);
300 ggiExit();
301 GGI_VideoQuit(NULL);
302 }
303
304 /* Set a palette for palletized modes */
305 if (GT_SCHEME(mode.graphtype) == GT_PALETTE)
306 {
307 ggiSetColorfulPalette(VIS);
308 ggiGetPalette(VIS, 0, 1 << bpp, pal);
309 }
310
311 db = ggiDBGetBuffer(VIS, 0);
312
313 /* Set up the new mode framebuffer */
314 current->flags = (SDL_FULLSCREEN | SDL_HWSURFACE);
315 current->w = mode.virt.x;
316 current->h = mode.virt.y;
317 current->pitch = db->buffer.plb.stride;
318 current->pixels = db->read;
319
320 /* Set the blit function */
321 this->UpdateRects = GGI_DirectUpdate;
322
323 /* We're done */
324 return(current);
325 }
326
327 static int GGI_AllocHWSurface(_THIS, SDL_Surface *surface)
328 {
329 return(-1);
330 }
331 static void GGI_FreeHWSurface(_THIS, SDL_Surface *surface)
332 {
333 return;
334 }
335 static int GGI_LockHWSurface(_THIS, SDL_Surface *surface)
336 {
337 return(0);
338 }
339 static void GGI_UnlockHWSurface(_THIS, SDL_Surface *surface)
340 {
341 return;
342 }
343
344 static void GGI_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
345 {
346 int i;
347
348 /* ggiFlush(VIS); */
349
350 for (i = 0; i < numrects; i++)
351 {
352 ggiFlushRegion(VIS, rects[i].x, rects[i].y, rects[i].w, rects[i].h);
353 }
354 return;
355 }
356
357 int GGI_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
358 {
359 int i;
360 ggi_color pal[256];
361
362 /* Set up the colormap */
363 for (i = 0; i < ncolors; i++)
364 {
365 pal[i].r = (colors[i].r << 8) | colors[i].r;
366 pal[i].g = (colors[i].g << 8) | colors[i].g;
367 pal[i].b = (colors[i].b << 8) | colors[i].b;
368 }
369
370 ggiSetPalette(VIS, firstcolor, ncolors, pal);
371
372 return 1;
373 }
374
375 void GGI_VideoQuit(_THIS)
376 {
377 }
378 void GGI_FinalQuit(void)
379 {
380 }