2010-07-21 19:15:18 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2010-07-22 22:43:43 -04:00
|
|
|
#include <i3/ipc.h>
|
2010-07-21 19:15:18 -04:00
|
|
|
#include <yajl/yajl_parse.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
struct outputs_json_params {
|
2010-08-03 15:20:11 -04:00
|
|
|
struct outputs_head *outputs;
|
|
|
|
i3_output *outputs_walk;
|
|
|
|
char *cur_key;
|
|
|
|
char *json;
|
2010-08-05 21:32:05 -04:00
|
|
|
bool init;
|
2010-07-21 19:15:18 -04:00
|
|
|
};
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
static int outputs_null_cb(void *params_) {
|
|
|
|
struct outputs_json_params *params = (struct outputs_json_params*) params_;
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
if (strcmp(params->cur_key, "current_workspace")) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
FREE(params->cur_key);
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
return 1;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
static int outputs_boolean_cb(void *params_, bool val) {
|
|
|
|
struct outputs_json_params *params = (struct outputs_json_params*) params_;
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
if (strcmp(params->cur_key, "active")) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
params->outputs_walk->active = val;
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
FREE(params->cur_key);
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
return 1;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
static int outputs_integer_cb(void *params_, long val) {
|
|
|
|
struct outputs_json_params *params = (struct outputs_json_params*) params_;
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "current_workspace")) {
|
|
|
|
params->outputs_walk->ws = (int) val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "x")) {
|
|
|
|
params->outputs_walk->rect.x = (int) val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "y")) {
|
|
|
|
params->outputs_walk->rect.y = (int) val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "width")) {
|
|
|
|
params->outputs_walk->rect.w = (int) val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(params->cur_key, "height")) {
|
|
|
|
params->outputs_walk->rect.h = (int) val;
|
|
|
|
FREE(params->cur_key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
static int outputs_string_cb(void *params_, const unsigned char *val, unsigned int len) {
|
|
|
|
struct outputs_json_params *params = (struct outputs_json_params*) params_;
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
if (strcmp(params->cur_key, "name")) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
char *name = malloc(sizeof(const unsigned char) * (len + 1));
|
|
|
|
strncpy(name, (const char*) val, len);
|
|
|
|
name[len] = '\0';
|
|
|
|
|
|
|
|
params->outputs_walk->name = name;
|
2010-08-03 15:20:11 -04:00
|
|
|
|
|
|
|
FREE(params->cur_key);
|
|
|
|
|
|
|
|
return 1;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
static int outputs_start_map_cb(void *params_) {
|
|
|
|
struct outputs_json_params *params = (struct outputs_json_params*) params_;
|
|
|
|
i3_output *new_output = NULL;
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
if (params->cur_key == NULL) {
|
|
|
|
new_output = malloc(sizeof(i3_output));
|
|
|
|
new_output->name = NULL;
|
|
|
|
new_output->ws = 0,
|
|
|
|
memset(&new_output->rect, 0, sizeof(rect));
|
|
|
|
new_output->bar = XCB_NONE;
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
new_output->workspaces = malloc(sizeof(struct ws_head));
|
|
|
|
TAILQ_INIT(new_output->workspaces);
|
2010-07-29 21:11:54 -04:00
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
params->outputs_walk = new_output;
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
return 1;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
static int outputs_end_map_cb(void *params_) {
|
|
|
|
struct outputs_json_params *params = (struct outputs_json_params*) params_;
|
|
|
|
|
|
|
|
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-05 23:49:57 -04:00
|
|
|
target->active = params->outputs_walk->active;
|
2010-08-05 21:32:05 -04:00
|
|
|
target->ws = params->outputs_walk->ws;
|
|
|
|
target->rect = params->outputs_walk->rect;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
static int outputs_map_key_cb(void *params_, const unsigned char *keyVal, unsigned int keyLen) {
|
|
|
|
struct outputs_json_params *params = (struct outputs_json_params*) params_;
|
|
|
|
FREE(params->cur_key);
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
|
|
|
|
strncpy(params->cur_key, (const char*) keyVal, keyLen);
|
|
|
|
params->cur_key[keyLen] = '\0';
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
return 1;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
yajl_callbacks outputs_callbacks = {
|
2010-08-03 15:20:11 -04:00
|
|
|
&outputs_null_cb,
|
|
|
|
&outputs_boolean_cb,
|
|
|
|
&outputs_integer_cb,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&outputs_string_cb,
|
|
|
|
&outputs_start_map_cb,
|
|
|
|
&outputs_map_key_cb,
|
2010-08-05 21:32:05 -04:00
|
|
|
&outputs_end_map_cb,
|
2010-08-03 15:20:11 -04:00
|
|
|
NULL,
|
|
|
|
NULL
|
2010-07-21 19:15:18 -04:00
|
|
|
};
|
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
void init_outputs() {
|
|
|
|
outputs = malloc(sizeof(struct outputs_head));
|
|
|
|
SLIST_INIT(outputs);
|
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
void parse_outputs_json(char *json) {
|
|
|
|
struct outputs_json_params params;
|
|
|
|
|
|
|
|
params.outputs_walk = NULL;
|
|
|
|
params.cur_key = NULL;
|
|
|
|
params.json = json;
|
|
|
|
|
|
|
|
yajl_handle handle;
|
|
|
|
yajl_parser_config parse_conf = { 0, 0 };
|
|
|
|
yajl_status state;
|
|
|
|
|
|
|
|
handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, (void*) ¶ms);
|
|
|
|
|
|
|
|
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:
|
|
|
|
case yajl_status_insufficient_data:
|
|
|
|
case yajl_status_error:
|
|
|
|
printf("ERROR: Could not parse outputs-reply!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
yajl_free(handle);
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
i3_output *get_output_by_name(char *name) {
|
|
|
|
i3_output *walk;
|
2010-08-05 21:32:05 -04:00
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-03 15:20:11 -04:00
|
|
|
SLIST_FOREACH(walk, outputs, slist) {
|
|
|
|
if (!strcmp(walk->name, name)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-21 19:15:18 -04:00
|
|
|
|
2010-08-03 15:20:11 -04:00
|
|
|
return walk;
|
2010-07-21 19:15:18 -04:00
|
|
|
}
|