13 #include <yajl/yajl_common.h>
14 #include <yajl/yajl_gen.h>
15 #include <yajl/yajl_parse.h>
16 #include <yajl/yajl_version.h>
40 static
int json_start_map(
void *ctx) {
43 LOG(
"creating new swallow\n");
50 DLOG(
"New floating_node\n");
52 json_node =
con_new(NULL, NULL);
54 DLOG(
"Parent is workspace = %p\n", ws);
57 json_node =
con_new(NULL, NULL);
58 json_node->
parent = parent;
70 json_node = json_node->
parent;
82 LOG(
"end of array\n");
88 LOG(
"focus (reverse) %d\n", mapping->
old_id);
93 LOG(
"got it! %p\n", con);
111 static int json_key(
void *ctx,
const unsigned char *val,
unsigned int len) {
113 static int json_key(
void *ctx,
const unsigned char *val,
size_t len) {
115 LOG(
"key: %.*s\n", (
int)len, val);
119 if (strcasecmp(
last_key,
"swallows") == 0)
122 if (strcasecmp(
last_key,
"rect") == 0)
125 if (strcasecmp(
last_key,
"window_rect") == 0)
128 if (strcasecmp(
last_key,
"geometry") == 0)
131 if (strcasecmp(
last_key,
"focus") == 0)
138 static int json_string(
void *ctx,
const unsigned char *val,
size_t len) {
140 static int json_string(
void *ctx,
const unsigned char *val,
unsigned int len) {
142 LOG(
"string: %.*s for key %s\n", len, val,
last_key);
145 if (strcasecmp(
last_key,
"class") == 0) {
146 current_swallow->
class =
scalloc((len+1) *
sizeof(
char));
147 memcpy(current_swallow->
class, val, len);
149 LOG(
"unhandled yet: swallow\n");
151 if (strcasecmp(
last_key,
"name") == 0) {
153 memcpy(json_node->
name, val, len);
154 }
else if (strcasecmp(
last_key,
"sticky_group") == 0) {
157 LOG(
"sticky_group of this container is %s\n", json_node->
sticky_group);
158 }
else if (strcasecmp(
last_key,
"orientation") == 0) {
161 if (strcasecmp(buf,
"none") == 0)
163 else if (strcasecmp(buf,
"horizontal") == 0)
165 else if (strcasecmp(buf,
"vertical") == 0)
167 else LOG(
"Unhandled orientation: %s\n", buf);
169 }
else if (strcasecmp(
last_key,
"border") == 0) {
172 if (strcasecmp(buf,
"none") == 0)
174 else if (strcasecmp(buf,
"1pixel") == 0)
176 else if (strcasecmp(buf,
"normal") == 0)
178 else LOG(
"Unhandled \"border\": %s\n", buf);
180 }
else if (strcasecmp(
last_key,
"layout") == 0) {
183 if (strcasecmp(buf,
"default") == 0)
184 json_node->
layout = L_DEFAULT;
185 else if (strcasecmp(buf,
"stacked") == 0)
186 json_node->
layout = L_STACKED;
187 else if (strcasecmp(buf,
"tabbed") == 0)
188 json_node->
layout = L_TABBED;
189 else if (strcasecmp(buf,
"dockarea") == 0)
190 json_node->
layout = L_DOCKAREA;
191 else if (strcasecmp(buf,
"output") == 0)
192 json_node->
layout = L_OUTPUT;
193 else LOG(
"Unhandled \"layout\": %s\n", buf);
195 }
else if (strcasecmp(
last_key,
"mark") == 0) {
198 json_node->
mark = buf;
199 }
else if (strcasecmp(
last_key,
"floating") == 0) {
202 if (strcasecmp(buf,
"auto_off") == 0)
203 json_node->
floating = FLOATING_AUTO_OFF;
204 else if (strcasecmp(buf,
"auto_on") == 0)
205 json_node->
floating = FLOATING_AUTO_ON;
206 else if (strcasecmp(buf,
"user_off") == 0)
207 json_node->
floating = FLOATING_USER_OFF;
208 else if (strcasecmp(buf,
"user_on") == 0)
209 json_node->
floating = FLOATING_USER_ON;
211 }
else if (strcasecmp(
last_key,
"scratchpad_state") == 0) {
214 if (strcasecmp(buf,
"none") == 0)
216 else if (strcasecmp(buf,
"fresh") == 0)
218 else if (strcasecmp(buf,
"changed") == 0)
227 static int json_int(
void *ctx,
long long val) {
233 if (strcasecmp(
last_key,
"type") == 0)
234 json_node->
type = val;
236 if (strcasecmp(
last_key,
"fullscreen_mode") == 0)
239 if (strcasecmp(
last_key,
"num") == 0)
240 json_node->
num = val;
247 focus_mapping->
old_id = val;
254 r = &(json_node->
rect);
260 else if (strcasecmp(
last_key,
"y") == 0)
262 else if (strcasecmp(
last_key,
"width") == 0)
264 else if (strcasecmp(
last_key,
"height") == 0)
266 else printf(
"WARNING: unknown key %s in rect\n",
last_key);
267 printf(
"rect now: (%d, %d, %d, %d)\n",
271 if (strcasecmp(
last_key,
"id") == 0) {
272 current_swallow->
id = val;
274 if (strcasecmp(
last_key,
"dock") == 0) {
275 current_swallow->
dock = val;
277 if (strcasecmp(
last_key,
"insert_where") == 0) {
287 if (strcasecmp(
last_key,
"focused") == 0 && val) {
292 if (strcasecmp(
last_key,
"restart_mode") == 0)
301 if (strcasecmp(
last_key,
"percent") == 0) {
310 if ((f = fopen(filename,
"r")) == NULL) {
311 LOG(
"Cannot open file\n");
314 char *buf = malloc(65535);
315 int n = fread(buf, 1, 65535, f);
316 LOG(
"read %d bytes\n", n);
319 yajl_callbacks callbacks;
320 memset(&callbacks,
'\0',
sizeof(yajl_callbacks));
321 callbacks.yajl_start_map = json_start_map;
330 g = yajl_gen_alloc(NULL);
331 hand = yajl_alloc(&callbacks, NULL, (
void*)g);
333 g = yajl_gen_alloc(NULL, NULL);
334 hand = yajl_alloc(&callbacks, NULL, NULL, (
void*)g);
342 setlocale(LC_NUMERIC,
"C");
343 stat = yajl_parse(hand, (
const unsigned char*)buf, n);
344 if (stat != yajl_status_ok)
346 unsigned char * str = yajl_get_error(hand, 1, (
const unsigned char*)buf, n);
347 fprintf(stderr,
"%s\n", (
const char *) str);
348 yajl_free_error(hand, str);
351 setlocale(LC_NUMERIC,
"");
353 yajl_complete_parse(hand);
355 yajl_parse_complete(hand);