2015-08-19 01:35:01 +02:00
|
|
|
// See https://i3wm.org/docs/ipc.html for protocol information
|
|
|
|
|
2015-08-16 20:24:18 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2015-08-16 22:02:16 +02:00
|
|
|
#include <stdbool.h>
|
2015-08-16 20:24:18 +02:00
|
|
|
#include <wlc/wlc.h>
|
|
|
|
#include <unistd.h>
|
2015-08-16 22:02:16 +02:00
|
|
|
#include <stdlib.h>
|
2015-08-19 01:35:01 +02:00
|
|
|
#include <sys/ioctl.h>
|
2015-08-20 14:49:54 +02:00
|
|
|
#include <fcntl.h>
|
2015-08-20 21:08:13 +02:00
|
|
|
#include <ctype.h>
|
2015-08-26 23:49:38 +02:00
|
|
|
#include <json-c/json.h>
|
2015-08-16 22:02:16 +02:00
|
|
|
#include "ipc.h"
|
2015-08-16 20:24:18 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "commands.h"
|
2015-08-20 21:08:13 +02:00
|
|
|
#include "list.h"
|
|
|
|
#include "stringop.h"
|
2015-08-16 20:24:18 +02:00
|
|
|
|
|
|
|
static int ipc_socket = -1;
|
2015-08-19 01:52:46 +02:00
|
|
|
static struct wlc_event_source *ipc_event_source = NULL;
|
2015-10-16 10:55:54 +02:00
|
|
|
static struct sockaddr_un *ipc_sockaddr = NULL;
|
2015-08-16 20:24:18 +02:00
|
|
|
|
2015-08-16 22:02:16 +02:00
|
|
|
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
struct ipc_client {
|
|
|
|
struct wlc_event_source *event_source;
|
|
|
|
int fd;
|
|
|
|
uint32_t payload_length;
|
|
|
|
enum ipc_command_type current_command;
|
|
|
|
};
|
2015-08-16 22:02:16 +02:00
|
|
|
|
2015-10-16 10:55:54 +02:00
|
|
|
struct sockaddr_un *ipc_user_sockaddr(void);
|
2015-08-19 01:35:01 +02:00
|
|
|
int ipc_handle_connection(int fd, uint32_t mask, void *data);
|
|
|
|
int ipc_client_handle_readable(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);
|
2015-08-27 00:41:07 +02:00
|
|
|
void ipc_get_workspaces_callback(swayc_t *workspace, void *data);
|
2015-08-20 21:08:13 +02:00
|
|
|
void ipc_get_outputs_callback(swayc_t *container, void *data);
|
|
|
|
|
2015-08-19 01:52:46 +02:00
|
|
|
void ipc_init(void) {
|
2015-08-16 20:24:18 +02:00
|
|
|
ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
|
|
|
if (ipc_socket == -1) {
|
|
|
|
sway_abort("Unable to create IPC socket");
|
|
|
|
}
|
|
|
|
|
2015-10-16 10:55:54 +02:00
|
|
|
ipc_sockaddr = ipc_user_sockaddr();
|
|
|
|
|
2015-08-19 01:40:49 +02:00
|
|
|
if (getenv("SWAYSOCK") != NULL) {
|
2015-10-16 10:55:54 +02:00
|
|
|
strncpy(ipc_sockaddr->sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr->sun_path));
|
2015-08-19 01:40:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 10:55:54 +02:00
|
|
|
unlink(ipc_sockaddr->sun_path);
|
|
|
|
if (bind(ipc_socket, (struct sockaddr *)ipc_sockaddr, sizeof(*ipc_sockaddr)) == -1) {
|
2015-08-16 20:24:18 +02:00
|
|
|
sway_abort("Unable to bind IPC socket");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(ipc_socket, 3) == -1) {
|
|
|
|
sway_abort("Unable to listen on IPC socket");
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:22:38 +02:00
|
|
|
// Set i3 IPC socket path so that i3-msg works out of the box
|
2015-10-16 10:55:54 +02:00
|
|
|
setenv("I3SOCK", ipc_sockaddr->sun_path, 1);
|
2015-08-20 15:22:38 +02:00
|
|
|
|
2015-08-19 01:52:46 +02:00
|
|
|
ipc_event_source = wlc_event_loop_add_fd(ipc_socket, WLC_EVENT_READABLE, ipc_handle_connection, NULL);
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:12:34 +02:00
|
|
|
void ipc_terminate(void) {
|
2015-08-19 01:52:46 +02:00
|
|
|
if (ipc_event_source) {
|
|
|
|
wlc_event_source_remove(ipc_event_source);
|
|
|
|
}
|
|
|
|
close(ipc_socket);
|
2015-10-16 10:55:54 +02:00
|
|
|
unlink(ipc_sockaddr->sun_path);
|
2015-10-16 12:02:41 +02:00
|
|
|
|
|
|
|
if (ipc_sockaddr) {
|
|
|
|
free(ipc_sockaddr);
|
|
|
|
}
|
2015-10-16 10:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_un *ipc_user_sockaddr(void) {
|
2015-10-16 13:06:30 +02:00
|
|
|
struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un));
|
2015-10-16 13:21:34 +02:00
|
|
|
if (ipc_sockaddr == NULL) {
|
|
|
|
sway_abort("can't malloc ipc_sockaddr");
|
2015-10-16 13:04:46 +02:00
|
|
|
}
|
2015-10-16 10:55:54 +02:00
|
|
|
|
2015-10-16 13:06:30 +02:00
|
|
|
ipc_sockaddr->sun_family = AF_UNIX;
|
2015-10-16 11:42:20 +02:00
|
|
|
|
2015-10-16 13:06:30 +02:00
|
|
|
int path_size = sizeof(ipc_sockaddr->sun_path);
|
2015-10-16 11:42:20 +02:00
|
|
|
|
2015-10-16 13:06:30 +02:00
|
|
|
// Without logind:
|
2015-10-16 13:04:46 +02:00
|
|
|
int allocating_path_size = snprintf(ipc_sockaddr->sun_path, path_size, "/tmp/sway-ipc.%i.sock", getuid());
|
|
|
|
|
2015-10-16 13:21:34 +02:00
|
|
|
if (allocating_path_size >= path_size) {
|
|
|
|
sway_abort("socket path won't fit into ipc_sockaddr->sun_path");
|
2015-10-16 13:04:46 +02:00
|
|
|
}
|
2015-10-16 10:55:54 +02:00
|
|
|
|
2015-10-16 13:06:30 +02:00
|
|
|
return ipc_sockaddr;
|
2015-08-16 20:24:18 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 22:02:16 +02:00
|
|
|
int ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
2015-08-19 01:52:46 +02:00
|
|
|
(void) fd; (void) data;
|
2015-08-16 22:02:16 +02:00
|
|
|
sway_log(L_DEBUG, "Event on IPC listening socket");
|
2015-08-19 01:35:01 +02:00
|
|
|
assert(mask == WLC_EVENT_READABLE);
|
2015-08-16 20:24:18 +02:00
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
int client_fd = accept(ipc_socket, NULL, NULL);
|
|
|
|
if (client_fd == -1) {
|
|
|
|
sway_log_errno(L_INFO, "Unable to accept IPC client connection");
|
2015-08-16 20:24:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-20 14:49:54 +02:00
|
|
|
int flags;
|
|
|
|
if ((flags=fcntl(client_fd, F_GETFD)) == -1 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) {
|
|
|
|
sway_log_errno(L_INFO, "Unable to set CLOEXEC on IPC client socket");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
struct ipc_client* client = malloc(sizeof(struct ipc_client));
|
|
|
|
client->payload_length = 0;
|
|
|
|
client->fd = client_fd;
|
|
|
|
client->event_source = wlc_event_loop_add_fd(client_fd, WLC_EVENT_READABLE, ipc_client_handle_readable, client);
|
2015-08-16 20:24:18 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-08-16 22:02:16 +02:00
|
|
|
|
|
|
|
static const int ipc_header_size = sizeof(ipc_magic)+8;
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
|
|
|
|
struct ipc_client *client = data;
|
|
|
|
sway_log(L_DEBUG, "Event on IPC client socket %d", client_fd);
|
2015-08-16 22:02:16 +02:00
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
if (mask & WLC_EVENT_ERROR) {
|
|
|
|
sway_log(L_INFO, "IPC Client socket error, removing client");
|
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask & WLC_EVENT_HANGUP) {
|
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
2015-08-16 22:02:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
int read_available;
|
2015-08-21 16:53:11 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-08-19 01:35:01 +02:00
|
|
|
|
|
|
|
// Wait for the rest of the command payload in case the header has already been read
|
|
|
|
if (client->payload_length > 0) {
|
2015-08-21 16:53:11 +02:00
|
|
|
if ((uint32_t)read_available >= client->payload_length) {
|
2015-08-19 01:35:01 +02:00
|
|
|
ipc_client_handle_command(client);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sway_log(L_DEBUG, "Too little data to read payload on IPC Client socket, waiting for more (%d < %d)", read_available, client->payload_length);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_available < ipc_header_size) {
|
|
|
|
sway_log(L_DEBUG, "Too little data to read header on IPC Client socket, waiting for more (%d < %d)", read_available, ipc_header_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[ipc_header_size];
|
|
|
|
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) {
|
2015-08-16 22:02:16 +02:00
|
|
|
sway_log(L_DEBUG, "IPC header check failed");
|
2015-08-19 01:35:01 +02:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
2015-08-16 22:02:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
client->payload_length = *(uint32_t *)&buf[sizeof(ipc_magic)];
|
|
|
|
client->current_command = (enum ipc_command_type) *(uint32_t *)&buf[sizeof(ipc_magic)+4];
|
2015-08-16 22:02:16 +02:00
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
if (read_available - received >= client->payload_length) {
|
|
|
|
ipc_client_handle_command(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipc_client_disconnect(struct ipc_client *client)
|
|
|
|
{
|
|
|
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sway_log(L_INFO, "IPC Client %d disconnected", client->fd);
|
|
|
|
wlc_event_source_remove(client->event_source);
|
|
|
|
close(client->fd);
|
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipc_client_handle_command(struct ipc_client *client) {
|
|
|
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[client->payload_length + 1];
|
|
|
|
if (client->payload_length > 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-16 22:02:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
switch (client->current_command) {
|
2015-08-20 15:15:04 +02:00
|
|
|
case IPC_COMMAND:
|
|
|
|
{
|
|
|
|
buf[client->payload_length] = '\0';
|
2015-10-22 13:00:37 +02:00
|
|
|
bool success = (handle_command(buf) == CMD_SUCCESS);
|
2015-08-20 15:15:04 +02:00
|
|
|
char reply[64];
|
|
|
|
int length = snprintf(reply, sizeof(reply), "{\"success\":%s}", success ? "true" : "false");
|
|
|
|
ipc_send_reply(client, reply, (uint32_t) length);
|
|
|
|
break;
|
|
|
|
}
|
2015-08-20 21:08:13 +02:00
|
|
|
case IPC_GET_WORKSPACES:
|
|
|
|
{
|
2015-08-27 00:41:07 +02:00
|
|
|
json_object *workspaces = json_object_new_array();
|
2015-09-14 01:46:16 +02:00
|
|
|
container_map(&root_container, ipc_get_workspaces_callback, workspaces);
|
2015-08-27 00:41:07 +02:00
|
|
|
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
|
2015-08-20 21:08:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IPC_GET_OUTPUTS:
|
|
|
|
{
|
2015-08-27 00:41:07 +02:00
|
|
|
json_object *outputs = json_object_new_array();
|
2015-09-14 01:46:16 +02:00
|
|
|
container_map(&root_container, ipc_get_outputs_callback, outputs);
|
2015-08-27 00:41:07 +02:00
|
|
|
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
|
2015-08-20 21:08:13 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-08-26 23:49:38 +02:00
|
|
|
case IPC_GET_VERSION:
|
|
|
|
{
|
2015-08-27 04:13:53 +02:00
|
|
|
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
|
|
|
|
char *full_version = calloc(strlen(SWAY_GIT_VERSION) + strlen(SWAY_GIT_BRANCH) + strlen(SWAY_VERSION_DATE) + 20, 1);
|
|
|
|
strcat(full_version, SWAY_GIT_VERSION);
|
|
|
|
strcat(full_version, " (");
|
|
|
|
strcat(full_version, SWAY_VERSION_DATE);
|
|
|
|
strcat(full_version, ", branch \"");
|
|
|
|
strcat(full_version, SWAY_GIT_BRANCH);
|
|
|
|
strcat(full_version, "\")");
|
2015-08-26 23:49:38 +02:00
|
|
|
json_object *json = json_object_new_object();
|
2015-08-27 04:13:53 +02:00
|
|
|
json_object_object_add(json, "human_readable", json_object_new_string(full_version));
|
|
|
|
// Todo once we actually release a version
|
2015-08-26 23:49:38 +02:00
|
|
|
json_object_object_add(json, "major", json_object_new_int(0));
|
|
|
|
json_object_object_add(json, "minor", json_object_new_int(0));
|
|
|
|
json_object_object_add(json, "patch", json_object_new_int(1));
|
2015-08-27 04:13:53 +02:00
|
|
|
#else
|
|
|
|
json_object_object_add(json, "human_readable", json_object_new_string("version not found"));
|
|
|
|
json_object_object_add(json, "major", json_object_new_int(0));
|
|
|
|
json_object_object_add(json, "minor", json_object_new_int(0));
|
|
|
|
json_object_object_add(json, "patch", json_object_new_int(0));
|
|
|
|
#endif
|
2015-08-26 23:49:38 +02:00
|
|
|
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
|
2015-08-27 04:13:53 +02:00
|
|
|
free(full_version);
|
2015-08-26 23:49:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-08-20 15:15:04 +02:00
|
|
|
default:
|
|
|
|
sway_log(L_INFO, "Unknown IPC command type %i", client->current_command);
|
|
|
|
ipc_client_disconnect(client);
|
2015-09-29 16:19:10 +02:00
|
|
|
return;
|
2015-08-16 22:02:16 +02:00
|
|
|
}
|
2015-08-19 01:35:01 +02:00
|
|
|
|
|
|
|
client->payload_length = 0;
|
2015-08-16 22:02:16 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) {
|
2015-08-16 22:02:16 +02:00
|
|
|
assert(payload);
|
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
char data[ipc_header_size];
|
2015-08-16 22:02:16 +02:00
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
|
|
|
*(uint32_t *)&(data[sizeof(ipc_magic)]) = payload_length;
|
|
|
|
*(uint32_t *)&(data[sizeof(ipc_magic)+4]) = client->current_command;
|
|
|
|
|
|
|
|
if (write(client->fd, data, ipc_header_size) == -1) {
|
|
|
|
sway_log_errno(L_INFO, "Unable to send header to IPC client");
|
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(client->fd, payload, payload_length) == -1) {
|
|
|
|
sway_log_errno(L_INFO, "Unable to send payload to IPC client");
|
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-16 22:02:16 +02:00
|
|
|
|
2015-08-19 01:35:01 +02:00
|
|
|
return true;
|
2015-08-16 22:02:16 +02:00
|
|
|
}
|
2015-08-20 21:08:13 +02:00
|
|
|
|
2015-08-27 00:41:07 +02:00
|
|
|
void ipc_get_workspaces_callback(swayc_t *workspace, void *data) {
|
|
|
|
if (workspace->type == C_WORKSPACE) {
|
|
|
|
int num = isdigit(workspace->name[0]) ? atoi(workspace->name) : -1;
|
|
|
|
json_object *object = json_object_new_object();
|
|
|
|
json_object *rect = json_object_new_object();
|
|
|
|
json_object_object_add(rect, "x", json_object_new_int((int32_t) workspace->x));
|
|
|
|
json_object_object_add(rect, "y", json_object_new_int((int32_t) workspace->y));
|
|
|
|
json_object_object_add(rect, "width", json_object_new_int((int32_t) workspace->width));
|
|
|
|
json_object_object_add(rect, "height", json_object_new_int((int32_t) workspace->height));
|
|
|
|
|
|
|
|
json_object_object_add(object, "num", json_object_new_int(num));
|
|
|
|
json_object_object_add(object, "name", json_object_new_string(workspace->name));
|
|
|
|
json_object_object_add(object, "visible", json_object_new_boolean(workspace->visible));
|
|
|
|
bool focused = root_container.focused == workspace->parent && workspace->parent->focused == workspace;
|
|
|
|
json_object_object_add(object, "focused", json_object_new_boolean(focused));
|
|
|
|
json_object_object_add(object, "rect", rect);
|
|
|
|
json_object_object_add(object, "output", json_object_new_string(workspace->parent->name));
|
|
|
|
json_object_object_add(object, "urgent", json_object_new_boolean(false));
|
|
|
|
|
|
|
|
json_object_array_add((json_object *)data, object);
|
2015-08-20 21:08:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipc_get_outputs_callback(swayc_t *container, void *data) {
|
|
|
|
if (container->type == C_OUTPUT) {
|
2015-08-27 00:41:07 +02:00
|
|
|
json_object *object = json_object_new_object();
|
|
|
|
json_object *rect = json_object_new_object();
|
|
|
|
json_object_object_add(rect, "x", json_object_new_int((int32_t) container->x));
|
|
|
|
json_object_object_add(rect, "y", json_object_new_int((int32_t) container->y));
|
|
|
|
json_object_object_add(rect, "width", json_object_new_int((int32_t) container->width));
|
|
|
|
json_object_object_add(rect, "height", json_object_new_int((int32_t) container->height));
|
|
|
|
|
|
|
|
json_object_object_add(object, "name", json_object_new_string(container->name));
|
|
|
|
json_object_object_add(object, "active", json_object_new_boolean(true));
|
|
|
|
json_object_object_add(object, "primary", json_object_new_boolean(false));
|
|
|
|
json_object_object_add(object, "rect", rect);
|
|
|
|
json_object_object_add(object, "current_workspace", container->focused ? json_object_new_string(container->focused->name) : NULL);
|
|
|
|
|
|
|
|
json_object_array_add((json_object *)data, object);
|
2015-08-20 21:08:13 +02:00
|
|
|
}
|
|
|
|
}
|