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 fbval_t pbuf
[PDFROWS
* PDFCOLS
];
29 static struct doc
*doc
;
32 static struct termios termios
;
33 static char filename
[256];
34 static int mark
[128]; /* mark page number */
35 static int mark_head
[128]; /* mark head position */
42 static void draw(void)
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
))
53 memset(pbuf
, 0x00, sizeof(pbuf
));
54 doc_draw(doc
, pbuf
, p
, PDFROWS
, PDFCOLS
, zoom
, rotate
);
61 static int readkey(void)
64 if (read(STDIN_FILENO
, &b
, 1) <= 0)
69 static int getcount(int def
)
71 int result
= count
? count
: def
;
76 static void printinfo(void)
79 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
80 filename
, num
, doc_pages(doc
), zoom
* 10);
84 static void term_setup(void)
86 struct termios newtermios
;
87 tcgetattr(STDIN_FILENO
, &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
)
104 static void reload(void)
107 doc
= doc_open(filename
);
111 static void mainloop(void)
113 int step
= fb_rows() / PAGESTEPS
;
114 int hstep
= fb_cols() / PAGESTEPS
;
118 signal(SIGCONT
, sigcont
);
120 while ((c
= readkey()) != -1) {
121 int maxhead
= PDFROWS
- fb_rows();
122 int maxleft
= PDFCOLS
- fb_cols();
126 showpage(num
+ getcount(1), 0);
130 showpage(num
- getcount(1), 0);
133 showpage(getcount(doc_pages(doc
)), 0);
138 showpage(num
, MIN(maxhead
, head
* zoom
/ _zoom
));
141 rotate
= getcount(0);
157 mark_head
[c2
] = head
/ zoom
;
166 if (isalpha(c2
) && mark
[c2
])
167 showpage(mark
[c2
], c
== '`' ? mark_head
[c2
] * zoom
: 0);
171 count
= count
* 10 + c
- '0';
175 head
+= step
* getcount(1);
178 head
-= step
* getcount(1);
181 left
+= hstep
* getcount(1);
184 left
-= hstep
* getcount(1);
197 head
+= fb_rows() * getcount(1) - step
;
201 head
-= fb_rows() * getcount(1) - step
;
206 /* no need to redraw */
209 head
= MAX(0, MIN(maxhead
, head
));
210 left
= MAX(0, MIN(maxleft
, left
));
216 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
218 int main(int argc
, char *argv
[])
220 char *hide
= "\x1b[?25l";
221 char *show
= "\x1b[?25h";
222 char *clear
= "\x1b[2J";
228 strcpy(filename
, argv
[argc
- 1]);
229 doc
= doc_open(filename
);
231 fprintf(stderr
, "cannot open <%s>\n", filename
);
234 while (i
+ 2 < argc
&& argv
[i
][0] == '-') {
235 if (argv
[i
][1] == 'r')
236 rotate
= atoi(argv
[i
+ 1]);
237 if (argv
[i
][1] == 'z')
238 zoom
= atoi(argv
[i
+ 1]);
239 if (argv
[i
][1] == 'p')
240 num
= atoi(argv
[i
+ 1]);
244 write(STDIN_FILENO
, hide
, strlen(hide
));
245 write(STDOUT_FILENO
, clear
, strlen(clear
));
249 left
= (PDFCOLS
- fb_cols()) / 2;
250 if (FBM_BPP(fb_mode()) != sizeof(fbval_t
))
251 fprintf(stderr
, "fbpdf: fbval_t doesn't match fb depth\n");
255 write(STDIN_FILENO
, show
, strlen(show
));