mirror of
https://github.com/swaywm/sway.git
synced 2024-11-14 06:24:20 +01:00
Move IPC client into common, refactor IPC
This commit is contained in:
parent
5ae359279b
commit
27f03c705d
78
common/ipc-client.c
Normal file
78
common/ipc-client.c
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "log.h"
|
||||||
|
#include "stringop.h"
|
||||||
|
#include "ipc-server.h"
|
||||||
|
#include "readline.h"
|
||||||
|
|
||||||
|
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
||||||
|
static const size_t ipc_header_size = sizeof(ipc_magic)+8;
|
||||||
|
|
||||||
|
char *get_socketpath(void) {
|
||||||
|
FILE *fp = popen("sway --get-socketpath", "r");
|
||||||
|
if (!fp) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
char *line = read_line(fp);
|
||||||
|
pclose(fp);
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len) {
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
int socketfd;
|
||||||
|
if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||||
|
sway_abort("Unable to open Unix socket");
|
||||||
|
}
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
strcpy(addr.sun_path, socket_path);
|
||||||
|
int l = sizeof(addr.sun_family) + strlen(addr.sun_path);
|
||||||
|
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
|
||||||
|
sway_abort("Unable to connect to %s", socket_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
char data[ipc_header_size];
|
||||||
|
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
||||||
|
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
||||||
|
data32[0] = len;
|
||||||
|
data32[1] = type;
|
||||||
|
|
||||||
|
if (write(socketfd, data, ipc_header_size) == -1) {
|
||||||
|
sway_abort("Unable to send IPC header");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write(socketfd, payload, len) == -1) {
|
||||||
|
sway_abort("Unable to send IPC payload");
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t total = 0;
|
||||||
|
while (total < ipc_header_size) {
|
||||||
|
ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0);
|
||||||
|
if (received < 0) {
|
||||||
|
sway_abort("Unable to receive IPC response");
|
||||||
|
}
|
||||||
|
total += received;
|
||||||
|
}
|
||||||
|
|
||||||
|
total = 0;
|
||||||
|
len = data32[0];
|
||||||
|
char *response = malloc(len + 1);
|
||||||
|
while (total < len) {
|
||||||
|
ssize_t received = recv(socketfd, response + total, len - total, 0);
|
||||||
|
if (received < 0) {
|
||||||
|
sway_abort("Unable to receive IPC response");
|
||||||
|
}
|
||||||
|
total += received;
|
||||||
|
}
|
||||||
|
response[len] = '\0';
|
||||||
|
|
||||||
|
close(socketfd);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
9
include/ipc-client.h
Normal file
9
include/ipc-client.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef _SWAY_IPC_CLIENT_H
|
||||||
|
#define _SWAY_IPC_CLIENT_H
|
||||||
|
|
||||||
|
#include "ipc.h"
|
||||||
|
|
||||||
|
char *get_socketpath(void);
|
||||||
|
char *ipc_single_command(const char *socket_path, uint32_t type, const char *payload, uint32_t len);
|
||||||
|
|
||||||
|
#endif
|
13
include/ipc-server.h
Normal file
13
include/ipc-server.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#ifndef _SWAY_IPC_SERVER_H
|
||||||
|
#define _SWAY_IPC_SERVER_H
|
||||||
|
|
||||||
|
#include "container.h"
|
||||||
|
#include "ipc.h"
|
||||||
|
|
||||||
|
void ipc_init(void);
|
||||||
|
void ipc_terminate(void);
|
||||||
|
struct sockaddr_un *ipc_user_sockaddr(void);
|
||||||
|
|
||||||
|
void ipc_event_workspace(swayc_t *old, swayc_t *new);
|
||||||
|
|
||||||
|
#endif
|
@ -1,8 +1,6 @@
|
|||||||
#ifndef _SWAY_IPC_H
|
#ifndef _SWAY_IPC_H
|
||||||
#define _SWAY_IPC_H
|
#define _SWAY_IPC_H
|
||||||
|
|
||||||
#include "container.h"
|
|
||||||
|
|
||||||
enum ipc_command_type {
|
enum ipc_command_type {
|
||||||
IPC_COMMAND = 0,
|
IPC_COMMAND = 0,
|
||||||
IPC_GET_WORKSPACES = 1,
|
IPC_GET_WORKSPACES = 1,
|
||||||
@ -15,10 +13,4 @@ enum ipc_command_type {
|
|||||||
IPC_SWAY_GET_PIXELS = 0x81
|
IPC_SWAY_GET_PIXELS = 0x81
|
||||||
};
|
};
|
||||||
|
|
||||||
void ipc_init(void);
|
|
||||||
void ipc_terminate(void);
|
|
||||||
struct sockaddr_un *ipc_user_sockaddr(void);
|
|
||||||
|
|
||||||
void ipc_event_workspace(swayc_t *old, swayc_t *new);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#include "layout.h"
|
#include "layout.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "input_state.h"
|
#include "input_state.h"
|
||||||
#include "ipc.h"
|
#include "ipc-server.h"
|
||||||
|
|
||||||
bool locked_container_focus = false;
|
bool locked_container_focus = false;
|
||||||
bool locked_view_focus = false;
|
bool locked_view_focus = false;
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
#include <list.h>
|
#include <list.h>
|
||||||
#include "ipc.h"
|
#include "ipc-server.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "commands.h"
|
#include "commands.h"
|
@ -14,7 +14,7 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "readline.h"
|
#include "readline.h"
|
||||||
#include "handlers.h"
|
#include "handlers.h"
|
||||||
#include "ipc.h"
|
#include "ipc-server.h"
|
||||||
#include "sway.h"
|
#include "sway.h"
|
||||||
|
|
||||||
static bool terminate_request = false;
|
static bool terminate_request = false;
|
||||||
|
@ -7,80 +7,14 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "stringop.h"
|
#include "stringop.h"
|
||||||
#include "ipc.h"
|
#include "ipc-client.h"
|
||||||
#include "readline.h"
|
#include "readline.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
|
||||||
static const size_t ipc_header_size = sizeof(ipc_magic)+8;
|
|
||||||
|
|
||||||
void sway_terminate(void) {
|
void sway_terminate(void) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_socketpath(void) {
|
|
||||||
FILE *fp = popen("sway --get-socketpath", "r");
|
|
||||||
if (!fp) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
char *line = read_line(fp);
|
|
||||||
pclose(fp);
|
|
||||||
return line;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *do_ipc(const char *socket_path, uint32_t type, const char *payload, uint32_t len) {
|
|
||||||
struct sockaddr_un addr;
|
|
||||||
int socketfd;
|
|
||||||
if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
||||||
sway_abort("Unable to open Unix socket");
|
|
||||||
}
|
|
||||||
addr.sun_family = AF_UNIX;
|
|
||||||
strcpy(addr.sun_path, socket_path);
|
|
||||||
int l = sizeof(addr.sun_family) + strlen(addr.sun_path);
|
|
||||||
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
|
|
||||||
sway_abort("Unable to connect to %s", socket_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
char data[ipc_header_size];
|
|
||||||
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
|
||||||
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
|
||||||
data32[0] = len;
|
|
||||||
data32[1] = type;
|
|
||||||
|
|
||||||
if (write(socketfd, data, ipc_header_size) == -1) {
|
|
||||||
sway_abort("Unable to send IPC header");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (write(socketfd, payload, len) == -1) {
|
|
||||||
sway_abort("Unable to send IPC payload");
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t total = 0;
|
|
||||||
while (total < ipc_header_size) {
|
|
||||||
ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0);
|
|
||||||
if (received < 0) {
|
|
||||||
sway_abort("Unable to receive IPC response");
|
|
||||||
}
|
|
||||||
total += received;
|
|
||||||
}
|
|
||||||
|
|
||||||
total = 0;
|
|
||||||
len = data32[0];
|
|
||||||
char *response = malloc(len + 1);
|
|
||||||
while (total < len) {
|
|
||||||
ssize_t received = recv(socketfd, response + total, len - total, 0);
|
|
||||||
if (received < 0) {
|
|
||||||
sway_abort("Unable to receive IPC response");
|
|
||||||
}
|
|
||||||
total += received;
|
|
||||||
}
|
|
||||||
response[len] = '\0';
|
|
||||||
|
|
||||||
close(socketfd);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
static int quiet = 0;
|
static int quiet = 0;
|
||||||
char *socket_path = NULL;
|
char *socket_path = NULL;
|
||||||
@ -159,7 +93,7 @@ int main(int argc, char **argv) {
|
|||||||
command = join_args(argv + optind, argc - optind);
|
command = join_args(argv + optind, argc - optind);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *resp = do_ipc(socket_path, type, command, strlen(command));
|
char *resp = ipc_single_command(socket_path, type, command, strlen(command));
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
printf("%s", resp);
|
printf("%s", resp);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user