Krolyxon
Commit d566ca8

Branches

Refs →

nsxivnsxiv.h

461 lines · 8.1 KB · Objective-C

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