aboutsummaryrefslogtreecommitdiff
path: root/mod/keycallback.c
diff options
context:
space:
mode:
Diffstat (limited to 'mod/keycallback.c')
-rw-r--r--mod/keycallback.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/mod/keycallback.c b/mod/keycallback.c
new file mode 100644
index 0000000..2352cbf
--- /dev/null
+++ b/mod/keycallback.c
@@ -0,0 +1,147 @@
+/*
+ * melisma - extensible and scriptable audio engine
+ * Copyright (C) 2026 Ludvig Gunne Lindström
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+#include <lauxlib.h>
+#include "melisma.h"
+#include "melisma/ring.h"
+
+static int fn = LUA_NOREF;
+static struct termios tty = {0};
+static struct termios oldtty = {0};
+static lua_State *state;
+static ml_ring *ring = NULL;
+
+static void make_arg(lua_State *s, size_t len, const void *data)
+{
+ lua_pushlstring(s,data,len);
+}
+
+static void io_handler(int fd, int flags, void *args)
+{
+ char c;
+
+ if (read(fd,&c,sizeof c) <= 0)
+ {
+ return;
+ }
+
+ ml_ring_put_blocking(ring,*(uint8_t*) &c);
+}
+
+static void cleanup(void)
+{
+ tcsetattr(STDIN_FILENO,TCSANOW,&oldtty);
+}
+
+static void proc(const ml_proc_args *args)
+{
+ if (fn == LUA_NOREF)
+ {
+ return;
+ }
+
+ char buf[1];
+ while (ml_ring_read(ring,buf,sizeof buf) == sizeof buf)
+ {
+ lua_pushlstring(state,buf,sizeof buf);
+ int arg = luaL_ref(state,LUA_REGISTRYINDEX);
+ ml_schedule_event(0,fn,arg);
+ }
+}
+
+static void setup(lua_State *s)
+{
+ static bool init = false;
+
+ if (init)
+ {
+ return;
+ }
+
+ init = true;
+
+ tcgetattr(STDIN_FILENO,&oldtty);
+ tty = oldtty;
+ tty.c_lflag &= ~ECHO;
+ tty.c_lflag &= ~ICANON;
+ tcsetattr(STDIN_FILENO,TCSANOW,&tty);
+ tcflush(STDIN_FILENO,TCIFLUSH);
+
+ ml_add_io_handler(STDIN_FILENO,POLLIN,io_handler,NULL);
+ ring = ml_ring_init(4096);
+ state = s;
+
+ ml_node *node = ml_create_node(proc,NULL);
+ ml_channel *src = ml_add_output(node);
+ ml_channel *dst = ml_add_input(ml_get_root_node());
+ ml_connect(src,dst);
+ ml_label_node(node,"keycallback");
+
+ atexit(cleanup);
+}
+
+static int l_set(lua_State *s)
+{
+ if (fn != LUA_NOREF)
+ {
+ luaL_unref(s,LUA_REGISTRYINDEX,fn);
+ fn = LUA_NOREF;
+ }
+
+ if (lua_isnil(s,1))
+ {
+ return 0;
+ }
+
+ luaL_checktype(s,1,LUA_TFUNCTION);
+ lua_pushvalue(s,1);
+ fn = luaL_ref(s,LUA_REGISTRYINDEX);
+
+ setup(s);
+
+ return 0;
+}
+
+int luaopen_melisma_keycallback(lua_State *s)
+{
+/*** Set callback for keyboard input.
+ local mkeycallback = require 'melisma.keycallback'
+
+ mkeycallback.set(function(c)
+ print('you pressed', c)
+ end)
+@module keycallback
+*/
+ lua_newtable(s);
+ int tbl = lua_absindex(s,lua_gettop(s));
+
+/*** Set the callback.
+@function set
+@tparam function fn Callback function.
+*/
+ lua_pushcfunction(s,l_set);
+ lua_setfield(s,tbl,"set");
+
+ lua_settop(s,tbl);
+ return 1;
+}