-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmage.c
581 lines (506 loc) · 13.1 KB
/
mage.c
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* See LICENSE file for copyright and license details. */
#include <sys/types.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include <Imlib2.h>
char *argv0;
#include "arg.h"
#include "util.h"
#include "drw.h"
/* macros */
#define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
#define LENGTH(X) (sizeof(X) / sizeof(X)[0])
#define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
#define ABS(X) ((X) > 0 ? (X) : -(X))
enum { SchemeNorm, SchemeBar }; /* color schemes */
enum { WMDelete, WMName, WMFullscreen, WMState, WMLast }; /* atoms */
typedef union {
int i;
unsigned int ui;
float f;
const void *v;
} Arg;
typedef struct ScaleMode ScaleMode;
typedef struct {
//int re; /* rendered */
const char *fname;
int checkpan;
int zoomed;
int w, h; /* original dimension */
float x, y; /* position of the pan() */
float z; /* zoom */
Imlib_Image crop; /* small handy version of the image, intented to be use in layouts. */
int cw, ch; /* crop dimension */
} Image;
struct ScaleMode {
const char *name;
void (*arrange)(Image *im);
};
typedef struct {
const char *name;
void (*arrange)(void);
} Layout;
typedef struct {
unsigned int b;
int (*func)(const Arg *);
const Arg arg;
} Mousekey;
typedef struct {
unsigned int mod;
KeySym keysym;
int (*func)(const Arg *);
const Arg arg;
} Shortcut;
/* function declarations */
static void addfile(const char *file);
static int advance(const Arg *arg);
static void bpress(XEvent *e);
static void check_pan(Image *img);
static void cleanup(void);
static void cmessage(XEvent *e);
static void configurenotify(XEvent *e);
static int cyclescale(const Arg *arg);
static void drawbar(void);
static void expose(XEvent *e);
static int first(const Arg *arg);
static int fliphorz(const Arg *arg);
static int flipvert(const Arg *arg);
static unsigned int getsize(const char *file);
static void img_zoom(Image *img, float z);
static void kpress(XEvent *e);
static int last(const Arg *arg);
static int panhorz(const Arg *arg);
static int panvert(const Arg *arg);
static int printfile(const Arg *arg);
static int quit(const Arg *arg);
static void readstdin(void);
static int reload(const Arg *arg);
static int rotate(const Arg *arg);
static void run(void);
static int savestate(const Arg *arg);
static void scaledown(Image *img);
static void scalefit(Image *img);
static void scaleheight(Image *img);
static void scalewidth(Image *img);
static int set_zoom(const Arg *arg);
static int setlayout(const Arg *arg);
static void setup(void);
static void singleview(void);
static void thumbnailview(void);
static int toggleantialias(const Arg *arg);
static int togglebar(const Arg *arg);
static int togglefullscreen(const Arg *arg);
static void usage(void);
static void xhints(void);
static int zoom(const Arg *arg);
static int loadimgs(Image *i);
/* variables */
static Atom atom[WMLast];
static char right[128], left[128]; /* status bar */
static unsigned int filecnt = 0;
static int dirty = 0;
static size_t fileidx;
static Image *ci, *images; /* current image and images */
static const ScaleMode *scale; /* scalemode */
static const Layout *lt; /* scalemode */
static Drw *drw;
static Clr **scheme;
static int running = 1;
static int bh = 0; /* bar geometry */
//static int by; /* bar y */
static int lrpad; /* sum of left and right padding for text */
static char *wmname = "mage";
static unsigned int numlockmask = 0; //should this be handled at all? (updatenumlockmask)
static Display *dpy;
static Colormap cmap;
static Window win;
static Visual *visual = NULL;
static int depth, screen;
static int scrw, scrh; /* X display screen geometry width, height */
static int winw, winh; /* window width and height */
static int winy; /* window height - bar height */
//static int winx, winy;
static Pixmap pm;
static GC gc;
/* config.h for applying patches and the configuration. */
#include "config.h"
static void (*handler[LASTEvent])(XEvent *) = {
[ButtonPress] = bpress,
[ClientMessage] = cmessage,
[ConfigureNotify] = configurenotify,
[Expose] = expose,
[KeyPress] = kpress,
};
//at the end everything should be merged
#include "image.c" //image (and imlib2) operations
#include "cmd.c" //config.h commands
void
addfile(const char *file)
{
Imlib_Image *im;
if ((im = imlib_load_image(file))) { /* can be opened */
imlib_context_set_image(im);
imlib_free_image();
images[fileidx].fname = file;
images[fileidx].checkpan = 0;
images[fileidx].zoomed = 0;
images[fileidx].z = 1.0;
fileidx++;
} else if (!quiet)
fprintf(stderr, "mage: File '%s' cannot be opened.\n", file);
}
void
bpress(XEvent *e)
{
unsigned int i;
for (i = 0; i < LENGTH(mshortcuts); i++)
if (e->xbutton.button == mshortcuts[i].b && mshortcuts[i].func)
if (mshortcuts[i].func(&(mshortcuts[i].arg))) {
lt->arrange();
drawbar();
}
}
void
cleanup(void)
{
unsigned int i;
for (i = 0; i < filecnt; i++) {
if (images[i].crop) {
imlib_context_set_image(images[i].crop);
imlib_free_image();
images[i].crop = NULL;
}
}
for (i = 0; i < LENGTH(colors); i++)
free(scheme[i]);
free(scheme);
free(images);
drw_free(drw);
XFreeGC(dpy, gc);
XFreePixmap(dpy, pm);
XDestroyWindow(dpy, win);
XSync(dpy, False);
XCloseDisplay(dpy);
}
void
cmessage(XEvent *e)
{
if (e->xclient.data.l[0] == atom[WMDelete])
running = 0;
}
void
configurenotify(XEvent *e)
{
XConfigureEvent *ev = &e->xconfigure;
if (winw != ev->width || winh != ev->height) {
winw = ev->width;
winh = ev->height;
drw_resize(drw, winw, winh);
}
}
void
drawbar(void)
{
int tw = 0;
winy = winh - bh;
if (!showbar)
return;
/* currently topbar is not supported */
//y = topbar ? 0 : xw.h - bh;
drw_setscheme(drw, scheme[SchemeBar]);
/* left text */
snprintf(left, LENGTH(left), "%s %dx%d", ci->fname, ci->w, ci->h);
drw_text(drw, 0, winy, winw / 2, bh, lrpad / 2, left, 0);
/* right text */
snprintf(right, LENGTH(right), "%s <%d%%> [%zu/%u]", ci->zoomed ? "" : scale->name, (int)(ci->z * 100.0), fileidx + 1, filecnt);
tw = TEXTW(right) - lrpad + 2; /* 2px right padding */
drw_text(drw, winw / 2, winy, winw / 2, bh, winw / 2 - (tw + lrpad / 2), right, 0);
drw_map(drw, win, 0, winy, winw, bh);
}
void
expose(XEvent *e)
{
XExposeEvent *ev = &e->xexpose;
if (ev->count == 0) {
ci->zoomed = 1;
lt->arrange();
drawbar();
}
}
unsigned int
getsize(const char *file)
{
DIR *dir;
struct dirent *e;
struct stat s;
unsigned int cnt = 0;
if (!(stat(file, &s))) {
if (S_ISDIR(s.st_mode)) { /* it's a dir */
if ((dir = opendir(file))) {
while ((e = readdir(dir))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
continue;
if (e->d_type == DT_REG)
cnt++;
}
closedir(dir);
} else if (!quiet)
fprintf(stderr, "mage: Directory '%s', cannot be opened.\n", file);
} else if (S_ISREG(s.st_mode)) {
if (imlib_load_image(file)) {
cnt++;
} else {
if (!quiet)
fprintf(stderr, "mage: File '%s' cannot be opened.\n", file);
return 0;
}
} else
fprintf(stderr, "mage: '%s' No such file or directory.\n", file);
}
return cnt;
}
void
kpress(XEvent *e)
{
const XKeyEvent *ev = &e->xkey;
unsigned int i;
KeySym keysym;
keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
for (i = 0; i < LENGTH(shortcuts); i++)
if (keysym == shortcuts[i].keysym
&& CLEANMASK(shortcuts[i].mod) == CLEANMASK(ev->state)
&& shortcuts[i].func)
if (shortcuts[i].func(&(shortcuts[i].arg))) { /* if the func returns something, reload */
lt->arrange();
drawbar();
}
}
void
readstdin(void)
{
size_t n;
ssize_t len;
char *line = NULL;
size_t oldi = filecnt; /* big arbitrary size */
int sizecnt = 2;
/* read stdin */
while ((len = getline(&line, &n, stdin)) > 0) {
if (line[len-1] == '\n')
line[len-1] = '\0';
addfile(line);
line = NULL;
if (fileidx >= oldi) {
/* need more space, realloc */
if (!(images = realloc(images, sizeof(Image) * sizecnt * oldi)))
die("cannot realloc %u bytes:", sizeof(Image) * sizecnt * oldi);
sizecnt++;
}
}
/* realloc exactly the number of files that can be opened */
filecnt = fileidx;
if (!(images = realloc(images, sizeof(Image) * fileidx)))
die("cannot realloc %u bytes:", sizeof(Image) * fileidx);
}
void
run(void)
{
XEvent ev;
while (running) {
if (!XNextEvent(dpy, &ev) && handler[ev.type])
handler[ev.type](&ev); /* call handler */
if (dirty == 1) {
lt->arrange();
drawbar();
dirty = 0;
}
}
}
void
setup(void)
{
unsigned int i;
XTextProperty prop;
XSetWindowAttributes wa;
XGCValues gcval;
if (!(dpy = XOpenDisplay(NULL)))
die("mage: Unable to open display");
/* init image */
fileidx = 0;
ci = images;
if (!scale)
scale = &scalemodes[0];
if (!lt)
lt = &layouts[0];
/* init screen */
screen = DefaultScreen(dpy);
scrw = DisplayWidth(dpy, screen);
scrh = DisplayHeight(dpy, screen);
visual = DefaultVisual(dpy, screen);
cmap = DefaultColormap(dpy, screen);
depth = DefaultDepth(dpy, screen);
pm = 0;
winw = winwidth;
winh = winheight;
//wa.background_pixel = 0;
//wa.border_pixel = 0;
wa.colormap = cmap;
wa.save_under = False;
wa.bit_gravity = CenterGravity;
wa.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask |
ButtonPressMask; //ButtonMotionMask
win = XCreateWindow(dpy, XRootWindow(dpy, screen), 0, 0, winw, winh, 0,
depth, InputOutput, visual, 0, &wa);
XSelectInput(dpy, win, wa.event_mask);
/* init atoms */
atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
atom[WMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
atom[WMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
atom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
XSetWMProtocols(dpy, win, &atom[WMDelete], 1);
if (!(drw = drw_create(dpy, screen, win, winw, winh)))
die("mage: Unable to create drawing context");
/* init appearance */
scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
for (i = 0; i < LENGTH(colors); i++)
scheme[i] = drw_scm_create(drw, colors[i], 2);
XSetWindowBackground(dpy, win, scheme[SchemeNorm][ColBg].pixel);
gcval.foreground = scheme[SchemeNorm][ColBg].pixel;
gc = XCreateGC(dpy, win, GCForeground, &gcval); //context for Pixmap
if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
die("no fonts could be loaded.");
lrpad = drw->fonts->h;
bh = drw->fonts->h + 2; /* two pixel padding */
/* init bar */
drawbar();
XStringListToTextProperty(&argv0, 1, &prop);
XSetWMName(dpy, win, &prop);
XSetTextProperty(dpy, win, &prop, atom[WMName]);
XFree(prop.value);
XMapRaised(dpy, win);
drw_resize(drw, winw, winh);
xhints();
XSync(dpy, False);
/* init imlib */
imlib_context_set_display(dpy);
imlib_context_set_visual(visual);
imlib_context_set_colormap(cmap);
//imlib_context_set_drawable(xw.pm);
//imlib_context_set_drawable(xw.win);
}
void
usage(void)
{
die("usage: %s [-fhpqrv] [-s scalemode] [-n class] file...", argv0);
}
void
xhints(void)
{
XClassHint class = {.res_name = wmname, .res_class = "mage"};
XWMHints wm = {.flags = InputHint, .input = True};
XSizeHints *sizeh = NULL;
if (!(sizeh = XAllocSizeHints()))
die("mage: Unable to allocate size hints");
sizeh->flags = PSize;
sizeh->height = winh;
sizeh->width = winw;
XSetWMProperties(dpy, win, NULL, NULL, NULL, 0, sizeh, &wm, &class);
XFree(sizeh);
}
int
main(int argc, char *argv[])
{
DIR *dir;
struct dirent *e;
int i, fs = 0;
char *mode;
void (*scalefunc)(Image *im);
ARGBEGIN {
case 'f':
fs = 1;
break;
case 'h':
usage();
break;
case 'p':
antialiasing = 0;
break;
case 'q':
quiet = 1;
break;
case 'r':
loaddirs = 1;
break;
case 'n':
wmname = EARGF(usage());
break;
case 's':
mode = EARGF(usage());
if (!strcmp(mode, "down"))
scalefunc = scaledown;
else if (!strcmp(mode, "width"))
scalefunc = scalewidth;
else if (!strcmp(mode, "height"))
scalefunc = scaleheight;
else if (!strcmp(mode, "fit"))
scalefunc = scalefit;
else
break;
for (ScaleMode *l = (ScaleMode *)scalemodes; l; l++)
if (l->arrange == scalefunc) {
scale = l;
break;
}
break;
case 'v':
die("mage-"VERSION);
break;
default:
usage();
break;
} ARGEND
if (!argv[0])
usage();
if (loaddirs) {
for (i = 0; i < argc; i++)
filecnt += getsize(argv[i]); /* count images in dir */
} else if (!strcmp(argv[0], "-"))
filecnt = 1024; /* big arbitrary size */
else
filecnt = argc;
images = ecalloc(filecnt, sizeof(Image));
if (loaddirs) {
for (i = 0; i < argc; i++) {
if ((dir = opendir(argv[i]))) {
while ((e = readdir(dir))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, ".."))
continue;
if (e->d_type == DT_REG) {
int len = strlen(argv[i]) + 1 + strlen(e->d_name) + 1;
char *f = ecalloc(len, sizeof(char));
snprintf(f, len, "%s/%s", argv[i], e->d_name);
addfile(f);
}
}
closedir(dir);
} else
addfile(argv[i]);
}
} else if (!strcmp(argv[0], "-"))
readstdin();
else /* handle images */
for (i = 0; i < argc; i++)
addfile(argv[i]);
filecnt = fileidx;
if (!filecnt)
die("mage: No more images to display");
setup();
if (fs)
togglefullscreen(NULL);
run();
cleanup();
return EXIT_SUCCESS;
}