Krolyxon
Commit 391e6e7

Branches

Refs →

nsxivcommands.c

423 lines · 8.3 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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <sys/wait.h>
23
24#include "commands.h"
25#include "image.h"
26#include "thumbs.h"
27#include "types.h"
28#include "util.h"
29
30void cleanup();
31void remove_file(int, unsigned char);
32void load_image(int);
33
34extern appmode_t mode;
35extern img_t img;
36extern tns_t tns;
37extern win_t win;
38
39extern fileinfo_t *files;
40extern int filecnt, fileidx;
41
42extern int timo_cursor;
43extern int timo_redraw;
44extern int timo_adelay;
45
46int it_quit(arg_t a) {
47 cleanup();
48 exit(0);
49}
50
51int it_switch_mode(arg_t a) {
52 if (mode == MODE_IMAGE) {
53 if (!tns.thumbs)
54 tns_init(&tns, filecnt);
55 img_close(&img, 0);
56 win_set_cursor(&win, CURSOR_ARROW);
57 timo_cursor = 0;
58 tns.sel = fileidx;
59 tns.dirty = 1;
60 mode = MODE_THUMB;
61 } else {
62 timo_cursor = TO_CURSOR_HIDE;
63 load_image(tns.sel);
64 mode = MODE_IMAGE;
65 }
66 return 1;
67}
68
69int it_toggle_fullscreen(arg_t a) {
70 win_toggle_fullscreen(&win);
71 if (mode == MODE_IMAGE)
72 img.checkpan = 1;
73 else
74 tns.dirty = 1;
75 timo_redraw = TO_WIN_RESIZE;
76 return 0;
77}
78
79int it_reload_image(arg_t a) {
80 if (mode == MODE_IMAGE) {
81 load_image(fileidx);
82 } else if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) {
83 remove_file(tns.sel, 0);
84 tns.dirty = 1;
85 if (tns.sel >= tns.cnt)
86 tns.sel = tns.cnt - 1;
87 }
88 return 1;
89}
90
91int it_remove_image(arg_t a) {
92 if (mode == MODE_IMAGE) {
93 remove_file(fileidx, 1);
94 load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
95 return 1;
96 } else if (tns.sel < tns.cnt) {
97 remove_file(tns.sel, 1);
98 tns.dirty = 1;
99 if (tns.sel >= tns.cnt)
100 tns.sel = tns.cnt - 1;
101 return 1;
102 } else {
103 return 0;
104 }
105}
106
107int i_navigate(arg_t a) {
108 int n = (int) a;
109
110 if (mode == MODE_IMAGE) {
111 n += fileidx;
112 if (n < 0)
113 n = 0;
114 if (n >= filecnt)
115 n = filecnt - 1;
116
117 if (n != fileidx) {
118 load_image(n);
119 return 1;
120 }
121 }
122 return 0;
123}
124
125int it_first(arg_t a) {
126 if (mode == MODE_IMAGE && fileidx != 0) {
127 load_image(0);
128 return 1;
129 } else if (mode == MODE_THUMB && tns.sel != 0) {
130 tns.sel = 0;
131 tns.dirty = 1;
132 return 1;
133 } else {
134 return 0;
135 }
136}
137
138int it_last(arg_t a) {
139 if (mode == MODE_IMAGE && fileidx != filecnt - 1) {
140 load_image(filecnt - 1);
141 return 1;
142 } else if (mode == MODE_THUMB && tns.sel != tns.cnt - 1) {
143 tns.sel = tns.cnt - 1;
144 tns.dirty = 1;
145 return 1;
146 } else {
147 return 0;
148 }
149}
150
151int i_navigate_frame(arg_t a) {
152 if (mode == MODE_IMAGE && !img.multi.animate)
153 return img_frame_navigate(&img, (int) a);
154 else
155 return 0;
156}
157
158int i_toggle_animation(arg_t a) {
159 if (mode != MODE_IMAGE)
160 return 0;
161
162 if (img.multi.animate) {
163 timo_adelay = 0;
164 img.multi.animate = 0;
165 return 0;
166 } else {
167 timo_adelay = img_frame_animate(&img, 1);
168 return 1;
169 }
170}
171
172int it_move(arg_t a) {
173 direction_t dir = (direction_t) a;
174
175 if (mode == MODE_IMAGE)
176 return img_pan(&img, &win, dir, 0);
177 else
178 return tns_move_selection(&tns, &win, dir);
179}
180
181int i_pan_screen(arg_t a) {
182 direction_t dir = (direction_t) a;
183
184 if (mode == MODE_IMAGE)
185 return img_pan(&img, &win, dir, 1);
186 else
187 return 0;
188}
189
190int i_pan_edge(arg_t a) {
191 direction_t dir = (direction_t) a;
192
193 if (mode == MODE_IMAGE)
194 return img_pan_edge(&img, &win, dir);
195 else
196 return 0;
197}
198
199/* Xlib helper function for i_drag() */
200Bool is_motionnotify(Display *d, XEvent *e, XPointer a) {
201 return e != NULL && e->type == MotionNotify;
202}
203
204int i_drag(arg_t a) {
205 int dx = 0, dy = 0, i, ox, oy, x, y;
206 unsigned int ui;
207 Bool dragging = True, next = False;
208 XEvent e;
209 Window w;
210
211 if (mode != MODE_IMAGE)
212 return 0;
213 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
214 return 0;
215
216 win_set_cursor(&win, CURSOR_HAND);
217
218 while (dragging) {
219 if (!next)
220 XMaskEvent(win.env.dpy,
221 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
222 switch (e.type) {
223 case ButtonPress:
224 case ButtonRelease:
225 dragging = False;
226 break;
227 case MotionNotify:
228 x = e.xmotion.x;
229 y = e.xmotion.y;
230 if (x >= 0 && x <= win.w && y >= 0 && y <= win.h) {
231 dx += x - ox;
232 dy += y - oy;
233 }
234 ox = x;
235 oy = y;
236 break;
237 }
238 if (dragging)
239 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
240 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
241 if (img_move(&img, &win, dx, dy))
242 img_render(&img, &win);
243 dx = dy = 0;
244 }
245 }
246
247 win_set_cursor(&win, CURSOR_ARROW);
248 timo_cursor = TO_CURSOR_HIDE;
249 timo_redraw = 0;
250
251 return 0;
252}
253
254int i_zoom(arg_t a) {
255 int scale = (int) a;
256
257 if (mode != MODE_IMAGE)
258 return 0;
259
260 if (scale > 0)
261 return img_zoom_in(&img, &win);
262 else if (scale < 0)
263 return img_zoom_out(&img, &win);
264 else
265 return img_zoom(&img, &win, 1.0);
266}
267
268int i_fit_to_win(arg_t a) {
269 int ret;
270
271 if (mode == MODE_IMAGE) {
272 if ((ret = img_fit_win(&img, &win)))
273 img_center(&img, &win);
274 return ret;
275 } else {
276 return 0;
277 }
278}
279
280int i_fit_to_img(arg_t a) {
281 int ret, x, y;
282 unsigned int w, h;
283
284 if (mode == MODE_IMAGE) {
285 x = MAX(0, win.x + img.x);
286 y = MAX(0, win.y + img.y);
287 w = img.w * img.zoom;
288 h = img.h * img.zoom;
289 if ((ret = win_moveresize(&win, x, y, w, h))) {
290 img.x = x - win.x;
291 img.y = y - win.y;
292 }
293 return ret;
294 } else {
295 return 0;
296 }
297}
298
299int i_rotate(arg_t a) {
300 direction_t dir = (direction_t) a;
301
302 if (mode == MODE_IMAGE) {
303 if (dir == DIR_LEFT) {
304 img_rotate_left(&img, &win);
305 return 1;
306 } else if (dir == DIR_RIGHT) {
307 img_rotate_right(&img, &win);
308 return 1;
309 }
310 }
311 return 0;
312}
313
314int i_toggle_antialias(arg_t a) {
315 if (mode == MODE_IMAGE) {
316 img_toggle_antialias(&img);
317 return 1;
318 } else {
319 return 0;
320 }
321}
322
323int i_toggle_alpha(arg_t a) {
324 if (mode == MODE_IMAGE) {
325 img.alpha ^= 1;
326 return 1;
327 } else {
328 return 0;
329 }
330}
331
332int it_open_with(arg_t a) {
333 const char *prog = (const char*) a;
334 pid_t pid;
335
336 if (!prog || !*prog)
337 return 0;
338
339 if((pid = fork()) == 0) {
340 execlp(prog, prog,
341 files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
342 warn("could not exec: %s", prog);
343 exit(1);
344 } else if (pid < 0) {
345 warn("could not for. program was: %s", prog);
346 }
347
348 return 0;
349}
350
351int it_shell_cmd(arg_t a) {
352 const char *cline = (const char*) a;
353 char *cn, *cmdline;
354 const char *co, *fpath;
355 int fpcnt, fplen, status;
356 pid_t pid;
357
358 if (!cline || !*cline)
359 return 0;
360
361 /* build command line: */
362 fpcnt = 0;
363 co = cline - 1;
364 while ((co = strchr(co + 1, '#')))
365 fpcnt++;
366 if (!fpcnt)
367 return 0;
368 fpath = files[mode == MODE_IMAGE ? fileidx : tns.sel].path;
369 fplen = strlen(fpath);
370 cn = cmdline = (char*) s_malloc((strlen(cline) + fpcnt * (fplen + 2)) *
371 sizeof(char));
372 /* replace all '#' with filename: */
373 for (co = cline; *co; co++) {
374 if (*co == '#') {
375 *cn++ = '"';
376 strcpy(cn, fpath);
377 cn += fplen;
378 *cn++ = '"';
379 } else {
380 *cn++ = *co;
381 }
382 }
383 *cn = '\0';
384
385 win_set_cursor(&win, CURSOR_WATCH);
386
387 if ((pid = fork()) == 0) {
388 execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
389 warn("could not exec: /bin/sh");
390 exit(1);
391 } else if (pid < 0) {
392 warn("could not fork. command line was: %s", cmdline);
393 goto end;
394 }
395
396 waitpid(pid, &status, 0);
397 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
398 warn("child exited with non-zero return value: %d. command line was: %s",
399 WEXITSTATUS(status), cmdline);
400
401 if (mode == MODE_IMAGE) {
402 if (fileidx < tns.cnt)
403 tns_load(&tns, fileidx, &files[fileidx], False, True);
404 img_close(&img, 1);
405 load_image(fileidx);
406 } else {
407 if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) {
408 remove_file(tns.sel, 0);
409 tns.dirty = 1;
410 if (tns.sel >= tns.cnt)
411 tns.sel = tns.cnt - 1;
412 }
413 }
414
415end:
416 if (mode == MODE_THUMB)
417 win_set_cursor(&win, CURSOR_ARROW);
418 /* else: cursor gets reset in redraw() */
419
420 free(cmdline);
421
422 return 1;
423}