You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
841 lines
25 KiB
841 lines
25 KiB
9 years ago
|
// See https://i3wm.org/docs/ipc.html for protocol information
|
||
7 years ago
|
#ifndef __FreeBSD__
|
||
|
// Any value will hide SOCK_CLOEXEC on FreeBSD (__BSD_VISIBLE=0)
|
||
8 years ago
|
#define _XOPEN_SOURCE 700
|
||
7 years ago
|
#endif
|
||
9 years ago
|
#include <errno.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <sys/un.h>
|
||
9 years ago
|
#include <stdbool.h>
|
||
9 years ago
|
#include <unistd.h>
|
||
9 years ago
|
#include <stdlib.h>
|
||
9 years ago
|
#include <sys/ioctl.h>
|
||
9 years ago
|
#include <fcntl.h>
|
||
9 years ago
|
#include <json-c/json.h>
|
||
9 years ago
|
#include <list.h>
|
||
9 years ago
|
#include <libinput.h>
|
||
8 years ago
|
#ifdef __linux__
|
||
|
struct ucred {
|
||
|
pid_t pid;
|
||
|
uid_t uid;
|
||
|
gid_t gid;
|
||
|
};
|
||
|
#endif
|
||
8 years ago
|
#include "sway/ipc-json.h"
|
||
|
#include "sway/ipc-server.h"
|
||
8 years ago
|
#include "sway/security.h"
|
||
8 years ago
|
#include "sway/config.h"
|
||
|
#include "sway/commands.h"
|
||
|
#include "sway/input.h"
|
||
7 years ago
|
#include "sway/server.h"
|
||
8 years ago
|
#include "stringop.h"
|
||
9 years ago
|
#include "log.h"
|
||
9 years ago
|
#include "list.h"
|
||
9 years ago
|
#include "util.h"
|
||
9 years ago
|
|
||
|
static int ipc_socket = -1;
|
||
7 years ago
|
static struct wl_event_source *ipc_event_source = NULL;
|
||
9 years ago
|
static struct sockaddr_un *ipc_sockaddr = NULL;
|
||
9 years ago
|
static list_t *ipc_client_list = NULL;
|
||
9 years ago
|
|
||
9 years ago
|
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
||
|
|
||
9 years ago
|
struct ipc_client {
|
||
7 years ago
|
struct wl_event_source *event_source;
|
||
|
struct wl_event_source *writable_event_source;
|
||
9 years ago
|
int fd;
|
||
|
uint32_t payload_length;
|
||
8 years ago
|
uint32_t security_policy;
|
||
9 years ago
|
enum ipc_command_type current_command;
|
||
9 years ago
|
enum ipc_command_type subscribed_events;
|
||
7 years ago
|
size_t write_buffer_len;
|
||
|
size_t write_buffer_size;
|
||
|
char *write_buffer;
|
||
9 years ago
|
};
|
||
9 years ago
|
|
||
9 years ago
|
struct sockaddr_un *ipc_user_sockaddr(void);
|
||
9 years ago
|
int ipc_handle_connection(int fd, uint32_t mask, void *data);
|
||
|
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data);
|
||
7 years ago
|
int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data);
|
||
9 years ago
|
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);
|
||
9 years ago
|
void ipc_get_workspaces_callback(swayc_t *workspace, void *data);
|
||
9 years ago
|
void ipc_get_outputs_callback(swayc_t *container, void *data);
|
||
8 years ago
|
static void ipc_get_marks_callback(swayc_t *container, void *data);
|
||
9 years ago
|
|
||
9 years ago
|
void ipc_init(void) {
|
||
9 years ago
|
ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
||
|
if (ipc_socket == -1) {
|
||
|
sway_abort("Unable to create IPC socket");
|
||
|
}
|
||
|
|
||
9 years ago
|
ipc_sockaddr = ipc_user_sockaddr();
|
||
|
|
||
9 years ago
|
// 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) {
|
||
9 years ago
|
strncpy(ipc_sockaddr->sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr->sun_path));
|
||
9 years ago
|
ipc_sockaddr->sun_path[sizeof(ipc_sockaddr->sun_path) - 1] = 0;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
unlink(ipc_sockaddr->sun_path);
|
||
|
if (bind(ipc_socket, (struct sockaddr *)ipc_sockaddr, sizeof(*ipc_sockaddr)) == -1) {
|
||
9 years ago
|
sway_abort("Unable to bind IPC socket");
|
||
|
}
|
||
|
|
||
|
if (listen(ipc_socket, 3) == -1) {
|
||
|
sway_abort("Unable to listen on IPC socket");
|
||
|
}
|
||
|
|
||
9 years ago
|
// Set i3 IPC socket path so that i3-msg works out of the box
|
||
9 years ago
|
setenv("I3SOCK", ipc_sockaddr->sun_path, 1);
|
||
|
setenv("SWAYSOCK", ipc_sockaddr->sun_path, 1);
|
||
9 years ago
|
|
||
9 years ago
|
ipc_client_list = create_list();
|
||
|
|
||
7 years ago
|
ipc_event_source = wl_event_loop_add_fd(server.wl_event_loop, ipc_socket,
|
||
|
WL_EVENT_READABLE, ipc_handle_connection, NULL);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
void ipc_terminate(void) {
|
||
9 years ago
|
if (ipc_event_source) {
|
||
7 years ago
|
wl_event_source_remove(ipc_event_source);
|
||
9 years ago
|
}
|
||
|
close(ipc_socket);
|
||
9 years ago
|
unlink(ipc_sockaddr->sun_path);
|
||
9 years ago
|
|
||
9 years ago
|
list_free(ipc_client_list);
|
||
|
|
||
9 years ago
|
if (ipc_sockaddr) {
|
||
|
free(ipc_sockaddr);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
struct sockaddr_un *ipc_user_sockaddr(void) {
|
||
9 years ago
|
struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un));
|
||
9 years ago
|
if (ipc_sockaddr == NULL) {
|
||
8 years ago
|
sway_abort("Can't allocate ipc_sockaddr");
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
ipc_sockaddr->sun_family = AF_UNIX;
|
||
|
int path_size = sizeof(ipc_sockaddr->sun_path);
|
||
9 years ago
|
|
||
9 years ago
|
// 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())) {
|
||
8 years ago
|
sway_abort("Socket path won't fit into ipc_sockaddr->sun_path");
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
return ipc_sockaddr;
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
static pid_t get_client_pid(int client_fd) {
|
||
8 years ago
|
// FreeBSD supports getting uid/gid, but not pid
|
||
|
#ifdef __linux__
|
||
8 years ago
|
struct ucred ucred;
|
||
|
socklen_t len = sizeof(struct ucred);
|
||
|
|
||
|
if (getsockopt(client_fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return ucred.pid;
|
||
8 years ago
|
#else
|
||
|
return -1;
|
||
|
#endif
|
||
8 years ago
|
}
|
||
|
|
||
9 years ago
|
int ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
||
9 years ago
|
(void) fd; (void) data;
|
||
9 years ago
|
sway_log(L_DEBUG, "Event on IPC listening socket");
|
||
7 years ago
|
assert(mask == WL_EVENT_READABLE);
|
||
9 years ago
|
|
||
9 years ago
|
int client_fd = accept(ipc_socket, NULL, NULL);
|
||
|
if (client_fd == -1) {
|
||
8 years ago
|
sway_log_errno(L_ERROR, "Unable to accept IPC client connection");
|
||
9 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
9 years ago
|
int flags;
|
||
8 years ago
|
if ((flags = fcntl(client_fd, F_GETFD)) == -1
|
||
|
|| fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
|
||
8 years ago
|
sway_log_errno(L_ERROR, "Unable to set CLOEXEC on IPC client socket");
|
||
9 years ago
|
close(client_fd);
|
||
9 years ago
|
return 0;
|
||
|
}
|
||
7 years ago
|
if ((flags = fcntl(client_fd, F_GETFL)) == -1
|
||
|
|| fcntl(client_fd, F_SETFL, flags|O_NONBLOCK) == -1) {
|
||
|
sway_log_errno(L_ERROR, "Unable to set NONBLOCK on IPC client socket");
|
||
|
close(client_fd);
|
||
|
return 0;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
struct ipc_client* client = malloc(sizeof(struct ipc_client));
|
||
8 years ago
|
if (!client) {
|
||
|
sway_log(L_ERROR, "Unable to allocate ipc client");
|
||
|
close(client_fd);
|
||
|
return 0;
|
||
|
}
|
||
9 years ago
|
client->payload_length = 0;
|
||
|
client->fd = client_fd;
|
||
8 years ago
|
client->subscribed_events = 0;
|
||
7 years ago
|
client->event_source = wl_event_loop_add_fd(server.wl_event_loop, client_fd,
|
||
|
WL_EVENT_READABLE, ipc_client_handle_readable, client);
|
||
7 years ago
|
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) {
|
||
|
sway_log(L_ERROR, "Unable to allocate ipc client write buffer");
|
||
|
close(client_fd);
|
||
|
return 0;
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
pid_t pid = get_client_pid(client->fd);
|
||
8 years ago
|
client->security_policy = get_ipc_policy_mask(pid);
|
||
8 years ago
|
|
||
7 years ago
|
sway_log(L_DEBUG, "New client: fd %d, pid %d", client_fd, pid);
|
||
|
|
||
9 years ago
|
list_add(ipc_client_list, client);
|
||
|
|
||
9 years ago
|
return 0;
|
||
|
}
|
||
9 years ago
|
|
||
|
static const int ipc_header_size = sizeof(ipc_magic)+8;
|
||
|
|
||
9 years ago
|
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
|
||
|
struct ipc_client *client = data;
|
||
9 years ago
|
|
||
7 years ago
|
if (mask & WL_EVENT_ERROR) {
|
||
8 years ago
|
sway_log(L_ERROR, "IPC Client socket error, removing client");
|
||
9 years ago
|
ipc_client_disconnect(client);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
7 years ago
|
if (mask & WL_EVENT_HANGUP) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Client %d hung up", client->fd);
|
||
9 years ago
|
ipc_client_disconnect(client);
|
||
|
return 0;
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
sway_log(L_DEBUG, "Client %d readable", client->fd);
|
||
|
|
||
9 years ago
|
int read_available;
|
||
9 years ago
|
if (ioctl(client_fd, FIONREAD, &read_available) == -1) {
|
||
|
sway_log_errno(L_INFO, "Unable to read IPC socket buffer size");
|
||
|
ipc_client_disconnect(client);
|
||
|
return 0;
|
||
|
}
|
||
9 years ago
|
|
||
|
// Wait for the rest of the command payload in case the header has already been read
|
||
|
if (client->payload_length > 0) {
|
||
9 years ago
|
if ((uint32_t)read_available >= client->payload_length) {
|
||
9 years ago
|
ipc_client_handle_command(client);
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (read_available < ipc_header_size) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
9 years ago
|
uint8_t buf[ipc_header_size];
|
||
|
uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic));
|
||
7 years ago
|
// Should be fully available, because read_available >= ipc_header_size
|
||
9 years ago
|
ssize_t received = recv(client_fd, buf, ipc_header_size, 0);
|
||
|
if (received == -1) {
|
||
|
sway_log_errno(L_INFO, "Unable to receive header from IPC client");
|
||
|
ipc_client_disconnect(client);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) {
|
||
9 years ago
|
sway_log(L_DEBUG, "IPC header check failed");
|
||
9 years ago
|
ipc_client_disconnect(client);
|
||
|
return 0;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
client->payload_length = buf32[0];
|
||
|
client->current_command = (enum ipc_command_type)buf32[1];
|
||
9 years ago
|
|
||
9 years ago
|
if (read_available - received >= (long)client->payload_length) {
|
||
9 years ago
|
ipc_client_handle_command(client);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
7 years ago
|
int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) {
|
||
|
struct ipc_client *client = data;
|
||
|
|
||
7 years ago
|
if (mask & WL_EVENT_ERROR) {
|
||
7 years ago
|
sway_log(L_ERROR, "IPC Client socket error, removing client");
|
||
|
ipc_client_disconnect(client);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
7 years ago
|
if (mask & WL_EVENT_HANGUP) {
|
||
7 years ago
|
sway_log(L_DEBUG, "Client %d hung up", client->fd);
|
||
|
ipc_client_disconnect(client);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (client->write_buffer_len <= 0) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
sway_log(L_DEBUG, "Client %d writable", client->fd);
|
||
|
|
||
|
ssize_t written = write(client->fd, client->write_buffer, client->write_buffer_len);
|
||
|
|
||
|
if (written == -1 && errno == EAGAIN) {
|
||
|
return 0;
|
||
|
} else if (written == -1) {
|
||
|
sway_log_errno(L_INFO, "Unable to send data from queue to IPC client");
|
||
|
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) {
|
||
7 years ago
|
wl_event_source_remove(client->writable_event_source);
|
||
7 years ago
|
client->writable_event_source = NULL;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
8 years ago
|
void ipc_client_disconnect(struct ipc_client *client) {
|
||
9 years ago
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (client->fd != -1) {
|
||
|
shutdown(client->fd, SHUT_RDWR);
|
||
|
}
|
||
|
|
||
9 years ago
|
sway_log(L_INFO, "IPC Client %d disconnected", client->fd);
|
||
7 years ago
|
wl_event_source_remove(client->event_source);
|
||
7 years ago
|
if (client->writable_event_source) {
|
||
7 years ago
|
wl_event_source_remove(client->writable_event_source);
|
||
7 years ago
|
}
|
||
9 years ago
|
int i = 0;
|
||
|
while (i < ipc_client_list->length && ipc_client_list->items[i] != client) i++;
|
||
|
list_del(ipc_client_list, i);
|
||
7 years ago
|
free(client->write_buffer);
|
||
9 years ago
|
close(client->fd);
|
||
|
free(client);
|
||
|
}
|
||
|
|
||
9 years ago
|
bool output_by_name_test(swayc_t *view, void *data) {
|
||
|
char *name = (char *)data;
|
||
|
if (view->type != C_OUTPUT) {
|
||
|
return false;
|
||
|
}
|
||
|
return !strcmp(name, view->name);
|
||
|
}
|
||
|
|
||
8 years ago
|
// greedy wildcard (only "*") matching
|
||
|
bool mime_type_matches(const char *mime_type, const char *pattern) {
|
||
8 years ago
|
const char *wildcard = NULL;
|
||
8 years ago
|
while (*mime_type && *pattern) {
|
||
|
if (*pattern == '*' && !wildcard) {
|
||
|
wildcard = pattern;
|
||
|
++pattern;
|
||
|
}
|
||
|
|
||
|
if (*mime_type != *pattern) {
|
||
|
if (!wildcard)
|
||
|
return false;
|
||
|
|
||
|
pattern = wildcard;
|
||
|
++mime_type;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
++mime_type;
|
||
|
++pattern;
|
||
|
}
|
||
|
|
||
|
while (*pattern == '*') {
|
||
|
++pattern;
|
||
|
}
|
||
|
|
||
|
return (*mime_type == *pattern);
|
||
|
}
|
||
|
|
||
9 years ago
|
void ipc_client_handle_command(struct ipc_client *client) {
|
||
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
9 years ago
|
char *buf = malloc(client->payload_length + 1);
|
||
9 years ago
|
if (!buf) {
|
||
8 years ago
|
sway_log_errno(L_INFO, "Unable to allocate IPC payload");
|
||
9 years ago
|
ipc_client_disconnect(client);
|
||
|
return;
|
||
|
}
|
||
8 years ago
|
if (client->payload_length > 0) {
|
||
7 years ago
|
// Payload should be fully available
|
||
9 years ago
|
ssize_t received = recv(client->fd, buf, client->payload_length, 0);
|
||
|
if (received == -1)
|
||
|
{
|
||
|
sway_log_errno(L_INFO, "Unable to receive payload from IPC client");
|
||
|
ipc_client_disconnect(client);
|
||
9 years ago
|
free(buf);
|
||
9 years ago
|
return;
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
buf[client->payload_length] = '\0';
|
||
9 years ago
|
|
||
8 years ago
|
const char *error_denied = "{ \"success\": false, \"error\": \"Permission denied\" }";
|
||
|
|
||
9 years ago
|
switch (client->current_command) {
|
||
9 years ago
|
case IPC_COMMAND:
|
||
|
{
|
||
8 years ago
|
if (!(client->security_policy & IPC_FEATURE_COMMAND)) {
|
||
|
goto exit_denied;
|
||
|
}
|
||
8 years ago
|
struct cmd_results *results = handle_command(buf, CONTEXT_IPC);
|
||
9 years ago
|
const char *json = cmd_results_to_json(results);
|
||
|
char reply[256];
|
||
|
int length = snprintf(reply, sizeof(reply), "%s", json);
|
||
9 years ago
|
ipc_send_reply(client, reply, (uint32_t) length);
|
||
9 years ago
|
free_cmd_results(results);
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
case IPC_SUBSCRIBE:
|
||
|
{
|
||
8 years ago
|
// TODO: Check if they're permitted to use these events
|
||
9 years ago
|
struct json_object *request = json_tokener_parse(buf);
|
||
|
if (request == NULL) {
|
||
|
ipc_send_reply(client, "{\"success\": false}", 18);
|
||
9 years ago
|
sway_log_errno(L_INFO, "Failed to read request");
|
||
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
|
|
||
|
// parse requested event types
|
||
|
for (int 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) {
|
||
8 years ago
|
client->subscribed_events |= event_mask(IPC_EVENT_WORKSPACE);
|
||
9 years ago
|
} else if (strcmp(event_type, "barconfig_update") == 0) {
|
||
8 years ago
|
client->subscribed_events |= event_mask(IPC_EVENT_BARCONFIG_UPDATE);
|
||
9 years ago
|
} else if (strcmp(event_type, "mode") == 0) {
|
||
8 years ago
|
client->subscribed_events |= event_mask(IPC_EVENT_MODE);
|
||
9 years ago
|
} else if (strcmp(event_type, "window") == 0) {
|
||
8 years ago
|
client->subscribed_events |= event_mask(IPC_EVENT_WINDOW);
|
||
9 years ago
|
} else if (strcmp(event_type, "modifier") == 0) {
|
||
8 years ago
|
client->subscribed_events |= event_mask(IPC_EVENT_MODIFIER);
|
||
9 years ago
|
} else if (strcmp(event_type, "binding") == 0) {
|
||
8 years ago
|
client->subscribed_events |= event_mask(IPC_EVENT_BINDING);
|
||
9 years ago
|
} else {
|
||
9 years ago
|
ipc_send_reply(client, "{\"success\": false}", 18);
|
||
|
json_object_put(request);
|
||
9 years ago
|
sway_log_errno(L_INFO, "Failed to parse request");
|
||
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
json_object_put(request);
|
||
|
|
||
|
ipc_send_reply(client, "{\"success\": true}", 17);
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
case IPC_GET_WORKSPACES:
|
||
|
{
|
||
8 years ago
|
if (!(client->security_policy & IPC_FEATURE_GET_WORKSPACES)) {
|
||
8 years ago
|
goto exit_denied;
|
||
|
}
|
||
9 years ago
|
json_object *workspaces = json_object_new_array();
|
||
9 years ago
|
container_map(&root_container, ipc_get_workspaces_callback, workspaces);
|
||
9 years ago
|
const char *json_string = json_object_to_json_string(workspaces);
|
||
|
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
|
||
|
json_object_put(workspaces); // free
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
case IPC_GET_INPUTS:
|
||
|
{
|
||
8 years ago
|
if (!(client->security_policy & IPC_FEATURE_GET_INPUTS)) {
|
||
8 years ago
|
goto exit_denied;
|
||
|
}
|
||
9 years ago
|
json_object *inputs = json_object_new_array();
|
||
7 years ago
|
/* TODO WLR
|
||
9 years ago
|
if (input_devices) {
|
||
8 years ago
|
for(int i = 0; i<input_devices->length; i++) {
|
||
9 years ago
|
struct libinput_device *device = input_devices->items[i];
|
||
8 years ago
|
json_object_array_add(inputs, ipc_json_describe_input(device));
|
||
9 years ago
|
}
|
||
|
}
|
||
7 years ago
|
*/
|
||
9 years ago
|
const char *json_string = json_object_to_json_string(inputs);
|
||
|
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
|
||
|
json_object_put(inputs);
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
case IPC_GET_OUTPUTS:
|
||
|
{
|
||
8 years ago
|
if (!(client->security_policy & IPC_FEATURE_GET_OUTPUTS)) {
|
||
8 years ago
|
goto exit_denied;
|
||
|
}
|
||
9 years ago
|
json_object *outputs = json_object_new_array();
|
||
9 years ago
|
container_map(&root_container, ipc_get_outputs_callback, outputs);
|
||
9 years ago
|
const char *json_string = json_object_to_json_string(outputs);
|
||
|
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
|
||
|
json_object_put(outputs); // free
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
case IPC_GET_TREE:
|
||
|
{
|
||
8 years ago
|
if (!(client->security_policy & IPC_FEATURE_GET_TREE)) {
|
||
|
goto exit_denied;
|
||
|
}
|
||
9 years ago
|
json_object *tree = ipc_json_describe_container_recursive(&root_container);
|
||
|
const char *json_string = json_object_to_json_string(tree);
|
||
|
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
|
||
|
json_object_put(tree);
|
||
|
goto exit_cleanup;
|
||
|
}
|
||
|
|
||
8 years ago
|
case IPC_GET_MARKS:
|
||
|
{
|
||
|
if (!(client->security_policy & IPC_FEATURE_GET_MARKS)) {
|
||
|
goto exit_denied;
|
||
|
}
|
||
|
json_object *marks = json_object_new_array();
|
||
|
container_map(&root_container, ipc_get_marks_callback, marks);
|
||
|
const char *json_string = json_object_to_json_string(marks);
|
||
|
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
|
||
|
json_object_put(marks);
|
||
|
goto exit_cleanup;
|
||
|
}
|
||
|
|
||
9 years ago
|
case IPC_GET_VERSION:
|
||
|
{
|
||
9 years ago
|
json_object *version = ipc_json_get_version();
|
||
|
const char *json_string = json_object_to_json_string(version);
|
||
9 years ago
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
||
9 years ago
|
json_object_put(version); // free
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
case IPC_GET_BAR_CONFIG:
|
||
|
{
|
||
8 years ago
|
if (!(client->security_policy & IPC_FEATURE_GET_BAR_CONFIG)) {
|
||
|
goto exit_denied;
|
||
|
}
|
||
9 years ago
|
if (!buf[0]) {
|
||
|
// Send list of configured bar IDs
|
||
|
json_object *bars = json_object_new_array();
|
||
|
int i;
|
||
|
for (i = 0; i < config->bars->length; ++i) {
|
||
|
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);
|
||
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
||
|
json_object_put(bars); // free
|
||
|
} else {
|
||
|
// Send particular bar's details
|
||
|
struct bar_config *bar = NULL;
|
||
|
int i;
|
||
|
for (i = 0; i < config->bars->length; ++i) {
|
||
|
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\" }";
|
||
|
ipc_send_reply(client, error, (uint32_t)strlen(error));
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
json_object *json = ipc_json_describe_bar_config(bar);
|
||
9 years ago
|
const char *json_string = json_object_to_json_string(json);
|
||
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
||
|
json_object_put(json); // free
|
||
|
}
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
case IPC_GET_CLIPBOARD:
|
||
7 years ago
|
// TODO WLR
|
||
|
break;
|
||
8 years ago
|
|
||
9 years ago
|
default:
|
||
|
sway_log(L_INFO, "Unknown IPC command type %i", client->current_command);
|
||
9 years ago
|
goto exit_cleanup;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
exit_denied:
|
||
8 years ago
|
ipc_send_reply(client, error_denied, (uint32_t)strlen(error_denied));
|
||
8 years ago
|
sway_log(L_DEBUG, "Denied IPC client access to %i", client->current_command);
|
||
8 years ago
|
|
||
9 years ago
|
exit_cleanup:
|
||
9 years ago
|
client->payload_length = 0;
|
||
9 years ago
|
free(buf);
|
||
9 years ago
|
return;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) {
|
||
9 years ago
|
assert(payload);
|
||
|
|
||
9 years ago
|
char data[ipc_header_size];
|
||
9 years ago
|
uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic));
|
||
9 years ago
|
|
||
9 years ago
|
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
||
9 years ago
|
data32[0] = payload_length;
|
||
|
data32[1] = client->current_command;
|
||
9 years ago
|
|
||
7 years ago
|
while (client->write_buffer_len + ipc_header_size + payload_length >=
|
||
|
client->write_buffer_size) {
|
||
|
client->write_buffer_size *= 2;
|
||
|
}
|
||
|
|
||
7 years ago
|
// TODO: reduce the limit back to 4 MB when screenshooter is implemented
|
||
|
if (client->write_buffer_size > (1 << 28)) { // 256 MB
|
||
7 years ago
|
sway_log(L_ERROR, "Client write buffer too big, disconnecting client");
|
||
|
ipc_client_disconnect(client);
|
||
9 years ago
|
return false;
|
||
|
}
|
||
|
|
||
7 years ago
|
char *new_buffer = realloc(client->write_buffer, client->write_buffer_size);
|
||
|
if (!new_buffer) {
|
||
|
sway_log(L_ERROR, "Unable to reallocate ipc client write buffer");
|
||
|
ipc_client_disconnect(client);
|
||
9 years ago
|
return false;
|
||
|
}
|
||
7 years ago
|
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) {
|
||
7 years ago
|
client->writable_event_source = wl_event_loop_add_fd(
|
||
|
server.wl_event_loop, client->fd, WL_EVENT_WRITABLE,
|
||
|
ipc_client_handle_writable, client);
|
||
7 years ago
|
}
|
||
9 years ago
|
|
||
7 years ago
|
sway_log(L_DEBUG, "Added IPC reply to client %d queue: %s", client->fd, payload);
|
||
8 years ago
|
|
||
9 years ago
|
return true;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void ipc_get_workspaces_callback(swayc_t *workspace, void *data) {
|
||
|
if (workspace->type == C_WORKSPACE) {
|
||
8 years ago
|
json_object *workspace_json = ipc_json_describe_container(workspace);
|
||
|
// override the default focused indicator because
|
||
|
// it's set differently for the get_workspaces reply
|
||
|
bool focused = root_container.focused == workspace->parent && workspace->parent->focused == workspace;
|
||
|
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);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void ipc_get_outputs_callback(swayc_t *container, void *data) {
|
||
|
if (container->type == C_OUTPUT) {
|
||
9 years ago
|
json_object_array_add((json_object *)data, ipc_json_describe_container(container));
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
static void ipc_get_marks_callback(swayc_t *container, void *data) {
|
||
|
json_object *object = (json_object *)data;
|
||
|
if (container->marks) {
|
||
|
for (int i = 0; i < container->marks->length; ++i) {
|
||
|
char *mark = (char *)container->marks->items[i];
|
||
|
json_object_array_add(object, json_object_new_string(mark));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void ipc_send_event(const char *json_string, enum ipc_command_type event) {
|
||
8 years ago
|
static struct {
|
||
|
enum ipc_command_type event;
|
||
|
enum ipc_feature feature;
|
||
|
} security_mappings[] = {
|
||
|
{ IPC_EVENT_WORKSPACE, IPC_FEATURE_EVENT_WORKSPACE },
|
||
|
{ IPC_EVENT_OUTPUT, IPC_FEATURE_EVENT_OUTPUT },
|
||
|
{ IPC_EVENT_MODE, IPC_FEATURE_EVENT_MODE },
|
||
|
{ IPC_EVENT_WINDOW, IPC_FEATURE_EVENT_WINDOW },
|
||
|
{ IPC_EVENT_BINDING, IPC_FEATURE_EVENT_BINDING },
|
||
|
{ IPC_EVENT_INPUT, IPC_FEATURE_EVENT_INPUT }
|
||
|
};
|
||
|
|
||
|
uint32_t security_mask = 0;
|
||
|
for (size_t i = 0; i < sizeof(security_mappings) / sizeof(security_mappings[0]); ++i) {
|
||
|
if (security_mappings[i].event == event) {
|
||
|
security_mask = security_mappings[i].feature;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
int i;
|
||
|
struct ipc_client *client;
|
||
|
for (i = 0; i < ipc_client_list->length; i++) {
|
||
|
client = ipc_client_list->items[i];
|
||
8 years ago
|
if (!(client->security_policy & security_mask)) {
|
||
|
continue;
|
||
|
}
|
||
8 years ago
|
if ((client->subscribed_events & event_mask(event)) == 0) {
|
||
9 years ago
|
continue;
|
||
|
}
|
||
|
client->current_command = event;
|
||
9 years ago
|
if (!ipc_send_reply(client, json_string, (uint32_t) strlen(json_string))) {
|
||
|
sway_log_errno(L_INFO, "Unable to send reply to IPC client");
|
||
|
ipc_client_disconnect(client);
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Sending workspace::%s event", change);
|
||
9 years ago
|
json_object *obj = json_object_new_object();
|
||
9 years ago
|
json_object_object_add(obj, "change", json_object_new_string(change));
|
||
|
if (strcmp("focus", change) == 0) {
|
||
|
if (old) {
|
||
8 years ago
|
json_object_object_add(obj, "old", ipc_json_describe_container_recursive(old));
|
||
9 years ago
|
} else {
|
||
|
json_object_object_add(obj, "old", NULL);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (new) {
|
||
8 years ago
|
json_object_object_add(obj, "current", ipc_json_describe_container_recursive(new));
|
||
9 years ago
|
} else {
|
||
9 years ago
|
json_object_object_add(obj, "current", NULL);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
const char *json_string = json_object_to_json_string(obj);
|
||
9 years ago
|
ipc_send_event(json_string, IPC_EVENT_WORKSPACE);
|
||
9 years ago
|
|
||
|
json_object_put(obj); // free
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
void ipc_event_window(swayc_t *window, const char *change) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Sending window::%s event", change);
|
||
9 years ago
|
json_object *obj = json_object_new_object();
|
||
|
json_object_object_add(obj, "change", json_object_new_string(change));
|
||
7 years ago
|
json_object_object_add(obj, "container", ipc_json_describe_container_recursive(window));
|
||
9 years ago
|
|
||
|
const char *json_string = json_object_to_json_string(obj);
|
||
|
ipc_send_event(json_string, IPC_EVENT_WINDOW);
|
||
|
|
||
|
json_object_put(obj); // free
|
||
|
}
|
||
|
|
||
9 years ago
|
void ipc_event_barconfig_update(struct bar_config *bar) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Sending barconfig_update event");
|
||
9 years ago
|
json_object *json = ipc_json_describe_bar_config(bar);
|
||
|
const char *json_string = json_object_to_json_string(json);
|
||
9 years ago
|
ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE);
|
||
9 years ago
|
|
||
|
json_object_put(json); // free
|
||
|
}
|
||
9 years ago
|
|
||
|
void ipc_event_mode(const char *mode) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Sending mode::%s event", mode);
|
||
9 years ago
|
json_object *obj = json_object_new_object();
|
||
|
json_object_object_add(obj, "change", json_object_new_string(mode));
|
||
|
|
||
|
const char *json_string = json_object_to_json_string(obj);
|
||
|
ipc_send_event(json_string, IPC_EVENT_MODE);
|
||
|
|
||
|
json_object_put(obj); // free
|
||
|
}
|
||
9 years ago
|
|
||
|
void ipc_event_modifier(uint32_t modifier, const char *state) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Sending modifier::%s event", state);
|
||
9 years ago
|
json_object *obj = json_object_new_object();
|
||
|
json_object_object_add(obj, "change", json_object_new_string(state));
|
||
|
|
||
9 years ago
|
const char *modifier_name = get_modifier_name_by_mask(modifier);
|
||
9 years ago
|
json_object_object_add(obj, "modifier", json_object_new_string(modifier_name));
|
||
|
|
||
|
const char *json_string = json_object_to_json_string(obj);
|
||
|
ipc_send_event(json_string, IPC_EVENT_MODIFIER);
|
||
|
|
||
|
json_object_put(obj); // free
|
||
|
}
|
||
9 years ago
|
|
||
|
static void ipc_event_binding(json_object *sb_obj) {
|
||
8 years ago
|
sway_log(L_DEBUG, "Sending binding::run event");
|
||
9 years ago
|
json_object *obj = json_object_new_object();
|
||
|
json_object_object_add(obj, "change", json_object_new_string("run"));
|
||
|
json_object_object_add(obj, "binding", sb_obj);
|
||
|
|
||
|
const char *json_string = json_object_to_json_string(obj);
|
||
|
ipc_send_event(json_string, IPC_EVENT_BINDING);
|
||
|
|
||
|
json_object_put(obj); // free
|
||
|
}
|
||
|
|
||
|
void ipc_event_binding_keyboard(struct sway_binding *sb) {
|
||
|
json_object *sb_obj = json_object_new_object();
|
||
|
json_object_object_add(sb_obj, "command", json_object_new_string(sb->command));
|
||
|
|
||
|
const char *names[10];
|
||
|
|
||
|
int len = get_modifier_names(names, sb->modifiers);
|
||
|
int i;
|
||
|
json_object *modifiers = json_object_new_array();
|
||
|
for (i = 0; i < len; ++i) {
|
||
|
json_object_array_add(modifiers, json_object_new_string(names[i]));
|
||
|
}
|
||
|
|
||
|
json_object_object_add(sb_obj, "event_state_mask", modifiers);
|
||
|
|
||
9 years ago
|
json_object *input_codes = json_object_new_array();
|
||
9 years ago
|
int input_code = 0;
|
||
9 years ago
|
json_object *symbols = json_object_new_array();
|
||
9 years ago
|
json_object *symbol = NULL;
|
||
9 years ago
|
|
||
|
if (sb->bindcode) { // bindcode: populate input_codes
|
||
|
uint32_t keycode;
|
||
|
for (i = 0; i < sb->keys->length; ++i) {
|
||
|
keycode = *(uint32_t *)sb->keys->items[i];
|
||
|
json_object_array_add(input_codes, json_object_new_int(keycode));
|
||
9 years ago
|
if (i == 0) {
|
||
|
input_code = keycode;
|
||
|
}
|
||
9 years ago
|
}
|
||
|
} else { // bindsym: populate symbols
|
||
|
uint32_t keysym;
|
||
|
char buffer[64];
|
||
|
for (i = 0; i < sb->keys->length; ++i) {
|
||
|
keysym = *(uint32_t *)sb->keys->items[i];
|
||
|
if (xkb_keysym_get_name(keysym, buffer, 64) > 0) {
|
||
9 years ago
|
json_object *str = json_object_new_string(buffer);
|
||
|
json_object_array_add(symbols, str);
|
||
|
if (i == 0) {
|
||
|
symbol = str;
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
json_object_object_add(sb_obj, "input_codes", input_codes);
|
||
9 years ago
|
json_object_object_add(sb_obj, "input_code", json_object_new_int(input_code));
|
||
9 years ago
|
json_object_object_add(sb_obj, "symbols", symbols);
|
||
9 years ago
|
json_object_object_add(sb_obj, "symbol", symbol);
|
||
9 years ago
|
json_object_object_add(sb_obj, "input_type", json_object_new_string("keyboard"));
|
||
|
|
||
|
ipc_event_binding(sb_obj);
|
||
|
}
|