Krolyxon
Commit ff8a846

Branches

Refs →

nsxivnsxiv.h

462 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#ifndef MIN
37#define MIN(a,b) ((a) < (b) ? (a) : (b))
38#endif
39#ifndef MAX
40#define MAX(a,b) ((a) > (b) ? (a) : (b))
41#endif
42
43#define ARRLEN(a) (sizeof(a) / sizeof((a)[0]))
44
45#define STREQ(s1,s2) (strcmp((s1), (s2)) == 0)
46
47#define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \
48 ((t1)->tv_usec - (t2)->tv_usec) / 1000)
49
50#define TV_SET_MSEC(tv,t) { \
51 (tv)->tv_sec = (t) / 1000; \
52 (tv)->tv_usec = (t) % 1000 * 1000; \
53}
54
55#define TV_ADD_MSEC(tv,t) { \
56 (tv)->tv_sec += (t) / 1000; \
57 (tv)->tv_usec += (t) % 1000 * 1000; \
58}
59
60typedef enum {
61 BO_BIG_ENDIAN,
62 BO_LITTLE_ENDIAN
63} byteorder_t;
64
65typedef enum {
66 MODE_IMAGE,
67 MODE_THUMB
68} appmode_t;
69
70typedef enum {
71 DIR_LEFT = 1,
72 DIR_RIGHT = 2,
73 DIR_UP = 4,
74 DIR_DOWN = 8
75} direction_t;
76
77typedef enum {
78 DEGREE_90 = 1,
79 DEGREE_180 = 2,
80 DEGREE_270 = 3
81} degree_t;
82
83typedef enum {
84 FLIP_HORIZONTAL = 1,
85 FLIP_VERTICAL = 2
86} flipdir_t;
87
88typedef enum {
89 SCALE_DOWN,
90 SCALE_FIT,
91 SCALE_FILL,
92 SCALE_WIDTH,
93 SCALE_HEIGHT,
94 SCALE_ZOOM
95} scalemode_t;
96
97typedef enum {
98 DRAG_RELATIVE,
99 DRAG_ABSOLUTE
100} dragmode_t;
101
102typedef enum {
103 CURSOR_ARROW,
104 CURSOR_DRAG,
105 CURSOR_WATCH,
106 CURSOR_LEFT,
107 CURSOR_RIGHT,
108 CURSOR_NONE,
109
110 CURSOR_COUNT
111} cursor_t;
112
113typedef enum {
114 FF_WARN = 1,
115 FF_MARK = 2,
116 FF_TN_INIT = 4
117} fileflags_t;
118
119typedef enum {
120 SUFFIX_EMPTY,
121 SUFFIX_BASENAME,
122 SUFFIX_FULLPATH,
123} suffixmode_t;
124
125typedef struct {
126 const char *name; /* as given by user */
127 const char *path; /* always absolute */
128 fileflags_t flags;
129} fileinfo_t;
130
131/* timeouts in milliseconds: */
132enum {
133 TO_REDRAW_RESIZE = 75,
134 TO_REDRAW_THUMBS = 200,
135 TO_CURSOR_HIDE = 1200,
136 TO_DOUBLE_CLICK = 300
137};
138
139typedef void (*timeout_f)(void);
140
141typedef struct arl arl_t;
142typedef struct img img_t;
143typedef struct opt opt_t;
144typedef struct tns tns_t;
145typedef struct win win_t;
146
147
148/* autoreload.c */
149
150struct arl {
151 int fd;
152 int wd_dir;
153 int wd_file;
154 char *filename;
155};
156
157void arl_init(arl_t*);
158void arl_cleanup(arl_t*);
159void arl_setup(arl_t*, const char* /* result of realpath(3) */);
160bool arl_handle(arl_t*);
161
162
163/* commands.c */
164
165typedef int arg_t;
166typedef bool (*cmd_f)(arg_t);
167
168#define G_CMD(c) g_##c,
169#define I_CMD(c) i_##c,
170#define T_CMD(c) t_##c,
171
172typedef enum {
173#include "commands.lst"
174 CMD_COUNT
175} cmd_id_t;
176
177typedef struct {
178 int mode;
179 cmd_f func;
180} cmd_t;
181
182typedef struct {
183 unsigned int mask;
184 KeySym ksym;
185 cmd_id_t cmd;
186 arg_t arg;
187} keymap_t;
188
189typedef struct {
190 unsigned int mask;
191 unsigned int button;
192 cmd_id_t cmd;
193 arg_t arg;
194} button_t;
195
196extern const cmd_t cmds[CMD_COUNT];
197
198
199/* image.c */
200
201typedef struct {
202 Imlib_Image im;
203 unsigned int delay;
204} img_frame_t;
205
206typedef struct {
207 img_frame_t *frames;
208 int cap;
209 int cnt;
210 int sel;
211 bool animate;
212 int framedelay;
213 int length;
214} multi_img_t;
215
216struct img {
217 Imlib_Image im;
218 int w;
219 int h;
220
221 win_t *win;
222 float x;
223 float y;
224
225 scalemode_t scalemode;
226 float zoom;
227
228 bool checkpan;
229 bool dirty;
230 bool aa;
231 bool alpha;
232
233 Imlib_Color_Modifier cmod;
234 int gamma;
235
236 struct {
237 bool on;
238 int delay;
239 } ss;
240
241 multi_img_t multi;
242};
243
244void img_init(img_t*, win_t*);
245bool img_load(img_t*, const fileinfo_t*);
246CLEANUP void img_close(img_t*, bool);
247void img_render(img_t*);
248bool img_fit_win(img_t*, scalemode_t);
249bool img_zoom(img_t*, float);
250bool img_zoom_in(img_t*);
251bool img_zoom_out(img_t*);
252bool img_pos(img_t*, float, float);
253bool img_move(img_t*, float, float);
254bool img_pan(img_t*, direction_t, int);
255bool img_pan_edge(img_t*, direction_t);
256void img_rotate(img_t*, degree_t);
257void img_flip(img_t*, flipdir_t);
258void img_toggle_antialias(img_t*);
259bool img_change_gamma(img_t*, int);
260bool img_frame_navigate(img_t*, int);
261bool img_frame_animate(img_t*);
262
263
264/* options.c */
265
266struct opt {
267 /* file list: */
268 char **filenames;
269 bool from_stdin;
270 bool to_stdout;
271 char stdout_separator;
272 bool recursive;
273 int filecnt;
274 int startnum;
275
276 /* image: */
277 scalemode_t scalemode;
278 float zoom;
279 bool animate;
280 int gamma;
281 int slideshow;
282 int framerate;
283
284 /* window: */
285 bool fullscreen;
286 bool hide_bar;
287 long embed;
288 char *geometry;
289 char *res_name;
290 const char *title_prefix;
291 suffixmode_t title_suffixmode;
292
293 /* misc flags: */
294 bool quiet;
295 bool thumb_mode;
296 bool clean_cache;
297 bool private_mode;
298};
299
300extern const opt_t *options;
301
302void print_usage(void);
303void print_version(void);
304void parse_options(int, char**);
305
306
307/* thumbs.c */
308
309typedef struct {
310 Imlib_Image im;
311 int w;
312 int h;
313 int x;
314 int y;
315} thumb_t;
316
317struct tns {
318 fileinfo_t *files;
319 thumb_t *thumbs;
320 const int *cnt;
321 int *sel;
322 int initnext;
323 int loadnext;
324 int first, end;
325 int r_first, r_end;
326
327 win_t *win;
328 int x;
329 int y;
330 int cols;
331 int rows;
332 int zl;
333 int bw;
334 int dim;
335
336 bool dirty;
337};
338
339void tns_clean_cache(tns_t*);
340void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
341CLEANUP void tns_free(tns_t*);
342bool tns_load(tns_t*, int, bool, bool);
343void tns_unload(tns_t*, int);
344void tns_render(tns_t*);
345void tns_mark(tns_t*, int, bool);
346void tns_highlight(tns_t*, int, bool);
347bool tns_move_selection(tns_t*, direction_t, int);
348bool tns_scroll(tns_t*, direction_t, bool);
349bool tns_zoom(tns_t*, int);
350int tns_translate(tns_t*, int, int);
351
352
353/* util.c */
354
355#include <dirent.h>
356
357typedef struct {
358 DIR *dir;
359 char *name;
360 int d;
361 bool recursive;
362
363 char **stack;
364 int stcap;
365 int stlen;
366} r_dir_t;
367
368extern const char *progname;
369
370void* emalloc(size_t);
371void* erealloc(void*, size_t);
372char* estrdup(const char*);
373void error(int, int, const char*, ...);
374void size_readable(float*, const char**);
375int r_opendir(r_dir_t*, const char*, bool);
376int r_closedir(r_dir_t*);
377char* r_readdir(r_dir_t*, bool);
378int r_mkdir(char*);
379
380
381/* window.c */
382
383#include <X11/Xutil.h>
384#include <X11/Xft/Xft.h>
385
386enum {
387 BAR_L_LEN = 512,
388 BAR_R_LEN = 64
389};
390
391enum {
392 ATOM_WM_DELETE_WINDOW,
393 ATOM__NET_WM_NAME,
394 ATOM__NET_WM_ICON_NAME,
395 ATOM__NET_WM_ICON,
396 ATOM__NET_WM_STATE,
397 ATOM__NET_WM_PID,
398 ATOM__NET_WM_STATE_FULLSCREEN,
399 ATOM_COUNT
400};
401
402typedef struct {
403 Display *dpy;
404 int scr;
405 int scrw, scrh;
406 Visual *vis;
407 Colormap cmap;
408 int depth;
409} win_env_t;
410
411typedef struct {
412 size_t size;
413 char *p;
414 char *buf;
415} win_bar_t;
416
417struct win {
418 Window xwin;
419 win_env_t env;
420
421 XftColor win_bg;
422 XftColor win_fg;
423 XftColor bar_bg;
424 XftColor bar_fg;
425 XftColor mrk_fg;
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 */
462