api_stash.c 2.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2015 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source 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
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <api_stash.h>

20
__realtime int
21 22
_lstash__gc(lua_State *L)
{
Hanspeter Portner's avatar
Hanspeter Portner committed
23 24
	lstash_t *lstash = lua_touserdata(L, 1);
	atom_ser_t *ser = &lstash->ser;
25

Hanspeter Portner's avatar
Hanspeter Portner committed
26
	if(ser->buf)
27
		moony_rt_free(ser->vm, ser->buf, ser->size);
28

Hanspeter Portner's avatar
Hanspeter Portner committed
29
	return 0;
30 31
}

32
__realtime int
33 34
_lstash_write(lua_State *L)
{
Hanspeter Portner's avatar
Hanspeter Portner committed
35
	lstash_t *lstash = lua_touserdata(L, 1);
36
	atom_ser_t *ser = &lstash->ser;
Hanspeter Portner's avatar
Hanspeter Portner committed
37
	lforge_t *lforge = &lstash->lforge;
38 39 40 41 42 43

	lforge->depth = 0;
	lforge->last.frames = 0;
	lforge->forge = &lstash->forge;
	
	ser->offset = 0; // reset stash pointer
44
	lv2_atom_forge_set_sink(&lstash->forge, _sink_rt, _deref, ser);
45 46 47 48 49

	LV2_Atom *atom = (LV2_Atom *)ser->buf;
	atom->type = 0;
	atom->size = 0;

Hanspeter Portner's avatar
Hanspeter Portner committed
50 51
	luaL_getmetatable(L, "lforge");
	lua_setmetatable(L, 1);
52 53 54 55

	return 1;
}

56
__realtime int
57 58
_lstash_read(lua_State *L)
{
Hanspeter Portner's avatar
Hanspeter Portner committed
59
	lstash_t *lstash = lua_touserdata(L, 1);
60
	atom_ser_t *ser = &lstash->ser;
Hanspeter Portner's avatar
Hanspeter Portner committed
61
	latom_t *latom = &lstash->latom;
62 63 64 65

	latom->atom = (const LV2_Atom *)ser->buf;
	latom->body.raw = LV2_ATOM_BODY_CONST(latom->atom);

Hanspeter Portner's avatar
Hanspeter Portner committed
66 67
	luaL_getmetatable(L, "latom");
	lua_setmetatable(L, 1);
68 69 70 71

	return 1;
}

72
__realtime int
73 74 75
_lstash(lua_State *L)
{
	moony_t *moony = lua_touserdata(L, lua_upvalueindex(1));
76
	moony_vm_t *vm = lua_touserdata(L, lua_upvalueindex(2));
77

78
	lstash_t *lstash = moony_newuserdata(L, moony, MOONY_UDATA_STASH, false);
79 80 81 82 83 84

	// initialize forge (URIDs)
	memcpy(&lstash->forge, &moony->forge, sizeof(LV2_Atom_Forge));

	// initialize memory pool
	atom_ser_t *ser = &lstash->ser;
85
	ser->vm = vm;
86
	ser->size = 1024;
87
	ser->offset = 0; // reset stash pointer
88
	ser->buf = moony_rt_alloc(vm, ser->size);
89 90 91 92

	if(!ser->buf)
		lua_pushnil(L); // memory allocation failed

Hanspeter Portner's avatar
Hanspeter Portner committed
93 94
	// start off as forge
	_lstash_write(L);
95

96 97 98 99 100 101
	return 1;
}

const luaL_Reg lstash_mt [] = {
	{NULL, NULL}
};