gri3-wm/i3bar/src/workspaces.c

279 lines
7.7 KiB
C
Raw Normal View History

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
* © 2010-2012 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
*
*/
2010-07-22 01:15:18 +02:00
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
2010-07-22 01:15:18 +02:00
#include <yajl/yajl_parse.h>
#include <yajl/yajl_version.h>
2010-07-22 01:15:18 +02:00
#include "common.h"
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;
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
*
*/
static int workspaces_boolean_cb(void *params_, int val) {
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)
*
*/
static int workspaces_integer_cb(void *params_, long long val) {
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
2010-08-03 21:20:11 +02:00
if (!strcmp(params->cur_key, "num")) {
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")) {
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")) {
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")) {
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")) {
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)
*
*/
static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
2010-07-22 01:15:18 +02:00
char *output_name;
2010-07-22 01:15:18 +02:00
if (!strcmp(params->cur_key, "name")) {
const char *ws_name = (const char *)val;
params->workspaces_walk->canonical_name = strndup(ws_name, len);
if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) {
/* Special case: strip off the workspace number */
static char ws_num[10];
snprintf(ws_num, sizeof(ws_num), "%d", params->workspaces_walk->num);
/* Calculate the length of the number str in the name */
size_t offset = strspn(ws_name, ws_num);
/* Also strip off the conventional ws name delimiter */
if (offset && ws_name[offset] == ':')
offset += 1;
/* Offset may be equal to length, in which case display the number */
params->workspaces_walk->name = (offset < len
? i3string_from_markup_with_length(ws_name + offset, len - offset)
: i3string_from_markup(ws_num));
} else {
/* Default case: just save the name */
params->workspaces_walk->name = i3string_from_markup_with_length(ws_name, len);
}
2010-08-06 03:32:05 +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",
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
return 1;
}
2010-07-30 03:11:54 +02:00
if (!strcmp(params->cur_key, "output")) {
/* We add the ws to the TAILQ of the output, it belongs to */
output_name = smalloc(sizeof(const unsigned char) * (len + 1));
strncpy(output_name, (const char *)val, len);
output_name[len] = '\0';
i3_output *target = get_output_by_name(output_name);
if (target) {
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
}
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_) {
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
*
*/
static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
struct workspaces_json_params *params = (struct workspaces_json_params *)params_;
2010-08-03 21:20:11 +02:00
FREE(params->cur_key);
2010-07-22 01:15:18 +02:00
2011-10-21 20:30:46 +02:00
params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
strncpy(params->cur_key, (const char *)keyVal, keyLen);
2010-08-03 21:20:11 +02:00
params->cur_key[keyLen] = '\0';
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
/* A datastructure to pass all these callbacks to yajl */
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;
handle = yajl_alloc(&workspaces_callbacks, NULL, (void *)&params);
2010-08-03 21:20:11 +02:00
state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
2010-08-03 21:20:11 +02:00
2015-03-24 13:41:16 +01:00
/* FIXME: Propper 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
*
*/
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;
}
i3_ws *ws_walk;
SLIST_FOREACH(outputs_walk, outputs, slist) {
2010-08-03 21:20:11 +02:00
if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
2012-08-07 19:46:23 +02:00
I3STRING_FREE(ws_walk->name);
FREE(ws_walk->canonical_name);
}
2010-08-03 21:20:11 +02:00
FREE_TAILQ(outputs_walk->workspaces, i3_ws);
}
}
2010-07-22 01:15:18 +02:00
}