aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 3bf667a10eaccf30b3f589a5045c1fb6d9a7248e (plain)
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
#define _GNU_SOURCE
#include <unistd.h>

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

#include "meh.h"

#define MODE_NORM 0
#define MODE_LIST 1
#define MODE_CTL 2
static int mode;

extern Display *display;

/* Supported Formats */
struct imageformat *formats[] = {
	&libjpeg,
	&bmp,
	&libpng,
	&netpbm,
	&giflib, /* HACK! make gif last (uses read()) */
	&imagemagick,
	NULL
};


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);
}

struct image *newimage(FILE *f){
	struct image *img = NULL;
	struct imageformat **fmt = formats;
	for(fmt = formats; *fmt; fmt++){
		if((img = (*fmt)->open(f))){
			img->ximg = NULL;
			img->state = NONE;
			return img;
		}
	}
	return NULL;
}

struct image *imgopen(FILE *f){
	return newimage(f);
}

/* For MODE_CTL */
int ctlfd;

static int imageslen;
static int imageidx;
static char **images;

static int width = 0, height = 0;
static struct image *curimg = NULL;

void freeimage(struct image *img){
	if(img){
		if(img->ximg)
			freeXImg(img->ximg);
		if(img->buf)
			free(img->buf);
		free(img);
	}
}

void nextimage(){
	if(++imageidx == imageslen)
		imageidx = 0;
}

void previmage(){
	if(--imageidx < 0)
		imageidx = imageslen - 1;
}

static void (*direction)() = nextimage;

void handlekeypress(XEvent *event){
	KeySym key = XLookupKeysym(&event->xkey, 0);
	switch(key){
		case XK_Escape:
		case XK_q:
			exit(0);
			break;
		case XK_Return:
			puts(images[imageidx]);
			fflush(stdout);
			break;
		case XK_j:
		case XK_k:
			if(mode == MODE_CTL)
				return;
			direction = key == XK_j ? nextimage : previmage;
			direction();
			/* Pass through */
		case XK_r:
			freeimage(curimg);
			curimg = NULL;
			break;
	}
}

void handleevent(XEvent *event){
	switch(event->type){
		case ConfigureNotify:
			if(width != event->xconfigure.width || height != event->xconfigure.height){
				width = event->xconfigure.width;
				height = event->xconfigure.height;
				if(curimg){
					if(curimg->ximg)
						XDestroyImage(curimg->ximg);
					curimg->ximg = NULL;
					if(curimg->state >= LOADED)
						curimg->state = LOADED;
					else if(curimg->state >= FASTLOADED)
						curimg->state = FASTLOADED;
				}

				/* Some window managers need reminding */
				if(curimg)
					setaspect(curimg->bufwidth, curimg->bufheight);
			}
			break;
		case Expose:
			if(curimg){
				if(curimg->state >= SCALED)
					curimg->state = SCALED;
				else if(curimg->state >= LOADED)
					curimg->state = LOADED;
				else if(curimg->state >= FASTSCALED)
					curimg->state = FASTSCALED;
			}
			break;
		case KeyPress:
			if(mode == MODE_CTL)
				break;
			handlekeypress(event);
			break;
	}
}

struct image *imageopen2(char *filename){
	struct image *i;
	FILE *f;
	if((f = fopen(filename, "rb"))){
		if((i = imgopen(f)))
			return i;
		else
			fprintf(stderr, "Invalid format '%s'\n", filename);
	}else{
		fprintf(stderr, "Cannot open '%s'\n", filename);
	}
	return NULL;
}

void doredraw(struct image **i){
	if(!*i){
		if(mode == MODE_CTL){
			if(images[0] == NULL)
				return;
			if(!(*i = imageopen2(images[0]))){
				images[0] = NULL;
				return;
			}
		}else{
			int firstimg = imageidx;
			while(!*i){
				if((*i = imageopen2(images[imageidx]))){
					break;
				}
				direction();
				if(imageidx == firstimg){
					fprintf(stderr, "No valid images to view\n");
					exit(EXIT_FAILURE);
				}
			}
		}

		(*i)->buf = NULL;
		(*i)->state = NONE;
	}else{
		imgstate dstate = (*i)->state + 1;
		if(dstate == LOADED || dstate == FASTLOADED){
			if((*i)->fmt->prep){
				(*i)->fmt->prep(*i);
			}
			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){
				/* We are done with the format's methods */
				(*i)->fmt->close(*i);
			}

			/* state should be set by format */
			assert((*i)->state == LOADED || (*i)->state == FASTLOADED);
		}else if(width && height){
			if(dstate == SCALED || dstate == FASTSCALED2 || dstate == FASTSCALED){
				(*i)->ximg = getimage(*i, width, height, dstate != SCALED);
				(*i)->state = dstate;
				dstate++; /* Always draw after rendering */
			}
		}
		if(dstate == DRAWN || dstate == FASTDRAWN || dstate == FASTDRAWN2){
			assert((*i)->ximg);
			drawimage((*i)->ximg, width, height);
			(*i)->state = dstate;
		}
	}
}

void run(){
	int xfd;
	xfd = ConnectionNumber(display);
	fd_set fds;
	struct timeval tv0 = {0, 0};
	struct timeval *tv = &tv0;
	int maxfd = xfd > ctlfd ? xfd : ctlfd;

	for(;;){
		FD_ZERO(&fds);
		FD_SET(xfd, &fds);
		if(mode == MODE_CTL)
			FD_SET(ctlfd, &fds); /* STDIN */
		int ret = select(maxfd+1, &fds, NULL, NULL, tv);
		if(ret == -1){
			perror("select failed\n");
		}
		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';
			tv = &tv0;
		}
		if(XPending(display)){
			tv = &tv0;
			XEvent event;
			do{
				XNextEvent(display, &event);
				handleevent(&event);
			}while(XPending(display));
		}else if(ret == 0){
			if(mode == MODE_CTL && images[0] == NULL){
				tv = NULL;
			}else if(!curimg || curimg->state != DRAWN){
				doredraw(&curimg);
			//}else if(!nextimg || nextimg->state != DRAWN){
			}else{
				/* If we get here  everything has been drawn in full or we need more input to continue */
				tv = NULL;
			}
		}
	}
}

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 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.2\n");
		return 0;
	}else{
		mode = MODE_NORM;
		images = &argv[1];
		imageslen = argc-1;
		imageidx = 0;
	}
	xinit();
	run();

	return 0;
}