]> git.sur5r.net Git - i3/i3/blob - include/libi3.h
Move ipc_send_message and ipc_recv_message to libi3
[i3/i3] / include / libi3.h
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #ifndef _LIBI3_H
6 #define _LIBI3_H
7
8 /**
9  * Try to get the socket path from X11 and return NULL if it doesn’t work.
10  *
11  * The memory for the socket path is dynamically allocated and has to be
12  * free()d by the caller.
13  *
14  */
15 char *socket_path_from_x11();
16
17 /**
18  * Safe-wrapper around malloc which exits if malloc returns NULL (meaning that
19  * there is no more memory available)
20  *
21  */
22 void *smalloc(size_t size);
23
24 /**
25  * Safe-wrapper around calloc which exits if malloc returns NULL (meaning that
26  * there is no more memory available)
27  *
28  */
29 void *scalloc(size_t size);
30
31 /**
32  * Safe-wrapper around realloc which exits if realloc returns NULL (meaning
33  * that there is no more memory available).
34  *
35  */
36 void *srealloc(void *ptr, size_t size);
37
38 /**
39  * Safe-wrapper around strdup which exits if malloc returns NULL (meaning that
40  * there is no more memory available)
41  *
42  */
43 char *sstrdup(const char *str);
44
45 /**
46  * Formats a message (payload) of the given size and type and sends it to i3 via
47  * the given socket file descriptor.
48  *
49  * Returns -1 when write() fails, errno will remain.
50  * Returns 0 on success.
51  *
52  */
53 int ipc_send_message(int sockfd, uint32_t message_size,
54                      uint32_t message_type, const uint8_t *payload);
55
56 /**
57  * Reads a message from the given socket file descriptor and stores its length
58  * (reply_length) as well as a pointer to its contents (reply).
59  *
60  * Returns -1 when read() fails, errno will remain.
61  * Returns -2 when the IPC protocol is violated (invalid magic, unexpected
62  * message type, EOF instead of a message). Additionally, the error will be
63  * printed to stderr.
64  * Returns 0 on success.
65  *
66  */
67 int ipc_recv_message(int sockfd, uint32_t message_type,
68                      uint32_t *reply_length, uint8_t **reply);
69
70 #endif