Krolyxon
Commit b11384a

Branches

Refs →

nsxivnsxiv.h

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