Mirror of Awesome WM window manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

luaa.h 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * luaa.h - Lua configuration management header
  3. *
  4. * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. */
  21. #ifndef AWESOME_LUA_H
  22. #define AWESOME_LUA_H
  23. #include <lua.h>
  24. #include <lauxlib.h>
  25. #include <basedir.h>
  26. #include "draw.h"
  27. #include "common/lualib.h"
  28. #include "common/luaclass.h"
  29. #define luaA_deprecate(L, repl) \
  30. do { \
  31. luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
  32. __FUNCTION__, repl); \
  33. lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
  34. signal_object_emit(L, &global_signals, "debug::deprecation", 1); \
  35. } while(0)
  36. static inline void free_string(char **c)
  37. {
  38. p_delete(c);
  39. }
  40. DO_ARRAY(char*, string, free_string)
  41. /** Print a warning about some Lua code.
  42. * This is less mean than luaL_error() which setjmp via lua_error() and kills
  43. * everything. This only warn, it's up to you to then do what's should be done.
  44. * \param L The Lua VM state.
  45. * \param fmt The warning message.
  46. */
  47. static inline void __attribute__ ((format(printf, 2, 3)))
  48. luaA_warn(lua_State *L, const char *fmt, ...)
  49. {
  50. va_list ap;
  51. luaL_where(L, 1);
  52. fprintf(stderr, "%s%sW: ", a_current_time_str(), lua_tostring(L, -1));
  53. lua_pop(L, 1);
  54. va_start(ap, fmt);
  55. vfprintf(stderr, fmt, ap);
  56. va_end(ap);
  57. fprintf(stderr, "\n");
  58. #if LUA_VERSION_NUM >= 502
  59. luaL_traceback(L, L, NULL, 2);
  60. fprintf(stderr, "%s\n", lua_tostring(L, -1));
  61. lua_pop(L, 1);
  62. #endif
  63. }
  64. static inline int
  65. luaA_typerror(lua_State *L, int narg, const char *tname)
  66. {
  67. const char *msg = lua_pushfstring(L, "%s expected, got %s",
  68. tname, luaL_typename(L, narg));
  69. #if LUA_VERSION_NUM >= 502
  70. luaL_traceback(L, L, NULL, 2);
  71. lua_concat(L, 2);
  72. #endif
  73. return luaL_argerror(L, narg, msg);
  74. }
  75. static inline int
  76. luaA_rangerror(lua_State *L, int narg, double min, double max)
  77. {
  78. const char *msg = lua_pushfstring(L, "value in [%f, %f] expected, got %f",
  79. min, max, (double) lua_tonumber(L, narg));
  80. #if LUA_VERSION_NUM >= 502
  81. luaL_traceback(L, L, NULL, 2);
  82. lua_concat(L, 2);
  83. #endif
  84. return luaL_argerror(L, narg, msg);
  85. }
  86. static inline void
  87. luaA_getuservalue(lua_State *L, int idx)
  88. {
  89. #if LUA_VERSION_NUM >= 502
  90. lua_getuservalue(L, idx);
  91. #else
  92. lua_getfenv(L, idx);
  93. #endif
  94. }
  95. static inline void
  96. luaA_setuservalue(lua_State *L, int idx)
  97. {
  98. #if LUA_VERSION_NUM >= 502
  99. lua_setuservalue(L, idx);
  100. #else
  101. lua_setfenv(L, idx);
  102. #endif
  103. }
  104. static inline size_t
  105. luaA_rawlen(lua_State *L, int idx)
  106. {
  107. #if LUA_VERSION_NUM >= 502
  108. return lua_rawlen(L, idx);
  109. #else
  110. return lua_objlen(L, idx);
  111. #endif
  112. }
  113. static inline void
  114. luaA_registerlib(lua_State *L, const char *libname, const luaL_Reg *l)
  115. {
  116. assert(libname);
  117. #if LUA_VERSION_NUM >= 502
  118. lua_newtable(L);
  119. luaL_setfuncs(L, l, 0);
  120. lua_pushvalue(L, -1);
  121. lua_setglobal(L, libname);
  122. #else
  123. luaL_register(L, libname, l);
  124. #endif
  125. }
  126. static inline void
  127. luaA_setfuncs(lua_State *L, const luaL_Reg *l)
  128. {
  129. #if LUA_VERSION_NUM >= 502
  130. luaL_setfuncs(L, l, 0);
  131. #else
  132. luaL_register(L, NULL, l);
  133. #endif
  134. }
  135. static inline bool
  136. luaA_checkboolean(lua_State *L, int n)
  137. {
  138. if(!lua_isboolean(L, n))
  139. luaA_typerror(L, n, "boolean");
  140. return lua_toboolean(L, n);
  141. }
  142. static inline lua_Number
  143. luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
  144. {
  145. lua_getfield(L, idx, name);
  146. if (lua_isnil(L, -1) || lua_isnumber(L, -1))
  147. def = luaL_optnumber(L, -1, def);
  148. lua_pop(L, 1);
  149. return def;
  150. }
  151. static inline lua_Number
  152. luaA_checknumber_range(lua_State *L, int n, lua_Number min, lua_Number max)
  153. {
  154. lua_Number result = lua_tonumber(L, n);
  155. if (result < min || result > max)
  156. luaA_rangerror(L, n, min, max);
  157. return result;
  158. }
  159. static inline lua_Number
  160. luaA_optnumber_range(lua_State *L, int narg, lua_Number def, lua_Number min, lua_Number max)
  161. {
  162. if (lua_isnoneornil(L, narg))
  163. return def;
  164. return luaA_checknumber_range(L, narg, min, max);
  165. }
  166. static inline lua_Number
  167. luaA_getopt_number_range(lua_State *L, int idx, const char *name, lua_Number def, lua_Number min, lua_Number max)
  168. {
  169. lua_getfield(L, idx, name);
  170. if (lua_isnil(L, -1) || lua_isnumber(L, -1))
  171. def = luaA_optnumber_range(L, -1, def, min, max);
  172. lua_pop(L, 1);
  173. return def;
  174. }
  175. static inline int
  176. luaA_checkinteger(lua_State *L, int n)
  177. {
  178. lua_Number d = lua_tonumber(L, n);
  179. if (d != (int)d)
  180. luaA_typerror(L, n, "integer");
  181. return d;
  182. }
  183. static inline lua_Integer
  184. luaA_optinteger (lua_State *L, int narg, lua_Integer def)
  185. {
  186. return luaL_opt(L, luaA_checkinteger, narg, def);
  187. }
  188. static inline int
  189. luaA_getopt_integer(lua_State *L, int idx, const char *name, lua_Integer def)
  190. {
  191. lua_getfield(L, idx, name);
  192. if (lua_isnil(L, -1) || lua_isnumber(L, -1))
  193. def = luaA_optinteger(L, -1, def);
  194. lua_pop(L, 1);
  195. return def;
  196. }
  197. static inline int
  198. luaA_checkinteger_range(lua_State *L, int n, lua_Number min, lua_Number max)
  199. {
  200. int result = luaA_checkinteger(L, n);
  201. if (result < min || result > max)
  202. luaA_rangerror(L, n, min, max);
  203. return result;
  204. }
  205. static inline lua_Integer
  206. luaA_optinteger_range(lua_State *L, int narg, lua_Integer def, lua_Number min, lua_Number max)
  207. {
  208. if (lua_isnoneornil(L, narg))
  209. return def;
  210. return luaA_checkinteger_range(L, narg, min, max);
  211. }
  212. static inline int
  213. luaA_getopt_integer_range(lua_State *L, int idx, const char *name, lua_Integer def, lua_Number min, lua_Number max)
  214. {
  215. lua_getfield(L, idx, name);
  216. if (lua_isnil(L, -1) || lua_isnumber(L, -1))
  217. def = luaA_optinteger_range(L, -1, def, min, max);
  218. lua_pop(L, 1);
  219. return def;
  220. }
  221. /** Push a area type to a table on stack.
  222. * \param L The Lua VM state.
  223. * \param geometry The area geometry to push.
  224. * \return The number of elements pushed on stack.
  225. */
  226. static inline int
  227. luaA_pusharea(lua_State *L, area_t geometry)
  228. {
  229. lua_createtable(L, 0, 4);
  230. lua_pushinteger(L, geometry.x);
  231. lua_setfield(L, -2, "x");
  232. lua_pushinteger(L, geometry.y);
  233. lua_setfield(L, -2, "y");
  234. lua_pushinteger(L, geometry.width);
  235. lua_setfield(L, -2, "width");
  236. lua_pushinteger(L, geometry.height);
  237. lua_setfield(L, -2, "height");
  238. return 1;
  239. }
  240. /** Register an Lua object.
  241. * \param L The Lua stack.
  242. * \param idx Index of the object in the stack.
  243. * \param ref A int address: it will be filled with the int
  244. * registered. If the address points to an already registered object, it will
  245. * be unregistered.
  246. * \return Always 0.
  247. */
  248. static inline int
  249. luaA_register(lua_State *L, int idx, int *ref)
  250. {
  251. lua_pushvalue(L, idx);
  252. if(*ref != LUA_REFNIL)
  253. luaL_unref(L, LUA_REGISTRYINDEX, *ref);
  254. *ref = luaL_ref(L, LUA_REGISTRYINDEX);
  255. return 0;
  256. }
  257. /** Unregister a Lua object.
  258. * \param L The Lua stack.
  259. * \param ref A reference to an Lua object.
  260. */
  261. static inline void
  262. luaA_unregister(lua_State *L, int *ref)
  263. {
  264. luaL_unref(L, LUA_REGISTRYINDEX, *ref);
  265. *ref = LUA_REFNIL;
  266. }
  267. /** Register a function.
  268. * \param L The Lua stack.
  269. * \param idx Index of the function in the stack.
  270. * \param fct A int address: it will be filled with the int
  271. * registered. If the address points to an already registered function, it will
  272. * be unregistered.
  273. * \return luaA_register value.
  274. */
  275. static inline int
  276. luaA_registerfct(lua_State *L, int idx, int *fct)
  277. {
  278. luaA_checkfunction(L, idx);
  279. return luaA_register(L, idx, fct);
  280. }
  281. typedef bool luaA_config_callback(const char *);
  282. void luaA_init(xdgHandle *, string_array_t *);
  283. const char *luaA_find_config(xdgHandle *, const char *, luaA_config_callback *);
  284. bool luaA_parserc(xdgHandle *, const char *);
  285. /** Global signals */
  286. signal_array_t global_signals;
  287. int luaA_class_index_miss_property(lua_State *, lua_object_t *);
  288. int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
  289. int luaA_default_index(lua_State *);
  290. int luaA_default_newindex(lua_State *);
  291. void luaA_emit_startup(void);
  292. void luaA_systray_invalidate(void);
  293. #endif
  294. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80