Krolyxon
Commit 5621975

Branches

Refs →

nsxivcommands.c

564 lines · 10.5 KB · C

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
34void cleanup(void);
35void remove_file(int, bool);
36void load_image(int);
37void open_info(void);
38void redraw(void);
39void reset_cursor(void);
40void animate(void);
41void set_timeout(timeout_f, int, bool);
42void reset_timeout(timeout_f);
43
44extern appmode_t mode;
45extern img_t img;
46extern tns_t tns;
47extern win_t win;
48
49extern fileinfo_t *files;
50extern int filecnt, fileidx;
51extern int markcnt;
52extern int alternate;
53
54extern int prefix;
55
56const int ss_delays[] = {
57 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
58};
59
60bool 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
74bool 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
93bool 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
105bool 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
118bool 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
129bool 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
145bool 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
162bool 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
183bool 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
193bool 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
207bool 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
223bool 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
231bool 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
245bool 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
254bool it_reverse_marks(arg_t a)
255{
256 int i, cnt = mode == MODE_IMAGE ? filecnt : tns.cnt;
257
258 for (i = 0; i < cnt; i++) {
259 files[i].marked = !files[i].marked;
260 markcnt += files[i].marked ? 1 : -1;
261 }
262
263 return true;
264}
265
266bool it_navigate_marked(arg_t a)
267{
268 long n = (long) a;
269 int d, i, cnt, sel, new;
270
271 if (mode == MODE_IMAGE)
272 cnt = filecnt, sel = new = fileidx;
273 else
274 cnt = tns.cnt, sel = new = tns.sel;
275 if (prefix > 0)
276 n *= prefix;
277 d = n > 0 ? 1 : -1;
278 for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
279 if (files[i].marked) {
280 n -= d;
281 new = i;
282 }
283 }
284 if (new != sel) {
285 if (mode == MODE_IMAGE) {
286 load_image(new);
287 } else {
288 tns.sel = new;
289 tns.dirty = true;
290 }
291 return true;
292 } else {
293 return false;
294 }
295}
296
297bool it_scroll_move(arg_t a)
298{
299 direction_t dir = (direction_t) a;
300
301 if (mode == MODE_IMAGE)
302 return img_pan(&img, dir, prefix);
303 else
304 return tns_move_selection(&tns, dir, prefix);
305}
306
307bool it_scroll_screen(arg_t a)
308{
309 direction_t dir = (direction_t) a;
310
311 if (mode == MODE_IMAGE)
312 return img_pan(&img, dir, -1);
313 else
314 return tns_scroll(&tns, dir, true);
315}
316
317bool i_scroll_to_edge(arg_t a)
318{
319 direction_t dir = (direction_t) a;
320
321 if (mode == MODE_IMAGE)
322 return img_pan_edge(&img, dir);
323 else
324 return false;
325}
326
327/* Xlib helper function for i_drag() */
328Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
329{
330 return e != NULL && e->type == MotionNotify;
331}
332
333#define WARP(x,y) \
334 XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
335 ox = x, oy = y; \
336 break
337
338bool i_drag(arg_t a)
339{
340 int dx = 0, dy = 0, i, ox, oy, x, y;
341 unsigned int ui;
342 bool dragging = true, next = false;
343 XEvent e;
344 Window w;
345
346 if (mode != MODE_IMAGE)
347 return false;
348 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
349 return false;
350
351 win_set_cursor(&win, CURSOR_HAND);
352
353 while (dragging) {
354 if (!next)
355 XMaskEvent(win.env.dpy,
356 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
357 switch (e.type) {
358 case ButtonPress:
359 case ButtonRelease:
360 dragging = false;
361 break;
362 case MotionNotify:
363 x = e.xmotion.x;
364 y = e.xmotion.y;
365
366 /* wrap the mouse around */
367 if (x <= 0) {
368 WARP(win.w - 2, y);
369 } else if (x >= win.w - 1) {
370 WARP(1, y);
371 } else if (y <= 0) {
372 WARP(x, win.h - 2);
373 } else if (y >= win.h - 1) {
374 WARP(x, 1);
375 }
376 dx += x - ox;
377 dy += y - oy;
378 ox = x;
379 oy = y;
380 break;
381 }
382 if (dragging)
383 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
384 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
385 if (img_move(&img, dx, dy)) {
386 img_render(&img);
387 win_draw(&win);
388 }
389 dx = dy = 0;
390 }
391 }
392
393 win_set_cursor(&win, CURSOR_ARROW);
394 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
395 reset_timeout(redraw);
396
397 return false;
398}
399
400bool i_zoom(arg_t a)
401{
402 long scale = (long) a;
403
404 if (mode != MODE_IMAGE)
405 return false;
406
407 if (scale > 0)
408 return img_zoom_in(&img);
409 else if (scale < 0)
410 return img_zoom_out(&img);
411 else
412 return false;
413}
414
415bool i_set_zoom(arg_t a)
416{
417 if (mode == MODE_IMAGE)
418 return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
419 else
420 return false;
421}
422
423bool i_fit_to_win(arg_t a)
424{
425 bool ret = false;
426 scalemode_t sm = (scalemode_t) a;
427
428 if (mode == MODE_IMAGE) {
429 if ((ret = img_fit_win(&img, sm)))
430 img_center(&img);
431 }
432 return ret;
433}
434
435bool i_fit_to_img(arg_t a)
436{
437 int x, y;
438 unsigned int w, h;
439 bool ret = false;
440
441 if (mode == MODE_IMAGE) {
442 x = MAX(0, win.x + img.x);
443 y = MAX(0, win.y + img.y);
444 w = img.w * img.zoom;
445 h = img.h * img.zoom;
446 if ((ret = win_moveresize(&win, x, y, w, h))) {
447 img.x = x - win.x;
448 img.y = y - win.y;
449 img.dirty = true;
450 }
451 }
452 return ret;
453}
454
455bool i_rotate(arg_t a)
456{
457 degree_t degree = (degree_t) a;
458
459 if (mode == MODE_IMAGE) {
460 img_rotate(&img, degree);
461 return true;
462 } else {
463 return false;
464 }
465}
466
467bool i_flip(arg_t a)
468{
469 flipdir_t dir = (flipdir_t) a;
470
471 if (mode == MODE_IMAGE) {
472 img_flip(&img, dir);
473 return true;
474 } else {
475 return false;
476 }
477}
478
479bool i_toggle_antialias(arg_t a)
480{
481 if (mode == MODE_IMAGE) {
482 img_toggle_antialias(&img);
483 return true;
484 } else {
485 return false;
486 }
487}
488
489bool it_toggle_alpha(arg_t a)
490{
491 img.alpha = tns.alpha = !img.alpha;
492 if (mode == MODE_IMAGE)
493 img.dirty = true;
494 else
495 tns.dirty = true;
496 return true;
497}
498
499bool it_open_with(arg_t a)
500{
501 const char *prog = (const char*) a;
502 pid_t pid;
503
504 if (prog == NULL || *prog == '\0')
505 return false;
506
507 if ((pid = fork()) == 0) {
508 execlp(prog, prog,
509 files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
510 warn("could not exec: %s", prog);
511 exit(EXIT_FAILURE);
512 } else if (pid < 0) {
513 warn("could not fork. program was: %s", prog);
514 }
515 return false;
516}
517
518bool it_shell_cmd(arg_t a)
519{
520 int n, status;
521 const char *cmdline = (const char*) a;
522 pid_t pid;
523
524 if (cmdline == NULL || *cmdline == '\0')
525 return false;
526
527 n = mode == MODE_IMAGE ? fileidx : tns.sel;
528
529 if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
530 warn("could not set env.-variable: SXIV_IMG. command line was: %s",
531 cmdline);
532 return false;
533 }
534
535 if ((pid = fork()) == 0) {
536 execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
537 warn("could not exec: /bin/sh. command line was: %s", cmdline);
538 exit(EXIT_FAILURE);
539 } else if (pid < 0) {
540 warn("could not fork. command line was: %s", cmdline);
541 return false;
542 }
543
544 win_set_cursor(&win, CURSOR_WATCH);
545
546 waitpid(pid, &status, 0);
547 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
548 warn("child exited with non-zero return value: %d. command line was: %s",
549 WEXITSTATUS(status), cmdline);
550
551 if (mode == MODE_IMAGE) {
552 img_close(&img, true);
553 load_image(fileidx);
554 }
555 if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
556 mode == MODE_THUMB)
557 {
558 remove_file(tns.sel, false);
559 tns.dirty = true;
560 if (tns.sel >= tns.cnt)
561 tns.sel = tns.cnt - 1;
562 }
563 return true;
564}