Krolyxon
Commit bb67215

Branches

Refs →

nsxivcommands.c

480 lines · 8.7 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 bool dirty = false;
286
287 img.multi.animate = !img.multi.animate;
288
289 if (img.multi.animate) {
290 dirty = img_frame_animate(&img, true);
291 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
292 } else {
293 reset_timeout(animate);
294 }
295 return dirty;
296}
297
298bool ci_scroll(arg_t a)
299{
300 direction_t dir = (direction_t) a;
301
302 return img_pan(&img, dir, prefix);
303}
304
305bool ci_scroll_to_edge(arg_t a)
306{
307 direction_t dir = (direction_t) a;
308
309 return img_pan_edge(&img, dir);
310}
311
312/* Xlib helper function for i_drag() */
313Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
314{
315 return e != NULL && e->type == MotionNotify;
316}
317
318#define WARP(x,y) \
319 XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
320 ox = x, oy = y; \
321 break
322
323bool ci_drag(arg_t a)
324{
325 int dx = 0, dy = 0, i, ox, oy, x, y;
326 unsigned int ui;
327 bool dragging = true, next = false;
328 XEvent e;
329 Window w;
330
331 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
332 return false;
333
334 win_set_cursor(&win, CURSOR_HAND);
335
336 while (dragging) {
337 if (!next)
338 XMaskEvent(win.env.dpy,
339 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
340 switch (e.type) {
341 case ButtonPress:
342 case ButtonRelease:
343 dragging = false;
344 break;
345 case MotionNotify:
346 x = e.xmotion.x;
347 y = e.xmotion.y;
348
349 /* wrap the mouse around */
350 if (x <= 0) {
351 WARP(win.w - 2, y);
352 } else if (x >= win.w - 1) {
353 WARP(1, y);
354 } else if (y <= 0) {
355 WARP(x, win.h - 2);
356 } else if (y >= win.h - 1) {
357 WARP(x, 1);
358 }
359 dx += x - ox;
360 dy += y - oy;
361 ox = x;
362 oy = y;
363 break;
364 }
365 if (dragging)
366 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
367 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
368 if (img_move(&img, dx, dy)) {
369 img_render(&img);
370 win_draw(&win);
371 }
372 dx = dy = 0;
373 }
374 }
375 win_set_cursor(&win, CURSOR_ARROW);
376 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
377 reset_timeout(redraw);
378
379 return true;
380}
381
382bool ci_zoom(arg_t a)
383{
384 long scale = (long) a;
385
386 if (scale > 0)
387 return img_zoom_in(&img);
388 else if (scale < 0)
389 return img_zoom_out(&img);
390 else
391 return false;
392}
393
394bool ci_set_zoom(arg_t a)
395{
396 return img_zoom(&img, (prefix ? prefix : (long) a) / 100.0);
397}
398
399bool ci_fit_to_win(arg_t a)
400{
401 scalemode_t sm = (scalemode_t) a;
402
403 return img_fit_win(&img, sm);
404}
405
406bool ci_rotate(arg_t a)
407{
408 degree_t degree = (degree_t) a;
409
410 img_rotate(&img, degree);
411 return true;
412}
413
414bool ci_flip(arg_t a)
415{
416 flipdir_t dir = (flipdir_t) a;
417
418 img_flip(&img, dir);
419 return true;
420}
421
422bool ci_change_gamma(arg_t a)
423{
424 return img_change_gamma(&img, (long) a);
425}
426
427bool ci_toggle_antialias(arg_t a)
428{
429 img_toggle_antialias(&img);
430 return true;
431}
432
433bool ci_toggle_alpha(arg_t a)
434{
435 img.alpha = !img.alpha;
436 img.dirty = true;
437 return true;
438}
439
440bool ci_slideshow(arg_t a)
441{
442 if (prefix > 0) {
443 img.ss.on = true;
444 img.ss.delay = prefix;
445 set_timeout(slideshow, img.ss.delay * 1000, true);
446 } else if (img.ss.on) {
447 img.ss.on = false;
448 reset_timeout(slideshow);
449 } else {
450 img.ss.on = true;
451 }
452 return true;
453}
454
455bool ct_move_sel(arg_t a)
456{
457 direction_t dir = (direction_t) a;
458
459 return tns_move_selection(&tns, dir, prefix);
460}
461
462bool ct_reload_all(arg_t a)
463{
464 tns_free(&tns);
465 tns_init(&tns, filecnt, &win);
466 return false;
467}
468
469
470#undef G_CMD
471#define G_CMD(c) { -1, cg_##c },
472#undef I_CMD
473#define I_CMD(c) { MODE_IMAGE, ci_##c },
474#undef T_CMD
475#define T_CMD(c) { MODE_THUMB, ct_##c },
476
477const cmd_t cmds[CMD_COUNT] = {
478#include "commands.lst"
479};
480