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