shithub: lua9

Download patch

ref: 1dded09f619d53f0b31d51870a9984aaf0152e02
parent: 4fc7db38e0451a3569a039b8d15d71bcb5b31250
author: telephil9 <telephil9@gmail.com>
date: Sun Oct 25 17:44:22 EDT 2020

Added emenuhit()

	This is exported as event.menuhit(button, mouse, menu).
	Menu is expected to be a table of strings.

--- a/event.c
+++ b/event.c
@@ -106,7 +106,44 @@
 	return 1;
 }
 
+static char**
+checkstrings(lua_State *L, int index)
+{
+	char **items;
+	int n, i;
+
+	if(lua_istable(L, index) == 0)
+		luaL_argerror(L, index, "table of strings expected");
+	n = luaL_len(L, index);
+	if(n == 0)
+		luaL_argerror(L, index, "table of strings is empty");
+	items = calloc(n + 1, sizeof(char*));
+	if(items == nil)
+		luaL_error(L, "out of memory");
+	items[n] = NULL;
+	for(i = 0; i < n; i++){
+		lua_rawgeti(L, index, i + 1);
+		items[i] = luaL_checkstring(L, lua_gettop(L));
+	}
+	return items;
+}
+
 static int
+lmenuhit(lua_State *L)
+{
+	int b, r;
+	Mouse m;
+	Menu menu;
+
+	b = luaL_checkinteger(L, 1);
+	m = checkmouse(L, 2);
+	menu.item = checkstrings(L, 3);
+	r = emenuhit(b, &m, &menu);
+	lua_pushinteger(L, r);
+	return 1;
+}
+
+static int
 lenter(lua_State *L)
 {
 	char buf[1024]; /* XXX do it better */
@@ -135,6 +172,7 @@
 	{ "cankbd",    lcankbd },
 	{ "timer",     ltimer },
 	{ "enter",     lenter },
+	{ "menuhit",   lmenuhit },
 	{ NULL, NULL }
 };