Krolyxon
Commit a2339e7

Branches

Refs →

nsxivnsxiv.h

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