Avoid doing extra work
[llpp.git] / main.ml
blob155816e3e3d9cc101b2172a672b54df3432a901b
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 let cbpeek b =
120 let rc = b.wc - b.len in
121 let rc = if rc < 0 then cbcap b + rc else rc in
122 b.store.(rc);
125 let cbdecr b = b.len <- b.len - 1;;
127 type layout =
128 { pageno : int
129 ; pagedimno : int
130 ; pagew : int
131 ; pageh : int
132 ; pagedispy : int
133 ; pagey : int
134 ; pagevh : int
135 ; pagex : int
139 type conf =
140 { mutable scrollbw : int
141 ; mutable scrollh : int
142 ; mutable icase : bool
143 ; mutable preload : bool
144 ; mutable pagebias : int
145 ; mutable verbose : bool
146 ; mutable scrollstep : int
147 ; mutable maxhfit : bool
148 ; mutable crophack : bool
149 ; mutable autoscrollstep : int
150 ; mutable showall : bool
151 ; mutable hlinks : bool
152 ; mutable underinfo : bool
153 ; mutable interpagespace : interpagespace
154 ; mutable zoom : float
155 ; mutable presentation : bool
156 ; mutable angle : angle
157 ; mutable winw : int
158 ; mutable winh : int
159 ; mutable savebmarks : bool
160 ; mutable proportional : proportional
161 ; mutable memlimit : int
162 ; mutable texcount : texcount
163 ; mutable sliceheight : sliceheight
164 ; mutable blockwidth : blockwidth
165 ; mutable thumbw : width
166 ; mutable jumpback : bool
167 ; mutable bgcolor : float * float * float
168 ; mutable bedefault : bool
169 ; mutable scrollbarinpm : bool
173 type anchor = pageno * top;;
175 type outline = string * int * anchor
176 and outlines =
177 | Oarray of outline array
178 | Olist of outline list
179 | Onarrow of string * outline array * outline array
182 type rect = float * float * float * float * float * float * float * float;;
184 type pagemapkey = pageno * width * angle * proportional * gen;;
186 let emptyanchor = (0, 0.0);;
188 type mode =
189 | Birdseye of (conf * leftx * pageno * pageno * anchor)
190 | Outline of (bool * int * int * outline array * string * int * mode)
191 | Items of (int * int * item array * string * int * mode)
192 | Textentry of (textentry * onleave)
193 | View
194 and onleave = leavetextentrystatus -> unit
195 and leavetextentrystatus = | Cancel | Confirm
196 and item = string * int * action
197 and action =
198 | Noaction
199 | Action of (int -> int -> string -> int -> mode)
202 let isbirdseye = function Birdseye _ -> true | _ -> false;;
203 let istextentry = function Textentry _ -> true | _ -> false;;
205 type state =
206 { mutable csock : Unix.file_descr
207 ; mutable ssock : Unix.file_descr
208 ; mutable w : int
209 ; mutable x : int
210 ; mutable y : int
211 ; mutable scrollw : int
212 ; mutable anchor : anchor
213 ; mutable maxy : int
214 ; mutable layout : layout list
215 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
216 ; mutable pdims : (pageno * width * height * leftx) list
217 ; mutable pagecount : int
218 ; pagecache : string circbuf
219 ; mutable rendering : bool
220 ; mutable mstate : mstate
221 ; mutable searchpattern : string
222 ; mutable rects : (pageno * recttype * rect) list
223 ; mutable rects1 : (pageno * recttype * rect) list
224 ; mutable text : string
225 ; mutable fullscreen : (width * height) option
226 ; mutable mode : mode
227 ; mutable outlines : outlines
228 ; mutable bookmarks : outline list
229 ; mutable path : string
230 ; mutable password : string
231 ; mutable invalidated : int
232 ; mutable colorscale : float
233 ; mutable memused : int
234 ; mutable gen : gen
235 ; mutable throttle : layout list option
236 ; mutable autoscroll :int option
237 ; mutable help : item array
238 ; mutable docinfo : (int * string) list
239 ; mutable deadline : float
240 ; hists : hists
242 and hists =
243 { pat : string circbuf
244 ; pag : string circbuf
245 ; nav : anchor circbuf
249 let defconf =
250 { scrollbw = 7
251 ; scrollh = 12
252 ; icase = true
253 ; preload = true
254 ; pagebias = 0
255 ; verbose = false
256 ; scrollstep = 24
257 ; maxhfit = true
258 ; crophack = false
259 ; autoscrollstep = 2
260 ; showall = false
261 ; hlinks = false
262 ; underinfo = false
263 ; interpagespace = 2
264 ; zoom = 1.0
265 ; presentation = false
266 ; angle = 0
267 ; winw = 900
268 ; winh = 900
269 ; savebmarks = true
270 ; proportional = true
271 ; memlimit = 32*1024*1024
272 ; texcount = 256
273 ; sliceheight = 24
274 ; blockwidth = 2048
275 ; thumbw = 76
276 ; jumpback = false
277 ; bgcolor = (0.5, 0.5, 0.5)
278 ; bedefault = false
279 ; scrollbarinpm = true
283 let conf = { defconf with angle = defconf.angle };;
285 let makehelp () =
286 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
287 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
290 let state =
291 { csock = Unix.stdin
292 ; ssock = Unix.stdin
293 ; x = 0
294 ; y = 0
295 ; w = 0
296 ; scrollw = 0
297 ; anchor = emptyanchor
298 ; layout = []
299 ; maxy = max_int
300 ; pagemap = Hashtbl.create 10
301 ; pagecache = cbnew 100 ""
302 ; pdims = []
303 ; pagecount = 0
304 ; rendering = false
305 ; mstate = Mnone
306 ; rects = []
307 ; rects1 = []
308 ; text = ""
309 ; mode = View
310 ; fullscreen = None
311 ; searchpattern = ""
312 ; outlines = Olist []
313 ; bookmarks = []
314 ; path = ""
315 ; password = ""
316 ; invalidated = 0
317 ; hists =
318 { nav = cbnew 100 (0, 0.0)
319 ; pat = cbnew 20 ""
320 ; pag = cbnew 10 ""
322 ; colorscale = 1.0
323 ; memused = 0
324 ; gen = 0
325 ; throttle = None
326 ; autoscroll = None
327 ; help = makehelp ()
328 ; docinfo = []
329 ; deadline = nan
333 let vlog fmt =
334 if conf.verbose
335 then
336 Printf.kprintf prerr_endline fmt
337 else
338 Printf.kprintf ignore fmt
341 let writecmd fd s =
342 let len = String.length s in
343 let n = 4 + len in
344 let b = Buffer.create n in
345 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
346 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
347 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
348 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
349 Buffer.add_string b s;
350 let s' = Buffer.contents b in
351 let n' = Unix.write fd s' 0 n in
352 if n' != n then failwith "write failed";
355 let readcmd fd =
356 let s = "xxxx" in
357 let n = Unix.read fd s 0 4 in
358 if n != 4 then failwith "incomplete read(len)";
359 let len = 0
360 lor (Char.code s.[0] lsl 24)
361 lor (Char.code s.[1] lsl 16)
362 lor (Char.code s.[2] lsl 8)
363 lor (Char.code s.[3] lsl 0)
365 let s = String.create len in
366 let n = Unix.read fd s 0 len in
367 if n != len then failwith "incomplete read(data)";
371 let makecmd s l =
372 let b = Buffer.create 10 in
373 Buffer.add_string b s;
374 let rec combine = function
375 | [] -> b
376 | x :: xs ->
377 Buffer.add_char b ' ';
378 let s =
379 match x with
380 | `b b -> if b then "1" else "0"
381 | `s s -> s
382 | `i i -> string_of_int i
383 | `f f -> string_of_float f
384 | `I f -> string_of_int (truncate f)
386 Buffer.add_string b s;
387 combine xs;
389 combine l;
392 let wcmd s l =
393 let cmd = Buffer.contents (makecmd s l) in
394 writecmd state.csock cmd;
397 let calcips h =
398 if conf.presentation
399 then
400 let d = conf.winh - h in
401 max 0 ((d + 1) / 2)
402 else
403 conf.interpagespace
406 let calcheight () =
407 let rec f pn ph pi fh l =
408 match l with
409 | (n, _, h, _) :: rest ->
410 let ips = calcips h in
411 let fh =
412 if conf.presentation
413 then fh+ips
414 else (
415 if isbirdseye state.mode && pn = 0
416 then fh + ips
417 else fh
420 let fh = fh + ((n - pn) * (ph + pi)) in
421 f n h ips fh rest;
423 | [] ->
424 let inc =
425 if conf.presentation || (isbirdseye state.mode && pn = 0)
426 then 0
427 else -pi
429 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
430 max 0 fh
432 let fh = f 0 0 0 0 state.pdims in
436 let getpageyh pageno =
437 let rec f pn ph pi y l =
438 match l with
439 | (n, _, h, _) :: rest ->
440 let ips = calcips h in
441 if n >= pageno
442 then
443 let h = if n = pageno then h else ph in
444 if conf.presentation && n = pageno
445 then
446 y + (pageno - pn) * (ph + pi) + pi, h
447 else
448 y + (pageno - pn) * (ph + pi), h
449 else
450 let y = y + (if conf.presentation then pi else 0) in
451 let y = y + (n - pn) * (ph + pi) in
452 f n h ips y rest
454 | [] ->
455 y + (pageno - pn) * (ph + pi), ph
457 f 0 0 0 0 state.pdims
460 let getpagey pageno = fst (getpageyh pageno);;
462 let layout y sh =
463 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
464 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
465 match pdims with
466 | (pageno', w, h, x) :: rest when pageno' = pageno ->
467 let ips = calcips h in
468 let yinc =
469 if conf.presentation || (isbirdseye state.mode && pageno = 0)
470 then ips
471 else 0
473 (w, h, ips, x), rest, pdimno + 1, yinc
474 | _ ->
475 prev, pdims, pdimno, 0
477 let dy = dy + yinc in
478 let py = py + yinc in
479 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
480 then
481 accu
482 else
483 let vy = y + dy in
484 if py + h <= vy - yinc
485 then
486 let py = py + h + ips in
487 let dy = max 0 (py - y) in
488 f ~pageno:(pageno+1)
489 ~pdimno
490 ~prev:curr
493 ~pdims:rest
494 ~cacheleft
495 ~accu
496 else
497 let pagey = vy - py in
498 let pagevh = h - pagey in
499 let pagevh = min (sh - dy) pagevh in
500 let off = if yinc > 0 then py - vy else 0 in
501 let py = py + h + ips in
502 let e =
503 { pageno = pageno
504 ; pagedimno = pdimno
505 ; pagew = w
506 ; pageh = h
507 ; pagedispy = dy + off
508 ; pagey = pagey + off
509 ; pagevh = pagevh - off
510 ; pagex = x
513 let accu = e :: accu in
514 f ~pageno:(pageno+1)
515 ~pdimno
516 ~prev:curr
518 ~dy:(dy+pagevh+ips)
519 ~pdims:rest
520 ~cacheleft:(cacheleft-1)
521 ~accu
523 if state.invalidated = 0
524 then (
525 let accu =
527 ~pageno:0
528 ~pdimno:~-1
529 ~prev:(0,0,0,0)
530 ~py:0
531 ~dy:0
532 ~pdims:state.pdims
533 ~cacheleft:(cbcap state.pagecache)
534 ~accu:[]
536 List.rev accu
538 else
542 let clamp incr =
543 let y = state.y + incr in
544 let y = max 0 y in
545 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
549 let getopaque pageno =
550 try Some (Hashtbl.find state.pagemap
551 (pageno, state.w, conf.angle, conf.proportional, state.gen))
552 with Not_found -> None
555 let cache pageno opaque =
556 Hashtbl.replace state.pagemap
557 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
560 let validopaque opaque = String.length opaque > 0;;
562 let render l =
563 match getopaque l.pageno with
564 | None when not state.rendering ->
565 state.rendering <- true;
566 cache l.pageno ("", -1);
567 wcmd "render" [`i (l.pageno + 1)
568 ;`i l.pagedimno
569 ;`i l.pagew
570 ;`i l.pageh];
571 | _ -> ()
574 let loadlayout layout =
575 let rec f all = function
576 | l :: ls ->
577 begin match getopaque l.pageno with
578 | None -> render l; f false ls
579 | Some (opaque, _) -> f (all && validopaque opaque) ls
581 | [] -> all
583 f (layout <> []) layout;
586 let findpageforopaque opaque =
587 Hashtbl.fold
588 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
589 state.pagemap None
592 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
594 let preload () =
595 let oktopreload =
596 if conf.preload && not state.rendering
597 then
598 let memleft = conf.memlimit - state.memused in
599 if memleft < 0
600 then
601 let opaque = cbpeek state.pagecache in
602 match findpageforopaque opaque with
603 | Some ((n, _, _, _, _), size) ->
604 memleft + size >= 0 && not (pagevisible state.layout n)
605 | None -> false
606 else true
607 else false
609 if oktopreload
610 then
611 let presentation = conf.presentation in
612 let interpagespace = conf.interpagespace in
613 let maxy = state.maxy in
614 conf.presentation <- false;
615 conf.interpagespace <- 0;
616 state.maxy <- calcheight ();
617 let y =
618 match state.layout with
619 | [] -> 0
620 | l :: _ -> getpagey l.pageno + l.pagey
622 let y = if y < conf.winh then 0 else y - conf.winh in
623 let h = state.y - y + conf.winh*3 in
624 let pages = layout y h in
625 List.iter render pages;
626 conf.presentation <- presentation;
627 conf.interpagespace <- interpagespace;
628 state.maxy <- maxy;
631 let gotoy y =
632 let y = max 0 y in
633 let y = min state.maxy y in
634 let pages = layout y conf.winh in
635 let ready = loadlayout pages in
636 if conf.showall
637 then (
638 if ready
639 then (
640 state.y <- y;
641 state.layout <- pages;
642 state.throttle <- None;
643 Glut.postRedisplay ();
645 else (
646 state.throttle <- Some pages;
649 else (
650 state.y <- y;
651 state.layout <- pages;
652 state.throttle <- None;
653 Glut.postRedisplay ();
655 begin match state.mode with
656 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
657 if not (pagevisible pages pageno)
658 then (
659 match state.layout with
660 | [] -> ()
661 | l :: _ ->
662 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
664 | _ -> ()
665 end;
666 preload ();
669 let gotoy_and_clear_text y =
670 gotoy y;
671 if not conf.verbose then state.text <- "";
674 let getanchor () =
675 match state.layout with
676 | [] -> emptyanchor
677 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
680 let getanchory (n, top) =
681 let y, h = getpageyh n in
682 y + (truncate (top *. float h));
685 let gotoanchor anchor =
686 gotoy (getanchory anchor);
689 let addnav () =
690 cbput state.hists.nav (getanchor ());
693 let getnav dir =
694 let anchor = cbgetc state.hists.nav dir in
695 getanchory anchor;
698 let gotopage n top =
699 let y, h = getpageyh n in
700 gotoy_and_clear_text (y + (truncate (top *. float h)));
703 let gotopage1 n top =
704 let y = getpagey n in
705 gotoy_and_clear_text (y + top);
708 let invalidate () =
709 state.layout <- [];
710 state.pdims <- [];
711 state.rects <- [];
712 state.rects1 <- [];
713 state.invalidated <- state.invalidated + 1;
716 let scalecolor c =
717 let c = c *. state.colorscale in
718 (c, c, c);
721 let scalecolor2 (r, g, b) =
722 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
725 let represent () =
726 state.maxy <- calcheight ();
727 match state.mode with
728 | Birdseye (_, _, pageno, _, _) ->
729 let y, h = getpageyh pageno in
730 let top = (conf.winh - h) / 2 in
731 gotoy (max 0 (y - top))
732 | _ -> gotoanchor state.anchor
735 let pagematrix () =
736 GlMat.mode `projection;
737 GlMat.load_identity ();
738 GlMat.rotate ~x:1.0 ~angle:180.0 ();
739 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
740 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
741 if state.x != 0
742 then (
743 GlMat.translate ~x:(float state.x) ();
747 let winmatrix () =
748 GlMat.mode `projection;
749 GlMat.load_identity ();
750 GlMat.rotate ~x:1.0 ~angle:180.0 ();
751 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
752 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
755 let reshape =
756 let firsttime = ref true in
757 fun ~w ~h ->
758 if state.invalidated = 0 && not !firsttime
759 then state.anchor <- getanchor ();
761 firsttime := false;
762 conf.winw <- w;
763 let w = truncate (float w *. conf.zoom) - state.scrollw in
764 let w = max w 2 in
765 state.w <- w;
766 conf.winh <- h;
767 GlMat.mode `modelview;
768 GlMat.load_identity ();
769 GlClear.color (scalecolor 1.0);
770 GlClear.clear [`color];
772 invalidate ();
773 wcmd "geometry" [`i w; `i h];
776 let drawstring size x y s =
777 Gl.enable `blend;
778 Gl.enable `texture_2d;
779 ignore (drawstr size x y s);
780 Gl.disable `blend;
781 Gl.disable `texture_2d;
784 let drawstring1 size x y s =
785 drawstr size x y s;
788 let enttext () =
789 let len = String.length state.text in
790 let drawstring s =
791 GlDraw.color (0.0, 0.0, 0.0);
792 GlDraw.rect
793 (0.0, float (conf.winh - 18))
794 (float (conf.winw - state.scrollw - 1), float conf.winh)
796 GlDraw.color (1.0, 1.0, 1.0);
797 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
799 match state.mode with
800 | Textentry ((prefix, text, _, _, _), _) ->
801 let s =
802 if len > 0
803 then
804 Printf.sprintf "%s%s_ [%s]" prefix text state.text
805 else
806 Printf.sprintf "%s%s_" prefix text
808 drawstring s
810 | _ ->
811 if len > 0 then drawstring state.text
814 let showtext c s =
815 state.text <- Printf.sprintf "%c%s" c s;
816 Glut.postRedisplay ();
819 let act cmd =
820 match cmd.[0] with
821 | 'c' ->
822 state.pdims <- [];
824 | 'D' ->
825 state.rects <- state.rects1;
826 Glut.postRedisplay ()
828 | 'C' ->
829 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
830 state.pagecount <- n;
831 state.invalidated <- state.invalidated - 1;
832 if state.invalidated = 0
833 then represent ()
835 | 't' ->
836 let s = Scanf.sscanf cmd "t %n"
837 (fun n -> String.sub cmd n (String.length cmd - n))
839 Glut.setWindowTitle s
841 | 'T' ->
842 let s = Scanf.sscanf cmd "T %n"
843 (fun n -> String.sub cmd n (String.length cmd - n))
845 if istextentry state.mode
846 then (
847 state.text <- s;
848 showtext ' ' s;
850 else (
851 state.text <- s;
852 Glut.postRedisplay ();
855 | 'V' ->
856 if conf.verbose
857 then
858 let s = Scanf.sscanf cmd "V %n"
859 (fun n -> String.sub cmd n (String.length cmd - n))
861 state.text <- s;
862 showtext ' ' s;
864 | 'F' ->
865 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
866 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
867 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
868 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
870 let y = (getpagey pageno) + truncate y0 in
871 addnav ();
872 gotoy y;
873 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
875 | 'R' ->
876 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
877 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
878 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
879 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
881 state.rects1 <-
882 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
884 | 'r' ->
885 let n, w, _h, r, l, s, p =
886 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
887 (fun n w h r l s p ->
888 (n-1, w, h, r, l != 0, s, p))
891 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
892 state.memused <- state.memused + s;
894 let layout =
895 match state.throttle with
896 | None -> state.layout
897 | Some layout -> layout
900 let rec gc () =
901 if (state.memused <= conf.memlimit) || cbempty state.pagecache
902 then ()
903 else (
904 let evictedopaque = cbpeek state.pagecache in
905 match findpageforopaque evictedopaque with
906 | None -> failwith "bug in gc"
907 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
908 if state.gen != gen || not (pagevisible layout evictedn)
909 then (
910 wcmd "free" [`s evictedopaque];
911 state.memused <- state.memused - evictedsize;
912 Hashtbl.remove state.pagemap k;
913 cbdecr state.pagecache;
914 gc ();
918 gc ();
920 cbput state.pagecache p;
921 state.rendering <- false;
923 begin match state.throttle with
924 | None ->
925 if pagevisible state.layout n
926 then gotoy state.y
927 else (
928 let allvisible = loadlayout state.layout in
929 if allvisible then preload ();
932 | Some layout ->
933 match layout with
934 | [] -> ()
935 | l :: _ ->
936 let y = getpagey l.pageno + l.pagey in
937 gotoy y
940 | 'l' ->
941 let pdim =
942 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
944 state.pdims <- pdim :: state.pdims
946 | 'o' ->
947 let (l, n, t, h, pos) =
948 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
950 let s = String.sub cmd pos (String.length cmd - pos) in
951 let outline = (s, l, (n, float t /. float h)) in
952 let outlines =
953 match state.outlines with
954 | Olist outlines -> Olist (outline :: outlines)
955 | Oarray _ -> Olist [outline]
956 | Onarrow _ -> Olist [outline]
958 state.outlines <- outlines
961 | 'i' ->
962 if String.length cmd > 1 && cmd.[1] = 'e'
963 then
964 state.docinfo <- List.rev state.docinfo
965 else
966 let s = Scanf.sscanf cmd "i %n"
967 (fun n -> String.sub cmd n (String.length cmd - n))
969 state.docinfo <- (1, s) :: state.docinfo
971 | _ ->
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday;;
977 let idle () =
978 if state.deadline == nan then state.deadline <- now ();
979 let rec loop delay =
980 let timeout =
981 if delay > 0.0
982 then max 0.0 (state.deadline -. now ())
983 else 0.0
985 let r, _, _ = Unix.select [state.csock] [] [] timeout in
986 state.deadline <- state.deadline +. delay;
987 begin match r with
988 | [] ->
989 begin match state.autoscroll with
990 | Some step when step != 0 ->
991 let y = state.y + step in
992 let y =
993 if y < 0
994 then state.maxy
995 else if y >= state.maxy then 0 else y
997 gotoy y;
998 if state.mode = View
999 then state.text <- "";
1000 | _ -> ()
1001 end;
1003 | _ ->
1004 let cmd = readcmd state.csock in
1005 act cmd;
1006 loop 0.0
1007 end;
1008 in loop 0.007
1011 let onhist cb =
1012 let rc = cb.rc in
1013 let action = function
1014 | HCprev -> cbget cb ~-1
1015 | HCnext -> cbget cb 1
1016 | HCfirst -> cbget cb ~-(cb.rc)
1017 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1018 and cancel () = cb.rc <- rc
1019 in (action, cancel)
1022 let search pattern forward =
1023 if String.length pattern > 0
1024 then
1025 let pn, py =
1026 match state.layout with
1027 | [] -> 0, 0
1028 | l :: _ ->
1029 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1031 let cmd =
1032 let b = makecmd "search"
1033 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1035 Buffer.add_char b ',';
1036 Buffer.add_string b pattern;
1037 Buffer.add_char b '\000';
1038 Buffer.contents b;
1040 writecmd state.csock cmd;
1043 let intentry text key =
1044 let c = Char.unsafe_chr key in
1045 match c with
1046 | '0' .. '9' ->
1047 let s = "x" in s.[0] <- c;
1048 let text = text ^ s in
1049 TEcont text
1051 | _ ->
1052 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1053 TEcont text
1056 let addchar s c =
1057 let b = Buffer.create (String.length s + 1) in
1058 Buffer.add_string b s;
1059 Buffer.add_char b c;
1060 Buffer.contents b;
1063 let textentry text key =
1064 let c = Char.unsafe_chr key in
1065 match c with
1066 | _ when key >= 32 && key < 127 ->
1067 let text = addchar text c in
1068 TEcont text
1070 | _ ->
1071 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1072 TEcont text
1075 let reinit angle proportional =
1076 state.anchor <- getanchor ();
1077 conf.angle <- angle;
1078 conf.proportional <- proportional;
1079 invalidate ();
1080 wcmd "reinit" [`i angle; `b proportional];
1083 let setzoom zoom =
1084 let zoom = max 0.01 zoom in
1085 if zoom <> conf.zoom
1086 then (
1087 if zoom <= 1.0
1088 then state.x <- 0;
1089 conf.zoom <- zoom;
1090 reshape conf.winw conf.winh;
1091 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1095 let enterbirdseye () =
1096 let zoom = float conf.thumbw /. float conf.winw in
1097 let birdseyepageno =
1098 let cy = conf.winh / 2 in
1099 let fold = function
1100 | [] -> 0
1101 | l :: rest ->
1102 let rec fold best = function
1103 | [] -> best.pageno
1104 | l :: rest ->
1105 let d = cy - (l.pagedispy + l.pagevh/2)
1106 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1107 if abs d < abs dbest
1108 then fold l rest
1109 else best.pageno
1110 in fold l rest
1112 fold state.layout
1114 state.mode <- Birdseye (
1115 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1117 conf.zoom <- zoom;
1118 conf.presentation <- false;
1119 conf.interpagespace <- 10;
1120 conf.hlinks <- false;
1121 state.x <- 0;
1122 state.mstate <- Mnone;
1123 conf.showall <- false;
1124 Glut.setCursor Glut.CURSOR_INHERIT;
1125 if conf.verbose
1126 then
1127 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1128 (100.0*.zoom)
1129 else
1130 state.text <- ""
1132 reshape conf.winw conf.winh;
1135 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1136 state.mode <- View;
1137 conf.zoom <- c.zoom;
1138 conf.presentation <- c.presentation;
1139 conf.interpagespace <- c.interpagespace;
1140 conf.showall <- c.showall;
1141 conf.hlinks <- c.hlinks;
1142 state.x <- leftx;
1143 if conf.verbose
1144 then
1145 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1146 (100.0*.conf.zoom)
1148 reshape conf.winw conf.winh;
1149 state.anchor <- if goback then anchor else (pageno, 0.0);
1152 let togglebirdseye () =
1153 match state.mode with
1154 | Birdseye vals -> leavebirdseye vals true
1155 | View | Outline _ -> enterbirdseye ()
1156 | _ -> ()
1159 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1160 let pageno = max 0 (pageno - 1) in
1161 let rec loop = function
1162 | [] -> gotopage1 pageno 0
1163 | l :: _ when l.pageno = pageno ->
1164 if l.pagedispy >= 0 && l.pagey = 0
1165 then Glut.postRedisplay ()
1166 else gotopage1 pageno 0
1167 | _ :: rest -> loop rest
1169 loop state.layout;
1170 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1173 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1174 let pageno = min (state.pagecount - 1) (pageno + 1) in
1175 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1176 let rec loop = function
1177 | [] ->
1178 let y, h = getpageyh pageno in
1179 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1180 gotoy (clamp dy)
1181 | l :: _ when l.pageno = pageno ->
1182 if l.pagevh != l.pageh
1183 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1184 else Glut.postRedisplay ()
1185 | _ :: rest -> loop rest
1187 loop state.layout
1190 let optentry mode _ key =
1191 let btos b = if b then "on" else "off" in
1192 let c = Char.unsafe_chr key in
1193 match c with
1194 | 's' ->
1195 let ondone s =
1196 try conf.scrollstep <- int_of_string s with exc ->
1197 state.text <- Printf.sprintf "bad integer `%s': %s"
1198 s (Printexc.to_string exc)
1200 TEswitch ("scroll step: ", "", None, intentry, ondone)
1202 | 'A' ->
1203 let ondone s =
1205 conf.autoscrollstep <- int_of_string s;
1206 if state.autoscroll <> None
1207 then state.autoscroll <- Some conf.autoscrollstep
1208 with exc ->
1209 state.text <- Printf.sprintf "bad integer `%s': %s"
1210 s (Printexc.to_string exc)
1212 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1214 | 'Z' ->
1215 let ondone s =
1217 let zoom = float (int_of_string s) /. 100.0 in
1218 setzoom zoom
1219 with exc ->
1220 state.text <- Printf.sprintf "bad integer `%s': %s"
1221 s (Printexc.to_string exc)
1223 TEswitch ("zoom: ", "", None, intentry, ondone)
1225 | 't' ->
1226 let ondone s =
1228 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1229 state.text <-
1230 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1231 begin match mode with
1232 | Birdseye beye ->
1233 leavebirdseye beye false;
1234 enterbirdseye ();
1235 | _ -> ();
1237 with exc ->
1238 state.text <- Printf.sprintf "bad integer `%s': %s"
1239 s (Printexc.to_string exc)
1241 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1243 | 'R' ->
1244 let ondone s =
1245 match try
1246 Some (int_of_string s)
1247 with exc ->
1248 state.text <- Printf.sprintf "bad integer `%s': %s"
1249 s (Printexc.to_string exc);
1250 None
1251 with
1252 | Some angle -> reinit angle conf.proportional
1253 | None -> ()
1255 TEswitch ("rotation: ", "", None, intentry, ondone)
1257 | 'i' ->
1258 conf.icase <- not conf.icase;
1259 TEdone ("case insensitive search " ^ (btos conf.icase))
1261 | 'p' ->
1262 conf.preload <- not conf.preload;
1263 gotoy state.y;
1264 TEdone ("preload " ^ (btos conf.preload))
1266 | 'v' ->
1267 conf.verbose <- not conf.verbose;
1268 TEdone ("verbose " ^ (btos conf.verbose))
1270 | 'h' ->
1271 conf.maxhfit <- not conf.maxhfit;
1272 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1273 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1275 | 'c' ->
1276 conf.crophack <- not conf.crophack;
1277 TEdone ("crophack " ^ btos conf.crophack)
1279 | 'a' ->
1280 conf.showall <- not conf.showall;
1281 TEdone ("throttle " ^ btos conf.showall)
1283 | 'f' ->
1284 conf.underinfo <- not conf.underinfo;
1285 TEdone ("underinfo " ^ btos conf.underinfo)
1287 | 'P' ->
1288 conf.savebmarks <- not conf.savebmarks;
1289 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1291 | 'S' ->
1292 let ondone s =
1294 let pageno, py =
1295 match state.layout with
1296 | [] -> 0, 0
1297 | l :: _ ->
1298 l.pageno, l.pagey
1300 conf.interpagespace <- int_of_string s;
1301 state.maxy <- calcheight ();
1302 let y = getpagey pageno in
1303 gotoy (y + py)
1304 with exc ->
1305 state.text <- Printf.sprintf "bad integer `%s': %s"
1306 s (Printexc.to_string exc)
1308 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1310 | 'l' ->
1311 reinit conf.angle (not conf.proportional);
1312 TEdone ("proprortional display " ^ btos conf.proportional)
1314 | _ ->
1315 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1316 TEstop
1319 let maxoutlinerows () = (conf.winh - 31) / 16;;
1321 let enterselector allowdel outlines errmsg msg =
1322 if Array.length outlines = 0
1323 then (
1324 showtext ' ' errmsg;
1326 else (
1327 state.text <- msg;
1328 Glut.setCursor Glut.CURSOR_INHERIT;
1329 let pageno =
1330 match state.layout with
1331 | [] -> -1
1332 | {pageno=pageno} :: _ -> pageno
1334 let active =
1335 let rec loop n =
1336 if n = Array.length outlines
1337 then 0
1338 else
1339 let (_, _, (outlinepageno, _)) = outlines.(n) in
1340 if outlinepageno >= pageno then n else loop (n+1)
1342 loop 0
1344 state.mode <- Outline
1345 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1346 state.mode);
1347 Glut.postRedisplay ();
1351 let enteroutlinemode () =
1352 let outlines, msg =
1353 match state.outlines with
1354 | Oarray a -> a, ""
1355 | Olist l ->
1356 let a = Array.of_list (List.rev l) in
1357 state.outlines <- Oarray a;
1358 a, ""
1359 | Onarrow (pat, a, _) ->
1360 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1362 enterselector false outlines "Document has no outline" msg;
1365 let enterbookmarkmode () =
1366 let bookmarks = Array.of_list state.bookmarks in
1367 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1370 let mode_to_string mode =
1371 let b = Buffer.create 10 in
1372 let rec f = function
1373 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1374 | View -> Buffer.add_string b "View"
1375 | Birdseye _ -> Buffer.add_string b "Birdseye"
1376 | Items _ -> Buffer.add_string b "Items"
1377 | Outline _ -> Buffer.add_string b "Outline"
1379 f mode;
1380 Buffer.contents b;
1383 let color_of_string s =
1384 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1385 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1389 let color_to_string (r, g, b) =
1390 let r = truncate (r *. 256.0)
1391 and g = truncate (g *. 256.0)
1392 and b = truncate (b *. 256.0) in
1393 Printf.sprintf "%d/%d/%d" r g b
1396 let enterinfomode () =
1397 let btos = function true -> "on" | _ -> "off" in
1398 let mode = state.mode in
1399 let rec makeitems () =
1400 let intp name get set =
1401 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1402 fun active first _ pan ->
1403 let ondone s =
1404 let n =
1405 try int_of_string s
1406 with exn ->
1407 state.text <- Printf.sprintf "bad integer `%s': %s"
1408 s (Printexc.to_string exn);
1409 max_int;
1411 if n != max_int then set n;
1413 let te = name ^ ": ", "", None, intentry, ondone in
1414 state.text <- "";
1415 Textentry (
1417 fun _ ->
1418 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1421 and boolp name get set =
1422 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1423 fun active first qsearch pan ->
1424 let v = get () in
1425 set (not v);
1426 Items (active, first, makeitems (), qsearch, pan, mode);
1428 and colorp name get set =
1429 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1430 fun active first _ pan ->
1431 let invalid = (nan, nan, nan) in
1432 let ondone s =
1433 let c =
1434 try color_of_string s
1435 with exn ->
1436 state.text <- Printf.sprintf "bad color `%s': %s"
1437 s (Printexc.to_string exn);
1438 invalid
1440 if c <> invalid
1441 then set c;
1443 let te = name ^ ": ", "", None, textentry, ondone in
1444 state.text <- "";
1445 Textentry (
1447 fun _ ->
1448 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1453 let birdseye = isbirdseye mode in
1454 let items = [
1455 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1457 boolp "presentation"
1458 (fun () -> conf.presentation)
1459 (fun v ->
1460 conf.presentation <- v;
1461 state.anchor <- getanchor ();
1462 represent ());
1464 boolp "ignore case in searches"
1465 (fun () -> conf.icase)
1466 (fun v -> conf.icase <- v);
1468 boolp "preload"
1469 (fun () -> conf.preload)
1470 (fun v -> conf.preload <- v);
1472 boolp "verbose"
1473 (fun () -> conf.verbose)
1474 (fun v -> conf.verbose <- v);
1476 boolp "max fit"
1477 (fun () -> conf.maxhfit)
1478 (fun v -> conf.maxhfit <- v);
1480 boolp "crop hack"
1481 (fun () -> conf.crophack)
1482 (fun v -> conf.crophack <- v);
1484 boolp "throttle"
1485 (fun () -> conf.showall)
1486 (fun v -> conf.showall <- v);
1488 boolp "highlight links"
1489 (fun () -> conf.hlinks)
1490 (fun v -> conf.hlinks <- v);
1492 boolp "under info"
1493 (fun () -> conf.underinfo)
1494 (fun v -> conf.underinfo <- v);
1495 boolp "persistent bookmarks"
1496 (fun () -> conf.savebmarks)
1497 (fun v -> conf.savebmarks <- v);
1499 boolp "proportional display"
1500 (fun () -> conf.proportional)
1501 (fun v -> reinit conf.angle v);
1503 boolp "persistent location"
1504 (fun () -> conf.jumpback)
1505 (fun v -> conf.jumpback <- v);
1507 "", 0, Noaction;
1509 intp "vertical margin"
1510 (fun () -> conf.interpagespace)
1511 (fun n ->
1512 conf.interpagespace <- n;
1513 let pageno, py =
1514 match state.layout with
1515 | [] -> 0, 0
1516 | l :: _ ->
1517 l.pageno, l.pagey
1519 state.maxy <- calcheight ();
1520 let y = getpagey pageno in
1521 gotoy (y + py)
1524 intp "page bias"
1525 (fun () -> conf.pagebias)
1526 (fun v -> conf.pagebias <- v);
1528 intp "scroll step"
1529 (fun () -> conf.scrollstep)
1530 (fun n -> conf.scrollstep <- n);
1532 intp "auto scroll step"
1533 (fun () ->
1534 match state.autoscroll with
1535 | Some step -> step
1536 | _ -> conf.autoscrollstep)
1537 (fun n ->
1538 if state.autoscroll <> None
1539 then state.autoscroll <- Some n;
1540 conf.autoscrollstep <- n);
1542 intp "zoom"
1543 (fun () -> truncate (conf.zoom *. 100.))
1544 (fun v -> setzoom ((float v) /. 100.));
1546 intp "rotation"
1547 (fun () -> conf.angle)
1548 (fun v -> reinit v conf.proportional);
1550 intp "scroll bar width"
1551 (fun () -> state.scrollw)
1552 (fun v ->
1553 state.scrollw <- v;
1554 conf.scrollbw <- v;
1555 reshape conf.winw conf.winh;
1558 intp "scroll handle height"
1559 (fun () -> conf.scrollh)
1560 (fun v -> conf.scrollh <- v;);
1562 intp "thumbnail width"
1563 (fun () -> conf.thumbw)
1564 (fun v ->
1565 conf.thumbw <- min 1920 v;
1566 match mode with
1567 | Birdseye beye ->
1568 leavebirdseye beye false;
1569 enterbirdseye ()
1570 | _ -> ()
1573 colorp "background color"
1574 (fun () -> conf.bgcolor)
1575 (fun v -> conf.bgcolor <- v);
1577 "", 0, Noaction;
1578 "Presentation mode", 0, Noaction;
1580 boolp "scrollbar visible"
1581 (fun () -> conf.scrollbarinpm)
1582 (fun v ->
1583 if v != conf.scrollbarinpm
1584 then (
1585 conf.scrollbarinpm <- v;
1586 if conf.presentation
1587 then (
1588 state.scrollw <- if v then conf.scrollbw else 0;
1589 reshape conf.winw conf.winh;
1594 "", 0, Noaction;
1595 "Pixmap Cache", 0, Noaction;
1597 intp "size (advisory)"
1598 (fun () -> conf.memlimit)
1599 (fun v -> conf.memlimit <- v);
1600 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1604 let tailer =
1605 let trailer =
1606 ("", 0, Noaction)
1607 :: ("Document", 0, Noaction)
1608 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1610 if birdseye
1611 then trailer
1612 else (
1613 ("", 0, Noaction)
1614 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1615 (btos conf.bedefault),
1617 Action (
1618 fun active first qsearch pan ->
1619 conf.bedefault <- not conf.bedefault;
1620 Items (active, first, makeitems (), qsearch, pan, mode);
1622 ) :: trailer
1625 Array.of_list (items @ tailer)
1627 state.text <- "";
1628 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1629 Glut.postRedisplay ();
1632 let enterhelpmode () =
1633 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1634 Glut.postRedisplay ();
1637 let quickbookmark ?title () =
1638 match state.layout with
1639 | [] -> ()
1640 | l :: _ ->
1641 let title =
1642 match title with
1643 | None ->
1644 let sec = Unix.gettimeofday () in
1645 let tm = Unix.localtime sec in
1646 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1647 (l.pageno+1)
1648 tm.Unix.tm_mday
1649 tm.Unix.tm_mon
1650 (tm.Unix.tm_year + 1900)
1651 tm.Unix.tm_hour
1652 tm.Unix.tm_min
1653 | Some title -> title
1655 state.bookmarks <-
1656 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1657 :: state.bookmarks
1660 let doreshape w h =
1661 state.fullscreen <- None;
1662 Glut.reshapeWindow w h;
1665 let writeopen path password =
1666 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1669 let opendoc path password =
1670 invalidate ();
1671 state.path <- path;
1672 state.password <- password;
1673 state.gen <- state.gen + 1;
1674 state.docinfo <- [];
1676 writeopen path password;
1677 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1678 wcmd "geometry" [`i state.w; `i conf.winh];
1681 let viewkeyboard key =
1682 let enttext te =
1683 let mode = state.mode in
1684 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1685 state.text <- "";
1686 enttext ();
1687 Glut.postRedisplay ()
1689 let c = Char.chr key in
1690 match c with
1691 | '\027' | 'q' -> (* escape *)
1692 raise Quit
1694 | '\008' -> (* backspace *)
1695 let y = getnav ~-1 in
1696 gotoy_and_clear_text y
1698 | 'o' ->
1699 enteroutlinemode ()
1701 | 'u' ->
1702 state.rects <- [];
1703 state.text <- "";
1704 Glut.postRedisplay ()
1706 | '/' | '?' ->
1707 let ondone isforw s =
1708 cbput state.hists.pat s;
1709 state.searchpattern <- s;
1710 search s isforw
1712 let s = String.create 1 in
1713 s.[0] <- c;
1714 enttext (s, "", Some (onhist state.hists.pat),
1715 textentry, ondone (c ='/'))
1717 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1718 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1719 setzoom (conf.zoom +. incr)
1721 | '+' ->
1722 let ondone s =
1723 let n =
1724 try int_of_string s with exc ->
1725 state.text <- Printf.sprintf "bad integer `%s': %s"
1726 s (Printexc.to_string exc);
1727 max_int
1729 if n != max_int
1730 then (
1731 conf.pagebias <- n;
1732 state.text <- "page bias is now " ^ string_of_int n;
1735 enttext ("page bias: ", "", None, intentry, ondone)
1737 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1738 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1739 setzoom (max 0.01 (conf.zoom -. decr))
1741 | '-' ->
1742 let ondone msg = state.text <- msg in
1743 enttext (
1744 "option [acfhilpstvAPRSZ]: ", "", None,
1745 optentry state.mode, ondone
1748 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1749 setzoom 1.0
1751 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1752 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1753 if zoom < 1.0
1754 then setzoom zoom
1756 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1757 togglebirdseye ()
1759 | '0' .. '9' ->
1760 let ondone s =
1761 let n =
1762 try int_of_string s with exc ->
1763 state.text <- Printf.sprintf "bad integer `%s': %s"
1764 s (Printexc.to_string exc);
1767 if n >= 0
1768 then (
1769 addnav ();
1770 cbput state.hists.pag (string_of_int n);
1771 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1774 let pageentry text key =
1775 match Char.unsafe_chr key with
1776 | 'g' -> TEdone text
1777 | _ -> intentry text key
1779 let text = "x" in text.[0] <- c;
1780 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1782 | 'b' ->
1783 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1784 reshape conf.winw conf.winh;
1786 | 'l' ->
1787 conf.hlinks <- not conf.hlinks;
1788 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1789 Glut.postRedisplay ()
1791 | 'a' ->
1792 begin match state.autoscroll with
1793 | Some step ->
1794 conf.autoscrollstep <- step;
1795 state.autoscroll <- None
1796 | None ->
1797 if conf.autoscrollstep = 0
1798 then state.autoscroll <- Some 1
1799 else state.autoscroll <- Some conf.autoscrollstep
1802 | 'P' ->
1803 conf.presentation <- not conf.presentation;
1804 if conf.presentation
1805 then (
1806 if not conf.scrollbarinpm
1807 then state.scrollw <- 0;
1809 else
1810 state.scrollw <- conf.scrollbw;
1812 showtext ' ' ("presentation mode " ^
1813 if conf.presentation then "on" else "off");
1814 state.anchor <- getanchor ();
1815 represent ()
1817 | 'f' ->
1818 begin match state.fullscreen with
1819 | None ->
1820 state.fullscreen <- Some (conf.winw, conf.winh);
1821 Glut.fullScreen ()
1822 | Some (w, h) ->
1823 state.fullscreen <- None;
1824 doreshape w h
1827 | 'g' ->
1828 gotoy_and_clear_text 0
1830 | 'G' ->
1831 gotopage1 (state.pagecount - 1) 0
1833 | 'n' ->
1834 search state.searchpattern true
1836 | 'p' | 'N' ->
1837 search state.searchpattern false
1839 | 't' ->
1840 begin match state.layout with
1841 | [] -> ()
1842 | l :: _ ->
1843 gotoy_and_clear_text (getpagey l.pageno)
1846 | ' ' ->
1847 begin match List.rev state.layout with
1848 | [] -> ()
1849 | l :: _ ->
1850 let pageno = min (l.pageno+1) (state.pagecount-1) in
1851 gotoy_and_clear_text (getpagey pageno)
1854 | '\127' -> (* del *)
1855 begin match state.layout with
1856 | [] -> ()
1857 | l :: _ ->
1858 let pageno = max 0 (l.pageno-1) in
1859 gotoy_and_clear_text (getpagey pageno)
1862 | '=' ->
1863 let f (fn, _) l =
1864 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1866 let fn, ln = List.fold_left f (-1, -1) state.layout in
1867 let s =
1868 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1869 let percent =
1870 if maxy <= 0
1871 then 100.
1872 else (100. *. (float state.y /. float maxy)) in
1873 if fn = ln
1874 then
1875 Printf.sprintf "Page %d of %d %.2f%%"
1876 (fn+1) state.pagecount percent
1877 else
1878 Printf.sprintf
1879 "Pages %d-%d of %d %.2f%%"
1880 (fn+1) (ln+1) state.pagecount percent
1882 showtext ' ' s;
1884 | 'w' ->
1885 begin match state.layout with
1886 | [] -> ()
1887 | l :: _ ->
1888 doreshape (l.pagew + state.scrollw) l.pageh;
1889 Glut.postRedisplay ();
1892 | '\'' ->
1893 enterbookmarkmode ()
1895 | 'h' ->
1896 enterhelpmode ()
1898 | 'i' ->
1899 enterinfomode ()
1901 | 'm' ->
1902 let ondone s =
1903 match state.layout with
1904 | l :: _ ->
1905 state.bookmarks <-
1906 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1907 :: state.bookmarks
1908 | _ -> ()
1910 enttext ("bookmark: ", "", None, textentry, ondone)
1912 | '~' ->
1913 quickbookmark ();
1914 showtext ' ' "Quick bookmark added";
1916 | 'z' ->
1917 begin match state.layout with
1918 | l :: _ ->
1919 let rect = getpdimrect l.pagedimno in
1920 let w, h =
1921 if conf.crophack
1922 then
1923 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1924 truncate (1.2 *. (rect.(3) -. rect.(0))))
1925 else
1926 (truncate (rect.(1) -. rect.(0)),
1927 truncate (rect.(3) -. rect.(0)))
1929 if w != 0 && h != 0
1930 then
1931 doreshape (w + state.scrollw) (h + conf.interpagespace)
1933 Glut.postRedisplay ();
1935 | [] -> ()
1938 | '\000' -> (* ctrl-2 *)
1939 let maxw = getmaxw () in
1940 if maxw > 0.0
1941 then setzoom (maxw /. float conf.winw)
1943 | '<' | '>' ->
1944 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1946 | '[' | ']' ->
1947 state.colorscale <-
1948 max 0.0
1949 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1950 Glut.postRedisplay ()
1952 | 'k' ->
1953 begin match state.mode with
1954 | Birdseye beye -> upbirdseye beye
1955 | _ -> gotoy (clamp (-conf.scrollstep))
1958 | 'j' ->
1959 begin match state.mode with
1960 | Birdseye beye -> downbirdseye beye
1961 | _ -> gotoy (clamp conf.scrollstep)
1964 | 'r' ->
1965 state.anchor <- getanchor ();
1966 opendoc state.path state.password
1968 | _ ->
1969 vlog "huh? %d %c" key (Char.chr key);
1972 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1973 let enttext te =
1974 state.mode <- Textentry (te, onleave);
1975 state.text <- "";
1976 enttext ();
1977 Glut.postRedisplay ()
1979 match Char.unsafe_chr key with
1980 | '\008' -> (* backspace *)
1981 let len = String.length text in
1982 if len = 0
1983 then (
1984 onleave Cancel;
1985 Glut.postRedisplay ();
1987 else (
1988 let s = String.sub text 0 (len - 1) in
1989 enttext (c, s, opthist, onkey, ondone)
1992 | '\r' | '\n' ->
1993 ondone text;
1994 onleave Confirm;
1995 Glut.postRedisplay ()
1997 | '\007' (* ctrl-g *)
1998 | '\027' -> (* escape *)
1999 begin match opthist with
2000 | None -> ()
2001 | Some (_, onhistcancel) -> onhistcancel ()
2002 end;
2003 onleave Cancel;
2004 Glut.postRedisplay ()
2006 | _ ->
2007 begin match onkey text key with
2008 | TEdone text ->
2009 onleave Confirm;
2010 ondone text;
2011 Glut.postRedisplay ()
2013 | TEcont text ->
2014 enttext (c, text, opthist, onkey, ondone);
2016 | TEstop ->
2017 onleave Cancel;
2018 Glut.postRedisplay ()
2020 | TEswitch te ->
2021 state.mode <- Textentry (te, onleave);
2022 Glut.postRedisplay ()
2023 end;
2026 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2027 match key with
2028 | 27 -> (* escape *)
2029 leavebirdseye beye true
2031 | 12 -> (* ctrl-l *)
2032 let y, h = getpageyh pageno in
2033 let top = (conf.winh - h) / 2 in
2034 gotoy (max 0 (y - top))
2036 | 13 -> (* enter *)
2037 leavebirdseye beye false
2039 | _ ->
2040 viewkeyboard key
2043 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2044 let set active first qsearch =
2045 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2047 let search active pattern incr =
2048 let dosearch re =
2049 let rec loop n =
2050 if n >= Array.length items || n < 0
2051 then None
2052 else
2053 let (s, _, _) = items.(n) in
2055 (try ignore (Str.search_forward re s 0); true
2056 with Not_found -> false)
2057 then Some n
2058 else loop (n + incr)
2060 loop active
2063 let re = Str.regexp_case_fold pattern in
2064 dosearch re
2065 with Failure s ->
2066 state.text <- s;
2067 None
2069 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2070 match key with
2071 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2072 let incr = if key = 18 then -1 else 1 in
2073 let active, first =
2074 match search (active + incr) qsearch incr with
2075 | None ->
2076 state.text <- qsearch ^ " [not found]";
2077 active, first
2078 | Some active ->
2079 state.text <- qsearch;
2080 active, firstof active
2082 set active first qsearch;
2083 Glut.postRedisplay ();
2085 | 8 -> (* backspace *)
2086 let len = String.length qsearch in
2087 if len = 0
2088 then ()
2089 else (
2090 if len = 1
2091 then (
2092 state.text <- "";
2093 set active first "";
2095 else
2096 let qsearch = String.sub qsearch 0 (len - 1) in
2097 let active, first =
2098 match search active qsearch ~-1 with
2099 | None ->
2100 state.text <- qsearch ^ " [not found]";
2101 active, first
2102 | Some active ->
2103 state.text <- qsearch;
2104 active, firstof active
2106 set active first qsearch
2108 Glut.postRedisplay ()
2110 | _ when key >= 32 && key < 127 ->
2111 let pattern = addchar qsearch (Char.chr key) in
2112 let active, first =
2113 match search active pattern 1 with
2114 | None ->
2115 state.text <- pattern ^ " [not found]";
2116 active, first
2117 | Some active ->
2118 state.text <- pattern;
2119 active, firstof active
2121 set active first pattern;
2122 Glut.postRedisplay ()
2124 | 27 -> (* escape *)
2125 state.text <- "";
2126 if String.length qsearch = 0
2127 then (
2128 state.mode <- oldmode;
2130 else (
2131 set active first "";
2133 Glut.postRedisplay ()
2135 | 13 -> (* enter *)
2136 if active >= 0 && active < Array.length items
2137 then (
2138 match items.(active) with
2139 | _, _, Action f ->
2140 state.mode <- f active first qsearch pan
2142 | _, _, Noaction ->
2143 state.text <- "";
2144 state.mode <- oldmode
2146 else (
2147 state.text <- "";
2148 state.mode <- oldmode
2150 Glut.postRedisplay ();
2152 | _ -> dolog "unknown key %d" key
2155 let outlinekeyboard key
2156 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2157 let narrow outlines pattern =
2158 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2159 match reopt with
2160 | None -> None
2161 | Some re ->
2162 let rec fold accu n =
2163 if n = -1
2164 then accu
2165 else
2166 let (s, _, _) as o = outlines.(n) in
2167 let accu =
2168 if (try ignore (Str.search_forward re s 0); true
2169 with Not_found -> false)
2170 then (o :: accu)
2171 else accu
2173 fold accu (n-1)
2175 let matched = fold [] (Array.length outlines - 1) in
2176 if matched = [] then None else Some (Array.of_list matched)
2178 let search active pattern incr =
2179 let dosearch re =
2180 let rec loop n =
2181 if n = Array.length outlines || n = -1
2182 then None
2183 else
2184 let (s, _, _) = outlines.(n) in
2186 (try ignore (Str.search_forward re s 0); true
2187 with Not_found -> false)
2188 then Some n
2189 else loop (n + incr)
2191 loop active
2194 let re = Str.regexp_case_fold pattern in
2195 dosearch re
2196 with Failure s ->
2197 state.text <- s;
2198 None
2200 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2201 match key with
2202 | 27 -> (* escape *)
2203 state.text <- "";
2204 if String.length qsearch = 0
2205 then (
2206 state.mode <- oldmode;
2208 else (
2209 state.mode <- Outline (
2210 allowdel, active, first, outlines, "", pan, oldmode
2213 Glut.postRedisplay ();
2215 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2216 let incr = if key = 18 then -1 else 1 in
2217 let active, first =
2218 match search (active + incr) qsearch incr with
2219 | None ->
2220 state.text <- qsearch ^ " [not found]";
2221 active, first
2222 | Some active ->
2223 state.text <- qsearch;
2224 active, firstof active
2226 state.mode <- Outline (
2227 allowdel, active, first, outlines, qsearch, pan, oldmode
2229 Glut.postRedisplay ();
2231 | 8 -> (* backspace *)
2232 let len = String.length qsearch in
2233 if len = 0
2234 then ()
2235 else (
2236 if len = 1
2237 then (
2238 state.text <- "";
2239 state.mode <- Outline (
2240 allowdel, active, first, outlines, "", pan, oldmode
2243 else
2244 let qsearch = String.sub qsearch 0 (len - 1) in
2245 let active, first =
2246 match search active qsearch ~-1 with
2247 | None ->
2248 state.text <- qsearch ^ " [not found]";
2249 active, first
2250 | Some active ->
2251 state.text <- qsearch;
2252 active, firstof active
2254 state.mode <- Outline (
2255 allowdel, active, first, outlines, qsearch, pan, oldmode
2258 Glut.postRedisplay ()
2260 | 13 -> (* enter *)
2261 if active < Array.length outlines
2262 then (
2263 let (_, _, anchor) = outlines.(active) in
2264 addnav ();
2265 gotoanchor anchor;
2267 state.text <- "";
2268 if allowdel then state.bookmarks <- Array.to_list outlines;
2269 state.mode <- oldmode;
2270 Glut.postRedisplay ();
2272 | _ when key >= 32 && key < 127 ->
2273 let pattern = addchar qsearch (Char.chr key) in
2274 let active, first =
2275 match search active pattern 1 with
2276 | None ->
2277 state.text <- pattern ^ " [not found]";
2278 active, first
2279 | Some active ->
2280 state.text <- pattern;
2281 active, firstof active
2283 state.mode <- Outline (
2284 allowdel, active, first, outlines, pattern, pan, oldmode
2286 Glut.postRedisplay ()
2288 | 14 when not allowdel -> (* ctrl-n *)
2289 if String.length qsearch > 0
2290 then (
2291 let optoutlines = narrow outlines qsearch in
2292 begin match optoutlines with
2293 | None -> state.text <- "can't narrow"
2294 | Some outlines ->
2295 state.mode <- Outline (
2296 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2298 match state.outlines with
2299 | Olist _ -> ()
2300 | Oarray a ->
2301 state.outlines <- Onarrow (qsearch, outlines, a)
2302 | Onarrow (_, _, b) ->
2303 state.outlines <- Onarrow (qsearch, outlines, b)
2304 end;
2306 Glut.postRedisplay ()
2308 | 21 when not allowdel -> (* ctrl-u *)
2309 let outline =
2310 match state.outlines with
2311 | Oarray a -> a
2312 | Olist l ->
2313 let a = Array.of_list (List.rev l) in
2314 state.outlines <- Oarray a;
2316 | Onarrow (_, _, b) ->
2317 state.outlines <- Oarray b;
2318 state.text <- "";
2321 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2322 Glut.postRedisplay ()
2324 | 12 -> (* ctrl-l *)
2325 state.mode <- Outline
2326 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2327 Glut.postRedisplay ()
2329 | 127 when allowdel -> (* delete *)
2330 let len = Array.length outlines - 1 in
2331 if len = 0
2332 then (
2333 state.mode <- View;
2334 state.bookmarks <- [];
2336 else (
2337 let bookmarks = Array.init len
2338 (fun i ->
2339 let i = if i >= active then i + 1 else i in
2340 outlines.(i)
2343 state.mode <-
2344 Outline (
2345 allowdel,
2346 min active (len-1),
2347 min first (len-1),
2348 bookmarks, qsearch,
2350 oldmode
2353 Glut.postRedisplay ()
2355 | _ -> dolog "unknown key %d" key
2358 let keyboard ~key ~x ~y =
2359 ignore x;
2360 ignore y;
2361 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
2362 then
2363 wcmd "interrupt" []
2364 else
2365 match state.mode with
2366 | Outline outline -> outlinekeyboard key outline
2367 | Textentry textentry -> textentrykeyboard key textentry
2368 | Birdseye birdseye -> birdseyekeyboard key birdseye
2369 | View -> viewkeyboard key
2370 | Items items -> itemskeyboard key items
2373 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2374 match key with
2375 | Glut.KEY_UP -> upbirdseye beye
2376 | Glut.KEY_DOWN -> downbirdseye beye
2378 | Glut.KEY_PAGE_UP ->
2379 begin match state.layout with
2380 | l :: _ ->
2381 if l.pagey != 0
2382 then (
2383 state.mode <- Birdseye (
2384 conf, leftx, l.pageno, hooverpageno, anchor
2386 gotopage1 l.pageno 0;
2388 else (
2389 let layout = layout (state.y-conf.winh) conf.winh in
2390 match layout with
2391 | [] -> gotoy (clamp (-conf.winh))
2392 | l :: _ ->
2393 state.mode <- Birdseye (
2394 conf, leftx, l.pageno, hooverpageno, anchor
2396 gotopage1 l.pageno 0
2399 | [] -> gotoy (clamp (-conf.winh))
2400 end;
2402 | Glut.KEY_PAGE_DOWN ->
2403 begin match List.rev state.layout with
2404 | l :: _ ->
2405 let layout = layout (state.y + conf.winh) conf.winh in
2406 begin match layout with
2407 | [] ->
2408 let incr = l.pageh - l.pagevh in
2409 if incr = 0
2410 then (
2411 state.mode <-
2412 Birdseye (
2413 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2415 Glut.postRedisplay ();
2417 else gotoy (clamp (incr + conf.interpagespace*2));
2419 | l :: _ ->
2420 state.mode <-
2421 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2422 gotopage1 l.pageno 0;
2425 | [] -> gotoy (clamp conf.winh)
2426 end;
2428 | Glut.KEY_HOME ->
2429 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2430 gotopage1 0 0
2432 | Glut.KEY_END ->
2433 let pageno = state.pagecount - 1 in
2434 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2435 if not (pagevisible state.layout pageno)
2436 then
2437 let h =
2438 match List.rev state.pdims with
2439 | [] -> conf.winh
2440 | (_, _, h, _) :: _ -> h
2442 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2443 else Glut.postRedisplay ();
2444 | _ -> ()
2447 let setautoscrollspeed step goingdown =
2448 let incr = max 1 ((abs step) / 2) in
2449 let incr = if goingdown then incr else -incr in
2450 let astep = step + incr in
2451 state.autoscroll <- Some astep;
2454 let special ~key ~x ~y =
2455 ignore x;
2456 ignore y;
2457 match state.mode with
2458 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2459 togglebirdseye ()
2461 | Birdseye vals ->
2462 birdseyespecial key vals
2464 | View when key = Glut.KEY_F1 ->
2465 enterhelpmode ()
2467 | View ->
2468 begin match state.autoscroll with
2469 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2470 setautoscrollspeed step (key = Glut.KEY_DOWN)
2472 | _ ->
2473 let y =
2474 match key with
2475 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2476 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2477 | Glut.KEY_DOWN -> clamp conf.scrollstep
2478 | Glut.KEY_PAGE_UP ->
2479 if Glut.getModifiers () land Glut.active_ctrl != 0
2480 then
2481 match state.layout with
2482 | [] -> state.y
2483 | l :: _ -> state.y - l.pagey
2484 else
2485 clamp (-conf.winh)
2486 | Glut.KEY_PAGE_DOWN ->
2487 if Glut.getModifiers () land Glut.active_ctrl != 0
2488 then
2489 match List.rev state.layout with
2490 | [] -> state.y
2491 | l :: _ -> getpagey l.pageno
2492 else
2493 clamp conf.winh
2494 | Glut.KEY_HOME -> addnav (); 0
2495 | Glut.KEY_END ->
2496 addnav ();
2497 state.maxy - (if conf.maxhfit then conf.winh else 0)
2499 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2500 Glut.getModifiers () land Glut.active_alt != 0 ->
2501 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2503 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2504 state.x <- state.x - 10;
2505 state.y
2506 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2507 state.x <- state.x + 10;
2508 state.y
2510 | _ -> state.y
2512 gotoy_and_clear_text y
2515 | Textentry
2516 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2517 let s =
2518 match key with
2519 | Glut.KEY_UP -> action HCprev
2520 | Glut.KEY_DOWN -> action HCnext
2521 | Glut.KEY_HOME -> action HCfirst
2522 | Glut.KEY_END -> action HClast
2523 | _ -> state.text
2525 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2526 Glut.postRedisplay ()
2528 | Textentry _ -> ()
2530 | Items (active, first, items, qsearch, pan, oldmode) ->
2531 let maxrows = maxoutlinerows () in
2532 let itemcount = Array.length items in
2533 let hasaction = function
2534 | (_, _, Noaction) -> false
2535 | _ -> true
2537 let find start incr =
2538 let rec find i =
2539 if i = -1 || i = itemcount
2540 then -1
2541 else (
2542 if hasaction items.(i)
2543 then i
2544 else find (i + incr)
2547 find start
2549 let set active first =
2550 let first = max 0 (min first (itemcount - maxrows)) in
2551 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2553 let navigate incr =
2554 let isvisible first n = n >= first && n - first <= maxrows in
2555 let active, first =
2556 let incr1 = if incr > 0 then 1 else -1 in
2557 if isvisible first active
2558 then
2559 let next =
2560 let next = active + incr in
2561 let next =
2562 if next < 0 || next >= itemcount
2563 then -1
2564 else find next incr1
2566 if next = -1 || abs (active - next) > maxrows
2567 then -1
2568 else next
2570 if next = -1
2571 then
2572 let first = first + incr in
2573 let first = max 0 (min first (itemcount - 1)) in
2574 let next =
2575 let next = active + incr in
2576 let next = max 0 (min next (itemcount - 1)) in
2577 find next ~-incr1
2579 let active = if next = -1 then active else next in
2580 active, first
2581 else
2582 let first = min next first in
2583 next, first
2584 else
2585 let first = first + incr in
2586 let first = max 0 (min first (itemcount - 1)) in
2587 let active =
2588 let next = active + incr in
2589 let next = max 0 (min next (itemcount - 1)) in
2590 let next = find next incr1 in
2591 if next = -1 || abs (active - first) > maxrows
2592 then active
2593 else next
2595 active, first
2597 set active first;
2598 Glut.postRedisplay ()
2600 begin match key with
2601 | Glut.KEY_UP -> navigate ~-1
2602 | Glut.KEY_DOWN -> navigate 1
2603 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2604 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2606 | Glut.KEY_RIGHT ->
2607 state.mode <- Items (
2608 active, first, items, qsearch, min 0 (pan - 1), oldmode
2610 Glut.postRedisplay ()
2612 | Glut.KEY_LEFT ->
2613 state.mode <- Items (
2614 active, first, items, qsearch, min 0 (pan + 1), oldmode
2616 Glut.postRedisplay ()
2618 | Glut.KEY_HOME ->
2619 let active = find 0 1 in
2620 set active 0;
2621 Glut.postRedisplay ()
2623 | Glut.KEY_END ->
2624 let first = max 0 (itemcount - maxrows) in
2625 let active = find (itemcount - 1) ~-1 in
2626 set active first;
2627 Glut.postRedisplay ()
2629 | _ -> ()
2630 end;
2632 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2633 let maxrows = maxoutlinerows () in
2634 let calcfirst first active =
2635 if active > first
2636 then
2637 let rows = active - first in
2638 if rows > maxrows then active - maxrows else first
2639 else active
2641 let navigate incr =
2642 let active = active + incr in
2643 let active = max 0 (min active (Array.length outlines - 1)) in
2644 let first = calcfirst first active in
2645 state.mode <- Outline (
2646 allowdel, active, first, outlines, qsearch, pan, oldmode
2648 Glut.postRedisplay ()
2650 let updownlevel incr =
2651 let len = Array.length outlines in
2652 let (_, curlevel, _) = outlines.(active) in
2653 let rec flow i =
2654 if i = len then i-1 else if i = -1 then 0 else
2655 let (_, l, _) = outlines.(i) in
2656 if l != curlevel then i else flow (i+incr)
2658 let active = flow active in
2659 let first = calcfirst first active in
2660 state.mode <- Outline (
2661 allowdel, active, first, outlines, qsearch, pan, oldmode
2663 Glut.postRedisplay ()
2665 match key with
2666 | Glut.KEY_UP -> navigate ~-1
2667 | Glut.KEY_DOWN -> navigate 1
2668 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2669 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2671 | Glut.KEY_RIGHT ->
2672 if Glut.getModifiers () land Glut.active_ctrl != 0
2673 then (
2674 state.mode <- Outline (
2675 allowdel, active, first, outlines,
2676 qsearch, min 0 (pan + 1), oldmode
2678 Glut.postRedisplay ();
2680 else (
2681 if not allowdel
2682 then updownlevel 1
2685 | Glut.KEY_LEFT ->
2686 if Glut.getModifiers () land Glut.active_ctrl != 0
2687 then (
2688 state.mode <- Outline (
2689 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2691 Glut.postRedisplay ();
2693 else (
2694 if not allowdel
2695 then updownlevel ~-1
2698 | Glut.KEY_HOME ->
2699 state.mode <- Outline (
2700 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2702 Glut.postRedisplay ()
2704 | Glut.KEY_END ->
2705 let active = Array.length outlines - 1 in
2706 let first = max 0 (active - maxrows) in
2707 state.mode <- Outline (
2708 allowdel, active, first, outlines, qsearch, pan, oldmode
2710 Glut.postRedisplay ()
2712 | _ -> ()
2715 let drawplaceholder l =
2716 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2717 GlDraw.rect
2718 (float l.pagex, float l.pagedispy)
2719 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2721 let x = if margin < 0 then -margin else l.pagex
2722 and y = l.pagedispy + 13 in
2723 GlDraw.color (0.0, 0.0, 0.0);
2724 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2727 let now () = Unix.gettimeofday ();;
2729 let drawpage l =
2730 let color =
2731 match state.mode with
2732 | Textentry _ -> scalecolor 0.4
2733 | View | Outline _ | Items _ -> scalecolor 1.0
2734 | Birdseye (_, _, pageno, hooverpageno, _) ->
2735 if l.pageno = hooverpageno
2736 then scalecolor 0.9
2737 else (
2738 if l.pageno = pageno
2739 then scalecolor 1.0
2740 else scalecolor 0.8
2743 GlDraw.color color;
2744 begin match getopaque l.pageno with
2745 | Some (opaque, _) when validopaque opaque ->
2746 let a = now () in
2747 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2748 let b = now () in
2749 let d = b-.a in
2750 vlog "draw %d %f sec" l.pageno d;
2752 | _ ->
2753 drawplaceholder l;
2754 end;
2757 let scrollph y =
2758 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2759 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2760 let sh = float conf.winh /. sh in
2761 let sh = max sh (float conf.scrollh) in
2763 let percent =
2764 if state.y = state.maxy
2765 then 1.0
2766 else float y /. float maxy
2768 let position = (float conf.winh -. sh) *. percent in
2770 let position =
2771 if position +. sh > float conf.winh
2772 then float conf.winh -. sh
2773 else position
2775 position, sh;
2778 let scrollindicator () =
2779 GlDraw.color (0.64 , 0.64, 0.64);
2780 GlDraw.rect
2781 (float (conf.winw - state.scrollw), 0.)
2782 (float conf.winw, float conf.winh)
2784 GlDraw.color (0.0, 0.0, 0.0);
2786 let position, sh = scrollph state.y in
2787 GlDraw.rect
2788 (float (conf.winw - state.scrollw), position)
2789 (float conf.winw, position +. sh)
2793 let showsel margin =
2794 match state.mstate with
2795 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2798 | Msel ((x0, y0), (x1, y1)) ->
2799 let rec loop = function
2800 | l :: ls ->
2801 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2802 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2803 then
2804 match getopaque l.pageno with
2805 | Some (opaque, _) when validopaque opaque ->
2806 let oy = -l.pagey + l.pagedispy in
2807 seltext opaque
2808 (x0 - margin - state.x, y0,
2809 x1 - margin - state.x, y1) oy;
2811 | _ -> ()
2812 else loop ls
2813 | [] -> ()
2815 loop state.layout
2818 let showrects () =
2819 let panx = float state.x in
2820 Gl.enable `blend;
2821 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2822 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2823 List.iter
2824 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2825 List.iter (fun l ->
2826 if l.pageno = pageno
2827 then (
2828 let d = float (l.pagedispy - l.pagey) in
2829 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2830 GlDraw.begins `quads;
2832 GlDraw.vertex2 (x0+.panx, y0+.d);
2833 GlDraw.vertex2 (x1+.panx, y1+.d);
2834 GlDraw.vertex2 (x2+.panx, y2+.d);
2835 GlDraw.vertex2 (x3+.panx, y3+.d);
2837 GlDraw.ends ();
2839 ) state.layout
2840 ) state.rects
2842 Gl.disable `blend;
2845 let showstrings trusted active first pan strings =
2846 Gl.enable `blend;
2847 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2848 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2849 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2850 GlDraw.color (1., 1., 1.);
2851 Gl.enable `texture_2d;
2853 let wx = measurestr 15 "w" in
2854 let tabx = 30.0*.wx +. float (pan*15) in
2855 let rec loop row =
2856 if row = Array.length strings || (row - first) * 16 > conf.winh
2857 then ()
2858 else (
2859 let (s, level, _) = strings.(row) in
2860 let y = (row - first) * 16 in
2861 let x = 5 + 15*(max 0 (level+pan)) in
2862 if row = active
2863 then (
2864 Gl.disable `texture_2d;
2865 GlDraw.polygon_mode `both `line;
2866 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2867 GlDraw.rect (0., float (y + 1))
2868 (float (conf.winw - 1), float (y + 18));
2869 GlDraw.polygon_mode `both `fill;
2870 GlDraw.color (1., 1., 1.);
2871 Gl.enable `texture_2d;
2874 let drawtabularstring x s =
2875 let _ =
2876 if trusted
2877 then
2878 let tabpos = try String.index s '\t' with Not_found -> -1 in
2879 if tabpos > 0
2880 then
2881 let len = String.length s - tabpos - 1 in
2882 let s1 = String.sub s 0 tabpos
2883 and s2 = String.sub s (tabpos + 1) len in
2884 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2885 let x = truncate (max xx tabx) in
2886 drawstring1 15 x (y + 16) s2
2887 else
2888 drawstring1 15 x (y + 16) s
2889 else
2890 drawstring1 15 x (y + 16) s
2894 drawtabularstring (x + pan*15) s;
2895 loop (row+1)
2898 loop first;
2899 Gl.disable `blend;
2900 Gl.disable `texture_2d;
2903 let showoutline (_, active, first, outlines, _, pan, _) =
2904 showstrings false active first pan outlines;
2907 let showitems (active, first, items, _, pan, _) =
2908 showstrings true active first pan items;
2911 let display () =
2912 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2913 GlDraw.viewport margin 0 state.w conf.winh;
2914 pagematrix ();
2915 GlClear.color (scalecolor2 conf.bgcolor);
2916 GlClear.clear [`color];
2917 if conf.zoom > 1.0
2918 then (
2919 Gl.enable `scissor_test;
2920 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2922 List.iter drawpage state.layout;
2923 if conf.zoom > 1.0
2924 then
2925 Gl.disable `scissor_test
2927 if state.x != 0
2928 then (
2929 let x = -.float state.x in
2930 GlMat.translate ~x ();
2932 showrects ();
2933 showsel margin;
2934 GlDraw.viewport 0 0 conf.winw conf.winh;
2935 winmatrix ();
2936 scrollindicator ();
2937 begin match state.mode with
2938 | Items items -> showitems items
2939 | Outline outline -> showoutline outline
2940 | _ -> ()
2941 end;
2942 enttext ();
2943 Glut.swapBuffers ();
2946 let getunder x y =
2947 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2948 let x = x - margin - state.x in
2949 let rec f = function
2950 | l :: rest ->
2951 begin match getopaque l.pageno with
2952 | Some (opaque, _) when validopaque opaque ->
2953 let y = y - l.pagedispy in
2954 if y > 0
2955 then
2956 let y = l.pagey + y in
2957 let x = x - l.pagex in
2958 match whatsunder opaque x y with
2959 | Unone -> f rest
2960 | under -> under
2961 else
2962 f rest
2963 | _ ->
2964 f rest
2966 | [] -> Unone
2968 f state.layout
2971 let viewmouse button bstate x y =
2972 match button with
2973 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2974 if Glut.getModifiers () land Glut.active_ctrl != 0
2975 then (
2976 match state.mstate with
2977 | Mzoom (oldn, i) ->
2978 if oldn = n
2979 then (
2980 if i = 2
2981 then
2982 let incr =
2983 match n with
2984 | 4 ->
2985 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2986 | _ ->
2987 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2989 let zoom = conf.zoom +. incr in
2990 setzoom zoom;
2991 state.mstate <- Mzoom (n, 0);
2992 else
2993 state.mstate <- Mzoom (n, i+1);
2995 else state.mstate <- Mzoom (n, 0)
2997 | _ -> state.mstate <- Mzoom (n, 0)
2999 else (
3000 match state.autoscroll with
3001 | Some step -> setautoscrollspeed step (n=4)
3002 | None ->
3003 let incr =
3004 if n = 3
3005 then -conf.scrollstep
3006 else conf.scrollstep
3008 let incr = incr * 2 in
3009 let y = clamp incr in
3010 gotoy_and_clear_text y
3013 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3014 if bstate = Glut.DOWN
3015 then (
3016 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3017 state.mstate <- Mpan (x, y)
3019 else
3020 state.mstate <- Mnone
3022 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3023 if bstate = Glut.DOWN
3024 then
3025 let position, sh = scrollph state.y in
3026 if y > truncate position && y < truncate (position +. sh)
3027 then
3028 state.mstate <- Mscroll
3029 else
3030 let percent = float y /. float conf.winh in
3031 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3032 gotoy desty;
3033 state.mstate <- Mscroll
3034 else
3035 state.mstate <- Mnone
3037 | Glut.LEFT_BUTTON ->
3038 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3039 begin match dest with
3040 | Ulinkgoto (pageno, top) ->
3041 if pageno >= 0
3042 then (
3043 addnav ();
3044 gotopage1 pageno top;
3047 | Ulinkuri s ->
3048 print_endline s
3050 | Unone when bstate = Glut.DOWN ->
3051 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3052 state.mstate <- Mpan (x, y);
3054 | Unone | Utext _ ->
3055 if bstate = Glut.DOWN
3056 then (
3057 if conf.angle mod 360 = 0
3058 then (
3059 state.mstate <- Msel ((x, y), (x, y));
3060 Glut.postRedisplay ()
3063 else (
3064 match state.mstate with
3065 | Mnone -> ()
3067 | Mzoom _ | Mscroll ->
3068 state.mstate <- Mnone
3070 | Mpan _ ->
3071 Glut.setCursor Glut.CURSOR_INHERIT;
3072 state.mstate <- Mnone
3074 | Msel ((_, y0), (_, y1)) ->
3075 let f l =
3076 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3077 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3078 then
3079 match getopaque l.pageno with
3080 | Some (opaque, _) when validopaque opaque ->
3081 copysel opaque
3082 | _ -> ()
3084 List.iter f state.layout;
3085 copysel ""; (* ugly *)
3086 Glut.setCursor Glut.CURSOR_INHERIT;
3087 state.mstate <- Mnone;
3091 | _ -> ()
3094 let birdseyemouse button bstate x y
3095 (conf, leftx, _, hooverpageno, anchor) =
3096 match button with
3097 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3098 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3099 let rec loop = function
3100 | [] -> ()
3101 | l :: rest ->
3102 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3103 && x > margin && x < margin + l.pagew
3104 then (
3105 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3107 else loop rest
3109 loop state.layout
3110 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3111 | _ -> ()
3114 let mouse bstate button x y =
3115 match state.mode with
3116 | View -> viewmouse button bstate x y
3117 | Birdseye beye -> birdseyemouse button bstate x y beye
3118 | Textentry _ | Outline _ | Items _ -> ()
3121 let mouse ~button ~state ~x ~y = mouse state button x y;;
3123 let motion ~x ~y =
3124 match state.mode with
3125 | Outline _ -> ()
3126 | _ ->
3127 match state.mstate with
3128 | Mzoom _ | Mnone -> ()
3130 | Mpan (x0, y0) ->
3131 let dx = x - x0
3132 and dy = y0 - y in
3133 state.mstate <- Mpan (x, y);
3134 if conf.zoom > 1.0 then state.x <- state.x + dx;
3135 let y = clamp dy in
3136 gotoy_and_clear_text y
3138 | Msel (a, _) ->
3139 state.mstate <- Msel (a, (x, y));
3140 Glut.postRedisplay ()
3142 | Mscroll ->
3143 let y = min conf.winh (max 0 y) in
3144 let percent = float y /. float conf.winh in
3145 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3146 gotoy_and_clear_text y
3149 let pmotion ~x ~y =
3150 match state.mode with
3151 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3152 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3153 let rec loop = function
3154 | [] ->
3155 if hooverpageno != -1
3156 then (
3157 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3158 Glut.postRedisplay ();
3160 | l :: rest ->
3161 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3162 && x > margin && x < margin + l.pagew
3163 then (
3164 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3165 Glut.postRedisplay ();
3167 else loop rest
3169 loop state.layout
3171 | Outline _ | Items _ | Textentry _ -> ()
3172 | View ->
3173 match state.mstate with
3174 | Mnone ->
3175 begin match getunder x y with
3176 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3177 | Ulinkuri uri ->
3178 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3179 Glut.setCursor Glut.CURSOR_INFO
3180 | Ulinkgoto (page, _) ->
3181 if conf.underinfo
3182 then showtext 'p' ("age: " ^ string_of_int page);
3183 Glut.setCursor Glut.CURSOR_INFO
3184 | Utext s ->
3185 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3186 Glut.setCursor Glut.CURSOR_TEXT
3189 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3193 module State =
3194 struct
3195 open Parser
3197 let fontpath = ref "";;
3199 let home =
3201 match Sys.os_type with
3202 | "Win32" -> Sys.getenv "HOMEPATH"
3203 | _ -> Sys.getenv "HOME"
3204 with exn ->
3205 prerr_endline
3206 ("Can not determine home directory location: " ^
3207 Printexc.to_string exn);
3211 let config_of c attrs =
3212 let apply c k v =
3214 match k with
3215 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3216 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3217 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3218 | "preload" -> { c with preload = bool_of_string v }
3219 | "page-bias" -> { c with pagebias = int_of_string v }
3220 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3221 | "auto-scroll-step" ->
3222 { c with autoscrollstep = max 0 (int_of_string v) }
3223 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3224 | "crop-hack" -> { c with crophack = bool_of_string v }
3225 | "throttle" -> { c with showall = bool_of_string v }
3226 | "highlight-links" -> { c with hlinks = bool_of_string v }
3227 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3228 | "vertical-margin" ->
3229 { c with interpagespace = max 0 (int_of_string v) }
3230 | "zoom" ->
3231 let zoom = float_of_string v /. 100. in
3232 let zoom = max 0.01 zoom in
3233 { c with zoom = zoom }
3234 | "presentation" -> { c with presentation = bool_of_string v }
3235 | "rotation-angle" -> { c with angle = int_of_string v }
3236 | "width" -> { c with winw = max 20 (int_of_string v) }
3237 | "height" -> { c with winh = max 20 (int_of_string v) }
3238 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3239 | "proportional-display" -> { c with proportional = bool_of_string v }
3240 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3241 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3242 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3243 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3244 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3245 | "persistent-location" -> { c with jumpback = bool_of_string v }
3246 | "background-color" -> { c with bgcolor = color_of_string v }
3247 | "scrollbar-in-presentation" ->
3248 { c with scrollbarinpm = bool_of_string v }
3249 | _ -> c
3250 with exn ->
3251 prerr_endline ("Error processing attribute (`" ^
3252 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3255 let rec fold c = function
3256 | [] -> c
3257 | (k, v) :: rest ->
3258 let c = apply c k v in
3259 fold c rest
3261 fold c attrs;
3264 let fromstring f pos n v d =
3265 try f v
3266 with exn ->
3267 dolog "Error processing attribute (%S=%S) at %d\n%s"
3268 n v pos (Printexc.to_string exn)
3273 let bookmark_of attrs =
3274 let rec fold title page rely = function
3275 | ("title", v) :: rest -> fold v page rely rest
3276 | ("page", v) :: rest -> fold title v rely rest
3277 | ("rely", v) :: rest -> fold title page v rest
3278 | _ :: rest -> fold title page rely rest
3279 | [] -> title, page, rely
3281 fold "invalid" "0" "0" attrs
3284 let doc_of attrs =
3285 let rec fold path page rely pan = function
3286 | ("path", v) :: rest -> fold v page rely pan rest
3287 | ("page", v) :: rest -> fold path v rely pan rest
3288 | ("rely", v) :: rest -> fold path page v pan rest
3289 | ("pan", v) :: rest -> fold path page rely v rest
3290 | _ :: rest -> fold path page rely pan rest
3291 | [] -> path, page, rely, pan
3293 fold "" "0" "0" "0" attrs
3296 let setconf dst src =
3297 dst.scrollbw <- src.scrollbw;
3298 dst.scrollh <- src.scrollh;
3299 dst.icase <- src.icase;
3300 dst.preload <- src.preload;
3301 dst.pagebias <- src.pagebias;
3302 dst.verbose <- src.verbose;
3303 dst.scrollstep <- src.scrollstep;
3304 dst.maxhfit <- src.maxhfit;
3305 dst.crophack <- src.crophack;
3306 dst.autoscrollstep <- src.autoscrollstep;
3307 dst.showall <- src.showall;
3308 dst.hlinks <- src.hlinks;
3309 dst.underinfo <- src.underinfo;
3310 dst.interpagespace <- src.interpagespace;
3311 dst.zoom <- src.zoom;
3312 dst.presentation <- src.presentation;
3313 dst.angle <- src.angle;
3314 dst.winw <- src.winw;
3315 dst.winh <- src.winh;
3316 dst.savebmarks <- src.savebmarks;
3317 dst.memlimit <- src.memlimit;
3318 dst.proportional <- src.proportional;
3319 dst.texcount <- src.texcount;
3320 dst.sliceheight <- src.sliceheight;
3321 dst.blockwidth <- src.blockwidth;
3322 dst.thumbw <- src.thumbw;
3323 dst.jumpback <- src.jumpback;
3324 dst.bgcolor <- src.bgcolor;
3325 dst.scrollbarinpm <- src.scrollbarinpm;
3328 let unent s =
3329 let l = String.length s in
3330 let b = Buffer.create l in
3331 unent b s 0 l;
3332 Buffer.contents b;
3335 let get s =
3336 let h = Hashtbl.create 10 in
3337 let dc = { defconf with angle = defconf.angle } in
3338 let rec toplevel v t spos _ =
3339 match t with
3340 | Vdata | Vcdata | Vend -> v
3341 | Vopen ("llppconfig", _, closed) ->
3342 if closed
3343 then v
3344 else { v with f = llppconfig }
3345 | Vopen _ ->
3346 error "unexpected subelement at top level" s spos
3347 | Vclose _ -> error "unexpected close at top level" s spos
3349 and llppconfig v t spos _ =
3350 match t with
3351 | Vdata | Vcdata -> v
3352 | Vend -> error "unexpected end of input in llppconfig" s spos
3353 | Vopen ("defaults", attrs, closed) ->
3354 let c = config_of dc attrs in
3355 setconf dc c;
3356 if closed
3357 then v
3358 else { v with f = skip "defaults" (fun () -> v) }
3360 | Vopen ("ui-font", _, closed) ->
3361 if closed
3362 then v
3363 else { v with f = uifont (Buffer.create 10) }
3365 | Vopen ("doc", attrs, closed) ->
3366 let pathent, spage, srely, span = doc_of attrs in
3367 let path = unent pathent
3368 and pageno = fromstring int_of_string spos "page" spage 0
3369 and rely = fromstring float_of_string spos "rely" srely 0.0
3370 and pan = fromstring int_of_string spos "pan" span 0 in
3371 let c = config_of dc attrs in
3372 let anchor = (pageno, rely) in
3373 if closed
3374 then (Hashtbl.add h path (c, [], pan, anchor); v)
3375 else { v with f = doc path pan anchor c [] }
3377 | Vopen _ ->
3378 error "unexpected subelement in llppconfig" s spos
3380 | Vclose "llppconfig" -> { v with f = toplevel }
3381 | Vclose _ -> error "unexpected close in llppconfig" s spos
3383 and uifont b v t spos epos =
3384 match t with
3385 | Vdata | Vcdata ->
3386 Buffer.add_substring b s spos (epos - spos);
3388 | Vopen (_, _, _) ->
3389 error "unexpected subelement in ui-font" s spos
3390 | Vclose "ui-font" ->
3391 if String.length !fontpath = 0
3392 then fontpath := Buffer.contents b;
3393 { v with f = llppconfig }
3394 | Vclose _ -> error "unexpected close in ui-font" s spos
3395 | Vend -> error "unexpected end of input in ui-font" s spos
3397 and doc path pan anchor c bookmarks v t spos _ =
3398 match t with
3399 | Vdata | Vcdata -> v
3400 | Vend -> error "unexpected end of input in doc" s spos
3401 | Vopen ("bookmarks", _, closed) ->
3402 if closed
3403 then v
3404 else { v with f = pbookmarks path pan anchor c bookmarks }
3406 | Vopen (_, _, _) ->
3407 error "unexpected subelement in doc" s spos
3409 | Vclose "doc" ->
3410 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3411 { v with f = llppconfig }
3413 | Vclose _ -> error "unexpected close in doc" s spos
3415 and pbookmarks path pan anchor c bookmarks v t spos _ =
3416 match t with
3417 | Vdata | Vcdata -> v
3418 | Vend -> error "unexpected end of input in bookmarks" s spos
3419 | Vopen ("item", attrs, closed) ->
3420 let titleent, spage, srely = bookmark_of attrs in
3421 let page = fromstring int_of_string spos "page" spage 0
3422 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3423 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3424 if closed
3425 then { v with f = pbookmarks path pan anchor c bookmarks }
3426 else
3427 let f () = v in
3428 { v with f = skip "item" f }
3430 | Vopen _ ->
3431 error "unexpected subelement in bookmarks" s spos
3433 | Vclose "bookmarks" ->
3434 { v with f = doc path pan anchor c bookmarks }
3436 | Vclose _ -> error "unexpected close in bookmarks" s spos
3438 and skip tag f v t spos _ =
3439 match t with
3440 | Vdata | Vcdata -> v
3441 | Vend ->
3442 error ("unexpected end of input in skipped " ^ tag) s spos
3443 | Vopen (tag', _, closed) ->
3444 if closed
3445 then v
3446 else
3447 let f' () = { v with f = skip tag f } in
3448 { v with f = skip tag' f' }
3449 | Vclose ctag ->
3450 if tag = ctag
3451 then f ()
3452 else error ("unexpected close in skipped " ^ tag) s spos
3455 parse { f = toplevel; accu = () } s;
3456 h, dc;
3459 let do_load f ic =
3461 let len = in_channel_length ic in
3462 let s = String.create len in
3463 really_input ic s 0 len;
3464 f s;
3465 with
3466 | Parse_error (msg, s, pos) ->
3467 let subs = subs s pos in
3468 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3469 failwith ("parse error: " ^ s)
3471 | exn ->
3472 failwith ("config load error: " ^ Printexc.to_string exn)
3475 let defconfpath =
3476 let dir =
3478 let dir = Filename.concat home ".config" in
3479 if Sys.is_directory dir then dir else home
3480 with _ -> home
3482 Filename.concat dir "llpp.conf"
3485 let confpath = ref defconfpath;;
3487 let load1 f =
3488 if Sys.file_exists !confpath
3489 then
3490 match
3491 (try Some (open_in_bin !confpath)
3492 with exn ->
3493 prerr_endline
3494 ("Error opening configuation file `" ^ !confpath ^ "': " ^
3495 Printexc.to_string exn);
3496 None
3498 with
3499 | Some ic ->
3500 begin try
3501 f (do_load get ic)
3502 with exn ->
3503 prerr_endline
3504 ("Error loading configuation from `" ^ !confpath ^ "': " ^
3505 Printexc.to_string exn);
3506 end;
3507 close_in ic;
3509 | None -> ()
3510 else
3511 f (Hashtbl.create 0, defconf)
3514 let load () =
3515 let f (h, dc) =
3516 let pc, pb, px, pa =
3518 Hashtbl.find h (Filename.basename state.path)
3519 with Not_found -> dc, [], 0, (0, 0.0)
3521 setconf defconf dc;
3522 setconf conf pc;
3523 state.bookmarks <- pb;
3524 state.x <- px;
3525 state.scrollw <- conf.scrollbw;
3526 if conf.jumpback
3527 then state.anchor <- pa;
3528 cbput state.hists.nav pa;
3530 load1 f
3533 let add_attrs bb always dc c =
3534 let ob s a b =
3535 if always || a != b
3536 then Printf.bprintf bb "\n %s='%b'" s a
3537 and oi s a b =
3538 if always || a != b
3539 then Printf.bprintf bb "\n %s='%d'" s a
3540 and oz s a b =
3541 if always || a <> b
3542 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
3543 and oc s a b =
3544 if always || a <> b
3545 then
3546 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3548 let w, h =
3549 if always
3550 then dc.winw, dc.winh
3551 else
3552 match state.fullscreen with
3553 | Some wh -> wh
3554 | None -> c.winw, c.winh
3556 let zoom, presentation, interpagespace, showall=
3557 if always
3558 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3559 else
3560 match state.mode with
3561 | Birdseye (bc, _, _, _, _) ->
3562 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3563 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3565 oi "width" w dc.winw;
3566 oi "height" h dc.winh;
3567 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3568 oi "scroll-handle-height" c.scrollh dc.scrollh;
3569 ob "case-insensitive-search" c.icase dc.icase;
3570 ob "preload" c.preload dc.preload;
3571 oi "page-bias" c.pagebias dc.pagebias;
3572 oi "scroll-step" c.scrollstep dc.scrollstep;
3573 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3574 ob "max-height-fit" c.maxhfit dc.maxhfit;
3575 ob "crop-hack" c.crophack dc.crophack;
3576 ob "throttle" showall dc.showall;
3577 ob "highlight-links" c.hlinks dc.hlinks;
3578 ob "under-cursor-info" c.underinfo dc.underinfo;
3579 oi "vertical-margin" interpagespace dc.interpagespace;
3580 oz "zoom" zoom dc.zoom;
3581 ob "presentation" presentation dc.presentation;
3582 oi "rotation-angle" c.angle dc.angle;
3583 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3584 ob "proportional-display" c.proportional dc.proportional;
3585 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3586 oi "tex-count" c.texcount dc.texcount;
3587 oi "slice-height" c.sliceheight dc.sliceheight;
3588 oi "block-width" c.blockwidth dc.blockwidth;
3589 oi "thumbnail-width" c.thumbw dc.thumbw;
3590 ob "persistent-location" c.jumpback dc.jumpback;
3591 oc "background-color" c.bgcolor dc.bgcolor;
3592 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3595 let save () =
3596 let bb = Buffer.create 32768 in
3597 let f (h, dc) =
3598 let dc = if conf.bedefault then conf else dc in
3599 Buffer.add_string bb "<llppconfig>\n";
3601 if String.length !fontpath > 0
3602 then Printf.bprintf bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3604 Buffer.add_string bb "<defaults ";
3605 add_attrs bb true dc dc;
3606 Buffer.add_string bb "/>\n";
3608 let adddoc path pan anchor c bookmarks =
3609 if bookmarks == [] && c = dc && anchor = emptyanchor
3610 then ()
3611 else (
3612 Printf.bprintf bb "<doc path='%s'"
3613 (enent path 0 (String.length path));
3615 if anchor <> emptyanchor
3616 then (
3617 let n, y = anchor in
3618 Printf.bprintf bb " page='%d'" n;
3619 if y > 1e-6
3620 then
3621 Printf.bprintf bb " rely='%f'" y
3625 if pan != 0
3626 then Printf.bprintf bb " pan='%d'" pan;
3628 add_attrs bb false dc c;
3630 begin match bookmarks with
3631 | [] -> Buffer.add_string bb "/>\n"
3632 | _ ->
3633 Buffer.add_string bb ">\n<bookmarks>\n";
3634 List.iter (fun (title, _level, (page, rely)) ->
3635 Printf.bprintf bb
3636 "<item title='%s' page='%d'"
3637 (enent title 0 (String.length title))
3638 page
3640 if rely > 1e-6
3641 then
3642 Printf.bprintf bb " rely='%f'" rely
3644 Buffer.add_string bb "/>\n";
3645 ) bookmarks;
3646 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3647 end;
3651 let pan =
3652 match state.mode with
3653 | Birdseye (_, pan, _, _, _) -> pan
3654 | _ -> state.x
3656 let basename = Filename.basename state.path in
3657 adddoc basename pan (getanchor ())
3658 { conf with
3659 autoscrollstep =
3660 match state.autoscroll with
3661 | Some step -> step
3662 | None -> conf.autoscrollstep }
3663 (if conf.savebmarks then state.bookmarks else []);
3665 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3666 if basename <> path
3667 then adddoc path x y c bookmarks
3668 ) h;
3669 Buffer.add_string bb "</llppconfig>";
3671 load1 f;
3672 if Buffer.length bb > 0
3673 then
3675 let tmp = !confpath ^ ".tmp" in
3676 let oc = open_out_bin tmp in
3677 Buffer.output_buffer oc bb;
3678 close_out oc;
3679 Sys.rename tmp !confpath;
3680 with exn ->
3681 prerr_endline
3682 ("error while saving configuration: " ^ Printexc.to_string exn)
3684 end;;
3686 let () =
3687 Arg.parse
3688 (Arg.align
3689 [("-p", Arg.String (fun s -> state.password <- s) ,
3690 "<password> Set password");
3692 ("-f", Arg.String (fun s -> State.fontpath := s),
3693 "<path> Set path to the user interface font");
3695 ("-c", Arg.String (fun s -> State.confpath := s),
3696 "<path> Set path to the configuration file");
3698 ("-v", Arg.Unit (fun () ->
3699 Printf.printf
3700 "%s\nconfiguration path: %s\n"
3701 Help.version
3702 State.defconfpath
3704 exit 0), " Print version and exit");
3707 (fun s -> state.path <- s)
3708 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3710 if String.length state.path = 0
3711 then (prerr_endline "file name missing"; exit 1);
3713 State.load ();
3715 let _ = Glut.init Sys.argv in
3716 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3717 let () = Glut.initWindowSize conf.winw conf.winh in
3718 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3720 let csock, ssock =
3721 if Sys.os_type = "Unix"
3722 then
3723 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3724 else
3725 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3726 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3727 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3728 Unix.bind sock addr;
3729 Unix.listen sock 1;
3730 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3731 Unix.connect csock addr;
3732 let ssock, _ = Unix.accept sock in
3733 Unix.close sock;
3734 let opts sock =
3735 Unix.setsockopt sock Unix.TCP_NODELAY true;
3736 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3738 opts ssock;
3739 opts csock;
3740 ssock, csock
3743 let () = Glut.displayFunc display in
3744 let () = Glut.reshapeFunc reshape in
3745 let () = Glut.keyboardFunc keyboard in
3746 let () = Glut.specialFunc special in
3747 let () = Glut.idleFunc (Some idle) in
3748 let () = Glut.mouseFunc mouse in
3749 let () = Glut.motionFunc motion in
3750 let () = Glut.passiveMotionFunc pmotion in
3752 init ssock (
3753 conf.angle, conf.proportional, conf.texcount,
3754 conf.sliceheight, conf.blockwidth, !State.fontpath
3756 state.csock <- csock;
3757 state.ssock <- ssock;
3758 state.text <- "Opening " ^ state.path;
3759 writeopen state.path state.password;
3761 while true do
3763 Glut.mainLoop ();
3764 with
3765 | Glut.BadEnum "key in special_of_int" ->
3766 showtext '!' " LablGlut bug: special key not recognized";
3768 | Quit ->
3769 if Sys.os_type <> "Unix"
3770 then Unix.shutdown ssock Unix.SHUTDOWN_ALL;
3771 State.save ();
3772 exit 0
3773 done;