Krolyxon
Commit 06164c2

Branches

Refs →

nsxivcommands.c

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