gri3-wm/i3bar/src/workspaces.c

281 lines
7.3 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
*
* 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) {
2010-08-03 21:20:11 +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)
*
*/
#if YAJL_MAJOR >= 2
static int workspaces_integer_cb(void *params_, long long val) {
#else
2010-08-03 21:20:11 +02:00
static int workspaces_integer_cb(void *params_, long val) {
#endif
2010-08-03 21:20:11 +02:00
struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
if (!strcmp(params->cur_key, "num")) {
params->workspaces_walk->num = (int) val;
FREE(params->cur_key);
return 1;
}
if (!strcmp(params->cur_key, "x")) {
params->workspaces_walk->rect.x = (int) val;
FREE(params->cur_key);
return 1;
}
if (!strcmp(params->cur_key, "y")) {
params->workspaces_walk->rect.y = (int) val;
FREE(params->cur_key);
return 1;
}
if (!strcmp(params->cur_key, "width")) {
params->workspaces_walk->rect.w = (int) val;
FREE(params->cur_key);
return 1;
}
if (!strcmp(params->cur_key, "height")) {
params->workspaces_walk->rect.h = (int) val;
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)
*
*/
#if YAJL_MAJOR >= 2
static int workspaces_string_cb(void *params_, const unsigned char *val, size_t len) {
#else
2010-08-03 21:20:11 +02:00
static int workspaces_string_cb(void *params_, const unsigned char *val, unsigned int len) {
#endif
2010-08-03 21:20:11 +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
char *output_name;
2010-07-22 01:15:18 +02:00
if (!strcmp(params->cur_key, "name")) {
2010-08-07 02:10:05 +02:00
/* Save the name */
2012-08-07 19:46:23 +02:00
params->workspaces_walk->name = i3string_from_utf8_with_length((const char *)val, len);
2010-08-06 03:32:05 +02:00
/* Save its rendered width */
2010-09-17 03:11:49 +02:00
params->workspaces_walk->name_width =
predict_text_width(params->workspaces_walk->name);
2010-08-06 03:32:05 +02:00
2012-08-07 19:46:23 +02:00
DLOG("Got Workspace %s, name_width: %d, glyphs: %zu\n",
i3string_as_utf8(params->workspaces_walk->name),
params->workspaces_walk->name_width,
2012-08-07 19:46:23 +02:00
i3string_get_num_glyphs(params->workspaces_walk->name));
2010-08-06 03:32:05 +02:00
FREE(params->cur_key);
return 1;
2010-07-22 01:15:18 +02:00
}
if (!strcmp(params->cur_key, "output")) {
2010-08-07 02:10:05 +02:00
/* We add the ws to the TAILQ of the output, it belongs to */
2011-10-21 20:30:46 +02:00
output_name = smalloc(sizeof(const unsigned char) * (len + 1));
2010-08-06 03:32:05 +02:00
strncpy(output_name, (const char*) val, len);
output_name[len] = '\0';
2011-10-21 23:17:41 +02:00
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-30 03:11:54 +02:00
2010-08-06 03:32:05 +02:00
FREE(output_name);
return 1;
2010-07-22 01:15:18 +02:00
}
return 0;
}
2010-08-07 02:10:05 +02:00
/*
* We hit the start of a json-map (rect or a new output)
*
*/
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.
*
* Essentially we just save it in the parsing-state
*
*/
#if YAJL_MAJOR >= 2
static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
#else
2010-08-03 21:20:11 +02:00
static int workspaces_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
#endif
2010-08-03 21:20:11 +02:00
struct workspaces_json_params *params = (struct workspaces_json_params*) params_;
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));
2010-08-03 21:20:11 +02:00
strncpy(params->cur_key, (const char*) keyVal, keyLen);
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 */
2010-07-22 01:15:18 +02:00
yajl_callbacks workspaces_callbacks = {
NULL,
2010-08-03 21:20:11 +02:00
&workspaces_boolean_cb,
&workspaces_integer_cb,
NULL,
NULL,
&workspaces_string_cb,
&workspaces_start_map_cb,
&workspaces_map_key_cb,
NULL,
NULL,
NULL
2010-07-22 01:15:18 +02:00
};
2010-08-07 02:10:05 +02:00
/*
* Start parsing the received json-string
*
*/
2010-08-03 21:20:11 +02:00
void parse_workspaces_json(char *json) {
/* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
* 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;
#if YAJL_MAJOR < 2
yajl_parser_config parse_conf = { 0, 0 };
2010-08-03 21:20:11 +02:00
handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) &params);
#else
handle = yajl_alloc(&workspaces_callbacks, NULL, (void*) &params);
#endif
2010-08-03 21:20:11 +02:00
state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
/* FIXME: Propper errorhandling for JSON-parsing */
switch (state) {
case yajl_status_ok:
break;
case yajl_status_client_canceled:
#if YAJL_MAJOR < 2
2010-08-03 21:20:11 +02:00
case yajl_status_insufficient_data:
#endif
2010-08-03 21:20:11 +02:00
case yajl_status_error:
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
/*
* 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;
2010-08-03 21:20:11 +02:00
SLIST_FOREACH(outputs_walk, outputs, slist) {
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);
}
2010-08-03 21:20:11 +02:00
FREE_TAILQ(outputs_walk->workspaces, i3_ws);
}
}
2010-07-22 01:15:18 +02:00
}