diff --git a/include/sway/server.h b/include/sway/server.h index f50d48f0..e0069fe8 100644 --- a/include/sway/server.h +++ b/include/sway/server.h @@ -164,7 +164,7 @@ extern bool allow_unsupported_gpu; void sway_terminate(int exit_code); -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); diff --git a/sway/main.c b/sway/main.c index 44d07679..4eb678d7 100644 --- a/sway/main.c +++ b/sway/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -208,6 +209,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} }; @@ -221,12 +224,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 Sets the Wayland socket name (for Wayland socket handover)\n" + " --wayland-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) { @@ -272,6 +279,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); @@ -286,6 +300,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) { @@ -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; } diff --git a/sway/server.c b/sway/server.c index 4e606a39..e31545b2 100644 --- a/sway/server.c +++ b/sway/server.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -250,7 +251,7 @@ static void handle_new_foreign_toplevel_capture_request(struct wl_listener *list wlr_ext_foreign_toplevel_image_capture_source_manager_v1_request_accept(request, view->image_capture_source); } -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); @@ -485,13 +486,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; + } } } diff --git a/sway/sway.1.scd b/sway/sway.1.scd index 75df5b01..86753908 100644 --- a/sway/sway.1.scd +++ b/sway/sway.1.scd @@ -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