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.c 26KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. * luaa.c - Lua configuration management
  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. /** awesome core API
  22. *
  23. * Additionally to the classes described here, one can also use X properties as
  24. * described in @{xproperties}.
  25. *
  26. * @author Julien Danjou &lt;julien@danjou.info&gt;
  27. * @copyright 2008-2009 Julien Danjou
  28. * @module awesome
  29. */
  30. /** Register a new xproperty.
  31. *
  32. * @tparam string name The name of the X11 property.
  33. * @tparam string type One of "string", "number" or "boolean".
  34. * @function register_xproperty
  35. */
  36. #define _GNU_SOURCE
  37. #include "luaa.h"
  38. #include "globalconf.h"
  39. #include "awesome.h"
  40. #include "common/backtrace.h"
  41. #include "common/version.h"
  42. #include "config.h"
  43. #include "event.h"
  44. #include "objects/client.h"
  45. #include "objects/drawable.h"
  46. #include "objects/drawin.h"
  47. #include "objects/screen.h"
  48. #include "objects/tag.h"
  49. #include "property.h"
  50. #include "selection.h"
  51. #include "spawn.h"
  52. #include "systray.h"
  53. #include "xkb.h"
  54. #include "xrdb.h"
  55. #include <lua.h>
  56. #include <lauxlib.h>
  57. #include <lualib.h>
  58. #include <basedir_fs.h>
  59. #include <xcb/xcb_atom.h>
  60. #include <xcb/xcb_aux.h>
  61. #include <unistd.h> /* for gethostname() */
  62. #ifdef WITH_DBUS
  63. extern const struct luaL_Reg awesome_dbus_lib[];
  64. #endif
  65. extern const struct luaL_Reg awesome_keygrabber_lib[];
  66. extern const struct luaL_Reg awesome_mousegrabber_lib[];
  67. extern const struct luaL_Reg awesome_root_lib[];
  68. extern const struct luaL_Reg awesome_mouse_methods[];
  69. extern const struct luaL_Reg awesome_mouse_meta[];
  70. /** A call into the Lua code aborted with an error.
  71. *
  72. * This signal is used in the example configuration, @{05-awesomerc.md},
  73. * to let a notification box pop up.
  74. * @param err Table with the error object, can be converted to a string with
  75. * `tostring(err)`.
  76. * @signal debug::error
  77. */
  78. /** A deprecated Lua function was called.
  79. *
  80. * @tparam string hint String with a hint on what to use instead of the
  81. * deprecated functionality.
  82. * @tparam[opt=nil] string|nil see The name of the newer API
  83. * @tparam[opt=nil] table|nil args The name of the newer API
  84. * @signal debug::deprecation
  85. */
  86. /** An invalid key was read from an object.
  87. *
  88. * This can happen if `foo` in an `c.foo` access does not exist.
  89. * @param unknown1 Class?
  90. * @param unknown2 Key?
  91. * @signal debug::index::miss
  92. */
  93. /** An invalid key was written to an object.
  94. *
  95. * This can happen if `foo` in an `c.foo = "bar"` assignment doesn't exist.
  96. * @param unknown1 Class?
  97. * @param unknown2 Key?
  98. * @param unknown3 Value?
  99. * @signal debug::newindex::miss
  100. */
  101. /** The systray should be updated.
  102. *
  103. * This signal is used in `wibox.widget.systray`.
  104. * @signal systray::update
  105. */
  106. /** The wallpaper has changed.
  107. *
  108. * This signal is used for pseudo-transparency in `wibox.drawable` if no
  109. * composite manager is running.
  110. * @signal wallpaper_changed
  111. */
  112. /** Keyboard map has changed.
  113. *
  114. * This signal is sent after the new keymap has been loaded. It is used in
  115. * `awful.widget.keyboardlayout` to redraw the layout.
  116. * @signal xkb::map_changed
  117. */
  118. /** Keyboard group has changed.
  119. *
  120. * It's used in `awful.widget.keyboardlayout` to redraw the layout.
  121. * @param group Integer containing the changed group
  122. * @signal xkb::group_changed.
  123. */
  124. /** Refresh.
  125. *
  126. * This signal is emitted as a kind of idle signal in the event loop.
  127. * One example usage is in `gears.timer` to executed delayed calls.
  128. * @signal refresh
  129. */
  130. /** Awesome is about to enter the event loop.
  131. *
  132. * This means all initialization has been done.
  133. * @signal startup
  134. */
  135. /** Awesome is exiting / about to restart.
  136. *
  137. * This signal is emitted in the `atexit` handler as well when awesome
  138. * restarts.
  139. * @param reason_restart Boolean value is true if the signal was sent
  140. * because of a restart.
  141. * @signal exit
  142. */
  143. /** The output status of a screen has changed.
  144. *
  145. * @param output String containing which output has changed.
  146. * @param connection_state String containing the connection status of
  147. * the output: It will be either "Connected", "Disconnected" or
  148. * "Unknown".
  149. * @signal screen::change
  150. */
  151. /** Path to config file */
  152. static char *conffile;
  153. /** Check whether a composite manager is running.
  154. * \return True if such a manager is running.
  155. */
  156. static bool
  157. composite_manager_running(void)
  158. {
  159. xcb_intern_atom_reply_t *atom_r;
  160. xcb_get_selection_owner_reply_t *selection_r;
  161. char *atom_name;
  162. bool result;
  163. if(!(atom_name = xcb_atom_name_by_screen("_NET_WM_CM", globalconf.default_screen)))
  164. {
  165. warn("error getting composite manager atom");
  166. return false;
  167. }
  168. atom_r = xcb_intern_atom_reply(globalconf.connection,
  169. xcb_intern_atom_unchecked(globalconf.connection, false,
  170. a_strlen(atom_name), atom_name),
  171. NULL);
  172. p_delete(&atom_name);
  173. if(!atom_r)
  174. return false;
  175. selection_r = xcb_get_selection_owner_reply(globalconf.connection,
  176. xcb_get_selection_owner_unchecked(globalconf.connection,
  177. atom_r->atom),
  178. NULL);
  179. p_delete(&atom_r);
  180. result = selection_r != NULL && selection_r->owner != XCB_NONE;
  181. p_delete(&selection_r);
  182. return result;
  183. }
  184. /** Quit awesome.
  185. * @tparam[opt=0] integer code The exit code to use when exiting.
  186. * @function quit
  187. */
  188. static int
  189. luaA_quit(lua_State *L)
  190. {
  191. if (!lua_isnoneornil(L, 1))
  192. globalconf.exit_code = luaL_checkinteger(L, 1);
  193. if (globalconf.loop == NULL)
  194. globalconf.loop = g_main_loop_new(NULL, FALSE);
  195. g_main_loop_quit(globalconf.loop);
  196. return 0;
  197. }
  198. /** Execute another application, probably a window manager, to replace
  199. * awesome.
  200. *
  201. * @param cmd The command line to execute.
  202. * @function exec
  203. */
  204. static int
  205. luaA_exec(lua_State *L)
  206. {
  207. const char *cmd = luaL_checkstring(L, 1);
  208. awesome_atexit(false);
  209. a_exec(cmd);
  210. return 0;
  211. }
  212. /** Restart awesome.
  213. * @function restart
  214. */
  215. static int
  216. luaA_restart(lua_State *L)
  217. {
  218. awesome_restart();
  219. return 0;
  220. }
  221. /** Send a signal to a process identified by its process id. See
  222. * `awesome.unix_signal` for a list of signals.
  223. * @tparam integer pid Process identifier
  224. * @tparam integer sig Signal number
  225. * @treturn boolean true if the signal was successfully sent, else false
  226. * @function kill
  227. */
  228. static int
  229. luaA_kill(lua_State *L)
  230. {
  231. int pid = luaA_checknumber_range(L, 1, 1, INT_MAX);
  232. int sig = luaA_checknumber_range(L, 2, 0, INT_MAX);
  233. int result = kill(pid, sig);
  234. lua_pushboolean(L, result == 0);
  235. return 1;
  236. }
  237. /** Synchronize with the X11 server. This is needed in the test suite to avoid
  238. * some race conditions. You should never need to use this function.
  239. * @function sync
  240. */
  241. static int
  242. luaA_sync(lua_State *L)
  243. {
  244. xcb_aux_sync(globalconf.connection);
  245. return 0;
  246. }
  247. /** Translate a GdkPixbuf to a cairo image surface..
  248. *
  249. * @param pixbuf The pixbuf as a light user datum.
  250. * @return A cairo surface as light user datum.
  251. * @function pixbuf_to_surface
  252. */
  253. static int
  254. luaA_pixbuf_to_surface(lua_State *L)
  255. {
  256. GdkPixbuf *pixbuf = (GdkPixbuf *) lua_touserdata(L, 1);
  257. cairo_surface_t *surface = draw_surface_from_pixbuf(pixbuf);
  258. /* lua has to make sure to free the ref or we have a leak */
  259. lua_pushlightuserdata(L, surface);
  260. return 1;
  261. }
  262. /** Load an image from a given path.
  263. *
  264. * @param name The file name.
  265. * @return[1] A cairo surface as light user datum.
  266. * @return[2] nil
  267. * @treturn[2] string Error message
  268. * @function load_image
  269. */
  270. static int
  271. luaA_load_image(lua_State *L)
  272. {
  273. /* TODO: Deprecate this function, Lua can use GdkPixbuf directly plus
  274. * awesome.pixbuf_to_surface
  275. */
  276. GError *error = NULL;
  277. const char *filename = luaL_checkstring(L, 1);
  278. cairo_surface_t *surface = draw_load_image(L, filename, &error);
  279. if (!surface) {
  280. lua_pushnil(L);
  281. lua_pushstring(L, error->message);
  282. g_error_free(error);
  283. return 2;
  284. }
  285. /* lua has to make sure to free the ref or we have a leak */
  286. lua_pushlightuserdata(L, surface);
  287. return 1;
  288. }
  289. /** Set the preferred size for client icons.
  290. *
  291. * The closest equal or bigger size is picked if present, otherwise the closest
  292. * smaller size is picked. The default is 0 pixels, ie. the smallest icon.
  293. *
  294. * @param size The size of the icons in pixels.
  295. * @function set_preferred_icon_size
  296. */
  297. static int
  298. luaA_set_preferred_icon_size(lua_State *L)
  299. {
  300. globalconf.preferred_icon_size = luaA_checkinteger_range(L, 1, 0, UINT32_MAX);
  301. return 0;
  302. }
  303. /** UTF-8 aware string length computing.
  304. * \param L The Lua VM state.
  305. * \return The number of elements pushed on stack.
  306. */
  307. static int
  308. luaA_mbstrlen(lua_State *L)
  309. {
  310. const char *cmd = luaL_checkstring(L, 1);
  311. lua_pushinteger(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
  312. return 1;
  313. }
  314. /** Enhanced type() function which recognize awesome objects.
  315. * \param L The Lua VM state.
  316. * \return The number of arguments pushed on the stack.
  317. */
  318. static int
  319. luaAe_type(lua_State *L)
  320. {
  321. luaL_checkany(L, 1);
  322. lua_pushstring(L, luaA_typename(L, 1));
  323. return 1;
  324. }
  325. /** Replace various standards Lua functions with our own.
  326. * \param L The Lua VM state.
  327. */
  328. static void
  329. luaA_fixups(lua_State *L)
  330. {
  331. /* export string.wlen */
  332. lua_getglobal(L, "string");
  333. lua_pushcfunction(L, luaA_mbstrlen);
  334. lua_setfield(L, -2, "wlen");
  335. lua_pop(L, 1);
  336. /* replace type */
  337. lua_pushcfunction(L, luaAe_type);
  338. lua_setglobal(L, "type");
  339. /* set selection */
  340. lua_pushcfunction(L, luaA_selection_get);
  341. lua_setglobal(L, "selection");
  342. }
  343. /**
  344. * The version of awesome.
  345. * @tfield string version
  346. */
  347. /**
  348. * The release name of awesome.
  349. * @tfield string release
  350. */
  351. /**
  352. * The configuration file which has been loaded.
  353. * @tfield string conffile
  354. */
  355. /**
  356. * True if we are still in startup, false otherwise.
  357. * @tfield boolean startup
  358. */
  359. /**
  360. * Error message for errors that occured during
  361. * startup.
  362. * @tfield string startup_errors
  363. */
  364. /**
  365. * True if a composite manager is running.
  366. * @tfield boolean composite_manager_running
  367. */
  368. /**
  369. * Table mapping between signal numbers and signal identifiers.
  370. * @tfield table unix_signal
  371. */
  372. /**
  373. * The hostname of the computer on which we are running.
  374. * @tfield string hostname
  375. */
  376. /**
  377. * The path where themes were installed to.
  378. * @tfield string themes_path
  379. */
  380. /**
  381. * The path where icons were installed to.
  382. * @tfield string icon_path
  383. */
  384. static int
  385. luaA_awesome_index(lua_State *L)
  386. {
  387. if(luaA_usemetatable(L, 1, 2))
  388. return 1;
  389. const char *buf = luaL_checkstring(L, 2);
  390. if(A_STREQ(buf, "conffile"))
  391. {
  392. lua_pushstring(L, conffile);
  393. return 1;
  394. }
  395. if(A_STREQ(buf, "version"))
  396. {
  397. lua_pushstring(L, awesome_version_string());
  398. return 1;
  399. }
  400. if(A_STREQ(buf, "release"))
  401. {
  402. lua_pushstring(L, awesome_release_string());
  403. return 1;
  404. }
  405. if(A_STREQ(buf, "startup"))
  406. {
  407. lua_pushboolean(L, globalconf.loop == NULL);
  408. return 1;
  409. }
  410. if(A_STREQ(buf, "startup_errors"))
  411. {
  412. if (globalconf.startup_errors.len == 0)
  413. return 0;
  414. lua_pushstring(L, globalconf.startup_errors.s);
  415. return 1;
  416. }
  417. if(A_STREQ(buf, "composite_manager_running"))
  418. {
  419. lua_pushboolean(L, composite_manager_running());
  420. return 1;
  421. }
  422. if(A_STREQ(buf, "hostname"))
  423. {
  424. /* No good way to handle failures... */
  425. char hostname[256] = "";
  426. gethostname(&hostname[0], countof(hostname));
  427. hostname[countof(hostname) - 1] = '\0';
  428. lua_pushstring(L, hostname);
  429. return 1;
  430. }
  431. if(A_STREQ(buf, "themes_path"))
  432. {
  433. lua_pushliteral(L, AWESOME_THEMES_PATH);
  434. return 1;
  435. }
  436. if(A_STREQ(buf, "icon_path"))
  437. {
  438. lua_pushliteral(L, AWESOME_ICON_PATH);
  439. return 1;
  440. }
  441. return luaA_default_index(L);
  442. }
  443. /** Add a global signal.
  444. *
  445. * @param name A string with the event name.
  446. * @param func The function to call.
  447. * @function connect_signal
  448. */
  449. static int
  450. luaA_awesome_connect_signal(lua_State *L)
  451. {
  452. const char *name = luaL_checkstring(L, 1);
  453. luaA_checkfunction(L, 2);
  454. signal_connect(&global_signals, name, luaA_object_ref(L, 2));
  455. return 0;
  456. }
  457. /** Remove a global signal.
  458. *
  459. * @param name A string with the event name.
  460. * @param func The function to call.
  461. * @function disconnect_signal
  462. */
  463. static int
  464. luaA_awesome_disconnect_signal(lua_State *L)
  465. {
  466. const char *name = luaL_checkstring(L, 1);
  467. luaA_checkfunction(L, 2);
  468. const void *func = lua_topointer(L, 2);
  469. if (signal_disconnect(&global_signals, name, func))
  470. luaA_object_unref(L, (void *) func);
  471. return 0;
  472. }
  473. /** Emit a global signal.
  474. *
  475. * @param name A string with the event name.
  476. * @param ... The signal arguments.
  477. * @function emit_signal
  478. */
  479. static int
  480. luaA_awesome_emit_signal(lua_State *L)
  481. {
  482. signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
  483. return 0;
  484. }
  485. static int
  486. luaA_panic(lua_State *L)
  487. {
  488. warn("unprotected error in call to Lua API (%s)",
  489. lua_tostring(L, -1));
  490. buffer_t buf;
  491. backtrace_get(&buf);
  492. warn("dumping backtrace\n%s", buf.s);
  493. warn("restarting awesome");
  494. awesome_restart();
  495. return 0;
  496. }
  497. #if LUA_VERSION_NUM >= 502
  498. static const char *
  499. luaA_tolstring(lua_State *L, int idx, size_t *len)
  500. {
  501. return luaL_tolstring(L, idx, len);
  502. }
  503. #else
  504. static const char *
  505. luaA_tolstring(lua_State *L, int idx, size_t *len)
  506. {
  507. /* Try using the metatable. If that fails, push the value itself onto
  508. * the stack.
  509. */
  510. if (!luaL_callmeta(L, idx, "__tostring"))
  511. lua_pushvalue(L, idx);
  512. switch (lua_type(L, -1)) {
  513. case LUA_TSTRING:
  514. lua_pushvalue(L, -1);
  515. break;
  516. case LUA_TBOOLEAN:
  517. if (lua_toboolean(L, -1))
  518. lua_pushliteral(L, "true");
  519. else
  520. lua_pushliteral(L, "false");
  521. break;
  522. case LUA_TNUMBER:
  523. lua_pushfstring(L, "%f", lua_tonumber(L, -1));
  524. break;
  525. case LUA_TNIL:
  526. lua_pushstring(L, "nil");
  527. break;
  528. default:
  529. lua_pushfstring(L, "%s: %p",
  530. lua_typename(L, lua_type(L, -1)),
  531. lua_topointer(L, -1));
  532. break;
  533. }
  534. lua_remove(L, -2);
  535. return lua_tolstring(L, -1, len);
  536. }
  537. #endif
  538. static int
  539. luaA_dofunction_on_error(lua_State *L)
  540. {
  541. /* Convert error to string, to prevent a follow-up error with lua_concat. */
  542. luaA_tolstring(L, -1, NULL);
  543. /* duplicate string error */
  544. lua_pushvalue(L, -1);
  545. /* emit error signal */
  546. signal_object_emit(L, &global_signals, "debug::error", 1);
  547. if(!luaL_dostring(L, "return debug.traceback(\"error while running function!\", 3)"))
  548. {
  549. /* Move traceback before error */
  550. lua_insert(L, -2);
  551. /* Insert sentence */
  552. lua_pushliteral(L, "\nerror: ");
  553. /* Move it before error */
  554. lua_insert(L, -2);
  555. lua_concat(L, 3);
  556. }
  557. return 1;
  558. }
  559. static void
  560. setup_awesome_signals(lua_State *L)
  561. {
  562. lua_getglobal(L, "awesome");
  563. lua_pushstring(L, "unix_signal");
  564. lua_newtable(L);
  565. #define SETUP_SIGNAL(sig) \
  566. do { \
  567. /* Set awesome.unix_signal["SIGSTOP"] = 42 */ \
  568. lua_pushinteger(L, sig); \
  569. lua_setfield(L, -2, #sig); \
  570. /* Set awesome.unix_signal[42] = "SIGSTOP" */ \
  571. lua_pushinteger(L, sig); \
  572. lua_pushstring(L, #sig); \
  573. lua_settable(L, -3); \
  574. } while (0)
  575. /* Non-standard signals. These are first so that e.g. (on my system)
  576. * signals[29] is SIGPOLL and not SIGIO (the value gets overwritten).
  577. */
  578. #ifdef SIGIOT
  579. SETUP_SIGNAL(SIGIOT);
  580. #endif
  581. #ifdef SIGEMT
  582. SETUP_SIGNAL(SIGEMT);
  583. #endif
  584. #ifdef SIGSTKFLT
  585. SETUP_SIGNAL(SIGSTKFLT);
  586. #endif
  587. #ifdef SIGIO
  588. SETUP_SIGNAL(SIGIO);
  589. #endif
  590. #ifdef SIGCLD
  591. SETUP_SIGNAL(SIGCLD);
  592. #endif
  593. #ifdef SIGPWR
  594. SETUP_SIGNAL(SIGPWR);
  595. #endif
  596. #ifdef SIGINFO
  597. SETUP_SIGNAL(SIGINFO);
  598. #endif
  599. #ifdef SIGLOST
  600. SETUP_SIGNAL(SIGLOST);
  601. #endif
  602. #ifdef SIGWINCH
  603. SETUP_SIGNAL(SIGWINCH);
  604. #endif
  605. #ifdef SIGUNUSED
  606. SETUP_SIGNAL(SIGUNUSED);
  607. #endif
  608. /* POSIX.1-1990, according to man 7 signal */
  609. SETUP_SIGNAL(SIGHUP);
  610. SETUP_SIGNAL(SIGINT);
  611. SETUP_SIGNAL(SIGQUIT);
  612. SETUP_SIGNAL(SIGILL);
  613. SETUP_SIGNAL(SIGABRT);
  614. SETUP_SIGNAL(SIGFPE);
  615. SETUP_SIGNAL(SIGKILL);
  616. SETUP_SIGNAL(SIGSEGV);
  617. SETUP_SIGNAL(SIGPIPE);
  618. SETUP_SIGNAL(SIGALRM);
  619. SETUP_SIGNAL(SIGTERM);
  620. SETUP_SIGNAL(SIGUSR1);
  621. SETUP_SIGNAL(SIGUSR2);
  622. SETUP_SIGNAL(SIGCHLD);
  623. SETUP_SIGNAL(SIGCONT);
  624. SETUP_SIGNAL(SIGSTOP);
  625. SETUP_SIGNAL(SIGTSTP);
  626. SETUP_SIGNAL(SIGTTIN);
  627. SETUP_SIGNAL(SIGTTOU);
  628. /* POSIX.1-2001, according to man 7 signal */
  629. SETUP_SIGNAL(SIGBUS);
  630. /* Some Operating Systems doesn't have SIGPOLL (e.g. FreeBSD) */
  631. #ifdef SIGPOLL
  632. SETUP_SIGNAL(SIGPOLL);
  633. #endif
  634. SETUP_SIGNAL(SIGPROF);
  635. SETUP_SIGNAL(SIGSYS);
  636. SETUP_SIGNAL(SIGTRAP);
  637. SETUP_SIGNAL(SIGURG);
  638. SETUP_SIGNAL(SIGVTALRM);
  639. SETUP_SIGNAL(SIGXCPU);
  640. SETUP_SIGNAL(SIGXFSZ);
  641. #undef SETUP_SIGNAL
  642. /* Set awesome.signal to the table we just created, key was already pushed */
  643. lua_rawset(L, -3);
  644. /* Pop "awesome" */
  645. lua_pop(L, 1);
  646. }
  647. /* Add things to the string on top of the stack */
  648. static void
  649. add_to_search_path(lua_State *L, string_array_t *searchpath, bool for_lua)
  650. {
  651. if (LUA_TSTRING != lua_type(L, -1))
  652. {
  653. warn("package.path is not a string");
  654. return;
  655. }
  656. foreach(entry, *searchpath)
  657. {
  658. int components;
  659. size_t len = a_strlen(*entry);
  660. lua_pushliteral(L, ";");
  661. lua_pushlstring(L, *entry, len);
  662. if (for_lua)
  663. lua_pushliteral(L, "/?.lua");
  664. else
  665. lua_pushliteral(L, "/?.so");
  666. lua_concat(L, 3);
  667. if (for_lua)
  668. {
  669. lua_pushliteral(L, ";");
  670. lua_pushlstring(L, *entry, len);
  671. lua_pushliteral(L, "/?/init.lua");
  672. lua_concat(L, 3);
  673. components = 2;
  674. } else {
  675. components = 1;
  676. }
  677. lua_concat(L, components + 1); /* concatenate with string on top of the stack */
  678. }
  679. /* add Lua lib path (/usr/share/awesome/lib by default) */
  680. if (for_lua)
  681. {
  682. lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
  683. lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
  684. lua_concat(L, 3); /* concatenate with thing on top of the stack when we were called */
  685. } else {
  686. lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.so");
  687. lua_concat(L, 2); /* concatenate with thing on top of the stack when we were called */
  688. }
  689. }
  690. /** Initialize the Lua VM
  691. * \param xdg An xdg handle to use to get XDG basedir.
  692. */
  693. void
  694. luaA_init(xdgHandle* xdg, string_array_t *searchpath)
  695. {
  696. lua_State *L;
  697. static const struct luaL_Reg awesome_lib[] =
  698. {
  699. { "quit", luaA_quit },
  700. { "exec", luaA_exec },
  701. { "spawn", luaA_spawn },
  702. { "restart", luaA_restart },
  703. { "connect_signal", luaA_awesome_connect_signal },
  704. { "disconnect_signal", luaA_awesome_disconnect_signal },
  705. { "emit_signal", luaA_awesome_emit_signal },
  706. { "systray", luaA_systray },
  707. { "load_image", luaA_load_image },
  708. { "pixbuf_to_surface", luaA_pixbuf_to_surface },
  709. { "set_preferred_icon_size", luaA_set_preferred_icon_size },
  710. { "register_xproperty", luaA_register_xproperty },
  711. { "set_xproperty", luaA_set_xproperty },
  712. { "get_xproperty", luaA_get_xproperty },
  713. { "__index", luaA_awesome_index },
  714. { "__newindex", luaA_default_newindex },
  715. { "xkb_set_layout_group", luaA_xkb_set_layout_group},
  716. { "xkb_get_layout_group", luaA_xkb_get_layout_group},
  717. { "xkb_get_group_names", luaA_xkb_get_group_names},
  718. { "xrdb_get_value", luaA_xrdb_get_value},
  719. { "kill", luaA_kill},
  720. { "sync", luaA_sync},
  721. { NULL, NULL }
  722. };
  723. L = globalconf.L.real_L_dont_use_directly = luaL_newstate();
  724. /* Set panic function */
  725. lua_atpanic(L, luaA_panic);
  726. /* Set error handling function */
  727. lualib_dofunction_on_error = luaA_dofunction_on_error;
  728. luaL_openlibs(L);
  729. luaA_fixups(L);
  730. luaA_object_setup(L);
  731. /* Export awesome lib */
  732. luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
  733. setup_awesome_signals(L);
  734. /* Export root lib */
  735. luaA_registerlib(L, "root", awesome_root_lib);
  736. lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
  737. #ifdef WITH_DBUS
  738. /* Export D-Bus lib */
  739. luaA_registerlib(L, "dbus", awesome_dbus_lib);
  740. lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
  741. #endif
  742. /* Export keygrabber lib */
  743. luaA_registerlib(L, "keygrabber", awesome_keygrabber_lib);
  744. lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
  745. /* Export mousegrabber lib */
  746. luaA_registerlib(L, "mousegrabber", awesome_mousegrabber_lib);
  747. lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
  748. /* Export mouse */
  749. luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
  750. /* Export screen */
  751. screen_class_setup(L);
  752. /* Export button */
  753. button_class_setup(L);
  754. /* Export tag */
  755. tag_class_setup(L);
  756. /* Export window */
  757. window_class_setup(L);
  758. /* Export drawable */
  759. drawable_class_setup(L);
  760. /* Export drawin */
  761. drawin_class_setup(L);
  762. /* Export client */
  763. client_class_setup(L);
  764. /* Export keys */
  765. key_class_setup(L);
  766. /* add Lua search paths */
  767. lua_getglobal(L, "package");
  768. if (LUA_TTABLE != lua_type(L, 1))
  769. {
  770. warn("package is not a table");
  771. return;
  772. }
  773. lua_getfield(L, 1, "path");
  774. add_to_search_path(L, searchpath, true);
  775. lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
  776. lua_getfield(L, 1, "cpath");
  777. add_to_search_path(L, searchpath, false);
  778. lua_setfield(L, 1, "cpath"); /* package.cpath = "concatenated string" */
  779. lua_pop(L, 1); /* pop "package" */
  780. }
  781. static void
  782. luaA_startup_error(const char *err)
  783. {
  784. if (globalconf.startup_errors.len > 0)
  785. buffer_addsl(&globalconf.startup_errors, "\n\n");
  786. buffer_adds(&globalconf.startup_errors, err);
  787. }
  788. static bool
  789. luaA_loadrc(const char *confpath)
  790. {
  791. lua_State *L = globalconf_get_lua_State();
  792. if(luaL_loadfile(L, confpath))
  793. {
  794. const char *err = lua_tostring(L, -1);
  795. luaA_startup_error(err);
  796. fprintf(stderr, "%s\n", err);
  797. lua_pop(L, 1);
  798. return false;
  799. }
  800. /* Set the conffile right now so it can be used inside the
  801. * configuration file. */
  802. conffile = a_strdup(confpath);
  803. /* Move error handling function before function */
  804. lua_pushcfunction(L, luaA_dofunction_on_error);
  805. lua_insert(L, -2);
  806. if(!lua_pcall(L, 0, 0, -2))
  807. {
  808. /* Pop luaA_dofunction_on_error */
  809. lua_pop(L, 1);
  810. return true;
  811. }
  812. const char *err = lua_tostring(L, -1);
  813. luaA_startup_error(err);
  814. fprintf(stderr, "%s\n", err);
  815. /* An error happened, so reset this. */
  816. conffile = NULL;
  817. /* Pop luaA_dofunction_on_error() and the error message */
  818. lua_pop(L, 2);
  819. return false;
  820. }
  821. /** Load a configuration file.
  822. * \param xdg An xdg handle to use to get XDG basedir.
  823. * \param confpatharg The configuration file to load.
  824. * \param run Run the configuration file.
  825. */
  826. bool
  827. luaA_parserc(xdgHandle* xdg, const char *confpatharg)
  828. {
  829. const char *confpath = luaA_find_config(xdg, confpatharg, luaA_loadrc);
  830. bool ret = confpath != NULL;
  831. p_delete(&confpath);
  832. return ret;
  833. }
  834. /** Find a config file for which the given callback returns true.
  835. * \param xdg An xdg handle to use to get XDG basedir.
  836. * \param confpatharg The configuration file to load.
  837. * \param callback The callback to call.
  838. */
  839. const char *
  840. luaA_find_config(xdgHandle* xdg, const char *confpatharg, luaA_config_callback *callback)
  841. {
  842. char *confpath = NULL;
  843. if(confpatharg && callback(confpatharg))
  844. {
  845. return a_strdup(confpatharg);
  846. }
  847. confpath = xdgConfigFind("awesome/rc.lua", xdg);
  848. char *tmp = confpath;
  849. /* confpath is "string1\0string2\0string3\0\0" */
  850. while(*tmp)
  851. {
  852. if(callback(tmp))
  853. {
  854. const char *ret = a_strdup(tmp);
  855. p_delete(&confpath);
  856. return ret;
  857. }
  858. tmp += a_strlen(tmp) + 1;
  859. }
  860. p_delete(&confpath);
  861. if(callback(AWESOME_DEFAULT_CONF))
  862. {
  863. return a_strdup(AWESOME_DEFAULT_CONF);
  864. }
  865. return NULL;
  866. }
  867. int
  868. luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
  869. {
  870. signal_object_emit(L, &global_signals, "debug::index::miss", 2);
  871. return 0;
  872. }
  873. int
  874. luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
  875. {
  876. signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
  877. return 0;
  878. }
  879. void
  880. luaA_emit_startup()
  881. {
  882. lua_State *L = globalconf_get_lua_State();
  883. signal_object_emit(L, &global_signals, "startup", 0);
  884. }
  885. void
  886. luaA_emit_refresh()
  887. {
  888. lua_State *L = globalconf_get_lua_State();
  889. signal_object_emit(L, &global_signals, "refresh", 0);
  890. }
  891. int
  892. luaA_default_index(lua_State *L)
  893. {
  894. return luaA_class_index_miss_property(L, NULL);
  895. }
  896. int
  897. luaA_default_newindex(lua_State *L)
  898. {
  899. return luaA_class_newindex_miss_property(L, NULL);
  900. }
  901. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80