1 /* $NetBSD: luapmf.c,v 1.6 2014/07/19 18:38:35 lneto Exp $ */
2
3 /*
4 * Copyright (c) 2011, 2013 Marc Balmer <mbalmer@NetBSD.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the Author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /* Lua pmf module */
32
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/lua.h>
36 #ifdef _MODULE
37 #include <sys/module.h>
38 #endif
39 #include <sys/reboot.h>
40
41 #include <lua.h>
42 #include <lauxlib.h>
43
44 #ifdef _MODULE
45 MODULE(MODULE_CLASS_MISC, luapmf, "lua");
46
47 static int
system_shutdown(lua_State * L)48 system_shutdown(lua_State *L)
49 {
50 pmf_system_shutdown(lua_tointeger(L, 1));
51 return 0;
52 }
53
54 static int
set_platform(lua_State * L)55 set_platform(lua_State *L)
56 {
57 const char *key, *value;
58
59 key = lua_tostring(L, -2);
60 value = lua_tostring(L, -1);
61 if (key != NULL && value != NULL)
62 pmf_set_platform(key, value);
63 return 0;
64 }
65
66 static int
get_platform(lua_State * L)67 get_platform(lua_State *L)
68 {
69 const char *key, *value;
70
71 key = lua_tostring(L, -1);
72 if (key != NULL) {
73 value = pmf_get_platform(key);
74 if (value != NULL)
75 lua_pushstring(L, value);
76 else
77 lua_pushnil(L);
78 } else
79 lua_pushnil(L);
80 return 1;
81
82 }
83
84 static int
luaopen_pmf(lua_State * L)85 luaopen_pmf(lua_State *L)
86 {
87 const luaL_Reg pmf_lib[ ] = {
88 { "system_shutdown", system_shutdown },
89 { "set_platform", set_platform },
90 { "get_platform", get_platform },
91 { NULL, NULL }
92 };
93
94 luaL_newlib(L, pmf_lib);
95
96 /* some integer values */
97 lua_pushinteger(L, PMFE_DISPLAY_ON);
98 lua_setfield(L, -2, "PMFE_DISPLAY_ON");
99 lua_pushinteger(L, PMFE_DISPLAY_REDUCED);
100 lua_setfield(L, -2, "PMFE_DISPLAY_REDUCED");
101 lua_pushinteger(L, PMFE_DISPLAY_STANDBY);
102 lua_setfield(L, -2, "PMFE_DISPLAY_STANDBY");
103 lua_pushinteger(L, PMFE_DISPLAY_SUSPEND);
104 lua_setfield(L, -2, "PMFE_DISPLAY_SUSPEND");
105 lua_pushinteger(L, PMFE_DISPLAY_OFF);
106 lua_setfield(L, -2, "PMFE_DISPLAY_OFF");
107 lua_pushinteger(L, PMFE_DISPLAY_BRIGHTNESS_UP);
108 lua_setfield(L, -2, "PMFE_DISPLAY_BRIGHTNESS_UP");
109 lua_pushinteger(L, PMFE_DISPLAY_BRIGHTNESS_DOWN);
110 lua_setfield(L, -2, "PMFE_DISPLAY_BRIGHTNESS_DOWN");
111 lua_pushinteger(L, PMFE_AUDIO_VOLUME_UP);
112 lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_UP");
113 lua_pushinteger(L, PMFE_AUDIO_VOLUME_DOWN);
114 lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_DOWN");
115 lua_pushinteger(L, PMFE_AUDIO_VOLUME_TOGGLE);
116 lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_TOGGLE");
117 lua_pushinteger(L, PMFE_CHASSIS_LID_CLOSE);
118 lua_setfield(L, -2, "PMFE_CHASSIS_LID_CLOSE");
119 lua_pushinteger(L, PMFE_CHASSIS_LID_OPEN);
120 lua_setfield(L, -2, "PMFE_CHASSIS_LID_OPEN");
121
122 /* reboot(2) howto arg */
123 lua_pushinteger(L, RB_AUTOBOOT);
124 lua_setfield(L, -2, "RB_AUTOBOOT");
125 lua_pushinteger(L, RB_ASKNAME);
126 lua_setfield(L, -2, "RB_ASKNAME");
127 lua_pushinteger(L, RB_DUMP);
128 lua_setfield(L, -2, "RB_DUMP");
129 lua_pushinteger(L, RB_HALT);
130 lua_setfield(L, -2, "RB_HALT");
131 lua_pushinteger(L, RB_POWERDOWN);
132 lua_setfield(L, -2, "RB_POWERDOWN");
133 lua_pushinteger(L, RB_KDB);
134 lua_setfield(L, -2, "RB_KDB");
135 lua_pushinteger(L, RB_NOSYNC);
136 lua_setfield(L, -2, "RB_NOSYNC");
137 lua_pushinteger(L, RB_RDONLY);
138 lua_setfield(L, -2, "RB_RDONLY");
139 lua_pushinteger(L, RB_SINGLE);
140 lua_setfield(L, -2, "RB_SINGLE");
141 lua_pushinteger(L, RB_USERCONF);
142 lua_setfield(L, -2, "RB_USERCONF");
143
144 return 1;
145 }
146
147 static int
luapmf_modcmd(modcmd_t cmd,void * opaque)148 luapmf_modcmd(modcmd_t cmd, void *opaque)
149 {
150 int error;
151 switch (cmd) {
152 case MODULE_CMD_INIT:
153 error = klua_mod_register("pmf", luaopen_pmf);
154 break;
155 case MODULE_CMD_FINI:
156 error = klua_mod_unregister("pmf");
157 break;
158 default:
159 error = ENOTTY;
160 }
161 return error;
162 }
163 #endif
164