Anchor on rotaiton or proprotionality change
[llpp.git] / main.ml
blob577f54bfd74c0ce140b18340724bd6e2cfdddeeb
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
597 then
598 let memleft = conf.memlimit - state.memused in
599 if memleft < 0
600 then
601 let opaque = cbpeek state.pagecache in
602 match findpageforopaque opaque with
603 | Some ((n, _, _, _, _), size) ->
604 memleft + size >= 0 && not (pagevisible state.layout n)
605 | None -> false
606 else true
607 else false
609 if oktopreload
610 then
611 let presentation = conf.presentation in
612 let interpagespace = conf.interpagespace in
613 let maxy = state.maxy in
614 conf.presentation <- false;
615 conf.interpagespace <- 0;
616 state.maxy <- calcheight ();
617 let y =
618 match state.layout with
619 | [] -> 0
620 | l :: _ -> getpagey l.pageno + l.pagey
622 let y = if y < conf.winh then 0 else y - conf.winh in
623 let h = state.y - y + conf.winh*3 in
624 let pages = layout y h in
625 List.iter render pages;
626 conf.presentation <- presentation;
627 conf.interpagespace <- interpagespace;
628 state.maxy <- maxy;
631 let gotoy y =
632 let y = max 0 y in
633 let y = min state.maxy y in
634 let pages = layout y conf.winh in
635 let ready = loadlayout pages in
636 if conf.showall
637 then (
638 if ready
639 then (
640 state.y <- y;
641 state.layout <- pages;
642 state.throttle <- None;
643 Glut.postRedisplay ();
645 else (
646 state.throttle <- Some pages;
649 else (
650 state.y <- y;
651 state.layout <- pages;
652 state.throttle <- None;
653 Glut.postRedisplay ();
655 begin match state.mode with
656 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
657 if not (pagevisible pages pageno)
658 then (
659 match state.layout with
660 | [] -> ()
661 | l :: _ ->
662 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
664 | _ -> ()
665 end;
666 preload ();
669 let gotoy_and_clear_text y =
670 gotoy y;
671 if not conf.verbose then state.text <- "";
674 let getanchor () =
675 match state.layout with
676 | [] -> emptyanchor
677 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
680 let getanchory (n, top) =
681 let y, h = getpageyh n in
682 y + (truncate (top *. float h));
685 let gotoanchor anchor =
686 gotoy (getanchory anchor);
689 let addnav () =
690 cbput state.hists.nav (getanchor ());
693 let getnav dir =
694 let anchor = cbgetc state.hists.nav dir in
695 getanchory anchor;
698 let gotopage n top =
699 let y, h = getpageyh n in
700 gotoy_and_clear_text (y + (truncate (top *. float h)));
703 let gotopage1 n top =
704 let y = getpagey n in
705 gotoy_and_clear_text (y + top);
708 let invalidate () =
709 state.layout <- [];
710 state.pdims <- [];
711 state.rects <- [];
712 state.rects1 <- [];
713 state.invalidated <- state.invalidated + 1;
716 let scalecolor c =
717 let c = c *. state.colorscale in
718 (c, c, c);
721 let scalecolor2 (r, g, b) =
722 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
725 let represent () =
726 state.maxy <- calcheight ();
727 match state.mode with
728 | Birdseye (_, _, pageno, _, _) ->
729 let y, h = getpageyh pageno in
730 let top = (conf.winh - h) / 2 in
731 gotoy (max 0 (y - top))
732 | _ -> gotoanchor state.anchor
735 let pagematrix () =
736 GlMat.mode `projection;
737 GlMat.load_identity ();
738 GlMat.rotate ~x:1.0 ~angle:180.0 ();
739 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
740 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
741 if state.x != 0
742 then (
743 GlMat.translate ~x:(float state.x) ();
747 let winmatrix () =
748 GlMat.mode `projection;
749 GlMat.load_identity ();
750 GlMat.rotate ~x:1.0 ~angle:180.0 ();
751 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
752 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
755 let reshape =
756 let firsttime = ref true in
757 fun ~w ~h ->
758 if state.invalidated = 0 && not !firsttime
759 then state.anchor <- getanchor ();
761 firsttime := false;
762 conf.winw <- w;
763 let w = truncate (float w *. conf.zoom) - state.scrollw in
764 let w = max w 2 in
765 state.w <- w;
766 conf.winh <- h;
767 GlMat.mode `modelview;
768 GlMat.load_identity ();
769 GlClear.color (scalecolor 1.0);
770 GlClear.clear [`color];
772 invalidate ();
773 wcmd "geometry" [`i w; `i h];
776 let drawstring size x y s =
777 Gl.enable `blend;
778 Gl.enable `texture_2d;
779 ignore (drawstr size x y s);
780 Gl.disable `blend;
781 Gl.disable `texture_2d;
784 let drawstring1 size x y s =
785 drawstr size x y s;
788 let enttext () =
789 let len = String.length state.text in
790 let drawstring s =
791 GlDraw.color (0.0, 0.0, 0.0);
792 GlDraw.rect
793 (0.0, float (conf.winh - 18))
794 (float (conf.winw - state.scrollw - 1), float conf.winh)
796 GlDraw.color (1.0, 1.0, 1.0);
797 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
799 match state.mode with
800 | Textentry ((prefix, text, _, _, _), _) ->
801 let s =
802 if len > 0
803 then
804 Printf.sprintf "%s%s_ [%s]" prefix text state.text
805 else
806 Printf.sprintf "%s%s_" prefix text
808 drawstring s
810 | _ ->
811 if len > 0 then drawstring state.text
814 let showtext c s =
815 state.text <- Printf.sprintf "%c%s" c s;
816 Glut.postRedisplay ();
819 let act cmd =
820 match cmd.[0] with
821 | 'c' ->
822 state.pdims <- [];
824 | 'D' ->
825 state.rects <- state.rects1;
826 Glut.postRedisplay ()
828 | 'C' ->
829 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
830 state.pagecount <- n;
831 state.invalidated <- state.invalidated - 1;
832 if state.invalidated = 0
833 then represent ()
835 | 't' ->
836 let s = Scanf.sscanf cmd "t %n"
837 (fun n -> String.sub cmd n (String.length cmd - n))
839 Glut.setWindowTitle s
841 | 'T' ->
842 let s = Scanf.sscanf cmd "T %n"
843 (fun n -> String.sub cmd n (String.length cmd - n))
845 if istextentry state.mode
846 then (
847 state.text <- s;
848 showtext ' ' s;
850 else (
851 state.text <- s;
852 Glut.postRedisplay ();
855 | 'V' ->
856 if conf.verbose
857 then
858 let s = Scanf.sscanf cmd "V %n"
859 (fun n -> String.sub cmd n (String.length cmd - n))
861 state.text <- s;
862 showtext ' ' s;
864 | 'F' ->
865 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
866 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
867 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
868 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
870 let y = (getpagey pageno) + truncate y0 in
871 addnav ();
872 gotoy y;
873 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
875 | 'R' ->
876 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
877 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
878 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
879 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
881 state.rects1 <-
882 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
884 | 'r' ->
885 let n, w, _h, r, l, s, p =
886 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
887 (fun n w h r l s p ->
888 (n-1, w, h, r, l != 0, s, p))
891 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
892 state.memused <- state.memused + s;
894 let layout =
895 match state.throttle with
896 | None -> state.layout
897 | Some layout -> layout
900 let rec gc () =
901 if (state.memused <= conf.memlimit) || cbempty state.pagecache
902 then ()
903 else (
904 let evictedopaque = cbpeek state.pagecache in
905 match findpageforopaque evictedopaque with
906 | None -> failwith "bug in gc"
907 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
908 if state.gen != gen || not (pagevisible layout evictedn)
909 then (
910 wcmd "free" [`s evictedopaque];
911 state.memused <- state.memused - evictedsize;
912 Hashtbl.remove state.pagemap k;
913 cbdecr state.pagecache;
914 gc ();
918 gc ();
920 cbput state.pagecache p;
921 state.rendering <- false;
923 begin match state.throttle with
924 | None ->
925 if pagevisible state.layout n
926 then gotoy state.y
927 else (
928 let allvisible = loadlayout state.layout in
929 if allvisible then preload ();
932 | Some layout ->
933 match layout with
934 | [] -> ()
935 | l :: _ ->
936 let y = getpagey l.pageno + l.pagey in
937 gotoy y
940 | 'l' ->
941 let pdim =
942 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
944 state.pdims <- pdim :: state.pdims
946 | 'o' ->
947 let (l, n, t, h, pos) =
948 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
950 let s = String.sub cmd pos (String.length cmd - pos) in
951 let outline = (s, l, (n, float t /. float h)) in
952 let outlines =
953 match state.outlines with
954 | Olist outlines -> Olist (outline :: outlines)
955 | Oarray _ -> Olist [outline]
956 | Onarrow _ -> Olist [outline]
958 state.outlines <- outlines
961 | 'i' ->
962 if String.length cmd > 1 && cmd.[1] = 'e'
963 then
964 state.docinfo <- List.rev state.docinfo
965 else
966 let s = Scanf.sscanf cmd "i %n"
967 (fun n -> String.sub cmd n (String.length cmd - n))
969 state.docinfo <- (1, s) :: state.docinfo
971 | _ ->
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday;;
977 let idle () =
978 if state.deadline == nan then state.deadline <- now ();
979 let rec loop delay =
980 let timeout =
981 if delay > 0.0
982 then max 0.0 (state.deadline -. now ())
983 else 0.0
985 let r, _, _ = Unix.select [state.csock] [] [] timeout in
986 state.deadline <- state.deadline +. delay;
987 begin match r with
988 | [] ->
989 begin match state.autoscroll with
990 | Some step when step != 0 ->
991 let y = state.y + step in
992 let y =
993 if y < 0
994 then state.maxy
995 else if y >= state.maxy then 0 else y
997 gotoy y;
998 if state.mode = View
999 then state.text <- "";
1000 | _ -> ()
1001 end;
1003 | _ ->
1004 let cmd = readcmd state.csock in
1005 act cmd;
1006 loop 0.0
1007 end;
1008 in loop 0.007
1011 let onhist cb =
1012 let rc = cb.rc in
1013 let action = function
1014 | HCprev -> cbget cb ~-1
1015 | HCnext -> cbget cb 1
1016 | HCfirst -> cbget cb ~-(cb.rc)
1017 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1018 and cancel () = cb.rc <- rc
1019 in (action, cancel)
1022 let search pattern forward =
1023 if String.length pattern > 0
1024 then
1025 let pn, py =
1026 match state.layout with
1027 | [] -> 0, 0
1028 | l :: _ ->
1029 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1031 let cmd =
1032 let b = makecmd "search"
1033 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1035 Buffer.add_char b ',';
1036 Buffer.add_string b pattern;
1037 Buffer.add_char b '\000';
1038 Buffer.contents b;
1040 writecmd state.csock cmd;
1043 let intentry text key =
1044 let c = Char.unsafe_chr key in
1045 match c with
1046 | '0' .. '9' ->
1047 let s = "x" in s.[0] <- c;
1048 let text = text ^ s in
1049 TEcont text
1051 | _ ->
1052 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1053 TEcont text
1056 let addchar s c =
1057 let b = Buffer.create (String.length s + 1) in
1058 Buffer.add_string b s;
1059 Buffer.add_char b c;
1060 Buffer.contents b;
1063 let textentry text key =
1064 let c = Char.unsafe_chr key in
1065 match c with
1066 | _ when key >= 32 && key < 127 ->
1067 let text = addchar text c in
1068 TEcont text
1070 | _ ->
1071 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1072 TEcont text
1075 let reinit angle proportional =
1076 state.anchor <- getanchor ();
1077 conf.angle <- angle;
1078 conf.proportional <- proportional;
1079 invalidate ();
1080 wcmd "reinit" [`i angle; `b proportional];
1083 let setzoom zoom =
1084 let zoom = max 0.01 zoom in
1085 if zoom <> conf.zoom
1086 then (
1087 if zoom <= 1.0
1088 then state.x <- 0;
1089 conf.zoom <- zoom;
1090 reshape conf.winw conf.winh;
1091 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1095 let enterbirdseye () =
1096 let zoom = float conf.thumbw /. float conf.winw in
1097 let birdseyepageno =
1098 let cy = conf.winh / 2 in
1099 let fold = function
1100 | [] -> 0
1101 | l :: rest ->
1102 let rec fold best = function
1103 | [] -> best.pageno
1104 | l :: rest ->
1105 let d = cy - (l.pagedispy + l.pagevh/2)
1106 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1107 if abs d < abs dbest
1108 then fold l rest
1109 else best.pageno
1110 in fold l rest
1112 fold state.layout
1114 state.mode <- Birdseye (
1115 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1117 conf.zoom <- zoom;
1118 conf.presentation <- false;
1119 conf.interpagespace <- 10;
1120 conf.hlinks <- false;
1121 state.x <- 0;
1122 state.mstate <- Mnone;
1123 conf.showall <- false;
1124 Glut.setCursor Glut.CURSOR_INHERIT;
1125 if conf.verbose
1126 then
1127 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1128 (100.0*.zoom)
1129 else
1130 state.text <- ""
1132 reshape conf.winw conf.winh;
1135 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1136 state.mode <- View;
1137 conf.zoom <- c.zoom;
1138 conf.presentation <- c.presentation;
1139 conf.interpagespace <- c.interpagespace;
1140 conf.showall <- c.showall;
1141 conf.hlinks <- c.hlinks;
1142 state.x <- leftx;
1143 if conf.verbose
1144 then
1145 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1146 (100.0*.conf.zoom)
1148 reshape conf.winw conf.winh;
1149 state.anchor <- if goback then anchor else (pageno, 0.0);
1152 let togglebirdseye () =
1153 match state.mode with
1154 | Birdseye vals -> leavebirdseye vals true
1155 | View | Outline _ -> enterbirdseye ()
1156 | _ -> ()
1159 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1160 let pageno = max 0 (pageno - 1) in
1161 let rec loop = function
1162 | [] -> gotopage1 pageno 0
1163 | l :: _ when l.pageno = pageno ->
1164 if l.pagedispy >= 0 && l.pagey = 0
1165 then Glut.postRedisplay ()
1166 else gotopage1 pageno 0
1167 | _ :: rest -> loop rest
1169 loop state.layout;
1170 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1173 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1174 let pageno = min (state.pagecount - 1) (pageno + 1) in
1175 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1176 let rec loop = function
1177 | [] ->
1178 let y, h = getpageyh pageno in
1179 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1180 gotoy (clamp dy)
1181 | l :: _ when l.pageno = pageno ->
1182 if l.pagevh != l.pageh
1183 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1184 else Glut.postRedisplay ()
1185 | _ :: rest -> loop rest
1187 loop state.layout
1190 let optentry mode _ key =
1191 let btos b = if b then "on" else "off" in
1192 let c = Char.unsafe_chr key in
1193 match c with
1194 | 's' ->
1195 let ondone s =
1196 try conf.scrollstep <- int_of_string s with exc ->
1197 state.text <- Printf.sprintf "bad integer `%s': %s"
1198 s (Printexc.to_string exc)
1200 TEswitch ("scroll step: ", "", None, intentry, ondone)
1202 | 'A' ->
1203 let ondone s =
1205 conf.autoscrollstep <- int_of_string s;
1206 if state.autoscroll <> None
1207 then state.autoscroll <- Some conf.autoscrollstep
1208 with exc ->
1209 state.text <- Printf.sprintf "bad integer `%s': %s"
1210 s (Printexc.to_string exc)
1212 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1214 | 'Z' ->
1215 let ondone s =
1217 let zoom = float (int_of_string s) /. 100.0 in
1218 setzoom zoom
1219 with exc ->
1220 state.text <- Printf.sprintf "bad integer `%s': %s"
1221 s (Printexc.to_string exc)
1223 TEswitch ("zoom: ", "", None, intentry, ondone)
1225 | 't' ->
1226 let ondone s =
1228 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1229 state.text <-
1230 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1231 begin match mode with
1232 | Birdseye beye ->
1233 leavebirdseye beye false;
1234 enterbirdseye ();
1235 | _ -> ();
1237 with exc ->
1238 state.text <- Printf.sprintf "bad integer `%s': %s"
1239 s (Printexc.to_string exc)
1241 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1243 | 'R' ->
1244 let ondone s =
1245 match try
1246 Some (int_of_string s)
1247 with exc ->
1248 state.text <- Printf.sprintf "bad integer `%s': %s"
1249 s (Printexc.to_string exc);
1250 None
1251 with
1252 | Some angle -> reinit angle conf.proportional
1253 | None -> ()
1255 TEswitch ("rotation: ", "", None, intentry, ondone)
1257 | 'i' ->
1258 conf.icase <- not conf.icase;
1259 TEdone ("case insensitive search " ^ (btos conf.icase))
1261 | 'p' ->
1262 conf.preload <- not conf.preload;
1263 gotoy state.y;
1264 TEdone ("preload " ^ (btos conf.preload))
1266 | 'v' ->
1267 conf.verbose <- not conf.verbose;
1268 TEdone ("verbose " ^ (btos conf.verbose))
1270 | 'h' ->
1271 conf.maxhfit <- not conf.maxhfit;
1272 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1273 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1275 | 'c' ->
1276 conf.crophack <- not conf.crophack;
1277 TEdone ("crophack " ^ btos conf.crophack)
1279 | 'a' ->
1280 conf.showall <- not conf.showall;
1281 TEdone ("throttle " ^ btos conf.showall)
1283 | 'f' ->
1284 conf.underinfo <- not conf.underinfo;
1285 TEdone ("underinfo " ^ btos conf.underinfo)
1287 | 'P' ->
1288 conf.savebmarks <- not conf.savebmarks;
1289 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1291 | 'S' ->
1292 let ondone s =
1294 let pageno, py =
1295 match state.layout with
1296 | [] -> 0, 0
1297 | l :: _ ->
1298 l.pageno, l.pagey
1300 conf.interpagespace <- int_of_string s;
1301 state.maxy <- calcheight ();
1302 let y = getpagey pageno in
1303 gotoy (y + py)
1304 with exc ->
1305 state.text <- Printf.sprintf "bad integer `%s': %s"
1306 s (Printexc.to_string exc)
1308 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1310 | 'l' ->
1311 reinit conf.angle (not conf.proportional);
1312 TEdone ("proprortional display " ^ btos conf.proportional)
1314 | _ ->
1315 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1316 TEstop
1319 let maxoutlinerows () = (conf.winh - 31) / 16;;
1321 let enterselector allowdel outlines errmsg msg =
1322 if Array.length outlines = 0
1323 then (
1324 showtext ' ' errmsg;
1326 else (
1327 state.text <- msg;
1328 Glut.setCursor Glut.CURSOR_INHERIT;
1329 let pageno =
1330 match state.layout with
1331 | [] -> -1
1332 | {pageno=pageno} :: _ -> pageno
1334 let active =
1335 let rec loop n =
1336 if n = Array.length outlines
1337 then 0
1338 else
1339 let (_, _, (outlinepageno, _)) = outlines.(n) in
1340 if outlinepageno >= pageno then n else loop (n+1)
1342 loop 0
1344 state.mode <- Outline
1345 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1346 state.mode);
1347 Glut.postRedisplay ();
1351 let enteroutlinemode () =
1352 let outlines, msg =
1353 match state.outlines with
1354 | Oarray a -> a, ""
1355 | Olist l ->
1356 let a = Array.of_list (List.rev l) in
1357 state.outlines <- Oarray a;
1358 a, ""
1359 | Onarrow (pat, a, _) ->
1360 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1362 enterselector false outlines "Document has no outline" msg;
1365 let enterbookmarkmode () =
1366 let bookmarks = Array.of_list state.bookmarks in
1367 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1370 let mode_to_string mode =
1371 let b = Buffer.create 10 in
1372 let rec f = function
1373 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1374 | View -> Buffer.add_string b "View"
1375 | Birdseye _ -> Buffer.add_string b "Birdseye"
1376 | Items _ -> Buffer.add_string b "Items"
1377 | Outline _ -> Buffer.add_string b "Outline"
1379 f mode;
1380 Buffer.contents b;
1383 let color_of_string s =
1384 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1385 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1389 let color_to_string (r, g, b) =
1390 let r = truncate (r *. 256.0)
1391 and g = truncate (g *. 256.0)
1392 and b = truncate (b *. 256.0) in
1393 Printf.sprintf "%d/%d/%d" r g b
1396 let enterinfomode () =
1397 let btos = function true -> "on" | _ -> "off" in
1398 let mode = state.mode in
1399 let rec makeitems () =
1400 let intp name get set =
1401 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1402 fun active first _ pan ->
1403 let ondone s =
1404 let n =
1405 try int_of_string s
1406 with exn ->
1407 state.text <- Printf.sprintf "bad integer `%s': %s"
1408 s (Printexc.to_string exn);
1409 max_int;
1411 if n != max_int then set n;
1413 let te = name ^ ": ", "", None, intentry, ondone in
1414 state.text <- "";
1415 Textentry (
1417 fun _ ->
1418 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1421 and boolp name get set =
1422 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1423 fun active first qsearch pan ->
1424 let v = get () in
1425 set (not v);
1426 Items (active, first, makeitems (), qsearch, pan, mode);
1428 and colorp name get set =
1429 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1430 fun active first _ pan ->
1431 let invalid = (nan, nan, nan) in
1432 let ondone s =
1433 let c =
1434 try color_of_string s
1435 with exn ->
1436 state.text <- Printf.sprintf "bad color `%s': %s"
1437 s (Printexc.to_string exn);
1438 invalid
1440 if c <> invalid
1441 then set c;
1443 let te = name ^ ": ", "", None, textentry, ondone in
1444 state.text <- "";
1445 Textentry (
1447 fun _ ->
1448 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1453 let birdseye = isbirdseye mode in
1454 let items = [
1455 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1457 boolp "presentation"
1458 (fun () -> conf.presentation)
1459 (fun v ->
1460 conf.presentation <- v;
1461 state.anchor <- getanchor ();
1462 represent ());
1464 boolp "ignore case in searches"
1465 (fun () -> conf.icase)
1466 (fun v -> conf.icase <- v);
1468 boolp "preload"
1469 (fun () -> conf.preload)
1470 (fun v -> conf.preload <- v);
1472 boolp "verbose"
1473 (fun () -> conf.verbose)
1474 (fun v -> conf.verbose <- v);
1476 boolp "max fit"
1477 (fun () -> conf.maxhfit)
1478 (fun v -> conf.maxhfit <- v);
1480 boolp "crop hack"
1481 (fun () -> conf.crophack)
1482 (fun v -> conf.crophack <- v);
1484 boolp "throttle"
1485 (fun () -> conf.showall)
1486 (fun v -> conf.showall <- v);
1488 boolp "highlight links"
1489 (fun () -> conf.hlinks)
1490 (fun v -> conf.hlinks <- v);
1492 boolp "under info"
1493 (fun () -> conf.underinfo)
1494 (fun v -> conf.underinfo <- v);
1495 boolp "persistent bookmarks"
1496 (fun () -> conf.savebmarks)
1497 (fun v -> conf.savebmarks <- v);
1499 boolp "proportional display"
1500 (fun () -> conf.proportional)
1501 (fun v -> reinit conf.angle v);
1503 boolp "persistent location"
1504 (fun () -> conf.jumpback)
1505 (fun v -> conf.jumpback <- v);
1507 "", 0, Noaction;
1509 intp "vertical margin"
1510 (fun () -> conf.interpagespace)
1511 (fun n ->
1512 conf.interpagespace <- n;
1513 let pageno, py =
1514 match state.layout with
1515 | [] -> 0, 0
1516 | l :: _ ->
1517 l.pageno, l.pagey
1519 state.maxy <- calcheight ();
1520 let y = getpagey pageno in
1521 gotoy (y + py)
1524 intp "page bias"
1525 (fun () -> conf.pagebias)
1526 (fun v -> conf.pagebias <- v);
1528 intp "scroll step"
1529 (fun () -> conf.scrollstep)
1530 (fun n -> conf.scrollstep <- n);
1532 intp "auto scroll step"
1533 (fun () ->
1534 match state.autoscroll with
1535 | Some step -> step
1536 | _ -> conf.autoscrollstep)
1537 (fun n ->
1538 if state.autoscroll <> None
1539 then state.autoscroll <- Some n;
1540 conf.autoscrollstep <- n);
1542 intp "zoom"
1543 (fun () -> truncate (conf.zoom *. 100.))
1544 (fun v -> setzoom ((float v) /. 100.));
1546 intp "rotation"
1547 (fun () -> conf.angle)
1548 (fun v -> reinit v conf.proportional);
1550 intp "scroll bar width"
1551 (fun () -> state.scrollw)
1552 (fun v ->
1553 state.scrollw <- v;
1554 conf.scrollbw <- v;
1555 reshape conf.winw conf.winh;
1558 intp "scroll handle height"
1559 (fun () -> conf.scrollh)
1560 (fun v -> conf.scrollh <- v;);
1562 intp "thumbnail width"
1563 (fun () -> conf.thumbw)
1564 (fun v ->
1565 conf.thumbw <- min 1920 v;
1566 match mode with
1567 | Birdseye beye ->
1568 leavebirdseye beye false;
1569 enterbirdseye ()
1570 | _ -> ()
1573 colorp "background color"
1574 (fun () -> conf.bgcolor)
1575 (fun v -> conf.bgcolor <- v);
1577 "", 0, Noaction;
1578 "Presentation mode", 0, Noaction;
1580 boolp "scrollbar visible"
1581 (fun () -> conf.scrollbarinpm)
1582 (fun v ->
1583 if v != conf.scrollbarinpm
1584 then (
1585 conf.scrollbarinpm <- v;
1586 if conf.presentation
1587 then (
1588 state.scrollw <- if v then conf.scrollbw else 0;
1589 reshape conf.winw conf.winh;
1594 "", 0, Noaction;
1595 "Pixmap Cache", 0, Noaction;
1597 intp "size (advisory)"
1598 (fun () -> conf.memlimit)
1599 (fun v -> conf.memlimit <- v);
1600 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1604 let tailer =
1605 let trailer =
1606 ("", 0, Noaction)
1607 :: ("Document", 0, Noaction)
1608 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1610 if birdseye
1611 then trailer
1612 else (
1613 ("", 0, Noaction)
1614 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1615 (btos conf.bedefault),
1617 Action (
1618 fun active first qsearch pan ->
1619 conf.bedefault <- not conf.bedefault;
1620 Items (active, first, makeitems (), qsearch, pan, mode);
1622 ) :: trailer
1625 Array.of_list (items @ tailer)
1627 state.text <- "";
1628 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1629 Glut.postRedisplay ();
1632 let enterhelpmode () =
1633 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1634 Glut.postRedisplay ();
1637 let quickbookmark ?title () =
1638 match state.layout with
1639 | [] -> ()
1640 | l :: _ ->
1641 let title =
1642 match title with
1643 | None ->
1644 let sec = Unix.gettimeofday () in
1645 let tm = Unix.localtime sec in
1646 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1647 (l.pageno+1)
1648 tm.Unix.tm_mday
1649 tm.Unix.tm_mon
1650 (tm.Unix.tm_year + 1900)
1651 tm.Unix.tm_hour
1652 tm.Unix.tm_min
1653 | Some title -> title
1655 state.bookmarks <-
1656 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1657 :: state.bookmarks
1660 let doreshape w h =
1661 state.fullscreen <- None;
1662 Glut.reshapeWindow w h;
1665 let writeopen path password =
1666 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1669 let opendoc path password =
1670 invalidate ();
1671 state.path <- path;
1672 state.password <- password;
1673 state.gen <- state.gen + 1;
1674 state.docinfo <- [];
1676 writeopen path password;
1677 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1678 wcmd "geometry" [`i state.w; `i conf.winh];
1681 let viewkeyboard key =
1682 let enttext te =
1683 let mode = state.mode in
1684 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1685 state.text <- "";
1686 enttext ();
1687 Glut.postRedisplay ()
1689 let c = Char.chr key in
1690 match c with
1691 | '\027' -> (* escape *)
1692 if String.length state.text > 0
1693 then (
1694 state.text <- "";
1695 Glut.postRedisplay ();
1697 else raise Quit;
1699 | 'q' ->
1700 raise Quit
1702 | '\008' -> (* backspace *)
1703 let y = getnav ~-1 in
1704 gotoy_and_clear_text y
1706 | 'o' ->
1707 enteroutlinemode ()
1709 | 'u' ->
1710 state.rects <- [];
1711 state.text <- "";
1712 Glut.postRedisplay ()
1714 | '/' | '?' ->
1715 let ondone isforw s =
1716 cbput state.hists.pat s;
1717 state.searchpattern <- s;
1718 search s isforw
1720 let s = String.create 1 in
1721 s.[0] <- c;
1722 enttext (s, "", Some (onhist state.hists.pat),
1723 textentry, ondone (c ='/'))
1725 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1726 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1727 setzoom (conf.zoom +. incr)
1729 | '+' ->
1730 let ondone s =
1731 let n =
1732 try int_of_string s with exc ->
1733 state.text <- Printf.sprintf "bad integer `%s': %s"
1734 s (Printexc.to_string exc);
1735 max_int
1737 if n != max_int
1738 then (
1739 conf.pagebias <- n;
1740 state.text <- "page bias is now " ^ string_of_int n;
1743 enttext ("page bias: ", "", None, intentry, ondone)
1745 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1746 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1747 setzoom (max 0.01 (conf.zoom -. decr))
1749 | '-' ->
1750 let ondone msg = state.text <- msg in
1751 enttext (
1752 "option [acfhilpstvAPRSZ]: ", "", None,
1753 optentry state.mode, ondone
1756 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1757 setzoom 1.0
1759 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1760 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1761 if zoom < 1.0
1762 then setzoom zoom
1764 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1765 togglebirdseye ()
1767 | '0' .. '9' ->
1768 let ondone s =
1769 let n =
1770 try int_of_string s with exc ->
1771 state.text <- Printf.sprintf "bad integer `%s': %s"
1772 s (Printexc.to_string exc);
1775 if n >= 0
1776 then (
1777 addnav ();
1778 cbput state.hists.pag (string_of_int n);
1779 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1782 let pageentry text key =
1783 match Char.unsafe_chr key with
1784 | 'g' -> TEdone text
1785 | _ -> intentry text key
1787 let text = "x" in text.[0] <- c;
1788 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1790 | 'b' ->
1791 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1792 reshape conf.winw conf.winh;
1794 | 'l' ->
1795 conf.hlinks <- not conf.hlinks;
1796 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1797 Glut.postRedisplay ()
1799 | 'a' ->
1800 begin match state.autoscroll with
1801 | Some step ->
1802 conf.autoscrollstep <- step;
1803 state.autoscroll <- None
1804 | None ->
1805 if conf.autoscrollstep = 0
1806 then state.autoscroll <- Some 1
1807 else state.autoscroll <- Some conf.autoscrollstep
1810 | 'P' ->
1811 conf.presentation <- not conf.presentation;
1812 if conf.presentation
1813 then (
1814 if not conf.scrollbarinpm
1815 then state.scrollw <- 0;
1817 else
1818 state.scrollw <- conf.scrollbw;
1820 showtext ' ' ("presentation mode " ^
1821 if conf.presentation then "on" else "off");
1822 state.anchor <- getanchor ();
1823 represent ()
1825 | 'f' ->
1826 begin match state.fullscreen with
1827 | None ->
1828 state.fullscreen <- Some (conf.winw, conf.winh);
1829 Glut.fullScreen ()
1830 | Some (w, h) ->
1831 state.fullscreen <- None;
1832 doreshape w h
1835 | 'g' ->
1836 gotoy_and_clear_text 0
1838 | 'n' ->
1839 search state.searchpattern true
1841 | 'p' | 'N' ->
1842 search state.searchpattern false
1844 | 't' ->
1845 begin match state.layout with
1846 | [] -> ()
1847 | l :: _ ->
1848 gotoy_and_clear_text (getpagey l.pageno)
1851 | ' ' ->
1852 begin match List.rev state.layout with
1853 | [] -> ()
1854 | l :: _ ->
1855 let pageno = min (l.pageno+1) (state.pagecount-1) in
1856 gotoy_and_clear_text (getpagey pageno)
1859 | '\127' -> (* del *)
1860 begin match state.layout with
1861 | [] -> ()
1862 | l :: _ ->
1863 let pageno = max 0 (l.pageno-1) in
1864 gotoy_and_clear_text (getpagey pageno)
1867 | '=' ->
1868 let f (fn, _) l =
1869 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1871 let fn, ln = List.fold_left f (-1, -1) state.layout in
1872 let s =
1873 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1874 let percent =
1875 if maxy <= 0
1876 then 100.
1877 else (100. *. (float state.y /. float maxy)) in
1878 if fn = ln
1879 then
1880 Printf.sprintf "Page %d of %d %.2f%%"
1881 (fn+1) state.pagecount percent
1882 else
1883 Printf.sprintf
1884 "Pages %d-%d of %d %.2f%%"
1885 (fn+1) (ln+1) state.pagecount percent
1887 showtext ' ' s;
1889 | 'w' ->
1890 begin match state.layout with
1891 | [] -> ()
1892 | l :: _ ->
1893 doreshape (l.pagew + state.scrollw) l.pageh;
1894 Glut.postRedisplay ();
1897 | '\'' ->
1898 enterbookmarkmode ()
1900 | 'h' ->
1901 enterhelpmode ()
1903 | 'i' ->
1904 enterinfomode ()
1906 | 'm' ->
1907 let ondone s =
1908 match state.layout with
1909 | l :: _ ->
1910 state.bookmarks <-
1911 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1912 :: state.bookmarks
1913 | _ -> ()
1915 enttext ("bookmark: ", "", None, textentry, ondone)
1917 | '~' ->
1918 quickbookmark ();
1919 showtext ' ' "Quick bookmark added";
1921 | 'z' ->
1922 begin match state.layout with
1923 | l :: _ ->
1924 let rect = getpdimrect l.pagedimno in
1925 let w, h =
1926 if conf.crophack
1927 then
1928 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1929 truncate (1.2 *. (rect.(3) -. rect.(0))))
1930 else
1931 (truncate (rect.(1) -. rect.(0)),
1932 truncate (rect.(3) -. rect.(0)))
1934 if w != 0 && h != 0
1935 then
1936 doreshape (w + state.scrollw) (h + conf.interpagespace)
1938 Glut.postRedisplay ();
1940 | [] -> ()
1943 | '\000' -> (* ctrl-2 *)
1944 let maxw = getmaxw () in
1945 if maxw > 0.0
1946 then setzoom (maxw /. float conf.winw)
1948 | '<' | '>' ->
1949 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1951 | '[' | ']' ->
1952 state.colorscale <-
1953 max 0.0
1954 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1955 Glut.postRedisplay ()
1957 | 'k' ->
1958 begin match state.mode with
1959 | Birdseye beye -> upbirdseye beye
1960 | _ -> gotoy (clamp (-conf.scrollstep))
1963 | 'j' ->
1964 begin match state.mode with
1965 | Birdseye beye -> downbirdseye beye
1966 | _ -> gotoy (clamp conf.scrollstep)
1969 | 'r' ->
1970 state.anchor <- getanchor ();
1971 opendoc state.path state.password
1973 | _ ->
1974 vlog "huh? %d %c" key (Char.chr key);
1977 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1978 let enttext te =
1979 state.mode <- Textentry (te, onleave);
1980 state.text <- "";
1981 enttext ();
1982 Glut.postRedisplay ()
1984 match Char.unsafe_chr key with
1985 | '\008' -> (* backspace *)
1986 let len = String.length text in
1987 if len = 0
1988 then (
1989 onleave Cancel;
1990 Glut.postRedisplay ();
1992 else (
1993 let s = String.sub text 0 (len - 1) in
1994 enttext (c, s, opthist, onkey, ondone)
1997 | '\r' | '\n' ->
1998 ondone text;
1999 onleave Confirm;
2000 Glut.postRedisplay ()
2002 | '\027' -> (* escape *)
2003 begin match opthist with
2004 | None -> ()
2005 | Some (_, onhistcancel) -> onhistcancel ()
2006 end;
2007 onleave Cancel;
2008 Glut.postRedisplay ()
2010 | _ ->
2011 begin match onkey text key with
2012 | TEdone text ->
2013 onleave Confirm;
2014 ondone text;
2015 Glut.postRedisplay ()
2017 | TEcont text ->
2018 enttext (c, text, opthist, onkey, ondone);
2020 | TEstop ->
2021 onleave Cancel;
2022 Glut.postRedisplay ()
2024 | TEswitch te ->
2025 state.mode <- Textentry (te, onleave);
2026 Glut.postRedisplay ()
2027 end;
2030 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2031 match key with
2032 | 27 -> (* escape *)
2033 leavebirdseye beye true
2035 | 12 -> (* ctrl-l *)
2036 let y, h = getpageyh pageno in
2037 let top = (conf.winh - h) / 2 in
2038 gotoy (max 0 (y - top))
2040 | 13 -> (* enter *)
2041 leavebirdseye beye false
2043 | _ ->
2044 viewkeyboard key
2047 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2048 let set active first qsearch =
2049 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2051 let search active pattern incr =
2052 let dosearch re =
2053 let rec loop n =
2054 if n >= Array.length items || n < 0
2055 then None
2056 else
2057 let (s, _, _) = items.(n) in
2059 (try ignore (Str.search_forward re s 0); true
2060 with Not_found -> false)
2061 then Some n
2062 else loop (n + incr)
2064 loop active
2067 let re = Str.regexp_case_fold pattern in
2068 dosearch re
2069 with Failure s ->
2070 state.text <- s;
2071 None
2073 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2074 match key with
2075 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2076 let incr = if key = 18 then -1 else 1 in
2077 let active, first =
2078 match search (active + incr) qsearch incr with
2079 | None ->
2080 state.text <- qsearch ^ " [not found]";
2081 active, first
2082 | Some active ->
2083 state.text <- qsearch;
2084 active, firstof active
2086 set active first qsearch;
2087 Glut.postRedisplay ();
2089 | 8 -> (* backspace *)
2090 let len = String.length qsearch in
2091 if len = 0
2092 then ()
2093 else (
2094 if len = 1
2095 then (
2096 state.text <- "";
2097 set active first "";
2099 else
2100 let qsearch = String.sub qsearch 0 (len - 1) in
2101 let active, first =
2102 match search active qsearch ~-1 with
2103 | None ->
2104 state.text <- qsearch ^ " [not found]";
2105 active, first
2106 | Some active ->
2107 state.text <- qsearch;
2108 active, firstof active
2110 set active first qsearch
2112 Glut.postRedisplay ()
2114 | _ when key >= 32 && key < 127 ->
2115 let pattern = addchar qsearch (Char.chr key) in
2116 let active, first =
2117 match search active pattern 1 with
2118 | None ->
2119 state.text <- pattern ^ " [not found]";
2120 active, first
2121 | Some active ->
2122 state.text <- pattern;
2123 active, firstof active
2125 set active first pattern;
2126 Glut.postRedisplay ()
2128 | 27 -> (* escape *)
2129 state.text <- "";
2130 if String.length qsearch = 0
2131 then (
2132 state.mode <- oldmode;
2134 else (
2135 set active first "";
2137 Glut.postRedisplay ()
2139 | 13 -> (* enter *)
2140 if active >= 0 && active < Array.length items
2141 then (
2142 match items.(active) with
2143 | _, _, Action f ->
2144 state.mode <- f active first qsearch pan
2146 | _, _, Noaction ->
2147 state.text <- "";
2148 state.mode <- oldmode
2150 else (
2151 state.text <- "";
2152 state.mode <- oldmode
2154 Glut.postRedisplay ();
2156 | _ -> dolog "unknown key %d" key
2159 let outlinekeyboard key
2160 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2161 let narrow outlines pattern =
2162 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2163 match reopt with
2164 | None -> None
2165 | Some re ->
2166 let rec fold accu n =
2167 if n = -1
2168 then accu
2169 else
2170 let (s, _, _) as o = outlines.(n) in
2171 let accu =
2172 if (try ignore (Str.search_forward re s 0); true
2173 with Not_found -> false)
2174 then (o :: accu)
2175 else accu
2177 fold accu (n-1)
2179 let matched = fold [] (Array.length outlines - 1) in
2180 if matched = [] then None else Some (Array.of_list matched)
2182 let search active pattern incr =
2183 let dosearch re =
2184 let rec loop n =
2185 if n = Array.length outlines || n = -1
2186 then None
2187 else
2188 let (s, _, _) = outlines.(n) in
2190 (try ignore (Str.search_forward re s 0); true
2191 with Not_found -> false)
2192 then Some n
2193 else loop (n + incr)
2195 loop active
2198 let re = Str.regexp_case_fold pattern in
2199 dosearch re
2200 with Failure s ->
2201 state.text <- s;
2202 None
2204 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2205 match key with
2206 | 27 -> (* escape *)
2207 state.text <- "";
2208 if String.length qsearch = 0
2209 then (
2210 state.mode <- oldmode;
2212 else (
2213 state.mode <- Outline (
2214 allowdel, active, first, outlines, "", pan, oldmode
2217 Glut.postRedisplay ();
2219 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2220 let incr = if key = 18 then -1 else 1 in
2221 let active, first =
2222 match search (active + incr) qsearch incr with
2223 | None ->
2224 state.text <- qsearch ^ " [not found]";
2225 active, first
2226 | Some active ->
2227 state.text <- qsearch;
2228 active, firstof active
2230 state.mode <- Outline (
2231 allowdel, active, first, outlines, qsearch, pan, oldmode
2233 Glut.postRedisplay ();
2235 | 8 -> (* backspace *)
2236 let len = String.length qsearch in
2237 if len = 0
2238 then ()
2239 else (
2240 if len = 1
2241 then (
2242 state.text <- "";
2243 state.mode <- Outline (
2244 allowdel, active, first, outlines, "", pan, oldmode
2247 else
2248 let qsearch = String.sub qsearch 0 (len - 1) in
2249 let active, first =
2250 match search active qsearch ~-1 with
2251 | None ->
2252 state.text <- qsearch ^ " [not found]";
2253 active, first
2254 | Some active ->
2255 state.text <- qsearch;
2256 active, firstof active
2258 state.mode <- Outline (
2259 allowdel, active, first, outlines, qsearch, pan, oldmode
2262 Glut.postRedisplay ()
2264 | 13 -> (* enter *)
2265 if active < Array.length outlines
2266 then (
2267 let (_, _, anchor) = outlines.(active) in
2268 addnav ();
2269 gotoanchor anchor;
2271 state.text <- "";
2272 if allowdel then state.bookmarks <- Array.to_list outlines;
2273 state.mode <- oldmode;
2274 Glut.postRedisplay ();
2276 | _ when key >= 32 && key < 127 ->
2277 let pattern = addchar qsearch (Char.chr key) in
2278 let active, first =
2279 match search active pattern 1 with
2280 | None ->
2281 state.text <- pattern ^ " [not found]";
2282 active, first
2283 | Some active ->
2284 state.text <- pattern;
2285 active, firstof active
2287 state.mode <- Outline (
2288 allowdel, active, first, outlines, pattern, pan, oldmode
2290 Glut.postRedisplay ()
2292 | 14 when not allowdel -> (* ctrl-n *)
2293 if String.length qsearch > 0
2294 then (
2295 let optoutlines = narrow outlines qsearch in
2296 begin match optoutlines with
2297 | None -> state.text <- "can't narrow"
2298 | Some outlines ->
2299 state.mode <- Outline (
2300 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2302 match state.outlines with
2303 | Olist _ -> ()
2304 | Oarray a ->
2305 state.outlines <- Onarrow (qsearch, outlines, a)
2306 | Onarrow (_, _, b) ->
2307 state.outlines <- Onarrow (qsearch, outlines, b)
2308 end;
2310 Glut.postRedisplay ()
2312 | 21 when not allowdel -> (* ctrl-u *)
2313 let outline =
2314 match state.outlines with
2315 | Oarray a -> a
2316 | Olist l ->
2317 let a = Array.of_list (List.rev l) in
2318 state.outlines <- Oarray a;
2320 | Onarrow (_, _, b) ->
2321 state.outlines <- Oarray b;
2322 state.text <- "";
2325 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2326 Glut.postRedisplay ()
2328 | 12 -> (* ctrl-l *)
2329 state.mode <- Outline
2330 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2331 Glut.postRedisplay ()
2333 | 127 when allowdel -> (* delete *)
2334 let len = Array.length outlines - 1 in
2335 if len = 0
2336 then (
2337 state.mode <- View;
2338 state.bookmarks <- [];
2340 else (
2341 let bookmarks = Array.init len
2342 (fun i ->
2343 let i = if i >= active then i + 1 else i in
2344 outlines.(i)
2347 state.mode <-
2348 Outline (
2349 allowdel,
2350 min active (len-1),
2351 min first (len-1),
2352 bookmarks, qsearch,
2354 oldmode
2357 Glut.postRedisplay ()
2359 | _ -> dolog "unknown key %d" key
2362 let keyboard ~key ~x ~y =
2363 ignore x;
2364 ignore y;
2365 if key = 7 (* ctrl-g *)
2366 then
2367 wcmd "interrupt" []
2368 else
2369 match state.mode with
2370 | Outline outline -> outlinekeyboard key outline
2371 | Textentry textentry -> textentrykeyboard key textentry
2372 | Birdseye birdseye -> birdseyekeyboard key birdseye
2373 | View -> viewkeyboard key
2374 | Items items -> itemskeyboard key items
2377 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2378 match key with
2379 | Glut.KEY_UP -> upbirdseye beye
2380 | Glut.KEY_DOWN -> downbirdseye beye
2382 | Glut.KEY_PAGE_UP ->
2383 begin match state.layout with
2384 | l :: _ ->
2385 if l.pagey != 0
2386 then (
2387 state.mode <- Birdseye (
2388 conf, leftx, l.pageno, hooverpageno, anchor
2390 gotopage1 l.pageno 0;
2392 else (
2393 let layout = layout (state.y-conf.winh) conf.winh in
2394 match layout with
2395 | [] -> gotoy (clamp (-conf.winh))
2396 | l :: _ ->
2397 state.mode <- Birdseye (
2398 conf, leftx, l.pageno, hooverpageno, anchor
2400 gotopage1 l.pageno 0
2403 | [] -> gotoy (clamp (-conf.winh))
2404 end;
2406 | Glut.KEY_PAGE_DOWN ->
2407 begin match List.rev state.layout with
2408 | l :: _ ->
2409 let layout = layout (state.y + conf.winh) conf.winh in
2410 begin match layout with
2411 | [] ->
2412 let incr = l.pageh - l.pagevh in
2413 if incr = 0
2414 then (
2415 state.mode <-
2416 Birdseye (
2417 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2419 Glut.postRedisplay ();
2421 else gotoy (clamp (incr + conf.interpagespace*2));
2423 | l :: _ ->
2424 state.mode <-
2425 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2426 gotopage1 l.pageno 0;
2429 | [] -> gotoy (clamp conf.winh)
2430 end;
2432 | Glut.KEY_HOME ->
2433 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2434 gotopage1 0 0
2436 | Glut.KEY_END ->
2437 let pageno = state.pagecount - 1 in
2438 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2439 if not (pagevisible state.layout pageno)
2440 then
2441 let h =
2442 match List.rev state.pdims with
2443 | [] -> conf.winh
2444 | (_, _, h, _) :: _ -> h
2446 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2447 else Glut.postRedisplay ();
2448 | _ -> ()
2451 let setautoscrollspeed step goingdown =
2452 let incr = max 1 ((abs step) / 2) in
2453 let incr = if goingdown then incr else -incr in
2454 let astep = step + incr in
2455 state.autoscroll <- Some astep;
2458 let special ~key ~x ~y =
2459 ignore x;
2460 ignore y;
2461 match state.mode with
2462 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2463 togglebirdseye ()
2465 | Birdseye vals ->
2466 birdseyespecial key vals
2468 | View when key = Glut.KEY_F1 ->
2469 enterhelpmode ()
2471 | View ->
2472 begin match state.autoscroll with
2473 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2474 setautoscrollspeed step (key = Glut.KEY_DOWN)
2476 | _ ->
2477 let y =
2478 match key with
2479 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2480 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2481 | Glut.KEY_DOWN -> clamp conf.scrollstep
2482 | Glut.KEY_PAGE_UP ->
2483 if Glut.getModifiers () land Glut.active_ctrl != 0
2484 then
2485 match state.layout with
2486 | [] -> state.y
2487 | l :: _ -> state.y - l.pagey
2488 else
2489 clamp (-conf.winh)
2490 | Glut.KEY_PAGE_DOWN ->
2491 if Glut.getModifiers () land Glut.active_ctrl != 0
2492 then
2493 match List.rev state.layout with
2494 | [] -> state.y
2495 | l :: _ -> getpagey l.pageno
2496 else
2497 clamp conf.winh
2498 | Glut.KEY_HOME -> addnav (); 0
2499 | Glut.KEY_END ->
2500 addnav ();
2501 state.maxy - (if conf.maxhfit then conf.winh else 0)
2503 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2504 Glut.getModifiers () land Glut.active_alt != 0 ->
2505 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2507 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2508 state.x <- state.x - 10;
2509 state.y
2510 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2511 state.x <- state.x + 10;
2512 state.y
2514 | _ -> state.y
2516 gotoy_and_clear_text y
2519 | Textentry
2520 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2521 let s =
2522 match key with
2523 | Glut.KEY_UP -> action HCprev
2524 | Glut.KEY_DOWN -> action HCnext
2525 | Glut.KEY_HOME -> action HCfirst
2526 | Glut.KEY_END -> action HClast
2527 | _ -> state.text
2529 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2530 Glut.postRedisplay ()
2532 | Textentry _ -> ()
2534 | Items (active, first, items, qsearch, pan, oldmode) ->
2535 let maxrows = maxoutlinerows () in
2536 let itemcount = Array.length items in
2537 let hasaction = function
2538 | (_, _, Noaction) -> false
2539 | _ -> true
2541 let find start incr =
2542 let rec find i =
2543 if i = -1 || i = itemcount
2544 then -1
2545 else (
2546 if hasaction items.(i)
2547 then i
2548 else find (i + incr)
2551 find start
2553 let set active first =
2554 let first = max 0 (min first (itemcount - maxrows)) in
2555 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2557 let navigate incr =
2558 let isvisible first n = n >= first && n - first <= maxrows in
2559 let active, first =
2560 let incr1 = if incr > 0 then 1 else -1 in
2561 if isvisible first active
2562 then
2563 let next =
2564 let next = active + incr in
2565 let next =
2566 if next < 0 || next >= itemcount
2567 then -1
2568 else find next incr1
2570 if next = -1 || abs (active - next) > maxrows
2571 then -1
2572 else next
2574 if next = -1
2575 then
2576 let first = first + incr in
2577 let first = max 0 (min first (itemcount - 1)) in
2578 let next =
2579 let next = active + incr in
2580 let next = max 0 (min next (itemcount - 1)) in
2581 find next ~-incr1
2583 let active = if next = -1 then active else next in
2584 active, first
2585 else
2586 let first = min next first in
2587 next, first
2588 else
2589 let first = first + incr in
2590 let first = max 0 (min first (itemcount - 1)) in
2591 let active =
2592 let next = active + incr in
2593 let next = max 0 (min next (itemcount - 1)) in
2594 let next = find next incr1 in
2595 if next = -1 || abs (active - first) > maxrows
2596 then active
2597 else next
2599 active, first
2601 set active first;
2602 Glut.postRedisplay ()
2604 begin match key with
2605 | Glut.KEY_UP -> navigate ~-1
2606 | Glut.KEY_DOWN -> navigate 1
2607 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2608 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2610 | Glut.KEY_RIGHT ->
2611 state.mode <- Items (
2612 active, first, items, qsearch, min 0 (pan - 1), oldmode
2614 Glut.postRedisplay ()
2616 | Glut.KEY_LEFT ->
2617 state.mode <- Items (
2618 active, first, items, qsearch, min 0 (pan + 1), oldmode
2620 Glut.postRedisplay ()
2622 | Glut.KEY_HOME ->
2623 let active = find 0 1 in
2624 set active 0;
2625 Glut.postRedisplay ()
2627 | Glut.KEY_END ->
2628 let first = max 0 (itemcount - maxrows) in
2629 let active = find (itemcount - 1) ~-1 in
2630 set active first;
2631 Glut.postRedisplay ()
2633 | _ -> ()
2634 end;
2636 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2637 let maxrows = maxoutlinerows () in
2638 let calcfirst first active =
2639 if active > first
2640 then
2641 let rows = active - first in
2642 if rows > maxrows then active - maxrows else first
2643 else active
2645 let navigate incr =
2646 let active = active + incr in
2647 let active = max 0 (min active (Array.length outlines - 1)) in
2648 let first = calcfirst first active in
2649 state.mode <- Outline (
2650 allowdel, active, first, outlines, qsearch, pan, oldmode
2652 Glut.postRedisplay ()
2654 let updownlevel incr =
2655 let len = Array.length outlines in
2656 let (_, curlevel, _) = outlines.(active) in
2657 let rec flow i =
2658 if i = len then i-1 else if i = -1 then 0 else
2659 let (_, l, _) = outlines.(i) in
2660 if l != curlevel then i else flow (i+incr)
2662 let active = flow active in
2663 let first = calcfirst first active in
2664 state.mode <- Outline (
2665 allowdel, active, first, outlines, qsearch, pan, oldmode
2667 Glut.postRedisplay ()
2669 match key with
2670 | Glut.KEY_UP -> navigate ~-1
2671 | Glut.KEY_DOWN -> navigate 1
2672 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2673 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2675 | Glut.KEY_RIGHT ->
2676 if Glut.getModifiers () land Glut.active_ctrl != 0
2677 then (
2678 state.mode <- Outline (
2679 allowdel, active, first, outlines,
2680 qsearch, min 0 (pan + 1), oldmode
2682 Glut.postRedisplay ();
2684 else (
2685 if not allowdel
2686 then updownlevel 1
2689 | Glut.KEY_LEFT ->
2690 if Glut.getModifiers () land Glut.active_ctrl != 0
2691 then (
2692 state.mode <- Outline (
2693 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2695 Glut.postRedisplay ();
2697 else (
2698 if not allowdel
2699 then updownlevel ~-1
2702 | Glut.KEY_HOME ->
2703 state.mode <- Outline (
2704 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2706 Glut.postRedisplay ()
2708 | Glut.KEY_END ->
2709 let active = Array.length outlines - 1 in
2710 let first = max 0 (active - maxrows) in
2711 state.mode <- Outline (
2712 allowdel, active, first, outlines, qsearch, pan, oldmode
2714 Glut.postRedisplay ()
2716 | _ -> ()
2719 let drawplaceholder l =
2720 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2721 GlDraw.rect
2722 (float l.pagex, float l.pagedispy)
2723 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2725 let x = if margin < 0 then -margin else l.pagex
2726 and y = l.pagedispy + 13 in
2727 GlDraw.color (0.0, 0.0, 0.0);
2728 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2731 let now () = Unix.gettimeofday ();;
2733 let drawpage l =
2734 let color =
2735 match state.mode with
2736 | Textentry _ -> scalecolor 0.4
2737 | View | Outline _ | Items _ -> scalecolor 1.0
2738 | Birdseye (_, _, pageno, hooverpageno, _) ->
2739 if l.pageno = hooverpageno
2740 then scalecolor 0.9
2741 else (
2742 if l.pageno = pageno
2743 then scalecolor 1.0
2744 else scalecolor 0.8
2747 GlDraw.color color;
2748 begin match getopaque l.pageno with
2749 | Some (opaque, _) when validopaque opaque ->
2750 let a = now () in
2751 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2752 let b = now () in
2753 let d = b-.a in
2754 vlog "draw %d %f sec" l.pageno d;
2756 | _ ->
2757 drawplaceholder l;
2758 end;
2761 let scrollph y =
2762 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2763 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2764 let sh = float conf.winh /. sh in
2765 let sh = max sh (float conf.scrollh) in
2767 let percent =
2768 if state.y = state.maxy
2769 then 1.0
2770 else float y /. float maxy
2772 let position = (float conf.winh -. sh) *. percent in
2774 let position =
2775 if position +. sh > float conf.winh
2776 then float conf.winh -. sh
2777 else position
2779 position, sh;
2782 let scrollindicator () =
2783 GlDraw.color (0.64 , 0.64, 0.64);
2784 GlDraw.rect
2785 (float (conf.winw - state.scrollw), 0.)
2786 (float conf.winw, float conf.winh)
2788 GlDraw.color (0.0, 0.0, 0.0);
2790 let position, sh = scrollph state.y in
2791 GlDraw.rect
2792 (float (conf.winw - state.scrollw), position)
2793 (float conf.winw, position +. sh)
2797 let showsel margin =
2798 match state.mstate with
2799 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2802 | Msel ((x0, y0), (x1, y1)) ->
2803 let rec loop = function
2804 | l :: ls ->
2805 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2806 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2807 then
2808 match getopaque l.pageno with
2809 | Some (opaque, _) when validopaque opaque ->
2810 let oy = -l.pagey + l.pagedispy in
2811 seltext opaque
2812 (x0 - margin - state.x, y0,
2813 x1 - margin - state.x, y1) oy;
2815 | _ -> ()
2816 else loop ls
2817 | [] -> ()
2819 loop state.layout
2822 let showrects () =
2823 let panx = float state.x in
2824 Gl.enable `blend;
2825 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2826 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2827 List.iter
2828 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2829 List.iter (fun l ->
2830 if l.pageno = pageno
2831 then (
2832 let d = float (l.pagedispy - l.pagey) in
2833 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2834 GlDraw.begins `quads;
2836 GlDraw.vertex2 (x0+.panx, y0+.d);
2837 GlDraw.vertex2 (x1+.panx, y1+.d);
2838 GlDraw.vertex2 (x2+.panx, y2+.d);
2839 GlDraw.vertex2 (x3+.panx, y3+.d);
2841 GlDraw.ends ();
2843 ) state.layout
2844 ) state.rects
2846 Gl.disable `blend;
2849 let showstrings trusted active first pan strings =
2850 Gl.enable `blend;
2851 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2852 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2853 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2854 GlDraw.color (1., 1., 1.);
2855 Gl.enable `texture_2d;
2857 let wx = measurestr 15 "w" in
2858 let tabx = 30.0*.wx +. float (pan*15) in
2859 let rec loop row =
2860 if row = Array.length strings || (row - first) * 16 > conf.winh
2861 then ()
2862 else (
2863 let (s, level, _) = strings.(row) in
2864 let y = (row - first) * 16 in
2865 let x = 5 + 15*(max 0 (level+pan)) in
2866 if row = active
2867 then (
2868 Gl.disable `texture_2d;
2869 GlDraw.polygon_mode `both `line;
2870 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2871 GlDraw.rect (0., float (y + 1))
2872 (float (conf.winw - 1), float (y + 18));
2873 GlDraw.polygon_mode `both `fill;
2874 GlDraw.color (1., 1., 1.);
2875 Gl.enable `texture_2d;
2878 let drawtabularstring x s =
2879 let _ =
2880 if trusted
2881 then
2882 let tabpos = try String.index s '\t' with Not_found -> -1 in
2883 if tabpos > 0
2884 then
2885 let len = String.length s - tabpos - 1 in
2886 let s1 = String.sub s 0 tabpos
2887 and s2 = String.sub s (tabpos + 1) len in
2888 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2889 let x = truncate (max xx tabx) in
2890 drawstring1 15 x (y + 16) s2
2891 else
2892 drawstring1 15 x (y + 16) s
2893 else
2894 drawstring1 15 x (y + 16) s
2898 drawtabularstring (x + pan*15) s;
2899 loop (row+1)
2902 loop first;
2903 Gl.disable `blend;
2904 Gl.disable `texture_2d;
2907 let showoutline (_, active, first, outlines, _, pan, _) =
2908 showstrings false active first pan outlines;
2911 let showitems (active, first, items, _, pan, _) =
2912 showstrings true active first pan items;
2915 let display () =
2916 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2917 GlDraw.viewport margin 0 state.w conf.winh;
2918 pagematrix ();
2919 GlClear.color (scalecolor2 conf.bgcolor);
2920 GlClear.clear [`color];
2921 if conf.zoom > 1.0
2922 then (
2923 Gl.enable `scissor_test;
2924 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2926 List.iter drawpage state.layout;
2927 if conf.zoom > 1.0
2928 then
2929 Gl.disable `scissor_test
2931 if state.x != 0
2932 then (
2933 let x = -.float state.x in
2934 GlMat.translate ~x ();
2936 showrects ();
2937 showsel margin;
2938 GlDraw.viewport 0 0 conf.winw conf.winh;
2939 winmatrix ();
2940 scrollindicator ();
2941 begin match state.mode with
2942 | Items items -> showitems items
2943 | Outline outline -> showoutline outline
2944 | _ -> ()
2945 end;
2946 enttext ();
2947 Glut.swapBuffers ();
2950 let getunder x y =
2951 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2952 let x = x - margin - state.x in
2953 let rec f = function
2954 | l :: rest ->
2955 begin match getopaque l.pageno with
2956 | Some (opaque, _) when validopaque opaque ->
2957 let y = y - l.pagedispy in
2958 if y > 0
2959 then
2960 let y = l.pagey + y in
2961 let x = x - l.pagex in
2962 match whatsunder opaque x y with
2963 | Unone -> f rest
2964 | under -> under
2965 else
2966 f rest
2967 | _ ->
2968 f rest
2970 | [] -> Unone
2972 f state.layout
2975 let viewmouse button bstate x y =
2976 match button with
2977 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2978 if Glut.getModifiers () land Glut.active_ctrl != 0
2979 then (
2980 match state.mstate with
2981 | Mzoom (oldn, i) ->
2982 if oldn = n
2983 then (
2984 if i = 2
2985 then
2986 let incr =
2987 match n with
2988 | 4 ->
2989 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2990 | _ ->
2991 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2993 let zoom = conf.zoom +. incr in
2994 setzoom zoom;
2995 state.mstate <- Mzoom (n, 0);
2996 else
2997 state.mstate <- Mzoom (n, i+1);
2999 else state.mstate <- Mzoom (n, 0)
3001 | _ -> state.mstate <- Mzoom (n, 0)
3003 else (
3004 match state.autoscroll with
3005 | Some step -> setautoscrollspeed step (n=4)
3006 | None ->
3007 let incr =
3008 if n = 3
3009 then -conf.scrollstep
3010 else conf.scrollstep
3012 let incr = incr * 2 in
3013 let y = clamp incr in
3014 gotoy_and_clear_text y
3017 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3018 if bstate = Glut.DOWN
3019 then (
3020 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3021 state.mstate <- Mpan (x, y)
3023 else
3024 state.mstate <- Mnone
3026 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3027 if bstate = Glut.DOWN
3028 then
3029 let position, sh = scrollph state.y in
3030 if y > truncate position && y < truncate (position +. sh)
3031 then
3032 state.mstate <- Mscroll
3033 else
3034 let percent = float y /. float conf.winh in
3035 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3036 gotoy desty;
3037 state.mstate <- Mscroll
3038 else
3039 state.mstate <- Mnone
3041 | Glut.LEFT_BUTTON ->
3042 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3043 begin match dest with
3044 | Ulinkgoto (pageno, top) ->
3045 if pageno >= 0
3046 then (
3047 addnav ();
3048 gotopage1 pageno top;
3051 | Ulinkuri s ->
3052 print_endline s
3054 | Unone when bstate = Glut.DOWN ->
3055 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3056 state.mstate <- Mpan (x, y);
3058 | Unone | Utext _ ->
3059 if bstate = Glut.DOWN
3060 then (
3061 if conf.angle mod 360 = 0
3062 then (
3063 state.mstate <- Msel ((x, y), (x, y));
3064 Glut.postRedisplay ()
3067 else (
3068 match state.mstate with
3069 | Mnone -> ()
3071 | Mzoom _ | Mscroll ->
3072 state.mstate <- Mnone
3074 | Mpan _ ->
3075 Glut.setCursor Glut.CURSOR_INHERIT;
3076 state.mstate <- Mnone
3078 | Msel ((_, y0), (_, y1)) ->
3079 let f l =
3080 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3081 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3082 then
3083 match getopaque l.pageno with
3084 | Some (opaque, _) when validopaque opaque ->
3085 copysel opaque
3086 | _ -> ()
3088 List.iter f state.layout;
3089 copysel ""; (* ugly *)
3090 Glut.setCursor Glut.CURSOR_INHERIT;
3091 state.mstate <- Mnone;
3095 | _ -> ()
3098 let birdseyemouse button bstate x y
3099 (conf, leftx, _, hooverpageno, anchor) =
3100 match button with
3101 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3102 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3103 let rec loop = function
3104 | [] -> ()
3105 | l :: rest ->
3106 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3107 && x > margin && x < margin + l.pagew
3108 then (
3109 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3111 else loop rest
3113 loop state.layout
3114 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3115 | _ -> ()
3118 let mouse bstate button x y =
3119 match state.mode with
3120 | View -> viewmouse button bstate x y
3121 | Birdseye beye -> birdseyemouse button bstate x y beye
3122 | Textentry _ | Outline _ | Items _ -> ()
3125 let mouse ~button ~state ~x ~y = mouse state button x y;;
3127 let motion ~x ~y =
3128 match state.mode with
3129 | Outline _ -> ()
3130 | _ ->
3131 match state.mstate with
3132 | Mzoom _ | Mnone -> ()
3134 | Mpan (x0, y0) ->
3135 let dx = x - x0
3136 and dy = y0 - y in
3137 state.mstate <- Mpan (x, y);
3138 if conf.zoom > 1.0 then state.x <- state.x + dx;
3139 let y = clamp dy in
3140 gotoy_and_clear_text y
3142 | Msel (a, _) ->
3143 state.mstate <- Msel (a, (x, y));
3144 Glut.postRedisplay ()
3146 | Mscroll ->
3147 let y = min conf.winh (max 0 y) in
3148 let percent = float y /. float conf.winh in
3149 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3150 gotoy_and_clear_text y
3153 let pmotion ~x ~y =
3154 match state.mode with
3155 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3156 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3157 let rec loop = function
3158 | [] ->
3159 if hooverpageno != -1
3160 then (
3161 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3162 Glut.postRedisplay ();
3164 | l :: rest ->
3165 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3166 && x > margin && x < margin + l.pagew
3167 then (
3168 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3169 Glut.postRedisplay ();
3171 else loop rest
3173 loop state.layout
3175 | Outline _ | Items _ | Textentry _ -> ()
3176 | View ->
3177 match state.mstate with
3178 | Mnone ->
3179 begin match getunder x y with
3180 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3181 | Ulinkuri uri ->
3182 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3183 Glut.setCursor Glut.CURSOR_INFO
3184 | Ulinkgoto (page, _) ->
3185 if conf.underinfo
3186 then showtext 'p' ("age: " ^ string_of_int page);
3187 Glut.setCursor Glut.CURSOR_INFO
3188 | Utext s ->
3189 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3190 Glut.setCursor Glut.CURSOR_TEXT
3193 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3198 module State =
3199 struct
3200 open Parser
3202 let fontpath = ref "";;
3204 let home =
3206 match Sys.os_type with
3207 | "Win32" -> Sys.getenv "HOMEPATH"
3208 | _ -> Sys.getenv "HOME"
3209 with exn ->
3210 prerr_endline
3211 ("Can not determine home directory location: " ^
3212 Printexc.to_string exn);
3216 let config_of c attrs =
3217 let apply c k v =
3219 match k with
3220 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3221 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3222 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3223 | "preload" -> { c with preload = bool_of_string v }
3224 | "page-bias" -> { c with pagebias = int_of_string v }
3225 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3226 | "auto-scroll-step" ->
3227 { c with autoscrollstep = max 0 (int_of_string v) }
3228 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3229 | "crop-hack" -> { c with crophack = bool_of_string v }
3230 | "throttle" -> { c with showall = bool_of_string v }
3231 | "highlight-links" -> { c with hlinks = bool_of_string v }
3232 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3233 | "vertical-margin" ->
3234 { c with interpagespace = max 0 (int_of_string v) }
3235 | "zoom" ->
3236 let zoom = float_of_string v /. 100. in
3237 let zoom = max 0.01 zoom in
3238 { c with zoom = zoom }
3239 | "presentation" -> { c with presentation = bool_of_string v }
3240 | "rotation-angle" -> { c with angle = int_of_string v }
3241 | "width" -> { c with winw = max 20 (int_of_string v) }
3242 | "height" -> { c with winh = max 20 (int_of_string v) }
3243 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3244 | "proportional-display" -> { c with proportional = bool_of_string v }
3245 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3246 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3247 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3248 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3249 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3250 | "persistent-location" -> { c with jumpback = bool_of_string v }
3251 | "background-color" -> { c with bgcolor = color_of_string v }
3252 | "scrollbar-in-presentation" ->
3253 { c with scrollbarinpm = bool_of_string v }
3254 | _ -> c
3255 with exn ->
3256 prerr_endline ("Error processing attribute (`" ^
3257 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3260 let rec fold c = function
3261 | [] -> c
3262 | (k, v) :: rest ->
3263 let c = apply c k v in
3264 fold c rest
3266 fold c attrs;
3269 let fromstring f pos n v d =
3270 try f v
3271 with exn ->
3272 dolog "Error processing attribute (%S=%S) at %d\n%s"
3273 n v pos (Printexc.to_string exn)
3278 let bookmark_of attrs =
3279 let rec fold title page rely = function
3280 | ("title", v) :: rest -> fold v page rely rest
3281 | ("page", v) :: rest -> fold title v rely rest
3282 | ("rely", v) :: rest -> fold title page v rest
3283 | _ :: rest -> fold title page rely rest
3284 | [] -> title, page, rely
3286 fold "invalid" "0" "0" attrs
3289 let doc_of attrs =
3290 let rec fold path page rely pan = function
3291 | ("path", v) :: rest -> fold v page rely pan rest
3292 | ("page", v) :: rest -> fold path v rely pan rest
3293 | ("rely", v) :: rest -> fold path page v pan rest
3294 | ("pan", v) :: rest -> fold path page rely v rest
3295 | _ :: rest -> fold path page rely pan rest
3296 | [] -> path, page, rely, pan
3298 fold "" "0" "0" "0" attrs
3301 let setconf dst src =
3302 dst.scrollbw <- src.scrollbw;
3303 dst.scrollh <- src.scrollh;
3304 dst.icase <- src.icase;
3305 dst.preload <- src.preload;
3306 dst.pagebias <- src.pagebias;
3307 dst.verbose <- src.verbose;
3308 dst.scrollstep <- src.scrollstep;
3309 dst.maxhfit <- src.maxhfit;
3310 dst.crophack <- src.crophack;
3311 dst.autoscrollstep <- src.autoscrollstep;
3312 dst.showall <- src.showall;
3313 dst.hlinks <- src.hlinks;
3314 dst.underinfo <- src.underinfo;
3315 dst.interpagespace <- src.interpagespace;
3316 dst.zoom <- src.zoom;
3317 dst.presentation <- src.presentation;
3318 dst.angle <- src.angle;
3319 dst.winw <- src.winw;
3320 dst.winh <- src.winh;
3321 dst.savebmarks <- src.savebmarks;
3322 dst.memlimit <- src.memlimit;
3323 dst.proportional <- src.proportional;
3324 dst.texcount <- src.texcount;
3325 dst.sliceheight <- src.sliceheight;
3326 dst.blockwidth <- src.blockwidth;
3327 dst.thumbw <- src.thumbw;
3328 dst.jumpback <- src.jumpback;
3329 dst.bgcolor <- src.bgcolor;
3330 dst.scrollbarinpm <- src.scrollbarinpm;
3333 let unent s =
3334 let l = String.length s in
3335 let b = Buffer.create l in
3336 unent b s 0 l;
3337 Buffer.contents b;
3340 let get s =
3341 let h = Hashtbl.create 10 in
3342 let dc = { defconf with angle = defconf.angle } in
3343 let rec toplevel v t spos _ =
3344 match t with
3345 | Vdata | Vcdata | Vend -> v
3346 | Vopen ("llppconfig", _, closed) ->
3347 if closed
3348 then v
3349 else { v with f = llppconfig }
3350 | Vopen _ ->
3351 error "unexpected subelement at top level" s spos
3352 | Vclose _ -> error "unexpected close at top level" s spos
3354 and llppconfig v t spos _ =
3355 match t with
3356 | Vdata | Vcdata -> v
3357 | Vend -> error "unexpected end of input in llppconfig" s spos
3358 | Vopen ("defaults", attrs, closed) ->
3359 let c = config_of dc attrs in
3360 setconf dc c;
3361 if closed
3362 then v
3363 else { v with f = skip "defaults" (fun () -> v) }
3365 | Vopen ("ui-font", _, closed) ->
3366 if closed
3367 then v
3368 else { v with f = uifont (Buffer.create 10) }
3370 | Vopen ("doc", attrs, closed) ->
3371 let pathent, spage, srely, span = doc_of attrs in
3372 let path = unent pathent
3373 and pageno = fromstring int_of_string spos "page" spage 0
3374 and rely = fromstring float_of_string spos "rely" srely 0.0
3375 and pan = fromstring int_of_string spos "pan" span 0 in
3376 let c = config_of dc attrs in
3377 let anchor = (pageno, rely) in
3378 if closed
3379 then (Hashtbl.add h path (c, [], pan, anchor); v)
3380 else { v with f = doc path pan anchor c [] }
3382 | Vopen _ ->
3383 error "unexpected subelement in llppconfig" s spos
3385 | Vclose "llppconfig" -> { v with f = toplevel }
3386 | Vclose _ -> error "unexpected close in llppconfig" s spos
3388 and uifont b v t spos epos =
3389 match t with
3390 | Vdata | Vcdata ->
3391 Buffer.add_substring b s spos (epos - spos);
3393 | Vopen (_, _, _) ->
3394 error "unexpected subelement in ui-font" s spos
3395 | Vclose "ui-font" ->
3396 if String.length !fontpath = 0
3397 then fontpath := Buffer.contents b;
3398 { v with f = llppconfig }
3399 | Vclose _ -> error "unexpected close in ui-font" s spos
3400 | Vend -> error "unexpected end of input in ui-font" s spos
3402 and doc path pan anchor c bookmarks v t spos _ =
3403 match t with
3404 | Vdata | Vcdata -> v
3405 | Vend -> error "unexpected end of input in doc" s spos
3406 | Vopen ("bookmarks", _, closed) ->
3407 if closed
3408 then v
3409 else { v with f = pbookmarks path pan anchor c bookmarks }
3411 | Vopen (_, _, _) ->
3412 error "unexpected subelement in doc" s spos
3414 | Vclose "doc" ->
3415 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3416 { v with f = llppconfig }
3418 | Vclose _ -> error "unexpected close in doc" s spos
3420 and pbookmarks path pan anchor c bookmarks v t spos _ =
3421 match t with
3422 | Vdata | Vcdata -> v
3423 | Vend -> error "unexpected end of input in bookmarks" s spos
3424 | Vopen ("item", attrs, closed) ->
3425 let titleent, spage, srely = bookmark_of attrs in
3426 let page = fromstring int_of_string spos "page" spage 0
3427 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3428 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3429 if closed
3430 then { v with f = pbookmarks path pan anchor c bookmarks }
3431 else
3432 let f () = v in
3433 { v with f = skip "item" f }
3435 | Vopen _ ->
3436 error "unexpected subelement in bookmarks" s spos
3438 | Vclose "bookmarks" ->
3439 { v with f = doc path pan anchor c bookmarks }
3441 | Vclose _ -> error "unexpected close in bookmarks" s spos
3443 and skip tag f v t spos _ =
3444 match t with
3445 | Vdata | Vcdata -> v
3446 | Vend ->
3447 error ("unexpected end of input in skipped " ^ tag) s spos
3448 | Vopen (tag', _, closed) ->
3449 if closed
3450 then v
3451 else
3452 let f' () = { v with f = skip tag f } in
3453 { v with f = skip tag' f' }
3454 | Vclose ctag ->
3455 if tag = ctag
3456 then f ()
3457 else error ("unexpected close in skipped " ^ tag) s spos
3460 parse { f = toplevel; accu = () } s;
3461 h, dc;
3464 let do_load f ic =
3466 let len = in_channel_length ic in
3467 let s = String.create len in
3468 really_input ic s 0 len;
3469 f s;
3470 with
3471 | Parse_error (msg, s, pos) ->
3472 let subs = subs s pos in
3473 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3474 failwith ("parse error: " ^ s)
3476 | exn ->
3477 failwith ("config load error: " ^ Printexc.to_string exn)
3480 let defconfpath =
3481 let dir =
3483 let dir = Filename.concat home ".config" in
3484 if Sys.is_directory dir then dir else home
3485 with _ -> home
3487 Filename.concat dir "llpp.conf"
3490 let confpath = ref defconfpath;;
3492 let load1 f =
3493 if Sys.file_exists !confpath
3494 then
3495 match
3496 (try Some (open_in_bin !confpath)
3497 with exn ->
3498 prerr_endline
3499 ("Error opening configuation file `" ^ !confpath ^ "': " ^
3500 Printexc.to_string exn);
3501 None
3503 with
3504 | Some ic ->
3505 begin try
3506 f (do_load get ic)
3507 with exn ->
3508 prerr_endline
3509 ("Error loading configuation from `" ^ !confpath ^ "': " ^
3510 Printexc.to_string exn);
3511 end;
3512 close_in ic;
3514 | None -> ()
3515 else
3516 f (Hashtbl.create 0, defconf)
3519 let load () =
3520 let f (h, dc) =
3521 let pc, pb, px, pa =
3523 Hashtbl.find h (Filename.basename state.path)
3524 with Not_found -> dc, [], 0, (0, 0.0)
3526 setconf defconf dc;
3527 setconf conf pc;
3528 state.bookmarks <- pb;
3529 state.x <- px;
3530 state.scrollw <- conf.scrollbw;
3531 if conf.jumpback
3532 then state.anchor <- pa;
3533 cbput state.hists.nav pa;
3535 load1 f
3538 let add_attrs bb always dc c =
3539 let ob s a b =
3540 if always || a != b
3541 then Printf.bprintf bb "\n %s='%b'" s a
3542 and oi s a b =
3543 if always || a != b
3544 then Printf.bprintf bb "\n %s='%d'" s a
3545 and oz s a b =
3546 if always || a <> b
3547 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
3548 and oc s a b =
3549 if always || a <> b
3550 then
3551 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3553 let w, h =
3554 if always
3555 then dc.winw, dc.winh
3556 else
3557 match state.fullscreen with
3558 | Some wh -> wh
3559 | None -> c.winw, c.winh
3561 let zoom, presentation, interpagespace, showall=
3562 if always
3563 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3564 else
3565 match state.mode with
3566 | Birdseye (bc, _, _, _, _) ->
3567 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3568 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3570 oi "width" w dc.winw;
3571 oi "height" h dc.winh;
3572 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3573 oi "scroll-handle-height" c.scrollh dc.scrollh;
3574 ob "case-insensitive-search" c.icase dc.icase;
3575 ob "preload" c.preload dc.preload;
3576 oi "page-bias" c.pagebias dc.pagebias;
3577 oi "scroll-step" c.scrollstep dc.scrollstep;
3578 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3579 ob "max-height-fit" c.maxhfit dc.maxhfit;
3580 ob "crop-hack" c.crophack dc.crophack;
3581 ob "throttle" showall dc.showall;
3582 ob "highlight-links" c.hlinks dc.hlinks;
3583 ob "under-cursor-info" c.underinfo dc.underinfo;
3584 oi "vertical-margin" interpagespace dc.interpagespace;
3585 oz "zoom" zoom dc.zoom;
3586 ob "presentation" presentation dc.presentation;
3587 oi "rotation-angle" c.angle dc.angle;
3588 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3589 ob "proportional-display" c.proportional dc.proportional;
3590 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3591 oi "tex-count" c.texcount dc.texcount;
3592 oi "slice-height" c.sliceheight dc.sliceheight;
3593 oi "block-width" c.blockwidth dc.blockwidth;
3594 oi "thumbnail-width" c.thumbw dc.thumbw;
3595 ob "persistent-location" c.jumpback dc.jumpback;
3596 oc "background-color" c.bgcolor dc.bgcolor;
3597 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3600 let save () =
3601 let bb = Buffer.create 32768 in
3602 let f (h, dc) =
3603 let dc = if conf.bedefault then conf else dc in
3604 Buffer.add_string bb "<llppconfig>\n";
3606 if String.length !fontpath > 0
3607 then Printf.bprintf bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3609 Buffer.add_string bb "<defaults ";
3610 add_attrs bb true dc dc;
3611 Buffer.add_string bb "/>\n";
3613 let adddoc path pan anchor c bookmarks =
3614 if bookmarks == [] && c = dc && anchor = emptyanchor
3615 then ()
3616 else (
3617 Printf.bprintf bb "<doc path='%s'"
3618 (enent path 0 (String.length path));
3620 if anchor <> emptyanchor
3621 then (
3622 let n, y = anchor in
3623 Printf.bprintf bb " page='%d'" n;
3624 if y > 1e-6
3625 then
3626 Printf.bprintf bb " rely='%f'" y
3630 if pan != 0
3631 then Printf.bprintf bb " pan='%d'" pan;
3633 add_attrs bb false dc c;
3635 begin match bookmarks with
3636 | [] -> Buffer.add_string bb "/>\n"
3637 | _ ->
3638 Buffer.add_string bb ">\n<bookmarks>\n";
3639 List.iter (fun (title, _level, (page, rely)) ->
3640 Printf.bprintf bb
3641 "<item title='%s' page='%d'"
3642 (enent title 0 (String.length title))
3643 page
3645 if rely > 1e-6
3646 then
3647 Printf.bprintf bb " rely='%f'" rely
3649 Buffer.add_string bb "/>\n";
3650 ) bookmarks;
3651 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3652 end;
3656 let pan =
3657 match state.mode with
3658 | Birdseye (_, pan, _, _, _) -> pan
3659 | _ -> state.x
3661 let basename = Filename.basename state.path in
3662 adddoc basename pan (getanchor ())
3663 { conf with
3664 autoscrollstep =
3665 match state.autoscroll with
3666 | Some step -> step
3667 | None -> conf.autoscrollstep }
3668 (if conf.savebmarks then state.bookmarks else []);
3670 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3671 if basename <> path
3672 then adddoc path x y c bookmarks
3673 ) h;
3674 Buffer.add_string bb "</llppconfig>";
3676 load1 f;
3677 if Buffer.length bb > 0
3678 then
3680 let tmp = !confpath ^ ".tmp" in
3681 let oc = open_out_bin tmp in
3682 Buffer.output_buffer oc bb;
3683 close_out oc;
3684 Sys.rename tmp !confpath;
3685 with exn ->
3686 prerr_endline
3687 ("error while saving configuration: " ^ Printexc.to_string exn)
3689 end;;
3691 let () =
3692 Arg.parse
3693 (Arg.align
3694 [("-p", Arg.String (fun s -> state.password <- s) ,
3695 "<password> Set password");
3697 ("-f", Arg.String (fun s -> State.fontpath := s),
3698 "<path> Set path to the user interface font");
3700 ("-c", Arg.String (fun s -> State.confpath := s),
3701 "<path> Set path to the configuration file");
3703 ("-v", Arg.Unit (fun () ->
3704 Printf.printf
3705 "%s\nconfiguration path: %s\n"
3706 Help.version
3707 State.defconfpath
3709 exit 0), " Print version and exit");
3712 (fun s -> state.path <- s)
3713 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3715 if String.length state.path = 0
3716 then (prerr_endline "file name missing"; exit 1);
3718 State.load ();
3720 let _ = Glut.init Sys.argv in
3721 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3722 let () = Glut.initWindowSize conf.winw conf.winh in
3723 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3725 let csock, ssock =
3726 if Sys.os_type = "Unix"
3727 then
3728 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3729 else
3730 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3731 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3732 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3733 Unix.bind sock addr;
3734 Unix.listen sock 1;
3735 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3736 Unix.connect csock addr;
3737 let ssock, _ = Unix.accept sock in
3738 Unix.close sock;
3739 let opts sock =
3740 Unix.setsockopt sock Unix.TCP_NODELAY true;
3741 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3743 opts ssock;
3744 opts csock;
3745 ssock, csock
3748 let () = Glut.displayFunc display in
3749 let () = Glut.reshapeFunc reshape in
3750 let () = Glut.keyboardFunc keyboard in
3751 let () = Glut.specialFunc special in
3752 let () = Glut.idleFunc (Some idle) in
3753 let () = Glut.mouseFunc mouse in
3754 let () = Glut.motionFunc motion in
3755 let () = Glut.passiveMotionFunc pmotion in
3757 init ssock (
3758 conf.angle, conf.proportional, conf.texcount,
3759 conf.sliceheight, conf.blockwidth, !State.fontpath
3761 state.csock <- csock;
3762 state.ssock <- ssock;
3763 state.text <- "Opening " ^ state.path;
3764 writeopen state.path state.password;
3766 let rec handlelablglutbug () =
3768 Glut.mainLoop ();
3769 with
3770 | Glut.BadEnum "key in special_of_int" ->
3771 showtext '!' " LablGlut bug: special key not recognized";
3772 handlelablglutbug ()
3774 | Quit->
3775 if Sys.os_type <> "Unix"
3776 then Unix.shutdown ssock Unix.SHUTDOWN_ALL;
3777 State.save ()
3779 handlelablglutbug ();