1 #include <string.h>
2 #include <xcb/xcb.h>
3 
4 #include "atom.h"
5 #include "common.h"
6 #include "log.h"
7 #include "utils.h"
8 
atom_getter(void * ud,const char * atom_name,int * err)9 static inline void *atom_getter(void *ud, const char *atom_name, int *err) {
10 	xcb_connection_t *c = ud;
11 	xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(
12 	    c, xcb_intern_atom(c, 0, to_u16_checked(strlen(atom_name)), atom_name), NULL);
13 
14 	xcb_atom_t atom = XCB_NONE;
15 	if (reply) {
16 		log_debug("Atom %s is %d", atom_name, reply->atom);
17 		atom = reply->atom;
18 		free(reply);
19 	} else {
20 		log_error("Failed to intern atoms");
21 		*err = 1;
22 	}
23 	return (void *)(intptr_t)atom;
24 }
25 
26 /**
27  * Create a new atom structure and fetch all predefined atoms
28  */
init_atoms(xcb_connection_t * c)29 struct atom *init_atoms(xcb_connection_t *c) {
30 	auto atoms = ccalloc(1, struct atom);
31 	atoms->c = new_cache((void *)c, atom_getter, NULL);
32 #define ATOM_GET(x) atoms->a##x = (xcb_atom_t)(intptr_t)cache_get(atoms->c, #x, NULL)
33 	LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST);
34 #undef ATOM_GET
35 	return atoms;
36 }
37