forked from xyb3rt/sxiv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_pallete.c
More file actions
311 lines (270 loc) · 7.14 KB
/
parse_pallete.c
File metadata and controls
311 lines (270 loc) · 7.14 KB
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
/* sxiv: main.c
* Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _POSIX_C_SOURCE 200112L
#define _MAPPINGS_CONFIG
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include "commands.h"
#include "image.h"
#include "options.h"
#include "thumbs.h"
#include "types.h"
#include "util.h"
#include "window.h"
#include "config.h"
#include "palette.h"
enum {
INFO_STR_LEN = 256,
FILENAME_CNT = 1024
};
typedef struct {
struct timeval when;
bool active;
timeout_f handler;
} timeout_t;
/* timeout handler functions: */
void redraw(void);
void reset_cursor(void);
void animate(void);
void clear_resize(void);
appmode_t mode;
img_t img;
tns_t tns;
win_t win;
fileinfo_t *files;
int filecnt, fileidx;
size_t filesize;
int prefix;
bool resized = false;
char win_bar_l[INFO_STR_LEN];
char win_bar_r[INFO_STR_LEN];
char win_title[INFO_STR_LEN];
timeout_t timeouts[] = {
{ { 0, 0 }, false, redraw },
{ { 0, 0 }, false, reset_cursor },
{ { 0, 0 }, false, animate },
{ { 0, 0 }, false, clear_resize },
};
void cleanup(void) {
static bool in = false;
if (!in) {
in = true;
img_close(&img, false);
tns_free(&tns);
win_close(&win);
}
}
void check_add_file(char *filename) {
const char *bn;
if (filename == NULL || *filename == '\0')
return;
if (access(filename, R_OK) < 0) {
warn("could not open file: %s", filename);
return;
}
if (fileidx == filecnt) {
filecnt *= 2;
files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
}
if (*filename != '/') {
files[fileidx].path = absolute_path(filename);
if (files[fileidx].path == NULL) {
warn("could not get absolute path of file: %s\n", filename);
return;
}
}
files[fileidx].loaded = false;
files[fileidx].name = s_strdup(filename);
if (*filename == '/')
files[fileidx].path = files[fileidx].name;
if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
files[fileidx].base = ++bn;
else
files[fileidx].base = files[fileidx].name;
fileidx++;
}
void remove_file(int n, bool manual) {
if (n < 0 || n >= filecnt)
return;
if (filecnt == 1) {
if (!manual)
fprintf(stderr, "sxiv: no more files to display, aborting\n");
cleanup();
exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
}
if (files[n].path != files[n].name)
free((void*) files[n].path);
free((void*) files[n].name);
if (n + 1 < filecnt)
memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
if (n + 1 < tns.cnt) {
memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
sizeof(thumb_t));
memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
}
filecnt--;
if (n < tns.cnt)
tns.cnt--;
}
void set_timeout(timeout_f handler, int time, bool overwrite) {
int i;
for (i = 0; i < ARRLEN(timeouts); i++) {
if (timeouts[i].handler == handler) {
if (!timeouts[i].active || overwrite) {
gettimeofday(&timeouts[i].when, 0);
TV_ADD_MSEC(&timeouts[i].when, time);
timeouts[i].active = true;
}
return;
}
}
}
void reset_timeout(timeout_f handler) {
int i;
for (i = 0; i < ARRLEN(timeouts); i++) {
if (timeouts[i].handler == handler) {
timeouts[i].active = false;
return;
}
}
}
bool check_timeouts(struct timeval *t) {
int i = 0, tdiff, tmin = -1;
struct timeval now;
gettimeofday(&now, 0);
while (i < ARRLEN(timeouts)) {
if (timeouts[i].active) {
tdiff = TV_DIFF(&timeouts[i].when, &now);
if (tdiff <= 0) {
timeouts[i].active = false;
if (timeouts[i].handler != NULL)
timeouts[i].handler();
i = tmin = -1;
} else if (tmin < 0 || tdiff < tmin) {
tmin = tdiff;
}
}
i++;
}
if (tmin > 0 && t != NULL)
TV_SET_MSEC(t, tmin);
return tmin > 0;
}
void update_info(void) {
int i, fw, pw, fi, ln, rn;
char frame_info[16];
const char *size_unit;
float size = filesize;
pw = 0;
for (i = filecnt; i > 0; i /= 10)
pw++;
if (mode == MODE_THUMB) {
if (tns.cnt != filecnt) {
snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
pw, tns.cnt, filecnt);
} else {
fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
pw, tns.sel + 1, filecnt, BAR_SEPARATOR);
ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
files[tns.sel].name) + fi;
if (win_textwidth(win_bar_l, ln, true) > win.w)
snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
files[tns.sel].base);
}
win_set_title(&win, "sxiv");
win_set_bar_info(&win, win_bar_l, NULL);
} else {
size_readable(&size, &size_unit);
if (img.multi.cnt > 0) {
fw = 0;
for (i = img.multi.cnt; i > 0; i /= 10)
fw++;
snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
BAR_SEPARATOR, fw, img.multi.sel+1, img.multi.cnt);
} else {
frame_info[0] = '\0';
}
fi = snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s",
pw, fileidx + 1, filecnt, BAR_SEPARATOR);
ln = snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
files[fileidx].name) + fi;
rn = snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
(int) (img.zoom * 100.0), frame_info);
if (win_textwidth(win_bar_l, ln, true) +
win_textwidth(win_bar_r, rn, true) > win.w)
{
snprintf(win_bar_l + fi, sizeof win_bar_l - fi, "%s",
files[fileidx].base);
}
win_set_bar_info(&win, win_bar_l, win_bar_r);
snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
win_set_title(&win, win_title);
}
}
void redraw(void) {
if (mode == MODE_IMAGE)
img_render(&img);
else
tns_render(&tns);
update_info();
win_draw(&win);
reset_timeout(redraw);
reset_cursor();
}
void reset_cursor(void) {
int i;
cursor_t cursor = CURSOR_NONE;
if (mode == MODE_IMAGE) {
for (i = 0; i < ARRLEN(timeouts); i++) {
if (timeouts[i].handler == reset_cursor) {
if (timeouts[i].active)
cursor = CURSOR_ARROW;
break;
}
}
} else {
if (tns.cnt != filecnt)
cursor = CURSOR_WATCH;
else
cursor = CURSOR_ARROW;
}
win_set_cursor(&win, cursor);
}
void animate(void) {
if (img_frame_animate(&img, false)) {
redraw();
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
}
}
void clear_resize(void) {
resized = false;
}
int main(int argc, char **argv) {
palette_t * p;
p = load_palettes("tag.palette.yml");
display_palette(p);
return 0;
}