Spawn swaybars when outputs are added

This commit is contained in:
Drew DeVault 2018-03-29 18:07:03 -04:00
parent 5c9cdbcdd2
commit 2719ddfe5e
4 changed files with 102 additions and 2 deletions

View file

@ -420,6 +420,7 @@ struct sway_binding *sway_binding_dup(struct sway_binding *sb);
/* Bar stuff */ /* Bar stuff */
void load_swaybars(); void load_swaybars();
void invoke_swaybar(struct bar_config *bar);
void terminate_swaybg(pid_t pid); void terminate_swaybg(pid_t pid);
struct bar_config *default_bar_config(void); struct bar_config *default_bar_config(void);
void free_bar_config(struct bar_config *bar); void free_bar_config(struct bar_config *bar);

View file

@ -141,3 +141,98 @@ cleanup:
free_bar_config(bar); free_bar_config(bar);
return NULL; return NULL;
} }
void invoke_swaybar(struct bar_config *bar) {
// Pipe to communicate errors
int filedes[2];
if (pipe(filedes) == -1) {
wlr_log(L_ERROR, "Pipe setup failed! Cannot fork into bar");
return;
}
bar->pid = fork();
if (bar->pid == 0) {
close(filedes[0]);
// run custom swaybar
size_t len = snprintf(NULL, 0, "%s -b %s",
bar->swaybar_command ? bar->swaybar_command : "swaybar",
bar->id);
char *command = malloc(len + 1);
if (!command) {
const char msg[] = "Unable to allocate swaybar command string";
size_t len = sizeof(msg);
if (write(filedes[1], &len, sizeof(int))) {};
if (write(filedes[1], msg, len)) {};
close(filedes[1]);
exit(1);
}
snprintf(command, len + 1, "%s -b %s",
bar->swaybar_command ? bar->swaybar_command : "swaybar",
bar->id);
char *const cmd[] = { "sh", "-c", command, NULL, };
close(filedes[1]);
execvp(cmd[0], cmd);
exit(1);
}
close(filedes[0]);
ssize_t len;
if (read(filedes[1], &len, sizeof(int)) == sizeof(int)) {
char *buf = malloc(len);
if(!buf) {
wlr_log(L_ERROR, "Cannot allocate error string");
return;
}
if (read(filedes[1], buf, len)) {
wlr_log(L_ERROR, "%s", buf);
}
free(buf);
}
close(filedes[1]);
}
static void terminate_swaybar(pid_t pid) {
int ret = kill(pid, SIGTERM);
if (ret != 0) {
wlr_log_errno(L_ERROR, "Unable to terminate swaybar %d", pid);
} else {
int status;
waitpid(pid, &status, 0);
}
}
static bool active_output(const char *name) {
swayc_t *cont = NULL;
for (int i = 0; i < root_container.children->length; ++i) {
cont = root_container.children->items[i];
if (cont->type == C_OUTPUT && strcasecmp(name, cont->name) == 0) {
return true;
}
}
return false;
}
void load_swaybars() {
for (int i = 0; i < config->bars->length; ++i) {
struct bar_config *bar = config->bars->items[i];
bool apply = false;
if (bar->outputs) {
for (int j = 0; j < bar->outputs->length; ++j) {
char *o = bar->outputs->items[j];
if (!strcmp(o, "*") || active_output(o)) {
apply = true;
break;
}
}
} else {
apply = true;
}
if (apply) {
if (bar->pid != 0) {
terminate_swaybar(bar->pid);
}
wlr_log(L_DEBUG, "Invoking swaybar for bar id '%s'", bar->id);
invoke_swaybar(bar);
}
}
}

View file

@ -8,6 +8,8 @@
#include <wlr/render/gles2.h> #include <wlr/render/gles2.h>
#include <wlr/types/wlr_compositor.h> #include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_layer_shell.h> #include <wlr/types/wlr_layer_shell.h>
#include <wlr/types/wlr_screenshooter.h>
#include <wlr/types/wlr_gamma_control.h>
#include <wlr/types/wlr_wl_shell.h> #include <wlr/types/wlr_wl_shell.h>
// TODO WLR: make Xwayland optional // TODO WLR: make Xwayland optional
#include <wlr/xwayland.h> #include <wlr/xwayland.h>
@ -45,10 +47,12 @@ bool server_init(struct sway_server *server) {
server->compositor = wlr_compositor_create( server->compositor = wlr_compositor_create(
server->wl_display, server->renderer); server->wl_display, server->renderer);
server->data_device_manager = server->data_device_manager =
wlr_data_device_manager_create(server->wl_display); wlr_data_device_manager_create(server->wl_display);
wlr_screenshooter_create(server->wl_display);
wlr_gamma_control_manager_create(server->wl_display);
server->new_output.notify = handle_new_output; server->new_output.notify = handle_new_output;
wl_signal_add(&server->backend->events.new_output, &server->new_output); wl_signal_add(&server->backend->events.new_output, &server->new_output);

View file

@ -165,8 +165,8 @@ swayc_t *new_output(struct sway_output *sway_output) {
} }
apply_output_config(oc, output); apply_output_config(oc, output);
add_child(&root_container, output); add_child(&root_container, output);
load_swaybars();
// Create workspace // Create workspace
char *ws_name = workspace_next_name(output->name); char *ws_name = workspace_next_name(output->name);