Krolyxon
Commit 0cb1d11

Branches

Refs →

nsxivcommands.c

481 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;
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].marked)
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)) {
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].marked = !files[fileidx].marked;
204 markcnt += files[fileidx].marked ? 1 : -1;
205 if (mode == MODE_THUMB)
206 tns_mark(&tns, fileidx, files[fileidx].marked);
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].marked = !files[i].marked;
216 markcnt += files[i].marked ? 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].marked = false;
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].marked) {
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 ci_navigate(arg_t a)
264{
265 long n = (long) a;
266
267 if (prefix > 0)
268 n *= prefix;
269 n += fileidx;
270 if (n < 0)
271 n = 0;
272 if (n >= filecnt)
273 n = filecnt - 1;
274
275 if (n != fileidx) {
276 load_image(n);
277 return true;
278 } else {
279 return false;
280 }
281}
282
283bool ci_alternate(arg_t a)
284{
285 load_image(alternate);
286 return true;
287}
288
289bool ci_navigate_frame(arg_t a)
290{
291 return !img.multi.animate && img_frame_navigate(&img, (long) a);
292}
293
294bool ci_toggle_animation(arg_t a)
295{
296 bool dirty = false;
297
298 if (img.multi.cnt > 0) {
299 img.multi.animate = !img.multi.animate;
300 if (img.multi.animate) {
301 dirty = img_frame_animate(&img);
302 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
303 } else {
304 reset_timeout(animate);
305 }
306 }
307 return dirty;
308}
309
310bool ci_scroll(arg_t a)
311{
312 direction_t dir = (direction_t) a;
313
314 return img_pan(&img, dir, prefix);
315}
316
317bool ci_scroll_to_edge(arg_t a)
318{
319 direction_t dir = (direction_t) a;
320
321 return img_pan_edge(&img, dir);
322}
323
324/* Xlib helper function for i_drag() */
325Bool is_motionnotify(Display *d, XEvent *e, XPointer a)
326{
327 return e != NULL && e->type == MotionNotify;
328}
329
330#define WARP(x,y) \
331 XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \
332 ox = x, oy = y; \
333 break
334
335bool ci_drag(arg_t a)
336{
337 int dx = 0, dy = 0, i, ox, oy, x, y;
338 unsigned int ui;
339 bool dragging = true, next = false;
340 XEvent e;
341 Window w;
342
343 if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
344 return false;
345
346 win_set_cursor(&win, CURSOR_HAND);
347
348 while (dragging) {
349 if (!next)
350 XMaskEvent(win.env.dpy,
351 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
352 switch (e.type) {
353 case ButtonPress:
354 case ButtonRelease:
355 dragging = false;
356 break;
357 case MotionNotify:
358 x = e.xmotion.x;
359 y = e.xmotion.y;
360
361 /* wrap the mouse around */
362 if (x <= 0) {
363 WARP(win.w - 2, y);
364 } else if (x >= win.w - 1) {
365 WARP(1, y);
366 } else if (y <= 0) {
367 WARP(x, win.h - 2);
368 } else if (y >= win.h - 1) {
369 WARP(x, 1);
370 }
371 dx += x - ox;
372 dy += y - oy;
373 ox = x;
374 oy = y;
375 break;
376 }
377 if (dragging)
378 next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
379 if ((!dragging || !next) && (dx != 0 || dy != 0)) {
380 if (img_move(&img, dx, dy)) {
381 img_render(&img);
382 win_draw(&win);
383 }
384 dx = dy = 0;
385 }
386 }
387 win_set_cursor(&win, CURSOR_ARROW);
388 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
389 reset_timeout(redraw);
390
391 return true;
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, files, &filecnt, &fileidx, &win);
466 tns.dirty = true;
467 return true;
468}
469
470
471#undef G_CMD
472#define G_CMD(c) { -1, cg_##c },
473#undef I_CMD
474#define I_CMD(c) { MODE_IMAGE, ci_##c },
475#undef T_CMD
476#define T_CMD(c) { MODE_THUMB, ct_##c },
477
478const cmd_t cmds[CMD_COUNT] = {
479#include "commands.lst"
480};
481