Krolyxon
Commit 43a04c4

Branches

Refs →

nsxivcommands.c

515 lines · 9.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 slideshow(void);
42void set_timeout(timeout_f, int, bool);
43void reset_timeout(timeout_f);
44
45extern appmode_t mode;
46extern img_t img;
47extern tns_t tns;
48extern win_t win;
49
50extern fileinfo_t *files;
51extern int filecnt, fileidx;
52extern int alternate;
53
54extern int prefix;
55extern bool extprefix;
56
57const int ss_delays[] = {
58 1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 180, 300, 600
59};
60
61cmdreturn_t it_quit(arg_t a)
62{
63 unsigned int i;
64
65 if (options->to_stdout) {
66 for (i = 0; i < filecnt; i++) {
67 if (files[i].marked)
68 printf("%s\n", files[i].name);
69 }
70 }
71 cleanup();
72 exit(EXIT_SUCCESS);
73}
74
75cmdreturn_t it_switch_mode(arg_t a)
76{
77 if (mode == MODE_IMAGE) {
78 if (tns.thumbs == NULL) {
79 tns_init(&tns, filecnt, &win);
80 tns.alpha = img.alpha;
81 }
82 img_close(&img, false);
83 reset_timeout(reset_cursor);
84 if (img.ss.on) {
85 img.ss.on = false;
86 reset_timeout(slideshow);
87 }
88 tns.sel = fileidx;
89 tns.dirty = true;
90 mode = MODE_THUMB;
91 } else {
92 load_image(tns.sel);
93 mode = MODE_IMAGE;
94 }
95 return CMD_DIRTY;
96}
97
98cmdreturn_t it_toggle_fullscreen(arg_t a)
99{
100 win_toggle_fullscreen(&win);
101 /* redraw after next ConfigureNotify event */
102 set_timeout(redraw, TO_REDRAW_RESIZE, false);
103 if (mode == MODE_IMAGE)
104 img.checkpan = img.dirty = true;
105 else
106 tns.dirty = true;
107 return CMD_OK;
108}
109
110cmdreturn_t it_toggle_bar(arg_t a)
111{
112 win_toggle_bar(&win);
113 if (mode == MODE_IMAGE) {
114 img.checkpan = img.dirty = true;
115 if (win.bar.h > 0)
116 open_info();
117 } else {
118 tns.dirty = true;
119 }
120 return CMD_DIRTY;
121}
122
123cmdreturn_t it_prefix_external(arg_t a)
124{
125 extprefix = true;
126 return CMD_OK;
127}
128
129cmdreturn_t t_reload_all(arg_t a)
130{
131 if (mode == MODE_THUMB) {
132 tns_free(&tns);
133 tns_init(&tns, filecnt, &win);
134 return CMD_DIRTY;
135 } else {
136 return CMD_INVALID;
137 }
138}
139
140cmdreturn_t it_reload_image(arg_t a)
141{
142 if (mode == MODE_IMAGE) {
143 load_image(fileidx);
144 } else {
145 win_set_cursor(&win, CURSOR_WATCH);
146 if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) {
147 remove_file(tns.sel, false);
148 tns.dirty = true;
149 if (tns.sel >= tns.cnt)
150 tns.sel = tns.cnt - 1;
151 }
152 }
153 return CMD_DIRTY;
154}
155
156cmdreturn_t it_remove_image(arg_t a)
157{
158 if (mode == MODE_IMAGE) {
159 remove_file(fileidx, true);
160 load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
161 return CMD_DIRTY;
162 } else if (tns.sel < tns.cnt) {
163 remove_file(tns.sel, true);
164 tns.dirty = true;
165 if (tns.sel >= tns.cnt)
166 tns.sel = tns.cnt - 1;
167 return CMD_DIRTY;
168 } else {
169 return CMD_OK;
170 }
171}
172
173cmdreturn_t i_navigate(arg_t a)
174{
175 long n = (long) a;
176
177 if (mode == MODE_IMAGE) {
178 if (prefix > 0)
179 n *= prefix;
180 n += fileidx;
181 if (n < 0)
182 n = 0;
183 if (n >= filecnt)
184 n = filecnt - 1;
185
186 if (n != fileidx) {
187 load_image(n);
188 return CMD_DIRTY;
189 }
190 }
191 return CMD_INVALID;
192}
193
194cmdreturn_t i_alternate(arg_t a)
195{
196 if (mode == MODE_IMAGE) {
197 load_image(alternate);
198 return CMD_DIRTY;
199 } else {
200 return CMD_INVALID;
201 }
202}
203
204cmdreturn_t it_first(arg_t a)
205{
206 if (mode == MODE_IMAGE && fileidx != 0) {
207 load_image(0);
208 return CMD_DIRTY;
209 } else if (mode == MODE_THUMB && tns.sel != 0) {
210 tns.sel = 0;
211 tns.dirty = true;
212 return CMD_DIRTY;
213 } else {
214 return CMD_OK;
215 }
216}
217
218cmdreturn_t it_n_or_last(arg_t a)
219{
220 int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
221
222 if (mode == MODE_IMAGE && fileidx != n) {
223 load_image(n);
224 return CMD_DIRTY;
225 } else if (mode == MODE_THUMB && tns.sel != n) {
226 tns.sel = n;
227 tns.dirty = true;
228 return CMD_DIRTY;
229 } else {
230 return CMD_OK;
231 }
232}
233
234cmdreturn_t i_navigate_frame(arg_t a)
235{
236 if (mode != MODE_IMAGE)
237 return CMD_INVALID;
238 else
239 return !img.multi.animate && img_frame_navigate(&img, (long) a);
240}
241
242cmdreturn_t i_toggle_animation(arg_t a)
243{
244 if (mode != MODE_IMAGE)
245 return CMD_INVALID;
246
247 if (img.multi.animate) {
248 reset_timeout(animate);
249 img.multi.animate = false;
250 } else if (img_frame_animate(&img, true)) {
251 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
252 }
253 return CMD_DIRTY;
254}
255
256cmdreturn_t it_toggle_image_mark(arg_t a)
257{
258 int sel = mode == MODE_IMAGE ? fileidx : tns.sel;
259
260 files[sel].marked = !files[sel].marked;
261 if (mode == MODE_THUMB)
262 tns_mark(&tns, sel, files[sel].marked);
263 return CMD_DIRTY;
264}
265
266cmdreturn_t it_reverse_marks(arg_t a)
267{
268 int i, cnt = mode == MODE_IMAGE ? filecnt : tns.cnt;
269
270 for (i = 0; i < cnt; i++)
271 files[i].marked = !files[i].marked;
272 if (mode == MODE_THUMB)
273 tns.dirty = true;
274 return CMD_DIRTY;
275}
276
277cmdreturn_t it_navigate_marked(arg_t a)
278{
279 long n = (long) a;
280 int d, i, cnt, sel, new;
281
282 if (mode == MODE_IMAGE)
283 cnt = filecnt, sel = new = fileidx;
284 else
285 cnt = tns.cnt, sel = new = tns.sel;
286 if (prefix > 0)
287 n *= prefix;
288 d = n > 0 ? 1 : -1;
289 for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
290 if (files[i].marked) {
291 n -= d;
292 new = i;
293 }
294 }
295 if (new != sel) {
296 if (mode == MODE_IMAGE) {
297 load_image(new);
298 } else {
299 tns.sel = new;
300 tns.dirty = true;
301 }
302 return CMD_DIRTY;
303 } else {
304 return CMD_OK;
305 }
306}
307
308cmdreturn_t it_scroll_move(arg_t a)
309{
310 direction_t dir = (direction_t) a;
311
312 if (mode == MODE_IMAGE)
313 return img_pan(&img, dir, prefix);
314 else
315 return tns_move_selection(&tns, dir, prefix);
316}
317
318cmdreturn_t it_scroll_screen(arg_t a)
319{
320 direction_t dir = (direction_t) a;
321
322 if (mode == MODE_IMAGE)
323 return img_pan(&img, dir, -1);
324 else
325 return tns_scroll(&tns, dir, true);
326}
327
328cmdreturn_t i_scroll_to_edge(arg_t a)
329{
330 direction_t dir = (direction_t) a;
331
332 if (mode == MODE_IMAGE)
333 return img_pan_edge(&img, dir);
334 else
335 return CMD_INVALID;
336}
337
338/* Xlib helper function for i_drag() */
339Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
340{
341 return e != NULL && e->type == MotionNotify;
342}
343
344#define WARP(x,y) \
345 XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
346 ox = x, oy = y; \
347 break
348
349cmdreturn_t i_drag(arg_t a)
350{
351 int dx = 0, dy = 0, i, ox, oy, x, y;
352 unsigned int ui;
353 bool dragging = true, next = false;
354 XEvent e;
355 Window w;
356
357 if (mode != MODE_IMAGE)
358 return CMD_INVALID;
359 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
360 return CMD_OK;
361
362 win_set_cursor(&win, CURSOR_HAND);
363
364 while (dragging) {
365 if (!next)
366 XMaskEvent(win.env.dpy,
367 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
368 switch (e.type) {
369 case ButtonPress:
370 case ButtonRelease:
371 dragging = false;
372 break;
373 case MotionNotify:
374 x = e.xmotion.x;
375 y = e.xmotion.y;
376
377 /* wrap the mouse around */
378 if (x <= 0) {
379 WARP(win.w - 2, y);
380 } else if (x >= win.w - 1) {
381 WARP(1, y);
382 } else if (y <= 0) {
383 WARP(x, win.h - 2);
384 } else if (y >= win.h - 1) {
385 WARP(x, 1);
386 }
387 dx += x - ox;
388 dy += y - oy;
389 ox = x;
390 oy = y;
391 break;
392 }
393 if (dragging)
394 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
395 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
396 if (img_move(&img, dx, dy)) {
397 img_render(&img);
398 win_draw(&win);
399 }
400 dx = dy = 0;
401 }
402 }
403
404 win_set_cursor(&win, CURSOR_ARROW);
405 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
406 reset_timeout(redraw);
407
408 return CMD_OK;
409}
410
411cmdreturn_t i_zoom(arg_t a)
412{
413 long scale = (long) a;
414
415 if (mode != MODE_IMAGE)
416 return CMD_INVALID;
417
418 if (scale > 0)
419 return img_zoom_in(&img);
420 else if (scale < 0)
421 return img_zoom_out(&img);
422 else
423 return CMD_OK;
424}
425
426cmdreturn_t i_set_zoom(arg_t a)
427{
428 if (mode == MODE_IMAGE)
429 return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
430 else
431 return CMD_INVALID;
432}
433
434cmdreturn_t i_fit_to_win(arg_t a)
435{
436 scalemode_t sm = (scalemode_t) a;
437
438 if (mode == MODE_IMAGE)
439 return img_fit_win(&img, sm);
440 else
441 return CMD_INVALID;
442}
443
444cmdreturn_t i_rotate(arg_t a)
445{
446 degree_t degree = (degree_t) a;
447
448 if (mode == MODE_IMAGE) {
449 img_rotate(&img, degree);
450 return CMD_DIRTY;
451 } else {
452 return CMD_INVALID;
453 }
454}
455
456cmdreturn_t i_flip(arg_t a)
457{
458 flipdir_t dir = (flipdir_t) a;
459
460 if (mode == MODE_IMAGE) {
461 img_flip(&img, dir);
462 return CMD_DIRTY;
463 } else {
464 return CMD_INVALID;
465 }
466}
467
468cmdreturn_t i_slideshow(arg_t a)
469{
470 if (mode == MODE_IMAGE) {
471 if (prefix > 0) {
472 img.ss.on = true;
473 img.ss.delay = prefix;
474 set_timeout(slideshow, img.ss.delay * 1000, true);
475 } else if (img.ss.on) {
476 img.ss.on = false;
477 reset_timeout(slideshow);
478 } else {
479 img.ss.on = true;
480 }
481 return CMD_DIRTY;
482 } else {
483 return CMD_INVALID;
484 }
485}
486
487cmdreturn_t i_toggle_antialias(arg_t a)
488{
489 if (mode == MODE_IMAGE) {
490 img_toggle_antialias(&img);
491 return CMD_DIRTY;
492 } else {
493 return CMD_INVALID;
494 }
495}
496
497cmdreturn_t 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 CMD_DIRTY;
505}
506
507cmdreturn_t i_change_gamma(arg_t a)
508{
509 if (mode == MODE_IMAGE) {
510 return img_change_gamma(&img, (long) a);
511 } else {
512 return CMD_INVALID;
513 }
514}
515