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
|
|
|
* outputs.c: Maintaining the outputs list
|
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>
|
2010-11-22 00:07:52 +01:00
|
|
|
#include <errno.h>
|
2010-07-23 04:43:43 +02:00
|
|
|
#include <i3/ipc.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 outputs_json_params {
|
2010-08-03 21:20:11 +02:00
|
|
|
struct outputs_head *outputs;
|
2014-06-15 19:07:02 +02:00
|
|
|
i3_output *outputs_walk;
|
|
|
|
char *cur_key;
|
|
|
|
char *json;
|
|
|
|
bool in_rect;
|
2010-07-22 01:15:18 +02:00
|
|
|
};
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
2015-03-23 20:56:49 +01:00
|
|
|
* Parse a null value (current_workspace)
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2010-08-03 21:20:11 +02:00
|
|
|
static int outputs_null_cb(void *params_) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct outputs_json_params *params = (struct outputs_json_params *)params_;
|
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 1;
|
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 (active)
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2011-08-17 00:05:05 +02:00
|
|
|
static int outputs_boolean_cb(void *params_, int val) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct outputs_json_params *params = (struct outputs_json_params *)params_;
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2012-04-01 16:07:15 +02:00
|
|
|
if (!strcmp(params->cur_key, "active")) {
|
|
|
|
params->outputs_walk->active = val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
2010-08-03 21:20:11 +02:00
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2012-04-01 16:07:15 +02:00
|
|
|
if (!strcmp(params->cur_key, "primary")) {
|
|
|
|
params->outputs_walk->primary = val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2012-04-01 16:07:15 +02:00
|
|
|
return 0;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
|
|
|
* Parse an integer (current_workspace or the rect)
|
|
|
|
*
|
|
|
|
*/
|
2011-04-28 20:23:12 +02:00
|
|
|
static int outputs_integer_cb(void *params_, long long val) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct outputs_json_params *params = (struct outputs_json_params *)params_;
|
2010-08-03 21:20:11 +02:00
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "current_workspace")) {
|
2014-06-15 19:07:02 +02:00
|
|
|
params->outputs_walk->ws = (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->outputs_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->outputs_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->outputs_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->outputs_walk->rect.h = (int)val;
|
2010-08-03 21:20:11 +02:00
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
|
|
|
* Parse a string (name)
|
|
|
|
*
|
|
|
|
*/
|
2011-04-28 20:23:12 +02:00
|
|
|
static int outputs_string_cb(void *params_, const unsigned char *val, size_t len) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct outputs_json_params *params = (struct outputs_json_params *)params_;
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-11-22 00:07:52 +01:00
|
|
|
if (!strcmp(params->cur_key, "current_workspace")) {
|
2015-10-17 22:14:48 +02:00
|
|
|
char *copy = NULL;
|
|
|
|
sasprintf(©, "%.*s", len, val);
|
2010-11-22 00:07:52 +01:00
|
|
|
|
|
|
|
char *end;
|
|
|
|
errno = 0;
|
|
|
|
long parsed_num = strtol(copy, &end, 10);
|
|
|
|
if (errno == 0 &&
|
|
|
|
(end && *end == '\0'))
|
|
|
|
params->outputs_walk->ws = parsed_num;
|
2015-10-17 22:14:48 +02:00
|
|
|
|
|
|
|
FREE(copy);
|
2010-11-22 00:07:52 +01:00
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
if (strcmp(params->cur_key, "name")) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2015-10-17 22:14:48 +02:00
|
|
|
sasprintf(&(params->outputs_walk->name), "%.*s", len, val);
|
2010-08-03 21:20:11 +02:00
|
|
|
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
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 outputs_start_map_cb(void *params_) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct outputs_json_params *params = (struct outputs_json_params *)params_;
|
2010-08-03 21:20:11 +02:00
|
|
|
i3_output *new_output = 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_output = smalloc(sizeof(i3_output));
|
2010-08-03 21:20:11 +02:00
|
|
|
new_output->name = NULL;
|
2015-10-26 16:27:09 +01:00
|
|
|
new_output->active = false;
|
|
|
|
new_output->primary = false;
|
|
|
|
new_output->visible = false;
|
2010-08-03 21:20:11 +02:00
|
|
|
new_output->ws = 0,
|
2015-10-26 16:27:09 +01:00
|
|
|
new_output->statusline_width = 0;
|
|
|
|
new_output->statusline_short_text = false;
|
2010-08-03 21:20:11 +02:00
|
|
|
memset(&new_output->rect, 0, sizeof(rect));
|
2015-09-05 23:38:15 +02:00
|
|
|
memset(&new_output->bar, 0, sizeof(surface_t));
|
|
|
|
memset(&new_output->buffer, 0, sizeof(surface_t));
|
2015-10-26 16:27:09 +01:00
|
|
|
memset(&new_output->statusline_buffer, 0, sizeof(surface_t));
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2011-10-21 20:30:46 +02:00
|
|
|
new_output->workspaces = smalloc(sizeof(struct ws_head));
|
2010-08-03 21:20:11 +02:00
|
|
|
TAILQ_INIT(new_output->workspaces);
|
2010-07-30 03:11:54 +02:00
|
|
|
|
2011-10-21 20:30:46 +02:00
|
|
|
new_output->trayclients = smalloc(sizeof(struct tc_head));
|
2011-08-15 15:57:52 +02:00
|
|
|
TAILQ_INIT(new_output->trayclients);
|
|
|
|
|
2010-08-06 03:32:05 +02:00
|
|
|
params->outputs_walk = new_output;
|
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
|
|
|
|
2011-10-21 23:17:41 +02:00
|
|
|
if (!strcmp(params->cur_key, "rect")) {
|
|
|
|
params->in_rect = true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
* We hit the end of a map (rect or a new output)
|
|
|
|
*
|
|
|
|
*/
|
2010-08-06 03:32:05 +02:00
|
|
|
static int outputs_end_map_cb(void *params_) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct outputs_json_params *params = (struct outputs_json_params *)params_;
|
2011-10-21 23:17:41 +02:00
|
|
|
if (params->in_rect) {
|
|
|
|
params->in_rect = false;
|
|
|
|
/* Ignore the end of a rect */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if we actually handle that output */
|
|
|
|
if (config.num_outputs > 0) {
|
|
|
|
bool handle_output = false;
|
|
|
|
for (int c = 0; c < config.num_outputs; c++) {
|
2017-03-12 21:17:12 +01:00
|
|
|
if (strcasecmp(params->outputs_walk->name, config.outputs[c]) == 0 ||
|
|
|
|
(strcasecmp(config.outputs[c], "primary") == 0 &&
|
|
|
|
params->outputs_walk->primary)) {
|
|
|
|
handle_output = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-21 23:17:41 +02:00
|
|
|
}
|
|
|
|
if (!handle_output) {
|
|
|
|
DLOG("Ignoring output \"%s\", not configured to handle it.\n",
|
|
|
|
params->outputs_walk->name);
|
|
|
|
FREE(params->outputs_walk->name);
|
|
|
|
FREE(params->outputs_walk->workspaces);
|
|
|
|
FREE(params->outputs_walk->trayclients);
|
|
|
|
FREE(params->outputs_walk);
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2010-08-06 03:32:05 +02:00
|
|
|
|
|
|
|
i3_output *target = get_output_by_name(params->outputs_walk->name);
|
|
|
|
|
|
|
|
if (target == NULL) {
|
|
|
|
SLIST_INSERT_HEAD(outputs, params->outputs_walk, slist);
|
|
|
|
} else {
|
2010-08-06 05:49:57 +02:00
|
|
|
target->active = params->outputs_walk->active;
|
2012-04-22 20:43:43 +02:00
|
|
|
target->primary = params->outputs_walk->primary;
|
2010-08-06 03:32:05 +02:00
|
|
|
target->ws = params->outputs_walk->ws;
|
|
|
|
target->rect = params->outputs_walk->rect;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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 outputs_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
|
2014-06-15 19:07:02 +02:00
|
|
|
struct outputs_json_params *params = (struct outputs_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 outputs_callbacks = {
|
|
|
|
.yajl_null = outputs_null_cb,
|
|
|
|
.yajl_boolean = outputs_boolean_cb,
|
|
|
|
.yajl_integer = outputs_integer_cb,
|
|
|
|
.yajl_string = outputs_string_cb,
|
|
|
|
.yajl_start_map = outputs_start_map_cb,
|
|
|
|
.yajl_map_key = outputs_map_key_cb,
|
|
|
|
.yajl_end_map = outputs_end_map_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
|
|
|
* Initiate the outputs list
|
2010-08-07 02:10:05 +02:00
|
|
|
*
|
|
|
|
*/
|
2012-08-23 12:55:28 +02:00
|
|
|
void init_outputs(void) {
|
2011-10-21 20:30:46 +02:00
|
|
|
outputs = smalloc(sizeof(struct outputs_head));
|
2010-08-06 03:32:05 +02:00
|
|
|
SLIST_INIT(outputs);
|
|
|
|
}
|
|
|
|
|
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_outputs_json(char *json) {
|
|
|
|
struct outputs_json_params params;
|
|
|
|
|
|
|
|
params.outputs_walk = NULL;
|
|
|
|
params.cur_key = NULL;
|
|
|
|
params.json = json;
|
2011-10-21 23:17:41 +02:00
|
|
|
params.in_rect = false;
|
2010-08-03 21:20:11 +02:00
|
|
|
|
|
|
|
yajl_handle handle;
|
|
|
|
yajl_status state;
|
2014-06-15 19:07:02 +02:00
|
|
|
handle = yajl_alloc(&outputs_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 errorhandling 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-23 20:56:49 +01:00
|
|
|
ELOG("Could not parse outputs reply!\n");
|
2010-08-03 21:20:11 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
yajl_free(handle);
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
|
|
|
|
2010-08-07 02:10:05 +02:00
|
|
|
/*
|
|
|
|
* Returns the output with the given name
|
|
|
|
*
|
|
|
|
*/
|
2010-08-03 21:20:11 +02:00
|
|
|
i3_output *get_output_by_name(char *name) {
|
|
|
|
i3_output *walk;
|
2010-08-06 03:32:05 +02:00
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-19 11:20:32 +02:00
|
|
|
SLIST_FOREACH(walk, outputs, slist) {
|
2010-08-03 21:20:11 +02:00
|
|
|
if (!strcmp(walk->name, name)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-22 01:15:18 +02:00
|
|
|
|
2010-08-03 21:20:11 +02:00
|
|
|
return walk;
|
2010-07-22 01:15:18 +02:00
|
|
|
}
|
2015-10-26 21:55:01 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the output has the currently focused workspace
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool output_has_focus(i3_output *output) {
|
|
|
|
i3_ws *ws_walk;
|
|
|
|
TAILQ_FOREACH(ws_walk, output->workspaces, tailq) {
|
|
|
|
if (ws_walk->focused) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|