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