read settings

This commit is contained in:
baskerville 2012-07-30 14:54:20 +02:00
parent ab172d61a3
commit be63805c22
11 changed files with 111 additions and 12 deletions

View file

@ -1,2 +1,2 @@
all:
gcc -pedantic -Wall -lxcb -o bspwm main.c utils.c
gcc -pedantic -Wall -llua -lxcb -o bspwm main.c utils.c settings.c luautils.c

View file

@ -4,6 +4,8 @@ set = {
inner_border_color = "#222222",
border_width = 3,
inner_border_width = 2,
window_gap = 2,
smart_window_surroundings = true
}
# vim: set ft=lua:
-- vim: set ft=lua:

52
luautils.c Normal file
View file

@ -0,0 +1,52 @@
#include "utils.h"
#include "luautils.h"
char *lua_stringexpr(lua_State* L, char* expr, char* def)
{
char buf[EXPR_BUF_SIZE];
char *result = def;
sprintf(buf, "TMP=%s", expr);
if (!luaL_dostring(L, buf)) {
lua_getglobal(L, "TMP");
if (lua_isstring(L, -1)) {
result = (char *) lua_tostring(L, -1);
}
lua_pop(L, 1);
}
return result;
}
double lua_doubleexpr(lua_State* L, char* expr, double def)
{
char buf[EXPR_BUF_SIZE];
double result = def;
sprintf(buf, "TMP=%s", expr);
if (!luaL_dostring(L, buf)) {
lua_getglobal(L, "TMP");
if (lua_isnumber(L, -1)) {
result = lua_tonumber(L, -1);
}
lua_pop(L, 1);
}
return result;
}
int lua_intexpr(lua_State* L, char* expr, int def)
{
return (int) lua_doubleexpr(L, expr, (double) def);
}
bool lua_boolexpr(lua_State* L, char* expr, bool def)
{
char buf[EXPR_BUF_SIZE];
bool result = def;
sprintf(buf, "TMP=%s", expr);
if (!luaL_dostring(L, buf)) {
lua_getglobal(L, "TMP");
if (lua_isboolean(L, -1)) {
result = (bool) lua_toboolean(L, -1);
}
lua_pop(L, 1);
}
return result;
}

10
luautils.h Normal file
View file

@ -0,0 +1,10 @@
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#define EXPR_BUF_SIZE 256
char* lua_stringexpr(lua_State*, char*, char*);
double lua_doubleexpr(lua_State*, char*, double);
int lua_intexpr(lua_State*, char*, int);
bool lua_boolexpr(lua_State*, char*, bool);

10
main.c
View file

@ -1,18 +1,20 @@
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include "utils.h"
#include "main.h"
#include "types.h"
#include "utils.h"
#include "settings.h"
int main(void)
{
fd_set descriptors;
int fifo, xfd, nbr;
char message[MSG_BUF_SIZE];
char message[READ_BUF_SIZE];
struct timeval timeout;
xcb_generic_event_t *event;
@ -26,11 +28,15 @@ int main(void)
/* O_RDWR is needed, see http://bit.ly/T0C5Mh */
fifo = open(INPUT_FIFO, O_RDWR | O_NONBLOCK);
if (fifo == -1)
die("error: could not open fifo\n");
FD_ZERO(&descriptors);
FD_SET(xfd, &descriptors);
FD_SET(fifo, &descriptors);
load_settings();
while (true) {
if (select(fifo + 1, &descriptors, 0, 0, &timeout)) {

2
main.h
View file

@ -1,6 +1,6 @@
#define INPUT_FIFO "/tmp/bspwm-input"
#define SELECT_TIMEOUT 300
#define MSG_BUF_SIZE 256
#define READ_BUF_SIZE 256
xcb_connection_t *dpy;
int default_screen;

View file

@ -1,8 +1,8 @@
call {
call = {
name = "set_split_ratio",
params = {
split_ratio = 0.333
}
}
# vim: set ft=lua:
-- vim: set ft=lua:

View file

@ -0,0 +1,24 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "settings.h"
#include "utils.h"
#include "luautils.h"
void load_settings(void)
{
lua_State *ls = lua_open();
luaopen_base(ls);
if (luaL_loadfile(ls, CONFIG_FILE) == 0) {
if (lua_pcall(ls, 0, 0, 0) == 0) {
normal_border_color = lua_stringexpr(ls, "set.normal_border_color", NORMAL_BORDER_COLOR);
} else {
die("error: cannot interpret configuration file\n");
}
} else {
die("error: could not load configuration file\n");
}
lua_close(ls);
}

View file

@ -1,6 +1,11 @@
#define CONFIG_FILE "bspwmrc"
#define BORDER_WIDTH 1
#define INNER_BORDER_WIDTH 2
#define NORMAL_BORDER_COLOR "#333333"
#define ACTIVE_BORDER_COLOR "#DDDDDD"
#define INNER_BORDER_COLOR "#111111"
char *normal_border_color;
void load_settings(void);

View file

@ -1,8 +1,3 @@
typedef enum {
false,
true
} bool;
typedef enum {
LAYOUT_TILED,
LAYOUT_MAX

View file

@ -1,4 +1,9 @@
#define LENGTH(x) (sizeof(x) / sizeof(*x))
/* #define EXIT_FAILURE -1 */
typedef enum {
false,
true
} bool;
void die(const char *errstr, ...);