2008-05-26 20:39:54 +02:00
|
|
|
/*
|
2009-04-03 13:09:17 +02:00
|
|
|
* luaa.c - Lua configuration management
|
2008-05-26 20:39:54 +02:00
|
|
|
*
|
2009-01-06 17:02:08 +01:00
|
|
|
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
|
2008-05-26 20:39:54 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-04-17 16:52:25 +02:00
|
|
|
#define _GNU_SOURCE
|
2008-11-08 14:22:20 +01:00
|
|
|
|
2008-06-16 23:47:14 +02:00
|
|
|
#include <ev.h>
|
2008-06-26 18:10:45 +02:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
#include <lualib.h>
|
|
|
|
|
2009-04-07 17:57:01 +02:00
|
|
|
#include <basedir_fs.h>
|
|
|
|
|
2010-08-22 17:01:28 +02:00
|
|
|
#include <oopango.h>
|
|
|
|
#include <oocairo.h>
|
|
|
|
|
2008-09-02 17:12:10 +02:00
|
|
|
#include "awesome.h"
|
2009-09-07 17:28:55 +02:00
|
|
|
#include "config.h"
|
2009-09-14 20:17:47 +02:00
|
|
|
#include "objects/timer.h"
|
2008-07-31 17:48:27 +02:00
|
|
|
#include "awesome-version-internal.h"
|
2008-06-17 22:12:30 +02:00
|
|
|
#include "ewmh.h"
|
2008-09-17 16:38:38 +02:00
|
|
|
#include "luaa.h"
|
2009-04-03 13:09:17 +02:00
|
|
|
#include "spawn.h"
|
2009-09-14 20:20:27 +02:00
|
|
|
#include "objects/tag.h"
|
2009-09-14 20:22:12 +02:00
|
|
|
#include "objects/client.h"
|
2010-10-06 13:35:14 +02:00
|
|
|
#include "objects/drawin.h"
|
2008-06-17 22:12:30 +02:00
|
|
|
#include "screen.h"
|
2008-08-26 19:35:37 +02:00
|
|
|
#include "event.h"
|
2009-01-06 17:02:08 +01:00
|
|
|
#include "selection.h"
|
2010-10-06 15:38:44 +02:00
|
|
|
#include "systray.h"
|
2009-04-03 17:11:51 +02:00
|
|
|
#include "common/xcursor.h"
|
2009-04-26 20:22:10 +02:00
|
|
|
#include "common/buffer.h"
|
2009-11-09 12:44:19 +01:00
|
|
|
#include "common/backtrace.h"
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-04-18 16:07:31 +02:00
|
|
|
#ifdef WITH_DBUS
|
2008-11-18 16:31:27 +01:00
|
|
|
extern const struct luaL_reg awesome_dbus_lib[];
|
2009-04-18 16:07:31 +02:00
|
|
|
#endif
|
2008-06-10 16:50:55 +02:00
|
|
|
extern const struct luaL_reg awesome_keygrabber_lib[];
|
2008-11-13 16:08:34 +01:00
|
|
|
extern const struct luaL_reg awesome_mousegrabber_lib[];
|
2009-08-14 16:46:35 +02:00
|
|
|
extern const struct luaL_reg awesome_root_lib[];
|
2008-06-13 15:35:47 +02:00
|
|
|
extern const struct luaL_reg awesome_mouse_methods[];
|
|
|
|
extern const struct luaL_reg awesome_mouse_meta[];
|
2008-08-11 17:14:02 +02:00
|
|
|
extern const struct luaL_reg awesome_screen_methods[];
|
|
|
|
extern const struct luaL_reg awesome_screen_meta[];
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-11-10 10:22:04 +01:00
|
|
|
/** Path to config file */
|
|
|
|
static char *conffile;
|
|
|
|
|
2008-05-26 20:39:54 +02:00
|
|
|
/** Quit awesome.
|
2008-10-01 09:55:28 +02:00
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
2008-05-26 20:39:54 +02:00
|
|
|
*/
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
2010-08-08 18:19:15 +02:00
|
|
|
luaA_quit(lua_State *L)
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
2008-06-17 00:30:53 +02:00
|
|
|
ev_unloop(globalconf.loop, 1);
|
2008-05-20 15:39:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-26 20:39:54 +02:00
|
|
|
/** Execute another application, probably a window manager, to replace
|
|
|
|
* awesome.
|
2008-06-10 20:12:51 +02:00
|
|
|
* \param L The Lua VM state.
|
2008-10-01 09:55:28 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
2008-06-10 20:12:51 +02:00
|
|
|
* \luastack
|
|
|
|
* \lparam The command line to execute.
|
2008-05-26 20:39:54 +02:00
|
|
|
*/
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
|
|
|
luaA_exec(lua_State *L)
|
|
|
|
{
|
|
|
|
const char *cmd = luaL_checkstring(L, 1);
|
|
|
|
|
2011-03-20 23:20:02 +01:00
|
|
|
awesome_atexit(false);
|
2008-05-20 15:39:47 +02:00
|
|
|
|
|
|
|
a_exec(cmd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-26 20:39:54 +02:00
|
|
|
/** Restart awesome.
|
|
|
|
*/
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
2010-08-08 18:19:15 +02:00
|
|
|
luaA_restart(lua_State *L)
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
2008-09-02 17:12:10 +02:00
|
|
|
awesome_restart();
|
2008-05-20 15:39:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-29 14:08:57 +01:00
|
|
|
/** Load an image from a given path.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam The command line to execute.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_load_image(lua_State *L)
|
|
|
|
{
|
|
|
|
const char *filename = luaL_checkstring(L, 1);
|
|
|
|
return draw_load_image(L, filename);
|
|
|
|
}
|
|
|
|
|
2008-10-19 19:14:55 +02:00
|
|
|
/** UTF-8 aware string length computing.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
2008-06-25 11:42:06 +02:00
|
|
|
static int
|
|
|
|
luaA_mbstrlen(lua_State *L)
|
|
|
|
{
|
|
|
|
const char *cmd = luaL_checkstring(L, 1);
|
2009-03-15 10:01:25 +01:00
|
|
|
lua_pushnumber(L, (ssize_t) mbstowcs(NULL, NONULL(cmd), 0));
|
2008-06-25 11:42:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-10-19 19:14:55 +02:00
|
|
|
/** Overload standard Lua next function to use __next key on metatable.
|
|
|
|
* \param L The Lua VM state.
|
2009-07-27 06:20:00 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
2008-10-19 19:14:55 +02:00
|
|
|
*/
|
2008-08-10 16:14:16 +02:00
|
|
|
static int
|
2008-10-19 19:14:55 +02:00
|
|
|
luaAe_next(lua_State *L)
|
2008-08-10 16:14:16 +02:00
|
|
|
{
|
|
|
|
if(luaL_getmetafield(L, 1, "__next"))
|
|
|
|
{
|
|
|
|
lua_insert(L, 1);
|
|
|
|
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
|
lua_settop(L, 2);
|
|
|
|
if(lua_next(L, 1))
|
|
|
|
return 2;
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-10-19 19:14:55 +02:00
|
|
|
/** Overload lua_next() function by using __next metatable field
|
|
|
|
* to get next elements.
|
|
|
|
* \param L The Lua VM stack.
|
|
|
|
* \param idx The index number of elements in stack.
|
|
|
|
* \return 1 if more elements to come, 0 otherwise.
|
|
|
|
*/
|
2009-11-09 20:27:56 +01:00
|
|
|
static int
|
2008-10-19 19:14:55 +02:00
|
|
|
luaA_next(lua_State *L, int idx)
|
|
|
|
{
|
|
|
|
if(luaL_getmetafield(L, idx, "__next"))
|
|
|
|
{
|
|
|
|
/* if idx is relative, reduce it since we got __next */
|
|
|
|
if(idx < 0) idx--;
|
|
|
|
/* copy table and then move key */
|
|
|
|
lua_pushvalue(L, idx);
|
|
|
|
lua_pushvalue(L, -3);
|
|
|
|
lua_remove(L, -4);
|
|
|
|
lua_pcall(L, 2, 2, 0);
|
|
|
|
/* next returned nil, it's the end */
|
|
|
|
if(lua_isnil(L, -1))
|
|
|
|
{
|
|
|
|
/* remove nil */
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2008-12-09 16:45:02 +01:00
|
|
|
else if(lua_istable(L, idx))
|
|
|
|
return lua_next(L, idx);
|
2008-12-10 10:10:49 +01:00
|
|
|
/* remove the key */
|
|
|
|
lua_pop(L, 1);
|
2008-12-09 16:45:02 +01:00
|
|
|
return 0;
|
2008-10-19 19:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Generic pairs function.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
2008-08-10 16:14:16 +02:00
|
|
|
static int
|
2008-10-19 19:14:55 +02:00
|
|
|
luaA_generic_pairs(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
|
|
|
|
lua_pushvalue(L, 1); /* state, */
|
|
|
|
lua_pushnil(L); /* and initial value */
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Overload standard pairs function to use __pairs field of metatables.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaAe_pairs(lua_State *L)
|
2008-08-10 16:14:16 +02:00
|
|
|
{
|
|
|
|
if(luaL_getmetafield(L, 1, "__pairs"))
|
|
|
|
{
|
|
|
|
lua_insert(L, 1);
|
|
|
|
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
|
return luaA_generic_pairs(L);
|
|
|
|
}
|
|
|
|
|
2008-11-06 17:33:58 +01:00
|
|
|
static int
|
|
|
|
luaA_ipairs_aux(lua_State *L)
|
|
|
|
{
|
|
|
|
int i = luaL_checkint(L, 2) + 1;
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
|
lua_pushinteger(L, i);
|
|
|
|
lua_rawgeti(L, 1, i);
|
|
|
|
return (lua_isnil(L, -1)) ? 0 : 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Overload standard ipairs function to use __ipairs field of metatables.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaAe_ipairs(lua_State *L)
|
|
|
|
{
|
|
|
|
if(luaL_getmetafield(L, 1, "__ipairs"))
|
|
|
|
{
|
|
|
|
lua_insert(L, 1);
|
|
|
|
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_pushinteger(L, 0); /* and initial value */
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2008-12-03 22:33:11 +01:00
|
|
|
/** Enhanced type() function which recognize awesome objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of arguments pushed on the stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaAe_type(lua_State *L)
|
|
|
|
{
|
|
|
|
luaL_checkany(L, 1);
|
2009-06-15 17:57:04 +02:00
|
|
|
lua_pushstring(L, luaA_typename(L, 1));
|
2008-12-03 22:33:11 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-10-01 09:55:28 +02:00
|
|
|
/** Replace various standards Lua functions with our own.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
*/
|
2008-06-25 11:42:06 +02:00
|
|
|
static void
|
|
|
|
luaA_fixups(lua_State *L)
|
|
|
|
{
|
2008-12-15 14:06:22 +01:00
|
|
|
/* export string.wlen */
|
2008-06-25 11:42:06 +02:00
|
|
|
lua_getglobal(L, "string");
|
|
|
|
lua_pushcfunction(L, luaA_mbstrlen);
|
2008-12-15 14:06:22 +01:00
|
|
|
lua_setfield(L, -2, "wlen");
|
2008-06-25 11:42:06 +02:00
|
|
|
lua_pop(L, 1);
|
2008-11-06 17:33:58 +01:00
|
|
|
/* replace next */
|
2008-08-10 16:14:16 +02:00
|
|
|
lua_pushliteral(L, "next");
|
2008-10-19 19:14:55 +02:00
|
|
|
lua_pushcfunction(L, luaAe_next);
|
2008-08-10 16:14:16 +02:00
|
|
|
lua_settable(L, LUA_GLOBALSINDEX);
|
2008-11-06 17:33:58 +01:00
|
|
|
/* replace pairs */
|
2008-08-10 16:14:16 +02:00
|
|
|
lua_pushliteral(L, "pairs");
|
2008-10-19 19:14:55 +02:00
|
|
|
lua_pushcfunction(L, luaAe_next);
|
|
|
|
lua_pushcclosure(L, luaAe_pairs, 1); /* pairs get next as upvalue */
|
2008-08-10 16:14:16 +02:00
|
|
|
lua_settable(L, LUA_GLOBALSINDEX);
|
2008-11-06 17:33:58 +01:00
|
|
|
/* replace ipairs */
|
|
|
|
lua_pushliteral(L, "ipairs");
|
|
|
|
lua_pushcfunction(L, luaA_ipairs_aux);
|
|
|
|
lua_pushcclosure(L, luaAe_ipairs, 1);
|
|
|
|
lua_settable(L, LUA_GLOBALSINDEX);
|
2008-12-03 22:33:11 +01:00
|
|
|
/* replace type */
|
|
|
|
lua_pushliteral(L, "type");
|
|
|
|
lua_pushcfunction(L, luaAe_type);
|
|
|
|
lua_settable(L, LUA_GLOBALSINDEX);
|
2009-01-06 17:02:08 +01:00
|
|
|
/* set selection */
|
|
|
|
lua_pushliteral(L, "selection");
|
|
|
|
lua_pushcfunction(L, luaA_selection_get);
|
|
|
|
lua_settable(L, LUA_GLOBALSINDEX);
|
2008-06-25 11:42:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-19 19:14:55 +02:00
|
|
|
/** Look for an item: table, function, etc.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param item The pointer item.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
luaA_hasitem(lua_State *L, const void *item)
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(luaA_next(L, -2))
|
|
|
|
{
|
|
|
|
if(lua_topointer(L, -1) == item)
|
|
|
|
{
|
|
|
|
/* remove value and key */
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(lua_istable(L, -1))
|
|
|
|
if(luaA_hasitem(L, item))
|
|
|
|
{
|
|
|
|
/* remove key and value */
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/* remove value */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-03 11:07:18 +02:00
|
|
|
/** Browse a table pushed on top of the index, and put all its table and
|
|
|
|
* sub-table into an array.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param elems The elements array.
|
|
|
|
* \return False if we encounter an elements already in list.
|
|
|
|
*/
|
|
|
|
static bool
|
2009-06-20 14:44:05 +02:00
|
|
|
luaA_isloop_check(lua_State *L, cptr_array_t *elems)
|
2009-04-03 11:07:18 +02:00
|
|
|
{
|
2009-04-03 11:44:08 +02:00
|
|
|
if(lua_istable(L, -1))
|
|
|
|
{
|
|
|
|
const void *object = lua_topointer(L, -1);
|
2009-04-03 11:07:18 +02:00
|
|
|
|
2009-04-03 11:44:08 +02:00
|
|
|
/* Check that the object table is not already in the list */
|
|
|
|
for(int i = 0; i < elems->len; i++)
|
|
|
|
if(elems->tab[i] == object)
|
|
|
|
return false;
|
2009-04-03 11:07:18 +02:00
|
|
|
|
2009-04-03 11:44:08 +02:00
|
|
|
/* push the table in the elements list */
|
2009-06-20 14:44:05 +02:00
|
|
|
cptr_array_append(elems, object);
|
2009-04-03 11:07:18 +02:00
|
|
|
|
2009-04-03 11:44:08 +02:00
|
|
|
/* look every object in the "table" */
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(luaA_next(L, -2))
|
2009-04-03 11:07:18 +02:00
|
|
|
{
|
2009-04-03 11:44:08 +02:00
|
|
|
if(!luaA_isloop_check(L, elems))
|
|
|
|
{
|
|
|
|
/* remove key and value */
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* remove value, keep key for next iteration */
|
|
|
|
lua_pop(L, 1);
|
2009-04-03 11:07:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if a table is a loop. When using tables as direct acyclic digram,
|
2008-10-27 14:17:12 +01:00
|
|
|
* this is useful.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param idx The index of the table in the stack
|
|
|
|
* \return True if the table loops.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
luaA_isloop(lua_State *L, int idx)
|
|
|
|
{
|
2009-04-03 11:07:18 +02:00
|
|
|
/* elems is an elements array that we will fill with all array we
|
|
|
|
* encounter while browsing the tables */
|
2009-06-20 14:44:05 +02:00
|
|
|
cptr_array_t elems;
|
2009-04-03 11:07:18 +02:00
|
|
|
|
2009-06-20 14:44:05 +02:00
|
|
|
cptr_array_init(&elems);
|
2009-04-03 11:07:18 +02:00
|
|
|
|
|
|
|
/* push table on top */
|
|
|
|
lua_pushvalue(L, idx);
|
|
|
|
|
2009-04-03 11:24:03 +02:00
|
|
|
bool ret = luaA_isloop_check(L, &elems);
|
|
|
|
|
|
|
|
/* remove pushed table */
|
|
|
|
lua_pop(L, 1);
|
2009-04-03 11:07:18 +02:00
|
|
|
|
2009-06-20 14:44:05 +02:00
|
|
|
cptr_array_wipe(&elems);
|
2009-04-03 11:07:18 +02:00
|
|
|
|
|
|
|
return !ret;
|
2008-10-27 14:17:12 +01:00
|
|
|
}
|
|
|
|
|
2008-11-13 15:06:42 +01:00
|
|
|
/** awesome global table.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
2008-11-13 15:11:37 +01:00
|
|
|
* \luastack
|
|
|
|
* \lfield font The default font.
|
2009-05-05 15:43:04 +02:00
|
|
|
* \lfield font_height The default font height.
|
2008-11-13 15:11:37 +01:00
|
|
|
* \lfield conffile The configuration file which has been loaded.
|
2008-11-13 15:06:42 +01:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_awesome_index(lua_State *L)
|
|
|
|
{
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
2010-09-02 19:08:08 +02:00
|
|
|
const char *buf = luaL_checkstring(L, 2);
|
2008-11-13 15:06:42 +01:00
|
|
|
|
2011-11-17 16:38:39 +01:00
|
|
|
if(A_STREQ(buf, "conffile"))
|
|
|
|
{
|
2009-11-10 10:22:04 +01:00
|
|
|
lua_pushstring(L, conffile);
|
2011-11-17 16:38:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(A_STREQ(buf, "version"))
|
|
|
|
{
|
2009-04-28 16:21:50 +02:00
|
|
|
lua_pushliteral(L, AWESOME_VERSION);
|
2011-11-17 16:38:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(A_STREQ(buf, "release"))
|
|
|
|
{
|
2009-04-28 16:21:50 +02:00
|
|
|
lua_pushliteral(L, AWESOME_RELEASE);
|
2011-11-17 16:38:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(A_STREQ(buf, "startup_errors") && globalconf.startup_errors.len != 0)
|
2011-10-23 15:59:15 +02:00
|
|
|
{
|
|
|
|
lua_pushstring(L, globalconf.startup_errors.s);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-11-13 15:06:42 +01:00
|
|
|
|
2011-11-17 16:38:39 +01:00
|
|
|
return 0;
|
2008-11-13 15:06:42 +01:00
|
|
|
}
|
|
|
|
|
2009-07-31 16:28:17 +02:00
|
|
|
/** Add a global signal.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam A string with the event name.
|
|
|
|
* \lparam The function to call.
|
|
|
|
*/
|
|
|
|
static int
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_awesome_connect_signal(lua_State *L)
|
2009-07-31 16:28:17 +02:00
|
|
|
{
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
|
|
|
luaA_checkfunction(L, 2);
|
2010-08-25 20:48:42 +02:00
|
|
|
signal_connect(&global_signals, name, luaA_object_ref(L, 2));
|
2009-07-31 16:28:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove a global signal.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam A string with the event name.
|
|
|
|
* \lparam The function to call.
|
|
|
|
*/
|
|
|
|
static int
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_awesome_disconnect_signal(lua_State *L)
|
2009-07-31 16:28:17 +02:00
|
|
|
{
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
|
|
|
luaA_checkfunction(L, 2);
|
|
|
|
const void *func = lua_topointer(L, 2);
|
2010-08-25 20:48:42 +02:00
|
|
|
signal_disconnect(&global_signals, name, func);
|
2009-07-31 16:28:17 +02:00
|
|
|
luaA_object_unref(L, (void *) func);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Emit a global signal.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam A string with the event name.
|
|
|
|
* \lparam The function to call.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_awesome_emit_signal(lua_State *L)
|
|
|
|
{
|
2009-08-18 18:11:27 +02:00
|
|
|
signal_object_emit(L, &global_signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
|
2009-07-31 16:28:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-27 16:04:40 +02:00
|
|
|
static int
|
|
|
|
luaA_panic(lua_State *L)
|
|
|
|
{
|
2009-11-09 12:44:19 +01:00
|
|
|
warn("unprotected error in call to Lua API (%s)",
|
2009-08-27 16:04:40 +02:00
|
|
|
lua_tostring(L, -1));
|
2009-11-09 12:44:19 +01:00
|
|
|
buffer_t buf;
|
|
|
|
backtrace_get(&buf);
|
|
|
|
warn("dumping backtrace\n%s", buf.s);
|
|
|
|
warn("restarting awesome");
|
2009-08-27 16:04:40 +02:00
|
|
|
awesome_restart();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-27 17:53:51 +02:00
|
|
|
static int
|
|
|
|
luaA_dofunction_on_error(lua_State *L)
|
|
|
|
{
|
2009-08-27 17:55:47 +02:00
|
|
|
/* duplicate string error */
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
/* emit error signal */
|
|
|
|
signal_object_emit(L, &global_signals, "debug::error", 1);
|
|
|
|
|
2009-08-27 17:53:51 +02:00
|
|
|
if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
|
|
|
|
{
|
|
|
|
/* Move traceback before error */
|
|
|
|
lua_insert(L, -2);
|
|
|
|
/* Insert sentence */
|
|
|
|
lua_pushliteral(L, "\nerror: ");
|
|
|
|
/* Move it before error */
|
|
|
|
lua_insert(L, -2);
|
|
|
|
lua_concat(L, 3);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-06-08 21:49:15 +02:00
|
|
|
/** Initialize the Lua VM
|
2009-04-08 15:47:31 +02:00
|
|
|
* \param xdg An xdg handle to use to get XDG basedir.
|
2008-06-08 21:49:15 +02:00
|
|
|
*/
|
|
|
|
void
|
2009-04-18 18:50:55 +02:00
|
|
|
luaA_init(xdgHandle* xdg)
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
|
|
|
lua_State *L;
|
|
|
|
static const struct luaL_reg awesome_lib[] =
|
|
|
|
{
|
|
|
|
{ "quit", luaA_quit },
|
|
|
|
{ "exec", luaA_exec },
|
2008-08-14 22:16:13 +02:00
|
|
|
{ "spawn", luaA_spawn },
|
2008-05-20 15:39:47 +02:00
|
|
|
{ "restart", luaA_restart },
|
2009-10-09 20:39:55 +02:00
|
|
|
{ "connect_signal", luaA_awesome_connect_signal },
|
|
|
|
{ "disconnect_signal", luaA_awesome_disconnect_signal },
|
2009-07-31 16:28:17 +02:00
|
|
|
{ "emit_signal", luaA_awesome_emit_signal },
|
2010-10-06 15:38:44 +02:00
|
|
|
{ "systray", luaA_systray },
|
2012-01-29 14:08:57 +01:00
|
|
|
{ "load_image", luaA_load_image },
|
2008-11-13 15:06:42 +01:00
|
|
|
{ "__index", luaA_awesome_index },
|
2008-05-20 15:39:47 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2008-06-28 13:57:11 +02:00
|
|
|
L = globalconf.L = luaL_newstate();
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-08-27 16:04:40 +02:00
|
|
|
/* Set panic function */
|
|
|
|
lua_atpanic(L, luaA_panic);
|
|
|
|
|
2009-08-27 17:53:51 +02:00
|
|
|
/* Set error handling function */
|
|
|
|
lualib_dofunction_on_error = luaA_dofunction_on_error;
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
luaL_openlibs(L);
|
|
|
|
|
2008-06-25 11:42:06 +02:00
|
|
|
luaA_fixups(L);
|
|
|
|
|
2009-07-28 10:40:51 +02:00
|
|
|
luaA_object_setup(L);
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
/* Export awesome lib */
|
2008-11-13 15:06:42 +01:00
|
|
|
luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-08-14 16:46:35 +02:00
|
|
|
/* Export root lib */
|
|
|
|
luaL_register(L, "root", awesome_root_lib);
|
|
|
|
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
|
2009-01-05 14:31:42 +01:00
|
|
|
|
2009-04-18 16:07:31 +02:00
|
|
|
#ifdef WITH_DBUS
|
2008-11-18 16:31:27 +01:00
|
|
|
/* Export D-Bus lib */
|
|
|
|
luaL_register(L, "dbus", awesome_dbus_lib);
|
2009-05-09 10:09:21 +02:00
|
|
|
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
|
2009-04-18 16:07:31 +02:00
|
|
|
#endif
|
2008-11-18 16:31:27 +01:00
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
/* Export keygrabber lib */
|
|
|
|
luaL_register(L, "keygrabber", awesome_keygrabber_lib);
|
2009-05-09 10:09:21 +02:00
|
|
|
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
|
2008-06-10 16:50:55 +02:00
|
|
|
|
2008-11-13 16:08:34 +01:00
|
|
|
/* Export mousegrabber lib */
|
|
|
|
luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
|
2009-05-09 10:09:21 +02:00
|
|
|
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
|
2008-11-13 16:08:34 +01:00
|
|
|
|
2008-08-11 17:14:02 +02:00
|
|
|
/* Export screen */
|
|
|
|
luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);
|
|
|
|
|
2008-06-13 15:35:47 +02:00
|
|
|
/* Export mouse */
|
|
|
|
luaA_openlib(L, "mouse", awesome_mouse_methods, awesome_mouse_meta);
|
|
|
|
|
2008-08-12 13:23:10 +02:00
|
|
|
/* Export button */
|
2009-08-17 14:10:03 +02:00
|
|
|
button_class_setup(L);
|
2008-08-12 13:23:10 +02:00
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
/* Export tag */
|
2009-07-01 16:01:44 +02:00
|
|
|
tag_class_setup(L);
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-09-30 16:04:42 +02:00
|
|
|
/* Export window */
|
|
|
|
window_class_setup(L);
|
|
|
|
|
2010-10-06 13:35:14 +02:00
|
|
|
/* Export drawin */
|
|
|
|
drawin_class_setup(L);
|
2008-09-21 20:39:23 +02:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
/* Export client */
|
2009-08-17 17:02:45 +02:00
|
|
|
client_class_setup(L);
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-05-28 12:15:00 +02:00
|
|
|
/* Export keys */
|
2009-06-24 17:44:06 +02:00
|
|
|
key_class_setup(L);
|
2008-05-28 12:15:00 +02:00
|
|
|
|
2009-06-30 12:10:30 +02:00
|
|
|
/* Export timer */
|
|
|
|
timer_class_setup(L);
|
|
|
|
|
2009-09-04 21:53:38 +02:00
|
|
|
/* add Lua search paths */
|
2009-05-09 10:09:21 +02:00
|
|
|
lua_getglobal(L, "package");
|
|
|
|
if (LUA_TTABLE != lua_type(L, 1))
|
|
|
|
{
|
|
|
|
warn("package is not a table");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lua_getfield(L, 1, "path");
|
|
|
|
if (LUA_TSTRING != lua_type(L, 2))
|
|
|
|
{
|
|
|
|
warn("package.path is not a string");
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-08 14:22:20 +01:00
|
|
|
|
2009-04-07 17:57:01 +02:00
|
|
|
/* add XDG_CONFIG_DIR as include path */
|
|
|
|
const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
|
|
|
|
for(; *xdgconfigdirs; xdgconfigdirs++)
|
2008-11-08 14:22:20 +01:00
|
|
|
{
|
2009-04-26 20:22:10 +02:00
|
|
|
size_t len = a_strlen(*xdgconfigdirs);
|
2009-05-09 10:09:21 +02:00
|
|
|
lua_pushliteral(L, ";");
|
|
|
|
lua_pushlstring(L, *xdgconfigdirs, len);
|
|
|
|
lua_pushliteral(L, "/awesome/?.lua");
|
|
|
|
lua_concat(L, 3);
|
|
|
|
|
|
|
|
lua_pushliteral(L, ";");
|
|
|
|
lua_pushlstring(L, *xdgconfigdirs, len);
|
|
|
|
lua_pushliteral(L, "/awesome/?/init.lua");
|
|
|
|
lua_concat(L, 3);
|
|
|
|
|
|
|
|
lua_concat(L, 3); /* concatenate with package.path */
|
2008-11-08 14:22:20 +01:00
|
|
|
}
|
2009-09-04 21:53:38 +02:00
|
|
|
|
|
|
|
/* add Lua lib path (/usr/share/awesome/lib by default) */
|
|
|
|
lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua");
|
|
|
|
lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua");
|
|
|
|
lua_concat(L, 3); /* concatenate with package.path */
|
2009-05-09 10:09:21 +02:00
|
|
|
lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */
|
2010-08-22 17:01:28 +02:00
|
|
|
|
|
|
|
lua_getfield(L, 1, "loaded");
|
|
|
|
|
|
|
|
/* Load oocairo */
|
|
|
|
if (luaopen_oocairo(L) != 1)
|
|
|
|
fatal("Loading oocairo failed");
|
|
|
|
lua_pushvalue(L, 3); /* Copy the module */
|
|
|
|
lua_setglobal(L, "oocairo"); /* Set the global entry */
|
|
|
|
lua_setfield(L, 2, "oocairo"); /* Make it require()able */
|
|
|
|
|
|
|
|
/* Load oopango */
|
|
|
|
if (luaopen_oopango(L) != 1)
|
|
|
|
fatal("Loading oopango failed");
|
|
|
|
lua_pushvalue(L, 3); /* Copy the module */
|
|
|
|
lua_setglobal(L, "oopango"); /* Set the global entry */
|
|
|
|
lua_setfield(L, 2, "oopango"); /* Make it require()able */
|
|
|
|
|
|
|
|
lua_pop(L, 2); /* pop "package" and "package.loaded" */
|
2010-08-25 23:00:36 +02:00
|
|
|
|
2010-08-27 21:42:14 +02:00
|
|
|
signal_add(&global_signals, "debug::error");
|
2011-12-05 00:34:04 +01:00
|
|
|
signal_add(&global_signals, "debug::deprecation");
|
2010-08-25 23:00:36 +02:00
|
|
|
signal_add(&global_signals, "debug::index::miss");
|
|
|
|
signal_add(&global_signals, "debug::newindex::miss");
|
2010-10-06 15:38:44 +02:00
|
|
|
signal_add(&global_signals, "systray::update");
|
2012-04-07 21:52:24 +02:00
|
|
|
signal_add(&global_signals, "wallpaper_changed");
|
2010-10-15 18:48:07 +02:00
|
|
|
signal_add(&global_signals, "refresh");
|
2010-08-26 17:46:17 +02:00
|
|
|
signal_add(&global_signals, "exit");
|
2008-11-08 14:22:20 +01:00
|
|
|
}
|
2008-07-08 14:07:56 +02:00
|
|
|
|
2011-10-23 15:59:15 +02:00
|
|
|
static void
|
|
|
|
luaA_startup_error(const char *err)
|
|
|
|
{
|
|
|
|
if (globalconf.startup_errors.len > 0)
|
|
|
|
buffer_addsl(&globalconf.startup_errors, "\n\n");
|
|
|
|
buffer_adds(&globalconf.startup_errors, err);
|
|
|
|
}
|
|
|
|
|
2008-09-30 16:59:08 +02:00
|
|
|
static bool
|
|
|
|
luaA_loadrc(const char *confpath, bool run)
|
|
|
|
{
|
2009-03-02 14:16:08 +01:00
|
|
|
if(!luaL_loadfile(globalconf.L, confpath))
|
2008-09-30 16:59:08 +02:00
|
|
|
{
|
2009-03-02 14:16:08 +01:00
|
|
|
if(run)
|
2008-09-30 16:59:08 +02:00
|
|
|
{
|
2011-09-17 17:21:34 +02:00
|
|
|
/* Set the conffile right now so it can be used inside the
|
|
|
|
* configuration file. */
|
|
|
|
conffile = a_strdup(confpath);
|
2009-03-02 14:16:08 +01:00
|
|
|
if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
|
2011-09-17 17:21:34 +02:00
|
|
|
{
|
2011-10-23 15:59:15 +02:00
|
|
|
const char *err = lua_tostring(globalconf.L, -1);
|
|
|
|
luaA_startup_error(err);
|
|
|
|
fprintf(stderr, "%s\n", err);
|
2011-09-17 17:21:34 +02:00
|
|
|
/* An error happened, so reset this. */
|
|
|
|
conffile = NULL;
|
|
|
|
}
|
2009-03-02 14:16:08 +01:00
|
|
|
else
|
|
|
|
return true;
|
2008-09-30 16:59:08 +02:00
|
|
|
}
|
|
|
|
else
|
2010-07-15 19:21:02 +02:00
|
|
|
{
|
2009-03-02 14:16:08 +01:00
|
|
|
lua_pop(globalconf.L, 1);
|
2010-07-15 19:21:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-09-30 16:59:08 +02:00
|
|
|
}
|
2009-03-02 14:16:08 +01:00
|
|
|
else
|
2011-10-23 15:59:15 +02:00
|
|
|
{
|
|
|
|
const char *err = lua_tostring(globalconf.L, -1);
|
|
|
|
luaA_startup_error(err);
|
|
|
|
fprintf(stderr, "%s\n", err);
|
|
|
|
}
|
2009-03-02 14:16:08 +01:00
|
|
|
|
2008-09-30 16:59:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-08 14:07:56 +02:00
|
|
|
/** Load a configuration file.
|
2009-04-08 15:47:31 +02:00
|
|
|
* \param xdg An xdg handle to use to get XDG basedir.
|
2008-09-30 16:59:08 +02:00
|
|
|
* \param confpatharg The configuration file to load.
|
|
|
|
* \param run Run the configuration file.
|
2008-06-08 21:49:15 +02:00
|
|
|
*/
|
2008-09-30 16:59:08 +02:00
|
|
|
bool
|
2009-04-18 18:50:55 +02:00
|
|
|
luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
|
2008-06-08 21:49:15 +02:00
|
|
|
{
|
2009-04-07 17:57:01 +02:00
|
|
|
char *confpath = NULL;
|
2008-11-08 14:22:20 +01:00
|
|
|
bool ret = false;
|
2008-07-08 14:07:56 +02:00
|
|
|
|
2008-11-08 14:22:20 +01:00
|
|
|
/* try to load, return if it's ok */
|
2009-03-02 14:16:08 +01:00
|
|
|
if(confpatharg)
|
2008-07-08 14:07:56 +02:00
|
|
|
{
|
2009-03-02 14:16:08 +01:00
|
|
|
if(luaA_loadrc(confpatharg, run))
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
else if(!run)
|
|
|
|
goto bailout;
|
2008-07-08 14:07:56 +02:00
|
|
|
}
|
2008-11-08 14:22:20 +01:00
|
|
|
|
2009-04-07 17:57:01 +02:00
|
|
|
confpath = xdgConfigFind("awesome/rc.lua", xdg);
|
2008-07-08 14:07:56 +02:00
|
|
|
|
2009-04-07 17:57:01 +02:00
|
|
|
char *tmp = confpath;
|
2008-07-08 14:07:56 +02:00
|
|
|
|
2009-04-07 17:57:01 +02:00
|
|
|
/* confpath is "string1\0string2\0string3\0\0" */
|
2009-06-01 13:01:31 +02:00
|
|
|
while(*tmp)
|
2008-07-08 14:07:56 +02:00
|
|
|
{
|
2009-04-07 17:57:01 +02:00
|
|
|
if(luaA_loadrc(tmp, run))
|
2008-11-08 14:22:20 +01:00
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
goto bailout;
|
|
|
|
}
|
2009-03-02 14:16:08 +01:00
|
|
|
else if(!run)
|
|
|
|
goto bailout;
|
2009-04-07 17:57:01 +02:00
|
|
|
tmp += a_strlen(tmp) + 1;
|
2009-06-01 13:01:31 +02:00
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-11-08 14:22:20 +01:00
|
|
|
bailout:
|
|
|
|
|
|
|
|
p_delete(&confpath);
|
|
|
|
|
2008-09-30 16:59:08 +02:00
|
|
|
return ret;
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
|
2009-08-20 17:37:46 +02:00
|
|
|
int
|
|
|
|
luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
|
|
|
|
{
|
|
|
|
signal_object_emit(L, &global_signals, "debug::index::miss", 2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
|
|
|
|
{
|
|
|
|
signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-15 18:48:07 +02:00
|
|
|
void
|
|
|
|
luaA_emit_refresh()
|
|
|
|
{
|
|
|
|
signal_object_emit(globalconf.L, &global_signals, "refresh", 0);
|
|
|
|
}
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|