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
];
31 static struct termios termios
;
32 static char filename
[256];
33 static int mark
[128]; /* page marks */
40 static struct doc
*doc
;
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
)
52 if (p
< 1 || p
> pagecount
)
54 memset(pbuf
, 0x00, sizeof(pbuf
));
55 doc_draw(doc
, pbuf
, p
, PDFROWS
, PDFCOLS
, zoom
, rotate
);
62 static int readkey(void)
65 if (read(STDIN_FILENO
, &b
, 1) <= 0)
70 static int getcount(int def
)
72 int result
= count
? count
: def
;
77 static void printinfo(void)
80 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
81 filename
, num
, pagecount
, zoom
* 10);
85 static void term_setup(void)
87 struct termios newtermios
;
88 tcgetattr(STDIN_FILENO
, &termios
);
90 newtermios
.c_lflag
&= ~ICANON
;
91 newtermios
.c_lflag
&= ~ECHO
;
92 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &newtermios
);
95 static void term_cleanup(void)
97 tcsetattr(STDIN_FILENO
, 0, &termios
);
100 static void sigcont(int sig
)
105 static void mainloop(void)
107 int step
= fb_rows() / PAGESTEPS
;
108 int hstep
= fb_cols() / PAGESTEPS
;
111 signal(SIGCONT
, sigcont
);
113 while ((c
= readkey()) != -1) {
114 int maxhead
= PDFROWS
- fb_rows();
115 int maxleft
= PDFCOLS
- fb_cols();
119 showpage(num
+ getcount(1));
123 showpage(num
- getcount(1));
126 showpage(getcount(pagecount
));
133 rotate
= getcount(0);
153 if (isalpha(c2
) && mark
[c2
])
158 count
= count
* 10 + c
- '0';
162 head
+= step
* getcount(1);
165 head
-= step
* getcount(1);
168 left
+= hstep
* getcount(1);
171 left
-= hstep
* getcount(1);
184 head
+= fb_rows() * getcount(1) - step
;
188 head
-= fb_rows() * getcount(1) - step
;
193 /* no need to redraw */
196 head
= MAX(0, MIN(maxhead
, head
));
197 left
= MAX(0, MIN(maxleft
, left
));
203 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
205 int main(int argc
, char *argv
[])
207 char *hide
= "\x1b[?25l";
208 char *show
= "\x1b[?25h";
209 char *clear
= "\x1b[2J";
215 strcpy(filename
, argv
[argc
- 1]);
216 doc
= doc_open(filename
);
218 printf("cannot open file\n");
221 pagecount
= doc_pages(doc
);
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
));