2018-03-29 23:41:02 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wordexp.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <strings.h>
|
2018-08-01 15:21:29 +02:00
|
|
|
#include <signal.h>
|
2018-03-29 23:41:02 +02:00
|
|
|
#include "sway/config.h"
|
Implement type safe arguments and demote sway_container
This commit changes the meaning of sway_container so that it only refers
to layout containers and view containers. Workspaces, outputs and the
root are no longer known as containers. Instead, root, outputs,
workspaces and containers are all a type of node, and containers come in
two types: layout containers and view containers.
In addition to the above, this implements type safe variables. This
means we use specific types such as sway_output and sway_workspace
instead of generic containers or nodes. However, it's worth noting that
in a few places places (eg. seat focus and transactions) referring to
them in a generic way is unavoidable which is why we still use nodes in
some places.
If you want a TL;DR, look at node.h, as well as the struct definitions
for root, output, workspace and container. Note that sway_output now
contains a workspaces list, and workspaces now contain a tiling and
floating list, and containers now contain a pointer back to the
workspace.
There are now functions for seat_get_focused_workspace and
seat_get_focused_container. The latter will return NULL if a workspace
itself is focused. Most other seat functions like seat_get_focus and
seat_set_focus now accept and return nodes.
In the config->handler_context struct, current_container has been
replaced with three pointers: node, container and workspace. node is the
same as what current_container was, while workspace is the workspace
that the node resides on and container is the actual container, which
may be NULL if a workspace itself is focused.
The global root_container variable has been replaced with one simply
called root, which is a pointer to the sway_root instance.
The way outputs are created, enabled, disabled and destroyed has
changed. Previously we'd wrap the sway_output in a container when it is
enabled, but as we don't have containers any more it needs a different
approach. The output_create and output_destroy functions previously
created/destroyed the container, but now they create/destroy the
sway_output. There is a new function output_disable to disable an output
without destroying it.
Containers have a new view property. If this is populated then the
container is a view container, otherwise it's a layout container. Like
before, this property is immutable for the life of the container.
Containers have both a `sway_container *parent` and
`sway_workspace *workspace`. As we use specific types now, parent cannot
point to a workspace so it'll be NULL for containers which are direct
children of the workspace. The workspace property is set for all
containers, except those which are hidden in the scratchpad as they have
no workspace.
In some cases we need to refer to workspaces in a container-like way.
For example, workspaces have layout and children, but when using
specific types this makes it difficult. Likewise, it's difficult for a
container to get its parent's layout when the parent could be another
container or a workspace. To make it easier, some helper functions have
been created: container_parent_layout and container_get_siblings.
container_remove_child has been renamed to container_detach and
container_replace_child has been renamed to container_replace.
`container_handle_fullscreen_reparent(con, old_parent)` has had the
old_parent removed. We now unfullscreen the workspace when detaching the
container, so this function is simplified and only needs one argument
now.
container_notify_subtree_changed has been renamed to
container_update_representation. This is more descriptive of its
purpose. I also wanted to be able to call it with whatever container was
changed rather than the container's parent, which makes bubbling up to
the workspace easier.
There are now state structs per node thing. ie. sway_output_state,
sway_workspace_state and sway_container_state.
The focus, move and layout commands have been completely refactored to
work with the specific types. I considered making these a separate PR,
but I'd be backporting my changes only to replace them again, and it's
easier just to test everything at once.
2018-08-30 13:00:10 +02:00
|
|
|
#include "sway/output.h"
|
2018-03-29 23:41:02 +02:00
|
|
|
#include "stringop.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2018-03-30 00:08:23 +02:00
|
|
|
static void terminate_swaybar(pid_t pid) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Terminating swaybar %d", pid);
|
2018-04-27 03:13:44 +02:00
|
|
|
int ret = kill(-pid, SIGTERM);
|
2018-03-30 00:08:23 +02:00
|
|
|
if (ret != 0) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to terminate swaybar %d", pid);
|
2018-03-30 00:08:23 +02:00
|
|
|
} else {
|
|
|
|
int status;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 23:41:02 +02:00
|
|
|
void free_bar_config(struct bar_config *bar) {
|
|
|
|
if (!bar) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-04 11:43:35 +02:00
|
|
|
free(bar->id);
|
2018-03-29 23:41:02 +02:00
|
|
|
free(bar->mode);
|
|
|
|
free(bar->position);
|
|
|
|
free(bar->hidden_state);
|
|
|
|
free(bar->status_command);
|
|
|
|
free(bar->font);
|
|
|
|
free(bar->separator_symbol);
|
|
|
|
// TODO: Free mouse bindings
|
|
|
|
list_free(bar->bindings);
|
|
|
|
if (bar->outputs) {
|
|
|
|
free_flat_list(bar->outputs);
|
|
|
|
}
|
|
|
|
if (bar->pid != 0) {
|
2018-03-30 00:08:23 +02:00
|
|
|
terminate_swaybar(bar->pid);
|
2018-03-29 23:41:02 +02:00
|
|
|
}
|
|
|
|
free(bar->colors.background);
|
|
|
|
free(bar->colors.statusline);
|
|
|
|
free(bar->colors.separator);
|
|
|
|
free(bar->colors.focused_background);
|
|
|
|
free(bar->colors.focused_statusline);
|
|
|
|
free(bar->colors.focused_separator);
|
|
|
|
free(bar->colors.focused_workspace_border);
|
|
|
|
free(bar->colors.focused_workspace_bg);
|
|
|
|
free(bar->colors.focused_workspace_text);
|
|
|
|
free(bar->colors.active_workspace_border);
|
|
|
|
free(bar->colors.active_workspace_bg);
|
|
|
|
free(bar->colors.active_workspace_text);
|
|
|
|
free(bar->colors.inactive_workspace_border);
|
|
|
|
free(bar->colors.inactive_workspace_bg);
|
|
|
|
free(bar->colors.inactive_workspace_text);
|
|
|
|
free(bar->colors.urgent_workspace_border);
|
|
|
|
free(bar->colors.urgent_workspace_bg);
|
|
|
|
free(bar->colors.urgent_workspace_text);
|
|
|
|
free(bar->colors.binding_mode_border);
|
|
|
|
free(bar->colors.binding_mode_bg);
|
|
|
|
free(bar->colors.binding_mode_text);
|
|
|
|
free(bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bar_config *default_bar_config(void) {
|
|
|
|
struct bar_config *bar = NULL;
|
2018-07-01 16:22:21 +02:00
|
|
|
bar = calloc(1, sizeof(struct bar_config));
|
2018-03-29 23:41:02 +02:00
|
|
|
if (!bar) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bar->outputs = NULL;
|
|
|
|
bar->position = strdup("bottom");
|
|
|
|
bar->pango_markup = false;
|
|
|
|
bar->swaybar_command = NULL;
|
|
|
|
bar->font = NULL;
|
|
|
|
bar->height = -1;
|
|
|
|
bar->workspace_buttons = true;
|
|
|
|
bar->wrap_scroll = false;
|
|
|
|
bar->separator_symbol = NULL;
|
|
|
|
bar->strip_workspace_numbers = false;
|
|
|
|
bar->binding_mode_indicator = true;
|
|
|
|
bar->verbose = false;
|
|
|
|
bar->pid = 0;
|
2018-07-01 16:22:21 +02:00
|
|
|
if (!(bar->mode = strdup("dock"))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->hidden_state = strdup("hide"))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->bindings = create_list())) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-03-29 23:41:02 +02:00
|
|
|
// set default colors
|
|
|
|
if (!(bar->colors.background = strndup("#000000ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.statusline = strndup("#ffffffff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.separator = strndup("#666666ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.focused_workspace_border = strndup("#4c7899ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.focused_workspace_bg = strndup("#285577ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.focused_workspace_text = strndup("#ffffffff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.active_workspace_border = strndup("#333333ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.active_workspace_bg = strndup("#5f676aff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.active_workspace_text = strndup("#ffffffff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.inactive_workspace_border = strndup("#333333ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.inactive_workspace_bg = strndup("#222222ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.inactive_workspace_text = strndup("#888888ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.urgent_workspace_border = strndup("#2f343aff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.urgent_workspace_bg = strndup("#900000ff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!(bar->colors.urgent_workspace_text = strndup("#ffffffff", 9))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
// if the following colors stay undefined, they fall back to background,
|
|
|
|
// statusline, separator and urgent_workspace_*.
|
|
|
|
bar->colors.focused_background = NULL;
|
|
|
|
bar->colors.focused_statusline = NULL;
|
|
|
|
bar->colors.focused_separator = NULL;
|
|
|
|
bar->colors.binding_mode_border = NULL;
|
|
|
|
bar->colors.binding_mode_bg = NULL;
|
|
|
|
bar->colors.binding_mode_text = NULL;
|
|
|
|
|
|
|
|
list_add(config->bars, bar);
|
|
|
|
return bar;
|
|
|
|
cleanup:
|
|
|
|
free_bar_config(bar);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-30 00:07:03 +02:00
|
|
|
|
2018-09-19 11:37:24 +02:00
|
|
|
static void invoke_swaybar(struct bar_config *bar) {
|
2018-03-30 00:07:03 +02:00
|
|
|
// Pipe to communicate errors
|
|
|
|
int filedes[2];
|
|
|
|
if (pipe(filedes) == -1) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "Pipe setup failed! Cannot fork into bar");
|
2018-03-30 00:07:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bar->pid = fork();
|
|
|
|
if (bar->pid == 0) {
|
2018-04-27 03:13:44 +02:00
|
|
|
setpgid(0, 0);
|
2018-03-30 00:07:03 +02:00
|
|
|
close(filedes[0]);
|
2018-08-01 15:21:29 +02:00
|
|
|
sigset_t set;
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigprocmask(SIG_SETMASK, &set, NULL);
|
2018-03-30 00:07:03 +02:00
|
|
|
|
|
|
|
// 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";
|
2018-04-27 03:16:29 +02:00
|
|
|
size_t msg_len = sizeof(msg);
|
2018-06-30 09:49:13 +02:00
|
|
|
if (write(filedes[1], &msg_len, sizeof(size_t))) {};
|
2018-04-27 03:16:29 +02:00
|
|
|
if (write(filedes[1], msg, msg_len)) {};
|
2018-03-30 00:07:03 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Spawned swaybar %d", bar->pid);
|
2018-03-30 00:07:03 +02:00
|
|
|
close(filedes[0]);
|
2018-06-30 09:49:13 +02:00
|
|
|
size_t len;
|
|
|
|
if (read(filedes[1], &len, sizeof(size_t)) == sizeof(size_t)) {
|
2018-03-30 00:07:03 +02:00
|
|
|
char *buf = malloc(len);
|
|
|
|
if(!buf) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "Cannot allocate error string");
|
2018-03-30 00:07:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (read(filedes[1], buf, len)) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "%s", buf);
|
2018-03-30 00:07:03 +02:00
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
close(filedes[1]);
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:37:24 +02:00
|
|
|
void load_swaybars(void) {
|
2018-03-30 00:07:03 +02:00
|
|
|
for (int i = 0; i < config->bars->length; ++i) {
|
|
|
|
struct bar_config *bar = config->bars->items[i];
|
2018-09-19 11:37:24 +02:00
|
|
|
if (bar->pid != 0) {
|
|
|
|
terminate_swaybar(bar->pid);
|
2018-03-30 00:07:03 +02:00
|
|
|
}
|
2018-09-19 11:37:24 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Invoking swaybar for bar id '%s'", bar->id);
|
|
|
|
invoke_swaybar(bar);
|
2018-03-30 00:07:03 +02:00
|
|
|
}
|
|
|
|
}
|