Krolyxon
Commit 38bc234

Branches

Refs →

nsxivcommands.c

552 lines · 10.3 KB · C

1/* Copyright 2011, 2012 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
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 "options.h"
30#include "thumbs.h"
31#include "util.h"
32#include "config.h"
33
34void cleanup(void);
35void remove_file(int, bool);
36void load_image(int);
37void open_info(void);
38void redraw(void);
39void reset_cursor(void);
40void animate(void);
41void set_timeout(timeout_f, int, bool);
42void reset_timeout(timeout_f);
43
44extern appmode_t mode;
45extern img_t img;
46extern tns_t tns;
47extern win_t win;
48
49extern fileinfo_t *files;
50extern int filecnt, fileidx;
51extern int alternate;
52
53extern int prefix;
54
55const int ss_delays[] = {
56 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
57};
58
59bool it_quit(arg_t a)
60{
61 unsigned int i;
62
63 if (options->to_stdout) {
64 for (i = 0; i < filecnt; i++) {
65 if (files[i].marked)
66 printf("%s\n", files[i].name);
67 }
68 }
69 cleanup();
70 exit(EXIT_SUCCESS);
71}
72
73bool it_switch_mode(arg_t a)
74{
75 if (mode == MODE_IMAGE) {
76 if (tns.thumbs == NULL) {
77 tns_init(&tns, filecnt, &win);
78 tns.alpha = img.alpha;
79 }
80 img_close(&img, false);
81 reset_timeout(reset_cursor);
82 tns.sel = fileidx;
83 tns.dirty = true;
84 mode = MODE_THUMB;
85 } else {
86 load_image(tns.sel);
87 mode = MODE_IMAGE;
88 }
89 return true;
90}
91
92bool it_toggle_fullscreen(arg_t a)
93{
94 win_toggle_fullscreen(&win);
95 /* redraw after next ConfigureNotify event */
96 set_timeout(redraw, TO_REDRAW_RESIZE, false);
97 if (mode == MODE_IMAGE)
98 img.checkpan = img.dirty = true;
99 else
100 tns.dirty = true;
101 return false;
102}
103
104bool it_toggle_bar(arg_t a)
105{
106 win_toggle_bar(&win);
107 if (mode == MODE_IMAGE) {
108 img.checkpan = img.dirty = true;
109 if (win.bar.h > 0)
110 open_info();
111 } else {
112 tns.dirty = true;
113 }
114 return true;
115}
116
117bool t_reload_all(arg_t a)
118{
119 if (mode == MODE_THUMB) {
120 tns_free(&tns);
121 tns_init(&tns, filecnt, &win);
122 return true;
123 } else {
124 return false;
125 }
126}
127
128bool it_reload_image(arg_t a)
129{
130 if (mode == MODE_IMAGE) {
131 load_image(fileidx);
132 } else {
133 win_set_cursor(&win, CURSOR_WATCH);
134 if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) {
135 remove_file(tns.sel, false);
136 tns.dirty = true;
137 if (tns.sel >= tns.cnt)
138 tns.sel = tns.cnt - 1;
139 }
140 }
141 return true;
142}
143
144bool it_remove_image(arg_t a)
145{
146 if (mode == MODE_IMAGE) {
147 remove_file(fileidx, true);
148 load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
149 return true;
150 } else if (tns.sel < tns.cnt) {
151 remove_file(tns.sel, true);
152 tns.dirty = true;
153 if (tns.sel >= tns.cnt)
154 tns.sel = tns.cnt - 1;
155 return true;
156 } else {
157 return false;
158 }
159}
160
161bool i_navigate(arg_t a)
162{
163 long n = (long) a;
164
165 if (mode == MODE_IMAGE) {
166 if (prefix > 0)
167 n *= prefix;
168 n += fileidx;
169 if (n < 0)
170 n = 0;
171 if (n >= filecnt)
172 n = filecnt - 1;
173
174 if (n != fileidx) {
175 load_image(n);
176 return true;
177 }
178 }
179 return false;
180}
181
182bool i_alternate(arg_t a)
183{
184 if (mode == MODE_IMAGE) {
185 load_image(alternate);
186 return true;
187 } else {
188 return false;
189 }
190}
191
192bool it_first(arg_t a)
193{
194 if (mode == MODE_IMAGE && fileidx != 0) {
195 load_image(0);
196 return true;
197 } else if (mode == MODE_THUMB && tns.sel != 0) {
198 tns.sel = 0;
199 tns.dirty = true;
200 return true;
201 } else {
202 return false;
203 }
204}
205
206bool it_n_or_last(arg_t a)
207{
208 int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
209
210 if (mode == MODE_IMAGE && fileidx != n) {
211 load_image(n);
212 return true;
213 } else if (mode == MODE_THUMB && tns.sel != n) {
214 tns.sel = n;
215 tns.dirty = true;
216 return true;
217 } else {
218 return false;
219 }
220}
221
222bool i_navigate_frame(arg_t a)
223{
224 if (mode == MODE_IMAGE && !img.multi.animate)
225 return img_frame_navigate(&img, (long) a);
226 else
227 return false;
228}
229
230bool i_toggle_animation(arg_t a)
231{
232 if (mode != MODE_IMAGE)
233 return false;
234
235 if (img.multi.animate) {
236 reset_timeout(animate);
237 img.multi.animate = false;
238 } else if (img_frame_animate(&img, true)) {
239 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
240 }
241 return true;
242}
243
244bool it_toggle_image_mark(arg_t a)
245{
246 int sel = mode == MODE_IMAGE ? fileidx : tns.sel;
247
248 files[sel].marked = !files[sel].marked;
249 if (mode == MODE_THUMB)
250 tns_mark(&tns, sel, files[sel].marked);
251 return true;
252}
253
254bool it_navigate_marked(arg_t a)
255{
256 long n = (long) a;
257 int d, i, cnt, sel, new;
258
259 if (mode == MODE_IMAGE)
260 cnt = filecnt, sel = new = fileidx;
261 else
262 cnt = tns.cnt, sel = new = tns.sel;
263 if (prefix > 0)
264 n *= prefix;
265 d = n > 0 ? 1 : -1;
266 for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
267 if (files[i].marked) {
268 n -= d;
269 new = i;
270 }
271 }
272 if (new != sel) {
273 if (mode == MODE_IMAGE) {
274 load_image(new);
275 } else {
276 tns.sel = new;
277 tns.dirty = true;
278 }
279 return true;
280 } else {
281 return false;
282 }
283}
284
285bool it_scroll_move(arg_t a)
286{
287 direction_t dir = (direction_t) a;
288
289 if (mode == MODE_IMAGE)
290 return img_pan(&img, dir, prefix);
291 else
292 return tns_move_selection(&tns, dir, prefix);
293}
294
295bool it_scroll_screen(arg_t a)
296{
297 direction_t dir = (direction_t) a;
298
299 if (mode == MODE_IMAGE)
300 return img_pan(&img, dir, -1);
301 else
302 return tns_scroll(&tns, dir, true);
303}
304
305bool i_scroll_to_edge(arg_t a)
306{
307 direction_t dir = (direction_t) a;
308
309 if (mode == MODE_IMAGE)
310 return img_pan_edge(&img, dir);
311 else
312 return false;
313}
314
315/* Xlib helper function for i_drag() */
316Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
317{
318 return e != NULL && e->type == MotionNotify;
319}
320
321#define WARP(x,y) \
322 XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
323 ox = x, oy = y; \
324 break
325
326bool i_drag(arg_t a)
327{
328 int dx = 0, dy = 0, i, ox, oy, x, y;
329 unsigned int ui;
330 bool dragging = true, next = false;
331 XEvent e;
332 Window w;
333
334 if (mode != MODE_IMAGE)
335 return false;
336 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
337 return false;
338
339 win_set_cursor(&win, CURSOR_HAND);
340
341 while (dragging) {
342 if (!next)
343 XMaskEvent(win.env.dpy,
344 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
345 switch (e.type) {
346 case ButtonPress:
347 case ButtonRelease:
348 dragging = false;
349 break;
350 case MotionNotify:
351 x = e.xmotion.x;
352 y = e.xmotion.y;
353
354 /* wrap the mouse around */
355 if (x <= 0) {
356 WARP(win.w - 2, y);
357 } else if (x >= win.w - 1) {
358 WARP(1, y);
359 } else if (y <= 0) {
360 WARP(x, win.h - 2);
361 } else if (y >= win.h - 1) {
362 WARP(x, 1);
363 }
364 dx += x - ox;
365 dy += y - oy;
366 ox = x;
367 oy = y;
368 break;
369 }
370 if (dragging)
371 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
372 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
373 if (img_move(&img, dx, dy)) {
374 img_render(&img);
375 win_draw(&win);
376 }
377 dx = dy = 0;
378 }
379 }
380
381 win_set_cursor(&win, CURSOR_ARROW);
382 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
383 reset_timeout(redraw);
384
385 return false;
386}
387
388bool i_zoom(arg_t a)
389{
390 long scale = (long) a;
391
392 if (mode != MODE_IMAGE)
393 return false;
394
395 if (scale > 0)
396 return img_zoom_in(&img);
397 else if (scale < 0)
398 return img_zoom_out(&img);
399 else
400 return false;
401}
402
403bool i_set_zoom(arg_t a)
404{
405 if (mode == MODE_IMAGE)
406 return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
407 else
408 return false;
409}
410
411bool i_fit_to_win(arg_t a)
412{
413 bool ret = false;
414 scalemode_t sm = (scalemode_t) a;
415
416 if (mode == MODE_IMAGE) {
417 if ((ret = img_fit_win(&img, sm)))
418 img_center(&img);
419 }
420 return ret;
421}
422
423bool i_fit_to_img(arg_t a)
424{
425 int x, y;
426 unsigned int w, h;
427 bool ret = false;
428
429 if (mode == MODE_IMAGE) {
430 x = MAX(0, win.x + img.x);
431 y = MAX(0, win.y + img.y);
432 w = img.w * img.zoom;
433 h = img.h * img.zoom;
434 if ((ret = win_moveresize(&win, x, y, w, h))) {
435 img.x = x - win.x;
436 img.y = y - win.y;
437 img.dirty = true;
438 }
439 }
440 return ret;
441}
442
443bool i_rotate(arg_t a)
444{
445 degree_t degree = (degree_t) a;
446
447 if (mode == MODE_IMAGE) {
448 img_rotate(&img, degree);
449 return true;
450 } else {
451 return false;
452 }
453}
454
455bool i_flip(arg_t a)
456{
457 flipdir_t dir = (flipdir_t) a;
458
459 if (mode == MODE_IMAGE) {
460 img_flip(&img, dir);
461 return true;
462 } else {
463 return false;
464 }
465}
466
467bool i_toggle_antialias(arg_t a)
468{
469 if (mode == MODE_IMAGE) {
470 img_toggle_antialias(&img);
471 return true;
472 } else {
473 return false;
474 }
475}
476
477bool it_toggle_alpha(arg_t a)
478{
479 img.alpha = tns.alpha = !img.alpha;
480 if (mode == MODE_IMAGE)
481 img.dirty = true;
482 else
483 tns.dirty = true;
484 return true;
485}
486
487bool it_open_with(arg_t a)
488{
489 const char *prog = (const char*) a;
490 pid_t pid;
491
492 if (prog == NULL || *prog == '\0')
493 return false;
494
495 if ((pid = fork()) == 0) {
496 execlp(prog, prog,
497 files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
498 warn("could not exec: %s", prog);
499 exit(EXIT_FAILURE);
500 } else if (pid < 0) {
501 warn("could not fork. program was: %s", prog);
502 }
503 return false;
504}
505
506bool it_shell_cmd(arg_t a)
507{
508 int n, status;
509 const char *cmdline = (const char*) a;
510 pid_t pid;
511
512 if (cmdline == NULL || *cmdline == '\0')
513 return false;
514
515 n = mode == MODE_IMAGE ? fileidx : tns.sel;
516
517 if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
518 warn("could not set env.-variable: SXIV_IMG. command line was: %s",
519 cmdline);
520 return false;
521 }
522
523 if ((pid = fork()) == 0) {
524 execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
525 warn("could not exec: /bin/sh. command line was: %s", cmdline);
526 exit(EXIT_FAILURE);
527 } else if (pid < 0) {
528 warn("could not fork. command line was: %s", cmdline);
529 return false;
530 }
531
532 win_set_cursor(&win, CURSOR_WATCH);
533
534 waitpid(pid, &status, 0);
535 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
536 warn("child exited with non-zero return value: %d. command line was: %s",
537 WEXITSTATUS(status), cmdline);
538
539 if (mode == MODE_IMAGE) {
540 img_close(&img, true);
541 load_image(fileidx);
542 }
543 if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
544 mode == MODE_THUMB)
545 {
546 remove_file(tns.sel, false);
547 tns.dirty = true;
548 if (tns.sel >= tns.cnt)
549 tns.sel = tns.cnt - 1;
550 }
551 return true;
552}