#ifndef COMMON_H
#define COMMON_H
#include "config.h"
#define _XOPEN_SOURCE 600
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#define COUNTOF(x) (sizeof(x)/sizeof((x)[0]))
#define CLEARFROM(x, field) memset( &((x)->field), 0, sizeof(*(x)) - ((const char *)&((x)->field) - (const char *)(x)) )
#define LOG(stream, prefix, suffix, result, ...) \
( fputs(prefix, stream), fprintf(stream, __VA_ARGS__), fputs(suffix, stream), fflush(stream), dummy(result) )
#ifdef NDEBUG
#define LOGDBG(...) dummy(1)
#else
#define LOGDBG(...) LOG(stdout, "DEBUG: ", "\n", 1, __VA_ARGS__)
#endif
#define LOGINF(...) LOG(stdout, "INFO: ", "\n", 1, __VA_ARGS__)
#define LOGWRN(...) LOG(stderr, "WARNING: ", "\n", 1, __VA_ARGS__)
#define LOGERR(...) LOG(stderr, "ERROR: ", "\n", 0, __VA_ARGS__)
struct App;
typedef struct App App;
static inline int dummy(int i) { return i; }
unsigned int keysymCharcode(unsigned int keySym);
int keysymString(unsigned int keySym, char *buf5bytes);
// cyclic monotonic timer
static inline unsigned int monotonicMs() {
struct timespec t = {};
clock_gettime(CLOCK_MONOTONIC, &t);
return (unsigned int)t.tv_sec*1000u + (unsigned int)t.tv_nsec/1000000u;
}
static inline void rectIntersect(int *x, int *y, int *w, int *h, int xx, int yy, int ww, int hh) {
if (*x < xx) *w += *x - xx, *x = xx;
if (*y < yy) *h += *y - yy, *y = xx;
ww += xx; hh += yy;
if (*x + *w > ww) *w = ww - *x;
if (*y + *h > hh) *h = hh - *y;
}
static inline void rectMerge(int *x, int *y, int *w, int *h, int xx, int yy, int ww, int hh) {
if (ww <= 0 || hh <= 0)
return;
if (*w <= 0 || *h <= 0)
{ *x = xx, *y = yy, *w = ww, *h = hh; return; }
if (*x > xx) *w += *x - xx, *x = xx;
if (*y > yy) *h += *y - yy, *y = yy;
ww += xx; hh += yy;
if (*x + *w < ww) *w = ww - *x;
if (*y + *h < hh) *h = hh - *y;
}
#endif