diff options
-rw-r--r-- | atom/atoms.gperf.m4 | 74 | ||||
-rw-r--r-- | atom/xcb_atom.h.m4 | 4 | ||||
-rw-r--r-- | configure.ac | 1 |
3 files changed, 79 insertions, 0 deletions
diff --git a/atom/atoms.gperf.m4 b/atom/atoms.gperf.m4 index ec733de..b51122c 100644 --- a/atom/atoms.gperf.m4 +++ b/atom/atoms.gperf.m4 @@ -1,6 +1,15 @@ %{ + +/* Rely on vasprintf (GNU extension) instead of vsnprintf if + possible... */ +#ifdef HAVE_VASPRINTF +#define _GNU_SOURCE +#include <stdio.h> +#endif + #include <xcb/xcb.h> #include <stdlib.h> +#include <stdarg.h> #include "xcb_atom.h" define(`COUNT', 0)dnl @@ -113,3 +122,68 @@ int get_atom_name(xcb_connection_t *c, xcb_atom_t atom, const char **namep, int free(atomr); return 1; } + +static char *makename(const char *fmt, ...) +{ + char *ret; + int n; + va_list ap; + +#ifndef HAVE_VASPRINTF + char *np; + int size = 64; + + /* First allocate 'size' bytes, should be enough usually */ + if((ret = malloc(size)) == NULL) + return NULL; + + while(1) + { + va_start(ap, fmt); + n = vsnprintf(ret, size, fmt, ap); + va_end(ap); + + if(n < 0) + return NULL; + + if(n < size) + return ret; + + size = n + 1; + if((np = realloc(ret, size)) == NULL) + { + free(ret); + return NULL; + } + + ret = np; + } +#else + va_start(ap, fmt); + n = vasprintf(&ret, fmt, ap); + va_end(ap); + + if(n < 0) + return NULL; + + return ret; +#endif +} + +char *xcb_atom_name_by_screen(const char *base, uint8_t screen) +{ + return makename("%s_S%u", base, screen); +} + +char *xcb_atom_name_by_resource(const char *base, uint32_t resource) +{ + return makename("%s_R%08X", base, resource); +} + +char *xcb_atom_name_unique(const char *base, uint32_t id) +{ + if(base) + return makename("%s_U%lu", base, id); + else + return makename("U%lu", id); +} diff --git a/atom/xcb_atom.h.m4 b/atom/xcb_atom.h.m4 index 6ea81d3..48e874f 100644 --- a/atom/xcb_atom.h.m4 +++ b/atom/xcb_atom.h.m4 @@ -20,6 +20,10 @@ xcb_atom_t intern_atom_fast_reply(xcb_connection_t *c, intern_atom_fast_cookie_t const char *get_atom_name_predefined(xcb_atom_t atom); int get_atom_name(xcb_connection_t *c, xcb_atom_t atom, const char **namep, int *lengthp); +char *xcb_atom_name_by_screen(const char *base, uint8_t screen); +char *xcb_atom_name_by_resource(const char *base, uint32_t resource); +char *xcb_atom_name_unique(const char *base, uint32_t id); + define(`DO', `extern const xcb_atom_t $1;')dnl include(atomlist.m4)`'dnl diff --git a/configure.ac b/configure.ac index 73e6fbb..9188350 100644 --- a/configure.ac +++ b/configure.ac @@ -20,6 +20,7 @@ else fi fi AC_SUBST(CWARNFLAGS) +AC_CHECK_FUNCS_ONCE(vasprintf) xcbincludedir='${includedir}/xcb' AC_SUBST(xcbincludedir) |