ref: aaad5f4c82f96f96103c545a5db4e46ef4e22d33
parent: 99c39874ceae2e7b0af75b43733baac8b2a50f35
author: Jacob Moody <moody@posixcafe.org>
date: Tue Feb 7 21:53:18 EST 2023
mouse grab
--- a/include/npe/SDL2/SDL.h
+++ b/include/npe/SDL2/SDL.h
@@ -66,6 +66,7 @@
int SDL_SetRelativeMouseMode(SDL_bool enabled);
int SDL_GetRelativeMouseMode(void);
void SDL_SetWindowIcon(SDL_Window*,SDL_Surface*);
+void SDL_SetWindowBordered(SDL_Window*,SDL_bool);
SDL_Keymod SDL_GetModState(void);
int SDL_ShowCursor(int toggle);
Uint64 SDL_GetPerformanceFrequency(void);
@@ -155,6 +156,7 @@
int SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color);
int SDL_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors, int firstcolor, int ncolors);
int SDL_BlitSurface(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
+int SDL_SoftStretch(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect);
enum {
SDL_QUERY = -1,
@@ -177,7 +179,7 @@
SDL_INIT_TIMER = 1<<0,
SDL_INIT_AUDIO = 1<<1,
SDL_INIT_VIDEO = 1<<2,
- SDL_INIT_JOYSTICK = 1<<3,
+ SDL_INIT_JOYSTICK = 0,
SDL_BLENDMODE_NONE = 0,
SDL_BLENDMODE_BLEND,
@@ -195,9 +197,12 @@
/* shit no one cares about */
SDL_TEXTUREACCESS_STREAMING = 0,
SDL_TEXTUREACCESS_STATIC = 0,
+ SDL_TEXTUREACCESS_TARGET = 0,
SDL_RENDERER_ACCELERATED = 0,
SDL_RENDERER_PRESENTVSYNC = 0,
SDL_INIT_NOPARACHUTE = 0,
+ SDL_RENDERER_SOFTWARE = 0,
+ SDL_SWSURFACE = 0,
/* FIXME steal from rio and add missing? */
SDL_SYSTEM_CURSOR_ARROW = 0,
--- a/include/npe/SDL2/SDL_events.h
+++ b/include/npe/SDL2/SDL_events.h
@@ -28,6 +28,7 @@
SDL_WINDOWEVENT_RESTORED,
SDL_WINDOWEVENT_ENTER,
SDL_WINDOWEVENT_LEAVE,
+ SDL_WINDOWEVENT_CLOSE,
SDL_JOYBALLMOTION,
SDL_PRESSED = SDL_KEYDOWN,
--- a/libnpe_sdl2/_sdl.h
+++ b/libnpe_sdl2/_sdl.h
@@ -12,6 +12,9 @@
struct npe_sdl {
Mousectl *mctl;
+ Rectangle grabout;
+ Point center;
+ int mgrab;
Mouse m, om;
int hints;
int mredraw;
--- a/libnpe_sdl2/events.c
+++ b/libnpe_sdl2/events.c
@@ -28,6 +28,7 @@
static int rune2scancode(Rune r);
static void kbdproc(void *);
+static void mouseproc(void *);
int
npe_sdl_init_input(void)
@@ -37,13 +38,16 @@
salt[Ckey].c = chancreate(sizeof(Rune), 20);
salt[Ckeytype].c = chancreate(sizeof(int), 20);
- salt[Cmouse].c = npe_sdl.mctl->c;
+ salt[Cmouse].c = chancreate(sizeof(Mouse), 20);
salt[Cresize].c = npe_sdl.mctl->resizec;
kctl.c = salt[Ckey].c; /* for enter() */
- if(salt[Ckey].c == nil || salt[Ckeytype].c == nil || proccreate(kbdproc, nil, 4096) < 0)
+ if(salt[Ckey].c == nil || salt[Ckeytype].c == nil || salt[Cmouse].c == nil)
return -1;
+ if(proccreate(kbdproc, nil, 4096) < 0 || proccreate(mouseproc, nil, 4096) < 0)
+ return -1;
+
return 0;
}
@@ -130,9 +134,13 @@
case Cmouse:
if(screen == nil)
break;
+
memset(e, 0, sizeof(*e));
e->motion.x = (npe_sdl.m.xy.x - screen->r.min.x) * npe_sdl.scale;
e->motion.y = (npe_sdl.m.xy.y - screen->r.min.y) * npe_sdl.scale;
+ e->motion.xrel = (npe_sdl.m.xy.x - npe_sdl.om.xy.x) * npe_sdl.scale;
+ e->motion.yrel = (npe_sdl.m.xy.y - npe_sdl.om.xy.y) * npe_sdl.scale;
+
if(!eqpt(npe_sdl.m.xy, npe_sdl.om.xy)){
npe_sdl.mredraw = 1;
if(npe_sdl.m.buttons == npe_sdl.om.buttons){
@@ -352,4 +360,22 @@
}
threadexits(nil);
+}
+
+static void
+mouseproc(void *)
+{
+ Mouse m;
+ for(;;){
+ recv(npe_sdl.mctl->c, &m);
+ if(npe_sdl.mgrab == SDL_TRUE){
+ if(!ptinrect(m.xy, npe_sdl.grabout)){
+ moveto(npe_sdl.mctl, npe_sdl.center);
+ npe_sdl.m.xy = npe_sdl.center;
+ npe_sdl.om.xy = npe_sdl.center;
+ continue; /* swallow */
+ }
+ }
+ send(salt[Cmouse].c, &m);
+ }
}
--- a/libnpe_sdl2/sdl2.c
+++ b/libnpe_sdl2/sdl2.c
@@ -77,6 +77,12 @@
USED(w); USED(icon);
}
+void
+SDL_SetWindowBordered(SDL_Window *w, SDL_bool flag)
+{
+ USED(w); USED(flag);
+}
+
int
SDL_Init(int mask)
{
@@ -940,6 +946,8 @@
if(n > 0){
getwindow(display, Refnone);
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));
}
}
}
@@ -1319,7 +1327,13 @@
int
SDL_SetRelativeMouseMode(SDL_bool enabled)
{
- /* FIXME implement mouse grab */
+ 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));
+ npe_sdl.mgrab = enabled;
+ if(enabled)
+ SDL_ShowCursor(0);
+ else
+ SDL_ShowCursor(1);
return 0;
}
@@ -1331,7 +1345,7 @@
int
SDL_GetRelativeMouseMode(void)
{
- return SDL_FALSE;
+ return npe_sdl.mgrab;
}
SDL_mutex*