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