2010-08-07 18:05:16 +02:00
|
|
|
/*
|
2011-10-09 15:28:20 +02:00
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
*
|
2010-08-07 18:05:16 +02:00
|
|
|
* 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)
|
2010-08-07 18:05:16 +02:00
|
|
|
*
|
2015-03-23 20:56:49 +01:00
|
|
|
* workspaces.c: Maintaining the workspace lists
|
2010-08-07 18:05:16 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-10-11 09:13:35 +02:00
|
|
|
#include "common.h"
|
|
|
|
|
2010-07-22 01:15:18 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2011-01-01 16:48:30 +01:00
|
|
|
#include <errno.h>
|
2010-07-22 01:15:18 +02:00
|
|
|
#include <yajl/yajl_parse.h>
|
2011-04-28 20:23:12 +02:00
|
|
|
#include <yajl/yajl_version.h>
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/* A datatype to pass through the callbacks to save the state */
|
2010-07-22 01:15:18 +02:00
|
|
|
struct workspaces_json_params {
|
2010-08-03 21:20:11 +02:00
|
|
|
struct ws_head *workspaces;
|
2014-06-15 19:07:02 +02:00
|
|
|
i3_ws *workspaces_walk;
|
|
|
|
char *cur_key;
|
|
|
|
char *json;
|
2010-07-22 01:15:18 +02:00
|
|
|
};
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
2011-08-12 23:09:59 +02:00
|
|
|
* Parse a boolean value (visible, focused, urgent)
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2011-08-17 00:05:05 +02:00
|
|
|
static int workspaces_boolean_cb(void *params_, int val) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
if (!strcmp(params->cur_key, "visible")) {
|
|
|
|
params->workspaces_walk->visible = val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
if (!strcmp(params->cur_key, "focused")) {
|
|
|
|
params->workspaces_walk->focused = val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
if (!strcmp(params->cur_key, "urgent")) {
|
|
|
|
params->workspaces_walk->urgent = val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
return 0;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
|
|
|
* Parse an integer (num or the rect)
|
|
|
|
*
|
|
|
|
*/
|
2011-04-28 20:23:12 +02:00
|
|
|
static int workspaces_integer_cb(void *params_, long long val) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
|
2010-08-03 21:20:11 +02:00
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "num")) {
|
2014-06-15 19:07:02 +02:00
|
|
|
params->workspaces_walk->num = (int)val;
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "x")) {
|
2014-06-15 19:07:02 +02:00
|
|
|
params->workspaces_walk->rect.x = (int)val;
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "y")) {
|
2014-06-15 19:07:02 +02:00
|
|
|
params->workspaces_walk->rect.y = (int)val;
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "width")) {
|
2014-06-15 19:07:02 +02:00
|
|
|
params->workspaces_walk->rect.w = (int)val;
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "height")) {
|
2014-06-15 19:07:02 +02:00
|
|
|
params->workspaces_walk->rect.h = (int)val;
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 0;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
|
|
|
* Parse a string (name, output)
|
|
|
|
*
|
|
|
|
*/
|
2011-04-28 20:23:12 +02:00
|
|
|
static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
if (!strcmp(params->cur_key, "name")) {
|
|
|
|
const char *ws_name = (const char *)val;
|
2015-05-06 16:28:29 +02:00
|
|
|
params->workspaces_walk->canonical_name = sstrndup(ws_name, len);
|
2014-05-05 19:56:47 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) {
|
|
|
|
/* Special case: strip off the workspace number */
|
|
|
|
static char ws_num[10];
|
2014-05-05 19:56:47 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num);
|
2014-05-05 19:56:47 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
/* Calculate the length of the number str in the name */
|
|
|
|
size_t offset = strspn(ws_name, ws_num);
|
2014-05-05 19:56:47 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
/* Also strip off the conventional ws name delimiter */
|
|
|
|
if (offset && ws_name[offset] == ':')
|
|
|
|
offset += 1;
|
2014-05-05 19:56:47 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
/* Offset may be equal to length, in which case display the number */
|
|
|
|
params->workspaces_walk->name = (offset < len
|
2015-02-12 20:45:34 +01:00
|
|
|
? i3string_from_markup_with_length(ws_name + offset, len - offset)
|
|
|
|
: i3string_from_markup(ws_num));
|
2014-05-05 19:56:47 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
} else {
|
|
|
|
/* Default case: just save the name */
|
2015-02-12 20:45:34 +01:00
|
|
|
params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len);
|
2014-06-15 19:07:02 +02:00
|
|
|
}
|
2010-08-06 03:32:05 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
/* Save its rendered width */
|
|
|
|
params->workspaces_walk->name_width =
|
|
|
|
predict_text_width(params->workspaces_walk->name);
|
2010-08-06 03:32:05 +02:00
|
|
|
|
2015-03-24 13:41:16 +01:00
|
|
|
DLOG("Got workspace canonical: %s, name: '%s', name_width: %d, glyphs: %zu\n",
|
2014-06-15 19:07:02 +02:00
|
|
|
params->workspaces_walk->canonical_name,
|
|
|
|
i3string_as_utf8(params->workspaces_walk->name),
|
|
|
|
params->workspaces_walk->name_width,
|
|
|
|
i3string_get_num_glyphs(params->workspaces_walk->name));
|
|
|
|
FREE(params->cur_key);
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2010-07-30 03:11:54 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
if (!strcmp(params->cur_key, "output")) {
|
|
|
|
/* We add the ws to the TAILQ of the output, it belongs to */
|
2015-10-17 22:14:48 +02:00
|
|
|
char *output_name = NULL;
|
|
|
|
sasprintf(&output_name, "%.*s", len, val);
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
i3_output *target = get_output_by_name(output_name);
|
2015-10-17 22:14:48 +02:00
|
|
|
if (target != NULL) {
|
2014-06-15 19:07:02 +02:00
|
|
|
params->workspaces_walk->output = target;
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
|
|
|
|
params->workspaces_walk,
|
|
|
|
tailq);
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
FREE(output_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
2015-03-23 20:56:49 +01:00
|
|
|
* We hit the start of a JSON map (rect or a new output)
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2010-08-03 21:20:11 +02:00
|
|
|
static int workspaces_start_map_cb(void *params_) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
|
2010-07-30 03:11:54 +02:00
|
|
|
|
2010-08-06 03:32:05 +02:00
|
|
|
i3_ws *new_workspace = NULL;
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
if (params->cur_key == NULL) {
|
2011-10-21 20:30:46 +02:00
|
|
|
new_workspace = smalloc(sizeof(i3_ws));
|
2010-08-03 21:20:11 +02:00
|
|
|
new_workspace->num = -1;
|
|
|
|
new_workspace->name = NULL;
|
|
|
|
new_workspace->visible = 0;
|
|
|
|
new_workspace->focused = 0;
|
|
|
|
new_workspace->urgent = 0;
|
|
|
|
memset(&new_workspace->rect, 0, sizeof(rect));
|
|
|
|
new_workspace->output = NULL;
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
params->workspaces_walk = new_workspace;
|
|
|
|
return 1;
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
return 1;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
|
|
|
* Parse a key.
|
|
|
|
*
|
2015-03-23 20:56:49 +01:00
|
|
|
* Essentially we just save it in the parsing state
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2011-04-28 20:23:12 +02:00
|
|
|
static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
2015-10-17 22:14:48 +02:00
|
|
|
sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
|
2010-08-03 21:20:11 +02:00
|
|
|
return 1;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/* A datastructure to pass all these callbacks to yajl */
|
2014-01-01 17:19:55 +01:00
|
|
|
static yajl_callbacks workspaces_callbacks = {
|
|
|
|
.yajl_boolean = workspaces_boolean_cb,
|
|
|
|
.yajl_integer = workspaces_integer_cb,
|
|
|
|
.yajl_string = workspaces_string_cb,
|
|
|
|
.yajl_start_map = workspaces_start_map_cb,
|
|
|
|
.yajl_map_key = workspaces_map_key_cb,
|
2010-07-22 01:15:18 +02:00
|
|
|
};
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
2015-03-23 20:56:49 +01:00
|
|
|
* Start parsing the received JSON string
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2010-08-03 21:20:11 +02:00
|
|
|
void parse_workspaces_json(char *json) {
|
2015-03-23 20:56:49 +01:00
|
|
|
/* FIXME: Fasciliate stream processing, i.e. allow starting to interpret
|
2010-08-03 21:20:11 +02:00
|
|
|
* JSON in chunks */
|
|
|
|
struct workspaces_json_params params;
|
|
|
|
|
|
|
|
free_workspaces();
|
|
|
|
|
|
|
|
params.workspaces_walk = NULL;
|
|
|
|
params.cur_key = NULL;
|
|
|
|
params.json = json;
|
|
|
|
|
|
|
|
yajl_handle handle;
|
|
|
|
yajl_status state;
|
2014-06-15 19:07:02 +02:00
|
|
|
handle = yajl_alloc(&workspaces_callbacks, NULL, (void *)¶ms);
|
2010-08-03 21:20:11 +02:00
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
|
2010-08-03 21:20:11 +02:00
|
|
|
|
2016-04-04 09:33:59 +02:00
|
|
|
/* FIXME: Proper error handling for JSON parsing */
|
2010-08-03 21:20:11 +02:00
|
|
|
switch (state) {
|
|
|
|
case yajl_status_ok:
|
|
|
|
break;
|
|
|
|
case yajl_status_client_canceled:
|
|
|
|
case yajl_status_error:
|
2015-03-24 13:41:16 +01:00
|
|
|
ELOG("Could not parse workspaces reply!\n");
|
2010-08-03 21:20:11 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
yajl_free(handle);
|
|
|
|
|
|
|
|
FREE(params.cur_key);
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
2015-03-23 20:56:49 +01:00
|
|
|
* free() all workspace data structures. Does not free() the heads of the tailqueues.
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2012-08-23 12:55:28 +02:00
|
|
|
void free_workspaces(void) {
|
2010-08-03 21:20:11 +02:00
|
|
|
i3_output *outputs_walk;
|
2011-01-26 01:21:36 +01:00
|
|
|
if (outputs == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-15 19:07:02 +02:00
|
|
|
i3_ws *ws_walk;
|
2011-01-26 00:34:15 +01:00
|
|
|
|
2014-06-19 11:20:32 +02:00
|
|
|
SLIST_FOREACH(outputs_walk, outputs, slist) {
|
2010-08-03 21:20:11 +02:00
|
|
|
if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
|
2014-06-19 11:20:32 +02:00
|
|
|
TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
|
2012-08-07 19:46:23 +02:00
|
|
|
I3STRING_FREE(ws_walk->name);
|
2014-05-05 19:56:47 +02:00
|
|
|
FREE(ws_walk->canonical_name);
|
2011-01-26 00:34:15 +01:00
|
|
|
}
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE_TAILQ(outputs_walk->workspaces, i3_ws);
|
|
|
|
}
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|