Krolyxon
Commit 9a7e97c

Branches

Refs →

nsxivcommands.c

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