Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sway/server: Implement wayland socket handover #8259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/sway/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ extern struct sway_debug debug;

extern bool allow_unsupported_gpu;

bool server_init(struct sway_server *server);
bool server_init(struct sway_server *server, const char * socket_name, int socket_fd);
void server_fini(struct sway_server *server);
bool server_start(struct sway_server *server);
void server_run(struct sway_server *server);
Expand Down
24 changes: 23 additions & 1 deletion sway/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <fcntl.h>
#include <getopt.h>
#include <pango/pangocairo.h>
#include <signal.h>
Expand Down Expand Up @@ -196,6 +197,8 @@ static const struct option long_options[] = {
{"verbose", no_argument, NULL, 'V'},
{"get-socketpath", no_argument, NULL, 'p'},
{"unsupported-gpu", no_argument, NULL, 'u'},
{"socket", required_argument, NULL, 's'},
{"wayland-fd", required_argument, NULL, 'S'},
{0, 0, 0, 0}
};

Expand All @@ -209,12 +212,16 @@ static const char usage[] =
" -v, --version Show the version number and quit.\n"
" -V, --verbose Enables more verbose logging.\n"
" --get-socketpath Gets the IPC socket path and prints it, then exits.\n"
" --socket <name> Sets the Wayland socket name (for Wayland socket handover)\n"
" --wayland-fd <fd> Sets the Wayland socket fd (for Wayland socket handover)\n"
"\n";

int main(int argc, char **argv) {
static bool verbose = false, debug = false, validate = false;

char *config_path = NULL;
char *socket_name = NULL;
int socket_fd = -1;

int c;
while (1) {
Expand Down Expand Up @@ -260,6 +267,13 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE);
}
break;
case 's': // --socket
free(socket_name);
socket_name = strdup(optarg);
break;
case 'S': // --wayland-fd
socket_fd = atoi(optarg);
break;
default:
fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE);
Expand All @@ -279,6 +293,14 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE);
}

// Fail if only one of --socket and --wayland-fd are given.
if ((socket_name == NULL) ^ (socket_fd == -1)) {
fprintf(stderr,
"Both --socket and --wayland-fd are required for Wayland "
"socket handover, but only one was provided. Aborting.\n");
exit(EXIT_FAILURE);
}

// As the 'callback' function for wlr_log is equivalent to that for
// sway, we do not need to override it.
if (debug) {
Expand Down Expand Up @@ -331,7 +353,7 @@ int main(int argc, char **argv) {

sway_log(SWAY_INFO, "Starting sway version " SWAY_VERSION);

if (!server_init(&server)) {
if (!server_init(&server, socket_name, socket_fd)) {
return 1;
}

Expand Down
25 changes: 17 additions & 8 deletions sway/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <wayland-server-core.h>
#include <wlr/backend.h>
#include <wlr/backend/headless.h>
Expand Down Expand Up @@ -214,7 +215,7 @@ static void handle_renderer_lost(struct wl_listener *listener, void *data) {
wlr_renderer_destroy(old_renderer);
}

bool server_init(struct sway_server *server) {
bool server_init(struct sway_server *server, const char * socket_name, int socket_fd) {
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);
Expand Down Expand Up @@ -403,13 +404,21 @@ bool server_init(struct sway_server *server) {

wl_list_init(&server->pending_launcher_ctxs);

// Avoid using "wayland-0" as display socket
char name_candidate[16];
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) {
server->socket = strdup(name_candidate);
break;
if (socket_name != NULL && socket_fd != -1) {
// Add the passed socket
fcntl(socket_fd, F_SETFD, FD_CLOEXEC);
if (wl_display_add_socket_fd(server->wl_display, socket_fd) >= 0) {
server->socket = strdup(socket_name);
}
} else {
// Avoid using "wayland-0" as display socket
char name_candidate[16];
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) {
server->socket = strdup(name_candidate);
break;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions sway/sway.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ sway - An i3-compatible Wayland compositor
*--get-socketpath*
Gets the IPC socket path and prints it, then exits.

*--socket NAME*
Sets the Wayland socket name (for Wayland socket handover)

*--wayland-fd FD*
Sets the Wayland socket file descriptor (for Wayland socket handover)

# DESCRIPTION

sway was created to fill the need of an i3-like window manager for Wayland. The
Expand Down