Workaround the fact that some PDFs have negative pagenumbers in outlines
[llpp.git] / main.ml
blob9ffce99f632b2b6b8b6d58210f2fdbaf6c49d30a
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 type params = angle * proportional * texcount * sliceheight
12 and pageno = int
13 and width = int
14 and height = int
15 and leftx = int
16 and opaque = string
17 and recttype = int
18 and pixmapsize = int
19 and angle = int
20 and proportional = bool
21 and presentation = bool
22 and interpagespace = int
23 and texcount = int
24 and sliceheight = int
25 and zoom = float
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
37 type mpos = int * int
38 and mstate =
39 | Msel of (mpos * mpos)
40 | Mpan of mpos
41 | Mscroll
42 | Mnone
45 type 'a circbuf =
46 { store : 'a array
47 ; mutable rc : int
48 ; mutable wc : int
49 ; mutable len : int
53 type textentry = (char * string * onhist * onkey * ondone)
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel) option
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollincr : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscroll : bool
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : zoom
147 ; mutable presentation : presentation
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
159 type outline = string * int * int * float;;
160 type outlines =
161 | Oarray of outline array
162 | Olist of outline list
163 | Onarrow of string * outline array * outline array
166 type rect = (float * float * float * float * float * float * float * float);;
168 type state =
169 { mutable csock : Unix.file_descr
170 ; mutable ssock : Unix.file_descr
171 ; mutable w : int
172 ; mutable x : int
173 ; mutable y : int
174 ; mutable ty : float
175 ; mutable maxy : int
176 ; mutable layout : layout list
177 ; pagemap :
178 ((pageno * width * angle * proportional), (opaque * pixmapsize)) Hashtbl.t
179 ; mutable pdims : (pageno * width * height * leftx) list
180 ; mutable pagecount : int
181 ; pagecache : string circbuf
182 ; mutable rendering : bool
183 ; mutable mstate : mstate
184 ; mutable searchpattern : string
185 ; mutable rects : (pageno * recttype * rect) list
186 ; mutable rects1 : (pageno * recttype * rect) list
187 ; mutable text : string
188 ; mutable fullscreen : (width * height) option
189 ; mutable birdseye : (zoom * leftx * presentation * interpagespace) option
190 ; mutable textentry : textentry option
191 ; mutable outlines : outlines
192 ; mutable outline : (bool * int * int * outline array * string) option
193 ; mutable bookmarks : outline list
194 ; mutable path : string
195 ; mutable password : string
196 ; mutable invalidated : int
197 ; mutable colorscale : float
198 ; mutable memused : int
199 ; mutable birdseyepageno : pageno
200 ; hists : hists
202 and hists =
203 { pat : string circbuf
204 ; pag : string circbuf
205 ; nav : float circbuf
209 let defconf =
210 { scrollw = 7
211 ; scrollh = 12
212 ; icase = true
213 ; preload = true
214 ; pagebias = 0
215 ; verbose = false
216 ; scrollincr = 24
217 ; maxhfit = true
218 ; crophack = false
219 ; autoscroll = false
220 ; showall = false
221 ; hlinks = false
222 ; underinfo = false
223 ; interpagespace = 2
224 ; zoom = 1.0
225 ; presentation = false
226 ; angle = 0
227 ; winw = 900
228 ; winh = 900
229 ; savebmarks = true
230 ; proportional = true
231 ; memlimit = 32*1024*1024
232 ; texcount = 256
233 ; sliceheight = 24
237 let conf = { defconf with angle = defconf.angle };;
239 let state =
240 { csock = Unix.stdin
241 ; ssock = Unix.stdin
242 ; w = 0
243 ; y = 0
244 ; x = 0
245 ; ty = 0.0
246 ; layout = []
247 ; maxy = max_int
248 ; pagemap = Hashtbl.create 10
249 ; pagecache = cbnew 100 ""
250 ; pdims = []
251 ; pagecount = 0
252 ; rendering = false
253 ; mstate = Mnone
254 ; rects = []
255 ; rects1 = []
256 ; text = ""
257 ; fullscreen = None
258 ; birdseye = None
259 ; textentry = None
260 ; searchpattern = ""
261 ; outlines = Olist []
262 ; outline = None
263 ; bookmarks = []
264 ; path = ""
265 ; password = ""
266 ; invalidated = 0
267 ; hists =
268 { nav = cbnew 100 0.0
269 ; pat = cbnew 20 ""
270 ; pag = cbnew 10 ""
272 ; colorscale = 1.0
273 ; memused = 0
274 ; birdseyepageno = 0
278 let vlog fmt =
279 if conf.verbose
280 then
281 Printf.kprintf prerr_endline fmt
282 else
283 Printf.kprintf ignore fmt
286 let writecmd fd s =
287 let len = String.length s in
288 let n = 4 + len in
289 let b = Buffer.create n in
290 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
291 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
292 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
293 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
294 Buffer.add_string b s;
295 let s' = Buffer.contents b in
296 let n' = Unix.write fd s' 0 n in
297 if n' != n then failwith "write failed";
300 let readcmd fd =
301 let s = "xxxx" in
302 let n = Unix.read fd s 0 4 in
303 if n != 4 then failwith "incomplete read(len)";
304 let len = 0
305 lor (Char.code s.[0] lsl 24)
306 lor (Char.code s.[1] lsl 16)
307 lor (Char.code s.[2] lsl 8)
308 lor (Char.code s.[3] lsl 0)
310 let s = String.create len in
311 let n = Unix.read fd s 0 len in
312 if n != len then failwith "incomplete read(data)";
316 let yratio y =
317 if y = state.maxy
318 then 1.0
319 else float y /. float state.maxy
322 let makecmd s l =
323 let b = Buffer.create 10 in
324 Buffer.add_string b s;
325 let rec combine = function
326 | [] -> b
327 | x :: xs ->
328 Buffer.add_char b ' ';
329 let s =
330 match x with
331 | `b b -> if b then "1" else "0"
332 | `s s -> s
333 | `i i -> string_of_int i
334 | `f f -> string_of_float f
335 | `I f -> string_of_int (truncate f)
337 Buffer.add_string b s;
338 combine xs;
340 combine l;
343 let wcmd s l =
344 let cmd = Buffer.contents (makecmd s l) in
345 writecmd state.csock cmd;
348 let calcips h =
349 if conf.presentation
350 then
351 let d = conf.winh - h in
352 max 0 ((d + 1) / 2)
353 else
354 conf.interpagespace
357 let calcheight () =
358 let rec f pn ph pi fh l =
359 match l with
360 | (n, _, h, _) :: rest ->
361 let ips = calcips h in
362 let fh =
363 if conf.presentation
364 then fh+ips
365 else (
366 if state.birdseye <> None && pn = 0
367 then fh + ips
368 else fh
371 let fh = fh + ((n - pn) * (ph + pi)) in
372 f n h ips fh rest
374 | [] ->
375 let inc =
376 if conf.presentation || (state.birdseye <> None && pn = 0)
377 then 0
378 else -pi
380 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
381 max 0 fh
383 let fh = f 0 0 0 0 state.pdims in
387 let getpageyh pageno =
388 let rec f pn ph pi y l =
389 match l with
390 | (n, _, h, _) :: rest ->
391 let ips = calcips h in
392 if n >= pageno
393 then
394 if conf.presentation && n = pageno
395 then
396 y + (pageno - pn) * (ph + pi) + pi, h
397 else
398 y + (pageno - pn) * (ph + pi), h
399 else
400 let y = y + (if conf.presentation then pi else 0) in
401 let y = y + (n - pn) * (ph + pi) in
402 f n h ips y rest
404 | [] ->
405 y + (pageno - pn) * (ph + pi), ph
407 f 0 0 0 0 state.pdims
410 let getpagey pageno = fst (getpageyh pageno);;
412 let layout y sh =
413 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
414 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
415 match pdims with
416 | (pageno', w, h, x) :: rest when pageno' = pageno ->
417 let ips = calcips h in
418 let yinc =
419 if conf.presentation || (state.birdseye <> None && pageno = 0)
420 then ips
421 else 0
423 (w, h, ips, x), rest, pdimno + 1, yinc
424 | _ ->
425 prev, pdims, pdimno, 0
427 let dy = dy + yinc in
428 let py = py + yinc in
429 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
430 then
431 accu
432 else
433 let vy = y + dy in
434 if py + h <= vy - yinc
435 then
436 let py = py + h + ips in
437 let dy = max 0 (py - y) in
438 f ~pageno:(pageno+1)
439 ~pdimno
440 ~prev:curr
443 ~pdims:rest
444 ~cacheleft
445 ~accu
446 else
447 let pagey = vy - py in
448 let pagevh = h - pagey in
449 let pagevh = min (sh - dy) pagevh in
450 let off = if yinc > 0 then py - vy else 0 in
451 let py = py + h + ips in
452 let e =
453 { pageno = pageno
454 ; pagedimno = pdimno
455 ; pagew = w
456 ; pageh = h
457 ; pagedispy = dy + off
458 ; pagey = pagey + off
459 ; pagevh = pagevh - off
460 ; pagex = x
463 let accu = e :: accu in
464 f ~pageno:(pageno+1)
465 ~pdimno
466 ~prev:curr
468 ~dy:(dy+pagevh+ips)
469 ~pdims:rest
470 ~cacheleft:(cacheleft-1)
471 ~accu
473 if state.invalidated = 0
474 then (
475 let accu =
477 ~pageno:0
478 ~pdimno:~-1
479 ~prev:(0,0,0,0)
480 ~py:0
481 ~dy:0
482 ~pdims:state.pdims
483 ~cacheleft:(cbcap state.pagecache)
484 ~accu:[]
486 List.rev accu
488 else
492 let clamp incr =
493 let y = state.y + incr in
494 let y = max 0 y in
495 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
499 let getopaque pageno =
500 try Some (Hashtbl.find state.pagemap
501 (pageno, state.w, conf.angle, conf.proportional))
502 with Not_found -> None
505 let cache pageno opaque =
506 Hashtbl.replace state.pagemap
507 (pageno, state.w, conf.angle, conf.proportional) opaque
510 let validopaque opaque = String.length opaque > 0;;
512 let render l =
513 match getopaque l.pageno with
514 | None when not state.rendering ->
515 state.rendering <- true;
516 cache l.pageno ("", -1);
517 wcmd "render" [`i (l.pageno + 1)
518 ;`i l.pagedimno
519 ;`i l.pagew
520 ;`i l.pageh];
522 | _ -> ()
525 let loadlayout layout =
526 let rec f all = function
527 | l :: ls ->
528 begin match getopaque l.pageno with
529 | None -> render l; f false ls
530 | Some (opaque, _) -> f (all && validopaque opaque) ls
532 | [] -> all
534 f (layout <> []) layout;
537 let findpageforopaque opaque =
538 Hashtbl.fold
539 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
540 state.pagemap None
543 let pagevisible n = List.exists (fun l -> l.pageno = n) state.layout;;
545 let preload () =
546 if conf.preload
547 then
548 let oktopreload =
549 let opaque = cbpeek state.pagecache in
550 match findpageforopaque opaque with
551 | Some ((n, _, _, _), size) ->
552 not (pagevisible n) && state.memused - size <= conf.memlimit
553 | None -> false
555 if oktopreload
556 then
557 let rely = yratio state.y in
558 let presentation = conf.presentation in
559 let interpagespace = conf.interpagespace in
560 let maxy = state.maxy in
561 conf.presentation <- false;
562 conf.interpagespace <- 0;
563 state.maxy <- calcheight ();
564 let y = truncate (float state.maxy *. rely) in
565 let y = if y < conf.winh then 0 else y - conf.winh in
566 let pages = layout y (conf.winh*3) in
567 List.iter render pages;
568 conf.presentation <- presentation;
569 conf.interpagespace <- interpagespace;
570 state.maxy <- maxy;
573 let gotoy y =
574 let y = max 0 y in
575 let y = min state.maxy y in
576 let pages = layout y conf.winh in
577 let ready = loadlayout pages in
578 state.ty <- yratio y;
579 if conf.showall
580 then (
581 if ready
582 then (
583 state.layout <- pages;
584 state.y <- y;
585 Glut.postRedisplay ();
588 else (
589 state.layout <- pages;
590 state.y <- y;
591 Glut.postRedisplay ();
593 if state.birdseye <> None
594 then (
595 if not (pagevisible state.birdseyepageno)
596 then
597 match state.layout with
598 | [] -> ()
599 | l :: _ -> state.birdseyepageno <- l.pageno
601 preload ();
604 let gotoy_and_clear_text y =
605 gotoy y;
606 if not conf.verbose then state.text <- "";
609 let addnav () =
610 cbput state.hists.nav (yratio state.y);
613 let getnav () =
614 let y = cbgetc state.hists.nav ~-1 in
615 truncate (y *. float state.maxy)
618 let gotopage n top =
619 let y, h = getpageyh n in
620 addnav ();
621 gotoy_and_clear_text (y + (truncate (top *. float h)));
624 let gotopage1 n top =
625 let y = getpagey n in
626 addnav ();
627 gotoy_and_clear_text (y + top);
630 let invalidate () =
631 state.layout <- [];
632 state.pdims <- [];
633 state.rects <- [];
634 state.rects1 <- [];
635 state.invalidated <- state.invalidated + 1;
638 let scalecolor c =
639 let c = c *. state.colorscale in
640 (c, c, c);
643 let represent () =
644 let y =
645 match state.layout with
646 | [] ->
647 let rely = yratio state.y in
648 state.maxy <- calcheight ();
649 truncate (float state.maxy *. rely)
651 | l :: _ ->
652 state.maxy <- calcheight ();
653 getpagey l.pageno
655 gotoy y
658 let pagematrix () =
659 GlMat.mode `projection;
660 GlMat.load_identity ();
661 GlMat.rotate ~x:1.0 ~angle:180.0 ();
662 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
663 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
666 let winmatrix () =
667 GlMat.mode `projection;
668 GlMat.load_identity ();
669 GlMat.rotate ~x:1.0 ~angle:180.0 ();
670 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
671 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
674 let reshape ~w ~h =
675 conf.winw <- w;
676 let w = truncate (float w *. conf.zoom) - conf.scrollw in
677 let w = max w 2 in
678 state.w <- w;
679 conf.winh <- h;
680 GlMat.mode `modelview;
681 GlMat.load_identity ();
682 GlClear.color (scalecolor 1.0);
683 GlClear.clear [`color];
685 invalidate ();
686 wcmd "geometry" [`i w; `i h];
689 let showtext c s =
690 GlDraw.color (0.0, 0.0, 0.0);
691 GlDraw.rect
692 (0.0, float (conf.winh - 18))
693 (float (conf.winw - conf.scrollw - 1), float conf.winh)
695 let font = Glut.BITMAP_8_BY_13 in
696 GlDraw.color (1.0, 1.0, 1.0);
697 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
698 Glut.bitmapCharacter ~font ~c:(Char.code c);
699 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
702 let enttext () =
703 let len = String.length state.text in
704 match state.textentry with
705 | None ->
706 if len > 0 then showtext ' ' state.text
708 | Some (c, text, _, _, _) ->
709 let s =
710 if len > 0
711 then
712 text ^ " [" ^ state.text ^ "]"
713 else
714 text
716 showtext c s;
719 let showtext c s =
720 if true
721 then (
722 state.text <- Printf.sprintf "%c%s" c s;
723 Glut.postRedisplay ();
725 else (
726 showtext c s;
727 Glut.swapBuffers ();
731 let act cmd =
732 match cmd.[0] with
733 | 'c' ->
734 state.pdims <- [];
736 | 'D' ->
737 state.rects <- state.rects1;
738 Glut.postRedisplay ()
740 | 'C' ->
741 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
742 state.pagecount <- n;
743 state.invalidated <- state.invalidated - 1;
744 if state.invalidated = 0
745 then represent ()
747 | 't' ->
748 let s = Scanf.sscanf cmd "t %n"
749 (fun n -> String.sub cmd n (String.length cmd - n))
751 Glut.setWindowTitle s
753 | 'T' ->
754 let s = Scanf.sscanf cmd "T %n"
755 (fun n -> String.sub cmd n (String.length cmd - n))
757 if state.textentry = None
758 then (
759 state.text <- s;
760 showtext ' ' s;
762 else (
763 state.text <- s;
764 Glut.postRedisplay ();
767 | 'V' ->
768 if conf.verbose
769 then
770 let s = Scanf.sscanf cmd "V %n"
771 (fun n -> String.sub cmd n (String.length cmd - n))
773 state.text <- s;
774 showtext ' ' s;
776 | 'F' ->
777 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
778 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
779 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
780 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
782 let y = (getpagey pageno) + truncate y0 in
783 addnav ();
784 gotoy y;
785 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
787 | 'R' ->
788 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
789 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
790 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
791 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
793 state.rects1 <-
794 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
796 | 'r' ->
797 let n, w, h, r, l, s, p =
798 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
799 (fun n w h r l s p ->
800 (n-1, w, h, r, l != 0, s, p))
803 Hashtbl.replace state.pagemap (n, w, r, l) (p, s);
804 state.memused <- state.memused + s;
806 let rec gc () =
807 if (state.memused <= conf.memlimit) || cbempty state.pagecache
808 then ()
809 else (
810 let evictedopaque = cbpeek state.pagecache in
811 match findpageforopaque evictedopaque with
812 | None -> failwith "bug in gc"
813 | Some ((evictedn, _, _, _) as k, evictedsize) ->
814 if not (pagevisible evictedn)
815 then (
816 wcmd "free" [`s evictedopaque];
817 state.memused <- state.memused - evictedsize;
818 Hashtbl.remove state.pagemap k;
819 cbdecr state.pagecache;
820 gc ();
824 gc ();
826 cbput state.pagecache p;
827 state.rendering <- false;
829 if conf.showall
830 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
831 else (
832 if pagevisible n
833 then gotoy state.y
834 else (ignore (loadlayout state.layout); preload ())
837 | 'l' ->
838 let (n, w, h, x) as pdim =
839 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
841 state.pdims <- pdim :: state.pdims
843 | 'o' ->
844 let (l, n, t, h, pos) =
845 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
847 let s = String.sub cmd pos (String.length cmd - pos) in
848 let s =
849 let l = String.length s in
850 let b = Buffer.create (String.length s) in
851 let rec loop pc2 i =
852 if i = l
853 then ()
854 else
855 let pc2 =
856 match s.[i] with
857 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
858 | '\xc2' -> true
859 | c ->
860 let c = if Char.code c land 0x80 = 0 then c else '?' in
861 Buffer.add_char b c;
862 false
864 loop pc2 (i+1)
866 loop false 0;
867 Buffer.contents b
869 let outline = (s, l, n, float t /. float h) in
870 let outlines =
871 match state.outlines with
872 | Olist outlines -> Olist (outline :: outlines)
873 | Oarray _ -> Olist [outline]
874 | Onarrow _ -> Olist [outline]
876 state.outlines <- outlines
878 | _ ->
879 log "unknown cmd `%S'" cmd
882 let now = Unix.gettimeofday;;
884 let idle () =
885 let rec loop delay =
886 let r, _, _ = Unix.select [state.csock] [] [] delay in
887 begin match r with
888 | [] ->
889 if conf.autoscroll
890 then begin
891 let y = state.y + conf.scrollincr in
892 let y = if y >= state.maxy then 0 else y in
893 gotoy y;
894 state.text <- "";
895 end;
897 | _ ->
898 let cmd = readcmd state.csock in
899 act cmd;
900 loop 0.0
901 end;
902 in loop 0.001
905 let onhist cb =
906 let rc = cb.rc in
907 let action = function
908 | HCprev -> cbget cb ~-1
909 | HCnext -> cbget cb 1
910 | HCfirst -> cbget cb ~-(cb.rc)
911 | HClast -> cbget cb (cb.len - 1 - cb.rc)
912 and cancel () = cb.rc <- rc
913 in (action, cancel)
916 let search pattern forward =
917 if String.length pattern > 0
918 then
919 let pn, py =
920 match state.layout with
921 | [] -> 0, 0
922 | l :: _ ->
923 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
925 let cmd =
926 let b = makecmd "search"
927 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
929 Buffer.add_char b ',';
930 Buffer.add_string b pattern;
931 Buffer.add_char b '\000';
932 Buffer.contents b;
934 writecmd state.csock cmd;
937 let intentry text key =
938 let c = Char.unsafe_chr key in
939 match c with
940 | '0' .. '9' ->
941 let s = "x" in s.[0] <- c;
942 let text = text ^ s in
943 TEcont text
945 | _ ->
946 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
947 TEcont text
950 let addchar s c =
951 let b = Buffer.create (String.length s + 1) in
952 Buffer.add_string b s;
953 Buffer.add_char b c;
954 Buffer.contents b;
957 let textentry text key =
958 let c = Char.unsafe_chr key in
959 match c with
960 | _ when key >= 32 && key < 127 ->
961 let text = addchar text c in
962 TEcont text
964 | _ ->
965 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
966 TEcont text
969 let reinit angle proportional =
970 conf.angle <- angle;
971 conf.proportional <- proportional;
972 invalidate ();
973 wcmd "reinit" [`i angle; `b proportional];
976 let optentry text key =
977 let btos b = if b then "on" else "off" in
978 let c = Char.unsafe_chr key in
979 match c with
980 | 's' ->
981 let ondone s =
982 try conf.scrollincr <- int_of_string s with exc ->
983 state.text <- Printf.sprintf "bad integer `%s': %s"
984 s (Printexc.to_string exc)
986 TEswitch ('#', "", None, intentry, ondone)
988 | 'R' ->
989 let ondone s =
990 match try
991 Some (int_of_string s)
992 with exc ->
993 state.text <- Printf.sprintf "bad integer `%s': %s"
994 s (Printexc.to_string exc);
995 None
996 with
997 | Some angle -> reinit angle conf.proportional
998 | None -> ()
1000 TEswitch ('^', "", None, intentry, ondone)
1002 | 'i' ->
1003 conf.icase <- not conf.icase;
1004 TEdone ("case insensitive search " ^ (btos conf.icase))
1006 | 'p' ->
1007 conf.preload <- not conf.preload;
1008 gotoy state.y;
1009 TEdone ("preload " ^ (btos conf.preload))
1011 | 'v' ->
1012 conf.verbose <- not conf.verbose;
1013 TEdone ("verbose " ^ (btos conf.verbose))
1015 | 'h' ->
1016 conf.maxhfit <- not conf.maxhfit;
1017 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1018 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1020 | 'c' ->
1021 conf.crophack <- not conf.crophack;
1022 TEdone ("crophack " ^ btos conf.crophack)
1024 | 'a' ->
1025 conf.showall <- not conf.showall;
1026 TEdone ("showall " ^ btos conf.showall)
1028 | 'f' ->
1029 conf.underinfo <- not conf.underinfo;
1030 TEdone ("underinfo " ^ btos conf.underinfo)
1032 | 'P' ->
1033 conf.savebmarks <- not conf.savebmarks;
1034 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1036 | 'S' ->
1037 let ondone s =
1039 conf.interpagespace <- int_of_string s;
1040 let rely = yratio state.y in
1041 state.maxy <- calcheight ();
1042 gotoy (truncate (float state.maxy *. rely));
1043 with exc ->
1044 state.text <- Printf.sprintf "bad integer `%s': %s"
1045 s (Printexc.to_string exc)
1047 TEswitch ('%', "", None, intentry, ondone)
1049 | 'l' ->
1050 reinit conf.angle (not conf.proportional);
1051 TEdone ("proprortional display " ^ btos conf.proportional)
1053 | _ ->
1054 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1055 TEstop
1058 let maxoutlinerows () = (conf.winh - 31) / 16;;
1060 let enterselector allowdel outlines errmsg msg =
1061 if Array.length outlines = 0
1062 then (
1063 showtext ' ' errmsg;
1065 else (
1066 state.text <- msg;
1067 Glut.setCursor Glut.CURSOR_INHERIT;
1068 let pageno =
1069 match state.layout with
1070 | [] -> -1
1071 | {pageno=pageno} :: rest -> pageno
1073 let active =
1074 let rec loop n =
1075 if n = Array.length outlines
1076 then 0
1077 else
1078 let (_, _, outlinepageno, _) = outlines.(n) in
1079 if outlinepageno >= pageno then n else loop (n+1)
1081 loop 0
1083 state.outline <-
1084 Some (allowdel, active,
1085 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1086 Glut.postRedisplay ();
1090 let enteroutlinemode () =
1091 let outlines, msg =
1092 match state.outlines with
1093 | Oarray a -> a, ""
1094 | Olist l ->
1095 let a = Array.of_list (List.rev l) in
1096 state.outlines <- Oarray a;
1097 a, ""
1098 | Onarrow (pat, a, b) ->
1099 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1101 enterselector false outlines "Document has no outline" msg;
1104 let enterbookmarkmode () =
1105 let bookmarks = Array.of_list state.bookmarks in
1106 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1109 let quickbookmark ?title () =
1110 match state.layout with
1111 | [] -> ()
1112 | l :: _ ->
1113 let title =
1114 match title with
1115 | None ->
1116 let sec = Unix.gettimeofday () in
1117 let tm = Unix.localtime sec in
1118 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1119 (l.pageno+1)
1120 tm.Unix.tm_mday
1121 tm.Unix.tm_mon
1122 (tm.Unix.tm_year + 1900)
1123 tm.Unix.tm_hour
1124 tm.Unix.tm_min
1125 | Some title -> title
1127 state.bookmarks <-
1128 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1131 let doreshape w h =
1132 state.fullscreen <- None;
1133 Glut.reshapeWindow w h;
1136 let writeopen path password =
1137 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1140 let opendoc path password =
1141 invalidate ();
1142 state.path <- path;
1143 state.password <- password;
1144 Hashtbl.clear state.pagemap;
1146 writeopen path password;
1147 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1148 wcmd "geometry" [`i state.w; `i conf.winh];
1151 let birdseyeoff (zoom, x, presentation, interpagespace) =
1152 state.birdseye <- None;
1153 conf.zoom <- zoom;
1154 conf.presentation <- presentation;
1155 conf.interpagespace <- interpagespace;
1156 state.x <- x;
1157 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1158 (100.0*.zoom);
1161 let viewkeyboard ~key ~x ~y =
1162 let enttext te =
1163 state.textentry <- te;
1164 state.text <- "";
1165 enttext ();
1166 Glut.postRedisplay ()
1168 match state.textentry with
1169 | None ->
1170 let c = Char.chr key in
1171 begin match c with
1172 | '\027' | 'q' ->
1173 exit 0
1175 | '\008' ->
1176 let y = getnav () in
1177 gotoy_and_clear_text y
1179 | '\013' ->
1180 begin match state.birdseye with
1181 | None -> ()
1182 | Some vals ->
1183 let y = getpagey state.birdseyepageno in
1184 state.y <- y;
1185 birdseyeoff vals;
1186 reshape conf.winw conf.winh;
1187 end;
1189 | 'o' ->
1190 enteroutlinemode ()
1192 | 'u' ->
1193 state.rects <- [];
1194 state.text <- "";
1195 Glut.postRedisplay ()
1197 | '/' | '?' ->
1198 let ondone isforw s =
1199 cbput state.hists.pat s;
1200 state.searchpattern <- s;
1201 search s isforw
1203 enttext (Some (c, "", Some (onhist state.hists.pat),
1204 textentry, ondone (c ='/')))
1206 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1207 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1208 conf.zoom <- min 2.2 (conf.zoom +. incr);
1209 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1210 reshape conf.winw conf.winh
1212 | '+' ->
1213 let ondone s =
1214 let n =
1215 try int_of_string s with exc ->
1216 state.text <- Printf.sprintf "bad integer `%s': %s"
1217 s (Printexc.to_string exc);
1218 max_int
1220 if n != max_int
1221 then (
1222 conf.pagebias <- n;
1223 state.text <- "page bias is now " ^ string_of_int n;
1226 enttext (Some ('+', "", None, intentry, ondone))
1228 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1229 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1230 conf.zoom <- max 0.01 (conf.zoom -. decr);
1231 if conf.zoom <= 1.0 then state.x <- 0;
1232 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1233 reshape conf.winw conf.winh;
1235 | '-' ->
1236 let ondone msg =
1237 state.text <- msg;
1239 enttext (Some ('-', "", None, optentry, ondone))
1241 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1242 state.x <- 0;
1243 conf.zoom <- 1.0;
1244 state.text <- "zoom is 100%";
1245 reshape conf.winw conf.winh
1247 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1248 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1249 if zoom < 1.0
1250 then (
1251 conf.zoom <- zoom;
1252 state.x <- 0;
1253 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1254 reshape conf.winw conf.winh;
1257 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1258 begin match state.birdseye with
1259 | None ->
1260 let zoom = 50.0 /. float state.w in
1261 state.birdseye <- Some (
1262 conf.zoom,
1263 state.x,
1264 conf.presentation,
1265 conf.interpagespace;
1267 conf.zoom <- zoom;
1268 conf.presentation <- false;
1269 conf.interpagespace <- 10;
1270 state.x <- 0;
1271 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1272 (100.0*.zoom)
1274 | Some vals ->
1275 birdseyeoff vals;
1276 end;
1277 reshape conf.winw conf.winh
1279 | '0' .. '9' ->
1280 let ondone s =
1281 let n =
1282 try int_of_string s with exc ->
1283 state.text <- Printf.sprintf "bad integer `%s': %s"
1284 s (Printexc.to_string exc);
1287 if n >= 0
1288 then (
1289 addnav ();
1290 cbput state.hists.pag (string_of_int n);
1291 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1294 let pageentry text key =
1295 match Char.unsafe_chr key with
1296 | 'g' -> TEdone text
1297 | _ -> intentry text key
1299 let text = "x" in text.[0] <- c;
1300 enttext (Some (':', text, Some (onhist state.hists.pag),
1301 pageentry, ondone))
1303 | 'b' ->
1304 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1305 reshape conf.winw conf.winh;
1307 | 'l' ->
1308 conf.hlinks <- not conf.hlinks;
1309 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1310 Glut.postRedisplay ()
1312 | 'a' ->
1313 conf.autoscroll <- not conf.autoscroll
1315 | 'P' ->
1316 conf.presentation <- not conf.presentation;
1317 showtext ' ' ("presentation mode " ^
1318 if conf.presentation then "on" else "off");
1319 represent ()
1321 | 'f' ->
1322 begin match state.fullscreen with
1323 | None ->
1324 state.fullscreen <- Some (conf.winw, conf.winh);
1325 Glut.fullScreen ()
1326 | Some (w, h) ->
1327 state.fullscreen <- None;
1328 doreshape w h
1331 | 'g' ->
1332 gotoy_and_clear_text 0
1334 | 'n' ->
1335 search state.searchpattern true
1337 | 'p' | 'N' ->
1338 search state.searchpattern false
1340 | 't' ->
1341 begin match state.layout with
1342 | [] -> ()
1343 | l :: _ ->
1344 gotoy_and_clear_text (getpagey l.pageno)
1347 | ' ' ->
1348 begin match List.rev state.layout with
1349 | [] -> ()
1350 | l :: _ ->
1351 let pageno = min (l.pageno+1) (state.pagecount-1) in
1352 gotoy_and_clear_text (getpagey pageno)
1355 | '\127' ->
1356 begin match state.layout with
1357 | [] -> ()
1358 | l :: _ ->
1359 let pageno = max 0 (l.pageno-1) in
1360 gotoy_and_clear_text (getpagey pageno)
1363 | '=' ->
1364 let f (fn, ln) l =
1365 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1367 let fn, ln = List.fold_left f (-1, -1) state.layout in
1368 let s =
1369 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1370 let percent =
1371 if maxy <= 0
1372 then 100.
1373 else (100. *. (float state.y /. float maxy)) in
1374 if fn = ln
1375 then
1376 Printf.sprintf "Page %d of %d %.2f%%"
1377 (fn+1) state.pagecount percent
1378 else
1379 Printf.sprintf
1380 "Pages %d-%d of %d %.2f%%"
1381 (fn+1) (ln+1) state.pagecount percent
1383 showtext ' ' s;
1385 | 'w' ->
1386 begin match state.layout with
1387 | [] -> ()
1388 | l :: _ ->
1389 doreshape (l.pagew + conf.scrollw) l.pageh;
1390 Glut.postRedisplay ();
1393 | '\'' ->
1394 enterbookmarkmode ()
1396 | 'm' ->
1397 let ondone s =
1398 match state.layout with
1399 | l :: _ ->
1400 state.bookmarks <-
1401 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1402 :: state.bookmarks
1403 | _ -> ()
1405 enttext (Some ('~', "", None, textentry, ondone))
1407 | '~' ->
1408 quickbookmark ();
1409 showtext ' ' "Quick bookmark added";
1411 | 'z' ->
1412 begin match state.layout with
1413 | l :: _ ->
1414 let rect = getpdimrect l.pagedimno in
1415 let w, h =
1416 if conf.crophack
1417 then
1418 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1419 truncate (1.2 *. (rect.(3) -. rect.(0))))
1420 else
1421 (truncate (rect.(1) -. rect.(0)),
1422 truncate (rect.(3) -. rect.(0)))
1424 if w != 0 && h != 0
1425 then
1426 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1428 Glut.postRedisplay ();
1430 | [] -> ()
1433 | '<' | '>' ->
1434 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1436 | '[' | ']' ->
1437 state.colorscale <-
1438 max 0.0
1439 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1440 Glut.postRedisplay ()
1442 | 'k' -> gotoy (clamp (-conf.scrollincr))
1443 | 'j' -> gotoy (clamp conf.scrollincr)
1445 | 'r' -> opendoc state.path state.password
1447 | _ ->
1448 vlog "huh? %d %c" key (Char.chr key);
1451 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1452 let len = String.length text in
1453 if len = 0
1454 then (
1455 state.textentry <- None;
1456 Glut.postRedisplay ();
1458 else (
1459 let s = String.sub text 0 (len - 1) in
1460 enttext (Some (c, s, opthist, onkey, ondone))
1463 | Some (c, text, onhist, onkey, ondone) ->
1464 begin match Char.unsafe_chr key with
1465 | '\r' | '\n' ->
1466 ondone text;
1467 state.textentry <- None;
1468 Glut.postRedisplay ()
1470 | '\027' ->
1471 begin match onhist with
1472 | None -> ()
1473 | Some (_, onhistcancel) -> onhistcancel ()
1474 end;
1475 state.textentry <- None;
1476 Glut.postRedisplay ()
1478 | _ ->
1479 begin match onkey text key with
1480 | TEdone text ->
1481 state.textentry <- None;
1482 ondone text;
1483 Glut.postRedisplay ()
1485 | TEcont text ->
1486 enttext (Some (c, text, onhist, onkey, ondone));
1488 | TEstop ->
1489 state.textentry <- None;
1490 Glut.postRedisplay ()
1492 | TEswitch te ->
1493 state.textentry <- Some te;
1494 Glut.postRedisplay ()
1495 end;
1496 end;
1499 let narrow outlines pattern =
1500 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1501 match reopt with
1502 | None -> None
1503 | Some re ->
1504 let rec fold accu n =
1505 if n = -1
1506 then accu
1507 else
1508 let (s, _, _, _) as o = outlines.(n) in
1509 let accu =
1510 if (try ignore (Str.search_forward re s 0); true
1511 with Not_found -> false)
1512 then (o :: accu)
1513 else accu
1515 fold accu (n-1)
1517 let matched = fold [] (Array.length outlines - 1) in
1518 if matched = [] then None else Some (Array.of_list matched)
1521 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1522 let search active pattern incr =
1523 let dosearch re =
1524 let rec loop n =
1525 if n = Array.length outlines || n = -1
1526 then None
1527 else
1528 let (s, _, _, _) = outlines.(n) in
1530 (try ignore (Str.search_forward re s 0); true
1531 with Not_found -> false)
1532 then Some n
1533 else loop (n + incr)
1535 loop active
1538 let re = Str.regexp_case_fold pattern in
1539 dosearch re
1540 with Failure s ->
1541 state.text <- s;
1542 None
1544 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1545 match key with
1546 | 27 ->
1547 if String.length qsearch = 0
1548 then (
1549 state.text <- "";
1550 state.outline <- None;
1551 Glut.postRedisplay ();
1553 else (
1554 state.text <- "";
1555 state.outline <- Some (allowdel, active, first, outlines, "");
1556 Glut.postRedisplay ();
1559 | 18 | 19 ->
1560 let incr = if key = 18 then -1 else 1 in
1561 let active, first =
1562 match search (active + incr) qsearch incr with
1563 | None ->
1564 state.text <- qsearch ^ " [not found]";
1565 active, first
1566 | Some active ->
1567 state.text <- qsearch;
1568 active, firstof active
1570 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1571 Glut.postRedisplay ();
1573 | 8 ->
1574 let len = String.length qsearch in
1575 if len = 0
1576 then ()
1577 else (
1578 if len = 1
1579 then (
1580 state.text <- "";
1581 state.outline <- Some (allowdel, active, first, outlines, "");
1583 else
1584 let qsearch = String.sub qsearch 0 (len - 1) in
1585 let active, first =
1586 match search active qsearch ~-1 with
1587 | None ->
1588 state.text <- qsearch ^ " [not found]";
1589 active, first
1590 | Some active ->
1591 state.text <- qsearch;
1592 active, firstof active
1594 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1596 Glut.postRedisplay ()
1598 | 13 ->
1599 if active < Array.length outlines
1600 then (
1601 let (_, _, n, t) = outlines.(active) in
1602 gotopage n t;
1604 state.text <- "";
1605 if allowdel then state.bookmarks <- Array.to_list outlines;
1606 state.outline <- None;
1607 Glut.postRedisplay ();
1609 | _ when key >= 32 && key < 127 ->
1610 let pattern = addchar qsearch (Char.chr key) in
1611 let active, first =
1612 match search active pattern 1 with
1613 | None ->
1614 state.text <- pattern ^ " [not found]";
1615 active, first
1616 | Some active ->
1617 state.text <- pattern;
1618 active, firstof active
1620 state.outline <- Some (allowdel, active, first, outlines, pattern);
1621 Glut.postRedisplay ()
1623 | 14 when not allowdel -> (* ctrl-n *)
1624 if String.length qsearch > 0
1625 then (
1626 let optoutlines = narrow outlines qsearch in
1627 begin match optoutlines with
1628 | None -> state.text <- "can't narrow"
1629 | Some outlines ->
1630 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1631 match state.outlines with
1632 | Olist l -> ()
1633 | Oarray a ->
1634 state.outlines <- Onarrow (qsearch, outlines, a)
1635 | Onarrow (pat, a, b) ->
1636 state.outlines <- Onarrow (qsearch, outlines, b)
1637 end;
1639 Glut.postRedisplay ()
1641 | 21 when not allowdel -> (* ctrl-u *)
1642 let outline =
1643 match state.outlines with
1644 | Oarray a -> a
1645 | Olist l ->
1646 let a = Array.of_list (List.rev l) in
1647 state.outlines <- Oarray a;
1649 | Onarrow (pat, a, b) ->
1650 state.outlines <- Oarray b;
1651 state.text <- "";
1654 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1655 Glut.postRedisplay ()
1657 | 12 ->
1658 state.outline <-
1659 Some (allowdel, active, firstof active, outlines, qsearch);
1660 Glut.postRedisplay ()
1662 | 127 when allowdel ->
1663 let len = Array.length outlines - 1 in
1664 if len = 0
1665 then (
1666 state.outline <- None;
1667 state.bookmarks <- [];
1669 else (
1670 let bookmarks = Array.init len
1671 (fun i ->
1672 let i = if i >= active then i + 1 else i in
1673 outlines.(i)
1676 state.outline <-
1677 Some (allowdel,
1678 min active (len-1),
1679 min first (len-1),
1680 bookmarks, qsearch)
1683 Glut.postRedisplay ()
1685 | _ -> log "unknown key %d" key
1688 let keyboard ~key ~x ~y =
1689 if key = 7
1690 then
1691 wcmd "interrupt" []
1692 else
1693 match state.outline with
1694 | None -> viewkeyboard ~key ~x ~y
1695 | Some outline -> outlinekeyboard ~key ~x ~y outline
1698 let special ~key ~x ~y =
1699 match state.outline with
1700 | None when state.birdseye <> None ->
1701 begin match key with
1702 | Glut.KEY_UP ->
1703 let pageno = max 0 (state.birdseyepageno - 1) in
1704 state.birdseyepageno <- pageno;
1705 if not (pagevisible pageno)
1706 then gotopage pageno 0.0
1707 else Glut.postRedisplay ();
1709 | Glut.KEY_DOWN ->
1710 let pageno = min (state.pagecount - 1) (state.birdseyepageno + 1) in
1711 state.birdseyepageno <- pageno;
1712 if not (pagevisible pageno)
1713 then
1714 begin match List.rev state.layout with
1715 | [] -> gotopage pageno 0.0
1716 | l :: _ ->
1717 gotoy (state.y + conf.interpagespace + l.pageh*2 - l.pagevh)
1719 else Glut.postRedisplay ();
1721 | Glut.KEY_PAGE_UP ->
1722 begin match state.layout with
1723 | l :: _ ->
1724 state.birdseyepageno <- max 0 (l.pageno - 1);
1725 gotopage state.birdseyepageno 0.0
1726 | [] -> gotoy (clamp (-conf.winh))
1727 end;
1728 | Glut.KEY_PAGE_DOWN ->
1729 begin match List.rev state.layout with
1730 | l :: _ ->
1731 state.birdseyepageno <- min (state.pagecount - 1) (l.pageno + 1);
1732 gotoy (clamp (l.pagedispy + conf.interpagespace + l.pageh))
1733 | [] -> gotoy (clamp conf.winh)
1734 end;
1736 | Glut.KEY_HOME ->
1737 state.birdseyepageno <- 0;
1738 gotopage 0 0.0
1739 | Glut.KEY_END ->
1740 state.birdseyepageno <- state.pagecount - 1;
1741 if not (pagevisible state.birdseyepageno)
1742 then
1743 gotopage state.birdseyepageno 0.0
1744 else
1745 Glut.postRedisplay ()
1747 | _ -> ()
1750 | None ->
1751 begin match state.textentry with
1752 | None ->
1753 let y =
1754 match key with
1755 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1756 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1757 | Glut.KEY_DOWN -> clamp conf.scrollincr
1758 | Glut.KEY_PAGE_UP ->
1759 if Glut.getModifiers () land Glut.active_ctrl != 0
1760 then
1761 match state.layout with
1762 | [] -> state.y
1763 | l :: _ -> state.y - l.pagey
1764 else
1765 clamp (-conf.winh)
1766 | Glut.KEY_PAGE_DOWN ->
1767 if Glut.getModifiers () land Glut.active_ctrl != 0
1768 then
1769 match List.rev state.layout with
1770 | [] -> state.y
1771 | l :: _ -> getpagey l.pageno
1772 else
1773 clamp conf.winh
1774 | Glut.KEY_HOME -> addnav (); 0
1775 | Glut.KEY_END ->
1776 addnav ();
1777 state.maxy - (if conf.maxhfit then conf.winh else 0)
1779 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1780 state.x <- state.x - 10;
1781 state.y
1782 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1783 state.x <- state.x + 10;
1784 state.y
1786 | _ -> state.y
1788 gotoy_and_clear_text y
1790 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1791 let s =
1792 match key with
1793 | Glut.KEY_UP -> action HCprev
1794 | Glut.KEY_DOWN -> action HCnext
1795 | Glut.KEY_HOME -> action HCfirst
1796 | Glut.KEY_END -> action HClast
1797 | _ -> state.text
1799 state.textentry <- Some (c, s, onhist, onkey, ondone);
1800 Glut.postRedisplay ()
1802 | _ -> ()
1805 | Some (allowdel, active, first, outlines, qsearch) ->
1806 let maxrows = maxoutlinerows () in
1807 let calcfirst first active =
1808 if active > first
1809 then
1810 let rows = active - first in
1811 if rows > maxrows then active - maxrows else first
1812 else active
1814 let navigate incr =
1815 let active = active + incr in
1816 let active = max 0 (min active (Array.length outlines - 1)) in
1817 let first = calcfirst first active in
1818 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1819 Glut.postRedisplay ()
1821 let updownlevel incr =
1822 let len = Array.length outlines in
1823 let (_, curlevel, _, _) = outlines.(active) in
1824 let rec flow i =
1825 if i = len then i-1 else if i = -1 then 0 else
1826 let (_, l, _, _) = outlines.(i) in
1827 if l != curlevel then i else flow (i+incr)
1829 let active = flow active in
1830 let first = calcfirst first active in
1831 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1832 Glut.postRedisplay ()
1834 match key with
1835 | Glut.KEY_UP -> navigate ~-1
1836 | Glut.KEY_DOWN -> navigate 1
1837 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1838 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1840 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1841 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1843 | Glut.KEY_HOME ->
1844 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1845 Glut.postRedisplay ()
1847 | Glut.KEY_END ->
1848 let active = Array.length outlines - 1 in
1849 let first = max 0 (active - maxrows) in
1850 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1851 Glut.postRedisplay ()
1853 | _ -> ()
1856 let drawplaceholder l =
1857 GlDraw.color (scalecolor 1.0);
1858 GlDraw.rect
1859 (float l.pagex, float l.pagedispy)
1860 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1862 let x = float l.pagex
1863 and y = float (l.pagedispy + 13) in
1864 let font = Glut.BITMAP_8_BY_13 in
1865 GlDraw.color (0.0, 0.0, 0.0);
1866 GlPix.raster_pos ~x ~y ();
1867 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1868 ("Loading " ^ string_of_int (l.pageno + 1));
1871 let now () = Unix.gettimeofday ();;
1873 let drawpage l =
1874 begin match getopaque l.pageno with
1875 | Some (opaque, _) when validopaque opaque ->
1876 if state.textentry = None
1877 then GlDraw.color (scalecolor 1.0)
1878 else GlDraw.color (scalecolor 0.4);
1879 let a = now () in
1880 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1881 opaque;
1882 let b = now () in
1883 let d = b-.a in
1884 vlog "draw %d %f sec" l.pageno d;
1886 | _ ->
1887 drawplaceholder l;
1888 end;
1889 if state.birdseye <> None && state.birdseyepageno = l.pageno
1890 then (
1891 GlDraw.polygon_mode `both `line;
1892 GlDraw.line_width 4.0;
1893 GlDraw.color (0.8, 0.0, 0.0);
1894 GlDraw.rect
1895 (float (l.pagex - 1), float (l.pagedispy - 1))
1896 (float (l.pagew + l.pagex + 1), float (l.pagedispy + l.pagevh + 1))
1898 GlDraw.line_width 1.0;
1899 GlDraw.polygon_mode `both `fill;
1903 let scrollph y =
1904 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1905 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1906 let sh = float conf.winh /. sh in
1907 let sh = max sh (float conf.scrollh) in
1909 let percent =
1910 if state.y = state.maxy
1911 then 1.0
1912 else float y /. float maxy
1914 let position = (float conf.winh -. sh) *. percent in
1916 let position =
1917 if position +. sh > float conf.winh
1918 then float conf.winh -. sh
1919 else position
1921 position, sh;
1924 let scrollindicator () =
1925 GlDraw.color (0.64 , 0.64, 0.64);
1926 GlDraw.rect
1927 (float (conf.winw - conf.scrollw), 0.)
1928 (float conf.winw, float conf.winh)
1930 GlDraw.color (0.0, 0.0, 0.0);
1932 let position, sh = scrollph state.y in
1933 GlDraw.rect
1934 (float (conf.winw - conf.scrollw), position)
1935 (float conf.winw, position +. sh)
1939 let showsel margin =
1940 match state.mstate with
1941 | Mnone | Mscroll _ | Mpan _ ->
1944 | Msel ((x0, y0), (x1, y1)) ->
1945 let rec loop = function
1946 | l :: ls ->
1947 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1948 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1949 then
1950 match getopaque l.pageno with
1951 | Some (opaque, _) when validopaque opaque ->
1952 let oy = -l.pagey + l.pagedispy in
1953 seltext opaque
1954 (x0 - margin - state.x, y0,
1955 x1 - margin - state.x, y1) oy;
1957 | _ -> ()
1958 else loop ls
1959 | [] -> ()
1961 loop state.layout
1964 let showrects () =
1965 let panx = float state.x in
1966 Gl.enable `blend;
1967 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1968 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1969 List.iter
1970 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1971 List.iter (fun l ->
1972 if l.pageno = pageno
1973 then (
1974 let d = float (l.pagedispy - l.pagey) in
1975 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1976 GlDraw.begins `quads;
1978 GlDraw.vertex2 (x0+.panx, y0+.d);
1979 GlDraw.vertex2 (x1+.panx, y1+.d);
1980 GlDraw.vertex2 (x2+.panx, y2+.d);
1981 GlDraw.vertex2 (x3+.panx, y3+.d);
1983 GlDraw.ends ();
1985 ) state.layout
1986 ) state.rects
1988 Gl.disable `blend;
1991 let showoutline = function
1992 | None -> ()
1993 | Some (allowdel, active, first, outlines, qsearch) ->
1994 Gl.enable `blend;
1995 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1996 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1997 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
1998 Gl.disable `blend;
2000 GlDraw.color (1., 1., 1.);
2001 let font = Glut.BITMAP_9_BY_15 in
2002 let draw_string x y s =
2003 GlPix.raster_pos ~x ~y ();
2004 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2006 let rec loop row =
2007 if row = Array.length outlines || (row - first) * 16 > conf.winh
2008 then ()
2009 else (
2010 let (s, l, _, _) = outlines.(row) in
2011 let y = (row - first) * 16 in
2012 let x = 5 + 15*l in
2013 if row = active
2014 then (
2015 Gl.enable `blend;
2016 GlDraw.polygon_mode `both `line;
2017 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2018 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2019 GlDraw.rect (0., float (y + 1))
2020 (float (conf.winw - 1), float (y + 18));
2021 GlDraw.polygon_mode `both `fill;
2022 Gl.disable `blend;
2023 GlDraw.color (1., 1., 1.);
2025 draw_string (float x) (float (y + 16)) s;
2026 loop (row+1)
2029 loop first
2032 let display () =
2033 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2034 GlDraw.viewport margin 0 state.w conf.winh;
2035 pagematrix ();
2036 if state.birdseye <> None
2037 then
2038 GlClear.color (0.5, 0.5, 0.55)
2039 else
2040 GlClear.color (scalecolor 0.5)
2042 GlClear.clear [`color];
2043 if state.x != 0
2044 then (
2045 let x = float state.x in
2046 GlMat.translate ~x ();
2048 if conf.zoom > 1.0
2049 then (
2050 Gl.enable `scissor_test;
2051 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2053 List.iter drawpage state.layout;
2054 if conf.zoom > 1.0
2055 then
2056 Gl.disable `scissor_test
2058 if state.x != 0
2059 then (
2060 let x = -.float state.x in
2061 GlMat.translate ~x ();
2063 showrects ();
2064 showsel margin;
2065 GlDraw.viewport 0 0 conf.winw conf.winh;
2066 winmatrix ();
2067 scrollindicator ();
2068 showoutline state.outline;
2069 enttext ();
2070 Glut.swapBuffers ();
2073 let getunder x y =
2074 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2075 let x = x - margin - state.x in
2076 let rec f = function
2077 | l :: rest ->
2078 begin match getopaque l.pageno with
2079 | Some (opaque, _) when validopaque opaque ->
2080 let y = y - l.pagedispy in
2081 if y > 0
2082 then
2083 let y = l.pagey + y in
2084 let x = x - l.pagex in
2085 match whatsunder opaque x y with
2086 | Unone -> f rest
2087 | under -> under
2088 else
2089 f rest
2090 | _ ->
2091 f rest
2093 | [] -> Unone
2095 f state.layout
2098 let mouse ~button ~bstate ~x ~y =
2099 match button with
2100 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2101 let incr =
2102 if n = 3
2103 then
2104 -conf.scrollincr
2105 else
2106 conf.scrollincr
2108 let incr = incr * 2 in
2109 let y = clamp incr in
2110 gotoy_and_clear_text y
2112 | Glut.LEFT_BUTTON when state.outline = None
2113 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2114 if bstate = Glut.DOWN
2115 then (
2116 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2117 state.mstate <- Mpan (x, y)
2119 else
2120 state.mstate <- Mnone
2122 | Glut.LEFT_BUTTON
2123 when state.outline = None && x > conf.winw - conf.scrollw ->
2124 if bstate = Glut.DOWN
2125 then
2126 let position, sh = scrollph state.y in
2127 if y > truncate position && y < truncate (position +. sh)
2128 then
2129 state.mstate <- Mscroll
2130 else
2131 let percent = float y /. float conf.winh in
2132 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2133 gotoy desty;
2134 state.mstate <- Mscroll
2135 else
2136 state.mstate <- Mnone
2138 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2139 begin match state.birdseye with
2140 | Some vals ->
2141 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2142 let rec loop = function
2143 | [] -> ()
2144 | l :: rest ->
2145 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2146 && x > margin && x < margin + l.pagew
2147 then (
2148 let y = getpagey l.pageno in
2149 state.y <- y;
2150 birdseyeoff vals;
2151 reshape conf.winw conf.winh;
2153 else loop rest
2155 loop state.layout;
2156 | None -> () (* impossible *)
2159 | Glut.LEFT_BUTTON when state.outline = None ->
2160 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2161 begin match dest with
2162 | Ulinkgoto (pageno, top) ->
2163 if pageno >= 0
2164 then
2165 gotopage1 pageno top
2167 | Ulinkuri s ->
2168 print_endline s
2170 | Unone when bstate = Glut.DOWN ->
2171 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2172 state.mstate <- Mpan (x, y);
2174 | Unone | Utext _ ->
2175 if bstate = Glut.DOWN
2176 then (
2177 if conf.angle mod 360 = 0
2178 then (
2179 state.mstate <- Msel ((x, y), (x, y));
2180 Glut.postRedisplay ()
2183 else (
2184 match state.mstate with
2185 | Mnone -> ()
2187 | Mscroll ->
2188 state.mstate <- Mnone
2190 | Mpan _ ->
2191 Glut.setCursor Glut.CURSOR_INHERIT;
2192 state.mstate <- Mnone
2194 | Msel ((x0, y0), (x1, y1)) ->
2195 let f l =
2196 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2197 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2198 then
2199 match getopaque l.pageno with
2200 | Some (opaque, _) when validopaque opaque ->
2201 copysel opaque
2202 | _ -> ()
2204 List.iter f state.layout;
2205 copysel ""; (* ugly *)
2206 Glut.setCursor Glut.CURSOR_INHERIT;
2207 state.mstate <- Mnone;
2211 | _ ->
2214 let mouse ~button ~state ~x ~y = mouse button state x y;;
2216 let motion ~x ~y =
2217 if state.outline = None
2218 then
2219 match state.mstate with
2220 | Mnone -> ()
2222 | Mpan (x0, y0) ->
2223 let dx = x - x0
2224 and dy = y0 - y in
2225 state.mstate <- Mpan (x, y);
2226 if conf.zoom > 1.0 then state.x <- state.x + dx;
2227 let y = clamp dy in
2228 gotoy_and_clear_text y
2230 | Msel (a, _) ->
2231 state.mstate <- Msel (a, (x, y));
2232 Glut.postRedisplay ()
2234 | Mscroll ->
2235 let y = min conf.winh (max 0 y) in
2236 let percent = float y /. float conf.winh in
2237 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2238 gotoy_and_clear_text y
2241 let pmotion ~x ~y =
2242 if state.outline = None && state.birdseye = None
2243 then
2244 match state.mstate with
2245 | Mnone ->
2246 begin match getunder x y with
2247 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2248 | Ulinkuri uri ->
2249 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2250 Glut.setCursor Glut.CURSOR_INFO
2251 | Ulinkgoto (page, y) ->
2252 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2253 Glut.setCursor Glut.CURSOR_INFO
2254 | Utext s ->
2255 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2256 Glut.setCursor Glut.CURSOR_TEXT
2259 | Mpan _ | Msel _ | Mscroll ->
2263 module State =
2264 struct
2265 open Parser
2267 let home =
2269 match Sys.os_type with
2270 | "Win32" -> Sys.getenv "HOMEPATH"
2271 | _ -> Sys.getenv "HOME"
2272 with exn ->
2273 prerr_endline
2274 ("Can not determine home directory location: " ^
2275 Printexc.to_string exn);
2279 let config_of c attrs =
2280 let apply c k v =
2282 match k with
2283 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2284 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2285 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2286 | "preload" -> { c with preload = bool_of_string v }
2287 | "page-bias" -> { c with pagebias = int_of_string v }
2288 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2289 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2290 | "crop-hack" -> { c with crophack = bool_of_string v }
2291 | "throttle" -> { c with showall = bool_of_string v }
2292 | "highlight-links" -> { c with hlinks = bool_of_string v }
2293 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2294 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2295 | "zoom" ->
2296 let zoom = float_of_string v /. 100. in
2297 let zoom = max 0.01 (min 2.2 zoom) in
2298 { c with zoom = zoom }
2299 | "presentation" -> { c with presentation = bool_of_string v }
2300 | "rotation-angle" -> { c with angle = int_of_string v }
2301 | "width" -> { c with winw = max 20 (int_of_string v) }
2302 | "height" -> { c with winh = max 20 (int_of_string v) }
2303 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2304 | "proportional-display" -> { c with proportional = bool_of_string v }
2305 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2306 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2307 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2308 | _ -> c
2309 with exn ->
2310 prerr_endline ("Error processing attribute (`" ^
2311 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2314 let rec fold c = function
2315 | [] -> c
2316 | (k, v) :: rest ->
2317 let c = apply c k v in
2318 fold c rest
2320 fold c attrs;
2323 let bookmark_of attrs =
2324 let rec fold title page rely = function
2325 | ("title", v) :: rest -> fold v page rely rest
2326 | ("page", v) :: rest -> fold title v rely rest
2327 | ("rely", v) :: rest -> fold title page v rest
2328 | _ :: rest -> fold title page rely rest
2329 | [] -> title, page, rely
2331 fold "invalid" "0" "0" attrs
2334 let setconf dst src =
2335 dst.scrollw <- src.scrollw;
2336 dst.scrollh <- src.scrollh;
2337 dst.icase <- src.icase;
2338 dst.preload <- src.preload;
2339 dst.pagebias <- src.pagebias;
2340 dst.verbose <- src.verbose;
2341 dst.scrollincr <- src.scrollincr;
2342 dst.maxhfit <- src.maxhfit;
2343 dst.crophack <- src.crophack;
2344 dst.autoscroll <- src.autoscroll;
2345 dst.showall <- src.showall;
2346 dst.hlinks <- src.hlinks;
2347 dst.underinfo <- src.underinfo;
2348 dst.interpagespace <- src.interpagespace;
2349 dst.zoom <- src.zoom;
2350 dst.presentation <- src.presentation;
2351 dst.angle <- src.angle;
2352 dst.winw <- src.winw;
2353 dst.winh <- src.winh;
2354 dst.savebmarks <- src.savebmarks;
2355 dst.memlimit <- src.memlimit;
2356 dst.proportional <- src.proportional;
2357 dst.texcount <- src.texcount;
2358 dst.sliceheight <- src.sliceheight;
2361 let unent s =
2362 let l = String.length s in
2363 let b = Buffer.create l in
2364 unent b s 0 l;
2365 Buffer.contents b;
2368 let get s =
2369 let h = Hashtbl.create 10 in
2370 let dc = { defconf with angle = defconf.angle } in
2371 let rec toplevel v t spos epos =
2372 match t with
2373 | Vdata | Vcdata | Vend -> v
2374 | Vopen ("llppconfig", attrs, closed) ->
2375 if closed
2376 then v
2377 else { v with f = llppconfig }
2378 | Vopen _ ->
2379 error "unexpected subelement at top level" s spos
2380 | Vclose tag -> error "unexpected close at top level" s spos
2382 and llppconfig v t spos epos =
2383 match t with
2384 | Vdata | Vcdata | Vend -> v
2385 | Vopen ("defaults", attrs, closed) ->
2386 let c = config_of dc attrs in
2387 setconf dc c;
2388 if closed
2389 then v
2390 else { v with f = skip "defaults" (fun () -> v) }
2392 | Vopen ("doc", attrs, closed) ->
2393 let pathent =
2395 List.assoc "path" attrs
2396 with Not_found -> error "doc is missing path attribute" s spos
2398 let path = unent pathent in
2399 let c = config_of dc attrs in
2400 let y =
2402 float_of_string (List.assoc "rely" attrs)
2403 with
2404 | Not_found -> 0.0
2405 | exn ->
2406 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2409 let x =
2411 int_of_string (List.assoc "pan" attrs)
2412 with
2413 | Not_found -> 0
2414 | exn ->
2415 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2418 if closed
2419 then (Hashtbl.add h path (c, [], x, y); v)
2420 else { v with f = doc path x y c [] }
2422 | Vopen (tag, _, closed) ->
2423 error "unexpected subelement in llppconfig" s spos
2425 | Vclose "llppconfig" -> { v with f = toplevel }
2426 | Vclose tag -> error "unexpected close in llppconfig" s spos
2428 and doc path x y c bookmarks v t spos epos =
2429 match t with
2430 | Vdata | Vcdata -> v
2431 | Vend -> error "unexpected end of input in doc" s spos
2432 | Vopen ("bookmarks", attrs, closed) ->
2433 { v with f = pbookmarks path x y c bookmarks }
2435 | Vopen (tag, _, _) ->
2436 error "unexpected subelement in doc" s spos
2438 | Vclose "doc" ->
2439 Hashtbl.add h path (c, List.rev bookmarks, x, y);
2440 { v with f = llppconfig }
2442 | Vclose tag -> error "unexpected close in doc" s spos
2444 and pbookmarks path x y c bookmarks v t spos epos =
2445 match t with
2446 | Vdata | Vcdata -> v
2447 | Vend -> error "unexpected end of input in bookmarks" s spos
2448 | Vopen ("item", attrs, closed) ->
2449 let titleent, spage, srely = bookmark_of attrs in
2450 let page =
2452 int_of_string spage
2453 with exn ->
2454 dolog "Failed to convert page %S to integer: %s"
2455 spage (Printexc.to_string exn);
2458 let rely =
2460 float_of_string srely
2461 with exn ->
2462 dolog "Failed to convert rely %S to real: %s"
2463 srely (Printexc.to_string exn);
2466 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2467 if closed
2468 then { v with f = pbookmarks path x y c bookmarks }
2469 else
2470 let f () = v in
2471 { v with f = skip "item" f }
2473 | Vopen _ ->
2474 error "unexpected subelement in bookmarks" s spos
2476 | Vclose "bookmarks" ->
2477 { v with f = doc path x y c bookmarks }
2479 | Vclose tag -> error "unexpected close in bookmarks" s spos
2481 and skip tag f v t spos epos =
2482 match t with
2483 | Vdata | Vcdata -> v
2484 | Vend ->
2485 error ("unexpected end of input in skipped " ^ tag) s spos
2486 | Vopen (tag', _, closed) ->
2487 if closed
2488 then v
2489 else
2490 let f' () = { v with f = skip tag f } in
2491 { v with f = skip tag' f' }
2492 | Vclose ctag ->
2493 if tag = ctag
2494 then f ()
2495 else error ("unexpected close in skipped " ^ tag) s spos
2498 parse { f = toplevel; accu = () } s;
2499 h, dc;
2502 let do_load f ic =
2504 let len = in_channel_length ic in
2505 let s = String.create len in
2506 really_input ic s 0 len;
2507 f s;
2508 with
2509 | Parse_error (msg, s, pos) ->
2510 let subs = subs s pos in
2511 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2512 failwith ("parse error: " ^ s)
2514 | exn ->
2515 failwith ("config load error: " ^ Printexc.to_string exn)
2518 let path =
2519 let dir =
2521 let dir = Filename.concat home ".config" in
2522 if Sys.is_directory dir then dir else home
2523 with _ -> home
2525 Filename.concat dir "llpp.conf"
2528 let load1 f =
2529 if Sys.file_exists path
2530 then
2531 match
2532 (try Some (open_in_bin path)
2533 with exn ->
2534 prerr_endline
2535 ("Error opening configuation file `" ^ path ^ "': " ^
2536 Printexc.to_string exn);
2537 None
2539 with
2540 | Some ic ->
2541 begin try
2542 f (do_load get ic)
2543 with exn ->
2544 prerr_endline
2545 ("Error loading configuation from `" ^ path ^ "': " ^
2546 Printexc.to_string exn);
2547 end;
2548 close_in ic;
2550 | None -> ()
2551 else
2552 f (Hashtbl.create 0, defconf)
2555 let load () =
2556 let f (h, dc) =
2557 let pc, pb, px, py =
2559 Hashtbl.find h state.path
2560 with Not_found -> dc, [], 0, 0.0
2562 setconf defconf dc;
2563 setconf conf pc;
2564 state.bookmarks <- pb;
2565 state.x <- px;
2566 cbput state.hists.nav py;
2568 load1 f
2571 let add_attrs bb always dc c =
2572 let ob s a b =
2573 if always || a != b
2574 then Printf.bprintf bb "\n %s='%b'" s a
2575 and oi s a b =
2576 if always || a != b
2577 then Printf.bprintf bb "\n %s='%d'" s a
2578 and oz s a b =
2579 if always || a <> b
2580 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2582 let w, h =
2583 if always
2584 then dc.winw, dc.winh
2585 else
2586 match state.fullscreen with
2587 | Some wh -> wh
2588 | None -> c.winw, c.winh
2590 let zoom, presentation, interpagespace =
2591 if always
2592 then dc.zoom, dc.presentation, dc.interpagespace
2593 else
2594 match state.birdseye with
2595 | Some (zoom, _, presentation, interpagespace) ->
2596 (zoom, presentation, interpagespace)
2597 | None -> c.zoom, c.presentation, c.interpagespace
2599 oi "width" w dc.winw;
2600 oi "height" h dc.winh;
2601 oi "scroll-bar-width" c.scrollw dc.scrollw;
2602 oi "scroll-handle-height" c.scrollh dc.scrollh;
2603 ob "case-insensitive-search" c.icase dc.icase;
2604 ob "preload" c.preload dc.preload;
2605 oi "page-bias" c.pagebias dc.pagebias;
2606 oi "scroll-step" c.scrollincr dc.scrollincr;
2607 ob "max-height-fit" c.maxhfit dc.maxhfit;
2608 ob "crop-hack" c.crophack dc.crophack;
2609 ob "throttle" c.showall dc.showall;
2610 ob "highlight-links" c.hlinks dc.hlinks;
2611 ob "under-cursor-info" c.underinfo dc.underinfo;
2612 oi "vertical-margin" interpagespace dc.interpagespace;
2613 oz "zoom" zoom dc.zoom;
2614 ob "presentation" presentation dc.presentation;
2615 oi "rotation-angle" c.angle dc.angle;
2616 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2617 ob "proportional-display" c.proportional dc.proportional;
2618 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2619 oi "texcount" c.texcount dc.texcount;
2620 oi "slice-height" c.sliceheight dc.sliceheight;
2623 let save () =
2624 let bb = Buffer.create 32768 in
2625 let f (h, dc) =
2626 Buffer.add_string bb "<llppconfig>\n<defaults ";
2627 add_attrs bb true dc dc;
2628 Buffer.add_string bb "/>\n";
2630 let adddoc path x y c bookmarks =
2631 if bookmarks == [] && c = dc && y = 0.0
2632 then ()
2633 else (
2634 Printf.bprintf bb "<doc path='%s'"
2635 (enent path 0 (String.length path));
2637 if y <> 0.0
2638 then Printf.bprintf bb " rely='%f'" y;
2640 if x != 0
2641 then Printf.bprintf bb " pan='%d'" x;
2643 add_attrs bb false dc c;
2645 begin match bookmarks with
2646 | [] -> Buffer.add_string bb "/>\n"
2647 | _ ->
2648 Buffer.add_string bb ">\n<bookmarks>\n";
2649 List.iter (fun (title, _level, page, rely) ->
2650 Printf.bprintf bb
2651 "<item title='%s' page='%d' rely='%f'/>\n"
2652 (enent title 0 (String.length title))
2653 page
2654 rely
2655 ) bookmarks;
2656 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2657 end;
2661 let x =
2662 match state.birdseye with
2663 | Some (_, x, _, _) -> x
2664 | None -> state.x
2666 adddoc state.path x (yratio state.y) conf
2667 (if conf.savebmarks then state.bookmarks else []);
2669 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2670 if path <> state.path
2671 then
2672 adddoc path x y c bookmarks
2673 ) h;
2674 Buffer.add_string bb "</llppconfig>";
2676 load1 f;
2677 if Buffer.length bb > 0
2678 then
2680 let tmp = path ^ ".tmp" in
2681 let oc = open_out_bin tmp in
2682 Buffer.output_buffer oc bb;
2683 close_out oc;
2684 Sys.rename tmp path;
2685 with exn ->
2686 prerr_endline
2687 ("error while saving configuration: " ^ Printexc.to_string exn)
2689 end;;
2691 let () =
2692 Arg.parse
2693 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2694 (fun s -> state.path <- s)
2695 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2697 let path =
2698 if String.length state.path = 0
2699 then (prerr_endline "filename missing"; exit 1)
2700 else (
2701 if Filename.is_relative state.path
2702 then Filename.concat (Sys.getcwd ()) state.path
2703 else state.path
2706 state.path <- path;
2708 State.load ();
2710 let _ = Glut.init Sys.argv in
2711 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2712 let () = Glut.initWindowSize conf.winw conf.winh in
2713 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2715 let csock, ssock =
2716 if Sys.os_type = "Unix"
2717 then
2718 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2719 else
2720 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2721 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2722 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2723 Unix.bind sock addr;
2724 Unix.listen sock 1;
2725 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2726 Unix.connect csock addr;
2727 let ssock, _ = Unix.accept sock in
2728 Unix.close sock;
2729 let opts sock =
2730 Unix.setsockopt sock Unix.TCP_NODELAY true;
2731 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2733 opts ssock;
2734 opts csock;
2735 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2736 ssock, csock
2739 let () = Glut.displayFunc display in
2740 let () = Glut.reshapeFunc reshape in
2741 let () = Glut.keyboardFunc keyboard in
2742 let () = Glut.specialFunc special in
2743 let () = Glut.idleFunc (Some idle) in
2744 let () = Glut.mouseFunc mouse in
2745 let () = Glut.motionFunc motion in
2746 let () = Glut.passiveMotionFunc pmotion in
2748 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2749 state.csock <- csock;
2750 state.ssock <- ssock;
2751 state.text <- "Opening " ^ path;
2752 writeopen state.path state.password;
2754 at_exit State.save;
2756 let rec handlelablglutbug () =
2758 Glut.mainLoop ();
2759 with Glut.BadEnum "key in special_of_int" ->
2760 showtext '!' " LablGlut bug: special key not recognized";
2761 handlelablglutbug ()
2763 handlelablglutbug ();