summaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar crondog <crondog@gmail.com>2015-12-22 22:36:57 +1100
committerLibravatar crondog <crondog@gmail.com>2015-12-22 22:36:57 +1100
commitd1f6f45cb38cc7aa238afcb0be5445b348a6c751 (patch)
treeeeec9dec518a3f445b9bc8a680dba6cfd467d1bf /swaybar
parentMerge pull request #393 from robotanarchy/musl-libc-compatibility (diff)
downloadsway-d1f6f45cb38cc7aa238afcb0be5445b348a6c751.tar.gz
sway-d1f6f45cb38cc7aa238afcb0be5445b348a6c751.tar.zst
sway-d1f6f45cb38cc7aa238afcb0be5445b348a6c751.zip
Make start on i3bar json parsing
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/main.c148
1 files changed, 143 insertions, 5 deletions
diff --git a/swaybar/main.c b/swaybar/main.c
index 4323d370..c568f6e9 100644
--- a/swaybar/main.c
+++ b/swaybar/main.c
@@ -45,6 +45,18 @@ struct workspace {
45 bool urgent; 45 bool urgent;
46}; 46};
47 47
48struct status_block {
49 char *full_text, *short_text, *align;
50 bool urgent;
51 uint32_t color;
52 int min_width;
53 char *name, *instance;
54 bool separator;
55 int separator_block_width;
56};
57
58list_t *status_line = NULL;
59
48list_t *workspaces = NULL; 60list_t *workspaces = NULL;
49int socketfd; 61int socketfd;
50pid_t pid; 62pid_t pid;
@@ -362,11 +374,22 @@ void render() {
362 // Command output 374 // Command output
363 cairo_set_source_u32(window->cairo, colors.statusline); 375 cairo_set_source_u32(window->cairo, colors.statusline);
364 int width, height; 376 int width, height;
365 get_text_size(window, &width, &height, "%s", line);
366
367 cairo_move_to(window->cairo, window->width - margin - width, margin);
368 pango_printf(window, "%s", line);
369 377
378 if (status_line) {
379 int i;
380 int moved = 0;
381 for ( i = status_line->length - 1; i >= 0; --i ) {
382 struct status_block *block = status_line->items[i];
383 if (block->full_text) {
384 get_text_size(window, &width, &height, "%s", block->full_text);
385 moved += width + block->separator_block_width;
386 cairo_move_to(window->cairo, window->width - margin - moved, margin);
387 cairo_set_source_u32(window->cairo, block->color);
388 pango_printf(window, "%s", block->full_text);
389 }
390 }
391 }
392
370 // Workspaces 393 // Workspaces
371 cairo_set_line_width(window->cairo, 1.0); 394 cairo_set_line_width(window->cairo, 1.0);
372 double x = 0.5; 395 double x = 0.5;
@@ -401,6 +424,116 @@ void render() {
401 } 424 }
402} 425}
403 426
427void parse_json(const char *text) {
428
429/*
430 * {
431 "full_text": "E: 10.0.0.1 (1000 Mbit/s)",
432 "short_text": "10.0.0.1",
433 "color": "#00ff00",
434 "min_width": 300,
435 "align": "right",
436 "urgent": false,
437 "name": "ethernet",
438 "instance": "eth0",
439 "separator": true,
440 "separator_block_width": 9
441}
442 *
443 *
444 *
445 * */
446
447 json_object *result = json_tokener_parse(text);
448 if (!result) {
449 sway_log(L_DEBUG, "xxx Failed to parse json");
450 return;
451 }
452
453 if (json_object_array_length(result) < 1) {
454 return;
455 }
456
457 if (status_line) {
458 free_flat_list(status_line);
459 }
460
461 status_line = create_list();
462
463
464 int i;
465 for (i = 0; i < json_object_array_length(result); ++i) {
466 json_object *full_text, *short_text, *color, *min_width, *align, *urgent;
467 json_object *name, *instance, *separator, *separator_block_width;
468
469 json_object *json = json_object_array_get_idx(result, i);
470 if (!json) {
471 continue;
472 }
473
474 json_object_object_get_ex(json, "full_text", &full_text);
475 json_object_object_get_ex(json, "short_text", &short_text);
476 json_object_object_get_ex(json, "color", &color);
477 json_object_object_get_ex(json, "min_width", &min_width);
478 json_object_object_get_ex(json, "align", &align);
479 json_object_object_get_ex(json, "urgent", &urgent);
480 json_object_object_get_ex(json, "name", &name);
481 json_object_object_get_ex(json, "instance", &instance);
482 json_object_object_get_ex(json, "separator", &separator);
483 json_object_object_get_ex(json, "separator_block_width", &separator_block_width);
484
485 struct status_block *new = malloc(sizeof(struct status_block));
486 memset(new, 0, sizeof(struct status_block));
487
488 if (full_text) {
489 new->full_text = strdup(json_object_get_string(full_text));
490 }
491
492 if (short_text) {
493 new->short_text = strdup(json_object_get_string(short_text));
494 }
495
496 if (color) {
497 new->color = parse_color(json_object_get_string(color));
498 }
499 else {
500 new->color = 0xFFFFFFFF;
501 }
502
503 if (min_width) {
504 new->min_width = json_object_get_int(min_width);
505 }
506 if (align) {
507 new->align = strdup(json_object_get_string(align));
508 }
509 if (urgent) {
510 new->urgent = json_object_get_int(urgent);
511 }
512 if (name) {
513 new->name = strdup(json_object_get_string(name));
514 }
515 if (instance) {
516 new->instance = strdup(json_object_get_string(instance));
517 }
518 if (separator) {
519 new->separator = json_object_get_int(separator);
520 }
521 else {
522 new->separator = true; // i3bar spec
523 }
524 if (separator_block_width) {
525 new->separator_block_width = json_object_get_int(separator_block_width);
526 }
527 else {
528 new->separator_block_width = 9; // i3bar spec
529 }
530 list_add(status_line, new);
531
532 }
533
534
535}
536
404void poll_for_update() { 537void poll_for_update() {
405 fd_set readfds; 538 fd_set readfds;
406 int activity; 539 int activity;
@@ -437,17 +570,22 @@ void poll_for_update() {
437 if (status_command && FD_ISSET(pipefd[0], &readfds)) { 570 if (status_command && FD_ISSET(pipefd[0], &readfds)) {
438 sway_log(L_DEBUG, "Got update from status command."); 571 sway_log(L_DEBUG, "Got update from status command.");
439 fgets(line, sizeof(line), command); 572 fgets(line, sizeof(line), command);
573 sway_log(L_DEBUG, "zzz %s", line);
440 int l = strlen(line) - 1; 574 int l = strlen(line) - 1;
441 if (line[l] == '\n') { 575 if (line[l] == '\n') {
442 line[l] = '\0'; 576 line[l] = '\0';
443 } 577 }
578 if (line[0] == ',') {
579 line[0] = ' ';
580 }
444 dirty = true; 581 dirty = true;
582 parse_json(line);
445 } 583 }
446 } 584 }
447} 585}
448 586
449int main(int argc, char **argv) { 587int main(int argc, char **argv) {
450 init_log(L_INFO); 588 init_log(L_DEBUG);
451 589
452 char *socket_path = NULL; 590 char *socket_path = NULL;
453 char *bar_id = NULL; 591 char *bar_id = NULL;