2019-08-09 23:56:04 +00:00
|
|
|
#include <string.h>
|
2019-05-05 23:34:08 +00:00
|
|
|
#include <xcb/xcb.h>
|
|
|
|
|
|
|
|
#include "atom.h"
|
2024-02-15 21:08:48 +00:00
|
|
|
#include "cache.h"
|
2019-05-05 23:34:08 +00:00
|
|
|
#include "common.h"
|
2024-02-15 21:08:48 +00:00
|
|
|
#include "compiler.h"
|
2019-08-09 23:56:04 +00:00
|
|
|
#include "log.h"
|
2024-02-15 21:08:48 +00:00
|
|
|
#include "utils.h"
|
2019-05-05 23:34:08 +00:00
|
|
|
|
2024-02-16 01:00:50 +00:00
|
|
|
struct atom_entry {
|
|
|
|
struct cache_handle entry;
|
|
|
|
xcb_atom_t atom;
|
|
|
|
};
|
|
|
|
|
2024-02-16 00:31:53 +00:00
|
|
|
static inline int atom_getter(struct cache *cache attr_unused, const char *atom_name,
|
|
|
|
struct cache_handle **value, void *user_data) {
|
|
|
|
xcb_connection_t *c = user_data;
|
2019-05-05 23:34:08 +00:00
|
|
|
xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(
|
2024-02-16 00:31:53 +00:00
|
|
|
c, xcb_intern_atom(c, 0, to_u16_checked(strlen(atom_name)), atom_name), NULL);
|
2019-05-05 23:34:08 +00:00
|
|
|
|
|
|
|
xcb_atom_t atom = XCB_NONE;
|
|
|
|
if (reply) {
|
|
|
|
log_debug("Atom %s is %d", atom_name, reply->atom);
|
|
|
|
atom = reply->atom;
|
|
|
|
free(reply);
|
|
|
|
} else {
|
|
|
|
log_error("Failed to intern atoms");
|
2024-02-15 21:08:48 +00:00
|
|
|
return -1;
|
2019-05-05 23:34:08 +00:00
|
|
|
}
|
2024-02-15 21:08:48 +00:00
|
|
|
|
|
|
|
struct atom_entry *entry = ccalloc(1, struct atom_entry);
|
|
|
|
entry->atom = atom;
|
|
|
|
*value = &entry->entry;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
atom_entry_free(struct cache *cache attr_unused, struct cache_handle *handle) {
|
|
|
|
struct atom_entry *entry = cache_entry(handle, struct atom_entry, entry);
|
|
|
|
free(entry);
|
2019-05-05 23:34:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 00:31:53 +00:00
|
|
|
xcb_atom_t get_atom(struct atom *a, const char *key, xcb_connection_t *c) {
|
|
|
|
struct cache_handle *entry = NULL;
|
|
|
|
if (cache_get_or_fetch(&a->c, key, &entry, c, atom_getter) < 0) {
|
|
|
|
log_error("Failed to get atom %s", key);
|
|
|
|
return XCB_NONE;
|
|
|
|
}
|
|
|
|
return cache_entry(entry, struct atom_entry, entry)->atom;
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:00:50 +00:00
|
|
|
xcb_atom_t get_atom_cached(struct atom *a, const char *key) {
|
|
|
|
return cache_entry(cache_get(&a->c, key), struct atom_entry, entry)->atom;
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:34:08 +00:00
|
|
|
/**
|
2019-05-06 00:24:38 +00:00
|
|
|
* Create a new atom structure and fetch all predefined atoms
|
2019-05-05 23:34:08 +00:00
|
|
|
*/
|
|
|
|
struct atom *init_atoms(xcb_connection_t *c) {
|
|
|
|
auto atoms = ccalloc(1, struct atom);
|
2024-02-16 00:31:53 +00:00
|
|
|
atoms->c = CACHE_INIT;
|
|
|
|
#define ATOM_GET(x) atoms->a##x = get_atom(atoms, #x, c)
|
2020-10-22 00:39:51 +00:00
|
|
|
LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST1);
|
|
|
|
LIST_APPLY(ATOM_GET, SEP_COLON, ATOM_LIST2);
|
2019-05-05 23:34:08 +00:00
|
|
|
#undef ATOM_GET
|
|
|
|
return atoms;
|
|
|
|
}
|
2024-02-15 21:08:48 +00:00
|
|
|
|
|
|
|
void destroy_atoms(struct atom *a) {
|
|
|
|
cache_invalidate_all(&a->c, atom_entry_free);
|
|
|
|
free(a);
|
|
|
|
}
|