2 SDL - Simple DirectMedia Layer
3 Copyright (C) 2003 Sam Hocevar
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.
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.
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
25 "@(#) $Id: libsdl-1.2.11-libcaca.patch,v 1.1 2006/09/18 16:06:06 mr_bones_ Exp $";
28 /* libcaca based SDL video driver implementation.
39 #include "SDL_error.h"
40 #include "SDL_video.h"
41 #include "SDL_mouse.h"
42 #include "../SDL_sysvideo.h"
43 #include "../SDL_pixels_c.h"
44 #include "../../events/SDL_events_c.h"
46 #include "SDL_cacavideo.h"
47 #include "SDL_cacaevents_c.h"
50 #ifdef CACA_API_VERSION_1
54 /* Initialization/Query functions */
55 static int Caca_VideoInit(_THIS, SDL_PixelFormat *vformat);
56 static SDL_Rect **Caca_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
57 static SDL_Surface *Caca_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
58 static void Caca_VideoQuit(_THIS);
60 /* Hardware surface functions */
61 static int Caca_AllocHWSurface(_THIS, SDL_Surface *surface);
62 static int Caca_LockHWSurface(_THIS, SDL_Surface *surface);
63 static int Caca_FlipHWSurface(_THIS, SDL_Surface *surface);
64 static void Caca_UnlockHWSurface(_THIS, SDL_Surface *surface);
65 static void Caca_FreeHWSurface(_THIS, SDL_Surface *surface);
67 /* Cache the VideoDevice struct */
68 static struct SDL_VideoDevice *local_this;
70 /* libcaca driver bootstrap functions */
72 static int Caca_Available(void)
74 return 1; /* Always available ! */
77 static void Caca_DeleteDevice(SDL_VideoDevice *device)
82 static SDL_VideoDevice *Caca_CreateDevice(int devindex)
84 SDL_VideoDevice *device;
86 /* Initialize all variables that we clean on shutdown */
87 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
89 memset(device, 0, (sizeof *device));
90 device->hidden = (struct SDL_PrivateVideoData *)
91 malloc((sizeof *device->hidden));
93 if ( (device == NULL) || (device->hidden == NULL) ) {
100 memset(device->hidden, 0, (sizeof *device->hidden));
102 /* Set the function pointers */
103 device->VideoInit = Caca_VideoInit;
104 device->ListModes = Caca_ListModes;
105 device->SetVideoMode = Caca_SetVideoMode;
106 device->CreateYUVOverlay = NULL;
107 device->SetColors = NULL;
108 device->UpdateRects = NULL;
109 device->VideoQuit = Caca_VideoQuit;
110 device->AllocHWSurface = Caca_AllocHWSurface;
111 device->CheckHWBlit = NULL;
112 device->FillHWRect = NULL;
113 device->SetHWColorKey = NULL;
114 device->SetHWAlpha = NULL;
115 device->LockHWSurface = Caca_LockHWSurface;
116 device->UnlockHWSurface = Caca_UnlockHWSurface;
117 device->FlipHWSurface = NULL;
118 device->FreeHWSurface = Caca_FreeHWSurface;
119 device->SetCaption = NULL;
120 device->SetIcon = NULL;
121 device->IconifyWindow = NULL;
122 device->GrabInput = NULL;
123 device->GetWMInfo = NULL;
124 device->InitOSKeymap = Caca_InitOSKeymap;
125 device->PumpEvents = Caca_PumpEvents;
127 device->free = Caca_DeleteDevice;
132 VideoBootStrap CACA_bootstrap = {
133 "caca", "Color ASCII Art Library",
134 Caca_Available, Caca_CreateDevice
137 int Caca_VideoInit(_THIS, SDL_PixelFormat *vformat)
141 /* Initialize all variables that we clean on shutdown */
142 for ( i=0; i<SDL_NUMMODES; ++i ) {
143 SDL_modelist[i] = malloc(sizeof(SDL_Rect));
144 SDL_modelist[i]->x = SDL_modelist[i]->y = 0;
146 /* Modes sorted largest to smallest */
147 SDL_modelist[0]->w = 1024; SDL_modelist[0]->h = 768;
148 SDL_modelist[1]->w = 800; SDL_modelist[1]->h = 600;
149 SDL_modelist[2]->w = 640; SDL_modelist[2]->h = 480;
150 SDL_modelist[3]->w = 320; SDL_modelist[3]->h = 400;
151 SDL_modelist[4]->w = 320; SDL_modelist[4]->h = 240;
152 SDL_modelist[5]->w = 320; SDL_modelist[5]->h = 200;
153 SDL_modelist[6] = NULL;
155 Caca_mutex = SDL_CreateMutex();
157 /* Initialize the library */
158 if ( caca_init() != 0 ) {
159 SDL_SetError("Unable to initialize libcaca");
163 /* Initialize private variables */
170 /* Determine the screen depth (use default 8-bit depth) */
171 vformat->BitsPerPixel = 8;
172 vformat->BytesPerPixel = 1;
178 SDL_Rect **Caca_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
180 if(format->BitsPerPixel != 8)
183 if ( flags & SDL_FULLSCREEN ) {
186 return (SDL_Rect **) -1;
190 /* Various screen update functions available */
191 static void Caca_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
193 SDL_Surface *Caca_SetVideoMode(_THIS, SDL_Surface *current,
194 int width, int height, int bpp, Uint32 flags)
202 caca_free_bitmap( Caca_bitmap );
206 Caca_buffer = malloc(2 * ((width + 15) & ~15) * height);
207 if ( ! Caca_buffer ) {
208 SDL_SetError("Couldn't allocate buffer for requested mode");
212 memset(Caca_buffer, 0, 2 * ((width + 15) & ~15) * height);
214 /* Allocate the new pixel format for the screen */
215 if ( ! SDL_ReallocFormat(current, 16, 0xf800, 0x07e0, 0x001f, 0) ) {
219 /* Set up the new mode framebuffer */
220 current->flags = SDL_FULLSCREEN;
221 Caca_w = current->w = width;
222 Caca_h = current->h = height;
223 current->pitch = 2 * ((width + 15) & ~15);
224 current->pixels = Caca_buffer;
226 /* Create the libcaca bitmap */
227 Caca_bitmap = caca_create_bitmap( 16, width, height, current->pitch, 0xf800, 0x07e0, 0x001f, 0x0000 );
228 if ( ! Caca_bitmap ) {
229 SDL_SetError("Couldn't allocate libcaca bitmap");
233 /* Set the blit function */
234 this->UpdateRects = Caca_DirectUpdate;
240 /* We don't actually allow hardware surfaces other than the main one */
241 static int Caca_AllocHWSurface(_THIS, SDL_Surface *surface)
245 static void Caca_FreeHWSurface(_THIS, SDL_Surface *surface)
250 /* We need to wait for vertical retrace on page flipped displays */
251 static int Caca_LockHWSurface(_THIS, SDL_Surface *surface)
256 static void Caca_UnlockHWSurface(_THIS, SDL_Surface *surface)
261 /* FIXME: How is this done with libcaca? */
262 static int Caca_FlipHWSurface(_THIS, SDL_Surface *surface)
264 SDL_mutexP(Caca_mutex);
266 SDL_mutexV(Caca_mutex);
270 static void Caca_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
272 SDL_mutexP(Caca_mutex);
273 caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
274 Caca_bitmap, Caca_buffer );
276 SDL_mutexV(Caca_mutex);
280 /* Note: If we are terminated, this could be called in the middle of
281 another SDL video routine -- notably UpdateRects.
283 void Caca_VideoQuit(_THIS)
287 /* Free video mode lists */
288 for ( i=0; i<SDL_NUMMODES; ++i ) {
289 if ( SDL_modelist[i] != NULL ) {
290 free(SDL_modelist[i]);
291 SDL_modelist[i] = NULL;
296 caca_free_bitmap( Caca_bitmap );
302 SDL_DestroyMutex(Caca_mutex);