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