/*
 * sdl12shim.h - the parts of the SDL 1.2 API that Kobo Deluxe uses, on top of SDL2.
 *
 * Written for fredygames.com to build Kobo Deluxe for the browser with Emscripten's SDL2 port.
 * Copyright (C) 2026 FredyGames. Same license as Kobo Deluxe: GNU GPL version 2 or later.
 *
 * Included before every source file of the game (emcc -include). Only covers what the game calls:
 * the video mode (one window, a 32-bit software screen surface shown through a streaming texture),
 * SDL 1.2 surface flags for colour keys and alpha, DisplayFormat, audio in the requested format, and
 * a few window/keyboard calls.
 */
#ifndef SDL12SHIM_H
#define SDL12SHIM_H

#include "SDL.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef SDL_Keycode SDLKey;

/* SDL 1.2 flags. Values chosen so they never collide with SDL2 surface flags. */
#define SDL_SRCCOLORKEY		0x00001000
#define SDL_SRCALPHA		0x00010000
#define SDL_HWSURFACE		0x00100000
#define SDL_ASYNCBLIT		0x00200000
#define SDL_DOUBLEBUF		0x00400000
#define SDL_FULLSCREEN		0x00800000
#define SDL_OPENGL		0x01000000
#define SDL_HWACCEL		0x02000000

/* Old key names */
#define SDLK_KP0		SDLK_KP_0
#define SDLK_KP1		SDLK_KP_1
#define SDLK_KP2		SDLK_KP_2
#define SDLK_KP3		SDLK_KP_3
#define SDLK_KP4		SDLK_KP_4
#define SDLK_KP5		SDLK_KP_5
#define SDLK_KP6		SDLK_KP_6
#define SDLK_KP7		SDLK_KP_7
#define SDLK_KP8		SDLK_KP_8
#define SDLK_KP9		SDLK_KP_9
#define SDLK_PRINT		SDLK_PRINTSCREEN
#define KMOD_META		KMOD_GUI

typedef enum { SDL_GRAB_QUERY = -1, SDL_GRAB_OFF = 0, SDL_GRAB_ON = 1 } SDL_GrabMode;

SDL_Surface *SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags);
SDL_Surface *SDL_GetVideoSurface(void);
int SDL_Flip(SDL_Surface *screen);
void SDL_UpdateRects(SDL_Surface *screen, int numrects, SDL_Rect *rects);
void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);
void SDL_WM_SetCaption(const char *title, const char *icon);
SDL_GrabMode SDL_WM_GrabInput(SDL_GrabMode mode);
int SDL_EnableUNICODE(int enable);
int SDL_EnableKeyRepeat(int delay, int interval);
int SDL_JoystickOpened(int device_index);
int SDL_SetAlpha(SDL_Surface *surface, Uint32 flags, Uint8 alpha);
int SDL12_SetColorKey(SDL_Surface *surface, Uint32 flags, Uint32 key);
int SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
SDL_Surface *SDL_DisplayFormat(SDL_Surface *surface);
SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface);
Uint32 SDL12_SurfaceFlags(SDL_Surface *surface);
Uint8 SDL12_SurfaceAlpha(SDL_Surface *surface);
Uint32 SDL12_SurfaceColorKey(SDL_Surface *surface);
int SDL12_KeyUnicode(const SDL_Keysym *keysym);
int SDL12_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained);

/* SDL 1.2 colour keys: SDL_SRCCOLORKEY turns the key on; any other flag (SDL_RLEACCEL) alone turns it off. */
#define SDL_SetColorKey(s, f, k)	SDL12_SetColorKey((s), (f), (k))
/* SDL 1.2 converted to the format the game asked for; SDL2 only does that without "obtained". */
#define SDL_OpenAudio(d, o)		SDL12_OpenAudio((d), (o))
#define SDL_AllocSurface		SDL_CreateRGBSurface
#define SDL_putenv(s)			SDL_setenv_unsafe_shim(s)
static inline int SDL_setenv_unsafe_shim(char *s)
{
	char name[64];
	const char *eq = SDL_strchr(s, '=');
	if(!eq || (size_t)(eq - s) >= sizeof(name))
		return -1;
	SDL_memcpy(name, s, eq - s);
	name[eq - s] = 0;
	return SDL_setenv(name, eq + 1, 1);
}

#ifdef __cplusplus
}
#endif

#endif /* SDL12SHIM_H */
