Simplify
[llpp.git] / main.ml
blobd835de7a2b4f4c15dcf461b31bd1a2a00a917da7
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;
872 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
874 let rec gc () =
875 if state.memused <= conf.memlimit || Queue.is_empty state.pagelru
876 then ()
877 else
878 let opaque = Queue.pop state.pagelru in
879 match findpageforopaque opaque with
880 | None -> failwith "wtf!"
881 | Some (pagekey, size) ->
882 wcmd "free" [`s opaque];
883 Hashtbl.remove state.pagemap pagekey;
884 state.memused <- state.memused - size;
885 gc ()
887 gc ();
889 Queue.push p state.pagelru;
890 state.rendering <- false;
892 begin match state.throttle with
893 | None ->
894 if pagevisible state.layout n
895 then Glut.postRedisplay ();
896 let allvisible = loadlayout state.layout in
897 if allvisible then preload ()
899 | Some layout ->
900 match layout with
901 | [] -> ()
902 | l :: _ ->
903 let y = getpagey l.pageno + l.pagey in
904 gotoy y
907 | 'l' ->
908 let pdim =
909 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
911 state.pdims <- pdim :: state.pdims
913 | 'o' ->
914 let (l, n, t, h, pos) =
915 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
917 let s = String.sub cmd pos (String.length cmd - pos) in
918 let outline = (s, l, (n, float t /. float h)) in
919 let outlines =
920 match state.outlines with
921 | Olist outlines -> Olist (outline :: outlines)
922 | Oarray _ -> Olist [outline]
923 | Onarrow _ -> Olist [outline]
925 state.outlines <- outlines
928 | 'i' ->
929 if String.length cmd > 1 && cmd.[1] = 'e'
930 then
931 state.docinfo <- List.rev state.docinfo
932 else
933 let s = Scanf.sscanf cmd "i %n"
934 (fun n -> String.sub cmd n (String.length cmd - n))
936 state.docinfo <- (1, s) :: state.docinfo
938 | _ ->
939 dolog "unknown cmd `%S'" cmd
942 let now = Unix.gettimeofday;;
944 let idle () =
945 if state.deadline == nan then state.deadline <- now ();
946 let rec loop delay =
947 let timeout =
948 if delay > 0.0
949 then max 0.0 (state.deadline -. now ())
950 else 0.0
952 let r, _, _ = Unix.select [state.csock] [] [] timeout in
953 begin match r with
954 | [] ->
955 begin match state.autoscroll with
956 | Some step when step != 0 ->
957 let y = state.y + step in
958 let y =
959 if y < 0
960 then state.maxy
961 else if y >= state.maxy then 0 else y
963 gotoy y;
964 if state.mode = View
965 then state.text <- "";
966 state.deadline <- state.deadline +. 0.005;
968 | _ ->
969 state.deadline <- state.deadline +. delay;
970 end;
972 | _ ->
973 let cmd = readcmd state.csock in
974 act cmd;
975 loop 0.0
976 end;
977 in loop 0.007
980 let onhist cb =
981 let rc = cb.rc in
982 let action = function
983 | HCprev -> cbget cb ~-1
984 | HCnext -> cbget cb 1
985 | HCfirst -> cbget cb ~-(cb.rc)
986 | HClast -> cbget cb (cb.len - 1 - cb.rc)
987 and cancel () = cb.rc <- rc
988 in (action, cancel)
991 let search pattern forward =
992 if String.length pattern > 0
993 then
994 let pn, py =
995 match state.layout with
996 | [] -> 0, 0
997 | l :: _ ->
998 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1000 let cmd =
1001 let b = makecmd "search"
1002 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1004 Buffer.add_char b ',';
1005 Buffer.add_string b pattern;
1006 Buffer.add_char b '\000';
1007 Buffer.contents b;
1009 writecmd state.csock cmd;
1012 let intentry text key =
1013 let c = Char.unsafe_chr key in
1014 match c with
1015 | '0' .. '9' ->
1016 let s = "x" in s.[0] <- c;
1017 let text = text ^ s in
1018 TEcont text
1020 | _ ->
1021 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1022 TEcont text
1025 let addchar s c =
1026 let b = Buffer.create (String.length s + 1) in
1027 Buffer.add_string b s;
1028 Buffer.add_char b c;
1029 Buffer.contents b;
1032 let textentry text key =
1033 let c = Char.unsafe_chr key in
1034 match c with
1035 | _ when key >= 32 && key < 127 ->
1036 let text = addchar text c in
1037 TEcont text
1039 | _ ->
1040 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1041 TEcont text
1044 let reinit angle proportional =
1045 state.anchor <- getanchor ();
1046 conf.angle <- angle;
1047 conf.proportional <- proportional;
1048 invalidate ();
1049 wcmd "reinit" [`i angle; `b proportional];
1052 let setzoom zoom =
1053 let zoom = max 0.01 zoom in
1054 if zoom <> conf.zoom
1055 then (
1056 if zoom <= 1.0
1057 then state.x <- 0;
1058 conf.zoom <- zoom;
1059 reshape conf.winw conf.winh;
1060 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1064 let enterbirdseye () =
1065 let zoom = float conf.thumbw /. float conf.winw in
1066 let birdseyepageno =
1067 let cy = conf.winh / 2 in
1068 let fold = function
1069 | [] -> 0
1070 | l :: rest ->
1071 let rec fold best = function
1072 | [] -> best.pageno
1073 | l :: rest ->
1074 let d = cy - (l.pagedispy + l.pagevh/2)
1075 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1076 if abs d < abs dbest
1077 then fold l rest
1078 else best.pageno
1079 in fold l rest
1081 fold state.layout
1083 state.mode <- Birdseye (
1084 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1086 conf.zoom <- zoom;
1087 conf.presentation <- false;
1088 conf.interpagespace <- 10;
1089 conf.hlinks <- false;
1090 state.x <- 0;
1091 state.mstate <- Mnone;
1092 conf.showall <- false;
1093 Glut.setCursor Glut.CURSOR_INHERIT;
1094 if conf.verbose
1095 then
1096 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1097 (100.0*.zoom)
1098 else
1099 state.text <- ""
1101 reshape conf.winw conf.winh;
1104 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1105 state.mode <- View;
1106 conf.zoom <- c.zoom;
1107 conf.presentation <- c.presentation;
1108 conf.interpagespace <- c.interpagespace;
1109 conf.showall <- c.showall;
1110 conf.hlinks <- c.hlinks;
1111 state.x <- leftx;
1112 if conf.verbose
1113 then
1114 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1115 (100.0*.conf.zoom)
1117 reshape conf.winw conf.winh;
1118 state.anchor <- if goback then anchor else (pageno, 0.0);
1121 let togglebirdseye () =
1122 match state.mode with
1123 | Birdseye vals -> leavebirdseye vals true
1124 | View | Outline _ -> enterbirdseye ()
1125 | _ -> ()
1128 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1129 let pageno = max 0 (pageno - 1) in
1130 let rec loop = function
1131 | [] -> gotopage1 pageno 0
1132 | l :: _ when l.pageno = pageno ->
1133 if l.pagedispy >= 0 && l.pagey = 0
1134 then Glut.postRedisplay ()
1135 else gotopage1 pageno 0
1136 | _ :: rest -> loop rest
1138 loop state.layout;
1139 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1142 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1143 let pageno = min (state.pagecount - 1) (pageno + 1) in
1144 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1145 let rec loop = function
1146 | [] ->
1147 let y, h = getpageyh pageno in
1148 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1149 gotoy (clamp dy)
1150 | l :: _ when l.pageno = pageno ->
1151 if l.pagevh != l.pageh
1152 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1153 else Glut.postRedisplay ()
1154 | _ :: rest -> loop rest
1156 loop state.layout
1159 let optentry mode _ key =
1160 let btos b = if b then "on" else "off" in
1161 let c = Char.unsafe_chr key in
1162 match c with
1163 | 's' ->
1164 let ondone s =
1165 try conf.scrollstep <- int_of_string s with exc ->
1166 state.text <- Printf.sprintf "bad integer `%s': %s"
1167 s (Printexc.to_string exc)
1169 TEswitch ("scroll step: ", "", None, intentry, ondone)
1171 | 'A' ->
1172 let ondone s =
1174 conf.autoscrollstep <- int_of_string s;
1175 if state.autoscroll <> None
1176 then state.autoscroll <- Some conf.autoscrollstep
1177 with exc ->
1178 state.text <- Printf.sprintf "bad integer `%s': %s"
1179 s (Printexc.to_string exc)
1181 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1183 | 'Z' ->
1184 let ondone s =
1186 let zoom = float (int_of_string s) /. 100.0 in
1187 setzoom zoom
1188 with exc ->
1189 state.text <- Printf.sprintf "bad integer `%s': %s"
1190 s (Printexc.to_string exc)
1192 TEswitch ("zoom: ", "", None, intentry, ondone)
1194 | 't' ->
1195 let ondone s =
1197 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1198 state.text <-
1199 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1200 begin match mode with
1201 | Birdseye beye ->
1202 leavebirdseye beye false;
1203 enterbirdseye ();
1204 | _ -> ();
1206 with exc ->
1207 state.text <- Printf.sprintf "bad integer `%s': %s"
1208 s (Printexc.to_string exc)
1210 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1212 | 'R' ->
1213 let ondone s =
1214 match try
1215 Some (int_of_string s)
1216 with exc ->
1217 state.text <- Printf.sprintf "bad integer `%s': %s"
1218 s (Printexc.to_string exc);
1219 None
1220 with
1221 | Some angle -> reinit angle conf.proportional
1222 | None -> ()
1224 TEswitch ("rotation: ", "", None, intentry, ondone)
1226 | 'i' ->
1227 conf.icase <- not conf.icase;
1228 TEdone ("case insensitive search " ^ (btos conf.icase))
1230 | 'p' ->
1231 conf.preload <- not conf.preload;
1232 gotoy state.y;
1233 TEdone ("preload " ^ (btos conf.preload))
1235 | 'v' ->
1236 conf.verbose <- not conf.verbose;
1237 TEdone ("verbose " ^ (btos conf.verbose))
1239 | 'h' ->
1240 conf.maxhfit <- not conf.maxhfit;
1241 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1242 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1244 | 'c' ->
1245 conf.crophack <- not conf.crophack;
1246 TEdone ("crophack " ^ btos conf.crophack)
1248 | 'a' ->
1249 conf.showall <- not conf.showall;
1250 TEdone ("throttle " ^ btos conf.showall)
1252 | 'f' ->
1253 conf.underinfo <- not conf.underinfo;
1254 TEdone ("underinfo " ^ btos conf.underinfo)
1256 | 'P' ->
1257 conf.savebmarks <- not conf.savebmarks;
1258 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1260 | 'S' ->
1261 let ondone s =
1263 let pageno, py =
1264 match state.layout with
1265 | [] -> 0, 0
1266 | l :: _ ->
1267 l.pageno, l.pagey
1269 conf.interpagespace <- int_of_string s;
1270 state.maxy <- calcheight ();
1271 let y = getpagey pageno in
1272 gotoy (y + py)
1273 with exc ->
1274 state.text <- Printf.sprintf "bad integer `%s': %s"
1275 s (Printexc.to_string exc)
1277 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1279 | 'l' ->
1280 reinit conf.angle (not conf.proportional);
1281 TEdone ("proprortional display " ^ btos conf.proportional)
1283 | _ ->
1284 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1285 TEstop
1288 let maxoutlinerows () = (conf.winh - 31) / 16;;
1290 let enterselector allowdel outlines errmsg msg =
1291 if Array.length outlines = 0
1292 then (
1293 showtext ' ' errmsg;
1295 else (
1296 state.text <- msg;
1297 Glut.setCursor Glut.CURSOR_INHERIT;
1298 let pageno =
1299 match state.layout with
1300 | [] -> -1
1301 | {pageno=pageno} :: _ -> pageno
1303 let active =
1304 let rec loop n =
1305 if n = Array.length outlines
1306 then 0
1307 else
1308 let (_, _, (outlinepageno, _)) = outlines.(n) in
1309 if outlinepageno >= pageno then n else loop (n+1)
1311 loop 0
1313 state.mode <- Outline
1314 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1315 state.mode);
1316 Glut.postRedisplay ();
1320 let enteroutlinemode () =
1321 let outlines, msg =
1322 match state.outlines with
1323 | Oarray a -> a, ""
1324 | Olist l ->
1325 let a = Array.of_list (List.rev l) in
1326 state.outlines <- Oarray a;
1327 a, ""
1328 | Onarrow (pat, a, _) ->
1329 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1331 enterselector false outlines "Document has no outline" msg;
1334 let enterbookmarkmode () =
1335 let bookmarks = Array.of_list state.bookmarks in
1336 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1339 let mode_to_string mode =
1340 let b = Buffer.create 10 in
1341 let rec f = function
1342 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1343 | View -> Buffer.add_string b "View"
1344 | Birdseye _ -> Buffer.add_string b "Birdseye"
1345 | Items _ -> Buffer.add_string b "Items"
1346 | Outline _ -> Buffer.add_string b "Outline"
1348 f mode;
1349 Buffer.contents b;
1352 let color_of_string s =
1353 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1354 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1358 let color_to_string (r, g, b) =
1359 let r = truncate (r *. 256.0)
1360 and g = truncate (g *. 256.0)
1361 and b = truncate (b *. 256.0) in
1362 Printf.sprintf "%d/%d/%d" r g b
1365 let enterinfomode () =
1366 let btos = function true -> "on" | _ -> "off" in
1367 let mode = state.mode in
1368 let rec makeitems () =
1369 let intp name get set =
1370 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1371 fun active first _ pan ->
1372 let ondone s =
1373 let n =
1374 try int_of_string s
1375 with exn ->
1376 state.text <- Printf.sprintf "bad integer `%s': %s"
1377 s (Printexc.to_string exn);
1378 max_int;
1380 if n != max_int then set n;
1382 let te = name ^ ": ", "", None, intentry, ondone in
1383 state.text <- "";
1384 Textentry (
1386 fun _ ->
1387 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1390 and boolp name get set =
1391 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1392 fun active first qsearch pan ->
1393 let v = get () in
1394 set (not v);
1395 Items (active, first, makeitems (), qsearch, pan, mode);
1397 and colorp name get set =
1398 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1399 fun active first _ pan ->
1400 let invalid = (nan, nan, nan) in
1401 let ondone s =
1402 let c =
1403 try color_of_string s
1404 with exn ->
1405 state.text <- Printf.sprintf "bad color `%s': %s"
1406 s (Printexc.to_string exn);
1407 invalid
1409 if c <> invalid
1410 then set c;
1412 let te = name ^ ": ", "", None, textentry, ondone in
1413 state.text <- "";
1414 Textentry (
1416 fun _ ->
1417 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1422 let birdseye = isbirdseye mode in
1423 let items = [
1424 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1426 boolp "presentation"
1427 (fun () -> conf.presentation)
1428 (fun v ->
1429 conf.presentation <- v;
1430 state.anchor <- getanchor ();
1431 represent ());
1433 boolp "ignore case in searches"
1434 (fun () -> conf.icase)
1435 (fun v -> conf.icase <- v);
1437 boolp "preload"
1438 (fun () -> conf.preload)
1439 (fun v -> conf.preload <- v);
1441 boolp "verbose"
1442 (fun () -> conf.verbose)
1443 (fun v -> conf.verbose <- v);
1445 boolp "max fit"
1446 (fun () -> conf.maxhfit)
1447 (fun v -> conf.maxhfit <- v);
1449 boolp "crop hack"
1450 (fun () -> conf.crophack)
1451 (fun v -> conf.crophack <- v);
1453 boolp "throttle"
1454 (fun () -> conf.showall)
1455 (fun v -> conf.showall <- v);
1457 boolp "highlight links"
1458 (fun () -> conf.hlinks)
1459 (fun v -> conf.hlinks <- v);
1461 boolp "under info"
1462 (fun () -> conf.underinfo)
1463 (fun v -> conf.underinfo <- v);
1464 boolp "persistent bookmarks"
1465 (fun () -> conf.savebmarks)
1466 (fun v -> conf.savebmarks <- v);
1468 boolp "proportional display"
1469 (fun () -> conf.proportional)
1470 (fun v -> reinit conf.angle v);
1472 boolp "persistent location"
1473 (fun () -> conf.jumpback)
1474 (fun v -> conf.jumpback <- v);
1476 "", 0, Noaction;
1478 intp "vertical margin"
1479 (fun () -> conf.interpagespace)
1480 (fun n ->
1481 conf.interpagespace <- n;
1482 let pageno, py =
1483 match state.layout with
1484 | [] -> 0, 0
1485 | l :: _ ->
1486 l.pageno, l.pagey
1488 state.maxy <- calcheight ();
1489 let y = getpagey pageno in
1490 gotoy (y + py)
1493 intp "page bias"
1494 (fun () -> conf.pagebias)
1495 (fun v -> conf.pagebias <- v);
1497 intp "scroll step"
1498 (fun () -> conf.scrollstep)
1499 (fun n -> conf.scrollstep <- n);
1501 intp "auto scroll step"
1502 (fun () ->
1503 match state.autoscroll with
1504 | Some step -> step
1505 | _ -> conf.autoscrollstep)
1506 (fun n ->
1507 if state.autoscroll <> None
1508 then state.autoscroll <- Some n;
1509 conf.autoscrollstep <- n);
1511 intp "zoom"
1512 (fun () -> truncate (conf.zoom *. 100.))
1513 (fun v -> setzoom ((float v) /. 100.));
1515 intp "rotation"
1516 (fun () -> conf.angle)
1517 (fun v -> reinit v conf.proportional);
1519 intp "scroll bar width"
1520 (fun () -> state.scrollw)
1521 (fun v ->
1522 state.scrollw <- v;
1523 conf.scrollbw <- v;
1524 reshape conf.winw conf.winh;
1527 intp "scroll handle height"
1528 (fun () -> conf.scrollh)
1529 (fun v -> conf.scrollh <- v;);
1531 intp "thumbnail width"
1532 (fun () -> conf.thumbw)
1533 (fun v ->
1534 conf.thumbw <- min 1920 v;
1535 match mode with
1536 | Birdseye beye ->
1537 leavebirdseye beye false;
1538 enterbirdseye ()
1539 | _ -> ()
1542 colorp "background color"
1543 (fun () -> conf.bgcolor)
1544 (fun v -> conf.bgcolor <- v);
1546 "", 0, Noaction;
1547 "Presentation mode", 0, Noaction;
1549 boolp "scrollbar visible"
1550 (fun () -> conf.scrollbarinpm)
1551 (fun v ->
1552 if v != conf.scrollbarinpm
1553 then (
1554 conf.scrollbarinpm <- v;
1555 if conf.presentation
1556 then (
1557 state.scrollw <- if v then conf.scrollbw else 0;
1558 reshape conf.winw conf.winh;
1563 "", 0, Noaction;
1564 "Pixmap Cache", 0, Noaction;
1566 intp "size (advisory)"
1567 (fun () -> conf.memlimit)
1568 (fun v -> conf.memlimit <- v);
1569 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1573 let tailer =
1574 let trailer =
1575 ("", 0, Noaction)
1576 :: ("Document", 0, Noaction)
1577 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1579 if birdseye
1580 then trailer
1581 else (
1582 ("", 0, Noaction)
1583 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1584 (btos conf.bedefault),
1586 Action (
1587 fun active first qsearch pan ->
1588 conf.bedefault <- not conf.bedefault;
1589 Items (active, first, makeitems (), qsearch, pan, mode);
1591 ) :: trailer
1594 Array.of_list (items @ tailer)
1596 state.text <- "";
1597 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1598 Glut.postRedisplay ();
1601 let enterhelpmode () =
1602 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1603 Glut.postRedisplay ();
1606 let quickbookmark ?title () =
1607 match state.layout with
1608 | [] -> ()
1609 | l :: _ ->
1610 let title =
1611 match title with
1612 | None ->
1613 let sec = Unix.gettimeofday () in
1614 let tm = Unix.localtime sec in
1615 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1616 (l.pageno+1)
1617 tm.Unix.tm_mday
1618 tm.Unix.tm_mon
1619 (tm.Unix.tm_year + 1900)
1620 tm.Unix.tm_hour
1621 tm.Unix.tm_min
1622 | Some title -> title
1624 state.bookmarks <-
1625 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1626 :: state.bookmarks
1629 let doreshape w h =
1630 state.fullscreen <- None;
1631 Glut.reshapeWindow w h;
1634 let writeopen path password =
1635 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1638 let opendoc path password =
1639 invalidate ();
1640 state.path <- path;
1641 state.password <- password;
1642 state.gen <- state.gen + 1;
1643 state.docinfo <- [];
1645 writeopen path password;
1646 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1647 wcmd "geometry" [`i state.w; `i conf.winh];
1650 let viewkeyboard key =
1651 let enttext te =
1652 let mode = state.mode in
1653 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1654 state.text <- "";
1655 enttext ();
1656 Glut.postRedisplay ()
1658 let c = Char.chr key in
1659 match c with
1660 | '\027' | 'q' -> (* escape *)
1661 raise Quit
1663 | '\008' -> (* backspace *)
1664 let y = getnav ~-1 in
1665 gotoy_and_clear_text y
1667 | 'o' ->
1668 enteroutlinemode ()
1670 | 'u' ->
1671 state.rects <- [];
1672 state.text <- "";
1673 Glut.postRedisplay ()
1675 | '/' | '?' ->
1676 let ondone isforw s =
1677 cbput state.hists.pat s;
1678 state.searchpattern <- s;
1679 search s isforw
1681 let s = String.create 1 in
1682 s.[0] <- c;
1683 enttext (s, "", Some (onhist state.hists.pat),
1684 textentry, ondone (c ='/'))
1686 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1687 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1688 setzoom (conf.zoom +. incr)
1690 | '+' ->
1691 let ondone s =
1692 let n =
1693 try int_of_string s with exc ->
1694 state.text <- Printf.sprintf "bad integer `%s': %s"
1695 s (Printexc.to_string exc);
1696 max_int
1698 if n != max_int
1699 then (
1700 conf.pagebias <- n;
1701 state.text <- "page bias is now " ^ string_of_int n;
1704 enttext ("page bias: ", "", None, intentry, ondone)
1706 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1707 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1708 setzoom (max 0.01 (conf.zoom -. decr))
1710 | '-' ->
1711 let ondone msg = state.text <- msg in
1712 enttext (
1713 "option [acfhilpstvAPRSZ]: ", "", None,
1714 optentry state.mode, ondone
1717 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1718 setzoom 1.0
1720 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1721 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1722 if zoom < 1.0
1723 then setzoom zoom
1725 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1726 togglebirdseye ()
1728 | '0' .. '9' ->
1729 let ondone s =
1730 let n =
1731 try int_of_string s with exc ->
1732 state.text <- Printf.sprintf "bad integer `%s': %s"
1733 s (Printexc.to_string exc);
1736 if n >= 0
1737 then (
1738 addnav ();
1739 cbput state.hists.pag (string_of_int n);
1740 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1743 let pageentry text key =
1744 match Char.unsafe_chr key with
1745 | 'g' -> TEdone text
1746 | _ -> intentry text key
1748 let text = "x" in text.[0] <- c;
1749 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1751 | 'b' ->
1752 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1753 reshape conf.winw conf.winh;
1755 | 'l' ->
1756 conf.hlinks <- not conf.hlinks;
1757 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1758 Glut.postRedisplay ()
1760 | 'a' ->
1761 begin match state.autoscroll with
1762 | Some step ->
1763 conf.autoscrollstep <- step;
1764 state.autoscroll <- None
1765 | None ->
1766 if conf.autoscrollstep = 0
1767 then state.autoscroll <- Some 1
1768 else state.autoscroll <- Some conf.autoscrollstep
1771 | 'P' ->
1772 conf.presentation <- not conf.presentation;
1773 if conf.presentation
1774 then (
1775 if not conf.scrollbarinpm
1776 then state.scrollw <- 0;
1778 else
1779 state.scrollw <- conf.scrollbw;
1781 showtext ' ' ("presentation mode " ^
1782 if conf.presentation then "on" else "off");
1783 state.anchor <- getanchor ();
1784 represent ()
1786 | 'f' ->
1787 begin match state.fullscreen with
1788 | None ->
1789 state.fullscreen <- Some (conf.winw, conf.winh);
1790 Glut.fullScreen ()
1791 | Some (w, h) ->
1792 state.fullscreen <- None;
1793 doreshape w h
1796 | 'g' ->
1797 gotoy_and_clear_text 0
1799 | 'G' ->
1800 gotopage1 (state.pagecount - 1) 0
1802 | 'n' ->
1803 search state.searchpattern true
1805 | 'p' | 'N' ->
1806 search state.searchpattern false
1808 | 't' ->
1809 begin match state.layout with
1810 | [] -> ()
1811 | l :: _ ->
1812 gotoy_and_clear_text (getpagey l.pageno)
1815 | ' ' ->
1816 begin match List.rev state.layout with
1817 | [] -> ()
1818 | l :: _ ->
1819 let pageno = min (l.pageno+1) (state.pagecount-1) in
1820 gotoy_and_clear_text (getpagey pageno)
1823 | '\127' -> (* del *)
1824 begin match state.layout with
1825 | [] -> ()
1826 | l :: _ ->
1827 let pageno = max 0 (l.pageno-1) in
1828 gotoy_and_clear_text (getpagey pageno)
1831 | '=' ->
1832 let f (fn, _) l =
1833 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1835 let fn, ln = List.fold_left f (-1, -1) state.layout in
1836 let s =
1837 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1838 let percent =
1839 if maxy <= 0
1840 then 100.
1841 else (100. *. (float state.y /. float maxy)) in
1842 if fn = ln
1843 then
1844 Printf.sprintf "Page %d of %d %.2f%%"
1845 (fn+1) state.pagecount percent
1846 else
1847 Printf.sprintf
1848 "Pages %d-%d of %d %.2f%%"
1849 (fn+1) (ln+1) state.pagecount percent
1851 showtext ' ' s;
1853 | 'w' ->
1854 begin match state.layout with
1855 | [] -> ()
1856 | l :: _ ->
1857 doreshape (l.pagew + state.scrollw) l.pageh;
1858 Glut.postRedisplay ();
1861 | '\'' ->
1862 enterbookmarkmode ()
1864 | 'h' ->
1865 enterhelpmode ()
1867 | 'i' ->
1868 enterinfomode ()
1870 | 'm' ->
1871 let ondone s =
1872 match state.layout with
1873 | l :: _ ->
1874 state.bookmarks <-
1875 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1876 :: state.bookmarks
1877 | _ -> ()
1879 enttext ("bookmark: ", "", None, textentry, ondone)
1881 | '~' ->
1882 quickbookmark ();
1883 showtext ' ' "Quick bookmark added";
1885 | 'z' ->
1886 begin match state.layout with
1887 | l :: _ ->
1888 let rect = getpdimrect l.pagedimno in
1889 let w, h =
1890 if conf.crophack
1891 then
1892 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1893 truncate (1.2 *. (rect.(3) -. rect.(0))))
1894 else
1895 (truncate (rect.(1) -. rect.(0)),
1896 truncate (rect.(3) -. rect.(0)))
1898 if w != 0 && h != 0
1899 then
1900 doreshape (w + state.scrollw) (h + conf.interpagespace)
1902 Glut.postRedisplay ();
1904 | [] -> ()
1907 | '\000' -> (* ctrl-2 *)
1908 let maxw = getmaxw () in
1909 if maxw > 0.0
1910 then setzoom (maxw /. float conf.winw)
1912 | '<' | '>' ->
1913 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1915 | '[' | ']' ->
1916 state.colorscale <-
1917 max 0.0
1918 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1919 Glut.postRedisplay ()
1921 | 'k' ->
1922 begin match state.mode with
1923 | Birdseye beye -> upbirdseye beye
1924 | _ -> gotoy (clamp (-conf.scrollstep))
1927 | 'j' ->
1928 begin match state.mode with
1929 | Birdseye beye -> downbirdseye beye
1930 | _ -> gotoy (clamp conf.scrollstep)
1933 | 'r' ->
1934 state.anchor <- getanchor ();
1935 opendoc state.path state.password
1937 | _ ->
1938 vlog "huh? %d %c" key (Char.chr key);
1941 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1942 let enttext te =
1943 state.mode <- Textentry (te, onleave);
1944 state.text <- "";
1945 enttext ();
1946 Glut.postRedisplay ()
1948 match Char.unsafe_chr key with
1949 | '\008' -> (* backspace *)
1950 let len = String.length text in
1951 if len = 0
1952 then (
1953 onleave Cancel;
1954 Glut.postRedisplay ();
1956 else (
1957 let s = String.sub text 0 (len - 1) in
1958 enttext (c, s, opthist, onkey, ondone)
1961 | '\r' | '\n' ->
1962 ondone text;
1963 onleave Confirm;
1964 Glut.postRedisplay ()
1966 | '\007' (* ctrl-g *)
1967 | '\027' -> (* escape *)
1968 begin match opthist with
1969 | None -> ()
1970 | Some (_, onhistcancel) -> onhistcancel ()
1971 end;
1972 onleave Cancel;
1973 Glut.postRedisplay ()
1975 | _ ->
1976 begin match onkey text key with
1977 | TEdone text ->
1978 onleave Confirm;
1979 ondone text;
1980 Glut.postRedisplay ()
1982 | TEcont text ->
1983 enttext (c, text, opthist, onkey, ondone);
1985 | TEstop ->
1986 onleave Cancel;
1987 Glut.postRedisplay ()
1989 | TEswitch te ->
1990 state.mode <- Textentry (te, onleave);
1991 Glut.postRedisplay ()
1992 end;
1995 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
1996 match key with
1997 | 27 -> (* escape *)
1998 leavebirdseye beye true
2000 | 12 -> (* ctrl-l *)
2001 let y, h = getpageyh pageno in
2002 let top = (conf.winh - h) / 2 in
2003 gotoy (max 0 (y - top))
2005 | 13 -> (* enter *)
2006 leavebirdseye beye false
2008 | _ ->
2009 viewkeyboard key
2012 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2013 let set active first qsearch =
2014 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2016 let search active pattern incr =
2017 let dosearch re =
2018 let rec loop n =
2019 if n >= Array.length items || n < 0
2020 then None
2021 else
2022 let (s, _, _) = items.(n) in
2024 (try ignore (Str.search_forward re s 0); true
2025 with Not_found -> false)
2026 then Some n
2027 else loop (n + incr)
2029 loop active
2032 let re = Str.regexp_case_fold pattern in
2033 dosearch re
2034 with Failure s ->
2035 state.text <- s;
2036 None
2038 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2039 match key with
2040 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2041 let incr = if key = 18 then -1 else 1 in
2042 let active, first =
2043 match search (active + incr) qsearch incr with
2044 | None ->
2045 state.text <- qsearch ^ " [not found]";
2046 active, first
2047 | Some active ->
2048 state.text <- qsearch;
2049 active, firstof active
2051 set active first qsearch;
2052 Glut.postRedisplay ();
2054 | 8 -> (* backspace *)
2055 let len = String.length qsearch in
2056 if len = 0
2057 then ()
2058 else (
2059 if len = 1
2060 then (
2061 state.text <- "";
2062 set active first "";
2064 else
2065 let qsearch = String.sub qsearch 0 (len - 1) in
2066 let active, first =
2067 match search active qsearch ~-1 with
2068 | None ->
2069 state.text <- qsearch ^ " [not found]";
2070 active, first
2071 | Some active ->
2072 state.text <- qsearch;
2073 active, firstof active
2075 set active first qsearch
2077 Glut.postRedisplay ()
2079 | _ when key >= 32 && key < 127 ->
2080 let pattern = addchar qsearch (Char.chr key) in
2081 let active, first =
2082 match search active pattern 1 with
2083 | None ->
2084 state.text <- pattern ^ " [not found]";
2085 active, first
2086 | Some active ->
2087 state.text <- pattern;
2088 active, firstof active
2090 set active first pattern;
2091 Glut.postRedisplay ()
2093 | 27 -> (* escape *)
2094 state.text <- "";
2095 if String.length qsearch = 0
2096 then (
2097 state.mode <- oldmode;
2099 else (
2100 set active first "";
2102 Glut.postRedisplay ()
2104 | 13 -> (* enter *)
2105 if active >= 0 && active < Array.length items
2106 then (
2107 match items.(active) with
2108 | _, _, Action f ->
2109 state.mode <- f active first qsearch pan
2111 | _, _, Noaction ->
2112 state.text <- "";
2113 state.mode <- oldmode
2115 else (
2116 state.text <- "";
2117 state.mode <- oldmode
2119 Glut.postRedisplay ();
2121 | _ -> dolog "unknown key %d" key
2124 let outlinekeyboard key
2125 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2126 let narrow outlines pattern =
2127 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2128 match reopt with
2129 | None -> None
2130 | Some re ->
2131 let rec fold accu n =
2132 if n = -1
2133 then accu
2134 else
2135 let (s, _, _) as o = outlines.(n) in
2136 let accu =
2137 if (try ignore (Str.search_forward re s 0); true
2138 with Not_found -> false)
2139 then (o :: accu)
2140 else accu
2142 fold accu (n-1)
2144 let matched = fold [] (Array.length outlines - 1) in
2145 if matched = [] then None else Some (Array.of_list matched)
2147 let search active pattern incr =
2148 let dosearch re =
2149 let rec loop n =
2150 if n = Array.length outlines || n = -1
2151 then None
2152 else
2153 let (s, _, _) = outlines.(n) in
2155 (try ignore (Str.search_forward re s 0); true
2156 with Not_found -> false)
2157 then Some n
2158 else loop (n + incr)
2160 loop active
2163 let re = Str.regexp_case_fold pattern in
2164 dosearch re
2165 with Failure s ->
2166 state.text <- s;
2167 None
2169 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2170 match key with
2171 | 27 -> (* escape *)
2172 state.text <- "";
2173 if String.length qsearch = 0
2174 then (
2175 state.mode <- oldmode;
2177 else (
2178 state.mode <- Outline (
2179 allowdel, active, first, outlines, "", pan, oldmode
2182 Glut.postRedisplay ();
2184 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2185 let incr = if key = 18 then -1 else 1 in
2186 let active, first =
2187 match search (active + incr) qsearch incr with
2188 | None ->
2189 state.text <- qsearch ^ " [not found]";
2190 active, first
2191 | Some active ->
2192 state.text <- qsearch;
2193 active, firstof active
2195 state.mode <- Outline (
2196 allowdel, active, first, outlines, qsearch, pan, oldmode
2198 Glut.postRedisplay ();
2200 | 8 -> (* backspace *)
2201 let len = String.length qsearch in
2202 if len = 0
2203 then ()
2204 else (
2205 if len = 1
2206 then (
2207 state.text <- "";
2208 state.mode <- Outline (
2209 allowdel, active, first, outlines, "", pan, oldmode
2212 else
2213 let qsearch = String.sub qsearch 0 (len - 1) in
2214 let active, first =
2215 match search active qsearch ~-1 with
2216 | None ->
2217 state.text <- qsearch ^ " [not found]";
2218 active, first
2219 | Some active ->
2220 state.text <- qsearch;
2221 active, firstof active
2223 state.mode <- Outline (
2224 allowdel, active, first, outlines, qsearch, pan, oldmode
2227 Glut.postRedisplay ()
2229 | 13 -> (* enter *)
2230 if active < Array.length outlines
2231 then (
2232 let (_, _, anchor) = outlines.(active) in
2233 addnav ();
2234 gotoanchor anchor;
2236 state.text <- "";
2237 if allowdel then state.bookmarks <- Array.to_list outlines;
2238 state.mode <- oldmode;
2239 Glut.postRedisplay ();
2241 | _ when key >= 32 && key < 127 ->
2242 let pattern = addchar qsearch (Char.chr key) in
2243 let active, first =
2244 match search active pattern 1 with
2245 | None ->
2246 state.text <- pattern ^ " [not found]";
2247 active, first
2248 | Some active ->
2249 state.text <- pattern;
2250 active, firstof active
2252 state.mode <- Outline (
2253 allowdel, active, first, outlines, pattern, pan, oldmode
2255 Glut.postRedisplay ()
2257 | 14 when not allowdel -> (* ctrl-n *)
2258 if String.length qsearch > 0
2259 then (
2260 let optoutlines = narrow outlines qsearch in
2261 begin match optoutlines with
2262 | None -> state.text <- "can't narrow"
2263 | Some outlines ->
2264 state.mode <- Outline (
2265 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2267 match state.outlines with
2268 | Olist _ -> ()
2269 | Oarray a ->
2270 state.outlines <- Onarrow (qsearch, outlines, a)
2271 | Onarrow (_, _, b) ->
2272 state.outlines <- Onarrow (qsearch, outlines, b)
2273 end;
2275 Glut.postRedisplay ()
2277 | 21 when not allowdel -> (* ctrl-u *)
2278 let outline =
2279 match state.outlines with
2280 | Oarray a -> a
2281 | Olist l ->
2282 let a = Array.of_list (List.rev l) in
2283 state.outlines <- Oarray a;
2285 | Onarrow (_, _, b) ->
2286 state.outlines <- Oarray b;
2287 state.text <- "";
2290 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2291 Glut.postRedisplay ()
2293 | 12 -> (* ctrl-l *)
2294 state.mode <- Outline
2295 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2296 Glut.postRedisplay ()
2298 | 127 when allowdel -> (* delete *)
2299 let len = Array.length outlines - 1 in
2300 if len = 0
2301 then (
2302 state.mode <- View;
2303 state.bookmarks <- [];
2305 else (
2306 let bookmarks = Array.init len
2307 (fun i ->
2308 let i = if i >= active then i + 1 else i in
2309 outlines.(i)
2312 state.mode <-
2313 Outline (
2314 allowdel,
2315 min active (len-1),
2316 min first (len-1),
2317 bookmarks, qsearch,
2319 oldmode
2322 Glut.postRedisplay ()
2324 | _ -> dolog "unknown key %d" key
2327 let keyboard ~key ~x ~y =
2328 ignore x;
2329 ignore y;
2330 if key = 7 && not (istextentry state.mode) (* ctrl-g *)
2331 then
2332 wcmd "interrupt" []
2333 else
2334 match state.mode with
2335 | Outline outline -> outlinekeyboard key outline
2336 | Textentry textentry -> textentrykeyboard key textentry
2337 | Birdseye birdseye -> birdseyekeyboard key birdseye
2338 | View -> viewkeyboard key
2339 | Items items -> itemskeyboard key items
2342 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2343 match key with
2344 | Glut.KEY_UP -> upbirdseye beye
2345 | Glut.KEY_DOWN -> downbirdseye beye
2347 | Glut.KEY_PAGE_UP ->
2348 begin match state.layout with
2349 | l :: _ ->
2350 if l.pagey != 0
2351 then (
2352 state.mode <- Birdseye (
2353 conf, leftx, l.pageno, hooverpageno, anchor
2355 gotopage1 l.pageno 0;
2357 else (
2358 let layout = layout (state.y-conf.winh) conf.winh in
2359 match layout with
2360 | [] -> gotoy (clamp (-conf.winh))
2361 | l :: _ ->
2362 state.mode <- Birdseye (
2363 conf, leftx, l.pageno, hooverpageno, anchor
2365 gotopage1 l.pageno 0
2368 | [] -> gotoy (clamp (-conf.winh))
2369 end;
2371 | Glut.KEY_PAGE_DOWN ->
2372 begin match List.rev state.layout with
2373 | l :: _ ->
2374 let layout = layout (state.y + conf.winh) conf.winh in
2375 begin match layout with
2376 | [] ->
2377 let incr = l.pageh - l.pagevh in
2378 if incr = 0
2379 then (
2380 state.mode <-
2381 Birdseye (
2382 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2384 Glut.postRedisplay ();
2386 else gotoy (clamp (incr + conf.interpagespace*2));
2388 | l :: _ ->
2389 state.mode <-
2390 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2391 gotopage1 l.pageno 0;
2394 | [] -> gotoy (clamp conf.winh)
2395 end;
2397 | Glut.KEY_HOME ->
2398 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2399 gotopage1 0 0
2401 | Glut.KEY_END ->
2402 let pageno = state.pagecount - 1 in
2403 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2404 if not (pagevisible state.layout pageno)
2405 then
2406 let h =
2407 match List.rev state.pdims with
2408 | [] -> conf.winh
2409 | (_, _, h, _) :: _ -> h
2411 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2412 else Glut.postRedisplay ();
2413 | _ -> ()
2416 let setautoscrollspeed step goingdown =
2417 let incr = max 1 ((abs step) / 2) in
2418 let incr = if goingdown then incr else -incr in
2419 let astep = step + incr in
2420 state.autoscroll <- Some astep;
2423 let special ~key ~x ~y =
2424 ignore x;
2425 ignore y;
2426 match state.mode with
2427 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2428 togglebirdseye ()
2430 | Birdseye vals ->
2431 birdseyespecial key vals
2433 | View when key = Glut.KEY_F1 ->
2434 enterhelpmode ()
2436 | View ->
2437 begin match state.autoscroll with
2438 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2439 setautoscrollspeed step (key = Glut.KEY_DOWN)
2441 | _ ->
2442 let y =
2443 match key with
2444 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2445 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2446 | Glut.KEY_DOWN -> clamp conf.scrollstep
2447 | Glut.KEY_PAGE_UP ->
2448 if Glut.getModifiers () land Glut.active_ctrl != 0
2449 then
2450 match state.layout with
2451 | [] -> state.y
2452 | l :: _ -> state.y - l.pagey
2453 else
2454 clamp (-conf.winh)
2455 | Glut.KEY_PAGE_DOWN ->
2456 if Glut.getModifiers () land Glut.active_ctrl != 0
2457 then
2458 match List.rev state.layout with
2459 | [] -> state.y
2460 | l :: _ -> getpagey l.pageno
2461 else
2462 clamp conf.winh
2463 | Glut.KEY_HOME -> addnav (); 0
2464 | Glut.KEY_END ->
2465 addnav ();
2466 state.maxy - (if conf.maxhfit then conf.winh else 0)
2468 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2469 Glut.getModifiers () land Glut.active_alt != 0 ->
2470 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2472 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2473 state.x <- state.x - 10;
2474 state.y
2475 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2476 state.x <- state.x + 10;
2477 state.y
2479 | _ -> state.y
2481 gotoy_and_clear_text y
2484 | Textentry
2485 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2486 let s =
2487 match key with
2488 | Glut.KEY_UP -> action HCprev
2489 | Glut.KEY_DOWN -> action HCnext
2490 | Glut.KEY_HOME -> action HCfirst
2491 | Glut.KEY_END -> action HClast
2492 | _ -> state.text
2494 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2495 Glut.postRedisplay ()
2497 | Textentry _ -> ()
2499 | Items (active, first, items, qsearch, pan, oldmode) ->
2500 let maxrows = maxoutlinerows () in
2501 let itemcount = Array.length items in
2502 let hasaction = function
2503 | (_, _, Noaction) -> false
2504 | _ -> true
2506 let find start incr =
2507 let rec find i =
2508 if i = -1 || i = itemcount
2509 then -1
2510 else (
2511 if hasaction items.(i)
2512 then i
2513 else find (i + incr)
2516 find start
2518 let set active first =
2519 let first = max 0 (min first (itemcount - maxrows)) in
2520 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2522 let navigate incr =
2523 let isvisible first n = n >= first && n - first <= maxrows in
2524 let active, first =
2525 let incr1 = if incr > 0 then 1 else -1 in
2526 if isvisible first active
2527 then
2528 let next =
2529 let next = active + incr in
2530 let next =
2531 if next < 0 || next >= itemcount
2532 then -1
2533 else find next incr1
2535 if next = -1 || abs (active - next) > maxrows
2536 then -1
2537 else next
2539 if next = -1
2540 then
2541 let first = first + incr in
2542 let first = max 0 (min first (itemcount - 1)) in
2543 let next =
2544 let next = active + incr in
2545 let next = max 0 (min next (itemcount - 1)) in
2546 find next ~-incr1
2548 let active = if next = -1 then active else next in
2549 active, first
2550 else
2551 let first = min next first in
2552 next, first
2553 else
2554 let first = first + incr in
2555 let first = max 0 (min first (itemcount - 1)) in
2556 let active =
2557 let next = active + incr in
2558 let next = max 0 (min next (itemcount - 1)) in
2559 let next = find next incr1 in
2560 if next = -1 || abs (active - first) > maxrows
2561 then active
2562 else next
2564 active, first
2566 set active first;
2567 Glut.postRedisplay ()
2569 begin match key with
2570 | Glut.KEY_UP -> navigate ~-1
2571 | Glut.KEY_DOWN -> navigate 1
2572 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2573 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2575 | Glut.KEY_RIGHT ->
2576 state.mode <- Items (
2577 active, first, items, qsearch, min 0 (pan - 1), oldmode
2579 Glut.postRedisplay ()
2581 | Glut.KEY_LEFT ->
2582 state.mode <- Items (
2583 active, first, items, qsearch, min 0 (pan + 1), oldmode
2585 Glut.postRedisplay ()
2587 | Glut.KEY_HOME ->
2588 let active = find 0 1 in
2589 set active 0;
2590 Glut.postRedisplay ()
2592 | Glut.KEY_END ->
2593 let first = max 0 (itemcount - maxrows) in
2594 let active = find (itemcount - 1) ~-1 in
2595 set active first;
2596 Glut.postRedisplay ()
2598 | _ -> ()
2599 end;
2601 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2602 let maxrows = maxoutlinerows () in
2603 let calcfirst first active =
2604 if active > first
2605 then
2606 let rows = active - first in
2607 if rows > maxrows then active - maxrows else first
2608 else active
2610 let navigate incr =
2611 let active = active + incr in
2612 let active = max 0 (min active (Array.length outlines - 1)) in
2613 let first = calcfirst first active in
2614 state.mode <- Outline (
2615 allowdel, active, first, outlines, qsearch, pan, oldmode
2617 Glut.postRedisplay ()
2619 let updownlevel incr =
2620 let len = Array.length outlines in
2621 let (_, curlevel, _) = outlines.(active) in
2622 let rec flow i =
2623 if i = len then i-1 else if i = -1 then 0 else
2624 let (_, l, _) = outlines.(i) in
2625 if l != curlevel then i else flow (i+incr)
2627 let active = flow active in
2628 let first = calcfirst first active in
2629 state.mode <- Outline (
2630 allowdel, active, first, outlines, qsearch, pan, oldmode
2632 Glut.postRedisplay ()
2634 match key with
2635 | Glut.KEY_UP -> navigate ~-1
2636 | Glut.KEY_DOWN -> navigate 1
2637 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2638 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2640 | Glut.KEY_RIGHT ->
2641 if Glut.getModifiers () land Glut.active_ctrl != 0
2642 then (
2643 state.mode <- Outline (
2644 allowdel, active, first, outlines,
2645 qsearch, min 0 (pan + 1), oldmode
2647 Glut.postRedisplay ();
2649 else (
2650 if not allowdel
2651 then updownlevel 1
2654 | Glut.KEY_LEFT ->
2655 if Glut.getModifiers () land Glut.active_ctrl != 0
2656 then (
2657 state.mode <- Outline (
2658 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2660 Glut.postRedisplay ();
2662 else (
2663 if not allowdel
2664 then updownlevel ~-1
2667 | Glut.KEY_HOME ->
2668 state.mode <- Outline (
2669 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2671 Glut.postRedisplay ()
2673 | Glut.KEY_END ->
2674 let active = Array.length outlines - 1 in
2675 let first = max 0 (active - maxrows) in
2676 state.mode <- Outline (
2677 allowdel, active, first, outlines, qsearch, pan, oldmode
2679 Glut.postRedisplay ()
2681 | _ -> ()
2684 let drawplaceholder l =
2685 GlDraw.color (1.0, 1.0, 1.0);
2686 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2687 GlDraw.rect
2688 (float l.pagex, float l.pagedispy)
2689 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2691 let x = if margin < 0 then -margin else l.pagex
2692 and y = l.pagedispy + 13 in
2693 GlDraw.color (0.0, 0.0, 0.0);
2694 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2697 let now () = Unix.gettimeofday ();;
2699 let drawpage l =
2700 let color =
2701 match state.mode with
2702 | Textentry _ -> scalecolor 0.4
2703 | View | Outline _ | Items _ -> scalecolor 1.0
2704 | Birdseye (_, _, pageno, hooverpageno, _) ->
2705 if l.pageno = hooverpageno
2706 then scalecolor 0.9
2707 else (
2708 if l.pageno = pageno
2709 then scalecolor 1.0
2710 else scalecolor 0.8
2713 GlDraw.color color;
2714 begin match getopaque l.pageno with
2715 | Some (opaque, _) when validopaque opaque ->
2716 let a = now () in
2717 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2718 let b = now () in
2719 let d = b-.a in
2720 vlog "draw %d %f sec" l.pageno d;
2722 | _ ->
2723 drawplaceholder l;
2724 end;
2727 let scrollph y =
2728 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2729 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2730 let sh = float conf.winh /. sh in
2731 let sh = max sh (float conf.scrollh) in
2733 let percent =
2734 if state.y = state.maxy
2735 then 1.0
2736 else float y /. float maxy
2738 let position = (float conf.winh -. sh) *. percent in
2740 let position =
2741 if position +. sh > float conf.winh
2742 then float conf.winh -. sh
2743 else position
2745 position, sh;
2748 let scrollindicator () =
2749 GlDraw.color (0.64 , 0.64, 0.64);
2750 GlDraw.rect
2751 (float (conf.winw - state.scrollw), 0.)
2752 (float conf.winw, float conf.winh)
2754 GlDraw.color (0.0, 0.0, 0.0);
2756 let position, sh = scrollph state.y in
2757 GlDraw.rect
2758 (float (conf.winw - state.scrollw), position)
2759 (float conf.winw, position +. sh)
2763 let showsel margin =
2764 match state.mstate with
2765 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2768 | Msel ((x0, y0), (x1, y1)) ->
2769 let rec loop = function
2770 | l :: ls ->
2771 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2772 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2773 then
2774 match getopaque l.pageno with
2775 | Some (opaque, _) when validopaque opaque ->
2776 let oy = -l.pagey + l.pagedispy in
2777 seltext opaque
2778 (x0 - margin - state.x, y0,
2779 x1 - margin - state.x, y1) oy;
2781 | _ -> ()
2782 else loop ls
2783 | [] -> ()
2785 loop state.layout
2788 let showrects () =
2789 let panx = float state.x in
2790 Gl.enable `blend;
2791 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2792 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2793 List.iter
2794 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2795 List.iter (fun l ->
2796 if l.pageno = pageno
2797 then (
2798 let d = float (l.pagedispy - l.pagey) in
2799 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2800 GlDraw.begins `quads;
2802 GlDraw.vertex2 (x0+.panx, y0+.d);
2803 GlDraw.vertex2 (x1+.panx, y1+.d);
2804 GlDraw.vertex2 (x2+.panx, y2+.d);
2805 GlDraw.vertex2 (x3+.panx, y3+.d);
2807 GlDraw.ends ();
2809 ) state.layout
2810 ) state.rects
2812 Gl.disable `blend;
2815 let showstrings trusted active first pan strings =
2816 Gl.enable `blend;
2817 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2818 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2819 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2820 GlDraw.color (1., 1., 1.);
2821 Gl.enable `texture_2d;
2823 let wx = measurestr 15 "w" in
2824 let tabx = 30.0*.wx +. float (pan*15) in
2825 let rec loop row =
2826 if row = Array.length strings || (row - first) * 16 > conf.winh
2827 then ()
2828 else (
2829 let (s, level, _) = strings.(row) in
2830 let y = (row - first) * 16 in
2831 let x = 5 + 15*(max 0 (level+pan)) in
2832 if row = active
2833 then (
2834 Gl.disable `texture_2d;
2835 GlDraw.polygon_mode `both `line;
2836 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2837 GlDraw.rect (1., float (y + 1))
2838 (float (conf.winw - 1), float (y + 18));
2839 GlDraw.polygon_mode `both `fill;
2840 GlDraw.color (1., 1., 1.);
2841 Gl.enable `texture_2d;
2844 let drawtabularstring x s =
2845 let _ =
2846 if trusted
2847 then
2848 let tabpos = try String.index s '\t' with Not_found -> -1 in
2849 if tabpos > 0
2850 then
2851 let len = String.length s - tabpos - 1 in
2852 let s1 = String.sub s 0 tabpos
2853 and s2 = String.sub s (tabpos + 1) len in
2854 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2855 let x = truncate (max xx tabx) in
2856 drawstring1 15 x (y + 16) s2
2857 else
2858 drawstring1 15 x (y + 16) s
2859 else
2860 drawstring1 15 x (y + 16) s
2864 drawtabularstring (x + pan*15) s;
2865 loop (row+1)
2868 loop first;
2869 Gl.disable `blend;
2870 Gl.disable `texture_2d;
2873 let showoutline (_, active, first, outlines, _, pan, _) =
2874 showstrings false active first pan outlines;
2877 let showitems (active, first, items, _, pan, _) =
2878 showstrings true active first pan items;
2881 let display () =
2882 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2883 GlDraw.viewport margin 0 state.w conf.winh;
2884 pagematrix ();
2885 GlClear.color (scalecolor2 conf.bgcolor);
2886 GlClear.clear [`color];
2887 if conf.zoom > 1.0
2888 then (
2889 Gl.enable `scissor_test;
2890 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2892 List.iter drawpage state.layout;
2893 if conf.zoom > 1.0
2894 then
2895 Gl.disable `scissor_test
2897 if state.x != 0
2898 then (
2899 let x = -.float state.x in
2900 GlMat.translate ~x ();
2902 showrects ();
2903 showsel margin;
2904 GlDraw.viewport 0 0 conf.winw conf.winh;
2905 winmatrix ();
2906 scrollindicator ();
2907 begin match state.mode with
2908 | Items items -> showitems items
2909 | Outline outline -> showoutline outline
2910 | _ -> ()
2911 end;
2912 enttext ();
2913 Glut.swapBuffers ();
2916 let getunder x y =
2917 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2918 let x = x - margin - state.x in
2919 let rec f = function
2920 | l :: rest ->
2921 begin match getopaque l.pageno with
2922 | Some (opaque, _) when validopaque opaque ->
2923 let y = y - l.pagedispy in
2924 if y > 0
2925 then
2926 let y = l.pagey + y in
2927 let x = x - l.pagex in
2928 match whatsunder opaque x y with
2929 | Unone -> f rest
2930 | under -> under
2931 else
2932 f rest
2933 | _ ->
2934 f rest
2936 | [] -> Unone
2938 f state.layout
2941 let viewmouse button bstate x y =
2942 match button with
2943 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2944 if Glut.getModifiers () land Glut.active_ctrl != 0
2945 then (
2946 match state.mstate with
2947 | Mzoom (oldn, i) ->
2948 if oldn = n
2949 then (
2950 if i = 2
2951 then
2952 let incr =
2953 match n with
2954 | 4 ->
2955 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2956 | _ ->
2957 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2959 let zoom = conf.zoom +. incr in
2960 setzoom zoom;
2961 state.mstate <- Mzoom (n, 0);
2962 else
2963 state.mstate <- Mzoom (n, i+1);
2965 else state.mstate <- Mzoom (n, 0)
2967 | _ -> state.mstate <- Mzoom (n, 0)
2969 else (
2970 match state.autoscroll with
2971 | Some step -> setautoscrollspeed step (n=4)
2972 | None ->
2973 let incr =
2974 if n = 3
2975 then -conf.scrollstep
2976 else conf.scrollstep
2978 let incr = incr * 2 in
2979 let y = clamp incr in
2980 gotoy_and_clear_text y
2983 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2984 if bstate = Glut.DOWN
2985 then (
2986 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2987 state.mstate <- Mpan (x, y)
2989 else
2990 state.mstate <- Mnone
2992 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
2993 if bstate = Glut.DOWN
2994 then
2995 let position, sh = scrollph state.y in
2996 if y > truncate position && y < truncate (position +. sh)
2997 then
2998 state.mstate <- Mscroll
2999 else
3000 let percent = float y /. float conf.winh in
3001 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3002 gotoy desty;
3003 state.mstate <- Mscroll
3004 else
3005 state.mstate <- Mnone
3007 | Glut.LEFT_BUTTON ->
3008 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3009 begin match dest with
3010 | Ulinkgoto (pageno, top) ->
3011 if pageno >= 0
3012 then (
3013 addnav ();
3014 gotopage1 pageno top;
3017 | Ulinkuri s ->
3018 print_endline s
3020 | Unone when bstate = Glut.DOWN ->
3021 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3022 state.mstate <- Mpan (x, y);
3024 | Unone | Utext _ ->
3025 if bstate = Glut.DOWN
3026 then (
3027 if conf.angle mod 360 = 0
3028 then (
3029 state.mstate <- Msel ((x, y), (x, y));
3030 Glut.postRedisplay ()
3033 else (
3034 match state.mstate with
3035 | Mnone -> ()
3037 | Mzoom _ | Mscroll ->
3038 state.mstate <- Mnone
3040 | Mpan _ ->
3041 Glut.setCursor Glut.CURSOR_INHERIT;
3042 state.mstate <- Mnone
3044 | Msel ((_, y0), (_, y1)) ->
3045 let f l =
3046 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3047 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3048 then
3049 match getopaque l.pageno with
3050 | Some (opaque, _) when validopaque opaque ->
3051 copysel opaque
3052 | _ -> ()
3054 List.iter f state.layout;
3055 copysel ""; (* ugly *)
3056 Glut.setCursor Glut.CURSOR_INHERIT;
3057 state.mstate <- Mnone;
3061 | _ -> ()
3064 let birdseyemouse button bstate x y
3065 (conf, leftx, _, hooverpageno, anchor) =
3066 match button with
3067 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3068 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3069 let rec loop = function
3070 | [] -> ()
3071 | l :: rest ->
3072 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3073 && x > margin && x < margin + l.pagew
3074 then (
3075 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3077 else loop rest
3079 loop state.layout
3080 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3081 | _ -> ()
3084 let mouse bstate button x y =
3085 match state.mode with
3086 | View -> viewmouse button bstate x y
3087 | Birdseye beye -> birdseyemouse button bstate x y beye
3088 | Textentry _ | Outline _ | Items _ -> ()
3091 let mouse ~button ~state ~x ~y = mouse state button x y;;
3093 let motion ~x ~y =
3094 match state.mode with
3095 | Outline _ -> ()
3096 | _ ->
3097 match state.mstate with
3098 | Mzoom _ | Mnone -> ()
3100 | Mpan (x0, y0) ->
3101 let dx = x - x0
3102 and dy = y0 - y in
3103 state.mstate <- Mpan (x, y);
3104 if conf.zoom > 1.0 then state.x <- state.x + dx;
3105 let y = clamp dy in
3106 gotoy_and_clear_text y
3108 | Msel (a, _) ->
3109 state.mstate <- Msel (a, (x, y));
3110 Glut.postRedisplay ()
3112 | Mscroll ->
3113 let y = min conf.winh (max 0 y) in
3114 let percent = float y /. float conf.winh in
3115 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3116 gotoy_and_clear_text y
3119 let pmotion ~x ~y =
3120 match state.mode with
3121 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3122 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3123 let rec loop = function
3124 | [] ->
3125 if hooverpageno != -1
3126 then (
3127 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3128 Glut.postRedisplay ();
3130 | l :: rest ->
3131 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3132 && x > margin && x < margin + l.pagew
3133 then (
3134 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3135 Glut.postRedisplay ();
3137 else loop rest
3139 loop state.layout
3141 | Outline _ | Items _ | Textentry _ -> ()
3142 | View ->
3143 match state.mstate with
3144 | Mnone ->
3145 begin match getunder x y with
3146 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3147 | Ulinkuri uri ->
3148 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3149 Glut.setCursor Glut.CURSOR_INFO
3150 | Ulinkgoto (page, _) ->
3151 if conf.underinfo
3152 then showtext 'p' ("age: " ^ string_of_int page);
3153 Glut.setCursor Glut.CURSOR_INFO
3154 | Utext s ->
3155 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3156 Glut.setCursor Glut.CURSOR_TEXT
3159 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3163 module State =
3164 struct
3165 open Parser
3167 let fontpath = ref "";;
3169 let home =
3171 match Sys.os_type with
3172 | "Win32" -> Sys.getenv "HOMEPATH"
3173 | _ -> Sys.getenv "HOME"
3174 with exn ->
3175 prerr_endline
3176 ("Can not determine home directory location: " ^
3177 Printexc.to_string exn);
3181 let config_of c attrs =
3182 let apply c k v =
3184 match k with
3185 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3186 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3187 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3188 | "preload" -> { c with preload = bool_of_string v }
3189 | "page-bias" -> { c with pagebias = int_of_string v }
3190 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3191 | "auto-scroll-step" ->
3192 { c with autoscrollstep = max 0 (int_of_string v) }
3193 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3194 | "crop-hack" -> { c with crophack = bool_of_string v }
3195 | "throttle" -> { c with showall = bool_of_string v }
3196 | "highlight-links" -> { c with hlinks = bool_of_string v }
3197 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3198 | "vertical-margin" ->
3199 { c with interpagespace = max 0 (int_of_string v) }
3200 | "zoom" ->
3201 let zoom = float_of_string v /. 100. in
3202 let zoom = max 0.01 zoom in
3203 { c with zoom = zoom }
3204 | "presentation" -> { c with presentation = bool_of_string v }
3205 | "rotation-angle" -> { c with angle = int_of_string v }
3206 | "width" -> { c with winw = max 20 (int_of_string v) }
3207 | "height" -> { c with winh = max 20 (int_of_string v) }
3208 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3209 | "proportional-display" -> { c with proportional = bool_of_string v }
3210 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3211 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3212 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3213 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3214 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3215 | "persistent-location" -> { c with jumpback = bool_of_string v }
3216 | "background-color" -> { c with bgcolor = color_of_string v }
3217 | "scrollbar-in-presentation" ->
3218 { c with scrollbarinpm = bool_of_string v }
3219 | _ -> c
3220 with exn ->
3221 prerr_endline ("Error processing attribute (`" ^
3222 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3225 let rec fold c = function
3226 | [] -> c
3227 | (k, v) :: rest ->
3228 let c = apply c k v in
3229 fold c rest
3231 fold c attrs;
3234 let fromstring f pos n v d =
3235 try f v
3236 with exn ->
3237 dolog "Error processing attribute (%S=%S) at %d\n%s"
3238 n v pos (Printexc.to_string exn)
3243 let bookmark_of attrs =
3244 let rec fold title page rely = function
3245 | ("title", v) :: rest -> fold v page rely rest
3246 | ("page", v) :: rest -> fold title v rely rest
3247 | ("rely", v) :: rest -> fold title page v rest
3248 | _ :: rest -> fold title page rely rest
3249 | [] -> title, page, rely
3251 fold "invalid" "0" "0" attrs
3254 let doc_of attrs =
3255 let rec fold path page rely pan = function
3256 | ("path", v) :: rest -> fold v page rely pan rest
3257 | ("page", v) :: rest -> fold path v rely pan rest
3258 | ("rely", v) :: rest -> fold path page v pan rest
3259 | ("pan", v) :: rest -> fold path page rely v rest
3260 | _ :: rest -> fold path page rely pan rest
3261 | [] -> path, page, rely, pan
3263 fold "" "0" "0" "0" attrs
3266 let setconf dst src =
3267 dst.scrollbw <- src.scrollbw;
3268 dst.scrollh <- src.scrollh;
3269 dst.icase <- src.icase;
3270 dst.preload <- src.preload;
3271 dst.pagebias <- src.pagebias;
3272 dst.verbose <- src.verbose;
3273 dst.scrollstep <- src.scrollstep;
3274 dst.maxhfit <- src.maxhfit;
3275 dst.crophack <- src.crophack;
3276 dst.autoscrollstep <- src.autoscrollstep;
3277 dst.showall <- src.showall;
3278 dst.hlinks <- src.hlinks;
3279 dst.underinfo <- src.underinfo;
3280 dst.interpagespace <- src.interpagespace;
3281 dst.zoom <- src.zoom;
3282 dst.presentation <- src.presentation;
3283 dst.angle <- src.angle;
3284 dst.winw <- src.winw;
3285 dst.winh <- src.winh;
3286 dst.savebmarks <- src.savebmarks;
3287 dst.memlimit <- src.memlimit;
3288 dst.proportional <- src.proportional;
3289 dst.texcount <- src.texcount;
3290 dst.sliceheight <- src.sliceheight;
3291 dst.blockwidth <- src.blockwidth;
3292 dst.thumbw <- src.thumbw;
3293 dst.jumpback <- src.jumpback;
3294 dst.bgcolor <- src.bgcolor;
3295 dst.scrollbarinpm <- src.scrollbarinpm;
3298 let unent s =
3299 let l = String.length s in
3300 let b = Buffer.create l in
3301 unent b s 0 l;
3302 Buffer.contents b;
3305 let get s =
3306 let h = Hashtbl.create 10 in
3307 let dc = { defconf with angle = defconf.angle } in
3308 let rec toplevel v t spos _ =
3309 match t with
3310 | Vdata | Vcdata | Vend -> v
3311 | Vopen ("llppconfig", _, closed) ->
3312 if closed
3313 then v
3314 else { v with f = llppconfig }
3315 | Vopen _ ->
3316 error "unexpected subelement at top level" s spos
3317 | Vclose _ -> error "unexpected close at top level" s spos
3319 and llppconfig v t spos _ =
3320 match t with
3321 | Vdata | Vcdata -> v
3322 | Vend -> error "unexpected end of input in llppconfig" s spos
3323 | Vopen ("defaults", attrs, closed) ->
3324 let c = config_of dc attrs in
3325 setconf dc c;
3326 if closed
3327 then v
3328 else { v with f = skip "defaults" (fun () -> v) }
3330 | Vopen ("ui-font", _, closed) ->
3331 if closed
3332 then v
3333 else { v with f = uifont (Buffer.create 10) }
3335 | Vopen ("doc", attrs, closed) ->
3336 let pathent, spage, srely, span = doc_of attrs in
3337 let path = unent pathent
3338 and pageno = fromstring int_of_string spos "page" spage 0
3339 and rely = fromstring float_of_string spos "rely" srely 0.0
3340 and pan = fromstring int_of_string spos "pan" span 0 in
3341 let c = config_of dc attrs in
3342 let anchor = (pageno, rely) in
3343 if closed
3344 then (Hashtbl.add h path (c, [], pan, anchor); v)
3345 else { v with f = doc path pan anchor c [] }
3347 | Vopen _ ->
3348 error "unexpected subelement in llppconfig" s spos
3350 | Vclose "llppconfig" -> { v with f = toplevel }
3351 | Vclose _ -> error "unexpected close in llppconfig" s spos
3353 and uifont b v t spos epos =
3354 match t with
3355 | Vdata | Vcdata ->
3356 Buffer.add_substring b s spos (epos - spos);
3358 | Vopen (_, _, _) ->
3359 error "unexpected subelement in ui-font" s spos
3360 | Vclose "ui-font" ->
3361 if String.length !fontpath = 0
3362 then fontpath := Buffer.contents b;
3363 { v with f = llppconfig }
3364 | Vclose _ -> error "unexpected close in ui-font" s spos
3365 | Vend -> error "unexpected end of input in ui-font" s spos
3367 and doc path pan anchor c bookmarks v t spos _ =
3368 match t with
3369 | Vdata | Vcdata -> v
3370 | Vend -> error "unexpected end of input in doc" s spos
3371 | Vopen ("bookmarks", _, closed) ->
3372 if closed
3373 then v
3374 else { v with f = pbookmarks path pan anchor c bookmarks }
3376 | Vopen (_, _, _) ->
3377 error "unexpected subelement in doc" s spos
3379 | Vclose "doc" ->
3380 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3381 { v with f = llppconfig }
3383 | Vclose _ -> error "unexpected close in doc" s spos
3385 and pbookmarks path pan anchor c bookmarks v t spos _ =
3386 match t with
3387 | Vdata | Vcdata -> v
3388 | Vend -> error "unexpected end of input in bookmarks" s spos
3389 | Vopen ("item", attrs, closed) ->
3390 let titleent, spage, srely = bookmark_of attrs in
3391 let page = fromstring int_of_string spos "page" spage 0
3392 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3393 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3394 if closed
3395 then { v with f = pbookmarks path pan anchor c bookmarks }
3396 else
3397 let f () = v in
3398 { v with f = skip "item" f }
3400 | Vopen _ ->
3401 error "unexpected subelement in bookmarks" s spos
3403 | Vclose "bookmarks" ->
3404 { v with f = doc path pan anchor c bookmarks }
3406 | Vclose _ -> error "unexpected close in bookmarks" s spos
3408 and skip tag f v t spos _ =
3409 match t with
3410 | Vdata | Vcdata -> v
3411 | Vend ->
3412 error ("unexpected end of input in skipped " ^ tag) s spos
3413 | Vopen (tag', _, closed) ->
3414 if closed
3415 then v
3416 else
3417 let f' () = { v with f = skip tag f } in
3418 { v with f = skip tag' f' }
3419 | Vclose ctag ->
3420 if tag = ctag
3421 then f ()
3422 else error ("unexpected close in skipped " ^ tag) s spos
3425 parse { f = toplevel; accu = () } s;
3426 h, dc;
3429 let do_load f ic =
3431 let len = in_channel_length ic in
3432 let s = String.create len in
3433 really_input ic s 0 len;
3434 f s;
3435 with
3436 | Parse_error (msg, s, pos) ->
3437 let subs = subs s pos in
3438 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3439 failwith ("parse error: " ^ s)
3441 | exn ->
3442 failwith ("config load error: " ^ Printexc.to_string exn)
3445 let defconfpath =
3446 let dir =
3448 let dir = Filename.concat home ".config" in
3449 if Sys.is_directory dir then dir else home
3450 with _ -> home
3452 Filename.concat dir "llpp.conf"
3455 let confpath = ref defconfpath;;
3457 let load1 f =
3458 if Sys.file_exists !confpath
3459 then
3460 match
3461 (try Some (open_in_bin !confpath)
3462 with exn ->
3463 prerr_endline
3464 ("Error opening configuation file `" ^ !confpath ^ "': " ^
3465 Printexc.to_string exn);
3466 None
3468 with
3469 | Some ic ->
3470 begin try
3471 f (do_load get ic)
3472 with exn ->
3473 prerr_endline
3474 ("Error loading configuation from `" ^ !confpath ^ "': " ^
3475 Printexc.to_string exn);
3476 end;
3477 close_in ic;
3479 | None -> ()
3480 else
3481 f (Hashtbl.create 0, defconf)
3484 let load () =
3485 let f (h, dc) =
3486 let pc, pb, px, pa =
3488 Hashtbl.find h (Filename.basename state.path)
3489 with Not_found -> dc, [], 0, (0, 0.0)
3491 setconf defconf dc;
3492 setconf conf pc;
3493 state.bookmarks <- pb;
3494 state.x <- px;
3495 state.scrollw <- conf.scrollbw;
3496 if conf.jumpback
3497 then state.anchor <- pa;
3498 cbput state.hists.nav pa;
3500 load1 f
3503 let add_attrs bb always dc c =
3504 let ob s a b =
3505 if always || a != b
3506 then Printf.bprintf bb "\n %s='%b'" s a
3507 and oi s a b =
3508 if always || a != b
3509 then Printf.bprintf bb "\n %s='%d'" s a
3510 and oz s a b =
3511 if always || a <> b
3512 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
3513 and oc s a b =
3514 if always || a <> b
3515 then
3516 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3518 let w, h =
3519 if always
3520 then dc.winw, dc.winh
3521 else
3522 match state.fullscreen with
3523 | Some wh -> wh
3524 | None -> c.winw, c.winh
3526 let zoom, presentation, interpagespace, showall=
3527 if always
3528 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3529 else
3530 match state.mode with
3531 | Birdseye (bc, _, _, _, _) ->
3532 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3533 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3535 oi "width" w dc.winw;
3536 oi "height" h dc.winh;
3537 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3538 oi "scroll-handle-height" c.scrollh dc.scrollh;
3539 ob "case-insensitive-search" c.icase dc.icase;
3540 ob "preload" c.preload dc.preload;
3541 oi "page-bias" c.pagebias dc.pagebias;
3542 oi "scroll-step" c.scrollstep dc.scrollstep;
3543 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3544 ob "max-height-fit" c.maxhfit dc.maxhfit;
3545 ob "crop-hack" c.crophack dc.crophack;
3546 ob "throttle" showall dc.showall;
3547 ob "highlight-links" c.hlinks dc.hlinks;
3548 ob "under-cursor-info" c.underinfo dc.underinfo;
3549 oi "vertical-margin" interpagespace dc.interpagespace;
3550 oz "zoom" zoom dc.zoom;
3551 ob "presentation" presentation dc.presentation;
3552 oi "rotation-angle" c.angle dc.angle;
3553 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3554 ob "proportional-display" c.proportional dc.proportional;
3555 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3556 oi "tex-count" c.texcount dc.texcount;
3557 oi "slice-height" c.sliceheight dc.sliceheight;
3558 oi "block-width" c.blockwidth dc.blockwidth;
3559 oi "thumbnail-width" c.thumbw dc.thumbw;
3560 ob "persistent-location" c.jumpback dc.jumpback;
3561 oc "background-color" c.bgcolor dc.bgcolor;
3562 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3565 let save () =
3566 let bb = Buffer.create 32768 in
3567 let f (h, dc) =
3568 let dc = if conf.bedefault then conf else dc in
3569 Buffer.add_string bb "<llppconfig>\n";
3571 if String.length !fontpath > 0
3572 then Printf.bprintf bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3574 Buffer.add_string bb "<defaults ";
3575 add_attrs bb true dc dc;
3576 Buffer.add_string bb "/>\n";
3578 let adddoc path pan anchor c bookmarks =
3579 if bookmarks == [] && c = dc && anchor = emptyanchor
3580 then ()
3581 else (
3582 Printf.bprintf bb "<doc path='%s'"
3583 (enent path 0 (String.length path));
3585 if anchor <> emptyanchor
3586 then (
3587 let n, y = anchor in
3588 Printf.bprintf bb " page='%d'" n;
3589 if y > 1e-6
3590 then
3591 Printf.bprintf bb " rely='%f'" y
3595 if pan != 0
3596 then Printf.bprintf bb " pan='%d'" pan;
3598 add_attrs bb false dc c;
3600 begin match bookmarks with
3601 | [] -> Buffer.add_string bb "/>\n"
3602 | _ ->
3603 Buffer.add_string bb ">\n<bookmarks>\n";
3604 List.iter (fun (title, _level, (page, rely)) ->
3605 Printf.bprintf bb
3606 "<item title='%s' page='%d'"
3607 (enent title 0 (String.length title))
3608 page
3610 if rely > 1e-6
3611 then
3612 Printf.bprintf bb " rely='%f'" rely
3614 Buffer.add_string bb "/>\n";
3615 ) bookmarks;
3616 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3617 end;
3621 let pan =
3622 match state.mode with
3623 | Birdseye (_, pan, _, _, _) -> pan
3624 | _ -> state.x
3626 let basename = Filename.basename state.path in
3627 adddoc basename pan (getanchor ())
3628 { conf with
3629 autoscrollstep =
3630 match state.autoscroll with
3631 | Some step -> step
3632 | None -> conf.autoscrollstep }
3633 (if conf.savebmarks then state.bookmarks else []);
3635 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3636 if basename <> path
3637 then adddoc path x y c bookmarks
3638 ) h;
3639 Buffer.add_string bb "</llppconfig>";
3641 load1 f;
3642 if Buffer.length bb > 0
3643 then
3645 let tmp = !confpath ^ ".tmp" in
3646 let oc = open_out_bin tmp in
3647 Buffer.output_buffer oc bb;
3648 close_out oc;
3649 Sys.rename tmp !confpath;
3650 with exn ->
3651 prerr_endline
3652 ("error while saving configuration: " ^ Printexc.to_string exn)
3654 end;;
3656 let () =
3657 Arg.parse
3658 (Arg.align
3659 [("-p", Arg.String (fun s -> state.password <- s) ,
3660 "<password> Set password");
3662 ("-f", Arg.String (fun s -> State.fontpath := s),
3663 "<path> Set path to the user interface font");
3665 ("-c", Arg.String (fun s -> State.confpath := s),
3666 "<path> Set path to the configuration file");
3668 ("-v", Arg.Unit (fun () ->
3669 Printf.printf
3670 "%s\nconfiguration path: %s\n"
3671 Help.version
3672 State.defconfpath
3674 exit 0), " Print version and exit");
3677 (fun s -> state.path <- s)
3678 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3680 if String.length state.path = 0
3681 then (prerr_endline "file name missing"; exit 1);
3683 State.load ();
3685 let _ = Glut.init Sys.argv in
3686 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3687 let () = Glut.initWindowSize conf.winw conf.winh in
3688 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3690 let csock, ssock =
3691 if Sys.os_type = "Unix"
3692 then
3693 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3694 else
3695 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3696 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3697 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3698 Unix.bind sock addr;
3699 Unix.listen sock 1;
3700 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3701 Unix.connect csock addr;
3702 let ssock, _ = Unix.accept sock in
3703 Unix.close sock;
3704 let opts sock =
3705 Unix.setsockopt sock Unix.TCP_NODELAY true;
3706 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3708 opts ssock;
3709 opts csock;
3710 ssock, csock
3713 let () = Glut.displayFunc display in
3714 let () = Glut.reshapeFunc reshape in
3715 let () = Glut.keyboardFunc keyboard in
3716 let () = Glut.specialFunc special in
3717 let () = Glut.idleFunc (Some idle) in
3718 let () = Glut.mouseFunc mouse in
3719 let () = Glut.motionFunc motion in
3720 let () = Glut.passiveMotionFunc pmotion in
3722 init ssock (
3723 conf.angle, conf.proportional, conf.texcount,
3724 conf.sliceheight, conf.blockwidth, !State.fontpath
3726 state.csock <- csock;
3727 state.ssock <- ssock;
3728 state.text <- "Opening " ^ state.path;
3729 writeopen state.path state.password;
3731 while true do
3733 Glut.mainLoop ();
3734 with
3735 | Glut.BadEnum "key in special_of_int" ->
3736 showtext '!' " LablGlut bug: special key not recognized";
3738 | Quit ->
3739 if Sys.os_type <> "Unix"
3740 then Unix.shutdown ssock Unix.SHUTDOWN_ALL;
3741 State.save ();
3742 exit 0
3743 done;