| 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 |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (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., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <X11/Xutil.h> |
| 26 | #include <X11/keysym.h> |
| 27 | |
| 28 | #include "commands.h" |
| 29 | #include "image.h" |
| 30 | #include "options.h" |
| 31 | #include "thumbs.h" |
| 32 | #include "types.h" |
| 33 | #include "util.h" |
| 34 | #include "window.h" |
| 35 | |
| 36 | #define _MAPPINGS_CONFIG |
| 37 | #include "config.h" |
| 38 | |
| 39 | enum { |
| 40 | TITLE_LEN = 256, |
| 41 | FNAME_CNT = 1024 |
| 42 | }; |
| 43 | |
| 44 | typedef struct { |
| 45 | struct timeval when; |
| 46 | Bool active; |
| 47 | timeout_f handler; |
| 48 | } timeout_t; |
| 49 | |
| 50 | /* timeout handler functions: */ |
| 51 | void redraw(); |
| 52 | void reset_cursor(); |
| 53 | void animate(); |
| 54 | |
| 55 | appmode_t mode; |
| 56 | img_t img; |
| 57 | tns_t tns; |
| 58 | win_t win; |
| 59 | |
| 60 | fileinfo_t *files; |
| 61 | int filecnt, fileidx; |
| 62 | size_t filesize; |
| 63 | |
| 64 | char win_title[TITLE_LEN]; |
| 65 | |
| 66 | timeout_t timeouts[] = { |
| 67 | { { 0, 0 }, False, redraw }, |
| 68 | { { 0, 0 }, False, reset_cursor }, |
| 69 | { { 0, 0 }, False, animate } |
| 70 | }; |
| 71 | |
| 72 | void cleanup() { |
| 73 | static int in = 0; |
| 74 | |
| 75 | if (!in++) { |
| 76 | img_close(&img, 0); |
| 77 | tns_free(&tns); |
| 78 | win_close(&win); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void check_add_file(char *filename) { |
| 83 | if (!filename || !*filename) |
| 84 | return; |
| 85 | |
| 86 | if (access(filename, R_OK)) { |
| 87 | warn("could not open file: %s", filename); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (fileidx == filecnt) { |
| 92 | filecnt *= 2; |
| 93 | files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t)); |
| 94 | } |
| 95 | if (*filename != '/') { |
| 96 | files[fileidx].path = absolute_path(filename); |
| 97 | if (!files[fileidx].path) { |
| 98 | warn("could not get absolute path of file: %s\n", filename); |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | files[fileidx].name = s_strdup(filename); |
| 103 | if (*filename == '/') |
| 104 | files[fileidx].path = files[fileidx].name; |
| 105 | fileidx++; |
| 106 | } |
| 107 | |
| 108 | void remove_file(int n, unsigned char silent) { |
| 109 | if (n < 0 || n >= filecnt) |
| 110 | return; |
| 111 | |
| 112 | if (filecnt == 1) { |
| 113 | if (!silent) |
| 114 | fprintf(stderr, "sxiv: no more files to display, aborting\n"); |
| 115 | cleanup(); |
| 116 | exit(!silent); |
| 117 | } |
| 118 | |
| 119 | if (files[n].path != files[n].name) |
| 120 | free((void*) files[n].path); |
| 121 | free((void*) files[n].name); |
| 122 | |
| 123 | if (n + 1 < filecnt) |
| 124 | memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t)); |
| 125 | if (n + 1 < tns.cnt) { |
| 126 | memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) * |
| 127 | sizeof(thumb_t)); |
| 128 | memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t)); |
| 129 | } |
| 130 | |
| 131 | filecnt--; |
| 132 | if (n < tns.cnt) |
| 133 | tns.cnt--; |
| 134 | } |
| 135 | |
| 136 | void set_timeout(timeout_f handler, int time, int overwrite) { |
| 137 | int i; |
| 138 | |
| 139 | for (i = 0; i < LEN(timeouts); i++) { |
| 140 | if (timeouts[i].handler == handler) { |
| 141 | if (!timeouts[i].active || overwrite) { |
| 142 | gettimeofday(&timeouts[i].when, 0); |
| 143 | MSEC_ADD_TO_TIMEVAL(time, &timeouts[i].when); |
| 144 | timeouts[i].active = True; |
| 145 | } |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void reset_timeout(timeout_f handler) { |
| 152 | int i; |
| 153 | |
| 154 | for (i = 0; i < LEN(timeouts); i++) { |
| 155 | if (timeouts[i].handler == handler) { |
| 156 | timeouts[i].active = False; |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | int check_timeouts(struct timeval *t) { |
| 163 | int i = 0, tdiff, tmin = -1; |
| 164 | struct timeval now; |
| 165 | |
| 166 | gettimeofday(&now, 0); |
| 167 | while (i < LEN(timeouts)) { |
| 168 | if (timeouts[i].active) { |
| 169 | tdiff = TIMEDIFF(&timeouts[i].when, &now); |
| 170 | if (tdiff <= 0) { |
| 171 | timeouts[i].active = False; |
| 172 | if (timeouts[i].handler) |
| 173 | timeouts[i].handler(); |
| 174 | i = tmin = -1; |
| 175 | } else if (tmin < 0 || tdiff < tmin) { |
| 176 | tmin = tdiff; |
| 177 | } |
| 178 | } |
| 179 | i++; |
| 180 | } |
| 181 | if (tmin > 0 && t) |
| 182 | MSEC_TO_TIMEVAL(tmin, t); |
| 183 | return tmin > 0; |
| 184 | } |
| 185 | |
| 186 | void load_image(int new) { |
| 187 | struct stat fstats; |
| 188 | |
| 189 | if (new < 0 || new >= filecnt) |
| 190 | return; |
| 191 | |
| 192 | win_set_cursor(&win, CURSOR_WATCH); |
| 193 | |
| 194 | img_close(&img, 0); |
| 195 | while (!img_load(&img, &files[new])) { |
| 196 | remove_file(new, 0); |
| 197 | if (new >= filecnt) |
| 198 | new = filecnt - 1; |
| 199 | } |
| 200 | |
| 201 | fileidx = new; |
| 202 | if (!stat(files[new].path, &fstats)) |
| 203 | filesize = fstats.st_size; |
| 204 | else |
| 205 | filesize = 0; |
| 206 | |
| 207 | if (img.multi.cnt) { |
| 208 | if (img.multi.animate) |
| 209 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, 1); |
| 210 | else |
| 211 | reset_timeout(animate); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void update_title() { |
| 216 | int n; |
| 217 | float size; |
| 218 | const char *unit; |
| 219 | |
| 220 | if (mode == MODE_THUMB) { |
| 221 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s", |
| 222 | tns.cnt ? tns.sel + 1 : 0, tns.cnt, |
| 223 | tns.cnt ? files[tns.sel].name : ""); |
| 224 | } else { |
| 225 | size = filesize; |
| 226 | size_readable(&size, &unit); |
| 227 | if (img.multi.cnt) |
| 228 | n = snprintf(win_title, TITLE_LEN, |
| 229 | "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s", |
| 230 | fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w, |
| 231 | img.h, size, unit, img.multi.sel + 1, img.multi.cnt, |
| 232 | files[fileidx].name); |
| 233 | else |
| 234 | n = snprintf(win_title, TITLE_LEN, |
| 235 | "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s", |
| 236 | fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w, |
| 237 | img.h, size, unit, files[fileidx].name); |
| 238 | } |
| 239 | |
| 240 | if (n >= TITLE_LEN) { |
| 241 | for (n = 0; n < 3; n++) |
| 242 | win_title[TITLE_LEN - n - 2] = '.'; |
| 243 | } |
| 244 | |
| 245 | win_set_title(&win, win_title); |
| 246 | } |
| 247 | |
| 248 | void redraw() { |
| 249 | if (mode == MODE_IMAGE) |
| 250 | img_render(&img, &win); |
| 251 | else |
| 252 | tns_render(&tns, &win); |
| 253 | update_title(); |
| 254 | reset_timeout(redraw); |
| 255 | reset_cursor(); |
| 256 | } |
| 257 | |
| 258 | void reset_cursor() { |
| 259 | int i; |
| 260 | cursor_t cursor = CURSOR_NONE; |
| 261 | |
| 262 | if (mode == MODE_IMAGE) { |
| 263 | for (i = 0; i < LEN(timeouts); i++) { |
| 264 | if (timeouts[i].handler == reset_cursor) { |
| 265 | if (timeouts[i].active) |
| 266 | cursor = CURSOR_ARROW; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | } else { |
| 271 | if (tns.cnt != filecnt) |
| 272 | cursor = CURSOR_WATCH; |
| 273 | else |
| 274 | cursor = CURSOR_ARROW; |
| 275 | } |
| 276 | win_set_cursor(&win, cursor); |
| 277 | } |
| 278 | |
| 279 | void animate() { |
| 280 | int delay; |
| 281 | |
| 282 | delay = img_frame_animate(&img, 0); |
| 283 | if (delay) { |
| 284 | set_timeout(animate, delay, 1); |
| 285 | redraw(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | Bool keymask(const keymap_t *k, unsigned int state) { |
| 290 | return (k->ctrl ? ControlMask : 0) == (state & ControlMask); |
| 291 | } |
| 292 | |
| 293 | Bool buttonmask(const button_t *b, unsigned int state) { |
| 294 | return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) == |
| 295 | (state & (ControlMask | ShiftMask)); |
| 296 | } |
| 297 | |
| 298 | void on_keypress(XKeyEvent *kev) { |
| 299 | int i; |
| 300 | KeySym ksym; |
| 301 | char key; |
| 302 | |
| 303 | if (!kev) |
| 304 | return; |
| 305 | |
| 306 | XLookupString(kev, &key, 1, &ksym, NULL); |
| 307 | |
| 308 | for (i = 0; i < LEN(keys); i++) { |
| 309 | if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) { |
| 310 | if (keys[i].cmd && keys[i].cmd(keys[i].arg)) |
| 311 | redraw(); |
| 312 | return; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void on_buttonpress(XButtonEvent *bev) { |
| 318 | int i, sel; |
| 319 | |
| 320 | if (!bev) |
| 321 | return; |
| 322 | |
| 323 | if (mode == MODE_IMAGE) { |
| 324 | win_set_cursor(&win, CURSOR_ARROW); |
| 325 | set_timeout(reset_cursor, TO_CURSOR_HIDE, 1); |
| 326 | |
| 327 | for (i = 0; i < LEN(buttons); i++) { |
| 328 | if (buttons[i].button == bev->button && |
| 329 | buttonmask(&buttons[i], bev->state)) |
| 330 | { |
| 331 | if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg)) |
| 332 | redraw(); |
| 333 | return; |
| 334 | } |
| 335 | } |
| 336 | } else { |
| 337 | /* thumbnail mode (hard-coded) */ |
| 338 | switch (bev->button) { |
| 339 | case Button1: |
| 340 | if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { |
| 341 | if (sel == tns.sel) { |
| 342 | mode = MODE_IMAGE; |
| 343 | set_timeout(reset_cursor, TO_CURSOR_HIDE, 1); |
| 344 | load_image(tns.sel); |
| 345 | redraw(); |
| 346 | } else { |
| 347 | tns_highlight(&tns, &win, tns.sel, False); |
| 348 | tns_highlight(&tns, &win, sel, True); |
| 349 | tns.sel = sel; |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | break; |
| 354 | case Button4: |
| 355 | case Button5: |
| 356 | if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN)) |
| 357 | redraw(); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void run() { |
| 364 | int xfd; |
| 365 | fd_set fds; |
| 366 | struct timeval timeout; |
| 367 | XEvent ev; |
| 368 | |
| 369 | redraw(); |
| 370 | |
| 371 | while (1) { |
| 372 | while (mode == MODE_THUMB && tns.cnt < filecnt && |
| 373 | !XPending(win.env.dpy)) |
| 374 | { |
| 375 | /* load thumbnails */ |
| 376 | set_timeout(redraw, TO_REDRAW_THUMBS, 0); |
| 377 | if (tns_load(&tns, tns.cnt, &files[tns.cnt], False, False)) |
| 378 | tns.cnt++; |
| 379 | else |
| 380 | remove_file(tns.cnt, 0); |
| 381 | if (tns.cnt == filecnt) |
| 382 | redraw(); |
| 383 | else |
| 384 | check_timeouts(NULL); |
| 385 | } |
| 386 | |
| 387 | while (!XPending(win.env.dpy) && check_timeouts(&timeout)) { |
| 388 | /* wait for timeouts */ |
| 389 | xfd = ConnectionNumber(win.env.dpy); |
| 390 | FD_ZERO(&fds); |
| 391 | FD_SET(xfd, &fds); |
| 392 | select(xfd + 1, &fds, 0, 0, &timeout); |
| 393 | } |
| 394 | |
| 395 | if (!XNextEvent(win.env.dpy, &ev)) { |
| 396 | /* handle events */ |
| 397 | switch (ev.type) { |
| 398 | case ButtonPress: |
| 399 | on_buttonpress(&ev.xbutton); |
| 400 | break; |
| 401 | case ClientMessage: |
| 402 | if ((Atom) ev.xclient.data.l[0] == wm_delete_win) |
| 403 | return; |
| 404 | break; |
| 405 | case ConfigureNotify: |
| 406 | if (win_configure(&win, &ev.xconfigure)) { |
| 407 | set_timeout(redraw, TO_REDRAW_RESIZE, 0); |
| 408 | if (mode == MODE_IMAGE) |
| 409 | img.checkpan = 1; |
| 410 | else |
| 411 | tns.dirty = 1; |
| 412 | } |
| 413 | break; |
| 414 | case KeyPress: |
| 415 | on_keypress(&ev.xkey); |
| 416 | break; |
| 417 | case MotionNotify: |
| 418 | if (mode == MODE_IMAGE) { |
| 419 | win_set_cursor(&win, CURSOR_ARROW); |
| 420 | set_timeout(reset_cursor, TO_CURSOR_HIDE, 1); |
| 421 | } |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | int fncmp(const void *a, const void *b) { |
| 429 | return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name); |
| 430 | } |
| 431 | |
| 432 | int main(int argc, char **argv) { |
| 433 | int i, len, start; |
| 434 | size_t n; |
| 435 | char *filename; |
| 436 | struct stat fstats; |
| 437 | r_dir_t dir; |
| 438 | |
| 439 | parse_options(argc, argv); |
| 440 | |
| 441 | if (options->clean_cache) { |
| 442 | tns_init(&tns, 0); |
| 443 | tns_clean_cache(&tns); |
| 444 | exit(0); |
| 445 | } |
| 446 | |
| 447 | if (!options->filecnt) { |
| 448 | print_usage(); |
| 449 | exit(1); |
| 450 | } |
| 451 | |
| 452 | if (options->recursive || options->from_stdin) |
| 453 | filecnt = FNAME_CNT; |
| 454 | else |
| 455 | filecnt = options->filecnt; |
| 456 | |
| 457 | files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); |
| 458 | fileidx = 0; |
| 459 | |
| 460 | /* build file list: */ |
| 461 | if (options->from_stdin) { |
| 462 | filename = NULL; |
| 463 | while ((len = getline(&filename, &n, stdin)) > 0) { |
| 464 | if (filename[len-1] == '\n') |
| 465 | filename[len-1] = '\0'; |
| 466 | check_add_file(filename); |
| 467 | } |
| 468 | } else { |
| 469 | for (i = 0; i < options->filecnt; i++) { |
| 470 | filename = options->filenames[i]; |
| 471 | |
| 472 | if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) { |
| 473 | check_add_file(filename); |
| 474 | } else { |
| 475 | if (!options->recursive) { |
| 476 | warn("ignoring directory: %s", filename); |
| 477 | continue; |
| 478 | } |
| 479 | if (r_opendir(&dir, filename)) { |
| 480 | warn("could not open directory: %s", filename); |
| 481 | continue; |
| 482 | } |
| 483 | start = fileidx; |
| 484 | printf("reading dir: %s\n", filename); |
| 485 | while ((filename = r_readdir(&dir))) { |
| 486 | check_add_file(filename); |
| 487 | free((void*) filename); |
| 488 | } |
| 489 | r_closedir(&dir); |
| 490 | if (fileidx - start > 1) |
| 491 | qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (!fileidx) { |
| 497 | fprintf(stderr, "sxiv: no valid image file given, aborting\n"); |
| 498 | exit(1); |
| 499 | } |
| 500 | |
| 501 | filecnt = fileidx; |
| 502 | fileidx = options->startnum < filecnt ? options->startnum : 0; |
| 503 | |
| 504 | win_init(&win); |
| 505 | img_init(&img, &win); |
| 506 | |
| 507 | if (options->thumbnails) { |
| 508 | mode = MODE_THUMB; |
| 509 | tns_init(&tns, filecnt); |
| 510 | while (!tns_load(&tns, 0, &files[0], False, False)) |
| 511 | remove_file(0, 0); |
| 512 | tns.cnt = 1; |
| 513 | } else { |
| 514 | mode = MODE_IMAGE; |
| 515 | tns.thumbs = NULL; |
| 516 | load_image(fileidx); |
| 517 | } |
| 518 | |
| 519 | win_open(&win); |
| 520 | |
| 521 | run(); |
| 522 | cleanup(); |
| 523 | |
| 524 | return 0; |
| 525 | } |