2008-05-20 15:39:47 +02:00
|
|
|
/*
|
|
|
|
* lua.h - Lua configuration management header
|
|
|
|
*
|
|
|
|
* Copyright © 2008 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AWESOME_LUA_H
|
|
|
|
#define AWESOME_LUA_H
|
|
|
|
|
2008-06-17 00:30:53 +02:00
|
|
|
#include <ev.h>
|
2008-05-20 19:02:40 +02:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
2008-05-20 15:39:47 +02:00
|
|
|
|
|
|
|
#include "common/util.h"
|
|
|
|
|
2008-06-04 19:21:21 +02:00
|
|
|
/** Object types */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
AWESOME_TYPE_STATUSBAR = 1,
|
|
|
|
AWESOME_TYPE_TITLEBAR
|
|
|
|
} awesome_type_t;
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
/** Type for Lua function */
|
|
|
|
typedef int luaA_function;
|
|
|
|
|
2008-06-16 11:31:38 +02:00
|
|
|
#define DO_LUA_GC(type, prefix, lua_type, type_unref) \
|
|
|
|
static int \
|
|
|
|
luaA_##prefix##_gc(lua_State *L) \
|
|
|
|
{ \
|
|
|
|
type **p = luaA_checkudata(L, 1, lua_type); \
|
|
|
|
type_unref(p); \
|
|
|
|
*p = NULL; \
|
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
|
2008-06-16 11:37:55 +02:00
|
|
|
#define DO_LUA_EQ(type, prefix, lua_type) \
|
|
|
|
static int \
|
|
|
|
luaA_##prefix##_eq(lua_State *L) \
|
|
|
|
{ \
|
|
|
|
type **p1 = luaA_checkudata(L, 1, lua_type); \
|
|
|
|
type **p2 = luaA_checkudata(L, 2, lua_type); \
|
|
|
|
lua_pushboolean(L, (*p1 == *p2)); \
|
|
|
|
return 1; \
|
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
#define luaA_dostring(L, cmd) \
|
|
|
|
do { \
|
|
|
|
if(cmd) \
|
|
|
|
if(luaL_dostring(L, cmd)) \
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("error executing Lua code: %s", \
|
2008-05-20 15:39:47 +02:00
|
|
|
lua_tostring(L, -1)); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define luaA_dofunction(L, f, n) \
|
|
|
|
do { \
|
|
|
|
if(f) \
|
|
|
|
{ \
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, f); \
|
|
|
|
if(n) \
|
|
|
|
lua_insert(L, - (n + 1)); \
|
|
|
|
if(lua_pcall(L, n, 0, 0)) \
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("error running function: %s", \
|
2008-05-20 15:39:47 +02:00
|
|
|
lua_tostring(L, -1)); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define luaA_checktable(L, n) \
|
|
|
|
do { \
|
|
|
|
if(!lua_istable(L, n)) \
|
|
|
|
luaL_typerror(L, n, "table"); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define luaA_checkfunction(L, n) \
|
|
|
|
do { \
|
|
|
|
if(!lua_isfunction(L, n)) \
|
|
|
|
luaL_typerror(L, n, "function"); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define luaA_checkscreen(screen) \
|
|
|
|
do { \
|
|
|
|
if(screen < 0 || screen >= globalconf.screens_info->nscreen) \
|
|
|
|
luaL_error(L, "invalid screen number: %d\n", screen); \
|
|
|
|
} while(0)
|
|
|
|
|
2008-06-04 18:27:10 +02:00
|
|
|
/** Check that an object is not a NULL reference.
|
|
|
|
* \param L The Lua state.
|
|
|
|
* \param ud The index of the object in the stack.
|
|
|
|
* \param tname The type name.
|
|
|
|
* \return A pointer to the object.
|
|
|
|
*/
|
|
|
|
static inline void *
|
|
|
|
luaA_checkudata(lua_State *L, int ud, const char *tname)
|
|
|
|
{
|
|
|
|
void **p = luaL_checkudata(L, ud, tname);
|
|
|
|
if(*p)
|
|
|
|
return p;
|
|
|
|
luaL_error(L, "invalid object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-12 13:27:45 +02:00
|
|
|
static inline bool
|
|
|
|
luaA_checkboolean(lua_State *L, int n)
|
|
|
|
{
|
|
|
|
if(!lua_isboolean(L, n))
|
|
|
|
luaL_typerror(L, n, "boolean");
|
|
|
|
return lua_toboolean(L, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
luaA_optboolean(lua_State *L, int idx, bool def)
|
|
|
|
{
|
|
|
|
return luaL_opt(L, luaA_checkboolean, idx, def);
|
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
static inline lua_Number
|
|
|
|
luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
|
|
|
|
{
|
|
|
|
/* assume that table is first on stack */
|
|
|
|
lua_getfield(L, idx, name);
|
|
|
|
/* return luaL_optnumber result */
|
|
|
|
return luaL_optnumber(L, -1, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *
|
|
|
|
luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
|
|
|
|
{
|
|
|
|
/* assume that table is first on stack */
|
|
|
|
lua_getfield(L, idx, name);
|
|
|
|
/* return luaL_optnumber result */
|
|
|
|
return luaL_optstring(L, -1, def);
|
|
|
|
}
|
|
|
|
|
2008-06-12 13:27:45 +02:00
|
|
|
static inline bool
|
|
|
|
luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
|
|
|
|
{
|
|
|
|
/* assume that table is first on stack */
|
|
|
|
lua_getfield(L, idx, name);
|
|
|
|
/* return luaL_optnumber result */
|
|
|
|
return luaA_optboolean(L, -1, def);
|
|
|
|
}
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
static inline int
|
|
|
|
luaA_settype(lua_State *L, const char *type)
|
|
|
|
{
|
|
|
|
luaL_getmetatable(L, type);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *
|
|
|
|
luaA_name_init(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_getfield(L, 1, "name");
|
|
|
|
return a_strdup(luaL_checkstring(L, -1));
|
|
|
|
}
|
|
|
|
|
2008-06-08 21:49:15 +02:00
|
|
|
void luaA_init(void);
|
2008-05-20 15:39:47 +02:00
|
|
|
bool luaA_parserc(const char *);
|
2008-06-04 19:21:21 +02:00
|
|
|
void luaA_pushpointer(void *, awesome_type_t);
|
2008-06-16 23:47:14 +02:00
|
|
|
void luaA_cs_init(void);
|
|
|
|
void luaA_cs_cleanup(void);
|
2008-06-17 00:30:53 +02:00
|
|
|
void luaA_on_timer(EV_P_ ev_timer *w, int revents);
|
2008-06-04 19:21:21 +02:00
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
#endif
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|