Make preloading slightly more aggressive
[llpp.git] / main.ml
blob98dca0acbffd9ab4c6c107f0d852c96b29e9c180
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params =
11 angle * proportional * texcount * sliceheight * blockwidth * fontpath
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 blockwidth = int
25 and gen = int
26 and top = float
27 and fontpath = string
30 external init : Unix.file_descr -> params -> unit = "ml_init";;
31 external draw : (int * int * int * bool) -> string -> unit = "ml_draw";;
32 external seltext : string -> (int * int * int * int) -> int -> unit =
33 "ml_seltext";;
34 external copysel : string -> unit = "ml_copysel";;
35 external getpdimrect : int -> float array = "ml_getpdimrect";;
36 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
37 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
38 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
39 external measurestr : int -> string -> float = "ml_measure_string";;
40 external getmaxw : unit -> float = "ml_getmaxw";;
42 type mpos = int * int
43 and mstate =
44 | Msel of (mpos * mpos)
45 | Mpan of mpos
46 | Mscroll
47 | Mzoom of (int * int)
48 | Mnone
51 type textentry = string * string * onhist option * onkey * ondone
52 and onkey = string -> int -> te
53 and ondone = string -> unit
54 and histcancel = unit -> unit
55 and onhist = ((histcmd -> string) * histcancel)
56 and histcmd = HCnext | HCprev | HCfirst | HClast
57 and te =
58 | TEstop
59 | TEdone of string
60 | TEcont of string
61 | TEswitch of textentry
64 type 'a circbuf =
65 { store : 'a array
66 ; mutable rc : int
67 ; mutable wc : int
68 ; mutable len : int
72 let cbnew n v =
73 { store = Array.create n v
74 ; rc = 0
75 ; wc = 0
76 ; len = 0
80 let cbcap b = Array.length b.store;;
82 let cbput b v =
83 let cap = cbcap b in
84 b.store.(b.wc) <- v;
85 b.wc <- (b.wc + 1) mod cap;
86 b.rc <- b.wc;
87 b.len <- min (b.len + 1) cap;
90 let cbempty b = b.len = 0;;
92 let cbgetg b circular dir =
93 if cbempty b
94 then b.store.(0)
95 else
96 let rc = b.rc + dir in
97 let rc =
98 if circular
99 then (
100 if rc = -1
101 then b.len-1
102 else (
103 if rc = b.len
104 then 0
105 else rc
108 else max 0 (min rc (b.len-1))
110 b.rc <- rc;
111 b.store.(rc);
114 let cbget b = cbgetg b false;;
115 let cbgetc b = cbgetg b true;;
117 let cbpeek b =
118 let rc = b.wc - b.len in
119 let rc = if rc < 0 then cbcap b + rc else rc in
120 b.store.(rc);
123 let cbdecr b = b.len <- b.len - 1;;
125 type layout =
126 { pageno : int
127 ; pagedimno : int
128 ; pagew : int
129 ; pageh : int
130 ; pagedispy : int
131 ; pagey : int
132 ; pagevh : int
133 ; pagex : int
137 type conf =
138 { mutable scrollbw : int
139 ; mutable scrollh : int
140 ; mutable icase : bool
141 ; mutable preload : bool
142 ; mutable pagebias : int
143 ; mutable verbose : bool
144 ; mutable scrollstep : int
145 ; mutable maxhfit : bool
146 ; mutable crophack : bool
147 ; mutable autoscrollstep : int
148 ; mutable showall : bool
149 ; mutable hlinks : bool
150 ; mutable underinfo : bool
151 ; mutable interpagespace : interpagespace
152 ; mutable zoom : float
153 ; mutable presentation : bool
154 ; mutable angle : angle
155 ; mutable winw : int
156 ; mutable winh : int
157 ; mutable savebmarks : bool
158 ; mutable proportional : proportional
159 ; mutable memlimit : int
160 ; mutable texcount : texcount
161 ; mutable sliceheight : sliceheight
162 ; mutable blockwidth : blockwidth
163 ; mutable thumbw : width
164 ; mutable jumpback : bool
165 ; mutable bgcolor : float * float * float
166 ; mutable bedefault : bool
167 ; mutable scrollbarinpm : bool
168 ; mutable uifont : string
172 type anchor = pageno * top;;
174 type outline = string * int * anchor
175 and outlines =
176 | Oarray of outline array
177 | Olist of outline list
178 | Onarrow of string * outline array * outline array
181 type rect = float * float * float * float * float * float * float * float;;
183 type pagemapkey = pageno * width * angle * proportional * gen;;
185 let emptyanchor = (0, 0.0);;
187 type mode =
188 | Birdseye of (conf * leftx * pageno * pageno * anchor)
189 | Outline of (bool * int * int * outline array * string * int * mode)
190 | Items of (int * int * item array * string * int * mode)
191 | Textentry of (textentry * onleave)
192 | View
193 and onleave = leavetextentrystatus -> unit
194 and leavetextentrystatus = | Cancel | Confirm
195 and item = string * int * action
196 and action =
197 | Noaction
198 | Action of (int -> int -> string -> int -> mode)
201 let isbirdseye = function Birdseye _ -> true | _ -> false;;
202 let istextentry = function Textentry _ -> true | _ -> false;;
204 type state =
205 { mutable csock : Unix.file_descr
206 ; mutable ssock : Unix.file_descr
207 ; mutable w : int
208 ; mutable x : int
209 ; mutable y : int
210 ; mutable scrollw : int
211 ; mutable anchor : anchor
212 ; mutable maxy : int
213 ; mutable layout : layout list
214 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
215 ; mutable pdims : (pageno * width * height * leftx) list
216 ; mutable pagecount : int
217 ; pagecache : string circbuf
218 ; mutable rendering : bool
219 ; mutable mstate : mstate
220 ; mutable searchpattern : string
221 ; mutable rects : (pageno * recttype * rect) list
222 ; mutable rects1 : (pageno * recttype * rect) list
223 ; mutable text : string
224 ; mutable fullscreen : (width * height) option
225 ; mutable mode : mode
226 ; mutable outlines : outlines
227 ; mutable bookmarks : outline list
228 ; mutable path : string
229 ; mutable password : string
230 ; mutable invalidated : int
231 ; mutable colorscale : float
232 ; mutable memused : int
233 ; mutable gen : gen
234 ; mutable throttle : layout list option
235 ; mutable autoscroll :int option
236 ; mutable help : item array
237 ; mutable docinfo : (int * string) list
238 ; mutable deadline : float
239 ; hists : hists
241 and hists =
242 { pat : string circbuf
243 ; pag : string circbuf
244 ; nav : anchor circbuf
248 let defconf =
249 { scrollbw = 7
250 ; scrollh = 12
251 ; icase = true
252 ; preload = true
253 ; pagebias = 0
254 ; verbose = false
255 ; scrollstep = 24
256 ; maxhfit = true
257 ; crophack = false
258 ; autoscrollstep = 2
259 ; showall = false
260 ; hlinks = false
261 ; underinfo = false
262 ; interpagespace = 2
263 ; zoom = 1.0
264 ; presentation = false
265 ; angle = 0
266 ; winw = 900
267 ; winh = 900
268 ; savebmarks = true
269 ; proportional = true
270 ; memlimit = 32*1024*1024
271 ; texcount = 256
272 ; sliceheight = 24
273 ; blockwidth = 2048
274 ; thumbw = 76
275 ; jumpback = false
276 ; bgcolor = (0.5, 0.5, 0.5)
277 ; bedefault = false
278 ; scrollbarinpm = true
279 ; uifont = ""
283 let conf = { defconf with angle = defconf.angle };;
285 let makehelp () =
286 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
287 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
290 let state =
291 { csock = Unix.stdin
292 ; ssock = Unix.stdin
293 ; x = 0
294 ; y = 0
295 ; w = 0
296 ; scrollw = 0
297 ; anchor = emptyanchor
298 ; layout = []
299 ; maxy = max_int
300 ; pagemap = Hashtbl.create 10
301 ; pagecache = cbnew 100 ""
302 ; pdims = []
303 ; pagecount = 0
304 ; rendering = false
305 ; mstate = Mnone
306 ; rects = []
307 ; rects1 = []
308 ; text = ""
309 ; mode = View
310 ; fullscreen = None
311 ; searchpattern = ""
312 ; outlines = Olist []
313 ; bookmarks = []
314 ; path = ""
315 ; password = ""
316 ; invalidated = 0
317 ; hists =
318 { nav = cbnew 100 (0, 0.0)
319 ; pat = cbnew 20 ""
320 ; pag = cbnew 10 ""
322 ; colorscale = 1.0
323 ; memused = 0
324 ; gen = 0
325 ; throttle = None
326 ; autoscroll = None
327 ; help = makehelp ()
328 ; docinfo = []
329 ; deadline = nan
333 let vlog fmt =
334 if conf.verbose
335 then
336 Printf.kprintf prerr_endline fmt
337 else
338 Printf.kprintf ignore fmt
341 let writecmd fd s =
342 let len = String.length s in
343 let n = 4 + len in
344 let b = Buffer.create n in
345 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
346 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
347 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
348 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
349 Buffer.add_string b s;
350 let s' = Buffer.contents b in
351 let n' = Unix.write fd s' 0 n in
352 if n' != n then failwith "write failed";
355 let readcmd fd =
356 let s = "xxxx" in
357 let n = Unix.read fd s 0 4 in
358 if n != 4 then failwith "incomplete read(len)";
359 let len = 0
360 lor (Char.code s.[0] lsl 24)
361 lor (Char.code s.[1] lsl 16)
362 lor (Char.code s.[2] lsl 8)
363 lor (Char.code s.[3] lsl 0)
365 let s = String.create len in
366 let n = Unix.read fd s 0 len in
367 if n != len then failwith "incomplete read(data)";
371 let makecmd s l =
372 let b = Buffer.create 10 in
373 Buffer.add_string b s;
374 let rec combine = function
375 | [] -> b
376 | x :: xs ->
377 Buffer.add_char b ' ';
378 let s =
379 match x with
380 | `b b -> if b then "1" else "0"
381 | `s s -> s
382 | `i i -> string_of_int i
383 | `f f -> string_of_float f
384 | `I f -> string_of_int (truncate f)
386 Buffer.add_string b s;
387 combine xs;
389 combine l;
392 let wcmd s l =
393 let cmd = Buffer.contents (makecmd s l) in
394 writecmd state.csock cmd;
397 let calcips h =
398 if conf.presentation
399 then
400 let d = conf.winh - h in
401 max 0 ((d + 1) / 2)
402 else
403 conf.interpagespace
406 let calcheight () =
407 let rec f pn ph pi fh l =
408 match l with
409 | (n, _, h, _) :: rest ->
410 let ips = calcips h in
411 let fh =
412 if conf.presentation
413 then fh+ips
414 else (
415 if isbirdseye state.mode && pn = 0
416 then fh + ips
417 else fh
420 let fh = fh + ((n - pn) * (ph + pi)) in
421 f n h ips fh rest;
423 | [] ->
424 let inc =
425 if conf.presentation || (isbirdseye state.mode && pn = 0)
426 then 0
427 else -pi
429 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
430 max 0 fh
432 let fh = f 0 0 0 0 state.pdims in
436 let getpageyh pageno =
437 let rec f pn ph pi y l =
438 match l with
439 | (n, _, h, _) :: rest ->
440 let ips = calcips h in
441 if n >= pageno
442 then
443 let h = if n = pageno then h else ph in
444 if conf.presentation && n = pageno
445 then
446 y + (pageno - pn) * (ph + pi) + pi, h
447 else
448 y + (pageno - pn) * (ph + pi), h
449 else
450 let y = y + (if conf.presentation then pi else 0) in
451 let y = y + (n - pn) * (ph + pi) in
452 f n h ips y rest
454 | [] ->
455 y + (pageno - pn) * (ph + pi), ph
457 f 0 0 0 0 state.pdims
460 let getpagey pageno = fst (getpageyh pageno);;
462 let layout y sh =
463 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
464 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
465 match pdims with
466 | (pageno', w, h, x) :: rest when pageno' = pageno ->
467 let ips = calcips h in
468 let yinc =
469 if conf.presentation || (isbirdseye state.mode && pageno = 0)
470 then ips
471 else 0
473 (w, h, ips, x), rest, pdimno + 1, yinc
474 | _ ->
475 prev, pdims, pdimno, 0
477 let dy = dy + yinc in
478 let py = py + yinc in
479 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
480 then
481 accu
482 else
483 let vy = y + dy in
484 if py + h <= vy - yinc
485 then
486 let py = py + h + ips in
487 let dy = max 0 (py - y) in
488 f ~pageno:(pageno+1)
489 ~pdimno
490 ~prev:curr
493 ~pdims:rest
494 ~cacheleft
495 ~accu
496 else
497 let pagey = vy - py in
498 let pagevh = h - pagey in
499 let pagevh = min (sh - dy) pagevh in
500 let off = if yinc > 0 then py - vy else 0 in
501 let py = py + h + ips in
502 let e =
503 { pageno = pageno
504 ; pagedimno = pdimno
505 ; pagew = w
506 ; pageh = h
507 ; pagedispy = dy + off
508 ; pagey = pagey + off
509 ; pagevh = pagevh - off
510 ; pagex = x
513 let accu = e :: accu in
514 f ~pageno:(pageno+1)
515 ~pdimno
516 ~prev:curr
518 ~dy:(dy+pagevh+ips)
519 ~pdims:rest
520 ~cacheleft:(cacheleft-1)
521 ~accu
523 if state.invalidated = 0
524 then (
525 let accu =
527 ~pageno:0
528 ~pdimno:~-1
529 ~prev:(0,0,0,0)
530 ~py:0
531 ~dy:0
532 ~pdims:state.pdims
533 ~cacheleft:(cbcap state.pagecache)
534 ~accu:[]
536 List.rev accu
538 else
542 let clamp incr =
543 let y = state.y + incr in
544 let y = max 0 y in
545 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
549 let getopaque pageno =
550 try Some (Hashtbl.find state.pagemap
551 (pageno, state.w, conf.angle, conf.proportional, state.gen))
552 with Not_found -> None
555 let cache pageno opaque =
556 Hashtbl.replace state.pagemap
557 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
560 let validopaque opaque = String.length opaque > 0;;
562 let render l =
563 match getopaque l.pageno with
564 | None when not state.rendering ->
565 state.rendering <- true;
566 cache l.pageno ("", -1);
567 wcmd "render" [`i (l.pageno + 1)
568 ;`i l.pagedimno
569 ;`i l.pagew
570 ;`i l.pageh];
571 | _ -> ()
574 let loadlayout layout =
575 let rec f all = function
576 | l :: ls ->
577 begin match getopaque l.pageno with
578 | None -> render l; f false ls
579 | Some (opaque, _) -> f (all && validopaque opaque) ls
581 | [] -> all
583 f (layout <> []) layout;
586 let findpageforopaque opaque =
587 Hashtbl.fold
588 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
589 state.pagemap None
592 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
594 let preload () =
595 let oktopreload =
596 if conf.preload
597 then
598 let memleft = conf.memlimit - state.memused in
599 if memleft < 0
600 then
601 let opaque = cbpeek state.pagecache in
602 match findpageforopaque opaque with
603 | Some ((n, _, _, _, _), size) ->
604 memleft + size >= 0 && not (pagevisible state.layout n)
605 | None -> false
606 else true
607 else false
609 if oktopreload
610 then
611 let presentation = conf.presentation in
612 let interpagespace = conf.interpagespace in
613 let maxy = state.maxy in
614 conf.presentation <- false;
615 conf.interpagespace <- 0;
616 state.maxy <- calcheight ();
617 let y =
618 match state.layout with
619 | [] -> 0
620 | l :: _ -> getpagey l.pageno + l.pagey
622 let y = if y < conf.winh then 0 else y - conf.winh in
623 let h = state.y - y + conf.winh*3 in
624 let pages = layout y h in
625 List.iter render pages;
626 conf.presentation <- presentation;
627 conf.interpagespace <- interpagespace;
628 state.maxy <- maxy;
631 let gotoy y =
632 let y = max 0 y in
633 let y = min state.maxy y in
634 let pages = layout y conf.winh in
635 let ready = loadlayout pages in
636 if conf.showall
637 then (
638 if ready
639 then (
640 state.y <- y;
641 state.layout <- pages;
642 state.throttle <- None;
643 Glut.postRedisplay ();
645 else (
646 state.throttle <- Some pages;
649 else (
650 state.y <- y;
651 state.layout <- pages;
652 state.throttle <- None;
653 Glut.postRedisplay ();
655 begin match state.mode with
656 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
657 if not (pagevisible pages pageno)
658 then (
659 match state.layout with
660 | [] -> ()
661 | l :: _ ->
662 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
664 | _ -> ()
665 end;
666 preload ();
669 let gotoy_and_clear_text y =
670 gotoy y;
671 if not conf.verbose then state.text <- "";
674 let getanchor () =
675 match state.layout with
676 | [] -> emptyanchor
677 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
680 let getanchory (n, top) =
681 let y, h = getpageyh n in
682 y + (truncate (top *. float h));
685 let gotoanchor anchor =
686 gotoy (getanchory anchor);
689 let addnav () =
690 cbput state.hists.nav (getanchor ());
693 let getnav dir =
694 let anchor = cbgetc state.hists.nav dir in
695 getanchory anchor;
698 let gotopage n top =
699 let y, h = getpageyh n in
700 gotoy_and_clear_text (y + (truncate (top *. float h)));
703 let gotopage1 n top =
704 let y = getpagey n in
705 gotoy_and_clear_text (y + top);
708 let invalidate () =
709 state.layout <- [];
710 state.pdims <- [];
711 state.rects <- [];
712 state.rects1 <- [];
713 state.invalidated <- state.invalidated + 1;
716 let scalecolor c =
717 let c = c *. state.colorscale in
718 (c, c, c);
721 let scalecolor2 (r, g, b) =
722 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
725 let represent () =
726 state.maxy <- calcheight ();
727 match state.mode with
728 | Birdseye (_, _, pageno, _, _) ->
729 let y, h = getpageyh pageno in
730 let top = (conf.winh - h) / 2 in
731 gotoy (max 0 (y - top))
732 | _ -> gotoanchor state.anchor
735 let pagematrix () =
736 GlMat.mode `projection;
737 GlMat.load_identity ();
738 GlMat.rotate ~x:1.0 ~angle:180.0 ();
739 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
740 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
741 if state.x != 0
742 then (
743 GlMat.translate ~x:(float state.x) ();
747 let winmatrix () =
748 GlMat.mode `projection;
749 GlMat.load_identity ();
750 GlMat.rotate ~x:1.0 ~angle:180.0 ();
751 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
752 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
755 let reshape =
756 let firsttime = ref true in
757 fun ~w ~h ->
758 if state.invalidated = 0 && not !firsttime
759 then state.anchor <- getanchor ();
761 firsttime := false;
762 conf.winw <- w;
763 let w = truncate (float w *. conf.zoom) - state.scrollw in
764 let w = max w 2 in
765 state.w <- w;
766 conf.winh <- h;
767 GlMat.mode `modelview;
768 GlMat.load_identity ();
769 GlClear.color (scalecolor 1.0);
770 GlClear.clear [`color];
772 invalidate ();
773 wcmd "geometry" [`i w; `i h];
776 let drawstring size x y s =
777 Gl.enable `blend;
778 Gl.enable `texture_2d;
779 ignore (drawstr size x y s);
780 Gl.disable `blend;
781 Gl.disable `texture_2d;
784 let drawstring1 size x y s =
785 drawstr size x y s;
788 let enttext () =
789 let len = String.length state.text in
790 let drawstring s =
791 GlDraw.color (0.0, 0.0, 0.0);
792 GlDraw.rect
793 (0.0, float (conf.winh - 18))
794 (float (conf.winw - state.scrollw - 1), float conf.winh)
796 GlDraw.color (1.0, 1.0, 1.0);
797 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
799 match state.mode with
800 | Textentry ((prefix, text, _, _, _), _) ->
801 let s =
802 if len > 0
803 then
804 Printf.sprintf "%s%s_ [%s]" prefix text state.text
805 else
806 Printf.sprintf "%s%s_" prefix text
808 drawstring s
810 | _ ->
811 if len > 0 then drawstring state.text
814 let showtext c s =
815 state.text <- Printf.sprintf "%c%s" c s;
816 Glut.postRedisplay ();
819 let act cmd =
820 match cmd.[0] with
821 | 'c' ->
822 state.pdims <- [];
824 | 'D' ->
825 state.rects <- state.rects1;
826 Glut.postRedisplay ()
828 | 'C' ->
829 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
830 state.pagecount <- n;
831 state.invalidated <- state.invalidated - 1;
832 if state.invalidated = 0
833 then represent ()
835 | 't' ->
836 let s = Scanf.sscanf cmd "t %n"
837 (fun n -> String.sub cmd n (String.length cmd - n))
839 Glut.setWindowTitle s
841 | 'T' ->
842 let s = Scanf.sscanf cmd "T %n"
843 (fun n -> String.sub cmd n (String.length cmd - n))
845 if istextentry state.mode
846 then (
847 state.text <- s;
848 showtext ' ' s;
850 else (
851 state.text <- s;
852 Glut.postRedisplay ();
855 | 'V' ->
856 if conf.verbose
857 then
858 let s = Scanf.sscanf cmd "V %n"
859 (fun n -> String.sub cmd n (String.length cmd - n))
861 state.text <- s;
862 showtext ' ' s;
864 | 'F' ->
865 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
866 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
867 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
868 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
870 let y = (getpagey pageno) + truncate y0 in
871 addnav ();
872 gotoy y;
873 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
875 | 'R' ->
876 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
877 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
878 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
879 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
881 state.rects1 <-
882 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
884 | 'r' ->
885 let n, w, _h, r, l, s, p =
886 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
887 (fun n w h r l s p ->
888 (n-1, w, h, r, l != 0, s, p))
891 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
892 state.memused <- state.memused + s;
894 let layout =
895 match state.throttle with
896 | None -> state.layout
897 | Some layout -> layout
900 let rec gc () =
901 if (state.memused <= conf.memlimit) || cbempty state.pagecache
902 then ()
903 else (
904 let evictedopaque = cbpeek state.pagecache in
905 match findpageforopaque evictedopaque with
906 | None -> failwith "bug in gc"
907 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
908 if state.gen != gen || not (pagevisible layout evictedn)
909 then (
910 wcmd "free" [`s evictedopaque];
911 state.memused <- state.memused - evictedsize;
912 Hashtbl.remove state.pagemap k;
913 cbdecr state.pagecache;
914 gc ();
918 gc ();
920 cbput state.pagecache p;
921 state.rendering <- false;
923 begin match state.throttle with
924 | None ->
925 if pagevisible state.layout n
926 then gotoy state.y
927 else (
928 let allvisible = loadlayout state.layout in
929 if allvisible then preload ();
932 | Some layout ->
933 match layout with
934 | [] -> ()
935 | l :: _ ->
936 let y = getpagey l.pageno + l.pagey in
937 gotoy y
940 | 'l' ->
941 let pdim =
942 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
944 state.pdims <- pdim :: state.pdims
946 | 'o' ->
947 let (l, n, t, h, pos) =
948 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
950 let s = String.sub cmd pos (String.length cmd - pos) in
951 let outline = (s, l, (n, float t /. float h)) in
952 let outlines =
953 match state.outlines with
954 | Olist outlines -> Olist (outline :: outlines)
955 | Oarray _ -> Olist [outline]
956 | Onarrow _ -> Olist [outline]
958 state.outlines <- outlines
961 | 'i' ->
962 if String.length cmd > 1 && cmd.[1] = 'e'
963 then
964 state.docinfo <- List.rev state.docinfo
965 else
966 let s = Scanf.sscanf cmd "i %n"
967 (fun n -> String.sub cmd n (String.length cmd - n))
969 state.docinfo <- (1, s) :: state.docinfo
971 | _ ->
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday;;
977 let idle () =
978 if state.deadline == nan then state.deadline <- now ();
979 let rec loop delay =
980 let timeout =
981 if delay > 0.0
982 then max 0.0 (state.deadline -. now ())
983 else 0.0
985 let r, _, _ = Unix.select [state.csock] [] [] timeout in
986 state.deadline <- state.deadline +. delay;
987 begin match r with
988 | [] ->
989 begin match state.autoscroll with
990 | Some step when step != 0 ->
991 let y = state.y + step in
992 let y =
993 if y < 0
994 then state.maxy
995 else if y >= state.maxy then 0 else y
997 gotoy y;
998 if state.mode = View
999 then state.text <- "";
1000 | _ -> ()
1001 end;
1003 | _ ->
1004 let cmd = readcmd state.csock in
1005 act cmd;
1006 loop 0.0
1007 end;
1008 in loop 0.007
1011 let onhist cb =
1012 let rc = cb.rc in
1013 let action = function
1014 | HCprev -> cbget cb ~-1
1015 | HCnext -> cbget cb 1
1016 | HCfirst -> cbget cb ~-(cb.rc)
1017 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1018 and cancel () = cb.rc <- rc
1019 in (action, cancel)
1022 let search pattern forward =
1023 if String.length pattern > 0
1024 then
1025 let pn, py =
1026 match state.layout with
1027 | [] -> 0, 0
1028 | l :: _ ->
1029 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1031 let cmd =
1032 let b = makecmd "search"
1033 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1035 Buffer.add_char b ',';
1036 Buffer.add_string b pattern;
1037 Buffer.add_char b '\000';
1038 Buffer.contents b;
1040 writecmd state.csock cmd;
1043 let intentry text key =
1044 let c = Char.unsafe_chr key in
1045 match c with
1046 | '0' .. '9' ->
1047 let s = "x" in s.[0] <- c;
1048 let text = text ^ s in
1049 TEcont text
1051 | _ ->
1052 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1053 TEcont text
1056 let addchar s c =
1057 let b = Buffer.create (String.length s + 1) in
1058 Buffer.add_string b s;
1059 Buffer.add_char b c;
1060 Buffer.contents b;
1063 let textentry text key =
1064 let c = Char.unsafe_chr key in
1065 match c with
1066 | _ when key >= 32 && key < 127 ->
1067 let text = addchar text c in
1068 TEcont text
1070 | _ ->
1071 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1072 TEcont text
1075 let reinit angle proportional =
1076 conf.angle <- angle;
1077 conf.proportional <- proportional;
1078 invalidate ();
1079 wcmd "reinit" [`i angle; `b proportional];
1082 let setzoom zoom =
1083 let zoom = max 0.01 zoom in
1084 if zoom <> conf.zoom
1085 then (
1086 if zoom <= 1.0
1087 then state.x <- 0;
1088 conf.zoom <- zoom;
1089 reshape conf.winw conf.winh;
1090 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1094 let enterbirdseye () =
1095 let zoom = float conf.thumbw /. float conf.winw in
1096 let birdseyepageno =
1097 let cy = conf.winh / 2 in
1098 let fold = function
1099 | [] -> 0
1100 | l :: rest ->
1101 let rec fold best = function
1102 | [] -> best.pageno
1103 | l :: rest ->
1104 let d = cy - (l.pagedispy + l.pagevh/2)
1105 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1106 if abs d < abs dbest
1107 then fold l rest
1108 else best.pageno
1109 in fold l rest
1111 fold state.layout
1113 state.mode <- Birdseye (
1114 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1116 conf.zoom <- zoom;
1117 conf.presentation <- false;
1118 conf.interpagespace <- 10;
1119 conf.hlinks <- false;
1120 state.x <- 0;
1121 state.mstate <- Mnone;
1122 conf.showall <- false;
1123 Glut.setCursor Glut.CURSOR_INHERIT;
1124 if conf.verbose
1125 then
1126 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1127 (100.0*.zoom)
1128 else
1129 state.text <- ""
1131 reshape conf.winw conf.winh;
1134 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1135 state.mode <- View;
1136 conf.zoom <- c.zoom;
1137 conf.presentation <- c.presentation;
1138 conf.interpagespace <- c.interpagespace;
1139 conf.showall <- c.showall;
1140 conf.hlinks <- c.hlinks;
1141 state.x <- leftx;
1142 if conf.verbose
1143 then
1144 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1145 (100.0*.conf.zoom)
1147 reshape conf.winw conf.winh;
1148 state.anchor <- if goback then anchor else (pageno, 0.0);
1151 let togglebirdseye () =
1152 match state.mode with
1153 | Birdseye vals -> leavebirdseye vals true
1154 | View | Outline _ -> enterbirdseye ()
1155 | _ -> ()
1158 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1159 let pageno = max 0 (pageno - 1) in
1160 let rec loop = function
1161 | [] -> gotopage1 pageno 0
1162 | l :: _ when l.pageno = pageno ->
1163 if l.pagedispy >= 0 && l.pagey = 0
1164 then Glut.postRedisplay ()
1165 else gotopage1 pageno 0
1166 | _ :: rest -> loop rest
1168 loop state.layout;
1169 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1172 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1173 let pageno = min (state.pagecount - 1) (pageno + 1) in
1174 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1175 let rec loop = function
1176 | [] ->
1177 let y, h = getpageyh pageno in
1178 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1179 gotoy (clamp dy)
1180 | l :: _ when l.pageno = pageno ->
1181 if l.pagevh != l.pageh
1182 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1183 else Glut.postRedisplay ()
1184 | _ :: rest -> loop rest
1186 loop state.layout
1189 let optentry mode _ key =
1190 let btos b = if b then "on" else "off" in
1191 let c = Char.unsafe_chr key in
1192 match c with
1193 | 's' ->
1194 let ondone s =
1195 try conf.scrollstep <- int_of_string s with exc ->
1196 state.text <- Printf.sprintf "bad integer `%s': %s"
1197 s (Printexc.to_string exc)
1199 TEswitch ("scroll step: ", "", None, intentry, ondone)
1201 | 'A' ->
1202 let ondone s =
1204 conf.autoscrollstep <- int_of_string s;
1205 if state.autoscroll <> None
1206 then state.autoscroll <- Some conf.autoscrollstep
1207 with exc ->
1208 state.text <- Printf.sprintf "bad integer `%s': %s"
1209 s (Printexc.to_string exc)
1211 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1213 | 'Z' ->
1214 let ondone s =
1216 let zoom = float (int_of_string s) /. 100.0 in
1217 setzoom zoom
1218 with exc ->
1219 state.text <- Printf.sprintf "bad integer `%s': %s"
1220 s (Printexc.to_string exc)
1222 TEswitch ("zoom: ", "", None, intentry, ondone)
1224 | 't' ->
1225 let ondone s =
1227 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1228 state.text <-
1229 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1230 begin match mode with
1231 | Birdseye beye ->
1232 leavebirdseye beye false;
1233 enterbirdseye ();
1234 | _ -> ();
1236 with exc ->
1237 state.text <- Printf.sprintf "bad integer `%s': %s"
1238 s (Printexc.to_string exc)
1240 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1242 | 'R' ->
1243 let ondone s =
1244 match try
1245 Some (int_of_string s)
1246 with exc ->
1247 state.text <- Printf.sprintf "bad integer `%s': %s"
1248 s (Printexc.to_string exc);
1249 None
1250 with
1251 | Some angle -> reinit angle conf.proportional
1252 | None -> ()
1254 TEswitch ("rotation: ", "", None, intentry, ondone)
1256 | 'i' ->
1257 conf.icase <- not conf.icase;
1258 TEdone ("case insensitive search " ^ (btos conf.icase))
1260 | 'p' ->
1261 conf.preload <- not conf.preload;
1262 gotoy state.y;
1263 TEdone ("preload " ^ (btos conf.preload))
1265 | 'v' ->
1266 conf.verbose <- not conf.verbose;
1267 TEdone ("verbose " ^ (btos conf.verbose))
1269 | 'h' ->
1270 conf.maxhfit <- not conf.maxhfit;
1271 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1272 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1274 | 'c' ->
1275 conf.crophack <- not conf.crophack;
1276 TEdone ("crophack " ^ btos conf.crophack)
1278 | 'a' ->
1279 conf.showall <- not conf.showall;
1280 TEdone ("throttle " ^ btos conf.showall)
1282 | 'f' ->
1283 conf.underinfo <- not conf.underinfo;
1284 TEdone ("underinfo " ^ btos conf.underinfo)
1286 | 'P' ->
1287 conf.savebmarks <- not conf.savebmarks;
1288 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1290 | 'S' ->
1291 let ondone s =
1293 let pageno, py =
1294 match state.layout with
1295 | [] -> 0, 0
1296 | l :: _ ->
1297 l.pageno, l.pagey
1299 conf.interpagespace <- int_of_string s;
1300 state.maxy <- calcheight ();
1301 let y = getpagey pageno in
1302 gotoy (y + py)
1303 with exc ->
1304 state.text <- Printf.sprintf "bad integer `%s': %s"
1305 s (Printexc.to_string exc)
1307 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1309 | 'l' ->
1310 reinit conf.angle (not conf.proportional);
1311 TEdone ("proprortional display " ^ btos conf.proportional)
1313 | _ ->
1314 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1315 TEstop
1318 let maxoutlinerows () = (conf.winh - 31) / 16;;
1320 let enterselector allowdel outlines errmsg msg =
1321 if Array.length outlines = 0
1322 then (
1323 showtext ' ' errmsg;
1325 else (
1326 state.text <- msg;
1327 Glut.setCursor Glut.CURSOR_INHERIT;
1328 let pageno =
1329 match state.layout with
1330 | [] -> -1
1331 | {pageno=pageno} :: _ -> pageno
1333 let active =
1334 let rec loop n =
1335 if n = Array.length outlines
1336 then 0
1337 else
1338 let (_, _, (outlinepageno, _)) = outlines.(n) in
1339 if outlinepageno >= pageno then n else loop (n+1)
1341 loop 0
1343 state.mode <- Outline
1344 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1345 state.mode);
1346 Glut.postRedisplay ();
1350 let enteroutlinemode () =
1351 let outlines, msg =
1352 match state.outlines with
1353 | Oarray a -> a, ""
1354 | Olist l ->
1355 let a = Array.of_list (List.rev l) in
1356 state.outlines <- Oarray a;
1357 a, ""
1358 | Onarrow (pat, a, _) ->
1359 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1361 enterselector false outlines "Document has no outline" msg;
1364 let enterbookmarkmode () =
1365 let bookmarks = Array.of_list state.bookmarks in
1366 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1369 let mode_to_string mode =
1370 let b = Buffer.create 10 in
1371 let rec f = function
1372 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1373 | View -> Buffer.add_string b "View"
1374 | Birdseye _ -> Buffer.add_string b "Birdseye"
1375 | Items _ -> Buffer.add_string b "Items"
1376 | Outline _ -> Buffer.add_string b "Outline"
1378 f mode;
1379 Buffer.contents b;
1382 let color_of_string s =
1383 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1384 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1388 let color_to_string (r, g, b) =
1389 let r = truncate (r *. 256.0)
1390 and g = truncate (g *. 256.0)
1391 and b = truncate (b *. 256.0) in
1392 Printf.sprintf "%d/%d/%d" r g b
1395 let enterinfomode () =
1396 let btos = function true -> "on" | _ -> "off" in
1397 let mode = state.mode in
1398 let rec makeitems () =
1399 let intp name get set =
1400 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1401 fun active first _ pan ->
1402 let ondone s =
1403 let n =
1404 try int_of_string s
1405 with exn ->
1406 state.text <- Printf.sprintf "bad integer `%s': %s"
1407 s (Printexc.to_string exn);
1408 max_int;
1410 if n != max_int then set n;
1412 let te = name ^ ": ", "", None, intentry, ondone in
1413 state.text <- "";
1414 Textentry (
1416 fun _ ->
1417 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1420 and boolp name get set =
1421 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1422 fun active first qsearch pan ->
1423 let v = get () in
1424 set (not v);
1425 Items (active, first, makeitems (), qsearch, pan, mode);
1427 and colorp name get set =
1428 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1429 fun active first _ pan ->
1430 let invalid = (nan, nan, nan) in
1431 let ondone s =
1432 let c =
1433 try color_of_string s
1434 with exn ->
1435 state.text <- Printf.sprintf "bad color `%s': %s"
1436 s (Printexc.to_string exn);
1437 invalid
1439 if c <> invalid
1440 then set c;
1442 let te = name ^ ": ", "", None, textentry, ondone in
1443 state.text <- "";
1444 Textentry (
1446 fun _ ->
1447 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1452 let birdseye = isbirdseye mode in
1453 let items = [
1454 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1456 boolp "presentation"
1457 (fun () -> conf.presentation)
1458 (fun v ->
1459 conf.presentation <- v;
1460 state.anchor <- getanchor ();
1461 represent ());
1463 boolp "ignore case in searches"
1464 (fun () -> conf.icase)
1465 (fun v -> conf.icase <- v);
1467 boolp "preload"
1468 (fun () -> conf.preload)
1469 (fun v -> conf.preload <- v);
1471 boolp "verbose"
1472 (fun () -> conf.verbose)
1473 (fun v -> conf.verbose <- v);
1475 boolp "max fit"
1476 (fun () -> conf.maxhfit)
1477 (fun v -> conf.maxhfit <- v);
1479 boolp "crop hack"
1480 (fun () -> conf.crophack)
1481 (fun v -> conf.crophack <- v);
1483 boolp "throttle"
1484 (fun () -> conf.showall)
1485 (fun v -> conf.showall <- v);
1487 boolp "highlight links"
1488 (fun () -> conf.hlinks)
1489 (fun v -> conf.hlinks <- v);
1491 boolp "under info"
1492 (fun () -> conf.underinfo)
1493 (fun v -> conf.underinfo <- v);
1494 boolp "persistent bookmarks"
1495 (fun () -> conf.savebmarks)
1496 (fun v -> conf.savebmarks <- v);
1498 boolp "proportional display"
1499 (fun () -> conf.proportional)
1500 (fun v -> reinit conf.angle v);
1502 boolp "persistent location"
1503 (fun () -> conf.jumpback)
1504 (fun v -> conf.jumpback <- v);
1506 "", 0, Noaction;
1508 intp "vertical margin"
1509 (fun () -> conf.interpagespace)
1510 (fun n ->
1511 conf.interpagespace <- n;
1512 let pageno, py =
1513 match state.layout with
1514 | [] -> 0, 0
1515 | l :: _ ->
1516 l.pageno, l.pagey
1518 state.maxy <- calcheight ();
1519 let y = getpagey pageno in
1520 gotoy (y + py)
1523 intp "page bias"
1524 (fun () -> conf.pagebias)
1525 (fun v -> conf.pagebias <- v);
1527 intp "scroll step"
1528 (fun () -> conf.scrollstep)
1529 (fun n -> conf.scrollstep <- n);
1531 intp "auto scroll step"
1532 (fun () ->
1533 match state.autoscroll with
1534 | Some step -> step
1535 | _ -> conf.autoscrollstep)
1536 (fun n ->
1537 if state.autoscroll <> None
1538 then state.autoscroll <- Some n;
1539 conf.autoscrollstep <- n);
1541 intp "zoom"
1542 (fun () -> truncate (conf.zoom *. 100.))
1543 (fun v -> setzoom ((float v) /. 100.));
1545 intp "rotation"
1546 (fun () -> conf.angle)
1547 (fun v -> reinit v conf.proportional);
1549 intp "scroll bar width"
1550 (fun () -> state.scrollw)
1551 (fun v ->
1552 state.scrollw <- v;
1553 conf.scrollbw <- v;
1554 reshape conf.winw conf.winh;
1557 intp "scroll handle height"
1558 (fun () -> conf.scrollh)
1559 (fun v -> conf.scrollh <- v;);
1561 intp "thumbnail width"
1562 (fun () -> conf.thumbw)
1563 (fun v ->
1564 conf.thumbw <- min 1920 v;
1565 match mode with
1566 | Birdseye beye ->
1567 leavebirdseye beye false;
1568 enterbirdseye ()
1569 | _ -> ()
1572 colorp "background color"
1573 (fun () -> conf.bgcolor)
1574 (fun v -> conf.bgcolor <- v);
1576 "", 0, Noaction;
1577 "Presentation mode", 0, Noaction;
1579 boolp "scrollbar visible"
1580 (fun () -> conf.scrollbarinpm)
1581 (fun v ->
1582 if v != conf.scrollbarinpm
1583 then (
1584 conf.scrollbarinpm <- v;
1585 if conf.presentation
1586 then (
1587 state.scrollw <- if v then conf.scrollbw else 0;
1588 reshape conf.winw conf.winh;
1593 "", 0, Noaction;
1594 "Pixmap Cache", 0, Noaction;
1596 intp "size (advisory)"
1597 (fun () -> conf.memlimit)
1598 (fun v -> conf.memlimit <- v);
1599 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1603 let tailer =
1604 let trailer =
1605 ("", 0, Noaction)
1606 :: ("Document", 0, Noaction)
1607 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1609 if birdseye
1610 then trailer
1611 else (
1612 ("", 0, Noaction)
1613 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1614 (btos conf.bedefault),
1616 Action (
1617 fun active first qsearch pan ->
1618 conf.bedefault <- not conf.bedefault;
1619 Items (active, first, makeitems (), qsearch, pan, mode);
1621 ) :: trailer
1624 Array.of_list (items @ tailer)
1626 state.text <- "";
1627 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1628 Glut.postRedisplay ();
1631 let enterhelpmode () =
1632 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1633 Glut.postRedisplay ();
1636 let quickbookmark ?title () =
1637 match state.layout with
1638 | [] -> ()
1639 | l :: _ ->
1640 let title =
1641 match title with
1642 | None ->
1643 let sec = Unix.gettimeofday () in
1644 let tm = Unix.localtime sec in
1645 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1646 (l.pageno+1)
1647 tm.Unix.tm_mday
1648 tm.Unix.tm_mon
1649 (tm.Unix.tm_year + 1900)
1650 tm.Unix.tm_hour
1651 tm.Unix.tm_min
1652 | Some title -> title
1654 state.bookmarks <-
1655 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1656 :: state.bookmarks
1659 let doreshape w h =
1660 state.fullscreen <- None;
1661 Glut.reshapeWindow w h;
1664 let writeopen path password =
1665 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1668 let opendoc path password =
1669 invalidate ();
1670 state.path <- path;
1671 state.password <- password;
1672 state.gen <- state.gen + 1;
1673 state.docinfo <- [];
1675 writeopen path password;
1676 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1677 wcmd "geometry" [`i state.w; `i conf.winh];
1680 let viewkeyboard key =
1681 let enttext te =
1682 let mode = state.mode in
1683 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1684 state.text <- "";
1685 enttext ();
1686 Glut.postRedisplay ()
1688 let c = Char.chr key in
1689 match c with
1690 | '\027' -> (* escape *)
1691 if String.length state.text > 0
1692 then (
1693 state.text <- "";
1694 Glut.postRedisplay ();
1696 else
1697 exit 0
1699 | 'q' ->
1700 exit 0
1702 | '\008' -> (* backspace *)
1703 let y = getnav ~-1 in
1704 gotoy_and_clear_text y
1706 | 'o' ->
1707 enteroutlinemode ()
1709 | 'u' ->
1710 state.rects <- [];
1711 state.text <- "";
1712 Glut.postRedisplay ()
1714 | '/' | '?' ->
1715 let ondone isforw s =
1716 cbput state.hists.pat s;
1717 state.searchpattern <- s;
1718 search s isforw
1720 let s = String.create 1 in
1721 s.[0] <- c;
1722 enttext (s, "", Some (onhist state.hists.pat),
1723 textentry, ondone (c ='/'))
1725 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1726 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1727 setzoom (conf.zoom +. incr)
1729 | '+' ->
1730 let ondone s =
1731 let n =
1732 try int_of_string s with exc ->
1733 state.text <- Printf.sprintf "bad integer `%s': %s"
1734 s (Printexc.to_string exc);
1735 max_int
1737 if n != max_int
1738 then (
1739 conf.pagebias <- n;
1740 state.text <- "page bias is now " ^ string_of_int n;
1743 enttext ("page bias: ", "", None, intentry, ondone)
1745 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1746 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1747 setzoom (max 0.01 (conf.zoom -. decr))
1749 | '-' ->
1750 let ondone msg = state.text <- msg in
1751 enttext (
1752 "option [acfhilpstvAPRSZ]: ", "", None,
1753 optentry state.mode, ondone
1756 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1757 setzoom 1.0
1759 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1760 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1761 if zoom < 1.0
1762 then setzoom zoom
1764 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1765 togglebirdseye ()
1767 | '0' .. '9' ->
1768 let ondone s =
1769 let n =
1770 try int_of_string s with exc ->
1771 state.text <- Printf.sprintf "bad integer `%s': %s"
1772 s (Printexc.to_string exc);
1775 if n >= 0
1776 then (
1777 addnav ();
1778 cbput state.hists.pag (string_of_int n);
1779 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1782 let pageentry text key =
1783 match Char.unsafe_chr key with
1784 | 'g' -> TEdone text
1785 | _ -> intentry text key
1787 let text = "x" in text.[0] <- c;
1788 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1790 | 'b' ->
1791 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1792 reshape conf.winw conf.winh;
1794 | 'l' ->
1795 conf.hlinks <- not conf.hlinks;
1796 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1797 Glut.postRedisplay ()
1799 | 'a' ->
1800 begin match state.autoscroll with
1801 | Some step ->
1802 conf.autoscrollstep <- step;
1803 state.autoscroll <- None
1804 | None ->
1805 if conf.autoscrollstep = 0
1806 then state.autoscroll <- Some 1
1807 else state.autoscroll <- Some conf.autoscrollstep
1810 | 'P' ->
1811 conf.presentation <- not conf.presentation;
1812 if conf.presentation
1813 then (
1814 if not conf.scrollbarinpm
1815 then state.scrollw <- 0;
1817 else
1818 state.scrollw <- conf.scrollbw;
1820 showtext ' ' ("presentation mode " ^
1821 if conf.presentation then "on" else "off");
1822 state.anchor <- getanchor ();
1823 represent ()
1825 | 'f' ->
1826 begin match state.fullscreen with
1827 | None ->
1828 state.fullscreen <- Some (conf.winw, conf.winh);
1829 Glut.fullScreen ()
1830 | Some (w, h) ->
1831 state.fullscreen <- None;
1832 doreshape w h
1835 | 'g' ->
1836 gotoy_and_clear_text 0
1838 | 'n' ->
1839 search state.searchpattern true
1841 | 'p' | 'N' ->
1842 search state.searchpattern false
1844 | 't' ->
1845 begin match state.layout with
1846 | [] -> ()
1847 | l :: _ ->
1848 gotoy_and_clear_text (getpagey l.pageno)
1851 | ' ' ->
1852 begin match List.rev state.layout with
1853 | [] -> ()
1854 | l :: _ ->
1855 let pageno = min (l.pageno+1) (state.pagecount-1) in
1856 gotoy_and_clear_text (getpagey pageno)
1859 | '\127' -> (* del *)
1860 begin match state.layout with
1861 | [] -> ()
1862 | l :: _ ->
1863 let pageno = max 0 (l.pageno-1) in
1864 gotoy_and_clear_text (getpagey pageno)
1867 | '=' ->
1868 let f (fn, _) l =
1869 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1871 let fn, ln = List.fold_left f (-1, -1) state.layout in
1872 let s =
1873 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1874 let percent =
1875 if maxy <= 0
1876 then 100.
1877 else (100. *. (float state.y /. float maxy)) in
1878 if fn = ln
1879 then
1880 Printf.sprintf "Page %d of %d %.2f%%"
1881 (fn+1) state.pagecount percent
1882 else
1883 Printf.sprintf
1884 "Pages %d-%d of %d %.2f%%"
1885 (fn+1) (ln+1) state.pagecount percent
1887 showtext ' ' s;
1889 | 'w' ->
1890 begin match state.layout with
1891 | [] -> ()
1892 | l :: _ ->
1893 doreshape (l.pagew + state.scrollw) l.pageh;
1894 Glut.postRedisplay ();
1897 | '\'' ->
1898 enterbookmarkmode ()
1900 | 'h' ->
1901 enterhelpmode ()
1903 | 'i' ->
1904 enterinfomode ()
1906 | 'm' ->
1907 let ondone s =
1908 match state.layout with
1909 | l :: _ ->
1910 state.bookmarks <-
1911 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1912 :: state.bookmarks
1913 | _ -> ()
1915 enttext ("bookmark: ", "", None, textentry, ondone)
1917 | '~' ->
1918 quickbookmark ();
1919 showtext ' ' "Quick bookmark added";
1921 | 'z' ->
1922 begin match state.layout with
1923 | l :: _ ->
1924 let rect = getpdimrect l.pagedimno in
1925 let w, h =
1926 if conf.crophack
1927 then
1928 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1929 truncate (1.2 *. (rect.(3) -. rect.(0))))
1930 else
1931 (truncate (rect.(1) -. rect.(0)),
1932 truncate (rect.(3) -. rect.(0)))
1934 if w != 0 && h != 0
1935 then
1936 doreshape (w + state.scrollw) (h + conf.interpagespace)
1938 Glut.postRedisplay ();
1940 | [] -> ()
1943 | '\000' -> (* ctrl-2 *)
1944 let maxw = getmaxw () in
1945 if maxw > 0.0
1946 then setzoom (maxw /. float conf.winw)
1948 | '<' | '>' ->
1949 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1951 | '[' | ']' ->
1952 state.colorscale <-
1953 max 0.0
1954 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1955 Glut.postRedisplay ()
1957 | 'k' ->
1958 begin match state.mode with
1959 | Birdseye beye -> upbirdseye beye
1960 | _ -> gotoy (clamp (-conf.scrollstep))
1963 | 'j' ->
1964 begin match state.mode with
1965 | Birdseye beye -> downbirdseye beye
1966 | _ -> gotoy (clamp conf.scrollstep)
1969 | 'r' ->
1970 state.anchor <- getanchor ();
1971 opendoc state.path state.password
1973 | _ ->
1974 vlog "huh? %d %c" key (Char.chr key);
1977 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1978 let enttext te =
1979 state.mode <- Textentry (te, onleave);
1980 state.text <- "";
1981 enttext ();
1982 Glut.postRedisplay ()
1984 match Char.unsafe_chr key with
1985 | '\008' -> (* backspace *)
1986 let len = String.length text in
1987 if len = 0
1988 then (
1989 onleave Cancel;
1990 Glut.postRedisplay ();
1992 else (
1993 let s = String.sub text 0 (len - 1) in
1994 enttext (c, s, opthist, onkey, ondone)
1997 | '\r' | '\n' ->
1998 ondone text;
1999 onleave Confirm;
2000 Glut.postRedisplay ()
2002 | '\027' -> (* escape *)
2003 begin match opthist with
2004 | None -> ()
2005 | Some (_, onhistcancel) -> onhistcancel ()
2006 end;
2007 onleave Cancel;
2008 Glut.postRedisplay ()
2010 | _ ->
2011 begin match onkey text key with
2012 | TEdone text ->
2013 onleave Confirm;
2014 ondone text;
2015 Glut.postRedisplay ()
2017 | TEcont text ->
2018 enttext (c, text, opthist, onkey, ondone);
2020 | TEstop ->
2021 onleave Cancel;
2022 Glut.postRedisplay ()
2024 | TEswitch te ->
2025 state.mode <- Textentry (te, onleave);
2026 Glut.postRedisplay ()
2027 end;
2030 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2031 match key with
2032 | 27 -> (* escape *)
2033 leavebirdseye beye true
2035 | 12 -> (* ctrl-l *)
2036 let y, h = getpageyh pageno in
2037 let top = (conf.winh - h) / 2 in
2038 gotoy (max 0 (y - top))
2040 | 13 -> (* enter *)
2041 leavebirdseye beye false
2043 | _ ->
2044 viewkeyboard key
2047 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2048 let set active first qsearch =
2049 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2051 let search active pattern incr =
2052 let dosearch re =
2053 let rec loop n =
2054 if n >= Array.length items || n < 0
2055 then None
2056 else
2057 let (s, _, _) = items.(n) in
2059 (try ignore (Str.search_forward re s 0); true
2060 with Not_found -> false)
2061 then Some n
2062 else loop (n + incr)
2064 loop active
2067 let re = Str.regexp_case_fold pattern in
2068 dosearch re
2069 with Failure s ->
2070 state.text <- s;
2071 None
2073 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2074 match key with
2075 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2076 let incr = if key = 18 then -1 else 1 in
2077 let active, first =
2078 match search (active + incr) qsearch incr with
2079 | None ->
2080 state.text <- qsearch ^ " [not found]";
2081 active, first
2082 | Some active ->
2083 state.text <- qsearch;
2084 active, firstof active
2086 set active first qsearch;
2087 Glut.postRedisplay ();
2089 | 8 -> (* backspace *)
2090 let len = String.length qsearch in
2091 if len = 0
2092 then ()
2093 else (
2094 if len = 1
2095 then (
2096 state.text <- "";
2097 set active first "";
2099 else
2100 let qsearch = String.sub qsearch 0 (len - 1) in
2101 let active, first =
2102 match search active qsearch ~-1 with
2103 | None ->
2104 state.text <- qsearch ^ " [not found]";
2105 active, first
2106 | Some active ->
2107 state.text <- qsearch;
2108 active, firstof active
2110 set active first qsearch
2112 Glut.postRedisplay ()
2114 | _ when key >= 32 && key < 127 ->
2115 let pattern = addchar qsearch (Char.chr key) in
2116 let active, first =
2117 match search active pattern 1 with
2118 | None ->
2119 state.text <- pattern ^ " [not found]";
2120 active, first
2121 | Some active ->
2122 state.text <- pattern;
2123 active, firstof active
2125 set active first pattern;
2126 Glut.postRedisplay ()
2128 | 27 -> (* escape *)
2129 state.text <- "";
2130 if String.length qsearch = 0
2131 then (
2132 state.mode <- oldmode;
2134 else (
2135 set active first "";
2137 Glut.postRedisplay ()
2139 | 13 -> (* enter *)
2140 if active >= 0 && active < Array.length items
2141 then (
2142 match items.(active) with
2143 | _, _, Action f ->
2144 state.mode <- f active first qsearch pan
2146 | _, _, Noaction ->
2147 state.text <- "";
2148 state.mode <- oldmode
2150 else (
2151 state.text <- "";
2152 state.mode <- oldmode
2154 Glut.postRedisplay ();
2156 | _ -> dolog "unknown key %d" key
2159 let outlinekeyboard key
2160 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2161 let narrow outlines pattern =
2162 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2163 match reopt with
2164 | None -> None
2165 | Some re ->
2166 let rec fold accu n =
2167 if n = -1
2168 then accu
2169 else
2170 let (s, _, _) as o = outlines.(n) in
2171 let accu =
2172 if (try ignore (Str.search_forward re s 0); true
2173 with Not_found -> false)
2174 then (o :: accu)
2175 else accu
2177 fold accu (n-1)
2179 let matched = fold [] (Array.length outlines - 1) in
2180 if matched = [] then None else Some (Array.of_list matched)
2182 let search active pattern incr =
2183 let dosearch re =
2184 let rec loop n =
2185 if n = Array.length outlines || n = -1
2186 then None
2187 else
2188 let (s, _, _) = outlines.(n) in
2190 (try ignore (Str.search_forward re s 0); true
2191 with Not_found -> false)
2192 then Some n
2193 else loop (n + incr)
2195 loop active
2198 let re = Str.regexp_case_fold pattern in
2199 dosearch re
2200 with Failure s ->
2201 state.text <- s;
2202 None
2204 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2205 match key with
2206 | 27 -> (* escape *)
2207 state.text <- "";
2208 if String.length qsearch = 0
2209 then (
2210 state.mode <- oldmode;
2212 else (
2213 state.mode <- Outline (
2214 allowdel, active, first, outlines, "", pan, oldmode
2217 Glut.postRedisplay ();
2219 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2220 let incr = if key = 18 then -1 else 1 in
2221 let active, first =
2222 match search (active + incr) qsearch incr with
2223 | None ->
2224 state.text <- qsearch ^ " [not found]";
2225 active, first
2226 | Some active ->
2227 state.text <- qsearch;
2228 active, firstof active
2230 state.mode <- Outline (
2231 allowdel, active, first, outlines, qsearch, pan, oldmode
2233 Glut.postRedisplay ();
2235 | 8 -> (* backspace *)
2236 let len = String.length qsearch in
2237 if len = 0
2238 then ()
2239 else (
2240 if len = 1
2241 then (
2242 state.text <- "";
2243 state.mode <- Outline (
2244 allowdel, active, first, outlines, "", pan, oldmode
2247 else
2248 let qsearch = String.sub qsearch 0 (len - 1) in
2249 let active, first =
2250 match search active qsearch ~-1 with
2251 | None ->
2252 state.text <- qsearch ^ " [not found]";
2253 active, first
2254 | Some active ->
2255 state.text <- qsearch;
2256 active, firstof active
2258 state.mode <- Outline (
2259 allowdel, active, first, outlines, qsearch, pan, oldmode
2262 Glut.postRedisplay ()
2264 | 13 -> (* enter *)
2265 if active < Array.length outlines
2266 then (
2267 let (_, _, anchor) = outlines.(active) in
2268 addnav ();
2269 gotoanchor anchor;
2271 state.text <- "";
2272 if allowdel then state.bookmarks <- Array.to_list outlines;
2273 state.mode <- oldmode;
2274 Glut.postRedisplay ();
2276 | _ when key >= 32 && key < 127 ->
2277 let pattern = addchar qsearch (Char.chr key) in
2278 let active, first =
2279 match search active pattern 1 with
2280 | None ->
2281 state.text <- pattern ^ " [not found]";
2282 active, first
2283 | Some active ->
2284 state.text <- pattern;
2285 active, firstof active
2287 state.mode <- Outline (
2288 allowdel, active, first, outlines, pattern, pan, oldmode
2290 Glut.postRedisplay ()
2292 | 14 when not allowdel -> (* ctrl-n *)
2293 if String.length qsearch > 0
2294 then (
2295 let optoutlines = narrow outlines qsearch in
2296 begin match optoutlines with
2297 | None -> state.text <- "can't narrow"
2298 | Some outlines ->
2299 state.mode <- Outline (
2300 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2302 match state.outlines with
2303 | Olist _ -> ()
2304 | Oarray a ->
2305 state.outlines <- Onarrow (qsearch, outlines, a)
2306 | Onarrow (_, _, b) ->
2307 state.outlines <- Onarrow (qsearch, outlines, b)
2308 end;
2310 Glut.postRedisplay ()
2312 | 21 when not allowdel -> (* ctrl-u *)
2313 let outline =
2314 match state.outlines with
2315 | Oarray a -> a
2316 | Olist l ->
2317 let a = Array.of_list (List.rev l) in
2318 state.outlines <- Oarray a;
2320 | Onarrow (_, _, b) ->
2321 state.outlines <- Oarray b;
2322 state.text <- "";
2325 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2326 Glut.postRedisplay ()
2328 | 12 -> (* ctrl-l *)
2329 state.mode <- Outline
2330 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2331 Glut.postRedisplay ()
2333 | 127 when allowdel -> (* delete *)
2334 let len = Array.length outlines - 1 in
2335 if len = 0
2336 then (
2337 state.mode <- View;
2338 state.bookmarks <- [];
2340 else (
2341 let bookmarks = Array.init len
2342 (fun i ->
2343 let i = if i >= active then i + 1 else i in
2344 outlines.(i)
2347 state.mode <-
2348 Outline (
2349 allowdel,
2350 min active (len-1),
2351 min first (len-1),
2352 bookmarks, qsearch,
2354 oldmode
2357 Glut.postRedisplay ()
2359 | _ -> dolog "unknown key %d" key
2362 let keyboard ~key ~x ~y =
2363 ignore x;
2364 ignore y;
2365 if key = 7 (* ctrl-g *)
2366 then
2367 wcmd "interrupt" []
2368 else
2369 match state.mode with
2370 | Outline outline -> outlinekeyboard key outline
2371 | Textentry textentry -> textentrykeyboard key textentry
2372 | Birdseye birdseye -> birdseyekeyboard key birdseye
2373 | View -> viewkeyboard key
2374 | Items items -> itemskeyboard key items
2377 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2378 match key with
2379 | Glut.KEY_UP -> upbirdseye beye
2380 | Glut.KEY_DOWN -> downbirdseye beye
2382 | Glut.KEY_PAGE_UP ->
2383 begin match state.layout with
2384 | l :: _ ->
2385 if l.pagey != 0
2386 then (
2387 state.mode <- Birdseye (
2388 conf, leftx, l.pageno, hooverpageno, anchor
2390 gotopage1 l.pageno 0;
2392 else (
2393 let layout = layout (state.y-conf.winh) conf.winh in
2394 match layout with
2395 | [] -> gotoy (clamp (-conf.winh))
2396 | l :: _ ->
2397 state.mode <- Birdseye (
2398 conf, leftx, l.pageno, hooverpageno, anchor
2400 gotopage1 l.pageno 0
2403 | [] -> gotoy (clamp (-conf.winh))
2404 end;
2406 | Glut.KEY_PAGE_DOWN ->
2407 begin match List.rev state.layout with
2408 | l :: _ ->
2409 let layout = layout (state.y + conf.winh) conf.winh in
2410 begin match layout with
2411 | [] ->
2412 let incr = l.pageh - l.pagevh in
2413 if incr = 0
2414 then (
2415 state.mode <-
2416 Birdseye (
2417 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2419 Glut.postRedisplay ();
2421 else gotoy (clamp (incr + conf.interpagespace*2));
2423 | l :: _ ->
2424 state.mode <-
2425 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2426 gotopage1 l.pageno 0;
2429 | [] -> gotoy (clamp conf.winh)
2430 end;
2432 | Glut.KEY_HOME ->
2433 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2434 gotopage1 0 0
2436 | Glut.KEY_END ->
2437 let pageno = state.pagecount - 1 in
2438 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2439 if not (pagevisible state.layout pageno)
2440 then
2441 let h =
2442 match List.rev state.pdims with
2443 | [] -> conf.winh
2444 | (_, _, h, _) :: _ -> h
2446 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2447 else Glut.postRedisplay ();
2448 | _ -> ()
2451 let setautoscrollspeed step goingdown =
2452 let incr = max 1 ((abs step) / 2) in
2453 let incr = if goingdown then incr else -incr in
2454 let astep = step + incr in
2455 state.autoscroll <- Some astep;
2458 let special ~key ~x ~y =
2459 ignore x;
2460 ignore y;
2461 match state.mode with
2462 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2463 togglebirdseye ()
2465 | Birdseye vals ->
2466 birdseyespecial key vals
2468 | View when key = Glut.KEY_F1 ->
2469 enterhelpmode ()
2471 | View ->
2472 begin match state.autoscroll with
2473 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2474 setautoscrollspeed step (key = Glut.KEY_DOWN)
2476 | _ ->
2477 let y =
2478 match key with
2479 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2480 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2481 | Glut.KEY_DOWN -> clamp conf.scrollstep
2482 | Glut.KEY_PAGE_UP ->
2483 if Glut.getModifiers () land Glut.active_ctrl != 0
2484 then
2485 match state.layout with
2486 | [] -> state.y
2487 | l :: _ -> state.y - l.pagey
2488 else
2489 clamp (-conf.winh)
2490 | Glut.KEY_PAGE_DOWN ->
2491 if Glut.getModifiers () land Glut.active_ctrl != 0
2492 then
2493 match List.rev state.layout with
2494 | [] -> state.y
2495 | l :: _ -> getpagey l.pageno
2496 else
2497 clamp conf.winh
2498 | Glut.KEY_HOME -> addnav (); 0
2499 | Glut.KEY_END ->
2500 addnav ();
2501 state.maxy - (if conf.maxhfit then conf.winh else 0)
2503 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2504 Glut.getModifiers () land Glut.active_alt != 0 ->
2505 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2507 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2508 state.x <- state.x - 10;
2509 state.y
2510 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2511 state.x <- state.x + 10;
2512 state.y
2514 | _ -> state.y
2516 gotoy_and_clear_text y
2519 | Textentry
2520 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2521 let s =
2522 match key with
2523 | Glut.KEY_UP -> action HCprev
2524 | Glut.KEY_DOWN -> action HCnext
2525 | Glut.KEY_HOME -> action HCfirst
2526 | Glut.KEY_END -> action HClast
2527 | _ -> state.text
2529 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2530 Glut.postRedisplay ()
2532 | Textentry _ -> ()
2534 | Items (active, first, items, qsearch, pan, oldmode) ->
2535 let maxrows = maxoutlinerows () in
2536 let itemcount = Array.length items in
2537 let hasaction = function
2538 | (_, _, Noaction) -> false
2539 | _ -> true
2541 let find start incr =
2542 let rec find i =
2543 if i = -1 || i = itemcount
2544 then -1
2545 else (
2546 if hasaction items.(i)
2547 then i
2548 else find (i + incr)
2551 find start
2553 let set active first =
2554 let first = max 0 (min first (itemcount - maxrows)) in
2555 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2557 let navigate incr =
2558 let isvisible first n = n >= first && n - first <= maxrows in
2559 let active, first =
2560 let incr1 = if incr > 0 then 1 else -1 in
2561 if isvisible first active
2562 then
2563 let next =
2564 let next = active + incr in
2565 let next =
2566 if next < 0 || next >= itemcount
2567 then -1
2568 else find next incr1
2570 if next = -1 || abs (active - next) > maxrows
2571 then -1
2572 else next
2574 if next = -1
2575 then
2576 let first = first + incr in
2577 let first = max 0 (min first (itemcount - 1)) in
2578 let next =
2579 let next = active + incr in
2580 let next = max 0 (min next (itemcount - 1)) in
2581 find next ~-incr1
2583 let active = if next = -1 then active else next in
2584 active, first
2585 else
2586 let first = min next first in
2587 next, first
2588 else
2589 let first = first + incr in
2590 let first = max 0 (min first (itemcount - 1)) in
2591 let active =
2592 let next = active + incr in
2593 let next = max 0 (min next (itemcount - 1)) in
2594 let next = find next incr1 in
2595 if next = -1 || abs (active - first) > maxrows
2596 then active
2597 else next
2599 active, first
2601 set active first;
2602 Glut.postRedisplay ()
2604 begin match key with
2605 | Glut.KEY_UP -> navigate ~-1
2606 | Glut.KEY_DOWN -> navigate 1
2607 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2608 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2610 | Glut.KEY_RIGHT ->
2611 state.mode <- Items (
2612 active, first, items, qsearch, min 0 (pan - 1), oldmode
2614 Glut.postRedisplay ()
2616 | Glut.KEY_LEFT ->
2617 state.mode <- Items (
2618 active, first, items, qsearch, min 0 (pan + 1), oldmode
2620 Glut.postRedisplay ()
2622 | Glut.KEY_HOME ->
2623 let active = find 0 1 in
2624 set active 0;
2625 Glut.postRedisplay ()
2627 | Glut.KEY_END ->
2628 let first = max 0 (itemcount - maxrows) in
2629 let active = find (itemcount - 1) ~-1 in
2630 set active first;
2631 Glut.postRedisplay ()
2633 | _ -> ()
2634 end;
2636 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2637 let maxrows = maxoutlinerows () in
2638 let calcfirst first active =
2639 if active > first
2640 then
2641 let rows = active - first in
2642 if rows > maxrows then active - maxrows else first
2643 else active
2645 let navigate incr =
2646 let active = active + incr in
2647 let active = max 0 (min active (Array.length outlines - 1)) in
2648 let first = calcfirst first active in
2649 state.mode <- Outline (
2650 allowdel, active, first, outlines, qsearch, pan, oldmode
2652 Glut.postRedisplay ()
2654 let updownlevel incr =
2655 let len = Array.length outlines in
2656 let (_, curlevel, _) = outlines.(active) in
2657 let rec flow i =
2658 if i = len then i-1 else if i = -1 then 0 else
2659 let (_, l, _) = outlines.(i) in
2660 if l != curlevel then i else flow (i+incr)
2662 let active = flow active in
2663 let first = calcfirst first active in
2664 state.mode <- Outline (
2665 allowdel, active, first, outlines, qsearch, pan, oldmode
2667 Glut.postRedisplay ()
2669 match key with
2670 | Glut.KEY_UP -> navigate ~-1
2671 | Glut.KEY_DOWN -> navigate 1
2672 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2673 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2675 | Glut.KEY_RIGHT ->
2676 if Glut.getModifiers () land Glut.active_ctrl != 0
2677 then (
2678 state.mode <- Outline (
2679 allowdel, active, first, outlines,
2680 qsearch, min 0 (pan + 1), oldmode
2682 Glut.postRedisplay ();
2684 else (
2685 if not allowdel
2686 then updownlevel 1
2689 | Glut.KEY_LEFT ->
2690 if Glut.getModifiers () land Glut.active_ctrl != 0
2691 then (
2692 state.mode <- Outline (
2693 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2695 Glut.postRedisplay ();
2697 else (
2698 if not allowdel
2699 then updownlevel ~-1
2702 | Glut.KEY_HOME ->
2703 state.mode <- Outline (
2704 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2706 Glut.postRedisplay ()
2708 | Glut.KEY_END ->
2709 let active = Array.length outlines - 1 in
2710 let first = max 0 (active - maxrows) in
2711 state.mode <- Outline (
2712 allowdel, active, first, outlines, qsearch, pan, oldmode
2714 Glut.postRedisplay ()
2716 | _ -> ()
2719 let drawplaceholder l =
2720 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2721 GlDraw.rect
2722 (float l.pagex, float l.pagedispy)
2723 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2725 let x = if margin < 0 then -margin else l.pagex
2726 and y = l.pagedispy + 13 in
2727 GlDraw.color (0.0, 0.0, 0.0);
2728 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2731 let now () = Unix.gettimeofday ();;
2733 let drawpage l =
2734 let color =
2735 match state.mode with
2736 | Textentry _ -> scalecolor 0.4
2737 | View | Outline _ | Items _ -> scalecolor 1.0
2738 | Birdseye (_, _, pageno, hooverpageno, _) ->
2739 if l.pageno = hooverpageno
2740 then scalecolor 0.9
2741 else (
2742 if l.pageno = pageno
2743 then scalecolor 1.0
2744 else scalecolor 0.8
2747 GlDraw.color color;
2748 begin match getopaque l.pageno with
2749 | Some (opaque, _) when validopaque opaque ->
2750 let a = now () in
2751 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2752 let b = now () in
2753 let d = b-.a in
2754 vlog "draw %d %f sec" l.pageno d;
2756 | _ ->
2757 drawplaceholder l;
2758 end;
2761 let scrollph y =
2762 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2763 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2764 let sh = float conf.winh /. sh in
2765 let sh = max sh (float conf.scrollh) in
2767 let percent =
2768 if state.y = state.maxy
2769 then 1.0
2770 else float y /. float maxy
2772 let position = (float conf.winh -. sh) *. percent in
2774 let position =
2775 if position +. sh > float conf.winh
2776 then float conf.winh -. sh
2777 else position
2779 position, sh;
2782 let scrollindicator () =
2783 GlDraw.color (0.64 , 0.64, 0.64);
2784 GlDraw.rect
2785 (float (conf.winw - state.scrollw), 0.)
2786 (float conf.winw, float conf.winh)
2788 GlDraw.color (0.0, 0.0, 0.0);
2790 let position, sh = scrollph state.y in
2791 GlDraw.rect
2792 (float (conf.winw - state.scrollw), position)
2793 (float conf.winw, position +. sh)
2797 let showsel margin =
2798 match state.mstate with
2799 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2802 | Msel ((x0, y0), (x1, y1)) ->
2803 let rec loop = function
2804 | l :: ls ->
2805 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2806 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2807 then
2808 match getopaque l.pageno with
2809 | Some (opaque, _) when validopaque opaque ->
2810 let oy = -l.pagey + l.pagedispy in
2811 seltext opaque
2812 (x0 - margin - state.x, y0,
2813 x1 - margin - state.x, y1) oy;
2815 | _ -> ()
2816 else loop ls
2817 | [] -> ()
2819 loop state.layout
2822 let showrects () =
2823 let panx = float state.x in
2824 Gl.enable `blend;
2825 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2826 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2827 List.iter
2828 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2829 List.iter (fun l ->
2830 if l.pageno = pageno
2831 then (
2832 let d = float (l.pagedispy - l.pagey) in
2833 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2834 GlDraw.begins `quads;
2836 GlDraw.vertex2 (x0+.panx, y0+.d);
2837 GlDraw.vertex2 (x1+.panx, y1+.d);
2838 GlDraw.vertex2 (x2+.panx, y2+.d);
2839 GlDraw.vertex2 (x3+.panx, y3+.d);
2841 GlDraw.ends ();
2843 ) state.layout
2844 ) state.rects
2846 Gl.disable `blend;
2849 let showstrings trusted active first pan strings =
2850 Gl.enable `blend;
2851 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2852 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2853 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2854 GlDraw.color (1., 1., 1.);
2855 Gl.enable `texture_2d;
2857 let wx = measurestr 15 "w" in
2858 let tabx = 30.0*.wx +. float (pan*15) in
2859 let rec loop row =
2860 if row = Array.length strings || (row - first) * 16 > conf.winh
2861 then ()
2862 else (
2863 let (s, level, _) = strings.(row) in
2864 let y = (row - first) * 16 in
2865 let x = 5 + 15*(max 0 (level+pan)) in
2866 if row = active
2867 then (
2868 Gl.disable `texture_2d;
2869 GlDraw.polygon_mode `both `line;
2870 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2871 GlDraw.rect (0., float (y + 1))
2872 (float (conf.winw - 1), float (y + 18));
2873 GlDraw.polygon_mode `both `fill;
2874 GlDraw.color (1., 1., 1.);
2875 Gl.enable `texture_2d;
2878 let drawtabularstring x s =
2879 let _ =
2880 if trusted
2881 then
2882 let tabpos = try String.index s '\t' with Not_found -> -1 in
2883 if tabpos > 0
2884 then
2885 let len = String.length s - tabpos - 1 in
2886 let s1 = String.sub s 0 tabpos
2887 and s2 = String.sub s (tabpos + 1) len in
2888 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2889 let x = truncate (max xx tabx) in
2890 drawstring1 15 x (y + 16) s2
2891 else
2892 drawstring1 15 x (y + 16) s
2893 else
2894 drawstring1 15 x (y + 16) s
2898 drawtabularstring (x + pan*15) s;
2899 loop (row+1)
2902 loop first;
2903 Gl.disable `blend;
2904 Gl.disable `texture_2d;
2907 let showoutline (_, active, first, outlines, _, pan, _) =
2908 showstrings false active first pan outlines;
2911 let showitems (active, first, items, _, pan, _) =
2912 showstrings true active first pan items;
2915 let display () =
2916 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2917 GlDraw.viewport margin 0 state.w conf.winh;
2918 pagematrix ();
2919 GlClear.color (scalecolor2 conf.bgcolor);
2920 GlClear.clear [`color];
2921 if conf.zoom > 1.0
2922 then (
2923 Gl.enable `scissor_test;
2924 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2926 List.iter drawpage state.layout;
2927 if conf.zoom > 1.0
2928 then
2929 Gl.disable `scissor_test
2931 if state.x != 0
2932 then (
2933 let x = -.float state.x in
2934 GlMat.translate ~x ();
2936 showrects ();
2937 showsel margin;
2938 GlDraw.viewport 0 0 conf.winw conf.winh;
2939 winmatrix ();
2940 scrollindicator ();
2941 begin match state.mode with
2942 | Items items -> showitems items
2943 | Outline outline -> showoutline outline
2944 | _ -> ()
2945 end;
2946 enttext ();
2947 Glut.swapBuffers ();
2950 let getunder x y =
2951 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2952 let x = x - margin - state.x in
2953 let rec f = function
2954 | l :: rest ->
2955 begin match getopaque l.pageno with
2956 | Some (opaque, _) when validopaque opaque ->
2957 let y = y - l.pagedispy in
2958 if y > 0
2959 then
2960 let y = l.pagey + y in
2961 let x = x - l.pagex in
2962 match whatsunder opaque x y with
2963 | Unone -> f rest
2964 | under -> under
2965 else
2966 f rest
2967 | _ ->
2968 f rest
2970 | [] -> Unone
2972 f state.layout
2975 let viewmouse button bstate x y =
2976 match button with
2977 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2978 if Glut.getModifiers () land Glut.active_ctrl != 0
2979 then (
2980 match state.mstate with
2981 | Mzoom (oldn, i) ->
2982 if oldn = n
2983 then (
2984 if i = 2
2985 then
2986 let incr =
2987 match n with
2988 | 4 ->
2989 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2990 | _ ->
2991 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2993 let zoom = conf.zoom +. incr in
2994 setzoom zoom;
2995 state.mstate <- Mzoom (n, 0);
2996 else
2997 state.mstate <- Mzoom (n, i+1);
2999 else state.mstate <- Mzoom (n, 0)
3001 | _ -> state.mstate <- Mzoom (n, 0)
3003 else (
3004 match state.autoscroll with
3005 | Some step -> setautoscrollspeed step (n=4)
3006 | None ->
3007 let incr =
3008 if n = 3
3009 then -conf.scrollstep
3010 else conf.scrollstep
3012 let incr = incr * 2 in
3013 let y = clamp incr in
3014 gotoy_and_clear_text y
3017 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3018 if bstate = Glut.DOWN
3019 then (
3020 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3021 state.mstate <- Mpan (x, y)
3023 else
3024 state.mstate <- Mnone
3026 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3027 if bstate = Glut.DOWN
3028 then
3029 let position, sh = scrollph state.y in
3030 if y > truncate position && y < truncate (position +. sh)
3031 then
3032 state.mstate <- Mscroll
3033 else
3034 let percent = float y /. float conf.winh in
3035 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3036 gotoy desty;
3037 state.mstate <- Mscroll
3038 else
3039 state.mstate <- Mnone
3041 | Glut.LEFT_BUTTON ->
3042 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3043 begin match dest with
3044 | Ulinkgoto (pageno, top) ->
3045 if pageno >= 0
3046 then (
3047 addnav ();
3048 gotopage1 pageno top;
3051 | Ulinkuri s ->
3052 print_endline s
3054 | Unone when bstate = Glut.DOWN ->
3055 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3056 state.mstate <- Mpan (x, y);
3058 | Unone | Utext _ ->
3059 if bstate = Glut.DOWN
3060 then (
3061 if conf.angle mod 360 = 0
3062 then (
3063 state.mstate <- Msel ((x, y), (x, y));
3064 Glut.postRedisplay ()
3067 else (
3068 match state.mstate with
3069 | Mnone -> ()
3071 | Mzoom _ | Mscroll ->
3072 state.mstate <- Mnone
3074 | Mpan _ ->
3075 Glut.setCursor Glut.CURSOR_INHERIT;
3076 state.mstate <- Mnone
3078 | Msel ((_, y0), (_, y1)) ->
3079 let f l =
3080 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3081 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3082 then
3083 match getopaque l.pageno with
3084 | Some (opaque, _) when validopaque opaque ->
3085 copysel opaque
3086 | _ -> ()
3088 List.iter f state.layout;
3089 copysel ""; (* ugly *)
3090 Glut.setCursor Glut.CURSOR_INHERIT;
3091 state.mstate <- Mnone;
3095 | _ -> ()
3098 let birdseyemouse button bstate x y
3099 (conf, leftx, _, hooverpageno, anchor) =
3100 match button with
3101 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3102 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3103 let rec loop = function
3104 | [] -> ()
3105 | l :: rest ->
3106 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3107 && x > margin && x < margin + l.pagew
3108 then (
3109 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3111 else loop rest
3113 loop state.layout
3114 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3115 | _ -> ()
3118 let mouse bstate button x y =
3119 match state.mode with
3120 | View -> viewmouse button bstate x y
3121 | Birdseye beye -> birdseyemouse button bstate x y beye
3122 | Textentry _ | Outline _ | Items _ -> ()
3125 let mouse ~button ~state ~x ~y = mouse state button x y;;
3127 let motion ~x ~y =
3128 match state.mode with
3129 | Outline _ -> ()
3130 | _ ->
3131 match state.mstate with
3132 | Mzoom _ | Mnone -> ()
3134 | Mpan (x0, y0) ->
3135 let dx = x - x0
3136 and dy = y0 - y in
3137 state.mstate <- Mpan (x, y);
3138 if conf.zoom > 1.0 then state.x <- state.x + dx;
3139 let y = clamp dy in
3140 gotoy_and_clear_text y
3142 | Msel (a, _) ->
3143 state.mstate <- Msel (a, (x, y));
3144 Glut.postRedisplay ()
3146 | Mscroll ->
3147 let y = min conf.winh (max 0 y) in
3148 let percent = float y /. float conf.winh in
3149 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3150 gotoy_and_clear_text y
3153 let pmotion ~x ~y =
3154 match state.mode with
3155 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3156 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3157 let rec loop = function
3158 | [] ->
3159 if hooverpageno != -1
3160 then (
3161 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3162 Glut.postRedisplay ();
3164 | l :: rest ->
3165 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3166 && x > margin && x < margin + l.pagew
3167 then (
3168 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3169 Glut.postRedisplay ();
3171 else loop rest
3173 loop state.layout
3175 | Outline _ | Items _ | Textentry _ -> ()
3176 | View ->
3177 match state.mstate with
3178 | Mnone ->
3179 begin match getunder x y with
3180 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3181 | Ulinkuri uri ->
3182 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3183 Glut.setCursor Glut.CURSOR_INFO
3184 | Ulinkgoto (page, _) ->
3185 if conf.underinfo
3186 then showtext 'p' ("age: " ^ string_of_int page);
3187 Glut.setCursor Glut.CURSOR_INFO
3188 | Utext s ->
3189 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3190 Glut.setCursor Glut.CURSOR_TEXT
3193 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3198 module State =
3199 struct
3200 open Parser
3202 let home =
3204 match Sys.os_type with
3205 | "Win32" -> Sys.getenv "HOMEPATH"
3206 | _ -> Sys.getenv "HOME"
3207 with exn ->
3208 prerr_endline
3209 ("Can not determine home directory location: " ^
3210 Printexc.to_string exn);
3214 let config_of c attrs =
3215 let apply c k v =
3217 match k with
3218 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3219 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3220 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3221 | "preload" -> { c with preload = bool_of_string v }
3222 | "page-bias" -> { c with pagebias = int_of_string v }
3223 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3224 | "auto-scroll-step" ->
3225 { c with autoscrollstep = max 0 (int_of_string v) }
3226 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3227 | "crop-hack" -> { c with crophack = bool_of_string v }
3228 | "throttle" -> { c with showall = bool_of_string v }
3229 | "highlight-links" -> { c with hlinks = bool_of_string v }
3230 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3231 | "vertical-margin" ->
3232 { c with interpagespace = max 0 (int_of_string v) }
3233 | "zoom" ->
3234 let zoom = float_of_string v /. 100. in
3235 let zoom = max 0.01 zoom in
3236 { c with zoom = zoom }
3237 | "presentation" -> { c with presentation = bool_of_string v }
3238 | "rotation-angle" -> { c with angle = int_of_string v }
3239 | "width" -> { c with winw = max 20 (int_of_string v) }
3240 | "height" -> { c with winh = max 20 (int_of_string v) }
3241 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3242 | "proportional-display" -> { c with proportional = bool_of_string v }
3243 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3244 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3245 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3246 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3247 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3248 | "persistent-location" -> { c with jumpback = bool_of_string v }
3249 | "background-color" -> { c with bgcolor = color_of_string v }
3250 | "scrollbar-in-presentation" ->
3251 { c with scrollbarinpm = bool_of_string v }
3252 | "ui-font" -> { c with uifont = v }
3253 | _ -> c
3254 with exn ->
3255 prerr_endline ("Error processing attribute (`" ^
3256 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3259 let rec fold c = function
3260 | [] -> c
3261 | (k, v) :: rest ->
3262 let c = apply c k v in
3263 fold c rest
3265 fold c attrs;
3268 let fromstring f pos n v d =
3269 try f v
3270 with exn ->
3271 dolog "Error processing attribute (%S=%S) at %d\n%s"
3272 n v pos (Printexc.to_string exn)
3277 let bookmark_of attrs =
3278 let rec fold title page rely = function
3279 | ("title", v) :: rest -> fold v page rely rest
3280 | ("page", v) :: rest -> fold title v rely rest
3281 | ("rely", v) :: rest -> fold title page v rest
3282 | _ :: rest -> fold title page rely rest
3283 | [] -> title, page, rely
3285 fold "invalid" "0" "0" attrs
3288 let doc_of attrs =
3289 let rec fold path page rely pan = function
3290 | ("path", v) :: rest -> fold v page rely pan rest
3291 | ("page", v) :: rest -> fold path v rely pan rest
3292 | ("rely", v) :: rest -> fold path page v pan rest
3293 | ("pan", v) :: rest -> fold path page rely v rest
3294 | _ :: rest -> fold path page rely pan rest
3295 | [] -> path, page, rely, pan
3297 fold "" "0" "0" "0" attrs
3300 let setconf dst src =
3301 dst.scrollbw <- src.scrollbw;
3302 dst.scrollh <- src.scrollh;
3303 dst.icase <- src.icase;
3304 dst.preload <- src.preload;
3305 dst.pagebias <- src.pagebias;
3306 dst.verbose <- src.verbose;
3307 dst.scrollstep <- src.scrollstep;
3308 dst.maxhfit <- src.maxhfit;
3309 dst.crophack <- src.crophack;
3310 dst.autoscrollstep <- src.autoscrollstep;
3311 dst.showall <- src.showall;
3312 dst.hlinks <- src.hlinks;
3313 dst.underinfo <- src.underinfo;
3314 dst.interpagespace <- src.interpagespace;
3315 dst.zoom <- src.zoom;
3316 dst.presentation <- src.presentation;
3317 dst.angle <- src.angle;
3318 dst.winw <- src.winw;
3319 dst.winh <- src.winh;
3320 dst.savebmarks <- src.savebmarks;
3321 dst.memlimit <- src.memlimit;
3322 dst.proportional <- src.proportional;
3323 dst.texcount <- src.texcount;
3324 dst.sliceheight <- src.sliceheight;
3325 dst.blockwidth <- src.blockwidth;
3326 dst.thumbw <- src.thumbw;
3327 dst.jumpback <- src.jumpback;
3328 dst.bgcolor <- src.bgcolor;
3329 dst.scrollbarinpm <- src.scrollbarinpm;
3330 dst.uifont <- src.uifont;
3333 let unent s =
3334 let l = String.length s in
3335 let b = Buffer.create l in
3336 unent b s 0 l;
3337 Buffer.contents b;
3340 let get s =
3341 let h = Hashtbl.create 10 in
3342 let dc = { defconf with angle = defconf.angle } in
3343 let rec toplevel v t spos _ =
3344 match t with
3345 | Vdata | Vcdata | Vend -> v
3346 | Vopen ("llppconfig", _, closed) ->
3347 if closed
3348 then v
3349 else { v with f = llppconfig }
3350 | Vopen _ ->
3351 error "unexpected subelement at top level" s spos
3352 | Vclose _ -> error "unexpected close at top level" s spos
3354 and llppconfig v t spos _ =
3355 match t with
3356 | Vdata | Vcdata | Vend -> v
3357 | Vopen ("defaults", attrs, closed) ->
3358 let c = config_of dc attrs in
3359 setconf dc c;
3360 if closed
3361 then v
3362 else { v with f = skip "defaults" (fun () -> v) }
3364 | Vopen ("doc", attrs, closed) ->
3365 let pathent, spage, srely, span = doc_of attrs in
3366 let path = unent pathent
3367 and pageno = fromstring int_of_string spos "page" spage 0
3368 and rely = fromstring float_of_string spos "rely" srely 0.0
3369 and pan = fromstring int_of_string spos "pan" span 0 in
3370 let c = config_of dc attrs in
3371 let anchor = (pageno, rely) in
3372 if closed
3373 then (Hashtbl.add h path (c, [], pan, anchor); v)
3374 else { v with f = doc path pan anchor c [] }
3376 | Vopen _ ->
3377 error "unexpected subelement in llppconfig" s spos
3379 | Vclose "llppconfig" -> { v with f = toplevel }
3380 | Vclose _ -> error "unexpected close in llppconfig" s spos
3382 and doc path pan anchor c bookmarks v t spos _ =
3383 match t with
3384 | Vdata | Vcdata -> v
3385 | Vend -> error "unexpected end of input in doc" s spos
3386 | Vopen ("bookmarks", _, closed) ->
3387 if closed
3388 then v
3389 else { v with f = pbookmarks path pan anchor c bookmarks }
3391 | Vopen (_, _, _) ->
3392 error "unexpected subelement in doc" s spos
3394 | Vclose "doc" ->
3395 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3396 { v with f = llppconfig }
3398 | Vclose _ -> error "unexpected close in doc" s spos
3400 and pbookmarks path pan anchor c bookmarks v t spos _ =
3401 match t with
3402 | Vdata | Vcdata -> v
3403 | Vend -> error "unexpected end of input in bookmarks" s spos
3404 | Vopen ("item", attrs, closed) ->
3405 let titleent, spage, srely = bookmark_of attrs in
3406 let page = fromstring int_of_string spos "page" spage 0
3407 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3408 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3409 if closed
3410 then { v with f = pbookmarks path pan anchor c bookmarks }
3411 else
3412 let f () = v in
3413 { v with f = skip "item" f }
3415 | Vopen _ ->
3416 error "unexpected subelement in bookmarks" s spos
3418 | Vclose "bookmarks" ->
3419 { v with f = doc path pan anchor c bookmarks }
3421 | Vclose _ -> error "unexpected close in bookmarks" s spos
3423 and skip tag f v t spos _ =
3424 match t with
3425 | Vdata | Vcdata -> v
3426 | Vend ->
3427 error ("unexpected end of input in skipped " ^ tag) s spos
3428 | Vopen (tag', _, closed) ->
3429 if closed
3430 then v
3431 else
3432 let f' () = { v with f = skip tag f } in
3433 { v with f = skip tag' f' }
3434 | Vclose ctag ->
3435 if tag = ctag
3436 then f ()
3437 else error ("unexpected close in skipped " ^ tag) s spos
3440 parse { f = toplevel; accu = () } s;
3441 h, dc;
3444 let do_load f ic =
3446 let len = in_channel_length ic in
3447 let s = String.create len in
3448 really_input ic s 0 len;
3449 f s;
3450 with
3451 | Parse_error (msg, s, pos) ->
3452 let subs = subs s pos in
3453 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3454 failwith ("parse error: " ^ s)
3456 | exn ->
3457 failwith ("config load error: " ^ Printexc.to_string exn)
3460 let path =
3461 let dir =
3463 let dir = Filename.concat home ".config" in
3464 if Sys.is_directory dir then dir else home
3465 with _ -> home
3467 Filename.concat dir "llpp.conf"
3470 let load1 f =
3471 if Sys.file_exists path
3472 then
3473 match
3474 (try Some (open_in_bin path)
3475 with exn ->
3476 prerr_endline
3477 ("Error opening configuation file `" ^ path ^ "': " ^
3478 Printexc.to_string exn);
3479 None
3481 with
3482 | Some ic ->
3483 begin try
3484 f (do_load get ic)
3485 with exn ->
3486 prerr_endline
3487 ("Error loading configuation from `" ^ path ^ "': " ^
3488 Printexc.to_string exn);
3489 end;
3490 close_in ic;
3492 | None -> ()
3493 else
3494 f (Hashtbl.create 0, defconf)
3497 let load () =
3498 let f (h, dc) =
3499 let pc, pb, px, pa =
3501 Hashtbl.find h (Filename.basename state.path)
3502 with Not_found -> dc, [], 0, (0, 0.0)
3504 setconf defconf dc;
3505 setconf conf pc;
3506 state.bookmarks <- pb;
3507 state.x <- px;
3508 state.scrollw <- conf.scrollbw;
3509 if conf.jumpback
3510 then state.anchor <- pa;
3511 cbput state.hists.nav pa;
3513 load1 f
3516 let add_attrs bb always dc c =
3517 let ob s a b =
3518 if always || a != b
3519 then Printf.bprintf bb "\n %s='%b'" s a
3520 and oi s a b =
3521 if always || a != b
3522 then Printf.bprintf bb "\n %s='%d'" s a
3523 and oz s a b =
3524 if always || a <> b
3525 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3526 and oc s a b =
3527 if always || a <> b
3528 then
3529 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3530 and os s a b =
3531 if always || a <> b
3532 then
3533 Printf.bprintf bb "\n %s='%s'" s a
3535 let w, h =
3536 if always
3537 then dc.winw, dc.winh
3538 else
3539 match state.fullscreen with
3540 | Some wh -> wh
3541 | None -> c.winw, c.winh
3543 let zoom, presentation, interpagespace, showall=
3544 if always
3545 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3546 else
3547 match state.mode with
3548 | Birdseye (bc, _, _, _, _) ->
3549 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3550 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3552 oi "width" w dc.winw;
3553 oi "height" h dc.winh;
3554 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3555 oi "scroll-handle-height" c.scrollh dc.scrollh;
3556 ob "case-insensitive-search" c.icase dc.icase;
3557 ob "preload" c.preload dc.preload;
3558 oi "page-bias" c.pagebias dc.pagebias;
3559 oi "scroll-step" c.scrollstep dc.scrollstep;
3560 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3561 ob "max-height-fit" c.maxhfit dc.maxhfit;
3562 ob "crop-hack" c.crophack dc.crophack;
3563 ob "throttle" showall dc.showall;
3564 ob "highlight-links" c.hlinks dc.hlinks;
3565 ob "under-cursor-info" c.underinfo dc.underinfo;
3566 oi "vertical-margin" interpagespace dc.interpagespace;
3567 oz "zoom" zoom dc.zoom;
3568 ob "presentation" presentation dc.presentation;
3569 oi "rotation-angle" c.angle dc.angle;
3570 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3571 ob "proportional-display" c.proportional dc.proportional;
3572 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3573 oi "tex-count" c.texcount dc.texcount;
3574 oi "slice-height" c.sliceheight dc.sliceheight;
3575 oi "block-width" c.blockwidth dc.blockwidth;
3576 oi "thumbnail-width" c.thumbw dc.thumbw;
3577 ob "persistent-location" c.jumpback dc.jumpback;
3578 oc "background-color" c.bgcolor dc.bgcolor;
3579 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3580 os "ui-font" c.uifont dc.uifont;
3583 let save () =
3584 let bb = Buffer.create 32768 in
3585 let f (h, dc) =
3586 let dc = if conf.bedefault then conf else dc in
3587 Buffer.add_string bb "<llppconfig>\n<defaults ";
3588 add_attrs bb true dc dc;
3589 Buffer.add_string bb "/>\n";
3591 let adddoc path pan anchor c bookmarks =
3592 if bookmarks == [] && c = dc && anchor = emptyanchor
3593 then ()
3594 else (
3595 Printf.bprintf bb "<doc path='%s'"
3596 (enent path 0 (String.length path));
3598 if anchor <> emptyanchor
3599 then (
3600 let n, y = anchor in
3601 Printf.bprintf bb " page='%d'" n;
3602 if y > 1e-6
3603 then
3604 Printf.bprintf bb " rely='%f'" y
3608 if pan != 0
3609 then Printf.bprintf bb " pan='%d'" pan;
3611 add_attrs bb false dc c;
3613 begin match bookmarks with
3614 | [] -> Buffer.add_string bb "/>\n"
3615 | _ ->
3616 Buffer.add_string bb ">\n<bookmarks>\n";
3617 List.iter (fun (title, _level, (page, rely)) ->
3618 Printf.bprintf bb
3619 "<item title='%s' page='%d'"
3620 (enent title 0 (String.length title))
3621 page
3623 if rely > 1e-6
3624 then
3625 Printf.bprintf bb " rely='%f'" rely
3627 Buffer.add_string bb "/>\n";
3628 ) bookmarks;
3629 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3630 end;
3634 let pan =
3635 match state.mode with
3636 | Birdseye (_, pan, _, _, _) -> pan
3637 | _ -> state.x
3639 let basename = Filename.basename state.path in
3640 adddoc basename pan (getanchor ())
3641 { conf with
3642 autoscrollstep =
3643 match state.autoscroll with
3644 | Some step -> step
3645 | None -> conf.autoscrollstep }
3646 (if conf.savebmarks then state.bookmarks else []);
3648 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3649 if basename <> path
3650 then adddoc path x y c bookmarks
3651 ) h;
3652 Buffer.add_string bb "</llppconfig>";
3654 load1 f;
3655 if Buffer.length bb > 0
3656 then
3658 let tmp = path ^ ".tmp" in
3659 let oc = open_out_bin tmp in
3660 Buffer.output_buffer oc bb;
3661 close_out oc;
3662 Sys.rename tmp path;
3663 with exn ->
3664 prerr_endline
3665 ("error while saving configuration: " ^ Printexc.to_string exn)
3667 end;;
3669 let () =
3670 Arg.parse
3671 (Arg.align
3672 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3673 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3674 " Print version and exit")]
3676 (fun s -> state.path <- s)
3677 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3679 if String.length state.path = 0
3680 then (prerr_endline "file name missing"; exit 1);
3682 State.load ();
3684 let _ = Glut.init Sys.argv in
3685 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3686 let () = Glut.initWindowSize conf.winw conf.winh in
3687 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3689 let csock, ssock =
3690 if Sys.os_type = "Unix"
3691 then
3692 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3693 else
3694 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3695 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3696 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3697 Unix.bind sock addr;
3698 Unix.listen sock 1;
3699 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3700 Unix.connect csock addr;
3701 let ssock, _ = Unix.accept sock in
3702 Unix.close sock;
3703 let opts sock =
3704 Unix.setsockopt sock Unix.TCP_NODELAY true;
3705 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3707 opts ssock;
3708 opts csock;
3709 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3710 ssock, csock
3713 let () = Glut.displayFunc display in
3714 let () = Glut.reshapeFunc reshape in
3715 let () = Glut.keyboardFunc keyboard in
3716 let () = Glut.specialFunc special in
3717 let () = Glut.idleFunc (Some idle) in
3718 let () = Glut.mouseFunc mouse in
3719 let () = Glut.motionFunc motion in
3720 let () = Glut.passiveMotionFunc pmotion in
3722 init ssock (
3723 conf.angle, conf.proportional, conf.texcount,
3724 conf.sliceheight, conf.blockwidth, conf.uifont
3726 state.csock <- csock;
3727 state.ssock <- ssock;
3728 state.text <- "Opening " ^ state.path;
3729 writeopen state.path state.password;
3731 at_exit State.save;
3733 let rec handlelablglutbug () =
3735 Glut.mainLoop ();
3736 with Glut.BadEnum "key in special_of_int" ->
3737 showtext '!' " LablGlut bug: special key not recognized";
3738 handlelablglutbug ()
3740 handlelablglutbug ();