1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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;
}
|