Krolyxon
Commit 4f5ce2e

Branches

Refs →

nsxivcommands.c

450 lines · 9.2 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, prefix);
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, -1);
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 false;
277}
278
279bool i_set_zoom(arg_t a) {
280 if (mode == MODE_IMAGE)
281 return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
282 else
283 return false;
284}
285
286bool i_fit_to_win(arg_t a) {
287 bool ret = false;
288
289 if (mode == MODE_IMAGE) {
290 if ((ret = img_fit_win(&img)))
291 img_center(&img);
292 }
293 return ret;
294}
295
296bool i_fit_to_img(arg_t a) {
297 int x, y;
298 unsigned int w, h;
299 bool ret = false;
300
301 if (mode == MODE_IMAGE) {
302 x = MAX(0, win.x + img.x);
303 y = MAX(0, win.y + img.y);
304 w = img.w * img.zoom;
305 h = img.h * img.zoom;
306 if ((ret = win_moveresize(&win, x, y, w, h))) {
307 img.x = x - win.x;
308 img.y = y - win.y;
309 }
310 }
311 return ret;
312}
313
314bool i_rotate(arg_t a) {
315 direction_t dir = (direction_t) a;
316
317 if (mode == MODE_IMAGE) {
318 if (dir == DIR_LEFT) {
319 img_rotate_left(&img);
320 return true;
321 } else if (dir == DIR_RIGHT) {
322 img_rotate_right(&img);
323 return true;
324 }
325 }
326 return false;
327}
328
329bool i_toggle_slideshow(arg_t a) {
330 if (mode == MODE_IMAGE) {
331 if (img.slideshow) {
332 img.slideshow = false;
333 reset_timeout(slideshow);
334 return true;
335 } else if (fileidx + 1 < filecnt) {
336 img.slideshow = true;
337 set_timeout(slideshow, img.ss_delay, true);
338 return true;
339 }
340 }
341 return false;
342}
343
344bool i_adjust_slideshow(arg_t a) {
345 long d = (long) a;
346 int i, delays[] = { 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600 };
347
348 if (mode != MODE_IMAGE || !img.slideshow)
349 return false;
350
351 if (d < 0) {
352 for (i = ARRLEN(delays) - 2; i >= 0; i--) {
353 if (img.ss_delay > delays[i] * 1000) {
354 img.ss_delay = delays[i] * 1000;
355 return true;
356 }
357 }
358 } else {
359 for (i = 1; i < ARRLEN(delays); i++) {
360 if (img.ss_delay < delays[i] * 1000) {
361 img.ss_delay = delays[i] * 1000;
362 return true;
363 }
364 }
365 }
366 return false;
367}
368
369bool i_toggle_antialias(arg_t a) {
370 if (mode == MODE_IMAGE) {
371 img_toggle_antialias(&img);
372 return true;
373 } else {
374 return false;
375 }
376}
377
378bool it_toggle_alpha(arg_t a) {
379 img.alpha = tns.alpha = !img.alpha;
380 if (mode == MODE_IMAGE)
381 img.dirty = true;
382 else
383 tns.dirty = true;
384 return true;
385}
386
387bool it_open_with(arg_t a) {
388 const char *prog = (const char*) a;
389 pid_t pid;
390
391 if (prog == NULL || *prog == '\0')
392 return false;
393
394 if ((pid = fork()) == 0) {
395 execlp(prog, prog,
396 files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
397 warn("could not exec: %s", prog);
398 exit(EXIT_FAILURE);
399 } else if (pid < 0) {
400 warn("could not fork. program was: %s", prog);
401 }
402 return false;
403}
404
405bool it_shell_cmd(arg_t a) {
406 int n, status;
407 const char *cmdline = (const char*) a;
408 pid_t pid;
409
410 if (cmdline == NULL || *cmdline == '\0')
411 return false;
412
413 n = mode == MODE_IMAGE ? fileidx : tns.sel;
414
415 if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
416 warn("could not set env.-variable: SXIV_IMG. command line was: %s",
417 cmdline);
418 return false;
419 }
420
421 if ((pid = fork()) == 0) {
422 execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
423 warn("could not exec: /bin/sh. command line was: %s", cmdline);
424 exit(EXIT_FAILURE);
425 } else if (pid < 0) {
426 warn("could not fork. command line was: %s", cmdline);
427 return false;
428 }
429
430 win_set_cursor(&win, CURSOR_WATCH);
431
432 waitpid(pid, &status, 0);
433 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
434 warn("child exited with non-zero return value: %d. command line was: %s",
435 WEXITSTATUS(status), cmdline);
436
437 if (mode == MODE_IMAGE) {
438 img_close(&img, true);
439 load_image(fileidx);
440 }
441 if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
442 mode == MODE_THUMB)
443 {
444 remove_file(tns.sel, false);
445 tns.dirty = true;
446 if (tns.sel >= tns.cnt)
447 tns.sel = tns.cnt - 1;
448 }
449 return true;
450}