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.
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
22 #define CTRLKEY(x) ((x) - 96)
25 #define PDFCOLS (1 << 11)
26 #define PDFROWS (1 << 12)
29 static struct doc
*doc
;
30 static fbval_t pbuf
[PDFROWS
* PDFCOLS
]; /* current page */
31 static int prows
, pcols
; /* the dimensions of current page */
34 static struct termios termios
;
35 static char filename
[256];
36 static int mark
[128]; /* mark page number */
37 static int mark_head
[128]; /* mark head position */
44 static void draw(void)
47 for (i
= head
; i
< MIN(head
+ fb_rows(), PDFROWS
); i
++)
48 fb_set(i
- head
, 0, pbuf
+ i
* PDFCOLS
+ left
, fb_cols());
51 static int showpage(int p
, int h
)
53 if (p
< 1 || p
> doc_pages(doc
))
55 memset(pbuf
, 0x00, sizeof(pbuf
));
58 doc_draw(doc
, p
, zoom
, rotate
, pbuf
, &prows
, &pcols
);
65 static void zoom_page(int z
)
68 zoom
= MIN(MAXZOOM
, MAX(1, z
));
69 showpage(num
, MIN(PDFROWS
- fb_rows(), head
* zoom
/ _zoom
));
72 static int readkey(void)
75 if (read(STDIN_FILENO
, &b
, 1) <= 0)
80 static int getcount(int def
)
82 int result
= count
? count
: def
;
87 static void printinfo(void)
90 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
91 filename
, num
, doc_pages(doc
), zoom
* 10);
95 static void term_setup(void)
97 struct termios newtermios
;
98 tcgetattr(STDIN_FILENO
, &termios
);
100 newtermios
.c_lflag
&= ~ICANON
;
101 newtermios
.c_lflag
&= ~ECHO
;
102 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
105 static void term_cleanup(void)
107 tcsetattr(STDIN_FILENO
, 0, &termios
);
110 static void sigcont(int sig
)
115 static void reload(void)
118 doc
= doc_open(filename
);
122 static int rightmost(int cont
)
126 for (i
= 0; i
< prows
; i
++) {
128 while (j
> ret
&& pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(0, 0, 0))
130 while (cont
&& j
> ret
&&
131 pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(255, 255, 255))
139 static int leftmost(int cont
)
143 for (i
= 0; i
< prows
; i
++) {
145 while (j
< ret
&& pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(0, 0, 0))
147 while (cont
&& j
< ret
&&
148 pbuf
[i
* PDFCOLS
+ j
] == FB_VAL(255, 255, 255))
156 static void mainloop(void)
158 int step
= fb_rows() / PAGESTEPS
;
159 int hstep
= fb_cols() / PAGESTEPS
;
162 signal(SIGCONT
, sigcont
);
164 while ((c
= readkey()) != -1) {
168 showpage(num
+ getcount(1), 0);
172 showpage(num
- getcount(1), 0);
175 showpage(getcount(doc_pages(doc
)), 0);
178 zoom_page(getcount(15));
181 zoom_page(zoom
* fb_cols() / pcols
);
184 if (leftmost(1) < rightmost(1))
185 zoom_page(zoom
* (fb_cols() - hstep
) /
186 (rightmost(1) - leftmost(1)));
189 zoom_page(zoom
* fb_rows() / prows
);
192 rotate
= getcount(0);
208 mark_head
[c2
] = head
/ zoom
;
217 if (isalpha(c2
) && mark
[c2
])
218 showpage(mark
[c2
], c
== '`' ? mark_head
[c2
] * zoom
: 0);
222 count
= count
* 10 + c
- '0';
226 head
+= step
* getcount(1);
229 head
-= step
* getcount(1);
232 left
+= hstep
* getcount(1);
235 left
-= hstep
* getcount(1);
241 head
= MAX(0, prows
- fb_rows());
244 head
= (prows
- fb_rows()) / 2;
248 head
+= fb_rows() * getcount(1) - step
;
252 head
-= fb_rows() * getcount(1) - step
;
258 left
= rightmost(0) - fb_cols();
261 left
= leftmost(1) - hstep
/ 2;
264 left
= rightmost(1) + hstep
/ 2 - fb_cols();
269 /* no need to redraw */
272 head
= MAX(0, MIN(PDFROWS
- fb_rows(), head
));
273 left
= MAX(0, MIN(PDFCOLS
- fb_cols(), left
));
279 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
281 int main(int argc
, char *argv
[])
283 char *hide
= "\x1b[?25l";
284 char *show
= "\x1b[?25h";
285 char *clear
= "\x1b[2J";
291 strcpy(filename
, argv
[argc
- 1]);
292 doc
= doc_open(filename
);
294 fprintf(stderr
, "cannot open <%s>\n", filename
);
297 while (i
+ 2 < argc
&& argv
[i
][0] == '-') {
298 if (argv
[i
][1] == 'r')
299 rotate
= atoi(argv
[i
+ 1]);
300 if (argv
[i
][1] == 'z')
301 zoom
= atoi(argv
[i
+ 1]);
302 if (argv
[i
][1] == 'p')
303 num
= atoi(argv
[i
+ 1]);
307 write(STDIN_FILENO
, hide
, strlen(hide
));
308 write(STDOUT_FILENO
, clear
, strlen(clear
));
312 left
= (PDFCOLS
- fb_cols()) / 2;
313 if (FBM_BPP(fb_mode()) != sizeof(fbval_t
))
314 fprintf(stderr
, "fbpdf: fbval_t doesn't match fb depth\n");
318 write(STDIN_FILENO
, show
, strlen(show
));