2008-05-20 15:39:47 +02:00
/*
2009-04-03 13:09:17 +02:00
* luaa . h - Lua configuration management header
2008-05-20 15:39:47 +02:00
*
2009-04-03 13:09:17 +02:00
* Copyright © 2008 - 2009 Julien Danjou < julien @ danjou . info >
2008-05-20 15:39:47 +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 .
*
*/
# ifndef AWESOME_LUA_H
# define AWESOME_LUA_H
2008-05-20 19:02:40 +02:00
# include <lua.h>
# include <lauxlib.h>
2008-05-20 15:39:47 +02:00
2009-04-08 15:47:31 +02:00
# include <basedir.h>
2008-09-11 14:11:13 +02:00
# include "draw.h"
2009-08-17 16:38:56 +02:00
# include "common/lualib.h"
2010-09-29 19:36:58 +02:00
# include "common/luaclass.h"
2008-05-20 15:39:47 +02:00
2022-01-22 16:09:10 +01:00
# if !(501 <= LUA_VERSION_NUM && LUA_VERSION_NUM < 505)
# error "Awesome only supports Lua versions 5.1-5.4 and LuaJIT2, please refer to https: //awesomewm.org/apidoc/documentation/10-building-and-testing.md.html#Building"
2021-04-13 22:49:50 +02:00
# endif
2008-11-14 13:44:47 +01:00
# define luaA_deprecate(L, repl) \
2009-08-27 18:05:44 +02:00
do { \
luaA_warn ( L , " %s: This function is deprecated and will be removed, see %s " , \
__FUNCTION__ , repl ) ; \
lua_pushlstring ( L , __FUNCTION__ , sizeof ( __FUNCTION__ ) ) ; \
signal_object_emit ( L , & global_signals , " debug::deprecation " , 1 ) ; \
} while ( 0 )
2008-08-12 12:08:20 +02:00
2016-07-24 14:57:14 +02:00
static inline void free_string ( char * * c )
{
p_delete ( c ) ;
}
DO_ARRAY ( char * , string , free_string )
2012-06-07 08:46:55 +02:00
/** Print a warning about some Lua code.
* This is less mean than luaL_error ( ) which setjmp via lua_error ( ) and kills
* everything . This only warn , it ' s up to you to then do what ' s should be done .
* \ param L The Lua VM state .
* \ param fmt The warning message .
*/
static inline void __attribute__ ( ( format ( printf , 2 , 3 ) ) )
luaA_warn ( lua_State * L , const char * fmt , . . . )
{
va_list ap ;
luaL_where ( L , 1 ) ;
2015-12-22 19:28:05 +01:00
fprintf ( stderr , " %s%sW: " , a_current_time_str ( ) , lua_tostring ( L , - 1 ) ) ;
2012-06-07 08:46:55 +02:00
lua_pop ( L , 1 ) ;
va_start ( ap , fmt ) ;
vfprintf ( stderr , fmt , ap ) ;
va_end ( ap ) ;
fprintf ( stderr , " \n " ) ;
2015-07-16 15:25:39 +02:00
2015-07-28 04:06:10 +02:00
# if LUA_VERSION_NUM >= 502
2015-07-16 15:25:39 +02:00
luaL_traceback ( L , L , NULL , 2 ) ;
fprintf ( stderr , " %s \n " , lua_tostring ( L , - 1 ) ) ;
2017-11-08 18:47:37 +01:00
lua_pop ( L , 1 ) ;
2015-07-28 04:06:10 +02:00
# endif
2012-06-07 08:46:55 +02:00
}
static inline int
luaA_typerror ( lua_State * L , int narg , const char * tname )
{
const char * msg = lua_pushfstring ( L , " %s expected, got %s " ,
tname , luaL_typename ( L , narg ) ) ;
2015-07-28 04:06:10 +02:00
# if LUA_VERSION_NUM >= 502
2015-07-16 15:25:39 +02:00
luaL_traceback ( L , L , NULL , 2 ) ;
lua_concat ( L , 2 ) ;
2015-07-28 04:06:10 +02:00
# endif
2012-06-07 08:46:55 +02:00
return luaL_argerror ( L , narg , msg ) ;
}
2016-04-17 13:37:15 +02:00
static inline int
luaA_rangerror ( lua_State * L , int narg , double min , double max )
{
const char * msg = lua_pushfstring ( L , " value in [%f, %f] expected, got %f " ,
min , max , ( double ) lua_tonumber ( L , narg ) ) ;
# if LUA_VERSION_NUM >= 502
luaL_traceback ( L , L , NULL , 2 ) ;
lua_concat ( L , 2 ) ;
# endif
return luaL_argerror ( L , narg , msg ) ;
}
2012-06-07 09:15:48 +02:00
static inline void
luaA_getuservalue ( lua_State * L , int idx )
{
# if LUA_VERSION_NUM >= 502
lua_getuservalue ( L , idx ) ;
# else
lua_getfenv ( L , idx ) ;
# endif
2015-03-29 17:50:11 +02:00
}
2012-06-07 09:15:48 +02:00
static inline void
luaA_setuservalue ( lua_State * L , int idx )
{
# if LUA_VERSION_NUM >= 502
lua_setuservalue ( L , idx ) ;
# else
lua_setfenv ( L , idx ) ;
# endif
2015-03-29 17:50:11 +02:00
}
2012-06-07 09:15:48 +02:00
2012-06-07 09:27:10 +02:00
static inline size_t
luaA_rawlen ( lua_State * L , int idx )
{
# if LUA_VERSION_NUM >= 502
return lua_rawlen ( L , idx ) ;
# else
return lua_objlen ( L , idx ) ;
# endif
}
2012-06-12 13:32:40 +02:00
static inline void
luaA_registerlib ( lua_State * L , const char * libname , const luaL_Reg * l )
{
2019-02-16 14:25:20 +01:00
assert ( libname ) ;
2012-06-12 13:32:40 +02:00
# if LUA_VERSION_NUM >= 502
2019-02-16 14:25:20 +01:00
lua_newtable ( L ) ;
luaL_setfuncs ( L , l , 0 ) ;
lua_pushvalue ( L , - 1 ) ;
lua_setglobal ( L , libname ) ;
2012-06-12 13:32:40 +02:00
# else
luaL_register ( L , libname , l ) ;
# endif
}
2019-02-16 14:25:20 +01:00
static inline void
luaA_setfuncs ( lua_State * L , const luaL_Reg * l )
{
# if LUA_VERSION_NUM >= 502
luaL_setfuncs ( L , l , 0 ) ;
# else
luaL_register ( L , NULL , l ) ;
# endif
}
2008-06-12 13:27:45 +02:00
static inline bool
luaA_checkboolean ( lua_State * L , int n )
{
if ( ! lua_isboolean ( L , n ) )
2012-06-07 08:46:55 +02:00
luaA_typerror ( L , n , " boolean " ) ;
2008-06-12 13:27:45 +02:00
return lua_toboolean ( L , n ) ;
}
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 )
{
lua_getfield ( L , idx , name ) ;
2009-04-06 09:47:44 +02:00
if ( lua_isnil ( L , - 1 ) | | lua_isnumber ( L , - 1 ) )
def = luaL_optnumber ( L , - 1 , def ) ;
2009-04-09 12:47:19 +02:00
lua_pop ( L , 1 ) ;
2009-04-06 09:47:44 +02:00
return def ;
2008-05-20 15:39:47 +02:00
}
2016-04-18 06:55:22 +02:00
static inline lua_Number
luaA_checknumber_range ( lua_State * L , int n , lua_Number min , lua_Number max )
{
lua_Number result = lua_tonumber ( L , n ) ;
if ( result < min | | result > max )
luaA_rangerror ( L , n , min , max ) ;
return result ;
}
static inline lua_Number
luaA_optnumber_range ( lua_State * L , int narg , lua_Number def , lua_Number min , lua_Number max )
{
if ( lua_isnoneornil ( L , narg ) )
return def ;
return luaA_checknumber_range ( L , narg , min , max ) ;
}
static inline lua_Number
luaA_getopt_number_range ( lua_State * L , int idx , const char * name , lua_Number def , lua_Number min , lua_Number max )
{
lua_getfield ( L , idx , name ) ;
if ( lua_isnil ( L , - 1 ) | | lua_isnumber ( L , - 1 ) )
def = luaA_optnumber_range ( L , - 1 , def , min , max ) ;
lua_pop ( L , 1 ) ;
return def ;
}
2015-07-16 15:33:20 +02:00
static inline int
luaA_checkinteger ( lua_State * L , int n )
{
2016-04-18 06:55:22 +02:00
lua_Number d = lua_tonumber ( L , n ) ;
2015-07-16 15:33:20 +02:00
if ( d ! = ( int ) d )
luaA_typerror ( L , n , " integer " ) ;
return d ;
}
static inline lua_Integer
luaA_optinteger ( lua_State * L , int narg , lua_Integer def )
{
return luaL_opt ( L , luaA_checkinteger , narg , def ) ;
}
static inline int
luaA_getopt_integer ( lua_State * L , int idx , const char * name , lua_Integer def )
{
lua_getfield ( L , idx , name ) ;
if ( lua_isnil ( L , - 1 ) | | lua_isnumber ( L , - 1 ) )
def = luaA_optinteger ( L , - 1 , def ) ;
lua_pop ( L , 1 ) ;
return def ;
}
2016-04-17 13:37:15 +02:00
static inline int
2016-04-26 00:51:51 +02:00
luaA_checkinteger_range ( lua_State * L , int n , lua_Number min , lua_Number max )
2016-04-17 13:37:15 +02:00
{
int result = luaA_checkinteger ( L , n ) ;
if ( result < min | | result > max )
luaA_rangerror ( L , n , min , max ) ;
return result ;
}
static inline lua_Integer
2016-04-26 00:51:51 +02:00
luaA_optinteger_range ( lua_State * L , int narg , lua_Integer def , lua_Number min , lua_Number max )
2016-04-17 13:37:15 +02:00
{
if ( lua_isnoneornil ( L , narg ) )
return def ;
return luaA_checkinteger_range ( L , narg , min , max ) ;
}
static inline int
2016-04-26 00:51:51 +02:00
luaA_getopt_integer_range ( lua_State * L , int idx , const char * name , lua_Integer def , lua_Number min , lua_Number max )
2016-04-17 13:37:15 +02:00
{
lua_getfield ( L , idx , name ) ;
if ( lua_isnil ( L , - 1 ) | | lua_isnumber ( L , - 1 ) )
def = luaA_optinteger_range ( L , - 1 , def , min , max ) ;
lua_pop ( L , 1 ) ;
return def ;
}
2008-08-20 12:00:22 +02:00
/** Push a area type to a table on stack.
* \ param L The Lua VM state .
* \ param geometry The area geometry to push .
* \ return The number of elements pushed on stack .
*/
static inline int
luaA_pusharea ( lua_State * L , area_t geometry )
{
2009-04-25 15:04:27 +02:00
lua_createtable ( L , 0 , 4 ) ;
2016-02-06 13:46:46 +01:00
lua_pushinteger ( L , geometry . x ) ;
2008-08-20 12:00:22 +02:00
lua_setfield ( L , - 2 , " x " ) ;
2016-02-06 13:46:46 +01:00
lua_pushinteger ( L , geometry . y ) ;
2008-08-20 12:00:22 +02:00
lua_setfield ( L , - 2 , " y " ) ;
2016-02-06 13:46:46 +01:00
lua_pushinteger ( L , geometry . width ) ;
2008-08-20 12:00:22 +02:00
lua_setfield ( L , - 2 , " width " ) ;
2016-02-06 13:46:46 +01:00
lua_pushinteger ( L , geometry . height ) ;
2008-08-20 12:00:22 +02:00
lua_setfield ( L , - 2 , " height " ) ;
return 1 ;
}
2008-10-20 11:43:58 +02:00
/** Register an Lua object.
* \ param L The Lua stack .
* \ param idx Index of the object in the stack .
2009-07-28 10:29:30 +02:00
* \ param ref A int address : it will be filled with the int
2009-08-30 07:26:19 +02:00
* registered . If the address points to an already registered object , it will
2008-10-20 11:43:58 +02:00
* be unregistered .
* \ return Always 0.
*/
static inline int
2009-07-28 10:29:30 +02:00
luaA_register ( lua_State * L , int idx , int * ref )
2008-10-20 11:43:58 +02:00
{
lua_pushvalue ( L , idx ) ;
if ( * ref ! = LUA_REFNIL )
luaL_unref ( L , LUA_REGISTRYINDEX , * ref ) ;
* ref = luaL_ref ( L , LUA_REGISTRYINDEX ) ;
return 0 ;
}
2008-11-10 17:26:24 +01:00
/** Unregister a Lua object.
* \ param L The Lua stack .
* \ param ref A reference to an Lua object .
*/
static inline void
2009-07-28 10:29:30 +02:00
luaA_unregister ( lua_State * L , int * ref )
2008-11-10 17:26:24 +01:00
{
luaL_unref ( L , LUA_REGISTRYINDEX , * ref ) ;
* ref = LUA_REFNIL ;
}
2008-07-29 10:43:11 +02:00
/** Register a function.
* \ param L The Lua stack .
2008-08-12 13:23:10 +02:00
* \ param idx Index of the function in the stack .
2009-07-28 10:29:30 +02:00
* \ param fct A int address : it will be filled with the int
2009-08-30 07:26:19 +02:00
* registered . If the address points to an already registered function , it will
2008-07-29 10:43:11 +02:00
* be unregistered .
2008-10-20 11:43:58 +02:00
* \ return luaA_register value .
2008-07-29 10:43:11 +02:00
*/
2008-07-09 12:12:52 +02:00
static inline int
2009-07-28 10:29:30 +02:00
luaA_registerfct ( lua_State * L , int idx , int * fct )
2008-07-09 12:12:52 +02:00
{
2008-08-12 13:23:10 +02:00
luaA_checkfunction ( L , idx ) ;
2008-10-20 11:43:58 +02:00
return luaA_register ( L , idx , fct ) ;
2008-07-09 12:12:52 +02:00
}
2017-01-25 14:06:41 +01:00
typedef bool luaA_config_callback ( const char * ) ;
2016-07-24 14:57:14 +02:00
void luaA_init ( xdgHandle * , string_array_t * ) ;
2017-01-25 14:06:41 +01:00
const char * luaA_find_config ( xdgHandle * , const char * , luaA_config_callback * ) ;
bool luaA_parserc ( xdgHandle * , const char * ) ;
2008-06-04 19:21:21 +02:00
2009-07-31 16:28:17 +02:00
/** Global signals */
2020-04-17 19:25:40 +02:00
extern signal_array_t global_signals ;
2009-07-31 16:28:17 +02:00
2009-08-20 17:37:46 +02:00
int luaA_class_index_miss_property ( lua_State * , lua_object_t * ) ;
int luaA_class_newindex_miss_property ( lua_State * , lua_object_t * ) ;
2014-03-23 19:09:11 +01:00
int luaA_default_index ( lua_State * ) ;
int luaA_default_newindex ( lua_State * ) ;
2015-02-14 15:41:11 +01:00
void luaA_emit_startup ( void ) ;
2009-08-20 17:37:46 +02:00
2010-10-06 15:38:44 +02:00
void luaA_systray_invalidate ( void ) ;
2008-05-20 15:39:47 +02:00
# endif
2011-09-11 16:50:01 +02:00
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80