Merge pull request #1469 from psychon/refactor-initialisation

Refactor Lua initialisation a bit
This commit is contained in:
Daniel Hahler 2017-01-26 11:13:27 +01:00 committed by GitHub
commit da69dd0864
5 changed files with 78 additions and 51 deletions

View File

@ -433,6 +433,12 @@ restart_on_signal(gpointer data)
return TRUE;
}
static bool
true_config_callback(const char *unused)
{
return true;
}
/** Print help and exit(2) with given exit_code.
* \param exit_code The exit code.
*/
@ -545,13 +551,37 @@ main(int argc, char **argv)
/* Get XDG basedir data */
xdgInitHandle(&xdg);
/* init lua */
luaA_init(&xdg, &searchpath);
string_array_wipe(&searchpath);
/* add XDG_CONFIG_DIR as include path */
const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(&xdg);
for(; *xdgconfigdirs; xdgconfigdirs++)
{
/* Append /awesome to *xdgconfigdirs */
const char *suffix = "/awesome";
size_t len = a_strlen(*xdgconfigdirs) + a_strlen(suffix) + 1;
char *entry = p_new(char, len);
a_strcat(entry, len, *xdgconfigdirs);
a_strcat(entry, len, suffix);
string_array_append(&searchpath, entry);
}
if (run_test)
{
if(!luaA_parserc(&xdg, confpath, false))
bool success = true;
/* Get the first config that will be tried */
const char *config = luaA_find_config(&xdg, confpath, true_config_callback);
/* Try to parse it */
lua_State *L = luaL_newstate();
if(luaL_loadfile(L, config))
{
const char *err = lua_tostring(L, -1);
fprintf(stderr, "%s\n", err);
success = false;
}
p_delete(&config);
lua_close(L);
if(!success)
{
fprintf(stderr, "✘ Configuration file syntax error.\n");
return EXIT_FAILURE;
@ -670,10 +700,6 @@ main(int argc, char **argv)
/* init atom cache */
atoms_init(globalconf.connection);
/* init screens information */
screen_scan();
/* do this only for real screen */
ewmh_init();
systray_init();
@ -724,8 +750,17 @@ main(int argc, char **argv)
/* get the current wallpaper, from now on we are informed when it changes */
root_update_wallpaper();
/* init lua */
luaA_init(&xdg, &searchpath);
string_array_wipe(&searchpath);
ewmh_init_lua();
/* init screens information */
screen_scan();
/* Parse and run configuration file */
if (!luaA_parserc(&xdg, confpath, true))
if (!luaA_parserc(&xdg, confpath))
fatal("couldn't find any rc file");
p_delete(&confpath);

6
ewmh.c
View File

@ -125,7 +125,6 @@ ewmh_client_update_frame_extents(lua_State *L)
void
ewmh_init(void)
{
lua_State *L = globalconf_get_lua_State();
xcb_window_t father;
xcb_screen_t *xscreen = globalconf.screen;
xcb_atom_t atom[] =
@ -206,7 +205,12 @@ ewmh_init(void)
i = getpid();
xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE,
father, _NET_WM_PID, XCB_ATOM_CARDINAL, 32, 1, &i);
}
void
ewmh_init_lua(void)
{
lua_State *L = globalconf_get_lua_State();
luaA_class_connect_signal(L, &client_class, "focus", ewmh_update_net_active_window);
luaA_class_connect_signal(L, &client_class, "unfocus", ewmh_update_net_active_window);

1
ewmh.h
View File

@ -30,6 +30,7 @@
typedef struct client_t client_t;
void ewmh_init(void);
void ewmh_init_lua(void);
void ewmh_update_net_numbers_of_desktop(void);
int ewmh_update_net_current_desktop(lua_State *);
void ewmh_update_net_desktop_names(void);

64
luaa.c
View File

@ -777,19 +777,6 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath)
/* Export keys */
key_class_setup(L);
/* add XDG_CONFIG_DIR as include path */
const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
for(; *xdgconfigdirs; xdgconfigdirs++)
{
/* Append /awesome to *xdgconfigdirs */
const char *suffix = "/awesome";
size_t len = a_strlen(*xdgconfigdirs) + a_strlen(suffix) + 1;
char *entry = p_new(char, len);
a_strcat(entry, len, *xdgconfigdirs);
a_strcat(entry, len, suffix);
string_array_append(searchpath, entry);
}
/* add Lua search paths */
lua_getglobal(L, "package");
if (LUA_TTABLE != lua_type(L, 1))
@ -817,7 +804,7 @@ luaA_startup_error(const char *err)
}
static bool
luaA_loadrc(const char *confpath, bool run)
luaA_loadrc(const char *confpath)
{
lua_State *L = globalconf_get_lua_State();
if(luaL_loadfile(L, confpath))
@ -829,12 +816,6 @@ luaA_loadrc(const char *confpath, bool run)
return false;
}
if(!run)
{
lua_pop(L, 1);
return true;
}
/* Set the conffile right now so it can be used inside the
* configuration file. */
conffile = a_strdup(confpath);
@ -865,21 +846,28 @@ luaA_loadrc(const char *confpath, bool run)
* \param run Run the configuration file.
*/
bool
luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
luaA_parserc(xdgHandle* xdg, const char *confpatharg)
{
const char *confpath = luaA_find_config(xdg, confpatharg, luaA_loadrc);
bool ret = confpath != NULL;
p_delete(&confpath);
return ret;
}
/** Find a config file for which the given callback returns true.
* \param xdg An xdg handle to use to get XDG basedir.
* \param confpatharg The configuration file to load.
* \param callback The callback to call.
*/
const char *
luaA_find_config(xdgHandle* xdg, const char *confpatharg, luaA_config_callback *callback)
{
char *confpath = NULL;
bool ret = false;
/* try to load, return if it's ok */
if(confpatharg)
if(confpatharg && callback(confpatharg))
{
if(luaA_loadrc(confpatharg, run))
{
ret = true;
goto bailout;
}
else if(!run)
goto bailout;
return a_strdup(confpatharg);
}
confpath = xdgConfigFind("awesome/rc.lua", xdg);
@ -889,21 +877,17 @@ luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run)
/* confpath is "string1\0string2\0string3\0\0" */
while(*tmp)
{
if(luaA_loadrc(tmp, run))
if(callback(tmp))
{
ret = true;
goto bailout;
const char *ret = a_strdup(tmp);
p_delete(&confpath);
return ret;
}
else if(!run)
goto bailout;
tmp += a_strlen(tmp) + 1;
}
bailout:
p_delete(&confpath);
return ret;
return NULL;
}
int

5
luaa.h
View File

@ -303,8 +303,11 @@ luaA_registerfct(lua_State *L, int idx, int *fct)
return luaA_register(L, idx, fct);
}
typedef bool luaA_config_callback(const char *);
void luaA_init(xdgHandle *, string_array_t *);
bool luaA_parserc(xdgHandle *, const char *, bool);
const char *luaA_find_config(xdgHandle *, const char *, luaA_config_callback *);
bool luaA_parserc(xdgHandle *, const char *);
/** Global signals */
signal_array_t global_signals;