Krolyxon
Commit 79556e9

Branches

Refs →

nsxivnsxiv.h

442 lines · 7.6 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
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_center(img_t*);
233bool img_pan_edge(img_t*, direction_t);
234void img_rotate(img_t*, degree_t);
235void img_flip(img_t*, flipdir_t);
236void img_toggle_antialias(img_t*);
237bool img_change_gamma(img_t*, int);
238bool img_frame_navigate(img_t*, int);
239bool img_frame_animate(img_t*);
240
241
242/* options.c */
243
244struct opt {
245 /* file list: */
246 char **filenames;
247 bool from_stdin;
248 bool to_stdout;
249 bool using_null;
250 bool recursive;
251 int filecnt;
252 int startnum;
253
254 /* image: */
255 scalemode_t scalemode;
256 float zoom;
257 bool animate;
258 int gamma;
259 int slideshow;
260 int framerate;
261
262 /* window: */
263 bool fullscreen;
264 bool hide_bar;
265 Window embed; /* unsigned long */
266 char *geometry;
267 char *res_name;
268 const char *title_prefix;
269 suffixmode_t title_suffixmode;
270
271 /* misc flags: */
272 bool quiet;
273 bool thumb_mode;
274 bool clean_cache;
275 bool private_mode;
276};
277
278extern const opt_t *options;
279
280void print_usage(void);
281void parse_options(int, char**);
282
283
284/* thumbs.c */
285
286typedef struct {
287 Imlib_Image im;
288 int w;
289 int h;
290 int x;
291 int y;
292} thumb_t;
293
294struct tns {
295 fileinfo_t *files;
296 thumb_t *thumbs;
297 const int *cnt;
298 int *sel;
299 int initnext;
300 int loadnext;
301 int first, end;
302 int r_first, r_end;
303
304 win_t *win;
305 int x;
306 int y;
307 int cols;
308 int rows;
309 int zl;
310 int bw;
311 int dim;
312
313 bool dirty;
314};
315
316void tns_clean_cache(void);
317void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
318CLEANUP void tns_free(tns_t*);
319bool tns_load(tns_t*, int, bool, bool);
320void tns_unload(tns_t*, int);
321void tns_render(tns_t*);
322void tns_mark(tns_t*, int, bool);
323void tns_highlight(tns_t*, int, bool);
324bool tns_move_selection(tns_t*, direction_t, int);
325bool tns_scroll(tns_t*, direction_t, bool);
326bool tns_zoom(tns_t*, int);
327int tns_translate(tns_t*, int, int);
328
329
330/* util.c */
331
332#include <dirent.h>
333
334typedef struct {
335 DIR *dir;
336 char *name;
337 int d;
338 bool recursive;
339
340 char **stack;
341 int stcap;
342 int stlen;
343} r_dir_t;
344
345extern const char *progname;
346
347void* emalloc(size_t);
348void* ecalloc(size_t, size_t);
349void* erealloc(void*, size_t);
350char* estrdup(const char*);
351void error(int, int, const char*, ...);
352int r_opendir(r_dir_t*, const char*, bool);
353int r_closedir(r_dir_t*);
354char* r_readdir(r_dir_t*, bool);
355int r_mkdir(char*);
356
357
358/* window.c */
359
360#include <X11/Xutil.h>
361#if HAVE_LIBFONTS
362#include <X11/Xft/Xft.h>
363#endif
364
365enum {
366 BAR_L_LEN = 512,
367 BAR_R_LEN = 64
368};
369
370enum {
371 ATOM_WM_DELETE_WINDOW,
372 ATOM__NET_WM_NAME,
373 ATOM__NET_WM_ICON_NAME,
374 ATOM__NET_WM_ICON,
375 ATOM__NET_WM_STATE,
376 ATOM__NET_WM_PID,
377 ATOM__NET_WM_STATE_FULLSCREEN,
378 ATOM_COUNT
379};
380
381typedef struct {
382 Display *dpy;
383 int scr;
384 int scrw, scrh;
385 Visual *vis;
386 Colormap cmap;
387 int depth;
388} win_env_t;
389
390typedef struct {
391 size_t size;
392 char *p;
393 char *buf;
394} win_bar_t;
395
396struct win {
397 Window xwin;
398 win_env_t env;
399
400 XColor win_bg;
401 XColor win_fg;
402 XColor mrk_fg;
403#if HAVE_LIBFONTS
404 XftColor bar_bg;
405 XftColor bar_fg;
406#endif
407
408 int x;
409 int y;
410 unsigned int w;
411 unsigned int h; /* = win height - bar height */
412 unsigned int bw;
413
414 struct {
415 int w;
416 int h;
417 Pixmap pm;
418 } buf;
419
420 struct {
421 unsigned int h;
422 win_bar_t l;
423 win_bar_t r;
424 } bar;
425};
426
427extern Atom atoms[ATOM_COUNT];
428
429void win_init(win_t*);
430void win_open(win_t*);
431CLEANUP void win_close(win_t*);
432bool win_configure(win_t*, XConfigureEvent*);
433void win_toggle_fullscreen(win_t*);
434void win_toggle_bar(win_t*);
435void win_clear(win_t*);
436void win_draw(win_t*);
437void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
438void win_set_title(win_t*, const char*);
439void win_set_cursor(win_t*, cursor_t);
440void win_cursor_pos(win_t*, int*, int*);
441
442#endif /* NSXIV_H */