/
config.c
423 lines (337 loc) · 12.9 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
* For license, see COPYING.
*/
#include "config.h"
#include "wmfs.h"
#include "parse.h"
#include "tag.h"
#include "screen.h"
#include "infobar.h"
#include "util.h"
#include "status.h"
#define ISTRDUP(t, s) \
do { \
if((tmp = s)) \
t = xstrdup(tmp); \
} while(/* CONSTCOND */ 0);
static void
config_mouse_section(struct mbhead *mousebinds, struct conf_sec **sec)
{
struct mousebind *m;
int i = 0;
char *p;
SLIST_INIT(mousebinds);
for(; sec[i]; ++i)
{
m = xcalloc(1, sizeof(struct mousebind));
m->button = fetch_opt_first(sec[i], "1", "button").num;
m->func = uicb_name_func(fetch_opt_first(sec[i], "", "func").str);
if((p = fetch_opt_first(sec[i], "", "cmd").str))
m->cmd = xstrdup(p);
m->use_area = false;
SLIST_INSERT_HEAD(mousebinds, m, next);
SLIST_INSERT_HEAD(&W->h.mousebind, m, globnext);
}
}
static void
config_theme(void)
{
struct theme *t, *p = NULL;
size_t i, n;
struct conf_sec *sec, **ks;
char *tmp;
/* [themes] */
sec = fetch_section_first(NULL, "themes");
ks = fetch_section(sec, "theme");
/* No theme section? Make one with default value anyway. */
if(!(n = fetch_section_count(ks)))
++n;
SLIST_INIT(&W->h.theme);
/* [theme]*/
for(i = 0; i < n; ++i)
{
t = (struct theme*)xcalloc(1, sizeof(struct theme));
t->name = fetch_opt_first(ks[i], "default", "name").str;
wmfs_init_font(fetch_opt_first(ks[i], "fixed", "font").str, t);
/* bars */
t->bars.fg = fgcolor_atoh(fetch_opt_first(ks[i], "#CCCCCC", "bars_fg").str);
t->bars.bg = bgcolor_atoh(fetch_opt_first(ks[i], "#222222", "bars_bg").str);
t->bars_width = fetch_opt_first(ks[i], "12", "bars_width").num;
/*
* Elements
*/
t->tags_n.fg = fgcolor_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_normal_fg").str);
t->tags_n.bg = bgcolor_atoh(fetch_opt_first(ks[i], "#222222", "tags_normal_bg").str);
t->tags_s.fg = fgcolor_atoh(fetch_opt_first(ks[i], "#222222", "tags_sel_fg").str);
t->tags_s.bg = bgcolor_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_sel_bg").str);
t->tags_o.fg = fgcolor_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_occupied_fg").str);
t->tags_o.bg = bgcolor_atoh(fetch_opt_first(ks[i], "#444444", "tags_occupied_bg").str);
t->tags_u.fg = fgcolor_atoh(fetch_opt_first(ks[i], "#444444", "tags_urgent_fg").str);
t->tags_u.bg = bgcolor_atoh(fetch_opt_first(ks[i], "#CC4444", "tags_urgent_bg").str);
t->tags_border_col = bgcolor_atoh(fetch_opt_first(ks[i], "#888888", "tags_border_color").str);
t->tags_border_width = fetch_opt_first(ks[i], "0", "tags_border_width").num;
/* status line */
t->tags_n_sl = status_new_ctx(NULL, t);
t->tags_s_sl = status_new_ctx(NULL, t);
t->tags_o_sl = status_new_ctx(NULL, t);
t->tags_u_sl = status_new_ctx(NULL, t);
ISTRDUP(t->tags_n_sl.status, fetch_opt_first(ks[i], "", "tags_normal_statusline").str);
ISTRDUP(t->tags_s_sl.status, fetch_opt_first(ks[i], "", "tags_sel_statusline").str);
ISTRDUP(t->tags_o_sl.status, fetch_opt_first(ks[i], "", "tags_occupied_statusline").str);
ISTRDUP(t->tags_u_sl.status, fetch_opt_first(ks[i], "", "tags_urgent_statusline").str);
if(t->tags_n_sl.status)
status_parse(&t->tags_n_sl);
if(t->tags_s_sl.status)
status_parse(&t->tags_s_sl);
if(t->tags_o_sl.status)
status_parse(&t->tags_o_sl);
if(t->tags_u_sl.status)
status_parse(&t->tags_u_sl);
/* Client / frame */
t->client_n.fg = fgcolor_atoh(fetch_opt_first(ks[i], "#CCCCCC", "client_normal_fg").str);
t->client_n.bg = bgcolor_atoh(fetch_opt_first(ks[i], "#222222", "client_normal_bg").str);
t->client_s.fg = fgcolor_atoh(fetch_opt_first(ks[i], "#222222", "client_sel_fg").str);
t->client_s.bg = bgcolor_atoh(fetch_opt_first(ks[i], "#CCCCCC", "client_sel_bg").str);
t->frame_bg = bgcolor_atoh(fetch_opt_first(ks[i], "#555555", "frame_bg").str);
t->client_titlebar_width = fetch_opt_first(ks[i], "12", "client_titlebar_width").num;
t->client_border_width = fetch_opt_first(ks[i], "1", "client_border_width").num;
/* status line */
t->client_n_sl = status_new_ctx(NULL, t);
t->client_s_sl = status_new_ctx(NULL, t);
t->client_f_sl = status_new_ctx(NULL, t);
ISTRDUP(t->client_n_sl.status, fetch_opt_first(ks[i], "", "client_normal_statusline").str);
ISTRDUP(t->client_s_sl.status, fetch_opt_first(ks[i], "", "client_sel_statusline").str);
ISTRDUP(t->client_f_sl.status, fetch_opt_first(ks[i], "", "client_free_statusline").str);
if(t->client_n_sl.status)
status_parse(&t->client_n_sl);
if(t->client_s_sl.status)
status_parse(&t->client_s_sl);
if(t->client_f_sl.status)
status_parse(&t->client_f_sl);
SLIST_INSERT_TAIL(&W->h.theme, t, next, p);
p = t;
}
free(ks);
}
static void
config_bars(void)
{
struct screen *s;
struct theme *t;
size_t i, n;
struct conf_sec *sec, **ks;
int screenid;
char *name, *elem;
enum barpos pos = BarTop;
/* [bars] */
sec = fetch_section_first(NULL, "bars");
ks = fetch_section(sec, "bar");
n = fetch_section_count(ks);
/* [bar] */
for(i = 0; i < n; ++i)
{
name = fetch_opt_first(ks[i], "default", "name").str;
elem = fetch_opt_first(ks[i], "", "elements").str;
screenid = fetch_opt_first(ks[i], "-1", "screen").num;
t = name_to_theme(fetch_opt_first(ks[i], "default", "theme").str);
pos = fetch_opt_first(ks[i], "0", "position").num;
SLIST_FOREACH(s, &W->h.screen, next)
if(screenid == s->id || screenid == -1)
infobar_new(s, name, t, pos, elem);
}
free(ks);
}
static void
config_tag(void)
{
struct screen *s;
struct tag *t;
size_t i, n;
struct conf_sec *sec, **ks, **mb;
char *name, *tmp;
int screenid;
/* [tags] */
sec = fetch_section_first(NULL, "tags");
ks = fetch_section(sec, "tag");
n = fetch_section_count(ks);
if (fetch_opt_first(sec, "1", "circular").boolean)
W->flags |= WMFS_TAGCIRC;
/* [mouse] */
if((mb = fetch_section(sec, "mouse")))
{
config_mouse_section(&W->tmp_head.tag, mb);
free(mb);
}
/* [tag] */
for(i = 0; i < n; ++i)
{
name = fetch_opt_first(ks[i], "tag", "name").str;
screenid = fetch_opt_first(ks[i], "-1", "screen").num;
SLIST_FOREACH(s, &W->h.screen, next)
if(screenid == s->id || screenid == -1)
{
t = tag_new(s, name);
t->statusctx = status_new_ctx(NULL, NULL);
ISTRDUP(t->statusctx.status, fetch_opt_first(ks[i], "", "statusline").str);
if(t->statusctx.status)
status_parse(&t->statusctx);
}
}
/* If no tag at all on a screen, add one anyway */
SLIST_FOREACH(s, &W->h.screen, next)
if(TAILQ_EMPTY(&s->tags))
tag_new(s, "tag");
free(ks);
}
static void
config_client(void)
{
struct conf_sec *sec, **mb;
char *tmp;
/* [client] */
sec = fetch_section_first(NULL, "client");
W->padding = fetch_opt_first(sec, "0", "padding").num;
W->client_mod = modkey_keysym(fetch_opt_first(sec, "Super", "key_modifier").str);
if(fetch_opt_first(sec, "0", "autofocus").boolean)
W->flags |= WMFS_AUTOFOCUS;
/* Get theme */
tmp = fetch_opt_first(sec, "default", "theme").str;
W->ctheme = name_to_theme(tmp);
/* Get focus configuration */
W->cfocus = 0;
tmp = fetch_opt_first(sec, "enter", "focus").str;
if(strstr(tmp, "enter"))
W->cfocus |= CFOCUS_ENTER;
if(strstr(tmp, "click"))
W->cfocus |= CFOCUS_CLICK;
/* [mouse] */
/* for client frame AND titlebar */
if((mb = fetch_section(sec, "mouse")))
{
config_mouse_section(&W->tmp_head.client, mb);
free(mb);
}
}
static void
config_rule(void)
{
int i, n;
struct conf_sec *sec, **ks;
struct rule *r;
char *tn, *tmp;
/* [rules] */
sec = fetch_section_first(NULL, "rules");
ks = fetch_section(sec, "rule");
n = fetch_section_count(ks);
SLIST_INIT(&W->h.rule);
/* [rule] */
for(i = 0; i < n; ++i)
{
r = (struct rule*)xcalloc(1, sizeof(struct rule));
ISTRDUP(r->class, fetch_opt_first(ks[i], "", "class").str);
ISTRDUP(r->instance, fetch_opt_first(ks[i], "", "instance").str);
ISTRDUP(r->role, fetch_opt_first(ks[i], "", "role").str);
ISTRDUP(r->name , fetch_opt_first(ks[i], "", "name").str);
ISTRDUP(r->client_machine, fetch_opt_first(ks[i], "", "client_machine").str);
r->screen = fetch_opt_first(ks[i], "-1", "screen").num;
r->tag = fetch_opt_first(ks[i], "-1", "tag").num;
FLAGAPPLY(r->flags, fetch_opt_first(ks[i], "false", "free").boolean, RULE_FREE);
FLAGAPPLY(r->flags, fetch_opt_first(ks[i], "false", "tab").boolean, RULE_TAB);
FLAGAPPLY(r->flags, fetch_opt_first(ks[i], "false", "ignore_tag").boolean, RULE_IGNORE_TAG);
if((tn = fetch_opt_first(ks[i], "", "theme").str))
r->theme = name_to_theme(tn);
else
r->theme = W->ctheme;
SLIST_INSERT_HEAD(&W->h.rule, r, next);
}
free(ks);
}
static void
config_launcher(void)
{
struct conf_sec *sec, **ks;
struct launcher *l;
int n, i;
/* [launchers] */
sec = fetch_section_first(NULL, "launchers");
ks = fetch_section(sec, "launcher");
n = fetch_section_count(ks);
SLIST_INIT(&W->h.launcher);
/* [launcher] */
for(i = 0; i < n; ++i)
{
l = xcalloc(1, sizeof(struct launcher));
l->name = xstrdup(fetch_opt_first(ks[i], "default", "name").str);
l->prompt = xstrdup(fetch_opt_first(ks[i], ":", "prompt").str);
l->command = xstrdup(fetch_opt_first(ks[i], "spawn", "command").str);
if((l->width = fetch_opt_first(ks[i], "150", "width").num) <= 0)
l->width = 150;
SLIST_INSERT_HEAD(&W->h.launcher, l, next);
}
free(ks);
}
static void
config_keybind(void)
{
int i, n;
size_t j;
struct conf_sec *sec, **ks;
struct opt_type *opt;
char *cmd;
struct keybind *k;
/* [keys] */
sec = fetch_section_first(NULL, "keys");
ks = fetch_section(sec, "key");
n = fetch_section_count(ks);
SLIST_INIT(&W->h.keybind);
/* [key] */
for(i = 0; i < n; ++i)
{
k = (struct keybind*)xcalloc(1, sizeof(struct keybind));
/* mod = {} */
opt = fetch_opt(ks[i], "", "mod");
for(j = k->mod = 0; j < fetch_opt_count(opt); ++j)
k->mod |= modkey_keysym(opt[j].str);
free(opt);
/* key = */
k->keysym = XStringToKeysym(fetch_opt_first(ks[i], "None", "key").str);
/* func = */
if(!(k->func = uicb_name_func(fetch_opt_first(ks[i], "", "func").str)))
{
warnxl("configuration: Unknown Function \"%s\".",
fetch_opt_first(ks[i], "", "func").str);
k->func = uicb_spawn;
}
/* cmd = */
if((cmd = fetch_opt_first(ks[i], "", "cmd").str))
k->cmd = xstrdup(cmd);
SLIST_INSERT_HEAD(&W->h.keybind, k, next);
}
wmfs_grab_keys();
free(ks);
}
void
config_init(void)
{
if(get_conf(W->confpath) == -1)
{
warnl("parsing configuration file (%s) failed.", W->confpath);
sprintf(W->confpath, "%s/"CONFIG_DEFAULT_PATH, getenv("HOME"));
if(get_conf(W->confpath) == -1)
{
warnxl("parsing default configuration file (%s) failed.", W->confpath);
sprintf(W->confpath, "%s/wmfs/wmfsrc", XDG_CONFIG_DIR);
if(get_conf(W->confpath) == -1)
errxl(1, "parsing system configuration file (%s) failed.", W->confpath);
}
}
config_theme();
config_keybind();
config_tag();
config_client();
config_bars();
config_rule();
config_launcher();
free_conf();
}