Evict more aggressively
[llpp.git] / main.ml
blob4c2703f183b2e9db372842b6765f0c80a72dbc16
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 exception Quit;;
12 type params =
13 angle * proportional * texcount * sliceheight * blockwidth * fontpath
14 and pageno = int
15 and width = int
16 and height = int
17 and leftx = int
18 and opaque = string
19 and recttype = int
20 and pixmapsize = int
21 and angle = int
22 and proportional = bool
23 and interpagespace = int
24 and texcount = int
25 and sliceheight = int
26 and blockwidth = int
27 and gen = int
28 and top = float
29 and fontpath = string
32 external init : Unix.file_descr -> params -> unit = "ml_init";;
33 external draw : (int * int * int * bool) -> string -> unit = "ml_draw";;
34 external seltext : string -> (int * int * int * int) -> int -> unit =
35 "ml_seltext";;
36 external copysel : string -> unit = "ml_copysel";;
37 external getpdimrect : int -> float array = "ml_getpdimrect";;
38 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
39 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
40 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
41 external measurestr : int -> string -> float = "ml_measure_string";;
42 external getmaxw : unit -> float = "ml_getmaxw";;
44 type mpos = int * int
45 and mstate =
46 | Msel of (mpos * mpos)
47 | Mpan of mpos
48 | Mscroll
49 | Mzoom of (int * int)
50 | Mnone
53 type textentry = string * string * onhist option * onkey * ondone
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel)
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
66 type 'a circbuf =
67 { store : 'a array
68 ; mutable rc : int
69 ; mutable wc : int
70 ; mutable len : int
74 let cbnew n v =
75 { store = Array.create n v
76 ; rc = 0
77 ; wc = 0
78 ; len = 0
82 let cbcap b = Array.length b.store;;
84 let cbput b v =
85 let cap = cbcap b in
86 b.store.(b.wc) <- v;
87 b.wc <- (b.wc + 1) mod cap;
88 b.rc <- b.wc;
89 b.len <- min (b.len + 1) cap;
92 let cbempty b = b.len = 0;;
94 let cbgetg b circular dir =
95 if cbempty b
96 then b.store.(0)
97 else
98 let rc = b.rc + dir in
99 let rc =
100 if circular
101 then (
102 if rc = -1
103 then b.len-1
104 else (
105 if rc = b.len
106 then 0
107 else rc
110 else max 0 (min rc (b.len-1))
112 b.rc <- rc;
113 b.store.(rc);
116 let cbget b = cbgetg b false;;
117 let cbgetc b = cbgetg b true;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollbw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollstep : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscrollstep : int
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable blockwidth : blockwidth
157 ; mutable thumbw : width
158 ; mutable jumpback : bool
159 ; mutable bgcolor : float * float * float
160 ; mutable bedefault : bool
161 ; mutable scrollbarinpm : bool
165 type anchor = pageno * top;;
167 type outline = string * int * anchor
168 and outlines =
169 | Oarray of outline array
170 | Olist of outline list
171 | Onarrow of string * outline array * outline array
174 type rect = float * float * float * float * float * float * float * float;;
176 type pagemapkey = pageno * width * angle * proportional * gen;;
178 let emptyanchor = (0, 0.0);;
180 type mode =
181 | Birdseye of (conf * leftx * pageno * pageno * anchor)
182 | Outline of (bool * int * int * outline array * string * int * mode)
183 | Items of (int * int * item array * string * int * mode)
184 | Textentry of (textentry * onleave)
185 | View
186 and onleave = leavetextentrystatus -> unit
187 and leavetextentrystatus = | Cancel | Confirm
188 and item = string * int * action
189 and action =
190 | Noaction
191 | Action of (int -> int -> string -> int -> mode)
194 let isbirdseye = function Birdseye _ -> true | _ -> false;;
195 let istextentry = function Textentry _ -> true | _ -> false;;
197 type state =
198 { mutable csock : Unix.file_descr
199 ; mutable ssock : Unix.file_descr
200 ; mutable w : int
201 ; mutable x : int
202 ; mutable y : int
203 ; mutable scrollw : int
204 ; mutable anchor : anchor
205 ; mutable maxy : int
206 ; mutable layout : layout list
207 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
208 ; pagelru : opaque Queue.t
209 ; mutable pdims : (pageno * width * height * leftx) list
210 ; mutable pagecount : int
211 ; mutable rendering : bool
212 ; mutable mstate : mstate
213 ; mutable searchpattern : string
214 ; mutable rects : (pageno * recttype * rect) list
215 ; mutable rects1 : (pageno * recttype * rect) list
216 ; mutable text : string
217 ; mutable fullscreen : (width * height) option
218 ; mutable mode : mode
219 ; mutable outlines : outlines
220 ; mutable bookmarks : outline list
221 ; mutable path : string
222 ; mutable password : string
223 ; mutable invalidated : int
224 ; mutable colorscale : float
225 ; mutable memused : int
226 ; mutable gen : gen
227 ; mutable throttle : layout list option
228 ; mutable autoscroll :int option
229 ; mutable help : item array
230 ; mutable docinfo : (int * string) list
231 ; mutable deadline : float
232 ; hists : hists
234 and hists =
235 { pat : string circbuf
236 ; pag : string circbuf
237 ; nav : anchor circbuf
241 let defconf =
242 { scrollbw = 7
243 ; scrollh = 12
244 ; icase = true
245 ; preload = true
246 ; pagebias = 0
247 ; verbose = false
248 ; scrollstep = 24
249 ; maxhfit = true
250 ; crophack = false
251 ; autoscrollstep = 2
252 ; showall = false
253 ; hlinks = false
254 ; underinfo = false
255 ; interpagespace = 2
256 ; zoom = 1.0
257 ; presentation = false
258 ; angle = 0
259 ; winw = 900
260 ; winh = 900
261 ; savebmarks = true
262 ; proportional = true
263 ; memlimit = 32*1024*1024
264 ; texcount = 256
265 ; sliceheight = 24
266 ; blockwidth = 2048
267 ; thumbw = 76
268 ; jumpback = false
269 ; bgcolor = (0.5, 0.5, 0.5)
270 ; bedefault = false
271 ; scrollbarinpm = true
275 let conf = { defconf with angle = defconf.angle };;
277 let makehelp () =
278 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
279 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
282 let state =
283 { csock = Unix.stdin
284 ; ssock = Unix.stdin
285 ; x = 0
286 ; y = 0
287 ; w = 0
288 ; scrollw = 0
289 ; anchor = emptyanchor
290 ; layout = []
291 ; maxy = max_int
292 ; pagelru = Queue.create ()
293 ; pagemap = Hashtbl.create 10
294 ; pdims = []
295 ; pagecount = 0
296 ; rendering = false
297 ; mstate = Mnone
298 ; rects = []
299 ; rects1 = []
300 ; text = ""
301 ; mode = View
302 ; fullscreen = None
303 ; searchpattern = ""
304 ; outlines = Olist []
305 ; bookmarks = []
306 ; path = ""
307 ; password = ""
308 ; invalidated = 0
309 ; hists =
310 { nav = cbnew 100 (0, 0.0)
311 ; pat = cbnew 20 ""
312 ; pag = cbnew 10 ""
314 ; colorscale = 1.0
315 ; memused = 0
316 ; gen = 0
317 ; throttle = None
318 ; autoscroll = None
319 ; help = makehelp ()
320 ; docinfo = []
321 ; deadline = nan
325 let vlog fmt =
326 if conf.verbose
327 then
328 Printf.kprintf prerr_endline fmt
329 else
330 Printf.kprintf ignore fmt
333 let writecmd fd s =
334 let len = String.length s in
335 let n = 4 + len in
336 let b = Buffer.create n in
337 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
338 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
339 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
340 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
341 Buffer.add_string b s;
342 let s' = Buffer.contents b in
343 let n' = Unix.write fd s' 0 n in
344 if n' != n then failwith "write failed";
347 let readcmd fd =
348 let s = "xxxx" in
349 let n = Unix.read fd s 0 4 in
350 if n != 4 then failwith "incomplete read(len)";
351 let len = 0
352 lor (Char.code s.[0] lsl 24)
353 lor (Char.code s.[1] lsl 16)
354 lor (Char.code s.[2] lsl 8)
355 lor (Char.code s.[3] lsl 0)
357 let s = String.create len in
358 let n = Unix.read fd s 0 len in
359 if n != len then failwith "incomplete read(data)";
363 let makecmd s l =
364 let b = Buffer.create 10 in
365 Buffer.add_string b s;
366 let rec combine = function
367 | [] -> b
368 | x :: xs ->
369 Buffer.add_char b ' ';
370 let s =
371 match x with
372 | `b b -> if b then "1" else "0"
373 | `s s -> s
374 | `i i -> string_of_int i
375 | `f f -> string_of_float f
376 | `I f -> string_of_int (truncate f)
378 Buffer.add_string b s;
379 combine xs;
381 combine l;
384 let wcmd s l =
385 let cmd = Buffer.contents (makecmd s l) in
386 writecmd state.csock cmd;
389 let calcips h =
390 if conf.presentation
391 then
392 let d = conf.winh - h in
393 max 0 ((d + 1) / 2)
394 else
395 conf.interpagespace
398 let calcheight () =
399 let rec f pn ph pi fh l =
400 match l with
401 | (n, _, h, _) :: rest ->
402 let ips = calcips h in
403 let fh =
404 if conf.presentation
405 then fh+ips
406 else (
407 if isbirdseye state.mode && pn = 0
408 then fh + ips
409 else fh
412 let fh = fh + ((n - pn) * (ph + pi)) in
413 f n h ips fh rest;
415 | [] ->
416 let inc =
417 if conf.presentation || (isbirdseye state.mode && pn = 0)
418 then 0
419 else -pi
421 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
422 max 0 fh
424 let fh = f 0 0 0 0 state.pdims in
428 let getpageyh pageno =
429 let rec f pn ph pi y l =
430 match l with
431 | (n, _, h, _) :: rest ->
432 let ips = calcips h in
433 if n >= pageno
434 then
435 let h = if n = pageno then h else ph in
436 if conf.presentation && n = pageno
437 then
438 y + (pageno - pn) * (ph + pi) + pi, h
439 else
440 y + (pageno - pn) * (ph + pi), h
441 else
442 let y = y + (if conf.presentation then pi else 0) in
443 let y = y + (n - pn) * (ph + pi) in
444 f n h ips y rest
446 | [] ->
447 y + (pageno - pn) * (ph + pi), ph
449 f 0 0 0 0 state.pdims
452 let getpagey pageno = fst (getpageyh pageno);;
454 let layout y sh =
455 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
456 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
457 match pdims with
458 | (pageno', w, h, x) :: rest when pageno' = pageno ->
459 let ips = calcips h in
460 let yinc =
461 if conf.presentation || (isbirdseye state.mode && pageno = 0)
462 then ips
463 else 0
465 (w, h, ips, x), rest, pdimno + 1, yinc
466 | _ ->
467 prev, pdims, pdimno, 0
469 let dy = dy + yinc in
470 let py = py + yinc in
471 if pageno = state.pagecount || dy >= sh
472 then
473 accu
474 else
475 let vy = y + dy in
476 if py + h <= vy - yinc
477 then
478 let py = py + h + ips in
479 let dy = max 0 (py - y) in
480 f ~pageno:(pageno+1)
481 ~pdimno
482 ~prev:curr
485 ~pdims:rest
486 ~accu
487 else
488 let pagey = vy - py in
489 let pagevh = h - pagey in
490 let pagevh = min (sh - dy) pagevh in
491 let off = if yinc > 0 then py - vy else 0 in
492 let py = py + h + ips in
493 let e =
494 { pageno = pageno
495 ; pagedimno = pdimno
496 ; pagew = w
497 ; pageh = h
498 ; pagedispy = dy + off
499 ; pagey = pagey + off
500 ; pagevh = pagevh - off
501 ; pagex = x
504 let accu = e :: accu in
505 f ~pageno:(pageno+1)
506 ~pdimno
507 ~prev:curr
509 ~dy:(dy+pagevh+ips)
510 ~pdims:rest
511 ~accu
513 if state.invalidated = 0
514 then (
515 let accu =
517 ~pageno:0
518 ~pdimno:~-1
519 ~prev:(0,0,0,0)
520 ~py:0
521 ~dy:0
522 ~pdims:state.pdims
523 ~accu:[]
525 List.rev accu
527 else
531 let clamp incr =
532 let y = state.y + incr in
533 let y = max 0 y in
534 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
538 let getopaque pageno =
539 try Some (Hashtbl.find state.pagemap
540 (pageno, state.w, conf.angle, conf.proportional, state.gen))
541 with Not_found -> None
544 let cache pageno opaque =
545 Hashtbl.replace state.pagemap
546 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
549 let validopaque opaque = String.length opaque > 0;;
551 let render l =
552 match getopaque l.pageno with
553 | None when not state.rendering ->
554 state.rendering <- true;
555 cache l.pageno ("", -1);
556 wcmd "render" [`i (l.pageno + 1)
557 ;`i l.pagedimno
558 ;`i l.pagew
559 ;`i l.pageh];
560 | _ -> ()
563 let loadlayout layout =
564 let rec f all = function
565 | l :: ls ->
566 begin match getopaque l.pageno with
567 | None -> render l; f false ls
568 | Some (opaque, _) -> f (all && validopaque opaque) ls
570 | [] -> all
572 f (layout <> []) layout;
575 let findpageforopaque opaque =
576 Hashtbl.fold
577 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
578 state.pagemap None
581 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
583 let preload () =
584 let oktopreload =
585 if conf.preload && not state.rendering
586 then conf.memlimit > state.memused
587 else false
589 if oktopreload
590 then
591 let presentation = conf.presentation in
592 let interpagespace = conf.interpagespace in
593 let maxy = state.maxy in
594 conf.presentation <- false;
595 conf.interpagespace <- 0;
596 state.maxy <- calcheight ();
597 let y =
598 match state.layout with
599 | [] -> 0
600 | l :: _ -> getpagey l.pageno + l.pagey
602 let y = if y < conf.winh then 0 else y - conf.winh in
603 let h = state.y - y + conf.winh*3 in
604 let pages = layout y h in
605 List.iter render pages;
606 conf.presentation <- presentation;
607 conf.interpagespace <- interpagespace;
608 state.maxy <- maxy;
611 let gotoy y =
612 let y = max 0 y in
613 let y = min state.maxy y in
614 let pages = layout y conf.winh in
615 let ready = loadlayout pages in
616 if conf.showall
617 then (
618 if ready
619 then (
620 state.y <- y;
621 state.layout <- pages;
622 state.throttle <- None;
623 Glut.postRedisplay ();
625 else (
626 state.throttle <- Some pages;
629 else (
630 state.y <- y;
631 state.layout <- pages;
632 state.throttle <- None;
633 Glut.postRedisplay ();
635 begin match state.mode with
636 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
637 if not (pagevisible pages pageno)
638 then (
639 match state.layout with
640 | [] -> ()
641 | l :: _ ->
642 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
644 | _ -> ()
645 end;
646 preload ();
649 let gotoy_and_clear_text y =
650 gotoy y;
651 if not conf.verbose then state.text <- "";
654 let getanchor () =
655 match state.layout with
656 | [] -> emptyanchor
657 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
660 let getanchory (n, top) =
661 let y, h = getpageyh n in
662 y + (truncate (top *. float h));
665 let gotoanchor anchor =
666 gotoy (getanchory anchor);
669 let addnav () =
670 cbput state.hists.nav (getanchor ());
673 let getnav dir =
674 let anchor = cbgetc state.hists.nav dir in
675 getanchory anchor;
678 let gotopage n top =
679 let y, h = getpageyh n in
680 gotoy_and_clear_text (y + (truncate (top *. float h)));
683 let gotopage1 n top =
684 let y = getpagey n in
685 gotoy_and_clear_text (y + top);
688 let invalidate () =
689 state.layout <- [];
690 state.pdims <- [];
691 state.rects <- [];
692 state.rects1 <- [];
693 state.invalidated <- state.invalidated + 1;
696 let scalecolor c =
697 let c = c *. state.colorscale in
698 (c, c, c);
701 let scalecolor2 (r, g, b) =
702 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
705 let represent () =
706 state.maxy <- calcheight ();
707 match state.mode with
708 | Birdseye (_, _, pageno, _, _) ->
709 let y, h = getpageyh pageno in
710 let top = (conf.winh - h) / 2 in
711 gotoy (max 0 (y - top))
712 | _ -> gotoanchor state.anchor
715 let pagematrix () =
716 GlMat.mode `projection;
717 GlMat.load_identity ();
718 GlMat.rotate ~x:1.0 ~angle:180.0 ();
719 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
720 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
721 if state.x != 0
722 then (
723 GlMat.translate ~x:(float state.x) ();
727 let winmatrix () =
728 GlMat.mode `projection;
729 GlMat.load_identity ();
730 GlMat.rotate ~x:1.0 ~angle:180.0 ();
731 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
732 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
735 let reshape =
736 let firsttime = ref true in
737 fun ~w ~h ->
738 if state.invalidated = 0 && not !firsttime
739 then state.anchor <- getanchor ();
741 firsttime := false;
742 conf.winw <- w;
743 let w = truncate (float w *. conf.zoom) - state.scrollw in
744 let w = max w 2 in
745 state.w <- w;
746 conf.winh <- h;
747 GlMat.mode `modelview;
748 GlMat.load_identity ();
749 GlClear.color (scalecolor 1.0);
750 GlClear.clear [`color];
752 invalidate ();
753 wcmd "geometry" [`i w; `i h];
756 let drawstring size x y s =
757 Gl.enable `blend;
758 Gl.enable `texture_2d;
759 ignore (drawstr size x y s);
760 Gl.disable `blend;
761 Gl.disable `texture_2d;
764 let drawstring1 size x y s =
765 drawstr size x y s;
768 let enttext () =
769 let len = String.length state.text in
770 let drawstring s =
771 GlDraw.color (0.0, 0.0, 0.0);
772 GlDraw.rect
773 (0.0, float (conf.winh - 18))
774 (float (conf.winw - state.scrollw - 1), float conf.winh)
776 GlDraw.color (1.0, 1.0, 1.0);
777 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
779 match state.mode with
780 | Textentry ((prefix, text, _, _, _), _) ->
781 let s =
782 if len > 0
783 then
784 Printf.sprintf "%s%s_ [%s]" prefix text state.text
785 else
786 Printf.sprintf "%s%s_" prefix text
788 drawstring s
790 | _ ->
791 if len > 0 then drawstring state.text
794 let showtext c s =
795 state.text <- Printf.sprintf "%c%s" c s;
796 Glut.postRedisplay ();
799 let act cmd =
800 match cmd.[0] with
801 | 'c' ->
802 state.pdims <- [];
804 | 'D' ->
805 state.rects <- state.rects1;
806 Glut.postRedisplay ()
808 | 'C' ->
809 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
810 state.pagecount <- n;
811 state.invalidated <- state.invalidated - 1;
812 if state.invalidated = 0
813 then represent ()
815 | 't' ->
816 let s = Scanf.sscanf cmd "t %n"
817 (fun n -> String.sub cmd n (String.length cmd - n))
819 Glut.setWindowTitle s
821 | 'T' ->
822 let s = Scanf.sscanf cmd "T %n"
823 (fun n -> String.sub cmd n (String.length cmd - n))
825 if istextentry state.mode
826 then (
827 state.text <- s;
828 showtext ' ' s;
830 else (
831 state.text <- s;
832 Glut.postRedisplay ();
835 | 'V' ->
836 if conf.verbose
837 then
838 let s = Scanf.sscanf cmd "V %n"
839 (fun n -> String.sub cmd n (String.length cmd - n))
841 state.text <- s;
842 showtext ' ' s;
844 | 'F' ->
845 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
846 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
847 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
848 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
850 let y = (getpagey pageno) + truncate y0 in
851 addnav ();
852 gotoy y;
853 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
855 | 'R' ->
856 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
857 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
858 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
859 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
861 state.rects1 <-
862 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
864 | 'r' ->
865 let n, w, _h, r, l, s, p =
866 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
867 (fun n w h r l s p ->
868 (n-1, w, h, r, l != 0, s, p))
871 state.memused <- state.memused + s;
873 let layout =
874 match state.throttle with
875 | None -> state.layout
876 | Some layout -> layout
879 let rec gc () =
880 if state.memused <= conf.memlimit || Queue.is_empty state.pagelru
881 then ()
882 else
883 let opaque = Queue.peek state.pagelru in
884 match findpageforopaque opaque with
885 | None -> failwith "bug in gc"
886 | Some (pagekey, size) ->
887 let n, w, a, p, g = pagekey in
888 if w != state.w
889 || a != conf.angle
890 || p != conf.proportional
891 || g != state.gen
892 || not (pagevisible layout n)
893 then (
894 ignore (Queue.pop state.pagelru);
895 wcmd "free" [`s opaque];
896 Hashtbl.remove state.pagemap pagekey;
897 state.memused <- state.memused - size;
898 gc ();
901 gc ();
903 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
904 Queue.push p state.pagelru;
905 state.rendering <- false;
907 begin match state.throttle with
908 | None ->
909 if pagevisible state.layout n
910 then Glut.postRedisplay ();
911 let allvisible = loadlayout state.layout in
912 if allvisible then preload ()
914 | Some layout ->
915 match layout with
916 | [] -> ()
917 | l :: _ ->
918 let y = getpagey l.pageno + l.pagey in
919 gotoy y
922 | 'l' ->
923 let pdim =
924 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
926 state.pdims <- pdim :: state.pdims
928 | 'o' ->
929 let (l, n, t, h, pos) =
930 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
932 let s = String.sub cmd pos (String.length cmd - pos) in
933 let outline = (s, l, (n, float t /. float h)) in
934 let outlines =
935 match state.outlines with
936 | Olist outlines -> Olist (outline :: outlines)
937 | Oarray _ -> Olist [outline]
938 | Onarrow _ -> Olist [outline]
940 state.outlines <- outlines
943 | 'i' ->
944 if String.length cmd > 1 && cmd.[1] = 'e'
945 then
946 state.docinfo <- List.rev state.docinfo
947 else
948 let s = Scanf.sscanf cmd "i %n"
949 (fun n -> String.sub cmd n (String.length cmd - n))
951 state.docinfo <- (1, s) :: state.docinfo
953 | _ ->
954 dolog "unknown cmd `%S'" cmd
957 let now = Unix.gettimeofday;;
959 let idle () =
960 if state.deadline == nan then state.deadline <- now ();
961 let rec loop delay =
962 let timeout =
963 if delay > 0.0
964 then max 0.0 (state.deadline -. now ())
965 else 0.0
967 let r, _, _ = Unix.select [state.csock] [] [] timeout in
968 begin match r with
969 | [] ->
970 begin match state.autoscroll with
971 | Some step when step != 0 ->
972 let y = state.y + step in
973 let y =
974 if y < 0
975 then state.maxy
976 else if y >= state.maxy then 0 else y
978 gotoy y;
979 if state.mode = View
980 then state.text <- "";
981 state.deadline <- state.deadline +. 0.005;
983 | _ ->
984 state.deadline <- state.deadline +. delay;
985 end;
987 | _ ->
988 let cmd = readcmd state.csock in
989 act cmd;
990 loop 0.0
991 end;
992 in loop 0.007
995 let onhist cb =
996 let rc = cb.rc in
997 let action = function
998 | HCprev -> cbget cb ~-1
999 | HCnext -> cbget cb 1
1000 | HCfirst -> cbget cb ~-(cb.rc)
1001 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1002 and cancel () = cb.rc <- rc
1003 in (action, cancel)
1006 let search pattern forward =
1007 if String.length pattern > 0
1008 then
1009 let pn, py =
1010 match state.layout with
1011 | [] -> 0, 0
1012 | l :: _ ->
1013 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1015 let cmd =
1016 let b = makecmd "search"
1017 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1019 Buffer.add_char b ',';
1020 Buffer.add_string b pattern;
1021 Buffer.add_char b '\000';
1022 Buffer.contents b;
1024 writecmd state.csock cmd;
1027 let intentry text key =
1028 let c = Char.unsafe_chr key in
1029 match c with
1030 | '0' .. '9' ->
1031 let s = "x" in s.[0] <- c;
1032 let text = text ^ s in
1033 TEcont text
1035 | _ ->
1036 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1037 TEcont text
1040 let addchar s c =
1041 let b = Buffer.create (String.length s + 1) in
1042 Buffer.add_string b s;
1043 Buffer.add_char b c;
1044 Buffer.contents b;
1047 let textentry text key =
1048 let c = Char.unsafe_chr key in
1049 match c with
1050 | _ when key >= 32 && key < 127 ->
1051 let text = addchar text c in
1052 TEcont text
1054 | _ ->
1055 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1056 TEcont text
1059 let reinit angle proportional =
1060 state.anchor <- getanchor ();
1061 conf.angle <- angle;
1062 conf.proportional <- proportional;
1063 invalidate ();
1064 wcmd "reinit" [`i angle; `b proportional];
1067 let setzoom zoom =
1068 let zoom = max 0.01 zoom in
1069 if zoom <> conf.zoom
1070 then (
1071 if zoom <= 1.0
1072 then state.x <- 0;
1073 conf.zoom <- zoom;
1074 reshape conf.winw conf.winh;
1075 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1079 let enterbirdseye () =
1080 let zoom = float conf.thumbw /. float conf.winw in
1081 let birdseyepageno =
1082 let cy = conf.winh / 2 in
1083 let fold = function
1084 | [] -> 0
1085 | l :: rest ->
1086 let rec fold best = function
1087 | [] -> best.pageno
1088 | l :: rest ->
1089 let d = cy - (l.pagedispy + l.pagevh/2)
1090 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1091 if abs d < abs dbest
1092 then fold l rest
1093 else best.pageno
1094 in fold l rest
1096 fold state.layout
1098 state.mode <- Birdseye (
1099 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1101 conf.zoom <- zoom;
1102 conf.presentation <- false;
1103 conf.interpagespace <- 10;
1104 conf.hlinks <- false;
1105 state.x <- 0;
1106 state.mstate <- Mnone;
1107 conf.showall <- false;
1108 Glut.setCursor Glut.CURSOR_INHERIT;
1109 if conf.verbose
1110 then
1111 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1112 (100.0*.zoom)
1113 else
1114 state.text <- ""
1116 reshape conf.winw conf.winh;
1119 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1120 state.mode <- View;
1121 conf.zoom <- c.zoom;
1122 conf.presentation <- c.presentation;
1123 conf.interpagespace <- c.interpagespace;
1124 conf.showall <- c.showall;
1125 conf.hlinks <- c.hlinks;
1126 state.x <- leftx;
1127 if conf.verbose
1128 then
1129 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1130 (100.0*.conf.zoom)
1132 reshape conf.winw conf.winh;
1133 state.anchor <- if goback then anchor else (pageno, 0.0);
1136 let togglebirdseye () =
1137 match state.mode with
1138 | Birdseye vals -> leavebirdseye vals true
1139 | View | Outline _ -> enterbirdseye ()
1140 | _ -> ()
1143 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1144 let pageno = max 0 (pageno - 1) in
1145 let rec loop = function
1146 | [] -> gotopage1 pageno 0
1147 | l :: _ when l.pageno = pageno ->
1148 if l.pagedispy >= 0 && l.pagey = 0
1149 then Glut.postRedisplay ()
1150 else gotopage1 pageno 0
1151 | _ :: rest -> loop rest
1153 loop state.layout;
1154 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1157 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1158 let pageno = min (state.pagecount - 1) (pageno + 1) in
1159 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1160 let rec loop = function
1161 | [] ->
1162 let y, h = getpageyh pageno in
1163 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1164 gotoy (clamp dy)
1165 | l :: _ when l.pageno = pageno ->
1166 if l.pagevh != l.pageh
1167 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1168 else Glut.postRedisplay ()
1169 | _ :: rest -> loop rest
1171 loop state.layout
1174 let optentry mode _ key =
1175 let btos b = if b then "on" else "off" in
1176 let c = Char.unsafe_chr key in
1177 match c with
1178 | 's' ->
1179 let ondone s =
1180 try conf.scrollstep <- int_of_string s with exc ->
1181 state.text <- Printf.sprintf "bad integer `%s': %s"
1182 s (Printexc.to_string exc)
1184 TEswitch ("scroll step: ", "", None, intentry, ondone)
1186 | 'A' ->
1187 let ondone s =
1189 conf.autoscrollstep <- int_of_string s;
1190 if state.autoscroll <> None
1191 then state.autoscroll <- Some conf.autoscrollstep
1192 with exc ->
1193 state.text <- Printf.sprintf "bad integer `%s': %s"
1194 s (Printexc.to_string exc)
1196 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1198 | 'Z' ->
1199 let ondone s =
1201 let zoom = float (int_of_string s) /. 100.0 in
1202 setzoom zoom
1203 with exc ->
1204 state.text <- Printf.sprintf "bad integer `%s': %s"
1205 s (Printexc.to_string exc)
1207 TEswitch ("zoom: ", "", None, intentry, ondone)
1209 | 't' ->
1210 let ondone s =
1212 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1213 state.text <-
1214 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1215 begin match mode with
1216 | Birdseye beye ->
1217 leavebirdseye beye false;
1218 enterbirdseye ();
1219 | _ -> ();
1221 with exc ->
1222 state.text <- Printf.sprintf "bad integer `%s': %s"
1223 s (Printexc.to_string exc)
1225 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1227 | 'R' ->
1228 let ondone s =
1229 match try
1230 Some (int_of_string s)
1231 with exc ->
1232 state.text <- Printf.sprintf "bad integer `%s': %s"
1233 s (Printexc.to_string exc);
1234 None
1235 with
1236 | Some angle -> reinit angle conf.proportional
1237 | None -> ()
1239 TEswitch ("rotation: ", "", None, intentry, ondone)
1241 | 'i' ->
1242 conf.icase <- not conf.icase;
1243 TEdone ("case insensitive search " ^ (btos conf.icase))
1245 | 'p' ->
1246 conf.preload <- not conf.preload;
1247 gotoy state.y;
1248 TEdone ("preload " ^ (btos conf.preload))
1250 | 'v' ->
1251 conf.verbose <- not conf.verbose;
1252 TEdone ("verbose " ^ (btos conf.verbose))
1254 | 'h' ->
1255 conf.maxhfit <- not conf.maxhfit;
1256 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1257 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1259 | 'c' ->
1260 conf.crophack <- not conf.crophack;
1261 TEdone ("crophack " ^ btos conf.crophack)
1263 | 'a' ->
1264 conf.showall <- not conf.showall;
1265 TEdone ("throttle " ^ btos conf.showall)
1267 | 'f' ->
1268 conf.underinfo <- not conf.underinfo;
1269 TEdone ("underinfo " ^ btos conf.underinfo)
1271 | 'P' ->
1272 conf.savebmarks <- not conf.savebmarks;
1273 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1275 | 'S' ->
1276 let ondone s =
1278 let pageno, py =
1279 match state.layout with
1280 | [] -> 0, 0
1281 | l :: _ ->
1282 l.pageno, l.pagey
1284 conf.interpagespace <- int_of_string s;
1285 state.maxy <- calcheight ();
1286 let y = getpagey pageno in
1287 gotoy (y + py)
1288 with exc ->
1289 state.text <- Printf.sprintf "bad integer `%s': %s"
1290 s (Printexc.to_string exc)
1292 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1294 | 'l' ->
1295 reinit conf.angle (not conf.proportional);
1296 TEdone ("proprortional display " ^ btos conf.proportional)
1298 | _ ->
1299 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1300 TEstop
1303 let maxoutlinerows () = (conf.winh - 31) / 16;;
1305 let enterselector allowdel outlines errmsg msg =
1306 if Array.length outlines = 0
1307 then (
1308 showtext ' ' errmsg;
1310 else (
1311 state.text <- msg;
1312 Glut.setCursor Glut.CURSOR_INHERIT;
1313 let pageno =
1314 match state.layout with
1315 | [] -> -1
1316 | {pageno=pageno} :: _ -> pageno
1318 let active =
1319 let rec loop n =
1320 if n = Array.length outlines
1321 then 0
1322 else
1323 let (_, _, (outlinepageno, _)) = outlines.(n) in
1324 if outlinepageno >= pageno then n else loop (n+1)
1326 loop 0
1328 state.mode <- Outline
1329 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1330 state.mode);
1331 Glut.postRedisplay ();
1335 let enteroutlinemode () =
1336 let outlines, msg =
1337 match state.outlines with
1338 | Oarray a -> a, ""
1339 | Olist l ->
1340 let a = Array.of_list (List.rev l) in
1341 state.outlines <- Oarray a;
1342 a, ""
1343 | Onarrow (pat, a, _) ->
1344 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1346 enterselector false outlines "Document has no outline" msg;
1349 let enterbookmarkmode () =
1350 let bookmarks = Array.of_list state.bookmarks in
1351 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1354 let mode_to_string mode =
1355 let b = Buffer.create 10 in
1356 let rec f = function
1357 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1358 | View -> Buffer.add_string b "View"
1359 | Birdseye _ -> Buffer.add_string b "Birdseye"
1360 | Items _ -> Buffer.add_string b "Items"
1361 | Outline _ -> Buffer.add_string b "Outline"
1363 f mode;
1364 Buffer.contents b;
1367 let color_of_string s =
1368 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1369 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1373 let color_to_string (r, g, b) =
1374 let r = truncate (r *. 256.0)
1375 and g = truncate (g *. 256.0)
1376 and b = truncate (b *. 256.0) in
1377 Printf.sprintf "%d/%d/%d" r g b
1380 let enterinfomode () =
1381 let btos = function true -> "on" | _ -> "off" in
1382 let mode = state.mode in
1383 let rec makeitems () =
1384 let intp name get set =
1385 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1386 fun active first _ pan ->
1387 let ondone s =
1388 let n =
1389 try int_of_string s
1390 with exn ->
1391 state.text <- Printf.sprintf "bad integer `%s': %s"
1392 s (Printexc.to_string exn);
1393 max_int;
1395 if n != max_int then set n;
1397 let te = name ^ ": ", "", None, intentry, ondone in
1398 state.text <- "";
1399 Textentry (
1401 fun _ ->
1402 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1405 and boolp name get set =
1406 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1407 fun active first qsearch pan ->
1408 let v = get () in
1409 set (not v);
1410 Items (active, first, makeitems (), qsearch, pan, mode);
1412 and colorp name get set =
1413 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1414 fun active first _ pan ->
1415 let invalid = (nan, nan, nan) in
1416 let ondone s =
1417 let c =
1418 try color_of_string s
1419 with exn ->
1420 state.text <- Printf.sprintf "bad color `%s': %s"
1421 s (Printexc.to_string exn);
1422 invalid
1424 if c <> invalid
1425 then set c;
1427 let te = name ^ ": ", "", None, textentry, ondone in
1428 state.text <- "";
1429 Textentry (
1431 fun _ ->
1432 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1437 let birdseye = isbirdseye mode in
1438 let items = [
1439 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1441 boolp "presentation"
1442 (fun () -> conf.presentation)
1443 (fun v ->
1444 conf.presentation <- v;
1445 state.anchor <- getanchor ();
1446 represent ());
1448 boolp "ignore case in searches"
1449 (fun () -> conf.icase)
1450 (fun v -> conf.icase <- v);
1452 boolp "preload"
1453 (fun () -> conf.preload)
1454 (fun v -> conf.preload <- v);
1456 boolp "verbose"
1457 (fun () -> conf.verbose)
1458 (fun v -> conf.verbose <- v);
1460 boolp "max fit"
1461 (fun () -> conf.maxhfit)
1462 (fun v -> conf.maxhfit <- v);
1464 boolp "crop hack"
1465 (fun () -> conf.crophack)
1466 (fun v -> conf.crophack <- v);
1468 boolp "throttle"
1469 (fun () -> conf.showall)
1470 (fun v -> conf.showall <- v);
1472 boolp "highlight links"
1473 (fun () -> conf.hlinks)
1474 (fun v -> conf.hlinks <- v);
1476 boolp "under info"
1477 (fun () -> conf.underinfo)
1478 (fun v -> conf.underinfo <- v);
1479 boolp "persistent bookmarks"
1480 (fun () -> conf.savebmarks)
1481 (fun v -> conf.savebmarks <- v);
1483 boolp "proportional display"
1484 (fun () -> conf.proportional)
1485 (fun v -> reinit conf.angle v);
1487 boolp "persistent location"
1488 (fun () -> conf.jumpback)
1489 (fun v -> conf.jumpback <- v);
1491 "", 0, Noaction;
1493 intp "vertical margin"
1494 (fun () -> conf.interpagespace)
1495 (fun n ->
1496 conf.interpagespace <- n;
1497 let pageno, py =
1498 match state.layout with
1499 | [] -> 0, 0
1500 | l :: _ ->
1501 l.pageno, l.pagey
1503 state.maxy <- calcheight ();
1504 let y = getpagey pageno in
1505 gotoy (y + py)
1508 intp "page bias"
1509 (fun () -> conf.pagebias)
1510 (fun v -> conf.pagebias <- v);
1512 intp "scroll step"
1513 (fun () -> conf.scrollstep)
1514 (fun n -> conf.scrollstep <- n);
1516 intp "auto scroll step"
1517 (fun () ->
1518 match state.autoscroll with
1519 | Some step -> step
1520 | _ -> conf.autoscrollstep)
1521 (fun n ->
1522 if state.autoscroll <> None
1523 then state.autoscroll <- Some n;
1524 conf.autoscrollstep <- n);
1526 intp "zoom"
1527 (fun () -> truncate (conf.zoom *. 100.))
1528 (fun v -> setzoom ((float v) /. 100.));
1530 intp "rotation"
1531 (fun () -> conf.angle)
1532 (fun v -> reinit v conf.proportional);
1534 intp "scroll bar width"
1535 (fun () -> state.scrollw)
1536 (fun v ->
1537 state.scrollw <- v;
1538 conf.scrollbw <- v;
1539 reshape conf.winw conf.winh;
1542 intp "scroll handle height"
1543 (fun () -> conf.scrollh)
1544 (fun v -> conf.scrollh <- v;);
1546 intp "thumbnail width"
1547 (fun () -> conf.thumbw)
1548 (fun v ->
1549 conf.thumbw <- min 1920 v;
1550 match mode with
1551 | Birdseye beye ->
1552 leavebirdseye beye false;
1553 enterbirdseye ()
1554 | _ -> ()
1557 colorp "background color"
1558 (fun () -> conf.bgcolor)
1559 (fun v -> conf.bgcolor <- v);
1561 "", 0, Noaction;
1562 "Presentation mode", 0, Noaction;
1564 boolp "scrollbar visible"
1565 (fun () -> conf.scrollbarinpm)
1566 (fun v ->
1567 if v != conf.scrollbarinpm
1568 then (
1569 conf.scrollbarinpm <- v;
1570 if conf.presentation
1571 then (
1572 state.scrollw <- if v then conf.scrollbw else 0;
1573 reshape conf.winw conf.winh;
1578 "", 0, Noaction;
1579 "Pixmap Cache", 0, Noaction;
1581 intp "size (advisory)"
1582 (fun () -> conf.memlimit)
1583 (fun v -> conf.memlimit <- v);
1584 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1588 let tailer =
1589 let trailer =
1590 ("", 0, Noaction)
1591 :: ("Document", 0, Noaction)
1592 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1594 if birdseye
1595 then trailer
1596 else (
1597 ("", 0, Noaction)
1598 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1599 (btos conf.bedefault),
1601 Action (
1602 fun active first qsearch pan ->
1603 conf.bedefault <- not conf.bedefault;
1604 Items (active, first, makeitems (), qsearch, pan, mode);
1606 ) :: trailer
1609 Array.of_list (items @ tailer)
1611 state.text <- "";
1612 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1613 Glut.postRedisplay ();
1616 let enterhelpmode () =
1617 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1618 Glut.postRedisplay ();
1621 let quickbookmark ?title () =
1622 match state.layout with
1623 | [] -> ()
1624 | l :: _ ->
1625 let title =
1626 match title with
1627 | None ->
1628 let sec = Unix.gettimeofday () in
1629 let tm = Unix.localtime sec in
1630 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1631 (l.pageno+1)
1632 tm.Unix.tm_mday
1633 tm.Unix.tm_mon
1634 (tm.Unix.tm_year + 1900)
1635 tm.Unix.tm_hour
1636 tm.Unix.tm_min
1637 | Some title -> title
1639 state.bookmarks <-
1640 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1641 :: state.bookmarks
1644 let doreshape w h =
1645 state.fullscreen <- None;
1646 Glut.reshapeWindow w h;
1649 let writeopen path password =
1650 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1653 let opendoc path password =
1654 invalidate ();
1655 state.path <- path;
1656 state.password <- password;
1657 state.gen <- state.gen + 1;
1658 state.docinfo <- [];
1660 writeopen path password;
1661 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1662 wcmd "geometry" [`i state.w; `i conf.winh];
1665 let viewkeyboard key =
1666 let enttext te =
1667 let mode = state.mode in
1668 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1669 state.text <- "";
1670 enttext ();
1671 Glut.postRedisplay ()
1673 let c = Char.chr key in
1674 match c with
1675 | '\027' | 'q' -> (* escape *)
1676 raise Quit
1678 | '\008' -> (* backspace *)
1679 let y = getnav ~-1 in
1680 gotoy_and_clear_text y
1682 | 'o' ->
1683 enteroutlinemode ()
1685 | 'u' ->
1686 state.rects <- [];
1687 state.text <- "";
1688 Glut.postRedisplay ()
1690 | '/' | '?' ->
1691 let ondone isforw s =
1692 cbput state.hists.pat s;
1693 state.searchpattern <- s;
1694 search s isforw
1696 let s = String.create 1 in
1697 s.[0] <- c;
1698 enttext (s, "", Some (onhist state.hists.pat),
1699 textentry, ondone (c ='/'))
1701 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1702 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1703 setzoom (conf.zoom +. incr)
1705 | '+' ->
1706 let ondone s =
1707 let n =
1708 try int_of_string s with exc ->
1709 state.text <- Printf.sprintf "bad integer `%s': %s"
1710 s (Printexc.to_string exc);
1711 max_int
1713 if n != max_int
1714 then (
1715 conf.pagebias <- n;
1716 state.text <- "page bias is now " ^ string_of_int n;
1719 enttext ("page bias: ", "", None, intentry, ondone)
1721 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1722 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1723 setzoom (max 0.01 (conf.zoom -. decr))
1725 | '-' ->
1726 let ondone msg = state.text <- msg in
1727 enttext (
1728 "option [acfhilpstvAPRSZ]: ", "", None,
1729 optentry state.mode, ondone
1732 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1733 setzoom 1.0
1735 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1736 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1737 if zoom < 1.0
1738 then setzoom zoom
1740 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1741 togglebirdseye ()
1743 | '0' .. '9' ->
1744 let ondone s =
1745 let n =
1746 try int_of_string s with exc ->
1747 state.text <- Printf.sprintf "bad integer `%s': %s"
1748 s (Printexc.to_string exc);
1751 if n >= 0
1752 then (
1753 addnav ();
1754 cbput state.hists.pag (string_of_int n);
1755 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1758 let pageentry text key =
1759 match Char.unsafe_chr key with
1760 | 'g' -> TEdone text
1761 | _ -> intentry text key
1763 let text = "x" in text.[0] <- c;
1764 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1766 | 'b' ->
1767 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1768 reshape conf.winw conf.winh;
1770 | 'l' ->
1771 conf.hlinks <- not conf.hlinks;
1772 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1773 Glut.postRedisplay ()
1775 | 'a' ->
1776 begin match state.autoscroll with
1777 | Some step ->
1778 conf.autoscrollstep <- step;
1779 state.autoscroll <- None
1780 | None ->
1781 if conf.autoscrollstep = 0
1782 then state.autoscroll <- Some 1
1783 else state.autoscroll <- Some conf.autoscrollstep
1786 | 'P' ->
1787 conf.presentation <- not conf.presentation;
1788 if conf.presentation
1789 then (
1790 if not conf.scrollbarinpm
1791 then state.scrollw <- 0;
1793 else
1794 state.scrollw <- conf.scrollbw;
1796 showtext ' ' ("presentation mode " ^
1797 if conf.presentation then "on" else "off");
1798 state.anchor <- getanchor ();
1799 represent ()
1801 | 'f' ->
1802 begin match state.fullscreen with
1803 | None ->
1804 state.fullscreen <- Some (conf.winw, conf.winh);
1805 Glut.fullScreen ()
1806 | Some (w, h) ->
1807 state.fullscreen <- None;
1808 doreshape w h
1811 | 'g' ->
1812 gotoy_and_clear_text 0
1814 | 'G' ->
1815 gotopage1 (state.pagecount - 1) 0
1817 | 'n' ->
1818 search state.searchpattern true
1820 | 'p' | 'N' ->
1821 search state.searchpattern false
1823 | 't' ->
1824 begin match state.layout with
1825 | [] -> ()
1826 | l :: _ ->
1827 gotoy_and_clear_text (getpagey l.pageno)
1830 | ' ' ->
1831 begin match List.rev state.layout with
1832 | [] -> ()
1833 | l :: _ ->
1834 let pageno = min (l.pageno+1) (state.pagecount-1) in
1835 gotoy_and_clear_text (getpagey pageno)
1838 | '\127' -> (* del *)
1839 begin match state.layout with
1840 | [] -> ()
1841 | l :: _ ->
1842 let pageno = max 0 (l.pageno-1) in
1843 gotoy_and_clear_text (getpagey pageno)
1846 | '=' ->
1847 let f (fn, _) l =
1848 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1850 let fn, ln = List.fold_left f (-1, -1) state.layout in
1851 let s =
1852 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1853 let percent =
1854 if maxy <= 0
1855 then 100.
1856 else (100. *. (float state.y /. float maxy)) in
1857 if fn = ln
1858 then
1859 Printf.sprintf "Page %d of %d %.2f%%"
1860 (fn+1) state.pagecount percent
1861 else
1862 Printf.sprintf
1863 "Pages %d-%d of %d %.2f%%"
1864 (fn+1) (ln+1) state.pagecount percent
1866 showtext ' ' s;
1868 | 'w' ->
1869 begin match state.layout with
1870 | [] -> ()
1871 | l :: _ ->
1872 doreshape (l.pagew + state.scrollw) l.pageh;
1873 Glut.postRedisplay ();
1876 | '\'' ->
1877 enterbookmarkmode ()
1879 | 'h' ->
1880 enterhelpmode ()
1882 | 'i' ->
1883 enterinfomode ()
1885 | 'm' ->
1886 let ondone s =
1887 match state.layout with
1888 | l :: _ ->
1889 state.bookmarks <-
1890 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1891 :: state.bookmarks
1892 | _ -> ()
1894 enttext ("bookmark: ", "", None, textentry, ondone)
1896 | '~' ->
1897 quickbookmark ();
1898 showtext ' ' "Quick bookmark added";
1900 | 'z' ->
1901 begin match state.layout with
1902 | l :: _ ->
1903 let rect = getpdimrect l.pagedimno in
1904 let w, h =
1905 if conf.crophack
1906 then
1907 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1908 truncate (1.2 *. (rect.(3) -. rect.(0))))
1909 else
1910 (truncate (rect.(1) -. rect.(0)),
1911 truncate (rect.(3) -. rect.(0)))
1913 if w != 0 && h != 0
1914 then
1915 doreshape (w + state.scrollw) (h + conf.interpagespace)
1917 Glut.postRedisplay ();
1919 | [] -> ()
1922 | '\000' -> (* ctrl-2 *)
1923 let maxw = getmaxw () in
1924 if maxw > 0.0
1925 then setzoom (maxw /. float conf.winw)
1927 | '<' | '>' ->
1928 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1930 | '[' | ']' ->
1931 state.colorscale <-
1932 max 0.0
1933 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1934 Glut.postRedisplay ()
1936 | 'k' ->
1937 begin match state.mode with
1938 | Birdseye beye -> upbirdseye beye
1939 | _ -> gotoy (clamp (-conf.scrollstep))
1942 | 'j' ->
1943 begin match state.mode with
1944 | Birdseye beye -> downbirdseye beye
1945 | _ -> gotoy (clamp conf.scrollstep)
1948 | 'r' ->
1949 state.anchor <- getanchor ();
1950 opendoc state.path state.password
1952 | _ ->
1953 vlog "huh? %d %c" key (Char.chr key);
1956 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1957 let enttext te =
1958 state.mode <- Textentry (te, onleave);
1959 state.text <- "";
1960 enttext ();
1961 Glut.postRedisplay ()
1963 match Char.unsafe_chr key with
1964 | '\008' -> (* backspace *)
1965 let len = String.length text in
1966 if len = 0
1967 then (
1968 onleave Cancel;
1969 Glut.postRedisplay ();
1971 else (
1972 let s = String.sub text 0 (len - 1) in
1973 enttext (c, s, opthist, onkey, ondone)
1976 | '\r' | '\n' ->
1977 ondone text;
1978 onleave Confirm;
1979 Glut.postRedisplay ()
1981 | '\007' (* ctrl-g *)
1982 | '\027' -> (* escape *)
1983 begin match opthist with
1984 | None -> ()
1985 | Some (_, onhistcancel) -> onhistcancel ()
1986 end;
1987 onleave Cancel;
1988 Glut.postRedisplay ()
1990 | _ ->
1991 begin match onkey text key with
1992 | TEdone text ->
1993 onleave Confirm;
1994 ondone text;
1995 Glut.postRedisplay ()
1997 | TEcont text ->
1998 enttext (c, text, opthist, onkey, ondone);
2000 | TEstop ->
2001 onleave Cancel;
2002 Glut.postRedisplay ()
2004 | TEswitch te ->
2005 state.mode <- Textentry (te, onleave);
2006 Glut.postRedisplay ()
2007 end;
2010 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2011 match key with
2012 | 27 -> (* escape *)
2013 leavebirdseye beye true
2015 | 12 -> (* ctrl-l *)
2016 let y, h = getpageyh pageno in
2017 let top = (conf.winh - h) / 2 in
2018 gotoy (max 0 (y - top))
2020 | 13 -> (* enter *)
2021 leavebirdseye beye false
2023 | _ ->
2024 viewkeyboard key
2027 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2028 let set active first qsearch =
2029 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2031 let search active pattern incr =
2032 let dosearch re =
2033 let rec loop n =
2034 if n >= Array.length items || n < 0
2035 then None
2036 else
2037 let (s, _, _) = items.(n) in
2039 (try ignore (Str.search_forward re s 0); true
2040 with Not_found -> false)
2041 then Some n
2042 else loop (n + incr)
2044 loop active
2047 let re = Str.regexp_case_fold pattern in
2048 dosearch re
2049 with Failure s ->
2050 state.text <- s;
2051 None
2053 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2054 match key with
2055 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2056 let incr = if key = 18 then -1 else 1 in
2057 let active, first =
2058 match search (active + incr) qsearch incr with
2059 | None ->
2060 state.text <- qsearch ^ " [not found]";
2061 active, first
2062 | Some active ->
2063 state.text <- qsearch;
2064 active, firstof active
2066 set active first qsearch;
2067 Glut.postRedisplay ();
2069 | 8 -> (* backspace *)
2070 let len = String.length qsearch in
2071 if len = 0
2072 then ()
2073 else (
2074 if len = 1
2075 then (
2076 state.text <- "";
2077 set active first "";
2079 else
2080 let qsearch = String.sub qsearch 0 (len - 1) in
2081 let active, first =
2082 match search active qsearch ~-1 with
2083 | None ->
2084 state.text <- qsearch ^ " [not found]";
2085 active, first
2086 | Some active ->
2087 state.text <- qsearch;
2088 active, firstof active
2090 set active first qsearch
2092 Glut.postRedisplay ()
2094 | _ when key >= 32 && key < 127 ->
2095 let pattern = addchar qsearch (Char.chr key) in
2096 let active, first =
2097 match search active pattern 1 with
2098 | None ->
2099 state.text <- pattern ^ " [not found]";
2100 active, first
2101 | Some active ->
2102 state.text <- pattern;
2103 active, firstof active
2105 set active first pattern;
2106 Glut.postRedisplay ()
2108 | 27 -> (* escape *)
2109 state.text <- "";
2110 if String.length qsearch = 0
2111 then (
2112 state.mode <- oldmode;
2114 else (
2115 set active first "";
2117 Glut.postRedisplay ()
2119 | 13 -> (* enter *)
2120 if active >= 0 && active < Array.length items
2121 then (
2122 match items.(active) with
2123 | _, _, Action f ->
2124 state.mode <- f active first qsearch pan
2126 | _, _, Noaction ->
2127 state.text <- "";
2128 state.mode <- oldmode
2130 else (
2131 state.text <- "";
2132 state.mode <- oldmode
2134 Glut.postRedisplay ();
2136 | _ -> dolog "unknown key %d" key
2139 let outlinekeyboard key
2140 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2141 let narrow outlines pattern =
2142 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2143 match reopt with
2144 | None -> None
2145 | Some re ->
2146 let rec fold accu n =
2147 if n = -1
2148 then accu
2149 else
2150 let (s, _, _) as o = outlines.(n) in
2151 let accu =
2152 if (try ignore (Str.search_forward re s 0); true
2153 with Not_found -> false)
2154 then (o :: accu)
2155 else accu
2157 fold accu (n-1)
2159 let matched = fold [] (Array.length outlines - 1) in
2160 if matched = [] then None else Some (Array.of_list matched)
2162 let search active pattern incr =
2163 let dosearch re =
2164 let rec loop n =
2165 if n = Array.length outlines || n = -1
2166 then None
2167 else
2168 let (s, _, _) = outlines.(n) in
2170 (try ignore (Str.search_forward re s 0); true
2171 with Not_found -> false)
2172 then Some n
2173 else loop (n + incr)
2175 loop active
2178 let re = Str.regexp_case_fold pattern in
2179 dosearch re
2180 with Failure s ->
2181 state.text <- s;
2182 None
2184 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2185 match key with
2186 | 27 -> (* escape *)
2187 state.text <- "";
2188 if String.length qsearch = 0
2189 then (
2190 state.mode <- oldmode;
2192 else (
2193 state.mode <- Outline (
2194 allowdel, active, first, outlines, "", pan, oldmode
2197 Glut.postRedisplay ();
2199 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2200 let incr = if key = 18 then -1 else 1 in
2201 let active, first =
2202 match search (active + incr) qsearch incr with
2203 | None ->
2204 state.text <- qsearch ^ " [not found]";
2205 active, first
2206 | Some active ->
2207 state.text <- qsearch;
2208 active, firstof active
2210 state.mode <- Outline (
2211 allowdel, active, first, outlines, qsearch, pan, oldmode
2213 Glut.postRedisplay ();
2215 | 8 -> (* backspace *)
2216 let len = String.length qsearch in
2217 if len = 0
2218 then ()
2219 else (
2220 if len = 1
2221 then (
2222 state.text <- "";
2223 state.mode <- Outline (
2224 allowdel, active, first, outlines, "", pan, oldmode
2227 else
2228 let qsearch = String.sub qsearch 0 (len - 1) in
2229 let active, first =
2230 match search active qsearch ~-1 with
2231 | None ->
2232 state.text <- qsearch ^ " [not found]";
2233 active, first
2234 | Some active ->
2235 state.text <- qsearch;
2236 active, firstof active
2238 state.mode <- Outline (
2239 allowdel, active, first, outlines, qsearch, pan, oldmode
2242 Glut.postRedisplay ()
2244 | 13 -> (* enter *)
2245 if active < Array.length outlines
2246 then (
2247 let (_, _, anchor) = outlines.(active) in
2248 addnav ();
2249 gotoanchor anchor;
2251 state.text <- "";
2252 if allowdel then state.bookmarks <- Array.to_list outlines;
2253 state.mode <- oldmode;
2254 Glut.postRedisplay ();
2256 | _ when key >= 32 && key < 127 ->
2257 let pattern = addchar qsearch (Char.chr key) in
2258 let active, first =
2259 match search active pattern 1 with
2260 | None ->
2261 state.text <- pattern ^ " [not found]";
2262 active, first
2263 | Some active ->
2264 state.text <- pattern;
2265 active, firstof active
2267 state.mode <- Outline (
2268 allowdel, active, first, outlines, pattern, pan, oldmode
2270 Glut.postRedisplay ()
2272 | 14 when not allowdel -> (* ctrl-n *)
2273 if String.length qsearch > 0
2274 then (
2275 let optoutlines = narrow outlines qsearch in
2276 begin match optoutlines with
2277 | None -> state.text <- "can't narrow"
2278 | Some outlines ->
2279 state.mode <- Outline (
2280 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2282 match state.outlines with
2283 | Olist _ -> ()
2284 | Oarray a ->
2285 state.outlines <- Onarrow (qsearch, outlines, a)
2286 | Onarrow (_, _, b) ->
2287 state.outlines <- Onarrow (qsearch, outlines, b)
2288 end;
2290 Glut.postRedisplay ()
2292 | 21 when not allowdel -> (* ctrl-u *)
2293 let outline =
2294 match state.outlines with
2295 | Oarray a -> a
2296 | Olist l ->
2297 let a = Array.of_list (List.rev l) in
2298 state.outlines <- Oarray a;
2300 | Onarrow (_, _, b) ->
2301 state.outlines <- Oarray b;
2302 state.text <- "";
2305 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2306 Glut.postRedisplay ()
2308 | 12 -> (* ctrl-l *)
2309 state.mode <- Outline
2310 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2311 Glut.postRedisplay ()
2313 | 127 when allowdel -> (* delete *)
2314 let len = Array.length outlines - 1 in
2315 if len = 0
2316 then (
2317 state.mode <- View;
2318 state.bookmarks <- [];
2320 else (
2321 let bookmarks = Array.init len
2322 (fun i ->
2323 let i = if i >= active then i + 1 else i in
2324 outlines.(i)
2327 state.mode <-
2328 Outline (
2329 allowdel,
2330 min active (len-1),
2331 min first (len-1),
2332 bookmarks, qsearch,
2334 oldmode
2337 Glut.postRedisplay ()
2339 | _ -> dolog "unknown key %d" key
2342 let keyboard ~key ~x ~y =
2343 ignore x;
2344 ignore y;
2345 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
2346 then
2347 wcmd "interrupt" []
2348 else
2349 match state.mode with
2350 | Outline outline -> outlinekeyboard key outline
2351 | Textentry textentry -> textentrykeyboard key textentry
2352 | Birdseye birdseye -> birdseyekeyboard key birdseye
2353 | View -> viewkeyboard key
2354 | Items items -> itemskeyboard key items
2357 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2358 match key with
2359 | Glut.KEY_UP -> upbirdseye beye
2360 | Glut.KEY_DOWN -> downbirdseye beye
2362 | Glut.KEY_PAGE_UP ->
2363 begin match state.layout with
2364 | l :: _ ->
2365 if l.pagey != 0
2366 then (
2367 state.mode <- Birdseye (
2368 conf, leftx, l.pageno, hooverpageno, anchor
2370 gotopage1 l.pageno 0;
2372 else (
2373 let layout = layout (state.y-conf.winh) conf.winh in
2374 match layout with
2375 | [] -> gotoy (clamp (-conf.winh))
2376 | l :: _ ->
2377 state.mode <- Birdseye (
2378 conf, leftx, l.pageno, hooverpageno, anchor
2380 gotopage1 l.pageno 0
2383 | [] -> gotoy (clamp (-conf.winh))
2384 end;
2386 | Glut.KEY_PAGE_DOWN ->
2387 begin match List.rev state.layout with
2388 | l :: _ ->
2389 let layout = layout (state.y + conf.winh) conf.winh in
2390 begin match layout with
2391 | [] ->
2392 let incr = l.pageh - l.pagevh in
2393 if incr = 0
2394 then (
2395 state.mode <-
2396 Birdseye (
2397 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2399 Glut.postRedisplay ();
2401 else gotoy (clamp (incr + conf.interpagespace*2));
2403 | l :: _ ->
2404 state.mode <-
2405 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2406 gotopage1 l.pageno 0;
2409 | [] -> gotoy (clamp conf.winh)
2410 end;
2412 | Glut.KEY_HOME ->
2413 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2414 gotopage1 0 0
2416 | Glut.KEY_END ->
2417 let pageno = state.pagecount - 1 in
2418 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2419 if not (pagevisible state.layout pageno)
2420 then
2421 let h =
2422 match List.rev state.pdims with
2423 | [] -> conf.winh
2424 | (_, _, h, _) :: _ -> h
2426 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2427 else Glut.postRedisplay ();
2428 | _ -> ()
2431 let setautoscrollspeed step goingdown =
2432 let incr = max 1 ((abs step) / 2) in
2433 let incr = if goingdown then incr else -incr in
2434 let astep = step + incr in
2435 state.autoscroll <- Some astep;
2438 let special ~key ~x ~y =
2439 ignore x;
2440 ignore y;
2441 match state.mode with
2442 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2443 togglebirdseye ()
2445 | Birdseye vals ->
2446 birdseyespecial key vals
2448 | View when key = Glut.KEY_F1 ->
2449 enterhelpmode ()
2451 | View ->
2452 begin match state.autoscroll with
2453 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2454 setautoscrollspeed step (key = Glut.KEY_DOWN)
2456 | _ ->
2457 let y =
2458 match key with
2459 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2460 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2461 | Glut.KEY_DOWN -> clamp conf.scrollstep
2462 | Glut.KEY_PAGE_UP ->
2463 if Glut.getModifiers () land Glut.active_ctrl != 0
2464 then
2465 match state.layout with
2466 | [] -> state.y
2467 | l :: _ -> state.y - l.pagey
2468 else
2469 clamp (-conf.winh)
2470 | Glut.KEY_PAGE_DOWN ->
2471 if Glut.getModifiers () land Glut.active_ctrl != 0
2472 then
2473 match List.rev state.layout with
2474 | [] -> state.y
2475 | l :: _ -> getpagey l.pageno
2476 else
2477 clamp conf.winh
2478 | Glut.KEY_HOME -> addnav (); 0
2479 | Glut.KEY_END ->
2480 addnav ();
2481 state.maxy - (if conf.maxhfit then conf.winh else 0)
2483 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2484 Glut.getModifiers () land Glut.active_alt != 0 ->
2485 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2487 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2488 state.x <- state.x - 10;
2489 state.y
2490 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2491 state.x <- state.x + 10;
2492 state.y
2494 | _ -> state.y
2496 gotoy_and_clear_text y
2499 | Textentry
2500 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2501 let s =
2502 match key with
2503 | Glut.KEY_UP -> action HCprev
2504 | Glut.KEY_DOWN -> action HCnext
2505 | Glut.KEY_HOME -> action HCfirst
2506 | Glut.KEY_END -> action HClast
2507 | _ -> state.text
2509 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2510 Glut.postRedisplay ()
2512 | Textentry _ -> ()
2514 | Items (active, first, items, qsearch, pan, oldmode) ->
2515 let maxrows = maxoutlinerows () in
2516 let itemcount = Array.length items in
2517 let hasaction = function
2518 | (_, _, Noaction) -> false
2519 | _ -> true
2521 let find start incr =
2522 let rec find i =
2523 if i = -1 || i = itemcount
2524 then -1
2525 else (
2526 if hasaction items.(i)
2527 then i
2528 else find (i + incr)
2531 find start
2533 let set active first =
2534 let first = max 0 (min first (itemcount - maxrows)) in
2535 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2537 let navigate incr =
2538 let isvisible first n = n >= first && n - first <= maxrows in
2539 let active, first =
2540 let incr1 = if incr > 0 then 1 else -1 in
2541 if isvisible first active
2542 then
2543 let next =
2544 let next = active + incr in
2545 let next =
2546 if next < 0 || next >= itemcount
2547 then -1
2548 else find next incr1
2550 if next = -1 || abs (active - next) > maxrows
2551 then -1
2552 else next
2554 if next = -1
2555 then
2556 let first = first + incr in
2557 let first = max 0 (min first (itemcount - 1)) in
2558 let next =
2559 let next = active + incr in
2560 let next = max 0 (min next (itemcount - 1)) in
2561 find next ~-incr1
2563 let active = if next = -1 then active else next in
2564 active, first
2565 else
2566 let first = min next first in
2567 next, first
2568 else
2569 let first = first + incr in
2570 let first = max 0 (min first (itemcount - 1)) in
2571 let active =
2572 let next = active + incr in
2573 let next = max 0 (min next (itemcount - 1)) in
2574 let next = find next incr1 in
2575 if next = -1 || abs (active - first) > maxrows
2576 then active
2577 else next
2579 active, first
2581 set active first;
2582 Glut.postRedisplay ()
2584 begin match key with
2585 | Glut.KEY_UP -> navigate ~-1
2586 | Glut.KEY_DOWN -> navigate 1
2587 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2588 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2590 | Glut.KEY_RIGHT ->
2591 state.mode <- Items (
2592 active, first, items, qsearch, min 0 (pan - 1), oldmode
2594 Glut.postRedisplay ()
2596 | Glut.KEY_LEFT ->
2597 state.mode <- Items (
2598 active, first, items, qsearch, min 0 (pan + 1), oldmode
2600 Glut.postRedisplay ()
2602 | Glut.KEY_HOME ->
2603 let active = find 0 1 in
2604 set active 0;
2605 Glut.postRedisplay ()
2607 | Glut.KEY_END ->
2608 let first = max 0 (itemcount - maxrows) in
2609 let active = find (itemcount - 1) ~-1 in
2610 set active first;
2611 Glut.postRedisplay ()
2613 | _ -> ()
2614 end;
2616 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2617 let maxrows = maxoutlinerows () in
2618 let calcfirst first active =
2619 if active > first
2620 then
2621 let rows = active - first in
2622 if rows > maxrows then active - maxrows else first
2623 else active
2625 let navigate incr =
2626 let active = active + incr in
2627 let active = max 0 (min active (Array.length outlines - 1)) in
2628 let first = calcfirst first active in
2629 state.mode <- Outline (
2630 allowdel, active, first, outlines, qsearch, pan, oldmode
2632 Glut.postRedisplay ()
2634 let updownlevel incr =
2635 let len = Array.length outlines in
2636 let (_, curlevel, _) = outlines.(active) in
2637 let rec flow i =
2638 if i = len then i-1 else if i = -1 then 0 else
2639 let (_, l, _) = outlines.(i) in
2640 if l != curlevel then i else flow (i+incr)
2642 let active = flow active in
2643 let first = calcfirst first active in
2644 state.mode <- Outline (
2645 allowdel, active, first, outlines, qsearch, pan, oldmode
2647 Glut.postRedisplay ()
2649 match key with
2650 | Glut.KEY_UP -> navigate ~-1
2651 | Glut.KEY_DOWN -> navigate 1
2652 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2653 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2655 | Glut.KEY_RIGHT ->
2656 if Glut.getModifiers () land Glut.active_ctrl != 0
2657 then (
2658 state.mode <- Outline (
2659 allowdel, active, first, outlines,
2660 qsearch, min 0 (pan + 1), oldmode
2662 Glut.postRedisplay ();
2664 else (
2665 if not allowdel
2666 then updownlevel 1
2669 | Glut.KEY_LEFT ->
2670 if Glut.getModifiers () land Glut.active_ctrl != 0
2671 then (
2672 state.mode <- Outline (
2673 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2675 Glut.postRedisplay ();
2677 else (
2678 if not allowdel
2679 then updownlevel ~-1
2682 | Glut.KEY_HOME ->
2683 state.mode <- Outline (
2684 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2686 Glut.postRedisplay ()
2688 | Glut.KEY_END ->
2689 let active = Array.length outlines - 1 in
2690 let first = max 0 (active - maxrows) in
2691 state.mode <- Outline (
2692 allowdel, active, first, outlines, qsearch, pan, oldmode
2694 Glut.postRedisplay ()
2696 | _ -> ()
2699 let drawplaceholder l =
2700 GlDraw.color (1.0, 1.0, 1.0);
2701 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2702 GlDraw.rect
2703 (float l.pagex, float l.pagedispy)
2704 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2706 let x = if margin < 0 then -margin else l.pagex
2707 and y = l.pagedispy + 13 in
2708 GlDraw.color (0.0, 0.0, 0.0);
2709 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2712 let now () = Unix.gettimeofday ();;
2714 let drawpage l =
2715 let color =
2716 match state.mode with
2717 | Textentry _ -> scalecolor 0.4
2718 | View | Outline _ | Items _ -> scalecolor 1.0
2719 | Birdseye (_, _, pageno, hooverpageno, _) ->
2720 if l.pageno = hooverpageno
2721 then scalecolor 0.9
2722 else (
2723 if l.pageno = pageno
2724 then scalecolor 1.0
2725 else scalecolor 0.8
2728 GlDraw.color color;
2729 begin match getopaque l.pageno with
2730 | Some (opaque, _) when validopaque opaque ->
2731 let a = now () in
2732 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2733 let b = now () in
2734 let d = b-.a in
2735 vlog "draw %d %f sec" l.pageno d;
2737 | _ ->
2738 drawplaceholder l;
2739 end;
2742 let scrollph y =
2743 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2744 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2745 let sh = float conf.winh /. sh in
2746 let sh = max sh (float conf.scrollh) in
2748 let percent =
2749 if state.y = state.maxy
2750 then 1.0
2751 else float y /. float maxy
2753 let position = (float conf.winh -. sh) *. percent in
2755 let position =
2756 if position +. sh > float conf.winh
2757 then float conf.winh -. sh
2758 else position
2760 position, sh;
2763 let scrollindicator () =
2764 GlDraw.color (0.64 , 0.64, 0.64);
2765 GlDraw.rect
2766 (float (conf.winw - state.scrollw), 0.)
2767 (float conf.winw, float conf.winh)
2769 GlDraw.color (0.0, 0.0, 0.0);
2771 let position, sh = scrollph state.y in
2772 GlDraw.rect
2773 (float (conf.winw - state.scrollw), position)
2774 (float conf.winw, position +. sh)
2778 let showsel margin =
2779 match state.mstate with
2780 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2783 | Msel ((x0, y0), (x1, y1)) ->
2784 let rec loop = function
2785 | l :: ls ->
2786 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2787 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2788 then
2789 match getopaque l.pageno with
2790 | Some (opaque, _) when validopaque opaque ->
2791 let oy = -l.pagey + l.pagedispy in
2792 seltext opaque
2793 (x0 - margin - state.x, y0,
2794 x1 - margin - state.x, y1) oy;
2796 | _ -> ()
2797 else loop ls
2798 | [] -> ()
2800 loop state.layout
2803 let showrects () =
2804 let panx = float state.x in
2805 Gl.enable `blend;
2806 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2807 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2808 List.iter
2809 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2810 List.iter (fun l ->
2811 if l.pageno = pageno
2812 then (
2813 let d = float (l.pagedispy - l.pagey) in
2814 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2815 GlDraw.begins `quads;
2817 GlDraw.vertex2 (x0+.panx, y0+.d);
2818 GlDraw.vertex2 (x1+.panx, y1+.d);
2819 GlDraw.vertex2 (x2+.panx, y2+.d);
2820 GlDraw.vertex2 (x3+.panx, y3+.d);
2822 GlDraw.ends ();
2824 ) state.layout
2825 ) state.rects
2827 Gl.disable `blend;
2830 let showstrings trusted active first pan strings =
2831 Gl.enable `blend;
2832 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2833 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2834 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2835 GlDraw.color (1., 1., 1.);
2836 Gl.enable `texture_2d;
2838 let wx = measurestr 15 "w" in
2839 let tabx = 30.0*.wx +. float (pan*15) in
2840 let rec loop row =
2841 if row = Array.length strings || (row - first) * 16 > conf.winh
2842 then ()
2843 else (
2844 let (s, level, _) = strings.(row) in
2845 let y = (row - first) * 16 in
2846 let x = 5 + 15*(max 0 (level+pan)) in
2847 if row = active
2848 then (
2849 Gl.disable `texture_2d;
2850 GlDraw.polygon_mode `both `line;
2851 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2852 GlDraw.rect (1., float (y + 1))
2853 (float (conf.winw - 1), float (y + 18));
2854 GlDraw.polygon_mode `both `fill;
2855 GlDraw.color (1., 1., 1.);
2856 Gl.enable `texture_2d;
2859 let drawtabularstring x s =
2860 let _ =
2861 if trusted
2862 then
2863 let tabpos = try String.index s '\t' with Not_found -> -1 in
2864 if tabpos > 0
2865 then
2866 let len = String.length s - tabpos - 1 in
2867 let s1 = String.sub s 0 tabpos
2868 and s2 = String.sub s (tabpos + 1) len in
2869 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2870 let x = truncate (max xx tabx) in
2871 drawstring1 15 x (y + 16) s2
2872 else
2873 drawstring1 15 x (y + 16) s
2874 else
2875 drawstring1 15 x (y + 16) s
2879 drawtabularstring (x + pan*15) s;
2880 loop (row+1)
2883 loop first;
2884 Gl.disable `blend;
2885 Gl.disable `texture_2d;
2888 let showoutline (_, active, first, outlines, _, pan, _) =
2889 showstrings false active first pan outlines;
2892 let showitems (active, first, items, _, pan, _) =
2893 showstrings true active first pan items;
2896 let display () =
2897 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2898 GlDraw.viewport margin 0 state.w conf.winh;
2899 pagematrix ();
2900 GlClear.color (scalecolor2 conf.bgcolor);
2901 GlClear.clear [`color];
2902 if conf.zoom > 1.0
2903 then (
2904 Gl.enable `scissor_test;
2905 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2907 List.iter drawpage state.layout;
2908 if conf.zoom > 1.0
2909 then
2910 Gl.disable `scissor_test
2912 if state.x != 0
2913 then (
2914 let x = -.float state.x in
2915 GlMat.translate ~x ();
2917 showrects ();
2918 showsel margin;
2919 GlDraw.viewport 0 0 conf.winw conf.winh;
2920 winmatrix ();
2921 scrollindicator ();
2922 begin match state.mode with
2923 | Items items -> showitems items
2924 | Outline outline -> showoutline outline
2925 | _ -> ()
2926 end;
2927 enttext ();
2928 Glut.swapBuffers ();
2931 let getunder x y =
2932 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2933 let x = x - margin - state.x in
2934 let rec f = function
2935 | l :: rest ->
2936 begin match getopaque l.pageno with
2937 | Some (opaque, _) when validopaque opaque ->
2938 let y = y - l.pagedispy in
2939 if y > 0
2940 then
2941 let y = l.pagey + y in
2942 let x = x - l.pagex in
2943 match whatsunder opaque x y with
2944 | Unone -> f rest
2945 | under -> under
2946 else
2947 f rest
2948 | _ ->
2949 f rest
2951 | [] -> Unone
2953 f state.layout
2956 let viewmouse button bstate x y =
2957 match button with
2958 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2959 if Glut.getModifiers () land Glut.active_ctrl != 0
2960 then (
2961 match state.mstate with
2962 | Mzoom (oldn, i) ->
2963 if oldn = n
2964 then (
2965 if i = 2
2966 then
2967 let incr =
2968 match n with
2969 | 4 ->
2970 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2971 | _ ->
2972 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2974 let zoom = conf.zoom +. incr in
2975 setzoom zoom;
2976 state.mstate <- Mzoom (n, 0);
2977 else
2978 state.mstate <- Mzoom (n, i+1);
2980 else state.mstate <- Mzoom (n, 0)
2982 | _ -> state.mstate <- Mzoom (n, 0)
2984 else (
2985 match state.autoscroll with
2986 | Some step -> setautoscrollspeed step (n=4)
2987 | None ->
2988 let incr =
2989 if n = 3
2990 then -conf.scrollstep
2991 else conf.scrollstep
2993 let incr = incr * 2 in
2994 let y = clamp incr in
2995 gotoy_and_clear_text y
2998 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2999 if bstate = Glut.DOWN
3000 then (
3001 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3002 state.mstate <- Mpan (x, y)
3004 else
3005 state.mstate <- Mnone
3007 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3008 if bstate = Glut.DOWN
3009 then
3010 let position, sh = scrollph state.y in
3011 if y > truncate position && y < truncate (position +. sh)
3012 then
3013 state.mstate <- Mscroll
3014 else
3015 let percent = float y /. float conf.winh in
3016 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3017 gotoy desty;
3018 state.mstate <- Mscroll
3019 else
3020 state.mstate <- Mnone
3022 | Glut.LEFT_BUTTON ->
3023 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3024 begin match dest with
3025 | Ulinkgoto (pageno, top) ->
3026 if pageno >= 0
3027 then (
3028 addnav ();
3029 gotopage1 pageno top;
3032 | Ulinkuri s ->
3033 print_endline s
3035 | Unone when bstate = Glut.DOWN ->
3036 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3037 state.mstate <- Mpan (x, y);
3039 | Unone | Utext _ ->
3040 if bstate = Glut.DOWN
3041 then (
3042 if conf.angle mod 360 = 0
3043 then (
3044 state.mstate <- Msel ((x, y), (x, y));
3045 Glut.postRedisplay ()
3048 else (
3049 match state.mstate with
3050 | Mnone -> ()
3052 | Mzoom _ | Mscroll ->
3053 state.mstate <- Mnone
3055 | Mpan _ ->
3056 Glut.setCursor Glut.CURSOR_INHERIT;
3057 state.mstate <- Mnone
3059 | Msel ((_, y0), (_, y1)) ->
3060 let f l =
3061 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3062 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3063 then
3064 match getopaque l.pageno with
3065 | Some (opaque, _) when validopaque opaque ->
3066 copysel opaque
3067 | _ -> ()
3069 List.iter f state.layout;
3070 copysel ""; (* ugly *)
3071 Glut.setCursor Glut.CURSOR_INHERIT;
3072 state.mstate <- Mnone;
3076 | _ -> ()
3079 let birdseyemouse button bstate x y
3080 (conf, leftx, _, hooverpageno, anchor) =
3081 match button with
3082 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3083 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3084 let rec loop = function
3085 | [] -> ()
3086 | l :: rest ->
3087 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3088 && x > margin && x < margin + l.pagew
3089 then (
3090 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3092 else loop rest
3094 loop state.layout
3095 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3096 | _ -> ()
3099 let mouse bstate button x y =
3100 match state.mode with
3101 | View -> viewmouse button bstate x y
3102 | Birdseye beye -> birdseyemouse button bstate x y beye
3103 | Textentry _ | Outline _ | Items _ -> ()
3106 let mouse ~button ~state ~x ~y = mouse state button x y;;
3108 let motion ~x ~y =
3109 match state.mode with
3110 | Outline _ -> ()
3111 | _ ->
3112 match state.mstate with
3113 | Mzoom _ | Mnone -> ()
3115 | Mpan (x0, y0) ->
3116 let dx = x - x0
3117 and dy = y0 - y in
3118 state.mstate <- Mpan (x, y);
3119 if conf.zoom > 1.0 then state.x <- state.x + dx;
3120 let y = clamp dy in
3121 gotoy_and_clear_text y
3123 | Msel (a, _) ->
3124 state.mstate <- Msel (a, (x, y));
3125 Glut.postRedisplay ()
3127 | Mscroll ->
3128 let y = min conf.winh (max 0 y) in
3129 let percent = float y /. float conf.winh in
3130 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3131 gotoy_and_clear_text y
3134 let pmotion ~x ~y =
3135 match state.mode with
3136 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3137 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3138 let rec loop = function
3139 | [] ->
3140 if hooverpageno != -1
3141 then (
3142 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3143 Glut.postRedisplay ();
3145 | l :: rest ->
3146 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3147 && x > margin && x < margin + l.pagew
3148 then (
3149 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3150 Glut.postRedisplay ();
3152 else loop rest
3154 loop state.layout
3156 | Outline _ | Items _ | Textentry _ -> ()
3157 | View ->
3158 match state.mstate with
3159 | Mnone ->
3160 begin match getunder x y with
3161 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3162 | Ulinkuri uri ->
3163 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3164 Glut.setCursor Glut.CURSOR_INFO
3165 | Ulinkgoto (page, _) ->
3166 if conf.underinfo
3167 then showtext 'p' ("age: " ^ string_of_int page);
3168 Glut.setCursor Glut.CURSOR_INFO
3169 | Utext s ->
3170 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3171 Glut.setCursor Glut.CURSOR_TEXT
3174 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3178 module State =
3179 struct
3180 open Parser
3182 let fontpath = ref "";;
3184 let home =
3186 match Sys.os_type with
3187 | "Win32" -> Sys.getenv "HOMEPATH"
3188 | _ -> Sys.getenv "HOME"
3189 with exn ->
3190 prerr_endline
3191 ("Can not determine home directory location: " ^
3192 Printexc.to_string exn);
3196 let config_of c attrs =
3197 let apply c k v =
3199 match k with
3200 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3201 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3202 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3203 | "preload" -> { c with preload = bool_of_string v }
3204 | "page-bias" -> { c with pagebias = int_of_string v }
3205 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3206 | "auto-scroll-step" ->
3207 { c with autoscrollstep = max 0 (int_of_string v) }
3208 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3209 | "crop-hack" -> { c with crophack = bool_of_string v }
3210 | "throttle" -> { c with showall = bool_of_string v }
3211 | "highlight-links" -> { c with hlinks = bool_of_string v }
3212 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3213 | "vertical-margin" ->
3214 { c with interpagespace = max 0 (int_of_string v) }
3215 | "zoom" ->
3216 let zoom = float_of_string v /. 100. in
3217 let zoom = max 0.01 zoom in
3218 { c with zoom = zoom }
3219 | "presentation" -> { c with presentation = bool_of_string v }
3220 | "rotation-angle" -> { c with angle = int_of_string v }
3221 | "width" -> { c with winw = max 20 (int_of_string v) }
3222 | "height" -> { c with winh = max 20 (int_of_string v) }
3223 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3224 | "proportional-display" -> { c with proportional = bool_of_string v }
3225 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3226 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3227 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3228 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3229 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3230 | "persistent-location" -> { c with jumpback = bool_of_string v }
3231 | "background-color" -> { c with bgcolor = color_of_string v }
3232 | "scrollbar-in-presentation" ->
3233 { c with scrollbarinpm = bool_of_string v }
3234 | _ -> c
3235 with exn ->
3236 prerr_endline ("Error processing attribute (`" ^
3237 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3240 let rec fold c = function
3241 | [] -> c
3242 | (k, v) :: rest ->
3243 let c = apply c k v in
3244 fold c rest
3246 fold c attrs;
3249 let fromstring f pos n v d =
3250 try f v
3251 with exn ->
3252 dolog "Error processing attribute (%S=%S) at %d\n%s"
3253 n v pos (Printexc.to_string exn)
3258 let bookmark_of attrs =
3259 let rec fold title page rely = function
3260 | ("title", v) :: rest -> fold v page rely rest
3261 | ("page", v) :: rest -> fold title v rely rest
3262 | ("rely", v) :: rest -> fold title page v rest
3263 | _ :: rest -> fold title page rely rest
3264 | [] -> title, page, rely
3266 fold "invalid" "0" "0" attrs
3269 let doc_of attrs =
3270 let rec fold path page rely pan = function
3271 | ("path", v) :: rest -> fold v page rely pan rest
3272 | ("page", v) :: rest -> fold path v rely pan rest
3273 | ("rely", v) :: rest -> fold path page v pan rest
3274 | ("pan", v) :: rest -> fold path page rely v rest
3275 | _ :: rest -> fold path page rely pan rest
3276 | [] -> path, page, rely, pan
3278 fold "" "0" "0" "0" attrs
3281 let setconf dst src =
3282 dst.scrollbw <- src.scrollbw;
3283 dst.scrollh <- src.scrollh;
3284 dst.icase <- src.icase;
3285 dst.preload <- src.preload;
3286 dst.pagebias <- src.pagebias;
3287 dst.verbose <- src.verbose;
3288 dst.scrollstep <- src.scrollstep;
3289 dst.maxhfit <- src.maxhfit;
3290 dst.crophack <- src.crophack;
3291 dst.autoscrollstep <- src.autoscrollstep;
3292 dst.showall <- src.showall;
3293 dst.hlinks <- src.hlinks;
3294 dst.underinfo <- src.underinfo;
3295 dst.interpagespace <- src.interpagespace;
3296 dst.zoom <- src.zoom;
3297 dst.presentation <- src.presentation;
3298 dst.angle <- src.angle;
3299 dst.winw <- src.winw;
3300 dst.winh <- src.winh;
3301 dst.savebmarks <- src.savebmarks;
3302 dst.memlimit <- src.memlimit;
3303 dst.proportional <- src.proportional;
3304 dst.texcount <- src.texcount;
3305 dst.sliceheight <- src.sliceheight;
3306 dst.blockwidth <- src.blockwidth;
3307 dst.thumbw <- src.thumbw;
3308 dst.jumpback <- src.jumpback;
3309 dst.bgcolor <- src.bgcolor;
3310 dst.scrollbarinpm <- src.scrollbarinpm;
3313 let unent s =
3314 let l = String.length s in
3315 let b = Buffer.create l in
3316 unent b s 0 l;
3317 Buffer.contents b;
3320 let get s =
3321 let h = Hashtbl.create 10 in
3322 let dc = { defconf with angle = defconf.angle } in
3323 let rec toplevel v t spos _ =
3324 match t with
3325 | Vdata | Vcdata | Vend -> v
3326 | Vopen ("llppconfig", _, closed) ->
3327 if closed
3328 then v
3329 else { v with f = llppconfig }
3330 | Vopen _ ->
3331 error "unexpected subelement at top level" s spos
3332 | Vclose _ -> error "unexpected close at top level" s spos
3334 and llppconfig v t spos _ =
3335 match t with
3336 | Vdata | Vcdata -> v
3337 | Vend -> error "unexpected end of input in llppconfig" s spos
3338 | Vopen ("defaults", attrs, closed) ->
3339 let c = config_of dc attrs in
3340 setconf dc c;
3341 if closed
3342 then v
3343 else { v with f = skip "defaults" (fun () -> v) }
3345 | Vopen ("ui-font", _, closed) ->
3346 if closed
3347 then v
3348 else { v with f = uifont (Buffer.create 10) }
3350 | Vopen ("doc", attrs, closed) ->
3351 let pathent, spage, srely, span = doc_of attrs in
3352 let path = unent pathent
3353 and pageno = fromstring int_of_string spos "page" spage 0
3354 and rely = fromstring float_of_string spos "rely" srely 0.0
3355 and pan = fromstring int_of_string spos "pan" span 0 in
3356 let c = config_of dc attrs in
3357 let anchor = (pageno, rely) in
3358 if closed
3359 then (Hashtbl.add h path (c, [], pan, anchor); v)
3360 else { v with f = doc path pan anchor c [] }
3362 | Vopen _ ->
3363 error "unexpected subelement in llppconfig" s spos
3365 | Vclose "llppconfig" -> { v with f = toplevel }
3366 | Vclose _ -> error "unexpected close in llppconfig" s spos
3368 and uifont b v t spos epos =
3369 match t with
3370 | Vdata | Vcdata ->
3371 Buffer.add_substring b s spos (epos - spos);
3373 | Vopen (_, _, _) ->
3374 error "unexpected subelement in ui-font" s spos
3375 | Vclose "ui-font" ->
3376 if String.length !fontpath = 0
3377 then fontpath := Buffer.contents b;
3378 { v with f = llppconfig }
3379 | Vclose _ -> error "unexpected close in ui-font" s spos
3380 | Vend -> error "unexpected end of input in ui-font" 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 defconfpath =
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 confpath = ref defconfpath;;
3472 let load1 f =
3473 if Sys.file_exists !confpath
3474 then
3475 match
3476 (try Some (open_in_bin !confpath)
3477 with exn ->
3478 prerr_endline
3479 ("Error opening configuation file `" ^ !confpath ^ "': " ^
3480 Printexc.to_string exn);
3481 None
3483 with
3484 | Some ic ->
3485 begin try
3486 f (do_load get ic)
3487 with exn ->
3488 prerr_endline
3489 ("Error loading configuation from `" ^ !confpath ^ "': " ^
3490 Printexc.to_string exn);
3491 end;
3492 close_in ic;
3494 | None -> ()
3495 else
3496 f (Hashtbl.create 0, defconf)
3499 let load () =
3500 let f (h, dc) =
3501 let pc, pb, px, pa =
3503 Hashtbl.find h (Filename.basename state.path)
3504 with Not_found -> dc, [], 0, (0, 0.0)
3506 setconf defconf dc;
3507 setconf conf pc;
3508 state.bookmarks <- pb;
3509 state.x <- px;
3510 state.scrollw <- conf.scrollbw;
3511 if conf.jumpback
3512 then state.anchor <- pa;
3513 cbput state.hists.nav pa;
3515 load1 f
3518 let add_attrs bb always dc c =
3519 let ob s a b =
3520 if always || a != b
3521 then Printf.bprintf bb "\n %s='%b'" s a
3522 and oi s a b =
3523 if always || a != b
3524 then Printf.bprintf bb "\n %s='%d'" s a
3525 and oz s a b =
3526 if always || a <> b
3527 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
3528 and oc s a b =
3529 if always || a <> b
3530 then
3531 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3533 let w, h =
3534 if always
3535 then dc.winw, dc.winh
3536 else
3537 match state.fullscreen with
3538 | Some wh -> wh
3539 | None -> c.winw, c.winh
3541 let zoom, presentation, interpagespace, showall=
3542 if always
3543 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3544 else
3545 match state.mode with
3546 | Birdseye (bc, _, _, _, _) ->
3547 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3548 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3550 oi "width" w dc.winw;
3551 oi "height" h dc.winh;
3552 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3553 oi "scroll-handle-height" c.scrollh dc.scrollh;
3554 ob "case-insensitive-search" c.icase dc.icase;
3555 ob "preload" c.preload dc.preload;
3556 oi "page-bias" c.pagebias dc.pagebias;
3557 oi "scroll-step" c.scrollstep dc.scrollstep;
3558 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3559 ob "max-height-fit" c.maxhfit dc.maxhfit;
3560 ob "crop-hack" c.crophack dc.crophack;
3561 ob "throttle" showall dc.showall;
3562 ob "highlight-links" c.hlinks dc.hlinks;
3563 ob "under-cursor-info" c.underinfo dc.underinfo;
3564 oi "vertical-margin" interpagespace dc.interpagespace;
3565 oz "zoom" zoom dc.zoom;
3566 ob "presentation" presentation dc.presentation;
3567 oi "rotation-angle" c.angle dc.angle;
3568 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3569 ob "proportional-display" c.proportional dc.proportional;
3570 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3571 oi "tex-count" c.texcount dc.texcount;
3572 oi "slice-height" c.sliceheight dc.sliceheight;
3573 oi "block-width" c.blockwidth dc.blockwidth;
3574 oi "thumbnail-width" c.thumbw dc.thumbw;
3575 ob "persistent-location" c.jumpback dc.jumpback;
3576 oc "background-color" c.bgcolor dc.bgcolor;
3577 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3580 let save () =
3581 let bb = Buffer.create 32768 in
3582 let f (h, dc) =
3583 let dc = if conf.bedefault then conf else dc in
3584 Buffer.add_string bb "<llppconfig>\n";
3586 if String.length !fontpath > 0
3587 then Printf.bprintf bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3589 Buffer.add_string bb "<defaults ";
3590 add_attrs bb true dc dc;
3591 Buffer.add_string bb "/>\n";
3593 let adddoc path pan anchor c bookmarks =
3594 if bookmarks == [] && c = dc && anchor = emptyanchor
3595 then ()
3596 else (
3597 Printf.bprintf bb "<doc path='%s'"
3598 (enent path 0 (String.length path));
3600 if anchor <> emptyanchor
3601 then (
3602 let n, y = anchor in
3603 Printf.bprintf bb " page='%d'" n;
3604 if y > 1e-6
3605 then
3606 Printf.bprintf bb " rely='%f'" y
3610 if pan != 0
3611 then Printf.bprintf bb " pan='%d'" pan;
3613 add_attrs bb false dc c;
3615 begin match bookmarks with
3616 | [] -> Buffer.add_string bb "/>\n"
3617 | _ ->
3618 Buffer.add_string bb ">\n<bookmarks>\n";
3619 List.iter (fun (title, _level, (page, rely)) ->
3620 Printf.bprintf bb
3621 "<item title='%s' page='%d'"
3622 (enent title 0 (String.length title))
3623 page
3625 if rely > 1e-6
3626 then
3627 Printf.bprintf bb " rely='%f'" rely
3629 Buffer.add_string bb "/>\n";
3630 ) bookmarks;
3631 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3632 end;
3636 let pan =
3637 match state.mode with
3638 | Birdseye (_, pan, _, _, _) -> pan
3639 | _ -> state.x
3641 let basename = Filename.basename state.path in
3642 adddoc basename pan (getanchor ())
3643 { conf with
3644 autoscrollstep =
3645 match state.autoscroll with
3646 | Some step -> step
3647 | None -> conf.autoscrollstep }
3648 (if conf.savebmarks then state.bookmarks else []);
3650 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3651 if basename <> path
3652 then adddoc path x y c bookmarks
3653 ) h;
3654 Buffer.add_string bb "</llppconfig>";
3656 load1 f;
3657 if Buffer.length bb > 0
3658 then
3660 let tmp = !confpath ^ ".tmp" in
3661 let oc = open_out_bin tmp in
3662 Buffer.output_buffer oc bb;
3663 close_out oc;
3664 Sys.rename tmp !confpath;
3665 with exn ->
3666 prerr_endline
3667 ("error while saving configuration: " ^ Printexc.to_string exn)
3669 end;;
3671 let () =
3672 Arg.parse
3673 (Arg.align
3674 [("-p", Arg.String (fun s -> state.password <- s) ,
3675 "<password> Set password");
3677 ("-f", Arg.String (fun s -> State.fontpath := s),
3678 "<path> Set path to the user interface font");
3680 ("-c", Arg.String (fun s -> State.confpath := s),
3681 "<path> Set path to the configuration file");
3683 ("-v", Arg.Unit (fun () ->
3684 Printf.printf
3685 "%s\nconfiguration path: %s\n"
3686 Help.version
3687 State.defconfpath
3689 exit 0), " Print version and exit");
3692 (fun s -> state.path <- s)
3693 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3695 if String.length state.path = 0
3696 then (prerr_endline "file name missing"; exit 1);
3698 State.load ();
3700 let _ = Glut.init Sys.argv in
3701 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3702 let () = Glut.initWindowSize conf.winw conf.winh in
3703 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3705 let csock, ssock =
3706 if Sys.os_type = "Unix"
3707 then
3708 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3709 else
3710 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3711 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3712 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3713 Unix.bind sock addr;
3714 Unix.listen sock 1;
3715 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3716 Unix.connect csock addr;
3717 let ssock, _ = Unix.accept sock in
3718 Unix.close sock;
3719 let opts sock =
3720 Unix.setsockopt sock Unix.TCP_NODELAY true;
3721 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3723 opts ssock;
3724 opts csock;
3725 ssock, csock
3728 let () = Glut.displayFunc display in
3729 let () = Glut.reshapeFunc reshape in
3730 let () = Glut.keyboardFunc keyboard in
3731 let () = Glut.specialFunc special in
3732 let () = Glut.idleFunc (Some idle) in
3733 let () = Glut.mouseFunc mouse in
3734 let () = Glut.motionFunc motion in
3735 let () = Glut.passiveMotionFunc pmotion in
3737 init ssock (
3738 conf.angle, conf.proportional, conf.texcount,
3739 conf.sliceheight, conf.blockwidth, !State.fontpath
3741 state.csock <- csock;
3742 state.ssock <- ssock;
3743 state.text <- "Opening " ^ state.path;
3744 writeopen state.path state.password;
3746 while true do
3748 Glut.mainLoop ();
3749 with
3750 | Glut.BadEnum "key in special_of_int" ->
3751 showtext '!' " LablGlut bug: special key not recognized";
3753 | Quit ->
3754 if Sys.os_type <> "Unix"
3755 then Unix.shutdown ssock Unix.SHUTDOWN_ALL;
3756 State.save ();
3757 exit 0
3758 done;