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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-11-10 10:35:53 +01:00
|
|
|
#include "common/util.h"
|
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>
|
|
|
|
|
|
|
|
#include <xcb/xcb.h>
|
2009-04-12 01:55:37 +02:00
|
|
|
#include <xcb/xtest.h>
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-04-07 17:57:01 +02:00
|
|
|
#include <basedir_fs.h>
|
|
|
|
|
2008-09-02 17:12:10 +02:00
|
|
|
#include "awesome.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-05-20 19:55:14 +02:00
|
|
|
#include "config.h"
|
2008-09-17 16:38:38 +02:00
|
|
|
#include "luaa.h"
|
2009-04-03 13:09:17 +02:00
|
|
|
#include "spawn.h"
|
2008-06-09 21:43:09 +02:00
|
|
|
#include "tag.h"
|
2008-05-20 15:39:47 +02:00
|
|
|
#include "client.h"
|
2008-06-17 22:12:30 +02:00
|
|
|
#include "screen.h"
|
2008-08-26 19:35:37 +02:00
|
|
|
#include "event.h"
|
2008-08-12 13:23:10 +02:00
|
|
|
#include "mouse.h"
|
2009-01-06 17:02:08 +01:00
|
|
|
#include "selection.h"
|
2009-02-24 17:32:48 +01:00
|
|
|
#include "window.h"
|
2009-04-03 17:11:51 +02:00
|
|
|
#include "common/xcursor.h"
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-10-26 18:54:22 +01:00
|
|
|
extern const struct luaL_reg awesome_hooks_lib[];
|
2008-11-18 16:31:27 +01:00
|
|
|
extern const struct luaL_reg awesome_dbus_lib[];
|
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[];
|
2008-08-12 13:23:10 +02:00
|
|
|
extern const struct luaL_reg awesome_button_methods[];
|
|
|
|
extern const struct luaL_reg awesome_button_meta[];
|
2008-09-11 14:39:47 +02:00
|
|
|
extern const struct luaL_reg awesome_image_methods[];
|
|
|
|
extern const struct luaL_reg awesome_image_meta[];
|
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
|
|
|
extern const struct luaL_reg awesome_client_methods[];
|
|
|
|
extern const struct luaL_reg awesome_client_meta[];
|
2008-06-09 21:43:09 +02:00
|
|
|
extern const struct luaL_reg awesome_tag_methods[];
|
|
|
|
extern const struct luaL_reg awesome_tag_meta[];
|
2008-05-20 15:39:47 +02:00
|
|
|
extern const struct luaL_reg awesome_widget_methods[];
|
|
|
|
extern const struct luaL_reg awesome_widget_meta[];
|
2008-09-21 20:39:23 +02:00
|
|
|
extern const struct luaL_reg awesome_wibox_methods[];
|
|
|
|
extern const struct luaL_reg awesome_wibox_meta[];
|
2008-12-18 11:33:19 +01:00
|
|
|
extern const struct luaL_reg awesome_key_methods[];
|
|
|
|
extern const struct luaL_reg awesome_key_meta[];
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-04-12 01:55:37 +02:00
|
|
|
/** Send fake events. Usually the current focused client will get it.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of element pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lvalue A client.
|
|
|
|
* \lparam The event type: key_press, key_release, button_press, button_release
|
|
|
|
* or motion_notify.
|
|
|
|
* \lparam The detail: in case of a key event, this is the keycode to send, in
|
|
|
|
* case of a button event this is the number of the button. In case of a motion
|
|
|
|
* event, this is a boolean value which if true make the coordinates relatives.
|
|
|
|
* \lparam In case of a motion event, this is the X coordinate.
|
|
|
|
* \lparam In case of a motion event, this is the Y coordinate.
|
|
|
|
* \lparam In case of a motion event, this is the screen number to move on.
|
|
|
|
* If not specified, the current one is used.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_root_fake_input(lua_State *L)
|
|
|
|
{
|
|
|
|
if(!globalconf.have_xtest)
|
|
|
|
{
|
|
|
|
luaA_warn(L, "XTest extension is not available, cannot fake input.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t tlen;
|
|
|
|
const char *stype = luaL_checklstring(L, 2, &tlen);
|
|
|
|
uint8_t type, detail;
|
|
|
|
int x = 0, y = 0;
|
|
|
|
xcb_window_t root = XCB_NONE;
|
|
|
|
|
|
|
|
switch(a_tokenize(stype, tlen))
|
|
|
|
{
|
|
|
|
case A_TK_KEY_PRESS:
|
|
|
|
type = XCB_KEY_PRESS;
|
|
|
|
detail = luaL_checknumber(L, 3); /* keycode */
|
|
|
|
break;
|
|
|
|
case A_TK_KEY_RELEASE:
|
|
|
|
type = XCB_KEY_RELEASE;
|
|
|
|
detail = luaL_checknumber(L, 3); /* keycode */
|
|
|
|
break;
|
|
|
|
case A_TK_BUTTON_PRESS:
|
|
|
|
type = XCB_BUTTON_PRESS;
|
|
|
|
detail = luaL_checknumber(L, 3); /* button number */
|
|
|
|
break;
|
|
|
|
case A_TK_BUTTON_RELEASE:
|
|
|
|
type = XCB_BUTTON_RELEASE;
|
|
|
|
detail = luaL_checknumber(L, 3); /* button number */
|
|
|
|
break;
|
|
|
|
case A_TK_MOTION_NOTIFY:
|
|
|
|
type = XCB_MOTION_NOTIFY;
|
|
|
|
detail = luaA_checkboolean(L, 3); /* relative to the current position or not */
|
|
|
|
x = luaL_checknumber(L, 4);
|
|
|
|
y = luaL_checknumber(L, 5);
|
|
|
|
if(lua_gettop(L) == 6 && !globalconf.xinerama_is_active)
|
|
|
|
{
|
|
|
|
int screen = luaL_checknumber(L, 6);
|
|
|
|
luaA_checkscreen(screen);
|
|
|
|
root = xutil_screen_get(globalconf.connection, screen)->root;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_test_fake_input(globalconf.connection,
|
|
|
|
type,
|
|
|
|
detail,
|
|
|
|
XCB_CURRENT_TIME,
|
|
|
|
root,
|
|
|
|
x, y,
|
|
|
|
0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:59:20 +01:00
|
|
|
/** Get or set global key bindings.
|
|
|
|
* This binding will be available when you'll press keys on root window.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of element pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam An array of key bindings objects, or nothing.
|
2009-04-03 17:06:44 +02:00
|
|
|
* \lreturn The array of key bindings objects of this client.
|
2009-01-05 16:59:20 +01:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_root_keys(lua_State *L)
|
|
|
|
{
|
|
|
|
if(lua_gettop(L) == 1)
|
|
|
|
{
|
|
|
|
luaA_key_array_set(L, 1, &globalconf.keys);
|
|
|
|
|
|
|
|
int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
|
|
|
|
|
|
|
|
for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
|
|
|
|
{
|
|
|
|
xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
|
|
|
|
xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
|
|
|
|
window_grabkeys(s->root, &globalconf.keys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return luaA_key_array_get(L, &globalconf.keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get or set global mouse bindings.
|
2008-08-12 13:23:10 +02:00
|
|
|
* This binding will be available when you'll click on root window.
|
2008-06-10 20:12:51 +02:00
|
|
|
* \param L The Lua VM state.
|
2008-08-12 13:23:10 +02:00
|
|
|
* \return The number of element pushed on stack.
|
2008-06-10 20:12:51 +02:00
|
|
|
* \luastack
|
2008-08-12 13:23:10 +02:00
|
|
|
* \lparam An array of mouse button bindings objects, or nothing.
|
2009-04-03 17:06:44 +02:00
|
|
|
* \lreturn The array of mouse button bindings objects.
|
2008-05-26 20:39:54 +02:00
|
|
|
*/
|
2008-05-20 15:39:47 +02:00
|
|
|
static int
|
2009-01-05 14:31:42 +01:00
|
|
|
luaA_root_buttons(lua_State *L)
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
2008-08-12 13:23:10 +02:00
|
|
|
if(lua_gettop(L) == 1)
|
2009-01-05 16:59:20 +01:00
|
|
|
luaA_button_array_set(L, 1, &globalconf.buttons);
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2009-01-05 16:59:20 +01:00
|
|
|
return luaA_button_array_get(L, &globalconf.buttons);
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
|
2009-04-03 17:11:51 +02:00
|
|
|
/** Set the root cursor.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of element pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam A X cursor name.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_root_cursor(lua_State *L)
|
|
|
|
{
|
|
|
|
const char *cursor_name = luaL_checkstring(L, 1);
|
|
|
|
uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
|
|
|
|
|
|
|
|
if(cursor_font)
|
|
|
|
{
|
|
|
|
uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
|
|
|
|
|
|
|
|
for(int screen_nbr = 0;
|
|
|
|
screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
|
|
|
|
screen_nbr++)
|
|
|
|
xcb_change_window_attributes(globalconf.connection,
|
|
|
|
xutil_screen_get(globalconf.connection, screen_nbr)->root,
|
|
|
|
XCB_CW_CURSOR,
|
|
|
|
change_win_vals);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
luaA_warn(L, "invalid cursor %s", cursor_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
luaA_quit(lua_State *L __attribute__ ((unused)))
|
|
|
|
{
|
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);
|
|
|
|
|
2008-09-02 17:12:10 +02:00
|
|
|
awesome_atexit();
|
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
|
|
|
|
luaA_restart(lua_State *L __attribute__ ((unused)))
|
|
|
|
{
|
2008-09-02 17:12:10 +02:00
|
|
|
awesome_restart();
|
2008-05-20 15:39:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-26 20:39:54 +02:00
|
|
|
static void
|
|
|
|
luaA_openlib(lua_State *L, const char *name,
|
|
|
|
const struct luaL_reg methods[],
|
|
|
|
const struct luaL_reg meta[])
|
|
|
|
{
|
2008-06-28 00:52:12 +02:00
|
|
|
luaL_newmetatable(L, name); /* 1 */
|
|
|
|
lua_pushvalue(L, -1); /* dup metatable 2 */
|
|
|
|
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
|
|
|
|
|
|
|
|
luaL_register(L, NULL, meta); /* 1 */
|
|
|
|
luaL_register(L, name, methods); /* 2 */
|
|
|
|
lua_pushvalue(L, -1); /* dup self as metatable 3 */
|
|
|
|
lua_setmetatable(L, -2); /* set self as metatable 2 */
|
|
|
|
lua_pop(L, 2);
|
2008-05-26 20:39:54 +02:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* \param 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
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
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);
|
|
|
|
#define CHECK_TYPE(type) \
|
|
|
|
do { \
|
2009-04-11 13:39:15 +02:00
|
|
|
if(luaA_toudata(L, 1, #type)) \
|
2008-12-03 22:33:11 +01:00
|
|
|
{ \
|
|
|
|
lua_pushliteral(L, #type); \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
CHECK_TYPE(wibox);
|
|
|
|
CHECK_TYPE(client);
|
|
|
|
CHECK_TYPE(image);
|
2008-12-18 11:33:19 +01:00
|
|
|
CHECK_TYPE(key);
|
2008-12-03 22:33:11 +01:00
|
|
|
CHECK_TYPE(button);
|
|
|
|
CHECK_TYPE(tag);
|
|
|
|
CHECK_TYPE(widget);
|
|
|
|
#undef CHECK_TYPE
|
|
|
|
lua_pushstring(L, luaL_typename(L, 1));
|
|
|
|
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
|
|
|
/** __next function for wtable objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_wtable_next(lua_State *L)
|
|
|
|
{
|
|
|
|
/* upvalue 1 is content table */
|
|
|
|
if(lua_next(L, lua_upvalueindex(1)))
|
|
|
|
return 2;
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-11-06 17:33:58 +01:00
|
|
|
/** __ipairs function for wtable objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_wtable_ipairs(lua_State *L)
|
|
|
|
{
|
|
|
|
/* push ipairs_aux */
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(2));
|
|
|
|
/* push content table */
|
|
|
|
lua_pushvalue(L, lua_upvalueindex(1));
|
|
|
|
lua_pushinteger(L, 0); /* and initial value */
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2008-10-19 19:14:55 +02:00
|
|
|
/** Index function of wtable objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_wtable_index(lua_State *L)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char *buf;
|
|
|
|
|
|
|
|
lua_pushvalue(L, 2);
|
|
|
|
/* check for size, waiting lua 5.2 and __len on tables */
|
|
|
|
if((buf = lua_tolstring(L, -1, &len)))
|
|
|
|
if(a_tokenize(buf, len) == A_TK_LEN)
|
|
|
|
{
|
|
|
|
lua_pushnumber(L, lua_objlen(L, lua_upvalueindex(1)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
/* upvalue 1 is content table */
|
|
|
|
lua_rawget(L, lua_upvalueindex(1));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Newndex function of wtable objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_wtable_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
bool invalid = false;
|
|
|
|
|
|
|
|
/* push key on top */
|
|
|
|
lua_pushvalue(L, 2);
|
|
|
|
/* get current key value in content table */
|
|
|
|
lua_rawget(L, lua_upvalueindex(1));
|
|
|
|
/* if value is a widget, notify change */
|
2009-04-11 13:39:15 +02:00
|
|
|
if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget"))
|
2008-10-19 19:14:55 +02:00
|
|
|
invalid = true;
|
|
|
|
|
|
|
|
lua_pop(L, 1); /* remove value */
|
|
|
|
|
|
|
|
/* if new value is a widget or a table */
|
|
|
|
if(lua_istable(L, 3))
|
|
|
|
{
|
|
|
|
luaA_table2wtable(L);
|
|
|
|
invalid = true;
|
|
|
|
}
|
2009-04-11 13:39:15 +02:00
|
|
|
else if(!invalid && luaA_toudata(L, 3, "widget"))
|
2008-10-19 19:14:55 +02:00
|
|
|
invalid = true;
|
|
|
|
|
|
|
|
/* upvalue 1 is content table */
|
|
|
|
lua_rawset(L, lua_upvalueindex(1));
|
|
|
|
|
|
|
|
if(invalid)
|
|
|
|
luaA_wibox_invalidate_byitem(L, lua_topointer(L, 1));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert the top element of the stack to a proxied wtable.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
luaA_table2wtable(lua_State *L)
|
|
|
|
{
|
|
|
|
if(!lua_istable(L, -1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
lua_newtable(L); /* create *real* content table */
|
|
|
|
lua_newtable(L); /* metatable */
|
|
|
|
lua_pushvalue(L, -2); /* copy content table */
|
2008-11-06 17:33:58 +01:00
|
|
|
lua_pushcfunction(L, luaA_ipairs_aux); /* push ipairs aux */
|
|
|
|
lua_pushcclosure(L, luaA_wtable_ipairs, 2);
|
2008-10-19 19:14:55 +02:00
|
|
|
lua_pushvalue(L, -3); /* copy content table */
|
2008-11-06 17:33:58 +01:00
|
|
|
lua_pushcclosure(L, luaA_wtable_next, 1); /* __next has the content table as upvalue */
|
2008-10-19 19:14:55 +02:00
|
|
|
lua_pushvalue(L, -4); /* copy content table */
|
2008-11-06 17:33:58 +01:00
|
|
|
lua_pushcclosure(L, luaA_wtable_index, 1); /* __index has the content table as upvalue */
|
|
|
|
lua_pushvalue(L, -5); /* copy content table */
|
2008-10-19 19:14:55 +02:00
|
|
|
lua_pushcclosure(L, luaA_wtable_newindex, 1); /* __newindex has the content table as upvalue */
|
|
|
|
/* set metatable field with just pushed closure */
|
2008-11-06 17:33:58 +01:00
|
|
|
lua_setfield(L, -5, "__newindex");
|
|
|
|
lua_setfield(L, -4, "__index");
|
|
|
|
lua_setfield(L, -3, "__next");
|
|
|
|
lua_setfield(L, -2, "__ipairs");
|
2008-10-19 19:14:55 +02:00
|
|
|
/* set metatable impossible to touch */
|
|
|
|
lua_pushliteral(L, "wtable");
|
|
|
|
lua_setfield(L, -2, "__metatable");
|
|
|
|
/* set new metatable on original table */
|
|
|
|
lua_setmetatable(L, -3);
|
|
|
|
|
|
|
|
/* initial key */
|
|
|
|
lua_pushnil(L);
|
|
|
|
/* go through original table */
|
|
|
|
while(lua_next(L, -3))
|
|
|
|
{
|
|
|
|
/* if convert value to wtable */
|
|
|
|
luaA_table2wtable(L);
|
|
|
|
/* copy key */
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
/* move key before value */
|
|
|
|
lua_insert(L, -2);
|
|
|
|
/* set same value in content table */
|
|
|
|
lua_rawset(L, -4);
|
|
|
|
/* copy key */
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
/* push the new value :-> */
|
|
|
|
lua_pushnil(L);
|
|
|
|
/* set orig[k] = nil */
|
|
|
|
lua_rawset(L, -5);
|
|
|
|
}
|
|
|
|
/* remove content table */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
luaA_isloop_check(lua_State *L, void_array_t *elems)
|
|
|
|
{
|
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 */
|
|
|
|
void_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 */
|
|
|
|
void_array_t elems;
|
|
|
|
|
|
|
|
void_array_init(&elems);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
void_array_wipe(&elems);
|
|
|
|
|
|
|
|
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.
|
|
|
|
* \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;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
const char *buf = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
switch(a_tokenize(buf, len))
|
|
|
|
{
|
|
|
|
case A_TK_FONT:
|
|
|
|
{
|
|
|
|
char *font = pango_font_description_to_string(globalconf.font->desc);
|
|
|
|
lua_pushstring(L, font);
|
|
|
|
g_free(font);
|
|
|
|
}
|
|
|
|
break;
|
2008-11-13 15:11:37 +01:00
|
|
|
case A_TK_CONFFILE:
|
|
|
|
lua_pushstring(L, globalconf.conffile);
|
|
|
|
break;
|
2008-11-13 15:18:56 +01:00
|
|
|
case A_TK_FG:
|
|
|
|
luaA_pushcolor(L, &globalconf.colors.fg);
|
|
|
|
break;
|
|
|
|
case A_TK_BG:
|
|
|
|
luaA_pushcolor(L, &globalconf.colors.bg);
|
|
|
|
break;
|
2008-11-13 15:06:42 +01:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Newindex function for the awesome global table.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_awesome_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
const char *buf = luaL_checklstring(L, 2, &len);
|
|
|
|
|
|
|
|
switch(a_tokenize(buf, len))
|
|
|
|
{
|
|
|
|
case A_TK_FONT:
|
|
|
|
{
|
|
|
|
const char *newfont = luaL_checkstring(L, 3);
|
|
|
|
draw_font_delete(&globalconf.font);
|
|
|
|
globalconf.font = draw_font_new(newfont);
|
|
|
|
}
|
|
|
|
break;
|
2008-11-13 15:18:56 +01:00
|
|
|
case A_TK_FG:
|
|
|
|
if((buf = luaL_checklstring(L, 3, &len)))
|
|
|
|
xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.fg, buf, len));
|
|
|
|
break;
|
|
|
|
case A_TK_BG:
|
|
|
|
if((buf = luaL_checklstring(L, 3, &len)))
|
|
|
|
xcolor_init_reply(xcolor_init_unchecked(&globalconf.colors.bg, buf, len));
|
|
|
|
break;
|
2008-11-13 15:06:42 +01:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-08 15:47:31 +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 },
|
2008-11-13 15:06:42 +01:00
|
|
|
{ "__index", luaA_awesome_index },
|
|
|
|
{ "__newindex", luaA_awesome_newindex },
|
2008-05-20 15:39:47 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2009-01-05 14:31:42 +01:00
|
|
|
static const struct luaL_reg root_lib[] =
|
|
|
|
{
|
|
|
|
{ "buttons", luaA_root_buttons },
|
2009-01-05 16:59:20 +01:00
|
|
|
{ "keys", luaA_root_keys },
|
2009-04-03 17:11:51 +02:00
|
|
|
{ "cursor", luaA_root_cursor },
|
2009-04-12 01:55:37 +02:00
|
|
|
{ "fake_input", luaA_root_fake_input },
|
2009-01-05 14:31:42 +01:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-06-28 13:57:11 +02:00
|
|
|
L = globalconf.L = luaL_newstate();
|
2008-05-20 15:39:47 +02:00
|
|
|
|
|
|
|
luaL_openlibs(L);
|
|
|
|
|
2008-06-25 11:42:06 +02:00
|
|
|
luaA_fixups(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-01-05 14:31:42 +01:00
|
|
|
/* Export root lib */
|
|
|
|
luaA_openlib(L, "root", root_lib, root_lib);
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
/* Export hooks lib */
|
2008-05-20 20:26:58 +02:00
|
|
|
luaL_register(L, "hooks", awesome_hooks_lib);
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-11-18 16:31:27 +01:00
|
|
|
/* Export D-Bus lib */
|
|
|
|
luaL_register(L, "dbus", awesome_dbus_lib);
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
/* Export keygrabber lib */
|
|
|
|
luaL_register(L, "keygrabber", awesome_keygrabber_lib);
|
|
|
|
|
2008-11-13 16:08:34 +01:00
|
|
|
/* Export mousegrabber lib */
|
|
|
|
luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
|
|
|
|
|
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 */
|
|
|
|
luaA_openlib(L, "button", awesome_button_methods, awesome_button_meta);
|
|
|
|
|
2008-09-11 14:39:47 +02:00
|
|
|
/* Export image */
|
|
|
|
luaA_openlib(L, "image", awesome_image_methods, awesome_image_meta);
|
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
/* Export tag */
|
|
|
|
luaA_openlib(L, "tag", awesome_tag_methods, awesome_tag_meta);
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-09-21 20:39:23 +02:00
|
|
|
/* Export wibox */
|
|
|
|
luaA_openlib(L, "wibox", awesome_wibox_methods, awesome_wibox_meta);
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
/* Export widget */
|
|
|
|
luaA_openlib(L, "widget", awesome_widget_methods, awesome_widget_meta);
|
|
|
|
|
|
|
|
/* Export client */
|
|
|
|
luaA_openlib(L, "client", awesome_client_methods, awesome_client_meta);
|
|
|
|
|
2008-05-28 12:15:00 +02:00
|
|
|
/* Export keys */
|
2008-12-18 11:33:19 +01:00
|
|
|
luaA_openlib(L, "key", awesome_key_methods, awesome_key_meta);
|
2008-05-28 12:15:00 +02:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
lua_pushliteral(L, "AWESOME_VERSION");
|
2008-07-31 17:48:27 +02:00
|
|
|
lua_pushstring(L, AWESOME_VERSION);
|
2008-05-20 15:39:47 +02:00
|
|
|
lua_settable(L, LUA_GLOBALSINDEX);
|
|
|
|
|
2008-09-05 01:29:38 +02:00
|
|
|
lua_pushliteral(L, "AWESOME_RELEASE");
|
|
|
|
lua_pushstring(L, AWESOME_RELEASE);
|
|
|
|
lua_settable(L, LUA_GLOBALSINDEX);
|
|
|
|
|
2008-07-09 11:51:08 +02:00
|
|
|
/* init hooks */
|
|
|
|
globalconf.hooks.manage = LUA_REFNIL;
|
|
|
|
globalconf.hooks.unmanage = LUA_REFNIL;
|
|
|
|
globalconf.hooks.focus = LUA_REFNIL;
|
|
|
|
globalconf.hooks.unfocus = LUA_REFNIL;
|
2008-09-22 17:54:48 +02:00
|
|
|
globalconf.hooks.mouse_enter = LUA_REFNIL;
|
2008-12-01 15:43:16 +01:00
|
|
|
globalconf.hooks.mouse_leave = LUA_REFNIL;
|
2008-07-09 11:51:08 +02:00
|
|
|
globalconf.hooks.arrange = LUA_REFNIL;
|
2008-10-20 15:01:16 +02:00
|
|
|
globalconf.hooks.clients = LUA_REFNIL;
|
2008-10-19 19:14:55 +02:00
|
|
|
globalconf.hooks.tags = LUA_REFNIL;
|
|
|
|
globalconf.hooks.tagged = LUA_REFNIL;
|
2008-09-22 17:54:48 +02:00
|
|
|
globalconf.hooks.property = LUA_REFNIL;
|
2008-07-09 11:51:08 +02:00
|
|
|
globalconf.hooks.timer = LUA_REFNIL;
|
2008-11-19 02:22:05 +01:00
|
|
|
#ifdef WITH_DBUS
|
|
|
|
globalconf.hooks.dbus = LUA_REFNIL;
|
|
|
|
#endif
|
2008-06-08 21:49:15 +02:00
|
|
|
|
2008-11-08 14:22:20 +01:00
|
|
|
/* add Lua lib path (/usr/share/awesome/lib by default) */
|
|
|
|
luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
|
|
|
|
luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?/init.lua\"");
|
|
|
|
|
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-07 17:57:01 +02:00
|
|
|
char *buf;
|
|
|
|
a_asprintf(&buf, "package.path = package.path .. \";%s/awesome/?.lua;%s/awesome/?/init.lua\"",
|
|
|
|
*xdgconfigdirs, *xdgconfigdirs);
|
|
|
|
luaA_dostring(L, buf);
|
|
|
|
p_delete(&buf);
|
2008-11-08 14:22:20 +01:00
|
|
|
}
|
|
|
|
}
|
2008-07-08 14:07:56 +02:00
|
|
|
|
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
|
|
|
{
|
2009-03-02 14:16:08 +01:00
|
|
|
if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0))
|
|
|
|
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
|
|
|
|
else
|
2008-09-30 16:59:08 +02:00
|
|
|
{
|
2009-03-02 14:16:08 +01:00
|
|
|
globalconf.conffile = a_strdup(confpath);
|
|
|
|
return true;
|
2008-09-30 16:59:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-03-02 14:16:08 +01:00
|
|
|
lua_pop(globalconf.L, 1);
|
|
|
|
return true;
|
2008-09-30 16:59:08 +02:00
|
|
|
}
|
2009-03-02 14:16:08 +01:00
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
|
|
|
|
|
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-08 15:47:31 +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" */
|
|
|
|
do
|
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;
|
|
|
|
} while(*tmp != 0);
|
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
|
|
|
}
|
|
|
|
|
2008-06-17 00:30:53 +02:00
|
|
|
void
|
|
|
|
luaA_on_timer(EV_P_ ev_timer *w, int revents)
|
|
|
|
{
|
2008-11-20 17:48:23 +01:00
|
|
|
if(globalconf.hooks.timer != LUA_REFNIL)
|
|
|
|
luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0, 0);
|
2009-04-10 17:39:11 +02:00
|
|
|
awesome_refresh();
|
2008-06-17 00:30:53 +02:00
|
|
|
}
|
2008-07-02 11:04:17 +02:00
|
|
|
|
2008-10-19 19:14:55 +02:00
|
|
|
/** Push a color as a string onto the stack
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param c The color to push.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
2008-07-02 11:04:17 +02:00
|
|
|
luaA_pushcolor(lua_State *L, const xcolor_t *c)
|
|
|
|
{
|
|
|
|
uint8_t r = (unsigned)c->red * 0xff / 0xffff;
|
|
|
|
uint8_t g = (unsigned)c->green * 0xff / 0xffff;
|
|
|
|
uint8_t b = (unsigned)c->blue * 0xff / 0xffff;
|
|
|
|
uint8_t a = (unsigned)c->alpha * 0xff / 0xffff;
|
2008-07-24 17:56:42 +02:00
|
|
|
char s[10];
|
2008-11-03 16:48:06 +01:00
|
|
|
int len;
|
2008-09-10 15:35:17 +02:00
|
|
|
/* do not print alpha if it's full */
|
|
|
|
if(a == 0xff)
|
2008-11-03 16:48:06 +01:00
|
|
|
len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
|
2008-09-10 15:35:17 +02:00
|
|
|
else
|
2008-11-03 16:48:06 +01:00
|
|
|
len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
|
|
|
|
lua_pushlstring(L, s, len);
|
2008-10-19 19:14:55 +02:00
|
|
|
return 1;
|
2008-07-02 11:04:17 +02:00
|
|
|
}
|
2009-04-03 13:09:17 +02:00
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|