| 1 | /* Copyright 2011, 2012, 2014 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 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | #include <sys/wait.h> |
| 23 | |
| 24 | #include "commands.h" |
| 25 | #include "image.h" |
| 26 | #include "options.h" |
| 27 | #include "thumbs.h" |
| 28 | #include "util.h" |
| 29 | |
| 30 | #define _IMAGE_CONFIG |
| 31 | #include "config.h" |
| 32 | |
| 33 | void remove_file(int, bool); |
| 34 | void load_image(int); |
| 35 | void open_info(void); |
| 36 | void redraw(void); |
| 37 | void reset_cursor(void); |
| 38 | void animate(void); |
| 39 | void slideshow(void); |
| 40 | void set_timeout(timeout_f, int, bool); |
| 41 | void reset_timeout(timeout_f); |
| 42 | |
| 43 | extern appmode_t mode; |
| 44 | extern img_t img; |
| 45 | extern tns_t tns; |
| 46 | extern win_t win; |
| 47 | |
| 48 | extern fileinfo_t *files; |
| 49 | extern int filecnt, fileidx; |
| 50 | extern int alternate; |
| 51 | extern int markcnt; |
| 52 | |
| 53 | extern int prefix; |
| 54 | extern bool extprefix; |
| 55 | |
| 56 | bool cg_quit(arg_t _) |
| 57 | { |
| 58 | unsigned int i; |
| 59 | |
| 60 | if (options->to_stdout && markcnt > 0) { |
| 61 | for (i = 0; i < filecnt; i++) { |
| 62 | if (files[i].flags & FF_MARK) |
| 63 | printf("%s\n", files[i].name); |
| 64 | } |
| 65 | } |
| 66 | exit(EXIT_SUCCESS); |
| 67 | } |
| 68 | |
| 69 | bool cg_switch_mode(arg_t _) |
| 70 | { |
| 71 | if (mode == MODE_IMAGE) { |
| 72 | if (tns.thumbs == NULL) |
| 73 | tns_init(&tns, files, &filecnt, &fileidx, &win); |
| 74 | img_close(&img, false); |
| 75 | reset_timeout(reset_cursor); |
| 76 | if (img.ss.on) { |
| 77 | img.ss.on = false; |
| 78 | reset_timeout(slideshow); |
| 79 | } |
| 80 | tns.dirty = true; |
| 81 | mode = MODE_THUMB; |
| 82 | } else { |
| 83 | load_image(fileidx); |
| 84 | mode = MODE_IMAGE; |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool cg_toggle_fullscreen(arg_t _) |
| 90 | { |
| 91 | win_toggle_fullscreen(&win); |
| 92 | /* redraw after next ConfigureNotify event */ |
| 93 | set_timeout(redraw, TO_REDRAW_RESIZE, false); |
| 94 | if (mode == MODE_IMAGE) |
| 95 | img.checkpan = img.dirty = true; |
| 96 | else |
| 97 | tns.dirty = true; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | bool cg_toggle_bar(arg_t _) |
| 102 | { |
| 103 | win_toggle_bar(&win); |
| 104 | if (mode == MODE_IMAGE) { |
| 105 | img.checkpan = img.dirty = true; |
| 106 | if (win.bar.h > 0) |
| 107 | open_info(); |
| 108 | } else { |
| 109 | tns.dirty = true; |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool cg_prefix_external(arg_t _) |
| 115 | { |
| 116 | extprefix = true; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | bool cg_reload_image(arg_t _) |
| 121 | { |
| 122 | if (mode == MODE_IMAGE) { |
| 123 | load_image(fileidx); |
| 124 | } else { |
| 125 | win_set_cursor(&win, CURSOR_WATCH); |
| 126 | if (!tns_load(&tns, fileidx, true, false)) { |
| 127 | remove_file(fileidx, false); |
| 128 | tns.dirty = true; |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | bool cg_remove_image(arg_t _) |
| 135 | { |
| 136 | remove_file(fileidx, true); |
| 137 | if (mode == MODE_IMAGE) |
| 138 | load_image(fileidx); |
| 139 | else |
| 140 | tns.dirty = true; |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | bool cg_first(arg_t _) |
| 145 | { |
| 146 | if (mode == MODE_IMAGE && fileidx != 0) { |
| 147 | load_image(0); |
| 148 | return true; |
| 149 | } else if (mode == MODE_THUMB && fileidx != 0) { |
| 150 | fileidx = 0; |
| 151 | tns.dirty = true; |
| 152 | return true; |
| 153 | } else { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | bool cg_n_or_last(arg_t _) |
| 159 | { |
| 160 | int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1; |
| 161 | |
| 162 | if (mode == MODE_IMAGE && fileidx != n) { |
| 163 | load_image(n); |
| 164 | return true; |
| 165 | } else if (mode == MODE_THUMB && fileidx != n) { |
| 166 | fileidx = n; |
| 167 | tns.dirty = true; |
| 168 | return true; |
| 169 | } else { |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool cg_scroll_screen(arg_t dir) |
| 175 | { |
| 176 | if (mode == MODE_IMAGE) |
| 177 | return img_pan(&img, dir, -1); |
| 178 | else |
| 179 | return tns_scroll(&tns, dir, true); |
| 180 | } |
| 181 | |
| 182 | bool cg_zoom(arg_t d) |
| 183 | { |
| 184 | if (mode == MODE_THUMB) |
| 185 | return tns_zoom(&tns, d); |
| 186 | else if (d > 0) |
| 187 | return img_zoom_in(&img); |
| 188 | else if (d < 0) |
| 189 | return img_zoom_out(&img); |
| 190 | else |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | bool cg_toggle_image_mark(arg_t _) |
| 195 | { |
| 196 | files[fileidx].flags ^= FF_MARK; |
| 197 | markcnt += files[fileidx].flags & FF_MARK ? 1 : -1; |
| 198 | if (mode == MODE_THUMB) |
| 199 | tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK)); |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | bool cg_reverse_marks(arg_t _) |
| 204 | { |
| 205 | int i; |
| 206 | |
| 207 | for (i = 0; i < filecnt; i++) { |
| 208 | files[i].flags ^= FF_MARK; |
| 209 | markcnt += files[i].flags & FF_MARK ? 1 : -1; |
| 210 | } |
| 211 | if (mode == MODE_THUMB) |
| 212 | tns.dirty = true; |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool cg_unmark_all(arg_t _) |
| 217 | { |
| 218 | int i; |
| 219 | |
| 220 | for (i = 0; i < filecnt; i++) |
| 221 | files[i].flags &= ~FF_MARK; |
| 222 | markcnt = 0; |
| 223 | if (mode == MODE_THUMB) |
| 224 | tns.dirty = true; |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | bool cg_navigate_marked(arg_t n) |
| 229 | { |
| 230 | int d, i; |
| 231 | int new = fileidx; |
| 232 | |
| 233 | if (prefix > 0) |
| 234 | n *= prefix; |
| 235 | d = n > 0 ? 1 : -1; |
| 236 | for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) { |
| 237 | if (files[i].flags & FF_MARK) { |
| 238 | n -= d; |
| 239 | new = i; |
| 240 | } |
| 241 | } |
| 242 | if (new != fileidx) { |
| 243 | if (mode == MODE_IMAGE) { |
| 244 | load_image(new); |
| 245 | } else { |
| 246 | fileidx = new; |
| 247 | tns.dirty = true; |
| 248 | } |
| 249 | return true; |
| 250 | } else { |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | bool cg_change_gamma(arg_t d) |
| 256 | { |
| 257 | if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) { |
| 258 | if (mode == MODE_THUMB) |
| 259 | tns.dirty = true; |
| 260 | return true; |
| 261 | } else { |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | bool ci_navigate(arg_t n) |
| 267 | { |
| 268 | if (prefix > 0) |
| 269 | n *= prefix; |
| 270 | n += fileidx; |
| 271 | if (n < 0) |
| 272 | n = 0; |
| 273 | if (n >= filecnt) |
| 274 | n = filecnt - 1; |
| 275 | |
| 276 | if (n != fileidx) { |
| 277 | load_image(n); |
| 278 | return true; |
| 279 | } else { |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | bool ci_alternate(arg_t _) |
| 285 | { |
| 286 | load_image(alternate); |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | bool ci_navigate_frame(arg_t d) |
| 291 | { |
| 292 | if (prefix > 0) |
| 293 | d *= prefix; |
| 294 | return !img.multi.animate && img_frame_navigate(&img, d); |
| 295 | } |
| 296 | |
| 297 | bool ci_toggle_animation(arg_t _) |
| 298 | { |
| 299 | bool dirty = false; |
| 300 | |
| 301 | if (img.multi.cnt > 0) { |
| 302 | img.multi.animate = !img.multi.animate; |
| 303 | if (img.multi.animate) { |
| 304 | dirty = img_frame_animate(&img); |
| 305 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 306 | } else { |
| 307 | reset_timeout(animate); |
| 308 | } |
| 309 | } |
| 310 | return dirty; |
| 311 | } |
| 312 | |
| 313 | bool ci_scroll(arg_t dir) |
| 314 | { |
| 315 | return img_pan(&img, dir, prefix); |
| 316 | } |
| 317 | |
| 318 | bool ci_scroll_to_edge(arg_t dir) |
| 319 | { |
| 320 | return img_pan_edge(&img, dir); |
| 321 | } |
| 322 | |
| 323 | /* Xlib helper function for i_drag() */ |
| 324 | Bool is_motionnotify(Display *d, XEvent *e, XPointer a) |
| 325 | { |
| 326 | return e != NULL && e->type == MotionNotify; |
| 327 | } |
| 328 | |
| 329 | #define WARP(x,y) \ |
| 330 | XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \ |
| 331 | ox = x, oy = y; \ |
| 332 | break |
| 333 | |
| 334 | bool ci_drag(arg_t _) |
| 335 | { |
| 336 | int dx = 0, dy = 0, i, ox, oy, x, y; |
| 337 | unsigned int ui; |
| 338 | bool dragging = true, next = false; |
| 339 | XEvent e; |
| 340 | Window w; |
| 341 | |
| 342 | if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui)) |
| 343 | return false; |
| 344 | |
| 345 | win_set_cursor(&win, CURSOR_HAND); |
| 346 | |
| 347 | while (dragging) { |
| 348 | if (!next) |
| 349 | XMaskEvent(win.env.dpy, |
| 350 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e); |
| 351 | switch (e.type) { |
| 352 | case ButtonPress: |
| 353 | case ButtonRelease: |
| 354 | dragging = false; |
| 355 | break; |
| 356 | case MotionNotify: |
| 357 | x = e.xmotion.x; |
| 358 | y = e.xmotion.y; |
| 359 | |
| 360 | /* wrap the mouse around */ |
| 361 | if (x <= 0) { |
| 362 | WARP(win.w - 2, y); |
| 363 | } else if (x >= win.w - 1) { |
| 364 | WARP(1, y); |
| 365 | } else if (y <= 0) { |
| 366 | WARP(x, win.h - 2); |
| 367 | } else if (y >= win.h - 1) { |
| 368 | WARP(x, 1); |
| 369 | } |
| 370 | dx += x - ox; |
| 371 | dy += y - oy; |
| 372 | ox = x; |
| 373 | oy = y; |
| 374 | break; |
| 375 | } |
| 376 | if (dragging) |
| 377 | next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None); |
| 378 | if ((!dragging || !next) && (dx != 0 || dy != 0)) { |
| 379 | if (img_move(&img, dx, dy)) { |
| 380 | img_render(&img); |
| 381 | win_draw(&win); |
| 382 | } |
| 383 | dx = dy = 0; |
| 384 | } |
| 385 | } |
| 386 | win_set_cursor(&win, CURSOR_ARROW); |
| 387 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 388 | reset_timeout(redraw); |
| 389 | |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | bool ci_set_zoom(arg_t zl) |
| 394 | { |
| 395 | return img_zoom(&img, (prefix ? prefix : zl) / 100.0); |
| 396 | } |
| 397 | |
| 398 | bool ci_fit_to_win(arg_t sm) |
| 399 | { |
| 400 | return img_fit_win(&img, sm); |
| 401 | } |
| 402 | |
| 403 | bool ci_rotate(arg_t degree) |
| 404 | { |
| 405 | img_rotate(&img, degree); |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | bool ci_flip(arg_t dir) |
| 410 | { |
| 411 | img_flip(&img, dir); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | bool ci_toggle_antialias(arg_t _) |
| 416 | { |
| 417 | img_toggle_antialias(&img); |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | bool ci_toggle_alpha(arg_t _) |
| 422 | { |
| 423 | img.alpha = !img.alpha; |
| 424 | img.dirty = true; |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | bool ci_slideshow(arg_t _) |
| 429 | { |
| 430 | if (prefix > 0) { |
| 431 | img.ss.on = true; |
| 432 | img.ss.delay = prefix * 10; |
| 433 | set_timeout(slideshow, img.ss.delay * 100, true); |
| 434 | } else if (img.ss.on) { |
| 435 | img.ss.on = false; |
| 436 | reset_timeout(slideshow); |
| 437 | } else { |
| 438 | img.ss.on = true; |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | bool ct_move_sel(arg_t dir) |
| 444 | { |
| 445 | return tns_move_selection(&tns, dir, prefix); |
| 446 | } |
| 447 | |
| 448 | bool ct_reload_all(arg_t _) |
| 449 | { |
| 450 | tns_free(&tns); |
| 451 | tns_init(&tns, files, &filecnt, &fileidx, &win); |
| 452 | tns.dirty = true; |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | #undef G_CMD |
| 458 | #define G_CMD(c) { -1, cg_##c }, |
| 459 | #undef I_CMD |
| 460 | #define I_CMD(c) { MODE_IMAGE, ci_##c }, |
| 461 | #undef T_CMD |
| 462 | #define T_CMD(c) { MODE_THUMB, ct_##c }, |
| 463 | |
| 464 | const cmd_t cmds[CMD_COUNT] = { |
| 465 | #include "commands.lst" |
| 466 | }; |
| 467 | |