Krolyxon
Commit 091aacb

Branches

Refs →

nsxivcommands.c

572 lines · 10.6 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_reverse_marks(arg_t a)
255{
256 int i, cnt = mode == MODE_IMAGE ? filecnt : tns.cnt;
257
258 for (i = 0; i < cnt; i++)
259 files[i].marked = !files[i].marked;
260 if (mode == MODE_THUMB)
261 tns.dirty = true;
262 return true;
263}
264
265bool it_navigate_marked(arg_t a)
266{
267 long n = (long) a;
268 int d, i, cnt, sel, new;
269
270 if (mode == MODE_IMAGE)
271 cnt = filecnt, sel = new = fileidx;
272 else
273 cnt = tns.cnt, sel = new = tns.sel;
274 if (prefix > 0)
275 n *= prefix;
276 d = n > 0 ? 1 : -1;
277 for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
278 if (files[i].marked) {
279 n -= d;
280 new = i;
281 }
282 }
283 if (new != sel) {
284 if (mode == MODE_IMAGE) {
285 load_image(new);
286 } else {
287 tns.sel = new;
288 tns.dirty = true;
289 }
290 return true;
291 } else {
292 return false;
293 }
294}
295
296bool it_scroll_move(arg_t a)
297{
298 direction_t dir = (direction_t) a;
299
300 if (mode == MODE_IMAGE)
301 return img_pan(&img, dir, prefix);
302 else
303 return tns_move_selection(&tns, dir, prefix);
304}
305
306bool it_scroll_screen(arg_t a)
307{
308 direction_t dir = (direction_t) a;
309
310 if (mode == MODE_IMAGE)
311 return img_pan(&img, dir, -1);
312 else
313 return tns_scroll(&tns, dir, true);
314}
315
316bool i_scroll_to_edge(arg_t a)
317{
318 direction_t dir = (direction_t) a;
319
320 if (mode == MODE_IMAGE)
321 return img_pan_edge(&img, dir);
322 else
323 return false;
324}
325
326/* Xlib helper function for i_drag() */
327Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
328{
329 return e != NULL && e->type == MotionNotify;
330}
331
332#define WARP(x,y) \
333 XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
334 ox = x, oy = y; \
335 break
336
337bool i_drag(arg_t a)
338{
339 int dx = 0, dy = 0, i, ox, oy, x, y;
340 unsigned int ui;
341 bool dragging = true, next = false;
342 XEvent e;
343 Window w;
344
345 if (mode != MODE_IMAGE)
346 return false;
347 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
348 return false;
349
350 win_set_cursor(&win, CURSOR_HAND);
351
352 while (dragging) {
353 if (!next)
354 XMaskEvent(win.env.dpy,
355 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
356 switch (e.type) {
357 case ButtonPress:
358 case ButtonRelease:
359 dragging = false;
360 break;
361 case MotionNotify:
362 x = e.xmotion.x;
363 y = e.xmotion.y;
364
365 /* wrap the mouse around */
366 if (x <= 0) {
367 WARP(win.w - 2, y);
368 } else if (x >= win.w - 1) {
369 WARP(1, y);
370 } else if (y <= 0) {
371 WARP(x, win.h - 2);
372 } else if (y >= win.h - 1) {
373 WARP(x, 1);
374 }
375 dx += x - ox;
376 dy += y - oy;
377 ox = x;
378 oy = y;
379 break;
380 }
381 if (dragging)
382 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
383 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
384 if (img_move(&img, dx, dy)) {
385 img_render(&img);
386 win_draw(&win);
387 }
388 dx = dy = 0;
389 }
390 }
391
392 win_set_cursor(&win, CURSOR_ARROW);
393 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
394 reset_timeout(redraw);
395
396 return false;
397}
398
399bool i_zoom(arg_t a)
400{
401 long scale = (long) a;
402
403 if (mode != MODE_IMAGE)
404 return false;
405
406 if (scale > 0)
407 return img_zoom_in(&img);
408 else if (scale < 0)
409 return img_zoom_out(&img);
410 else
411 return false;
412}
413
414bool i_set_zoom(arg_t a)
415{
416 if (mode == MODE_IMAGE)
417 return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
418 else
419 return false;
420}
421
422bool i_fit_to_win(arg_t a)
423{
424 bool ret = false;
425 scalemode_t sm = (scalemode_t) a;
426
427 if (mode == MODE_IMAGE) {
428 if ((ret = img_fit_win(&img, sm)))
429 img_center(&img);
430 }
431 return ret;
432}
433
434bool i_fit_to_img(arg_t a)
435{
436 int x, y;
437 unsigned int w, h;
438 bool ret = false;
439
440 if (mode == MODE_IMAGE) {
441 x = MAX(0, win.x + img.x);
442 y = MAX(0, win.y + img.y);
443 w = img.w * img.zoom;
444 h = img.h * img.zoom;
445 if ((ret = win_moveresize(&win, x, y, w, h))) {
446 img.x = x - win.x;
447 img.y = y - win.y;
448 img.dirty = true;
449 }
450 }
451 return ret;
452}
453
454bool i_rotate(arg_t a)
455{
456 degree_t degree = (degree_t) a;
457
458 if (mode == MODE_IMAGE) {
459 img_rotate(&img, degree);
460 return true;
461 } else {
462 return false;
463 }
464}
465
466bool i_flip(arg_t a)
467{
468 flipdir_t dir = (flipdir_t) a;
469
470 if (mode == MODE_IMAGE) {
471 img_flip(&img, dir);
472 return true;
473 } else {
474 return false;
475 }
476}
477
478bool i_toggle_antialias(arg_t a)
479{
480 if (mode == MODE_IMAGE) {
481 img_toggle_antialias(&img);
482 return true;
483 } else {
484 return false;
485 }
486}
487
488bool i_change_gamma(arg_t a)
489{
490 if (mode == MODE_IMAGE) {
491 return img_change_gamma(&img, (long) a);
492 } else {
493 return false;
494 }
495}
496
497bool it_toggle_alpha(arg_t a)
498{
499 img.alpha = tns.alpha = !img.alpha;
500 if (mode == MODE_IMAGE)
501 img.dirty = true;
502 else
503 tns.dirty = true;
504 return true;
505}
506
507bool it_open_with(arg_t a)
508{
509 const char *prog = (const char*) a;
510 pid_t pid;
511
512 if (prog == NULL || *prog == '\0')
513 return false;
514
515 if ((pid = fork()) == 0) {
516 execlp(prog, prog,
517 files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
518 warn("could not exec: %s", prog);
519 exit(EXIT_FAILURE);
520 } else if (pid < 0) {
521 warn("could not fork. program was: %s", prog);
522 }
523 return false;
524}
525
526bool it_shell_cmd(arg_t a)
527{
528 int n, status;
529 const char *cmdline = (const char*) a;
530 pid_t pid;
531
532 if (cmdline == NULL || *cmdline == '\0')
533 return false;
534
535 n = mode == MODE_IMAGE ? fileidx : tns.sel;
536
537 if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
538 warn("could not set env.-variable: SXIV_IMG. command line was: %s",
539 cmdline);
540 return false;
541 }
542
543 if ((pid = fork()) == 0) {
544 execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
545 warn("could not exec: /bin/sh. command line was: %s", cmdline);
546 exit(EXIT_FAILURE);
547 } else if (pid < 0) {
548 warn("could not fork. command line was: %s", cmdline);
549 return false;
550 }
551
552 win_set_cursor(&win, CURSOR_WATCH);
553
554 waitpid(pid, &status, 0);
555 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
556 warn("child exited with non-zero return value: %d. command line was: %s",
557 WEXITSTATUS(status), cmdline);
558
559 if (mode == MODE_IMAGE) {
560 img_close(&img, true);
561 load_image(fileidx);
562 }
563 if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
564 mode == MODE_THUMB)
565 {
566 remove_file(tns.sel, false);
567 tns.dirty = true;
568 if (tns.sel >= tns.cnt)
569 tns.sel = tns.cnt - 1;
570 }
571 return true;
572}