shithub: npe

Download patch

ref: e8d44e6791e9568d0a4e3291d15692f9223870f1
parent: 96089afd35646c653b1997b48cc4cf7bd5fa2e1a
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Sep 1 15:27:39 EDT 2024

sdl2: SDL_WaitEvent(NULL), SDL_PushEvent (thanks cgnarne)

--- a/include/npe/SDL2/SDL_events.h
+++ b/include/npe/SDL2/SDL_events.h
@@ -143,7 +143,7 @@
 int SDL_PollEvent(SDL_Event *event);
 int SDL_PushEvent(SDL_Event *event);
 int SDL_WaitEvent(SDL_Event *event);
-int SDL_RegisterEvents(int);
+Uint32 SDL_RegisterEvents(int);
 int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action, Uint32 minType, Uint32 maxType);
 Uint32 SDL_GetWindowID(SDL_Window *window);
 void SDL_PumpEvents(void);
--- a/libnpe_sdl2/events.c
+++ b/libnpe_sdl2/events.c
@@ -6,6 +6,7 @@
 	Ckeytype,
 	Cmouse,
 	Cresize,
+	Cevent,
 	Numchan,
 
 	Rdown = 0,
@@ -17,6 +18,7 @@
 static Rune rune;
 static Keyboardctl kctl;
 static int quitreq;
+static SDL_Event evt;
 
 static Alt salt[Numchan+1] = {
 	[Ckey] = { nil, &rune, CHANRCV },
@@ -23,6 +25,7 @@
 	[Ckeytype] = { nil, nil, CHANNOP },
 	[Cmouse] = { nil, &npe_sdl.m, CHANRCV },
 	[Cresize] = { nil, nil, CHANRCV },
+	[Cevent] = { nil, &evt, CHANRCV },
 	[Numchan] = { nil, nil, CHANNOBLK },
 };
 
@@ -39,6 +42,7 @@
 	salt[Ckeytype].c = chancreate(sizeof(int), 20);
 	salt[Cmouse].c = chancreate(sizeof(Mouse), 20);
 	salt[Cresize].c = npe_sdl.mctl->resizec;
+	salt[Cevent].c = chancreate(sizeof(SDL_Event), 20);
 	kctl.c = salt[Ckey].c; /* for enter() */
 
 	if(salt[Ckey].c == nil || salt[Ckeytype].c == nil || salt[Cmouse].c == nil)
@@ -69,19 +73,24 @@
 	return 0;
 }
 
-int
-SDL_RegisterEvents(int)
+Uint32
+SDL_RegisterEvents(int n)
 {
-	/* FIXME this will need implementation if SDL_PushEvent is */
-	return -1;
+	static Uint32 userevent = 0x1000;
+	Uint32 u;
+
+	if(userevent+n > 0x2000 || n < 0)
+		u = (Uint32)-1;
+	else
+		u = userevent += n;
+
+	return u;
 }
 
 int
 SDL_PushEvent(SDL_Event *event)
 {
-	/* FIXME does it matter? */
-	USED(event);
-	return -1;
+	return send(salt[Cevent].c, event) > 0 ? 1 : -1;
 }
 
 int
@@ -209,6 +218,10 @@
 		e->window.event = SDL_WINDOWEVENT_EXPOSED;
 		e->window.windowID = 1; //TODO more then one?
 		goto Filter;
+
+	case Cevent:
+		memcpy(e, &evt, sizeof(*e));
+		goto Filter;
 	}
 
 	return 0;
@@ -223,9 +236,14 @@
 SDL_WaitEvent(SDL_Event *e)
 {
 	int r;
+	SDL_Event ee;
 
 	salt[Numchan].op = CHANEND;
-	r = SDL_PollEvent(e);
+	if(e == nil){
+		r = SDL_PollEvent(&ee);
+		SDL_PushEvent(&ee);
+	} else
+		r = SDL_PollEvent(e);
 	salt[Numchan].op = CHANNOBLK;
 
 	return r;