* vim:ts=4:sw=4:expandtab
*
* i3bar - an xcb-based status- and ws-bar for i3
- * © 2010-2012 Axel Wagner and contributors (see also: LICENSE)
+ * © 2010 Axel Wagner and contributors (see also: LICENSE)
*
- * outputs.c: Maintaining the output-list
+ * outputs.c: Maintaining the outputs list
*
*/
+#include "common.h"
+
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <yajl/yajl_parse.h>
#include <yajl/yajl_version.h>
-#include "common.h"
-
/* A datatype to pass through the callbacks to save the state */
struct outputs_json_params {
struct outputs_head *outputs;
};
/*
- * Parse a null-value (current_workspace)
+ * Parse a null value (current_workspace)
*
*/
static int outputs_null_cb(void *params_) {
struct outputs_json_params *params = (struct outputs_json_params *)params_;
if (!strcmp(params->cur_key, "current_workspace")) {
- char *copy = smalloc(sizeof(const unsigned char) * (len + 1));
- strncpy(copy, (const char *)val, len);
- copy[len] = '\0';
+ char *copy = NULL;
+ sasprintf(©, "%.*s", len, val);
char *end;
errno = 0;
if (errno == 0 &&
(end && *end == '\0'))
params->outputs_walk->ws = parsed_num;
- free(copy);
+
+ FREE(copy);
FREE(params->cur_key);
return 1;
}
return 0;
}
- char *name = smalloc(sizeof(const unsigned char) * (len + 1));
- strncpy(name, (const char *)val, len);
- name[len] = '\0';
-
- params->outputs_walk->name = name;
+ sasprintf(&(params->outputs_walk->name), "%.*s", len, val);
FREE(params->cur_key);
-
return 1;
}
/*
- * We hit the start of a json-map (rect or a new output)
+ * We hit the start of a JSON map (rect or a new output)
*
*/
static int outputs_start_map_cb(void *params_) {
if (params->cur_key == NULL) {
new_output = smalloc(sizeof(i3_output));
new_output->name = NULL;
+ new_output->active = false;
+ new_output->primary = false;
+ new_output->visible = false;
new_output->ws = 0,
+ new_output->statusline_width = 0;
+ new_output->statusline_short_text = false;
memset(&new_output->rect, 0, sizeof(rect));
- new_output->bar = XCB_NONE;
+ memset(&new_output->bar, 0, sizeof(surface_t));
+ memset(&new_output->buffer, 0, sizeof(surface_t));
+ memset(&new_output->statusline_buffer, 0, sizeof(surface_t));
new_output->workspaces = smalloc(sizeof(struct ws_head));
TAILQ_INIT(new_output->workspaces);
return 1;
}
+static void clear_output(i3_output *output) {
+ FREE(output->name);
+ FREE(output->workspaces);
+ FREE(output->trayclients);
+}
+
/*
* We hit the end of a map (rect or a new output)
*
if (config.num_outputs > 0) {
bool handle_output = false;
for (int c = 0; c < config.num_outputs; c++) {
- if (strcasecmp(params->outputs_walk->name, config.outputs[c]) != 0)
- continue;
-
- handle_output = true;
- break;
+ if (strcasecmp(params->outputs_walk->name, config.outputs[c]) == 0 ||
+ (strcasecmp(config.outputs[c], "primary") == 0 &&
+ params->outputs_walk->primary)) {
+ handle_output = true;
+ break;
+ }
}
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);
+ clear_output(params->outputs_walk);
FREE(params->outputs_walk);
FREE(params->cur_key);
return 1;
target->primary = params->outputs_walk->primary;
target->ws = params->outputs_walk->ws;
target->rect = params->outputs_walk->rect;
+
+ clear_output(params->outputs_walk);
+ FREE(params->outputs_walk);
}
return 1;
}
/*
* Parse a key.
*
- * Essentially we just save it in the parsing-state
+ * Essentially we just save it in the parsing state
*
*/
static int outputs_map_key_cb(void *params_, const unsigned char *keyVal, size_t keyLen) {
struct outputs_json_params *params = (struct outputs_json_params *)params_;
FREE(params->cur_key);
-
- params->cur_key = smalloc(sizeof(unsigned char) * (keyLen + 1));
- strncpy(params->cur_key, (const char *)keyVal, keyLen);
- params->cur_key[keyLen] = '\0';
-
+ sasprintf(&(params->cur_key), "%.*s", keyLen, keyVal);
return 1;
}
};
/*
- * Initiate the output-list
+ * Initiate the outputs list
*
*/
void init_outputs(void) {
}
/*
- * Start parsing the received json-string
+ * Start parsing the received JSON string
*
*/
void parse_outputs_json(char *json) {
struct outputs_json_params params;
-
params.outputs_walk = NULL;
params.cur_key = NULL;
params.json = json;
state = yajl_parse(handle, (const unsigned char *)json, strlen(json));
- /* FIXME: Propper errorhandling for JSON-parsing */
+ /* FIXME: Proper errorhandling for JSON-parsing */
switch (state) {
case yajl_status_ok:
break;
case yajl_status_client_canceled:
case yajl_status_error:
- ELOG("Could not parse outputs-reply!\n");
+ ELOG("Could not parse outputs reply!\n");
exit(EXIT_FAILURE);
break;
}
yajl_free(handle);
}
+/*
+ * free() all outputs data structures.
+ *
+ */
+void free_outputs(void) {
+ free_workspaces();
+
+ i3_output *outputs_walk;
+ if (outputs == NULL) {
+ return;
+ }
+ SLIST_FOREACH(outputs_walk, outputs, slist) {
+ destroy_window(outputs_walk);
+ if (outputs_walk->trayclients != NULL && !TAILQ_EMPTY(outputs_walk->trayclients)) {
+ FREE_TAILQ(outputs_walk->trayclients, trayclient);
+ }
+ clear_output(outputs_walk);
+ }
+ FREE_SLIST(outputs, i3_output);
+}
+
/*
* Returns the output with the given name
*
return walk;
}
+
+/*
+ * 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;
+}