| 1 | /* Copyright 2011-2020 Bert Muennich |
| 2 | * Copyright 2021 nsxiv contributors |
| 3 | * |
| 4 | * This file is a part of nsxiv. |
| 5 | * |
| 6 | * nsxiv is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published |
| 8 | * by the Free Software Foundation; either version 2 of the License, |
| 9 | * or (at your option) any later version. |
| 10 | * |
| 11 | * nsxiv is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with nsxiv. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NSXIV_H |
| 21 | #define NSXIV_H |
| 22 | |
| 23 | #include <stdarg.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <stdio.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <Imlib2.h> |
| 29 | #include <X11/Xlib.h> |
| 30 | |
| 31 | /* |
| 32 | * Annotation for functions called in cleanup(). |
| 33 | * These functions are not allowed to call error(!0, ...) or exit(). |
| 34 | */ |
| 35 | #define CLEANUP |
| 36 | |
| 37 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 38 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 39 | #define ABS(a) ((a) > 0 ? (a) : -(a)) |
| 40 | |
| 41 | #define ARRLEN(a) (sizeof(a) / sizeof((a)[0])) |
| 42 | |
| 43 | #define STREQ(s1,s2) (strcmp((s1), (s2)) == 0) |
| 44 | |
| 45 | #define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \ |
| 46 | ((t1)->tv_usec - (t2)->tv_usec) / 1000) |
| 47 | |
| 48 | #define TV_SET_MSEC(tv,t) { \ |
| 49 | (tv)->tv_sec = (t) / 1000; \ |
| 50 | (tv)->tv_usec = (t) % 1000 * 1000; \ |
| 51 | } |
| 52 | |
| 53 | #define TV_ADD_MSEC(tv,t) { \ |
| 54 | (tv)->tv_sec += (t) / 1000; \ |
| 55 | (tv)->tv_usec += (t) % 1000 * 1000; \ |
| 56 | } |
| 57 | |
| 58 | typedef enum { |
| 59 | BO_BIG_ENDIAN, |
| 60 | BO_LITTLE_ENDIAN |
| 61 | } byteorder_t; |
| 62 | |
| 63 | typedef enum { |
| 64 | MODE_ALL, |
| 65 | MODE_IMAGE, |
| 66 | MODE_THUMB |
| 67 | } appmode_t; |
| 68 | |
| 69 | typedef enum { |
| 70 | DIR_LEFT = 1, |
| 71 | DIR_RIGHT = 2, |
| 72 | DIR_UP = 4, |
| 73 | DIR_DOWN = 8 |
| 74 | } direction_t; |
| 75 | |
| 76 | typedef enum { |
| 77 | DEGREE_90 = 1, |
| 78 | DEGREE_180 = 2, |
| 79 | DEGREE_270 = 3 |
| 80 | } degree_t; |
| 81 | |
| 82 | typedef enum { |
| 83 | FLIP_HORIZONTAL = 1, |
| 84 | FLIP_VERTICAL = 2 |
| 85 | } flipdir_t; |
| 86 | |
| 87 | typedef enum { |
| 88 | SCALE_DOWN, |
| 89 | SCALE_FIT, |
| 90 | SCALE_FILL, |
| 91 | SCALE_WIDTH, |
| 92 | SCALE_HEIGHT, |
| 93 | SCALE_ZOOM |
| 94 | } scalemode_t; |
| 95 | |
| 96 | typedef enum { |
| 97 | DRAG_RELATIVE, |
| 98 | DRAG_ABSOLUTE |
| 99 | } dragmode_t; |
| 100 | |
| 101 | typedef enum { |
| 102 | CURSOR_ARROW, |
| 103 | CURSOR_DRAG_ABSOLUTE, |
| 104 | CURSOR_DRAG_RELATIVE, |
| 105 | CURSOR_WATCH, |
| 106 | CURSOR_LEFT, |
| 107 | CURSOR_RIGHT, |
| 108 | CURSOR_NONE, |
| 109 | |
| 110 | CURSOR_COUNT |
| 111 | } cursor_t; |
| 112 | |
| 113 | typedef enum { |
| 114 | FF_WARN = 1, |
| 115 | FF_MARK = 2, |
| 116 | FF_TN_INIT = 4 |
| 117 | } fileflags_t; |
| 118 | |
| 119 | typedef enum { |
| 120 | SUFFIX_EMPTY, |
| 121 | SUFFIX_BASENAME, |
| 122 | SUFFIX_FULLPATH |
| 123 | } suffixmode_t; |
| 124 | |
| 125 | typedef 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: */ |
| 132 | enum { |
| 133 | TO_REDRAW_RESIZE = 75, |
| 134 | TO_REDRAW_THUMBS = 200, |
| 135 | TO_CURSOR_HIDE = 1200, |
| 136 | TO_DOUBLE_CLICK = 300 |
| 137 | }; |
| 138 | |
| 139 | typedef void (*timeout_f)(void); |
| 140 | |
| 141 | typedef struct arl arl_t; |
| 142 | typedef struct img img_t; |
| 143 | typedef struct opt opt_t; |
| 144 | typedef struct tns tns_t; |
| 145 | typedef struct win win_t; |
| 146 | |
| 147 | |
| 148 | /* autoreload.c */ |
| 149 | |
| 150 | struct arl { |
| 151 | int fd; |
| 152 | int wd_dir; |
| 153 | int wd_file; |
| 154 | char *filename; |
| 155 | }; |
| 156 | |
| 157 | void arl_init(arl_t*); |
| 158 | void arl_cleanup(arl_t*); |
| 159 | void arl_setup(arl_t*, const char* /* result of realpath(3) */); |
| 160 | bool arl_handle(arl_t*); |
| 161 | |
| 162 | |
| 163 | /* commands.c */ |
| 164 | |
| 165 | typedef int arg_t; |
| 166 | typedef bool (*cmd_f)(arg_t); |
| 167 | |
| 168 | typedef struct { |
| 169 | cmd_f func; |
| 170 | appmode_t mode; |
| 171 | } cmd_t; |
| 172 | |
| 173 | typedef struct { |
| 174 | unsigned int mask; |
| 175 | KeySym ksym; |
| 176 | cmd_t cmd; |
| 177 | arg_t arg; |
| 178 | } keymap_t; |
| 179 | |
| 180 | typedef struct { |
| 181 | unsigned int mask; |
| 182 | unsigned int button; |
| 183 | cmd_t cmd; |
| 184 | arg_t arg; |
| 185 | } button_t; |
| 186 | |
| 187 | |
| 188 | /* image.c */ |
| 189 | |
| 190 | typedef struct { |
| 191 | Imlib_Image im; |
| 192 | unsigned int delay; |
| 193 | } img_frame_t; |
| 194 | |
| 195 | typedef struct { |
| 196 | img_frame_t *frames; |
| 197 | int cap; |
| 198 | int cnt; |
| 199 | int sel; |
| 200 | bool animate; |
| 201 | unsigned int framedelay; |
| 202 | int length; |
| 203 | } multi_img_t; |
| 204 | |
| 205 | struct img { |
| 206 | Imlib_Image im; |
| 207 | int w; |
| 208 | int h; |
| 209 | |
| 210 | win_t *win; |
| 211 | float x; |
| 212 | float y; |
| 213 | |
| 214 | Imlib_Color_Modifier cmod; |
| 215 | int gamma; |
| 216 | |
| 217 | scalemode_t scalemode; |
| 218 | float zoom; |
| 219 | |
| 220 | bool checkpan; |
| 221 | bool dirty; |
| 222 | bool aa; |
| 223 | bool alpha; |
| 224 | |
| 225 | struct { |
| 226 | bool on; |
| 227 | int delay; |
| 228 | } ss; |
| 229 | |
| 230 | multi_img_t multi; |
| 231 | }; |
| 232 | |
| 233 | void img_init(img_t*, win_t*); |
| 234 | bool img_load(img_t*, const fileinfo_t*); |
| 235 | CLEANUP void img_close(img_t*, bool); |
| 236 | void img_render(img_t*); |
| 237 | bool img_fit_win(img_t*, scalemode_t); |
| 238 | bool img_zoom(img_t*, int); |
| 239 | bool img_zoom_to(img_t*, float); |
| 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 | char stdout_separator; |
| 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 | Window embed; /* unsigned long */ |
| 276 | char *geometry; |
| 277 | char *res_name; |
| 278 | const char *title_prefix; |
| 279 | suffixmode_t title_suffixmode; |
| 280 | |
| 281 | /* misc flags: */ |
| 282 | bool quiet; |
| 283 | bool thumb_mode; |
| 284 | bool clean_cache; |
| 285 | bool private_mode; |
| 286 | }; |
| 287 | |
| 288 | extern const opt_t *options; |
| 289 | |
| 290 | void print_usage(void); |
| 291 | void print_version(void); |
| 292 | void parse_options(int, char**); |
| 293 | |
| 294 | |
| 295 | /* thumbs.c */ |
| 296 | |
| 297 | typedef struct { |
| 298 | Imlib_Image im; |
| 299 | int w; |
| 300 | int h; |
| 301 | int x; |
| 302 | int y; |
| 303 | } thumb_t; |
| 304 | |
| 305 | struct tns { |
| 306 | fileinfo_t *files; |
| 307 | thumb_t *thumbs; |
| 308 | const int *cnt; |
| 309 | int *sel; |
| 310 | int initnext; |
| 311 | int loadnext; |
| 312 | int first, end; |
| 313 | int r_first, r_end; |
| 314 | |
| 315 | win_t *win; |
| 316 | int x; |
| 317 | int y; |
| 318 | int cols; |
| 319 | int rows; |
| 320 | int zl; |
| 321 | int bw; |
| 322 | int dim; |
| 323 | |
| 324 | bool dirty; |
| 325 | }; |
| 326 | |
| 327 | void tns_clean_cache(void); |
| 328 | void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*); |
| 329 | CLEANUP void tns_free(tns_t*); |
| 330 | bool tns_load(tns_t*, int, bool, bool); |
| 331 | void tns_unload(tns_t*, int); |
| 332 | void tns_render(tns_t*); |
| 333 | void tns_mark(tns_t*, int, bool); |
| 334 | void tns_highlight(tns_t*, int, bool); |
| 335 | bool tns_move_selection(tns_t*, direction_t, int); |
| 336 | bool tns_scroll(tns_t*, direction_t, bool); |
| 337 | bool tns_zoom(tns_t*, int); |
| 338 | int tns_translate(tns_t*, int, int); |
| 339 | |
| 340 | |
| 341 | /* util.c */ |
| 342 | |
| 343 | #include <dirent.h> |
| 344 | |
| 345 | typedef struct { |
| 346 | DIR *dir; |
| 347 | char *name; |
| 348 | int d; |
| 349 | bool recursive; |
| 350 | |
| 351 | char **stack; |
| 352 | int stcap; |
| 353 | int stlen; |
| 354 | } r_dir_t; |
| 355 | |
| 356 | extern const char *progname; |
| 357 | |
| 358 | void* emalloc(size_t); |
| 359 | void* erealloc(void*, size_t); |
| 360 | char* estrdup(const char*); |
| 361 | void error(int, int, const char*, ...); |
| 362 | void size_readable(float*, const char**); |
| 363 | int r_opendir(r_dir_t*, const char*, bool); |
| 364 | int r_closedir(r_dir_t*); |
| 365 | char* r_readdir(r_dir_t*, bool); |
| 366 | int r_mkdir(char*); |
| 367 | |
| 368 | |
| 369 | /* window.c */ |
| 370 | |
| 371 | #include <X11/Xutil.h> |
| 372 | #if HAVE_LIBFONTS |
| 373 | #include <X11/Xft/Xft.h> |
| 374 | #endif |
| 375 | |
| 376 | enum { |
| 377 | BAR_L_LEN = 512, |
| 378 | BAR_R_LEN = 64 |
| 379 | }; |
| 380 | |
| 381 | enum { |
| 382 | ATOM_WM_DELETE_WINDOW, |
| 383 | ATOM__NET_WM_NAME, |
| 384 | ATOM__NET_WM_ICON_NAME, |
| 385 | ATOM__NET_WM_ICON, |
| 386 | ATOM__NET_WM_STATE, |
| 387 | ATOM__NET_WM_PID, |
| 388 | ATOM__NET_WM_STATE_FULLSCREEN, |
| 389 | ATOM_COUNT |
| 390 | }; |
| 391 | |
| 392 | typedef struct { |
| 393 | Display *dpy; |
| 394 | int scr; |
| 395 | int scrw, scrh; |
| 396 | Visual *vis; |
| 397 | Colormap cmap; |
| 398 | int depth; |
| 399 | } win_env_t; |
| 400 | |
| 401 | typedef struct { |
| 402 | size_t size; |
| 403 | char *p; |
| 404 | char *buf; |
| 405 | } win_bar_t; |
| 406 | |
| 407 | struct win { |
| 408 | Window xwin; |
| 409 | win_env_t env; |
| 410 | |
| 411 | XColor win_bg; |
| 412 | XColor win_fg; |
| 413 | XColor mrk_fg; |
| 414 | #if HAVE_LIBFONTS |
| 415 | XftColor bar_bg; |
| 416 | XftColor bar_fg; |
| 417 | #endif |
| 418 | |
| 419 | int x; |
| 420 | int y; |
| 421 | unsigned int w; |
| 422 | unsigned int h; /* = win height - bar height */ |
| 423 | unsigned int bw; |
| 424 | |
| 425 | struct { |
| 426 | int w; |
| 427 | int h; |
| 428 | Pixmap pm; |
| 429 | } buf; |
| 430 | |
| 431 | struct { |
| 432 | unsigned int h; |
| 433 | win_bar_t l; |
| 434 | win_bar_t r; |
| 435 | } bar; |
| 436 | }; |
| 437 | |
| 438 | extern Atom atoms[ATOM_COUNT]; |
| 439 | |
| 440 | void win_init(win_t*); |
| 441 | void win_open(win_t*); |
| 442 | CLEANUP void win_close(win_t*); |
| 443 | bool win_configure(win_t*, XConfigureEvent*); |
| 444 | void win_toggle_fullscreen(win_t*); |
| 445 | void win_toggle_bar(win_t*); |
| 446 | void win_clear(win_t*); |
| 447 | void win_draw(win_t*); |
| 448 | void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long); |
| 449 | void win_set_title(win_t*, const char*); |
| 450 | void win_set_cursor(win_t*, cursor_t); |
| 451 | void win_cursor_pos(win_t*, int*, int*); |
| 452 | |
| 453 | #endif /* NSXIV_H */ |