-
Notifications
You must be signed in to change notification settings - Fork 2
/
commands.c
606 lines (548 loc) · 11.3 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* Copyright 2011, 2012, 2014 Bert Muennich
*
* This file is part of sxiv.
*
* sxiv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* sxiv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sxiv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "sxiv.h"
#include "config.h"
void remove_file(int, bool);
void load_image(int);
bool mark_image(int, bool);
void close_info(void);
void open_info(void);
int ptr_third_x(void);
void redraw(void);
void reset_cursor(void);
void animate(void);
void slideshow(void);
void set_timeout(timeout_f, int, bool);
void reset_timeout(timeout_f);
extern appmode_t mode;
extern img_t img;
extern tns_t tns;
extern win_t win;
extern fileinfo_t *files;
extern int filecnt, fileidx;
extern int alternate;
extern int markcnt;
extern int markidx;
#ifdef ENABLE_COUNT
extern int prefix;
#endif /* ENABLE_COUNT */
//extern bool extprefix; /* Not needed! */
bool
img_frame_goto(img_t *, int);
bool
cg_switch_mode(arg_t _)
{
if (mode == MODE_IMAGE) {
if (tns.thumbs == NULL)
tns_init(&tns, files, &filecnt, &fileidx, &win);
img_close(&img, false);
reset_timeout(reset_cursor);
if (img.ss.on) {
img.ss.on = false;
reset_timeout(slideshow);
}
tns.dirty = true;
mode = MODE_THUMB;
} else {
load_image(fileidx);
mode = MODE_IMAGE;
}
return true;
}
bool
cg_toggle_fullscreen(arg_t _)
{
win_toggle_fullscreen(&win);
set_timeout(redraw, TO_REDRAW_RESIZE, false); /* redraw after next ConfigureNotify event */
if (mode == MODE_IMAGE)
img.checkpan = img.dirty = true;
else
tns.dirty = true;
return false;
}
bool
cg_toggle_bar(arg_t _)
{
win_toggle_bar(&win);
if (mode == MODE_IMAGE) {
if (win.bar.h > 0)
open_info();
else
close_info();
img.checkpan = img.dirty = true;
} else
tns.dirty = true;
return true;
}
/* Not needed
bool cg_prefix_external(arg_t _)
{
extprefix = true;
return false;
}
*/
bool
cg_reload_image(arg_t _)
{
if (mode == MODE_IMAGE)
load_image(fileidx);
else {
win_set_cursor(&win, CURSOR_WATCH);
if (!tns_load(&tns, fileidx, true, false)) {
remove_file(fileidx, false);
tns.dirty = true;
}
}
return true;
}
bool
cg_remove_image(arg_t _)
{
remove_file(fileidx, true);
if (mode == MODE_IMAGE)
load_image(fileidx);
else
tns.dirty = true;
return true;
}
bool
cg_first(arg_t _)
{
if (mode == MODE_IMAGE && fileidx != 0) {
load_image(0);
return true;
} else if (mode == MODE_THUMB && fileidx != 0) {
fileidx = 0;
tns.dirty = true;
return true;
} else
return false;
}
bool
cg_n_or_last(arg_t _)
{
#ifdef ENABLE_COUNT
int n = prefix != 0 && prefix - 1 < filecnt ? prefix - 1 : filecnt - 1;
if (mode == MODE_IMAGE && fileidx != n) {
load_image(n);
return true;
} else if (mode == MODE_THUMB && fileidx != n) {
fileidx = n;
#else
if (mode == MODE_IMAGE) {
load_image(filecnt - 1);
return true;
} else if (mode == MODE_THUMB && fileidx != -1) {
fileidx = filecnt - 1;
#endif /* ENABLE_COUNT */
tns.dirty = true;
return true;
} else
return false;
}
bool
cg_scroll_screen(arg_t dir)
{
if (mode == MODE_IMAGE)
#ifdef ENABLE_COUNT
return img_pan(&img, dir, -1);
#else
return img_pan(&img, dir);
#endif /* ENABLE_COUNT */
else
return tns_scroll(&tns, dir, true);
}
bool
cg_zoom(arg_t d)
{
if (mode == MODE_THUMB)
return tns_zoom(&tns, d);
else if (d > 0)
return img_zoom_in(&img);
else if (d < 0)
return img_zoom_out(&img);
else
return false;
}
bool
cg_toggle_image_mark(arg_t _)
{
return mark_image(fileidx, !(files[fileidx].flags & FF_MARK));
}
bool
cg_reverse_marks(arg_t _)
{
int i;
for (i = 0; i < filecnt; i++) {
files[i].flags ^= FF_MARK;
markcnt += files[i].flags & FF_MARK ? 1 : -1;
}
if (mode == MODE_THUMB)
tns.dirty = true;
return true;
}
bool
cg_mark_range(arg_t _)
{
int d = markidx < fileidx ? 1 : -1, end, i;
bool dirty = false, on = !!(files[markidx].flags & FF_MARK);
for (i = markidx + d, end = fileidx + d; i != end; i += d)
dirty |= mark_image(i, on);
return dirty;
}
bool
cg_unmark_all(arg_t _)
{
int i;
for (i = 0; i < filecnt; i++)
files[i].flags &= ~FF_MARK;
markcnt = 0;
if (mode == MODE_THUMB)
tns.dirty = true;
return true;
}
bool
cg_navigate_marked(arg_t n)
{
int d, i;
int new = fileidx;
#ifdef ENABLE_COUNT
if (prefix > 0)
n *= prefix;
#endif /* ENABLE_COUNT */
d = n > 0 ? 1 : -1;
if (markcnt == 0) /* Preventing crash if no image is mark */
return false;
/* Loop through all marked images */
//for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) { /*This doen't work?*/
for (i = fileidx + d; n != 0; i += d) {
if (i < 0)
i += filecnt;
else if (i >= filecnt)
i -= filecnt;
if (files[i].flags & FF_MARK) {
n -= d;
new = i;
}
}
if (new != fileidx) {
if (mode == MODE_IMAGE)
load_image(new);
else {
fileidx = new;
tns.dirty = true;
}
return true;
} else
return false;
}
bool
cg_change_gamma(arg_t d)
{
#ifdef ENABLE_COUNT
if (img_change_gamma(&img, d * (prefix > 0 ? prefix : 1))) {
#else
if (img_change_gamma(&img, d)) {
#endif /* ENABLE_COUNT */
if (mode == MODE_THUMB)
tns.dirty = true;
return true;
} else
return false;
}
bool
ci_navigate(arg_t n)
{
#ifdef ENABLE_COUNT
if (prefix > 0)
n *= prefix;
#endif /* ENABLE_COUNT */
n += fileidx;
if (n < 0)
n = filecnt - 1;
if (n >= filecnt)
n = 0;
if (n != fileidx) {
load_image(n);
return true;
} else
return false;
}
bool
ci_cursor_navigate(arg_t _)
{
return ci_navigate(ptr_third_x() - 1);
}
bool
ci_alternate(arg_t _)
{
load_image(alternate);
return true;
}
bool
ci_navigate_frame(arg_t d)//How to toggle animation if navigate frame is activated?
{
int frame;
#ifdef ENABLE_COUNT
if (prefix > 0)
d *= prefix;
#endif /* ENABLE_COUNT */
// return !img.multi.animate && img_frame_navigate(&img, d);
if (img.multi.cnt > 0) {
// img.multi.animate = !img.multi.animate;
// if (img.multi.animate) {
// set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
// } else
// reset_timeout(animate);
frame = (img.multi.sel + d) % img.multi.cnt;
while (frame < 0)
frame += img.multi.cnt;
/* This one doen't care if it's playing the animaton */
return img_frame_goto(&img,frame);
/* This one cares if it's playing, so will only navigate if animation is paused */
//return !img.multi.animate && img_frame_goto(&img, frame);
} else
return img_frame_navigate(&img, d);
//return !img.multi.animate && img_frame_navigate(&img, d);
}
bool
ci_toggle_animation(arg_t _)
{
bool dirty = false;
if (img.multi.cnt > 0) {
img.multi.animate = !img.multi.animate;
if (img.multi.animate) {
dirty = img_frame_animate(&img);
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
} else
reset_timeout(animate);
}
return dirty;
}
bool
ci_scroll(arg_t dir)
{
#ifdef ENABLE_COUNT
return img_pan(&img, dir, prefix);
#else
return img_pan(&img, dir);
#endif /* ENABLE_COUNT */
}
bool
ci_scroll_to_edge(arg_t dir)
//If zoomed, then navigate else scroll to edge
/* If not zoomed, zoom one float on the array of zoom on config.h */
{
// return img_pan_edge(&img, dir);
//if (img_zoom_diff(&img, NULL) >= 0) {
//return img_zoom(&img, 110.0 / 100.0) && img_pan_edge(&img, dir);
//return img_zoom_in(&img);
//return img_fit_win(&img, SCALE_WIDTH) && img_pan_edge(&img, dir);
//if (img_fit_win(&img, SCALE_WIDTH))
//return img_pan_edge(&img, dir);
//else return img_zoom(&img, 110.0 / 100.0) && img_pan_edge(&img, dir);
// arg_t n;
// switch (dir) {
// case DIR_UP: n = -1; break;
// case DIR_DOWN: n = 1; break;
// default: n = 0; break;
// }
// return ci_navigate(n);
// } else {
//return ci_scroll(dir);
return img_pan_edge(&img, dir);
//}
}
bool
ci_scroll_or_navigate(arg_t dir)
{
if (img_zoom_diff(&img, NULL) >= 0) {
arg_t n;
switch (dir) {
case DIR_UP: n = -1; break;
case DIR_DOWN: n = 1; break;
default: n = 0; break;
}
return ci_navigate(n);
} else
return ci_scroll(dir);
}
bool
ci_drag(arg_t mode)
{
int x, y, ox, oy;
float px, py;
XEvent e;
if ((int)(img.w * img.zoom) <= win.w && (int)(img.h * img.zoom) <= win.h)
return false;
win_set_cursor(&win, CURSOR_DRAG);
win_cursor_pos(&win, &x, &y);
ox = x;
oy = y;
for (;;) {
if (mode == DRAG_ABSOLUTE) {
px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8)
* (win.w - img.w * img.zoom);
py = MIN(MAX(0.0, y - win.h*0.1), win.h*0.8) / (win.h*0.8)
* (win.h - img.h * img.zoom);
} else {
px = img.x + x - ox;
py = img.y + y - oy;
}
if (img_pos(&img, px, py)) {
img_render(&img);
win_draw(&win);
}
XMaskEvent(win.env.dpy,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
if (e.type == ButtonPress || e.type == ButtonRelease)
break;
while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e));
ox = x;
oy = y;
x = e.xmotion.x;
y = e.xmotion.y;
}
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
reset_cursor();
return true;
}
bool
ci_set_zoom(arg_t zl)
{
#ifdef ENABLE_COUNT
return img_zoom(&img, (prefix ? prefix : zl) / 100.0);
#else
return img_zoom(&img, zl / 100.0);
#endif /* ENABLE_COUNT */
}
bool
ci_fit_to_win(arg_t sm)
{
return img_fit_win(&img, sm);
}
bool
ci_rotate(arg_t degree)
{
img_rotate(&img, degree);
return true;
}
bool
ci_flip(arg_t dir)
{
img_flip(&img, dir);
return true;
}
bool
ci_toggle_antialias(arg_t _)
{
img_toggle_antialias(&img);
return true;
}
bool
ci_toggle_alpha(arg_t _)
{
img.alpha = !img.alpha;
img.dirty = true;
return true;
}
bool
ci_slideshow(arg_t _)
{
#ifdef ENABLE_COUNT
if (prefix > 0) {
img.ss.on = true;
img.ss.delay = prefix * 10;
set_timeout(slideshow, img.ss.delay * 100, true);
} else
#endif /* ENABLE_COUNT */
if (img.ss.on) {
img.ss.on = false;
reset_timeout(slideshow);
} else
img.ss.on = true;
return true;
}
bool
ct_move_sel(arg_t dir)
{
#ifdef ENABLE_COUNT
return tns_move_selection(&tns, dir, prefix);
#else
return tns_move_selection(&tns, dir, -1); //How to disable 'prefix' function on this command?
#endif /* ENABLE_COUNT */
}
bool
ct_reload_all(arg_t _)
{
tns_free(&tns);
tns_init(&tns, files, &filecnt, &fileidx, &win);
tns.dirty = true;
return true;
}
/* Customs */
bool
cg_quit(arg_t _)
{
unsigned int i;
if (options->to_stdout && markcnt > 0) {
for (i = 0; i < filecnt; i++) {
if (files[i].flags & FF_MARK)
printf("%s\n", files[i].name);
}
}
exit(EXIT_SUCCESS);
}
bool
ci_random_navigate(arg_t _)
{
int n = rand() % filecnt;
if (n != fileidx) {
load_image(n);
return true;
} else
return false;
}
bool
cg_dmenu_output(arg_t _)
{
printf("%s\n", files[fileidx].name);
//printf("%s\n", files[fileidx].path);
exit(EXIT_SUCCESS);
}
/*
bool
cg_toggle_squarethumb(arg_t _)
{
squarethumb = !squarethumb;
//return false;
}
*/
#undef G_CMD
#define G_CMD(c) { -1, cg_##c },
#undef I_CMD
#define I_CMD(c) { MODE_IMAGE, ci_##c },
#undef T_CMD
#define T_CMD(c) { MODE_THUMB, ct_##c },
const cmd_t cmds[CMD_COUNT] = {
#include "commands.lst"
};