Run git describe in the source directory
[llpp.git] / main.ml
blobad9c299a3b17901597e9620e7c36c228d046e8b6
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 cb = { state.pagecache with len = state.pagecache.len } in
602 let rec simgc memleft =
603 if cbempty cb
604 then false
605 else
606 let opaque = cbpeek cb in
607 match findpageforopaque opaque with
608 | Some ((n, _, _, _, _), size) ->
609 let memleft = memleft + size in
610 if memleft >= 0
611 then not (pagevisible state.layout n)
612 else (cbdecr cb; simgc memleft)
613 | None -> false
615 simgc memleft
616 else true
617 else false
619 if oktopreload
620 then
621 let presentation = conf.presentation in
622 let interpagespace = conf.interpagespace in
623 let maxy = state.maxy in
624 conf.presentation <- false;
625 conf.interpagespace <- 0;
626 state.maxy <- calcheight ();
627 let y =
628 match state.layout with
629 | [] -> 0
630 | l :: _ -> getpagey l.pageno + l.pagey
632 let y = if y < conf.winh then 0 else y - conf.winh in
633 let h = state.y - y + conf.winh*3 in
634 let pages = layout y h in
635 List.iter render pages;
636 conf.presentation <- presentation;
637 conf.interpagespace <- interpagespace;
638 state.maxy <- maxy;
641 let gotoy y =
642 let y = max 0 y in
643 let y = min state.maxy y in
644 let pages = layout y conf.winh in
645 let ready = loadlayout pages in
646 if conf.showall
647 then (
648 if ready
649 then (
650 state.y <- y;
651 state.layout <- pages;
652 state.throttle <- None;
653 Glut.postRedisplay ();
655 else (
656 state.throttle <- Some pages;
659 else (
660 state.y <- y;
661 state.layout <- pages;
662 state.throttle <- None;
663 Glut.postRedisplay ();
665 begin match state.mode with
666 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
667 if not (pagevisible pages pageno)
668 then (
669 match state.layout with
670 | [] -> ()
671 | l :: _ ->
672 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
674 | _ -> ()
675 end;
676 preload ();
679 let gotoy_and_clear_text y =
680 gotoy y;
681 if not conf.verbose then state.text <- "";
684 let getanchor () =
685 match state.layout with
686 | [] -> emptyanchor
687 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
690 let getanchory (n, top) =
691 let y, h = getpageyh n in
692 y + (truncate (top *. float h));
695 let gotoanchor anchor =
696 gotoy (getanchory anchor);
699 let addnav () =
700 cbput state.hists.nav (getanchor ());
703 let getnav dir =
704 let anchor = cbgetc state.hists.nav dir in
705 getanchory anchor;
708 let gotopage n top =
709 let y, h = getpageyh n in
710 gotoy_and_clear_text (y + (truncate (top *. float h)));
713 let gotopage1 n top =
714 let y = getpagey n in
715 gotoy_and_clear_text (y + top);
718 let invalidate () =
719 state.layout <- [];
720 state.pdims <- [];
721 state.rects <- [];
722 state.rects1 <- [];
723 state.invalidated <- state.invalidated + 1;
726 let scalecolor c =
727 let c = c *. state.colorscale in
728 (c, c, c);
731 let scalecolor2 (r, g, b) =
732 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
735 let represent () =
736 state.maxy <- calcheight ();
737 match state.mode with
738 | Birdseye (_, _, pageno, _, _) ->
739 let y, h = getpageyh pageno in
740 let top = (conf.winh - h) / 2 in
741 gotoy (max 0 (y - top))
742 | _ -> gotoanchor state.anchor
745 let pagematrix () =
746 GlMat.mode `projection;
747 GlMat.load_identity ();
748 GlMat.rotate ~x:1.0 ~angle:180.0 ();
749 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
750 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
751 if state.x != 0
752 then (
753 GlMat.translate ~x:(float state.x) ();
757 let winmatrix () =
758 GlMat.mode `projection;
759 GlMat.load_identity ();
760 GlMat.rotate ~x:1.0 ~angle:180.0 ();
761 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
762 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
765 let reshape =
766 let firsttime = ref true in
767 fun ~w ~h ->
768 if state.invalidated = 0 && not !firsttime
769 then state.anchor <- getanchor ();
771 firsttime := false;
772 conf.winw <- w;
773 let w = truncate (float w *. conf.zoom) - state.scrollw in
774 let w = max w 2 in
775 state.w <- w;
776 conf.winh <- h;
777 GlMat.mode `modelview;
778 GlMat.load_identity ();
779 GlClear.color (scalecolor 1.0);
780 GlClear.clear [`color];
782 invalidate ();
783 wcmd "geometry" [`i w; `i h];
786 let drawstring size x y s =
787 Gl.enable `blend;
788 Gl.enable `texture_2d;
789 ignore (drawstr size x y s);
790 Gl.disable `blend;
791 Gl.disable `texture_2d;
794 let drawstring1 size x y s =
795 drawstr size x y s;
798 let enttext () =
799 let len = String.length state.text in
800 let drawstring s =
801 GlDraw.color (0.0, 0.0, 0.0);
802 GlDraw.rect
803 (0.0, float (conf.winh - 18))
804 (float (conf.winw - state.scrollw - 1), float conf.winh)
806 GlDraw.color (1.0, 1.0, 1.0);
807 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
809 match state.mode with
810 | Textentry ((prefix, text, _, _, _), _) ->
811 let s =
812 if len > 0
813 then
814 Printf.sprintf "%s%s_ [%s]" prefix text state.text
815 else
816 Printf.sprintf "%s%s_" prefix text
818 drawstring s
820 | _ ->
821 if len > 0 then drawstring state.text
824 let showtext c s =
825 state.text <- Printf.sprintf "%c%s" c s;
826 Glut.postRedisplay ();
829 let act cmd =
830 match cmd.[0] with
831 | 'c' ->
832 state.pdims <- [];
834 | 'D' ->
835 state.rects <- state.rects1;
836 Glut.postRedisplay ()
838 | 'C' ->
839 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
840 state.pagecount <- n;
841 state.invalidated <- state.invalidated - 1;
842 if state.invalidated = 0
843 then represent ()
845 | 't' ->
846 let s = Scanf.sscanf cmd "t %n"
847 (fun n -> String.sub cmd n (String.length cmd - n))
849 Glut.setWindowTitle s
851 | 'T' ->
852 let s = Scanf.sscanf cmd "T %n"
853 (fun n -> String.sub cmd n (String.length cmd - n))
855 if istextentry state.mode
856 then (
857 state.text <- s;
858 showtext ' ' s;
860 else (
861 state.text <- s;
862 Glut.postRedisplay ();
865 | 'V' ->
866 if conf.verbose
867 then
868 let s = Scanf.sscanf cmd "V %n"
869 (fun n -> String.sub cmd n (String.length cmd - n))
871 state.text <- s;
872 showtext ' ' s;
874 | 'F' ->
875 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
876 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
877 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
878 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
880 let y = (getpagey pageno) + truncate y0 in
881 addnav ();
882 gotoy y;
883 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
885 | 'R' ->
886 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
887 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
888 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
889 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
891 state.rects1 <-
892 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
894 | 'r' ->
895 let n, w, _h, r, l, s, p =
896 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
897 (fun n w h r l s p ->
898 (n-1, w, h, r, l != 0, s, p))
901 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
902 state.memused <- state.memused + s;
904 let layout =
905 match state.throttle with
906 | None -> state.layout
907 | Some layout -> layout
910 let rec gc () =
911 if (state.memused <= conf.memlimit) || cbempty state.pagecache
912 then ()
913 else (
914 let evictedopaque = cbpeek state.pagecache in
915 match findpageforopaque evictedopaque with
916 | None -> failwith "bug in gc"
917 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
918 if state.gen != gen || not (pagevisible layout evictedn)
919 then (
920 wcmd "free" [`s evictedopaque];
921 state.memused <- state.memused - evictedsize;
922 Hashtbl.remove state.pagemap k;
923 cbdecr state.pagecache;
924 gc ();
928 gc ();
930 cbput state.pagecache p;
931 state.rendering <- false;
933 begin match state.throttle with
934 | None ->
935 if pagevisible state.layout n
936 then gotoy state.y
937 else (
938 let allvisible = loadlayout state.layout in
939 if allvisible then preload ();
942 | Some layout ->
943 match layout with
944 | [] -> ()
945 | l :: _ ->
946 let y = getpagey l.pageno + l.pagey in
947 gotoy y
950 | 'l' ->
951 let pdim =
952 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
954 state.pdims <- pdim :: state.pdims
956 | 'o' ->
957 let (l, n, t, h, pos) =
958 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
960 let s = String.sub cmd pos (String.length cmd - pos) in
961 let outline = (s, l, (n, float t /. float h)) in
962 let outlines =
963 match state.outlines with
964 | Olist outlines -> Olist (outline :: outlines)
965 | Oarray _ -> Olist [outline]
966 | Onarrow _ -> Olist [outline]
968 state.outlines <- outlines
971 | 'i' ->
972 if String.length cmd > 1 && cmd.[1] = 'e'
973 then
974 state.docinfo <- List.rev state.docinfo
975 else
976 let s = Scanf.sscanf cmd "i %n"
977 (fun n -> String.sub cmd n (String.length cmd - n))
979 state.docinfo <- (1, s) :: state.docinfo
981 | _ ->
982 dolog "unknown cmd `%S'" cmd
985 let now = Unix.gettimeofday;;
987 let idle () =
988 if state.deadline == nan then state.deadline <- now ();
989 let rec loop delay =
990 let timeout =
991 if delay > 0.0
992 then max 0.0 (state.deadline -. now ())
993 else 0.0
995 let r, _, _ = Unix.select [state.csock] [] [] timeout in
996 state.deadline <- state.deadline +. delay;
997 begin match r with
998 | [] ->
999 begin match state.autoscroll with
1000 | Some step when step != 0 ->
1001 let y = state.y + step in
1002 let y =
1003 if y < 0
1004 then state.maxy
1005 else if y >= state.maxy then 0 else y
1007 gotoy y;
1008 if state.mode = View
1009 then state.text <- "";
1010 | _ -> ()
1011 end;
1013 | _ ->
1014 let cmd = readcmd state.csock in
1015 act cmd;
1016 loop 0.0
1017 end;
1018 in loop 0.007
1021 let onhist cb =
1022 let rc = cb.rc in
1023 let action = function
1024 | HCprev -> cbget cb ~-1
1025 | HCnext -> cbget cb 1
1026 | HCfirst -> cbget cb ~-(cb.rc)
1027 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1028 and cancel () = cb.rc <- rc
1029 in (action, cancel)
1032 let search pattern forward =
1033 if String.length pattern > 0
1034 then
1035 let pn, py =
1036 match state.layout with
1037 | [] -> 0, 0
1038 | l :: _ ->
1039 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1041 let cmd =
1042 let b = makecmd "search"
1043 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1045 Buffer.add_char b ',';
1046 Buffer.add_string b pattern;
1047 Buffer.add_char b '\000';
1048 Buffer.contents b;
1050 writecmd state.csock cmd;
1053 let intentry text key =
1054 let c = Char.unsafe_chr key in
1055 match c with
1056 | '0' .. '9' ->
1057 let s = "x" in s.[0] <- c;
1058 let text = text ^ s in
1059 TEcont text
1061 | _ ->
1062 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1063 TEcont text
1066 let addchar s c =
1067 let b = Buffer.create (String.length s + 1) in
1068 Buffer.add_string b s;
1069 Buffer.add_char b c;
1070 Buffer.contents b;
1073 let textentry text key =
1074 let c = Char.unsafe_chr key in
1075 match c with
1076 | _ when key >= 32 && key < 127 ->
1077 let text = addchar text c in
1078 TEcont text
1080 | _ ->
1081 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1082 TEcont text
1085 let reinit angle proportional =
1086 state.anchor <- getanchor ();
1087 conf.angle <- angle;
1088 conf.proportional <- proportional;
1089 invalidate ();
1090 wcmd "reinit" [`i angle; `b proportional];
1093 let setzoom zoom =
1094 let zoom = max 0.01 zoom in
1095 if zoom <> conf.zoom
1096 then (
1097 if zoom <= 1.0
1098 then state.x <- 0;
1099 conf.zoom <- zoom;
1100 reshape conf.winw conf.winh;
1101 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1105 let enterbirdseye () =
1106 let zoom = float conf.thumbw /. float conf.winw in
1107 let birdseyepageno =
1108 let cy = conf.winh / 2 in
1109 let fold = function
1110 | [] -> 0
1111 | l :: rest ->
1112 let rec fold best = function
1113 | [] -> best.pageno
1114 | l :: rest ->
1115 let d = cy - (l.pagedispy + l.pagevh/2)
1116 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1117 if abs d < abs dbest
1118 then fold l rest
1119 else best.pageno
1120 in fold l rest
1122 fold state.layout
1124 state.mode <- Birdseye (
1125 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1127 conf.zoom <- zoom;
1128 conf.presentation <- false;
1129 conf.interpagespace <- 10;
1130 conf.hlinks <- false;
1131 state.x <- 0;
1132 state.mstate <- Mnone;
1133 conf.showall <- false;
1134 Glut.setCursor Glut.CURSOR_INHERIT;
1135 if conf.verbose
1136 then
1137 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1138 (100.0*.zoom)
1139 else
1140 state.text <- ""
1142 reshape conf.winw conf.winh;
1145 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1146 state.mode <- View;
1147 conf.zoom <- c.zoom;
1148 conf.presentation <- c.presentation;
1149 conf.interpagespace <- c.interpagespace;
1150 conf.showall <- c.showall;
1151 conf.hlinks <- c.hlinks;
1152 state.x <- leftx;
1153 if conf.verbose
1154 then
1155 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1156 (100.0*.conf.zoom)
1158 reshape conf.winw conf.winh;
1159 state.anchor <- if goback then anchor else (pageno, 0.0);
1162 let togglebirdseye () =
1163 match state.mode with
1164 | Birdseye vals -> leavebirdseye vals true
1165 | View | Outline _ -> enterbirdseye ()
1166 | _ -> ()
1169 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1170 let pageno = max 0 (pageno - 1) in
1171 let rec loop = function
1172 | [] -> gotopage1 pageno 0
1173 | l :: _ when l.pageno = pageno ->
1174 if l.pagedispy >= 0 && l.pagey = 0
1175 then Glut.postRedisplay ()
1176 else gotopage1 pageno 0
1177 | _ :: rest -> loop rest
1179 loop state.layout;
1180 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1183 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1184 let pageno = min (state.pagecount - 1) (pageno + 1) in
1185 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1186 let rec loop = function
1187 | [] ->
1188 let y, h = getpageyh pageno in
1189 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1190 gotoy (clamp dy)
1191 | l :: _ when l.pageno = pageno ->
1192 if l.pagevh != l.pageh
1193 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1194 else Glut.postRedisplay ()
1195 | _ :: rest -> loop rest
1197 loop state.layout
1200 let optentry mode _ key =
1201 let btos b = if b then "on" else "off" in
1202 let c = Char.unsafe_chr key in
1203 match c with
1204 | 's' ->
1205 let ondone s =
1206 try conf.scrollstep <- int_of_string s with exc ->
1207 state.text <- Printf.sprintf "bad integer `%s': %s"
1208 s (Printexc.to_string exc)
1210 TEswitch ("scroll step: ", "", None, intentry, ondone)
1212 | 'A' ->
1213 let ondone s =
1215 conf.autoscrollstep <- int_of_string s;
1216 if state.autoscroll <> None
1217 then state.autoscroll <- Some conf.autoscrollstep
1218 with exc ->
1219 state.text <- Printf.sprintf "bad integer `%s': %s"
1220 s (Printexc.to_string exc)
1222 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1224 | 'Z' ->
1225 let ondone s =
1227 let zoom = float (int_of_string s) /. 100.0 in
1228 setzoom zoom
1229 with exc ->
1230 state.text <- Printf.sprintf "bad integer `%s': %s"
1231 s (Printexc.to_string exc)
1233 TEswitch ("zoom: ", "", None, intentry, ondone)
1235 | 't' ->
1236 let ondone s =
1238 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1239 state.text <-
1240 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1241 begin match mode with
1242 | Birdseye beye ->
1243 leavebirdseye beye false;
1244 enterbirdseye ();
1245 | _ -> ();
1247 with exc ->
1248 state.text <- Printf.sprintf "bad integer `%s': %s"
1249 s (Printexc.to_string exc)
1251 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1253 | 'R' ->
1254 let ondone s =
1255 match try
1256 Some (int_of_string s)
1257 with exc ->
1258 state.text <- Printf.sprintf "bad integer `%s': %s"
1259 s (Printexc.to_string exc);
1260 None
1261 with
1262 | Some angle -> reinit angle conf.proportional
1263 | None -> ()
1265 TEswitch ("rotation: ", "", None, intentry, ondone)
1267 | 'i' ->
1268 conf.icase <- not conf.icase;
1269 TEdone ("case insensitive search " ^ (btos conf.icase))
1271 | 'p' ->
1272 conf.preload <- not conf.preload;
1273 gotoy state.y;
1274 TEdone ("preload " ^ (btos conf.preload))
1276 | 'v' ->
1277 conf.verbose <- not conf.verbose;
1278 TEdone ("verbose " ^ (btos conf.verbose))
1280 | 'h' ->
1281 conf.maxhfit <- not conf.maxhfit;
1282 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1283 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1285 | 'c' ->
1286 conf.crophack <- not conf.crophack;
1287 TEdone ("crophack " ^ btos conf.crophack)
1289 | 'a' ->
1290 conf.showall <- not conf.showall;
1291 TEdone ("throttle " ^ btos conf.showall)
1293 | 'f' ->
1294 conf.underinfo <- not conf.underinfo;
1295 TEdone ("underinfo " ^ btos conf.underinfo)
1297 | 'P' ->
1298 conf.savebmarks <- not conf.savebmarks;
1299 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1301 | 'S' ->
1302 let ondone s =
1304 let pageno, py =
1305 match state.layout with
1306 | [] -> 0, 0
1307 | l :: _ ->
1308 l.pageno, l.pagey
1310 conf.interpagespace <- int_of_string s;
1311 state.maxy <- calcheight ();
1312 let y = getpagey pageno in
1313 gotoy (y + py)
1314 with exc ->
1315 state.text <- Printf.sprintf "bad integer `%s': %s"
1316 s (Printexc.to_string exc)
1318 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1320 | 'l' ->
1321 reinit conf.angle (not conf.proportional);
1322 TEdone ("proprortional display " ^ btos conf.proportional)
1324 | _ ->
1325 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1326 TEstop
1329 let maxoutlinerows () = (conf.winh - 31) / 16;;
1331 let enterselector allowdel outlines errmsg msg =
1332 if Array.length outlines = 0
1333 then (
1334 showtext ' ' errmsg;
1336 else (
1337 state.text <- msg;
1338 Glut.setCursor Glut.CURSOR_INHERIT;
1339 let pageno =
1340 match state.layout with
1341 | [] -> -1
1342 | {pageno=pageno} :: _ -> pageno
1344 let active =
1345 let rec loop n =
1346 if n = Array.length outlines
1347 then 0
1348 else
1349 let (_, _, (outlinepageno, _)) = outlines.(n) in
1350 if outlinepageno >= pageno then n else loop (n+1)
1352 loop 0
1354 state.mode <- Outline
1355 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1356 state.mode);
1357 Glut.postRedisplay ();
1361 let enteroutlinemode () =
1362 let outlines, msg =
1363 match state.outlines with
1364 | Oarray a -> a, ""
1365 | Olist l ->
1366 let a = Array.of_list (List.rev l) in
1367 state.outlines <- Oarray a;
1368 a, ""
1369 | Onarrow (pat, a, _) ->
1370 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1372 enterselector false outlines "Document has no outline" msg;
1375 let enterbookmarkmode () =
1376 let bookmarks = Array.of_list state.bookmarks in
1377 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1380 let mode_to_string mode =
1381 let b = Buffer.create 10 in
1382 let rec f = function
1383 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1384 | View -> Buffer.add_string b "View"
1385 | Birdseye _ -> Buffer.add_string b "Birdseye"
1386 | Items _ -> Buffer.add_string b "Items"
1387 | Outline _ -> Buffer.add_string b "Outline"
1389 f mode;
1390 Buffer.contents b;
1393 let color_of_string s =
1394 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1395 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1399 let color_to_string (r, g, b) =
1400 let r = truncate (r *. 256.0)
1401 and g = truncate (g *. 256.0)
1402 and b = truncate (b *. 256.0) in
1403 Printf.sprintf "%d/%d/%d" r g b
1406 let enterinfomode () =
1407 let btos = function true -> "on" | _ -> "off" in
1408 let mode = state.mode in
1409 let rec makeitems () =
1410 let intp name get set =
1411 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1412 fun active first _ pan ->
1413 let ondone s =
1414 let n =
1415 try int_of_string s
1416 with exn ->
1417 state.text <- Printf.sprintf "bad integer `%s': %s"
1418 s (Printexc.to_string exn);
1419 max_int;
1421 if n != max_int then set n;
1423 let te = name ^ ": ", "", None, intentry, ondone in
1424 state.text <- "";
1425 Textentry (
1427 fun _ ->
1428 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1431 and boolp name get set =
1432 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1433 fun active first qsearch pan ->
1434 let v = get () in
1435 set (not v);
1436 Items (active, first, makeitems (), qsearch, pan, mode);
1438 and colorp name get set =
1439 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1440 fun active first _ pan ->
1441 let invalid = (nan, nan, nan) in
1442 let ondone s =
1443 let c =
1444 try color_of_string s
1445 with exn ->
1446 state.text <- Printf.sprintf "bad color `%s': %s"
1447 s (Printexc.to_string exn);
1448 invalid
1450 if c <> invalid
1451 then set c;
1453 let te = name ^ ": ", "", None, textentry, ondone in
1454 state.text <- "";
1455 Textentry (
1457 fun _ ->
1458 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1463 let birdseye = isbirdseye mode in
1464 let items = [
1465 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1467 boolp "presentation"
1468 (fun () -> conf.presentation)
1469 (fun v ->
1470 conf.presentation <- v;
1471 state.anchor <- getanchor ();
1472 represent ());
1474 boolp "ignore case in searches"
1475 (fun () -> conf.icase)
1476 (fun v -> conf.icase <- v);
1478 boolp "preload"
1479 (fun () -> conf.preload)
1480 (fun v -> conf.preload <- v);
1482 boolp "verbose"
1483 (fun () -> conf.verbose)
1484 (fun v -> conf.verbose <- v);
1486 boolp "max fit"
1487 (fun () -> conf.maxhfit)
1488 (fun v -> conf.maxhfit <- v);
1490 boolp "crop hack"
1491 (fun () -> conf.crophack)
1492 (fun v -> conf.crophack <- v);
1494 boolp "throttle"
1495 (fun () -> conf.showall)
1496 (fun v -> conf.showall <- v);
1498 boolp "highlight links"
1499 (fun () -> conf.hlinks)
1500 (fun v -> conf.hlinks <- v);
1502 boolp "under info"
1503 (fun () -> conf.underinfo)
1504 (fun v -> conf.underinfo <- v);
1505 boolp "persistent bookmarks"
1506 (fun () -> conf.savebmarks)
1507 (fun v -> conf.savebmarks <- v);
1509 boolp "proportional display"
1510 (fun () -> conf.proportional)
1511 (fun v -> reinit conf.angle v);
1513 boolp "persistent location"
1514 (fun () -> conf.jumpback)
1515 (fun v -> conf.jumpback <- v);
1517 "", 0, Noaction;
1519 intp "vertical margin"
1520 (fun () -> conf.interpagespace)
1521 (fun n ->
1522 conf.interpagespace <- n;
1523 let pageno, py =
1524 match state.layout with
1525 | [] -> 0, 0
1526 | l :: _ ->
1527 l.pageno, l.pagey
1529 state.maxy <- calcheight ();
1530 let y = getpagey pageno in
1531 gotoy (y + py)
1534 intp "page bias"
1535 (fun () -> conf.pagebias)
1536 (fun v -> conf.pagebias <- v);
1538 intp "scroll step"
1539 (fun () -> conf.scrollstep)
1540 (fun n -> conf.scrollstep <- n);
1542 intp "auto scroll step"
1543 (fun () ->
1544 match state.autoscroll with
1545 | Some step -> step
1546 | _ -> conf.autoscrollstep)
1547 (fun n ->
1548 if state.autoscroll <> None
1549 then state.autoscroll <- Some n;
1550 conf.autoscrollstep <- n);
1552 intp "zoom"
1553 (fun () -> truncate (conf.zoom *. 100.))
1554 (fun v -> setzoom ((float v) /. 100.));
1556 intp "rotation"
1557 (fun () -> conf.angle)
1558 (fun v -> reinit v conf.proportional);
1560 intp "scroll bar width"
1561 (fun () -> state.scrollw)
1562 (fun v ->
1563 state.scrollw <- v;
1564 conf.scrollbw <- v;
1565 reshape conf.winw conf.winh;
1568 intp "scroll handle height"
1569 (fun () -> conf.scrollh)
1570 (fun v -> conf.scrollh <- v;);
1572 intp "thumbnail width"
1573 (fun () -> conf.thumbw)
1574 (fun v ->
1575 conf.thumbw <- min 1920 v;
1576 match mode with
1577 | Birdseye beye ->
1578 leavebirdseye beye false;
1579 enterbirdseye ()
1580 | _ -> ()
1583 colorp "background color"
1584 (fun () -> conf.bgcolor)
1585 (fun v -> conf.bgcolor <- v);
1587 "", 0, Noaction;
1588 "Presentation mode", 0, Noaction;
1590 boolp "scrollbar visible"
1591 (fun () -> conf.scrollbarinpm)
1592 (fun v ->
1593 if v != conf.scrollbarinpm
1594 then (
1595 conf.scrollbarinpm <- v;
1596 if conf.presentation
1597 then (
1598 state.scrollw <- if v then conf.scrollbw else 0;
1599 reshape conf.winw conf.winh;
1604 "", 0, Noaction;
1605 "Pixmap Cache", 0, Noaction;
1607 intp "size (advisory)"
1608 (fun () -> conf.memlimit)
1609 (fun v -> conf.memlimit <- v);
1610 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1614 let tailer =
1615 let trailer =
1616 ("", 0, Noaction)
1617 :: ("Document", 0, Noaction)
1618 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1620 if birdseye
1621 then trailer
1622 else (
1623 ("", 0, Noaction)
1624 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1625 (btos conf.bedefault),
1627 Action (
1628 fun active first qsearch pan ->
1629 conf.bedefault <- not conf.bedefault;
1630 Items (active, first, makeitems (), qsearch, pan, mode);
1632 ) :: trailer
1635 Array.of_list (items @ tailer)
1637 state.text <- "";
1638 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1639 Glut.postRedisplay ();
1642 let enterhelpmode () =
1643 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1644 Glut.postRedisplay ();
1647 let quickbookmark ?title () =
1648 match state.layout with
1649 | [] -> ()
1650 | l :: _ ->
1651 let title =
1652 match title with
1653 | None ->
1654 let sec = Unix.gettimeofday () in
1655 let tm = Unix.localtime sec in
1656 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1657 (l.pageno+1)
1658 tm.Unix.tm_mday
1659 tm.Unix.tm_mon
1660 (tm.Unix.tm_year + 1900)
1661 tm.Unix.tm_hour
1662 tm.Unix.tm_min
1663 | Some title -> title
1665 state.bookmarks <-
1666 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1667 :: state.bookmarks
1670 let doreshape w h =
1671 state.fullscreen <- None;
1672 Glut.reshapeWindow w h;
1675 let writeopen path password =
1676 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1679 let opendoc path password =
1680 invalidate ();
1681 state.path <- path;
1682 state.password <- password;
1683 state.gen <- state.gen + 1;
1684 state.docinfo <- [];
1686 writeopen path password;
1687 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1688 wcmd "geometry" [`i state.w; `i conf.winh];
1691 let viewkeyboard key =
1692 let enttext te =
1693 let mode = state.mode in
1694 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1695 state.text <- "";
1696 enttext ();
1697 Glut.postRedisplay ()
1699 let c = Char.chr key in
1700 match c with
1701 | '\027' | 'q' -> (* escape *)
1702 raise Quit
1704 | '\008' -> (* backspace *)
1705 let y = getnav ~-1 in
1706 gotoy_and_clear_text y
1708 | 'o' ->
1709 enteroutlinemode ()
1711 | 'u' ->
1712 state.rects <- [];
1713 state.text <- "";
1714 Glut.postRedisplay ()
1716 | '/' | '?' ->
1717 let ondone isforw s =
1718 cbput state.hists.pat s;
1719 state.searchpattern <- s;
1720 search s isforw
1722 let s = String.create 1 in
1723 s.[0] <- c;
1724 enttext (s, "", Some (onhist state.hists.pat),
1725 textentry, ondone (c ='/'))
1727 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1728 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1729 setzoom (conf.zoom +. incr)
1731 | '+' ->
1732 let ondone s =
1733 let n =
1734 try int_of_string s with exc ->
1735 state.text <- Printf.sprintf "bad integer `%s': %s"
1736 s (Printexc.to_string exc);
1737 max_int
1739 if n != max_int
1740 then (
1741 conf.pagebias <- n;
1742 state.text <- "page bias is now " ^ string_of_int n;
1745 enttext ("page bias: ", "", None, intentry, ondone)
1747 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1748 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1749 setzoom (max 0.01 (conf.zoom -. decr))
1751 | '-' ->
1752 let ondone msg = state.text <- msg in
1753 enttext (
1754 "option [acfhilpstvAPRSZ]: ", "", None,
1755 optentry state.mode, ondone
1758 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1759 setzoom 1.0
1761 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1762 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1763 if zoom < 1.0
1764 then setzoom zoom
1766 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1767 togglebirdseye ()
1769 | '0' .. '9' ->
1770 let ondone s =
1771 let n =
1772 try int_of_string s with exc ->
1773 state.text <- Printf.sprintf "bad integer `%s': %s"
1774 s (Printexc.to_string exc);
1777 if n >= 0
1778 then (
1779 addnav ();
1780 cbput state.hists.pag (string_of_int n);
1781 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1784 let pageentry text key =
1785 match Char.unsafe_chr key with
1786 | 'g' -> TEdone text
1787 | _ -> intentry text key
1789 let text = "x" in text.[0] <- c;
1790 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1792 | 'b' ->
1793 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1794 reshape conf.winw conf.winh;
1796 | 'l' ->
1797 conf.hlinks <- not conf.hlinks;
1798 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1799 Glut.postRedisplay ()
1801 | 'a' ->
1802 begin match state.autoscroll with
1803 | Some step ->
1804 conf.autoscrollstep <- step;
1805 state.autoscroll <- None
1806 | None ->
1807 if conf.autoscrollstep = 0
1808 then state.autoscroll <- Some 1
1809 else state.autoscroll <- Some conf.autoscrollstep
1812 | 'P' ->
1813 conf.presentation <- not conf.presentation;
1814 if conf.presentation
1815 then (
1816 if not conf.scrollbarinpm
1817 then state.scrollw <- 0;
1819 else
1820 state.scrollw <- conf.scrollbw;
1822 showtext ' ' ("presentation mode " ^
1823 if conf.presentation then "on" else "off");
1824 state.anchor <- getanchor ();
1825 represent ()
1827 | 'f' ->
1828 begin match state.fullscreen with
1829 | None ->
1830 state.fullscreen <- Some (conf.winw, conf.winh);
1831 Glut.fullScreen ()
1832 | Some (w, h) ->
1833 state.fullscreen <- None;
1834 doreshape w h
1837 | 'g' ->
1838 gotoy_and_clear_text 0
1840 | 'G' ->
1841 gotopage1 (state.pagecount - 1) 0
1843 | 'n' ->
1844 search state.searchpattern true
1846 | 'p' | 'N' ->
1847 search state.searchpattern false
1849 | 't' ->
1850 begin match state.layout with
1851 | [] -> ()
1852 | l :: _ ->
1853 gotoy_and_clear_text (getpagey l.pageno)
1856 | ' ' ->
1857 begin match List.rev state.layout with
1858 | [] -> ()
1859 | l :: _ ->
1860 let pageno = min (l.pageno+1) (state.pagecount-1) in
1861 gotoy_and_clear_text (getpagey pageno)
1864 | '\127' -> (* del *)
1865 begin match state.layout with
1866 | [] -> ()
1867 | l :: _ ->
1868 let pageno = max 0 (l.pageno-1) in
1869 gotoy_and_clear_text (getpagey pageno)
1872 | '=' ->
1873 let f (fn, _) l =
1874 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1876 let fn, ln = List.fold_left f (-1, -1) state.layout in
1877 let s =
1878 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1879 let percent =
1880 if maxy <= 0
1881 then 100.
1882 else (100. *. (float state.y /. float maxy)) in
1883 if fn = ln
1884 then
1885 Printf.sprintf "Page %d of %d %.2f%%"
1886 (fn+1) state.pagecount percent
1887 else
1888 Printf.sprintf
1889 "Pages %d-%d of %d %.2f%%"
1890 (fn+1) (ln+1) state.pagecount percent
1892 showtext ' ' s;
1894 | 'w' ->
1895 begin match state.layout with
1896 | [] -> ()
1897 | l :: _ ->
1898 doreshape (l.pagew + state.scrollw) l.pageh;
1899 Glut.postRedisplay ();
1902 | '\'' ->
1903 enterbookmarkmode ()
1905 | 'h' ->
1906 enterhelpmode ()
1908 | 'i' ->
1909 enterinfomode ()
1911 | 'm' ->
1912 let ondone s =
1913 match state.layout with
1914 | l :: _ ->
1915 state.bookmarks <-
1916 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1917 :: state.bookmarks
1918 | _ -> ()
1920 enttext ("bookmark: ", "", None, textentry, ondone)
1922 | '~' ->
1923 quickbookmark ();
1924 showtext ' ' "Quick bookmark added";
1926 | 'z' ->
1927 begin match state.layout with
1928 | l :: _ ->
1929 let rect = getpdimrect l.pagedimno in
1930 let w, h =
1931 if conf.crophack
1932 then
1933 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1934 truncate (1.2 *. (rect.(3) -. rect.(0))))
1935 else
1936 (truncate (rect.(1) -. rect.(0)),
1937 truncate (rect.(3) -. rect.(0)))
1939 if w != 0 && h != 0
1940 then
1941 doreshape (w + state.scrollw) (h + conf.interpagespace)
1943 Glut.postRedisplay ();
1945 | [] -> ()
1948 | '\000' -> (* ctrl-2 *)
1949 let maxw = getmaxw () in
1950 if maxw > 0.0
1951 then setzoom (maxw /. float conf.winw)
1953 | '<' | '>' ->
1954 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1956 | '[' | ']' ->
1957 state.colorscale <-
1958 max 0.0
1959 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1960 Glut.postRedisplay ()
1962 | 'k' ->
1963 begin match state.mode with
1964 | Birdseye beye -> upbirdseye beye
1965 | _ -> gotoy (clamp (-conf.scrollstep))
1968 | 'j' ->
1969 begin match state.mode with
1970 | Birdseye beye -> downbirdseye beye
1971 | _ -> gotoy (clamp conf.scrollstep)
1974 | 'r' ->
1975 state.anchor <- getanchor ();
1976 opendoc state.path state.password
1978 | _ ->
1979 vlog "huh? %d %c" key (Char.chr key);
1982 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1983 let enttext te =
1984 state.mode <- Textentry (te, onleave);
1985 state.text <- "";
1986 enttext ();
1987 Glut.postRedisplay ()
1989 match Char.unsafe_chr key with
1990 | '\008' -> (* backspace *)
1991 let len = String.length text in
1992 if len = 0
1993 then (
1994 onleave Cancel;
1995 Glut.postRedisplay ();
1997 else (
1998 let s = String.sub text 0 (len - 1) in
1999 enttext (c, s, opthist, onkey, ondone)
2002 | '\r' | '\n' ->
2003 ondone text;
2004 onleave Confirm;
2005 Glut.postRedisplay ()
2007 | '\007' (* ctrl-g *)
2008 | '\027' -> (* escape *)
2009 begin match opthist with
2010 | None -> ()
2011 | Some (_, onhistcancel) -> onhistcancel ()
2012 end;
2013 onleave Cancel;
2014 Glut.postRedisplay ()
2016 | _ ->
2017 begin match onkey text key with
2018 | TEdone text ->
2019 onleave Confirm;
2020 ondone text;
2021 Glut.postRedisplay ()
2023 | TEcont text ->
2024 enttext (c, text, opthist, onkey, ondone);
2026 | TEstop ->
2027 onleave Cancel;
2028 Glut.postRedisplay ()
2030 | TEswitch te ->
2031 state.mode <- Textentry (te, onleave);
2032 Glut.postRedisplay ()
2033 end;
2036 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2037 match key with
2038 | 27 -> (* escape *)
2039 leavebirdseye beye true
2041 | 12 -> (* ctrl-l *)
2042 let y, h = getpageyh pageno in
2043 let top = (conf.winh - h) / 2 in
2044 gotoy (max 0 (y - top))
2046 | 13 -> (* enter *)
2047 leavebirdseye beye false
2049 | _ ->
2050 viewkeyboard key
2053 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2054 let set active first qsearch =
2055 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2057 let search active pattern incr =
2058 let dosearch re =
2059 let rec loop n =
2060 if n >= Array.length items || n < 0
2061 then None
2062 else
2063 let (s, _, _) = items.(n) in
2065 (try ignore (Str.search_forward re s 0); true
2066 with Not_found -> false)
2067 then Some n
2068 else loop (n + incr)
2070 loop active
2073 let re = Str.regexp_case_fold pattern in
2074 dosearch re
2075 with Failure s ->
2076 state.text <- s;
2077 None
2079 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2080 match key with
2081 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2082 let incr = if key = 18 then -1 else 1 in
2083 let active, first =
2084 match search (active + incr) qsearch incr with
2085 | None ->
2086 state.text <- qsearch ^ " [not found]";
2087 active, first
2088 | Some active ->
2089 state.text <- qsearch;
2090 active, firstof active
2092 set active first qsearch;
2093 Glut.postRedisplay ();
2095 | 8 -> (* backspace *)
2096 let len = String.length qsearch in
2097 if len = 0
2098 then ()
2099 else (
2100 if len = 1
2101 then (
2102 state.text <- "";
2103 set active first "";
2105 else
2106 let qsearch = String.sub qsearch 0 (len - 1) in
2107 let active, first =
2108 match search active qsearch ~-1 with
2109 | None ->
2110 state.text <- qsearch ^ " [not found]";
2111 active, first
2112 | Some active ->
2113 state.text <- qsearch;
2114 active, firstof active
2116 set active first qsearch
2118 Glut.postRedisplay ()
2120 | _ when key >= 32 && key < 127 ->
2121 let pattern = addchar qsearch (Char.chr key) in
2122 let active, first =
2123 match search active pattern 1 with
2124 | None ->
2125 state.text <- pattern ^ " [not found]";
2126 active, first
2127 | Some active ->
2128 state.text <- pattern;
2129 active, firstof active
2131 set active first pattern;
2132 Glut.postRedisplay ()
2134 | 27 -> (* escape *)
2135 state.text <- "";
2136 if String.length qsearch = 0
2137 then (
2138 state.mode <- oldmode;
2140 else (
2141 set active first "";
2143 Glut.postRedisplay ()
2145 | 13 -> (* enter *)
2146 if active >= 0 && active < Array.length items
2147 then (
2148 match items.(active) with
2149 | _, _, Action f ->
2150 state.mode <- f active first qsearch pan
2152 | _, _, Noaction ->
2153 state.text <- "";
2154 state.mode <- oldmode
2156 else (
2157 state.text <- "";
2158 state.mode <- oldmode
2160 Glut.postRedisplay ();
2162 | _ -> dolog "unknown key %d" key
2165 let outlinekeyboard key
2166 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2167 let narrow outlines pattern =
2168 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2169 match reopt with
2170 | None -> None
2171 | Some re ->
2172 let rec fold accu n =
2173 if n = -1
2174 then accu
2175 else
2176 let (s, _, _) as o = outlines.(n) in
2177 let accu =
2178 if (try ignore (Str.search_forward re s 0); true
2179 with Not_found -> false)
2180 then (o :: accu)
2181 else accu
2183 fold accu (n-1)
2185 let matched = fold [] (Array.length outlines - 1) in
2186 if matched = [] then None else Some (Array.of_list matched)
2188 let search active pattern incr =
2189 let dosearch re =
2190 let rec loop n =
2191 if n = Array.length outlines || n = -1
2192 then None
2193 else
2194 let (s, _, _) = outlines.(n) in
2196 (try ignore (Str.search_forward re s 0); true
2197 with Not_found -> false)
2198 then Some n
2199 else loop (n + incr)
2201 loop active
2204 let re = Str.regexp_case_fold pattern in
2205 dosearch re
2206 with Failure s ->
2207 state.text <- s;
2208 None
2210 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2211 match key with
2212 | 27 -> (* escape *)
2213 state.text <- "";
2214 if String.length qsearch = 0
2215 then (
2216 state.mode <- oldmode;
2218 else (
2219 state.mode <- Outline (
2220 allowdel, active, first, outlines, "", pan, oldmode
2223 Glut.postRedisplay ();
2225 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2226 let incr = if key = 18 then -1 else 1 in
2227 let active, first =
2228 match search (active + incr) qsearch incr with
2229 | None ->
2230 state.text <- qsearch ^ " [not found]";
2231 active, first
2232 | Some active ->
2233 state.text <- qsearch;
2234 active, firstof active
2236 state.mode <- Outline (
2237 allowdel, active, first, outlines, qsearch, pan, oldmode
2239 Glut.postRedisplay ();
2241 | 8 -> (* backspace *)
2242 let len = String.length qsearch in
2243 if len = 0
2244 then ()
2245 else (
2246 if len = 1
2247 then (
2248 state.text <- "";
2249 state.mode <- Outline (
2250 allowdel, active, first, outlines, "", pan, oldmode
2253 else
2254 let qsearch = String.sub qsearch 0 (len - 1) in
2255 let active, first =
2256 match search active qsearch ~-1 with
2257 | None ->
2258 state.text <- qsearch ^ " [not found]";
2259 active, first
2260 | Some active ->
2261 state.text <- qsearch;
2262 active, firstof active
2264 state.mode <- Outline (
2265 allowdel, active, first, outlines, qsearch, pan, oldmode
2268 Glut.postRedisplay ()
2270 | 13 -> (* enter *)
2271 if active < Array.length outlines
2272 then (
2273 let (_, _, anchor) = outlines.(active) in
2274 addnav ();
2275 gotoanchor anchor;
2277 state.text <- "";
2278 if allowdel then state.bookmarks <- Array.to_list outlines;
2279 state.mode <- oldmode;
2280 Glut.postRedisplay ();
2282 | _ when key >= 32 && key < 127 ->
2283 let pattern = addchar qsearch (Char.chr key) in
2284 let active, first =
2285 match search active pattern 1 with
2286 | None ->
2287 state.text <- pattern ^ " [not found]";
2288 active, first
2289 | Some active ->
2290 state.text <- pattern;
2291 active, firstof active
2293 state.mode <- Outline (
2294 allowdel, active, first, outlines, pattern, pan, oldmode
2296 Glut.postRedisplay ()
2298 | 14 when not allowdel -> (* ctrl-n *)
2299 if String.length qsearch > 0
2300 then (
2301 let optoutlines = narrow outlines qsearch in
2302 begin match optoutlines with
2303 | None -> state.text <- "can't narrow"
2304 | Some outlines ->
2305 state.mode <- Outline (
2306 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2308 match state.outlines with
2309 | Olist _ -> ()
2310 | Oarray a ->
2311 state.outlines <- Onarrow (qsearch, outlines, a)
2312 | Onarrow (_, _, b) ->
2313 state.outlines <- Onarrow (qsearch, outlines, b)
2314 end;
2316 Glut.postRedisplay ()
2318 | 21 when not allowdel -> (* ctrl-u *)
2319 let outline =
2320 match state.outlines with
2321 | Oarray a -> a
2322 | Olist l ->
2323 let a = Array.of_list (List.rev l) in
2324 state.outlines <- Oarray a;
2326 | Onarrow (_, _, b) ->
2327 state.outlines <- Oarray b;
2328 state.text <- "";
2331 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2332 Glut.postRedisplay ()
2334 | 12 -> (* ctrl-l *)
2335 state.mode <- Outline
2336 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2337 Glut.postRedisplay ()
2339 | 127 when allowdel -> (* delete *)
2340 let len = Array.length outlines - 1 in
2341 if len = 0
2342 then (
2343 state.mode <- View;
2344 state.bookmarks <- [];
2346 else (
2347 let bookmarks = Array.init len
2348 (fun i ->
2349 let i = if i >= active then i + 1 else i in
2350 outlines.(i)
2353 state.mode <-
2354 Outline (
2355 allowdel,
2356 min active (len-1),
2357 min first (len-1),
2358 bookmarks, qsearch,
2360 oldmode
2363 Glut.postRedisplay ()
2365 | _ -> dolog "unknown key %d" key
2368 let keyboard ~key ~x ~y =
2369 ignore x;
2370 ignore y;
2371 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
2372 then
2373 wcmd "interrupt" []
2374 else
2375 match state.mode with
2376 | Outline outline -> outlinekeyboard key outline
2377 | Textentry textentry -> textentrykeyboard key textentry
2378 | Birdseye birdseye -> birdseyekeyboard key birdseye
2379 | View -> viewkeyboard key
2380 | Items items -> itemskeyboard key items
2383 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2384 match key with
2385 | Glut.KEY_UP -> upbirdseye beye
2386 | Glut.KEY_DOWN -> downbirdseye beye
2388 | Glut.KEY_PAGE_UP ->
2389 begin match state.layout with
2390 | l :: _ ->
2391 if l.pagey != 0
2392 then (
2393 state.mode <- Birdseye (
2394 conf, leftx, l.pageno, hooverpageno, anchor
2396 gotopage1 l.pageno 0;
2398 else (
2399 let layout = layout (state.y-conf.winh) conf.winh in
2400 match layout with
2401 | [] -> gotoy (clamp (-conf.winh))
2402 | l :: _ ->
2403 state.mode <- Birdseye (
2404 conf, leftx, l.pageno, hooverpageno, anchor
2406 gotopage1 l.pageno 0
2409 | [] -> gotoy (clamp (-conf.winh))
2410 end;
2412 | Glut.KEY_PAGE_DOWN ->
2413 begin match List.rev state.layout with
2414 | l :: _ ->
2415 let layout = layout (state.y + conf.winh) conf.winh in
2416 begin match layout with
2417 | [] ->
2418 let incr = l.pageh - l.pagevh in
2419 if incr = 0
2420 then (
2421 state.mode <-
2422 Birdseye (
2423 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2425 Glut.postRedisplay ();
2427 else gotoy (clamp (incr + conf.interpagespace*2));
2429 | l :: _ ->
2430 state.mode <-
2431 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2432 gotopage1 l.pageno 0;
2435 | [] -> gotoy (clamp conf.winh)
2436 end;
2438 | Glut.KEY_HOME ->
2439 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2440 gotopage1 0 0
2442 | Glut.KEY_END ->
2443 let pageno = state.pagecount - 1 in
2444 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2445 if not (pagevisible state.layout pageno)
2446 then
2447 let h =
2448 match List.rev state.pdims with
2449 | [] -> conf.winh
2450 | (_, _, h, _) :: _ -> h
2452 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2453 else Glut.postRedisplay ();
2454 | _ -> ()
2457 let setautoscrollspeed step goingdown =
2458 let incr = max 1 ((abs step) / 2) in
2459 let incr = if goingdown then incr else -incr in
2460 let astep = step + incr in
2461 state.autoscroll <- Some astep;
2464 let special ~key ~x ~y =
2465 ignore x;
2466 ignore y;
2467 match state.mode with
2468 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2469 togglebirdseye ()
2471 | Birdseye vals ->
2472 birdseyespecial key vals
2474 | View when key = Glut.KEY_F1 ->
2475 enterhelpmode ()
2477 | View ->
2478 begin match state.autoscroll with
2479 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2480 setautoscrollspeed step (key = Glut.KEY_DOWN)
2482 | _ ->
2483 let y =
2484 match key with
2485 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2486 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2487 | Glut.KEY_DOWN -> clamp conf.scrollstep
2488 | Glut.KEY_PAGE_UP ->
2489 if Glut.getModifiers () land Glut.active_ctrl != 0
2490 then
2491 match state.layout with
2492 | [] -> state.y
2493 | l :: _ -> state.y - l.pagey
2494 else
2495 clamp (-conf.winh)
2496 | Glut.KEY_PAGE_DOWN ->
2497 if Glut.getModifiers () land Glut.active_ctrl != 0
2498 then
2499 match List.rev state.layout with
2500 | [] -> state.y
2501 | l :: _ -> getpagey l.pageno
2502 else
2503 clamp conf.winh
2504 | Glut.KEY_HOME -> addnav (); 0
2505 | Glut.KEY_END ->
2506 addnav ();
2507 state.maxy - (if conf.maxhfit then conf.winh else 0)
2509 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2510 Glut.getModifiers () land Glut.active_alt != 0 ->
2511 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2513 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2514 state.x <- state.x - 10;
2515 state.y
2516 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2517 state.x <- state.x + 10;
2518 state.y
2520 | _ -> state.y
2522 gotoy_and_clear_text y
2525 | Textentry
2526 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2527 let s =
2528 match key with
2529 | Glut.KEY_UP -> action HCprev
2530 | Glut.KEY_DOWN -> action HCnext
2531 | Glut.KEY_HOME -> action HCfirst
2532 | Glut.KEY_END -> action HClast
2533 | _ -> state.text
2535 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2536 Glut.postRedisplay ()
2538 | Textentry _ -> ()
2540 | Items (active, first, items, qsearch, pan, oldmode) ->
2541 let maxrows = maxoutlinerows () in
2542 let itemcount = Array.length items in
2543 let hasaction = function
2544 | (_, _, Noaction) -> false
2545 | _ -> true
2547 let find start incr =
2548 let rec find i =
2549 if i = -1 || i = itemcount
2550 then -1
2551 else (
2552 if hasaction items.(i)
2553 then i
2554 else find (i + incr)
2557 find start
2559 let set active first =
2560 let first = max 0 (min first (itemcount - maxrows)) in
2561 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2563 let navigate incr =
2564 let isvisible first n = n >= first && n - first <= maxrows in
2565 let active, first =
2566 let incr1 = if incr > 0 then 1 else -1 in
2567 if isvisible first active
2568 then
2569 let next =
2570 let next = active + incr in
2571 let next =
2572 if next < 0 || next >= itemcount
2573 then -1
2574 else find next incr1
2576 if next = -1 || abs (active - next) > maxrows
2577 then -1
2578 else next
2580 if next = -1
2581 then
2582 let first = first + incr in
2583 let first = max 0 (min first (itemcount - 1)) in
2584 let next =
2585 let next = active + incr in
2586 let next = max 0 (min next (itemcount - 1)) in
2587 find next ~-incr1
2589 let active = if next = -1 then active else next in
2590 active, first
2591 else
2592 let first = min next first in
2593 next, first
2594 else
2595 let first = first + incr in
2596 let first = max 0 (min first (itemcount - 1)) in
2597 let active =
2598 let next = active + incr in
2599 let next = max 0 (min next (itemcount - 1)) in
2600 let next = find next incr1 in
2601 if next = -1 || abs (active - first) > maxrows
2602 then active
2603 else next
2605 active, first
2607 set active first;
2608 Glut.postRedisplay ()
2610 begin match key with
2611 | Glut.KEY_UP -> navigate ~-1
2612 | Glut.KEY_DOWN -> navigate 1
2613 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2614 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2616 | Glut.KEY_RIGHT ->
2617 state.mode <- Items (
2618 active, first, items, qsearch, min 0 (pan - 1), oldmode
2620 Glut.postRedisplay ()
2622 | Glut.KEY_LEFT ->
2623 state.mode <- Items (
2624 active, first, items, qsearch, min 0 (pan + 1), oldmode
2626 Glut.postRedisplay ()
2628 | Glut.KEY_HOME ->
2629 let active = find 0 1 in
2630 set active 0;
2631 Glut.postRedisplay ()
2633 | Glut.KEY_END ->
2634 let first = max 0 (itemcount - maxrows) in
2635 let active = find (itemcount - 1) ~-1 in
2636 set active first;
2637 Glut.postRedisplay ()
2639 | _ -> ()
2640 end;
2642 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2643 let maxrows = maxoutlinerows () in
2644 let calcfirst first active =
2645 if active > first
2646 then
2647 let rows = active - first in
2648 if rows > maxrows then active - maxrows else first
2649 else active
2651 let navigate incr =
2652 let active = active + incr in
2653 let active = max 0 (min active (Array.length outlines - 1)) in
2654 let first = calcfirst first active in
2655 state.mode <- Outline (
2656 allowdel, active, first, outlines, qsearch, pan, oldmode
2658 Glut.postRedisplay ()
2660 let updownlevel incr =
2661 let len = Array.length outlines in
2662 let (_, curlevel, _) = outlines.(active) in
2663 let rec flow i =
2664 if i = len then i-1 else if i = -1 then 0 else
2665 let (_, l, _) = outlines.(i) in
2666 if l != curlevel then i else flow (i+incr)
2668 let active = flow active in
2669 let first = calcfirst first active in
2670 state.mode <- Outline (
2671 allowdel, active, first, outlines, qsearch, pan, oldmode
2673 Glut.postRedisplay ()
2675 match key with
2676 | Glut.KEY_UP -> navigate ~-1
2677 | Glut.KEY_DOWN -> navigate 1
2678 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2679 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2681 | Glut.KEY_RIGHT ->
2682 if Glut.getModifiers () land Glut.active_ctrl != 0
2683 then (
2684 state.mode <- Outline (
2685 allowdel, active, first, outlines,
2686 qsearch, min 0 (pan + 1), oldmode
2688 Glut.postRedisplay ();
2690 else (
2691 if not allowdel
2692 then updownlevel 1
2695 | Glut.KEY_LEFT ->
2696 if Glut.getModifiers () land Glut.active_ctrl != 0
2697 then (
2698 state.mode <- Outline (
2699 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2701 Glut.postRedisplay ();
2703 else (
2704 if not allowdel
2705 then updownlevel ~-1
2708 | Glut.KEY_HOME ->
2709 state.mode <- Outline (
2710 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2712 Glut.postRedisplay ()
2714 | Glut.KEY_END ->
2715 let active = Array.length outlines - 1 in
2716 let first = max 0 (active - maxrows) in
2717 state.mode <- Outline (
2718 allowdel, active, first, outlines, qsearch, pan, oldmode
2720 Glut.postRedisplay ()
2722 | _ -> ()
2725 let drawplaceholder l =
2726 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2727 GlDraw.rect
2728 (float l.pagex, float l.pagedispy)
2729 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2731 let x = if margin < 0 then -margin else l.pagex
2732 and y = l.pagedispy + 13 in
2733 GlDraw.color (0.0, 0.0, 0.0);
2734 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2737 let now () = Unix.gettimeofday ();;
2739 let drawpage l =
2740 let color =
2741 match state.mode with
2742 | Textentry _ -> scalecolor 0.4
2743 | View | Outline _ | Items _ -> scalecolor 1.0
2744 | Birdseye (_, _, pageno, hooverpageno, _) ->
2745 if l.pageno = hooverpageno
2746 then scalecolor 0.9
2747 else (
2748 if l.pageno = pageno
2749 then scalecolor 1.0
2750 else scalecolor 0.8
2753 GlDraw.color color;
2754 begin match getopaque l.pageno with
2755 | Some (opaque, _) when validopaque opaque ->
2756 let a = now () in
2757 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2758 let b = now () in
2759 let d = b-.a in
2760 vlog "draw %d %f sec" l.pageno d;
2762 | _ ->
2763 drawplaceholder l;
2764 end;
2767 let scrollph y =
2768 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2769 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2770 let sh = float conf.winh /. sh in
2771 let sh = max sh (float conf.scrollh) in
2773 let percent =
2774 if state.y = state.maxy
2775 then 1.0
2776 else float y /. float maxy
2778 let position = (float conf.winh -. sh) *. percent in
2780 let position =
2781 if position +. sh > float conf.winh
2782 then float conf.winh -. sh
2783 else position
2785 position, sh;
2788 let scrollindicator () =
2789 GlDraw.color (0.64 , 0.64, 0.64);
2790 GlDraw.rect
2791 (float (conf.winw - state.scrollw), 0.)
2792 (float conf.winw, float conf.winh)
2794 GlDraw.color (0.0, 0.0, 0.0);
2796 let position, sh = scrollph state.y in
2797 GlDraw.rect
2798 (float (conf.winw - state.scrollw), position)
2799 (float conf.winw, position +. sh)
2803 let showsel margin =
2804 match state.mstate with
2805 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2808 | Msel ((x0, y0), (x1, y1)) ->
2809 let rec loop = function
2810 | l :: ls ->
2811 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2812 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2813 then
2814 match getopaque l.pageno with
2815 | Some (opaque, _) when validopaque opaque ->
2816 let oy = -l.pagey + l.pagedispy in
2817 seltext opaque
2818 (x0 - margin - state.x, y0,
2819 x1 - margin - state.x, y1) oy;
2821 | _ -> ()
2822 else loop ls
2823 | [] -> ()
2825 loop state.layout
2828 let showrects () =
2829 let panx = float state.x in
2830 Gl.enable `blend;
2831 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2832 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2833 List.iter
2834 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2835 List.iter (fun l ->
2836 if l.pageno = pageno
2837 then (
2838 let d = float (l.pagedispy - l.pagey) in
2839 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2840 GlDraw.begins `quads;
2842 GlDraw.vertex2 (x0+.panx, y0+.d);
2843 GlDraw.vertex2 (x1+.panx, y1+.d);
2844 GlDraw.vertex2 (x2+.panx, y2+.d);
2845 GlDraw.vertex2 (x3+.panx, y3+.d);
2847 GlDraw.ends ();
2849 ) state.layout
2850 ) state.rects
2852 Gl.disable `blend;
2855 let showstrings trusted active first pan strings =
2856 Gl.enable `blend;
2857 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2858 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2859 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2860 GlDraw.color (1., 1., 1.);
2861 Gl.enable `texture_2d;
2863 let wx = measurestr 15 "w" in
2864 let tabx = 30.0*.wx +. float (pan*15) in
2865 let rec loop row =
2866 if row = Array.length strings || (row - first) * 16 > conf.winh
2867 then ()
2868 else (
2869 let (s, level, _) = strings.(row) in
2870 let y = (row - first) * 16 in
2871 let x = 5 + 15*(max 0 (level+pan)) in
2872 if row = active
2873 then (
2874 Gl.disable `texture_2d;
2875 GlDraw.polygon_mode `both `line;
2876 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2877 GlDraw.rect (1., float (y + 1))
2878 (float (conf.winw - 1), float (y + 18));
2879 GlDraw.polygon_mode `both `fill;
2880 GlDraw.color (1., 1., 1.);
2881 Gl.enable `texture_2d;
2884 let drawtabularstring x s =
2885 let _ =
2886 if trusted
2887 then
2888 let tabpos = try String.index s '\t' with Not_found -> -1 in
2889 if tabpos > 0
2890 then
2891 let len = String.length s - tabpos - 1 in
2892 let s1 = String.sub s 0 tabpos
2893 and s2 = String.sub s (tabpos + 1) len in
2894 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2895 let x = truncate (max xx tabx) in
2896 drawstring1 15 x (y + 16) s2
2897 else
2898 drawstring1 15 x (y + 16) s
2899 else
2900 drawstring1 15 x (y + 16) s
2904 drawtabularstring (x + pan*15) s;
2905 loop (row+1)
2908 loop first;
2909 Gl.disable `blend;
2910 Gl.disable `texture_2d;
2913 let showoutline (_, active, first, outlines, _, pan, _) =
2914 showstrings false active first pan outlines;
2917 let showitems (active, first, items, _, pan, _) =
2918 showstrings true active first pan items;
2921 let display () =
2922 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2923 GlDraw.viewport margin 0 state.w conf.winh;
2924 pagematrix ();
2925 GlClear.color (scalecolor2 conf.bgcolor);
2926 GlClear.clear [`color];
2927 if conf.zoom > 1.0
2928 then (
2929 Gl.enable `scissor_test;
2930 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2932 List.iter drawpage state.layout;
2933 if conf.zoom > 1.0
2934 then
2935 Gl.disable `scissor_test
2937 if state.x != 0
2938 then (
2939 let x = -.float state.x in
2940 GlMat.translate ~x ();
2942 showrects ();
2943 showsel margin;
2944 GlDraw.viewport 0 0 conf.winw conf.winh;
2945 winmatrix ();
2946 scrollindicator ();
2947 begin match state.mode with
2948 | Items items -> showitems items
2949 | Outline outline -> showoutline outline
2950 | _ -> ()
2951 end;
2952 enttext ();
2953 Glut.swapBuffers ();
2956 let getunder x y =
2957 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2958 let x = x - margin - state.x in
2959 let rec f = function
2960 | l :: rest ->
2961 begin match getopaque l.pageno with
2962 | Some (opaque, _) when validopaque opaque ->
2963 let y = y - l.pagedispy in
2964 if y > 0
2965 then
2966 let y = l.pagey + y in
2967 let x = x - l.pagex in
2968 match whatsunder opaque x y with
2969 | Unone -> f rest
2970 | under -> under
2971 else
2972 f rest
2973 | _ ->
2974 f rest
2976 | [] -> Unone
2978 f state.layout
2981 let viewmouse button bstate x y =
2982 match button with
2983 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2984 if Glut.getModifiers () land Glut.active_ctrl != 0
2985 then (
2986 match state.mstate with
2987 | Mzoom (oldn, i) ->
2988 if oldn = n
2989 then (
2990 if i = 2
2991 then
2992 let incr =
2993 match n with
2994 | 4 ->
2995 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2996 | _ ->
2997 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2999 let zoom = conf.zoom +. incr in
3000 setzoom zoom;
3001 state.mstate <- Mzoom (n, 0);
3002 else
3003 state.mstate <- Mzoom (n, i+1);
3005 else state.mstate <- Mzoom (n, 0)
3007 | _ -> state.mstate <- Mzoom (n, 0)
3009 else (
3010 match state.autoscroll with
3011 | Some step -> setautoscrollspeed step (n=4)
3012 | None ->
3013 let incr =
3014 if n = 3
3015 then -conf.scrollstep
3016 else conf.scrollstep
3018 let incr = incr * 2 in
3019 let y = clamp incr in
3020 gotoy_and_clear_text y
3023 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3024 if bstate = Glut.DOWN
3025 then (
3026 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3027 state.mstate <- Mpan (x, y)
3029 else
3030 state.mstate <- Mnone
3032 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3033 if bstate = Glut.DOWN
3034 then
3035 let position, sh = scrollph state.y in
3036 if y > truncate position && y < truncate (position +. sh)
3037 then
3038 state.mstate <- Mscroll
3039 else
3040 let percent = float y /. float conf.winh in
3041 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3042 gotoy desty;
3043 state.mstate <- Mscroll
3044 else
3045 state.mstate <- Mnone
3047 | Glut.LEFT_BUTTON ->
3048 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3049 begin match dest with
3050 | Ulinkgoto (pageno, top) ->
3051 if pageno >= 0
3052 then (
3053 addnav ();
3054 gotopage1 pageno top;
3057 | Ulinkuri s ->
3058 print_endline s
3060 | Unone when bstate = Glut.DOWN ->
3061 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3062 state.mstate <- Mpan (x, y);
3064 | Unone | Utext _ ->
3065 if bstate = Glut.DOWN
3066 then (
3067 if conf.angle mod 360 = 0
3068 then (
3069 state.mstate <- Msel ((x, y), (x, y));
3070 Glut.postRedisplay ()
3073 else (
3074 match state.mstate with
3075 | Mnone -> ()
3077 | Mzoom _ | Mscroll ->
3078 state.mstate <- Mnone
3080 | Mpan _ ->
3081 Glut.setCursor Glut.CURSOR_INHERIT;
3082 state.mstate <- Mnone
3084 | Msel ((_, y0), (_, y1)) ->
3085 let f l =
3086 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3087 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3088 then
3089 match getopaque l.pageno with
3090 | Some (opaque, _) when validopaque opaque ->
3091 copysel opaque
3092 | _ -> ()
3094 List.iter f state.layout;
3095 copysel ""; (* ugly *)
3096 Glut.setCursor Glut.CURSOR_INHERIT;
3097 state.mstate <- Mnone;
3101 | _ -> ()
3104 let birdseyemouse button bstate x y
3105 (conf, leftx, _, hooverpageno, anchor) =
3106 match button with
3107 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3108 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3109 let rec loop = function
3110 | [] -> ()
3111 | l :: rest ->
3112 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3113 && x > margin && x < margin + l.pagew
3114 then (
3115 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3117 else loop rest
3119 loop state.layout
3120 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3121 | _ -> ()
3124 let mouse bstate button x y =
3125 match state.mode with
3126 | View -> viewmouse button bstate x y
3127 | Birdseye beye -> birdseyemouse button bstate x y beye
3128 | Textentry _ | Outline _ | Items _ -> ()
3131 let mouse ~button ~state ~x ~y = mouse state button x y;;
3133 let motion ~x ~y =
3134 match state.mode with
3135 | Outline _ -> ()
3136 | _ ->
3137 match state.mstate with
3138 | Mzoom _ | Mnone -> ()
3140 | Mpan (x0, y0) ->
3141 let dx = x - x0
3142 and dy = y0 - y in
3143 state.mstate <- Mpan (x, y);
3144 if conf.zoom > 1.0 then state.x <- state.x + dx;
3145 let y = clamp dy in
3146 gotoy_and_clear_text y
3148 | Msel (a, _) ->
3149 state.mstate <- Msel (a, (x, y));
3150 Glut.postRedisplay ()
3152 | Mscroll ->
3153 let y = min conf.winh (max 0 y) in
3154 let percent = float y /. float conf.winh in
3155 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3156 gotoy_and_clear_text y
3159 let pmotion ~x ~y =
3160 match state.mode with
3161 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3162 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3163 let rec loop = function
3164 | [] ->
3165 if hooverpageno != -1
3166 then (
3167 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3168 Glut.postRedisplay ();
3170 | l :: rest ->
3171 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3172 && x > margin && x < margin + l.pagew
3173 then (
3174 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3175 Glut.postRedisplay ();
3177 else loop rest
3179 loop state.layout
3181 | Outline _ | Items _ | Textentry _ -> ()
3182 | View ->
3183 match state.mstate with
3184 | Mnone ->
3185 begin match getunder x y with
3186 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3187 | Ulinkuri uri ->
3188 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3189 Glut.setCursor Glut.CURSOR_INFO
3190 | Ulinkgoto (page, _) ->
3191 if conf.underinfo
3192 then showtext 'p' ("age: " ^ string_of_int page);
3193 Glut.setCursor Glut.CURSOR_INFO
3194 | Utext s ->
3195 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3196 Glut.setCursor Glut.CURSOR_TEXT
3199 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3203 module State =
3204 struct
3205 open Parser
3207 let fontpath = ref "";;
3209 let home =
3211 match Sys.os_type with
3212 | "Win32" -> Sys.getenv "HOMEPATH"
3213 | _ -> Sys.getenv "HOME"
3214 with exn ->
3215 prerr_endline
3216 ("Can not determine home directory location: " ^
3217 Printexc.to_string exn);
3221 let config_of c attrs =
3222 let apply c k v =
3224 match k with
3225 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3226 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3227 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3228 | "preload" -> { c with preload = bool_of_string v }
3229 | "page-bias" -> { c with pagebias = int_of_string v }
3230 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3231 | "auto-scroll-step" ->
3232 { c with autoscrollstep = max 0 (int_of_string v) }
3233 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3234 | "crop-hack" -> { c with crophack = bool_of_string v }
3235 | "throttle" -> { c with showall = bool_of_string v }
3236 | "highlight-links" -> { c with hlinks = bool_of_string v }
3237 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3238 | "vertical-margin" ->
3239 { c with interpagespace = max 0 (int_of_string v) }
3240 | "zoom" ->
3241 let zoom = float_of_string v /. 100. in
3242 let zoom = max 0.01 zoom in
3243 { c with zoom = zoom }
3244 | "presentation" -> { c with presentation = bool_of_string v }
3245 | "rotation-angle" -> { c with angle = int_of_string v }
3246 | "width" -> { c with winw = max 20 (int_of_string v) }
3247 | "height" -> { c with winh = max 20 (int_of_string v) }
3248 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3249 | "proportional-display" -> { c with proportional = bool_of_string v }
3250 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3251 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3252 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3253 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3254 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3255 | "persistent-location" -> { c with jumpback = bool_of_string v }
3256 | "background-color" -> { c with bgcolor = color_of_string v }
3257 | "scrollbar-in-presentation" ->
3258 { c with scrollbarinpm = bool_of_string v }
3259 | _ -> c
3260 with exn ->
3261 prerr_endline ("Error processing attribute (`" ^
3262 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3265 let rec fold c = function
3266 | [] -> c
3267 | (k, v) :: rest ->
3268 let c = apply c k v in
3269 fold c rest
3271 fold c attrs;
3274 let fromstring f pos n v d =
3275 try f v
3276 with exn ->
3277 dolog "Error processing attribute (%S=%S) at %d\n%s"
3278 n v pos (Printexc.to_string exn)
3283 let bookmark_of attrs =
3284 let rec fold title page rely = function
3285 | ("title", v) :: rest -> fold v page rely rest
3286 | ("page", v) :: rest -> fold title v rely rest
3287 | ("rely", v) :: rest -> fold title page v rest
3288 | _ :: rest -> fold title page rely rest
3289 | [] -> title, page, rely
3291 fold "invalid" "0" "0" attrs
3294 let doc_of attrs =
3295 let rec fold path page rely pan = function
3296 | ("path", v) :: rest -> fold v page rely pan rest
3297 | ("page", v) :: rest -> fold path v rely pan rest
3298 | ("rely", v) :: rest -> fold path page v pan rest
3299 | ("pan", v) :: rest -> fold path page rely v rest
3300 | _ :: rest -> fold path page rely pan rest
3301 | [] -> path, page, rely, pan
3303 fold "" "0" "0" "0" attrs
3306 let setconf dst src =
3307 dst.scrollbw <- src.scrollbw;
3308 dst.scrollh <- src.scrollh;
3309 dst.icase <- src.icase;
3310 dst.preload <- src.preload;
3311 dst.pagebias <- src.pagebias;
3312 dst.verbose <- src.verbose;
3313 dst.scrollstep <- src.scrollstep;
3314 dst.maxhfit <- src.maxhfit;
3315 dst.crophack <- src.crophack;
3316 dst.autoscrollstep <- src.autoscrollstep;
3317 dst.showall <- src.showall;
3318 dst.hlinks <- src.hlinks;
3319 dst.underinfo <- src.underinfo;
3320 dst.interpagespace <- src.interpagespace;
3321 dst.zoom <- src.zoom;
3322 dst.presentation <- src.presentation;
3323 dst.angle <- src.angle;
3324 dst.winw <- src.winw;
3325 dst.winh <- src.winh;
3326 dst.savebmarks <- src.savebmarks;
3327 dst.memlimit <- src.memlimit;
3328 dst.proportional <- src.proportional;
3329 dst.texcount <- src.texcount;
3330 dst.sliceheight <- src.sliceheight;
3331 dst.blockwidth <- src.blockwidth;
3332 dst.thumbw <- src.thumbw;
3333 dst.jumpback <- src.jumpback;
3334 dst.bgcolor <- src.bgcolor;
3335 dst.scrollbarinpm <- src.scrollbarinpm;
3338 let unent s =
3339 let l = String.length s in
3340 let b = Buffer.create l in
3341 unent b s 0 l;
3342 Buffer.contents b;
3345 let get s =
3346 let h = Hashtbl.create 10 in
3347 let dc = { defconf with angle = defconf.angle } in
3348 let rec toplevel v t spos _ =
3349 match t with
3350 | Vdata | Vcdata | Vend -> v
3351 | Vopen ("llppconfig", _, closed) ->
3352 if closed
3353 then v
3354 else { v with f = llppconfig }
3355 | Vopen _ ->
3356 error "unexpected subelement at top level" s spos
3357 | Vclose _ -> error "unexpected close at top level" s spos
3359 and llppconfig v t spos _ =
3360 match t with
3361 | Vdata | Vcdata -> v
3362 | Vend -> error "unexpected end of input in llppconfig" s spos
3363 | Vopen ("defaults", attrs, closed) ->
3364 let c = config_of dc attrs in
3365 setconf dc c;
3366 if closed
3367 then v
3368 else { v with f = skip "defaults" (fun () -> v) }
3370 | Vopen ("ui-font", _, closed) ->
3371 if closed
3372 then v
3373 else { v with f = uifont (Buffer.create 10) }
3375 | Vopen ("doc", attrs, closed) ->
3376 let pathent, spage, srely, span = doc_of attrs in
3377 let path = unent pathent
3378 and pageno = fromstring int_of_string spos "page" spage 0
3379 and rely = fromstring float_of_string spos "rely" srely 0.0
3380 and pan = fromstring int_of_string spos "pan" span 0 in
3381 let c = config_of dc attrs in
3382 let anchor = (pageno, rely) in
3383 if closed
3384 then (Hashtbl.add h path (c, [], pan, anchor); v)
3385 else { v with f = doc path pan anchor c [] }
3387 | Vopen _ ->
3388 error "unexpected subelement in llppconfig" s spos
3390 | Vclose "llppconfig" -> { v with f = toplevel }
3391 | Vclose _ -> error "unexpected close in llppconfig" s spos
3393 and uifont b v t spos epos =
3394 match t with
3395 | Vdata | Vcdata ->
3396 Buffer.add_substring b s spos (epos - spos);
3398 | Vopen (_, _, _) ->
3399 error "unexpected subelement in ui-font" s spos
3400 | Vclose "ui-font" ->
3401 if String.length !fontpath = 0
3402 then fontpath := Buffer.contents b;
3403 { v with f = llppconfig }
3404 | Vclose _ -> error "unexpected close in ui-font" s spos
3405 | Vend -> error "unexpected end of input in ui-font" s spos
3407 and doc path pan anchor c bookmarks v t spos _ =
3408 match t with
3409 | Vdata | Vcdata -> v
3410 | Vend -> error "unexpected end of input in doc" s spos
3411 | Vopen ("bookmarks", _, closed) ->
3412 if closed
3413 then v
3414 else { v with f = pbookmarks path pan anchor c bookmarks }
3416 | Vopen (_, _, _) ->
3417 error "unexpected subelement in doc" s spos
3419 | Vclose "doc" ->
3420 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3421 { v with f = llppconfig }
3423 | Vclose _ -> error "unexpected close in doc" s spos
3425 and pbookmarks path pan anchor c bookmarks v t spos _ =
3426 match t with
3427 | Vdata | Vcdata -> v
3428 | Vend -> error "unexpected end of input in bookmarks" s spos
3429 | Vopen ("item", attrs, closed) ->
3430 let titleent, spage, srely = bookmark_of attrs in
3431 let page = fromstring int_of_string spos "page" spage 0
3432 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3433 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3434 if closed
3435 then { v with f = pbookmarks path pan anchor c bookmarks }
3436 else
3437 let f () = v in
3438 { v with f = skip "item" f }
3440 | Vopen _ ->
3441 error "unexpected subelement in bookmarks" s spos
3443 | Vclose "bookmarks" ->
3444 { v with f = doc path pan anchor c bookmarks }
3446 | Vclose _ -> error "unexpected close in bookmarks" s spos
3448 and skip tag f v t spos _ =
3449 match t with
3450 | Vdata | Vcdata -> v
3451 | Vend ->
3452 error ("unexpected end of input in skipped " ^ tag) s spos
3453 | Vopen (tag', _, closed) ->
3454 if closed
3455 then v
3456 else
3457 let f' () = { v with f = skip tag f } in
3458 { v with f = skip tag' f' }
3459 | Vclose ctag ->
3460 if tag = ctag
3461 then f ()
3462 else error ("unexpected close in skipped " ^ tag) s spos
3465 parse { f = toplevel; accu = () } s;
3466 h, dc;
3469 let do_load f ic =
3471 let len = in_channel_length ic in
3472 let s = String.create len in
3473 really_input ic s 0 len;
3474 f s;
3475 with
3476 | Parse_error (msg, s, pos) ->
3477 let subs = subs s pos in
3478 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3479 failwith ("parse error: " ^ s)
3481 | exn ->
3482 failwith ("config load error: " ^ Printexc.to_string exn)
3485 let defconfpath =
3486 let dir =
3488 let dir = Filename.concat home ".config" in
3489 if Sys.is_directory dir then dir else home
3490 with _ -> home
3492 Filename.concat dir "llpp.conf"
3495 let confpath = ref defconfpath;;
3497 let load1 f =
3498 if Sys.file_exists !confpath
3499 then
3500 match
3501 (try Some (open_in_bin !confpath)
3502 with exn ->
3503 prerr_endline
3504 ("Error opening configuation file `" ^ !confpath ^ "': " ^
3505 Printexc.to_string exn);
3506 None
3508 with
3509 | Some ic ->
3510 begin try
3511 f (do_load get ic)
3512 with exn ->
3513 prerr_endline
3514 ("Error loading configuation from `" ^ !confpath ^ "': " ^
3515 Printexc.to_string exn);
3516 end;
3517 close_in ic;
3519 | None -> ()
3520 else
3521 f (Hashtbl.create 0, defconf)
3524 let load () =
3525 let f (h, dc) =
3526 let pc, pb, px, pa =
3528 Hashtbl.find h (Filename.basename state.path)
3529 with Not_found -> dc, [], 0, (0, 0.0)
3531 setconf defconf dc;
3532 setconf conf pc;
3533 state.bookmarks <- pb;
3534 state.x <- px;
3535 state.scrollw <- conf.scrollbw;
3536 if conf.jumpback
3537 then state.anchor <- pa;
3538 cbput state.hists.nav pa;
3540 load1 f
3543 let add_attrs bb always dc c =
3544 let ob s a b =
3545 if always || a != b
3546 then Printf.bprintf bb "\n %s='%b'" s a
3547 and oi s a b =
3548 if always || a != b
3549 then Printf.bprintf bb "\n %s='%d'" s a
3550 and oz s a b =
3551 if always || a <> b
3552 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
3553 and oc s a b =
3554 if always || a <> b
3555 then
3556 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3558 let w, h =
3559 if always
3560 then dc.winw, dc.winh
3561 else
3562 match state.fullscreen with
3563 | Some wh -> wh
3564 | None -> c.winw, c.winh
3566 let zoom, presentation, interpagespace, showall=
3567 if always
3568 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3569 else
3570 match state.mode with
3571 | Birdseye (bc, _, _, _, _) ->
3572 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3573 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3575 oi "width" w dc.winw;
3576 oi "height" h dc.winh;
3577 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3578 oi "scroll-handle-height" c.scrollh dc.scrollh;
3579 ob "case-insensitive-search" c.icase dc.icase;
3580 ob "preload" c.preload dc.preload;
3581 oi "page-bias" c.pagebias dc.pagebias;
3582 oi "scroll-step" c.scrollstep dc.scrollstep;
3583 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3584 ob "max-height-fit" c.maxhfit dc.maxhfit;
3585 ob "crop-hack" c.crophack dc.crophack;
3586 ob "throttle" showall dc.showall;
3587 ob "highlight-links" c.hlinks dc.hlinks;
3588 ob "under-cursor-info" c.underinfo dc.underinfo;
3589 oi "vertical-margin" interpagespace dc.interpagespace;
3590 oz "zoom" zoom dc.zoom;
3591 ob "presentation" presentation dc.presentation;
3592 oi "rotation-angle" c.angle dc.angle;
3593 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3594 ob "proportional-display" c.proportional dc.proportional;
3595 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3596 oi "tex-count" c.texcount dc.texcount;
3597 oi "slice-height" c.sliceheight dc.sliceheight;
3598 oi "block-width" c.blockwidth dc.blockwidth;
3599 oi "thumbnail-width" c.thumbw dc.thumbw;
3600 ob "persistent-location" c.jumpback dc.jumpback;
3601 oc "background-color" c.bgcolor dc.bgcolor;
3602 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3605 let save () =
3606 let bb = Buffer.create 32768 in
3607 let f (h, dc) =
3608 let dc = if conf.bedefault then conf else dc in
3609 Buffer.add_string bb "<llppconfig>\n";
3611 if String.length !fontpath > 0
3612 then Printf.bprintf bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3614 Buffer.add_string bb "<defaults ";
3615 add_attrs bb true dc dc;
3616 Buffer.add_string bb "/>\n";
3618 let adddoc path pan anchor c bookmarks =
3619 if bookmarks == [] && c = dc && anchor = emptyanchor
3620 then ()
3621 else (
3622 Printf.bprintf bb "<doc path='%s'"
3623 (enent path 0 (String.length path));
3625 if anchor <> emptyanchor
3626 then (
3627 let n, y = anchor in
3628 Printf.bprintf bb " page='%d'" n;
3629 if y > 1e-6
3630 then
3631 Printf.bprintf bb " rely='%f'" y
3635 if pan != 0
3636 then Printf.bprintf bb " pan='%d'" pan;
3638 add_attrs bb false dc c;
3640 begin match bookmarks with
3641 | [] -> Buffer.add_string bb "/>\n"
3642 | _ ->
3643 Buffer.add_string bb ">\n<bookmarks>\n";
3644 List.iter (fun (title, _level, (page, rely)) ->
3645 Printf.bprintf bb
3646 "<item title='%s' page='%d'"
3647 (enent title 0 (String.length title))
3648 page
3650 if rely > 1e-6
3651 then
3652 Printf.bprintf bb " rely='%f'" rely
3654 Buffer.add_string bb "/>\n";
3655 ) bookmarks;
3656 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3657 end;
3661 let pan =
3662 match state.mode with
3663 | Birdseye (_, pan, _, _, _) -> pan
3664 | _ -> state.x
3666 let basename = Filename.basename state.path in
3667 adddoc basename pan (getanchor ())
3668 { conf with
3669 autoscrollstep =
3670 match state.autoscroll with
3671 | Some step -> step
3672 | None -> conf.autoscrollstep }
3673 (if conf.savebmarks then state.bookmarks else []);
3675 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3676 if basename <> path
3677 then adddoc path x y c bookmarks
3678 ) h;
3679 Buffer.add_string bb "</llppconfig>";
3681 load1 f;
3682 if Buffer.length bb > 0
3683 then
3685 let tmp = !confpath ^ ".tmp" in
3686 let oc = open_out_bin tmp in
3687 Buffer.output_buffer oc bb;
3688 close_out oc;
3689 Sys.rename tmp !confpath;
3690 with exn ->
3691 prerr_endline
3692 ("error while saving configuration: " ^ Printexc.to_string exn)
3694 end;;
3696 let () =
3697 Arg.parse
3698 (Arg.align
3699 [("-p", Arg.String (fun s -> state.password <- s) ,
3700 "<password> Set password");
3702 ("-f", Arg.String (fun s -> State.fontpath := s),
3703 "<path> Set path to the user interface font");
3705 ("-c", Arg.String (fun s -> State.confpath := s),
3706 "<path> Set path to the configuration file");
3708 ("-v", Arg.Unit (fun () ->
3709 Printf.printf
3710 "%s\nconfiguration path: %s\n"
3711 Help.version
3712 State.defconfpath
3714 exit 0), " Print version and exit");
3717 (fun s -> state.path <- s)
3718 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3720 if String.length state.path = 0
3721 then (prerr_endline "file name missing"; exit 1);
3723 State.load ();
3725 let _ = Glut.init Sys.argv in
3726 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3727 let () = Glut.initWindowSize conf.winw conf.winh in
3728 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3730 let csock, ssock =
3731 if Sys.os_type = "Unix"
3732 then
3733 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3734 else
3735 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3736 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3737 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3738 Unix.bind sock addr;
3739 Unix.listen sock 1;
3740 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3741 Unix.connect csock addr;
3742 let ssock, _ = Unix.accept sock in
3743 Unix.close sock;
3744 let opts sock =
3745 Unix.setsockopt sock Unix.TCP_NODELAY true;
3746 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3748 opts ssock;
3749 opts csock;
3750 ssock, csock
3753 let () = Glut.displayFunc display in
3754 let () = Glut.reshapeFunc reshape in
3755 let () = Glut.keyboardFunc keyboard in
3756 let () = Glut.specialFunc special in
3757 let () = Glut.idleFunc (Some idle) in
3758 let () = Glut.mouseFunc mouse in
3759 let () = Glut.motionFunc motion in
3760 let () = Glut.passiveMotionFunc pmotion in
3762 init ssock (
3763 conf.angle, conf.proportional, conf.texcount,
3764 conf.sliceheight, conf.blockwidth, !State.fontpath
3766 state.csock <- csock;
3767 state.ssock <- ssock;
3768 state.text <- "Opening " ^ state.path;
3769 writeopen state.path state.password;
3771 while true do
3773 Glut.mainLoop ();
3774 with
3775 | Glut.BadEnum "key in special_of_int" ->
3776 showtext '!' " LablGlut bug: special key not recognized";
3778 | Quit ->
3779 if Sys.os_type <> "Unix"
3780 then Unix.shutdown ssock Unix.SHUTDOWN_ALL;
3781 State.save ();
3782 exit 0
3783 done;