#include <sys/param.h>
#include <sys/device.h>
#include <sys/lua.h>
#ifdef _MODULE
#include <sys/module.h>
#endif
#include <sys/reboot.h>
#include <lua.h>
#include <lauxlib.h>
#ifdef _MODULE
MODULE(MODULE_CLASS_MISC, luapmf, "lua");
static int
system_shutdown(lua_State *L)
{
pmf_system_shutdown(lua_tointeger(L, 1));
return 0;
}
static int
set_platform(lua_State *L)
{
const char *key, *value;
key = lua_tostring(L, -2);
value = lua_tostring(L, -1);
if (key != NULL && value != NULL)
pmf_set_platform(key, value);
return 0;
}
static int
get_platform(lua_State *L)
{
const char *key, *value;
key = lua_tostring(L, -1);
if (key != NULL) {
value = pmf_get_platform(key);
if (value != NULL)
lua_pushstring(L, value);
else
lua_pushnil(L);
} else
lua_pushnil(L);
return 1;
}
static int
luaopen_pmf(lua_State *L)
{
const luaL_Reg pmf_lib[ ] = {
{ "system_shutdown", system_shutdown },
{ "set_platform", set_platform },
{ "get_platform", get_platform },
{ NULL, NULL }
};
luaL_newlib(L, pmf_lib);
lua_pushinteger(L, PMFE_DISPLAY_ON);
lua_setfield(L, -2, "PMFE_DISPLAY_ON");
lua_pushinteger(L, PMFE_DISPLAY_REDUCED);
lua_setfield(L, -2, "PMFE_DISPLAY_REDUCED");
lua_pushinteger(L, PMFE_DISPLAY_STANDBY);
lua_setfield(L, -2, "PMFE_DISPLAY_STANDBY");
lua_pushinteger(L, PMFE_DISPLAY_SUSPEND);
lua_setfield(L, -2, "PMFE_DISPLAY_SUSPEND");
lua_pushinteger(L, PMFE_DISPLAY_OFF);
lua_setfield(L, -2, "PMFE_DISPLAY_OFF");
lua_pushinteger(L, PMFE_DISPLAY_BRIGHTNESS_UP);
lua_setfield(L, -2, "PMFE_DISPLAY_BRIGHTNESS_UP");
lua_pushinteger(L, PMFE_DISPLAY_BRIGHTNESS_DOWN);
lua_setfield(L, -2, "PMFE_DISPLAY_BRIGHTNESS_DOWN");
lua_pushinteger(L, PMFE_AUDIO_VOLUME_UP);
lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_UP");
lua_pushinteger(L, PMFE_AUDIO_VOLUME_DOWN);
lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_DOWN");
lua_pushinteger(L, PMFE_AUDIO_VOLUME_TOGGLE);
lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_TOGGLE");
lua_pushinteger(L, PMFE_CHASSIS_LID_CLOSE);
lua_setfield(L, -2, "PMFE_CHASSIS_LID_CLOSE");
lua_pushinteger(L, PMFE_CHASSIS_LID_OPEN);
lua_setfield(L, -2, "PMFE_CHASSIS_LID_OPEN");
lua_pushinteger(L, RB_AUTOBOOT);
lua_setfield(L, -2, "RB_AUTOBOOT");
lua_pushinteger(L, RB_ASKNAME);
lua_setfield(L, -2, "RB_ASKNAME");
lua_pushinteger(L, RB_DUMP);
lua_setfield(L, -2, "RB_DUMP");
lua_pushinteger(L, RB_HALT);
lua_setfield(L, -2, "RB_HALT");
lua_pushinteger(L, RB_POWERDOWN);
lua_setfield(L, -2, "RB_POWERDOWN");
lua_pushinteger(L, RB_KDB);
lua_setfield(L, -2, "RB_KDB");
lua_pushinteger(L, RB_NOSYNC);
lua_setfield(L, -2, "RB_NOSYNC");
lua_pushinteger(L, RB_RDONLY);
lua_setfield(L, -2, "RB_RDONLY");
lua_pushinteger(L, RB_SINGLE);
lua_setfield(L, -2, "RB_SINGLE");
lua_pushinteger(L, RB_USERCONF);
lua_setfield(L, -2, "RB_USERCONF");
return 1;
}
static int
luapmf_modcmd(modcmd_t cmd, void *opaque)
{
int error;
switch (cmd) {
case MODULE_CMD_INIT:
error = klua_mod_register("pmf", luaopen_pmf);
break;
case MODULE_CMD_FINI:
error = klua_mod_unregister("pmf");
break;
default:
error = ENOTTY;
}
return error;
}
#endif