Codebase list i3-gaps / 6a17515 i3bar / src / config.c
6a17515

Tree @6a17515 (Download .tar.gz)

config.c @6a17515raw · history · blame

  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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * vim:ts=4:sw=4:expandtab
 *
 * i3bar - an xcb-based status- and ws-bar for i3
 * © 2010 Axel Wagner and contributors (see also: LICENSE)
 *
 * config.c: Parses the configuration (received from i3).
 *
 */
#include "common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <yajl/yajl_parse.h>

config_t config;
static char *cur_key;
static bool parsing_bindings;
static bool parsing_tray_outputs;

/*
 * Parse a key.
 *
 * Essentially we just save it in cur_key.
 *
 */
static int config_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
    FREE(cur_key);
    sasprintf(&(cur_key), "%.*s", keyLen, keyVal);

    if (strcmp(cur_key, "bindings") == 0) {
        parsing_bindings = true;
    }

    if (strcmp(cur_key, "tray_outputs") == 0) {
        parsing_tray_outputs = true;
    }

    return 1;
}

static int config_end_array_cb(void *params_) {
    parsing_bindings = false;
    parsing_tray_outputs = false;
    return 1;
}

/*
 * Parse a null value (current_workspace)
 *
 */
static int config_null_cb(void *params_) {
    if (!strcmp(cur_key, "id")) {
        /* If 'id' is NULL, the bar config was not found. Error out. */
        ELOG("No such bar config. Use 'i3-msg -t get_bar_config' to get the available configs.\n");
        ELOG("Are you starting i3bar by hand? You should not:\n");
        ELOG("Configure a 'bar' block in your i3 config and i3 will launch i3bar automatically.\n");
        exit(EXIT_FAILURE);
    }

    return 1;
}

/*
 * Parse a string
 *
 */
static int config_string_cb(void *params_, const unsigned char *val, size_t _len) {
    int len = (int)_len;
    /* The id and socket_path are ignored, we already know them. */
    if (!strcmp(cur_key, "id") || !strcmp(cur_key, "socket_path"))
        return 1;

    if (parsing_bindings) {
        if (strcmp(cur_key, "command") == 0) {
            binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
            if (binding == NULL) {
                ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
                return 0;
            }

            if (binding->command != NULL) {
                ELOG("The binding for input_code = %d already has a command. This is a bug in i3.\n", binding->input_code);
                return 0;
            }

            sasprintf(&(binding->command), "%.*s", len, val);
            return 1;
        }

        ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
        return 0;
    }

    if (parsing_tray_outputs) {
        DLOG("Adding tray_output = %.*s to the list.\n", len, val);
        tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
        sasprintf(&(tray_output->output), "%.*s", len, val);
        TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
        return 1;
    }

    if (!strcmp(cur_key, "mode")) {
        DLOG("mode = %.*s, len = %d\n", len, val, len);
        config.hide_on_modifier = (len == strlen("dock") && !strncmp((const char *)val, "dock", strlen("dock")) ? M_DOCK
                                                                                                                : (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? M_HIDE
                                                                                                                                                                                                : M_INVISIBLE));
        return 1;
    }

    if (!strcmp(cur_key, "hidden_state")) {
        DLOG("hidden_state = %.*s, len = %d\n", len, val, len);
        config.hidden_state = (len == strlen("hide") && !strncmp((const char *)val, "hide", strlen("hide")) ? S_HIDE : S_SHOW);
        return 1;
    }

    /* Kept for backwards compatibility. */
    if (!strcmp(cur_key, "modifier")) {
        DLOG("modifier = %.*s\n", len, val);
        if (len == strlen("none") && !strncmp((const char *)val, "none", strlen("none"))) {
            config.modifier = XCB_NONE;
            return 1;
        }

        if (len == strlen("shift") && !strncmp((const char *)val, "shift", strlen("shift"))) {
            config.modifier = XCB_MOD_MASK_SHIFT;
            return 1;
        }
        if (len == strlen("ctrl") && !strncmp((const char *)val, "ctrl", strlen("ctrl"))) {
            config.modifier = XCB_MOD_MASK_CONTROL;
            return 1;
        }
        if (len == strlen("Mod") + 1 && !strncmp((const char *)val, "Mod", strlen("Mod"))) {
            switch (val[3]) {
                case '1':
                    config.modifier = XCB_MOD_MASK_1;
                    return 1;
                case '2':
                    config.modifier = XCB_MOD_MASK_2;
                    return 1;
                case '3':
                    config.modifier = XCB_MOD_MASK_3;
                    return 1;
                case '5':
                    config.modifier = XCB_MOD_MASK_5;
                    return 1;
            }
        }

        config.modifier = XCB_MOD_MASK_4;
        return 1;
    }

    /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
     * users updating from that version and restarting i3bar before i3. */
    if (!strcmp(cur_key, "wheel_up_cmd")) {
        DLOG("wheel_up_cmd = %.*s\n", len, val);
        binding_t *binding = scalloc(1, sizeof(binding_t));
        binding->input_code = 4;
        sasprintf(&(binding->command), "%.*s", len, val);
        TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
        return 1;
    }

    /* This key was sent in <= 4.10.2. We keep it around to avoid breakage for
     * users updating from that version and restarting i3bar before i3. */
    if (!strcmp(cur_key, "wheel_down_cmd")) {
        DLOG("wheel_down_cmd = %.*s\n", len, val);
        binding_t *binding = scalloc(1, sizeof(binding_t));
        binding->input_code = 5;
        sasprintf(&(binding->command), "%.*s", len, val);
        TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);
        return 1;
    }

    if (!strcmp(cur_key, "position")) {
        DLOG("position = %.*s\n", len, val);
        config.position = (len == strlen("top") && !strncmp((const char *)val, "top", strlen("top")) ? POS_TOP : POS_BOT);
        return 1;
    }

    if (!strcmp(cur_key, "status_command")) {
        DLOG("command = %.*s\n", len, val);
        sasprintf(&config.command, "%.*s", len, val);
        return 1;
    }

    if (!strcmp(cur_key, "font")) {
        DLOG("font = %.*s\n", len, val);
        FREE(config.fontname);
        sasprintf(&config.fontname, "%.*s", len, val);
        return 1;
    }

    if (!strcmp(cur_key, "separator_symbol")) {
        DLOG("separator = %.*s\n", len, val);
        I3STRING_FREE(config.separator_symbol);
        config.separator_symbol = i3string_from_utf8_with_length((const char *)val, len);
        return 1;
    }

    if (!strcmp(cur_key, "outputs")) {
        DLOG("+output %.*s\n", len, val);
        int new_num_outputs = config.num_outputs + 1;
        config.outputs = srealloc(config.outputs, sizeof(char *) * new_num_outputs);
        sasprintf(&config.outputs[config.num_outputs], "%.*s", len, val);
        config.num_outputs = new_num_outputs;
        return 1;
    }

    /* We keep the old single tray_output working for users who only restart i3bar
     * after updating. */
    if (!strcmp(cur_key, "tray_output")) {
        DLOG("Found deprecated key tray_output %.*s.\n", len, val);
        tray_output_t *tray_output = scalloc(1, sizeof(tray_output_t));
        sasprintf(&(tray_output->output), "%.*s", len, val);
        TAILQ_INSERT_TAIL(&(config.tray_outputs), tray_output, tray_outputs);
        return 1;
    }

#define COLOR(json_name, struct_name)                                  \
    do {                                                               \
        if (!strcmp(cur_key, #json_name)) {                            \
            DLOG(#json_name " = " #struct_name " = %.*s\n", len, val); \
            sasprintf(&(config.colors.struct_name), "%.*s", len, val); \
            return 1;                                                  \
        }                                                              \
    } while (0)

    COLOR(statusline, bar_fg);
    COLOR(background, bar_bg);
    COLOR(separator, sep_fg);
    COLOR(focused_statusline, focus_bar_fg);
    COLOR(focused_background, focus_bar_bg);
    COLOR(focused_separator, focus_sep_fg);
    COLOR(focused_workspace_border, focus_ws_border);
    COLOR(focused_workspace_bg, focus_ws_bg);
    COLOR(focused_workspace_text, focus_ws_fg);
    COLOR(active_workspace_border, active_ws_border);
    COLOR(active_workspace_bg, active_ws_bg);
    COLOR(active_workspace_text, active_ws_fg);
    COLOR(inactive_workspace_border, inactive_ws_border);
    COLOR(inactive_workspace_bg, inactive_ws_bg);
    COLOR(inactive_workspace_text, inactive_ws_fg);
    COLOR(urgent_workspace_border, urgent_ws_border);
    COLOR(urgent_workspace_bg, urgent_ws_bg);
    COLOR(urgent_workspace_text, urgent_ws_fg);
    COLOR(binding_mode_border, binding_mode_border);
    COLOR(binding_mode_bg, binding_mode_bg);
    COLOR(binding_mode_text, binding_mode_fg);

    printf("got unexpected string %.*s for cur_key = %s\n", len, val, cur_key);

    return 0;
}

/*
 * Parse a boolean value
 *
 */
static int config_boolean_cb(void *params_, int val) {
    if (parsing_bindings) {
        if (strcmp(cur_key, "release") == 0) {
            binding_t *binding = TAILQ_LAST(&(config.bindings), bindings_head);
            if (binding == NULL) {
                ELOG("There is no binding to put the current command onto. This is a bug in i3.\n");
                return 0;
            }

            binding->release = val;
            return 1;
        }

        ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
    }

    if (!strcmp(cur_key, "binding_mode_indicator")) {
        DLOG("binding_mode_indicator = %d\n", val);
        config.disable_binding_mode_indicator = !val;
        return 1;
    }

    if (!strcmp(cur_key, "workspace_buttons")) {
        DLOG("workspace_buttons = %d\n", val);
        config.disable_ws = !val;
        return 1;
    }

    if (!strcmp(cur_key, "strip_workspace_numbers")) {
        DLOG("strip_workspace_numbers = %d\n", val);
        config.strip_ws_numbers = val;
        return 1;
    }

    if (!strcmp(cur_key, "strip_workspace_name")) {
        DLOG("strip_workspace_name = %d\n", val);
        config.strip_ws_name = val;
        return 1;
    }

    if (!strcmp(cur_key, "verbose")) {
        if (!config.verbose) {
            DLOG("verbose = %d\n", val);
            config.verbose = val;
        }
        return 1;
    }

    return 0;
}

/*
 * Parse an integer value
 *
 */
static int config_integer_cb(void *params_, long long val) {
    if (parsing_bindings) {
        if (strcmp(cur_key, "input_code") == 0) {
            binding_t *binding = scalloc(1, sizeof(binding_t));
            binding->input_code = val;
            TAILQ_INSERT_TAIL(&(config.bindings), binding, bindings);

            return 1;
        }

        ELOG("Unknown key \"%s\" while parsing bar bindings.\n", cur_key);
        return 0;
    }

    if (!strcmp(cur_key, "bar_height")) {
        DLOG("bar_height = %lld", val);
        config.bar_height = (uint32_t)val;
        return 1;
    }

    if (!strcmp(cur_key, "tray_padding")) {
        DLOG("tray_padding = %lld\n", val);
        config.tray_padding = val;
        return 1;
    }

    if (!strcmp(cur_key, "modifier")) {
        DLOG("modifier = %lld\n", val);
        config.modifier = (uint32_t)val;
        return 1;
    }

    if (!strcmp(cur_key, "workspace_min_width")) {
        DLOG("workspace_min_width = %lld\n", val);
        config.ws_min_width = val;
        return 1;
    }

    return 0;
}

/* A datastructure to pass all these callbacks to yajl */
static yajl_callbacks outputs_callbacks = {
    .yajl_null = config_null_cb,
    .yajl_integer = config_integer_cb,
    .yajl_boolean = config_boolean_cb,
    .yajl_string = config_string_cb,
    .yajl_end_array = config_end_array_cb,
    .yajl_map_key = config_map_key_cb,
};

/*
 * Start parsing the received bar configuration JSON string
 *
 */
void parse_config_json(char *json) {
    yajl_handle handle = yajl_alloc(&outputs_callbacks, NULL, NULL);

    TAILQ_INIT(&(config.bindings));
    TAILQ_INIT(&(config.tray_outputs));

    yajl_status state = yajl_parse(handle, (const unsigned char *)json, strlen(json));

    /* FIXME: Proper error handling for JSON parsing */
    switch (state) {
        case yajl_status_ok:
            break;
        case yajl_status_client_canceled:
        case yajl_status_error:
            ELOG("Could not parse config reply!\n");
            exit(EXIT_FAILURE);
            break;
    }

    yajl_free(handle);
}

static int i3bar_config_string_cb(void *params_, const unsigned char *val, size_t _len) {
    sasprintf(&config.bar_id, "%.*s", (int)_len, val);
    return 0; /* Stop parsing */
}

/*
 * Start parsing the received bar configuration list. The only usecase right
 * now is to automatically get the first bar id.
 *
 */
void parse_get_first_i3bar_config(char *json) {
    yajl_callbacks configs_callbacks = {
        .yajl_string = i3bar_config_string_cb,
    };
    yajl_handle handle = yajl_alloc(&configs_callbacks, NULL, NULL);
    yajl_parse(handle, (const unsigned char *)json, strlen(json));
    yajl_free(handle);
}

/*
 * free()s the color strings as soon as they are not needed anymore.
 *
 */
void free_colors(struct xcb_color_strings_t *colors) {
#define FREE_COLOR(x)    \
    do {                 \
        FREE(colors->x); \
    } while (0)
    FREE_COLOR(bar_fg);
    FREE_COLOR(bar_bg);
    FREE_COLOR(sep_fg);
    FREE_COLOR(focus_bar_fg);
    FREE_COLOR(focus_bar_bg);
    FREE_COLOR(focus_sep_fg);
    FREE_COLOR(active_ws_fg);
    FREE_COLOR(active_ws_bg);
    FREE_COLOR(active_ws_border);
    FREE_COLOR(inactive_ws_fg);
    FREE_COLOR(inactive_ws_bg);
    FREE_COLOR(inactive_ws_border);
    FREE_COLOR(urgent_ws_fg);
    FREE_COLOR(urgent_ws_bg);
    FREE_COLOR(urgent_ws_border);
    FREE_COLOR(focus_ws_fg);
    FREE_COLOR(focus_ws_bg);
    FREE_COLOR(focus_ws_border);
    FREE_COLOR(binding_mode_fg);
    FREE_COLOR(binding_mode_bg);
    FREE_COLOR(binding_mode_border);
#undef FREE_COLOR
}