-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.h
202 lines (166 loc) · 4.2 KB
/
misc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef MISC_H
#define MISC_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <sys/select.h>
#include "types.h"
#ifdef __GNUC__
#define __printf(a, b) __attribute__((format(printf, a, b)))
#else
#define __printf(a, b)
#endif
#ifndef __GLIBC_PREREQ
#define __GLIBC_PREREQ(...) 0
#endif
#define ARR_LEN(a) (sizeof(a) / sizeof(a[0]))
#define LOOKUP(key, table) (assert(key < ARR_LEN(table)), \
assert(key >= 0), \
table[key])
static inline void* xmalloc(size_t s)
{
void* p = malloc(s);
if (!p) {
perror("malloc");
abort();
}
return p;
}
static inline void* xcalloc(size_t s)
{
void* p = calloc(1, s);
if (!p) {
perror("calloc");
abort();
}
return p;
}
static inline void* xrealloc(void* p, size_t s)
{
void* newp = realloc(p, s);
if (!newp) {
perror("realloc");
abort();
}
return newp;
}
static inline char* xstrdup(const char* str)
{
char* s = strdup(str);
if (!s) {
perror("strdup");
abort();
}
return s;
}
static inline char* xvasprintf(const char* fmt, va_list va)
{
char* ret;
int status = vasprintf(&ret, fmt, va);
if (status < 0) {
perror("xvasprintf");
abort();
}
return ret;
}
static inline char* xasprintf(const char* fmt, ...)
{
va_list va;
char* ret;
va_start(va, fmt);
ret = xvasprintf(fmt, va);
va_end(va);
return ret;
}
static inline void xfree(void* p)
{
free(p);
}
static inline void fdset_add(int fd, fd_set* set, int* nfds)
{
FD_SET(fd, set);
if (fd >= *nfds)
*nfds = fd + 1;
}
enum {
LL_BUG = 1,
LL_ERROR,
LL_WARN,
LL_INFO,
LL_VERBOSE,
LL_DEBUG,
LL_DEBUG2,
};
__printf(1, 2) void initerr(const char* fmt, ...);
#define initdie(...) do { initerr(__VA_ARGS__); exit(1); } while (0)
__printf(2, 3) void mlog(unsigned int level, const char* fmt, ...);
#define errlog(fmt, ...) mlog(LL_ERROR, "Error: "fmt, ##__VA_ARGS__)
#define warn(fmt, ...) mlog(LL_WARN, "Warning: "fmt, ##__VA_ARGS__)
#define bug(fmt, ...) mlog(LL_BUG, "BUG (please report!): "fmt, ##__VA_ARGS__)
#define info(fmt, ...) mlog(LL_INFO, fmt, ##__VA_ARGS__)
#define vinfo(fmt, ...) mlog(LL_VERBOSE, fmt, ##__VA_ARGS__)
#define debug(fmt, ...) mlog(LL_DEBUG, fmt, ##__VA_ARGS__)
#define debug2(fmt, ...) mlog(LL_DEBUG2, fmt, ##__VA_ARGS__)
typedef enum {
MASTER,
REMOTE,
} opmode_t;
extern opmode_t opmode;
extern struct node* focused_node;
static inline int is_remote(struct node* n)
{
return !!n->remote;
}
static inline int is_master(struct node* n)
{
return !n->remote;
}
void run_remote(void);
void set_loglevel(unsigned int level);
extern struct msgchan stdio_msgchan;
void send_keyevent(struct remote* rmt, keycode_t kc, pressrel_t pr);
void send_moverel(struct remote* rmt, int32_t dx, int32_t dy);
void send_clickevent(struct remote* rmt, mousebutton_t button, pressrel_t pr);
void send_setbrightness(struct remote* rmt, float f);
int get_fd_nonblock(int fd);
void set_fd_nonblock(int fd, int nb);
void set_fd_cloexec(int fd, int ce);
char* expand_word(const char* wd);
struct kvpair* flatten_kvmap(const struct kvmap* kvm, u_int* numpairs);
struct kvmap* unflatten_kvmap(const struct kvpair* pairs, u_int numpairs);
void set_clipboard_from_buf(const void* buf, size_t len);
void explicit_bzero(void* p, size_t n);
/*
* Make a function to produce a gamma value for index 'idx' in a gamma table
* by scaling (by compressing/expanding the X axis and interpolating, not just
* multiplying the absolute value along the Y axis, so as to preserve relative
* RGB curves) the values in the given 'from' array ('numents' long) 'scale'.
*
* This is done this way because OSX uses floats (with a "CGGammaValue"
* typedef) for its gamma tables, whereas X11 uses unsigned shorts.
*/
#define MAKE_GAMMA_SCALE_FN(name, type, defloat) \
type name(type* from, int numents, int idx, float scale) \
{ \
float f_idx, f_loidx, frac; \
int loidx; \
float lo, hi; \
\
assert(scale >= 0.0); \
\
f_idx = (float)idx * scale; \
\
frac = modff(f_idx, &f_loidx); \
loidx = lrintf(f_loidx); \
\
if (loidx >= numents - 1) \
return from[numents-1]; \
\
lo = (float)(from[loidx]); \
hi = (float)(from[loidx+1]); \
\
return defloat(lo + (frac * (hi - lo))); \
}
#endif /* MISC_H */