Krolyxon
Commit f255e1c

Branches

Refs →

nsxivnsxiv.h

479 lines · 8.3 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
48#define TV_ADD_MSEC(tv,t) { \
49 (tv)->tv_sec += (t) / 1000; \
50 (tv)->tv_usec += (t) % 1000 * 1000; \
51}
52
53typedef enum {
54 MODE_ALL,
55 MODE_IMAGE,
56 MODE_THUMB
57} appmode_t;
58
59typedef enum {
60 DIR_LEFT = 1,
61 DIR_RIGHT = 2,
62 DIR_UP = 4,
63 DIR_DOWN = 8
64} direction_t;
65
66typedef enum {
67 DEGREE_90 = 1,
68 DEGREE_180 = 2,
69 DEGREE_270 = 3
70} degree_t;
71
72typedef enum {
73 FLIP_HORIZONTAL = 1,
74 FLIP_VERTICAL = 2
75} flipdir_t;
76
77typedef enum {
78 SCALE_DOWN,
79 SCALE_FIT,
80 SCALE_FILL,
81 SCALE_WIDTH,
82 SCALE_HEIGHT,
83 SCALE_ZOOM
84} scalemode_t;
85
86typedef enum {
87 DRAG_RELATIVE,
88 DRAG_ABSOLUTE
89} dragmode_t;
90
91typedef enum {
92 CURSOR_ARROW,
93 CURSOR_DRAG_ABSOLUTE,
94 CURSOR_DRAG_RELATIVE,
95 CURSOR_WATCH,
96 CURSOR_LEFT,
97 CURSOR_RIGHT,
98 CURSOR_NONE,
99
100 CURSOR_COUNT
101} cursor_t;
102
103typedef enum {
104 FF_WARN = 1,
105 FF_MARK = 2,
106 FF_TN_INIT = 4
107} fileflags_t;
108
109typedef struct {
110 const char *name; /* as given by user */
111 const char *path; /* always absolute */
112 fileflags_t flags;
113} fileinfo_t;
114
115/* timeouts in milliseconds: */
116enum {
117 TO_REDRAW_RESIZE = 75,
118 TO_REDRAW_THUMBS = 200,
119 TO_CURSOR_HIDE = 1200,
120 TO_DOUBLE_CLICK = 300
121};
122
123typedef void (*timeout_f)(void);
124
125typedef struct arl arl_t;
126typedef struct img img_t;
127typedef struct opt opt_t;
128typedef struct tns tns_t;
129typedef struct win win_t;
130
131
132/* autoreload.c */
133
134struct arl {
135 int fd;
136 int wd_dir;
137 int wd_file;
138 char *filename;
139};
140
141void arl_init(arl_t*);
142void arl_cleanup(arl_t*);
143void arl_setup(arl_t*, const char* /* result of realpath(3) */);
144bool arl_handle(arl_t*);
145
146
147/* commands.c */
148
149typedef int arg_t;
150typedef bool (*cmd_f)(arg_t);
151
152typedef struct {
153 cmd_f func;
154 appmode_t mode;
155} cmd_t;
156
157typedef struct {
158 unsigned int mask;
159 KeySym ksym_or_button;
160 cmd_t cmd;
161 arg_t arg;
162} keymap_t;
163
164typedef keymap_t button_t;
165
166
167/* image.c */
168
169typedef struct {
170 Imlib_Image im;
171 unsigned int delay;
172} img_frame_t;
173
174typedef struct {
175 img_frame_t *frames;
176 int cap;
177 int cnt;
178 int sel;
179 bool animate;
180 unsigned int framedelay;
181 int length;
182} multi_img_t;
183
184struct img {
185 Imlib_Image im;
186 int w;
187 int h;
188
189 win_t *win;
190 float x;
191 float y;
192
193 Imlib_Color_Modifier cmod;
194 int gamma;
195
196 scalemode_t scalemode;
197 float zoom;
198
199 bool checkpan;
200 bool dirty;
201 bool aa;
202 bool alpha;
203
204 struct {
205 bool on;
206 int delay;
207 } ss;
208
209 multi_img_t multi;
210};
211
212void img_init(img_t*, win_t*);
213bool img_load(img_t*, const fileinfo_t*);
214CLEANUP void img_close(img_t*, bool);
215void img_render(img_t*);
216bool img_fit_win(img_t*, scalemode_t);
217bool img_zoom(img_t*, int);
218bool img_zoom_to(img_t*, float);
219bool img_pos(img_t*, float, float);
220bool img_pan(img_t*, direction_t, int);
221bool img_pan_center(img_t*);
222bool img_pan_edge(img_t*, direction_t);
223void img_rotate(img_t*, degree_t);
224void img_flip(img_t*, flipdir_t);
225void img_toggle_antialias(img_t*);
226bool img_change_gamma(img_t*, int);
227bool img_frame_navigate(img_t*, int);
228bool img_frame_animate(img_t*);
229Imlib_Image img_open(const fileinfo_t*);
230#if HAVE_LIBEXIF
231void exif_auto_orientate(const fileinfo_t*);
232#endif
233
234
235/* options.c */
236
237struct opt {
238 /* file list: */
239 char **filenames;
240 bool from_stdin;
241 bool to_stdout;
242 bool using_null;
243 bool recursive;
244 int filecnt;
245 int startnum;
246
247 /* image: */
248 scalemode_t scalemode;
249 float zoom;
250 bool animate;
251 int gamma;
252 int slideshow;
253 int framerate;
254
255 /* window: */
256 bool fullscreen;
257 bool hide_bar;
258 Window embed; /* unsigned long */
259 char *geometry;
260 char *res_name;
261
262 /* misc flags: */
263 bool quiet;
264 bool thumb_mode;
265 bool clean_cache;
266 bool private_mode;
267};
268
269extern const opt_t *options;
270
271void print_usage(void);
272void parse_options(int, char**);
273
274
275/* thumbs.c */
276
277typedef struct {
278 Imlib_Image im;
279 int w;
280 int h;
281 int x;
282 int y;
283} thumb_t;
284
285struct tns {
286 fileinfo_t *files;
287 thumb_t *thumbs;
288 const int *cnt;
289 int *sel;
290 int initnext;
291 int loadnext;
292 int first, end;
293 int r_first, r_end;
294
295 win_t *win;
296 int x;
297 int y;
298 int cols;
299 int rows;
300 int zl;
301 int bw;
302 int dim;
303
304 bool dirty;
305};
306
307void tns_clean_cache(void);
308void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
309CLEANUP void tns_free(tns_t*);
310bool tns_load(tns_t*, int, bool, bool);
311void tns_unload(tns_t*, int);
312void tns_render(tns_t*);
313void tns_mark(tns_t*, int, bool);
314void tns_highlight(tns_t*, int, bool);
315bool tns_move_selection(tns_t*, direction_t, int);
316bool tns_scroll(tns_t*, direction_t, bool);
317bool tns_zoom(tns_t*, int);
318int tns_translate(tns_t*, int, int);
319
320
321/* util.c */
322
323#include <dirent.h>
324
325typedef struct {
326 DIR *dir;
327 char *name;
328 int d;
329 bool recursive;
330
331 char **stack;
332 int stcap;
333 int stlen;
334} r_dir_t;
335
336typedef struct {
337 int readfd;
338 int writefd;
339 pid_t pid;
340} spawn_t;
341
342enum {
343 X_READ = (1 << 0),
344 X_WRITE = (1 << 1)
345};
346
347extern const char *progname;
348
349void* emalloc(size_t);
350void* ecalloc(size_t, size_t);
351void* erealloc(void*, size_t);
352char* estrdup(const char*);
353void error(int, int, const char*, ...);
354int r_opendir(r_dir_t*, const char*, bool);
355int r_closedir(r_dir_t*);
356char* r_readdir(r_dir_t*, bool);
357int r_mkdir(char*);
358void construct_argv(char**, unsigned int, ...);
359spawn_t spawn(const char*, char *const [], unsigned int);
360
361
362/* window.c */
363
364#include <X11/Xutil.h>
365#if HAVE_LIBFONTS
366#include <X11/Xft/Xft.h>
367#endif
368
369enum {
370 BAR_L_LEN = 512,
371 BAR_R_LEN = 64
372};
373
374enum {
375 ATOM_WM_DELETE_WINDOW,
376 ATOM__NET_WM_NAME,
377 ATOM__NET_WM_ICON_NAME,
378 ATOM__NET_WM_ICON,
379 ATOM__NET_WM_STATE,
380 ATOM__NET_WM_PID,
381 ATOM__NET_WM_STATE_FULLSCREEN,
382 ATOM_UTF8_STRING,
383 ATOM_WM_NAME,
384 ATOM_WM_ICON_NAME,
385 ATOM_COUNT
386};
387
388typedef struct {
389 Display *dpy;
390 int scr;
391 int scrw, scrh;
392 Visual *vis;
393 Colormap cmap;
394 int depth;
395} win_env_t;
396
397typedef struct {
398 size_t size;
399 char *p;
400 char *buf;
401} win_bar_t;
402
403struct win {
404 Window xwin;
405 win_env_t env;
406
407 XColor win_bg;
408 XColor win_fg;
409 XColor mrk_fg;
410#if HAVE_LIBFONTS
411 XftColor bar_bg;
412 XftColor bar_fg;
413#endif
414
415 int x;
416 int y;
417 unsigned int w;
418 unsigned int h; /* = win height - bar height */
419 unsigned int bw;
420
421 struct {
422 int w;
423 int h;
424 Pixmap pm;
425 } buf;
426
427 struct {
428 unsigned int h;
429 bool top;
430 win_bar_t l;
431 win_bar_t r;
432 } bar;
433};
434
435extern Atom atoms[ATOM_COUNT];
436
437void win_init(win_t*);
438void win_open(win_t*);
439CLEANUP void win_close(win_t*);
440bool win_configure(win_t*, XConfigureEvent*);
441void win_toggle_fullscreen(win_t*);
442void win_toggle_bar(win_t*);
443void win_clear(win_t*);
444void win_draw(win_t*);
445void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
446void win_set_title(win_t*, bool);
447void win_set_cursor(win_t*, cursor_t);
448void win_cursor_pos(win_t*, int*, int*);
449
450/* main.c */
451
452/* timeout handler functions: */
453void redraw(void);
454void reset_cursor(void);
455void animate(void);
456void slideshow(void);
457void clear_resize(void);
458
459void remove_file(int, bool);
460void set_timeout(timeout_f, int, bool);
461void reset_timeout(timeout_f);
462size_t get_win_title(unsigned char*, int, bool);
463void close_info(void);
464void open_info(void);
465void load_image(int);
466bool mark_image(int, bool);
467int nav_button(void);
468void handle_key_handler(bool);
469
470extern appmode_t mode;
471extern const XButtonEvent *xbutton_ev;
472extern fileinfo_t *files;
473extern int filecnt, fileidx;
474extern int alternate;
475extern int markcnt;
476extern int markidx;
477extern int prefix;
478
479#endif /* NSXIV_H */