Krolyxon
Commit 808a10e

Branches

Refs →

nsxivcommands.c

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