Krolyxon
Commit 37a42db

Branches

Refs →

nsxivcommands.c

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