4 #include <yajl/yajl_parse.h>
8 /* A datatype to pass through the callbacks to save the state */
9 struct workspaces_json_params {
10 struct ws_head *workspaces;
11 i3_ws *workspaces_walk;
17 * Parse a booleant-value (visible, focused, urgent)
20 static int workspaces_boolean_cb(void *params_, bool val) {
21 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
23 if (!strcmp(params->cur_key, "visible")) {
24 params->workspaces_walk->visible = val;
25 FREE(params->cur_key);
29 if (!strcmp(params->cur_key, "focused")) {
30 params->workspaces_walk->focused = val;
31 FREE(params->cur_key);
35 if (!strcmp(params->cur_key, "urgent")) {
36 params->workspaces_walk->urgent = val;
37 FREE(params->cur_key);
41 FREE(params->cur_key);
47 * Parse an integer (num or the rect)
50 static int workspaces_integer_cb(void *params_, long val) {
51 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
53 if (!strcmp(params->cur_key, "num")) {
54 params->workspaces_walk->num = (int) val;
55 FREE(params->cur_key);
59 if (!strcmp(params->cur_key, "x")) {
60 params->workspaces_walk->rect.x = (int) val;
61 FREE(params->cur_key);
65 if (!strcmp(params->cur_key, "y")) {
66 params->workspaces_walk->rect.y = (int) val;
67 FREE(params->cur_key);
71 if (!strcmp(params->cur_key, "width")) {
72 params->workspaces_walk->rect.w = (int) val;
73 FREE(params->cur_key);
77 if (!strcmp(params->cur_key, "height")) {
78 params->workspaces_walk->rect.h = (int) val;
79 FREE(params->cur_key);
83 FREE(params->cur_key);
88 * Parse a string (name, output)
91 static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
93 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
97 if (!strcmp(params->cur_key, "name")) {
99 params->workspaces_walk->name = malloc(sizeof(const unsigned char) * (len + 1));
100 strncpy(params->workspaces_walk->name, (const char*) val, len);
101 params->workspaces_walk->name[len] = '\0';
103 /* Convert the name to ucs2, save it's length in glyphs and calculate it'srendered width */
105 xcb_char2b_t *ucs2_name = (xcb_char2b_t*) convert_utf8_to_ucs2(params->workspaces_walk->name, &ucs2_len);
106 params->workspaces_walk->ucs2_name = ucs2_name;
107 params->workspaces_walk->name_glyphs = ucs2_len;
108 params->workspaces_walk->name_width = get_string_width(params->workspaces_walk->ucs2_name,
109 params->workspaces_walk->name_glyphs);
111 printf("Got Workspace %s, name_width: %d, glyphs: %d\n",
112 params->workspaces_walk->name,
113 params->workspaces_walk->name_width,
114 params->workspaces_walk->name_glyphs);
115 FREE(params->cur_key);
120 if (!strcmp(params->cur_key, "output")) {
121 /* We add the ws to the TAILQ of the output, it belongs to */
122 output_name = malloc(sizeof(const unsigned char) * (len + 1));
123 strncpy(output_name, (const char*) val, len);
124 output_name[len] = '\0';
125 params->workspaces_walk->output = get_output_by_name(output_name);
127 TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
128 params->workspaces_walk,
139 * We hit the start of a json-map (rect or a new output)
142 static int workspaces_start_map_cb(void *params_) {
143 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
145 i3_ws *new_workspace = NULL;
147 if (params->cur_key == NULL) {
148 new_workspace = malloc(sizeof(i3_ws));
149 new_workspace->num = -1;
150 new_workspace->name = NULL;
151 new_workspace->visible = 0;
152 new_workspace->focused = 0;
153 new_workspace->urgent = 0;
154 memset(&new_workspace->rect, 0, sizeof(rect));
155 new_workspace->output = NULL;
157 params->workspaces_walk = new_workspace;
167 * Essentially we just save it in the parsing-state
170 static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
171 struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
172 FREE(params->cur_key);
174 params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
175 if (params->cur_key == NULL) {
176 printf("ERROR: Could not allocate memory!\n");
179 strncpy(params->cur_key, (const char*) keyVal, keyLen);
180 params->cur_key[keyLen] = '\0';
185 /* A datastructure to pass all these callbacks to yajl */
186 yajl_callbacks workspaces_callbacks = {
188 &workspaces_boolean_cb,
189 &workspaces_integer_cb,
192 &workspaces_string_cb,
193 &workspaces_start_map_cb,
194 &workspaces_map_key_cb,
201 * Start parsing the received json-string
204 void parse_workspaces_json(char *json) {
205 /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
207 struct workspaces_json_params params;
211 params.workspaces_walk = NULL;
212 params.cur_key = NULL;
216 yajl_parser_config parse_conf = { 0, 0 };
219 handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) ¶ms);
221 state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
223 /* FIXME: Propper errorhandling for JSON-parsing */
227 case yajl_status_client_canceled:
228 case yajl_status_insufficient_data:
229 case yajl_status_error:
230 printf("ERROR: Could not parse workspaces-reply!\n");
237 FREE(params.cur_key);
241 * free() all workspace data-structures
244 void free_workspaces() {
245 i3_output *outputs_walk;
246 SLIST_FOREACH(outputs_walk, outputs, slist) {
247 if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
248 FREE_TAILQ(outputs_walk->workspaces, i3_ws);