2 * fbpdf - a small framebuffer pdf viewer using mupdf
4 * Copyright (C) 2009-2012 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)
28 static struct doc
*doc
;
29 static fbval_t pbuf
[PDFROWS
* PDFCOLS
]; /* current page */
30 static int prows
, pcols
; /* the dimensions of current page */
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 */
43 static void draw(void)
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
))
54 memset(pbuf
, 0x00, sizeof(pbuf
));
57 doc_draw(doc
, p
, zoom
, rotate
, pbuf
, &prows
, &pcols
);
64 static void zoom_page(int z
)
68 showpage(num
, MIN(PDFROWS
- fb_rows(), head
* zoom
/ _zoom
));
71 static int readkey(void)
74 if (read(STDIN_FILENO
, &b
, 1) <= 0)
79 static int getcount(int def
)
81 int result
= count
? count
: def
;
86 static void printinfo(void)
89 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
90 filename
, num
, doc_pages(doc
), zoom
* 10);
94 static void term_setup(void)
96 struct termios newtermios
;
97 tcgetattr(STDIN_FILENO
, &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
)
114 static void reload(void)
117 doc
= doc_open(filename
);
121 static void mainloop(void)
123 int step
= fb_rows() / PAGESTEPS
;
124 int hstep
= fb_cols() / PAGESTEPS
;
127 signal(SIGCONT
, sigcont
);
129 while ((c
= readkey()) != -1) {
133 showpage(num
+ getcount(1), 0);
137 showpage(num
- getcount(1), 0);
140 showpage(getcount(doc_pages(doc
)), 0);
143 zoom_page(getcount(15));
146 zoom_page(zoom
* fb_cols() / pcols
);
149 zoom_page(zoom
* fb_rows() / prows
);
152 rotate
= getcount(0);
168 mark_head
[c2
] = head
/ zoom
;
177 if (isalpha(c2
) && mark
[c2
])
178 showpage(mark
[c2
], c
== '`' ? mark_head
[c2
] * zoom
: 0);
182 count
= count
* 10 + c
- '0';
186 head
+= step
* getcount(1);
189 head
-= step
* getcount(1);
192 left
+= hstep
* getcount(1);
195 left
-= hstep
* getcount(1);
201 head
= MAX(0, prows
- fb_rows());
208 head
+= fb_rows() * getcount(1) - step
;
212 head
-= fb_rows() * getcount(1) - step
;
217 /* no need to redraw */
220 head
= MAX(0, MIN(PDFROWS
- fb_rows(), head
));
221 left
= MAX(0, MIN(PDFCOLS
- fb_cols(), left
));
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";
239 strcpy(filename
, argv
[argc
- 1]);
240 doc
= doc_open(filename
);
242 fprintf(stderr
, "cannot open <%s>\n", filename
);
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]);
255 write(STDIN_FILENO
, hide
, strlen(hide
));
256 write(STDOUT_FILENO
, clear
, strlen(clear
));
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");
266 write(STDIN_FILENO
, show
, strlen(show
));