Krolyxon
Commit 1449bfc

Branches

Refs →

nsxivnsxiv.h

461 lines · 7.8 KB · Objective-C

1/* Copyright 2011 Bert Muennich
2 *
3 * This file is a part of nsxiv.
4 *
5 * nsxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * nsxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef NSXIV_H
20#define NSXIV_H
21
22#include <stdarg.h>
23#include <stdbool.h>
24#include <stdio.h>
25#include <sys/time.h>
26#include <sys/types.h>
27#include <Imlib2.h>
28#include <X11/Xlib.h>
29
30/*
31 * Annotation for functions called in cleanup().
32 * These functions are not allowed to call error(!0, ...) or exit().
33 */
34#define CLEANUP
35
36#define MIN(a,b) ((a) < (b) ? (a) : (b))
37#define MAX(a,b) ((a) > (b) ? (a) : (b))
38#define ABS(a) ((a) > 0 ? (a) : -(a))
39
40#define ARRLEN(a) (sizeof(a) / sizeof((a)[0]))
41
42#define STREQ(s1,s2) (strcmp((s1), (s2)) == 0)
43
44#define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \
45 ((t1)->tv_usec - (t2)->tv_usec) / 1000)
46
47#define TV_SET_MSEC(tv,t) { \
48 (tv)->tv_sec = (t) / 1000; \
49 (tv)->tv_usec = (t) % 1000 * 1000; \
50}
51
52#define TV_ADD_MSEC(tv,t) { \
53 (tv)->tv_sec += (t) / 1000; \
54 (tv)->tv_usec += (t) % 1000 * 1000; \
55}
56
57typedef enum {
58 BO_BIG_ENDIAN,
59 BO_LITTLE_ENDIAN
60} byteorder_t;
61
62typedef enum {
63 MODE_IMAGE,
64 MODE_THUMB
65} appmode_t;
66
67typedef enum {
68 DIR_LEFT = 1,
69 DIR_RIGHT = 2,
70 DIR_UP = 4,
71 DIR_DOWN = 8
72} direction_t;
73
74typedef enum {
75 DEGREE_90 = 1,
76 DEGREE_180 = 2,
77 DEGREE_270 = 3
78} degree_t;
79
80typedef enum {
81 FLIP_HORIZONTAL = 1,
82 FLIP_VERTICAL = 2
83} flipdir_t;
84
85typedef enum {
86 SCALE_DOWN,
87 SCALE_FIT,
88 SCALE_FILL,
89 SCALE_WIDTH,
90 SCALE_HEIGHT,
91 SCALE_ZOOM
92} scalemode_t;
93
94typedef enum {
95 DRAG_RELATIVE,
96 DRAG_ABSOLUTE
97} dragmode_t;
98
99typedef enum {
100 CURSOR_ARROW,
101 CURSOR_DRAG,
102 CURSOR_WATCH,
103 CURSOR_LEFT,
104 CURSOR_RIGHT,
105 CURSOR_NONE,
106
107 CURSOR_COUNT
108} cursor_t;
109
110typedef enum {
111 FF_WARN = 1,
112 FF_MARK = 2,
113 FF_TN_INIT = 4
114} fileflags_t;
115
116typedef enum {
117 SUFFIX_EMPTY,
118 SUFFIX_BASENAME,
119 SUFFIX_FULLPATH,
120} suffixmode_t;
121
122typedef struct {
123 const char *name; /* as given by user */
124 const char *path; /* always absolute */
125 fileflags_t flags;
126} fileinfo_t;
127
128/* timeouts in milliseconds: */
129enum {
130 TO_REDRAW_RESIZE = 75,
131 TO_REDRAW_THUMBS = 200,
132 TO_CURSOR_HIDE = 1200,
133 TO_DOUBLE_CLICK = 300
134};
135
136typedef void (*timeout_f)(void);
137
138typedef struct arl arl_t;
139typedef struct img img_t;
140typedef struct opt opt_t;
141typedef struct tns tns_t;
142typedef struct win win_t;
143
144
145/* autoreload.c */
146
147struct arl {
148 int fd;
149 int wd_dir;
150 int wd_file;
151 char *filename;
152};
153
154void arl_init(arl_t*);
155void arl_cleanup(arl_t*);
156void arl_setup(arl_t*, const char* /* result of realpath(3) */);
157bool arl_handle(arl_t*);
158
159
160/* commands.c */
161
162typedef int arg_t;
163typedef bool (*cmd_f)(arg_t);
164
165#define G_CMD(c) g_##c,
166#define I_CMD(c) i_##c,
167#define T_CMD(c) t_##c,
168
169typedef enum {
170#include "commands.lst"
171 CMD_COUNT
172} cmd_id_t;
173
174typedef struct {
175 int mode;
176 cmd_f func;
177} cmd_t;
178
179typedef struct {
180 unsigned int mask;
181 KeySym ksym;
182 cmd_id_t cmd;
183 arg_t arg;
184} keymap_t;
185
186typedef struct {
187 unsigned int mask;
188 unsigned int button;
189 cmd_id_t cmd;
190 arg_t arg;
191} button_t;
192
193extern const cmd_t cmds[CMD_COUNT];
194
195
196/* image.c */
197
198typedef struct {
199 Imlib_Image im;
200 unsigned int delay;
201} img_frame_t;
202
203typedef struct {
204 img_frame_t *frames;
205 int cap;
206 int cnt;
207 int sel;
208 bool animate;
209 int framedelay;
210 int length;
211} multi_img_t;
212
213struct img {
214 Imlib_Image im;
215 int w;
216 int h;
217
218 win_t *win;
219 float x;
220 float y;
221
222 scalemode_t scalemode;
223 float zoom;
224
225 bool checkpan;
226 bool dirty;
227 bool aa;
228 bool alpha;
229
230 Imlib_Color_Modifier cmod;
231 int gamma;
232
233 struct {
234 bool on;
235 int delay;
236 } ss;
237
238 multi_img_t multi;
239};
240
241void img_init(img_t*, win_t*);
242bool img_load(img_t*, const fileinfo_t*);
243CLEANUP void img_close(img_t*, bool);
244void img_render(img_t*);
245bool img_fit_win(img_t*, scalemode_t);
246bool img_zoom(img_t*, int);
247bool img_zoom_to(img_t*, float);
248bool img_pos(img_t*, float, float);
249bool img_move(img_t*, float, float);
250bool img_pan(img_t*, direction_t, int);
251bool img_pan_edge(img_t*, direction_t);
252void img_rotate(img_t*, degree_t);
253void img_flip(img_t*, flipdir_t);
254void img_toggle_antialias(img_t*);
255bool img_change_gamma(img_t*, int);
256bool img_frame_navigate(img_t*, int);
257bool img_frame_animate(img_t*);
258
259
260/* options.c */
261
262struct opt {
263 /* file list: */
264 char **filenames;
265 bool from_stdin;
266 bool to_stdout;
267 char stdout_separator;
268 bool recursive;
269 int filecnt;
270 int startnum;
271
272 /* image: */
273 scalemode_t scalemode;
274 float zoom;
275 bool animate;
276 int gamma;
277 int slideshow;
278 int framerate;
279
280 /* window: */
281 bool fullscreen;
282 bool hide_bar;
283 long embed;
284 char *geometry;
285 char *res_name;
286 const char *title_prefix;
287 suffixmode_t title_suffixmode;
288
289 /* misc flags: */
290 bool quiet;
291 bool thumb_mode;
292 bool clean_cache;
293 bool private_mode;
294};
295
296extern const opt_t *options;
297
298void print_usage(void);
299void print_version(void);
300void parse_options(int, char**);
301
302
303/* thumbs.c */
304
305typedef struct {
306 Imlib_Image im;
307 int w;
308 int h;
309 int x;
310 int y;
311} thumb_t;
312
313struct tns {
314 fileinfo_t *files;
315 thumb_t *thumbs;
316 const int *cnt;
317 int *sel;
318 int initnext;
319 int loadnext;
320 int first, end;
321 int r_first, r_end;
322
323 win_t *win;
324 int x;
325 int y;
326 int cols;
327 int rows;
328 int zl;
329 int bw;
330 int dim;
331
332 bool dirty;
333};
334
335void tns_clean_cache(tns_t*);
336void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
337CLEANUP void tns_free(tns_t*);
338bool tns_load(tns_t*, int, bool, bool);
339void tns_unload(tns_t*, int);
340void tns_render(tns_t*);
341void tns_mark(tns_t*, int, bool);
342void tns_highlight(tns_t*, int, bool);
343bool tns_move_selection(tns_t*, direction_t, int);
344bool tns_scroll(tns_t*, direction_t, bool);
345bool tns_zoom(tns_t*, int);
346int tns_translate(tns_t*, int, int);
347
348
349/* util.c */
350
351#include <dirent.h>
352
353typedef struct {
354 DIR *dir;
355 char *name;
356 int d;
357 bool recursive;
358
359 char **stack;
360 int stcap;
361 int stlen;
362} r_dir_t;
363
364extern const char *progname;
365
366void* emalloc(size_t);
367void* erealloc(void*, size_t);
368char* estrdup(const char*);
369void error(int, int, const char*, ...);
370void size_readable(float*, const char**);
371int r_opendir(r_dir_t*, const char*, bool);
372int r_closedir(r_dir_t*);
373char* r_readdir(r_dir_t*, bool);
374int r_mkdir(char*);
375
376
377/* window.c */
378
379#include <X11/Xutil.h>
380#if HAVE_LIBFONTS
381#include <X11/Xft/Xft.h>
382#endif
383
384enum {
385 BAR_L_LEN = 512,
386 BAR_R_LEN = 64
387};
388
389enum {
390 ATOM_WM_DELETE_WINDOW,
391 ATOM__NET_WM_NAME,
392 ATOM__NET_WM_ICON_NAME,
393 ATOM__NET_WM_ICON,
394 ATOM__NET_WM_STATE,
395 ATOM__NET_WM_PID,
396 ATOM__NET_WM_STATE_FULLSCREEN,
397 ATOM_COUNT
398};
399
400typedef struct {
401 Display *dpy;
402 int scr;
403 int scrw, scrh;
404 Visual *vis;
405 Colormap cmap;
406 int depth;
407} win_env_t;
408
409typedef struct {
410 size_t size;
411 char *p;
412 char *buf;
413} win_bar_t;
414
415struct win {
416 Window xwin;
417 win_env_t env;
418
419 unsigned long win_bg;
420 unsigned long win_fg;
421 unsigned long mrk_fg;
422#if HAVE_LIBFONTS
423 XftColor bar_bg;
424 XftColor bar_fg;
425#endif
426
427 int x;
428 int y;
429 unsigned int w;
430 unsigned int h; /* = win height - bar height */
431 unsigned int bw;
432
433 struct {
434 int w;
435 int h;
436 Pixmap pm;
437 } buf;
438
439 struct {
440 unsigned int h;
441 win_bar_t l;
442 win_bar_t r;
443 } bar;
444};
445
446extern Atom atoms[ATOM_COUNT];
447
448void win_init(win_t*);
449void win_open(win_t*);
450CLEANUP void win_close(win_t*);
451bool win_configure(win_t*, XConfigureEvent*);
452void win_toggle_fullscreen(win_t*);
453void win_toggle_bar(win_t*);
454void win_clear(win_t*);
455void win_draw(win_t*);
456void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
457void win_set_title(win_t*, const char*);
458void win_set_cursor(win_t*, cursor_t);
459void win_cursor_pos(win_t*, int*, int*);
460
461#endif /* NSXIV_H */