| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
#include <uthash.h> |
| 6 |
#include <xcb/xcb.h> |
| 7 |
|
| 8 |
#include "atom.h" |
| 9 |
#include "compiler.h" |
| 10 |
#include "log.h" |
| 11 |
#include "utils/cache.h" |
| 12 |
#include "utils/misc.h" |
| 13 |
|
| 14 |
struct atom_entry { |
| 15 |
struct cache_handle entry; |
| 16 |
UT_hash_handle hh; |
| 17 |
xcb_atom_t atom; |
| 18 |
}; |
| 19 |
|
| 20 |
struct atom_impl { |
| 21 |
struct atom base; |
| 22 |
struct cache c; |
| 23 |
struct atom_entry *atom_to_name; |
| 24 |
cache_getter_t getter; |
| 25 |
}; |
| 26 |
|
| 27 |
static inline int atom_getter(struct cache *cache, const char *atom_name, size_t keylen, |
| 28 |
struct cache_handle **value, void *user_data) { |
| 29 |
xcb_connection_t *c = user_data; |
| 30 |
auto atoms = container_of(cache, struct atom_impl, c); |
| 31 |
xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply( |
| 32 |
c, xcb_intern_atom(c, 0, to_u16_checked(keylen), atom_name), NULL); |
| 33 |
|
| 34 |
xcb_atom_t atom = XCB_NONE; |
| 35 |
if (reply) { |
| 36 |
log_debug("Atom %.*s is %d", (int)keylen, atom_name, reply->atom); |
| 37 |
atom = reply->atom; |
| 38 |
free(reply); |
| 39 |
} else { |
| 40 |
log_error("Failed to intern atoms"); |
| 41 |
return -1; |
| 42 |
} |
| 43 |
|
| 44 |
struct atom_entry *entry = ccalloc(1, struct atom_entry); |
| 45 |
entry->atom = atom; |
| 46 |
HASH_ADD_INT(atoms->atom_to_name, atom, entry); |
| 47 |
*value = &entry->entry; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
static inline int |
| 52 |
known_atom_getter(struct cache *cache attr_unused, const char *atom_name attr_unused, |
| 53 |
size_t keylen attr_unused, struct cache_handle **value, void *user_data) { |
| 54 |
auto atom = *(xcb_atom_t *)user_data; |
| 55 |
struct atom_entry *entry = ccalloc(1, struct atom_entry); |
| 56 |
entry->atom = atom; |
| 57 |
*value = &entry->entry; |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static inline void atom_entry_free(struct cache *cache, struct cache_handle *handle) { |
| 62 |
auto entry = cache_entry(handle, struct atom_entry, entry); |
| 63 |
auto atoms = container_of(cache, struct atom_impl, c); |
| 64 |
HASH_DEL(atoms->atom_to_name, entry); |
| 65 |
free(entry); |
| 66 |
} |
| 67 |
|
| 68 |
xcb_atom_t get_atom(struct atom *a, const char *key, size_t keylen, xcb_connection_t *c) { |
| 69 |
struct cache_handle *entry = NULL; |
| 70 |
auto atoms = container_of(a, struct atom_impl, base); |
| 71 |
if (cache_get_or_fetch(&atoms->c, key, keylen, &entry, c, atoms->getter) < 0) { |
| 72 |
log_error("Failed to get atom %s", key); |
| 73 |
return XCB_NONE; |
| 74 |
} |
| 75 |
return cache_entry(entry, struct atom_entry, entry)->atom; |
| 76 |
} |
| 77 |
|
| 78 |
xcb_atom_t get_atom_cached(struct atom *a, const char *key, size_t keylen) { |
| 79 |
auto atoms = container_of(a, struct atom_impl, base); |
| 80 |
auto entry = cache_get(&atoms->c, key, keylen); |
| 81 |
if (!entry) { |
| 82 |
return XCB_NONE; |
| 83 |
} |
| 84 |
return cache_entry(entry, struct atom_entry, entry)->atom; |
| 85 |
} |
| 86 |
|
| 87 |
const char *get_atom_name(struct atom *a, xcb_atom_t atom, xcb_connection_t *c) { |
| 88 |
struct atom_entry *entry = NULL; |
| 89 |
auto atoms = container_of(a, struct atom_impl, base); |
| 90 |
HASH_FIND(hh, atoms->atom_to_name, &atom, sizeof(xcb_atom_t), entry); |
| 91 |
if (!entry) { |
| 92 |
BUG_ON(c == NULL); |
| 93 |
auto r = xcb_get_atom_name_reply(c, xcb_get_atom_name(c, atom), NULL); |
| 94 |
if (!r) { |
| 95 |
log_error("Failed to get atom name"); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
char *atom_name = xcb_get_atom_name_name(r); |
| 99 |
auto len = (size_t)xcb_get_atom_name_name_length(r); |
| 100 |
struct cache_handle *handle = NULL; |
| 101 |
cache_get_or_fetch(&atoms->c, atom_name, len, &handle, &atom, known_atom_getter); |
| 102 |
entry = cache_entry(handle, struct atom_entry, entry); |
| 103 |
HASH_ADD_INT(atoms->atom_to_name, atom, entry); |
| 104 |
free(r); |
| 105 |
} |
| 106 |
return entry->entry.key; |
| 107 |
} |
| 108 |
|
| 109 |
const char *get_atom_name_cached(struct atom *a, xcb_atom_t atom) { |
| 110 |
struct atom_entry *entry = NULL; |
| 111 |
auto atoms = container_of(a, struct atom_impl, base); |
| 112 |
HASH_FIND(hh, atoms->atom_to_name, &atom, sizeof(xcb_atom_t), entry); |
| 113 |
if (!entry) { |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
return entry->entry.key; |
| 117 |
} |
| 118 |
|
| 119 |
static inline struct atom *init_atoms_impl(xcb_connection_t *c, cache_getter_t getter) { |
| 120 |
auto atoms = ccalloc(1, struct atom_impl); |
| 121 |
atoms->c = CACHE_INIT; |
| 122 |
atoms->getter = getter; |
| 123 |
#define ATOM_GET(x) atoms->base.a##x = get_atom(&atoms->base, #x, sizeof(#x) - 1, c) |
| 124 |
LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST1); |
| 125 |
LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST2); |
| 126 |
#undef ATOM_GET |
| 127 |
return &atoms->base; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
struct atom *init_atoms(xcb_connection_t *c) { |
| 134 |
return init_atoms_impl(c, atom_getter); |
| 135 |
} |
| 136 |
|
| 137 |
void destroy_atoms(struct atom *a) { |
| 138 |
auto atoms = container_of(a, struct atom_impl, base); |
| 139 |
cache_invalidate_all(&atoms->c, atom_entry_free); |
| 140 |
assert(atoms->atom_to_name == NULL); |
| 141 |
free(a); |
| 142 |
} |
| 143 |
|
| 144 |
#if defined(UNIT_TEST) || defined(CONFIG_FUZZER) |
| 145 |
|
| 146 |
static inline int mock_atom_getter(struct cache *cache, const char *atom_name attr_unused, |
| 147 |
size_t atom_len attr_unused, struct cache_handle **value, |
| 148 |
void *user_data attr_unused) { |
| 149 |
auto atoms = container_of(cache, struct atom_impl, c); |
| 150 |
xcb_atom_t atom = (xcb_atom_t)HASH_COUNT(atoms->atom_to_name) + 1; |
| 151 |
struct atom_entry *entry = ccalloc(1, struct atom_entry); |
| 152 |
entry->atom = atom; |
| 153 |
HASH_ADD_INT(atoms->atom_to_name, atom, entry); |
| 154 |
*value = &entry->entry; |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
struct atom *init_mock_atoms(void) { |
| 159 |
return init_atoms_impl(NULL, mock_atom_getter); |
| 160 |
} |
| 161 |
|
| 162 |
#else |
| 163 |
|
| 164 |
struct atom *init_mock_atoms(void) { |
| 165 |
abort(); |
| 166 |
} |
| 167 |
|
| 168 |
#endif |