| 1 | /* sxiv: main.c |
| 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <dirent.h> |
| 23 | #include <sys/select.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <X11/Xlib.h> |
| 30 | #include <X11/Xutil.h> |
| 31 | #include <X11/keysym.h> |
| 32 | |
| 33 | #include "config.h" |
| 34 | #include "image.h" |
| 35 | #include "options.h" |
| 36 | #include "thumbs.h" |
| 37 | #include "util.h" |
| 38 | #include "window.h" |
| 39 | |
| 40 | #ifdef EXT_COMMANDS |
| 41 | #include "commands.h" |
| 42 | #endif |
| 43 | |
| 44 | typedef enum { |
| 45 | MODE_NORMAL = 0, |
| 46 | MODE_THUMBS |
| 47 | } appmode_t; |
| 48 | |
| 49 | void update_title(); |
| 50 | int check_append(const char*); |
| 51 | void read_dir_rec(const char*); |
| 52 | void run(); |
| 53 | |
| 54 | appmode_t mode; |
| 55 | img_t img; |
| 56 | tns_t tns; |
| 57 | win_t win; |
| 58 | |
| 59 | #define DNAME_CNT 512 |
| 60 | #define FNAME_CNT 1024 |
| 61 | const char **filenames; |
| 62 | int filecnt, fileidx; |
| 63 | size_t filesize; |
| 64 | |
| 65 | #define TITLE_LEN 256 |
| 66 | char win_title[TITLE_LEN]; |
| 67 | |
| 68 | void cleanup() { |
| 69 | static int in = 0; |
| 70 | |
| 71 | if (!in++) { |
| 72 | img_close(&img, 0); |
| 73 | img_free(&img); |
| 74 | tns_free(&tns, &win); |
| 75 | win_close(&win); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | int load_image(int new) { |
| 80 | struct stat fstats; |
| 81 | |
| 82 | if (new >= 0 && new < filecnt) { |
| 83 | img_close(&img, 0); |
| 84 | fileidx = new; |
| 85 | if (!stat(filenames[fileidx], &fstats)) |
| 86 | filesize = fstats.st_size; |
| 87 | else |
| 88 | filesize = 0; |
| 89 | return img_load(&img, filenames[fileidx]); |
| 90 | } else { |
| 91 | return 0; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | int main(int argc, char **argv) { |
| 96 | int i; |
| 97 | const char *filename; |
| 98 | struct stat fstats; |
| 99 | |
| 100 | parse_options(argc, argv); |
| 101 | |
| 102 | if (!options->filecnt) { |
| 103 | print_usage(); |
| 104 | exit(1); |
| 105 | } |
| 106 | |
| 107 | if (options->recursive || options->from_stdin) |
| 108 | filecnt = FNAME_CNT; |
| 109 | else |
| 110 | filecnt = options->filecnt; |
| 111 | |
| 112 | filenames = (const char**) s_malloc(filecnt * sizeof(const char*)); |
| 113 | fileidx = 0; |
| 114 | |
| 115 | if (options->from_stdin) { |
| 116 | while ((filename = readline(stdin))) { |
| 117 | if (!*filename || !check_append(filename)) |
| 118 | free((void*) filename); |
| 119 | } |
| 120 | } else { |
| 121 | for (i = 0; i < options->filecnt; ++i) { |
| 122 | filename = options->filenames[i]; |
| 123 | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { |
| 124 | if (options->recursive) |
| 125 | read_dir_rec(filename); |
| 126 | else |
| 127 | warn("ignoring directory: %s", filename); |
| 128 | } else { |
| 129 | check_append(filename); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | filecnt = fileidx; |
| 135 | fileidx = 0; |
| 136 | |
| 137 | if (!filecnt) { |
| 138 | fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); |
| 139 | exit(1); |
| 140 | } |
| 141 | |
| 142 | win_open(&win); |
| 143 | img_init(&img, &win); |
| 144 | |
| 145 | if (options->thumbnails) { |
| 146 | mode = MODE_THUMBS; |
| 147 | tns_init(&tns, filecnt); |
| 148 | win_clear(&win); |
| 149 | win_draw(&win); |
| 150 | } else { |
| 151 | mode = MODE_NORMAL; |
| 152 | tns.thumbs = NULL; |
| 153 | load_image(fileidx); |
| 154 | img_render(&img, &win); |
| 155 | } |
| 156 | |
| 157 | update_title(); |
| 158 | run(); |
| 159 | cleanup(); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | void update_title() { |
| 165 | int n; |
| 166 | float size; |
| 167 | const char *unit; |
| 168 | |
| 169 | if (mode == MODE_THUMBS) { |
| 170 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s", |
| 171 | tns.cnt ? tns.sel + 1 : 0, tns.cnt, |
| 172 | tns.cnt ? filenames[tns.sel] : ""); |
| 173 | } else { |
| 174 | if (img.im) { |
| 175 | size = filesize; |
| 176 | size_readable(&size, &unit); |
| 177 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s", |
| 178 | fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit, |
| 179 | filenames[fileidx]); |
| 180 | } else { |
| 181 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] broken: %s", |
| 182 | fileidx + 1, filecnt, filenames[fileidx]); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (n >= TITLE_LEN) { |
| 187 | win_title[TITLE_LEN - 2] = '.'; |
| 188 | win_title[TITLE_LEN - 3] = '.'; |
| 189 | win_title[TITLE_LEN - 4] = '.'; |
| 190 | } |
| 191 | |
| 192 | win_set_title(&win, win_title); |
| 193 | } |
| 194 | |
| 195 | int check_append(const char *filename) { |
| 196 | if (!filename) |
| 197 | return 0; |
| 198 | |
| 199 | if (img_check(filename)) { |
| 200 | if (fileidx == filecnt) { |
| 201 | filecnt *= 2; |
| 202 | filenames = (const char**) s_realloc(filenames, |
| 203 | filecnt * sizeof(const char*)); |
| 204 | } |
| 205 | filenames[fileidx++] = filename; |
| 206 | return 1; |
| 207 | } else { |
| 208 | return 0; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | int fncmp(const void *a, const void *b) { |
| 213 | return strcoll(*((char* const*) a), *((char* const*) b)); |
| 214 | } |
| 215 | |
| 216 | void read_dir_rec(const char *dirname) { |
| 217 | char *filename; |
| 218 | const char **dirnames; |
| 219 | int dircnt, diridx; |
| 220 | int fcnt, fstart; |
| 221 | unsigned char first; |
| 222 | size_t len; |
| 223 | DIR *dir; |
| 224 | struct dirent *dentry; |
| 225 | struct stat fstats; |
| 226 | |
| 227 | if (!dirname) |
| 228 | return; |
| 229 | |
| 230 | dircnt = DNAME_CNT; |
| 231 | diridx = first = 1; |
| 232 | dirnames = (const char**) s_malloc(dircnt * sizeof(const char*)); |
| 233 | dirnames[0] = dirname; |
| 234 | |
| 235 | fcnt = 0; |
| 236 | fstart = fileidx; |
| 237 | |
| 238 | while (diridx > 0) { |
| 239 | dirname = dirnames[--diridx]; |
| 240 | if (!(dir = opendir(dirname))) { |
| 241 | warn("could not open directory: %s", dirname); |
| 242 | } else { |
| 243 | while ((dentry = readdir(dir))) { |
| 244 | if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) |
| 245 | continue; |
| 246 | |
| 247 | len = strlen(dirname) + strlen(dentry->d_name) + 2; |
| 248 | filename = (char*) s_malloc(len * sizeof(char)); |
| 249 | snprintf(filename, len, "%s/%s", dirname, dentry->d_name); |
| 250 | |
| 251 | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { |
| 252 | if (diridx == dircnt) { |
| 253 | dircnt *= 2; |
| 254 | dirnames = (const char**) s_realloc(dirnames, |
| 255 | dircnt * sizeof(const char*)); |
| 256 | } |
| 257 | dirnames[diridx++] = filename; |
| 258 | } else { |
| 259 | if (check_append(filename)) |
| 260 | ++fcnt; |
| 261 | else |
| 262 | free(filename); |
| 263 | } |
| 264 | } |
| 265 | closedir(dir); |
| 266 | } |
| 267 | |
| 268 | if (!first) |
| 269 | free((void*) dirname); |
| 270 | else |
| 271 | first = 0; |
| 272 | } |
| 273 | |
| 274 | if (fcnt > 1) |
| 275 | qsort(filenames + fstart, fcnt, sizeof(char*), fncmp); |
| 276 | |
| 277 | free(dirnames); |
| 278 | } |
| 279 | |
| 280 | #ifdef EXT_COMMANDS |
| 281 | int run_command(const char *cline, Bool reload) { |
| 282 | int fncnt, fnlen; |
| 283 | char *cn, *cmdline; |
| 284 | const char *co, *fname; |
| 285 | pid_t pid; |
| 286 | int ret, status; |
| 287 | |
| 288 | if (!cline || !*cline) |
| 289 | return 0; |
| 290 | |
| 291 | fncnt = 0; |
| 292 | co = cline - 1; |
| 293 | while ((co = strchr(co + 1, '#'))) |
| 294 | ++fncnt; |
| 295 | |
| 296 | if (!fncnt) |
| 297 | return 0; |
| 298 | |
| 299 | ret = 0; |
| 300 | fname = filenames[mode == MODE_NORMAL ? fileidx : tns.sel]; |
| 301 | fnlen = strlen(fname); |
| 302 | cn = cmdline = (char*) s_malloc((strlen(cline) + fncnt * (fnlen + 2)) * |
| 303 | sizeof(char)); |
| 304 | |
| 305 | /* replace all '#' with filename */ |
| 306 | for (co = cline; *co; ++co) { |
| 307 | if (*co == '#') { |
| 308 | *cn++ = '"'; |
| 309 | strcpy(cn, fname); |
| 310 | cn += fnlen; |
| 311 | *cn++ = '"'; |
| 312 | } else { |
| 313 | *cn++ = *co; |
| 314 | } |
| 315 | } |
| 316 | *cn = '\0'; |
| 317 | |
| 318 | if ((pid = fork()) == 0) { |
| 319 | execlp("/bin/sh", "/bin/sh", "-c", cmdline, NULL); |
| 320 | warn("could not exec: /bin/sh"); |
| 321 | exit(1); |
| 322 | } else if (pid < 0) { |
| 323 | warn("could not fork. command line was: %s", cmdline); |
| 324 | } else if (reload) { |
| 325 | waitpid(pid, &status, 0); |
| 326 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) |
| 327 | ret = 1; |
| 328 | else |
| 329 | warn("child exited with non-zero return value: %d. command line was: %s", |
| 330 | WEXITSTATUS(status), cmdline); |
| 331 | } |
| 332 | |
| 333 | free(cmdline); |
| 334 | return ret; |
| 335 | } |
| 336 | #endif /* EXT_COMMANDS */ |
| 337 | |
| 338 | |
| 339 | /* event handling */ |
| 340 | |
| 341 | #define TO_WIN_RESIZE 75000; |
| 342 | #define TO_IMAGE_DRAG 1000; |
| 343 | #define TO_CURSOR_HIDE 1500000; |
| 344 | #define TO_THUMBS_LOAD 75000; |
| 345 | int timo_cursor; |
| 346 | int timo_redraw; |
| 347 | |
| 348 | unsigned char drag; |
| 349 | int mox, moy; |
| 350 | |
| 351 | void redraw() { |
| 352 | if (mode == MODE_NORMAL) |
| 353 | img_render(&img, &win); |
| 354 | else |
| 355 | tns_render(&tns, &win); |
| 356 | update_title(); |
| 357 | timo_redraw = 0; |
| 358 | } |
| 359 | |
| 360 | void on_keypress(XKeyEvent *kev) { |
| 361 | int x, y; |
| 362 | unsigned int w, h; |
| 363 | char key; |
| 364 | KeySym ksym; |
| 365 | int changed; |
| 366 | |
| 367 | if (!kev) |
| 368 | return; |
| 369 | |
| 370 | XLookupString(kev, &key, 1, &ksym, NULL); |
| 371 | changed = 0; |
| 372 | |
| 373 | #ifdef EXT_COMMANDS |
| 374 | /* external commands from commands.h */ |
| 375 | if (CLEANMASK(kev->state) & ControlMask) { |
| 376 | for (x = 0; x < LEN(commands); ++x) { |
| 377 | if (commands[x].ksym == ksym) { |
| 378 | win_set_cursor(&win, CURSOR_WATCH); |
| 379 | if (run_command(commands[x].cmdline, commands[x].reload)) { |
| 380 | if (mode == MODE_NORMAL) { |
| 381 | img_close(&img, 1); |
| 382 | load_image(fileidx); |
| 383 | tns_load(&tns, &win, fileidx, filenames[fileidx]); |
| 384 | } else { |
| 385 | tns_load(&tns, &win, tns.sel, filenames[tns.sel]); |
| 386 | } |
| 387 | redraw(); |
| 388 | } |
| 389 | win_set_cursor(&win, mode == MODE_NORMAL ? CURSOR_NONE : CURSOR_ARROW); |
| 390 | return; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | #endif |
| 395 | |
| 396 | if (mode == MODE_NORMAL) { |
| 397 | switch (ksym) { |
| 398 | /* navigate image list */ |
| 399 | case XK_n: |
| 400 | case XK_space: |
| 401 | if (fileidx + 1 < filecnt) |
| 402 | changed = load_image(fileidx + 1); |
| 403 | break; |
| 404 | case XK_p: |
| 405 | case XK_BackSpace: |
| 406 | if (fileidx > 0) |
| 407 | changed = load_image(fileidx - 1); |
| 408 | break; |
| 409 | case XK_bracketleft: |
| 410 | if (fileidx != 0) |
| 411 | changed = load_image(MAX(0, fileidx - 10)); |
| 412 | break; |
| 413 | case XK_bracketright: |
| 414 | if (fileidx != filecnt - 1) |
| 415 | changed = load_image(MIN(fileidx + 10, filecnt - 1)); |
| 416 | break; |
| 417 | case XK_g: |
| 418 | if (fileidx != 0) |
| 419 | changed = load_image(0); |
| 420 | break; |
| 421 | case XK_G: |
| 422 | if (fileidx != filecnt - 1) |
| 423 | changed = load_image(filecnt - 1); |
| 424 | break; |
| 425 | |
| 426 | /* zooming */ |
| 427 | case XK_plus: |
| 428 | case XK_equal: |
| 429 | changed = img_zoom_in(&img); |
| 430 | break; |
| 431 | case XK_minus: |
| 432 | changed = img_zoom_out(&img); |
| 433 | break; |
| 434 | case XK_0: |
| 435 | changed = img_zoom(&img, 1.0); |
| 436 | break; |
| 437 | case XK_w: |
| 438 | if ((changed = img_fit_win(&img, &win))) |
| 439 | img_center(&img, &win); |
| 440 | break; |
| 441 | |
| 442 | /* panning */ |
| 443 | case XK_h: |
| 444 | case XK_Left: |
| 445 | changed = img_pan(&img, &win, PAN_LEFT); |
| 446 | break; |
| 447 | case XK_j: |
| 448 | case XK_Down: |
| 449 | changed = img_pan(&img, &win, PAN_DOWN); |
| 450 | break; |
| 451 | case XK_k: |
| 452 | case XK_Up: |
| 453 | changed = img_pan(&img, &win, PAN_UP); |
| 454 | break; |
| 455 | case XK_l: |
| 456 | case XK_Right: |
| 457 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 458 | break; |
| 459 | |
| 460 | /* rotation */ |
| 461 | case XK_less: |
| 462 | img_rotate_left(&img, &win); |
| 463 | changed = 1; |
| 464 | break; |
| 465 | case XK_greater: |
| 466 | img_rotate_right(&img, &win); |
| 467 | changed = 1; |
| 468 | break; |
| 469 | |
| 470 | /* control window */ |
| 471 | case XK_W: |
| 472 | x = MAX(0, win.x + img.x); |
| 473 | y = MAX(0, win.y + img.y); |
| 474 | w = img.w * img.zoom; |
| 475 | h = img.h * img.zoom; |
| 476 | if ((changed = win_moveresize(&win, x, y, w, h))) { |
| 477 | img.x = x - win.x; |
| 478 | img.y = y - win.y; |
| 479 | } |
| 480 | break; |
| 481 | |
| 482 | /* switch to thumbnail mode */ |
| 483 | case XK_Return: |
| 484 | if (!tns.thumbs) |
| 485 | tns_init(&tns, filecnt); |
| 486 | img_close(&img, 0); |
| 487 | mode = MODE_THUMBS; |
| 488 | win_set_cursor(&win, CURSOR_ARROW); |
| 489 | timo_cursor = 0; |
| 490 | tns.sel = fileidx; |
| 491 | changed = tns.dirty = 1; |
| 492 | break; |
| 493 | |
| 494 | /* miscellaneous */ |
| 495 | case XK_a: |
| 496 | img_toggle_antialias(&img); |
| 497 | changed = 1; |
| 498 | break; |
| 499 | case XK_A: |
| 500 | img.alpha ^= 1; |
| 501 | changed = 1; |
| 502 | break; |
| 503 | case XK_r: |
| 504 | changed = load_image(fileidx); |
| 505 | break; |
| 506 | } |
| 507 | } else { |
| 508 | /* thumbnail mode */ |
| 509 | switch (ksym) { |
| 510 | /* open selected image */ |
| 511 | case XK_Return: |
| 512 | load_image(tns.sel); |
| 513 | mode = MODE_NORMAL; |
| 514 | win_set_cursor(&win, CURSOR_NONE); |
| 515 | changed = 1; |
| 516 | break; |
| 517 | |
| 518 | /* move selection */ |
| 519 | case XK_h: |
| 520 | case XK_Left: |
| 521 | changed = tns_move_selection(&tns, &win, TNS_LEFT); |
| 522 | break; |
| 523 | case XK_j: |
| 524 | case XK_Down: |
| 525 | changed = tns_move_selection(&tns, &win, TNS_DOWN); |
| 526 | break; |
| 527 | case XK_k: |
| 528 | case XK_Up: |
| 529 | changed = tns_move_selection(&tns, &win, TNS_UP); |
| 530 | break; |
| 531 | case XK_l: |
| 532 | case XK_Right: |
| 533 | changed = tns_move_selection(&tns, &win, TNS_RIGHT); |
| 534 | break; |
| 535 | case XK_g: |
| 536 | if (tns.sel != 0) { |
| 537 | tns.sel = 0; |
| 538 | changed = tns.dirty = 1; |
| 539 | } |
| 540 | break; |
| 541 | case XK_G: |
| 542 | if (tns.sel != tns.cnt - 1) { |
| 543 | tns.sel = tns.cnt - 1; |
| 544 | changed = tns.dirty = 1; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /* common key mappings */ |
| 550 | switch (ksym) { |
| 551 | case XK_Escape: |
| 552 | cleanup(); |
| 553 | exit(2); |
| 554 | case XK_q: |
| 555 | cleanup(); |
| 556 | exit(0); |
| 557 | |
| 558 | case XK_f: |
| 559 | win_toggle_fullscreen(&win); |
| 560 | /* render on next configurenotify */ |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | if (changed) |
| 565 | redraw(); |
| 566 | } |
| 567 | |
| 568 | void on_buttonpress(XButtonEvent *bev) { |
| 569 | int changed, sel; |
| 570 | unsigned int mask; |
| 571 | |
| 572 | if (!bev) |
| 573 | return; |
| 574 | |
| 575 | mask = CLEANMASK(bev->state); |
| 576 | changed = 0; |
| 577 | |
| 578 | if (mode == MODE_NORMAL) { |
| 579 | switch (bev->button) { |
| 580 | case Button1: |
| 581 | if (fileidx + 1 < filecnt) |
| 582 | changed = load_image(fileidx + 1); |
| 583 | break; |
| 584 | case Button2: |
| 585 | mox = bev->x; |
| 586 | moy = bev->y; |
| 587 | win_set_cursor(&win, CURSOR_HAND); |
| 588 | timo_cursor = 0; |
| 589 | drag = 1; |
| 590 | break; |
| 591 | case Button3: |
| 592 | if (fileidx > 0) |
| 593 | changed = load_image(fileidx - 1); |
| 594 | break; |
| 595 | case Button4: |
| 596 | if (mask == ControlMask) |
| 597 | changed = img_zoom_in(&img); |
| 598 | else if (mask == ShiftMask) |
| 599 | changed = img_pan(&img, &win, PAN_LEFT); |
| 600 | else |
| 601 | changed = img_pan(&img, &win, PAN_UP); |
| 602 | break; |
| 603 | case Button5: |
| 604 | if (mask == ControlMask) |
| 605 | changed = img_zoom_out(&img); |
| 606 | else if (mask == ShiftMask) |
| 607 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 608 | else |
| 609 | changed = img_pan(&img, &win, PAN_DOWN); |
| 610 | break; |
| 611 | case 6: |
| 612 | changed = img_pan(&img, &win, PAN_LEFT); |
| 613 | break; |
| 614 | case 7: |
| 615 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 616 | break; |
| 617 | } |
| 618 | } else { |
| 619 | /* thumbnail mode */ |
| 620 | switch (bev->button) { |
| 621 | case Button1: |
| 622 | if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { |
| 623 | if (sel == tns.sel) { |
| 624 | load_image(tns.sel); |
| 625 | mode = MODE_NORMAL; |
| 626 | timo_cursor = TO_CURSOR_HIDE; |
| 627 | } else { |
| 628 | tns_highlight(&tns, &win, tns.sel, False); |
| 629 | tns_highlight(&tns, &win, sel, True); |
| 630 | tns.sel = sel; |
| 631 | } |
| 632 | changed = 1; |
| 633 | break; |
| 634 | } |
| 635 | break; |
| 636 | case Button4: |
| 637 | changed = tns_scroll(&tns, TNS_UP); |
| 638 | break; |
| 639 | case Button5: |
| 640 | changed = tns_scroll(&tns, TNS_DOWN); |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | if (changed) |
| 646 | redraw(); |
| 647 | } |
| 648 | |
| 649 | void on_motionnotify(XMotionEvent *mev) { |
| 650 | if (!mev) |
| 651 | return; |
| 652 | |
| 653 | if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) { |
| 654 | if (img_move(&img, &win, mev->x - mox, mev->y - moy)) |
| 655 | timo_redraw = TO_IMAGE_DRAG; |
| 656 | |
| 657 | mox = mev->x; |
| 658 | moy = mev->y; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | void run() { |
| 663 | int xfd, timeout; |
| 664 | fd_set fds; |
| 665 | struct timeval tt, t0, t1; |
| 666 | XEvent ev; |
| 667 | |
| 668 | timo_cursor = timo_redraw = 0; |
| 669 | drag = 0; |
| 670 | |
| 671 | if (mode == MODE_NORMAL) |
| 672 | timo_cursor = TO_CURSOR_HIDE; |
| 673 | |
| 674 | while (1) { |
| 675 | if (mode == MODE_THUMBS && tns.cnt < filecnt) { |
| 676 | win_set_cursor(&win, CURSOR_WATCH); |
| 677 | gettimeofday(&t0, 0); |
| 678 | |
| 679 | while (!XPending(win.env.dpy) && tns.cnt < filecnt) { |
| 680 | /* tns.cnt is increased inside tns_load */ |
| 681 | tns_load(&tns, &win, tns.cnt, filenames[tns.cnt]); |
| 682 | gettimeofday(&t1, 0); |
| 683 | if (TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0) >= 0.25) |
| 684 | break; |
| 685 | } |
| 686 | if (tns.cnt == filecnt) |
| 687 | win_set_cursor(&win, CURSOR_ARROW); |
| 688 | if (!XPending(win.env.dpy)) { |
| 689 | redraw(); |
| 690 | continue; |
| 691 | } else { |
| 692 | timo_redraw = TO_THUMBS_LOAD; |
| 693 | } |
| 694 | } else if (timo_cursor || timo_redraw) { |
| 695 | gettimeofday(&t0, 0); |
| 696 | if (timo_cursor && timo_redraw) |
| 697 | timeout = MIN(timo_cursor, timo_redraw); |
| 698 | else if (timo_cursor) |
| 699 | timeout = timo_cursor; |
| 700 | else |
| 701 | timeout = timo_redraw; |
| 702 | tt.tv_sec = timeout / 1000000; |
| 703 | tt.tv_usec = timeout % 1000000; |
| 704 | xfd = ConnectionNumber(win.env.dpy); |
| 705 | FD_ZERO(&fds); |
| 706 | FD_SET(xfd, &fds); |
| 707 | |
| 708 | if (!XPending(win.env.dpy)) |
| 709 | select(xfd + 1, &fds, 0, 0, &tt); |
| 710 | gettimeofday(&t1, 0); |
| 711 | timeout = MIN((TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0)) * 1000000, timeout); |
| 712 | |
| 713 | /* timeouts fired? */ |
| 714 | if (timo_cursor) { |
| 715 | timo_cursor = MAX(0, timo_cursor - timeout); |
| 716 | if (!timo_cursor) |
| 717 | win_set_cursor(&win, CURSOR_NONE); |
| 718 | } |
| 719 | if (timo_redraw) { |
| 720 | timo_redraw = MAX(0, timo_redraw - timeout); |
| 721 | if (!timo_redraw) |
| 722 | redraw(); |
| 723 | } |
| 724 | if (!XPending(win.env.dpy) && (timo_cursor || timo_redraw)) |
| 725 | continue; |
| 726 | } |
| 727 | |
| 728 | if (!XNextEvent(win.env.dpy, &ev)) { |
| 729 | switch (ev.type) { |
| 730 | case KeyPress: |
| 731 | on_keypress(&ev.xkey); |
| 732 | break; |
| 733 | case ButtonPress: |
| 734 | on_buttonpress(&ev.xbutton); |
| 735 | break; |
| 736 | case ButtonRelease: |
| 737 | if (ev.xbutton.button == Button2) { |
| 738 | drag = 0; |
| 739 | if (mode == MODE_NORMAL) { |
| 740 | win_set_cursor(&win, CURSOR_ARROW); |
| 741 | timo_cursor = TO_CURSOR_HIDE; |
| 742 | } |
| 743 | } |
| 744 | break; |
| 745 | case MotionNotify: |
| 746 | if (drag) { |
| 747 | on_motionnotify(&ev.xmotion); |
| 748 | } else if (mode == MODE_NORMAL) { |
| 749 | if (!timo_cursor) |
| 750 | win_set_cursor(&win, CURSOR_ARROW); |
| 751 | timo_cursor = TO_CURSOR_HIDE; |
| 752 | } |
| 753 | break; |
| 754 | case ConfigureNotify: |
| 755 | if (win_configure(&win, &ev.xconfigure)) { |
| 756 | timo_redraw = TO_WIN_RESIZE; |
| 757 | if (mode == MODE_NORMAL) |
| 758 | img.checkpan = 1; |
| 759 | else |
| 760 | tns.dirty = 1; |
| 761 | } |
| 762 | break; |
| 763 | case ClientMessage: |
| 764 | if ((Atom) ev.xclient.data.l[0] == wm_delete_win) |
| 765 | return; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | } |