| 1 |
/* |
| 2 |
* vim:ts=4:sw=4:expandtab |
| 3 |
* |
| 4 |
* i3bar - an xcb-based status- and ws-bar for i3 |
| 5 |
* © 2010 Axel Wagner and contributors (see also: LICENSE) |
| 6 |
* |
| 7 |
* workspaces.c: Maintaining the workspace lists |
| 8 |
* |
| 9 |
*/ |
| 10 |
#include "common.h" |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include <yajl/yajl_parse.h> |
| 16 |
|
| 17 |
/* A datatype to pass through the callbacks to save the state */ |
| 18 |
struct workspaces_json_params { |
| 19 |
struct ws_head *workspaces; |
| 20 |
i3_ws *workspaces_walk; |
| 21 |
char *cur_key; |
| 22 |
bool need_output; |
| 23 |
bool parsing_rect; |
| 24 |
}; |
| 25 |
|
| 26 |
/* |
| 27 |
* Parse a boolean value (visible, focused, urgent) |
| 28 |
* |
| 29 |
*/ |
| 30 |
static int workspaces_boolean_cb(void *params_, const int val) { |
| 31 |
struct workspaces_json_params *params = params_; |
| 32 |
|
| 33 |
if (!strcmp(params->cur_key, "visible")) { |
| 34 |
params->workspaces_walk->visible = val; |
| 35 |
FREE(params->cur_key); |
| 36 |
return 1; |
| 37 |
} |
| 38 |
|
| 39 |
if (!strcmp(params->cur_key, "focused")) { |
| 40 |
params->workspaces_walk->focused = val; |
| 41 |
FREE(params->cur_key); |
| 42 |
return 1; |
| 43 |
} |
| 44 |
|
| 45 |
if (!strcmp(params->cur_key, "urgent")) { |
| 46 |
params->workspaces_walk->urgent = val; |
| 47 |
FREE(params->cur_key); |
| 48 |
return 1; |
| 49 |
} |
| 50 |
|
| 51 |
FREE(params->cur_key); |
| 52 |
|
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
/* |
| 57 |
* Parse an integer (num or the rect) |
| 58 |
* |
| 59 |
*/ |
| 60 |
static int workspaces_integer_cb(void *params_, const long long val) { |
| 61 |
struct workspaces_json_params *params = params_; |
| 62 |
|
| 63 |
if (!strcmp(params->cur_key, "id")) { |
| 64 |
params->workspaces_walk->id = val; |
| 65 |
FREE(params->cur_key); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
if (!strcmp(params->cur_key, "num")) { |
| 70 |
params->workspaces_walk->num = (int)val; |
| 71 |
FREE(params->cur_key); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
/* rect is unused, so we don't bother to save it */ |
| 76 |
if (!strcmp(params->cur_key, "x")) { |
| 77 |
FREE(params->cur_key); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
if (!strcmp(params->cur_key, "y")) { |
| 82 |
FREE(params->cur_key); |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
if (!strcmp(params->cur_key, "width")) { |
| 87 |
FREE(params->cur_key); |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
if (!strcmp(params->cur_key, "height")) { |
| 92 |
FREE(params->cur_key); |
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
FREE(params->cur_key); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* Parse a string (name, output) |
| 102 |
* |
| 103 |
*/ |
| 104 |
static int workspaces_string_cb(void *params_, const unsigned char *val, const size_t len) { |
| 105 |
struct workspaces_json_params *params = params_; |
| 106 |
|
| 107 |
if (!strcmp(params->cur_key, "name")) { |
| 108 |
const char *ws_name = (const char *)val; |
| 109 |
params->workspaces_walk->canonical_name = sstrndup(ws_name, len); |
| 110 |
|
| 111 |
if ((config.strip_ws_numbers || config.strip_ws_name) && params->workspaces_walk->num >= 0) { |
| 112 |
/* Special case: strip off the workspace number/name */ |
| 113 |
static char ws_num[32]; |
| 114 |
|
| 115 |
snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num); |
| 116 |
|
| 117 |
/* Calculate the length of the number str in the name */ |
| 118 |
size_t offset = strspn(ws_name, ws_num); |
| 119 |
|
| 120 |
/* Also strip off the conventional ws name delimiter */ |
| 121 |
if (offset && ws_name[offset] == ':') { |
| 122 |
offset += 1; |
| 123 |
} |
| 124 |
|
| 125 |
if (config.strip_ws_numbers) { |
| 126 |
/* Offset may be equal to length, in which case display the number */ |
| 127 |
params->workspaces_walk->name = offset < len |
| 128 |
? i3string_from_markup_with_length(ws_name + offset, len - offset) |
| 129 |
: i3string_from_markup(ws_num); |
| 130 |
} else { |
| 131 |
params->workspaces_walk->name = i3string_from_markup(ws_num); |
| 132 |
} |
| 133 |
} else { |
| 134 |
/* Default case: just save the name */ |
| 135 |
params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len); |
| 136 |
} |
| 137 |
|
| 138 |
/* Save its rendered width */ |
| 139 |
params->workspaces_walk->name_width = |
| 140 |
predict_text_width(params->workspaces_walk->name); |
| 141 |
|
| 142 |
DLOG("Got workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n", |
| 143 |
params->workspaces_walk->canonical_name, |
| 144 |
i3string_as_utf8(params->workspaces_walk->name), |
| 145 |
params->workspaces_walk->name_width, |
| 146 |
i3string_get_num_glyphs(params->workspaces_walk->name)); |
| 147 |
FREE(params->cur_key); |
| 148 |
|
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
if (!strcmp(params->cur_key, "output")) { |
| 153 |
/* We add the ws to the TAILQ of the output, it belongs to */ |
| 154 |
char *output_name = NULL; |
| 155 |
sasprintf(&output_name, "%.*s", (int)len, val); |
| 156 |
|
| 157 |
i3_output *target = get_output_by_name(output_name); |
| 158 |
i3_ws *ws = params->workspaces_walk; |
| 159 |
if (target != NULL) { |
| 160 |
ws->output = target; |
| 161 |
TAILQ_INSERT_TAIL(ws->output->workspaces, ws, tailq); |
| 162 |
} |
| 163 |
|
| 164 |
params->need_output = false; |
| 165 |
FREE(output_name); |
| 166 |
FREE(params->cur_key); |
| 167 |
|
| 168 |
return 1; |
| 169 |
} |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* We hit the start of a JSON map (rect or a new workspace) |
| 176 |
* |
| 177 |
*/ |
| 178 |
static int workspaces_start_map_cb(void *params_) { |
| 179 |
struct workspaces_json_params *params = params_; |
| 180 |
|
| 181 |
if (params->cur_key == NULL) { |
| 182 |
i3_ws *new_workspace = scalloc(1, sizeof(i3_ws)); |
| 183 |
new_workspace->num = -1; |
| 184 |
|
| 185 |
params->workspaces_walk = new_workspace; |
| 186 |
params->need_output = true; |
| 187 |
params->parsing_rect = false; |
| 188 |
} else { |
| 189 |
params->parsing_rect = true; |
| 190 |
} |
| 191 |
|
| 192 |
return 1; |
| 193 |
} |
| 194 |
|
| 195 |
static int workspaces_end_map_cb(void *params_) { |
| 196 |
struct workspaces_json_params *params = params_; |
| 197 |
|
| 198 |
if (params->parsing_rect) { |
| 199 |
params->parsing_rect = false; |
| 200 |
return 1; |
| 201 |
} |
| 202 |
|
| 203 |
i3_ws *ws = params->workspaces_walk; |
| 204 |
if (!ws || ws->output) { |
| 205 |
return 1; /* workspace already assigned to output */ |
| 206 |
} |
| 207 |
|
| 208 |
/* If we processed the output field but didn't find the output, the |
| 209 |
* workspace belongs to an output this bar instance doesn't manage. */ |
| 210 |
if (!params->need_output) { |
| 211 |
I3STRING_FREE(ws->name); |
| 212 |
FREE(ws->canonical_name); |
| 213 |
FREE(params->workspaces_walk); |
| 214 |
return 1; |
| 215 |
} |
| 216 |
|
| 217 |
if (!ws->name || SLIST_EMPTY(outputs)) { /* Invalid state */ |
| 218 |
I3STRING_FREE(ws->name); |
| 219 |
FREE(ws->canonical_name); |
| 220 |
FREE(params->workspaces_walk); |
| 221 |
return 1; |
| 222 |
} |
| 223 |
|
| 224 |
/* Handle no output case - fallback to primary */ |
| 225 |
ws->output = get_output_by_name("primary"); |
| 226 |
if (ws->output == NULL) { |
| 227 |
ws->output = SLIST_FIRST(outputs); |
| 228 |
} |
| 229 |
TAILQ_INSERT_TAIL(ws->output->workspaces, ws, tailq); |
| 230 |
|
| 231 |
return 1; |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
* Parse a key. |
| 236 |
* |
| 237 |
* Essentially we just save it in the parsing state |
| 238 |
* |
| 239 |
*/ |
| 240 |
static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, const size_t keyLen) { |
| 241 |
struct workspaces_json_params *params = params_; |
| 242 |
FREE(params->cur_key); |
| 243 |
sasprintf(¶ms->cur_key, "%.*s", (int)keyLen, keyVal); |
| 244 |
return 1; |
| 245 |
} |
| 246 |
|
| 247 |
/* A data structure to pass all these callbacks to yajl */ |
| 248 |
static yajl_callbacks workspaces_callbacks = { |
| 249 |
.yajl_boolean = workspaces_boolean_cb, |
| 250 |
.yajl_integer = workspaces_integer_cb, |
| 251 |
.yajl_string = workspaces_string_cb, |
| 252 |
.yajl_start_map = workspaces_start_map_cb, |
| 253 |
.yajl_end_map = workspaces_end_map_cb, |
| 254 |
.yajl_map_key = workspaces_map_key_cb, |
| 255 |
}; |
| 256 |
|
| 257 |
/* |
| 258 |
* Parse the received JSON string |
| 259 |
* |
| 260 |
*/ |
| 261 |
void parse_workspaces_json(const unsigned char *json, const size_t size) { |
| 262 |
free_workspaces(); |
| 263 |
|
| 264 |
struct workspaces_json_params params = {0}; |
| 265 |
const yajl_handle handle = yajl_alloc(&workspaces_callbacks, NULL, ¶ms); |
| 266 |
const yajl_status state = yajl_parse(handle, json, size); |
| 267 |
|
| 268 |
/* FIXME: Proper error handling for JSON parsing */ |
| 269 |
switch (state) { |
| 270 |
case yajl_status_ok: |
| 271 |
break; |
| 272 |
case yajl_status_client_canceled: |
| 273 |
case yajl_status_error: { |
| 274 |
unsigned char *err = yajl_get_error(handle, 1, json, size); |
| 275 |
ELOG("Could not parse workspaces reply, error:\n%s\njson:---%s---\n", (char *)err, (char *)json); |
| 276 |
yajl_free_error(handle, err); |
| 277 |
|
| 278 |
if (config.workspace_command) { |
| 279 |
kill_ws_child(); |
| 280 |
set_workspace_button_error("Could not parse workspace_command's JSON"); |
| 281 |
} else { |
| 282 |
exit(EXIT_FAILURE); |
| 283 |
} |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
yajl_free(handle); |
| 289 |
FREE(params.cur_key); |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
* free() all workspace data structures. Does not free() the heads of the tailqueues. |
| 294 |
* |
| 295 |
*/ |
| 296 |
void free_workspaces(void) { |
| 297 |
if (outputs == NULL) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
i3_output *outputs_walk; |
| 302 |
SLIST_FOREACH (outputs_walk, outputs, slist) { |
| 303 |
if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) { |
| 304 |
i3_ws *ws_walk; |
| 305 |
TAILQ_FOREACH (ws_walk, outputs_walk->workspaces, tailq) { |
| 306 |
I3STRING_FREE(ws_walk->name); |
| 307 |
FREE(ws_walk->canonical_name); |
| 308 |
} |
| 309 |
FREE_TAILQ(outputs_walk->workspaces, i3_ws); |
| 310 |
} |
| 311 |
} |
| 312 |
} |