2017-11-23 03:37:07 +01:00
|
|
|
// See https://i3wm.org/docs/ipc.html for protocol information
|
|
|
|
#ifndef __FreeBSD__
|
|
|
|
// Any value will hide SOCK_CLOEXEC on FreeBSD (__BSD_VISIBLE=0)
|
|
|
|
#define _XOPEN_SOURCE 700
|
|
|
|
#endif
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <json-c/json.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wayland-server.h>
|
|
|
|
#include "sway/commands.h"
|
2018-07-08 21:34:47 +02:00
|
|
|
#include "sway/config.h"
|
2018-07-15 07:20:21 +02:00
|
|
|
#include "sway/desktop/transaction.h"
|
2017-11-23 03:37:07 +01:00
|
|
|
#include "sway/ipc-json.h"
|
|
|
|
#include "sway/ipc-server.h"
|
2018-06-05 23:56:32 +02:00
|
|
|
#include "sway/output.h"
|
2017-11-23 03:37:07 +01:00
|
|
|
#include "sway/server.h"
|
2017-12-18 20:27:38 +01:00
|
|
|
#include "sway/input/input-manager.h"
|
2018-03-29 22:51:36 +02:00
|
|
|
#include "sway/input/seat.h"
|
2018-05-22 23:35:39 +02:00
|
|
|
#include "sway/tree/view.h"
|
2017-11-23 03:37:07 +01:00
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
static int ipc_socket = -1;
|
|
|
|
static struct wl_event_source *ipc_event_source = NULL;
|
|
|
|
static struct sockaddr_un *ipc_sockaddr = NULL;
|
|
|
|
static list_t *ipc_client_list = NULL;
|
2018-07-04 07:45:35 +02:00
|
|
|
static struct wl_listener ipc_display_destroy;
|
2017-11-23 03:37:07 +01:00
|
|
|
|
|
|
|
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
|
|
|
|
|
|
|
struct ipc_client {
|
|
|
|
struct wl_event_source *event_source;
|
|
|
|
struct wl_event_source *writable_event_source;
|
|
|
|
struct sway_server *server;
|
|
|
|
int fd;
|
|
|
|
uint32_t payload_length;
|
|
|
|
uint32_t security_policy;
|
|
|
|
enum ipc_command_type current_command;
|
|
|
|
enum ipc_command_type subscribed_events;
|
|
|
|
size_t write_buffer_len;
|
|
|
|
size_t write_buffer_size;
|
|
|
|
char *write_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sockaddr_un *ipc_user_sockaddr(void);
|
|
|
|
int ipc_handle_connection(int fd, uint32_t mask, void *data);
|
|
|
|
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data);
|
|
|
|
int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data);
|
|
|
|
void ipc_client_disconnect(struct ipc_client *client);
|
|
|
|
void ipc_client_handle_command(struct ipc_client *client);
|
|
|
|
bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length);
|
|
|
|
|
2018-07-04 07:45:35 +02:00
|
|
|
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
|
|
|
if (ipc_event_source) {
|
|
|
|
wl_event_source_remove(ipc_event_source);
|
|
|
|
}
|
|
|
|
close(ipc_socket);
|
|
|
|
unlink(ipc_sockaddr->sun_path);
|
|
|
|
|
2018-07-04 11:48:57 +02:00
|
|
|
while (ipc_client_list->length) {
|
|
|
|
struct ipc_client *client = ipc_client_list->items[0];
|
|
|
|
ipc_client_disconnect(client);
|
|
|
|
}
|
2018-07-04 07:45:35 +02:00
|
|
|
list_free(ipc_client_list);
|
|
|
|
|
|
|
|
if (ipc_sockaddr) {
|
|
|
|
free(ipc_sockaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_remove(&ipc_display_destroy.link);
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:37:07 +01:00
|
|
|
void ipc_init(struct sway_server *server) {
|
|
|
|
ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
|
|
|
if (ipc_socket == -1) {
|
|
|
|
sway_abort("Unable to create IPC socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_sockaddr = ipc_user_sockaddr();
|
|
|
|
|
|
|
|
// We want to use socket name set by user, not existing socket from another sway instance.
|
|
|
|
if (getenv("SWAYSOCK") != NULL && access(getenv("SWAYSOCK"), F_OK) == -1) {
|
2018-04-13 15:35:23 +02:00
|
|
|
strncpy(ipc_sockaddr->sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr->sun_path) - 1);
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_sockaddr->sun_path[sizeof(ipc_sockaddr->sun_path) - 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(ipc_sockaddr->sun_path);
|
|
|
|
if (bind(ipc_socket, (struct sockaddr *)ipc_sockaddr, sizeof(*ipc_sockaddr)) == -1) {
|
|
|
|
sway_abort("Unable to bind IPC socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(ipc_socket, 3) == -1) {
|
|
|
|
sway_abort("Unable to listen on IPC socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set i3 IPC socket path so that i3-msg works out of the box
|
|
|
|
setenv("I3SOCK", ipc_sockaddr->sun_path, 1);
|
|
|
|
setenv("SWAYSOCK", ipc_sockaddr->sun_path, 1);
|
|
|
|
|
|
|
|
ipc_client_list = create_list();
|
|
|
|
|
2018-07-04 07:45:35 +02:00
|
|
|
ipc_display_destroy.notify = handle_display_destroy;
|
|
|
|
wl_display_add_destroy_listener(server->wl_display, &ipc_display_destroy);
|
|
|
|
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_event_source = wl_event_loop_add_fd(server->wl_event_loop, ipc_socket,
|
|
|
|
WL_EVENT_READABLE, ipc_handle_connection, server);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_un *ipc_user_sockaddr(void) {
|
|
|
|
struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un));
|
|
|
|
if (ipc_sockaddr == NULL) {
|
|
|
|
sway_abort("Can't allocate ipc_sockaddr");
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_sockaddr->sun_family = AF_UNIX;
|
|
|
|
int path_size = sizeof(ipc_sockaddr->sun_path);
|
|
|
|
|
|
|
|
// Env var typically set by logind, e.g. "/run/user/<user-id>"
|
|
|
|
const char *dir = getenv("XDG_RUNTIME_DIR");
|
|
|
|
if (!dir) {
|
|
|
|
dir = "/tmp";
|
|
|
|
}
|
|
|
|
if (path_size <= snprintf(ipc_sockaddr->sun_path, path_size,
|
|
|
|
"%s/sway-ipc.%i.%i.sock", dir, getuid(), getpid())) {
|
|
|
|
sway_abort("Socket path won't fit into ipc_sockaddr->sun_path");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipc_sockaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
|
|
|
(void) fd;
|
|
|
|
struct sway_server *server = data;
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Event on IPC listening socket");
|
2017-11-23 03:37:07 +01:00
|
|
|
assert(mask == WL_EVENT_READABLE);
|
|
|
|
|
|
|
|
int client_fd = accept(ipc_socket, NULL, NULL);
|
|
|
|
if (client_fd == -1) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to accept IPC client connection");
|
2017-11-23 03:37:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
if ((flags = fcntl(client_fd, F_GETFD)) == -1
|
|
|
|
|| fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to set CLOEXEC on IPC client socket");
|
2017-11-23 03:37:07 +01:00
|
|
|
close(client_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((flags = fcntl(client_fd, F_GETFL)) == -1
|
|
|
|
|| fcntl(client_fd, F_SETFL, flags|O_NONBLOCK) == -1) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Unable to set NONBLOCK on IPC client socket");
|
2017-11-23 03:37:07 +01:00
|
|
|
close(client_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ipc_client *client = malloc(sizeof(struct ipc_client));
|
|
|
|
if (!client) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "Unable to allocate ipc client");
|
2017-11-23 03:37:07 +01:00
|
|
|
close(client_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
client->server = server;
|
|
|
|
client->payload_length = 0;
|
|
|
|
client->fd = client_fd;
|
|
|
|
client->subscribed_events = 0;
|
|
|
|
client->event_source = wl_event_loop_add_fd(server->wl_event_loop,
|
|
|
|
client_fd, WL_EVENT_READABLE, ipc_client_handle_readable, client);
|
|
|
|
client->writable_event_source = NULL;
|
|
|
|
|
|
|
|
client->write_buffer_size = 128;
|
|
|
|
client->write_buffer_len = 0;
|
|
|
|
client->write_buffer = malloc(client->write_buffer_size);
|
|
|
|
if (!client->write_buffer) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "Unable to allocate ipc client write buffer");
|
2017-11-23 03:37:07 +01:00
|
|
|
close(client_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "New client: fd %d", client_fd);
|
2017-11-23 03:37:07 +01:00
|
|
|
list_add(ipc_client_list, client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int ipc_header_size = sizeof(ipc_magic) + 8;
|
|
|
|
|
|
|
|
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
|
|
|
|
struct ipc_client *client = data;
|
|
|
|
|
|
|
|
if (mask & WL_EVENT_ERROR) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "IPC Client socket error, removing client");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask & WL_EVENT_HANGUP) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Client %d hung up", client->fd);
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Client %d readable", client->fd);
|
2017-11-23 03:37:07 +01:00
|
|
|
|
|
|
|
int read_available;
|
|
|
|
if (ioctl(client_fd, FIONREAD, &read_available) == -1) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Unable to read IPC socket buffer size");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the rest of the command payload in case the header has already been read
|
|
|
|
if (client->payload_length > 0) {
|
|
|
|
if ((uint32_t)read_available >= client->payload_length) {
|
|
|
|
ipc_client_handle_command(client);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_available < ipc_header_size) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t buf[ipc_header_size];
|
|
|
|
uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic));
|
|
|
|
// Should be fully available, because read_available >= ipc_header_size
|
|
|
|
ssize_t received = recv(client_fd, buf, ipc_header_size, 0);
|
|
|
|
if (received == -1) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "IPC header check failed");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
client->payload_length = buf32[0];
|
|
|
|
client->current_command = (enum ipc_command_type)buf32[1];
|
|
|
|
|
|
|
|
if (read_available - received >= (long)client->payload_length) {
|
|
|
|
ipc_client_handle_command(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-06 01:01:03 +02:00
|
|
|
static bool ipc_has_event_listeners(enum ipc_command_type event) {
|
|
|
|
for (int i = 0; i < ipc_client_list->length; i++) {
|
2018-04-06 04:50:10 +02:00
|
|
|
struct ipc_client *client = ipc_client_list->items[i];
|
2018-04-06 01:01:03 +02:00
|
|
|
if ((client->subscribed_events & event_mask(event)) == 0) {
|
2018-04-06 04:50:10 +02:00
|
|
|
return true;
|
2018-04-06 01:01:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-06 04:50:10 +02:00
|
|
|
return false;
|
2018-04-06 01:01:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 01:53:15 +01:00
|
|
|
static void ipc_send_event(const char *json_string, enum ipc_command_type event) {
|
|
|
|
struct ipc_client *client;
|
2018-04-06 01:01:03 +02:00
|
|
|
for (int i = 0; i < ipc_client_list->length; i++) {
|
2018-02-28 01:53:15 +01:00
|
|
|
client = ipc_client_list->items[i];
|
|
|
|
if ((client->subscribed_events & event_mask(event)) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
client->current_command = event;
|
|
|
|
if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Unable to send reply to IPC client");
|
2018-06-30 10:02:23 +02:00
|
|
|
/* ipc_send_reply destroys client on error, which also
|
|
|
|
* removes it from the list, so we need to process
|
|
|
|
* current index again */
|
|
|
|
i--;
|
2018-02-28 01:53:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 06:02:29 +02:00
|
|
|
void ipc_event_workspace(struct sway_container *old,
|
|
|
|
struct sway_container *new, const char *change) {
|
2018-04-06 01:01:03 +02:00
|
|
|
if (!ipc_has_event_listeners(IPC_EVENT_WORKSPACE)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Sending workspace::%s event", change);
|
2018-03-29 22:51:36 +02:00
|
|
|
json_object *obj = json_object_new_object();
|
|
|
|
json_object_object_add(obj, "change", json_object_new_string(change));
|
|
|
|
if (strcmp("focus", change) == 0) {
|
|
|
|
if (old) {
|
|
|
|
json_object_object_add(obj, "old",
|
|
|
|
ipc_json_describe_container_recursive(old));
|
|
|
|
} else {
|
|
|
|
json_object_object_add(obj, "old", NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new) {
|
|
|
|
json_object_object_add(obj, "current",
|
|
|
|
ipc_json_describe_container_recursive(new));
|
|
|
|
} else {
|
|
|
|
json_object_object_add(obj, "current", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *json_string = json_object_to_json_string(obj);
|
|
|
|
ipc_send_event(json_string, IPC_EVENT_WORKSPACE);
|
|
|
|
json_object_put(obj);
|
|
|
|
}
|
|
|
|
|
2018-03-30 05:41:33 +02:00
|
|
|
void ipc_event_window(struct sway_container *window, const char *change) {
|
2018-04-06 01:01:03 +02:00
|
|
|
if (!ipc_has_event_listeners(IPC_EVENT_WINDOW)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Sending window::%s event", change);
|
2018-02-28 01:53:15 +01:00
|
|
|
json_object *obj = json_object_new_object();
|
|
|
|
json_object_object_add(obj, "change", json_object_new_string(change));
|
|
|
|
json_object_object_add(obj, "container", ipc_json_describe_container_recursive(window));
|
|
|
|
|
|
|
|
const char *json_string = json_object_to_json_string(obj);
|
|
|
|
ipc_send_event(json_string, IPC_EVENT_WINDOW);
|
2018-03-30 04:10:33 +02:00
|
|
|
json_object_put(obj);
|
2018-02-28 01:53:15 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 23:20:03 +02:00
|
|
|
void ipc_event_barconfig_update(struct bar_config *bar) {
|
2018-04-06 01:01:03 +02:00
|
|
|
if (!ipc_has_event_listeners(IPC_EVENT_BARCONFIG_UPDATE)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Sending barconfig_update event");
|
2018-03-29 23:20:03 +02:00
|
|
|
json_object *json = ipc_json_describe_bar_config(bar);
|
|
|
|
|
|
|
|
const char *json_string = json_object_to_json_string(json);
|
|
|
|
ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE);
|
2018-03-30 04:10:33 +02:00
|
|
|
json_object_put(json);
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:12:14 +02:00
|
|
|
void ipc_event_mode(const char *mode, bool pango) {
|
2018-04-06 01:01:03 +02:00
|
|
|
if (!ipc_has_event_listeners(IPC_EVENT_MODE)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Sending mode::%s event", mode);
|
2018-03-30 04:10:33 +02:00
|
|
|
json_object *obj = json_object_new_object();
|
|
|
|
json_object_object_add(obj, "change", json_object_new_string(mode));
|
2018-07-06 00:12:14 +02:00
|
|
|
json_object_object_add(obj, "pango_markup",
|
|
|
|
json_object_new_boolean(pango));
|
2018-03-30 04:10:33 +02:00
|
|
|
|
|
|
|
const char *json_string = json_object_to_json_string(obj);
|
|
|
|
ipc_send_event(json_string, IPC_EVENT_MODE);
|
|
|
|
json_object_put(obj);
|
2018-03-29 23:20:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-23 03:37:07 +01:00
|
|
|
int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) {
|
|
|
|
struct ipc_client *client = data;
|
|
|
|
|
|
|
|
if (mask & WL_EVENT_ERROR) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "IPC Client socket error, removing client");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask & WL_EVENT_HANGUP) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Client %d hung up", client->fd);
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->write_buffer_len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Client %d writable", client->fd);
|
2017-11-23 03:37:07 +01:00
|
|
|
|
|
|
|
ssize_t written = write(client->fd, client->write_buffer, client->write_buffer_len);
|
|
|
|
|
|
|
|
if (written == -1 && errno == EAGAIN) {
|
|
|
|
return 0;
|
|
|
|
} else if (written == -1) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Unable to send data from queue to IPC client");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(client->write_buffer, client->write_buffer + written, client->write_buffer_len - written);
|
|
|
|
client->write_buffer_len -= written;
|
|
|
|
|
|
|
|
if (client->write_buffer_len == 0 && client->writable_event_source) {
|
|
|
|
wl_event_source_remove(client->writable_event_source);
|
|
|
|
client->writable_event_source = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipc_client_disconnect(struct ipc_client *client) {
|
|
|
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-30 10:03:45 +02:00
|
|
|
shutdown(client->fd, SHUT_RDWR);
|
2017-11-23 03:37:07 +01:00
|
|
|
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_INFO, "IPC Client %d disconnected", client->fd);
|
2017-11-23 03:37:07 +01:00
|
|
|
wl_event_source_remove(client->event_source);
|
|
|
|
if (client->writable_event_source) {
|
|
|
|
wl_event_source_remove(client->writable_event_source);
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
while (i < ipc_client_list->length && ipc_client_list->items[i] != client) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
list_del(ipc_client_list, i);
|
|
|
|
free(client->write_buffer);
|
|
|
|
close(client->fd);
|
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
2018-03-30 06:02:29 +02:00
|
|
|
static void ipc_get_workspaces_callback(struct sway_container *workspace,
|
|
|
|
void *data) {
|
2018-03-30 04:16:43 +02:00
|
|
|
if (workspace->type != C_WORKSPACE) {
|
|
|
|
return;
|
2018-03-29 22:51:36 +02:00
|
|
|
}
|
2018-03-30 04:16:43 +02:00
|
|
|
json_object *workspace_json = ipc_json_describe_container(workspace);
|
|
|
|
// override the default focused indicator because
|
|
|
|
// it's set differently for the get_workspaces reply
|
|
|
|
struct sway_seat *seat =
|
2018-04-02 14:49:38 +02:00
|
|
|
input_manager_get_default_seat(input_manager);
|
2018-04-02 14:45:37 +02:00
|
|
|
struct sway_container *focused_ws = seat_get_focus(seat);
|
2018-03-30 22:12:02 +02:00
|
|
|
if (focused_ws != NULL && focused_ws->type != C_WORKSPACE) {
|
2018-03-30 06:02:29 +02:00
|
|
|
focused_ws = container_parent(focused_ws, C_WORKSPACE);
|
2018-03-30 04:16:43 +02:00
|
|
|
}
|
|
|
|
bool focused = workspace == focused_ws;
|
|
|
|
json_object_object_del(workspace_json, "focused");
|
|
|
|
json_object_object_add(workspace_json, "focused",
|
|
|
|
json_object_new_boolean(focused));
|
|
|
|
json_object_array_add((json_object *)data, workspace_json);
|
2018-03-30 19:56:31 +02:00
|
|
|
|
2018-04-02 14:45:37 +02:00
|
|
|
focused_ws = seat_get_focus_inactive(seat, workspace->parent);
|
2018-03-30 19:56:31 +02:00
|
|
|
if (focused_ws->type != C_WORKSPACE) {
|
|
|
|
focused_ws = container_parent(focused_ws, C_WORKSPACE);
|
|
|
|
}
|
|
|
|
bool visible = workspace == focused_ws;
|
|
|
|
json_object_object_add(workspace_json, "visible",
|
|
|
|
json_object_new_boolean(visible));
|
2018-03-29 22:51:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 23:35:39 +02:00
|
|
|
static void ipc_get_marks_callback(struct sway_container *con, void *data) {
|
|
|
|
json_object *marks = (json_object *)data;
|
|
|
|
if (con->type == C_VIEW && con->sway_view->marks) {
|
|
|
|
for (int i = 0; i < con->sway_view->marks->length; ++i) {
|
|
|
|
char *mark = (char *)con->sway_view->marks->items[i];
|
|
|
|
json_object_array_add(marks, json_object_new_string(mark));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:37:07 +01:00
|
|
|
void ipc_client_handle_command(struct ipc_client *client) {
|
|
|
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = malloc(client->payload_length + 1);
|
|
|
|
if (!buf) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Unable to allocate IPC payload");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (client->payload_length > 0) {
|
|
|
|
// Payload should be fully available
|
|
|
|
ssize_t received = recv(client->fd, buf, client->payload_length, 0);
|
|
|
|
if (received == -1)
|
|
|
|
{
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Unable to receive payload from IPC client");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf[client->payload_length] = '\0';
|
|
|
|
|
2018-06-30 10:18:47 +02:00
|
|
|
bool client_valid = true;
|
2017-11-23 03:37:07 +01:00
|
|
|
switch (client->current_command) {
|
|
|
|
case IPC_COMMAND:
|
|
|
|
{
|
2018-02-24 18:50:24 +01:00
|
|
|
struct cmd_results *results = execute_command(buf, NULL);
|
2018-07-15 07:20:21 +02:00
|
|
|
transaction_commit_dirty();
|
2018-07-05 00:07:59 +02:00
|
|
|
char *json = cmd_results_to_json(results);
|
|
|
|
int length = strlen(json);
|
|
|
|
client_valid = ipc_send_reply(client, json, (uint32_t)length);
|
2018-07-04 11:48:57 +02:00
|
|
|
free(json);
|
2017-11-23 03:37:07 +01:00
|
|
|
free_cmd_results(results);
|
|
|
|
goto exit_cleanup;
|
2017-12-03 14:33:52 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:06:03 +01:00
|
|
|
case IPC_GET_OUTPUTS:
|
|
|
|
{
|
|
|
|
json_object *outputs = json_object_new_array();
|
|
|
|
for (int i = 0; i < root_container.children->length; ++i) {
|
2018-03-30 05:41:33 +02:00
|
|
|
struct sway_container *container = root_container.children->items[i];
|
2017-12-18 14:06:03 +01:00
|
|
|
if (container->type == C_OUTPUT) {
|
|
|
|
json_object_array_add(outputs,
|
|
|
|
ipc_json_describe_container(container));
|
|
|
|
}
|
|
|
|
}
|
2018-06-07 02:10:42 +02:00
|
|
|
struct sway_output *output;
|
|
|
|
wl_list_for_each(output, &root_container.sway_root->outputs, link) {
|
2018-06-05 23:56:32 +02:00
|
|
|
if (!output->swayc) {
|
|
|
|
json_object_array_add(outputs,
|
|
|
|
ipc_json_describe_disabled_output(output));
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 14:06:03 +01:00
|
|
|
const char *json_string = json_object_to_json_string(outputs);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
2017-12-18 14:06:03 +01:00
|
|
|
json_object_put(outputs); // free
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2018-03-29 22:51:36 +02:00
|
|
|
case IPC_GET_WORKSPACES:
|
|
|
|
{
|
|
|
|
json_object *workspaces = json_object_new_array();
|
2018-03-30 06:02:29 +02:00
|
|
|
container_for_each_descendant_dfs(&root_container,
|
|
|
|
ipc_get_workspaces_callback, workspaces);
|
2018-03-29 22:51:36 +02:00
|
|
|
const char *json_string = json_object_to_json_string(workspaces);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
2018-03-29 22:51:36 +02:00
|
|
|
json_object_put(workspaces); // free
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2018-02-28 01:53:15 +01:00
|
|
|
case IPC_SUBSCRIBE:
|
|
|
|
{
|
|
|
|
// TODO: Check if they're permitted to use these events
|
|
|
|
struct json_object *request = json_tokener_parse(buf);
|
|
|
|
if (request == NULL) {
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid = ipc_send_reply(client, "{\"success\": false}", 18);
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Failed to read request");
|
2018-02-28 01:53:15 +01:00
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse requested event types
|
|
|
|
for (size_t i = 0; i < json_object_array_length(request); i++) {
|
|
|
|
const char *event_type = json_object_get_string(json_object_array_get_idx(request, i));
|
|
|
|
if (strcmp(event_type, "workspace") == 0) {
|
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_WORKSPACE);
|
|
|
|
} else if (strcmp(event_type, "barconfig_update") == 0) {
|
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_BARCONFIG_UPDATE);
|
|
|
|
} else if (strcmp(event_type, "mode") == 0) {
|
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_MODE);
|
|
|
|
} else if (strcmp(event_type, "window") == 0) {
|
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_WINDOW);
|
|
|
|
} else if (strcmp(event_type, "modifier") == 0) {
|
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_MODIFIER);
|
|
|
|
} else if (strcmp(event_type, "binding") == 0) {
|
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_BINDING);
|
|
|
|
} else {
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, "{\"success\": false}", 18);
|
2018-02-28 01:53:15 +01:00
|
|
|
json_object_put(request);
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log_errno(WLR_INFO, "Failed to parse request");
|
2018-02-28 01:53:15 +01:00
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_put(request);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid = ipc_send_reply(client, "{\"success\": true}", 17);
|
2018-02-28 01:53:15 +01:00
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:27:38 +01:00
|
|
|
case IPC_GET_INPUTS:
|
|
|
|
{
|
|
|
|
json_object *inputs = json_object_new_array();
|
|
|
|
struct sway_input_device *device = NULL;
|
|
|
|
wl_list_for_each(device, &input_manager->devices, link) {
|
|
|
|
json_object_array_add(inputs, ipc_json_describe_input(device));
|
|
|
|
}
|
|
|
|
const char *json_string = json_object_to_json_string(inputs);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
2017-12-18 20:27:38 +01:00
|
|
|
json_object_put(inputs); // free
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2018-05-11 15:51:47 +02:00
|
|
|
case IPC_GET_SEATS:
|
|
|
|
{
|
|
|
|
json_object *seats = json_object_new_array();
|
|
|
|
struct sway_seat *seat = NULL;
|
|
|
|
wl_list_for_each(seat, &input_manager->seats, link) {
|
|
|
|
json_object_array_add(seats, ipc_json_describe_seat(seat));
|
|
|
|
}
|
|
|
|
const char *json_string = json_object_to_json_string(seats);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
2018-05-11 15:51:47 +02:00
|
|
|
json_object_put(seats); // free
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-12-03 14:33:52 +01:00
|
|
|
case IPC_GET_TREE:
|
|
|
|
{
|
|
|
|
json_object *tree =
|
|
|
|
ipc_json_describe_container_recursive(&root_container);
|
|
|
|
const char *json_string = json_object_to_json_string(tree);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
|
2017-12-03 14:33:52 +01:00
|
|
|
json_object_put(tree);
|
|
|
|
goto exit_cleanup;
|
2017-11-23 03:37:07 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 23:35:39 +02:00
|
|
|
case IPC_GET_MARKS:
|
|
|
|
{
|
|
|
|
json_object *marks = json_object_new_array();
|
|
|
|
container_descendants(&root_container, C_VIEW, ipc_get_marks_callback,
|
|
|
|
marks);
|
|
|
|
const char *json_string = json_object_to_json_string(marks);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
2018-05-22 23:35:39 +02:00
|
|
|
json_object_put(marks);
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:37:07 +01:00
|
|
|
case IPC_GET_VERSION:
|
|
|
|
{
|
|
|
|
json_object *version = ipc_json_get_version();
|
|
|
|
const char *json_string = json_object_to_json_string(version);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
2017-11-23 03:37:07 +01:00
|
|
|
json_object_put(version); // free
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2018-03-29 22:51:36 +02:00
|
|
|
case IPC_GET_BAR_CONFIG:
|
|
|
|
{
|
|
|
|
if (!buf[0]) {
|
|
|
|
// Send list of configured bar IDs
|
|
|
|
json_object *bars = json_object_new_array();
|
2018-03-30 04:23:41 +02:00
|
|
|
for (int i = 0; i < config->bars->length; ++i) {
|
2018-03-29 22:51:36 +02:00
|
|
|
struct bar_config *bar = config->bars->items[i];
|
|
|
|
json_object_array_add(bars, json_object_new_string(bar->id));
|
|
|
|
}
|
|
|
|
const char *json_string = json_object_to_json_string(bars);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string,
|
|
|
|
(uint32_t)strlen(json_string));
|
2018-03-29 22:51:36 +02:00
|
|
|
json_object_put(bars); // free
|
|
|
|
} else {
|
|
|
|
// Send particular bar's details
|
|
|
|
struct bar_config *bar = NULL;
|
2018-03-30 04:23:41 +02:00
|
|
|
for (int i = 0; i < config->bars->length; ++i) {
|
2018-03-29 22:51:36 +02:00
|
|
|
bar = config->bars->items[i];
|
|
|
|
if (strcmp(buf, bar->id) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bar = NULL;
|
|
|
|
}
|
|
|
|
if (!bar) {
|
|
|
|
const char *error = "{ \"success\": false, \"error\": \"No bar with that ID\" }";
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, error, (uint32_t)strlen(error));
|
2018-03-29 22:51:36 +02:00
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
json_object *json = ipc_json_describe_bar_config(bar);
|
|
|
|
const char *json_string = json_object_to_json_string(json);
|
2018-06-30 10:18:47 +02:00
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string,
|
|
|
|
(uint32_t)strlen(json_string));
|
2018-03-29 22:51:36 +02:00
|
|
|
json_object_put(json); // free
|
|
|
|
}
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:32:42 +02:00
|
|
|
case IPC_GET_BINDING_MODES:
|
|
|
|
{
|
|
|
|
json_object *modes = json_object_new_array();
|
|
|
|
for (int i = 0; i < config->modes->length; i++) {
|
|
|
|
struct sway_mode *mode = config->modes->items[i];
|
|
|
|
json_object_array_add(modes, json_object_new_string(mode->name));
|
|
|
|
}
|
|
|
|
const char *json_string = json_object_to_json_string(modes);
|
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
|
|
|
json_object_put(modes); // free
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:34:47 +02:00
|
|
|
case IPC_GET_CONFIG:
|
|
|
|
{
|
|
|
|
json_object *json = json_object_new_object();
|
|
|
|
json_object_object_add(json, "config", json_object_new_string(config->current_config));
|
|
|
|
const char *json_string = json_object_to_json_string(json);
|
|
|
|
client_valid =
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
|
|
|
json_object_put(json); // free
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:37:07 +01:00
|
|
|
default:
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_INFO, "Unknown IPC command type %i", client->current_command);
|
2017-11-23 03:37:07 +01:00
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit_cleanup:
|
2018-06-30 10:18:47 +02:00
|
|
|
if (client_valid) {
|
|
|
|
client->payload_length = 0;
|
|
|
|
}
|
2017-11-23 03:37:07 +01:00
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) {
|
|
|
|
assert(payload);
|
|
|
|
|
|
|
|
char data[ipc_header_size];
|
|
|
|
uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic));
|
|
|
|
|
|
|
|
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
|
|
|
data32[0] = payload_length;
|
|
|
|
data32[1] = client->current_command;
|
|
|
|
|
|
|
|
while (client->write_buffer_len + ipc_header_size + payload_length >=
|
|
|
|
client->write_buffer_size) {
|
|
|
|
client->write_buffer_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->write_buffer_size > 4e6) { // 4 MB
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *new_buffer = realloc(client->write_buffer, client->write_buffer_size);
|
|
|
|
if (!new_buffer) {
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer");
|
2017-11-23 03:37:07 +01:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
client->write_buffer = new_buffer;
|
|
|
|
|
|
|
|
memcpy(client->write_buffer + client->write_buffer_len, data, ipc_header_size);
|
|
|
|
client->write_buffer_len += ipc_header_size;
|
|
|
|
memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length);
|
|
|
|
client->write_buffer_len += payload_length;
|
|
|
|
|
|
|
|
if (!client->writable_event_source) {
|
|
|
|
client->writable_event_source = wl_event_loop_add_fd(
|
|
|
|
server.wl_event_loop, client->fd, WL_EVENT_WRITABLE,
|
|
|
|
ipc_client_handle_writable, client);
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload);
|
2017-11-23 03:37:07 +01:00
|
|
|
return true;
|
|
|
|
}
|