]> git.sur5r.net Git - i3/i3/blob - include/libi3.h
Move get_mod_mask to libi3, use it in i3 and i3-config-wizard
[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 #include <stdarg.h>
9 #include <xcb/xcb.h>
10 #include <xcb/xproto.h>
11 #include <xcb/xcb_keysyms.h>
12
13 /**
14  * Try to get the socket path from X11 and return NULL if it doesn’t work.
15  *
16  * The memory for the socket path is dynamically allocated and has to be
17  * free()d by the caller.
18  *
19  */
20 char *socket_path_from_x11();
21
22 /**
23  * Safe-wrapper around malloc which exits if malloc returns NULL (meaning that
24  * there is no more memory available)
25  *
26  */
27 void *smalloc(size_t size);
28
29 /**
30  * Safe-wrapper around calloc which exits if malloc returns NULL (meaning that
31  * there is no more memory available)
32  *
33  */
34 void *scalloc(size_t size);
35
36 /**
37  * Safe-wrapper around realloc which exits if realloc returns NULL (meaning
38  * that there is no more memory available).
39  *
40  */
41 void *srealloc(void *ptr, size_t size);
42
43 /**
44  * Safe-wrapper around strdup which exits if malloc returns NULL (meaning that
45  * there is no more memory available)
46  *
47  */
48 char *sstrdup(const char *str);
49
50 /**
51  * Safe-wrapper around asprintf which exits if it returns -1 (meaning that
52  * there is no more memory available)
53  *
54  */
55 int sasprintf(char **strp, const char *fmt, ...);
56
57 /**
58  * Connects to the i3 IPC socket and returns the file descriptor for the
59  * socket. die()s if anything goes wrong.
60  *
61  */
62 int ipc_connect(const char *socket_path);
63
64 /**
65  * Formats a message (payload) of the given size and type and sends it to i3 via
66  * the given socket file descriptor.
67  *
68  * Returns -1 when write() fails, errno will remain.
69  * Returns 0 on success.
70  *
71  */
72 int ipc_send_message(int sockfd, uint32_t message_size,
73                      uint32_t message_type, const uint8_t *payload);
74
75 /**
76  * Reads a message from the given socket file descriptor and stores its length
77  * (reply_length) as well as a pointer to its contents (reply).
78  *
79  * Returns -1 when read() fails, errno will remain.
80  * Returns -2 when the IPC protocol is violated (invalid magic, unexpected
81  * message type, EOF instead of a message). Additionally, the error will be
82  * printed to stderr.
83  * Returns 0 on success.
84  *
85  */
86 int ipc_recv_message(int sockfd, uint32_t message_type,
87                      uint32_t *reply_length, uint8_t **reply);
88
89 /**
90  * Generates a configure_notify event and sends it to the given window
91  * Applications need this to think they’ve configured themselves correctly.
92  * The truth is, however, that we will manage them.
93  *
94  */
95 void fake_configure_notify(xcb_connection_t *conn, xcb_rectangle_t r, xcb_window_t window, int border_width);
96
97 /**
98  * Returns the colorpixel to use for the given hex color (think of HTML). Only
99  * works for true-color (vast majority of cases) at the moment, avoiding a
100  * roundtrip to X11.
101  *
102  * The hex_color has to start with #, for example #FF00FF.
103  *
104  * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
105  * This has to be done by the caller.
106  *
107  * NOTE that this function may in the future rely on a global xcb_connection_t
108  * variable called 'conn' to be present.
109  *
110  */
111 uint32_t get_colorpixel(const char *hex) __attribute__((const));
112
113 #if defined(__APPLE__)
114
115 /*
116  * Taken from FreeBSD
117  * Returns a pointer to a new string which is a duplicate of the
118  * string, but only copies at most n characters.
119  *
120  */
121 char *strndup(const char *str, size_t n);
122
123 #endif
124
125 /**
126  * All-in-one function which returns the modifier mask (XCB_MOD_MASK_*) for the
127  * given keysymbol, for example for XCB_NUM_LOCK (usually configured to mod2).
128  *
129  * This function initiates one round-trip. Use get_mod_mask_for() directly if
130  * you already have the modifier mapping and key symbols.
131  *
132  */
133 uint32_t aio_get_mod_mask_for(uint32_t keysym, xcb_key_symbols_t *symbols);
134
135 /**
136  * Returns the modifier mask (XCB_MOD_MASK_*) for the given keysymbol, for
137  * example for XCB_NUM_LOCK (usually configured to mod2).
138  *
139  * This function does not initiate any round-trips.
140  *
141  */
142 uint32_t get_mod_mask_for(uint32_t keysym,
143                            xcb_key_symbols_t *symbols,
144                            xcb_get_modifier_mapping_reply_t *modmap_reply);
145
146 #endif