2015-08-19 09:35:01 +10:00
|
|
|
// See https://i3wm.org/docs/ipc.html for protocol information
|
|
|
|
|
2015-08-17 04:24:18 +10:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2015-08-17 06:02:16 +10:00
|
|
|
#include <stdbool.h>
|
2016-02-08 22:06:33 +11:00
|
|
|
#include <wlc/wlc-render.h>
|
2015-08-17 04:24:18 +10:00
|
|
|
#include <unistd.h>
|
2015-08-17 06:02:16 +10:00
|
|
|
#include <stdlib.h>
|
2015-08-19 09:35:01 +10:00
|
|
|
#include <sys/ioctl.h>
|
2015-08-20 22:49:54 +10:00
|
|
|
#include <fcntl.h>
|
2015-08-27 07:49:38 +10:00
|
|
|
#include <json-c/json.h>
|
2015-10-28 07:59:17 +11:00
|
|
|
#include <list.h>
|
2016-01-17 21:53:37 +11:00
|
|
|
#include <libinput.h>
|
2016-09-01 22:18:37 +10:00
|
|
|
#include "sway/ipc-json.h"
|
|
|
|
#include "sway/ipc-server.h"
|
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/input.h"
|
|
|
|
#include "stringop.h"
|
2015-08-17 04:24:18 +10:00
|
|
|
#include "log.h"
|
2015-08-21 05:08:13 +10:00
|
|
|
#include "list.h"
|
2016-01-06 04:07:43 +11:00
|
|
|
#include "util.h"
|
2015-08-17 04:24:18 +10:00
|
|
|
|
|
|
|
static int ipc_socket = -1;
|
2015-08-19 09:52:46 +10:00
|
|
|
static struct wlc_event_source *ipc_event_source = NULL;
|
2015-10-16 19:55:54 +11:00
|
|
|
static struct sockaddr_un *ipc_sockaddr = NULL;
|
2015-10-28 07:59:17 +11:00
|
|
|
static list_t *ipc_client_list = NULL;
|
2015-08-17 04:24:18 +10:00
|
|
|
|
2015-08-17 06:02:16 +10:00
|
|
|
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
struct ipc_client {
|
|
|
|
struct wlc_event_source *event_source;
|
|
|
|
int fd;
|
|
|
|
uint32_t payload_length;
|
|
|
|
enum ipc_command_type current_command;
|
2015-10-28 07:59:17 +11:00
|
|
|
enum ipc_command_type subscribed_events;
|
2015-08-19 09:35:01 +10:00
|
|
|
};
|
2015-08-17 06:02:16 +10:00
|
|
|
|
2016-02-08 22:06:33 +11:00
|
|
|
static list_t *ipc_get_pixel_requests = NULL;
|
|
|
|
|
|
|
|
struct get_pixels_request {
|
|
|
|
struct ipc_client *client;
|
|
|
|
wlc_handle output;
|
2016-09-19 07:41:08 +10:00
|
|
|
struct wlc_geometry geo;
|
2016-02-08 22:06:33 +11:00
|
|
|
};
|
|
|
|
|
2015-10-16 19:55:54 +11:00
|
|
|
struct sockaddr_un *ipc_user_sockaddr(void);
|
2015-08-19 09:35:01 +10: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 08:41:07 +10:00
|
|
|
void ipc_get_workspaces_callback(swayc_t *workspace, void *data);
|
2015-08-21 05:08:13 +10:00
|
|
|
void ipc_get_outputs_callback(swayc_t *container, void *data);
|
|
|
|
|
2016-07-20 21:56:14 +10:00
|
|
|
#define event_mask(ev) (1 << (ev & 0x7F))
|
|
|
|
|
2015-08-19 09:52:46 +10:00
|
|
|
void ipc_init(void) {
|
2015-08-17 04:24:18 +10: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 19:55:54 +11:00
|
|
|
ipc_sockaddr = ipc_user_sockaddr();
|
|
|
|
|
2015-11-14 03:53:46 +11:00
|
|
|
// 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) {
|
2015-10-16 19:55:54 +11:00
|
|
|
strncpy(ipc_sockaddr->sun_path, getenv("SWAYSOCK"), sizeof(ipc_sockaddr->sun_path));
|
2016-05-02 20:24:00 +10:00
|
|
|
ipc_sockaddr->sun_path[sizeof(ipc_sockaddr->sun_path) - 1] = 0;
|
2015-08-19 09:40:49 +10:00
|
|
|
}
|
|
|
|
|
2015-10-16 19:55:54 +11:00
|
|
|
unlink(ipc_sockaddr->sun_path);
|
|
|
|
if (bind(ipc_socket, (struct sockaddr *)ipc_sockaddr, sizeof(*ipc_sockaddr)) == -1) {
|
2015-08-17 04:24:18 +10: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 23:22:38 +10:00
|
|
|
// Set i3 IPC socket path so that i3-msg works out of the box
|
2015-11-27 08:49:36 +11:00
|
|
|
setenv("I3SOCK", ipc_sockaddr->sun_path, 1);
|
|
|
|
setenv("SWAYSOCK", ipc_sockaddr->sun_path, 1);
|
2015-08-20 23:22:38 +10:00
|
|
|
|
2015-10-28 07:59:17 +11:00
|
|
|
ipc_client_list = create_list();
|
2016-02-08 22:06:33 +11:00
|
|
|
ipc_get_pixel_requests = create_list();
|
2015-10-28 07:59:17 +11:00
|
|
|
|
2015-08-19 09:52:46 +10:00
|
|
|
ipc_event_source = wlc_event_loop_add_fd(ipc_socket, WLC_EVENT_READABLE, ipc_handle_connection, NULL);
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:12:34 +10:00
|
|
|
void ipc_terminate(void) {
|
2015-08-19 09:52:46 +10:00
|
|
|
if (ipc_event_source) {
|
|
|
|
wlc_event_source_remove(ipc_event_source);
|
|
|
|
}
|
|
|
|
close(ipc_socket);
|
2015-10-16 19:55:54 +11:00
|
|
|
unlink(ipc_sockaddr->sun_path);
|
2015-10-16 21:02:41 +11:00
|
|
|
|
2015-10-28 07:59:17 +11:00
|
|
|
list_free(ipc_client_list);
|
|
|
|
|
2015-10-16 21:02:41 +11:00
|
|
|
if (ipc_sockaddr) {
|
|
|
|
free(ipc_sockaddr);
|
|
|
|
}
|
2015-10-16 19:55:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_un *ipc_user_sockaddr(void) {
|
2015-10-16 22:06:30 +11:00
|
|
|
struct sockaddr_un *ipc_sockaddr = malloc(sizeof(struct sockaddr_un));
|
2015-10-16 22:21:34 +11:00
|
|
|
if (ipc_sockaddr == NULL) {
|
|
|
|
sway_abort("can't malloc ipc_sockaddr");
|
2015-10-16 22:04:46 +11:00
|
|
|
}
|
2015-10-16 19:55:54 +11:00
|
|
|
|
2015-10-16 22:06:30 +11:00
|
|
|
ipc_sockaddr->sun_family = AF_UNIX;
|
|
|
|
int path_size = sizeof(ipc_sockaddr->sun_path);
|
2015-10-16 20:42:20 +11:00
|
|
|
|
2015-11-13 03:19:58 +11:00
|
|
|
// 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())) {
|
2015-10-16 22:21:34 +11:00
|
|
|
sway_abort("socket path won't fit into ipc_sockaddr->sun_path");
|
2015-10-16 22:04:46 +11:00
|
|
|
}
|
2015-10-16 19:55:54 +11:00
|
|
|
|
2015-10-16 22:06:30 +11:00
|
|
|
return ipc_sockaddr;
|
2015-08-17 04:24:18 +10:00
|
|
|
}
|
|
|
|
|
2015-08-17 06:02:16 +10:00
|
|
|
int ipc_handle_connection(int fd, uint32_t mask, void *data) {
|
2015-08-19 09:52:46 +10:00
|
|
|
(void) fd; (void) data;
|
2015-08-17 06:02:16 +10:00
|
|
|
sway_log(L_DEBUG, "Event on IPC listening socket");
|
2015-08-19 09:35:01 +10:00
|
|
|
assert(mask == WLC_EVENT_READABLE);
|
2015-08-17 04:24:18 +10:00
|
|
|
|
2015-08-19 09:35:01 +10: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-17 04:24:18 +10:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:49:54 +10: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");
|
2016-05-03 00:49:33 +10:00
|
|
|
close(client_fd);
|
2015-08-20 22:49:54 +10:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
struct ipc_client* client = malloc(sizeof(struct ipc_client));
|
|
|
|
client->payload_length = 0;
|
|
|
|
client->fd = client_fd;
|
2016-07-28 23:38:02 +10:00
|
|
|
client->subscribed_events = 0;
|
2015-08-19 09:35:01 +10:00
|
|
|
client->event_source = wlc_event_loop_add_fd(client_fd, WLC_EVENT_READABLE, ipc_client_handle_readable, client);
|
2015-08-17 04:24:18 +10:00
|
|
|
|
2015-10-28 07:59:17 +11:00
|
|
|
list_add(ipc_client_list, client);
|
|
|
|
|
2015-08-17 04:24:18 +10:00
|
|
|
return 0;
|
|
|
|
}
|
2015-08-17 06:02:16 +10:00
|
|
|
|
|
|
|
static const int ipc_header_size = sizeof(ipc_magic)+8;
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
|
|
|
|
struct ipc_client *client = data;
|
2015-08-17 06:02:16 +10:00
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
if (mask & WLC_EVENT_ERROR) {
|
|
|
|
sway_log(L_INFO, "IPC Client socket error, removing client");
|
2015-10-28 07:59:17 +11:00
|
|
|
client->fd = -1;
|
2015-08-19 09:35:01 +10:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask & WLC_EVENT_HANGUP) {
|
2015-10-28 07:59:17 +11:00
|
|
|
client->fd = -1;
|
2015-08-19 09:35:01 +10:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
2015-08-17 06:02:16 +10:00
|
|
|
}
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
int read_available;
|
2015-08-22 00:53:11 +10: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 09:35:01 +10:00
|
|
|
|
|
|
|
// Wait for the rest of the command payload in case the header has already been read
|
|
|
|
if (client->payload_length > 0) {
|
2015-08-22 00:53:11 +10:00
|
|
|
if ((uint32_t)read_available >= client->payload_length) {
|
2015-08-19 09:35:01 +10:00
|
|
|
ipc_client_handle_command(client);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_available < ipc_header_size) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-12 00:43:36 +11:00
|
|
|
uint8_t buf[ipc_header_size];
|
|
|
|
uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic));
|
2015-08-19 09:35:01 +10:00
|
|
|
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-17 06:02:16 +10:00
|
|
|
sway_log(L_DEBUG, "IPC header check failed");
|
2015-08-19 09:35:01 +10:00
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return 0;
|
2015-08-17 06:02:16 +10:00
|
|
|
}
|
|
|
|
|
2015-11-12 00:43:36 +11:00
|
|
|
client->payload_length = buf32[0];
|
|
|
|
client->current_command = (enum ipc_command_type)buf32[1];
|
2015-08-17 06:02:16 +10:00
|
|
|
|
2016-04-18 00:26:26 +10:00
|
|
|
if (read_available - received >= (long)client->payload_length) {
|
2015-08-19 09:35:01 +10:00
|
|
|
ipc_client_handle_command(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipc_client_disconnect(struct ipc_client *client)
|
|
|
|
{
|
|
|
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-28 07:59:17 +11:00
|
|
|
if (client->fd != -1) {
|
|
|
|
shutdown(client->fd, SHUT_RDWR);
|
|
|
|
}
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
sway_log(L_INFO, "IPC Client %d disconnected", client->fd);
|
|
|
|
wlc_event_source_remove(client->event_source);
|
2015-10-28 07:59:17 +11:00
|
|
|
int i = 0;
|
|
|
|
while (i < ipc_client_list->length && ipc_client_list->items[i] != client) i++;
|
|
|
|
list_del(ipc_client_list, i);
|
2015-08-19 09:35:01 +10:00
|
|
|
close(client->fd);
|
|
|
|
free(client);
|
|
|
|
}
|
|
|
|
|
2015-11-13 04:37:23 +11:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-02-08 22:06:33 +11:00
|
|
|
void ipc_get_pixels(wlc_handle output) {
|
|
|
|
if (ipc_get_pixel_requests->length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_t *unhandled = create_list();
|
|
|
|
|
|
|
|
struct get_pixels_request *req;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ipc_get_pixel_requests->length; ++i) {
|
|
|
|
req = ipc_get_pixel_requests->items[i];
|
|
|
|
if (req->output != output) {
|
|
|
|
list_add(unhandled, req);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:41:08 +10:00
|
|
|
const struct wlc_size *size = &req->geo.size;
|
2016-02-08 22:06:33 +11:00
|
|
|
struct wlc_geometry g_out;
|
|
|
|
char response_header[9];
|
|
|
|
memset(response_header, 0, sizeof(response_header));
|
|
|
|
char *data = malloc(sizeof(response_header) + size->w * size->h * 4);
|
2016-09-19 07:41:08 +10:00
|
|
|
wlc_pixels_read(WLC_RGBA8888, &req->geo, &g_out, data + sizeof(response_header));
|
2016-02-08 22:06:33 +11:00
|
|
|
|
|
|
|
response_header[0] = 1;
|
|
|
|
uint32_t *_size = (uint32_t *)(response_header + 1);
|
|
|
|
_size[0] = g_out.size.w;
|
|
|
|
_size[1] = g_out.size.h;
|
|
|
|
size_t len = sizeof(response_header) + (g_out.size.w * g_out.size.h * 4);
|
|
|
|
memcpy(data, response_header, sizeof(response_header));
|
|
|
|
ipc_send_reply(req->client, data, len);
|
|
|
|
free(data);
|
|
|
|
// free the request since it has been handled
|
|
|
|
free(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
// free old list of pixel requests and set new list to all unhandled
|
|
|
|
// requests (request for another output).
|
|
|
|
list_free(ipc_get_pixel_requests);
|
|
|
|
ipc_get_pixel_requests = unhandled;
|
2015-11-13 04:37:23 +11:00
|
|
|
}
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
void ipc_client_handle_command(struct ipc_client *client) {
|
|
|
|
if (!sway_assert(client != NULL, "client != NULL")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-13 04:37:23 +11:00
|
|
|
char *buf = malloc(client->payload_length + 1);
|
2016-05-03 06:32:20 +10:00
|
|
|
if (!buf) {
|
|
|
|
sway_log_errno(L_INFO, "Out of memory");
|
|
|
|
ipc_client_disconnect(client);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-19 09:35:01 +10:00
|
|
|
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);
|
2015-11-13 04:37:23 +11:00
|
|
|
free(buf);
|
2015-08-19 09:35:01 +10:00
|
|
|
return;
|
|
|
|
}
|
2015-08-17 06:02:16 +10:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
buf[client->payload_length] = '\0';
|
2015-08-17 06:02:16 +10:00
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
switch (client->current_command) {
|
2015-08-20 23:15:04 +10:00
|
|
|
case IPC_COMMAND:
|
|
|
|
{
|
2016-12-03 00:17:45 +11:00
|
|
|
struct cmd_results *results = handle_command(buf, CONTEXT_IPC);
|
2015-10-22 23:14:13 +11:00
|
|
|
const char *json = cmd_results_to_json(results);
|
|
|
|
char reply[256];
|
|
|
|
int length = snprintf(reply, sizeof(reply), "%s", json);
|
2015-08-20 23:15:04 +10:00
|
|
|
ipc_send_reply(client, reply, (uint32_t) length);
|
2015-10-22 23:14:13 +11:00
|
|
|
free_cmd_results(results);
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-08-20 23:15:04 +10:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2015-10-28 07:59:17 +11:00
|
|
|
case IPC_SUBSCRIBE:
|
|
|
|
{
|
|
|
|
struct json_object *request = json_tokener_parse(buf);
|
|
|
|
if (request == NULL) {
|
|
|
|
ipc_send_reply(client, "{\"success\": false}", 18);
|
2016-05-03 06:32:20 +10:00
|
|
|
sway_log_errno(L_INFO, "Failed to read request");
|
|
|
|
goto exit_cleanup;
|
2015-10-28 07:59:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2016-07-20 21:56:14 +10:00
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_WORKSPACE);
|
2015-12-22 01:28:34 +11:00
|
|
|
} else if (strcmp(event_type, "barconfig_update") == 0) {
|
2016-07-20 21:56:14 +10:00
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_BARCONFIG_UPDATE);
|
2016-01-04 08:00:59 +11:00
|
|
|
} else if (strcmp(event_type, "mode") == 0) {
|
2016-07-20 21:56:14 +10:00
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_MODE);
|
2016-07-12 23:58:24 +10:00
|
|
|
} else if (strcmp(event_type, "window") == 0) {
|
2016-07-20 21:56:14 +10:00
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_WINDOW);
|
2016-01-05 12:20:20 +11:00
|
|
|
} else if (strcmp(event_type, "modifier") == 0) {
|
2016-07-20 21:56:14 +10:00
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_MODIFIER);
|
2016-01-09 00:41:09 +11:00
|
|
|
#if SWAY_BINDING_EVENT
|
2016-01-07 03:01:45 +11:00
|
|
|
} else if (strcmp(event_type, "binding") == 0) {
|
2016-07-20 21:56:14 +10:00
|
|
|
client->subscribed_events |= event_mask(IPC_EVENT_BINDING);
|
2016-01-09 00:41:09 +11:00
|
|
|
#endif
|
2015-12-22 01:28:34 +11:00
|
|
|
} else {
|
2015-10-28 07:59:17 +11:00
|
|
|
ipc_send_reply(client, "{\"success\": false}", 18);
|
|
|
|
json_object_put(request);
|
2016-05-03 06:32:20 +10:00
|
|
|
sway_log_errno(L_INFO, "Failed to parse request");
|
|
|
|
goto exit_cleanup;
|
2015-10-28 07:59:17 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_put(request);
|
|
|
|
|
|
|
|
ipc_send_reply(client, "{\"success\": true}", 17);
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-10-28 07:59:17 +11:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2015-08-21 05:08:13 +10:00
|
|
|
case IPC_GET_WORKSPACES:
|
|
|
|
{
|
2015-08-27 08:41:07 +10:00
|
|
|
json_object *workspaces = json_object_new_array();
|
2015-09-14 09:46:16 +10:00
|
|
|
container_map(&root_container, ipc_get_workspaces_callback, workspaces);
|
2015-08-27 08:41:07 +10: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
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-08-21 05:08:13 +10:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2016-01-17 21:53:37 +11:00
|
|
|
case IPC_GET_INPUTS:
|
|
|
|
{
|
|
|
|
json_object *inputs = json_object_new_array();
|
|
|
|
if (input_devices) {
|
|
|
|
for(int i=0; i<input_devices->length; i++) {
|
|
|
|
struct libinput_device *device = input_devices->items[i];
|
|
|
|
char* identifier = libinput_dev_unique_id(device);
|
|
|
|
json_object *device_object = json_object_new_object();
|
|
|
|
json_object_object_add(device_object, "identifier", json_object_new_string(identifier));
|
|
|
|
json_object_array_add(inputs, device_object);
|
|
|
|
free(identifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2016-01-17 21:53:37 +11:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2015-08-21 05:08:13 +10:00
|
|
|
case IPC_GET_OUTPUTS:
|
|
|
|
{
|
2015-08-27 08:41:07 +10:00
|
|
|
json_object *outputs = json_object_new_array();
|
2015-09-14 09:46:16 +10:00
|
|
|
container_map(&root_container, ipc_get_outputs_callback, outputs);
|
2015-08-27 08:41:07 +10: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
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-08-21 05:08:13 +10:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2016-07-05 04:34:44 +10: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);
|
|
|
|
ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
|
|
|
|
json_object_put(tree);
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:49:38 +10:00
|
|
|
case IPC_GET_VERSION:
|
|
|
|
{
|
2016-07-05 04:34:44 +10:00
|
|
|
json_object *version = ipc_json_get_version();
|
|
|
|
const char *json_string = json_object_to_json_string(version);
|
2015-12-16 00:25:53 +11:00
|
|
|
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
|
2016-07-05 04:34:44 +10:00
|
|
|
json_object_put(version); // free
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-08-27 07:49:38 +10:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2015-11-13 04:37:23 +11:00
|
|
|
case IPC_SWAY_GET_PIXELS:
|
|
|
|
{
|
|
|
|
char response_header[9];
|
|
|
|
memset(response_header, 0, sizeof(response_header));
|
2016-09-19 07:41:08 +10:00
|
|
|
|
|
|
|
json_object *obj = json_tokener_parse(buf);
|
|
|
|
json_object *o, *x, *y, *w, *h;
|
|
|
|
|
|
|
|
json_object_object_get_ex(obj, "output", &o);
|
|
|
|
json_object_object_get_ex(obj, "x", &x);
|
|
|
|
json_object_object_get_ex(obj, "y", &y);
|
|
|
|
json_object_object_get_ex(obj, "w", &w);
|
|
|
|
json_object_object_get_ex(obj, "h", &h);
|
|
|
|
|
|
|
|
struct wlc_geometry g = {
|
|
|
|
.origin = {
|
|
|
|
.x = json_object_get_int(x),
|
|
|
|
.y = json_object_get_int(y)
|
|
|
|
},
|
|
|
|
.size = {
|
|
|
|
.w = json_object_get_int(w),
|
|
|
|
.h = json_object_get_int(h)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
swayc_t *output = swayc_by_test(&root_container, output_by_name_test, (void *)json_object_get_string(o));
|
|
|
|
json_object_put(obj);
|
|
|
|
|
2015-11-13 04:37:23 +11:00
|
|
|
if (!output) {
|
|
|
|
sway_log(L_ERROR, "IPC GET_PIXELS request with unknown output name");
|
|
|
|
ipc_send_reply(client, response_header, sizeof(response_header));
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-11-13 04:37:23 +11:00
|
|
|
}
|
2016-02-08 22:06:33 +11:00
|
|
|
struct get_pixels_request *req = malloc(sizeof(struct get_pixels_request));
|
|
|
|
req->client = client;
|
|
|
|
req->output = output->handle;
|
2016-09-19 07:41:08 +10:00
|
|
|
req->geo = g;
|
2016-02-08 22:06:33 +11:00
|
|
|
list_add(ipc_get_pixel_requests, req);
|
|
|
|
wlc_output_schedule_render(output->handle);
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-11-13 04:37:23 +11:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2015-12-16 00:25:53 +11:00
|
|
|
case IPC_GET_BAR_CONFIG:
|
|
|
|
{
|
|
|
|
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));
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-12-16 00:25:53 +11:00
|
|
|
}
|
2015-12-22 01:28:34 +11:00
|
|
|
json_object *json = ipc_json_describe_bar_config(bar);
|
2015-12-16 00:25:53 +11: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
|
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-12-16 00:25:53 +11:00
|
|
|
}
|
2016-05-03 06:32:20 +10:00
|
|
|
|
2015-08-20 23:15:04 +10:00
|
|
|
default:
|
|
|
|
sway_log(L_INFO, "Unknown IPC command type %i", client->current_command);
|
2016-05-03 06:32:20 +10:00
|
|
|
goto exit_cleanup;
|
2015-08-17 06:02:16 +10:00
|
|
|
}
|
2015-08-19 09:35:01 +10:00
|
|
|
|
2016-05-03 06:32:20 +10:00
|
|
|
exit_cleanup:
|
2016-06-01 00:25:54 +10:00
|
|
|
client->payload_length = 0;
|
2015-11-13 04:37:23 +11:00
|
|
|
free(buf);
|
2016-05-03 06:32:20 +10:00
|
|
|
return;
|
2015-08-17 06:02:16 +10:00
|
|
|
}
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) {
|
2015-08-17 06:02:16 +10:00
|
|
|
assert(payload);
|
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
char data[ipc_header_size];
|
2015-11-12 00:43:36 +11:00
|
|
|
uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic));
|
2015-08-17 06:02:16 +10:00
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
2015-11-12 00:43:36 +11:00
|
|
|
data32[0] = payload_length;
|
|
|
|
data32[1] = client->current_command;
|
2015-08-19 09:35:01 +10:00
|
|
|
|
|
|
|
if (write(client->fd, data, ipc_header_size) == -1) {
|
|
|
|
sway_log_errno(L_INFO, "Unable to send header to IPC client");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(client->fd, payload, payload_length) == -1) {
|
|
|
|
sway_log_errno(L_INFO, "Unable to send payload to IPC client");
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-17 06:02:16 +10:00
|
|
|
|
2015-08-19 09:35:01 +10:00
|
|
|
return true;
|
2015-08-17 06:02:16 +10:00
|
|
|
}
|
2015-08-21 05:08:13 +10:00
|
|
|
|
2015-08-27 08:41:07 +10:00
|
|
|
void ipc_get_workspaces_callback(swayc_t *workspace, void *data) {
|
|
|
|
if (workspace->type == C_WORKSPACE) {
|
2016-09-18 13:00:11 +10: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
|
|
|
|
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);
|
2015-08-21 05:08:13 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipc_get_outputs_callback(swayc_t *container, void *data) {
|
|
|
|
if (container->type == C_OUTPUT) {
|
2016-07-05 04:34:44 +10:00
|
|
|
json_object_array_add((json_object *)data, ipc_json_describe_container(container));
|
2016-02-24 00:40:46 +11:00
|
|
|
}
|
2015-12-22 01:28:34 +11:00
|
|
|
}
|
|
|
|
|
2016-01-04 06:37:10 +11:00
|
|
|
void ipc_send_event(const char *json_string, enum ipc_command_type event) {
|
|
|
|
int i;
|
|
|
|
struct ipc_client *client;
|
|
|
|
for (i = 0; i < ipc_client_list->length; i++) {
|
|
|
|
client = ipc_client_list->items[i];
|
2016-07-20 21:56:14 +10:00
|
|
|
if ((client->subscribed_events & event_mask(event)) == 0) {
|
2016-01-04 06:37:10 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
client->current_command = event;
|
2016-05-03 06:32:20 +10:00
|
|
|
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);
|
|
|
|
}
|
2016-01-04 06:37:10 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-22 08:01:36 +11:00
|
|
|
void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change) {
|
2016-08-05 11:48:20 +10:00
|
|
|
sway_log(L_DEBUG, "Sending workspace::%s event", change);
|
2015-10-28 07:59:17 +11:00
|
|
|
json_object *obj = json_object_new_object();
|
2015-12-22 08:01:36 +11:00
|
|
|
json_object_object_add(obj, "change", json_object_new_string(change));
|
|
|
|
if (strcmp("focus", change) == 0) {
|
|
|
|
if (old) {
|
2016-08-05 11:15:06 +10:00
|
|
|
json_object_object_add(obj, "old", ipc_json_describe_container_recursive(old));
|
2015-12-22 08:01:36 +11:00
|
|
|
} else {
|
|
|
|
json_object_object_add(obj, "old", NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new) {
|
2016-08-05 11:15:06 +10:00
|
|
|
json_object_object_add(obj, "current", ipc_json_describe_container_recursive(new));
|
2015-12-14 21:43:17 +11:00
|
|
|
} else {
|
2015-12-22 08:01:36 +11:00
|
|
|
json_object_object_add(obj, "current", NULL);
|
2015-12-14 21:43:17 +11:00
|
|
|
}
|
2015-12-22 08:01:36 +11:00
|
|
|
|
2015-10-28 07:59:17 +11:00
|
|
|
const char *json_string = json_object_to_json_string(obj);
|
2016-01-04 06:37:10 +11:00
|
|
|
ipc_send_event(json_string, IPC_EVENT_WORKSPACE);
|
2015-10-28 07:59:17 +11:00
|
|
|
|
|
|
|
json_object_put(obj); // free
|
2015-08-21 05:08:13 +10:00
|
|
|
}
|
2015-12-22 01:28:34 +11:00
|
|
|
|
2016-07-12 23:58:24 +10:00
|
|
|
void ipc_event_window(swayc_t *window, const char *change) {
|
2016-08-05 11:48:20 +10:00
|
|
|
sway_log(L_DEBUG, "Sending window::%s event", change);
|
2016-07-12 23:58:24 +10:00
|
|
|
json_object *obj = json_object_new_object();
|
|
|
|
json_object_object_add(obj, "change", json_object_new_string(change));
|
|
|
|
if (strcmp(change, "close") == 0 || !window) {
|
|
|
|
json_object_object_add(obj, "container", NULL);
|
|
|
|
} else {
|
2016-07-21 09:06:34 +10:00
|
|
|
json_object_object_add(obj, "container", ipc_json_describe_container(window));
|
2016-07-12 23:58:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *json_string = json_object_to_json_string(obj);
|
|
|
|
ipc_send_event(json_string, IPC_EVENT_WINDOW);
|
|
|
|
|
|
|
|
json_object_put(obj); // free
|
|
|
|
}
|
|
|
|
|
2015-12-22 01:28:34 +11:00
|
|
|
void ipc_event_barconfig_update(struct bar_config *bar) {
|
2016-08-05 11:48:20 +10:00
|
|
|
sway_log(L_DEBUG, "Sending barconfig_update event");
|
2015-12-22 01:28:34 +11:00
|
|
|
json_object *json = ipc_json_describe_bar_config(bar);
|
|
|
|
const char *json_string = json_object_to_json_string(json);
|
2016-01-04 06:37:10 +11:00
|
|
|
ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE);
|
2015-12-22 01:28:34 +11:00
|
|
|
|
|
|
|
json_object_put(json); // free
|
|
|
|
}
|
2016-01-04 08:00:59 +11:00
|
|
|
|
|
|
|
void ipc_event_mode(const char *mode) {
|
2016-08-05 11:48:20 +10:00
|
|
|
sway_log(L_DEBUG, "Sending mode::%s event", mode);
|
2016-01-04 08:00:59 +11:00
|
|
|
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
|
|
|
|
}
|
2016-01-05 12:20:20 +11:00
|
|
|
|
|
|
|
void ipc_event_modifier(uint32_t modifier, const char *state) {
|
2016-08-05 11:48:20 +10:00
|
|
|
sway_log(L_DEBUG, "Sending modifier::%s event", state);
|
2016-01-05 12:20:20 +11:00
|
|
|
json_object *obj = json_object_new_object();
|
|
|
|
json_object_object_add(obj, "change", json_object_new_string(state));
|
|
|
|
|
2016-01-06 04:07:43 +11:00
|
|
|
const char *modifier_name = get_modifier_name_by_mask(modifier);
|
2016-01-05 12:20:20 +11:00
|
|
|
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
|
|
|
|
}
|
2016-01-07 03:01:45 +11:00
|
|
|
|
2016-01-09 00:41:09 +11:00
|
|
|
#if SWAY_BINDING_EVENT
|
2016-01-07 03:01:45 +11:00
|
|
|
static void ipc_event_binding(json_object *sb_obj) {
|
2016-08-05 11:48:20 +10:00
|
|
|
sway_log(L_DEBUG, "Sending binding::run event");
|
2016-01-07 03:01:45 +11:00
|
|
|
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
|
|
|
|
}
|
2016-01-09 00:41:09 +11:00
|
|
|
#endif
|
2016-01-07 03:01:45 +11:00
|
|
|
|
|
|
|
void ipc_event_binding_keyboard(struct sway_binding *sb) {
|
2016-01-09 00:41:09 +11:00
|
|
|
#if SWAY_BINDING_EVENT
|
2016-01-07 03:01:45 +11:00
|
|
|
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);
|
|
|
|
|
2016-01-10 06:31:18 +11:00
|
|
|
json_object *input_codes = json_object_new_array();
|
2016-01-10 06:56:42 +11:00
|
|
|
int input_code = 0;
|
2016-01-07 03:01:45 +11:00
|
|
|
json_object *symbols = json_object_new_array();
|
2016-01-10 06:56:42 +11:00
|
|
|
json_object *symbol = NULL;
|
2016-01-10 06:31:18 +11:00
|
|
|
|
|
|
|
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));
|
2016-01-10 06:56:42 +11:00
|
|
|
if (i == 0) {
|
|
|
|
input_code = keycode;
|
|
|
|
}
|
2016-01-10 06:31:18 +11:00
|
|
|
}
|
|
|
|
} 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) {
|
2016-01-10 06:56:42 +11:00
|
|
|
json_object *str = json_object_new_string(buffer);
|
|
|
|
json_object_array_add(symbols, str);
|
|
|
|
if (i == 0) {
|
|
|
|
symbol = str;
|
|
|
|
}
|
2016-01-10 06:31:18 +11:00
|
|
|
}
|
2016-01-07 03:01:45 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 06:31:18 +11:00
|
|
|
json_object_object_add(sb_obj, "input_codes", input_codes);
|
2016-01-10 06:56:42 +11:00
|
|
|
json_object_object_add(sb_obj, "input_code", json_object_new_int(input_code));
|
2016-01-07 03:01:45 +11:00
|
|
|
json_object_object_add(sb_obj, "symbols", symbols);
|
2016-01-10 06:56:42 +11:00
|
|
|
json_object_object_add(sb_obj, "symbol", symbol);
|
2016-01-07 03:01:45 +11:00
|
|
|
json_object_object_add(sb_obj, "input_type", json_object_new_string("keyboard"));
|
|
|
|
|
|
|
|
ipc_event_binding(sb_obj);
|
2016-01-09 00:41:09 +11:00
|
|
|
#endif
|
2016-01-07 03:01:45 +11:00
|
|
|
}
|