Krolyxon
Commit df71437

Branches

Refs →

nsxivsxiv.h

451 lines · 7.6 KB · Objective-C

1/* Copyright 2011 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv 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 * sxiv 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 sxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef SXIV_H
20#define SXIV_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_WIDTH,
92 SCALE_HEIGHT,
93 SCALE_ZOOM
94} scalemode_t;
95
96typedef enum {
97 CURSOR_ARROW,
98 CURSOR_DRAG,
99 CURSOR_WATCH,
100 CURSOR_LEFT,
101 CURSOR_RIGHT,
102 CURSOR_NONE,
103
104 CURSOR_COUNT
105} cursor_t;
106
107typedef enum {
108 FF_WARN = 1,
109 FF_MARK = 2,
110 FF_TN_INIT = 4
111} fileflags_t;
112
113typedef struct {
114 const char *name; /* as given by user */
115 const char *path; /* always absolute */
116 const char *base;
117 fileflags_t flags;
118} fileinfo_t;
119
120/* timeouts in milliseconds: */
121enum {
122 TO_REDRAW_RESIZE = 75,
123 TO_REDRAW_THUMBS = 200,
124 TO_CURSOR_HIDE = 1200,
125 TO_DOUBLE_CLICK = 300
126};
127
128typedef void (*timeout_f)(void);
129
130typedef struct arl arl_t;
131typedef struct img img_t;
132typedef struct opt opt_t;
133typedef struct tns tns_t;
134typedef struct win win_t;
135
136
137/* autoreload.c */
138
139struct arl {
140 int fd;
141 int wd_dir;
142 int wd_file;
143 char *filename;
144};
145
146void arl_init(arl_t*);
147void arl_cleanup(arl_t*);
148void arl_setup(arl_t*, const char* /* result of realpath(3) */);
149bool arl_handle(arl_t*);
150
151
152/* commands.c */
153
154typedef int arg_t;
155typedef bool (*cmd_f)(arg_t);
156
157#define G_CMD(c) g_##c,
158#define I_CMD(c) i_##c,
159#define T_CMD(c) t_##c,
160
161typedef enum {
162#include "commands.lst"
163 CMD_COUNT
164} cmd_id_t;
165
166typedef struct {
167 int mode;
168 cmd_f func;
169} cmd_t;
170
171typedef struct {
172 unsigned int mask;
173 KeySym ksym;
174 cmd_id_t cmd;
175 arg_t arg;
176} keymap_t;
177
178typedef struct {
179 unsigned int mask;
180 unsigned int button;
181 cmd_id_t cmd;
182 arg_t arg;
183} button_t;
184
185extern const cmd_t cmds[CMD_COUNT];
186
187
188/* image.c */
189
190typedef struct {
191 Imlib_Image im;
192 unsigned int delay;
193} img_frame_t;
194
195typedef struct {
196 img_frame_t *frames;
197 int cap;
198 int cnt;
199 int sel;
200 bool animate;
201 int framedelay;
202 int length;
203} multi_img_t;
204
205struct img {
206 Imlib_Image im;
207 int w;
208 int h;
209
210 win_t *win;
211 float x;
212 float y;
213
214 scalemode_t scalemode;
215 float zoom;
216
217 bool checkpan;
218 bool dirty;
219 bool aa;
220 bool alpha;
221
222 Imlib_Color_Modifier cmod;
223 int gamma;
224
225 struct {
226 bool on;
227 int delay;
228 } ss;
229
230 multi_img_t multi;
231};
232
233void img_init(img_t*, win_t*);
234bool img_load(img_t*, const fileinfo_t*);
235CLEANUP void img_close(img_t*, bool);
236void img_render(img_t*);
237bool img_fit_win(img_t*, scalemode_t);
238bool img_zoom(img_t*, float);
239bool img_zoom_in(img_t*);
240bool img_zoom_out(img_t*);
241bool img_pos(img_t*, float, float);
242bool img_move(img_t*, float, float);
243bool img_pan(img_t*, direction_t, int);
244bool img_pan_edge(img_t*, direction_t);
245void img_rotate(img_t*, degree_t);
246void img_flip(img_t*, flipdir_t);
247void img_toggle_antialias(img_t*);
248bool img_change_gamma(img_t*, int);
249bool img_frame_navigate(img_t*, int);
250bool img_frame_animate(img_t*);
251
252
253/* options.c */
254
255struct opt {
256 /* file list: */
257 char **filenames;
258 bool from_stdin;
259 bool to_stdout;
260 bool recursive;
261 int filecnt;
262 int startnum;
263
264 /* image: */
265 scalemode_t scalemode;
266 float zoom;
267 bool animate;
268 int gamma;
269 int slideshow;
270 int framerate;
271
272 /* window: */
273 bool fullscreen;
274 bool hide_bar;
275 long embed;
276 char *geometry;
277 char *res_name;
278
279 /* misc flags: */
280 bool quiet;
281 bool thumb_mode;
282 bool clean_cache;
283 bool private_mode;
284};
285
286extern const opt_t *options;
287
288void print_usage(void);
289void print_version(void);
290void parse_options(int, char**);
291
292
293/* thumbs.c */
294
295typedef struct {
296 Imlib_Image im;
297 int w;
298 int h;
299 int x;
300 int y;
301} thumb_t;
302
303struct tns {
304 fileinfo_t *files;
305 thumb_t *thumbs;
306 const int *cnt;
307 int *sel;
308 int initnext;
309 int loadnext;
310 int first, end;
311 int r_first, r_end;
312
313 win_t *win;
314 int x;
315 int y;
316 int cols;
317 int rows;
318 int zl;
319 int bw;
320 int dim;
321
322 bool dirty;
323};
324
325void tns_clean_cache(tns_t*);
326void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
327CLEANUP void tns_free(tns_t*);
328bool tns_load(tns_t*, int, bool, bool);
329void tns_unload(tns_t*, int);
330void tns_render(tns_t*);
331void tns_mark(tns_t*, int, bool);
332void tns_highlight(tns_t*, int, bool);
333bool tns_move_selection(tns_t*, direction_t, int);
334bool tns_scroll(tns_t*, direction_t, bool);
335bool tns_zoom(tns_t*, int);
336int tns_translate(tns_t*, int, int);
337
338
339/* util.c */
340
341#include <dirent.h>
342
343typedef struct {
344 DIR *dir;
345 char *name;
346 int d;
347 bool recursive;
348
349 char **stack;
350 int stcap;
351 int stlen;
352} r_dir_t;
353
354extern const char *progname;
355
356void* emalloc(size_t);
357void* erealloc(void*, size_t);
358char* estrdup(const char*);
359void error(int, int, const char*, ...);
360void size_readable(float*, const char**);
361int r_opendir(r_dir_t*, const char*, bool);
362int r_closedir(r_dir_t*);
363char* r_readdir(r_dir_t*);
364int r_mkdir(char*);
365
366
367/* window.c */
368
369#include <X11/Xutil.h>
370#include <X11/Xft/Xft.h>
371
372enum {
373 BAR_L_LEN = 512,
374 BAR_R_LEN = 64
375};
376
377enum {
378 ATOM_WM_DELETE_WINDOW,
379 ATOM__NET_WM_NAME,
380 ATOM__NET_WM_ICON_NAME,
381 ATOM__NET_WM_ICON,
382 ATOM__NET_WM_STATE,
383 ATOM__NET_WM_STATE_FULLSCREEN,
384 ATOM__NET_SUPPORTED,
385 ATOM_COUNT
386};
387
388typedef struct {
389 Display *dpy;
390 int scr;
391 int scrw, scrh;
392 Visual *vis;
393 Colormap cmap;
394 int depth;
395} win_env_t;
396
397typedef struct {
398 size_t size;
399 char *p;
400 char *buf;
401} win_bar_t;
402
403struct win {
404 Window xwin;
405 win_env_t env;
406
407 XftColor bgcol;
408 XftColor fscol;
409 XftColor selcol;
410
411 int x;
412 int y;
413 unsigned int w;
414 unsigned int h; /* = win height - bar height */
415 unsigned int bw;
416
417 bool fullscreen;
418
419 struct {
420 int w;
421 int h;
422 Pixmap pm;
423 } buf;
424
425 struct {
426 unsigned int h;
427 win_bar_t l;
428 win_bar_t r;
429 XftColor bgcol;
430 XftColor fgcol;
431 } bar;
432};
433
434extern Atom atoms[ATOM_COUNT];
435
436void win_init(win_t*);
437void win_open(win_t*);
438CLEANUP void win_close(win_t*);
439bool win_configure(win_t*, XConfigureEvent*);
440void win_toggle_fullscreen(win_t*);
441void win_toggle_bar(win_t*);
442void win_clear(win_t*);
443void win_draw(win_t*);
444void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
445int win_textwidth(const win_env_t*, const char*, unsigned int, bool);
446void win_set_title(win_t*, const char*);
447void win_set_cursor(win_t*, cursor_t);
448void win_cursor_pos(win_t*, int*, int*);
449
450#endif /* SXIV_H */
451