shithub: npe

ref: 979ae53cfee913c2d303ab0f95aaa65dace1c7b5
dir: /libnpe_sdl3/window.c/

View raw version
#include "_sdl.h"

struct SDL_Window {
	int dummy;
};
static SDL_Window onewin;

void
SDL_SetWindowIcon(SDL_Window *w, SDL_Surface *icon)
{
	USED(w); USED(icon);
}

void
SDL_SetWindowBordered(SDL_Window *w, SDL_bool flag)
{
	USED(w); USED(flag);
}

Uint32
SDL_GetWindowPixelFormat(SDL_Window *)
{
	if(screen != nil)
		return chan2pixel(screen->chan);
	return 0;
}

void
SDL_GetWindowSize(SDL_Window *, int *w, int *h)
{
	/* no matter what rio decides */
	*w = npe_sdl.physw;
	*h = npe_sdl.physh;
}

bool
SDL_GetWindowSizeInPixels(SDL_Window *, int *w, int *h)
{
	*w = Dx(screen->r);
	*h = Dy(screen->r);
	return true;
}

void
SDL_GetWindowPosition(SDL_Window *, int *x, int *y)
{
	*x = screen->r.min.x;
	*y = screen->r.min.y;
}

int
SDL_GetWindowBordersSize(SDL_Window *, int *t, int *l, int *b, int *r)
{
	if(t != nil)
		*t = Borderwidth;
	if(l != nil)
		*l = Borderwidth;
	if(b != nil)
		*b = Borderwidth;
	if(r != nil)
		*r = Borderwidth;
	return 0;
}

void
SDL_SetWindowSize(SDL_Window *, int w, int h)
{
	int f, n;

	if(w == 0 || h == 0)
		return;
	if(npe_sdl.physw != w || npe_sdl.physh != h){
		if((f = open("/dev/wctl", OWRITE|OCEXEC)) >= 0){
			n = fprint(f, "resize -dx %d -dy %d", w+Borderwidth*2, h+Borderwidth*2);
			if(n > 0){
				while(getwindow(display, Refnone) != 1)
					;
				npe_sdl.physw = w;
				npe_sdl.physh = h;
				npe_sdl.fullredraw = 1;
			}else{
				fprint(2, "SDL_SetWindowSize: resize: %r\n");
			}
			close(f);
		}else{
			fprint(2, "SDL_SetWindowSize: open: %r\n");
		}
	}
}

void
SDL_SetWindowMinimumSize(SDL_Window *window, int min_w, int min_h)
{
	SDL_SetWindowSize(window, min_w, min_h);
}

int
SDL_ShowSimpleMessageBox(Uint32, char *title, char *message, SDL_Window *)
{
	/* FIXME display a GUI window? */
	fprint(2, "%s: %s\n", title, message);
	return 0;
}

int
SDL_SetWindowFullscreen(SDL_Window *, Uint32)
{
	/* FIXME again, ft2 does NOT check the error code, figure something out */
	werrstr("SDL_SetWindowFullscreen: not implemented");
	return -1;
}

void
SDL_SetWindowGrab(SDL_Window *, SDL_bool grabbed)
{
	/* FIXME not sure if it's worth anything */
	USED(grabbed);
}

void
SDL_SetWindowPosition(SDL_Window *, int x, int y)
{
	int f, n;

	if((f = open("/dev/wctl", OWRITE|OCEXEC)) >= 0){
		n = fprint(f, "move -minx %d -miny %d", x, y);
		close(f);
		if(n > 0){
			while(getwindow(display, Refnone) != 1)
				;
			npe_sdl.fullredraw = 1;
			npe_sdl.grabout = insetrect(screen->r, Dx(screen->r)/8);
			npe_sdl.center = addpt(screen->r.min, Pt(Dx(screen->r)/2, Dy(screen->r)/2));
		}
	}
}

void
SDL_RestoreWindow(SDL_Window *)
{
	/* nothing to do here */
}

void
SDL_RaiseWindow(SDL_Window *)
{
	/* nothing to do here */
}

void
SDL_ShowWindow(SDL_Window *)
{
	/* nothing to do here */
}

bool
SDL_SyncWindow(SDL_Window *)
{
	return true;
}

bool
SDL_CreateWindowAndRenderer(const char *title, int w, int h, SDL_WindowFlags f, SDL_Window **win, SDL_Renderer **rend)
{
	USED(f);
	SDL_SetWindowSize(&onewin, w, h);
	*win = &onewin;
	*rend = SDL_CreateRenderer(&onewin, nil);
	SDL_SetWindowTitle(&onewin, title);
	return true;
}

void
SDL_DestroyWindow(SDL_Window *)
{
}

SDL_Window *
SDL_CreateWindow(char *title, int x, int y, int w, int h, Uint32)
{
	SDL_SetWindowTitle(&onewin, title);
	SDL_SetWindowSize(&onewin, w, h);

	if(x != SDL_WINDOWPOS_UNDEFINED && y != SDL_WINDOWPOS_UNDEFINED){ /* FIXME either of these can be undefined */
		if(x == SDL_WINDOWPOS_CENTERED)
			x = display->image->r.min.x + (Dx(display->image->r) - w)/2;
		if(y == SDL_WINDOWPOS_CENTERED)
			y = display->image->r.min.y + (Dy(display->image->r) - h)/2;
		SDL_SetWindowPosition(&onewin, x, y);
	}

	return &onewin;
}