dwm/IPCClient.c
mihirlad55 4a230d362a [PATCH] Add IPC support through a unix socket
This patch currently supports the following requests:
* Run custom commands with arguments (similar to key bind functions)
* Get monitor properties
* Get all available layouts
* Get available tags
* Get client properties
* Subscribe to tag change, client focus change, and layout change,
  monitor focus change, focused title change, and client state change
  events

This patch includes a dwm-msg cli program that supports all of the
above requests for easy integration into shell scripts.

The messages are sent in a JSON format to promote integration to
increase scriptability in languages like Python/JavaScript.

The patch requires YAJL for JSON parsing and a system with epoll
support. Portability is planned to be increased in the future.

This patch is best applied after all other patches to avoid merge
conflicts.

For more info on the IPC implementation and how to send/receive
messages, documentation can be found at
https://github.com/mihirlad55/dwm-ipc
2023-08-04 21:33:07 +02:00

67 lines
1.2 KiB
C

#include "IPCClient.h"
#include <string.h>
#include <sys/epoll.h>
#include "util.h"
IPCClient *
ipc_client_new(int fd)
{
IPCClient *c = (IPCClient *)malloc(sizeof(IPCClient));
if (c == NULL) return NULL;
// Initialize struct
memset(&c->event, 0, sizeof(struct epoll_event));
c->buffer_size = 0;
c->buffer = NULL;
c->fd = fd;
c->event.data.fd = fd;
c->next = NULL;
c->prev = NULL;
c->subscriptions = 0;
return c;
}
void
ipc_list_add_client(IPCClientList *list, IPCClient *nc)
{
DEBUG("Adding client with fd %d to list\n", nc->fd);
if (*list == NULL) {
// List is empty, point list at first client
*list = nc;
} else {
IPCClient *c;
// Go to last client in list
for (c = *list; c && c->next; c = c->next)
;
c->next = nc;
nc->prev = c;
}
}
void
ipc_list_remove_client(IPCClientList *list, IPCClient *c)
{
IPCClient *cprev = c->prev;
IPCClient *cnext = c->next;
if (cprev != NULL) cprev->next = c->next;
if (cnext != NULL) cnext->prev = c->prev;
if (c == *list) *list = c->next;
}
IPCClient *
ipc_list_get_client(IPCClientList list, int fd)
{
for (IPCClient *c = list; c; c = c->next) {
if (c->fd == fd) return c;
}
return NULL;
}