Krolyxon
Commit e310136

Branches

Refs →

nsxivcommands.c

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