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