2012-11-10 13:41:39 +01:00
|
|
|
/*
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
|
|
|
* i3bar - an xcb-based status- and ws-bar for i3
|
2015-04-04 02:17:56 +02:00
|
|
|
* © 2010 Axel Wagner and contributors (see also: LICENSE)
|
2012-11-10 13:41:39 +01:00
|
|
|
*
|
2015-03-23 20:56:49 +01:00
|
|
|
* mode.c: Handle mode event and show current binding mode in the bar
|
2012-11-10 13:41:39 +01:00
|
|
|
*
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
#include "common.h"
|
|
|
|
|
2012-11-10 13:41:39 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <yajl/yajl_parse.h>
|
|
|
|
#include <yajl/yajl_version.h>
|
|
|
|
|
|
|
|
/* A datatype to pass through the callbacks to save the state */
|
|
|
|
struct mode_json_params {
|
2014-06-15 19:07:02 +02:00
|
|
|
char *json;
|
|
|
|
char *cur_key;
|
2015-10-12 23:43:47 +02:00
|
|
|
char *name;
|
|
|
|
bool pango_markup;
|
2014-06-15 19:07:02 +02:00
|
|
|
mode *mode;
|
2012-11-10 13:41:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a string (change)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int mode_string_cb(void *params_, const unsigned char *val, size_t len) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct mode_json_params *params = (struct mode_json_params *)params_;
|
2012-11-10 13:41:39 +01:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
if (!strcmp(params->cur_key, "change")) {
|
2015-10-17 22:14:48 +02:00
|
|
|
sasprintf(&(params->name), "%.*s", len, val);
|
2014-06-15 19:07:02 +02:00
|
|
|
FREE(params->cur_key);
|
2015-10-12 23:43:47 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a boolean.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int mode_boolean_cb(void *params_, int val) {
|
|
|
|
struct mode_json_params *params = (struct mode_json_params *)params_;
|
2012-11-10 13:41:39 +01:00
|
|
|
|
2015-10-12 23:43:47 +02:00
|
|
|
if (strcmp(params->cur_key, "pango_markup") == 0) {
|
|
|
|
DLOG("Setting pango_markup to %d.\n", val);
|
|
|
|
params->pango_markup = val;
|
|
|
|
|
|
|
|
FREE(params->cur_key);
|
2014-06-15 19:07:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2012-11-10 13:41:39 +01:00
|
|
|
|
2015-10-12 23:43:47 +02:00
|
|
|
FREE(params->cur_key);
|
2014-06-15 19:07:02 +02:00
|
|
|
return 0;
|
2012-11-10 13:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a key.
|
|
|
|
*
|
2015-03-23 20:56:49 +01:00
|
|
|
* Essentially we just save it in the parsing state
|
2012-11-10 13:41:39 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int mode_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct mode_json_params *params = (struct mode_json_params *)params_;
|
2012-11-10 13:41:39 +01:00
|
|
|
FREE(params->cur_key);
|
2015-10-17 22:14:48 +02:00
|
|
|
sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
|
2012-11-10 13:41:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:43:47 +02:00
|
|
|
static int mode_end_map_cb(void *params_) {
|
|
|
|
struct mode_json_params *params = (struct mode_json_params *)params_;
|
|
|
|
|
|
|
|
/* Save the name */
|
|
|
|
params->mode->name = i3string_from_utf8(params->name);
|
|
|
|
i3string_set_markup(params->mode->name, params->pango_markup);
|
|
|
|
/* Save its rendered width */
|
|
|
|
params->mode->width = predict_text_width(params->mode->name);
|
|
|
|
|
|
|
|
DLOG("Got mode change: %s\n", i3string_as_utf8(params->mode->name));
|
|
|
|
FREE(params->cur_key);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-10 13:41:39 +01:00
|
|
|
/* A datastructure to pass all these callbacks to yajl */
|
2014-01-01 17:19:55 +01:00
|
|
|
static yajl_callbacks mode_callbacks = {
|
|
|
|
.yajl_string = mode_string_cb,
|
2015-10-12 23:43:47 +02:00
|
|
|
.yajl_boolean = mode_boolean_cb,
|
2014-01-01 17:19:55 +01:00
|
|
|
.yajl_map_key = mode_map_key_cb,
|
2015-10-12 23:43:47 +02:00
|
|
|
.yajl_end_map = mode_end_map_cb,
|
2012-11-10 13:41:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2015-03-23 20:56:49 +01:00
|
|
|
* Start parsing the received JSON string
|
2012-11-10 13:41:39 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void parse_mode_json(char *json) {
|
2015-03-23 20:56:49 +01:00
|
|
|
/* FIXME: Fasciliate stream processing, i.e. allow starting to interpret
|
2012-11-10 13:41:39 +01:00
|
|
|
* JSON in chunks */
|
|
|
|
struct mode_json_params params;
|
|
|
|
|
|
|
|
mode binding;
|
|
|
|
|
|
|
|
params.cur_key = NULL;
|
|
|
|
params.json = json;
|
|
|
|
params.mode = &binding;
|
|
|
|
|
|
|
|
yajl_handle handle;
|
|
|
|
yajl_status state;
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
handle = yajl_alloc(&mode_callbacks, NULL, (void *)¶ms);
|
2012-11-10 13:41:39 +01:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
|
2012-11-10 13:41:39 +01:00
|
|
|
|
2016-04-04 09:33:59 +02:00
|
|
|
/* FIXME: Proper error handling for JSON parsing */
|
2012-11-10 13:41:39 +01:00
|
|
|
switch (state) {
|
|
|
|
case yajl_status_ok:
|
|
|
|
break;
|
|
|
|
case yajl_status_client_canceled:
|
|
|
|
case yajl_status_error:
|
2015-03-23 20:56:49 +01:00
|
|
|
ELOG("Could not parse mode event!\n");
|
2012-11-10 13:41:39 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't want to indicate default binding mode */
|
|
|
|
if (strcmp("default", i3string_as_utf8(params.mode->name)) == 0)
|
|
|
|
I3STRING_FREE(params.mode->name);
|
|
|
|
|
|
|
|
/* Set the new binding mode */
|
|
|
|
set_current_mode(&binding);
|
|
|
|
|
|
|
|
yajl_free(handle);
|
|
|
|
|
|
|
|
FREE(params.cur_key);
|
|
|
|
}
|