diff options
author | progandy <code@progandy> | 2015-12-22 17:26:40 +0100 |
---|---|---|
committer | progandy <code@progandy> | 2015-12-22 17:26:40 +0100 |
commit | d37169a9271327ae4583929a8aaec0cd53a37659 (patch) | |
tree | 70751e0b4a04b8b3824b90715395b5bbbbccf46a /swaybar/main.c | |
parent | Clean up a bit (diff) | |
download | sway-d37169a9271327ae4583929a8aaec0cd53a37659.tar.gz sway-d37169a9271327ae4583929a8aaec0cd53a37659.tar.zst sway-d37169a9271327ae4583929a8aaec0cd53a37659.zip |
swaybar: fix whitespace
Diffstat (limited to 'swaybar/main.c')
-rw-r--r-- | swaybar/main.c | 260 |
1 files changed, 127 insertions, 133 deletions
diff --git a/swaybar/main.c b/swaybar/main.c index 8e92b18f..e8d26573 100644 --- a/swaybar/main.c +++ b/swaybar/main.c | |||
@@ -46,13 +46,13 @@ struct workspace { | |||
46 | }; | 46 | }; |
47 | 47 | ||
48 | struct status_block { | 48 | struct status_block { |
49 | char *full_text, *short_text, *align; | 49 | char *full_text, *short_text, *align; |
50 | bool urgent; | 50 | bool urgent; |
51 | uint32_t color; | 51 | uint32_t color; |
52 | int min_width; | 52 | int min_width; |
53 | char *name, *instance; | 53 | char *name, *instance; |
54 | bool separator; | 54 | bool separator; |
55 | int separator_block_width; | 55 | int separator_block_width; |
56 | }; | 56 | }; |
57 | 57 | ||
58 | list_t *status_line = NULL; | 58 | list_t *status_line = NULL; |
@@ -375,21 +375,21 @@ void render() { | |||
375 | cairo_set_source_u32(window->cairo, colors.statusline); | 375 | cairo_set_source_u32(window->cairo, colors.statusline); |
376 | int width, height; | 376 | int width, height; |
377 | 377 | ||
378 | if (status_line) { | 378 | if (status_line) { |
379 | int i; | 379 | int i; |
380 | int moved = 0; | 380 | int moved = 0; |
381 | for ( i = status_line->length - 1; i >= 0; --i ) { | 381 | for ( i = status_line->length - 1; i >= 0; --i ) { |
382 | struct status_block *block = status_line->items[i]; | 382 | struct status_block *block = status_line->items[i]; |
383 | if (block->full_text) { | 383 | if (block->full_text) { |
384 | get_text_size(window, &width, &height, "%s", block->full_text); | 384 | get_text_size(window, &width, &height, "%s", block->full_text); |
385 | moved += width + block->separator_block_width; | 385 | moved += width + block->separator_block_width; |
386 | cairo_move_to(window->cairo, window->width - margin - moved, margin); | 386 | cairo_move_to(window->cairo, window->width - margin - moved, margin); |
387 | cairo_set_source_u32(window->cairo, block->color); | 387 | cairo_set_source_u32(window->cairo, block->color); |
388 | pango_printf(window, "%s", block->full_text); | 388 | pango_printf(window, "%s", block->full_text); |
389 | } | 389 | } |
390 | } | 390 | } |
391 | } | 391 | } |
392 | 392 | ||
393 | // Workspaces | 393 | // Workspaces |
394 | cairo_set_line_width(window->cairo, 1.0); | 394 | cairo_set_line_width(window->cairo, 1.0); |
395 | double x = 0.5; | 395 | double x = 0.5; |
@@ -425,124 +425,118 @@ void render() { | |||
425 | } | 425 | } |
426 | 426 | ||
427 | void parse_json(const char *text) { | 427 | void parse_json(const char *text) { |
428 | 428 | /* the array of objects looks like this: | |
429 | /* | 429 | * [ { |
430 | * { | 430 | * "full_text": "E: 10.0.0.1 (1000 Mbit/s)", |
431 | "full_text": "E: 10.0.0.1 (1000 Mbit/s)", | 431 | * "short_text": "10.0.0.1", |
432 | "short_text": "10.0.0.1", | 432 | * "color": "#00ff00", |
433 | "color": "#00ff00", | 433 | * "min_width": 300, |
434 | "min_width": 300, | 434 | * "align": "right", |
435 | "align": "right", | 435 | * "urgent": false, |
436 | "urgent": false, | 436 | * "name": "ethernet", |
437 | "name": "ethernet", | 437 | * "instance": "eth0", |
438 | "instance": "eth0", | 438 | * "separator": true, |
439 | "separator": true, | 439 | * "separator_block_width": 9 |
440 | "separator_block_width": 9 | 440 | * }, |
441 | } | 441 | * { ... }, ... |
442 | * | 442 | * ] |
443 | * | 443 | */ |
444 | * | ||
445 | * */ | ||
446 | |||
447 | json_object *results = json_tokener_parse(text); | 444 | json_object *results = json_tokener_parse(text); |
448 | if (!results) { | 445 | if (!results) { |
449 | sway_log(L_DEBUG, "xxx Failed to parse json"); | 446 | sway_log(L_DEBUG, "xxx Failed to parse json"); |
450 | return; | 447 | return; |
451 | } | 448 | } |
452 | 449 | ||
453 | if (json_object_array_length(results) < 1) { | 450 | if (json_object_array_length(results) < 1) { |
454 | return; | 451 | return; |
455 | } | 452 | } |
456 | 453 | ||
457 | if (status_line) { | 454 | if (status_line) { |
458 | free_flat_list(status_line); | 455 | free_flat_list(status_line); |
459 | } | 456 | } |
460 | 457 | ||
461 | status_line = create_list(); | 458 | status_line = create_list(); |
462 | 459 | ||
463 | int i; | 460 | int i; |
464 | for (i = 0; i < json_object_array_length(results); ++i) { | 461 | for (i = 0; i < json_object_array_length(results); ++i) { |
465 | json_object *full_text, *short_text, *color, *min_width, *align, *urgent; | 462 | json_object *full_text, *short_text, *color, *min_width, *align, *urgent; |
466 | json_object *name, *instance, *separator, *separator_block_width; | 463 | json_object *name, *instance, *separator, *separator_block_width; |
467 | 464 | ||
468 | json_object *json = json_object_array_get_idx(results, i); | 465 | json_object *json = json_object_array_get_idx(results, i); |
469 | 466 | if (!json) { | |
470 | if (!json) { | 467 | continue; |
471 | continue; | 468 | } |
472 | } | 469 | |
473 | 470 | json_object_object_get_ex(json, "full_text", &full_text); | |
474 | json_object_object_get_ex(json, "full_text", &full_text); | 471 | json_object_object_get_ex(json, "short_text", &short_text); |
475 | json_object_object_get_ex(json, "short_text", &short_text); | 472 | json_object_object_get_ex(json, "color", &color); |
476 | json_object_object_get_ex(json, "color", &color); | 473 | json_object_object_get_ex(json, "min_width", &min_width); |
477 | json_object_object_get_ex(json, "min_width", &min_width); | 474 | json_object_object_get_ex(json, "align", &align); |
478 | json_object_object_get_ex(json, "align", &align); | 475 | json_object_object_get_ex(json, "urgent", &urgent); |
479 | json_object_object_get_ex(json, "urgent", &urgent); | 476 | json_object_object_get_ex(json, "name", &name); |
480 | json_object_object_get_ex(json, "name", &name); | 477 | json_object_object_get_ex(json, "instance", &instance); |
481 | json_object_object_get_ex(json, "instance", &instance); | 478 | json_object_object_get_ex(json, "separator", &separator); |
482 | json_object_object_get_ex(json, "separator", &separator); | 479 | json_object_object_get_ex(json, "separator_block_width", &separator_block_width); |
483 | json_object_object_get_ex(json, "separator_block_width", &separator_block_width); | 480 | |
484 | 481 | struct status_block *new = malloc(sizeof(struct status_block)); | |
485 | struct status_block *new = malloc(sizeof(struct status_block)); | 482 | memset(new, 0, sizeof(struct status_block)); |
486 | memset(new, 0, sizeof(struct status_block)); | 483 | |
487 | 484 | if (full_text) { | |
488 | if (full_text) { | 485 | new->full_text = strdup(json_object_get_string(full_text)); |
489 | new->full_text = strdup(json_object_get_string(full_text)); | 486 | } |
490 | } | 487 | |
491 | 488 | if (short_text) { | |
492 | if (short_text) { | 489 | new->short_text = strdup(json_object_get_string(short_text)); |
493 | new->short_text = strdup(json_object_get_string(short_text)); | 490 | } |
494 | } | 491 | |
495 | 492 | if (color) { | |
496 | if (color) { | 493 | new->color = parse_color(json_object_get_string(color)); |
497 | new->color = parse_color(json_object_get_string(color)); | 494 | } |
498 | } | 495 | else { |
499 | else { | 496 | new->color = 0xFFFFFFFF; |
500 | new->color = 0xFFFFFFFF; | 497 | } |
501 | } | 498 | |
502 | 499 | if (min_width) { | |
503 | if (min_width) { | 500 | new->min_width = json_object_get_int(min_width); |
504 | new->min_width = json_object_get_int(min_width); | 501 | } |
505 | } | 502 | |
506 | 503 | if (align) { | |
507 | if (align) { | 504 | new->align = strdup(json_object_get_string(align)); |
508 | new->align = strdup(json_object_get_string(align)); | 505 | } |
509 | } | 506 | else { |
510 | else { | 507 | new->align = strdup("left"); |
511 | new->align = strdup("left"); | 508 | } |
512 | } | 509 | |
513 | 510 | if (urgent) { | |
514 | if (urgent) { | 511 | new->urgent = json_object_get_int(urgent); |
515 | new->urgent = json_object_get_int(urgent); | 512 | } |
516 | } | 513 | |
517 | 514 | if (name) { | |
518 | if (name) { | 515 | new->name = strdup(json_object_get_string(name)); |
519 | new->name = strdup(json_object_get_string(name)); | 516 | } |
520 | } | 517 | |
521 | 518 | if (instance) { | |
522 | if (instance) { | 519 | new->instance = strdup(json_object_get_string(instance)); |
523 | new->instance = strdup(json_object_get_string(instance)); | 520 | } |
524 | } | 521 | |
525 | 522 | if (separator) { | |
526 | if (separator) { | 523 | new->separator = json_object_get_int(separator); |
527 | new->separator = json_object_get_int(separator); | 524 | } |
528 | } | 525 | else { |
529 | else { | 526 | new->separator = true; // i3bar spec |
530 | new->separator = true; // i3bar spec | 527 | } |
531 | } | 528 | |
532 | 529 | if (separator_block_width) { | |
533 | if (separator_block_width) { | 530 | new->separator_block_width = json_object_get_int(separator_block_width); |
534 | new->separator_block_width = json_object_get_int(separator_block_width); | 531 | } |
535 | } | 532 | else { |
536 | else { | 533 | new->separator_block_width = 9; // i3bar spec |
537 | new->separator_block_width = 9; // i3bar spec | 534 | } |
538 | } | ||
539 | 535 | ||
540 | list_add(status_line, new); | 536 | list_add(status_line, new); |
541 | 537 | } | |
542 | } | ||
543 | 538 | ||
544 | json_object_put(results); | 539 | json_object_put(results); |
545 | |||
546 | } | 540 | } |
547 | 541 | ||
548 | void poll_for_update() { | 542 | void poll_for_update() { |
@@ -581,16 +575,16 @@ void poll_for_update() { | |||
581 | if (status_command && FD_ISSET(pipefd[0], &readfds)) { | 575 | if (status_command && FD_ISSET(pipefd[0], &readfds)) { |
582 | sway_log(L_DEBUG, "Got update from status command."); | 576 | sway_log(L_DEBUG, "Got update from status command."); |
583 | fgets(line, sizeof(line), command); | 577 | fgets(line, sizeof(line), command); |
584 | sway_log(L_DEBUG, "zzz %s", line); | 578 | sway_log(L_DEBUG, "zzz %s", line); |
585 | int l = strlen(line) - 1; | 579 | int l = strlen(line) - 1; |
586 | if (line[l] == '\n') { | 580 | if (line[l] == '\n') { |
587 | line[l] = '\0'; | 581 | line[l] = '\0'; |
588 | } | 582 | } |
589 | if (line[0] == ',') { | 583 | if (line[0] == ',') { |
590 | line[0] = ' '; | 584 | line[0] = ' '; |
591 | } | 585 | } |
592 | dirty = true; | 586 | dirty = true; |
593 | parse_json(line); | 587 | parse_json(line); |
594 | } | 588 | } |
595 | } | 589 | } |
596 | } | 590 | } |