Krolyxon
Commit 3a22e6a

Branches

Refs →

nsxivnsxiv.h

485 lines · 8.5 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*);
235Imlib_Image img_open(const fileinfo_t*);
236#if HAVE_LIBEXIF
237void exif_auto_orientate(const fileinfo_t*);
238#endif
239
240
241/* options.c */
242
243struct opt {
244 /* file list: */
245 char **filenames;
246 bool from_stdin;
247 bool to_stdout;
248 bool using_null;
249 bool recursive;
250 int filecnt;
251 int startnum;
252
253 /* image: */
254 scalemode_t scalemode;
255 float zoom;
256 bool animate;
257 int gamma;
258 int slideshow;
259 int framerate;
260
261 /* window: */
262 bool fullscreen;
263 bool hide_bar;
264 Window embed; /* unsigned long */
265 char *geometry;
266 char *res_name;
267
268 /* misc flags: */
269 bool quiet;
270 bool thumb_mode;
271 bool clean_cache;
272 bool private_mode;
273};
274
275extern const opt_t *options;
276
277void print_usage(void);
278void parse_options(int, char**);
279
280
281/* thumbs.c */
282
283typedef struct {
284 Imlib_Image im;
285 int w;
286 int h;
287 int x;
288 int y;
289} thumb_t;
290
291struct tns {
292 fileinfo_t *files;
293 thumb_t *thumbs;
294 const int *cnt;
295 int *sel;
296 int initnext;
297 int loadnext;
298 int first, end;
299 int r_first, r_end;
300
301 win_t *win;
302 int x;
303 int y;
304 int cols;
305 int rows;
306 int zl;
307 int bw;
308 int dim;
309
310 bool dirty;
311};
312
313void tns_clean_cache(void);
314void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
315CLEANUP void tns_free(tns_t*);
316bool tns_load(tns_t*, int, bool, bool);
317void tns_unload(tns_t*, int);
318void tns_render(tns_t*);
319void tns_mark(tns_t*, int, bool);
320void tns_highlight(tns_t*, int, bool);
321bool tns_move_selection(tns_t*, direction_t, int);
322bool tns_scroll(tns_t*, direction_t, bool);
323bool tns_zoom(tns_t*, int);
324int tns_translate(tns_t*, int, int);
325
326
327/* util.c */
328
329#include <dirent.h>
330
331typedef struct {
332 DIR *dir;
333 char *name;
334 int d;
335 bool recursive;
336
337 char **stack;
338 int stcap;
339 int stlen;
340} r_dir_t;
341
342typedef struct {
343 int readfd;
344 int writefd;
345 pid_t pid;
346} spawn_t;
347
348enum {
349 X_READ = (1 << 0),
350 X_WRITE = (1 << 1)
351};
352
353extern const char *progname;
354
355void* emalloc(size_t);
356void* ecalloc(size_t, size_t);
357void* erealloc(void*, size_t);
358char* estrdup(const char*);
359void error(int, int, const char*, ...);
360int r_opendir(r_dir_t*, const char*, bool);
361int r_closedir(r_dir_t*);
362char* r_readdir(r_dir_t*, bool);
363int r_mkdir(char*);
364void construct_argv(char**, unsigned int, ...);
365spawn_t spawn(const char*, char *const [], unsigned int);
366
367
368/* window.c */
369
370#include <X11/Xutil.h>
371#if HAVE_LIBFONTS
372#include <X11/Xft/Xft.h>
373#endif
374
375enum {
376 BAR_L_LEN = 512,
377 BAR_R_LEN = 64
378};
379
380enum {
381 ATOM_WM_DELETE_WINDOW,
382 ATOM__NET_WM_NAME,
383 ATOM__NET_WM_ICON_NAME,
384 ATOM__NET_WM_ICON,
385 ATOM__NET_WM_STATE,
386 ATOM__NET_WM_PID,
387 ATOM__NET_WM_STATE_FULLSCREEN,
388 ATOM_UTF8_STRING,
389 ATOM_WM_NAME,
390 ATOM_WM_ICON_NAME,
391 ATOM_COUNT
392};
393
394typedef struct {
395 Display *dpy;
396 int scr;
397 int scrw, scrh;
398 Visual *vis;
399 Colormap cmap;
400 int depth;
401} win_env_t;
402
403typedef struct {
404 size_t size;
405 char *p;
406 char *buf;
407} win_bar_t;
408
409struct win {
410 Window xwin;
411 win_env_t env;
412
413 XColor win_bg;
414 XColor win_fg;
415 XColor mrk_fg;
416#if HAVE_LIBFONTS
417 XftColor bar_bg;
418 XftColor bar_fg;
419#endif
420
421 int x;
422 int y;
423 unsigned int w;
424 unsigned int h; /* = win height - bar height */
425 unsigned int bw;
426
427 struct {
428 int w;
429 int h;
430 Pixmap pm;
431 } buf;
432
433 struct {
434 unsigned int h;
435 bool top;
436 win_bar_t l;
437 win_bar_t r;
438 } bar;
439};
440
441extern Atom atoms[ATOM_COUNT];
442
443void win_init(win_t*);
444void win_open(win_t*);
445CLEANUP void win_close(win_t*);
446bool win_configure(win_t*, XConfigureEvent*);
447void win_toggle_fullscreen(win_t*);
448void win_toggle_bar(win_t*);
449void win_clear(win_t*);
450void win_draw(win_t*);
451void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
452void win_set_title(win_t*, bool);
453void win_set_cursor(win_t*, cursor_t);
454void win_cursor_pos(win_t*, int*, int*);
455
456/* main.c */
457
458/* timeout handler functions: */
459void redraw(void);
460void reset_cursor(void);
461void animate(void);
462void slideshow(void);
463void clear_resize(void);
464
465void remove_file(int, bool);
466void set_timeout(timeout_f, int, bool);
467void reset_timeout(timeout_f);
468size_t get_win_title(unsigned char*, int, bool);
469void close_info(void);
470void open_info(void);
471void load_image(int);
472bool mark_image(int, bool);
473int nav_button(void);
474void handle_key_handler(bool);
475
476extern appmode_t mode;
477extern const XButtonEvent *xbutton_ev;
478extern fileinfo_t *files;
479extern int filecnt, fileidx;
480extern int alternate;
481extern int markcnt;
482extern int markidx;
483extern int prefix;
484
485#endif /* NSXIV_H */