3e9edcd455c0b8bde384cc1dfb00230a3485060b
[fbpdf.git] / fbpdf.c
blob3e9edcd455c0b8bde384cc1dfb00230a3485060b
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];
29 static struct doc *doc;
31 static int num = 1;
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 */
36 static int zoom = 15;
37 static int rotate;
38 static int head;
39 static int left;
40 static int count;
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, int h)
51 if (p < 1 || p > doc_pages(doc))
52 return 0;
53 memset(pbuf, 0x00, sizeof(pbuf));
54 doc_draw(doc, pbuf, p, PDFROWS, PDFCOLS, zoom, rotate);
55 num = p;
56 head = h;
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, doc_pages(doc), 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, c2;
109 term_setup();
110 signal(SIGCONT, sigcont);
111 showpage(num, 0);
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), 0);
119 break;
120 case CTRLKEY('b'):
121 case 'K':
122 showpage(num - getcount(1), 0);
123 break;
124 case 'G':
125 showpage(getcount(doc_pages(doc)), 0);
126 break;
127 case 'z':
128 zoom = getcount(15);
129 showpage(num, 0);
130 break;
131 case 'r':
132 rotate = getcount(0);
133 showpage(num, 0);
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 case 'm':
145 c2 = readkey();
146 if (isalpha(c2)) {
147 mark[c2] = num;
148 mark_head[c2] = head;
150 break;
151 case '`':
152 case '\'':
153 c2 = readkey();
154 if (isalpha(c2) && mark[c2])
155 showpage(mark[c2], c == '`' ? mark_head[c2] : 0);
156 break;
157 default:
158 if (isdigit(c))
159 count = count * 10 + c - '0';
161 switch (c) {
162 case 'j':
163 head += step * getcount(1);
164 break;
165 case 'k':
166 head -= step * getcount(1);
167 break;
168 case 'l':
169 left += hstep * getcount(1);
170 break;
171 case 'h':
172 left -= hstep * getcount(1);
173 break;
174 case 'H':
175 head = 0;
176 break;
177 case 'L':
178 head = maxhead;
179 break;
180 case 'M':
181 head = maxhead / 2;
182 break;
183 case ' ':
184 case CTRL('d'):
185 head += fb_rows() * getcount(1) - step;
186 break;
187 case 127:
188 case CTRL('u'):
189 head -= fb_rows() * getcount(1) - step;
190 break;
191 case CTRLKEY('l'):
192 break;
193 default:
194 /* no need to redraw */
195 continue;
197 head = MAX(0, MIN(maxhead, head));
198 left = MAX(0, MIN(maxleft, left));
199 draw();
203 static char *usage =
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";
211 int i = 1;
212 if (argc < 2) {
213 printf(usage);
214 return 1;
216 strcpy(filename, argv[argc - 1]);
217 doc = doc_open(filename);
218 if (!doc) {
219 fprintf(stderr, "cannot open <%s>\n", filename);
220 return 1;
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]);
229 i += 2;
232 write(STDIN_FILENO, hide, strlen(hide));
233 write(STDOUT_FILENO, clear, strlen(clear));
234 printinfo();
235 if (fb_init())
236 return 1;
237 if (FBM_BPP(fb_mode()) != sizeof(fbval_t))
238 fprintf(stderr, "fbpdf: fbval_t doesn't match fb depth\n");
239 else
240 mainloop();
241 fb_free();
242 write(STDIN_FILENO, show, strlen(show));
243 printf("\n");
244 doc_close(doc);
245 return 0;