mupdf: support version 1.0rc1
[fbpdf.git] / fbpdf.c
blobfce058952eafa36968dd5ac34de9de34111e6c34
1 /*
2 * fbpdf - a small framebuffer pdf viewer using mupdf
4 * Copyright (C) 2009-2011 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
7 */
8 #include <ctype.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <pty.h>
15 #include "draw.h"
16 #include "doc.h"
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
21 #define PAGESTEPS 8
22 #define CTRLKEY(x) ((x) - 96)
23 #define MAXWIDTH 2
24 #define MAXHEIGHT 3
25 #define PDFCOLS (1 << 11)
26 #define PDFROWS (1 << 12)
28 static fbval_t pbuf[PDFROWS * PDFCOLS];
29 static struct doc *doc;
31 static int num = 1;
32 static struct termios termios;
33 static char filename[256];
34 static int mark[128]; /* page marks */
35 static int mark_head[128]; /* head in page marks */
36 static int zoom = 15;
37 static int rotate;
38 static int head;
39 static int left;
40 static int count;
42 static void draw(void)
44 int i;
45 for (i = head; i < MIN(head + fb_rows(), PDFROWS); i++)
46 fb_set(i - head, 0, pbuf + i * PDFCOLS + left, fb_cols());
49 static int showpage(int p, int h)
51 if (p < 1 || p > doc_pages(doc))
52 return 0;
53 memset(pbuf, 0x00, sizeof(pbuf));
54 doc_draw(doc, pbuf, p, PDFROWS, PDFCOLS, zoom, rotate);
55 num = p;
56 head = h;
57 draw();
58 return 0;
61 static int readkey(void)
63 unsigned char b;
64 if (read(STDIN_FILENO, &b, 1) <= 0)
65 return -1;
66 return b;
69 static int getcount(int def)
71 int result = count ? count : def;
72 count = 0;
73 return result;
76 static void printinfo(void)
78 printf("\x1b[H");
79 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
80 filename, num, doc_pages(doc), zoom * 10);
81 fflush(stdout);
84 static void term_setup(void)
86 struct termios newtermios;
87 tcgetattr(STDIN_FILENO, &termios);
88 newtermios = termios;
89 newtermios.c_lflag &= ~ICANON;
90 newtermios.c_lflag &= ~ECHO;
91 tcsetattr(STDIN_FILENO, TCSAFLUSH, &newtermios);
94 static void term_cleanup(void)
96 tcsetattr(STDIN_FILENO, 0, &termios);
99 static void sigcont(int sig)
101 term_setup();
104 static void reload(void)
106 doc_close(doc);
107 doc = doc_open(filename);
108 showpage(num, head);
111 static void mainloop(void)
113 int step = fb_rows() / PAGESTEPS;
114 int hstep = fb_cols() / PAGESTEPS;
115 int c, c2;
116 term_setup();
117 signal(SIGCONT, sigcont);
118 showpage(num, 0);
119 while ((c = readkey()) != -1) {
120 int maxhead = PDFROWS - fb_rows();
121 int maxleft = PDFCOLS - fb_cols();
122 switch (c) {
123 case CTRLKEY('f'):
124 case 'J':
125 showpage(num + getcount(1), 0);
126 break;
127 case CTRLKEY('b'):
128 case 'K':
129 showpage(num - getcount(1), 0);
130 break;
131 case 'G':
132 showpage(getcount(doc_pages(doc)), 0);
133 break;
134 case 'z':
135 zoom = getcount(15);
136 showpage(num, 0);
137 break;
138 case 'r':
139 rotate = getcount(0);
140 showpage(num, 0);
141 break;
142 case 'i':
143 printinfo();
144 break;
145 case 'q':
146 term_cleanup();
147 return;
148 case 27:
149 count = 0;
150 break;
151 case 'm':
152 c2 = readkey();
153 if (isalpha(c2)) {
154 mark[c2] = num;
155 mark_head[c2] = head;
157 break;
158 case 'e':
159 reload();
160 break;
161 case '`':
162 case '\'':
163 c2 = readkey();
164 if (isalpha(c2) && mark[c2])
165 showpage(mark[c2], c == '`' ? mark_head[c2] : 0);
166 break;
167 default:
168 if (isdigit(c))
169 count = count * 10 + c - '0';
171 switch (c) {
172 case 'j':
173 head += step * getcount(1);
174 break;
175 case 'k':
176 head -= step * getcount(1);
177 break;
178 case 'l':
179 left += hstep * getcount(1);
180 break;
181 case 'h':
182 left -= hstep * getcount(1);
183 break;
184 case 'H':
185 head = 0;
186 break;
187 case 'L':
188 head = maxhead;
189 break;
190 case 'M':
191 head = maxhead / 2;
192 break;
193 case ' ':
194 case CTRL('d'):
195 head += fb_rows() * getcount(1) - step;
196 break;
197 case 127:
198 case CTRL('u'):
199 head -= fb_rows() * getcount(1) - step;
200 break;
201 case CTRLKEY('l'):
202 break;
203 default:
204 /* no need to redraw */
205 continue;
207 head = MAX(0, MIN(maxhead, head));
208 left = MAX(0, MIN(maxleft, left));
209 draw();
213 static char *usage =
214 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
216 int main(int argc, char *argv[])
218 char *hide = "\x1b[?25l";
219 char *show = "\x1b[?25h";
220 char *clear = "\x1b[2J";
221 int i = 1;
222 if (argc < 2) {
223 printf(usage);
224 return 1;
226 strcpy(filename, argv[argc - 1]);
227 doc = doc_open(filename);
228 if (!doc) {
229 fprintf(stderr, "cannot open <%s>\n", filename);
230 return 1;
232 while (i + 2 < argc && argv[i][0] == '-') {
233 if (argv[i][1] == 'r')
234 rotate = atoi(argv[i + 1]);
235 if (argv[i][1] == 'z')
236 zoom = atoi(argv[i + 1]);
237 if (argv[i][1] == 'p')
238 num = atoi(argv[i + 1]);
239 i += 2;
242 write(STDIN_FILENO, hide, strlen(hide));
243 write(STDOUT_FILENO, clear, strlen(clear));
244 printinfo();
245 if (fb_init())
246 return 1;
247 if (FBM_BPP(fb_mode()) != sizeof(fbval_t))
248 fprintf(stderr, "fbpdf: fbval_t doesn't match fb depth\n");
249 else
250 mainloop();
251 fb_free();
252 write(STDIN_FILENO, show, strlen(show));
253 printf("\n");
254 doc_close(doc);
255 return 0;