| 1 | /* sxiv: main.c |
| 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the |
| 6 | * Free Software Foundation; either version 2 of the License, or (at your |
| 7 | * option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _POSIX_C_SOURCE 200112L |
| 20 | #define _MAPPINGS_CONFIG |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <X11/Xutil.h> |
| 29 | #include <X11/keysym.h> |
| 30 | |
| 31 | #include "commands.h" |
| 32 | #include "image.h" |
| 33 | #include "options.h" |
| 34 | #include "thumbs.h" |
| 35 | #include "types.h" |
| 36 | #include "util.h" |
| 37 | #include "window.h" |
| 38 | #include "config.h" |
| 39 | |
| 40 | enum { |
| 41 | TITLE_LEN = 256, |
| 42 | FNAME_CNT = 1024 |
| 43 | }; |
| 44 | |
| 45 | typedef struct { |
| 46 | struct timeval when; |
| 47 | bool active; |
| 48 | timeout_f handler; |
| 49 | } timeout_t; |
| 50 | |
| 51 | /* timeout handler functions: */ |
| 52 | void redraw(void); |
| 53 | void reset_cursor(void); |
| 54 | void animate(void); |
| 55 | void slideshow(void); |
| 56 | |
| 57 | appmode_t mode; |
| 58 | img_t img; |
| 59 | tns_t tns; |
| 60 | win_t win; |
| 61 | |
| 62 | fileinfo_t *files; |
| 63 | int filecnt, fileidx; |
| 64 | size_t filesize; |
| 65 | |
| 66 | char win_title[TITLE_LEN]; |
| 67 | |
| 68 | timeout_t timeouts[] = { |
| 69 | { { 0, 0 }, false, redraw }, |
| 70 | { { 0, 0 }, false, reset_cursor }, |
| 71 | { { 0, 0 }, false, animate }, |
| 72 | { { 0, 0 }, false, slideshow }, |
| 73 | }; |
| 74 | |
| 75 | void cleanup(void) { |
| 76 | static bool in = false; |
| 77 | |
| 78 | if (!in) { |
| 79 | in = true; |
| 80 | img_close(&img, false); |
| 81 | tns_free(&tns); |
| 82 | win_close(&win); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void check_add_file(char *filename) { |
| 87 | if (filename == NULL || *filename == '\0') |
| 88 | return; |
| 89 | |
| 90 | if (access(filename, R_OK) < 0) { |
| 91 | warn("could not open file: %s", filename); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (fileidx == filecnt) { |
| 96 | filecnt *= 2; |
| 97 | files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t)); |
| 98 | } |
| 99 | if (*filename != '/') { |
| 100 | files[fileidx].path = absolute_path(filename); |
| 101 | if (files[fileidx].path == NULL) { |
| 102 | warn("could not get absolute path of file: %s\n", filename); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | files[fileidx].loaded = false; |
| 107 | files[fileidx].name = s_strdup(filename); |
| 108 | if (*filename == '/') |
| 109 | files[fileidx].path = files[fileidx].name; |
| 110 | fileidx++; |
| 111 | } |
| 112 | |
| 113 | void remove_file(int n, bool manual) { |
| 114 | if (n < 0 || n >= filecnt) |
| 115 | return; |
| 116 | |
| 117 | if (filecnt == 1) { |
| 118 | if (!manual) |
| 119 | fprintf(stderr, "sxiv: no more files to display, aborting\n"); |
| 120 | cleanup(); |
| 121 | exit(manual ? EXIT_SUCCESS : EXIT_FAILURE); |
| 122 | } |
| 123 | |
| 124 | if (files[n].path != files[n].name) |
| 125 | free((void*) files[n].path); |
| 126 | free((void*) files[n].name); |
| 127 | |
| 128 | if (n + 1 < filecnt) |
| 129 | memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t)); |
| 130 | if (n + 1 < tns.cnt) { |
| 131 | memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) * |
| 132 | sizeof(thumb_t)); |
| 133 | memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t)); |
| 134 | } |
| 135 | |
| 136 | filecnt--; |
| 137 | if (n < tns.cnt) |
| 138 | tns.cnt--; |
| 139 | } |
| 140 | |
| 141 | void set_timeout(timeout_f handler, int time, bool overwrite) { |
| 142 | int i; |
| 143 | |
| 144 | for (i = 0; i < ARRLEN(timeouts); i++) { |
| 145 | if (timeouts[i].handler == handler) { |
| 146 | if (!timeouts[i].active || overwrite) { |
| 147 | gettimeofday(&timeouts[i].when, 0); |
| 148 | tv_add_msec(&timeouts[i].when, time); |
| 149 | timeouts[i].active = true; |
| 150 | } |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void reset_timeout(timeout_f handler) { |
| 157 | int i; |
| 158 | |
| 159 | for (i = 0; i < ARRLEN(timeouts); i++) { |
| 160 | if (timeouts[i].handler == handler) { |
| 161 | timeouts[i].active = false; |
| 162 | return; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | bool check_timeouts(struct timeval *t) { |
| 168 | int i = 0, tdiff, tmin = -1; |
| 169 | struct timeval now; |
| 170 | |
| 171 | gettimeofday(&now, 0); |
| 172 | while (i < ARRLEN(timeouts)) { |
| 173 | if (timeouts[i].active) { |
| 174 | tdiff = tv_diff(&timeouts[i].when, &now); |
| 175 | if (tdiff <= 0) { |
| 176 | timeouts[i].active = false; |
| 177 | if (timeouts[i].handler != NULL) |
| 178 | timeouts[i].handler(); |
| 179 | i = tmin = -1; |
| 180 | } else if (tmin < 0 || tdiff < tmin) { |
| 181 | tmin = tdiff; |
| 182 | } |
| 183 | } |
| 184 | i++; |
| 185 | } |
| 186 | if (tmin > 0 && t != NULL) |
| 187 | tv_set_msec(t, tmin); |
| 188 | return tmin > 0; |
| 189 | } |
| 190 | |
| 191 | void load_image(int new) { |
| 192 | struct stat fstats; |
| 193 | |
| 194 | if (new < 0 || new >= filecnt) |
| 195 | return; |
| 196 | |
| 197 | win_set_cursor(&win, CURSOR_WATCH); |
| 198 | |
| 199 | img_close(&img, false); |
| 200 | while (!img_load(&img, &files[new])) { |
| 201 | remove_file(new, false); |
| 202 | if (new >= filecnt) |
| 203 | new = filecnt - 1; |
| 204 | } |
| 205 | |
| 206 | files[new].loaded = true; |
| 207 | fileidx = new; |
| 208 | if (stat(files[new].path, &fstats) == 0) |
| 209 | filesize = fstats.st_size; |
| 210 | else |
| 211 | filesize = 0; |
| 212 | |
| 213 | if (img.multi.cnt > 0 && img.multi.animate) |
| 214 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 215 | else |
| 216 | reset_timeout(animate); |
| 217 | } |
| 218 | |
| 219 | void update_title(void) { |
| 220 | int n; |
| 221 | char sshow_info[16]; |
| 222 | char frame_info[16]; |
| 223 | float size, time; |
| 224 | const char *size_unit, *time_unit; |
| 225 | |
| 226 | if (mode == MODE_THUMB) { |
| 227 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s", |
| 228 | tns.cnt ? tns.sel + 1 : 0, tns.cnt, |
| 229 | tns.cnt ? files[tns.sel].name : ""); |
| 230 | } else { |
| 231 | size = filesize; |
| 232 | size_readable(&size, &size_unit); |
| 233 | |
| 234 | if (img.slideshow) { |
| 235 | time = img.ss_delay / 1000.0; |
| 236 | time_readable(&time, &time_unit); |
| 237 | snprintf(sshow_info, sizeof(sshow_info), "*%d%s* ", |
| 238 | (int) time, time_unit); |
| 239 | } else { |
| 240 | sshow_info[0] = '\0'; |
| 241 | } |
| 242 | if (img.multi.cnt > 0) |
| 243 | snprintf(frame_info, sizeof(frame_info), "{%d/%d} ", |
| 244 | img.multi.sel + 1, img.multi.cnt); |
| 245 | else |
| 246 | frame_info[0] = '\0'; |
| 247 | |
| 248 | n = snprintf(win_title, TITLE_LEN, |
| 249 | "sxiv: [%d/%d] <%dx%d:%d%%> (%.2f%s) %s%s%s", |
| 250 | fileidx + 1, filecnt, img.w, img.h, |
| 251 | (int) (img.zoom * 100.0), size, size_unit, |
| 252 | sshow_info, frame_info, files[fileidx].name); |
| 253 | } |
| 254 | |
| 255 | if (n >= TITLE_LEN) { |
| 256 | for (n = 0; n < 3; n++) |
| 257 | win_title[TITLE_LEN - n - 2] = '.'; |
| 258 | } |
| 259 | |
| 260 | win_set_title(&win, win_title); |
| 261 | } |
| 262 | |
| 263 | void redraw(void) { |
| 264 | if (mode == MODE_IMAGE) { |
| 265 | img_render(&img); |
| 266 | if (img.slideshow && !img.multi.animate) { |
| 267 | if (fileidx + 1 < filecnt) |
| 268 | set_timeout(slideshow, img.ss_delay, true); |
| 269 | else |
| 270 | img.slideshow = false; |
| 271 | } |
| 272 | } else { |
| 273 | tns_render(&tns); |
| 274 | } |
| 275 | update_title(); |
| 276 | reset_timeout(redraw); |
| 277 | reset_cursor(); |
| 278 | } |
| 279 | |
| 280 | void reset_cursor(void) { |
| 281 | int i; |
| 282 | cursor_t cursor = CURSOR_NONE; |
| 283 | |
| 284 | if (mode == MODE_IMAGE) { |
| 285 | for (i = 0; i < ARRLEN(timeouts); i++) { |
| 286 | if (timeouts[i].handler == reset_cursor) { |
| 287 | if (timeouts[i].active) |
| 288 | cursor = CURSOR_ARROW; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | } else { |
| 293 | if (tns.cnt != filecnt) |
| 294 | cursor = CURSOR_WATCH; |
| 295 | else |
| 296 | cursor = CURSOR_ARROW; |
| 297 | } |
| 298 | win_set_cursor(&win, cursor); |
| 299 | } |
| 300 | |
| 301 | void animate(void) { |
| 302 | if (img_frame_animate(&img, false)) { |
| 303 | redraw(); |
| 304 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void slideshow(void) { |
| 309 | if (mode == MODE_IMAGE && !img.multi.animate) { |
| 310 | if (fileidx + 1 < filecnt) { |
| 311 | load_image(fileidx + 1); |
| 312 | redraw(); |
| 313 | } else { |
| 314 | img.slideshow = false; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | bool keymask(const keymap_t *k, unsigned int state) { |
| 320 | return (k->ctrl ? ControlMask : 0) == (state & ControlMask); |
| 321 | } |
| 322 | |
| 323 | bool buttonmask(const button_t *b, unsigned int state) { |
| 324 | return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) == |
| 325 | (state & (ControlMask | ShiftMask)); |
| 326 | } |
| 327 | |
| 328 | void on_keypress(XKeyEvent *kev) { |
| 329 | int i; |
| 330 | KeySym ksym; |
| 331 | char key; |
| 332 | |
| 333 | if (kev == NULL) |
| 334 | return; |
| 335 | |
| 336 | XLookupString(kev, &key, 1, &ksym, NULL); |
| 337 | |
| 338 | for (i = 0; i < ARRLEN(keys); i++) { |
| 339 | if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) { |
| 340 | if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg)) |
| 341 | redraw(); |
| 342 | return; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void on_buttonpress(XButtonEvent *bev) { |
| 348 | int i, sel; |
| 349 | |
| 350 | if (bev == NULL) |
| 351 | return; |
| 352 | |
| 353 | if (mode == MODE_IMAGE) { |
| 354 | win_set_cursor(&win, CURSOR_ARROW); |
| 355 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 356 | |
| 357 | for (i = 0; i < ARRLEN(buttons); i++) { |
| 358 | if (buttons[i].button == bev->button && |
| 359 | buttonmask(&buttons[i], bev->state)) |
| 360 | { |
| 361 | if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg)) |
| 362 | redraw(); |
| 363 | return; |
| 364 | } |
| 365 | } |
| 366 | } else { |
| 367 | /* thumbnail mode (hard-coded) */ |
| 368 | switch (bev->button) { |
| 369 | case Button1: |
| 370 | if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { |
| 371 | if (sel == tns.sel) { |
| 372 | mode = MODE_IMAGE; |
| 373 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 374 | load_image(tns.sel); |
| 375 | } else { |
| 376 | tns_highlight(&tns, tns.sel, false); |
| 377 | tns_highlight(&tns, sel, true); |
| 378 | tns.sel = sel; |
| 379 | } |
| 380 | redraw(); |
| 381 | break; |
| 382 | } |
| 383 | break; |
| 384 | case Button4: |
| 385 | case Button5: |
| 386 | if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN)) |
| 387 | redraw(); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void run(void) { |
| 394 | int xfd; |
| 395 | fd_set fds; |
| 396 | struct timeval timeout; |
| 397 | XEvent ev; |
| 398 | |
| 399 | redraw(); |
| 400 | |
| 401 | while (true) { |
| 402 | while (mode == MODE_THUMB && tns.cnt < filecnt && |
| 403 | XPending(win.env.dpy) == 0) |
| 404 | { |
| 405 | /* load thumbnails */ |
| 406 | set_timeout(redraw, TO_REDRAW_THUMBS, false); |
| 407 | if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) |
| 408 | tns.cnt++; |
| 409 | else |
| 410 | remove_file(tns.cnt, false); |
| 411 | if (tns.cnt == filecnt) |
| 412 | redraw(); |
| 413 | else |
| 414 | check_timeouts(NULL); |
| 415 | } |
| 416 | |
| 417 | while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) { |
| 418 | /* wait for timeouts */ |
| 419 | xfd = ConnectionNumber(win.env.dpy); |
| 420 | FD_ZERO(&fds); |
| 421 | FD_SET(xfd, &fds); |
| 422 | select(xfd + 1, &fds, 0, 0, &timeout); |
| 423 | } |
| 424 | |
| 425 | XNextEvent(win.env.dpy, &ev); |
| 426 | switch (ev.type) { |
| 427 | /* handle events */ |
| 428 | case ButtonPress: |
| 429 | on_buttonpress(&ev.xbutton); |
| 430 | break; |
| 431 | case ClientMessage: |
| 432 | if ((Atom) ev.xclient.data.l[0] == wm_delete_win) |
| 433 | return; |
| 434 | break; |
| 435 | case ConfigureNotify: |
| 436 | if (win_configure(&win, &ev.xconfigure)) { |
| 437 | set_timeout(redraw, TO_REDRAW_RESIZE, false); |
| 438 | if (mode == MODE_IMAGE) |
| 439 | img.checkpan = true; |
| 440 | else |
| 441 | tns.dirty = true; |
| 442 | } |
| 443 | break; |
| 444 | case KeyPress: |
| 445 | on_keypress(&ev.xkey); |
| 446 | break; |
| 447 | case MotionNotify: |
| 448 | if (mode == MODE_IMAGE) { |
| 449 | win_set_cursor(&win, CURSOR_ARROW); |
| 450 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 451 | } |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | int fncmp(const void *a, const void *b) { |
| 458 | return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name); |
| 459 | } |
| 460 | |
| 461 | int main(int argc, char **argv) { |
| 462 | int i, start; |
| 463 | size_t n; |
| 464 | ssize_t len; |
| 465 | char *filename; |
| 466 | struct stat fstats; |
| 467 | r_dir_t dir; |
| 468 | |
| 469 | parse_options(argc, argv); |
| 470 | |
| 471 | if (options->clean_cache) { |
| 472 | tns_init(&tns, 0, NULL); |
| 473 | tns_clean_cache(&tns); |
| 474 | exit(EXIT_SUCCESS); |
| 475 | } |
| 476 | |
| 477 | if (options->filecnt == 0) { |
| 478 | print_usage(); |
| 479 | exit(EXIT_FAILURE); |
| 480 | } |
| 481 | |
| 482 | if (options->recursive || options->from_stdin) |
| 483 | filecnt = FNAME_CNT; |
| 484 | else |
| 485 | filecnt = options->filecnt; |
| 486 | |
| 487 | files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); |
| 488 | fileidx = 0; |
| 489 | |
| 490 | /* build file list: */ |
| 491 | if (options->from_stdin) { |
| 492 | filename = NULL; |
| 493 | while ((len = get_line(&filename, &n, stdin)) > 0) { |
| 494 | if (filename[len-1] == '\n') |
| 495 | filename[len-1] = '\0'; |
| 496 | check_add_file(filename); |
| 497 | } |
| 498 | if (filename != NULL) |
| 499 | free(filename); |
| 500 | } else { |
| 501 | for (i = 0; i < options->filecnt; i++) { |
| 502 | filename = options->filenames[i]; |
| 503 | |
| 504 | if (stat(filename, &fstats) < 0) { |
| 505 | warn("could not stat file: %s", filename); |
| 506 | continue; |
| 507 | } |
| 508 | if (!S_ISDIR(fstats.st_mode)) { |
| 509 | check_add_file(filename); |
| 510 | } else { |
| 511 | if (!options->recursive) { |
| 512 | warn("ignoring directory: %s", filename); |
| 513 | continue; |
| 514 | } |
| 515 | if (r_opendir(&dir, filename) < 0) { |
| 516 | warn("could not open directory: %s", filename); |
| 517 | continue; |
| 518 | } |
| 519 | start = fileidx; |
| 520 | while ((filename = r_readdir(&dir)) != NULL) { |
| 521 | check_add_file(filename); |
| 522 | free((void*) filename); |
| 523 | } |
| 524 | r_closedir(&dir); |
| 525 | if (fileidx - start > 1) |
| 526 | qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if (fileidx == 0) { |
| 532 | fprintf(stderr, "sxiv: no valid image file given, aborting\n"); |
| 533 | exit(EXIT_FAILURE); |
| 534 | } |
| 535 | |
| 536 | filecnt = fileidx; |
| 537 | fileidx = options->startnum < filecnt ? options->startnum : 0; |
| 538 | |
| 539 | win_init(&win); |
| 540 | img_init(&img, &win); |
| 541 | |
| 542 | if (options->thumb_mode) { |
| 543 | mode = MODE_THUMB; |
| 544 | tns_init(&tns, filecnt, &win); |
| 545 | while (!tns_load(&tns, 0, &files[0], false, false)) |
| 546 | remove_file(0, false); |
| 547 | tns.cnt = 1; |
| 548 | } else { |
| 549 | mode = MODE_IMAGE; |
| 550 | tns.thumbs = NULL; |
| 551 | load_image(fileidx); |
| 552 | } |
| 553 | |
| 554 | win_open(&win); |
| 555 | |
| 556 | run(); |
| 557 | cleanup(); |
| 558 | |
| 559 | return 0; |
| 560 | } |