Krolyxon
Commit 157cda5

Branches

Refs →

nsxivnsxiv.h

440 lines · 7.5 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 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 enum {
115 SUFFIX_EMPTY,
116 SUFFIX_BASENAME,
117 SUFFIX_FULLPATH
118} suffixmode_t;
119
120typedef struct {
121 const char *name; /* as given by user */
122 const char *path; /* always absolute */
123 fileflags_t flags;
124} fileinfo_t;
125
126/* timeouts in milliseconds: */
127enum {
128 TO_REDRAW_RESIZE = 75,
129 TO_REDRAW_THUMBS = 200,
130 TO_CURSOR_HIDE = 1200,
131 TO_DOUBLE_CLICK = 300
132};
133
134typedef void (*timeout_f)(void);
135
136typedef struct arl arl_t;
137typedef struct img img_t;
138typedef struct opt opt_t;
139typedef struct tns tns_t;
140typedef struct win win_t;
141
142
143/* autoreload.c */
144
145struct arl {
146 int fd;
147 int wd_dir;
148 int wd_file;
149 char *filename;
150};
151
152void arl_init(arl_t*);
153void arl_cleanup(arl_t*);
154void arl_setup(arl_t*, const char* /* result of realpath(3) */);
155bool arl_handle(arl_t*);
156
157
158/* commands.c */
159
160typedef int arg_t;
161typedef bool (*cmd_f)(arg_t);
162
163typedef struct {
164 cmd_f func;
165 appmode_t mode;
166} cmd_t;
167
168typedef struct {
169 unsigned int mask;
170 KeySym ksym_or_button;
171 cmd_t cmd;
172 arg_t arg;
173} keymap_t;
174
175typedef keymap_t button_t;
176
177
178/* image.c */
179
180typedef struct {
181 Imlib_Image im;
182 unsigned int delay;
183} img_frame_t;
184
185typedef struct {
186 img_frame_t *frames;
187 int cap;
188 int cnt;
189 int sel;
190 bool animate;
191 unsigned int framedelay;
192 int length;
193} multi_img_t;
194
195struct img {
196 Imlib_Image im;
197 int w;
198 int h;
199
200 win_t *win;
201 float x;
202 float y;
203
204 Imlib_Color_Modifier cmod;
205 int gamma;
206
207 scalemode_t scalemode;
208 float zoom;
209
210 bool checkpan;
211 bool dirty;
212 bool aa;
213 bool alpha;
214
215 struct {
216 bool on;
217 int delay;
218 } ss;
219
220 multi_img_t multi;
221};
222
223void img_init(img_t*, win_t*);
224bool img_load(img_t*, const fileinfo_t*);
225CLEANUP void img_close(img_t*, bool);
226void img_render(img_t*);
227bool img_fit_win(img_t*, scalemode_t);
228bool img_zoom(img_t*, int);
229bool img_zoom_to(img_t*, float);
230bool img_pos(img_t*, float, float);
231bool img_pan(img_t*, direction_t, int);
232bool img_pan_edge(img_t*, direction_t);
233void img_rotate(img_t*, degree_t);
234void img_flip(img_t*, flipdir_t);
235void img_toggle_antialias(img_t*);
236bool img_change_gamma(img_t*, int);
237bool img_frame_navigate(img_t*, int);
238bool img_frame_animate(img_t*);
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 const char *title_prefix;
268 suffixmode_t title_suffixmode;
269
270 /* misc flags: */
271 bool quiet;
272 bool thumb_mode;
273 bool clean_cache;
274 bool private_mode;
275};
276
277extern const opt_t *options;
278
279void print_usage(void);
280void parse_options(int, char**);
281
282
283/* thumbs.c */
284
285typedef struct {
286 Imlib_Image im;
287 int w;
288 int h;
289 int x;
290 int y;
291} thumb_t;
292
293struct tns {
294 fileinfo_t *files;
295 thumb_t *thumbs;
296 const int *cnt;
297 int *sel;
298 int initnext;
299 int loadnext;
300 int first, end;
301 int r_first, r_end;
302
303 win_t *win;
304 int x;
305 int y;
306 int cols;
307 int rows;
308 int zl;
309 int bw;
310 int dim;
311
312 bool dirty;
313};
314
315void tns_clean_cache(void);
316void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
317CLEANUP void tns_free(tns_t*);
318bool tns_load(tns_t*, int, bool, bool);
319void tns_unload(tns_t*, int);
320void tns_render(tns_t*);
321void tns_mark(tns_t*, int, bool);
322void tns_highlight(tns_t*, int, bool);
323bool tns_move_selection(tns_t*, direction_t, int);
324bool tns_scroll(tns_t*, direction_t, bool);
325bool tns_zoom(tns_t*, int);
326int tns_translate(tns_t*, int, int);
327
328
329/* util.c */
330
331#include <dirent.h>
332
333typedef struct {
334 DIR *dir;
335 char *name;
336 int d;
337 bool recursive;
338
339 char **stack;
340 int stcap;
341 int stlen;
342} r_dir_t;
343
344extern const char *progname;
345
346void* emalloc(size_t);
347void* erealloc(void*, size_t);
348char* estrdup(const char*);
349void error(int, int, const char*, ...);
350int r_opendir(r_dir_t*, const char*, bool);
351int r_closedir(r_dir_t*);
352char* r_readdir(r_dir_t*, bool);
353int r_mkdir(char*);
354
355
356/* window.c */
357
358#include <X11/Xutil.h>
359#if HAVE_LIBFONTS
360#include <X11/Xft/Xft.h>
361#endif
362
363enum {
364 BAR_L_LEN = 512,
365 BAR_R_LEN = 64
366};
367
368enum {
369 ATOM_WM_DELETE_WINDOW,
370 ATOM__NET_WM_NAME,
371 ATOM__NET_WM_ICON_NAME,
372 ATOM__NET_WM_ICON,
373 ATOM__NET_WM_STATE,
374 ATOM__NET_WM_PID,
375 ATOM__NET_WM_STATE_FULLSCREEN,
376 ATOM_COUNT
377};
378
379typedef struct {
380 Display *dpy;
381 int scr;
382 int scrw, scrh;
383 Visual *vis;
384 Colormap cmap;
385 int depth;
386} win_env_t;
387
388typedef struct {
389 size_t size;
390 char *p;
391 char *buf;
392} win_bar_t;
393
394struct win {
395 Window xwin;
396 win_env_t env;
397
398 XColor win_bg;
399 XColor win_fg;
400 XColor mrk_fg;
401#if HAVE_LIBFONTS
402 XftColor bar_bg;
403 XftColor bar_fg;
404#endif
405
406 int x;
407 int y;
408 unsigned int w;
409 unsigned int h; /* = win height - bar height */
410 unsigned int bw;
411
412 struct {
413 int w;
414 int h;
415 Pixmap pm;
416 } buf;
417
418 struct {
419 unsigned int h;
420 win_bar_t l;
421 win_bar_t r;
422 } bar;
423};
424
425extern Atom atoms[ATOM_COUNT];
426
427void win_init(win_t*);
428void win_open(win_t*);
429CLEANUP void win_close(win_t*);
430bool win_configure(win_t*, XConfigureEvent*);
431void win_toggle_fullscreen(win_t*);
432void win_toggle_bar(win_t*);
433void win_clear(win_t*);
434void win_draw(win_t*);
435void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
436void win_set_title(win_t*, const char*);
437void win_set_cursor(win_t*, cursor_t);
438void win_cursor_pos(win_t*, int*, int*);
439
440#endif /* NSXIV_H */