Krolyxon
Commit ca7368a

Branches

Refs →

nsxivnsxiv.h

446 lines · 7.6 KB · Objective-C

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