Krolyxon
Commit 156a537

Branches

Refs →

nsxivsxiv.h

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