shithub: slug

Download patch

ref: fc6a696cb43ffd4dd373118bdae89a4053341414
parent: e929ff6b56ae907e96b25b65a6c91db90d8393d4
author: phil9 <[email protected]>
date: Wed Nov 30 09:44:37 EST 2022

expose window width and height as global variables on lua side

--- a/a.h
+++ b/a.h
@@ -16,7 +16,7 @@
 	Image *i;
 };
 
-void	resize(int, int);
+void	resize(lua_State*, int, int);
 
 void	drawcanvas(void);
 void	initstate(void);
--- a/api.c
+++ b/api.c
@@ -43,7 +43,7 @@
 		return 0;
 	width = w;
 	height = h;
-	resize(w, h);
+	resize(L, w, h);
 	return 0;
 }
 
--- a/slug.c
+++ b/slug.c
@@ -38,7 +38,7 @@
 }
 
 void
-resize(int w, int h)
+resize(lua_State *L, int w, int h)
 {
 	int fd, n;
 	char buf[255];
@@ -50,6 +50,10 @@
 	if(write(fd, buf, n) != n)
 		fprint(2, "write error: %r\n");
 	close(fd);
+	lua_pushnumber(L, w);
+	lua_setglobal(L, "width");
+	lua_pushnumber(L, h);
+	lua_setglobal(L, "height");
 }
 
 void
@@ -86,7 +90,7 @@
 	}
 	registerfuncs(L);
 	initstate();
-	resize(width, height);
+	resize(L, width, height);
 	drawing = 0;
 	lsetup(L);
 	drawing = 1;
@@ -98,7 +102,7 @@
 		case 1:
 			if(getwindow(display, Refnone)<0)
 				sysfatal("getwindow: %r");
-			resize(width, height);
+			resize(L, width, height);
 			drawcanvas();
 			break;
 		case 2: