| 1 | /* Copyright 2011, 2012 Bert Muennich |
| 2 | * |
| 3 | * This file is part of sxiv. |
| 4 | * |
| 5 | * sxiv 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 | * sxiv 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 sxiv. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #define _POSIX_C_SOURCE 200112L |
| 20 | #define _IMAGE_CONFIG |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #include "commands.h" |
| 28 | #include "image.h" |
| 29 | #include "options.h" |
| 30 | #include "thumbs.h" |
| 31 | #include "util.h" |
| 32 | #include "config.h" |
| 33 | |
| 34 | void cleanup(void); |
| 35 | void remove_file(int, bool); |
| 36 | void load_image(int); |
| 37 | void open_info(void); |
| 38 | void redraw(void); |
| 39 | void reset_cursor(void); |
| 40 | void animate(void); |
| 41 | void set_timeout(timeout_f, int, bool); |
| 42 | void reset_timeout(timeout_f); |
| 43 | |
| 44 | extern appmode_t mode; |
| 45 | extern img_t img; |
| 46 | extern tns_t tns; |
| 47 | extern win_t win; |
| 48 | |
| 49 | extern fileinfo_t *files; |
| 50 | extern int filecnt, fileidx; |
| 51 | extern int markcnt; |
| 52 | extern int alternate; |
| 53 | |
| 54 | extern int prefix; |
| 55 | |
| 56 | const int ss_delays[] = { |
| 57 | 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600 |
| 58 | }; |
| 59 | |
| 60 | bool it_quit(arg_t a) |
| 61 | { |
| 62 | unsigned int i; |
| 63 | |
| 64 | if (options->to_stdout && markcnt > 0) { |
| 65 | for (i = 0; i < filecnt; i++) { |
| 66 | if (files[i].marked) |
| 67 | printf("%s\n", files[i].name); |
| 68 | } |
| 69 | } |
| 70 | cleanup(); |
| 71 | exit(EXIT_SUCCESS); |
| 72 | } |
| 73 | |
| 74 | bool it_switch_mode(arg_t a) |
| 75 | { |
| 76 | if (mode == MODE_IMAGE) { |
| 77 | if (tns.thumbs == NULL) { |
| 78 | tns_init(&tns, filecnt, &win); |
| 79 | tns.alpha = img.alpha; |
| 80 | } |
| 81 | img_close(&img, false); |
| 82 | reset_timeout(reset_cursor); |
| 83 | tns.sel = fileidx; |
| 84 | tns.dirty = true; |
| 85 | mode = MODE_THUMB; |
| 86 | } else { |
| 87 | load_image(tns.sel); |
| 88 | mode = MODE_IMAGE; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool it_toggle_fullscreen(arg_t a) |
| 94 | { |
| 95 | win_toggle_fullscreen(&win); |
| 96 | /* redraw after next ConfigureNotify event */ |
| 97 | set_timeout(redraw, TO_REDRAW_RESIZE, false); |
| 98 | if (mode == MODE_IMAGE) |
| 99 | img.checkpan = img.dirty = true; |
| 100 | else |
| 101 | tns.dirty = true; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | bool it_toggle_bar(arg_t a) |
| 106 | { |
| 107 | win_toggle_bar(&win); |
| 108 | if (mode == MODE_IMAGE) { |
| 109 | img.checkpan = img.dirty = true; |
| 110 | if (win.bar.h > 0) |
| 111 | open_info(); |
| 112 | } else { |
| 113 | tns.dirty = true; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool t_reload_all(arg_t a) |
| 119 | { |
| 120 | if (mode == MODE_THUMB) { |
| 121 | tns_free(&tns); |
| 122 | tns_init(&tns, filecnt, &win); |
| 123 | return true; |
| 124 | } else { |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool it_reload_image(arg_t a) |
| 130 | { |
| 131 | if (mode == MODE_IMAGE) { |
| 132 | load_image(fileidx); |
| 133 | } else { |
| 134 | win_set_cursor(&win, CURSOR_WATCH); |
| 135 | if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) { |
| 136 | remove_file(tns.sel, false); |
| 137 | tns.dirty = true; |
| 138 | if (tns.sel >= tns.cnt) |
| 139 | tns.sel = tns.cnt - 1; |
| 140 | } |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool it_remove_image(arg_t a) |
| 146 | { |
| 147 | if (mode == MODE_IMAGE) { |
| 148 | remove_file(fileidx, true); |
| 149 | load_image(fileidx >= filecnt ? filecnt - 1 : fileidx); |
| 150 | return true; |
| 151 | } else if (tns.sel < tns.cnt) { |
| 152 | remove_file(tns.sel, true); |
| 153 | tns.dirty = true; |
| 154 | if (tns.sel >= tns.cnt) |
| 155 | tns.sel = tns.cnt - 1; |
| 156 | return true; |
| 157 | } else { |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | bool i_navigate(arg_t a) |
| 163 | { |
| 164 | long n = (long) a; |
| 165 | |
| 166 | if (mode == MODE_IMAGE) { |
| 167 | if (prefix > 0) |
| 168 | n *= prefix; |
| 169 | n += fileidx; |
| 170 | if (n < 0) |
| 171 | n = 0; |
| 172 | if (n >= filecnt) |
| 173 | n = filecnt - 1; |
| 174 | |
| 175 | if (n != fileidx) { |
| 176 | load_image(n); |
| 177 | return true; |
| 178 | } |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | bool i_alternate(arg_t a) |
| 184 | { |
| 185 | if (mode == MODE_IMAGE) { |
| 186 | load_image(alternate); |
| 187 | return true; |
| 188 | } else { |
| 189 | return false; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | bool it_first(arg_t a) |
| 194 | { |
| 195 | if (mode == MODE_IMAGE && fileidx != 0) { |
| 196 | load_image(0); |
| 197 | return true; |
| 198 | } else if (mode == MODE_THUMB && tns.sel != 0) { |
| 199 | tns.sel = 0; |
| 200 | tns.dirty = true; |
| 201 | return true; |
| 202 | } else { |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | bool it_n_or_last(arg_t a) |
| 208 | { |
| 209 | int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1; |
| 210 | |
| 211 | if (mode == MODE_IMAGE && fileidx != n) { |
| 212 | load_image(n); |
| 213 | return true; |
| 214 | } else if (mode == MODE_THUMB && tns.sel != n) { |
| 215 | tns.sel = n; |
| 216 | tns.dirty = true; |
| 217 | return true; |
| 218 | } else { |
| 219 | return false; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | bool i_navigate_frame(arg_t a) |
| 224 | { |
| 225 | if (mode == MODE_IMAGE && !img.multi.animate) |
| 226 | return img_frame_navigate(&img, (long) a); |
| 227 | else |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | bool i_toggle_animation(arg_t a) |
| 232 | { |
| 233 | if (mode != MODE_IMAGE) |
| 234 | return false; |
| 235 | |
| 236 | if (img.multi.animate) { |
| 237 | reset_timeout(animate); |
| 238 | img.multi.animate = false; |
| 239 | } else if (img_frame_animate(&img, true)) { |
| 240 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 241 | } |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | bool it_toggle_image_mark(arg_t a) |
| 246 | { |
| 247 | int sel = mode == MODE_IMAGE ? fileidx : tns.sel; |
| 248 | |
| 249 | files[sel].marked = !files[sel].marked; |
| 250 | markcnt += files[sel].marked ? 1 : -1; |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | bool it_navigate_marked(arg_t a) |
| 255 | { |
| 256 | long n = (long) a; |
| 257 | int d, i, cnt, sel, new; |
| 258 | |
| 259 | if (mode == MODE_IMAGE) |
| 260 | cnt = filecnt, sel = new = fileidx; |
| 261 | else |
| 262 | cnt = tns.cnt, sel = new = tns.sel; |
| 263 | if (prefix > 0) |
| 264 | n *= prefix; |
| 265 | d = n > 0 ? 1 : -1; |
| 266 | for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) { |
| 267 | if (files[i].marked) { |
| 268 | n -= d; |
| 269 | new = i; |
| 270 | } |
| 271 | } |
| 272 | if (new != sel) { |
| 273 | if (mode == MODE_IMAGE) { |
| 274 | load_image(new); |
| 275 | } else { |
| 276 | tns.sel = new; |
| 277 | tns.dirty = true; |
| 278 | } |
| 279 | return true; |
| 280 | } else { |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | bool it_scroll_move(arg_t a) |
| 286 | { |
| 287 | direction_t dir = (direction_t) a; |
| 288 | |
| 289 | if (mode == MODE_IMAGE) |
| 290 | return img_pan(&img, dir, prefix); |
| 291 | else |
| 292 | return tns_move_selection(&tns, dir, prefix); |
| 293 | } |
| 294 | |
| 295 | bool it_scroll_screen(arg_t a) |
| 296 | { |
| 297 | direction_t dir = (direction_t) a; |
| 298 | |
| 299 | if (mode == MODE_IMAGE) |
| 300 | return img_pan(&img, dir, -1); |
| 301 | else |
| 302 | return tns_scroll(&tns, dir, true); |
| 303 | } |
| 304 | |
| 305 | bool i_scroll_to_edge(arg_t a) |
| 306 | { |
| 307 | direction_t dir = (direction_t) a; |
| 308 | |
| 309 | if (mode == MODE_IMAGE) |
| 310 | return img_pan_edge(&img, dir); |
| 311 | else |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | /* Xlib helper function for i_drag() */ |
| 316 | Bool is_motionnotify(Display *d, XEvent *e, XPointer a) |
| 317 | { |
| 318 | return e != NULL && e->type == MotionNotify; |
| 319 | } |
| 320 | |
| 321 | #define WARP(x,y) \ |
| 322 | XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \ |
| 323 | ox = x, oy = y; \ |
| 324 | break |
| 325 | |
| 326 | bool i_drag(arg_t a) |
| 327 | { |
| 328 | int dx = 0, dy = 0, i, ox, oy, x, y; |
| 329 | unsigned int ui; |
| 330 | bool dragging = true, next = false; |
| 331 | XEvent e; |
| 332 | Window w; |
| 333 | |
| 334 | if (mode != MODE_IMAGE) |
| 335 | return false; |
| 336 | if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui)) |
| 337 | return false; |
| 338 | |
| 339 | win_set_cursor(&win, CURSOR_HAND); |
| 340 | |
| 341 | while (dragging) { |
| 342 | if (!next) |
| 343 | XMaskEvent(win.env.dpy, |
| 344 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e); |
| 345 | switch (e.type) { |
| 346 | case ButtonPress: |
| 347 | case ButtonRelease: |
| 348 | dragging = false; |
| 349 | break; |
| 350 | case MotionNotify: |
| 351 | x = e.xmotion.x; |
| 352 | y = e.xmotion.y; |
| 353 | |
| 354 | /* wrap the mouse around */ |
| 355 | if (x <= 0) { |
| 356 | WARP(win.w - 2, y); |
| 357 | } else if (x >= win.w - 1) { |
| 358 | WARP(1, y); |
| 359 | } else if (y <= 0) { |
| 360 | WARP(x, win.h - 2); |
| 361 | } else if (y >= win.h - 1) { |
| 362 | WARP(x, 1); |
| 363 | } |
| 364 | dx += x - ox; |
| 365 | dy += y - oy; |
| 366 | ox = x; |
| 367 | oy = y; |
| 368 | break; |
| 369 | } |
| 370 | if (dragging) |
| 371 | next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None); |
| 372 | if ((!dragging || !next) && (dx != 0 || dy != 0)) { |
| 373 | if (img_move(&img, dx, dy)) { |
| 374 | img_render(&img); |
| 375 | win_draw(&win); |
| 376 | } |
| 377 | dx = dy = 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | win_set_cursor(&win, CURSOR_ARROW); |
| 382 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 383 | reset_timeout(redraw); |
| 384 | |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | bool i_zoom(arg_t a) |
| 389 | { |
| 390 | long scale = (long) a; |
| 391 | |
| 392 | if (mode != MODE_IMAGE) |
| 393 | return false; |
| 394 | |
| 395 | if (scale > 0) |
| 396 | return img_zoom_in(&img); |
| 397 | else if (scale < 0) |
| 398 | return img_zoom_out(&img); |
| 399 | else |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | bool i_set_zoom(arg_t a) |
| 404 | { |
| 405 | if (mode == MODE_IMAGE) |
| 406 | return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0); |
| 407 | else |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | bool i_fit_to_win(arg_t a) |
| 412 | { |
| 413 | bool ret = false; |
| 414 | scalemode_t sm = (scalemode_t) a; |
| 415 | |
| 416 | if (mode == MODE_IMAGE) { |
| 417 | if ((ret = img_fit_win(&img, sm))) |
| 418 | img_center(&img); |
| 419 | } |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | bool i_fit_to_img(arg_t a) |
| 424 | { |
| 425 | int x, y; |
| 426 | unsigned int w, h; |
| 427 | bool ret = false; |
| 428 | |
| 429 | if (mode == MODE_IMAGE) { |
| 430 | x = MAX(0, win.x + img.x); |
| 431 | y = MAX(0, win.y + img.y); |
| 432 | w = img.w * img.zoom; |
| 433 | h = img.h * img.zoom; |
| 434 | if ((ret = win_moveresize(&win, x, y, w, h))) { |
| 435 | img.x = x - win.x; |
| 436 | img.y = y - win.y; |
| 437 | img.dirty = true; |
| 438 | } |
| 439 | } |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | bool i_rotate(arg_t a) |
| 444 | { |
| 445 | degree_t degree = (degree_t) a; |
| 446 | |
| 447 | if (mode == MODE_IMAGE) { |
| 448 | img_rotate(&img, degree); |
| 449 | return true; |
| 450 | } else { |
| 451 | return false; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | bool i_flip(arg_t a) |
| 456 | { |
| 457 | flipdir_t dir = (flipdir_t) a; |
| 458 | |
| 459 | if (mode == MODE_IMAGE) { |
| 460 | img_flip(&img, dir); |
| 461 | return true; |
| 462 | } else { |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | bool i_toggle_antialias(arg_t a) |
| 468 | { |
| 469 | if (mode == MODE_IMAGE) { |
| 470 | img_toggle_antialias(&img); |
| 471 | return true; |
| 472 | } else { |
| 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | bool it_toggle_alpha(arg_t a) |
| 478 | { |
| 479 | img.alpha = tns.alpha = !img.alpha; |
| 480 | if (mode == MODE_IMAGE) |
| 481 | img.dirty = true; |
| 482 | else |
| 483 | tns.dirty = true; |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | bool it_open_with(arg_t a) |
| 488 | { |
| 489 | const char *prog = (const char*) a; |
| 490 | pid_t pid; |
| 491 | |
| 492 | if (prog == NULL || *prog == '\0') |
| 493 | return false; |
| 494 | |
| 495 | if ((pid = fork()) == 0) { |
| 496 | execlp(prog, prog, |
| 497 | files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL); |
| 498 | warn("could not exec: %s", prog); |
| 499 | exit(EXIT_FAILURE); |
| 500 | } else if (pid < 0) { |
| 501 | warn("could not fork. program was: %s", prog); |
| 502 | } |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | bool it_shell_cmd(arg_t a) |
| 507 | { |
| 508 | int n, status; |
| 509 | const char *cmdline = (const char*) a; |
| 510 | pid_t pid; |
| 511 | |
| 512 | if (cmdline == NULL || *cmdline == '\0') |
| 513 | return false; |
| 514 | |
| 515 | n = mode == MODE_IMAGE ? fileidx : tns.sel; |
| 516 | |
| 517 | if (setenv("SXIV_IMG", files[n].path, 1) < 0) { |
| 518 | warn("could not set env.-variable: SXIV_IMG. command line was: %s", |
| 519 | cmdline); |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | if ((pid = fork()) == 0) { |
| 524 | execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL); |
| 525 | warn("could not exec: /bin/sh. command line was: %s", cmdline); |
| 526 | exit(EXIT_FAILURE); |
| 527 | } else if (pid < 0) { |
| 528 | warn("could not fork. command line was: %s", cmdline); |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | win_set_cursor(&win, CURSOR_WATCH); |
| 533 | |
| 534 | waitpid(pid, &status, 0); |
| 535 | if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) |
| 536 | warn("child exited with non-zero return value: %d. command line was: %s", |
| 537 | WEXITSTATUS(status), cmdline); |
| 538 | |
| 539 | if (mode == MODE_IMAGE) { |
| 540 | img_close(&img, true); |
| 541 | load_image(fileidx); |
| 542 | } |
| 543 | if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) && |
| 544 | mode == MODE_THUMB) |
| 545 | { |
| 546 | remove_file(tns.sel, false); |
| 547 | tns.dirty = true; |
| 548 | if (tns.sel >= tns.cnt) |
| 549 | tns.sel = tns.cnt - 1; |
| 550 | } |
| 551 | return true; |
| 552 | } |