Krolyxon
Commit 591be8c

Branches

Refs →

nsxivnsxiv.h

452 lines · 7.8 KB · Objective-C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021-2022 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef NSXIV_H
21#define NSXIV_H
22
23#include <stdarg.h>
24#include <stdbool.h>
25#include <stdio.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <Imlib2.h>
29#include <X11/Xlib.h>
30
31/*
32 * Annotation for functions called in cleanup().
33 * These functions are not allowed to call error(!0, ...) or exit().
34 */
35#define CLEANUP
36
37#define MIN(a,b) ((a) < (b) ? (a) : (b))
38#define MAX(a,b) ((a) > (b) ? (a) : (b))
39#define ABS(a) ((a) > 0 ? (a) : -(a))
40
41#define ARRLEN(a) (sizeof(a) / sizeof((a)[0]))
42
43#define STREQ(s1,s2) (strcmp((s1), (s2)) == 0)
44
45#define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \
46 ((t1)->tv_usec - (t2)->tv_usec) / 1000)
47#define TV_TO_MS(tv) (((tv)->tv_sec * 1000) + ((tv)->tv_usec / 1000))
48
49#define TV_SET_MSEC(tv,t) { \
50 (tv)->tv_sec = (t) / 1000; \
51 (tv)->tv_usec = (t) % 1000 * 1000; \
52}
53
54#define TV_ADD_MSEC(tv,t) { \
55 (tv)->tv_sec += (t) / 1000; \
56 (tv)->tv_usec += (t) % 1000 * 1000; \
57}
58
59typedef enum {
60 MODE_ALL,
61 MODE_IMAGE,
62 MODE_THUMB
63} appmode_t;
64
65typedef enum {
66 DIR_LEFT = 1,
67 DIR_RIGHT = 2,
68 DIR_UP = 4,
69 DIR_DOWN = 8
70} direction_t;
71
72typedef enum {
73 DEGREE_90 = 1,
74 DEGREE_180 = 2,
75 DEGREE_270 = 3
76} degree_t;
77
78typedef enum {
79 FLIP_HORIZONTAL = 1,
80 FLIP_VERTICAL = 2
81} flipdir_t;
82
83typedef enum {
84 SCALE_DOWN,
85 SCALE_FIT,
86 SCALE_FILL,
87 SCALE_WIDTH,
88 SCALE_HEIGHT,
89 SCALE_ZOOM
90} scalemode_t;
91
92typedef enum {
93 DRAG_RELATIVE,
94 DRAG_ABSOLUTE
95} dragmode_t;
96
97typedef enum {
98 CURSOR_ARROW,
99 CURSOR_DRAG_ABSOLUTE,
100 CURSOR_DRAG_RELATIVE,
101 CURSOR_WATCH,
102 CURSOR_LEFT,
103 CURSOR_RIGHT,
104 CURSOR_NONE,
105
106 CURSOR_COUNT
107} cursor_t;
108
109typedef enum {
110 FF_WARN = 1,
111 FF_MARK = 2,
112 FF_TN_INIT = 4
113} fileflags_t;
114
115typedef struct {
116 const char *name; /* as given by user */
117 const char *path; /* always absolute */
118 fileflags_t flags;
119} fileinfo_t;
120
121/* timeouts in milliseconds: */
122enum {
123 TO_REDRAW_RESIZE = 75,
124 TO_REDRAW_THUMBS = 200,
125 TO_CURSOR_HIDE = 1200,
126 TO_DOUBLE_CLICK = 300
127};
128
129typedef void (*timeout_f)(void);
130
131typedef struct arl arl_t;
132typedef struct img img_t;
133typedef struct opt opt_t;
134typedef struct tns tns_t;
135typedef struct win win_t;
136
137
138/* autoreload.c */
139
140struct arl {
141 int fd;
142 int wd_dir;
143 int wd_file;
144 char *filename;
145};
146
147void arl_init(arl_t*);
148void arl_cleanup(arl_t*);
149void arl_setup(arl_t*, const char* /* result of realpath(3) */);
150bool arl_handle(arl_t*);
151
152
153/* commands.c */
154
155typedef int arg_t;
156typedef bool (*cmd_f)(arg_t);
157
158typedef struct {
159 cmd_f func;
160 appmode_t mode;
161} cmd_t;
162
163typedef struct {
164 unsigned int mask;
165 KeySym ksym_or_button;
166 cmd_t cmd;
167 arg_t arg;
168} keymap_t;
169
170typedef keymap_t button_t;
171
172
173/* image.c */
174
175typedef struct {
176 Imlib_Image im;
177 unsigned int delay;
178} img_frame_t;
179
180typedef struct {
181 img_frame_t *frames;
182 int cap;
183 int cnt;
184 int sel;
185 bool animate;
186 unsigned int framedelay;
187 int length;
188} multi_img_t;
189
190struct img {
191 Imlib_Image im;
192 int w;
193 int h;
194
195 win_t *win;
196 float x;
197 float y;
198
199 Imlib_Color_Modifier cmod;
200 int gamma;
201
202 scalemode_t scalemode;
203 float zoom;
204
205 bool checkpan;
206 bool dirty;
207 bool aa;
208 bool alpha;
209
210 struct {
211 bool on;
212 int delay;
213 } ss;
214
215 multi_img_t multi;
216};
217
218void img_init(img_t*, win_t*);
219bool img_load(img_t*, const fileinfo_t*);
220CLEANUP void img_close(img_t*, bool);
221void img_render(img_t*);
222bool img_fit_win(img_t*, scalemode_t);
223bool img_zoom(img_t*, int);
224bool img_zoom_to(img_t*, float);
225bool img_pos(img_t*, float, float);
226bool img_pan(img_t*, direction_t, int);
227bool img_pan_center(img_t*);
228bool img_pan_edge(img_t*, direction_t);
229void img_rotate(img_t*, degree_t);
230void img_flip(img_t*, flipdir_t);
231void img_toggle_antialias(img_t*);
232bool img_change_gamma(img_t*, int);
233bool img_frame_navigate(img_t*, int);
234bool img_frame_animate(img_t*);
235
236
237/* options.c */
238
239struct opt {
240 /* file list: */
241 char **filenames;
242 bool from_stdin;
243 bool to_stdout;
244 bool using_null;
245 bool recursive;
246 int filecnt;
247 int startnum;
248
249 /* image: */
250 scalemode_t scalemode;
251 float zoom;
252 bool animate;
253 int gamma;
254 int slideshow;
255 int framerate;
256
257 /* window: */
258 bool fullscreen;
259 bool hide_bar;
260 Window embed; /* unsigned long */
261 char *geometry;
262 char *res_name;
263
264 /* misc flags: */
265 bool quiet;
266 bool thumb_mode;
267 bool clean_cache;
268 bool private_mode;
269};
270
271extern const opt_t *options;
272
273void print_usage(void);
274void parse_options(int, char**);
275
276
277/* thumbs.c */
278
279typedef struct {
280 Imlib_Image im;
281 int w;
282 int h;
283 int x;
284 int y;
285} thumb_t;
286
287struct tns {
288 fileinfo_t *files;
289 thumb_t *thumbs;
290 const int *cnt;
291 int *sel;
292 int initnext;
293 int loadnext;
294 int first, end;
295 int r_first, r_end;
296
297 win_t *win;
298 int x;
299 int y;
300 int cols;
301 int rows;
302 int zl;
303 int bw;
304 int dim;
305
306 bool dirty;
307};
308
309void tns_clean_cache(void);
310void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
311CLEANUP void tns_free(tns_t*);
312bool tns_load(tns_t*, int, bool, bool);
313void tns_unload(tns_t*, int);
314void tns_render(tns_t*);
315void tns_mark(tns_t*, int, bool);
316void tns_highlight(tns_t*, int, bool);
317bool tns_move_selection(tns_t*, direction_t, int);
318bool tns_scroll(tns_t*, direction_t, bool);
319bool tns_zoom(tns_t*, int);
320int tns_translate(tns_t*, int, int);
321
322
323/* util.c */
324
325#include <dirent.h>
326
327typedef struct {
328 DIR *dir;
329 char *name;
330 int d;
331 bool recursive;
332
333 char **stack;
334 int stcap;
335 int stlen;
336} r_dir_t;
337
338typedef struct {
339 int readfd;
340 int writefd;
341 pid_t pid;
342} spawn_t;
343
344enum {
345 X_READ = (1 << 0),
346 X_WRITE = (1 << 1)
347};
348
349extern const char *progname;
350
351void* emalloc(size_t);
352void* ecalloc(size_t, size_t);
353void* erealloc(void*, size_t);
354char* estrdup(const char*);
355void error(int, int, const char*, ...);
356int r_opendir(r_dir_t*, const char*, bool);
357int r_closedir(r_dir_t*);
358char* r_readdir(r_dir_t*, bool);
359int r_mkdir(char*);
360void construct_argv(char**, unsigned int, ...);
361spawn_t spawn(const char*, char *const [], unsigned int);
362
363
364/* window.c */
365
366#include <X11/Xutil.h>
367#if HAVE_LIBFONTS
368#include <X11/Xft/Xft.h>
369#endif
370
371enum {
372 BAR_L_LEN = 512,
373 BAR_R_LEN = 64
374};
375
376enum {
377 ATOM_WM_DELETE_WINDOW,
378 ATOM__NET_WM_NAME,
379 ATOM__NET_WM_ICON_NAME,
380 ATOM__NET_WM_ICON,
381 ATOM__NET_WM_STATE,
382 ATOM__NET_WM_PID,
383 ATOM__NET_WM_STATE_FULLSCREEN,
384 ATOM_UTF8_STRING,
385 ATOM_WM_NAME,
386 ATOM_WM_ICON_NAME,
387 ATOM_COUNT
388};
389
390typedef struct {
391 Display *dpy;
392 int scr;
393 int scrw, scrh;
394 Visual *vis;
395 Colormap cmap;
396 int depth;
397} win_env_t;
398
399typedef struct {
400 size_t size;
401 char *p;
402 char *buf;
403} win_bar_t;
404
405struct win {
406 Window xwin;
407 win_env_t env;
408
409 XColor win_bg;
410 XColor win_fg;
411 XColor mrk_fg;
412#if HAVE_LIBFONTS
413 XftColor bar_bg;
414 XftColor bar_fg;
415#endif
416
417 int x;
418 int y;
419 unsigned int w;
420 unsigned int h; /* = win height - bar height */
421 unsigned int bw;
422
423 struct {
424 int w;
425 int h;
426 Pixmap pm;
427 } buf;
428
429 struct {
430 unsigned int h;
431 bool top;
432 win_bar_t l;
433 win_bar_t r;
434 } bar;
435};
436
437extern Atom atoms[ATOM_COUNT];
438
439void win_init(win_t*);
440void win_open(win_t*);
441CLEANUP void win_close(win_t*);
442bool win_configure(win_t*, XConfigureEvent*);
443void win_toggle_fullscreen(win_t*);
444void win_toggle_bar(win_t*);
445void win_clear(win_t*);
446void win_draw(win_t*);
447void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
448void win_set_title(win_t*, bool);
449void win_set_cursor(win_t*, cursor_t);
450void win_cursor_pos(win_t*, int*, int*);
451
452#endif /* NSXIV_H */