2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 micahjd@users.sourceforge.net
25 #include "SDL_config.h"
27 #include "SDL_video.h"
28 #include "SDL_mouse.h"
29 #include "../SDL_sysvideo.h"
30 #include "../SDL_pixels_c.h"
31 #include "../../events/SDL_events_c.h"
33 #include "SDL_pgvideo.h"
34 #include "SDL_pgevents_c.h"
36 #define PGVID_DRIVER_NAME "picogui"
38 /* Initialization/Query functions */
39 static int PG_VideoInit(_THIS, SDL_PixelFormat *vformat);
40 static SDL_Rect **PG_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
41 static SDL_Surface *PG_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
42 static int PG_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
43 static void PG_VideoQuit(_THIS);
45 /* Hardware surface functions */
46 static int PG_AllocHWSurface(_THIS, SDL_Surface *surface);
47 static int PG_LockHWSurface(_THIS, SDL_Surface *surface);
48 static void PG_UnlockHWSurface(_THIS, SDL_Surface *surface);
49 static void PG_FreeHWSurface(_THIS, SDL_Surface *surface);
52 static void PG_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
54 // The implementation dependent data for the window manager cursor
56 /* Our cursor is a PicoGUI theme */
61 void PG_SetCaption(_THIS, const char *title, const char *icon);
62 WMcursor * PG_CreateWMCursor (_THIS,Uint8 * data, Uint8 * mask,
63 int w, int h, int hot_x, int hot_y);
64 void PG_FreeWMCursor (_THIS, WMcursor * cursor);
65 void PG_WarpWMCursor(_THIS, Uint16 x, Uint16 y);
66 int PG_ShowWMCursor (_THIS, WMcursor * cursor);
68 /* PicoGUI driver bootstrap functions */
70 static int PG_Available(void)
72 /* FIXME: The current client lib doesn't give a way to see if the picogui
73 * server is reachable without causing a fatal error if it isn't.
74 * This should be fixed in cli_c2, but until then assume we can
75 * connect. Since more common drivers like X11 are probed first anyway,
76 * this shouldn't be a huge problem.
81 static void PG_DeleteDevice(SDL_VideoDevice *device)
83 SDL_free(device->hidden);
87 static SDL_VideoDevice *PG_CreateDevice(int devindex)
89 SDL_VideoDevice *device;
91 /* Initialize all variables that we clean on shutdown */
92 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
94 SDL_memset(device, 0, (sizeof *device));
95 device->hidden = (struct SDL_PrivateVideoData *)
96 SDL_malloc((sizeof *device->hidden));
98 if ( (device == NULL) || (device->hidden == NULL) ) {
105 SDL_memset(device->hidden, 0, (sizeof *device->hidden));
107 /* Set the function pointers */
108 device->VideoInit = PG_VideoInit;
109 device->ListModes = PG_ListModes;
110 device->SetVideoMode = PG_SetVideoMode;
111 device->CreateYUVOverlay = NULL;
112 device->SetColors = PG_SetColors;
113 device->UpdateRects = PG_UpdateRects;
114 device->VideoQuit = PG_VideoQuit;
115 device->AllocHWSurface = PG_AllocHWSurface;
116 device->CheckHWBlit = NULL;
117 device->FillHWRect = NULL;
118 device->SetHWColorKey = NULL;
119 device->SetHWAlpha = NULL;
120 device->LockHWSurface = PG_LockHWSurface;
121 device->UnlockHWSurface = PG_UnlockHWSurface;
122 device->FlipHWSurface = NULL;
123 device->FreeHWSurface = PG_FreeHWSurface;
124 device->SetCaption = PG_SetCaption;
125 device->SetIcon = NULL;
126 device->IconifyWindow = NULL;
127 device->GrabInput = NULL;
129 device->PumpEvents = PG_PumpEvents;
130 device->InitOSKeymap = PG_InitOSKeymap;
132 device->ShowWMCursor = PG_ShowWMCursor;
133 device->CreateWMCursor = PG_CreateWMCursor;
134 device->FreeWMCursor = PG_FreeWMCursor;
135 device->WarpWMCursor = PG_WarpWMCursor;
137 device->free = PG_DeleteDevice;
142 VideoBootStrap PG_bootstrap = {
143 PGVID_DRIVER_NAME, "PicoGUI SDL driver",
144 PG_Available, PG_CreateDevice
148 int PG_VideoInit(_THIS, SDL_PixelFormat *vformat)
150 /* Connect to the PicoGUI server. No way to process command line args yet,
151 * but since this is based on SHM it's not important to be able to specify
152 * a remote PicoGUI server.
154 * FIXME: Another nitpick about the current client lib is there's no
155 * clean way to indicate that command line args are not available.
157 pgInit(0,(char**)"");
158 this->hidden->mi = *pgGetVideoMode();
160 /* Create a picogui application and canvas. We'll populate the canvas later. */
161 this->hidden->wApp = pgRegisterApp(PG_APP_NORMAL,"SDL",0);
162 this->hidden->wCanvas = pgNewWidget(PG_WIDGET_CANVAS,0,0);
163 pgSetWidget(PGDEFAULT,
164 PG_WP_SIDE, PG_S_ALL,
169 /* Determine the current screen size */
170 this->info.current_w = this->hidden->mi.lxres;
171 this->info.current_h = this->hidden->mi.lyres;
173 /* Determine the screen depth.
174 * We change this during the SDL_SetVideoMode implementation...
175 * Round up to the nearest Bytes per pixel
177 vformat->BitsPerPixel = this->hidden->mi.bpp;
178 vformat->BytesPerPixel = this->hidden->mi.bpp >> 3;
179 if (this->hidden->mi.bpp & 7)
180 vformat->BytesPerPixel++;
186 SDL_Rect **PG_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
188 return (SDL_Rect **) -1;
191 SDL_Surface *PG_SetVideoMode(_THIS, SDL_Surface *current,
192 int width, int height, int bpp, Uint32 flags)
194 if ( this->hidden->bitmap ) {
195 /* Free old bitmap */
196 if (current->pixels) {
197 shmdt(current->pixels);
198 current->pixels = NULL;
200 pgDelete(this->hidden->bitmap);
203 /* Allocate the new pixel format for the screen */
204 if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
205 SDL_SetError("Couldn't allocate new pixel format for requested mode");
209 /* Create a new picogui bitmap */
210 this->hidden->bitmap = pgCreateBitmap(width,height);
211 this->hidden->shm = *pgMakeSHMBitmap(this->hidden->bitmap);
212 current->pixels = shmat(shmget(this->hidden->shm.shm_key,
213 this->hidden->shm.shm_length,0),NULL,0);
215 /* Reset the canvas, and draw persistent and incremental grops.
216 * Use mapping and offsets to center it.
219 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_NUKE, 0);
221 /* 0. Set the source position during incremental rendering
223 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROP, 5, PG_GROP_SETSRC,0,0,0,0);
224 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROPFLAGS, 1, PG_GROPF_INCREMENTAL);
226 /* 1. Incremental bitmap rendering
228 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROP, 6, PG_GROP_BITMAP,
229 0,0,0,0,this->hidden->bitmap);
230 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROPFLAGS, 1, PG_GROPF_INCREMENTAL);
232 /* 2. Normal bitmap rendering
234 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROP, 6, PG_GROP_BITMAP,
235 0,0,this->hidden->shm.width,this->hidden->shm.height,this->hidden->bitmap);
237 /* Set up the new mode framebuffer */
239 current->w = this->hidden->shm.width;
240 current->h = this->hidden->shm.height;
241 current->pitch = this->hidden->shm.pitch;
243 /* Set up pixel format */
244 current->format->BitsPerPixel = this->hidden->shm.bpp;
245 current->format->BytesPerPixel = this->hidden->shm.bpp >> 3;
246 if (this->hidden->shm.bpp & 7)
247 current->format->BytesPerPixel++;
248 current->format->palette = NULL;
249 current->format->Rmask = this->hidden->shm.red_mask;
250 current->format->Gmask = this->hidden->shm.green_mask;
251 current->format->Bmask = this->hidden->shm.blue_mask;
252 current->format->Amask = this->hidden->shm.alpha_mask;
253 current->format->Rshift = this->hidden->shm.red_shift;
254 current->format->Gshift = this->hidden->shm.green_shift;
255 current->format->Bshift = this->hidden->shm.blue_shift;
256 current->format->Ashift = this->hidden->shm.alpha_shift;
257 current->format->Rloss = 8 - this->hidden->shm.red_length;
258 current->format->Gloss = 8 - this->hidden->shm.green_length;
259 current->format->Bloss = 8 - this->hidden->shm.blue_length;
260 current->format->Aloss = 8 - this->hidden->shm.alpha_length;
269 /* We don't actually allow hardware surfaces other than the main one */
270 static int PG_AllocHWSurface(_THIS, SDL_Surface *surface)
274 static void PG_FreeHWSurface(_THIS, SDL_Surface *surface)
279 /* We need to wait for vertical retrace on page flipped displays */
280 static int PG_LockHWSurface(_THIS, SDL_Surface *surface)
285 static void PG_UnlockHWSurface(_THIS, SDL_Surface *surface)
290 static void PG_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
294 for (i = 0; i < numrects; i++) {
295 if (rects[i].w <= 0 || rects[i].h <= 0)
298 /* Schedule an incremental update for this rectangle, using
299 * the canvas gropnodes we've loaded beforehand.
301 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_FINDGROP, 1, 0);
302 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_MOVEGROP, 4,
303 rects[i].x, rects[i].y,
304 rects[i].w, rects[i].h);
305 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_FINDGROP, 1, 1);
306 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_MOVEGROP, 4,
307 rects[i].x, rects[i].y,
308 rects[i].w, rects[i].h);
310 /* Go perform the update */
311 pgWriteCmd(this->hidden->wCanvas, PGCANVAS_INCREMENTAL, 0);
312 pgSubUpdate(this->hidden->wCanvas);
316 int PG_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
318 /* do nothing of note. */
322 /* Note: If we are terminated, this could be called in the middle of
323 another SDL video routine -- notably UpdateRects.
325 void PG_VideoQuit(_THIS)
327 if (this->screen->pixels != NULL)
329 shmdt(this->screen->pixels);
330 this->screen->pixels = NULL;
331 pgDelete(this->hidden->bitmap);
333 pgDelete(this->hidden->wCanvas);
334 pgDelete(this->hidden->wApp);
337 void PG_SetCaption(_THIS, const char *title, const char *icon)
340 pgReplaceText(this->hidden->wApp, title);
344 /* FIXME: The cursor stuff isn't implemented yet! */
346 WMcursor * PG_CreateWMCursor (_THIS,Uint8 * data, Uint8 * mask,
347 int w, int h, int hot_x, int hot_y)
349 static WMcursor dummy;
353 void PG_FreeWMCursor (_THIS, WMcursor * cursor)
357 void PG_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
361 int PG_ShowWMCursor (_THIS, WMcursor * cursor)