2018-03-29 05:04:20 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
2019-02-14 16:43:34 +01:00
|
|
|
#include <stdbool.h>
|
2016-01-24 02:34:20 +01:00
|
|
|
#include <stdlib.h>
|
2016-02-22 17:27:17 +01:00
|
|
|
#include <string.h>
|
2016-01-24 02:34:20 +01:00
|
|
|
#include <sys/wait.h>
|
2018-03-29 05:04:20 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wayland-client.h>
|
2018-03-31 03:38:28 +02:00
|
|
|
#include <wayland-cursor.h>
|
2018-12-09 16:10:41 +01:00
|
|
|
#include "config.h"
|
2018-09-24 22:56:35 +02:00
|
|
|
#include "swaybar/bar.h"
|
2016-09-01 14:18:37 +02:00
|
|
|
#include "swaybar/config.h"
|
2018-09-24 22:56:35 +02:00
|
|
|
#include "swaybar/i3bar.h"
|
2018-10-17 21:21:27 +02:00
|
|
|
#include "swaybar/input.h"
|
2018-03-29 05:56:02 +02:00
|
|
|
#include "swaybar/ipc.h"
|
2018-09-24 22:56:35 +02:00
|
|
|
#include "swaybar/status_line.h"
|
|
|
|
#include "swaybar/render.h"
|
2018-10-28 11:25:47 +01:00
|
|
|
#if HAVE_TRAY
|
|
|
|
#include "swaybar/tray/tray.h"
|
|
|
|
#endif
|
2018-03-29 05:56:02 +02:00
|
|
|
#include "ipc-client.h"
|
2016-01-24 02:34:20 +01:00
|
|
|
#include "list.h"
|
2018-03-31 04:02:55 +02:00
|
|
|
#include "log.h"
|
2018-10-13 08:04:37 +02:00
|
|
|
#include "loop.h"
|
2018-03-29 05:04:20 +02:00
|
|
|
#include "pool-buffer.h"
|
|
|
|
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
2018-09-17 16:04:09 +02:00
|
|
|
#include "xdg-output-unstable-v1-client-protocol.h"
|
2024-08-09 09:54:22 +02:00
|
|
|
#include "xdg-shell-client-protocol.h"
|
2016-01-24 02:34:20 +01:00
|
|
|
|
2018-04-24 23:04:19 +02:00
|
|
|
void free_workspaces(struct wl_list *list) {
|
|
|
|
struct swaybar_workspace *ws, *tmp;
|
|
|
|
wl_list_for_each_safe(ws, tmp, list, link) {
|
|
|
|
wl_list_remove(&ws->link);
|
|
|
|
free(ws->name);
|
2018-11-17 17:11:28 +01:00
|
|
|
free(ws->label);
|
2018-04-24 23:04:19 +02:00
|
|
|
free(ws);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 21:39:57 +02:00
|
|
|
static void swaybar_output_free(struct swaybar_output *output) {
|
|
|
|
if (!output) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Removing output %s", output->name);
|
2018-09-19 11:37:24 +02:00
|
|
|
if (output->layer_surface != NULL) {
|
|
|
|
zwlr_layer_surface_v1_destroy(output->layer_surface);
|
|
|
|
}
|
|
|
|
if (output->surface != NULL) {
|
|
|
|
wl_surface_destroy(output->surface);
|
|
|
|
}
|
2018-04-05 21:39:57 +02:00
|
|
|
wl_output_destroy(output->output);
|
|
|
|
destroy_buffer(&output->buffers[0]);
|
|
|
|
destroy_buffer(&output->buffers[1]);
|
2018-10-12 21:18:04 +02:00
|
|
|
free_hotspots(&output->hotspots);
|
2018-04-24 23:04:19 +02:00
|
|
|
free_workspaces(&output->workspaces);
|
2018-04-05 21:39:57 +02:00
|
|
|
wl_list_remove(&output->link);
|
|
|
|
free(output->name);
|
2019-01-13 18:06:35 +01:00
|
|
|
free(output->identifier);
|
2018-04-05 21:39:57 +02:00
|
|
|
free(output);
|
|
|
|
}
|
|
|
|
|
2018-09-28 17:17:15 +02:00
|
|
|
static void set_output_dirty(struct swaybar_output *output) {
|
|
|
|
if (output->frame_scheduled) {
|
|
|
|
output->dirty = true;
|
2018-10-12 21:18:04 +02:00
|
|
|
} else if (output->surface) {
|
2018-09-28 17:17:15 +02:00
|
|
|
render_frame(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
static void layer_surface_configure(void *data,
|
|
|
|
struct zwlr_layer_surface_v1 *surface,
|
|
|
|
uint32_t serial, uint32_t width, uint32_t height) {
|
|
|
|
struct swaybar_output *output = data;
|
|
|
|
output->width = width;
|
|
|
|
output->height = height;
|
|
|
|
zwlr_layer_surface_v1_ack_configure(surface, serial);
|
2018-09-28 17:17:15 +02:00
|
|
|
set_output_dirty(output);
|
2016-07-11 05:23:47 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
static void layer_surface_closed(void *_output,
|
|
|
|
struct zwlr_layer_surface_v1 *surface) {
|
2018-03-30 04:25:25 +02:00
|
|
|
struct swaybar_output *output = _output;
|
2018-04-05 21:39:57 +02:00
|
|
|
swaybar_output_free(output);
|
2018-03-29 05:04:20 +02:00
|
|
|
}
|
2017-09-23 15:22:40 +02:00
|
|
|
|
2021-02-04 03:45:51 +01:00
|
|
|
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
|
2018-03-29 05:04:20 +02:00
|
|
|
.configure = layer_surface_configure,
|
|
|
|
.closed = layer_surface_closed,
|
|
|
|
};
|
|
|
|
|
2018-09-19 11:37:24 +02:00
|
|
|
static void add_layer_surface(struct swaybar_output *output) {
|
2018-10-12 21:32:48 +02:00
|
|
|
if (output->layer_surface) {
|
2018-09-19 11:37:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct swaybar *bar = output->bar;
|
|
|
|
|
2018-10-12 21:32:48 +02:00
|
|
|
struct swaybar_config *config = bar->config;
|
|
|
|
bool hidden = strcmp(config->mode, "hide") == 0;
|
2019-02-24 06:59:36 +01:00
|
|
|
bool overlay = !hidden && strcmp(config->mode, "overlay") == 0;
|
2018-09-19 11:37:24 +02:00
|
|
|
output->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
|
|
|
|
bar->layer_shell, output->surface, output->output,
|
2019-02-24 06:59:36 +01:00
|
|
|
hidden || overlay ? ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY :
|
2018-09-19 11:37:24 +02:00
|
|
|
ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel");
|
|
|
|
assert(output->layer_surface);
|
|
|
|
zwlr_layer_surface_v1_add_listener(output->layer_surface,
|
|
|
|
&layer_surface_listener, output);
|
2018-10-12 21:32:48 +02:00
|
|
|
|
2019-02-24 06:59:36 +01:00
|
|
|
if (overlay) {
|
|
|
|
// Empty input region
|
2022-03-14 17:48:39 +01:00
|
|
|
struct wl_region *region = wl_compositor_create_region(bar->compositor);
|
|
|
|
wl_surface_set_input_region(output->surface, region);
|
|
|
|
wl_region_destroy(region);
|
2019-02-24 06:59:36 +01:00
|
|
|
}
|
|
|
|
|
2018-10-12 21:32:48 +02:00
|
|
|
zwlr_layer_surface_v1_set_anchor(output->layer_surface, config->position);
|
2019-02-24 06:59:36 +01:00
|
|
|
if (hidden || overlay) {
|
2018-10-12 21:32:48 +02:00
|
|
|
zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 03:41:11 +02:00
|
|
|
void destroy_layer_surface(struct swaybar_output *output) {
|
2018-10-12 21:32:48 +02:00
|
|
|
if (!output->layer_surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
zwlr_layer_surface_v1_destroy(output->layer_surface);
|
|
|
|
wl_surface_attach(output->surface, NULL, 0, 0); // detach buffer
|
|
|
|
output->layer_surface = NULL;
|
|
|
|
output->width = 0;
|
|
|
|
output->frame_scheduled = false;
|
|
|
|
}
|
|
|
|
|
2018-12-07 13:40:45 +01:00
|
|
|
void set_bar_dirty(struct swaybar *bar) {
|
2018-10-21 17:46:40 +02:00
|
|
|
struct swaybar_output *output;
|
|
|
|
wl_list_for_each(output, &bar->outputs, link) {
|
|
|
|
set_output_dirty(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 21:32:48 +02:00
|
|
|
bool determine_bar_visibility(struct swaybar *bar, bool moving_layer) {
|
|
|
|
struct swaybar_config *config = bar->config;
|
|
|
|
bool visible = !(strcmp(config->mode, "invisible") == 0 ||
|
|
|
|
(strcmp(config->mode, config->hidden_state) == 0 // both "hide"
|
2019-04-24 06:25:49 +02:00
|
|
|
&& !bar->visible_by_modifier && !bar->visible_by_urgency
|
|
|
|
&& !bar->visible_by_mode));
|
2018-10-12 21:32:48 +02:00
|
|
|
|
2018-10-21 17:46:40 +02:00
|
|
|
// Create/destroy layer surfaces as needed
|
2018-10-12 21:32:48 +02:00
|
|
|
struct swaybar_output *output;
|
2018-10-21 17:46:40 +02:00
|
|
|
wl_list_for_each(output, &bar->outputs, link) {
|
|
|
|
// When moving to a different layer, we need to destroy and re-create
|
|
|
|
// the layer surface
|
|
|
|
if (!visible || moving_layer) {
|
|
|
|
destroy_layer_surface(output);
|
2018-10-12 21:32:48 +02:00
|
|
|
}
|
2018-10-21 17:46:40 +02:00
|
|
|
|
|
|
|
if (visible) {
|
|
|
|
add_layer_surface(output);
|
2018-10-12 21:32:48 +02:00
|
|
|
}
|
2018-10-21 17:46:40 +02:00
|
|
|
}
|
|
|
|
set_bar_dirty(bar);
|
|
|
|
|
|
|
|
if (visible != bar->visible) {
|
|
|
|
bar->visible = visible;
|
|
|
|
|
2018-10-23 10:38:01 +02:00
|
|
|
if (bar->status) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Sending %s signal to status command",
|
2018-10-23 10:38:01 +02:00
|
|
|
visible ? "cont" : "stop");
|
2021-11-21 10:55:20 +01:00
|
|
|
kill(-bar->status->pid, visible ?
|
2018-10-23 10:38:01 +02:00
|
|
|
bar->status->cont_signal : bar->status->stop_signal);
|
|
|
|
}
|
2018-10-12 21:32:48 +02:00
|
|
|
}
|
2018-10-21 17:46:40 +02:00
|
|
|
|
2018-10-12 21:32:48 +02:00
|
|
|
return visible;
|
2018-09-19 11:37:24 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 18:06:35 +01:00
|
|
|
static bool bar_uses_output(struct swaybar_output *output) {
|
2019-09-03 03:41:11 +02:00
|
|
|
if (wl_list_empty(&output->bar->config->outputs)) {
|
2018-09-19 11:37:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-13 18:06:35 +01:00
|
|
|
char *identifier = output->identifier;
|
2018-09-19 11:37:24 +02:00
|
|
|
struct config_output *coutput;
|
2019-01-13 18:06:35 +01:00
|
|
|
wl_list_for_each(coutput, &output->bar->config->outputs, link) {
|
|
|
|
if (strcmp(coutput->name, output->name) == 0 ||
|
|
|
|
(identifier && strcmp(coutput->name, identifier) == 0)) {
|
2018-09-19 11:37:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-21 05:37:08 +02:00
|
|
|
static void output_geometry(void *data, struct wl_output *wl_output, int32_t x,
|
2018-04-04 03:06:28 +02:00
|
|
|
int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel,
|
|
|
|
const char *make, const char *model, int32_t transform) {
|
2018-09-21 05:37:08 +02:00
|
|
|
struct swaybar_output *output = data;
|
|
|
|
output->subpixel = subpixel;
|
2018-04-04 03:06:28 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 17:17:15 +02:00
|
|
|
static void output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
|
2018-04-04 03:06:28 +02:00
|
|
|
int32_t width, int32_t height, int32_t refresh) {
|
|
|
|
// Who cares
|
|
|
|
}
|
|
|
|
|
2018-09-28 17:17:15 +02:00
|
|
|
static void output_done(void *data, struct wl_output *wl_output) {
|
|
|
|
struct swaybar_output *output = data;
|
|
|
|
set_output_dirty(output);
|
2018-04-04 03:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void output_scale(void *data, struct wl_output *wl_output,
|
|
|
|
int32_t factor) {
|
|
|
|
struct swaybar_output *output = data;
|
|
|
|
output->scale = factor;
|
2019-04-24 05:40:00 +02:00
|
|
|
|
|
|
|
bool render = false;
|
|
|
|
struct swaybar_seat *seat;
|
|
|
|
wl_list_for_each(seat, &output->bar->seats, link) {
|
|
|
|
if (output == seat->pointer.current) {
|
|
|
|
update_cursor(seat);
|
|
|
|
render = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (render) {
|
2018-12-15 09:21:08 +01:00
|
|
|
render_frame(output);
|
|
|
|
}
|
2018-04-04 03:06:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 03:45:51 +01:00
|
|
|
static const struct wl_output_listener output_listener = {
|
2018-04-04 03:06:28 +02:00
|
|
|
.geometry = output_geometry,
|
|
|
|
.mode = output_mode,
|
|
|
|
.done = output_done,
|
|
|
|
.scale = output_scale,
|
|
|
|
};
|
|
|
|
|
2018-09-17 16:04:09 +02:00
|
|
|
static void xdg_output_handle_logical_position(void *data,
|
|
|
|
struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y) {
|
2018-12-07 13:39:35 +01:00
|
|
|
struct swaybar_output *output = data;
|
|
|
|
output->output_x = x;
|
|
|
|
output->output_y = y;
|
2018-09-17 16:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xdg_output_handle_logical_size(void *data,
|
|
|
|
struct zxdg_output_v1 *xdg_output, int32_t width, int32_t height) {
|
2018-12-07 13:39:35 +01:00
|
|
|
struct swaybar_output *output = data;
|
|
|
|
output->output_height = height;
|
|
|
|
output->output_width = width;
|
2018-09-17 16:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xdg_output_handle_done(void *data,
|
|
|
|
struct zxdg_output_v1 *xdg_output) {
|
2018-09-19 11:37:24 +02:00
|
|
|
struct swaybar_output *output = data;
|
|
|
|
struct swaybar *bar = output->bar;
|
|
|
|
|
2019-09-03 03:41:11 +02:00
|
|
|
if (!wl_list_empty(&output->link)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:37:24 +02:00
|
|
|
assert(output->name != NULL);
|
2019-01-13 18:06:35 +01:00
|
|
|
if (!bar_uses_output(output)) {
|
2019-09-03 03:41:11 +02:00
|
|
|
wl_list_remove(&output->link);
|
|
|
|
wl_list_insert(&bar->unused_outputs, &output->link);
|
2018-09-19 11:37:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-03 03:41:11 +02:00
|
|
|
wl_list_remove(&output->link);
|
|
|
|
wl_list_insert(&bar->outputs, &output->link);
|
2018-09-19 11:37:24 +02:00
|
|
|
|
2019-09-03 03:41:11 +02:00
|
|
|
output->surface = wl_compositor_create_surface(bar->compositor);
|
|
|
|
assert(output->surface);
|
2018-10-21 17:46:40 +02:00
|
|
|
|
2019-09-03 03:41:11 +02:00
|
|
|
determine_bar_visibility(bar, false);
|
|
|
|
|
|
|
|
if (bar->running && bar->config->workspace_buttons) {
|
|
|
|
ipc_get_workspaces(bar);
|
2018-09-19 11:37:24 +02:00
|
|
|
}
|
2018-09-17 16:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void xdg_output_handle_name(void *data,
|
|
|
|
struct zxdg_output_v1 *xdg_output, const char *name) {
|
|
|
|
struct swaybar_output *output = data;
|
|
|
|
free(output->name);
|
|
|
|
output->name = strdup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xdg_output_handle_description(void *data,
|
|
|
|
struct zxdg_output_v1 *xdg_output, const char *description) {
|
2019-01-13 18:06:35 +01:00
|
|
|
// wlroots currently sets the description to `make model serial (name)`
|
|
|
|
// If this changes in the future, this will need to be modified.
|
|
|
|
struct swaybar_output *output = data;
|
|
|
|
free(output->identifier);
|
|
|
|
output->identifier = NULL;
|
|
|
|
char *paren = strrchr(description, '(');
|
|
|
|
if (paren) {
|
|
|
|
size_t length = paren - description;
|
|
|
|
output->identifier = malloc(length);
|
|
|
|
if (!output->identifier) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "Failed to allocate output identifier");
|
2019-01-13 18:06:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
strncpy(output->identifier, description, length);
|
|
|
|
output->identifier[length - 1] = '\0';
|
|
|
|
}
|
2018-09-17 16:04:09 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 03:45:51 +01:00
|
|
|
static const struct zxdg_output_v1_listener xdg_output_listener = {
|
2018-09-17 16:04:09 +02:00
|
|
|
.logical_position = xdg_output_handle_logical_position,
|
|
|
|
.logical_size = xdg_output_handle_logical_size,
|
|
|
|
.done = xdg_output_handle_done,
|
|
|
|
.name = xdg_output_handle_name,
|
|
|
|
.description = xdg_output_handle_description,
|
|
|
|
};
|
|
|
|
|
2018-09-19 11:37:24 +02:00
|
|
|
static void add_xdg_output(struct swaybar_output *output) {
|
|
|
|
if (output->xdg_output != NULL) {
|
|
|
|
return;
|
2018-04-10 05:38:15 +02:00
|
|
|
}
|
2018-09-19 11:37:24 +02:00
|
|
|
assert(output->bar->xdg_output_manager != NULL);
|
|
|
|
output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
|
|
|
|
output->bar->xdg_output_manager, output->output);
|
|
|
|
zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener,
|
|
|
|
output);
|
2018-04-10 05:38:15 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
static void handle_global(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name, const char *interface, uint32_t version) {
|
|
|
|
struct swaybar *bar = data;
|
|
|
|
if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
|
|
|
bar->compositor = wl_registry_bind(registry, name,
|
2018-12-15 09:21:08 +01:00
|
|
|
&wl_compositor_interface, 4);
|
2018-03-31 03:38:28 +02:00
|
|
|
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
2019-04-24 05:40:00 +02:00
|
|
|
struct swaybar_seat *seat = calloc(1, sizeof(struct swaybar_seat));
|
|
|
|
if (!seat) {
|
|
|
|
sway_abort("Failed to allocate swaybar_seat");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
seat->bar = bar;
|
|
|
|
seat->wl_name = name;
|
2020-04-23 23:07:04 +02:00
|
|
|
seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 5);
|
2019-04-24 05:40:00 +02:00
|
|
|
wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
|
|
|
|
wl_list_insert(&bar->seats, &seat->link);
|
2018-03-29 05:04:20 +02:00
|
|
|
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
|
|
|
|
bar->shm = wl_registry_bind(registry, name,
|
|
|
|
&wl_shm_interface, 1);
|
|
|
|
} else if (strcmp(interface, wl_output_interface.name) == 0) {
|
2018-09-17 16:04:09 +02:00
|
|
|
struct swaybar_output *output =
|
|
|
|
calloc(1, sizeof(struct swaybar_output));
|
|
|
|
output->bar = bar;
|
|
|
|
output->output = wl_registry_bind(registry, name,
|
|
|
|
&wl_output_interface, 3);
|
|
|
|
wl_output_add_listener(output->output, &output_listener, output);
|
|
|
|
output->scale = 1;
|
|
|
|
output->wl_name = name;
|
|
|
|
wl_list_init(&output->workspaces);
|
|
|
|
wl_list_init(&output->hotspots);
|
2018-09-19 11:37:24 +02:00
|
|
|
wl_list_init(&output->link);
|
|
|
|
if (bar->xdg_output_manager != NULL) {
|
|
|
|
add_xdg_output(output);
|
|
|
|
}
|
2018-03-29 05:04:20 +02:00
|
|
|
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
|
|
|
|
bar->layer_shell = wl_registry_bind(
|
|
|
|
registry, name, &zwlr_layer_shell_v1_interface, 1);
|
2018-09-17 16:04:09 +02:00
|
|
|
} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
|
|
|
|
bar->xdg_output_manager = wl_registry_bind(registry, name,
|
|
|
|
&zxdg_output_manager_v1_interface, 2);
|
2023-07-30 22:07:28 +02:00
|
|
|
} else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
|
|
|
|
bar->cursor_shape_manager = wl_registry_bind(registry, name,
|
|
|
|
&wp_cursor_shape_manager_v1_interface, 1);
|
2024-08-09 09:54:22 +02:00
|
|
|
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
|
|
|
bar->wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
|
2016-07-17 17:26:38 +02:00
|
|
|
}
|
2018-03-29 05:04:20 +02:00
|
|
|
}
|
2016-07-17 17:26:38 +02:00
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
static void handle_global_remove(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name) {
|
2018-04-05 21:39:57 +02:00
|
|
|
struct swaybar *bar = data;
|
|
|
|
struct swaybar_output *output, *tmp;
|
|
|
|
wl_list_for_each_safe(output, tmp, &bar->outputs, link) {
|
|
|
|
if (output->wl_name == name) {
|
|
|
|
swaybar_output_free(output);
|
2019-04-24 05:40:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 03:41:11 +02:00
|
|
|
wl_list_for_each_safe(output, tmp, &bar->unused_outputs, link) {
|
|
|
|
if (output->wl_name == name) {
|
|
|
|
swaybar_output_free(output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 05:40:00 +02:00
|
|
|
struct swaybar_seat *seat, *tmp_seat;
|
|
|
|
wl_list_for_each_safe(seat, tmp_seat, &bar->seats, link) {
|
|
|
|
if (seat->wl_name == name) {
|
|
|
|
swaybar_seat_free(seat);
|
|
|
|
return;
|
2018-04-05 21:39:57 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 05:14:20 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
static const struct wl_registry_listener registry_listener = {
|
|
|
|
.global = handle_global,
|
|
|
|
.global_remove = handle_global_remove,
|
|
|
|
};
|
2016-01-24 02:34:20 +01:00
|
|
|
|
2018-09-30 16:09:09 +02:00
|
|
|
bool bar_setup(struct swaybar *bar, const char *socket_path) {
|
2018-10-23 13:25:52 +02:00
|
|
|
bar->visible = true;
|
|
|
|
bar->config = init_config();
|
|
|
|
wl_list_init(&bar->outputs);
|
2019-09-03 03:41:11 +02:00
|
|
|
wl_list_init(&bar->unused_outputs);
|
2019-04-24 05:40:00 +02:00
|
|
|
wl_list_init(&bar->seats);
|
2018-10-13 08:04:37 +02:00
|
|
|
bar->eventloop = loop_create();
|
2017-06-08 01:45:28 +02:00
|
|
|
|
2018-03-29 05:56:02 +02:00
|
|
|
bar->ipc_socketfd = ipc_open_socket(socket_path);
|
|
|
|
bar->ipc_event_socketfd = ipc_open_socket(socket_path);
|
2018-09-30 16:09:09 +02:00
|
|
|
if (!ipc_initialize(bar)) {
|
2018-09-24 19:42:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-29 05:56:02 +02:00
|
|
|
|
2018-04-04 03:29:43 +02:00
|
|
|
bar->display = wl_display_connect(NULL);
|
2018-10-15 13:57:59 +02:00
|
|
|
if (!bar->display) {
|
|
|
|
sway_abort("Unable to connect to the compositor. "
|
|
|
|
"If your compositor is running, check or set the "
|
|
|
|
"WAYLAND_DISPLAY environment variable.");
|
|
|
|
}
|
2018-03-29 05:04:20 +02:00
|
|
|
|
|
|
|
struct wl_registry *registry = wl_display_get_registry(bar->display);
|
|
|
|
wl_registry_add_listener(registry, ®istry_listener, bar);
|
|
|
|
wl_display_roundtrip(bar->display);
|
2018-09-17 16:04:09 +02:00
|
|
|
assert(bar->compositor && bar->layer_shell && bar->shm &&
|
|
|
|
bar->xdg_output_manager);
|
|
|
|
|
2018-10-21 17:46:40 +02:00
|
|
|
// Second roundtrip for xdg-output
|
2018-04-04 03:06:28 +02:00
|
|
|
wl_display_roundtrip(bar->display);
|
|
|
|
|
2023-07-30 22:07:28 +02:00
|
|
|
if (!bar->cursor_shape_manager) {
|
|
|
|
struct swaybar_seat *seat;
|
|
|
|
wl_list_for_each(seat, &bar->seats, link) {
|
|
|
|
struct swaybar_pointer *pointer = &seat->pointer;
|
|
|
|
if (!pointer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pointer->cursor_surface =
|
|
|
|
wl_compositor_create_surface(bar->compositor);
|
|
|
|
assert(pointer->cursor_surface);
|
2019-04-24 05:40:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-29 05:04:20 +02:00
|
|
|
|
2020-06-11 02:29:07 +02:00
|
|
|
if (bar->config->status_command) {
|
|
|
|
bar->status = status_line_init(bar->config->status_command);
|
|
|
|
bar->status->bar = bar;
|
|
|
|
}
|
|
|
|
|
2018-10-28 11:25:47 +01:00
|
|
|
#if HAVE_TRAY
|
2018-12-11 17:27:39 +01:00
|
|
|
if (!bar->config->tray_hidden) {
|
|
|
|
bar->tray = create_tray(bar);
|
|
|
|
}
|
2018-10-28 11:25:47 +01:00
|
|
|
#endif
|
|
|
|
|
2018-10-12 22:17:32 +02:00
|
|
|
if (bar->config->workspace_buttons) {
|
2018-10-20 22:12:54 +02:00
|
|
|
ipc_get_workspaces(bar);
|
|
|
|
}
|
2018-10-21 17:46:40 +02:00
|
|
|
determine_bar_visibility(bar, false);
|
2018-09-24 19:42:25 +02:00
|
|
|
return true;
|
2018-03-29 21:16:12 +02:00
|
|
|
}
|
|
|
|
|
2018-04-21 04:45:34 +02:00
|
|
|
static void display_in(int fd, short mask, void *data) {
|
|
|
|
struct swaybar *bar = data;
|
2021-07-08 13:17:15 +02:00
|
|
|
if (mask & (POLLHUP | POLLERR)) {
|
|
|
|
if (mask & POLLERR) {
|
|
|
|
sway_log(SWAY_ERROR, "Wayland display poll error");
|
|
|
|
}
|
|
|
|
bar->running = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-29 05:04:20 +02:00
|
|
|
if (wl_display_dispatch(bar->display) == -1) {
|
2021-07-08 13:17:15 +02:00
|
|
|
sway_log(SWAY_ERROR, "wl_display_dispatch failed");
|
2019-02-14 16:43:34 +01:00
|
|
|
bar->running = false;
|
2017-06-08 01:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 04:45:34 +02:00
|
|
|
static void ipc_in(int fd, short mask, void *data) {
|
|
|
|
struct swaybar *bar = data;
|
2021-07-08 13:16:11 +02:00
|
|
|
if (mask & (POLLHUP | POLLERR)) {
|
|
|
|
if (mask & POLLERR) {
|
|
|
|
sway_log(SWAY_ERROR, "IPC poll error");
|
|
|
|
}
|
|
|
|
bar->running = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-31 20:39:18 +02:00
|
|
|
if (handle_ipc_readable(bar)) {
|
2018-09-28 17:17:15 +02:00
|
|
|
set_bar_dirty(bar);
|
2018-03-29 21:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 03:41:11 +02:00
|
|
|
void status_in(int fd, short mask, void *data) {
|
2018-04-21 04:45:34 +02:00
|
|
|
struct swaybar *bar = data;
|
2018-04-21 04:39:46 +02:00
|
|
|
if (mask & (POLLHUP | POLLERR)) {
|
|
|
|
status_error(bar->status, "[error reading from status command]");
|
2018-09-28 17:17:15 +02:00
|
|
|
set_bar_dirty(bar);
|
2018-10-14 04:28:38 +02:00
|
|
|
loop_remove_fd(bar->eventloop, fd);
|
2018-04-21 04:39:46 +02:00
|
|
|
} else if (status_handle_readable(bar->status)) {
|
2018-09-28 17:17:15 +02:00
|
|
|
set_bar_dirty(bar);
|
2018-03-29 16:38:17 +02:00
|
|
|
}
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
void bar_run(struct swaybar *bar) {
|
2018-10-13 08:04:37 +02:00
|
|
|
loop_add_fd(bar->eventloop, wl_display_get_fd(bar->display), POLLIN,
|
|
|
|
display_in, bar);
|
|
|
|
loop_add_fd(bar->eventloop, bar->ipc_event_socketfd, POLLIN, ipc_in, bar);
|
2018-03-29 21:16:12 +02:00
|
|
|
if (bar->status) {
|
2018-10-14 04:28:38 +02:00
|
|
|
loop_add_fd(bar->eventloop, bar->status->read_fd, POLLIN,
|
|
|
|
status_in, bar);
|
2018-03-29 21:16:12 +02:00
|
|
|
}
|
2018-10-28 11:25:47 +01:00
|
|
|
#if HAVE_TRAY
|
|
|
|
if (bar->tray) {
|
|
|
|
loop_add_fd(bar->eventloop, bar->tray->fd, POLLIN, tray_in, bar->tray->bus);
|
|
|
|
}
|
|
|
|
#endif
|
2019-02-14 16:43:34 +01:00
|
|
|
while (bar->running) {
|
2018-10-23 13:22:51 +02:00
|
|
|
errno = 0;
|
|
|
|
if (wl_display_flush(bar->display) == -1 && errno != EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2018-10-13 08:04:37 +02:00
|
|
|
loop_poll(bar->eventloop);
|
2016-01-24 02:34:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
static void free_outputs(struct wl_list *list) {
|
|
|
|
struct swaybar_output *output, *tmp;
|
|
|
|
wl_list_for_each_safe(output, tmp, list, link) {
|
2018-04-24 23:04:19 +02:00
|
|
|
swaybar_output_free(output);
|
2016-01-24 02:34:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 05:40:00 +02:00
|
|
|
static void free_seats(struct wl_list *list) {
|
|
|
|
struct swaybar_seat *seat, *tmp;
|
|
|
|
wl_list_for_each_safe(seat, tmp, list, link) {
|
|
|
|
swaybar_seat_free(seat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 05:04:20 +02:00
|
|
|
void bar_teardown(struct swaybar *bar) {
|
2018-10-28 11:25:47 +01:00
|
|
|
#if HAVE_TRAY
|
|
|
|
destroy_tray(bar->tray);
|
|
|
|
#endif
|
2024-08-09 09:54:22 +02:00
|
|
|
xdg_wm_base_destroy(bar->wm_base);
|
2018-03-29 05:04:20 +02:00
|
|
|
free_outputs(&bar->outputs);
|
2019-09-03 03:41:11 +02:00
|
|
|
free_outputs(&bar->unused_outputs);
|
2019-04-24 05:40:00 +02:00
|
|
|
free_seats(&bar->seats);
|
2016-03-31 11:50:07 +02:00
|
|
|
if (bar->config) {
|
|
|
|
free_config(bar->config);
|
|
|
|
}
|
2018-03-29 21:19:42 +02:00
|
|
|
close(bar->ipc_event_socketfd);
|
|
|
|
close(bar->ipc_socketfd);
|
|
|
|
if (bar->status) {
|
|
|
|
status_line_free(bar->status);
|
|
|
|
}
|
2018-09-30 16:09:09 +02:00
|
|
|
free(bar->id);
|
2018-10-12 21:23:01 +02:00
|
|
|
free(bar->mode);
|
2016-01-24 02:34:20 +01:00
|
|
|
}
|