| 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 | |
| 60 | typedef enum { |
| 61 | BO_BIG_ENDIAN, |
| 62 | BO_LITTLE_ENDIAN |
| 63 | } byteorder_t; |
| 64 | |
| 65 | typedef enum { |
| 66 | MODE_IMAGE, |
| 67 | MODE_THUMB |
| 68 | } appmode_t; |
| 69 | |
| 70 | typedef enum { |
| 71 | DIR_LEFT = 1, |
| 72 | DIR_RIGHT = 2, |
| 73 | DIR_UP = 4, |
| 74 | DIR_DOWN = 8 |
| 75 | } direction_t; |
| 76 | |
| 77 | typedef enum { |
| 78 | DEGREE_90 = 1, |
| 79 | DEGREE_180 = 2, |
| 80 | DEGREE_270 = 3 |
| 81 | } degree_t; |
| 82 | |
| 83 | typedef enum { |
| 84 | FLIP_HORIZONTAL = 1, |
| 85 | FLIP_VERTICAL = 2 |
| 86 | } flipdir_t; |
| 87 | |
| 88 | typedef enum { |
| 89 | SCALE_DOWN, |
| 90 | SCALE_FIT, |
| 91 | SCALE_WIDTH, |
| 92 | SCALE_HEIGHT, |
| 93 | SCALE_ZOOM |
| 94 | } scalemode_t; |
| 95 | |
| 96 | typedef 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 | |
| 107 | typedef enum { |
| 108 | FF_WARN = 1, |
| 109 | FF_MARK = 2, |
| 110 | FF_TN_INIT = 4 |
| 111 | } fileflags_t; |
| 112 | |
| 113 | typedef struct { |
| 114 | const char *name; /* as given by user */ |
| 115 | const char *path; /* always absolute */ |
| 116 | fileflags_t flags; |
| 117 | } fileinfo_t; |
| 118 | |
| 119 | /* timeouts in milliseconds: */ |
| 120 | enum { |
| 121 | TO_REDRAW_RESIZE = 75, |
| 122 | TO_REDRAW_THUMBS = 200, |
| 123 | TO_CURSOR_HIDE = 1200, |
| 124 | TO_DOUBLE_CLICK = 300 |
| 125 | }; |
| 126 | |
| 127 | typedef void (*timeout_f)(void); |
| 128 | |
| 129 | typedef struct arl arl_t; |
| 130 | typedef struct img img_t; |
| 131 | typedef struct opt opt_t; |
| 132 | typedef struct tns tns_t; |
| 133 | typedef struct win win_t; |
| 134 | |
| 135 | |
| 136 | /* autoreload.c */ |
| 137 | |
| 138 | struct arl { |
| 139 | int fd; |
| 140 | int wd_dir; |
| 141 | int wd_file; |
| 142 | char *filename; |
| 143 | }; |
| 144 | |
| 145 | void arl_init(arl_t*); |
| 146 | void arl_cleanup(arl_t*); |
| 147 | void arl_setup(arl_t*, const char* /* result of realpath(3) */); |
| 148 | bool arl_handle(arl_t*); |
| 149 | |
| 150 | |
| 151 | /* commands.c */ |
| 152 | |
| 153 | typedef int arg_t; |
| 154 | typedef bool (*cmd_f)(arg_t); |
| 155 | |
| 156 | #define G_CMD(c) g_##c, |
| 157 | #define I_CMD(c) i_##c, |
| 158 | #define T_CMD(c) t_##c, |
| 159 | |
| 160 | typedef enum { |
| 161 | #include "commands.lst" |
| 162 | CMD_COUNT |
| 163 | } cmd_id_t; |
| 164 | |
| 165 | typedef struct { |
| 166 | int mode; |
| 167 | cmd_f func; |
| 168 | } cmd_t; |
| 169 | |
| 170 | typedef struct { |
| 171 | unsigned int mask; |
| 172 | KeySym ksym; |
| 173 | cmd_id_t cmd; |
| 174 | arg_t arg; |
| 175 | } keymap_t; |
| 176 | |
| 177 | typedef struct { |
| 178 | unsigned int mask; |
| 179 | unsigned int button; |
| 180 | cmd_id_t cmd; |
| 181 | arg_t arg; |
| 182 | } button_t; |
| 183 | |
| 184 | extern const cmd_t cmds[CMD_COUNT]; |
| 185 | |
| 186 | |
| 187 | /* image.c */ |
| 188 | |
| 189 | typedef struct { |
| 190 | Imlib_Image im; |
| 191 | unsigned int delay; |
| 192 | } img_frame_t; |
| 193 | |
| 194 | typedef struct { |
| 195 | img_frame_t *frames; |
| 196 | int cap; |
| 197 | int cnt; |
| 198 | int sel; |
| 199 | bool animate; |
| 200 | int framedelay; |
| 201 | int length; |
| 202 | } multi_img_t; |
| 203 | |
| 204 | struct img { |
| 205 | Imlib_Image im; |
| 206 | int w; |
| 207 | int h; |
| 208 | |
| 209 | win_t *win; |
| 210 | float x; |
| 211 | float y; |
| 212 | |
| 213 | scalemode_t scalemode; |
| 214 | float zoom; |
| 215 | |
| 216 | bool checkpan; |
| 217 | bool dirty; |
| 218 | bool aa; |
| 219 | bool alpha; |
| 220 | |
| 221 | Imlib_Color_Modifier cmod; |
| 222 | int gamma; |
| 223 | |
| 224 | struct { |
| 225 | bool on; |
| 226 | int delay; |
| 227 | } ss; |
| 228 | |
| 229 | multi_img_t multi; |
| 230 | }; |
| 231 | |
| 232 | void img_init(img_t*, win_t*); |
| 233 | bool img_load(img_t*, const fileinfo_t*); |
| 234 | CLEANUP void img_close(img_t*, bool); |
| 235 | void img_render(img_t*); |
| 236 | bool img_fit_win(img_t*, scalemode_t); |
| 237 | bool img_zoom(img_t*, float); |
| 238 | bool img_zoom_in(img_t*); |
| 239 | bool img_zoom_out(img_t*); |
| 240 | bool img_pos(img_t*, float, float); |
| 241 | bool img_move(img_t*, float, float); |
| 242 | bool img_pan(img_t*, direction_t, int); |
| 243 | bool img_pan_edge(img_t*, direction_t); |
| 244 | void img_rotate(img_t*, degree_t); |
| 245 | void img_flip(img_t*, flipdir_t); |
| 246 | void img_toggle_antialias(img_t*); |
| 247 | bool img_change_gamma(img_t*, int); |
| 248 | bool img_frame_navigate(img_t*, int); |
| 249 | bool img_frame_animate(img_t*); |
| 250 | |
| 251 | |
| 252 | /* options.c */ |
| 253 | |
| 254 | struct opt { |
| 255 | /* file list: */ |
| 256 | char **filenames; |
| 257 | bool from_stdin; |
| 258 | bool to_stdout; |
| 259 | bool recursive; |
| 260 | int filecnt; |
| 261 | int startnum; |
| 262 | |
| 263 | /* image: */ |
| 264 | scalemode_t scalemode; |
| 265 | float zoom; |
| 266 | bool animate; |
| 267 | int gamma; |
| 268 | int slideshow; |
| 269 | int framerate; |
| 270 | |
| 271 | /* window: */ |
| 272 | bool fullscreen; |
| 273 | bool hide_bar; |
| 274 | long embed; |
| 275 | char *geometry; |
| 276 | char *res_name; |
| 277 | |
| 278 | /* misc flags: */ |
| 279 | bool quiet; |
| 280 | bool thumb_mode; |
| 281 | bool clean_cache; |
| 282 | bool private_mode; |
| 283 | }; |
| 284 | |
| 285 | extern const opt_t *options; |
| 286 | |
| 287 | void print_usage(void); |
| 288 | void print_version(void); |
| 289 | void parse_options(int, char**); |
| 290 | |
| 291 | |
| 292 | /* thumbs.c */ |
| 293 | |
| 294 | typedef struct { |
| 295 | Imlib_Image im; |
| 296 | int w; |
| 297 | int h; |
| 298 | int x; |
| 299 | int y; |
| 300 | } thumb_t; |
| 301 | |
| 302 | struct tns { |
| 303 | fileinfo_t *files; |
| 304 | thumb_t *thumbs; |
| 305 | const int *cnt; |
| 306 | int *sel; |
| 307 | int initnext; |
| 308 | int loadnext; |
| 309 | int first, end; |
| 310 | int r_first, r_end; |
| 311 | |
| 312 | win_t *win; |
| 313 | int x; |
| 314 | int y; |
| 315 | int cols; |
| 316 | int rows; |
| 317 | int zl; |
| 318 | int bw; |
| 319 | int dim; |
| 320 | |
| 321 | bool dirty; |
| 322 | }; |
| 323 | |
| 324 | void tns_clean_cache(tns_t*); |
| 325 | void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*); |
| 326 | CLEANUP void tns_free(tns_t*); |
| 327 | bool tns_load(tns_t*, int, bool, bool); |
| 328 | void tns_unload(tns_t*, int); |
| 329 | void tns_render(tns_t*); |
| 330 | void tns_mark(tns_t*, int, bool); |
| 331 | void tns_highlight(tns_t*, int, bool); |
| 332 | bool tns_move_selection(tns_t*, direction_t, int); |
| 333 | bool tns_scroll(tns_t*, direction_t, bool); |
| 334 | bool tns_zoom(tns_t*, int); |
| 335 | int tns_translate(tns_t*, int, int); |
| 336 | |
| 337 | |
| 338 | /* util.c */ |
| 339 | |
| 340 | #include <dirent.h> |
| 341 | |
| 342 | typedef struct { |
| 343 | DIR *dir; |
| 344 | char *name; |
| 345 | int d; |
| 346 | bool recursive; |
| 347 | |
| 348 | char **stack; |
| 349 | int stcap; |
| 350 | int stlen; |
| 351 | } r_dir_t; |
| 352 | |
| 353 | extern const char *progname; |
| 354 | |
| 355 | void* emalloc(size_t); |
| 356 | void* erealloc(void*, size_t); |
| 357 | char* estrdup(const char*); |
| 358 | void error(int, int, const char*, ...); |
| 359 | void size_readable(float*, const char**); |
| 360 | int r_opendir(r_dir_t*, const char*, bool); |
| 361 | int r_closedir(r_dir_t*); |
| 362 | char* r_readdir(r_dir_t*); |
| 363 | int r_mkdir(char*); |
| 364 | void* utf8codepoint(const void * __restrict__ str, long * __restrict__ out_codepoint); |
| 365 | |
| 366 | |
| 367 | /* window.c */ |
| 368 | |
| 369 | #include <X11/Xutil.h> |
| 370 | #include <X11/Xft/Xft.h> |
| 371 | |
| 372 | enum { |
| 373 | BAR_L_LEN = 512, |
| 374 | BAR_R_LEN = 64 |
| 375 | }; |
| 376 | |
| 377 | enum { |
| 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 | |
| 388 | typedef 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 | |
| 397 | typedef struct { |
| 398 | size_t size; |
| 399 | char *p; |
| 400 | char *buf; |
| 401 | } win_bar_t; |
| 402 | |
| 403 | struct 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 | |
| 434 | extern Atom atoms[ATOM_COUNT]; |
| 435 | |
| 436 | void win_init(win_t*); |
| 437 | void win_open(win_t*); |
| 438 | CLEANUP void win_close(win_t*); |
| 439 | bool win_configure(win_t*, XConfigureEvent*); |
| 440 | void win_toggle_fullscreen(win_t*); |
| 441 | void win_toggle_bar(win_t*); |
| 442 | void win_clear(win_t*); |
| 443 | void win_draw(win_t*); |
| 444 | void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long); |
| 445 | int win_textwidth(const win_env_t*, const char*, unsigned int, bool, XftFont*); |
| 446 | void win_set_title(win_t*, const char*); |
| 447 | void win_set_cursor(win_t*, cursor_t); |
| 448 | void win_cursor_pos(win_t*, int*, int*); |
| 449 | |
| 450 | #endif /* SXIV_H */ |
| 451 | |