Move to pervasive use of anchors
[llpp.git] / main.ml
blob13bfa518684ed3f59951b65828a800fbe980121b
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 interpagespace = int
22 and texcount = int
23 and sliceheight = int
24 and gen = int
25 and top = 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 : float
147 ; mutable presentation : bool
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
156 ; mutable thumbw : width
160 type outline = string * int * int * float;;
161 type outlines =
162 | Oarray of outline array
163 | Olist of outline list
164 | Onarrow of string * outline array * outline array
167 type rect = (float * float * float * float * float * float * float * float);;
169 type pagemapkey = (pageno * width * angle * proportional * gen);;
171 type anchor = pageno * top;;
173 type state =
174 { mutable csock : Unix.file_descr
175 ; mutable ssock : Unix.file_descr
176 ; mutable w : int
177 ; mutable x : int
178 ; mutable y : int
179 ; mutable anchor : anchor
180 ; mutable maxy : int
181 ; mutable layout : layout list
182 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
183 ; mutable pdims : (pageno * width * height * leftx) list
184 ; mutable pagecount : int
185 ; pagecache : string circbuf
186 ; mutable rendering : bool
187 ; mutable mstate : mstate
188 ; mutable searchpattern : string
189 ; mutable rects : (pageno * recttype * rect) list
190 ; mutable rects1 : (pageno * recttype * rect) list
191 ; mutable text : string
192 ; mutable fullscreen : (width * height) option
193 ; mutable birdseye : (conf * leftx * pageno * pageno) option
194 ; mutable textentry : textentry option
195 ; mutable outlines : outlines
196 ; mutable outline : (bool * int * int * outline array * string) option
197 ; mutable bookmarks : outline list
198 ; mutable path : string
199 ; mutable password : string
200 ; mutable invalidated : int
201 ; mutable colorscale : float
202 ; mutable memused : int
203 ; mutable gen : gen
204 ; mutable throttle : layout list option
205 ; hists : hists
207 and hists =
208 { pat : string circbuf
209 ; pag : string circbuf
210 ; nav : anchor circbuf
214 let defconf =
215 { scrollw = 7
216 ; scrollh = 12
217 ; icase = true
218 ; preload = true
219 ; pagebias = 0
220 ; verbose = false
221 ; scrollincr = 24
222 ; maxhfit = true
223 ; crophack = false
224 ; autoscroll = false
225 ; showall = false
226 ; hlinks = false
227 ; underinfo = false
228 ; interpagespace = 2
229 ; zoom = 1.0
230 ; presentation = false
231 ; angle = 0
232 ; winw = 900
233 ; winh = 900
234 ; savebmarks = true
235 ; proportional = true
236 ; memlimit = 32*1024*1024
237 ; texcount = 256
238 ; sliceheight = 24
239 ; thumbw = 76
243 let conf = { defconf with angle = defconf.angle };;
245 let state =
246 { csock = Unix.stdin
247 ; ssock = Unix.stdin
248 ; x = 0
249 ; y = 0
250 ; anchor = (0, 0.0)
251 ; w = 0
252 ; layout = []
253 ; maxy = max_int
254 ; pagemap = Hashtbl.create 10
255 ; pagecache = cbnew 100 ""
256 ; pdims = []
257 ; pagecount = 0
258 ; rendering = false
259 ; mstate = Mnone
260 ; rects = []
261 ; rects1 = []
262 ; text = ""
263 ; fullscreen = None
264 ; birdseye = None
265 ; textentry = None
266 ; searchpattern = ""
267 ; outlines = Olist []
268 ; outline = None
269 ; bookmarks = []
270 ; path = ""
271 ; password = ""
272 ; invalidated = 0
273 ; hists =
274 { nav = cbnew 100 (0, 0.0)
275 ; pat = cbnew 20 ""
276 ; pag = cbnew 10 ""
278 ; colorscale = 1.0
279 ; memused = 0
280 ; gen = 0
281 ; throttle = None
285 let vlog fmt =
286 if conf.verbose
287 then
288 Printf.kprintf prerr_endline fmt
289 else
290 Printf.kprintf ignore fmt
293 let writecmd fd s =
294 let len = String.length s in
295 let n = 4 + len in
296 let b = Buffer.create n in
297 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
298 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
299 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
300 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
301 Buffer.add_string b s;
302 let s' = Buffer.contents b in
303 let n' = Unix.write fd s' 0 n in
304 if n' != n then failwith "write failed";
307 let readcmd fd =
308 let s = "xxxx" in
309 let n = Unix.read fd s 0 4 in
310 if n != 4 then failwith "incomplete read(len)";
311 let len = 0
312 lor (Char.code s.[0] lsl 24)
313 lor (Char.code s.[1] lsl 16)
314 lor (Char.code s.[2] lsl 8)
315 lor (Char.code s.[3] lsl 0)
317 let s = String.create len in
318 let n = Unix.read fd s 0 len in
319 if n != len then failwith "incomplete read(data)";
323 let makecmd s l =
324 let b = Buffer.create 10 in
325 Buffer.add_string b s;
326 let rec combine = function
327 | [] -> b
328 | x :: xs ->
329 Buffer.add_char b ' ';
330 let s =
331 match x with
332 | `b b -> if b then "1" else "0"
333 | `s s -> s
334 | `i i -> string_of_int i
335 | `f f -> string_of_float f
336 | `I f -> string_of_int (truncate f)
338 Buffer.add_string b s;
339 combine xs;
341 combine l;
344 let wcmd s l =
345 let cmd = Buffer.contents (makecmd s l) in
346 writecmd state.csock cmd;
349 let calcips h =
350 if conf.presentation
351 then
352 let d = conf.winh - h in
353 max 0 ((d + 1) / 2)
354 else
355 conf.interpagespace
358 let calcheight () =
359 let rec f pn ph pi fh l =
360 match l with
361 | (n, _, h, _) :: rest ->
362 let ips = calcips h in
363 let fh =
364 if conf.presentation
365 then fh+ips
366 else (
367 if state.birdseye <> None && pn = 0
368 then fh + ips
369 else fh
372 let fh = fh + ((n - pn) * (ph + pi)) in
373 f n h ips fh rest
375 | [] ->
376 let inc =
377 if conf.presentation || (state.birdseye <> None && pn = 0)
378 then 0
379 else -pi
381 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
382 max 0 fh
384 let fh = f 0 0 0 0 state.pdims in
388 let getpageyh pageno =
389 let rec f pn ph pi y l =
390 match l with
391 | (n, _, h, _) :: rest ->
392 let ips = calcips h in
393 if n >= pageno
394 then
395 if conf.presentation && n = pageno
396 then
397 y + (pageno - pn) * (ph + pi) + pi, h
398 else
399 y + (pageno - pn) * (ph + pi), h
400 else
401 let y = y + (if conf.presentation then pi else 0) in
402 let y = y + (n - pn) * (ph + pi) in
403 f n h ips y rest
405 | [] ->
406 y + (pageno - pn) * (ph + pi), ph
408 f 0 0 0 0 state.pdims
411 let getpagey pageno = fst (getpageyh pageno);;
413 let layout y sh =
414 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
415 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
416 match pdims with
417 | (pageno', w, h, x) :: rest when pageno' = pageno ->
418 let ips = calcips h in
419 let yinc =
420 if conf.presentation || (state.birdseye <> None && pageno = 0)
421 then ips
422 else 0
424 (w, h, ips, x), rest, pdimno + 1, yinc
425 | _ ->
426 prev, pdims, pdimno, 0
428 let dy = dy + yinc in
429 let py = py + yinc in
430 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
431 then
432 accu
433 else
434 let vy = y + dy in
435 if py + h <= vy - yinc
436 then
437 let py = py + h + ips in
438 let dy = max 0 (py - y) in
439 f ~pageno:(pageno+1)
440 ~pdimno
441 ~prev:curr
444 ~pdims:rest
445 ~cacheleft
446 ~accu
447 else
448 let pagey = vy - py in
449 let pagevh = h - pagey in
450 let pagevh = min (sh - dy) pagevh in
451 let off = if yinc > 0 then py - vy else 0 in
452 let py = py + h + ips in
453 let e =
454 { pageno = pageno
455 ; pagedimno = pdimno
456 ; pagew = w
457 ; pageh = h
458 ; pagedispy = dy + off
459 ; pagey = pagey + off
460 ; pagevh = pagevh - off
461 ; pagex = x
464 let accu = e :: accu in
465 f ~pageno:(pageno+1)
466 ~pdimno
467 ~prev:curr
469 ~dy:(dy+pagevh+ips)
470 ~pdims:rest
471 ~cacheleft:(cacheleft-1)
472 ~accu
474 if state.invalidated = 0
475 then (
476 let accu =
478 ~pageno:0
479 ~pdimno:~-1
480 ~prev:(0,0,0,0)
481 ~py:0
482 ~dy:0
483 ~pdims:state.pdims
484 ~cacheleft:(cbcap state.pagecache)
485 ~accu:[]
487 List.rev accu
489 else
493 let clamp incr =
494 let y = state.y + incr in
495 let y = max 0 y in
496 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
500 let getopaque pageno =
501 try Some (Hashtbl.find state.pagemap
502 (pageno, state.w, conf.angle, conf.proportional, state.gen))
503 with Not_found -> None
506 let cache pageno opaque =
507 Hashtbl.replace state.pagemap
508 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
511 let validopaque opaque = String.length opaque > 0;;
513 let render l =
514 match getopaque l.pageno with
515 | None when not state.rendering ->
516 state.rendering <- true;
517 cache l.pageno ("", -1);
518 wcmd "render" [`i (l.pageno + 1)
519 ;`i l.pagedimno
520 ;`i l.pagew
521 ;`i l.pageh];
523 | _ -> ()
526 let loadlayout layout =
527 let rec f all = function
528 | l :: ls ->
529 begin match getopaque l.pageno with
530 | None -> render l; f false ls
531 | Some (opaque, _) -> f (all && validopaque opaque) ls
533 | [] -> all
535 f (layout <> []) layout;
538 let findpageforopaque opaque =
539 Hashtbl.fold
540 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
541 state.pagemap None
544 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
546 let preload () =
547 let oktopreload =
548 if conf.preload
549 then
550 let memleft = conf.memlimit - state.memused in
551 if memleft < 0
552 then
553 let opaque = cbpeek state.pagecache in
554 match findpageforopaque opaque with
555 | Some ((n, _, _, _, _), size) ->
556 memleft + size >= 0 && not (pagevisible state.layout n)
557 | None -> false
558 else true
559 else false
561 if oktopreload
562 then
563 let presentation = conf.presentation in
564 let interpagespace = conf.interpagespace in
565 let maxy = state.maxy in
566 conf.presentation <- false;
567 conf.interpagespace <- 0;
568 state.maxy <- calcheight ();
569 let y =
570 match state.layout with
571 | [] -> 0
572 | l :: _ -> getpagey l.pageno
574 let y = if y < conf.winh then 0 else y - conf.winh in
575 let pages = layout y (conf.winh*3) in
576 List.iter render pages;
577 conf.presentation <- presentation;
578 conf.interpagespace <- interpagespace;
579 state.maxy <- maxy;
582 let gotoy y =
583 let y = max 0 y in
584 let y = min state.maxy y in
585 let pages = layout y conf.winh in
586 let ready = loadlayout pages in
587 if conf.showall
588 then (
589 if ready
590 then (
591 state.y <- y;
592 state.layout <- pages;
593 state.throttle <- None;
594 Glut.postRedisplay ();
596 else (
597 state.throttle <- Some pages;
600 else (
601 state.y <- y;
602 state.layout <- pages;
603 state.throttle <- None;
604 Glut.postRedisplay ();
606 begin match state.birdseye with
607 | Some (conf, leftx, pageno, hooverpageno) ->
608 if not (pagevisible pages pageno)
609 then (
610 match state.layout with
611 | [] -> ()
612 | l :: _ ->
613 state.birdseye <- Some (conf, leftx, l.pageno, hooverpageno)
615 | _ -> ()
616 end;
617 preload ();
620 let gotoy_and_clear_text y =
621 gotoy y;
622 if not conf.verbose then state.text <- "";
625 let emptyanchor = (0, 0.0);;
627 let getanchor () =
628 match state.layout with
629 | [] -> emptyanchor
630 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
633 let getanchory (n, top) =
634 let y, h = getpageyh n in
635 y + (truncate (top *. float h));
638 let gotoanchor anchor =
639 gotoy (getanchory anchor);
642 let addnav () =
643 cbput state.hists.nav (getanchor ());
646 let getnav () =
647 let anchor = cbgetc state.hists.nav ~-1 in
648 getanchory anchor;
651 let gotopage n top =
652 let y, h = getpageyh n in
653 addnav ();
654 gotoy_and_clear_text (y + (truncate (top *. float h)));
657 let gotopage1 n top =
658 let y = getpagey n in
659 addnav ();
660 gotoy_and_clear_text (y + top);
663 let invalidate () =
664 state.layout <- [];
665 state.pdims <- [];
666 state.rects <- [];
667 state.rects1 <- [];
668 state.invalidated <- state.invalidated + 1;
671 let scalecolor c =
672 let c = c *. state.colorscale in
673 (c, c, c);
676 let represent () =
677 state.maxy <- calcheight ();
678 gotoanchor state.anchor;
681 let pagematrix () =
682 GlMat.mode `projection;
683 GlMat.load_identity ();
684 GlMat.rotate ~x:1.0 ~angle:180.0 ();
685 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
686 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
689 let winmatrix () =
690 GlMat.mode `projection;
691 GlMat.load_identity ();
692 GlMat.rotate ~x:1.0 ~angle:180.0 ();
693 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
694 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
697 let reshape ~w ~h =
698 if state.invalidated = 0
699 then state.anchor <- getanchor ();
701 conf.winw <- w;
702 let w = truncate (float w *. conf.zoom) - conf.scrollw in
703 let w = max w 2 in
704 state.w <- w;
705 conf.winh <- h;
706 GlMat.mode `modelview;
707 GlMat.load_identity ();
708 GlClear.color (scalecolor 1.0);
709 GlClear.clear [`color];
711 invalidate ();
712 wcmd "geometry" [`i w; `i h];
715 let showtext c s =
716 GlDraw.color (0.0, 0.0, 0.0);
717 GlDraw.rect
718 (0.0, float (conf.winh - 18))
719 (float (conf.winw - conf.scrollw - 1), float conf.winh)
721 let font = Glut.BITMAP_8_BY_13 in
722 GlDraw.color (1.0, 1.0, 1.0);
723 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
724 Glut.bitmapCharacter ~font ~c:(Char.code c);
725 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
728 let enttext () =
729 let len = String.length state.text in
730 match state.textentry with
731 | None ->
732 if len > 0 then showtext ' ' state.text
734 | Some (c, text, _, _, _) ->
735 let s =
736 if len > 0
737 then
738 text ^ " [" ^ state.text ^ "]"
739 else
740 text
742 showtext c s;
745 let showtext c s =
746 if true
747 then (
748 state.text <- Printf.sprintf "%c%s" c s;
749 Glut.postRedisplay ();
751 else (
752 showtext c s;
753 Glut.swapBuffers ();
757 let act cmd =
758 match cmd.[0] with
759 | 'c' ->
760 state.pdims <- [];
762 | 'D' ->
763 state.rects <- state.rects1;
764 Glut.postRedisplay ()
766 | 'C' ->
767 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
768 state.pagecount <- n;
769 state.invalidated <- state.invalidated - 1;
770 if state.invalidated = 0
771 then represent ()
773 | 't' ->
774 let s = Scanf.sscanf cmd "t %n"
775 (fun n -> String.sub cmd n (String.length cmd - n))
777 Glut.setWindowTitle s
779 | 'T' ->
780 let s = Scanf.sscanf cmd "T %n"
781 (fun n -> String.sub cmd n (String.length cmd - n))
783 if state.textentry = None
784 then (
785 state.text <- s;
786 showtext ' ' s;
788 else (
789 state.text <- s;
790 Glut.postRedisplay ();
793 | 'V' ->
794 if conf.verbose
795 then
796 let s = Scanf.sscanf cmd "V %n"
797 (fun n -> String.sub cmd n (String.length cmd - n))
799 state.text <- s;
800 showtext ' ' s;
802 | 'F' ->
803 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
804 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
805 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
806 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
808 let y = (getpagey pageno) + truncate y0 in
809 addnav ();
810 gotoy y;
811 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
813 | 'R' ->
814 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
815 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
816 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
817 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
819 state.rects1 <-
820 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
822 | 'r' ->
823 let n, w, h, r, l, s, p =
824 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
825 (fun n w h r l s p ->
826 (n-1, w, h, r, l != 0, s, p))
829 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
830 state.memused <- state.memused + s;
832 let layout =
833 match state.throttle with
834 | None -> state.layout
835 | Some layout -> layout
838 let rec gc () =
839 if (state.memused <= conf.memlimit) || cbempty state.pagecache
840 then ()
841 else (
842 let evictedopaque = cbpeek state.pagecache in
843 match findpageforopaque evictedopaque with
844 | None -> failwith "bug in gc"
845 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
846 if state.gen != gen || not (pagevisible layout evictedn)
847 then (
848 wcmd "free" [`s evictedopaque];
849 state.memused <- state.memused - evictedsize;
850 Hashtbl.remove state.pagemap k;
851 cbdecr state.pagecache;
852 gc ();
856 gc ();
858 cbput state.pagecache p;
859 state.rendering <- false;
861 begin match state.throttle with
862 | None ->
863 if pagevisible state.layout n
864 then gotoy state.y
865 else (
866 let allvisible = loadlayout state.layout in
867 if allvisible then preload ();
870 | Some layout ->
871 match layout with
872 | [] -> ()
873 | l :: _ ->
874 let y = getpagey l.pageno + l.pagey in
875 gotoy y
878 | 'l' ->
879 let (n, w, h, x) as pdim =
880 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
882 state.pdims <- pdim :: state.pdims
884 | 'o' ->
885 let (l, n, t, h, pos) =
886 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
888 let s = String.sub cmd pos (String.length cmd - pos) in
889 let s =
890 let l = String.length s in
891 let b = Buffer.create (String.length s) in
892 let rec loop pc2 i =
893 if i = l
894 then ()
895 else
896 let pc2 =
897 match s.[i] with
898 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
899 | '\xc2' -> true
900 | c ->
901 let c = if Char.code c land 0x80 = 0 then c else '?' in
902 Buffer.add_char b c;
903 false
905 loop pc2 (i+1)
907 loop false 0;
908 Buffer.contents b
910 let outline = (s, l, n, float t /. float h) in
911 let outlines =
912 match state.outlines with
913 | Olist outlines -> Olist (outline :: outlines)
914 | Oarray _ -> Olist [outline]
915 | Onarrow _ -> Olist [outline]
917 state.outlines <- outlines
919 | _ ->
920 log "unknown cmd `%S'" cmd
923 let now = Unix.gettimeofday;;
925 let idle () =
926 let rec loop delay =
927 let r, _, _ = Unix.select [state.csock] [] [] delay in
928 begin match r with
929 | [] ->
930 if conf.autoscroll
931 then begin
932 let y = state.y + conf.scrollincr in
933 let y = if y >= state.maxy then 0 else y in
934 gotoy y;
935 state.text <- "";
936 end;
938 | _ ->
939 let cmd = readcmd state.csock in
940 act cmd;
941 loop 0.0
942 end;
943 in loop 0.001
946 let onhist cb =
947 let rc = cb.rc in
948 let action = function
949 | HCprev -> cbget cb ~-1
950 | HCnext -> cbget cb 1
951 | HCfirst -> cbget cb ~-(cb.rc)
952 | HClast -> cbget cb (cb.len - 1 - cb.rc)
953 and cancel () = cb.rc <- rc
954 in (action, cancel)
957 let search pattern forward =
958 if String.length pattern > 0
959 then
960 let pn, py =
961 match state.layout with
962 | [] -> 0, 0
963 | l :: _ ->
964 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
966 let cmd =
967 let b = makecmd "search"
968 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
970 Buffer.add_char b ',';
971 Buffer.add_string b pattern;
972 Buffer.add_char b '\000';
973 Buffer.contents b;
975 writecmd state.csock cmd;
978 let intentry text key =
979 let c = Char.unsafe_chr key in
980 match c with
981 | '0' .. '9' ->
982 let s = "x" in s.[0] <- c;
983 let text = text ^ s in
984 TEcont text
986 | _ ->
987 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
988 TEcont text
991 let addchar s c =
992 let b = Buffer.create (String.length s + 1) in
993 Buffer.add_string b s;
994 Buffer.add_char b c;
995 Buffer.contents b;
998 let textentry text key =
999 let c = Char.unsafe_chr key in
1000 match c with
1001 | _ when key >= 32 && key < 127 ->
1002 let text = addchar text c in
1003 TEcont text
1005 | _ ->
1006 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1007 TEcont text
1010 let reinit angle proportional =
1011 conf.angle <- angle;
1012 conf.proportional <- proportional;
1013 invalidate ();
1014 wcmd "reinit" [`i angle; `b proportional];
1017 let optentry text key =
1018 let btos b = if b then "on" else "off" in
1019 let c = Char.unsafe_chr key in
1020 match c with
1021 | 's' ->
1022 let ondone s =
1023 try conf.scrollincr <- int_of_string s with exc ->
1024 state.text <- Printf.sprintf "bad integer `%s': %s"
1025 s (Printexc.to_string exc)
1027 TEswitch ('#', "", None, intentry, ondone)
1029 | 'R' ->
1030 let ondone s =
1031 match try
1032 Some (int_of_string s)
1033 with exc ->
1034 state.text <- Printf.sprintf "bad integer `%s': %s"
1035 s (Printexc.to_string exc);
1036 None
1037 with
1038 | Some angle -> reinit angle conf.proportional
1039 | None -> ()
1041 TEswitch ('^', "", None, intentry, ondone)
1043 | 'i' ->
1044 conf.icase <- not conf.icase;
1045 TEdone ("case insensitive search " ^ (btos conf.icase))
1047 | 'p' ->
1048 conf.preload <- not conf.preload;
1049 gotoy state.y;
1050 TEdone ("preload " ^ (btos conf.preload))
1052 | 'v' ->
1053 conf.verbose <- not conf.verbose;
1054 TEdone ("verbose " ^ (btos conf.verbose))
1056 | 'h' ->
1057 conf.maxhfit <- not conf.maxhfit;
1058 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1059 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1061 | 'c' ->
1062 conf.crophack <- not conf.crophack;
1063 TEdone ("crophack " ^ btos conf.crophack)
1065 | 'a' ->
1066 conf.showall <- not conf.showall;
1067 TEdone ("showall " ^ btos conf.showall)
1069 | 'f' ->
1070 conf.underinfo <- not conf.underinfo;
1071 TEdone ("underinfo " ^ btos conf.underinfo)
1073 | 'P' ->
1074 conf.savebmarks <- not conf.savebmarks;
1075 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1077 | 'S' ->
1078 let ondone s =
1080 let pageno, py =
1081 match state.layout with
1082 | [] -> 0, 0
1083 | l :: _ ->
1084 l.pageno, l.pagey
1086 conf.interpagespace <- int_of_string s;
1087 state.maxy <- calcheight ();
1088 let y = getpagey pageno in
1089 gotoy (y + py)
1090 with exc ->
1091 state.text <- Printf.sprintf "bad integer `%s': %s"
1092 s (Printexc.to_string exc)
1094 TEswitch ('%', "", None, intentry, ondone)
1096 | 'l' ->
1097 reinit conf.angle (not conf.proportional);
1098 TEdone ("proprortional display " ^ btos conf.proportional)
1100 | _ ->
1101 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1102 TEstop
1105 let maxoutlinerows () = (conf.winh - 31) / 16;;
1107 let enterselector allowdel outlines errmsg msg =
1108 if Array.length outlines = 0
1109 then (
1110 showtext ' ' errmsg;
1112 else (
1113 state.text <- msg;
1114 Glut.setCursor Glut.CURSOR_INHERIT;
1115 let pageno =
1116 match state.layout with
1117 | [] -> -1
1118 | {pageno=pageno} :: rest -> pageno
1120 let active =
1121 let rec loop n =
1122 if n = Array.length outlines
1123 then 0
1124 else
1125 let (_, _, outlinepageno, _) = outlines.(n) in
1126 if outlinepageno >= pageno then n else loop (n+1)
1128 loop 0
1130 state.outline <-
1131 Some (allowdel, active,
1132 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
1133 Glut.postRedisplay ();
1137 let enteroutlinemode () =
1138 let outlines, msg =
1139 match state.outlines with
1140 | Oarray a -> a, ""
1141 | Olist l ->
1142 let a = Array.of_list (List.rev l) in
1143 state.outlines <- Oarray a;
1144 a, ""
1145 | Onarrow (pat, a, b) ->
1146 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1148 enterselector false outlines "Document has no outline" msg;
1151 let enterbookmarkmode () =
1152 let bookmarks = Array.of_list state.bookmarks in
1153 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1156 let quickbookmark ?title () =
1157 match state.layout with
1158 | [] -> ()
1159 | l :: _ ->
1160 let title =
1161 match title with
1162 | None ->
1163 let sec = Unix.gettimeofday () in
1164 let tm = Unix.localtime sec in
1165 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1166 (l.pageno+1)
1167 tm.Unix.tm_mday
1168 tm.Unix.tm_mon
1169 (tm.Unix.tm_year + 1900)
1170 tm.Unix.tm_hour
1171 tm.Unix.tm_min
1172 | Some title -> title
1174 state.bookmarks <-
1175 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1178 let doreshape w h =
1179 state.fullscreen <- None;
1180 Glut.reshapeWindow w h;
1183 let writeopen path password =
1184 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1187 let opendoc path password =
1188 invalidate ();
1189 state.path <- path;
1190 state.password <- password;
1191 state.gen <- state.gen + 1;
1193 writeopen path password;
1194 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1195 wcmd "geometry" [`i state.w; `i conf.winh];
1198 let birdseyeon () =
1199 let zoom = float conf.thumbw /. float conf.winw in
1200 let birdseyepageno =
1201 match state.layout with
1202 | [] -> 0
1203 | l :: _ -> l.pageno
1205 state.birdseye <-
1206 Some ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1207 conf.zoom <- zoom;
1208 conf.presentation <- false;
1209 conf.interpagespace <- 10;
1210 conf.hlinks <- false;
1211 state.x <- 0;
1212 state.mstate <- Mnone;
1213 conf.showall <- false;
1214 Glut.setCursor Glut.CURSOR_INHERIT;
1215 if conf.verbose
1216 then
1217 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1218 (100.0*.zoom)
1219 else
1220 state.text <- ""
1224 let birdseyeoff (c, leftx, pageno, _) =
1225 state.birdseye <- None;
1226 conf.zoom <- c.zoom;
1227 conf.presentation <- c.presentation;
1228 conf.interpagespace <- c.interpagespace;
1229 conf.showall <- c.showall;
1230 conf.hlinks <- c.hlinks;
1231 state.x <- leftx;
1232 if conf.verbose
1233 then
1234 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1235 (100.0*.conf.zoom)
1239 let togglebirdseye () =
1240 match state.birdseye with
1241 | None -> birdseyeon ()
1242 | Some vals -> birdseyeoff vals
1245 let viewkeyboard ~key ~x ~y =
1246 let enttext te =
1247 state.textentry <- te;
1248 state.text <- "";
1249 enttext ();
1250 Glut.postRedisplay ()
1252 match state.textentry with
1253 | None ->
1254 let c = Char.chr key in
1255 begin match c with
1256 | '\027' | 'q' ->
1257 exit 0
1259 | '\008' ->
1260 let y = getnav () in
1261 gotoy_and_clear_text y
1263 | '\013' ->
1264 begin match state.birdseye with
1265 | None -> ()
1266 | Some ((_, _, pageno, _) as vals) ->
1267 birdseyeoff vals;
1268 reshape conf.winw conf.winh;
1269 state.anchor <- (pageno, 0.0);
1270 end;
1272 | 'o' ->
1273 enteroutlinemode ()
1275 | 'u' ->
1276 state.rects <- [];
1277 state.text <- "";
1278 Glut.postRedisplay ()
1280 | '/' | '?' ->
1281 let ondone isforw s =
1282 cbput state.hists.pat s;
1283 state.searchpattern <- s;
1284 search s isforw
1286 enttext (Some (c, "", Some (onhist state.hists.pat),
1287 textentry, ondone (c ='/')))
1289 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1290 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1291 conf.zoom <- min 2.2 (conf.zoom +. incr);
1292 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1293 reshape conf.winw conf.winh
1295 | '+' ->
1296 let ondone s =
1297 let n =
1298 try int_of_string s with exc ->
1299 state.text <- Printf.sprintf "bad integer `%s': %s"
1300 s (Printexc.to_string exc);
1301 max_int
1303 if n != max_int
1304 then (
1305 conf.pagebias <- n;
1306 state.text <- "page bias is now " ^ string_of_int n;
1309 enttext (Some ('+', "", None, intentry, ondone))
1311 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1312 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1313 conf.zoom <- max 0.01 (conf.zoom -. decr);
1314 if conf.zoom <= 1.0 then state.x <- 0;
1315 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1316 reshape conf.winw conf.winh;
1318 | '-' ->
1319 let ondone msg =
1320 state.text <- msg;
1322 enttext (Some ('-', "", None, optentry, ondone))
1324 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1325 state.x <- 0;
1326 conf.zoom <- 1.0;
1327 state.text <- "zoom is 100%";
1328 reshape conf.winw conf.winh
1330 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1331 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1332 if zoom < 1.0
1333 then (
1334 conf.zoom <- zoom;
1335 state.x <- 0;
1336 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1337 reshape conf.winw conf.winh;
1340 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1341 togglebirdseye ();
1342 reshape conf.winw conf.winh;
1344 | '0' .. '9' ->
1345 let ondone s =
1346 let n =
1347 try int_of_string s with exc ->
1348 state.text <- Printf.sprintf "bad integer `%s': %s"
1349 s (Printexc.to_string exc);
1352 if n >= 0
1353 then (
1354 addnav ();
1355 cbput state.hists.pag (string_of_int n);
1356 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1359 let pageentry text key =
1360 match Char.unsafe_chr key with
1361 | 'g' -> TEdone text
1362 | _ -> intentry text key
1364 let text = "x" in text.[0] <- c;
1365 enttext (Some (':', text, Some (onhist state.hists.pag),
1366 pageentry, ondone))
1368 | 'b' ->
1369 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1370 reshape conf.winw conf.winh;
1372 | 'l' ->
1373 conf.hlinks <- not conf.hlinks;
1374 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1375 Glut.postRedisplay ()
1377 | 'a' ->
1378 conf.autoscroll <- not conf.autoscroll
1380 | 'P' ->
1381 conf.presentation <- not conf.presentation;
1382 showtext ' ' ("presentation mode " ^
1383 if conf.presentation then "on" else "off");
1384 represent ()
1386 | 'f' ->
1387 begin match state.fullscreen with
1388 | None ->
1389 state.fullscreen <- Some (conf.winw, conf.winh);
1390 Glut.fullScreen ()
1391 | Some (w, h) ->
1392 state.fullscreen <- None;
1393 doreshape w h
1396 | 'g' ->
1397 gotoy_and_clear_text 0
1399 | 'n' ->
1400 search state.searchpattern true
1402 | 'p' | 'N' ->
1403 search state.searchpattern false
1405 | 't' ->
1406 begin match state.layout with
1407 | [] -> ()
1408 | l :: _ ->
1409 gotoy_and_clear_text (getpagey l.pageno)
1412 | ' ' ->
1413 begin match List.rev state.layout with
1414 | [] -> ()
1415 | l :: _ ->
1416 let pageno = min (l.pageno+1) (state.pagecount-1) in
1417 gotoy_and_clear_text (getpagey pageno)
1420 | '\127' ->
1421 begin match state.layout with
1422 | [] -> ()
1423 | l :: _ ->
1424 let pageno = max 0 (l.pageno-1) in
1425 gotoy_and_clear_text (getpagey pageno)
1428 | '=' ->
1429 let f (fn, ln) l =
1430 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1432 let fn, ln = List.fold_left f (-1, -1) state.layout in
1433 let s =
1434 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1435 let percent =
1436 if maxy <= 0
1437 then 100.
1438 else (100. *. (float state.y /. float maxy)) in
1439 if fn = ln
1440 then
1441 Printf.sprintf "Page %d of %d %.2f%%"
1442 (fn+1) state.pagecount percent
1443 else
1444 Printf.sprintf
1445 "Pages %d-%d of %d %.2f%%"
1446 (fn+1) (ln+1) state.pagecount percent
1448 showtext ' ' s;
1450 | 'w' ->
1451 begin match state.layout with
1452 | [] -> ()
1453 | l :: _ ->
1454 doreshape (l.pagew + conf.scrollw) l.pageh;
1455 Glut.postRedisplay ();
1458 | '\'' ->
1459 enterbookmarkmode ()
1461 | 'm' ->
1462 let ondone s =
1463 match state.layout with
1464 | l :: _ ->
1465 state.bookmarks <-
1466 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1467 :: state.bookmarks
1468 | _ -> ()
1470 enttext (Some ('~', "", None, textentry, ondone))
1472 | '~' ->
1473 quickbookmark ();
1474 showtext ' ' "Quick bookmark added";
1476 | 'z' ->
1477 begin match state.layout with
1478 | l :: _ ->
1479 let rect = getpdimrect l.pagedimno in
1480 let w, h =
1481 if conf.crophack
1482 then
1483 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1484 truncate (1.2 *. (rect.(3) -. rect.(0))))
1485 else
1486 (truncate (rect.(1) -. rect.(0)),
1487 truncate (rect.(3) -. rect.(0)))
1489 if w != 0 && h != 0
1490 then
1491 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1493 Glut.postRedisplay ();
1495 | [] -> ()
1498 | '<' | '>' ->
1499 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1501 | '[' | ']' ->
1502 state.colorscale <-
1503 max 0.0
1504 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1505 Glut.postRedisplay ()
1507 | 'k' -> gotoy (clamp (-conf.scrollincr))
1508 | 'j' -> gotoy (clamp conf.scrollincr)
1510 | 'r' -> opendoc state.path state.password
1512 | _ ->
1513 vlog "huh? %d %c" key (Char.chr key);
1516 | Some (c, text, opthist, onkey, ondone) when key = 8 ->
1517 let len = String.length text in
1518 if len = 0
1519 then (
1520 state.textentry <- None;
1521 Glut.postRedisplay ();
1523 else (
1524 let s = String.sub text 0 (len - 1) in
1525 enttext (Some (c, s, opthist, onkey, ondone))
1528 | Some (c, text, onhist, onkey, ondone) ->
1529 begin match Char.unsafe_chr key with
1530 | '\r' | '\n' ->
1531 ondone text;
1532 state.textentry <- None;
1533 Glut.postRedisplay ()
1535 | '\027' ->
1536 begin match onhist with
1537 | None -> ()
1538 | Some (_, onhistcancel) -> onhistcancel ()
1539 end;
1540 state.textentry <- None;
1541 Glut.postRedisplay ()
1543 | _ ->
1544 begin match onkey text key with
1545 | TEdone text ->
1546 state.textentry <- None;
1547 ondone text;
1548 Glut.postRedisplay ()
1550 | TEcont text ->
1551 enttext (Some (c, text, onhist, onkey, ondone));
1553 | TEstop ->
1554 state.textentry <- None;
1555 Glut.postRedisplay ()
1557 | TEswitch te ->
1558 state.textentry <- Some te;
1559 Glut.postRedisplay ()
1560 end;
1561 end;
1564 let narrow outlines pattern =
1565 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1566 match reopt with
1567 | None -> None
1568 | Some re ->
1569 let rec fold accu n =
1570 if n = -1
1571 then accu
1572 else
1573 let (s, _, _, _) as o = outlines.(n) in
1574 let accu =
1575 if (try ignore (Str.search_forward re s 0); true
1576 with Not_found -> false)
1577 then (o :: accu)
1578 else accu
1580 fold accu (n-1)
1582 let matched = fold [] (Array.length outlines - 1) in
1583 if matched = [] then None else Some (Array.of_list matched)
1586 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1587 let search active pattern incr =
1588 let dosearch re =
1589 let rec loop n =
1590 if n = Array.length outlines || n = -1
1591 then None
1592 else
1593 let (s, _, _, _) = outlines.(n) in
1595 (try ignore (Str.search_forward re s 0); true
1596 with Not_found -> false)
1597 then Some n
1598 else loop (n + incr)
1600 loop active
1603 let re = Str.regexp_case_fold pattern in
1604 dosearch re
1605 with Failure s ->
1606 state.text <- s;
1607 None
1609 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1610 match key with
1611 | 27 ->
1612 if String.length qsearch = 0
1613 then (
1614 state.text <- "";
1615 state.outline <- None;
1616 Glut.postRedisplay ();
1618 else (
1619 state.text <- "";
1620 state.outline <- Some (allowdel, active, first, outlines, "");
1621 Glut.postRedisplay ();
1624 | 18 | 19 ->
1625 let incr = if key = 18 then -1 else 1 in
1626 let active, first =
1627 match search (active + incr) qsearch incr with
1628 | None ->
1629 state.text <- qsearch ^ " [not found]";
1630 active, first
1631 | Some active ->
1632 state.text <- qsearch;
1633 active, firstof active
1635 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1636 Glut.postRedisplay ();
1638 | 8 ->
1639 let len = String.length qsearch in
1640 if len = 0
1641 then ()
1642 else (
1643 if len = 1
1644 then (
1645 state.text <- "";
1646 state.outline <- Some (allowdel, active, first, outlines, "");
1648 else
1649 let qsearch = String.sub qsearch 0 (len - 1) in
1650 let active, first =
1651 match search active qsearch ~-1 with
1652 | None ->
1653 state.text <- qsearch ^ " [not found]";
1654 active, first
1655 | Some active ->
1656 state.text <- qsearch;
1657 active, firstof active
1659 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1661 Glut.postRedisplay ()
1663 | 13 ->
1664 if active < Array.length outlines
1665 then (
1666 let (_, _, n, t) = outlines.(active) in
1667 gotopage n t;
1669 state.text <- "";
1670 if allowdel then state.bookmarks <- Array.to_list outlines;
1671 state.outline <- None;
1672 Glut.postRedisplay ();
1674 | _ when key >= 32 && key < 127 ->
1675 let pattern = addchar qsearch (Char.chr key) in
1676 let active, first =
1677 match search active pattern 1 with
1678 | None ->
1679 state.text <- pattern ^ " [not found]";
1680 active, first
1681 | Some active ->
1682 state.text <- pattern;
1683 active, firstof active
1685 state.outline <- Some (allowdel, active, first, outlines, pattern);
1686 Glut.postRedisplay ()
1688 | 14 when not allowdel -> (* ctrl-n *)
1689 if String.length qsearch > 0
1690 then (
1691 let optoutlines = narrow outlines qsearch in
1692 begin match optoutlines with
1693 | None -> state.text <- "can't narrow"
1694 | Some outlines ->
1695 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1696 match state.outlines with
1697 | Olist l -> ()
1698 | Oarray a ->
1699 state.outlines <- Onarrow (qsearch, outlines, a)
1700 | Onarrow (pat, a, b) ->
1701 state.outlines <- Onarrow (qsearch, outlines, b)
1702 end;
1704 Glut.postRedisplay ()
1706 | 21 when not allowdel -> (* ctrl-u *)
1707 let outline =
1708 match state.outlines with
1709 | Oarray a -> a
1710 | Olist l ->
1711 let a = Array.of_list (List.rev l) in
1712 state.outlines <- Oarray a;
1714 | Onarrow (pat, a, b) ->
1715 state.outlines <- Oarray b;
1716 state.text <- "";
1719 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1720 Glut.postRedisplay ()
1722 | 12 ->
1723 state.outline <-
1724 Some (allowdel, active, firstof active, outlines, qsearch);
1725 Glut.postRedisplay ()
1727 | 127 when allowdel ->
1728 let len = Array.length outlines - 1 in
1729 if len = 0
1730 then (
1731 state.outline <- None;
1732 state.bookmarks <- [];
1734 else (
1735 let bookmarks = Array.init len
1736 (fun i ->
1737 let i = if i >= active then i + 1 else i in
1738 outlines.(i)
1741 state.outline <-
1742 Some (allowdel,
1743 min active (len-1),
1744 min first (len-1),
1745 bookmarks, qsearch)
1748 Glut.postRedisplay ()
1750 | _ -> log "unknown key %d" key
1753 let keyboard ~key ~x ~y =
1754 if key = 7
1755 then
1756 wcmd "interrupt" []
1757 else
1758 match state.outline with
1759 | None -> viewkeyboard ~key ~x ~y
1760 | Some outline -> outlinekeyboard ~key ~x ~y outline
1763 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1764 match key with
1765 | Glut.KEY_UP ->
1766 let pageno = max 0 (pageno - 1) in
1767 let move =
1768 let rec loop = function
1769 | [] -> true
1770 | l :: rest ->
1771 if l.pageno = pageno
1772 then l.pagey != 0
1773 else loop rest
1775 loop state.layout
1777 if move
1778 then gotopage1 pageno 0
1779 else Glut.postRedisplay ();
1780 state.birdseye <- Some (conf, leftx, pageno, hooverpageno)
1782 | Glut.KEY_DOWN ->
1783 let pageno = min (state.pagecount - 1) (pageno + 1) in
1784 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1785 let rec loop = function
1786 | [] -> gotopage1 pageno 0
1787 | l :: rest ->
1788 if l.pageno = pageno
1789 then (
1790 if l.pagevh != l.pageh
1791 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1792 else Glut.postRedisplay ()
1794 else loop rest
1796 loop state.layout
1798 | Glut.KEY_PAGE_UP ->
1799 begin match state.layout with
1800 | l :: _ ->
1801 if l.pageno = pageno
1802 then (
1803 match layout (state.y - conf.winh) conf.winh with
1804 | [] -> gotoy (clamp (-conf.winh))
1805 | l :: _ ->
1806 let pageno = max 0 (l.pageno - 1) in
1807 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1808 gotopage pageno 0.0
1810 else (
1811 let pageno = max 0 (l.pageno - 1) in
1812 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1813 gotopage pageno 0.0
1815 | [] -> gotoy (clamp (-conf.winh))
1816 end;
1817 | Glut.KEY_PAGE_DOWN ->
1818 begin match List.rev state.layout with
1819 | l :: _ ->
1820 let pageno = min l.pageno (state.pagecount - 1) in
1821 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1822 gotoy (clamp (l.pagedispy + l.pageh))
1823 | [] -> gotoy (clamp conf.winh)
1824 end;
1826 | Glut.KEY_HOME ->
1827 state.birdseye <- Some (conf, leftx, 0, hooverpageno);
1828 gotopage 0 0.0
1829 | Glut.KEY_END ->
1830 let pageno = state.pagecount - 1 in
1831 state.birdseye <- Some (conf, leftx, pageno, hooverpageno);
1832 if not (pagevisible state.layout pageno)
1833 then
1834 let h =
1835 match List.rev state.pdims with
1836 | [] -> conf.winh
1837 | (_, _, h, _) :: _ -> h
1839 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1840 else Glut.postRedisplay ();
1841 | _ -> ()
1844 let special ~key ~x ~y =
1845 match state.outline, state.birdseye with
1846 | None, _ when key = Glut.KEY_F9 ->
1847 togglebirdseye ();
1848 reshape conf.winw conf.winh;
1850 | None, (Some vals) ->
1851 birdseyespecial key x y vals
1853 | None, None ->
1854 begin match state.textentry with
1855 | None ->
1856 let y =
1857 match key with
1858 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1859 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1860 | Glut.KEY_DOWN -> clamp conf.scrollincr
1861 | Glut.KEY_PAGE_UP ->
1862 if Glut.getModifiers () land Glut.active_ctrl != 0
1863 then
1864 match state.layout with
1865 | [] -> state.y
1866 | l :: _ -> state.y - l.pagey
1867 else
1868 clamp (-conf.winh)
1869 | Glut.KEY_PAGE_DOWN ->
1870 if Glut.getModifiers () land Glut.active_ctrl != 0
1871 then
1872 match List.rev state.layout with
1873 | [] -> state.y
1874 | l :: _ -> getpagey l.pageno
1875 else
1876 clamp conf.winh
1877 | Glut.KEY_HOME -> addnav (); 0
1878 | Glut.KEY_END ->
1879 addnav ();
1880 state.maxy - (if conf.maxhfit then conf.winh else 0)
1882 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1883 state.x <- state.x - 10;
1884 state.y
1885 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1886 state.x <- state.x + 10;
1887 state.y
1889 | _ -> state.y
1891 gotoy_and_clear_text y
1893 | Some (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1894 let s =
1895 match key with
1896 | Glut.KEY_UP -> action HCprev
1897 | Glut.KEY_DOWN -> action HCnext
1898 | Glut.KEY_HOME -> action HCfirst
1899 | Glut.KEY_END -> action HClast
1900 | _ -> state.text
1902 state.textentry <- Some (c, s, onhist, onkey, ondone);
1903 Glut.postRedisplay ()
1905 | _ -> ()
1908 | Some (allowdel, active, first, outlines, qsearch), _ ->
1909 let maxrows = maxoutlinerows () in
1910 let calcfirst first active =
1911 if active > first
1912 then
1913 let rows = active - first in
1914 if rows > maxrows then active - maxrows else first
1915 else active
1917 let navigate incr =
1918 let active = active + incr in
1919 let active = max 0 (min active (Array.length outlines - 1)) in
1920 let first = calcfirst first active in
1921 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1922 Glut.postRedisplay ()
1924 let updownlevel incr =
1925 let len = Array.length outlines in
1926 let (_, curlevel, _, _) = outlines.(active) in
1927 let rec flow i =
1928 if i = len then i-1 else if i = -1 then 0 else
1929 let (_, l, _, _) = outlines.(i) in
1930 if l != curlevel then i else flow (i+incr)
1932 let active = flow active in
1933 let first = calcfirst first active in
1934 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1935 Glut.postRedisplay ()
1937 match key with
1938 | Glut.KEY_UP -> navigate ~-1
1939 | Glut.KEY_DOWN -> navigate 1
1940 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1941 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1943 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1944 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1946 | Glut.KEY_HOME ->
1947 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1948 Glut.postRedisplay ()
1950 | Glut.KEY_END ->
1951 let active = Array.length outlines - 1 in
1952 let first = max 0 (active - maxrows) in
1953 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1954 Glut.postRedisplay ()
1956 | _ -> ()
1959 let drawplaceholder l =
1960 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1961 GlDraw.rect
1962 (float l.pagex, float l.pagedispy)
1963 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
1965 let x = float (if margin < 0 then -margin else l.pagex)
1966 and y = float (l.pagedispy + 13) in
1967 let font = Glut.BITMAP_8_BY_13 in
1968 GlDraw.color (0.0, 0.0, 0.0);
1969 GlPix.raster_pos ~x ~y ();
1970 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1971 ("Loading " ^ string_of_int (l.pageno + 1));
1974 let now () = Unix.gettimeofday ();;
1976 let drawpage l =
1977 if state.textentry = None
1978 then (
1979 match state.birdseye with
1980 | None -> GlDraw.color (scalecolor 1.0);
1981 | Some (_, _, pageno, hooverpageno) ->
1982 let color =
1983 if l.pageno = pageno
1984 then 1.0
1985 else (
1986 if l.pageno = hooverpageno
1987 then 0.9
1988 else 0.8
1991 GlDraw.color (scalecolor color);
1993 else GlDraw.color (scalecolor 0.4);
1994 begin match getopaque l.pageno with
1995 | Some (opaque, _) when validopaque opaque ->
1996 let a = now () in
1997 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1998 opaque;
1999 let b = now () in
2000 let d = b-.a in
2001 vlog "draw %d %f sec" l.pageno d;
2003 | _ ->
2004 drawplaceholder l;
2005 end;
2008 let scrollph y =
2009 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2010 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2011 let sh = float conf.winh /. sh in
2012 let sh = max sh (float conf.scrollh) in
2014 let percent =
2015 if state.y = state.maxy
2016 then 1.0
2017 else float y /. float maxy
2019 let position = (float conf.winh -. sh) *. percent in
2021 let position =
2022 if position +. sh > float conf.winh
2023 then float conf.winh -. sh
2024 else position
2026 position, sh;
2029 let scrollindicator () =
2030 GlDraw.color (0.64 , 0.64, 0.64);
2031 GlDraw.rect
2032 (float (conf.winw - conf.scrollw), 0.)
2033 (float conf.winw, float conf.winh)
2035 GlDraw.color (0.0, 0.0, 0.0);
2037 let position, sh = scrollph state.y in
2038 GlDraw.rect
2039 (float (conf.winw - conf.scrollw), position)
2040 (float conf.winw, position +. sh)
2044 let showsel margin =
2045 match state.mstate with
2046 | Mnone | Mscroll _ | Mpan _ ->
2049 | Msel ((x0, y0), (x1, y1)) ->
2050 let rec loop = function
2051 | l :: ls ->
2052 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2053 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2054 then
2055 match getopaque l.pageno with
2056 | Some (opaque, _) when validopaque opaque ->
2057 let oy = -l.pagey + l.pagedispy in
2058 seltext opaque
2059 (x0 - margin - state.x, y0,
2060 x1 - margin - state.x, y1) oy;
2062 | _ -> ()
2063 else loop ls
2064 | [] -> ()
2066 loop state.layout
2069 let showrects () =
2070 let panx = float state.x in
2071 Gl.enable `blend;
2072 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2073 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2074 List.iter
2075 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2076 List.iter (fun l ->
2077 if l.pageno = pageno
2078 then (
2079 let d = float (l.pagedispy - l.pagey) in
2080 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2081 GlDraw.begins `quads;
2083 GlDraw.vertex2 (x0+.panx, y0+.d);
2084 GlDraw.vertex2 (x1+.panx, y1+.d);
2085 GlDraw.vertex2 (x2+.panx, y2+.d);
2086 GlDraw.vertex2 (x3+.panx, y3+.d);
2088 GlDraw.ends ();
2090 ) state.layout
2091 ) state.rects
2093 Gl.disable `blend;
2096 let showoutline = function
2097 | None -> ()
2098 | Some (allowdel, active, first, outlines, qsearch) ->
2099 Gl.enable `blend;
2100 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2101 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2102 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2103 Gl.disable `blend;
2105 GlDraw.color (1., 1., 1.);
2106 let font = Glut.BITMAP_9_BY_15 in
2107 let draw_string x y s =
2108 GlPix.raster_pos ~x ~y ();
2109 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2111 let rec loop row =
2112 if row = Array.length outlines || (row - first) * 16 > conf.winh
2113 then ()
2114 else (
2115 let (s, l, _, _) = outlines.(row) in
2116 let y = (row - first) * 16 in
2117 let x = 5 + 15*l in
2118 if row = active
2119 then (
2120 Gl.enable `blend;
2121 GlDraw.polygon_mode `both `line;
2122 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2123 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2124 GlDraw.rect (0., float (y + 1))
2125 (float (conf.winw - 1), float (y + 18));
2126 GlDraw.polygon_mode `both `fill;
2127 Gl.disable `blend;
2128 GlDraw.color (1., 1., 1.);
2130 draw_string (float x) (float (y + 16)) s;
2131 loop (row+1)
2134 loop first
2137 let display () =
2138 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2139 GlDraw.viewport margin 0 state.w conf.winh;
2140 pagematrix ();
2141 GlClear.color (scalecolor 0.5);
2142 GlClear.clear [`color];
2143 if state.x != 0
2144 then (
2145 let x = float state.x in
2146 GlMat.translate ~x ();
2148 if conf.zoom > 1.0
2149 then (
2150 Gl.enable `scissor_test;
2151 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2153 List.iter drawpage state.layout;
2154 if conf.zoom > 1.0
2155 then
2156 Gl.disable `scissor_test
2158 if state.x != 0
2159 then (
2160 let x = -.float state.x in
2161 GlMat.translate ~x ();
2163 showrects ();
2164 showsel margin;
2165 GlDraw.viewport 0 0 conf.winw conf.winh;
2166 winmatrix ();
2167 scrollindicator ();
2168 showoutline state.outline;
2169 enttext ();
2170 Glut.swapBuffers ();
2173 let getunder x y =
2174 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2175 let x = x - margin - state.x in
2176 let rec f = function
2177 | l :: rest ->
2178 begin match getopaque l.pageno with
2179 | Some (opaque, _) when validopaque opaque ->
2180 let y = y - l.pagedispy in
2181 if y > 0
2182 then
2183 let y = l.pagey + y in
2184 let x = x - l.pagex in
2185 match whatsunder opaque x y with
2186 | Unone -> f rest
2187 | under -> under
2188 else
2189 f rest
2190 | _ ->
2191 f rest
2193 | [] -> Unone
2195 f state.layout
2198 let mouse ~button ~bstate ~x ~y =
2199 match button with
2200 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2201 let incr =
2202 if n = 3
2203 then
2204 -conf.scrollincr
2205 else
2206 conf.scrollincr
2208 let incr = incr * 2 in
2209 let y = clamp incr in
2210 gotoy_and_clear_text y
2212 | Glut.LEFT_BUTTON when state.outline = None
2213 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2214 if bstate = Glut.DOWN
2215 then (
2216 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2217 state.mstate <- Mpan (x, y)
2219 else
2220 state.mstate <- Mnone
2222 | Glut.LEFT_BUTTON
2223 when state.outline = None && x > conf.winw - conf.scrollw ->
2224 if bstate = Glut.DOWN
2225 then
2226 let position, sh = scrollph state.y in
2227 if y > truncate position && y < truncate (position +. sh)
2228 then
2229 state.mstate <- Mscroll
2230 else
2231 let percent = float y /. float conf.winh in
2232 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2233 gotoy desty;
2234 state.mstate <- Mscroll
2235 else
2236 state.mstate <- Mnone
2238 | Glut.LEFT_BUTTON when state.outline = None && state.birdseye <> None ->
2239 begin match state.birdseye with
2240 | Some (conf, leftx, pageno, hooverpageno) ->
2241 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2242 let rec loop = function
2243 | [] -> ()
2244 | l :: rest ->
2245 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2246 && x > margin && x < margin + l.pagew
2247 then (
2248 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2249 reshape conf.winw conf.winh;
2250 state.anchor <- (l.pageno, 0.0);
2252 else loop rest
2254 loop state.layout;
2255 | None -> () (* impossible *)
2258 | Glut.LEFT_BUTTON when state.outline = None ->
2259 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2260 begin match dest with
2261 | Ulinkgoto (pageno, top) ->
2262 if pageno >= 0
2263 then
2264 gotopage1 pageno top
2266 | Ulinkuri s ->
2267 print_endline s
2269 | Unone when bstate = Glut.DOWN ->
2270 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2271 state.mstate <- Mpan (x, y);
2273 | Unone | Utext _ ->
2274 if bstate = Glut.DOWN
2275 then (
2276 if conf.angle mod 360 = 0
2277 then (
2278 state.mstate <- Msel ((x, y), (x, y));
2279 Glut.postRedisplay ()
2282 else (
2283 match state.mstate with
2284 | Mnone -> ()
2286 | Mscroll ->
2287 state.mstate <- Mnone
2289 | Mpan _ ->
2290 Glut.setCursor Glut.CURSOR_INHERIT;
2291 state.mstate <- Mnone
2293 | Msel ((x0, y0), (x1, y1)) ->
2294 let f l =
2295 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2296 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2297 then
2298 match getopaque l.pageno with
2299 | Some (opaque, _) when validopaque opaque ->
2300 copysel opaque
2301 | _ -> ()
2303 List.iter f state.layout;
2304 copysel ""; (* ugly *)
2305 Glut.setCursor Glut.CURSOR_INHERIT;
2306 state.mstate <- Mnone;
2310 | _ ->
2313 let mouse ~button ~state ~x ~y = mouse button state x y;;
2315 let motion ~x ~y =
2316 if state.outline = None
2317 then
2318 match state.mstate with
2319 | Mnone -> ()
2321 | Mpan (x0, y0) ->
2322 let dx = x - x0
2323 and dy = y0 - y in
2324 state.mstate <- Mpan (x, y);
2325 if conf.zoom > 1.0 then state.x <- state.x + dx;
2326 let y = clamp dy in
2327 gotoy_and_clear_text y
2329 | Msel (a, _) ->
2330 state.mstate <- Msel (a, (x, y));
2331 Glut.postRedisplay ()
2333 | Mscroll ->
2334 let y = min conf.winh (max 0 y) in
2335 let percent = float y /. float conf.winh in
2336 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2337 gotoy_and_clear_text y
2340 let pmotion ~x ~y =
2341 match state.birdseye with
2342 | Some (conf, leftx, pageno, hooverpageno) ->
2343 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2344 let rec loop = function
2345 | [] ->
2346 if hooverpageno != -1
2347 then (
2348 state.birdseye <- Some (conf, leftx, pageno, -1);
2349 Glut.postRedisplay ();
2351 | l :: rest ->
2352 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2353 && x > margin && x < margin + l.pagew
2354 then (
2355 state.birdseye <- Some (conf, leftx, pageno, l.pageno);
2356 Glut.postRedisplay ();
2358 else loop rest
2360 loop state.layout
2362 | None ->
2363 if state.outline = None
2364 then
2365 match state.mstate with
2366 | Mnone ->
2367 begin match getunder x y with
2368 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2369 | Ulinkuri uri ->
2370 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2371 Glut.setCursor Glut.CURSOR_INFO
2372 | Ulinkgoto (page, y) ->
2373 if conf.underinfo
2374 then showtext 'p' ("age: " ^ string_of_int page);
2375 Glut.setCursor Glut.CURSOR_INFO
2376 | Utext s ->
2377 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2378 Glut.setCursor Glut.CURSOR_TEXT
2381 | Mpan _ | Msel _ | Mscroll ->
2386 module State =
2387 struct
2388 open Parser
2390 let home =
2392 match Sys.os_type with
2393 | "Win32" -> Sys.getenv "HOMEPATH"
2394 | _ -> Sys.getenv "HOME"
2395 with exn ->
2396 prerr_endline
2397 ("Can not determine home directory location: " ^
2398 Printexc.to_string exn);
2402 let config_of c attrs =
2403 let apply c k v =
2405 match k with
2406 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2407 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2408 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2409 | "preload" -> { c with preload = bool_of_string v }
2410 | "page-bias" -> { c with pagebias = int_of_string v }
2411 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2412 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2413 | "crop-hack" -> { c with crophack = bool_of_string v }
2414 | "throttle" -> { c with showall = bool_of_string v }
2415 | "highlight-links" -> { c with hlinks = bool_of_string v }
2416 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2417 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2418 | "zoom" ->
2419 let zoom = float_of_string v /. 100. in
2420 let zoom = max 0.01 (min 2.2 zoom) in
2421 { c with zoom = zoom }
2422 | "presentation" -> { c with presentation = bool_of_string v }
2423 | "rotation-angle" -> { c with angle = int_of_string v }
2424 | "width" -> { c with winw = max 20 (int_of_string v) }
2425 | "height" -> { c with winh = max 20 (int_of_string v) }
2426 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2427 | "proportional-display" -> { c with proportional = bool_of_string v }
2428 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2429 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2430 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2431 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2432 | _ -> c
2433 with exn ->
2434 prerr_endline ("Error processing attribute (`" ^
2435 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2438 let rec fold c = function
2439 | [] -> c
2440 | (k, v) :: rest ->
2441 let c = apply c k v in
2442 fold c rest
2444 fold c attrs;
2447 let bookmark_of attrs =
2448 let rec fold title page rely = function
2449 | ("title", v) :: rest -> fold v page rely rest
2450 | ("page", v) :: rest -> fold title v rely rest
2451 | ("rely", v) :: rest -> fold title page v rest
2452 | _ :: rest -> fold title page rely rest
2453 | [] -> title, page, rely
2455 fold "invalid" "0" "0" attrs
2458 let setconf dst src =
2459 dst.scrollw <- src.scrollw;
2460 dst.scrollh <- src.scrollh;
2461 dst.icase <- src.icase;
2462 dst.preload <- src.preload;
2463 dst.pagebias <- src.pagebias;
2464 dst.verbose <- src.verbose;
2465 dst.scrollincr <- src.scrollincr;
2466 dst.maxhfit <- src.maxhfit;
2467 dst.crophack <- src.crophack;
2468 dst.autoscroll <- src.autoscroll;
2469 dst.showall <- src.showall;
2470 dst.hlinks <- src.hlinks;
2471 dst.underinfo <- src.underinfo;
2472 dst.interpagespace <- src.interpagespace;
2473 dst.zoom <- src.zoom;
2474 dst.presentation <- src.presentation;
2475 dst.angle <- src.angle;
2476 dst.winw <- src.winw;
2477 dst.winh <- src.winh;
2478 dst.savebmarks <- src.savebmarks;
2479 dst.memlimit <- src.memlimit;
2480 dst.proportional <- src.proportional;
2481 dst.texcount <- src.texcount;
2482 dst.sliceheight <- src.sliceheight;
2483 dst.thumbw <- src.thumbw;
2486 let unent s =
2487 let l = String.length s in
2488 let b = Buffer.create l in
2489 unent b s 0 l;
2490 Buffer.contents b;
2493 let get s =
2494 let h = Hashtbl.create 10 in
2495 let dc = { defconf with angle = defconf.angle } in
2496 let rec toplevel v t spos epos =
2497 match t with
2498 | Vdata | Vcdata | Vend -> v
2499 | Vopen ("llppconfig", attrs, closed) ->
2500 if closed
2501 then v
2502 else { v with f = llppconfig }
2503 | Vopen _ ->
2504 error "unexpected subelement at top level" s spos
2505 | Vclose tag -> error "unexpected close at top level" s spos
2507 and llppconfig v t spos epos =
2508 match t with
2509 | Vdata | Vcdata | Vend -> v
2510 | Vopen ("defaults", attrs, closed) ->
2511 let c = config_of dc attrs in
2512 setconf dc c;
2513 if closed
2514 then v
2515 else { v with f = skip "defaults" (fun () -> v) }
2517 | Vopen ("doc", attrs, closed) ->
2518 let pathent =
2520 List.assoc "path" attrs
2521 with Not_found -> error "doc is missing path attribute" s spos
2523 let path = unent pathent in
2524 let c = config_of dc attrs in
2525 let pageno, rely, x =
2526 let safef f n v d =
2527 try f v
2528 with exn ->
2529 dolog "error accessing %s (%S) at postion %d:\n %s"
2530 n v spos (Printexc.to_string exn);
2533 let rec fold pageno rely x = function
2534 | [] -> pageno, rely, x
2535 | ("rely", v) :: rest ->
2536 fold pageno (safef float_of_string "rely" v 0.0) x rest
2537 | ("page", v) :: rest ->
2538 fold (safef int_of_string "page" v 0) rely x rest
2539 | ("x", v) :: rest ->
2540 fold pageno rely (safef int_of_string "x" v 0) rest
2541 | _ :: rest ->
2542 fold pageno rely x rest
2544 fold 0 0.0 0 attrs
2546 let anchor = (pageno, rely) in
2547 if closed
2548 then (Hashtbl.add h path (c, [], x, anchor); v)
2549 else { v with f = doc path x anchor c [] }
2551 | Vopen (tag, _, closed) ->
2552 error "unexpected subelement in llppconfig" s spos
2554 | Vclose "llppconfig" -> { v with f = toplevel }
2555 | Vclose tag -> error "unexpected close in llppconfig" s spos
2557 and doc path x anchor c bookmarks v t spos epos =
2558 match t with
2559 | Vdata | Vcdata -> v
2560 | Vend -> error "unexpected end of input in doc" s spos
2561 | Vopen ("bookmarks", attrs, closed) ->
2562 { v with f = pbookmarks path x anchor c bookmarks }
2564 | Vopen (tag, _, _) ->
2565 error "unexpected subelement in doc" s spos
2567 | Vclose "doc" ->
2568 Hashtbl.add h path (c, List.rev bookmarks, x, anchor);
2569 { v with f = llppconfig }
2571 | Vclose tag -> error "unexpected close in doc" s spos
2573 and pbookmarks path x anchor c bookmarks v t spos epos =
2574 match t with
2575 | Vdata | Vcdata -> v
2576 | Vend -> error "unexpected end of input in bookmarks" s spos
2577 | Vopen ("item", attrs, closed) ->
2578 let titleent, spage, srely = bookmark_of attrs in
2579 let page =
2581 int_of_string spage
2582 with exn ->
2583 dolog "Failed to convert page %S to integer: %s"
2584 spage (Printexc.to_string exn);
2587 let rely =
2589 float_of_string srely
2590 with exn ->
2591 dolog "Failed to convert rely %S to real: %s"
2592 srely (Printexc.to_string exn);
2595 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2596 if closed
2597 then { v with f = pbookmarks path x anchor c bookmarks }
2598 else
2599 let f () = v in
2600 { v with f = skip "item" f }
2602 | Vopen _ ->
2603 error "unexpected subelement in bookmarks" s spos
2605 | Vclose "bookmarks" ->
2606 { v with f = doc path x anchor c bookmarks }
2608 | Vclose tag -> error "unexpected close in bookmarks" s spos
2610 and skip tag f v t spos epos =
2611 match t with
2612 | Vdata | Vcdata -> v
2613 | Vend ->
2614 error ("unexpected end of input in skipped " ^ tag) s spos
2615 | Vopen (tag', _, closed) ->
2616 if closed
2617 then v
2618 else
2619 let f' () = { v with f = skip tag f } in
2620 { v with f = skip tag' f' }
2621 | Vclose ctag ->
2622 if tag = ctag
2623 then f ()
2624 else error ("unexpected close in skipped " ^ tag) s spos
2627 parse { f = toplevel; accu = () } s;
2628 h, dc;
2631 let do_load f ic =
2633 let len = in_channel_length ic in
2634 let s = String.create len in
2635 really_input ic s 0 len;
2636 f s;
2637 with
2638 | Parse_error (msg, s, pos) ->
2639 let subs = subs s pos in
2640 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2641 failwith ("parse error: " ^ s)
2643 | exn ->
2644 failwith ("config load error: " ^ Printexc.to_string exn)
2647 let path =
2648 let dir =
2650 let dir = Filename.concat home ".config" in
2651 if Sys.is_directory dir then dir else home
2652 with _ -> home
2654 Filename.concat dir "llpp.conf"
2657 let load1 f =
2658 if Sys.file_exists path
2659 then
2660 match
2661 (try Some (open_in_bin path)
2662 with exn ->
2663 prerr_endline
2664 ("Error opening configuation file `" ^ path ^ "': " ^
2665 Printexc.to_string exn);
2666 None
2668 with
2669 | Some ic ->
2670 begin try
2671 f (do_load get ic)
2672 with exn ->
2673 prerr_endline
2674 ("Error loading configuation from `" ^ path ^ "': " ^
2675 Printexc.to_string exn);
2676 end;
2677 close_in ic;
2679 | None -> ()
2680 else
2681 f (Hashtbl.create 0, defconf)
2684 let load () =
2685 let f (h, dc) =
2686 let pc, pb, px, pa =
2688 Hashtbl.find h state.path
2689 with Not_found -> dc, [], 0, (0, 0.0)
2691 setconf defconf dc;
2692 setconf conf pc;
2693 state.bookmarks <- pb;
2694 state.x <- px;
2695 cbput state.hists.nav pa;
2697 load1 f
2700 let add_attrs bb always dc c =
2701 let ob s a b =
2702 if always || a != b
2703 then Printf.bprintf bb "\n %s='%b'" s a
2704 and oi s a b =
2705 if always || a != b
2706 then Printf.bprintf bb "\n %s='%d'" s a
2707 and oz s a b =
2708 if always || a <> b
2709 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2711 let w, h =
2712 if always
2713 then dc.winw, dc.winh
2714 else
2715 match state.fullscreen with
2716 | Some wh -> wh
2717 | None -> c.winw, c.winh
2719 let zoom, presentation, interpagespace, showall=
2720 if always
2721 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2722 else
2723 match state.birdseye with
2724 | Some (bc, _, _, _) ->
2725 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2726 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2728 oi "width" w dc.winw;
2729 oi "height" h dc.winh;
2730 oi "scroll-bar-width" c.scrollw dc.scrollw;
2731 oi "scroll-handle-height" c.scrollh dc.scrollh;
2732 ob "case-insensitive-search" c.icase dc.icase;
2733 ob "preload" c.preload dc.preload;
2734 oi "page-bias" c.pagebias dc.pagebias;
2735 oi "scroll-step" c.scrollincr dc.scrollincr;
2736 ob "max-height-fit" c.maxhfit dc.maxhfit;
2737 ob "crop-hack" c.crophack dc.crophack;
2738 ob "throttle" showall dc.showall;
2739 ob "highlight-links" c.hlinks dc.hlinks;
2740 ob "under-cursor-info" c.underinfo dc.underinfo;
2741 oi "vertical-margin" interpagespace dc.interpagespace;
2742 oz "zoom" zoom dc.zoom;
2743 ob "presentation" presentation dc.presentation;
2744 oi "rotation-angle" c.angle dc.angle;
2745 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2746 ob "proportional-display" c.proportional dc.proportional;
2747 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2748 oi "texcount" c.texcount dc.texcount;
2749 oi "slice-height" c.sliceheight dc.sliceheight;
2750 oi "thumbnail-width" c.thumbw dc.thumbw;
2753 let save () =
2754 let bb = Buffer.create 32768 in
2755 let f (h, dc) =
2756 Buffer.add_string bb "<llppconfig>\n<defaults ";
2757 add_attrs bb true dc dc;
2758 Buffer.add_string bb "/>\n";
2760 let adddoc path x anchor c bookmarks =
2761 if bookmarks == [] && c = dc && anchor == emptyanchor
2762 then ()
2763 else (
2764 Printf.bprintf bb "<doc path='%s'"
2765 (enent path 0 (String.length path));
2767 if anchor != emptyanchor
2768 then (
2769 let n, y = anchor in
2770 Printf.bprintf bb " page='%d'" n;
2771 Printf.bprintf bb " rely='%f'" y;
2774 if x != 0
2775 then Printf.bprintf bb " pan='%d'" x;
2777 add_attrs bb false dc c;
2779 begin match bookmarks with
2780 | [] -> Buffer.add_string bb "/>\n"
2781 | _ ->
2782 Buffer.add_string bb ">\n<bookmarks>\n";
2783 List.iter (fun (title, _level, page, rely) ->
2784 Printf.bprintf bb
2785 "<item title='%s' page='%d' rely='%f'/>\n"
2786 (enent title 0 (String.length title))
2787 page
2788 rely
2789 ) bookmarks;
2790 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2791 end;
2795 let x =
2796 match state.birdseye with
2797 | Some (_, x, _, _) -> x
2798 | None -> state.x
2800 adddoc state.path x (getanchor ()) conf
2801 (if conf.savebmarks then state.bookmarks else []);
2803 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2804 if path <> state.path
2805 then
2806 adddoc path x y c bookmarks
2807 ) h;
2808 Buffer.add_string bb "</llppconfig>";
2810 load1 f;
2811 if Buffer.length bb > 0
2812 then
2814 let tmp = path ^ ".tmp" in
2815 let oc = open_out_bin tmp in
2816 Buffer.output_buffer oc bb;
2817 close_out oc;
2818 Sys.rename tmp path;
2819 with exn ->
2820 prerr_endline
2821 ("error while saving configuration: " ^ Printexc.to_string exn)
2823 end;;
2825 let () =
2826 Arg.parse
2827 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2828 (fun s -> state.path <- s)
2829 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2831 let path =
2832 if String.length state.path = 0
2833 then (prerr_endline "filename missing"; exit 1)
2834 else (
2835 if Filename.is_relative state.path
2836 then Filename.concat (Sys.getcwd ()) state.path
2837 else state.path
2840 state.path <- path;
2842 State.load ();
2844 let _ = Glut.init Sys.argv in
2845 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2846 let () = Glut.initWindowSize conf.winw conf.winh in
2847 let _ = Glut.createWindow ("llpp " ^ Filename.basename path) in
2849 let csock, ssock =
2850 if Sys.os_type = "Unix"
2851 then
2852 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2853 else
2854 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2855 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2856 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2857 Unix.bind sock addr;
2858 Unix.listen sock 1;
2859 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2860 Unix.connect csock addr;
2861 let ssock, _ = Unix.accept sock in
2862 Unix.close sock;
2863 let opts sock =
2864 Unix.setsockopt sock Unix.TCP_NODELAY true;
2865 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2867 opts ssock;
2868 opts csock;
2869 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2870 ssock, csock
2873 let () = Glut.displayFunc display in
2874 let () = Glut.reshapeFunc reshape in
2875 let () = Glut.keyboardFunc keyboard in
2876 let () = Glut.specialFunc special in
2877 let () = Glut.idleFunc (Some idle) in
2878 let () = Glut.mouseFunc mouse in
2879 let () = Glut.motionFunc motion in
2880 let () = Glut.passiveMotionFunc pmotion in
2882 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2883 state.csock <- csock;
2884 state.ssock <- ssock;
2885 state.text <- "Opening " ^ path;
2886 writeopen state.path state.password;
2888 at_exit State.save;
2890 let rec handlelablglutbug () =
2892 Glut.mainLoop ();
2893 with Glut.BadEnum "key in special_of_int" ->
2894 showtext '!' " LablGlut bug: special key not recognized";
2895 handlelablglutbug ()
2897 handlelablglutbug ();