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