poppler: update to version 0.22.1
[fbpdf.git] / fbpdf.c
blob8cfc880b82b82c8e901b42dd6426cf9bc5d0eb22
1 /*
2 * fbpdf - a small framebuffer pdf viewer using mupdf
4 * Copyright (C) 2009-2013 Ali Gholami Rudi <ali at rudi dot ir>
6 * This program is released under the Modified BSD license.
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 struct doc *doc;
29 static fbval_t pbuf[PDFROWS * PDFCOLS]; /* current page */
30 static int prows, pcols; /* the dimensions of current page */
32 static int num = 1;
33 static struct termios termios;
34 static char filename[256];
35 static int mark[128]; /* mark page number */
36 static int mark_head[128]; /* mark head position */
37 static int zoom = 15;
38 static int rotate;
39 static int head;
40 static int left;
41 static int count;
43 static void draw(void)
45 int i;
46 for (i = head; i < MIN(head + fb_rows(), PDFROWS); i++)
47 fb_set(i - head, 0, pbuf + i * PDFCOLS + left, fb_cols());
50 static int showpage(int p, int h)
52 if (p < 1 || p > doc_pages(doc))
53 return 0;
54 memset(pbuf, 0x00, sizeof(pbuf));
55 prows = PDFROWS;
56 pcols = PDFCOLS;
57 doc_draw(doc, p, zoom, rotate, pbuf, &prows, &pcols);
58 num = p;
59 head = h;
60 draw();
61 return 0;
64 static void zoom_page(int z)
66 int _zoom = zoom;
67 zoom = z;
68 showpage(num, MIN(PDFROWS - fb_rows(), head * zoom / _zoom));
71 static int readkey(void)
73 unsigned char b;
74 if (read(STDIN_FILENO, &b, 1) <= 0)
75 return -1;
76 return b;
79 static int getcount(int def)
81 int result = count ? count : def;
82 count = 0;
83 return result;
86 static void printinfo(void)
88 printf("\x1b[H");
89 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
90 filename, num, doc_pages(doc), zoom * 10);
91 fflush(stdout);
94 static void term_setup(void)
96 struct termios newtermios;
97 tcgetattr(STDIN_FILENO, &termios);
98 newtermios = termios;
99 newtermios.c_lflag &= ~ICANON;
100 newtermios.c_lflag &= ~ECHO;
101 tcsetattr(STDIN_FILENO, TCSAFLUSH, &newtermios);
104 static void term_cleanup(void)
106 tcsetattr(STDIN_FILENO, 0, &termios);
109 static void sigcont(int sig)
111 term_setup();
114 static void reload(void)
116 doc_close(doc);
117 doc = doc_open(filename);
118 showpage(num, head);
121 static void mainloop(void)
123 int step = fb_rows() / PAGESTEPS;
124 int hstep = fb_cols() / PAGESTEPS;
125 int c, c2;
126 term_setup();
127 signal(SIGCONT, sigcont);
128 showpage(num, 0);
129 while ((c = readkey()) != -1) {
130 switch (c) {
131 case CTRLKEY('f'):
132 case 'J':
133 showpage(num + getcount(1), 0);
134 break;
135 case CTRLKEY('b'):
136 case 'K':
137 showpage(num - getcount(1), 0);
138 break;
139 case 'G':
140 showpage(getcount(doc_pages(doc)), 0);
141 break;
142 case 'z':
143 zoom_page(getcount(15));
144 break;
145 case 'w':
146 zoom_page(zoom * fb_cols() / pcols);
147 break;
148 case 'f':
149 zoom_page(zoom * fb_rows() / prows);
150 break;
151 case 'r':
152 rotate = getcount(0);
153 showpage(num, 0);
154 break;
155 case 'i':
156 printinfo();
157 break;
158 case 'q':
159 term_cleanup();
160 return;
161 case 27:
162 count = 0;
163 break;
164 case 'm':
165 c2 = readkey();
166 if (isalpha(c2)) {
167 mark[c2] = num;
168 mark_head[c2] = head / zoom;
170 break;
171 case 'e':
172 reload();
173 break;
174 case '`':
175 case '\'':
176 c2 = readkey();
177 if (isalpha(c2) && mark[c2])
178 showpage(mark[c2], c == '`' ? mark_head[c2] * zoom : 0);
179 break;
180 default:
181 if (isdigit(c))
182 count = count * 10 + c - '0';
184 switch (c) {
185 case 'j':
186 head += step * getcount(1);
187 break;
188 case 'k':
189 head -= step * getcount(1);
190 break;
191 case 'l':
192 left += hstep * getcount(1);
193 break;
194 case 'h':
195 left -= hstep * getcount(1);
196 break;
197 case 'H':
198 head = 0;
199 break;
200 case 'L':
201 head = MAX(0, prows - fb_rows());
202 break;
203 case 'M':
204 head = prows / 2;
205 break;
206 case ' ':
207 case CTRL('d'):
208 head += fb_rows() * getcount(1) - step;
209 break;
210 case 127:
211 case CTRL('u'):
212 head -= fb_rows() * getcount(1) - step;
213 break;
214 case CTRLKEY('l'):
215 break;
216 default:
217 /* no need to redraw */
218 continue;
220 head = MAX(0, MIN(PDFROWS - fb_rows(), head));
221 left = MAX(0, MIN(PDFCOLS - fb_cols(), left));
222 draw();
226 static char *usage =
227 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
229 int main(int argc, char *argv[])
231 char *hide = "\x1b[?25l";
232 char *show = "\x1b[?25h";
233 char *clear = "\x1b[2J";
234 int i = 1;
235 if (argc < 2) {
236 printf(usage);
237 return 1;
239 strcpy(filename, argv[argc - 1]);
240 doc = doc_open(filename);
241 if (!doc) {
242 fprintf(stderr, "cannot open <%s>\n", filename);
243 return 1;
245 while (i + 2 < argc && argv[i][0] == '-') {
246 if (argv[i][1] == 'r')
247 rotate = atoi(argv[i + 1]);
248 if (argv[i][1] == 'z')
249 zoom = atoi(argv[i + 1]);
250 if (argv[i][1] == 'p')
251 num = atoi(argv[i + 1]);
252 i += 2;
255 write(STDIN_FILENO, hide, strlen(hide));
256 write(STDOUT_FILENO, clear, strlen(clear));
257 printinfo();
258 if (fb_init())
259 return 1;
260 left = (PDFCOLS - fb_cols()) / 2;
261 if (FBM_BPP(fb_mode()) != sizeof(fbval_t))
262 fprintf(stderr, "fbpdf: fbval_t doesn't match fb depth\n");
263 else
264 mainloop();
265 fb_free();
266 write(STDIN_FILENO, show, strlen(show));
267 printf("\n");
268 doc_close(doc);
269 return 0;