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.
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]; /* page marks */
35 static int mark_head
[128]; /* head in page marks */
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 mainloop(void)
106 int step
= fb_rows() / PAGESTEPS
;
107 int hstep
= fb_cols() / PAGESTEPS
;
110 signal(SIGCONT
, sigcont
);
112 while ((c
= readkey()) != -1) {
113 int maxhead
= PDFROWS
- fb_rows();
114 int maxleft
= PDFCOLS
- fb_cols();
118 showpage(num
+ getcount(1), 0);
122 showpage(num
- getcount(1), 0);
125 showpage(getcount(doc_pages(doc
)), 0);
132 rotate
= getcount(0);
148 mark_head
[c2
] = head
;
154 if (isalpha(c2
) && mark
[c2
])
155 showpage(mark
[c2
], c
== '`' ? mark_head
[c2
] : 0);
159 count
= count
* 10 + c
- '0';
163 head
+= step
* getcount(1);
166 head
-= step
* getcount(1);
169 left
+= hstep
* getcount(1);
172 left
-= hstep
* getcount(1);
185 head
+= fb_rows() * getcount(1) - step
;
189 head
-= fb_rows() * getcount(1) - step
;
194 /* no need to redraw */
197 head
= MAX(0, MIN(maxhead
, head
));
198 left
= MAX(0, MIN(maxleft
, left
));
204 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
206 int main(int argc
, char *argv
[])
208 char *hide
= "\x1b[?25l";
209 char *show
= "\x1b[?25h";
210 char *clear
= "\x1b[2J";
216 strcpy(filename
, argv
[argc
- 1]);
217 doc
= doc_open(filename
);
219 fprintf(stderr
, "cannot open <%s>\n", filename
);
222 while (i
+ 2 < argc
&& argv
[i
][0] == '-') {
223 if (argv
[i
][1] == 'r')
224 rotate
= atoi(argv
[i
+ 1]);
225 if (argv
[i
][1] == 'z')
226 zoom
= atoi(argv
[i
+ 1]);
227 if (argv
[i
][1] == 'p')
228 num
= atoi(argv
[i
+ 1]);
232 write(STDIN_FILENO
, hide
, strlen(hide
));
233 write(STDOUT_FILENO
, clear
, strlen(clear
));
237 if (FBM_BPP(fb_mode()) != sizeof(fbval_t
))
238 fprintf(stderr
, "fbpdf: fbval_t doesn't match fb depth\n");
242 write(STDIN_FILENO
, show
, strlen(show
));