| 1 | /* Copyright 2011-2013 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 | #define _POSIX_C_SOURCE 200112L |
| 20 | #define _MAPPINGS_CONFIG |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
| 28 | #include <signal.h> |
| 29 | #include <sys/select.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <X11/keysym.h> |
| 34 | |
| 35 | #include "types.h" |
| 36 | #include "commands.h" |
| 37 | #include "image.h" |
| 38 | #include "options.h" |
| 39 | #include "thumbs.h" |
| 40 | #include "util.h" |
| 41 | #include "window.h" |
| 42 | #include "config.h" |
| 43 | |
| 44 | enum { |
| 45 | FILENAME_CNT = 1024, |
| 46 | TITLE_LEN = 256 |
| 47 | }; |
| 48 | |
| 49 | typedef struct { |
| 50 | struct timeval when; |
| 51 | bool active; |
| 52 | timeout_f handler; |
| 53 | } timeout_t; |
| 54 | |
| 55 | /* timeout handler functions: */ |
| 56 | void redraw(void); |
| 57 | void reset_cursor(void); |
| 58 | void animate(void); |
| 59 | void clear_resize(void); |
| 60 | |
| 61 | appmode_t mode; |
| 62 | img_t img; |
| 63 | tns_t tns; |
| 64 | win_t win; |
| 65 | |
| 66 | fileinfo_t *files; |
| 67 | int filecnt, fileidx; |
| 68 | int alternate; |
| 69 | |
| 70 | int prefix; |
| 71 | |
| 72 | bool resized = false; |
| 73 | |
| 74 | const char * const INFO_SCRIPT = ".sxiv/exec/image-info"; |
| 75 | struct { |
| 76 | char *script; |
| 77 | int fd; |
| 78 | unsigned int i, lastsep; |
| 79 | bool open; |
| 80 | } info; |
| 81 | |
| 82 | timeout_t timeouts[] = { |
| 83 | { { 0, 0 }, false, redraw }, |
| 84 | { { 0, 0 }, false, reset_cursor }, |
| 85 | { { 0, 0 }, false, animate }, |
| 86 | { { 0, 0 }, false, clear_resize }, |
| 87 | }; |
| 88 | |
| 89 | void cleanup(void) |
| 90 | { |
| 91 | static bool in = false; |
| 92 | |
| 93 | if (!in) { |
| 94 | in = true; |
| 95 | img_close(&img, false); |
| 96 | tns_free(&tns); |
| 97 | win_close(&win); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void check_add_file(char *filename) |
| 102 | { |
| 103 | const char *bn; |
| 104 | |
| 105 | if (filename == NULL || *filename == '\0') |
| 106 | return; |
| 107 | |
| 108 | if (access(filename, R_OK) < 0) { |
| 109 | warn("could not open file: %s", filename); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if (fileidx == filecnt) { |
| 114 | filecnt *= 2; |
| 115 | files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t)); |
| 116 | } |
| 117 | if (*filename != '/') { |
| 118 | files[fileidx].path = absolute_path(filename); |
| 119 | if (files[fileidx].path == NULL) { |
| 120 | warn("could not get absolute path of file: %s\n", filename); |
| 121 | return; |
| 122 | } |
| 123 | } |
| 124 | files[fileidx].loaded = false; |
| 125 | files[fileidx].name = s_strdup(filename); |
| 126 | if (*filename == '/') |
| 127 | files[fileidx].path = files[fileidx].name; |
| 128 | if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0') |
| 129 | files[fileidx].base = ++bn; |
| 130 | else |
| 131 | files[fileidx].base = files[fileidx].name; |
| 132 | fileidx++; |
| 133 | } |
| 134 | |
| 135 | void remove_file(int n, bool manual) |
| 136 | { |
| 137 | if (n < 0 || n >= filecnt) |
| 138 | return; |
| 139 | |
| 140 | if (filecnt == 1) { |
| 141 | if (!manual) |
| 142 | fprintf(stderr, "sxiv: no more files to display, aborting\n"); |
| 143 | cleanup(); |
| 144 | exit(manual ? EXIT_SUCCESS : EXIT_FAILURE); |
| 145 | } |
| 146 | |
| 147 | if (files[n].path != files[n].name) |
| 148 | free((void*) files[n].path); |
| 149 | free((void*) files[n].name); |
| 150 | |
| 151 | if (n + 1 < filecnt) |
| 152 | memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t)); |
| 153 | if (n + 1 < tns.cnt) { |
| 154 | memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) * |
| 155 | sizeof(thumb_t)); |
| 156 | memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t)); |
| 157 | } |
| 158 | |
| 159 | filecnt--; |
| 160 | if (n < tns.cnt) |
| 161 | tns.cnt--; |
| 162 | } |
| 163 | |
| 164 | void set_timeout(timeout_f handler, int time, bool overwrite) |
| 165 | { |
| 166 | int i; |
| 167 | |
| 168 | for (i = 0; i < ARRLEN(timeouts); i++) { |
| 169 | if (timeouts[i].handler == handler) { |
| 170 | if (!timeouts[i].active || overwrite) { |
| 171 | gettimeofday(&timeouts[i].when, 0); |
| 172 | TV_ADD_MSEC(&timeouts[i].when, time); |
| 173 | timeouts[i].active = true; |
| 174 | } |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void reset_timeout(timeout_f handler) |
| 181 | { |
| 182 | int i; |
| 183 | |
| 184 | for (i = 0; i < ARRLEN(timeouts); i++) { |
| 185 | if (timeouts[i].handler == handler) { |
| 186 | timeouts[i].active = false; |
| 187 | return; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | bool check_timeouts(struct timeval *t) |
| 193 | { |
| 194 | int i = 0, tdiff, tmin = -1; |
| 195 | struct timeval now; |
| 196 | |
| 197 | while (i < ARRLEN(timeouts)) { |
| 198 | if (timeouts[i].active) { |
| 199 | gettimeofday(&now, 0); |
| 200 | tdiff = TV_DIFF(&timeouts[i].when, &now); |
| 201 | if (tdiff <= 0) { |
| 202 | timeouts[i].active = false; |
| 203 | if (timeouts[i].handler != NULL) |
| 204 | timeouts[i].handler(); |
| 205 | i = tmin = -1; |
| 206 | } else if (tmin < 0 || tdiff < tmin) { |
| 207 | tmin = tdiff; |
| 208 | } |
| 209 | } |
| 210 | i++; |
| 211 | } |
| 212 | if (tmin > 0 && t != NULL) |
| 213 | TV_SET_MSEC(t, tmin); |
| 214 | return tmin > 0; |
| 215 | } |
| 216 | |
| 217 | void open_info(void) |
| 218 | { |
| 219 | static pid_t pid; |
| 220 | int pfd[2]; |
| 221 | |
| 222 | if (info.script == NULL || info.open || win.bar.h == 0) |
| 223 | return; |
| 224 | if (info.fd != -1) { |
| 225 | close(info.fd); |
| 226 | kill(pid, SIGTERM); |
| 227 | info.fd = -1; |
| 228 | } |
| 229 | win.bar.l[0] = '\0'; |
| 230 | |
| 231 | if (pipe(pfd) < 0) |
| 232 | return; |
| 233 | pid = fork(); |
| 234 | if (pid > 0) { |
| 235 | close(pfd[1]); |
| 236 | fcntl(pfd[0], F_SETFL, O_NONBLOCK); |
| 237 | info.fd = pfd[0]; |
| 238 | info.i = info.lastsep = 0; |
| 239 | info.open = true; |
| 240 | } else if (pid == 0) { |
| 241 | close(pfd[0]); |
| 242 | dup2(pfd[1], 1); |
| 243 | execl(info.script, info.script, files[fileidx].name, NULL); |
| 244 | warn("could not exec: %s", info.script); |
| 245 | exit(EXIT_FAILURE); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void read_info(void) |
| 250 | { |
| 251 | ssize_t i, n; |
| 252 | char buf[BAR_L_LEN]; |
| 253 | |
| 254 | while (true) { |
| 255 | n = read(info.fd, buf, sizeof(buf)); |
| 256 | if (n < 0 && errno == EAGAIN) |
| 257 | return; |
| 258 | else if (n == 0) |
| 259 | goto end; |
| 260 | for (i = 0; i < n; i++) { |
| 261 | if (buf[i] == '\n') { |
| 262 | if (info.lastsep == 0) { |
| 263 | win.bar.l[info.i++] = ' '; |
| 264 | info.lastsep = 1; |
| 265 | } |
| 266 | } else { |
| 267 | win.bar.l[info.i++] = buf[i]; |
| 268 | info.lastsep = 0; |
| 269 | } |
| 270 | if (info.i + 1 == sizeof(win.bar.l)) |
| 271 | goto end; |
| 272 | } |
| 273 | } |
| 274 | end: |
| 275 | info.i -= info.lastsep; |
| 276 | win.bar.l[info.i] = '\0'; |
| 277 | win_update_bar(&win); |
| 278 | info.fd = -1; |
| 279 | while (waitpid(-1, NULL, WNOHANG) > 0); |
| 280 | } |
| 281 | |
| 282 | void load_image(int new) |
| 283 | { |
| 284 | if (new < 0 || new >= filecnt) |
| 285 | return; |
| 286 | |
| 287 | win_set_cursor(&win, CURSOR_WATCH); |
| 288 | |
| 289 | img_close(&img, false); |
| 290 | while (!img_load(&img, &files[new])) { |
| 291 | remove_file(new, false); |
| 292 | if (new >= filecnt) |
| 293 | new = filecnt - 1; |
| 294 | } |
| 295 | |
| 296 | files[new].loaded = true; |
| 297 | alternate = fileidx; |
| 298 | fileidx = new; |
| 299 | |
| 300 | info.open = false; |
| 301 | open_info(); |
| 302 | |
| 303 | if (img.multi.cnt > 0 && img.multi.animate) |
| 304 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 305 | else |
| 306 | reset_timeout(animate); |
| 307 | } |
| 308 | |
| 309 | void update_info(void) |
| 310 | { |
| 311 | int sel; |
| 312 | unsigned int i, fn, fw, n; |
| 313 | unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r); |
| 314 | char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN]; |
| 315 | bool ow_info; |
| 316 | |
| 317 | for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); |
| 318 | sel = mode == MODE_IMAGE ? fileidx : tns.sel; |
| 319 | |
| 320 | /* update window title */ |
| 321 | if (mode == MODE_THUMB) { |
| 322 | win_set_title(&win, "sxiv"); |
| 323 | } else { |
| 324 | snprintf(title, sizeof(title), "sxiv - %s", files[sel].name); |
| 325 | win_set_title(&win, title); |
| 326 | } |
| 327 | |
| 328 | /* update bar contents */ |
| 329 | if (win.bar.h == 0) |
| 330 | return; |
| 331 | if (mode == MODE_THUMB) { |
| 332 | if (tns.cnt == filecnt) { |
| 333 | n = snprintf(rt, rlen, "%0*d/%d", fw, sel + 1, filecnt); |
| 334 | ow_info = true; |
| 335 | } else { |
| 336 | snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.cnt, filecnt); |
| 337 | rt[0] = '\0'; |
| 338 | ow_info = false; |
| 339 | } |
| 340 | } else { |
| 341 | n = snprintf(rt, rlen, "%3d%% | ", (int) (img.zoom * 100.0)); |
| 342 | if (img.multi.cnt > 0) { |
| 343 | for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10); |
| 344 | n += snprintf(rt + n, rlen - n, "%0*d/%d | ", |
| 345 | fn, img.multi.sel + 1, img.multi.cnt); |
| 346 | } |
| 347 | n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt); |
| 348 | ow_info = info.script == NULL; |
| 349 | } |
| 350 | if (ow_info) { |
| 351 | fn = strlen(files[sel].name); |
| 352 | if (fn < llen && |
| 353 | win_textwidth(files[sel].name, fn, true) + |
| 354 | win_textwidth(rt, n, true) < win.w) |
| 355 | { |
| 356 | strncpy(lt, files[sel].name, llen); |
| 357 | } else { |
| 358 | strncpy(lt, files[sel].base, llen); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void redraw(void) |
| 364 | { |
| 365 | if (mode == MODE_IMAGE) |
| 366 | img_render(&img); |
| 367 | else |
| 368 | tns_render(&tns); |
| 369 | update_info(); |
| 370 | win_draw(&win); |
| 371 | reset_timeout(redraw); |
| 372 | reset_cursor(); |
| 373 | } |
| 374 | |
| 375 | void reset_cursor(void) |
| 376 | { |
| 377 | int i; |
| 378 | cursor_t cursor = CURSOR_NONE; |
| 379 | |
| 380 | if (mode == MODE_IMAGE) { |
| 381 | for (i = 0; i < ARRLEN(timeouts); i++) { |
| 382 | if (timeouts[i].handler == reset_cursor) { |
| 383 | if (timeouts[i].active) |
| 384 | cursor = CURSOR_ARROW; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | } else { |
| 389 | if (tns.cnt != filecnt) |
| 390 | cursor = CURSOR_WATCH; |
| 391 | else |
| 392 | cursor = CURSOR_ARROW; |
| 393 | } |
| 394 | win_set_cursor(&win, cursor); |
| 395 | } |
| 396 | |
| 397 | void animate(void) |
| 398 | { |
| 399 | if (img_frame_animate(&img, false)) { |
| 400 | redraw(); |
| 401 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void clear_resize(void) |
| 406 | { |
| 407 | resized = false; |
| 408 | } |
| 409 | |
| 410 | bool keymask(const keymap_t *k, unsigned int state) |
| 411 | { |
| 412 | return (k->ctrl ? ControlMask : 0) == (state & ControlMask); |
| 413 | } |
| 414 | |
| 415 | bool buttonmask(const button_t *b, unsigned int state) |
| 416 | { |
| 417 | return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) == |
| 418 | (state & (ControlMask | ShiftMask)); |
| 419 | } |
| 420 | |
| 421 | void on_keypress(XKeyEvent *kev) |
| 422 | { |
| 423 | int i; |
| 424 | KeySym ksym; |
| 425 | char key; |
| 426 | |
| 427 | if (kev == NULL) |
| 428 | return; |
| 429 | |
| 430 | XLookupString(kev, &key, 1, &ksym, NULL); |
| 431 | |
| 432 | if ((ksym == XK_Escape || (key >= '0' && key <= '9')) && |
| 433 | (kev->state & ControlMask) == 0) |
| 434 | { |
| 435 | /* number prefix for commands */ |
| 436 | prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0'); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | for (i = 0; i < ARRLEN(keys); i++) { |
| 441 | if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) { |
| 442 | if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg)) |
| 443 | redraw(); |
| 444 | prefix = 0; |
| 445 | return; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void on_buttonpress(XButtonEvent *bev) |
| 451 | { |
| 452 | int i, sel; |
| 453 | |
| 454 | if (bev == NULL) |
| 455 | return; |
| 456 | |
| 457 | if (mode == MODE_IMAGE) { |
| 458 | win_set_cursor(&win, CURSOR_ARROW); |
| 459 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 460 | |
| 461 | for (i = 0; i < ARRLEN(buttons); i++) { |
| 462 | if (buttons[i].button == bev->button && |
| 463 | buttonmask(&buttons[i], bev->state)) |
| 464 | { |
| 465 | if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg)) |
| 466 | redraw(); |
| 467 | return; |
| 468 | } |
| 469 | } |
| 470 | } else { |
| 471 | /* thumbnail mode (hard-coded) */ |
| 472 | switch (bev->button) { |
| 473 | case Button1: |
| 474 | if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { |
| 475 | if (sel == tns.sel) { |
| 476 | mode = MODE_IMAGE; |
| 477 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 478 | load_image(tns.sel); |
| 479 | } else { |
| 480 | tns_highlight(&tns, tns.sel, false); |
| 481 | tns_highlight(&tns, sel, true); |
| 482 | tns.sel = sel; |
| 483 | } |
| 484 | redraw(); |
| 485 | break; |
| 486 | } |
| 487 | break; |
| 488 | case Button4: |
| 489 | case Button5: |
| 490 | if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN, |
| 491 | (bev->state & ControlMask) != 0)) |
| 492 | redraw(); |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void run(void) |
| 499 | { |
| 500 | int xfd; |
| 501 | fd_set fds; |
| 502 | struct timeval timeout; |
| 503 | bool discard, to_set; |
| 504 | XEvent ev, nextev; |
| 505 | |
| 506 | redraw(); |
| 507 | |
| 508 | while (true) { |
| 509 | while (mode == MODE_THUMB && tns.cnt < filecnt && |
| 510 | XPending(win.env.dpy) == 0) |
| 511 | { |
| 512 | /* load thumbnails */ |
| 513 | set_timeout(redraw, TO_REDRAW_THUMBS, false); |
| 514 | if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) { |
| 515 | tns.cnt++; |
| 516 | } else { |
| 517 | remove_file(tns.cnt, false); |
| 518 | if (tns.sel >= tns.cnt) |
| 519 | tns.sel--; |
| 520 | } |
| 521 | if (tns.cnt == filecnt) |
| 522 | redraw(); |
| 523 | else |
| 524 | check_timeouts(NULL); |
| 525 | } |
| 526 | |
| 527 | while (XPending(win.env.dpy) == 0 |
| 528 | && ((to_set = check_timeouts(&timeout)) || info.fd != -1)) |
| 529 | { |
| 530 | /* check for timeouts & input */ |
| 531 | xfd = ConnectionNumber(win.env.dpy); |
| 532 | FD_ZERO(&fds); |
| 533 | FD_SET(xfd, &fds); |
| 534 | if (info.fd != -1) { |
| 535 | FD_SET(info.fd, &fds); |
| 536 | xfd = MAX(xfd, info.fd); |
| 537 | } |
| 538 | select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL); |
| 539 | if (FD_ISSET(info.fd, &fds)) |
| 540 | read_info(); |
| 541 | } |
| 542 | |
| 543 | do { |
| 544 | XNextEvent(win.env.dpy, &ev); |
| 545 | discard = false; |
| 546 | if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) { |
| 547 | XPeekEvent(win.env.dpy, &nextev); |
| 548 | switch (ev.type) { |
| 549 | case ConfigureNotify: |
| 550 | discard = ev.type == nextev.type; |
| 551 | break; |
| 552 | case KeyPress: |
| 553 | discard = (nextev.type == KeyPress || nextev.type == KeyRelease) |
| 554 | && ev.xkey.keycode == nextev.xkey.keycode; |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | } while (discard); |
| 559 | |
| 560 | switch (ev.type) { |
| 561 | /* handle events */ |
| 562 | case ButtonPress: |
| 563 | on_buttonpress(&ev.xbutton); |
| 564 | break; |
| 565 | case ClientMessage: |
| 566 | if ((Atom) ev.xclient.data.l[0] == wm_delete_win) |
| 567 | return; |
| 568 | break; |
| 569 | case ConfigureNotify: |
| 570 | if (win_configure(&win, &ev.xconfigure)) { |
| 571 | if (mode == MODE_IMAGE) { |
| 572 | img.dirty = true; |
| 573 | img.checkpan = true; |
| 574 | } else { |
| 575 | tns.dirty = true; |
| 576 | } |
| 577 | if (!resized || win.fullscreen) { |
| 578 | redraw(); |
| 579 | set_timeout(clear_resize, TO_REDRAW_RESIZE, false); |
| 580 | resized = true; |
| 581 | } else { |
| 582 | set_timeout(redraw, TO_REDRAW_RESIZE, false); |
| 583 | } |
| 584 | } |
| 585 | break; |
| 586 | case Expose: |
| 587 | win_expose(&win, &ev.xexpose); |
| 588 | break; |
| 589 | case KeyPress: |
| 590 | on_keypress(&ev.xkey); |
| 591 | break; |
| 592 | case MotionNotify: |
| 593 | if (mode == MODE_IMAGE) { |
| 594 | win_set_cursor(&win, CURSOR_ARROW); |
| 595 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 596 | } |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | int fncmp(const void *a, const void *b) |
| 603 | { |
| 604 | return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name); |
| 605 | } |
| 606 | |
| 607 | int main(int argc, char **argv) |
| 608 | { |
| 609 | int i, start; |
| 610 | size_t n; |
| 611 | ssize_t len; |
| 612 | char *filename; |
| 613 | const char *homedir; |
| 614 | struct stat fstats; |
| 615 | r_dir_t dir; |
| 616 | |
| 617 | parse_options(argc, argv); |
| 618 | |
| 619 | if (options->clean_cache) { |
| 620 | tns_init(&tns, 0, NULL); |
| 621 | tns_clean_cache(&tns); |
| 622 | exit(EXIT_SUCCESS); |
| 623 | } |
| 624 | |
| 625 | if (options->filecnt == 0 && !options->from_stdin) { |
| 626 | print_usage(); |
| 627 | exit(EXIT_FAILURE); |
| 628 | } |
| 629 | |
| 630 | if (options->recursive || options->from_stdin) |
| 631 | filecnt = FILENAME_CNT; |
| 632 | else |
| 633 | filecnt = options->filecnt; |
| 634 | |
| 635 | files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); |
| 636 | fileidx = 0; |
| 637 | |
| 638 | if (options->from_stdin) { |
| 639 | filename = NULL; |
| 640 | while ((len = get_line(&filename, &n, stdin)) > 0) { |
| 641 | if (filename[len-1] == '\n') |
| 642 | filename[len-1] = '\0'; |
| 643 | check_add_file(filename); |
| 644 | } |
| 645 | if (filename != NULL) |
| 646 | free(filename); |
| 647 | } |
| 648 | |
| 649 | for (i = 0; i < options->filecnt; i++) { |
| 650 | filename = options->filenames[i]; |
| 651 | |
| 652 | if (stat(filename, &fstats) < 0) { |
| 653 | warn("could not stat file: %s", filename); |
| 654 | continue; |
| 655 | } |
| 656 | if (!S_ISDIR(fstats.st_mode)) { |
| 657 | check_add_file(filename); |
| 658 | } else { |
| 659 | if (!options->recursive) { |
| 660 | warn("ignoring directory: %s", filename); |
| 661 | continue; |
| 662 | } |
| 663 | if (r_opendir(&dir, filename) < 0) { |
| 664 | warn("could not open directory: %s", filename); |
| 665 | continue; |
| 666 | } |
| 667 | start = fileidx; |
| 668 | while ((filename = r_readdir(&dir)) != NULL) { |
| 669 | check_add_file(filename); |
| 670 | free((void*) filename); |
| 671 | } |
| 672 | r_closedir(&dir); |
| 673 | if (fileidx - start > 1) |
| 674 | qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (fileidx == 0) { |
| 679 | fprintf(stderr, "sxiv: no valid image file given, aborting\n"); |
| 680 | exit(EXIT_FAILURE); |
| 681 | } |
| 682 | |
| 683 | filecnt = fileidx; |
| 684 | fileidx = options->startnum < filecnt ? options->startnum : 0; |
| 685 | |
| 686 | win_init(&win); |
| 687 | img_init(&img, &win); |
| 688 | |
| 689 | if ((homedir = getenv("HOME")) == NULL) { |
| 690 | warn("could not locate home directory"); |
| 691 | } else { |
| 692 | len = strlen(homedir) + strlen(INFO_SCRIPT) + 2; |
| 693 | info.script = (char*) s_malloc(len); |
| 694 | snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT); |
| 695 | if (access(info.script, X_OK) != 0) { |
| 696 | free(info.script); |
| 697 | info.script = NULL; |
| 698 | } |
| 699 | } |
| 700 | info.fd = -1; |
| 701 | |
| 702 | if (options->thumb_mode) { |
| 703 | mode = MODE_THUMB; |
| 704 | tns_init(&tns, filecnt, &win); |
| 705 | while (!tns_load(&tns, 0, &files[0], false, false)) |
| 706 | remove_file(0, false); |
| 707 | tns.cnt = 1; |
| 708 | } else { |
| 709 | mode = MODE_IMAGE; |
| 710 | tns.thumbs = NULL; |
| 711 | load_image(fileidx); |
| 712 | } |
| 713 | |
| 714 | win_open(&win); |
| 715 | |
| 716 | run(); |
| 717 | cleanup(); |
| 718 | |
| 719 | return 0; |
| 720 | } |