| 1 | /* sxiv: commands.c |
| 2 | * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the |
| 6 | * Free Software Foundation; either version 2 of the License, or (at your |
| 7 | * option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 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 "thumbs.h" |
| 30 | #include "util.h" |
| 31 | #include "config.h" |
| 32 | |
| 33 | void cleanup(void); |
| 34 | void remove_file(int, bool); |
| 35 | void load_image(int); |
| 36 | void redraw(void); |
| 37 | void reset_cursor(void); |
| 38 | void animate(void); |
| 39 | void set_timeout(timeout_f, int, bool); |
| 40 | void reset_timeout(timeout_f); |
| 41 | |
| 42 | extern appmode_t mode; |
| 43 | extern img_t img; |
| 44 | extern tns_t tns; |
| 45 | extern win_t win; |
| 46 | |
| 47 | extern fileinfo_t *files; |
| 48 | extern int filecnt, fileidx; |
| 49 | |
| 50 | extern int prefix; |
| 51 | |
| 52 | const int ss_delays[] = { |
| 53 | 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600 |
| 54 | }; |
| 55 | |
| 56 | bool it_quit(arg_t a) { |
| 57 | cleanup(); |
| 58 | exit(EXIT_SUCCESS); |
| 59 | } |
| 60 | |
| 61 | bool it_switch_mode(arg_t a) { |
| 62 | if (mode == MODE_IMAGE) { |
| 63 | if (tns.thumbs == NULL) |
| 64 | tns_init(&tns, filecnt, &win); |
| 65 | img_close(&img, false); |
| 66 | reset_timeout(reset_cursor); |
| 67 | tns.sel = fileidx; |
| 68 | tns.dirty = true; |
| 69 | mode = MODE_THUMB; |
| 70 | } else { |
| 71 | load_image(tns.sel); |
| 72 | mode = MODE_IMAGE; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool it_toggle_fullscreen(arg_t a) { |
| 78 | win_toggle_fullscreen(&win); |
| 79 | /* redraw after next ConfigureNotify event */ |
| 80 | set_timeout(redraw, TO_REDRAW_RESIZE, false); |
| 81 | if (mode == MODE_IMAGE) |
| 82 | img.checkpan = true; |
| 83 | else |
| 84 | tns.dirty = true; |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | bool it_toggle_bar(arg_t a) { |
| 89 | win_toggle_bar(&win); |
| 90 | if (mode == MODE_IMAGE) |
| 91 | img.checkpan = img.dirty = true; |
| 92 | else |
| 93 | tns.dirty = true; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | bool t_reload_all(arg_t a) { |
| 98 | if (mode == MODE_THUMB) { |
| 99 | tns_free(&tns); |
| 100 | tns_init(&tns, filecnt, &win); |
| 101 | return true; |
| 102 | } else { |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool it_reload_image(arg_t a) { |
| 108 | if (mode == MODE_IMAGE) { |
| 109 | load_image(fileidx); |
| 110 | } else { |
| 111 | win_set_cursor(&win, CURSOR_WATCH); |
| 112 | if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) { |
| 113 | remove_file(tns.sel, false); |
| 114 | tns.dirty = true; |
| 115 | if (tns.sel >= tns.cnt) |
| 116 | tns.sel = tns.cnt - 1; |
| 117 | } |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool it_remove_image(arg_t a) { |
| 123 | if (mode == MODE_IMAGE) { |
| 124 | remove_file(fileidx, true); |
| 125 | load_image(fileidx >= filecnt ? filecnt - 1 : fileidx); |
| 126 | return true; |
| 127 | } else if (tns.sel < tns.cnt) { |
| 128 | remove_file(tns.sel, true); |
| 129 | tns.dirty = true; |
| 130 | if (tns.sel >= tns.cnt) |
| 131 | tns.sel = tns.cnt - 1; |
| 132 | return true; |
| 133 | } else { |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | bool i_navigate(arg_t a) { |
| 139 | long n = (long) a; |
| 140 | |
| 141 | if (mode == MODE_IMAGE) { |
| 142 | if (prefix > 0) |
| 143 | n *= prefix; |
| 144 | n += fileidx; |
| 145 | if (n < 0) |
| 146 | n = 0; |
| 147 | if (n >= filecnt) |
| 148 | n = filecnt - 1; |
| 149 | |
| 150 | if (n != fileidx) { |
| 151 | load_image(n); |
| 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool it_first(arg_t a) { |
| 159 | if (mode == MODE_IMAGE && fileidx != 0) { |
| 160 | load_image(0); |
| 161 | return true; |
| 162 | } else if (mode == MODE_THUMB && tns.sel != 0) { |
| 163 | tns.sel = 0; |
| 164 | tns.dirty = true; |
| 165 | return true; |
| 166 | } else { |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | bool it_n_or_last(arg_t a) { |
| 172 | int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1; |
| 173 | |
| 174 | if (mode == MODE_IMAGE && fileidx != n) { |
| 175 | load_image(n); |
| 176 | return true; |
| 177 | } else if (mode == MODE_THUMB && tns.sel != n) { |
| 178 | tns.sel = n; |
| 179 | tns.dirty = true; |
| 180 | return true; |
| 181 | } else { |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | bool i_navigate_frame(arg_t a) { |
| 187 | if (mode == MODE_IMAGE && !img.multi.animate) |
| 188 | return img_frame_navigate(&img, (long) a); |
| 189 | else |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | bool i_toggle_animation(arg_t a) { |
| 194 | if (mode != MODE_IMAGE) |
| 195 | return false; |
| 196 | |
| 197 | if (img.multi.animate) { |
| 198 | reset_timeout(animate); |
| 199 | img.multi.animate = false; |
| 200 | } else if (img_frame_animate(&img, true)) { |
| 201 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | bool it_scroll_move(arg_t a) { |
| 207 | direction_t dir = (direction_t) a; |
| 208 | |
| 209 | if (mode == MODE_IMAGE) |
| 210 | return img_pan(&img, dir, prefix); |
| 211 | else |
| 212 | return tns_move_selection(&tns, dir); |
| 213 | } |
| 214 | |
| 215 | bool it_scroll_screen(arg_t a) { |
| 216 | direction_t dir = (direction_t) a; |
| 217 | |
| 218 | if (mode == MODE_IMAGE) |
| 219 | return img_pan(&img, dir, -1); |
| 220 | else |
| 221 | return tns_scroll(&tns, dir, true); |
| 222 | } |
| 223 | |
| 224 | bool i_scroll_to_edge(arg_t a) { |
| 225 | direction_t dir = (direction_t) a; |
| 226 | |
| 227 | if (mode == MODE_IMAGE) |
| 228 | return img_pan_edge(&img, dir); |
| 229 | else |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | /* Xlib helper function for i_drag() */ |
| 234 | Bool is_motionnotify(Display *d, XEvent *e, XPointer a) { |
| 235 | return e != NULL && e->type == MotionNotify; |
| 236 | } |
| 237 | |
| 238 | bool i_drag(arg_t a) { |
| 239 | int dx = 0, dy = 0, i, ox, oy, x, y; |
| 240 | unsigned int ui; |
| 241 | bool dragging = true, next = false; |
| 242 | XEvent e; |
| 243 | Window w; |
| 244 | |
| 245 | if (mode != MODE_IMAGE) |
| 246 | return false; |
| 247 | if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui)) |
| 248 | return false; |
| 249 | |
| 250 | win_set_cursor(&win, CURSOR_HAND); |
| 251 | |
| 252 | while (dragging) { |
| 253 | if (!next) |
| 254 | XMaskEvent(win.env.dpy, |
| 255 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e); |
| 256 | switch (e.type) { |
| 257 | case ButtonPress: |
| 258 | case ButtonRelease: |
| 259 | dragging = false; |
| 260 | break; |
| 261 | case MotionNotify: |
| 262 | x = e.xmotion.x; |
| 263 | y = e.xmotion.y; |
| 264 | if (x >= 0 && x <= win.w && y >= 0 && y <= win.h) { |
| 265 | dx += x - ox; |
| 266 | dy += y - oy; |
| 267 | } |
| 268 | ox = x; |
| 269 | oy = y; |
| 270 | break; |
| 271 | } |
| 272 | if (dragging) |
| 273 | next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None); |
| 274 | if ((!dragging || !next) && (dx != 0 || dy != 0)) { |
| 275 | if (img_move(&img, dx, dy)) { |
| 276 | img_render(&img); |
| 277 | win_draw(&win); |
| 278 | } |
| 279 | dx = dy = 0; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | win_set_cursor(&win, CURSOR_ARROW); |
| 284 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 285 | reset_timeout(redraw); |
| 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool i_zoom(arg_t a) { |
| 291 | long scale = (long) a; |
| 292 | |
| 293 | if (mode != MODE_IMAGE) |
| 294 | return false; |
| 295 | |
| 296 | if (scale > 0) |
| 297 | return img_zoom_in(&img); |
| 298 | else if (scale < 0) |
| 299 | return img_zoom_out(&img); |
| 300 | else |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | bool i_set_zoom(arg_t a) { |
| 305 | if (mode == MODE_IMAGE) |
| 306 | return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0); |
| 307 | else |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | bool i_fit_to_win(arg_t a) { |
| 312 | bool ret = false; |
| 313 | |
| 314 | if (mode == MODE_IMAGE) { |
| 315 | if ((ret = img_fit_win(&img))) |
| 316 | img_center(&img); |
| 317 | } |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | bool i_fit_to_img(arg_t a) { |
| 322 | int x, y; |
| 323 | unsigned int w, h; |
| 324 | bool ret = false; |
| 325 | |
| 326 | if (mode == MODE_IMAGE) { |
| 327 | x = MAX(0, win.x + img.x); |
| 328 | y = MAX(0, win.y + img.y); |
| 329 | w = img.w * img.zoom; |
| 330 | h = img.h * img.zoom; |
| 331 | if ((ret = win_moveresize(&win, x, y, w, h))) { |
| 332 | img.x = x - win.x; |
| 333 | img.y = y - win.y; |
| 334 | img.dirty = true; |
| 335 | } |
| 336 | } |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | bool i_rotate(arg_t a) { |
| 341 | direction_t dir = (direction_t) a; |
| 342 | |
| 343 | if (mode == MODE_IMAGE) { |
| 344 | if (dir == DIR_LEFT) { |
| 345 | img_rotate_left(&img); |
| 346 | return true; |
| 347 | } else if (dir == DIR_RIGHT) { |
| 348 | img_rotate_right(&img); |
| 349 | return true; |
| 350 | } |
| 351 | } |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | bool i_flip(arg_t a) { |
| 356 | flipdir_t dir = (flipdir_t) a; |
| 357 | |
| 358 | if (mode == MODE_IMAGE) { |
| 359 | img_flip(&img, dir); |
| 360 | return true; |
| 361 | } else { |
| 362 | return false; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | bool i_toggle_antialias(arg_t a) { |
| 367 | if (mode == MODE_IMAGE) { |
| 368 | img_toggle_antialias(&img); |
| 369 | return true; |
| 370 | } else { |
| 371 | return false; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | bool it_toggle_alpha(arg_t a) { |
| 376 | img.alpha = tns.alpha = !img.alpha; |
| 377 | if (mode == MODE_IMAGE) |
| 378 | img.dirty = true; |
| 379 | else |
| 380 | tns.dirty = true; |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | bool it_open_with(arg_t a) { |
| 385 | const char *prog = (const char*) a; |
| 386 | pid_t pid; |
| 387 | |
| 388 | if (prog == NULL || *prog == '\0') |
| 389 | return false; |
| 390 | |
| 391 | if ((pid = fork()) == 0) { |
| 392 | execlp(prog, prog, |
| 393 | files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL); |
| 394 | warn("could not exec: %s", prog); |
| 395 | exit(EXIT_FAILURE); |
| 396 | } else if (pid < 0) { |
| 397 | warn("could not fork. program was: %s", prog); |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | bool it_shell_cmd(arg_t a) { |
| 403 | int n, status; |
| 404 | const char *cmdline = (const char*) a; |
| 405 | pid_t pid; |
| 406 | |
| 407 | if (cmdline == NULL || *cmdline == '\0') |
| 408 | return false; |
| 409 | |
| 410 | n = mode == MODE_IMAGE ? fileidx : tns.sel; |
| 411 | |
| 412 | if (setenv("SXIV_IMG", files[n].path, 1) < 0) { |
| 413 | warn("could not set env.-variable: SXIV_IMG. command line was: %s", |
| 414 | cmdline); |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | if ((pid = fork()) == 0) { |
| 419 | execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL); |
| 420 | warn("could not exec: /bin/sh. command line was: %s", cmdline); |
| 421 | exit(EXIT_FAILURE); |
| 422 | } else if (pid < 0) { |
| 423 | warn("could not fork. command line was: %s", cmdline); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | win_set_cursor(&win, CURSOR_WATCH); |
| 428 | |
| 429 | waitpid(pid, &status, 0); |
| 430 | if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) |
| 431 | warn("child exited with non-zero return value: %d. command line was: %s", |
| 432 | WEXITSTATUS(status), cmdline); |
| 433 | |
| 434 | if (mode == MODE_IMAGE) { |
| 435 | img_close(&img, true); |
| 436 | load_image(fileidx); |
| 437 | } |
| 438 | if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) && |
| 439 | mode == MODE_THUMB) |
| 440 | { |
| 441 | remove_file(tns.sel, false); |
| 442 | tns.dirty = true; |
| 443 | if (tns.sel >= tns.cnt) |
| 444 | tns.sel = tns.cnt - 1; |
| 445 | } |
| 446 | return true; |
| 447 | } |