Krolyxon
Commit 2d4264a

Branches

Refs →

nsxivcommands.c

424 lines · 8.7 KB · C

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