Krolyxon
Commit d721d84

Branches

Refs →

nsxivcommands.c

445 lines · 9.0 KB · C

1/* sxiv: commands.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
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();
32void remove_file(int, bool);
33void load_image(int);
34void redraw();
35void reset_cursor();
36void animate();
37void slideshow();
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
49bool it_quit(arg_t a) {
50 cleanup();
51 exit(EXIT_SUCCESS);
52}
53
54bool it_switch_mode(arg_t a) {
55 if (mode == MODE_IMAGE) {
56 if (!tns.thumbs)
57 tns_init(&tns, filecnt, &win);
58 img_close(&img, false);
59 reset_timeout(reset_cursor);
60 if (img.slideshow) {
61 img.slideshow = false;
62 reset_timeout(slideshow);
63 }
64 tns.sel = fileidx;
65 tns.dirty = true;
66 mode = MODE_THUMB;
67 } else {
68 load_image(tns.sel);
69 mode = MODE_IMAGE;
70 }
71 return true;
72}
73
74bool it_toggle_fullscreen(arg_t a) {
75 win_toggle_fullscreen(&win);
76 set_timeout(redraw, TO_REDRAW_RESIZE, false);
77 if (mode == MODE_IMAGE)
78 img.checkpan = true;
79 else
80 tns.dirty = true;
81 return false;
82}
83
84bool it_reload_image(arg_t a) {
85 if (mode == MODE_IMAGE) {
86 load_image(fileidx);
87 } else {
88 win_set_cursor(&win, CURSOR_WATCH);
89 if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) {
90 remove_file(tns.sel, false);
91 tns.dirty = true;
92 if (tns.sel >= tns.cnt)
93 tns.sel = tns.cnt - 1;
94 }
95 }
96 return true;
97}
98
99bool it_remove_image(arg_t a) {
100 if (mode == MODE_IMAGE) {
101 remove_file(fileidx, true);
102 load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
103 return true;
104 } else if (tns.sel < tns.cnt) {
105 remove_file(tns.sel, true);
106 tns.dirty = true;
107 if (tns.sel >= tns.cnt)
108 tns.sel = tns.cnt - 1;
109 return true;
110 } else {
111 return false;
112 }
113}
114
115bool i_navigate(arg_t a) {
116 long n = (long) a;
117
118 if (mode == MODE_IMAGE) {
119 n += fileidx;
120 if (n < 0)
121 n = 0;
122 if (n >= filecnt)
123 n = filecnt - 1;
124
125 if (n != fileidx) {
126 load_image(n);
127 return true;
128 }
129 }
130 return false;
131}
132
133bool it_first(arg_t a) {
134 if (mode == MODE_IMAGE && fileidx != 0) {
135 load_image(0);
136 return true;
137 } else if (mode == MODE_THUMB && tns.sel != 0) {
138 tns.sel = 0;
139 tns.dirty = true;
140 return true;
141 } else {
142 return false;
143 }
144}
145
146bool it_last(arg_t a) {
147 if (mode == MODE_IMAGE && fileidx != filecnt - 1) {
148 load_image(filecnt - 1);
149 return true;
150 } else if (mode == MODE_THUMB && tns.sel != tns.cnt - 1) {
151 tns.sel = tns.cnt - 1;
152 tns.dirty = true;
153 return true;
154 } else {
155 return false;
156 }
157}
158
159bool i_navigate_frame(arg_t a) {
160 if (mode == MODE_IMAGE && !img.multi.animate)
161 return img_frame_navigate(&img, (long) a);
162 else
163 return false;
164}
165
166bool i_toggle_animation(arg_t a) {
167 if (mode != MODE_IMAGE)
168 return false;
169
170 if (img.multi.animate) {
171 reset_timeout(animate);
172 img.multi.animate = false;
173 } else if (img_frame_animate(&img, true)) {
174 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
175 }
176 return true;
177}
178
179bool it_move(arg_t a) {
180 direction_t dir = (direction_t) a;
181
182 if (mode == MODE_IMAGE)
183 return img_pan(&img, dir, false);
184 else
185 return tns_move_selection(&tns, dir);
186}
187
188bool i_pan_screen(arg_t a) {
189 direction_t dir = (direction_t) a;
190
191 if (mode == MODE_IMAGE)
192 return img_pan(&img, dir, true);
193 else
194 return false;
195}
196
197bool i_pan_edge(arg_t a) {
198 direction_t dir = (direction_t) a;
199
200 if (mode == MODE_IMAGE)
201 return img_pan_edge(&img, dir);
202 else
203 return false;
204}
205
206/* Xlib helper function for i_drag() */
207Bool is_motionnotify(Display *d, XEvent *e, XPointer a) {
208 return e != NULL && e->type == MotionNotify;
209}
210
211bool i_drag(arg_t a) {
212 int dx = 0, dy = 0, i, ox, oy, x, y;
213 unsigned int ui;
214 bool dragging = true, next = false;
215 XEvent e;
216 Window w;
217
218 if (mode != MODE_IMAGE)
219 return false;
220 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
221 return false;
222
223 win_set_cursor(&win, CURSOR_HAND);
224
225 while (dragging) {
226 if (!next)
227 XMaskEvent(win.env.dpy,
228 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
229 switch (e.type) {
230 case ButtonPress:
231 case ButtonRelease:
232 dragging = false;
233 break;
234 case MotionNotify:
235 x = e.xmotion.x;
236 y = e.xmotion.y;
237 if (x >= 0 && x <= win.w && y >= 0 && y <= win.h) {
238 dx += x - ox;
239 dy += y - oy;
240 }
241 ox = x;
242 oy = y;
243 break;
244 }
245 if (dragging)
246 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
247 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
248 if (img_move(&img, dx, dy))
249 img_render(&img);
250 dx = dy = 0;
251 }
252 }
253
254 win_set_cursor(&win, CURSOR_ARROW);
255 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
256 reset_timeout(redraw);
257
258 return false;
259}
260
261bool i_zoom(arg_t a) {
262 long scale = (long) a;
263
264 if (mode != MODE_IMAGE)
265 return false;
266
267 if (scale > 0)
268 return img_zoom_in(&img);
269 else if (scale < 0)
270 return img_zoom_out(&img);
271 else
272 return img_zoom(&img, 1.0);
273}
274
275bool i_fit_to_win(arg_t a) {
276 bool ret;
277
278 if (mode == MODE_IMAGE) {
279 if ((ret = img_fit_win(&img)))
280 img_center(&img);
281 return ret;
282 } else {
283 return false;
284 }
285}
286
287bool i_fit_to_img(arg_t a) {
288 int x, y;
289 unsigned int w, h;
290 bool ret;
291
292 if (mode == MODE_IMAGE) {
293 x = MAX(0, win.x + img.x);
294 y = MAX(0, win.y + img.y);
295 w = img.w * img.zoom;
296 h = img.h * img.zoom;
297 if ((ret = win_moveresize(&win, x, y, w, h))) {
298 img.x = x - win.x;
299 img.y = y - win.y;
300 }
301 return ret;
302 } else {
303 return false;
304 }
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 || !*prog)
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
396 return false;
397}
398
399bool it_shell_cmd(arg_t a) {
400 int n, status;
401 const char *cmdline = (const char*) a;
402 pid_t pid;
403
404 if (!cmdline || !*cmdline)
405 return 0;
406
407 n = mode == MODE_IMAGE ? fileidx : tns.sel;
408
409 if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
410 warn("could not set env.-variable: SXIV_IMG. command line was: %s",
411 cmdline);
412 return false;
413 }
414
415 if ((pid = fork()) == 0) {
416 execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
417 warn("could not exec: /bin/sh. command line was: %s", cmdline);
418 exit(EXIT_FAILURE);
419 } else if (pid < 0) {
420 warn("could not fork. command line was: %s", cmdline);
421 return false;
422 }
423
424 win_set_cursor(&win, CURSOR_WATCH);
425
426 waitpid(pid, &status, 0);
427 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
428 warn("child exited with non-zero return value: %d. command line was: %s",
429 WEXITSTATUS(status), cmdline);
430
431 if (mode == MODE_IMAGE) {
432 img_close(&img, true);
433 load_image(fileidx);
434 }
435 if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
436 mode == MODE_THUMB)
437 {
438 remove_file(tns.sel, false);
439 tns.dirty = true;
440 if (tns.sel >= tns.cnt)
441 tns.sel = tns.cnt - 1;
442 }
443
444 return true;
445}