4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
15 #include "common/input.h"
16 #include "linux/fbdev.h"
17 #include "linux/oshide.h"
19 static SDL_Surface *g_screen;
20 static void *g_screen_fbp;
21 static Uint16 g_8bpp_pal[256];
22 static Uint32 g_start_ticks;
24 static inline int min(int v1, int v2)
26 return v1 < v2 ? v1 : v2;
29 static SDL_Surface *alloc_surface(int w, int h, int bpp)
31 // SDL has some pointer overuse, every surface has a format,
32 // why make format accessible through pointer?
38 ret = calloc(1, sizeof(*ret));
42 ret->s.format = &ret->f;
43 ret->f.BitsPerPixel = bpp;
44 ret->f.BytesPerPixel = (bpp + 7) / 8;
48 ret->s.pitch = w * ret->f.BytesPerPixel;
55 SDL_Init(Uint32 flags)
59 if (g_start_ticks == 0) {
66 g_start_ticks = SDL_GetTicks();
76 if (g_start_ticks != 0) {
83 SDL_InitSubSystem(Uint32 flags)
90 SDL_QuitSubSystem(Uint32 flags)
95 DECLSPEC Uint32 SDLCALL
101 gettimeofday(&tv, NULL);
102 ret = tv.tv_sec * 1000;
103 ret += tv.tv_usec * 1048 >> 20;
105 ret -= g_start_ticks;
106 dbg(" SDL_GetTicks %d", ret);
110 DECLSPEC SDL_Surface * SDLCALL
111 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
113 struct vout_fbdev *fbdev;
115 trace("%d, %d, %d, %08x", width, height, bpp, flags);
120 if (bpp != 8 && bpp != 16) {
121 err("unsupported bpp: %d\n", bpp);
125 // XXX: destroy old g_screen?
127 g_screen = alloc_surface(width, height, bpp);
128 if (g_screen == NULL)
131 fbdev = vout_fbdev_init("/dev/fb0", &width, &height, 0);
133 goto fail_fbdev_init;
135 g_screen_fbp = vout_fbdev_flip(fbdev);
137 g_screen->pixels = g_screen_fbp;
139 // we have to fake this for now..
140 g_screen->pixels = calloc(g_screen->pitch * height, 1);
142 if (g_screen->pixels == NULL) {
146 g_screen->hwdata = (void *)fbdev;
148 dbg("returning %p", g_screen);
152 vout_fbdev_finish(fbdev);
160 SDL_Flip(SDL_Surface *screen)
162 struct vout_fbdev *fbdev;
166 if (screen != g_screen) {
167 err("flip not on screen surface?");
171 if (screen->format->BitsPerPixel == 8) {
172 int l = screen->pitch * screen->h;
174 do_clut(g_screen_fbp, screen->pixels, g_8bpp_pal, l);
176 Uint16 *d = g_screen_fbp;
177 Uint8 *s = screen->pixels;
179 // XXX: perhaps optimize this sh*t
180 for (; l > 0; d++, s++, l--)
185 fbdev = (void *)screen->hwdata;
186 g_screen_fbp = vout_fbdev_flip(fbdev);
188 if (screen->format->BitsPerPixel != 8)
189 screen->pixels = g_screen_fbp;
194 static int do_rect_clip(const SDL_Surface *s, SDL_Rect *r)
196 int x = r->x, y = r->y, w = r->w, h = r->h;
221 r->x = x; r->y = y; r->w = w; r->h = h;
222 return (w > 0 && h > 0) ? 0 : -1;
226 SDL_UpperBlit(SDL_Surface *src, SDL_Rect *srcrect,
227 SDL_Surface *dst, SDL_Rect *dstrect)
229 int sx = 0, sy = 0, sw, sh;
231 SDL_Rect tmprect = { 0, };
233 trace("%p, %p, %p, %p", src, srcrect, dst, dstrect);
235 if (src == NULL || dst == NULL)
238 if (srcrect != NULL) {
239 // XXX: dst pos may need to be adjusted in some corner cases
240 if (do_rect_clip(src, srcrect) < 0)
254 // SDL just uses source w and h
257 if (do_rect_clip(dst, dstrect) < 0)
264 dbg("(%d %d %d %d) -> (%d %d %d %d)", sx, sy, sw, sh, dx, dy, sw, sh);
266 if (src->format->BitsPerPixel == dst->format->BitsPerPixel) {
267 int Bpp = src->format->BytesPerPixel;
268 int dpitch = dst->pitch;
269 int spitch = src->pitch;
270 Uint8 *d = (Uint8 *)dst->pixels + dpitch * dy + dx * Bpp;
271 Uint8 *s = (Uint8 *)src->pixels + spitch * sy + sx * Bpp;
273 for (sw *= Bpp; sh > 0; d += dpitch, s += spitch, sh--)
282 DECLSPEC SDL_Surface * SDLCALL
283 SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth,
284 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
288 trace("%08x, %d, %d, %d, %04x, %04x, %04x, %04x",
289 flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
291 ret = alloc_surface(width, height, depth);
295 ret->pixels = calloc(ret->pitch * height, 1);
296 if (ret->pixels == NULL)
299 dbg("returning %p", ret);
308 SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
312 trace("%p, %p, %d, %d", surface, colors, firstcolor, ncolors);
314 if (surface != g_screen)
317 to = min(ARRAY_SIZE(g_8bpp_pal), firstcolor + ncolors);
318 for (i = firstcolor; i < to; i++) {
319 SDL_Color *c = &colors[i - firstcolor];
320 int r = c->r, g = c->g, b = c->b;
321 // noiz2sa gets a good deal darker that real SDL,
322 // don't know what's going on there
324 if (r < 0xf8 && (r & 7) > 3) r += 8;
325 if (g < 0xfc && (g & 3) > 1) g += 4;
326 if (b < 0xf8 && (b & 7) > 3) b += 8;
328 g_8bpp_pal[i] = ((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3);
329 // g_8bpp_pal[i] += 0x0821;
336 SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
338 trace("%p, %p, %04x", dst, dstrect, color);
340 if (dst->format->BytesPerPixel != 1) {
345 memset(dst->pixels, color, dst->pitch * dst->h);
351 DECLSPEC SDL_Surface * SDLCALL
352 SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch,
353 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
358 DECLSPEC SDL_RWops * SDLCALL
359 SDL_RWFromFile(const char *file, const char *mode)
364 trace("%s, %s", file, mode);
366 f = fopen(file, mode);
370 ret = calloc(1, sizeof(*ret));
375 ret->hidden.stdio.fp = f;
380 DECLSPEC void SDLCALL
381 SDL_FreeRW(SDL_RWops *area)
383 if (area->hidden.stdio.fp) {
384 fclose(area->hidden.stdio.fp);
385 area->hidden.stdio.fp = NULL;
390 DECLSPEC SDL_Surface * SDLCALL
391 SDL_LoadBMP_RW(SDL_RWops *src, int freesrc)
394 unsigned char magic[2];
404 uint32_t compress_type;
410 } __attribute__((packed)) bmp;
411 SDL_Surface *ret = NULL;
412 int data_size, read_size;
413 uint8_t *tmp_buf = NULL;
417 trace("%p, %d", src, freesrc);
419 f = src->hidden.stdio.fp;
423 if (fread(&bmp, 1, sizeof(bmp), f) != sizeof(bmp)) {
424 err("bmp read error");
428 if (bmp.magic[0] != 'B' || bmp.magic[1] != 'M' || bmp.header_sz != 40) {
429 err("unhandled BMP format, sz=%d", bmp.header_sz);
433 if ((unsigned)bmp.width > 0xffff || (unsigned)bmp.height > 0xffff) {
434 err("BMP size %dx%d out of range", bmp.width, bmp.height);
438 if (bmp.bitspp != 4) {
439 err("BMP unhandled bpp: %d", bmp.bitspp);
443 bytespp = bmp.bitspp < 8 ? 1 : (bmp.bitspp + 7) / 8;
444 data_size = bmp.width * bmp.height * bytespp;
445 tmp_buf = malloc(data_size);
449 if (fseek(f, bmp.bmp_offset, SEEK_SET) != 0) {
450 err("BMP seek error");
454 read_size = bmp.width * bmp.bitspp / 8;
455 for (i = bmp.height - 1; i > 0; i--) {
456 if (fread((char *)tmp_buf + i * read_size, 1, read_size, f) != read_size) {
457 err("BMP read error");
462 if (bmp.bitspp == 4) {
463 // just convert to 8bpp now
464 int c = bmp.width * bmp.height / 2;
465 uint8_t *p = tmp_buf;
466 for (i = c - 1; i >= 0; i--) {
467 p[i * 2 + 1] = p[i] & 0xf;
468 p[i * 2] = p[i] >> 4;
472 ret = alloc_surface(bmp.width, bmp.height, 8);
476 ret->pixels = tmp_buf;
488 DECLSPEC SDL_Surface * SDLCALL
489 SDL_ConvertSurface(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags)
494 trace("%p, %p, %08x", src, fmt, flags);
496 if (src->format->BitsPerPixel != fmt->BitsPerPixel) {
497 err("convert: not implemented");
501 data = malloc(src->pitch * src->h);
504 memcpy(data, src->pixels, src->pitch * src->h);
506 ret = alloc_surface(src->w, src->h, fmt->BitsPerPixel);
516 SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
518 trace("%p, %08x, %04x", surface, flag, key);
522 DECLSPEC void SDLCALL
523 SDL_WM_SetCaption(const char *title, const char *icon)
525 trace("%s, %s", title, icon);
529 SDL_ShowCursor(int toggle)
535 DECLSPEC char * SDLCALL