|
|
|
@ -31,9 +31,7 @@ static const int IPC_SOCKET_BACKLOG = 5; |
|
|
|
|
* Create IPC socket at specified path and return file descriptor to socket. |
|
|
|
|
* This initializes the static variable sockaddr. |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_create_socket(const char *filename) |
|
|
|
|
{ |
|
|
|
|
static int ipc_create_socket(const char *filename) { |
|
|
|
|
char *normal_filename; |
|
|
|
|
char *parent; |
|
|
|
|
const size_t addr_size = sizeof(struct sockaddr_un); |
|
|
|
@ -44,8 +42,8 @@ ipc_create_socket(const char *filename) |
|
|
|
|
// In case socket file exists
|
|
|
|
|
unlink(normal_filename); |
|
|
|
|
|
|
|
|
|
// For portability clear the addr structure, since some implementations have
|
|
|
|
|
// nonstandard fields in the structure
|
|
|
|
|
// For portability clear the addr structure, since some implementations
|
|
|
|
|
// have nonstandard fields in the structure
|
|
|
|
|
memset(&sockaddr, 0, addr_size); |
|
|
|
|
|
|
|
|
|
parentdir(normal_filename, &parent); |
|
|
|
@ -83,17 +81,16 @@ ipc_create_socket(const char *filename) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal function used to receive IPC messages from a given file descriptor. |
|
|
|
|
* Internal function used to receive IPC messages from a given file |
|
|
|
|
* descriptor. |
|
|
|
|
* |
|
|
|
|
* Returns -1 on error reading (could be EAGAIN or EINTR) |
|
|
|
|
* Returns -2 if EOF before header could be read |
|
|
|
|
* Returns -3 if invalid IPC header |
|
|
|
|
* Returns -4 if message length exceeds MAX_MESSAGE_SIZE |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_recv_message(int fd, uint8_t *msg_type, uint32_t *reply_size, |
|
|
|
|
uint8_t **reply) |
|
|
|
|
{ |
|
|
|
|
static int ipc_recv_message(int fd, uint8_t *msg_type, uint32_t *reply_size, |
|
|
|
|
uint8_t **reply) { |
|
|
|
|
uint32_t read_bytes = 0; |
|
|
|
|
const int32_t to_read = sizeof(dwm_ipc_header_t); |
|
|
|
|
char header[to_read]; |
|
|
|
@ -105,15 +102,19 @@ ipc_recv_message(int fd, uint8_t *msg_type, uint32_t *reply_size, |
|
|
|
|
|
|
|
|
|
if (n == 0) { |
|
|
|
|
if (read_bytes == 0) { |
|
|
|
|
fprintf(stderr, "Unexpectedly reached EOF while reading header."); |
|
|
|
|
fprintf(stderr, |
|
|
|
|
"Read %" PRIu32 " bytes, expected %" PRIu32 " total bytes.\n", |
|
|
|
|
"Unexpectedly reached EOF while reading header."); |
|
|
|
|
fprintf(stderr, |
|
|
|
|
"Read %" PRIu32 " bytes, expected %" PRIu32 |
|
|
|
|
" total bytes.\n", |
|
|
|
|
read_bytes, to_read); |
|
|
|
|
return -2; |
|
|
|
|
} else { |
|
|
|
|
fprintf(stderr, "Unexpectedly reached EOF while reading header."); |
|
|
|
|
fprintf(stderr, |
|
|
|
|
"Read %" PRIu32 " bytes, expected %" PRIu32 " total bytes.\n", |
|
|
|
|
"Unexpectedly reached EOF while reading header."); |
|
|
|
|
fprintf(stderr, |
|
|
|
|
"Read %" PRIu32 " bytes, expected %" PRIu32 |
|
|
|
|
" total bytes.\n", |
|
|
|
|
read_bytes, to_read); |
|
|
|
|
return -3; |
|
|
|
|
} |
|
|
|
@ -155,18 +156,22 @@ ipc_recv_message(int fd, uint8_t *msg_type, uint32_t *reply_size, |
|
|
|
|
|
|
|
|
|
read_bytes = 0; |
|
|
|
|
while (read_bytes < *reply_size) { |
|
|
|
|
const ssize_t n = read(fd, *reply + read_bytes, *reply_size - read_bytes); |
|
|
|
|
const ssize_t n = |
|
|
|
|
read(fd, *reply + read_bytes, *reply_size - read_bytes); |
|
|
|
|
|
|
|
|
|
if (n == 0) { |
|
|
|
|
fprintf(stderr, "Unexpectedly reached EOF while reading payload."); |
|
|
|
|
fprintf(stderr, "Read %" PRIu32 " bytes, expected %" PRIu32 " bytes.\n", |
|
|
|
|
fprintf(stderr, |
|
|
|
|
"Unexpectedly reached EOF while reading payload."); |
|
|
|
|
fprintf(stderr, |
|
|
|
|
"Read %" PRIu32 " bytes, expected %" PRIu32 " bytes.\n", |
|
|
|
|
read_bytes, *reply_size); |
|
|
|
|
free(*reply); |
|
|
|
|
return -2; |
|
|
|
|
} else if (n == -1) { |
|
|
|
|
// TODO: Should we return and wait for another epoll event?
|
|
|
|
|
// This would require saving the partial read in some way.
|
|
|
|
|
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) continue; |
|
|
|
|
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
free(*reply); |
|
|
|
|
return -1; |
|
|
|
@ -186,13 +191,12 @@ ipc_recv_message(int fd, uint8_t *msg_type, uint32_t *reply_size, |
|
|
|
|
* Returns -1 on unknown error trying to write, errno will carry over from |
|
|
|
|
* write() call |
|
|
|
|
*/ |
|
|
|
|
static ssize_t |
|
|
|
|
ipc_write_message(int fd, const void *buf, size_t count) |
|
|
|
|
{ |
|
|
|
|
static ssize_t ipc_write_message(int fd, const void *buf, size_t count) { |
|
|
|
|
size_t written = 0; |
|
|
|
|
|
|
|
|
|
while (written < count) { |
|
|
|
|
const ssize_t n = write(fd, (uint8_t *)buf + written, count - written); |
|
|
|
|
const ssize_t n = |
|
|
|
|
write(fd, (uint8_t *) buf + written, count - written); |
|
|
|
|
|
|
|
|
|
if (n == -1) { |
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
|
|
|
@ -215,20 +219,16 @@ ipc_write_message(int fd, const void *buf, size_t count) |
|
|
|
|
* handle, set yajl options, and in the future any other initialization that |
|
|
|
|
* should occur for event messages. |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_event_init_message(yajl_gen *gen) |
|
|
|
|
{ |
|
|
|
|
static void ipc_event_init_message(yajl_gen *gen) { |
|
|
|
|
*gen = yajl_gen_alloc(NULL); |
|
|
|
|
yajl_gen_config(*gen, yajl_gen_beautify, 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepares buffers of IPC subscribers of specified event using buffer from yajl |
|
|
|
|
* handle. |
|
|
|
|
* Prepares buffers of IPC subscribers of specified event using buffer from |
|
|
|
|
* yajl handle. |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_event_prepare_send_message(yajl_gen gen, IPCEvent event) |
|
|
|
|
{ |
|
|
|
|
static void ipc_event_prepare_send_message(yajl_gen gen, IPCEvent event) { |
|
|
|
|
const unsigned char *buffer; |
|
|
|
|
size_t len = 0; |
|
|
|
|
|
|
|
|
@ -251,21 +251,17 @@ ipc_event_prepare_send_message(yajl_gen gen, IPCEvent event) |
|
|
|
|
* handle, set yajl options, and in the future any other initialization that |
|
|
|
|
* should occur for reply messages. |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_reply_init_message(yajl_gen *gen) |
|
|
|
|
{ |
|
|
|
|
static void ipc_reply_init_message(yajl_gen *gen) { |
|
|
|
|
*gen = yajl_gen_alloc(NULL); |
|
|
|
|
yajl_gen_config(*gen, yajl_gen_beautify, 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepares the IPC client's buffer with a message using the buffer of the yajl |
|
|
|
|
* handle. |
|
|
|
|
* Prepares the IPC client's buffer with a message using the buffer of the |
|
|
|
|
* yajl handle. |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_reply_prepare_send_message(yajl_gen gen, IPCClient *c, |
|
|
|
|
IPCMessageType msg_type) |
|
|
|
|
{ |
|
|
|
|
static void ipc_reply_prepare_send_message(yajl_gen gen, IPCClient *c, |
|
|
|
|
IPCMessageType msg_type) { |
|
|
|
|
const unsigned char *buffer; |
|
|
|
|
size_t len = 0; |
|
|
|
|
|
|
|
|
@ -284,9 +280,7 @@ ipc_reply_prepare_send_message(yajl_gen gen, IPCClient *c, |
|
|
|
|
* Returns 0 if a command with the specified name was found |
|
|
|
|
* Returns -1 if a command with the specified name could not be found |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_get_ipc_command(const char *name, IPCCommand *ipc_command) |
|
|
|
|
{ |
|
|
|
|
static int ipc_get_ipc_command(const char *name, IPCCommand *ipc_command) { |
|
|
|
|
for (int i = 0; i < ipc_commands_len; i++) { |
|
|
|
|
if (strcmp(ipc_commands[i].name, name) == 0) { |
|
|
|
|
*ipc_command = ipc_commands[i]; |
|
|
|
@ -307,9 +301,8 @@ ipc_get_ipc_command(const char *name, IPCCommand *ipc_command) |
|
|
|
|
* Returns 0 if the message was successfully parsed |
|
|
|
|
* Returns -1 otherwise |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_parse_run_command(char *msg, IPCParsedCommand *parsed_command) |
|
|
|
|
{ |
|
|
|
|
static int ipc_parse_run_command(char *msg, |
|
|
|
|
IPCParsedCommand *parsed_command) { |
|
|
|
|
char error_buffer[1000]; |
|
|
|
|
yajl_val parent = yajl_tree_parse(msg, error_buffer, 1000); |
|
|
|
|
|
|
|
|
@ -336,7 +329,8 @@ ipc_parse_run_command(char *msg, IPCParsedCommand *parsed_command) |
|
|
|
|
|
|
|
|
|
const char *command_name = YAJL_GET_STRING(command_val); |
|
|
|
|
size_t command_name_len = strlen(command_name); |
|
|
|
|
parsed_command->name = (char *)malloc((command_name_len + 1) * sizeof(char)); |
|
|
|
|
parsed_command->name = |
|
|
|
|
(char *) malloc((command_name_len + 1) * sizeof(char)); |
|
|
|
|
strcpy(parsed_command->name, command_name); |
|
|
|
|
|
|
|
|
|
DEBUG("Received command: %s\n", parsed_command->name); |
|
|
|
@ -409,11 +403,10 @@ ipc_parse_run_command(char *msg, IPCParsedCommand *parsed_command) |
|
|
|
|
/**
|
|
|
|
|
* Free the members of a IPCParsedCommand struct |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_free_parsed_command_members(IPCParsedCommand *command) |
|
|
|
|
{ |
|
|
|
|
static void ipc_free_parsed_command_members(IPCParsedCommand *command) { |
|
|
|
|
for (int i = 0; i < command->argc; i++) { |
|
|
|
|
if (command->arg_types[i] == ARG_TYPE_STR) free((void *)command->args[i].v); |
|
|
|
|
if (command->arg_types[i] == ARG_TYPE_STR) |
|
|
|
|
free((void *) command->args[i].v); |
|
|
|
|
} |
|
|
|
|
free(command->args); |
|
|
|
|
free(command->arg_types); |
|
|
|
@ -428,10 +421,10 @@ ipc_free_parsed_command_members(IPCParsedCommand *command) |
|
|
|
|
* Returns -1 if the argument count doesn't match |
|
|
|
|
* Returns -2 if the argument types don't match |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_validate_run_command(IPCParsedCommand *parsed, const IPCCommand actual) |
|
|
|
|
{ |
|
|
|
|
if (actual.argc != parsed->argc) return -1; |
|
|
|
|
static int ipc_validate_run_command(IPCParsedCommand *parsed, |
|
|
|
|
const IPCCommand actual) { |
|
|
|
|
if (actual.argc != parsed->argc) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < parsed->argc; i++) { |
|
|
|
|
ArgType ptype = parsed->arg_types[i]; |
|
|
|
@ -458,9 +451,7 @@ ipc_validate_run_command(IPCParsedCommand *parsed, const IPCCommand actual) |
|
|
|
|
* Returns 0 if a valid event name was given |
|
|
|
|
* Returns -1 otherwise |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_event_stoi(const char *subscription, IPCEvent *event) |
|
|
|
|
{ |
|
|
|
|
static int ipc_event_stoi(const char *subscription, IPCEvent *event) { |
|
|
|
|
if (strcmp(subscription, "tag_change_event") == 0) |
|
|
|
|
*event = IPC_EVENT_TAG_CHANGE; |
|
|
|
|
else if (strcmp(subscription, "client_focus_change_event") == 0) |
|
|
|
@ -479,16 +470,15 @@ ipc_event_stoi(const char *subscription, IPCEvent *event) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse a IPC_TYPE_SUBSCRIBE message from a client. This function extracts the |
|
|
|
|
* event name and the subscription action from the message. |
|
|
|
|
* Parse a IPC_TYPE_SUBSCRIBE message from a client. This function extracts |
|
|
|
|
* the event name and the subscription action from the message. |
|
|
|
|
* |
|
|
|
|
* Returns 0 if message was successfully parsed |
|
|
|
|
* Returns -1 otherwise |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_parse_subscribe(const char *msg, IPCSubscriptionAction *subscribe, |
|
|
|
|
IPCEvent *event) |
|
|
|
|
{ |
|
|
|
|
static int ipc_parse_subscribe(const char *msg, |
|
|
|
|
IPCSubscriptionAction *subscribe, |
|
|
|
|
IPCEvent *event) { |
|
|
|
|
char error_buffer[100]; |
|
|
|
|
yajl_val parent = yajl_tree_parse((char *) msg, error_buffer, 100); |
|
|
|
|
|
|
|
|
@ -514,7 +504,8 @@ ipc_parse_subscribe(const char *msg, IPCSubscriptionAction *subscribe, |
|
|
|
|
const char *event_str = YAJL_GET_STRING(event_val); |
|
|
|
|
DEBUG("Received event: %s\n", event_str); |
|
|
|
|
|
|
|
|
|
if (ipc_event_stoi(event_str, event) < 0) return -1; |
|
|
|
|
if (ipc_event_stoi(event_str, event) < 0) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
const char *action_path[] = {"action", 0}; |
|
|
|
|
yajl_val action_val = yajl_tree_get(parent, action_path, yajl_t_string); |
|
|
|
@ -547,9 +538,7 @@ ipc_parse_subscribe(const char *msg, IPCSubscriptionAction *subscribe, |
|
|
|
|
* Returns 0 if message was successfully parsed |
|
|
|
|
* Returns -1 otherwise |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_parse_get_dwm_client(const char *msg, Window *win) |
|
|
|
|
{ |
|
|
|
|
static int ipc_parse_get_dwm_client(const char *msg, Window *win) { |
|
|
|
|
char error_buffer[100]; |
|
|
|
|
|
|
|
|
|
yajl_val parent = yajl_tree_parse(msg, error_buffer, 100); |
|
|
|
@ -581,20 +570,19 @@ ipc_parse_get_dwm_client(const char *msg, Window *win) |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called when an IPC_TYPE_RUN_COMMAND message is received from a client. This |
|
|
|
|
* function parses, executes the given command, and prepares a reply message to |
|
|
|
|
* the client indicating success/failure. |
|
|
|
|
* function parses, executes the given command, and prepares a reply message |
|
|
|
|
* to the client indicating success/failure. |
|
|
|
|
* |
|
|
|
|
* NOTE: There is currently no check for argument validity beyond the number of |
|
|
|
|
* arguments given and types of arguments. There is also no way to check if the |
|
|
|
|
* function succeeded based on dwm's void(const Arg*) function types. Pointer |
|
|
|
|
* arguments can cause crashes if they are not validated in the function itself. |
|
|
|
|
* NOTE: There is currently no check for argument validity beyond the number |
|
|
|
|
* of arguments given and types of arguments. There is also no way to check if |
|
|
|
|
* the function succeeded based on dwm's void(const Arg*) function types. |
|
|
|
|
* Pointer arguments can cause crashes if they are not validated in the |
|
|
|
|
* function itself. |
|
|
|
|
* |
|
|
|
|
* Returns 0 if message was successfully parsed |
|
|
|
|
* Returns -1 on failure parsing message |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_run_command(IPCClient *ipc_client, char *msg) |
|
|
|
|
{ |
|
|
|
|
static int ipc_run_command(IPCClient *ipc_client, char *msg) { |
|
|
|
|
IPCParsedCommand parsed_command; |
|
|
|
|
IPCCommand ipc_command; |
|
|
|
|
|
|
|
|
@ -609,7 +597,8 @@ ipc_run_command(IPCClient *ipc_client, char *msg) |
|
|
|
|
|
|
|
|
|
if (ipc_get_ipc_command(parsed_command.name, &ipc_command) < 0) { |
|
|
|
|
ipc_prepare_reply_failure(ipc_client, IPC_TYPE_RUN_COMMAND, |
|
|
|
|
"Command %s not found", parsed_command.name); |
|
|
|
|
"Command %s not found", |
|
|
|
|
parsed_command.name); |
|
|
|
|
ipc_free_parsed_command_members(&parsed_command); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
@ -630,7 +619,8 @@ ipc_run_command(IPCClient *ipc_client, char *msg) |
|
|
|
|
if (parsed_command.argc == 1) |
|
|
|
|
ipc_command.func.single_param(parsed_command.args); |
|
|
|
|
else if (parsed_command.argc > 1) |
|
|
|
|
ipc_command.func.array_param(parsed_command.args, parsed_command.argc); |
|
|
|
|
ipc_command.func.array_param(parsed_command.args, |
|
|
|
|
parsed_command.argc); |
|
|
|
|
|
|
|
|
|
DEBUG("Called function for command %s\n", parsed_command.name); |
|
|
|
|
|
|
|
|
@ -644,9 +634,7 @@ ipc_run_command(IPCClient *ipc_client, char *msg) |
|
|
|
|
* Called when an IPC_TYPE_GET_MONITORS message is received from a client. It |
|
|
|
|
* prepares a reply with the properties of all of the monitors in JSON. |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_get_monitors(IPCClient *c, Monitor *mons, Monitor *selmon) |
|
|
|
|
{ |
|
|
|
|
static void ipc_get_monitors(IPCClient *c, Monitor *mons, Monitor *selmon) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_reply_init_message(&gen); |
|
|
|
|
dump_monitors(gen, mons, selmon); |
|
|
|
@ -658,9 +646,8 @@ ipc_get_monitors(IPCClient *c, Monitor *mons, Monitor *selmon) |
|
|
|
|
* Called when an IPC_TYPE_GET_TAGS message is received from a client. It |
|
|
|
|
* prepares a reply with info about all the tags in JSON. |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_get_tags(IPCClient *c, const char *tags[], const int tags_len) |
|
|
|
|
{ |
|
|
|
|
static void ipc_get_tags(IPCClient *c, const char *tags[], |
|
|
|
|
const int tags_len) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_reply_init_message(&gen); |
|
|
|
|
|
|
|
|
@ -673,9 +660,8 @@ ipc_get_tags(IPCClient *c, const char *tags[], const int tags_len) |
|
|
|
|
* Called when an IPC_TYPE_GET_LAYOUTS message is received from a client. It |
|
|
|
|
* prepares a reply with a JSON array of available layouts |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
ipc_get_layouts(IPCClient *c, const Layout layouts[], const int layouts_len) |
|
|
|
|
{ |
|
|
|
|
static void ipc_get_layouts(IPCClient *c, const Layout layouts[], |
|
|
|
|
const int layouts_len) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_reply_init_message(&gen); |
|
|
|
|
|
|
|
|
@ -685,20 +671,20 @@ ipc_get_layouts(IPCClient *c, const Layout layouts[], const int layouts_len) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called when an IPC_TYPE_GET_DWM_CLIENT message is received from a client. It |
|
|
|
|
* prepares a JSON reply with the properties of the client with the specified |
|
|
|
|
* window XID. |
|
|
|
|
* Called when an IPC_TYPE_GET_DWM_CLIENT message is received from a client. |
|
|
|
|
* It prepares a JSON reply with the properties of the client with the |
|
|
|
|
* specified window XID. |
|
|
|
|
* |
|
|
|
|
* Returns 0 if the message was successfully parsed and if the client with the |
|
|
|
|
* specified window XID was found |
|
|
|
|
* Returns -1 if the message could not be parsed |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_get_dwm_client(IPCClient *ipc_client, const char *msg, const Monitor *mons) |
|
|
|
|
{ |
|
|
|
|
static int ipc_get_dwm_client(IPCClient *ipc_client, const char *msg, |
|
|
|
|
const Monitor *mons) { |
|
|
|
|
Window win; |
|
|
|
|
|
|
|
|
|
if (ipc_parse_get_dwm_client(msg, &win) < 0) return -1; |
|
|
|
|
if (ipc_parse_get_dwm_client(msg, &win) < 0) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
// Find client with specified window XID
|
|
|
|
|
for (const Monitor *m = mons; m; m = m->next) |
|
|
|
@ -722,20 +708,19 @@ ipc_get_dwm_client(IPCClient *ipc_client, const char *msg, const Monitor *mons) |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called when an IPC_TYPE_SUBSCRIBE message is received from a client. It |
|
|
|
|
* subscribes/unsubscribes the client from the specified event and replies with |
|
|
|
|
* the result. |
|
|
|
|
* subscribes/unsubscribes the client from the specified event and replies |
|
|
|
|
* with the result. |
|
|
|
|
* |
|
|
|
|
* Returns 0 if the message was successfully parsed. |
|
|
|
|
* Returns -1 if the message could not be parsed |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
ipc_subscribe(IPCClient *c, const char *msg) |
|
|
|
|
{ |
|
|
|
|
static int ipc_subscribe(IPCClient *c, const char *msg) { |
|
|
|
|
IPCSubscriptionAction action = IPC_ACTION_SUBSCRIBE; |
|
|
|
|
IPCEvent event = 0; |
|
|
|
|
|
|
|
|
|
if (ipc_parse_subscribe(msg, &action, &event)) { |
|
|
|
|
ipc_prepare_reply_failure(c, IPC_TYPE_SUBSCRIBE, "Event does not exist"); |
|
|
|
|
ipc_prepare_reply_failure(c, IPC_TYPE_SUBSCRIBE, |
|
|
|
|
"Event does not exist"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -755,15 +740,14 @@ ipc_subscribe(IPCClient *c, const char *msg) |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_init(const char *socket_path, const int p_epoll_fd, IPCCommand commands[], |
|
|
|
|
const int commands_len) |
|
|
|
|
{ |
|
|
|
|
int ipc_init(const char *socket_path, const int p_epoll_fd, |
|
|
|
|
IPCCommand commands[], const int commands_len) { |
|
|
|
|
// Initialize struct to 0
|
|
|
|
|
memset(&sock_epoll_event, 0, sizeof(sock_epoll_event)); |
|
|
|
|
|
|
|
|
|
int socket_fd = ipc_create_socket(socket_path); |
|
|
|
|
if (socket_fd < 0) return -1; |
|
|
|
|
if (socket_fd < 0) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
ipc_commands = commands; |
|
|
|
|
ipc_commands_len = commands_len; |
|
|
|
@ -781,9 +765,7 @@ ipc_init(const char *socket_path, const int p_epoll_fd, IPCCommand commands[], |
|
|
|
|
return socket_fd; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_cleanup() |
|
|
|
|
{ |
|
|
|
|
void ipc_cleanup() { |
|
|
|
|
IPCClient *c = ipc_clients; |
|
|
|
|
// Free clients and their buffers
|
|
|
|
|
while (c) { |
|
|
|
@ -809,27 +791,19 @@ ipc_cleanup() |
|
|
|
|
close(sock_fd); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_get_sock_fd() |
|
|
|
|
{ |
|
|
|
|
int ipc_get_sock_fd() { |
|
|
|
|
return sock_fd; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
IPCClient * |
|
|
|
|
ipc_get_client(int fd) |
|
|
|
|
{ |
|
|
|
|
IPCClient *ipc_get_client(int fd) { |
|
|
|
|
return ipc_list_get_client(ipc_clients, fd); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_is_client_registered(int fd) |
|
|
|
|
{ |
|
|
|
|
int ipc_is_client_registered(int fd) { |
|
|
|
|
return (ipc_get_client(fd) != NULL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_accept_client() |
|
|
|
|
{ |
|
|
|
|
int ipc_accept_client() { |
|
|
|
|
int fd = -1; |
|
|
|
|
|
|
|
|
|
struct sockaddr_un client_addr; |
|
|
|
@ -852,7 +826,8 @@ ipc_accept_client() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
IPCClient *nc = ipc_client_new(fd); |
|
|
|
|
if (nc == NULL) return -1; |
|
|
|
|
if (nc == NULL) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
// Wake up to messages from this client
|
|
|
|
|
nc->event.data.fd = fd; |
|
|
|
@ -866,9 +841,7 @@ ipc_accept_client() |
|
|
|
|
return fd; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_drop_client(IPCClient *c) |
|
|
|
|
{ |
|
|
|
|
int ipc_drop_client(IPCClient *c) { |
|
|
|
|
int fd = c->fd; |
|
|
|
|
shutdown(fd, SHUT_RDWR); |
|
|
|
|
int res = close(fd); |
|
|
|
@ -891,21 +864,20 @@ ipc_drop_client(IPCClient *c) |
|
|
|
|
return res; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_read_client(IPCClient *c, IPCMessageType *msg_type, uint32_t *msg_size, |
|
|
|
|
char **msg) |
|
|
|
|
{ |
|
|
|
|
int ipc_read_client(IPCClient *c, IPCMessageType *msg_type, |
|
|
|
|
uint32_t *msg_size, char **msg) { |
|
|
|
|
int fd = c->fd; |
|
|
|
|
int ret = |
|
|
|
|
ipc_recv_message(fd, (uint8_t *)msg_type, msg_size, (uint8_t **)msg); |
|
|
|
|
int ret = ipc_recv_message(fd, (uint8_t *) msg_type, msg_size, |
|
|
|
|
(uint8_t **) msg); |
|
|
|
|
|
|
|
|
|
if (ret < 0) { |
|
|
|
|
// This will happen if these errors occur while reading header
|
|
|
|
|
if (ret == -1 && |
|
|
|
|
(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
|
|
|
|
if (ret == -1 |
|
|
|
|
&& (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)) |
|
|
|
|
return -2; |
|
|
|
|
|
|
|
|
|
fprintf(stderr, "Error reading message: dropping client at fd %d\n", fd); |
|
|
|
|
fprintf(stderr, "Error reading message: dropping client at fd %d\n", |
|
|
|
|
fd); |
|
|
|
|
ipc_drop_client(c); |
|
|
|
|
|
|
|
|
|
return -1; |
|
|
|
@ -929,12 +901,11 @@ ipc_read_client(IPCClient *c, IPCMessageType *msg_type, uint32_t *msg_size, |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ssize_t |
|
|
|
|
ipc_write_client(IPCClient *c) |
|
|
|
|
{ |
|
|
|
|
ssize_t ipc_write_client(IPCClient *c) { |
|
|
|
|
const ssize_t n = ipc_write_message(c->fd, c->buffer, c->buffer_size); |
|
|
|
|
|
|
|
|
|
if (n < 0) return n; |
|
|
|
|
if (n < 0) |
|
|
|
|
return n; |
|
|
|
|
|
|
|
|
|
// TODO: Deal with client timeouts
|
|
|
|
|
|
|
|
|
@ -959,12 +930,11 @@ ipc_write_client(IPCClient *c) |
|
|
|
|
return n; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_prepare_send_message(IPCClient *c, const IPCMessageType msg_type, |
|
|
|
|
const uint32_t msg_size, const char *msg) |
|
|
|
|
{ |
|
|
|
|
dwm_ipc_header_t header = { |
|
|
|
|
.magic = IPC_MAGIC_ARR, .type = msg_type, .size = msg_size}; |
|
|
|
|
void ipc_prepare_send_message(IPCClient *c, const IPCMessageType msg_type, |
|
|
|
|
const uint32_t msg_size, const char *msg) { |
|
|
|
|
dwm_ipc_header_t header = {.magic = IPC_MAGIC_ARR, |
|
|
|
|
.type = msg_type, |
|
|
|
|
.size = msg_size}; |
|
|
|
|
|
|
|
|
|
uint32_t header_size = sizeof(dwm_ipc_header_t); |
|
|
|
|
uint32_t packet_size = header_size + msg_size; |
|
|
|
@ -987,10 +957,8 @@ ipc_prepare_send_message(IPCClient *c, const IPCMessageType msg_type, |
|
|
|
|
epoll_ctl(epoll_fd, EPOLL_CTL_MOD, c->fd, &c->event); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_prepare_reply_failure(IPCClient *c, IPCMessageType msg_type, |
|
|
|
|
const char *format, ...) |
|
|
|
|
{ |
|
|
|
|
void ipc_prepare_reply_failure(IPCClient *c, IPCMessageType msg_type, |
|
|
|
|
const char *format, ...) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
va_list args; |
|
|
|
|
|
|
|
|
@ -1013,39 +981,32 @@ ipc_prepare_reply_failure(IPCClient *c, IPCMessageType msg_type, |
|
|
|
|
free(buffer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_prepare_reply_success(IPCClient *c, IPCMessageType msg_type) |
|
|
|
|
{ |
|
|
|
|
void ipc_prepare_reply_success(IPCClient *c, IPCMessageType msg_type) { |
|
|
|
|
const char *success_msg = "{\"result\":\"success\"}"; |
|
|
|
|
const size_t msg_len = strlen(success_msg) + 1; // +1 for null char
|
|
|
|
|
|
|
|
|
|
ipc_prepare_send_message(c, msg_type, msg_len, success_msg); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_tag_change_event(int mon_num, TagState old_state, TagState new_state) |
|
|
|
|
{ |
|
|
|
|
void ipc_tag_change_event(int mon_num, TagState old_state, |
|
|
|
|
TagState new_state) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_event_init_message(&gen); |
|
|
|
|
dump_tag_event(gen, mon_num, old_state, new_state); |
|
|
|
|
ipc_event_prepare_send_message(gen, IPC_EVENT_TAG_CHANGE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_client_focus_change_event(int mon_num, Client *old_client, |
|
|
|
|
Client *new_client) |
|
|
|
|
{ |
|
|
|
|
void ipc_client_focus_change_event(int mon_num, Client *old_client, |
|
|
|
|
Client *new_client) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_event_init_message(&gen); |
|
|
|
|
dump_client_focus_change_event(gen, old_client, new_client, mon_num); |
|
|
|
|
ipc_event_prepare_send_message(gen, IPC_EVENT_CLIENT_FOCUS_CHANGE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_layout_change_event(const int mon_num, const char *old_symbol, |
|
|
|
|
void ipc_layout_change_event(const int mon_num, const char *old_symbol, |
|
|
|
|
const Layout *old_layout, const char *new_symbol, |
|
|
|
|
const Layout *new_layout) |
|
|
|
|
{ |
|
|
|
|
const Layout *new_layout) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_event_init_message(&gen); |
|
|
|
|
dump_layout_change_event(gen, mon_num, old_symbol, old_layout, new_symbol, |
|
|
|
@ -1053,30 +1014,27 @@ ipc_layout_change_event(const int mon_num, const char *old_symbol, |
|
|
|
|
ipc_event_prepare_send_message(gen, IPC_EVENT_LAYOUT_CHANGE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_monitor_focus_change_event(const int last_mon_num, const int new_mon_num) |
|
|
|
|
{ |
|
|
|
|
void ipc_monitor_focus_change_event(const int last_mon_num, |
|
|
|
|
const int new_mon_num) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_event_init_message(&gen); |
|
|
|
|
dump_monitor_focus_change_event(gen, last_mon_num, new_mon_num); |
|
|
|
|
ipc_event_prepare_send_message(gen, IPC_EVENT_MONITOR_FOCUS_CHANGE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_focused_title_change_event(const int mon_num, const Window client_id, |
|
|
|
|
const char *old_name, const char *new_name) |
|
|
|
|
{ |
|
|
|
|
void ipc_focused_title_change_event(const int mon_num, const Window client_id, |
|
|
|
|
const char *old_name, |
|
|
|
|
const char *new_name) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_event_init_message(&gen); |
|
|
|
|
dump_focused_title_change_event(gen, mon_num, client_id, old_name, new_name); |
|
|
|
|
dump_focused_title_change_event(gen, mon_num, client_id, old_name, |
|
|
|
|
new_name); |
|
|
|
|
ipc_event_prepare_send_message(gen, IPC_EVENT_FOCUSED_TITLE_CHANGE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_focused_state_change_event(const int mon_num, const Window client_id, |
|
|
|
|
void ipc_focused_state_change_event(const int mon_num, const Window client_id, |
|
|
|
|
const ClientState *old_state, |
|
|
|
|
const ClientState *new_state) |
|
|
|
|
{ |
|
|
|
|
const ClientState *new_state) { |
|
|
|
|
yajl_gen gen; |
|
|
|
|
ipc_event_init_message(&gen); |
|
|
|
|
dump_focused_state_change_event(gen, mon_num, client_id, old_state, |
|
|
|
@ -1084,20 +1042,21 @@ ipc_focused_state_change_event(const int mon_num, const Window client_id, |
|
|
|
|
ipc_event_prepare_send_message(gen, IPC_EVENT_FOCUSED_STATE_CHANGE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ipc_send_events(Monitor *mons, Monitor **lastselmon, Monitor *selmon) |
|
|
|
|
{ |
|
|
|
|
void ipc_send_events(Monitor *mons, Monitor **lastselmon, Monitor *selmon) { |
|
|
|
|
for (Monitor *m = mons; m; m = m->next) { |
|
|
|
|
unsigned int urg = 0, occ = 0, tagset = 0; |
|
|
|
|
|
|
|
|
|
for (Client *c = m->clients; c; c = c->next) { |
|
|
|
|
occ |= c->tags; |
|
|
|
|
|
|
|
|
|
if (c->isurgent) urg |= c->tags; |
|
|
|
|
if (c->isurgent) |
|
|
|
|
urg |= c->tags; |
|
|
|
|
} |
|
|
|
|
tagset = m->tagset[m->seltags]; |
|
|
|
|
|
|
|
|
|
TagState new_state = {.selected = tagset, .occupied = occ, .urgent = urg}; |
|
|
|
|
TagState new_state = {.selected = tagset, |
|
|
|
|
.occupied = occ, |
|
|
|
|
.urgent = urg}; |
|
|
|
|
|
|
|
|
|
if (memcmp(&m->tagstate, &new_state, sizeof(TagState)) != 0) { |
|
|
|
|
ipc_tag_change_event(m->num, m->tagstate, new_state); |
|
|
|
@ -1109,22 +1068,24 @@ ipc_send_events(Monitor *mons, Monitor **lastselmon, Monitor *selmon) |
|
|
|
|
m->lastsel = m->sel; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (strcmp(m->ltsymbol, m->lastltsymbol) != 0 || |
|
|
|
|
m->lastlt != m->lt[m->sellt]) { |
|
|
|
|
ipc_layout_change_event(m->num, m->lastltsymbol, m->lastlt, m->ltsymbol, |
|
|
|
|
m->lt[m->sellt]); |
|
|
|
|
if (strcmp(m->ltsymbol, m->lastltsymbol) != 0 |
|
|
|
|
|| m->lastlt != m->lt[m->sellt]) { |
|
|
|
|
ipc_layout_change_event(m->num, m->lastltsymbol, m->lastlt, |
|
|
|
|
m->ltsymbol, m->lt[m->sellt]); |
|
|
|
|
strcpy(m->lastltsymbol, m->ltsymbol); |
|
|
|
|
m->lastlt = m->lt[m->sellt]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (*lastselmon != selmon) { |
|
|
|
|
if (*lastselmon != NULL) |
|
|
|
|
ipc_monitor_focus_change_event((*lastselmon)->num, selmon->num); |
|
|
|
|
ipc_monitor_focus_change_event((*lastselmon)->num, |
|
|
|
|
selmon->num); |
|
|
|
|
*lastselmon = selmon; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Client *sel = m->sel; |
|
|
|
|
if (!sel) continue; |
|
|
|
|
if (!sel) |
|
|
|
|
continue; |
|
|
|
|
ClientState *o = &m->sel->prevstate; |
|
|
|
|
ClientState n = {.oldstate = sel->oldstate, |
|
|
|
|
.isfixed = sel->isfixed, |
|
|
|
@ -1139,12 +1100,11 @@ ipc_send_events(Monitor *mons, Monitor **lastselmon, Monitor *selmon) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_handle_client_epoll_event(struct epoll_event *ev, Monitor *mons, |
|
|
|
|
int ipc_handle_client_epoll_event(struct epoll_event *ev, Monitor *mons, |
|
|
|
|
Monitor **lastselmon, Monitor *selmon, |
|
|
|
|
const char *tags[], const int tags_len, |
|
|
|
|
const Layout *layouts, const int layouts_len) |
|
|
|
|
{ |
|
|
|
|
const Layout *layouts, |
|
|
|
|
const int layouts_len) { |
|
|
|
|
int fd = ev->data.fd; |
|
|
|
|
IPCClient *c = ipc_get_client(fd); |
|
|
|
|
|
|
|
|
@ -1153,14 +1113,16 @@ ipc_handle_client_epoll_event(struct epoll_event *ev, Monitor *mons, |
|
|
|
|
ipc_drop_client(c); |
|
|
|
|
} else if (ev->events & EPOLLOUT) { |
|
|
|
|
DEBUG("Sending message to client at fd %d...\n", fd); |
|
|
|
|
if (c->buffer_size) ipc_write_client(c); |
|
|
|
|
if (c->buffer_size) |
|
|
|
|
ipc_write_client(c); |
|
|
|
|
} else if (ev->events & EPOLLIN) { |
|
|
|
|
IPCMessageType msg_type = 0; |
|
|
|
|
uint32_t msg_size = 0; |
|
|
|
|
char *msg = NULL; |
|
|
|
|
|
|
|
|
|
DEBUG("Received message from fd %d\n", fd); |
|
|
|
|
if (ipc_read_client(c, &msg_type, &msg_size, &msg) < 0) return -1; |
|
|
|
|
if (ipc_read_client(c, &msg_type, &msg_size, &msg) < 0) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
if (msg_type == IPC_TYPE_GET_MONITORS) |
|
|
|
|
ipc_get_monitors(c, mons, selmon); |
|
|
|
@ -1169,12 +1131,15 @@ ipc_handle_client_epoll_event(struct epoll_event *ev, Monitor *mons, |
|
|
|
|
else if (msg_type == IPC_TYPE_GET_LAYOUTS) |
|
|
|
|
ipc_get_layouts(c, layouts, layouts_len); |
|
|
|
|
else if (msg_type == IPC_TYPE_RUN_COMMAND) { |
|
|
|
|
if (ipc_run_command(c, msg) < 0) return -1; |
|
|
|
|
if (ipc_run_command(c, msg) < 0) |
|
|
|
|
return -1; |
|
|
|
|
ipc_send_events(mons, lastselmon, selmon); |
|
|
|
|
} else if (msg_type == IPC_TYPE_GET_DWM_CLIENT) { |
|
|
|
|
if (ipc_get_dwm_client(c, msg, mons) < 0) return -1; |
|
|
|
|
if (ipc_get_dwm_client(c, msg, mons) < 0) |
|
|
|
|
return -1; |
|
|
|
|
} else if (msg_type == IPC_TYPE_SUBSCRIBE) { |
|
|
|
|
if (ipc_subscribe(c, msg) < 0) return -1; |
|
|
|
|
if (ipc_subscribe(c, msg) < 0) |
|
|
|
|
return -1; |
|
|
|
|
} else { |
|
|
|
|
fprintf(stderr, "Invalid message type received from fd %d", fd); |
|
|
|
|
ipc_prepare_reply_failure(c, msg_type, "Invalid message type: %d", |
|
|
|
@ -1182,17 +1147,17 @@ ipc_handle_client_epoll_event(struct epoll_event *ev, Monitor *mons, |
|
|
|
|
} |
|
|
|
|
free(msg); |
|
|
|
|
} else { |
|
|
|
|
fprintf(stderr, "Epoll event returned %d from fd %d\n", ev->events, fd); |
|
|
|
|
fprintf(stderr, "Epoll event returned %d from fd %d\n", ev->events, |
|
|
|
|
fd); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
ipc_handle_socket_epoll_event(struct epoll_event *ev) |
|
|
|
|
{ |
|
|
|
|
if (!(ev->events & EPOLLIN)) return -1; |
|
|
|
|
int ipc_handle_socket_epoll_event(struct epoll_event *ev) { |
|
|
|
|
if (!(ev->events & EPOLLIN)) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
// EPOLLIN means incoming client connection request
|
|
|
|
|
fputs("Received EPOLLIN event on socket\n", stderr); |
|
|
|
|