Krolyxon
Commit 5b01c15

Branches

Refs →

nsxivcommands.c

470 lines · 8.5 KB · C

1/* Copyright 2011, 2012, 2014 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
61bool cg_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
75bool cg_switch_mode(arg_t a)
76{
77 if (mode == MODE_IMAGE) {
78 if (tns.thumbs == NULL)
79 tns_init(&tns, filecnt, &win, &fileidx);
80 img_close(&img, false);
81 reset_timeout(reset_cursor);
82 if (img.ss.on) {
83 img.ss.on = false;
84 reset_timeout(slideshow);
85 }
86 tns.dirty = true;
87 mode = MODE_THUMB;
88 } else {
89 load_image(fileidx);
90 mode = MODE_IMAGE;
91 }
92 return true;
93}
94
95bool cg_toggle_fullscreen(arg_t a)
96{
97 win_toggle_fullscreen(&win);
98 /* redraw after next ConfigureNotify event */
99 set_timeout(redraw, TO_REDRAW_RESIZE, false);
100 if (mode == MODE_IMAGE)
101 img.checkpan = img.dirty = true;
102 else
103 tns.dirty = true;
104 return false;
105}
106
107bool cg_toggle_bar(arg_t a)
108{
109 win_toggle_bar(&win);
110 if (mode == MODE_IMAGE) {
111 img.checkpan = img.dirty = true;
112 if (win.bar.h > 0)
113 open_info();
114 } else {
115 tns.dirty = true;
116 }
117 return true;
118}
119
120bool cg_prefix_external(arg_t a)
121{
122 extprefix = true;
123 return false;
124}
125
126bool cg_reload_image(arg_t a)
127{
128 if (mode == MODE_IMAGE) {
129 load_image(fileidx);
130 } else {
131 win_set_cursor(&win, CURSOR_WATCH);
132 if (!tns_load(&tns, fileidx, &files[fileidx], true, false)) {
133 remove_file(fileidx, false);
134 tns.dirty = true;
135 }
136 }
137 return true;
138}
139
140bool cg_remove_image(arg_t a)
141{
142 if (mode == MODE_IMAGE) {
143 remove_file(fileidx, true);
144 load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
145 return true;
146 } else if (fileidx < tns.cnt) {
147 remove_file(fileidx, true);
148 tns.dirty = true;
149 return true;
150 } else {
151 return false;
152 }
153}
154
155bool cg_first(arg_t a)
156{
157 if (mode == MODE_IMAGE && fileidx != 0) {
158 load_image(0);
159 return true;
160 } else if (mode == MODE_THUMB && fileidx != 0) {
161 fileidx = 0;
162 tns.dirty = true;
163 return true;
164 } else {
165 return false;
166 }
167}
168
169bool cg_n_or_last(arg_t a)
170{
171 int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
172
173 if (mode == MODE_IMAGE && fileidx != n) {
174 load_image(n);
175 return true;
176 } else if (mode == MODE_THUMB && fileidx != n) {
177 fileidx = n;
178 tns.dirty = true;
179 return true;
180 } else {
181 return false;
182 }
183}
184
185bool cg_scroll_screen(arg_t a)
186{
187 direction_t dir = (direction_t) a;
188
189 if (mode == MODE_IMAGE)
190 return img_pan(&img, dir, -1);
191 else
192 return tns_scroll(&tns, dir, true);
193}
194
195bool cg_toggle_image_mark(arg_t a)
196{
197 files[fileidx].marked = !files[fileidx].marked;
198 if (mode == MODE_THUMB)
199 tns_mark(&tns, fileidx, files[fileidx].marked);
200 return true;
201}
202
203bool cg_reverse_marks(arg_t a)
204{
205 int i, cnt = mode == MODE_IMAGE ? filecnt : tns.cnt;
206
207 for (i = 0; i < cnt; i++)
208 files[i].marked = !files[i].marked;
209 if (mode == MODE_THUMB)
210 tns.dirty = true;
211 return true;
212}
213
214bool cg_navigate_marked(arg_t a)
215{
216 long n = (long) a;
217 int d, i;
218 int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx;
219
220 if (prefix > 0)
221 n *= prefix;
222 d = n > 0 ? 1 : -1;
223 for (i = fileidx + d; n != 0 && i >= 0 && i < cnt; i += d) {
224 if (files[i].marked) {
225 n -= d;
226 new = i;
227 }
228 }
229 if (new != fileidx) {
230 if (mode == MODE_IMAGE) {
231 load_image(new);
232 } else {
233 fileidx = new;
234 tns.dirty = true;
235 }
236 return true;
237 } else {
238 return false;
239 }
240}
241
242bool ci_navigate(arg_t a)
243{
244 long n = (long) a;
245
246 if (prefix > 0)
247 n *= prefix;
248 n += fileidx;
249 if (n < 0)
250 n = 0;
251 if (n >= filecnt)
252 n = filecnt - 1;
253
254 if (n != fileidx) {
255 load_image(n);
256 return true;
257 } else {
258 return false;
259 }
260}
261
262bool ci_alternate(arg_t a)
263{
264 load_image(alternate);
265 return true;
266}
267
268bool ci_navigate_frame(arg_t a)
269{
270 return !img.multi.animate && img_frame_navigate(&img, (long) a);
271}
272
273bool ci_toggle_animation(arg_t a)
274{
275 bool dirty = false;
276
277 img.multi.animate = !img.multi.animate;
278
279 if (img.multi.animate) {
280 dirty = img_frame_animate(&img, true);
281 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
282 } else {
283 reset_timeout(animate);
284 }
285 return dirty;
286}
287
288bool ci_scroll(arg_t a)
289{
290 direction_t dir = (direction_t) a;
291
292 return img_pan(&img, dir, prefix);
293}
294
295bool ci_scroll_to_edge(arg_t a)
296{
297 direction_t dir = (direction_t) a;
298
299 return img_pan_edge(&img, dir);
300}
301
302/* Xlib helper function for i_drag() */
303Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
304{
305 return e != NULL && e->type == MotionNotify;
306}
307
308#define WARP(x,y) \
309 XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
310 ox = x, oy = y; \
311 break
312
313bool ci_drag(arg_t a)
314{
315 int dx = 0, dy = 0, i, ox, oy, x, y;
316 unsigned int ui;
317 bool dragging = true, next = false;
318 XEvent e;
319 Window w;
320
321 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
322 return false;
323
324 win_set_cursor(&win, CURSOR_HAND);
325
326 while (dragging) {
327 if (!next)
328 XMaskEvent(win.env.dpy,
329 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
330 switch (e.type) {
331 case ButtonPress:
332 case ButtonRelease:
333 dragging = false;
334 break;
335 case MotionNotify:
336 x = e.xmotion.x;
337 y = e.xmotion.y;
338
339 /* wrap the mouse around */
340 if (x <= 0) {
341 WARP(win.w - 2, y);
342 } else if (x >= win.w - 1) {
343 WARP(1, y);
344 } else if (y <= 0) {
345 WARP(x, win.h - 2);
346 } else if (y >= win.h - 1) {
347 WARP(x, 1);
348 }
349 dx += x - ox;
350 dy += y - oy;
351 ox = x;
352 oy = y;
353 break;
354 }
355 if (dragging)
356 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
357 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
358 if (img_move(&img, dx, dy)) {
359 img_render(&img);
360 win_draw(&win);
361 }
362 dx = dy = 0;
363 }
364 }
365 win_set_cursor(&win, CURSOR_ARROW);
366 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
367 reset_timeout(redraw);
368
369 return true;
370}
371
372bool ci_zoom(arg_t a)
373{
374 long scale = (long) a;
375
376 if (scale > 0)
377 return img_zoom_in(&img);
378 else if (scale < 0)
379 return img_zoom_out(&img);
380 else
381 return false;
382}
383
384bool ci_set_zoom(arg_t a)
385{
386 return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
387}
388
389bool ci_fit_to_win(arg_t a)
390{
391 scalemode_t sm = (scalemode_t) a;
392
393 return img_fit_win(&img, sm);
394}
395
396bool ci_rotate(arg_t a)
397{
398 degree_t degree = (degree_t) a;
399
400 img_rotate(&img, degree);
401 return true;
402}
403
404bool ci_flip(arg_t a)
405{
406 flipdir_t dir = (flipdir_t) a;
407
408 img_flip(&img, dir);
409 return true;
410}
411
412bool ci_change_gamma(arg_t a)
413{
414 return img_change_gamma(&img, (long) a);
415}
416
417bool ci_toggle_antialias(arg_t a)
418{
419 img_toggle_antialias(&img);
420 return true;
421}
422
423bool ci_toggle_alpha(arg_t a)
424{
425 img.alpha = !img.alpha;
426 img.dirty = true;
427 return true;
428}
429
430bool ci_slideshow(arg_t a)
431{
432 if (prefix > 0) {
433 img.ss.on = true;
434 img.ss.delay = prefix;
435 set_timeout(slideshow, img.ss.delay * 1000, true);
436 } else if (img.ss.on) {
437 img.ss.on = false;
438 reset_timeout(slideshow);
439 } else {
440 img.ss.on = true;
441 }
442 return true;
443}
444
445bool ct_move_sel(arg_t a)
446{
447 direction_t dir = (direction_t) a;
448
449 return tns_move_selection(&tns, dir, prefix);
450}
451
452bool ct_reload_all(arg_t a)
453{
454 tns_free(&tns);
455 tns_init(&tns, filecnt, &win, &fileidx);
456 return false;
457}
458
459
460#undef G_CMD
461#define G_CMD(c) { -1, cg_##c },
462#undef I_CMD
463#define I_CMD(c) { MODE_IMAGE, ci_##c },
464#undef T_CMD
465#define T_CMD(c) { MODE_THUMB, ct_##c },
466
467const cmd_t cmds[CMD_COUNT] = {
468#include "commands.lst"
469};
470