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