summaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c147
1 files changed, 103 insertions, 44 deletions
diff --git a/sway/config.c b/sway/config.c
index 970225f4..23fe5388 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -3,6 +3,9 @@
3#include <stdlib.h> 3#include <stdlib.h>
4#include <unistd.h> 4#include <unistd.h>
5#include <wordexp.h> 5#include <wordexp.h>
6#include <sys/types.h>
7#include <sys/wait.h>
8#include <signal.h>
6#include "wayland-desktop-shell-server-protocol.h" 9#include "wayland-desktop-shell-server-protocol.h"
7#include "readline.h" 10#include "readline.h"
8#include "stringop.h" 11#include "stringop.h"
@@ -360,6 +363,91 @@ void merge_output_config(struct output_config *dst, struct output_config *src) {
360 } 363 }
361} 364}
362 365
366static void invoke_swaybar(swayc_t *output, struct bar_config *bar, int output_i) {
367 sway_log(L_DEBUG, "Invoking swaybar for output %s[%d] and bar %s", output->name, output_i, bar->id);
368
369 size_t bufsize = 4;
370 char output_id[bufsize];
371 snprintf(output_id, bufsize, "%d", output_i);
372 output_id[bufsize-1] = 0;
373
374 char *const cmd[] = {
375 "swaybar",
376 "-b",
377 bar->id,
378 output_id,
379 NULL,
380 };
381
382 pid_t *pid = malloc(sizeof(pid_t));
383 *pid = fork();
384 if (*pid == 0) {
385 execvp(cmd[0], cmd);
386 }
387
388 // add swaybar pid to output
389 list_add(output->bar_pids, pid);
390}
391
392static void terminate_swaybars(list_t *pids) {
393 int i, ret;
394 pid_t *pid;
395 for (i = 0; i < pids->length; ++i) {
396 pid = pids->items[i];
397 ret = kill(*pid, SIGTERM);
398 if (ret != 0) {
399 sway_log(L_ERROR, "Unable to terminate swaybar [pid: %d]", *pid);
400 } else {
401 int status;
402 waitpid(*pid, &status, 0);
403 }
404 }
405
406 // empty pids list
407 for (i = 0; i < pids->length; ++i) {
408 pid = pids->items[i];
409 list_del(pids, i);
410 free(pid);
411 }
412}
413
414void load_swaybars(swayc_t *output, int output_idx) {
415 // Check for bars
416 list_t *bars = create_list();
417 struct bar_config *bar = NULL;
418 int i;
419 for (i = 0; i < config->bars->length; ++i) {
420 bar = config->bars->items[i];
421 bool apply = false;
422 if (bar->outputs) {
423 int j;
424 for (j = 0; j < bar->outputs->length; ++j) {
425 char *o = bar->outputs->items[j];
426 if (strcmp(o, "*") || strcasecmp(o, output->name)) {
427 apply = true;
428 break;
429 }
430 }
431 } else {
432 apply = true;
433 }
434 if (apply) {
435 list_add(bars, bar);
436 }
437 }
438
439 // terminate swaybar processes previously spawned for this
440 // output.
441 terminate_swaybars(output->bar_pids);
442
443 for (i = 0; i < bars->length; ++i) {
444 bar = bars->items[i];
445 invoke_swaybar(output, bar, output_idx);
446 }
447
448 list_free(bars);
449}
450
363void apply_output_config(struct output_config *oc, swayc_t *output) { 451void apply_output_config(struct output_config *oc, swayc_t *output) {
364 if (oc && oc->width > 0 && oc->height > 0) { 452 if (oc && oc->width > 0 && oc->height > 0) {
365 output->width = oc->width; 453 output->width = oc->width;
@@ -407,6 +495,16 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
407 495
408 if (oc && oc->background) { 496 if (oc && oc->background) {
409 497
498 if (output->bg_pid != 0) {
499 int ret = kill(output->bg_pid, SIGTERM);
500 if (ret != 0) {
501 sway_log(L_ERROR, "Unable to terminate swaybg [pid: %d]", output->bg_pid);
502 } else {
503 int status;
504 waitpid(output->bg_pid, &status, 0);
505 }
506 }
507
410 sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background); 508 sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background);
411 509
412 size_t bufsize = 4; 510 size_t bufsize = 4;
@@ -421,54 +519,15 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
421 oc->background_option, 519 oc->background_option,
422 NULL, 520 NULL,
423 }; 521 };
424 if (fork() == 0) {
425 execvp(cmd[0], cmd);
426 }
427 }
428
429 // Check for a bar
430 struct bar_config *bar = NULL;
431 int i;
432 for (i = 0; i < config->bars->length; ++i) {
433 bar = config->bars->items[i];
434 bool apply = false;
435 if (bar->outputs) {
436 int j;
437 for (j = 0; j < bar->outputs->length; ++j) {
438 char *o = bar->outputs->items[j];
439 if (strcmp(o, "*") || strcasecmp(o, output->name)) {
440 apply = true;
441 break;
442 }
443 }
444 } else {
445 apply = true;
446 }
447 if (apply) {
448 break;
449 } else {
450 bar = NULL;
451 }
452 }
453 if (bar) {
454 sway_log(L_DEBUG, "Invoking swaybar for output %s[%d] and bar %s", output->name, i, bar->id);
455 522
456 size_t bufsize = 4; 523 output->bg_pid = fork();
457 char output_id[bufsize]; 524 if (output->bg_pid == 0) {
458 snprintf(output_id, bufsize, "%d", output_i);
459 output_id[bufsize-1] = 0;
460
461 char *const cmd[] = {
462 "swaybar",
463 "-b",
464 bar->id,
465 output_id,
466 NULL,
467 };
468 if (fork() == 0) {
469 execvp(cmd[0], cmd); 525 execvp(cmd[0], cmd);
470 } 526 }
471 } 527 }
528
529 // load swaybars for output
530 load_swaybars(output, output_i);
472} 531}
473 532
474char *do_var_replacement(char *str) { 533char *do_var_replacement(char *str) {