djvu: include file renames and better zoom scale
[fbpdf.git] / fbpdf.c
blob42051c513de9437f61700f1549f4b12e39aa3e0a
1 /*
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.
7 */
8 #include <ctype.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <pty.h>
15 #include "draw.h"
16 #include "doc.h"
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
21 #define PAGESTEPS 8
22 #define CTRLKEY(x) ((x) - 96)
23 #define MAXWIDTH 2
24 #define MAXHEIGHT 3
25 #define PDFCOLS (1 << 11)
26 #define PDFROWS (1 << 12)
28 static fbval_t pbuf[PDFROWS * PDFCOLS];
30 static int num = 1;
31 static struct termios termios;
32 static char filename[256];
33 static int zoom = 15;
34 static int rotate;
35 static int head;
36 static int left;
37 static int count;
39 static struct doc *doc;
40 static int pagecount;
42 static void draw(void)
44 int i;
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)
51 if (p < 1 || p > pagecount)
52 return 0;
53 memset(pbuf, 0x00, sizeof(pbuf));
54 doc_draw(doc, pbuf, p, PDFROWS, PDFCOLS, zoom, rotate);
55 num = p;
56 head = 0;
57 draw();
58 return 0;
61 static int readkey(void)
63 unsigned char b;
64 if (read(STDIN_FILENO, &b, 1) <= 0)
65 return -1;
66 return b;
69 static int getcount(int def)
71 int result = count ? count : def;
72 count = 0;
73 return result;
76 static void printinfo(void)
78 printf("\x1b[H");
79 printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1b[K",
80 filename, num, pagecount, zoom * 10);
81 fflush(stdout);
84 static void term_setup(void)
86 struct termios newtermios;
87 tcgetattr(STDIN_FILENO, &termios);
88 newtermios = 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)
101 term_setup();
104 static void mainloop(void)
106 int step = fb_rows() / PAGESTEPS;
107 int hstep = fb_cols() / PAGESTEPS;
108 int c;
109 term_setup();
110 signal(SIGCONT, sigcont);
111 showpage(num);
112 while ((c = readkey()) != -1) {
113 int maxhead = PDFROWS - fb_rows();
114 int maxleft = PDFCOLS - fb_cols();
115 switch (c) {
116 case CTRLKEY('f'):
117 case 'J':
118 showpage(num + getcount(1));
119 break;
120 case CTRLKEY('b'):
121 case 'K':
122 showpage(num - getcount(1));
123 break;
124 case 'G':
125 showpage(getcount(pagecount));
126 break;
127 case 'z':
128 zoom = getcount(15);
129 showpage(num);
130 break;
131 case 'r':
132 rotate = getcount(0);
133 showpage(num);
134 break;
135 case 'i':
136 printinfo();
137 break;
138 case 'q':
139 term_cleanup();
140 return;
141 case 27:
142 count = 0;
143 break;
144 default:
145 if (isdigit(c))
146 count = count * 10 + c - '0';
148 switch (c) {
149 case 'j':
150 head += step * getcount(1);
151 break;
152 case 'k':
153 head -= step * getcount(1);
154 break;
155 case 'l':
156 left += hstep * getcount(1);
157 break;
158 case 'h':
159 left -= hstep * getcount(1);
160 break;
161 case 'H':
162 head = 0;
163 break;
164 case 'L':
165 head = maxhead;
166 break;
167 case 'M':
168 head = maxhead / 2;
169 break;
170 case ' ':
171 case CTRL('d'):
172 head += fb_rows() * getcount(1) - step;
173 break;
174 case 127:
175 case CTRL('u'):
176 head -= fb_rows() * getcount(1) - step;
177 break;
178 case CTRLKEY('l'):
179 break;
180 default:
181 /* no need to redraw */
182 continue;
184 head = MAX(0, MIN(maxhead, head));
185 left = MAX(0, MIN(maxleft, left));
186 draw();
190 static char *usage =
191 "usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename\n";
193 int main(int argc, char *argv[])
195 char *hide = "\x1b[?25l";
196 char *show = "\x1b[?25h";
197 char *clear = "\x1b[2J";
198 int i = 1;
199 if (argc < 2) {
200 printf(usage);
201 return 1;
203 strcpy(filename, argv[argc - 1]);
204 doc = doc_open(filename);
205 if (!doc) {
206 printf("cannot open file\n");
207 return 1;
209 pagecount = doc_pages(doc);
210 while (i + 2 < argc && argv[i][0] == '-') {
211 if (argv[i][1] == 'r')
212 rotate = atoi(argv[i + 1]);
213 if (argv[i][1] == 'z')
214 zoom = atoi(argv[i + 1]);
215 if (argv[i][1] == 'p')
216 num = atoi(argv[i + 1]);
217 i += 2;
220 write(STDIN_FILENO, hide, strlen(hide));
221 write(STDOUT_FILENO, clear, strlen(clear));
222 printinfo();
223 if (fb_init())
224 return 1;
225 if (FBM_BPP(fb_mode()) != sizeof(fbval_t))
226 fprintf(stderr, "fbpdf: fbval_t doesn't match fb depth\n");
227 else
228 mainloop();
229 fb_free();
230 write(STDIN_FILENO, show, strlen(show));
231 printf("\n");
232 doc_close(doc);
233 return 0;