1 /* $NetBSD: luapmf.c,v 1.3 2013/12/16 23:35:48 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 48 system_shutdown(lua_State *L) 49 { 50 pmf_system_shutdown(lua_tointeger(L, 1)); 51 return 0; 52 } 53 54 static int 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 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 85 luaopen_pmf(void *ls) 86 { 87 lua_State *L = (lua_State *)ls; 88 const luaL_Reg pmf_lib[ ] = { 89 { "system_shutdown", system_shutdown }, 90 { "set_platform", set_platform }, 91 { "get_platform", get_platform }, 92 { NULL, NULL } 93 }; 94 95 luaL_register(L, "pmf", pmf_lib); 96 97 /* some integer values */ 98 lua_pushinteger(L, PMFE_DISPLAY_ON); 99 lua_setfield(L, -2, "PMFE_DISPLAY_ON"); 100 lua_pushinteger(L, PMFE_DISPLAY_REDUCED); 101 lua_setfield(L, -2, "PMFE_DISPLAY_REDUCED"); 102 lua_pushinteger(L, PMFE_DISPLAY_STANDBY); 103 lua_setfield(L, -2, "PMFE_DISPLAY_STANDBY"); 104 lua_pushinteger(L, PMFE_DISPLAY_SUSPEND); 105 lua_setfield(L, -2, "PMFE_DISPLAY_SUSPEND"); 106 lua_pushinteger(L, PMFE_DISPLAY_OFF); 107 lua_setfield(L, -2, "PMFE_DISPLAY_OFF"); 108 lua_pushinteger(L, PMFE_DISPLAY_BRIGHTNESS_UP); 109 lua_setfield(L, -2, "PMFE_DISPLAY_BRIGHTNESS_UP"); 110 lua_pushinteger(L, PMFE_DISPLAY_BRIGHTNESS_DOWN); 111 lua_setfield(L, -2, "PMFE_DISPLAY_BRIGHTNESS_DOWN"); 112 lua_pushinteger(L, PMFE_AUDIO_VOLUME_UP); 113 lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_UP"); 114 lua_pushinteger(L, PMFE_AUDIO_VOLUME_DOWN); 115 lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_DOWN"); 116 lua_pushinteger(L, PMFE_AUDIO_VOLUME_TOGGLE); 117 lua_setfield(L, -2, "PMFE_AUDIO_VOLUME_TOGGLE"); 118 lua_pushinteger(L, PMFE_CHASSIS_LID_CLOSE); 119 lua_setfield(L, -2, "PMFE_CHASSIS_LID_CLOSE"); 120 lua_pushinteger(L, PMFE_CHASSIS_LID_OPEN); 121 lua_setfield(L, -2, "PMFE_CHASSIS_LID_OPEN"); 122 123 /* reboot(2) howto arg */ 124 lua_pushinteger(L, RB_AUTOBOOT); 125 lua_setfield(L, -2, "RB_AUTOBOOT"); 126 lua_pushinteger(L, RB_ASKNAME); 127 lua_setfield(L, -2, "RB_ASKNAME"); 128 lua_pushinteger(L, RB_DUMP); 129 lua_setfield(L, -2, "RB_DUMP"); 130 lua_pushinteger(L, RB_HALT); 131 lua_setfield(L, -2, "RB_HALT"); 132 lua_pushinteger(L, RB_POWERDOWN); 133 lua_setfield(L, -2, "RB_POWERDOWN"); 134 lua_pushinteger(L, RB_KDB); 135 lua_setfield(L, -2, "RB_KDB"); 136 lua_pushinteger(L, RB_NOSYNC); 137 lua_setfield(L, -2, "RB_NOSYNC"); 138 lua_pushinteger(L, RB_RDONLY); 139 lua_setfield(L, -2, "RB_RDONLY"); 140 lua_pushinteger(L, RB_SINGLE); 141 lua_setfield(L, -2, "RB_SINGLE"); 142 lua_pushinteger(L, RB_USERCONF); 143 lua_setfield(L, -2, "RB_USERCONF"); 144 145 return 1; 146 } 147 148 static int 149 luapmf_modcmd(modcmd_t cmd, void *opaque) 150 { 151 int error; 152 switch (cmd) { 153 case MODULE_CMD_INIT: 154 error = lua_mod_register("pmf", luaopen_pmf); 155 break; 156 case MODULE_CMD_FINI: 157 error = lua_mod_unregister("pmf"); 158 break; 159 default: 160 error = ENOTTY; 161 } 162 return error; 163 } 164 #endif 165