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
|
#define _GNU_SOURCE
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "meh.h"
#define MODE_NORM 0
#define MODE_LIST 1
#define MODE_CTL 2
static int mode;
/* Supported Formats */
static struct imageformat *formats[] = {
&libjpeg,
&bmp,
&libpng,
&netpbm,
&giflib, /* HACK! make gif last (uses read()) */
&imagemagick,
NULL
};
static void usage(){
printf("USAGE: meh [FILE1 [FILE2 [...]]]\n");
printf(" meh -list [LISTFILE] : Treat file as list of images. Defaults to stdin.\n");
printf(" meh -ctl : Display files as they are received on stdin\n");
printf(" meh -v : Print version and exit.\n");
exit(EXIT_FAILURE);
}
static struct image *newimage(FILE *f){
struct image *img = NULL;
struct imageformat **fmt = formats;
for(fmt = formats; *fmt; fmt++){
if((img = (*fmt)->open(f))){
img->state = NONE;
img->backend = NULL;
return img;
}
}
return NULL;
}
/* For MODE_CTL */
static int ctlfd;
static int imageslen;
static int imageidx;
static char **images;
int width = 0, height = 0;
struct image *curimg = NULL;
struct image *nextimg = NULL;
struct image *previmg = NULL;
static void freeimage(struct image *img){
if(img){
backend_free(img);
if(img->buf)
free(img->buf);
free(img);
}
}
static int incidx(int i){
return ++i >= imageslen ? 0 : i;
}
static int decidx(int i){
return --i < 0 ? imageslen - 1 : i;
}
static int (*direction)(int) = incidx;
void key_reload(){
freeimage(curimg);
curimg = NULL;
}
void key_next(){
if(mode != MODE_CTL){
if(curimg)
curimg->state &= LOADED | SLOWLOADED;
freeimage(previmg);
previmg = curimg;
curimg = nextimg;
nextimg = NULL;
if(curimg){
imageidx = curimg->idx;
}else{
imageidx = (direction = incidx)(imageidx);
}
}
}
void key_prev(){
if(mode != MODE_CTL){
if(curimg)
curimg->state &= LOADED | SLOWLOADED;
freeimage(nextimg);
nextimg = curimg;
curimg = previmg;
previmg = NULL;
if(curimg){
imageidx = curimg->idx;
}else{
imageidx = (direction = decidx)(imageidx);
}
}
}
void key_quit(){
exit(EXIT_SUCCESS);
}
void key_action(){
puts(images[imageidx]);
fflush(stdout);
}
struct image *imageopen2(char *filename){
struct image *i;
FILE *f;
if((f = fopen(filename, "rb"))){
if((i = newimage(f))){
return i;
}
else
fprintf(stderr, "Invalid format '%s'\n", filename);
}else{
fprintf(stderr, "Cannot open '%s'\n", filename);
}
return NULL;
}
static int doredraw(struct image **i, int idx, int (*dir)(int), int dstates){
if(!*i){
if(mode == MODE_CTL){
if(images[0] == NULL)
return 0;
if(!(*i = imageopen2(images[0]))){
images[0] = NULL;
return 0;
}
}else{
int firstimg = idx;
while(!*i){
if((*i = imageopen2(images[idx]))){
break;
}
idx = dir(idx);
if(idx == firstimg){
fprintf(stderr, "No valid images to view\n");
exit(EXIT_FAILURE);
}
}
}
(*i)->idx = idx;
(*i)->buf = NULL;
(*i)->state = NONE;
return 1;
}else{
imgstate state = (*i)->state;
if(!(state & LOADED) || ((state & (SLOWLOADED | DRAWN)) == (DRAWN)) ){
if((*i)->fmt->prep){
(*i)->fmt->prep(*i);
}
backend_setaspect((*i)->bufwidth, (*i)->bufheight);
if((*i)->buf)
free((*i)->buf);
(*i)->buf = malloc(3 * (*i)->bufwidth * (*i)->bufheight);
TDEBUG_START
if((*i)->fmt->read(*i)){
fprintf(stderr, "read error!\n");
}
TDEBUG_END("read");
if(((*i)->state & LOADED) && ((*i)->state & SLOWLOADED)){
/* We are done with the format's methods */
(*i)->fmt->close(*i);
}
(*i)->state &= LOADED | SLOWLOADED;
/* state should be set by format */
assert((*i)->state & LOADED);
return 1;
}else if(width && height){
if((dstates & SCALED) && (state & LOADED) && !(state & SCALED)){
backend_prepare(*i, width, height, !!(state & SCALED));
(*i)->state &= LOADED | SLOWLOADED | SCALED | SLOWSCALED;
/* state should be set by backend */
assert((*i)->state & SCALED);
/* state should not be drawn so that we will draw later (also assures correct return value) */
assert(!((*i)->state & DRAWN));
}
}
if((dstates & DRAWN) && ((*i)->state & SCALED) && !(state & DRAWN)){
backend_draw(*i, width, height);
(*i)->state |= DRAWN;
return 1;
}
}
return 0;
}
static void readlist(FILE *f){
int lsize = 16;
imageslen = 0;
images = NULL;
while(!feof(f)){
images = realloc(images, lsize * sizeof(char *));
while(imageslen < lsize && !feof(f)){
char *line = NULL;
size_t slen = 0;
ssize_t read;
read = getline(&line, &slen, f);
if(read > 1){
line[read-1] = '\0';
images[imageslen++] = line;
}else if(line){
free(line);
}
}
lsize *= 2;
}
if(!imageslen){
fprintf(stderr, "No images to view\n");
exit(EXIT_FAILURE);
}
}
int setup_fds(fd_set *fds){
FD_ZERO(fds);
if(mode == MODE_CTL)
FD_SET(ctlfd, fds); /* STDIN */
return ctlfd;
}
int process_fds(fd_set *fds){
if(FD_ISSET(ctlfd, fds)){
assert(mode == MODE_CTL);
size_t slen = 0;
ssize_t read;
if(images[0]){
free(images[0]);
}
freeimage(curimg);
curimg = NULL;
images[0] = NULL;
if((read = getline(images, &slen, stdin)) == -1){
exit(EXIT_SUCCESS);
}
images[0][read-1] = '\0';
return 1;
}
return 0;
}
int process_idle(){
if(mode == MODE_CTL && images[0] == NULL){
return 0;
}else{
int ret = doredraw(&curimg, imageidx, direction, ~0);
imageidx = curimg->idx;
if(!ret){
ret = doredraw(&nextimg, incidx(imageidx), incidx, LOADED | SLOWLOADED);
}
if(!ret){
ret = doredraw(&previmg, decidx(imageidx), decidx, LOADED | SLOWLOADED);
}
return ret;
}
}
int main(int argc, char *argv[]){
if(argc < 2)
usage();
if(!strcmp(argv[1], "-ctl")){
if(argc != 2)
usage();
mode = MODE_CTL;
images = malloc(sizeof(char *));
images[0] = NULL;
imageslen = 1;
imageidx = 0;
ctlfd = 0;
}else if(!strcmp(argv[1], "-list")){
mode = MODE_LIST;
if(argc == 2){
readlist(stdin);
}else if(argc == 3){
FILE *f = fopen(argv[2], "r");
readlist(f);
fclose(f);
}else{
usage();
}
}else if(!strcmp(argv[1], "-v")){
printf("meh version 0.3\n");
return 0;
}else{
mode = MODE_NORM;
images = &argv[1];
imageslen = argc-1;
imageidx = 0;
}
backend_init();
backend_run();
return 0;
}
|