aboutsummaryrefslogtreecommitdiff
path: root/src/event.c
blob: 2d5a41f7e8e4414a9453ff00272e5219ae81dfb7 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * 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 <stdbool.h>

#include <jack/jack.h>

#include <lua.h>
#include <lauxlib.h>

#include "mem.h"
#include "event.h"
#include "util.h"
#include "melisma.h"

#define WHEEL_SIZE 4096
#define MAX_EVENTS 2048
#define MAINTHREAD_EVENT_CAPACITY (1<<17)
#define EVENT_HANDLE_TYPENAME "event handle"

struct event
{
	ml_event_id   id;
	unsigned long seq;
	unsigned long rounds;
	int           fn;
	int           arg;
	ml_event_id   next_sched;
	ml_event_id   next_free;
	bool          cancel;
};

extern jack_client_t *client;

static struct event *pool;
static size_t        pool_len;
static size_t        pool_cap;
static size_t        num_events;
static size_t        counter;
static ml_event_id   free_head = NO_EVENT;
static ml_event_id   wheel[WHEEL_SIZE];
static size_t        cursor;
static int           fn_cache_ref;

static void init_event(ml_event_id id)
{
	struct event *e = &pool[id];
	e->seq = counter++;
	e->next_free = NO_EVENT;
	e->next_sched = NO_EVENT;
	e->cancel = false;
}

static ml_event_id alloc_event(void)
{
	ml_event_id id;

	if (free_head != NO_EVENT)
	{
		id = free_head;
		free_head = pool[id].next_free;
		goto done;
	}

	if (pool_len == pool_cap)
	{
		pool_cap = pool_cap == 0 ? 64 : pool_cap * 2;
		pool = srealloc(pool,sizeof(*pool)*pool_cap);
	}

	id = pool_len++;

done:
	init_event(id);
	num_events++;
	return id;
}

static void free_event(ml_event_id id)
{
	pool[id].seq = (unsigned long) -1;

	if (id == pool_len - 1)
	{
		pool_len--;
	}
	else
	{
		pool[id].next_free = free_head;
		free_head = id;
	}

	num_events--;
}

static void schedule(ml_event_id id, unsigned long t)
{
	if (num_events == MAX_EVENTS)
	{
		fprintf(stderr,"WARNING: event dropped\n");
		return;
	}

	unsigned long rounds = t / WHEEL_SIZE;
	unsigned long index = (t+cursor) % WHEEL_SIZE;
	ml_event_id next = wheel[index];
	ml_event_id prev = NO_EVENT;

	while (next != NO_EVENT && pool[next].rounds <= rounds)
	{
		prev = next;
		next = pool[next].next_sched;
	}

	if (prev == NO_EVENT)
	{
		wheel[index] = id;
	}
	else
	{
		pool[prev].next_sched = id;
	}

	pool[id].rounds = rounds;
	pool[id].next_sched = next;
}

static bool cancel(long id, long seq)
{
	extern lua_State *state;
	struct event *e = &pool[id];

	if (e->seq == seq)
	{
		luaL_unref(state,LUA_REGISTRYINDEX,e->arg);
		e->arg = LUA_NOREF;
		e->cancel = true;
		return true;
	}

	return false;
}

ml_event_handle ml_schedule_event(unsigned long t, int fn, int arg)
{
	ml_event_id id = alloc_event();

	struct event *e = &pool[id];
	e->fn = fn;
	e->arg = arg;

	schedule(id,t);

	ml_event_handle hdl = {
		.id = id,
		.seq = e->seq,
	};
	return hdl;
}

void ml_cancel_event(const ml_event_handle *hdl)
{
	cancel(hdl->id,hdl->seq);
}

ml_event_id next_event(unsigned long tmax, unsigned long *t)
{
	*t = 0;

	while (true)
	{
		if (*t == tmax)
		{
			return NO_EVENT;
		}

		if (cursor == WHEEL_SIZE)
		{
			cursor = 0;
		}

		ml_event_id id = wheel[cursor];

		if (id == NO_EVENT)
		{
			cursor++;
			(*t)++;
			continue;
		}

		if (pool[id].rounds > 0)
		{
			while (id != NO_EVENT)
			{
				pool[id].rounds--;
				id = pool[id].next_sched;
			}

			cursor++;
			(*t)++;
			continue;
		}

		wheel[cursor] = pool[id].next_sched;

		if (pool[id].cancel)
		{
			free_event(id);
			continue;
		}

		return id;
	}
}

void dispatch_event(ml_event_id id)
{
	struct event *e = &pool[id];

	int fn = e->fn;
	int arg = e->arg;

	extern lua_State *state;

	lua_rawgeti(state,LUA_REGISTRYINDEX,fn);
	lua_rawgeti(state,LUA_REGISTRYINDEX,arg);

	if (lua_pcall_with_backtrace(state,1,0) != LUA_OK)
	{
		const char *err = luaL_checkstring(state,-1);
		fprintf(stderr,"%s\n",err);
		lua_pop(state,1);
	}

	luaL_unref(state,LUA_REGISTRYINDEX,pool[id].arg);

	free_event(id);
}

static int make_fn_ref(lua_State *s, int fn)
{
	int ref;
	int top = lua_gettop(s);

	lua_pushvalue(s,fn);
	fn = lua_gettop(s);

	lua_rawgeti(s,LUA_REGISTRYINDEX,fn_cache_ref);
	int fn_cache = lua_gettop(s);

	lua_pushvalue(s,fn);
	lua_gettable(s,fn_cache);

	if (lua_isinteger(s,-1))
	{
		ref = lua_tointeger(s,-1);
		goto done;
	}

	lua_pushvalue(s,fn);
	ref = luaL_ref(s,LUA_REGISTRYINDEX);

	lua_pushvalue(s,fn);
	lua_pushinteger(s,ref);
	lua_settable(s,fn_cache);

done:
	lua_settop(s,top);
	return ref;
}

static int l_schedule(lua_State *s)
{
	float real_t = luaL_checknumber(s,1);
	luaL_checktype(s,2,LUA_TFUNCTION);

	int fn = make_fn_ref(s,2);

	lua_pushvalue(s,3);
	int arg = luaL_ref(s,LUA_REGISTRYINDEX);

	unsigned long sr = ml_sample_rate();
	unsigned long t = sr * real_t;

	ml_event_id id = alloc_event();
	struct event *e = &pool[id];

	e->fn = fn;
	e->arg = arg;

	schedule(id,t);

	ml_event_handle *h = lua_newuserdata(s,sizeof *h);
	luaL_setmetatable(s,EVENT_HANDLE_TYPENAME);
	h->id = id;
	h->seq = e->seq;

	return 1;
}

static int l_cancel(lua_State *s)
{
	ml_event_handle *h = luaL_checkudata(s,1,EVENT_HANDLE_TYPENAME);
	lua_pushboolean(s,cancel(h->id,h->seq));
	return 1;
}

static int load_events(lua_State *s)
{
	luaL_newmetatable(s,EVENT_HANDLE_TYPENAME);

	lua_newtable(s);
	int tbl = lua_gettop(s);

	lua_pushcfunction(s,l_schedule);
	lua_setfield(s,tbl,"schedule");

	lua_pushcfunction(s,l_cancel);
	lua_setfield(s,tbl,"cancel");

	lua_rawgeti(s,LUA_REGISTRYINDEX,fn_cache_ref);
	lua_setfield(s,tbl,"function_cache");

	lua_settop(s,tbl);
	return 1;
}

static void create_module(lua_State *s)
{
	int top = lua_gettop(s);

	lua_getglobal(s,"package");
	int package = lua_gettop(s);

	lua_getfield(s,package,"preload");
	int preload = lua_gettop(s);

	lua_pushcfunction(s,load_events);
	lua_setfield(s,preload,"melisma.event");

	lua_settop(s,top);
}

static void create_fn_cache(lua_State *s)
{
	lua_newtable(s);
	fn_cache_ref = luaL_ref(s,LUA_REGISTRYINDEX);
}

void init_events(lua_State *s)
{
	create_fn_cache(s);
	create_module(s);

	for (size_t i = 0; i < WHEEL_SIZE; i++)
	{
		wheel[i] = NO_EVENT;
	}
}