Krolyxon
Commit 7fb8a61

Branches

Refs →

nsxivnsxiv.h

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