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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xinerama.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
#include <sys/time.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
#define OOM_CHECK(p) do { if ((p) == NULL) die("Out of memory\n"); } while (0)
struct image {
char *name;
char *path;
int ox, oy;
int x, y;
int w, h;
int channels;
XImage **frames;
int *durations;
int framei, framecnt;
float frametime;
time_t last_frame;
struct image *next;
};
static void die(const char *fmtstr, ...);
static void monitor_offset(int num, int *x, int *y);
static void convert_rgb_bgr(uint8_t *pix, int width, int height,
int stride, int pixlen);
static void draw_img(struct image *img);
static void draw(void);
static void load(const char *path);
static bool load_attrs(FILE *file, struct image *img, int *lineno);
static void load_img(struct image *img);
static void free_img(struct image *img);
static const char *config_path;
static bool debug;
static struct image *images;
static Display *display;
static Screen *screen;
static Window root;
static Visual *vis;
static GC gc;
void
die(const char *fmtstr, ...)
{
va_list ap;
va_start(ap, fmtstr);
vfprintf(stderr, fmtstr, ap);
va_end(ap);
exit(1);
}
void
monitor_offset(int num, int *x, int *y)
{
XineramaScreenInfo *info;
int mcount, screen;
if (!XineramaIsActive(display))
die("No monitor support\n");
if (!(info = XineramaQueryScreens(display, &mcount)))
die("Failed to query xinerama\n");
if (num < 0 || num > mcount)
die("Monitor number out of range\n");
*x = info[num].x_org;
*y = info[num].y_org;
}
void
convert_rgb_bgr(uint8_t *pix, int width, int height, int stride, int pixlen)
{
uint8_t tmp, *cpix;
int x, y;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
cpix = &pix[y * stride + x * pixlen];
tmp = cpix[0];
cpix[0] = cpix[2];
cpix[2] = tmp;
}
}
}
void
draw_img(struct image *img)
{
if (debug) printf("DRAW %s\n", img->name);
XPutImage(display, root, gc, img->frames[img->framei], 0, 0,
img->x, img->y, img->w, img->h);
}
void
draw(void)
{
struct image *img;
for (img = images; img; img = img->next)
draw_img(img);
}
void
load(const char *path)
{
struct image img;
struct image **next;
int lineno;
FILE *file;
file = fopen(path, "r");
if (!file) die("Failed to open path: %s\n", path);
images = NULL;
next = &images;
lineno = 1;
while (!feof(file)) {
memset(&img, 0, sizeof(struct image));
img.last_frame = 0;
if (!load_attrs(file, &img, &lineno))
break; /* EOF */
img.x += img.ox;
img.y += img.oy;
load_img(&img);
*next = malloc(sizeof(struct image));
OOM_CHECK(*next);
memcpy(*next, &img, sizeof(struct image));
next = &((*next)->next);
}
fclose(file);
}
bool
load_attrs(FILE *file, struct image *img, int *lineno)
{
char *key, *val;
char line[256];
char *tok, end;
bool partial;
partial = false;
while (fgets(line, sizeof(line), file)) {
tok = strchr(line, '\n');
if (tok) *tok = '\0';
if (!strcmp(line, "")) {
if (partial) return true;
continue;
}
if (line[0] == '#')
continue;
partial = true;
if (!img->name) {
end = '\0';
if (sscanf(line, "[%m[-_a-zA-Z0-9]]%c",
&img->name, &end) != 1 || end)
die("Invalid title line %i:\n%s %c\n",
*lineno, line, end);
continue;
}
tok = strchr(line, ' ');
if (!tok) die("[%s] Invalid line %i: %s\n",
img->name, *lineno, line);
*tok = '\0';
key = line;
val = tok + 1;
if (!strcmp(key, "pos")) {
tok = strchr(val, 'x');
if (!tok) die("[%s] Invalid pos: %s\n",
img->name, val);
*tok = '\0';
img->x = atoi(val);
img->y = atoi(tok + 1);
} else if (!strcmp(key, "size")) {
tok = strchr(val, 'x');
if (!tok) die("[%s] Invalid size: %s\n",
img->name, val);
*tok = '\0';
if ((img->w = atoi(val)) <= 0)
die("[%s] Invalid width: %s\n",
img->name, val);
if ((img->h = atoi(tok + 1)) <= 0)
die("[%s] Invalid height: %s\n",
img->name, tok + 1);
} else if (!strcmp(key, "monitor")) {
monitor_offset(atoi(val), &img->ox, &img->oy);
} else if (!strcmp(key, "frametime")) {
img->frametime = atof(val);
} else if (!strcmp(key, "path")) {
img->path = strdup(val);
OOM_CHECK(img->path);
} else {
die("[%s] Invalid line %i:\n%s\n",
img->name, *lineno, line);
}
(*lineno)++;
}
return partial;
}
void
load_img(struct image *img)
{
char *args[4] = { NULL };
int width, height, i;
void *pix, *rpix;
stbi__context ctx;
stbi__result_info info;
stbi__gif gif;
uint8_t *frames;
FILE *file;
if (debug) printf("LOAD %s\n", img->name);
if (!(file = stbi__fopen(img->path, "rb")))
die("[%s] Path does not exist\n", img->name);
stbi__start_file(&ctx, file);
if (stbi__gif_test(&ctx)) {
frames = stbi__load_gif_main(&ctx, &img->durations,
&width, &height, &img->framecnt, &img->channels, 4);
if (!frames || !img->framecnt)
die("[%s] Failed to load gif image\n");
img->frames = malloc(sizeof(XImage*) * img->framecnt);
OOM_CHECK(img->frames);
for (i = 0; i < img->framecnt; i++) {
pix = frames + width * height * 4 * i;
rpix = malloc(img->w * img->h * 4);
OOM_CHECK(rpix);
if (!stbir_resize_uint8(pix, width, height, 0,
rpix, img->w, img->h, 0, 4))
die("[%s] Failed to resize image\n", img->name);
convert_rgb_bgr(rpix, img->w, img->h, img->w * 4, 4);
img->frames[i] = XCreateImage(display, vis,
24, ZPixmap, 0, rpix,
img->w, img->h, 32, 0);
if (!img->frames[i]) die("Failed to create XImage\n");
}
STBI_FREE(frames);
} else {
pix = stbi__load_main(&ctx, &width, &height,
&img->channels, 4, &info, 8);
if (!pix) die("[%s] Failed to load image\n", img->name);
if (!img->w || !img->h) {
img->w = width;
img->h = height;
}
rpix = malloc(img->w * img->h * 4);
OOM_CHECK(rpix);
if (!stbir_resize_uint8(pix, width, height, 0,
rpix, img->w, img->h, 0, 4))
die("[%s] Failed to resize image\n", img->name);
img->durations = NULL;
img->frames = malloc(sizeof(XImage*));
OOM_CHECK(img->frames);
img->frames[0] = XCreateImage(display, vis, 24,
ZPixmap, 0, rpix, img->w, img->h, 32, 0);
if (!img->frames[0]) die("Failed to create XImage\n");
free(pix);
}
for (i = 0; i < img->framecnt; i++) {
if (img->frametime)
img->durations[i] = img->frametime * 100;
else
img->durations[i] = img->durations[i] / 10;
}
img->framei = 0;
fclose(file);
}
void
free_img(struct image *img)
{
int i;
free(img->name);
free(img->path);
for (i = 0; i < img->framecnt; i++) {
free(img->frames[i]->data);
XFree(img->frames[i]);
}
STBI_FREE(img->durations);
}
void
init(void)
{
display = XOpenDisplay(NULL);
if (!display) die("Failed to open display\n");
screen = XDefaultScreenOfDisplay(display);
if (!screen) die("Failed to get screen\n");
vis = DefaultVisualOfScreen(screen);
root = RootWindowOfScreen(screen);
gc = XCreateGC(display, root, 0, NULL);
XSelectInput(display, root, ExposureMask);
XSync(display, True);
load(config_path);
}
void
deinit(void)
{
struct image *img, *next;
for (img = images; img; ) {
next = img->next;
free_img(img);
free(img);
img = next;
}
XCloseDisplay(display);
}
int
main(int argc, const char *argv[])
{
struct image *img;
const char **arg;
struct timeval tv, nowtv;
XEvent event;
time_t now;
bool done;
fd_set fds;
int xfd;
debug = false;
config_path = "drawrc";
for (arg = argv + 1; *arg; arg++) {
if (!strcmp(*arg, "-d")) {
debug = true;
} else if (!strcmp(*arg, "-c")) {
config_path = *(++arg);
} else {
die("USAGE: bgdraw [-d] [-c CONFIG]\n");
}
}
init();
draw();
done = false;
xfd = XConnectionNumber(display);
while (!done) {
FD_ZERO(&fds);
FD_SET(xfd, &fds);
tv.tv_usec = 50000;
tv.tv_sec = 0;
/* wait for X event or timer */
select(xfd + 1, &fds, NULL, NULL, &tv);
/* handle exposure events */
while (XPending(display)) {
XNextEvent(display, &event);
switch (event.type) {
case Expose:
draw();
break;
}
}
/* handle reloads */
gettimeofday(&nowtv, NULL);
now = nowtv.tv_sec * 100 + nowtv.tv_usec / 10000;
for (img = images; img; img = img->next) {
if (img->durations && img->last_frame
+ img->durations[img->framei] < now) {
img->last_frame = now;
draw_img(img);
img->framei++;
if (img->framei == img->framecnt)
img->framei = 0;
}
}
}
deinit();
}
|