2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010 Axel Wagner and contributors (see also: LICENSE)
7 * mode.c: Handle mode event and show current binding mode in the bar
14 #include <yajl/yajl_parse.h>
15 #include <yajl/yajl_version.h>
19 /* A datatype to pass through the callbacks to save the state */
20 struct mode_json_params {
27 * Parse a string (change)
30 static int mode_string_cb(void *params_, const unsigned char *val, size_t len) {
31 struct mode_json_params *params = (struct mode_json_params *)params_;
33 if (!strcmp(params->cur_key, "change")) {
35 params->mode->name = i3string_from_markup_with_length((const char *)val, len);
36 /* Save its rendered width */
37 params->mode->width = predict_text_width(params->mode->name);
39 DLOG("Got mode change: %s\n", i3string_as_utf8(params->mode->name));
40 FREE(params->cur_key);
51 * Essentially we just save it in the parsing state
54 static int mode_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
55 struct mode_json_params *params = (struct mode_json_params *)params_;
56 FREE(params->cur_key);
58 params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
59 strncpy(params->cur_key, (const char *)keyVal, keyLen);
60 params->cur_key[keyLen] = '\0';
65 /* A datastructure to pass all these callbacks to yajl */
66 static yajl_callbacks mode_callbacks = {
67 .yajl_string = mode_string_cb,
68 .yajl_map_key = mode_map_key_cb,
72 * Start parsing the received JSON string
75 void parse_mode_json(char *json) {
76 /* FIXME: Fasciliate stream processing, i.e. allow starting to interpret
78 struct mode_json_params params;
82 params.cur_key = NULL;
84 params.mode = &binding;
89 handle = yajl_alloc(&mode_callbacks, NULL, (void *)¶ms);
91 state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
93 /* FIXME: Propper error handling for JSON parsing */
97 case yajl_status_client_canceled:
98 case yajl_status_error:
99 ELOG("Could not parse mode event!\n");
104 /* We don't want to indicate default binding mode */
105 if (strcmp("default", i3string_as_utf8(params.mode->name)) == 0)
106 I3STRING_FREE(params.mode->name);
108 /* Set the new binding mode */
109 set_current_mode(&binding);
113 FREE(params.cur_key);