Krolyxon
Commit a3838c1

Branches

Refs →

nsxivcommands.c

468 lines · 8.4 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 _)
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 _)
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 _)
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 _)
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 _)
118{
119 extprefix = true;
120 return false;
121}
122
123bool cg_reload_image(arg_t _)
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 _)
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 _)
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 _)
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 dir)
178{
179 if (mode == MODE_IMAGE)
180 return img_pan(&img, dir, -1);
181 else
182 return tns_scroll(&tns, dir, true);
183}
184
185bool cg_zoom(arg_t d)
186{
187 if (mode == MODE_THUMB)
188 return tns_zoom(&tns, d);
189 else if (d > 0)
190 return img_zoom_in(&img);
191 else if (d < 0)
192 return img_zoom_out(&img);
193 else
194 return false;
195}
196
197bool cg_toggle_image_mark(arg_t _)
198{
199 files[fileidx].flags ^= FF_MARK;
200 markcnt += files[fileidx].flags & FF_MARK ? 1 : -1;
201 if (mode == MODE_THUMB)
202 tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK));
203 return true;
204}
205
206bool cg_reverse_marks(arg_t _)
207{
208 int i;
209
210 for (i = 0; i < filecnt; i++) {
211 files[i].flags ^= FF_MARK;
212 markcnt += files[i].flags & FF_MARK ? 1 : -1;
213 }
214 if (mode == MODE_THUMB)
215 tns.dirty = true;
216 return true;
217}
218
219bool cg_unmark_all(arg_t _)
220{
221 int i;
222
223 for (i = 0; i < filecnt; i++)
224 files[i].flags &= ~FF_MARK;
225 markcnt = 0;
226 if (mode == MODE_THUMB)
227 tns.dirty = true;
228 return true;
229}
230
231bool cg_navigate_marked(arg_t n)
232{
233 int d, i;
234 int new = fileidx;
235
236 if (prefix > 0)
237 n *= prefix;
238 d = n > 0 ? 1 : -1;
239 for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
240 if (files[i].flags & FF_MARK) {
241 n -= d;
242 new = i;
243 }
244 }
245 if (new != fileidx) {
246 if (mode == MODE_IMAGE) {
247 load_image(new);
248 } else {
249 fileidx = new;
250 tns.dirty = true;
251 }
252 return true;
253 } else {
254 return false;
255 }
256}
257
258bool cg_change_gamma(arg_t d)
259{
260 if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) {
261 if (mode == MODE_THUMB)
262 tns.dirty = true;
263 return true;
264 } else {
265 return false;
266 }
267}
268
269bool ci_navigate(arg_t n)
270{
271 if (prefix > 0)
272 n *= prefix;
273 n += fileidx;
274 if (n < 0)
275 n = 0;
276 if (n >= filecnt)
277 n = filecnt - 1;
278
279 if (n != fileidx) {
280 load_image(n);
281 return true;
282 } else {
283 return false;
284 }
285}
286
287bool ci_alternate(arg_t _)
288{
289 load_image(alternate);
290 return true;
291}
292
293bool ci_navigate_frame(arg_t d)
294{
295 return !img.multi.animate && img_frame_navigate(&img, d);
296}
297
298bool ci_toggle_animation(arg_t _)
299{
300 bool dirty = false;
301
302 if (img.multi.cnt > 0) {
303 img.multi.animate = !img.multi.animate;
304 if (img.multi.animate) {
305 dirty = img_frame_animate(&img);
306 set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
307 } else {
308 reset_timeout(animate);
309 }
310 }
311 return dirty;
312}
313
314bool ci_scroll(arg_t dir)
315{
316 return img_pan(&img, dir, prefix);
317}
318
319bool ci_scroll_to_edge(arg_t dir)
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 _)
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 zl)
395{
396 return img_zoom(&img, (prefix ? prefix : zl) / 100.0);
397}
398
399bool ci_fit_to_win(arg_t sm)
400{
401 return img_fit_win(&img, sm);
402}
403
404bool ci_rotate(arg_t degree)
405{
406 img_rotate(&img, degree);
407 return true;
408}
409
410bool ci_flip(arg_t dir)
411{
412 img_flip(&img, dir);
413 return true;
414}
415
416bool ci_toggle_antialias(arg_t _)
417{
418 img_toggle_antialias(&img);
419 return true;
420}
421
422bool ci_toggle_alpha(arg_t _)
423{
424 img.alpha = !img.alpha;
425 img.dirty = true;
426 return true;
427}
428
429bool ci_slideshow(arg_t _)
430{
431 if (prefix > 0) {
432 img.ss.on = true;
433 img.ss.delay = prefix;
434 set_timeout(slideshow, img.ss.delay * 1000, true);
435 } else if (img.ss.on) {
436 img.ss.on = false;
437 reset_timeout(slideshow);
438 } else {
439 img.ss.on = true;
440 }
441 return true;
442}
443
444bool ct_move_sel(arg_t dir)
445{
446 return tns_move_selection(&tns, dir, prefix);
447}
448
449bool ct_reload_all(arg_t _)
450{
451 tns_free(&tns);
452 tns_init(&tns, files, &filecnt, &fileidx, &win);
453 tns.dirty = true;
454 return true;
455}
456
457
458#undef G_CMD
459#define G_CMD(c) { -1, cg_##c },
460#undef I_CMD
461#define I_CMD(c) { MODE_IMAGE, ci_##c },
462#undef T_CMD
463#define T_CMD(c) { MODE_THUMB, ct_##c },
464
465const cmd_t cmds[CMD_COUNT] = {
466#include "commands.lst"
467};
468