8 #include "linux/fbdev.h"
10 #define err(fmt, ...) fprintf(stderr, "psdl: " fmt "\n", ##__VA_ARGS__)
12 #define trace(fmt, ...) printf(" %s(" fmt ")\n", __FUNCTION__, ##__VA_ARGS__)
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;
23 static Uint8 g_keystate[SDLK_LAST];
25 static inline int min(int v1, int v2)
27 return v1 < v2 ? v1 : v2;
30 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
32 static SDL_Surface *alloc_surface(int w, int h, int bpp)
34 // SDL has some pointer overuse, every surface has a format,
35 // why make format accessible through pointer?
41 ret = calloc(1, sizeof(*ret));
45 ret->s.format = &ret->f;
46 ret->f.BitsPerPixel = bpp;
47 ret->f.BytesPerPixel = (bpp + 7) / 8;
51 ret->s.pitch = w * ret->f.BytesPerPixel;
58 SDL_Init(Uint32 flags)
63 g_start_ticks = SDL_GetTicks();
75 SDL_InitSubSystem(Uint32 flags)
82 SDL_QuitSubSystem(Uint32 flags)
87 DECLSPEC Uint32 SDLCALL
93 gettimeofday(&tv, NULL);
94 ret = tv.tv_sec * 1000;
95 ret += tv.tv_usec * 1048 >> 20;
98 dbg(" SDL_GetTicks %d", ret);
102 DECLSPEC SDL_Surface * SDLCALL
103 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
105 struct vout_fbdev *fbdev;
107 trace("%d, %d, %d, %08x", width, height, bpp, flags);
109 if (bpp != 8 && bpp != 16) {
110 err("unsupported bpp: %d\n", bpp);
114 // XXX: destroy old g_screen?
116 g_screen = alloc_surface(width, height, bpp);
117 if (g_screen == NULL)
120 fbdev = vout_fbdev_init("/dev/fb0", &width, &height, 0);
122 goto fail_fbdev_init;
124 g_screen_fbp = vout_fbdev_flip(fbdev);
126 g_screen->pixels = g_screen_fbp;
128 // we have to fake this for now..
129 g_screen->pixels = calloc(g_screen->pitch * height, 1);
131 if (g_screen->pixels == NULL) {
135 g_screen->hwdata = (void *)fbdev;
137 dbg("returning %p", g_screen);
141 vout_fbdev_finish(fbdev);
149 SDL_Flip(SDL_Surface *screen)
151 struct vout_fbdev *fbdev;
155 if (screen != g_screen) {
156 err("flip not on screen surface?");
160 if (screen->format->BitsPerPixel == 8) {
161 int l = screen->pitch * screen->h;
163 do_clut(g_screen_fbp, screen->pixels, g_8bpp_pal, l);
165 Uint16 *d = g_screen_fbp;
166 Uint8 *s = screen->pixels;
168 // XXX: perhaps optimize this sh*t
169 for (; l > 0; d++, s++, l--)
174 fbdev = (void *)screen->hwdata;
175 g_screen_fbp = vout_fbdev_flip(fbdev);
177 if (screen->format->BitsPerPixel != 8)
178 screen->pixels = g_screen_fbp;
183 static int do_rect_clip(const SDL_Surface *s, SDL_Rect *r)
185 int x = r->x, y = r->y, w = r->w, h = r->h;
210 r->x = x; r->y = y; r->w = w; r->h = h;
211 return (w > 0 && h > 0) ? 0 : -1;
215 SDL_UpperBlit(SDL_Surface *src, SDL_Rect *srcrect,
216 SDL_Surface *dst, SDL_Rect *dstrect)
218 int sx = 0, sy = 0, sw, sh;
220 SDL_Rect tmprect = { 0, };
222 trace("%p, %p, %p, %p", src, srcrect, dst, dstrect);
224 if (src == NULL || dst == NULL)
227 if (srcrect != NULL) {
228 // XXX: dst pos may need to be adjusted in some corner cases
229 if (do_rect_clip(src, srcrect) < 0)
243 // SDL just uses source w and h
246 if (do_rect_clip(dst, dstrect) < 0)
253 dbg("(%d %d %d %d) -> (%d %d %d %d)", sx, sy, sw, sh, dx, dy, sw, sh);
255 if (src->format->BitsPerPixel == dst->format->BitsPerPixel) {
256 int Bpp = src->format->BytesPerPixel;
257 int dpitch = dst->pitch;
258 int spitch = src->pitch;
259 Uint8 *d = (Uint8 *)dst->pixels + dpitch * dy + dx * Bpp;
260 Uint8 *s = (Uint8 *)src->pixels + spitch * sy + sx * Bpp;
262 for (sw *= Bpp; sh > 0; d += dpitch, s += spitch, sh--)
270 DECLSPEC SDL_Surface * SDLCALL
271 SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth,
272 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
276 trace("%08x, %d, %d, %d, %04x, %04x, %04x, %04x",
277 flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
279 ret = alloc_surface(width, height, depth);
283 ret->pixels = calloc(ret->pitch * height, 1);
284 if (ret->pixels == NULL)
287 dbg("returning %p", ret);
296 SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
300 trace("%p, %p, %d, %d", surface, colors, firstcolor, ncolors);
302 if (surface != g_screen)
305 to = min(ARRAY_SIZE(g_8bpp_pal), firstcolor + ncolors);
306 for (i = firstcolor; i < to; i++) {
307 SDL_Color *c = &colors[i - firstcolor];
308 int r = c->r, g = c->g, b = c->b;
309 // noiz2sa gets a good deal darker that real SDL,
310 // don't know what's going on there
312 if (r < 0xf8 && (r & 7) > 3) r += 8;
313 if (g < 0xfc && (g & 3) > 1) g += 4;
314 if (b < 0xf8 && (b & 7) > 3) b += 8;
316 g_8bpp_pal[i] = ((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3);
317 // g_8bpp_pal[i] += 0x0821;
324 SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
326 trace("%p, %p, %04x", dst, dstrect, color);
328 if (dst->format->BytesPerPixel == 2) {
329 err("SDL_FillRect TODO");
333 memset(dst->pixels, color, dst->pitch * dst->h);
339 DECLSPEC SDL_Surface * SDLCALL
340 SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch,
341 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
346 DECLSPEC SDL_RWops * SDLCALL
347 SDL_RWFromFile(const char *file, const char *mode)
352 trace("%s, %s", file, mode);
354 f = fopen(file, mode);
358 ret = calloc(1, sizeof(*ret));
363 ret->hidden.stdio.fp = f;
368 DECLSPEC void SDLCALL
369 SDL_FreeRW(SDL_RWops *area)
371 if (area->hidden.stdio.fp) {
372 fclose(area->hidden.stdio.fp);
373 area->hidden.stdio.fp = NULL;
378 DECLSPEC SDL_Surface * SDLCALL
379 SDL_LoadBMP_RW(SDL_RWops *src, int freesrc)
382 unsigned char magic[2];
392 uint32_t compress_type;
398 } __attribute__((packed)) bmp;
399 SDL_Surface *ret = NULL;
400 int data_size, read_size;
405 trace("%p, %d", src, freesrc);
407 f = src->hidden.stdio.fp;
411 if (fread(&bmp, 1, sizeof(bmp), f) != sizeof(bmp)) {
412 err("bmp read error");
416 if (bmp.magic[0] != 'B' || bmp.magic[1] != 'M' || bmp.header_sz != 40) {
417 err("unhandled BMP format, sz=%d", bmp.header_sz);
421 if ((unsigned)bmp.width > 0xffff || (unsigned)bmp.height > 0xffff) {
422 err("BMP size %dx%d out of range", bmp.width, bmp.height);
426 if (bmp.bitspp != 4) {
427 err("BMP unhandled bpp: %d", bmp.bitspp);
431 bytespp = bmp.bitspp < 8 ? 1 : (bmp.bitspp + 7) / 8;
432 data_size = bmp.width * bmp.height * bytespp;
433 tmp_buf = malloc(data_size);
437 if (fseek(f, bmp.bmp_offset, SEEK_SET) != 0) {
438 err("BMP seek error");
442 read_size = bmp.width * bmp.bitspp / 8;
443 for (i = bmp.height - 1; i > 0; i--) {
444 if (fread((char *)tmp_buf + i * read_size, 1, read_size, f) != read_size) {
445 err("BMP read error");
450 if (bmp.bitspp == 4) {
451 // just convert to 8bpp now
452 int c = bmp.width * bmp.height / 2;
453 uint8_t *p = tmp_buf;
454 for (i = c - 1; i >= 0; i--) {
455 p[i * 2 + 1] = p[i] & 0xf;
456 p[i * 2] = p[i] >> 4;
460 ret = alloc_surface(bmp.width, bmp.height, 8);
464 ret->pixels = tmp_buf;
476 DECLSPEC SDL_Surface * SDLCALL
477 SDL_ConvertSurface(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags)
482 trace("%p, %p, %08x", src, fmt, flags);
484 if (src->format->BitsPerPixel != fmt->BitsPerPixel) {
485 err("convert: not implemented");
489 data = malloc(src->pitch * src->h);
492 memcpy(data, src->pixels, src->pitch * src->h);
494 ret = alloc_surface(src->w, src->h, fmt->BitsPerPixel);
504 SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
506 trace("%p, %08x, %04x", surface, flag, key);
510 DECLSPEC SDL_Joystick * SDLCALL
511 SDL_JoystickOpen(int device_index)
513 trace("%d", device_index);
518 SDL_PollEvent(SDL_Event *event)
524 DECLSPEC Uint8 * SDLCALL
525 SDL_GetKeyState(int *numkeys)
527 trace("%p", numkeys);
530 *numkeys = ARRAY_SIZE(g_keystate);
534 DECLSPEC void SDLCALL
535 SDL_WM_SetCaption(const char *title, const char *icon)
537 trace("%s, %s", title, icon);
541 SDL_ShowCursor(int toggle)
547 DECLSPEC char * SDLCALL