| 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 "sxiv.h" |
| 20 | #define _IMAGE_CONFIG |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/wait.h> |
| 27 | |
| 28 | void remove_file(int, bool); |
| 29 | void load_image(int); |
| 30 | void close_info(void); |
| 31 | void open_info(void); |
| 32 | int ptr_third_x(void); |
| 33 | void redraw(void); |
| 34 | void reset_cursor(void); |
| 35 | void animate(void); |
| 36 | void slideshow(void); |
| 37 | void set_timeout(timeout_f, int, bool); |
| 38 | void reset_timeout(timeout_f); |
| 39 | |
| 40 | extern appmode_t mode; |
| 41 | extern img_t img; |
| 42 | extern tns_t tns; |
| 43 | extern win_t win; |
| 44 | |
| 45 | extern fileinfo_t *files; |
| 46 | extern int filecnt, fileidx; |
| 47 | extern int alternate; |
| 48 | extern int markcnt; |
| 49 | extern int markidx; |
| 50 | |
| 51 | extern int prefix; |
| 52 | extern bool extprefix; |
| 53 | |
| 54 | bool cg_quit(arg_t _) |
| 55 | { |
| 56 | unsigned int i; |
| 57 | |
| 58 | if (options->to_stdout && markcnt > 0) { |
| 59 | for (i = 0; i < filecnt; i++) { |
| 60 | if (files[i].flags & FF_MARK) |
| 61 | printf("%s\n", files[i].name); |
| 62 | } |
| 63 | } |
| 64 | exit(EXIT_SUCCESS); |
| 65 | } |
| 66 | |
| 67 | bool cg_switch_mode(arg_t _) |
| 68 | { |
| 69 | if (mode == MODE_IMAGE) { |
| 70 | if (tns.thumbs == NULL) |
| 71 | tns_init(&tns, files, &filecnt, &fileidx, &win); |
| 72 | img_close(&img, false); |
| 73 | reset_timeout(reset_cursor); |
| 74 | if (img.ss.on) { |
| 75 | img.ss.on = false; |
| 76 | reset_timeout(slideshow); |
| 77 | } |
| 78 | tns.dirty = true; |
| 79 | mode = MODE_THUMB; |
| 80 | } else { |
| 81 | load_image(fileidx); |
| 82 | mode = MODE_IMAGE; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool cg_toggle_fullscreen(arg_t _) |
| 88 | { |
| 89 | win_toggle_fullscreen(&win); |
| 90 | /* redraw after next ConfigureNotify event */ |
| 91 | set_timeout(redraw, TO_REDRAW_RESIZE, false); |
| 92 | if (mode == MODE_IMAGE) |
| 93 | img.checkpan = img.dirty = true; |
| 94 | else |
| 95 | tns.dirty = true; |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | bool cg_toggle_bar(arg_t _) |
| 100 | { |
| 101 | win_toggle_bar(&win); |
| 102 | if (mode == MODE_IMAGE) { |
| 103 | if (win.bar.h > 0) |
| 104 | open_info(); |
| 105 | else |
| 106 | close_info(); |
| 107 | img.checkpan = img.dirty = true; |
| 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 | markidx = fileidx; |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | bool cg_reverse_marks(arg_t _) |
| 205 | { |
| 206 | int i; |
| 207 | |
| 208 | for (i = 0; i < filecnt; i++) { |
| 209 | files[i].flags ^= FF_MARK; |
| 210 | markcnt += files[i].flags & FF_MARK ? 1 : -1; |
| 211 | } |
| 212 | if (mode == MODE_THUMB) |
| 213 | tns.dirty = true; |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | bool cg_mark_range(arg_t _) |
| 218 | { |
| 219 | int i, from, to; |
| 220 | |
| 221 | from = markidx; |
| 222 | to = fileidx; |
| 223 | if (fileidx < markidx) { |
| 224 | from = fileidx; |
| 225 | to = markidx; |
| 226 | } |
| 227 | for (i = from; i <= to; i++) { |
| 228 | if (files[markidx].flags & FF_MARK) { |
| 229 | if (!(files[i].flags & FF_MARK)) { |
| 230 | files[i].flags |= FF_MARK; |
| 231 | markcnt += 1; |
| 232 | } |
| 233 | } else { |
| 234 | if (files[i].flags & FF_MARK) { |
| 235 | files[i].flags &= ~FF_MARK; |
| 236 | markcnt -= 1; |
| 237 | } |
| 238 | } |
| 239 | if (mode == MODE_THUMB) |
| 240 | tns_mark(&tns, i, !!(files[i].flags & FF_MARK)); |
| 241 | } |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | bool cg_unmark_all(arg_t _) |
| 246 | { |
| 247 | int i; |
| 248 | |
| 249 | for (i = 0; i < filecnt; i++) |
| 250 | files[i].flags &= ~FF_MARK; |
| 251 | markcnt = 0; |
| 252 | if (mode == MODE_THUMB) |
| 253 | tns.dirty = true; |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | bool cg_navigate_marked(arg_t n) |
| 258 | { |
| 259 | int d, i; |
| 260 | int new = fileidx; |
| 261 | |
| 262 | if (prefix > 0) |
| 263 | n *= prefix; |
| 264 | d = n > 0 ? 1 : -1; |
| 265 | for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) { |
| 266 | if (files[i].flags & FF_MARK) { |
| 267 | n -= d; |
| 268 | new = i; |
| 269 | } |
| 270 | } |
| 271 | if (new != fileidx) { |
| 272 | if (mode == MODE_IMAGE) { |
| 273 | load_image(new); |
| 274 | } else { |
| 275 | fileidx = new; |
| 276 | tns.dirty = true; |
| 277 | } |
| 278 | return true; |
| 279 | } else { |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | bool cg_change_gamma(arg_t d) |
| 285 | { |
| 286 | if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) { |
| 287 | if (mode == MODE_THUMB) |
| 288 | tns.dirty = true; |
| 289 | return true; |
| 290 | } else { |
| 291 | return false; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | bool ci_navigate(arg_t n) |
| 296 | { |
| 297 | if (prefix > 0) |
| 298 | n *= prefix; |
| 299 | n += fileidx; |
| 300 | if (n < 0) |
| 301 | n = 0; |
| 302 | if (n >= filecnt) |
| 303 | n = filecnt - 1; |
| 304 | |
| 305 | if (n != fileidx) { |
| 306 | load_image(n); |
| 307 | return true; |
| 308 | } else { |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | bool ci_cursor_navigate(arg_t _) |
| 314 | { |
| 315 | return ci_navigate(ptr_third_x() - 1); |
| 316 | } |
| 317 | |
| 318 | bool ci_alternate(arg_t _) |
| 319 | { |
| 320 | load_image(alternate); |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | bool ci_navigate_frame(arg_t d) |
| 325 | { |
| 326 | if (prefix > 0) |
| 327 | d *= prefix; |
| 328 | return !img.multi.animate && img_frame_navigate(&img, d); |
| 329 | } |
| 330 | |
| 331 | bool ci_toggle_animation(arg_t _) |
| 332 | { |
| 333 | bool dirty = false; |
| 334 | |
| 335 | if (img.multi.cnt > 0) { |
| 336 | img.multi.animate = !img.multi.animate; |
| 337 | if (img.multi.animate) { |
| 338 | dirty = img_frame_animate(&img); |
| 339 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); |
| 340 | } else { |
| 341 | reset_timeout(animate); |
| 342 | } |
| 343 | } |
| 344 | return dirty; |
| 345 | } |
| 346 | |
| 347 | bool ci_scroll(arg_t dir) |
| 348 | { |
| 349 | return img_pan(&img, dir, prefix); |
| 350 | } |
| 351 | |
| 352 | bool ci_scroll_to_edge(arg_t dir) |
| 353 | { |
| 354 | return img_pan_edge(&img, dir); |
| 355 | } |
| 356 | |
| 357 | bool ci_drag(arg_t mode) |
| 358 | { |
| 359 | int x, y, ox, oy; |
| 360 | float px, py; |
| 361 | XEvent e; |
| 362 | |
| 363 | if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h) |
| 364 | return false; |
| 365 | |
| 366 | win_set_cursor(&win, CURSOR_DRAG); |
| 367 | |
| 368 | win_cursor_pos(&win, &x, &y); |
| 369 | ox = x; |
| 370 | oy = y; |
| 371 | |
| 372 | for (;;) { |
| 373 | if (mode == DRAG_ABSOLUTE) { |
| 374 | px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8) |
| 375 | * (win.w - img.w * img.zoom); |
| 376 | py = MIN(MAX(0.0, y - win.h*0.1), win.h*0.8) / (win.h*0.8) |
| 377 | * (win.h - img.h * img.zoom); |
| 378 | } else { |
| 379 | px = img.x + x - ox; |
| 380 | py = img.y + y - oy; |
| 381 | } |
| 382 | |
| 383 | if (img_pos(&img, px, py)) { |
| 384 | img_render(&img); |
| 385 | win_draw(&win); |
| 386 | } |
| 387 | XMaskEvent(win.env.dpy, |
| 388 | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e); |
| 389 | if (e.type == ButtonPress || e.type == ButtonRelease) |
| 390 | break; |
| 391 | while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e)); |
| 392 | ox = x; |
| 393 | oy = y; |
| 394 | x = e.xmotion.x; |
| 395 | y = e.xmotion.y; |
| 396 | } |
| 397 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); |
| 398 | reset_cursor(); |
| 399 | |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | bool ci_set_zoom(arg_t zl) |
| 404 | { |
| 405 | return img_zoom(&img, (prefix ? prefix : zl) / 100.0); |
| 406 | } |
| 407 | |
| 408 | bool ci_fit_to_win(arg_t sm) |
| 409 | { |
| 410 | return img_fit_win(&img, sm); |
| 411 | } |
| 412 | |
| 413 | bool ci_rotate(arg_t degree) |
| 414 | { |
| 415 | img_rotate(&img, degree); |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | bool ci_flip(arg_t dir) |
| 420 | { |
| 421 | img_flip(&img, dir); |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | bool ci_toggle_antialias(arg_t _) |
| 426 | { |
| 427 | img_toggle_antialias(&img); |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | bool ci_toggle_alpha(arg_t _) |
| 432 | { |
| 433 | img.alpha = !img.alpha; |
| 434 | img.dirty = true; |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | bool ci_slideshow(arg_t _) |
| 439 | { |
| 440 | if (prefix > 0) { |
| 441 | img.ss.on = true; |
| 442 | img.ss.delay = prefix * 10; |
| 443 | set_timeout(slideshow, img.ss.delay * 100, true); |
| 444 | } else if (img.ss.on) { |
| 445 | img.ss.on = false; |
| 446 | reset_timeout(slideshow); |
| 447 | } else { |
| 448 | img.ss.on = true; |
| 449 | } |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | bool ct_move_sel(arg_t dir) |
| 454 | { |
| 455 | return tns_move_selection(&tns, dir, prefix); |
| 456 | } |
| 457 | |
| 458 | bool ct_reload_all(arg_t _) |
| 459 | { |
| 460 | tns_free(&tns); |
| 461 | tns_init(&tns, files, &filecnt, &fileidx, &win); |
| 462 | tns.dirty = true; |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | #undef G_CMD |
| 468 | #define G_CMD(c) { -1, cg_##c }, |
| 469 | #undef I_CMD |
| 470 | #define I_CMD(c) { MODE_IMAGE, ci_##c }, |
| 471 | #undef T_CMD |
| 472 | #define T_CMD(c) { MODE_THUMB, ct_##c }, |
| 473 | |
| 474 | const cmd_t cmds[CMD_COUNT] = { |
| 475 | #include "commands.lst" |
| 476 | }; |
| 477 | |