8
#include <acpid/driver.h>
11
int acpi_channel_create(lua_State *L)
13
lua_pushinteger(L, 1);
16
const char *name = lua_tostring(L, -1);
17
const struct acpi_driver_ops *ops = acpi_driver_find(name);
23
lua_pushinteger(L, 2);
26
struct acpi_channel *channel = malloc(sizeof(struct acpi_channel));
27
int ret = (*ops->create)(channel, L);
33
lua_pushlightuserdata(L, channel);
38
void acpi_channel_register(struct acpi_channel *channel, const struct acpi_channel_ops *ops)
43
void acpi_channel_watch(struct acpi_channel *channel, struct acpi_channel_descriptor *cds, int fd, int events)
45
cds->channel = channel;
51
static int printL(lua_State *L)
54
for (int i = 1; i < lua_gettop(L); ++i)
55
printf("%s, ", lua_tostring(L, i));
56
printf("%s\n", lua_tostring(L, lua_gettop(L)));
61
static int execL(lua_State *L)
63
if (lua_gettop(L) > 12)
66
const char *args[12] = { lua_tostring(L, 1) };
67
for (int i = 1; i < 12; ++i)
68
args[i] = lua_tostring(L, i + 1);
75
execvp(lua_tostring(L, 1), args);
78
waitpid(pid, NULL, 0);
84
int acpi_channel_event(struct acpi_channel *channel, lua_State *L, const char *hid, unsigned long event, unsigned long data)
86
/* Send a dbus event */
87
acpi_dbus_event(hid, event, data);
89
/* Setup en basic Lua environment */
90
lua_pushcclosure(L, execL, 0);
91
lua_setfield(L, LUA_GLOBALSINDEX, "exec");
93
lua_pushcclosure(L, printL, 0);
94
lua_setfield(L, LUA_GLOBALSINDEX, "print");
96
/* First we let the channel handler process the event. */
97
lua_pushlightuserdata(L, channel);
98
lua_gettable(L, LUA_REGISTRYINDEX);
100
lua_pushstring(L, hid);
101
lua_pushinteger(L, event);
102
lua_pushinteger(L, data);
104
/* TODO: For now we ignore the return values. */
105
lua_pcall(L, 3, 0, 0);
107
/* Now we pass the arguments to all script functions. If a function
108
* returns something else than nil, we abort the processing. */
109
lua_getfield(L, LUA_GLOBALSINDEX, "script");
110
for (int i = 1; i < 100; ++i) {
111
lua_pushinteger(L, i);
114
if (lua_type(L, -1) == LUA_TFUNCTION) {
115
lua_pushstring(L, hid);
116
lua_pushinteger(L, event);
117
lua_pushinteger(L, data);
119
if (lua_pcall(L, 3, 1, 0) || lua_type(L, -1)) {
120
printf("script[%02d] processing aborted: %s\n", i, lua_tostring(L, -1));
136
static const struct acpi_driver *drivers[4];
137
static unsigned char num;
140
void acpi_driver_register(const struct acpi_driver *driver)
142
drivers[num++] = driver;
145
const struct acpi_driver_ops *acpi_driver_find(const char *name)
147
for (int i = 0; i < num; ++i) {
148
if (strcmp(drivers[i]->name, name) == 0)
149
return &drivers[i]->ops;