Krolyxon
Commit dc727b8

Branches

Refs →

nsxivcommands.c

443 lines · 9.1 KB · C

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