Add primary_selection config option

See: https://github.com/swaywm/sway/issues/4511

Adds a bool config option `primary_selection`, which explicitly
enables/disables the primary selection clipboard. Defaults to enabled.

This is implemented as a launch-only option which enables or disables the creation of the
`zwp_primary_selection_device_manager_v1` global.

Co-authored-by: Tilde Rose <t1lde@protonmail.com>
This commit is contained in:
Aidan Dang 2022-03-16 22:22:41 +00:00 committed by Alex Janka
parent 59c2b7a884
commit 59a10cf4c7
2 changed files with 107 additions and 74 deletions

View file

@ -97,6 +97,7 @@ static const struct cmd_handler handlers[] = {
{ "no_focus", cmd_no_focus },
{ "output", cmd_output },
{ "popup_during_fullscreen", cmd_popup_during_fullscreen },
{ "primary_selection", cmd_primary_selection },
{ "seat", cmd_seat },
{ "set", cmd_set },
{ "shadow_blur_radius", cmd_shadow_blur_radius },

View file

@ -63,33 +63,38 @@
#define SWAY_LAYER_SHELL_VERSION 4
#if WLR_HAS_DRM_BACKEND
static void handle_drm_lease_request(struct wl_listener *listener, void *data) {
static void handle_drm_lease_request(struct wl_listener *listener, void *data)
{
/* We only offer non-desktop outputs, but in the future we might want to do
* more logic here. */
struct wlr_drm_lease_request_v1 *req = data;
struct wlr_drm_lease_v1 *lease = wlr_drm_lease_request_v1_grant(req);
if (!lease) {
if (!lease)
{
sway_log(SWAY_ERROR, "Failed to grant lease request");
wlr_drm_lease_request_v1_reject(req);
}
}
#endif
static bool is_privileged(const struct wl_global *global) {
static bool is_privileged(const struct wl_global *global)
{
#if WLR_HAS_DRM_BACKEND
if (server.drm_lease_manager != NULL) {
if (server.drm_lease_manager != NULL)
{
struct wlr_drm_lease_device_v1 *drm_lease_dev;
wl_list_for_each(drm_lease_dev, &server.drm_lease_manager->devices, link) {
if (drm_lease_dev->global == global) {
wl_list_for_each(drm_lease_dev, &server.drm_lease_manager->devices, link)
{
if (drm_lease_dev->global == global)
{
return true;
}
}
}
#endif
return
global == server.output_manager_v1->global ||
return global == server.output_manager_v1->global ||
global == server.output_power_manager_v1->global ||
global == server.input_method->global ||
global == server.foreign_toplevel_manager->global ||
@ -107,10 +112,12 @@ static bool is_privileged(const struct wl_global *global) {
}
static bool filter_global(const struct wl_client *client,
const struct wl_global *global, void *data) {
const struct wl_global *global, void *data)
{
#if HAVE_XWAYLAND
struct wlr_xwayland *xwayland = server.xwayland.wlr_xwayland;
if (xwayland && global == xwayland->shell_v1->global) {
if (xwayland && global == xwayland->shell_v1->global)
{
return xwayland->server != NULL && client == xwayland->server->client;
}
#endif
@ -120,14 +127,16 @@ static bool filter_global(const struct wl_client *client,
const struct wlr_security_context_v1_state *security_context =
wlr_security_context_manager_v1_lookup_client(
server.security_context_manager_v1, (struct wl_client *)client);
if (is_privileged(global)) {
if (is_privileged(global))
{
return security_context == NULL;
}
return true;
}
bool server_init(struct sway_server *server) {
bool server_init(struct sway_server *server)
{
sway_log(SWAY_DEBUG, "Initializing Wayland server");
server->wl_display = wl_display_create();
server->wl_event_loop = wl_display_get_event_loop(server->wl_display);
@ -135,20 +144,23 @@ bool server_init(struct sway_server *server) {
wl_display_set_global_filter(server->wl_display, filter_global, NULL);
server->backend = wlr_backend_autocreate(server->wl_display, &server->session);
if (!server->backend) {
if (!server->backend)
{
sway_log(SWAY_ERROR, "Unable to create backend");
return false;
}
server->renderer = fx_renderer_create(server->backend);
if (!server->renderer) {
if (!server->renderer)
{
sway_log(SWAY_ERROR, "Failed to create fx_renderer");
return false;
}
wlr_renderer_init_wl_shm(server->renderer, server->wl_display);
if (wlr_renderer_get_dmabuf_texture_formats(server->renderer) != NULL) {
if (wlr_renderer_get_dmabuf_texture_formats(server->renderer) != NULL)
{
wlr_drm_create(server->wl_display, server->renderer);
server->linux_dmabuf_v1 = wlr_linux_dmabuf_v1_create_with_renderer(
server->wl_display, 4, server->renderer);
@ -156,7 +168,8 @@ bool server_init(struct sway_server *server) {
server->allocator = wlr_allocator_autocreate(server->backend,
server->renderer);
if (!server->allocator) {
if (!server->allocator)
{
sway_log(SWAY_ERROR, "Failed to create allocator");
return false;
}
@ -256,13 +269,16 @@ bool server_init(struct sway_server *server) {
sway_session_lock_init();
#if WLR_HAS_DRM_BACKEND
server->drm_lease_manager=
server->drm_lease_manager =
wlr_drm_lease_v1_manager_create(server->wl_display, server->backend);
if (server->drm_lease_manager) {
if (server->drm_lease_manager)
{
server->drm_lease_request.notify = handle_drm_lease_request;
wl_signal_add(&server->drm_lease_manager->events.request,
&server->drm_lease_request);
} else {
}
else
{
sway_log(SWAY_DEBUG, "Failed to create wlr_drm_lease_device_v1");
sway_log(SWAY_INFO, "VR will not be available");
}
@ -302,26 +318,32 @@ bool server_init(struct sway_server *server) {
// Avoid using "wayland-0" as display socket
char name_candidate[16];
for (unsigned int i = 1; i <= 32; ++i) {
for (unsigned int i = 1; i <= 32; ++i)
{
snprintf(name_candidate, sizeof(name_candidate), "wayland-%u", i);
if (wl_display_add_socket(server->wl_display, name_candidate) >= 0) {
if (wl_display_add_socket(server->wl_display, name_candidate) >= 0)
{
server->socket = strdup(name_candidate);
break;
}
}
if (!server->socket) {
if (!server->socket)
{
sway_log(SWAY_ERROR, "Unable to open wayland socket");
wlr_backend_destroy(server->backend);
return false;
}
server->headless_backend = wlr_headless_backend_create(server->wl_display);
if (!server->headless_backend) {
if (!server->headless_backend)
{
sway_log(SWAY_ERROR, "Failed to create secondary headless backend");
wlr_backend_destroy(server->backend);
return false;
} else {
}
else
{
wlr_multi_backend_add(server->backend, server->headless_backend);
}
@ -331,7 +353,8 @@ bool server_init(struct sway_server *server) {
root->fallback_output = output_create(wlr_output);
// This may have been set already via -Dtxn-timeout
if (!server->txn_timeout_ms) {
if (!server->txn_timeout_ms)
{
server->txn_timeout_ms = 200;
}
@ -343,7 +366,8 @@ bool server_init(struct sway_server *server) {
return true;
}
void server_fini(struct sway_server *server) {
void server_fini(struct sway_server *server)
{
// TODO: free sway-specific resources
#if HAVE_XWAYLAND
wlr_xwayland_destroy(server->xwayland.wlr_xwayland);
@ -353,18 +377,23 @@ void server_fini(struct sway_server *server) {
list_free(server->dirty_nodes);
}
bool server_start(struct sway_server *server) {
bool server_start(struct sway_server *server)
{
#if HAVE_XWAYLAND
if (config->xwayland != XWAYLAND_MODE_DISABLED) {
if (config->xwayland != XWAYLAND_MODE_DISABLED)
{
sway_log(SWAY_DEBUG, "Initializing Xwayland (lazy=%d)",
config->xwayland == XWAYLAND_MODE_LAZY);
server->xwayland.wlr_xwayland =
wlr_xwayland_create(server->wl_display, server->compositor,
config->xwayland == XWAYLAND_MODE_LAZY);
if (!server->xwayland.wlr_xwayland) {
if (!server->xwayland.wlr_xwayland)
{
sway_log(SWAY_ERROR, "Failed to start Xwayland");
unsetenv("DISPLAY");
} else {
}
else
{
wl_signal_add(&server->xwayland.wlr_xwayland->events.new_surface,
&server->xwayland_surface);
server->xwayland_surface.notify = handle_xwayland_surface;
@ -379,13 +408,15 @@ bool server_start(struct sway_server *server) {
}
#endif
if (config->primary_selection) {
if (config->primary_selection)
{
wlr_primary_selection_v1_device_manager_create(server->wl_display);
}
sway_log(SWAY_INFO, "Starting backend on wayland display '%s'",
server->socket);
if (!wlr_backend_start(server->backend)) {
if (!wlr_backend_start(server->backend))
{
sway_log(SWAY_ERROR, "Failed to start backend");
wlr_backend_destroy(server->backend);
return false;
@ -394,7 +425,8 @@ bool server_start(struct sway_server *server) {
return true;
}
void server_run(struct sway_server *server) {
void server_run(struct sway_server *server)
{
sway_log(SWAY_INFO, "Running compositor on wayland display '%s'",
server->socket);
wl_display_run(server->wl_display);