Be more explicit
[llpp.git] / main.ml
blobed54df0eb2e53e426772092aba0dd4849f5ea559
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 type params = angle * proportional * texcount * sliceheight * fontpath
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
25 and fontpath = string
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
37 external measurestr : int -> string -> float = "ml_measure_string";;
39 type mpos = int * int
40 and mstate =
41 | Msel of (mpos * mpos)
42 | Mpan of mpos
43 | Mscroll
44 | Mzoom of (int * int)
45 | Mnone
48 type textentry = string * string * onhist option * onkey * ondone
49 and onkey = string -> int -> te
50 and ondone = string -> unit
51 and histcancel = unit -> unit
52 and onhist = ((histcmd -> string) * histcancel)
53 and histcmd = HCnext | HCprev | HCfirst | HClast
54 and te =
55 | TEstop
56 | TEdone of string
57 | TEcont of string
58 | TEswitch of textentry
61 type 'a circbuf =
62 { store : 'a array
63 ; mutable rc : int
64 ; mutable wc : int
65 ; mutable len : int
69 let cbnew n v =
70 { store = Array.create n v
71 ; rc = 0
72 ; wc = 0
73 ; len = 0
77 let cbcap b = Array.length b.store;;
79 let cbput b v =
80 let cap = cbcap b in
81 b.store.(b.wc) <- v;
82 b.wc <- (b.wc + 1) mod cap;
83 b.rc <- b.wc;
84 b.len <- min (b.len + 1) cap;
87 let cbempty b = b.len = 0;;
89 let cbgetg b circular dir =
90 if cbempty b
91 then b.store.(0)
92 else
93 let rc = b.rc + dir in
94 let rc =
95 if circular
96 then (
97 if rc = -1
98 then b.len-1
99 else (
100 if rc = b.len
101 then 0
102 else rc
105 else max 0 (min rc (b.len-1))
107 b.rc <- rc;
108 b.store.(rc);
111 let cbget b = cbgetg b false;;
112 let cbgetc b = cbgetg b true;;
114 let cbpeek b =
115 let rc = b.wc - b.len in
116 let rc = if rc < 0 then cbcap b + rc else rc in
117 b.store.(rc);
120 let cbdecr b = b.len <- b.len - 1;;
122 type layout =
123 { pageno : int
124 ; pagedimno : int
125 ; pagew : int
126 ; pageh : int
127 ; pagedispy : int
128 ; pagey : int
129 ; pagevh : int
130 ; pagex : int
134 type conf =
135 { mutable scrollbw : int
136 ; mutable scrollh : int
137 ; mutable icase : bool
138 ; mutable preload : bool
139 ; mutable pagebias : int
140 ; mutable verbose : bool
141 ; mutable scrollstep : int
142 ; mutable maxhfit : bool
143 ; mutable crophack : bool
144 ; mutable autoscrollstep : int
145 ; mutable showall : bool
146 ; mutable hlinks : bool
147 ; mutable underinfo : bool
148 ; mutable interpagespace : interpagespace
149 ; mutable zoom : float
150 ; mutable presentation : bool
151 ; mutable angle : angle
152 ; mutable winw : int
153 ; mutable winh : int
154 ; mutable savebmarks : bool
155 ; mutable proportional : proportional
156 ; mutable memlimit : int
157 ; mutable texcount : texcount
158 ; mutable sliceheight : sliceheight
159 ; mutable thumbw : width
160 ; mutable jumpback : bool
161 ; mutable bgcolor : float * float * float
162 ; mutable bedefault : bool
163 ; mutable scrollbarinpm : bool
164 ; mutable uifont : string
168 type anchor = pageno * top;;
170 type outline = string * int * anchor
171 and outlines =
172 | Oarray of outline array
173 | Olist of outline list
174 | Onarrow of string * outline array * outline array
177 type rect = float * float * float * float * float * float * float * float;;
179 type pagemapkey = pageno * width * angle * proportional * gen;;
181 let emptyanchor = (0, 0.0);;
183 type mode =
184 | Birdseye of (conf * leftx * pageno * pageno * anchor)
185 | Outline of (bool * int * int * outline array * string * int * mode)
186 | Items of (int * int * item array * string * int * mode)
187 | Textentry of (textentry * onleave)
188 | View
189 and onleave = leavetextentrystatus -> unit
190 and leavetextentrystatus = | Cancel | Confirm
191 and item = string * int * action
192 and action =
193 | Noaction
194 | Action of (int -> int -> string -> int -> mode)
197 let isbirdseye = function Birdseye _ -> true | _ -> false;;
198 let istextentry = function Textentry _ -> true | _ -> false;;
200 type state =
201 { mutable csock : Unix.file_descr
202 ; mutable ssock : Unix.file_descr
203 ; mutable w : int
204 ; mutable x : int
205 ; mutable y : int
206 ; mutable scrollw : int
207 ; mutable anchor : anchor
208 ; mutable maxy : int
209 ; mutable layout : layout list
210 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
211 ; mutable pdims : (pageno * width * height * leftx) list
212 ; mutable pagecount : int
213 ; pagecache : string circbuf
214 ; mutable rendering : bool
215 ; mutable mstate : mstate
216 ; mutable searchpattern : string
217 ; mutable rects : (pageno * recttype * rect) list
218 ; mutable rects1 : (pageno * recttype * rect) list
219 ; mutable text : string
220 ; mutable fullscreen : (width * height) option
221 ; mutable mode : mode
222 ; mutable outlines : outlines
223 ; mutable bookmarks : outline list
224 ; mutable path : string
225 ; mutable password : string
226 ; mutable invalidated : int
227 ; mutable colorscale : float
228 ; mutable memused : int
229 ; mutable gen : gen
230 ; mutable throttle : layout list option
231 ; mutable ascrollstep : int
232 ; mutable help : item array
233 ; mutable docinfo : (int * string) list
234 ; mutable deadline : float
235 ; hists : hists
237 and hists =
238 { pat : string circbuf
239 ; pag : string circbuf
240 ; nav : anchor circbuf
244 let defconf =
245 { scrollbw = 7
246 ; scrollh = 12
247 ; icase = true
248 ; preload = true
249 ; pagebias = 0
250 ; verbose = false
251 ; scrollstep = 24
252 ; maxhfit = true
253 ; crophack = false
254 ; autoscrollstep = 24
255 ; showall = false
256 ; hlinks = false
257 ; underinfo = false
258 ; interpagespace = 2
259 ; zoom = 1.0
260 ; presentation = false
261 ; angle = 0
262 ; winw = 900
263 ; winh = 900
264 ; savebmarks = true
265 ; proportional = true
266 ; memlimit = 32*1024*1024
267 ; texcount = 256
268 ; sliceheight = 24
269 ; thumbw = 76
270 ; jumpback = false
271 ; bgcolor = (0.5, 0.5, 0.5)
272 ; bedefault = false
273 ; scrollbarinpm = true
274 ; uifont = ""
278 let conf = { defconf with angle = defconf.angle };;
280 let makehelp () =
281 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
282 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
285 let state =
286 { csock = Unix.stdin
287 ; ssock = Unix.stdin
288 ; x = 0
289 ; y = 0
290 ; w = 0
291 ; scrollw = 0
292 ; anchor = emptyanchor
293 ; layout = []
294 ; maxy = max_int
295 ; pagemap = Hashtbl.create 10
296 ; pagecache = cbnew 100 ""
297 ; pdims = []
298 ; pagecount = 0
299 ; rendering = false
300 ; mstate = Mnone
301 ; rects = []
302 ; rects1 = []
303 ; text = ""
304 ; mode = View
305 ; fullscreen = None
306 ; searchpattern = ""
307 ; outlines = Olist []
308 ; bookmarks = []
309 ; path = ""
310 ; password = ""
311 ; invalidated = 0
312 ; hists =
313 { nav = cbnew 100 (0, 0.0)
314 ; pat = cbnew 20 ""
315 ; pag = cbnew 10 ""
317 ; colorscale = 1.0
318 ; memused = 0
319 ; gen = 0
320 ; throttle = None
321 ; ascrollstep = 0
322 ; help = makehelp ()
323 ; docinfo = []
324 ; deadline = nan
328 let vlog fmt =
329 if conf.verbose
330 then
331 Printf.kprintf prerr_endline fmt
332 else
333 Printf.kprintf ignore fmt
336 let writecmd fd s =
337 let len = String.length s in
338 let n = 4 + len in
339 let b = Buffer.create n in
340 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
341 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
342 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
343 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
344 Buffer.add_string b s;
345 let s' = Buffer.contents b in
346 let n' = Unix.write fd s' 0 n in
347 if n' != n then failwith "write failed";
350 let readcmd fd =
351 let s = "xxxx" in
352 let n = Unix.read fd s 0 4 in
353 if n != 4 then failwith "incomplete read(len)";
354 let len = 0
355 lor (Char.code s.[0] lsl 24)
356 lor (Char.code s.[1] lsl 16)
357 lor (Char.code s.[2] lsl 8)
358 lor (Char.code s.[3] lsl 0)
360 let s = String.create len in
361 let n = Unix.read fd s 0 len in
362 if n != len then failwith "incomplete read(data)";
366 let makecmd s l =
367 let b = Buffer.create 10 in
368 Buffer.add_string b s;
369 let rec combine = function
370 | [] -> b
371 | x :: xs ->
372 Buffer.add_char b ' ';
373 let s =
374 match x with
375 | `b b -> if b then "1" else "0"
376 | `s s -> s
377 | `i i -> string_of_int i
378 | `f f -> string_of_float f
379 | `I f -> string_of_int (truncate f)
381 Buffer.add_string b s;
382 combine xs;
384 combine l;
387 let wcmd s l =
388 let cmd = Buffer.contents (makecmd s l) in
389 writecmd state.csock cmd;
392 let calcips h =
393 if conf.presentation
394 then
395 let d = conf.winh - h in
396 max 0 ((d + 1) / 2)
397 else
398 conf.interpagespace
401 let calcheight () =
402 let rec f pn ph pi fh l =
403 match l with
404 | (n, _, h, _) :: rest ->
405 let ips = calcips h in
406 let fh =
407 if conf.presentation
408 then fh+ips
409 else (
410 if isbirdseye state.mode && pn = 0
411 then fh + ips
412 else fh
415 let fh = fh + ((n - pn) * (ph + pi)) in
416 f n h ips fh rest;
418 | [] ->
419 let inc =
420 if conf.presentation || (isbirdseye state.mode && pn = 0)
421 then 0
422 else -pi
424 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
425 max 0 fh
427 let fh = f 0 0 0 0 state.pdims in
431 let getpageyh pageno =
432 let rec f pn ph pi y l =
433 match l with
434 | (n, _, h, _) :: rest ->
435 let ips = calcips h in
436 if n >= pageno
437 then
438 let h = if n = pageno then h else ph in
439 if conf.presentation && n = pageno
440 then
441 y + (pageno - pn) * (ph + pi) + pi, h
442 else
443 y + (pageno - pn) * (ph + pi), h
444 else
445 let y = y + (if conf.presentation then pi else 0) in
446 let y = y + (n - pn) * (ph + pi) in
447 f n h ips y rest
449 | [] ->
450 y + (pageno - pn) * (ph + pi), ph
452 f 0 0 0 0 state.pdims
455 let getpagey pageno = fst (getpageyh pageno);;
457 let layout y sh =
458 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
459 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
460 match pdims with
461 | (pageno', w, h, x) :: rest when pageno' = pageno ->
462 let ips = calcips h in
463 let yinc =
464 if conf.presentation || (isbirdseye state.mode && pageno = 0)
465 then ips
466 else 0
468 (w, h, ips, x), rest, pdimno + 1, yinc
469 | _ ->
470 prev, pdims, pdimno, 0
472 let dy = dy + yinc in
473 let py = py + yinc in
474 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
475 then
476 accu
477 else
478 let vy = y + dy in
479 if py + h <= vy - yinc
480 then
481 let py = py + h + ips in
482 let dy = max 0 (py - y) in
483 f ~pageno:(pageno+1)
484 ~pdimno
485 ~prev:curr
488 ~pdims:rest
489 ~cacheleft
490 ~accu
491 else
492 let pagey = vy - py in
493 let pagevh = h - pagey in
494 let pagevh = min (sh - dy) pagevh in
495 let off = if yinc > 0 then py - vy else 0 in
496 let py = py + h + ips in
497 let e =
498 { pageno = pageno
499 ; pagedimno = pdimno
500 ; pagew = w
501 ; pageh = h
502 ; pagedispy = dy + off
503 ; pagey = pagey + off
504 ; pagevh = pagevh - off
505 ; pagex = x
508 let accu = e :: accu in
509 f ~pageno:(pageno+1)
510 ~pdimno
511 ~prev:curr
513 ~dy:(dy+pagevh+ips)
514 ~pdims:rest
515 ~cacheleft:(cacheleft-1)
516 ~accu
518 if state.invalidated = 0
519 then (
520 let accu =
522 ~pageno:0
523 ~pdimno:~-1
524 ~prev:(0,0,0,0)
525 ~py:0
526 ~dy:0
527 ~pdims:state.pdims
528 ~cacheleft:(cbcap state.pagecache)
529 ~accu:[]
531 List.rev accu
533 else
537 let clamp incr =
538 let y = state.y + incr in
539 let y = max 0 y in
540 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
544 let getopaque pageno =
545 try Some (Hashtbl.find state.pagemap
546 (pageno, state.w, conf.angle, conf.proportional, state.gen))
547 with Not_found -> None
550 let cache pageno opaque =
551 Hashtbl.replace state.pagemap
552 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
555 let validopaque opaque = String.length opaque > 0;;
557 let render l =
558 match getopaque l.pageno with
559 | None when not state.rendering ->
560 state.rendering <- true;
561 cache l.pageno ("", -1);
562 wcmd "render" [`i (l.pageno + 1)
563 ;`i l.pagedimno
564 ;`i l.pagew
565 ;`i l.pageh];
566 | _ -> ()
569 let loadlayout layout =
570 let rec f all = function
571 | l :: ls ->
572 begin match getopaque l.pageno with
573 | None -> render l; f false ls
574 | Some (opaque, _) -> f (all && validopaque opaque) ls
576 | [] -> all
578 f (layout <> []) layout;
581 let findpageforopaque opaque =
582 Hashtbl.fold
583 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
584 state.pagemap None
587 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
589 let preload () =
590 let oktopreload =
591 if conf.preload
592 then
593 let memleft = conf.memlimit - state.memused in
594 if memleft < 0
595 then
596 let opaque = cbpeek state.pagecache in
597 match findpageforopaque opaque with
598 | Some ((n, _, _, _, _), size) ->
599 memleft + size >= 0 && not (pagevisible state.layout n)
600 | None -> false
601 else true
602 else false
604 if oktopreload
605 then
606 let presentation = conf.presentation in
607 let interpagespace = conf.interpagespace in
608 let maxy = state.maxy in
609 conf.presentation <- false;
610 conf.interpagespace <- 0;
611 state.maxy <- calcheight ();
612 let y =
613 match state.layout with
614 | [] -> 0
615 | l :: _ -> getpagey l.pageno + l.pagey
617 let y = if y < conf.winh then 0 else y - conf.winh in
618 let pages = layout y (conf.winh*3) in
619 List.iter render pages;
620 conf.presentation <- presentation;
621 conf.interpagespace <- interpagespace;
622 state.maxy <- maxy;
625 let gotoy y =
626 let y = max 0 y in
627 let y = min state.maxy y in
628 let pages = layout y conf.winh in
629 let ready = loadlayout pages in
630 if conf.showall
631 then (
632 if ready
633 then (
634 state.y <- y;
635 state.layout <- pages;
636 state.throttle <- None;
637 Glut.postRedisplay ();
639 else (
640 state.throttle <- Some pages;
643 else (
644 state.y <- y;
645 state.layout <- pages;
646 state.throttle <- None;
647 Glut.postRedisplay ();
649 begin match state.mode with
650 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
651 if not (pagevisible pages pageno)
652 then (
653 match state.layout with
654 | [] -> ()
655 | l :: _ ->
656 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
658 | _ -> ()
659 end;
660 preload ();
663 let gotoy_and_clear_text y =
664 gotoy y;
665 if not conf.verbose then state.text <- "";
668 let getanchor () =
669 match state.layout with
670 | [] -> emptyanchor
671 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
674 let getanchory (n, top) =
675 let y, h = getpageyh n in
676 y + (truncate (top *. float h));
679 let gotoanchor anchor =
680 gotoy (getanchory anchor);
683 let addnav () =
684 cbput state.hists.nav (getanchor ());
687 let getnav dir =
688 let anchor = cbgetc state.hists.nav dir in
689 getanchory anchor;
692 let gotopage n top =
693 let y, h = getpageyh n in
694 gotoy_and_clear_text (y + (truncate (top *. float h)));
697 let gotopage1 n top =
698 let y = getpagey n in
699 gotoy_and_clear_text (y + top);
702 let invalidate () =
703 state.layout <- [];
704 state.pdims <- [];
705 state.rects <- [];
706 state.rects1 <- [];
707 state.invalidated <- state.invalidated + 1;
710 let scalecolor c =
711 let c = c *. state.colorscale in
712 (c, c, c);
715 let scalecolor2 (r, g, b) =
716 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
719 let represent () =
720 state.maxy <- calcheight ();
721 match state.mode with
722 | Birdseye (_, _, pageno, _, _) ->
723 let y, h = getpageyh pageno in
724 let top = (conf.winh - h) / 2 in
725 gotoy (max 0 (y - top))
726 | _ -> gotoanchor state.anchor
729 let pagematrix () =
730 GlMat.mode `projection;
731 GlMat.load_identity ();
732 GlMat.rotate ~x:1.0 ~angle:180.0 ();
733 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
734 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
735 if state.x != 0
736 then (
737 GlMat.translate ~x:(float state.x) ();
741 let winmatrix () =
742 GlMat.mode `projection;
743 GlMat.load_identity ();
744 GlMat.rotate ~x:1.0 ~angle:180.0 ();
745 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
746 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
749 let reshape =
750 let firsttime = ref true in
751 fun ~w ~h ->
752 if state.invalidated = 0 && not !firsttime
753 then (state.anchor <- getanchor (); firsttime := false);
755 conf.winw <- w;
756 let w = truncate (float w *. conf.zoom) - state.scrollw in
757 let w = max w 2 in
758 state.w <- w;
759 conf.winh <- h;
760 GlMat.mode `modelview;
761 GlMat.load_identity ();
762 GlClear.color (scalecolor 1.0);
763 GlClear.clear [`color];
765 invalidate ();
766 wcmd "geometry" [`i w; `i h];
769 let drawstring size x y s =
770 Gl.enable `blend;
771 Gl.enable `texture_2d;
772 ignore (drawstr size x y s);
773 Gl.disable `blend;
774 Gl.disable `texture_2d;
777 let drawstring1 size x y s =
778 drawstr size x y s;
781 let enttext () =
782 let len = String.length state.text in
783 let drawstring s =
784 GlDraw.color (0.0, 0.0, 0.0);
785 GlDraw.rect
786 (0.0, float (conf.winh - 18))
787 (float (conf.winw - state.scrollw - 1), float conf.winh)
789 GlDraw.color (1.0, 1.0, 1.0);
790 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
792 match state.mode with
793 | Textentry ((prefix, text, _, _, _), _) ->
794 let s =
795 if len > 0
796 then
797 Printf.sprintf "%s%s_ [%s]" prefix text state.text
798 else
799 Printf.sprintf "%s%s_" prefix text
801 drawstring s
803 | _ ->
804 if len > 0 then drawstring state.text
807 let showtext c s =
808 state.text <- Printf.sprintf "%c%s" c s;
809 Glut.postRedisplay ();
812 let act cmd =
813 match cmd.[0] with
814 | 'c' ->
815 state.pdims <- [];
817 | 'D' ->
818 state.rects <- state.rects1;
819 Glut.postRedisplay ()
821 | 'C' ->
822 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
823 state.pagecount <- n;
824 state.invalidated <- state.invalidated - 1;
825 if state.invalidated = 0
826 then represent ()
828 | 't' ->
829 let s = Scanf.sscanf cmd "t %n"
830 (fun n -> String.sub cmd n (String.length cmd - n))
832 Glut.setWindowTitle s
834 | 'T' ->
835 let s = Scanf.sscanf cmd "T %n"
836 (fun n -> String.sub cmd n (String.length cmd - n))
838 if istextentry state.mode
839 then (
840 state.text <- s;
841 showtext ' ' s;
843 else (
844 state.text <- s;
845 Glut.postRedisplay ();
848 | 'V' ->
849 if conf.verbose
850 then
851 let s = Scanf.sscanf cmd "V %n"
852 (fun n -> String.sub cmd n (String.length cmd - n))
854 state.text <- s;
855 showtext ' ' s;
857 | 'F' ->
858 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
859 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
860 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
861 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
863 let y = (getpagey pageno) + truncate y0 in
864 addnav ();
865 gotoy y;
866 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
868 | 'R' ->
869 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
870 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
871 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
872 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
874 state.rects1 <-
875 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
877 | 'r' ->
878 let n, w, _h, r, l, s, p =
879 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
880 (fun n w h r l s p ->
881 (n-1, w, h, r, l != 0, s, p))
884 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
885 state.memused <- state.memused + s;
887 let layout =
888 match state.throttle with
889 | None -> state.layout
890 | Some layout -> layout
893 let rec gc () =
894 if (state.memused <= conf.memlimit) || cbempty state.pagecache
895 then ()
896 else (
897 let evictedopaque = cbpeek state.pagecache in
898 match findpageforopaque evictedopaque with
899 | None -> failwith "bug in gc"
900 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
901 if state.gen != gen || not (pagevisible layout evictedn)
902 then (
903 wcmd "free" [`s evictedopaque];
904 state.memused <- state.memused - evictedsize;
905 Hashtbl.remove state.pagemap k;
906 cbdecr state.pagecache;
907 gc ();
911 gc ();
913 cbput state.pagecache p;
914 state.rendering <- false;
916 begin match state.throttle with
917 | None ->
918 if pagevisible state.layout n
919 then gotoy state.y
920 else (
921 let allvisible = loadlayout state.layout in
922 if allvisible then preload ();
925 | Some layout ->
926 match layout with
927 | [] -> ()
928 | l :: _ ->
929 let y = getpagey l.pageno + l.pagey in
930 gotoy y
933 | 'l' ->
934 let pdim =
935 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
937 state.pdims <- pdim :: state.pdims
939 | 'o' ->
940 let (l, n, t, h, pos) =
941 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
943 let s = String.sub cmd pos (String.length cmd - pos) in
944 let outline = (s, l, (n, float t /. float h)) in
945 let outlines =
946 match state.outlines with
947 | Olist outlines -> Olist (outline :: outlines)
948 | Oarray _ -> Olist [outline]
949 | Onarrow _ -> Olist [outline]
951 state.outlines <- outlines
954 | 'i' ->
955 let s = Scanf.sscanf cmd "i %n"
956 (fun n -> String.sub cmd n (String.length cmd - n))
958 let len = String.length s in
959 let rec fold accu pos =
960 let eolpos =
961 try String.index_from s pos '\n' with Not_found -> len
963 if eolpos = len
964 then List.rev accu
965 else
966 let line = String.sub s pos (eolpos - pos) in
967 fold ((1, line)::accu) (eolpos+1)
969 state.docinfo <- fold state.docinfo 0
971 | _ ->
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday;;
977 let idle () =
978 if state.deadline == nan then state.deadline <- now ();
979 let rec loop delay =
980 let timeout =
981 if delay > 0.0
982 then max 0.0 (state.deadline -. now ())
983 else 0.0
985 let r, _, _ = Unix.select [state.csock] [] [] timeout in
986 state.deadline <- state.deadline +. delay;
987 begin match r with
988 | [] ->
989 if state.ascrollstep > 0
990 then begin
991 let y = state.y + state.ascrollstep in
992 let y = if y >= state.maxy then 0 else y in
993 gotoy y;
994 state.text <- "";
995 end;
997 | _ ->
998 let cmd = readcmd state.csock in
999 act cmd;
1000 loop 0.0
1001 end;
1002 in loop 0.007
1005 let onhist cb =
1006 let rc = cb.rc in
1007 let action = function
1008 | HCprev -> cbget cb ~-1
1009 | HCnext -> cbget cb 1
1010 | HCfirst -> cbget cb ~-(cb.rc)
1011 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1012 and cancel () = cb.rc <- rc
1013 in (action, cancel)
1016 let search pattern forward =
1017 if String.length pattern > 0
1018 then
1019 let pn, py =
1020 match state.layout with
1021 | [] -> 0, 0
1022 | l :: _ ->
1023 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1025 let cmd =
1026 let b = makecmd "search"
1027 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1029 Buffer.add_char b ',';
1030 Buffer.add_string b pattern;
1031 Buffer.add_char b '\000';
1032 Buffer.contents b;
1034 writecmd state.csock cmd;
1037 let intentry text key =
1038 let c = Char.unsafe_chr key in
1039 match c with
1040 | '0' .. '9' ->
1041 let s = "x" in s.[0] <- c;
1042 let text = text ^ s in
1043 TEcont text
1045 | _ ->
1046 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1047 TEcont text
1050 let addchar s c =
1051 let b = Buffer.create (String.length s + 1) in
1052 Buffer.add_string b s;
1053 Buffer.add_char b c;
1054 Buffer.contents b;
1057 let textentry text key =
1058 let c = Char.unsafe_chr key in
1059 match c with
1060 | _ when key >= 32 && key < 127 ->
1061 let text = addchar text c in
1062 TEcont text
1064 | _ ->
1065 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1066 TEcont text
1069 let reinit angle proportional =
1070 conf.angle <- angle;
1071 conf.proportional <- proportional;
1072 invalidate ();
1073 wcmd "reinit" [`i angle; `b proportional];
1076 let setzoom zoom =
1077 let zoom = max 0.01 (min 2.2 zoom) in
1078 if zoom <> conf.zoom
1079 then (
1080 if zoom <= 1.0
1081 then state.x <- 0;
1082 conf.zoom <- zoom;
1083 reshape conf.winw conf.winh;
1084 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1088 let enterbirdseye () =
1089 let zoom = float conf.thumbw /. float conf.winw in
1090 let birdseyepageno =
1091 let cy = conf.winh / 2 in
1092 let fold = function
1093 | [] -> 0
1094 | l :: rest ->
1095 let rec fold best = function
1096 | [] -> best.pageno
1097 | l :: rest ->
1098 let d = cy - (l.pagedispy + l.pagevh/2)
1099 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1100 if abs d < abs dbest
1101 then fold l rest
1102 else best.pageno
1103 in fold l rest
1105 fold state.layout
1107 state.mode <- Birdseye (
1108 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1110 conf.zoom <- zoom;
1111 conf.presentation <- false;
1112 conf.interpagespace <- 10;
1113 conf.hlinks <- false;
1114 state.x <- 0;
1115 state.mstate <- Mnone;
1116 conf.showall <- false;
1117 Glut.setCursor Glut.CURSOR_INHERIT;
1118 if conf.verbose
1119 then
1120 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1121 (100.0*.zoom)
1122 else
1123 state.text <- ""
1125 reshape conf.winw conf.winh;
1128 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1129 state.mode <- View;
1130 conf.zoom <- c.zoom;
1131 conf.presentation <- c.presentation;
1132 conf.interpagespace <- c.interpagespace;
1133 conf.showall <- c.showall;
1134 conf.hlinks <- c.hlinks;
1135 state.x <- leftx;
1136 if conf.verbose
1137 then
1138 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1139 (100.0*.conf.zoom)
1141 reshape conf.winw conf.winh;
1142 state.anchor <- if goback then anchor else (pageno, 0.0);
1145 let togglebirdseye () =
1146 match state.mode with
1147 | Birdseye vals -> leavebirdseye vals true
1148 | View | Outline _ -> enterbirdseye ()
1149 | _ -> ()
1152 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1153 let pageno = max 0 (pageno - 1) in
1154 let rec loop = function
1155 | [] -> gotopage1 pageno 0
1156 | l :: _ when l.pageno = pageno ->
1157 if l.pagedispy >= 0 && l.pagey = 0
1158 then Glut.postRedisplay ()
1159 else gotopage1 pageno 0
1160 | _ :: rest -> loop rest
1162 loop state.layout;
1163 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1166 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1167 let pageno = min (state.pagecount - 1) (pageno + 1) in
1168 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1169 let rec loop = function
1170 | [] ->
1171 let y, h = getpageyh pageno in
1172 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1173 gotoy (clamp dy)
1174 | l :: _ when l.pageno = pageno ->
1175 if l.pagevh != l.pageh
1176 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1177 else Glut.postRedisplay ()
1178 | _ :: rest -> loop rest
1180 loop state.layout
1183 let optentry mode _ key =
1184 let btos b = if b then "on" else "off" in
1185 let c = Char.unsafe_chr key in
1186 match c with
1187 | 's' ->
1188 let ondone s =
1189 try conf.scrollstep <- int_of_string s with exc ->
1190 state.text <- Printf.sprintf "bad integer `%s': %s"
1191 s (Printexc.to_string exc)
1193 TEswitch ("scroll step", "", None, intentry, ondone)
1195 | 'A' ->
1196 let ondone s =
1198 conf.autoscrollstep <- int_of_string s;
1199 if state.ascrollstep > 0
1200 then state.ascrollstep <- conf.autoscrollstep;
1201 with exc ->
1202 state.text <- Printf.sprintf "bad integer `%s': %s"
1203 s (Printexc.to_string exc)
1205 TEswitch ("auto scroll step", "", None, intentry, ondone)
1207 | 'Z' ->
1208 let ondone s =
1210 let zoom = float (int_of_string s) /. 100.0 in
1211 setzoom zoom
1212 with exc ->
1213 state.text <- Printf.sprintf "bad integer `%s': %s"
1214 s (Printexc.to_string exc)
1216 TEswitch ("zoom", "", None, intentry, ondone)
1218 | 't' ->
1219 let ondone s =
1221 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1222 state.text <-
1223 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1224 begin match mode with
1225 | Birdseye beye ->
1226 leavebirdseye beye false;
1227 enterbirdseye ();
1228 | _ -> ();
1230 with exc ->
1231 state.text <- Printf.sprintf "bad integer `%s': %s"
1232 s (Printexc.to_string exc)
1234 TEswitch ("thumbnail width", "", None, intentry, ondone)
1236 | 'R' ->
1237 let ondone s =
1238 match try
1239 Some (int_of_string s)
1240 with exc ->
1241 state.text <- Printf.sprintf "bad integer `%s': %s"
1242 s (Printexc.to_string exc);
1243 None
1244 with
1245 | Some angle -> reinit angle conf.proportional
1246 | None -> ()
1248 TEswitch ("rotation", "", None, intentry, ondone)
1250 | 'i' ->
1251 conf.icase <- not conf.icase;
1252 TEdone ("case insensitive search " ^ (btos conf.icase))
1254 | 'p' ->
1255 conf.preload <- not conf.preload;
1256 gotoy state.y;
1257 TEdone ("preload " ^ (btos conf.preload))
1259 | 'v' ->
1260 conf.verbose <- not conf.verbose;
1261 TEdone ("verbose " ^ (btos conf.verbose))
1263 | 'h' ->
1264 conf.maxhfit <- not conf.maxhfit;
1265 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1266 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1268 | 'c' ->
1269 conf.crophack <- not conf.crophack;
1270 TEdone ("crophack " ^ btos conf.crophack)
1272 | 'a' ->
1273 conf.showall <- not conf.showall;
1274 TEdone ("throttle " ^ btos conf.showall)
1276 | 'f' ->
1277 conf.underinfo <- not conf.underinfo;
1278 TEdone ("underinfo " ^ btos conf.underinfo)
1280 | 'P' ->
1281 conf.savebmarks <- not conf.savebmarks;
1282 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1284 | 'S' ->
1285 let ondone s =
1287 let pageno, py =
1288 match state.layout with
1289 | [] -> 0, 0
1290 | l :: _ ->
1291 l.pageno, l.pagey
1293 conf.interpagespace <- int_of_string s;
1294 state.maxy <- calcheight ();
1295 let y = getpagey pageno in
1296 gotoy (y + py)
1297 with exc ->
1298 state.text <- Printf.sprintf "bad integer `%s': %s"
1299 s (Printexc.to_string exc)
1301 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1303 | 'l' ->
1304 reinit conf.angle (not conf.proportional);
1305 TEdone ("proprortional display: " ^ btos conf.proportional)
1307 | _ ->
1308 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1309 TEstop
1312 let maxoutlinerows () = (conf.winh - 31) / 16;;
1314 let enterselector allowdel outlines errmsg msg =
1315 if Array.length outlines = 0
1316 then (
1317 showtext ' ' errmsg;
1319 else (
1320 state.text <- msg;
1321 Glut.setCursor Glut.CURSOR_INHERIT;
1322 let pageno =
1323 match state.layout with
1324 | [] -> -1
1325 | {pageno=pageno} :: _ -> pageno
1327 let active =
1328 let rec loop n =
1329 if n = Array.length outlines
1330 then 0
1331 else
1332 let (_, _, (outlinepageno, _)) = outlines.(n) in
1333 if outlinepageno >= pageno then n else loop (n+1)
1335 loop 0
1337 state.mode <- Outline
1338 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1339 state.mode);
1340 Glut.postRedisplay ();
1344 let enteroutlinemode () =
1345 let outlines, msg =
1346 match state.outlines with
1347 | Oarray a -> a, ""
1348 | Olist l ->
1349 let a = Array.of_list (List.rev l) in
1350 state.outlines <- Oarray a;
1351 a, ""
1352 | Onarrow (pat, a, _) ->
1353 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1355 enterselector false outlines "Document has no outline" msg;
1358 let enterbookmarkmode () =
1359 let bookmarks = Array.of_list state.bookmarks in
1360 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1363 let mode_to_string mode =
1364 let b = Buffer.create 10 in
1365 let rec f = function
1366 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1367 | View -> Buffer.add_string b "View"
1368 | Birdseye _ -> Buffer.add_string b "Birdseye"
1369 | Items _ -> Buffer.add_string b "Items"
1370 | Outline _ -> Buffer.add_string b "Outline"
1372 f mode;
1373 Buffer.contents b;
1376 let color_of_string s =
1377 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1378 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1382 let color_to_string (r, g, b) =
1383 let r = truncate (r *. 256.0)
1384 and g = truncate (g *. 256.0)
1385 and b = truncate (b *. 256.0) in
1386 Printf.sprintf "%d/%d/%d" r g b
1389 let enterinfomode () =
1390 let btos = function true -> "on" | _ -> "off" in
1391 let mode = state.mode in
1392 let rec makeitems () =
1393 let intp name get set =
1394 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1395 fun active first _ pan ->
1396 let ondone s =
1397 let n =
1398 try int_of_string s
1399 with exn ->
1400 state.text <- Printf.sprintf "bad integer `%s': %s"
1401 s (Printexc.to_string exn);
1402 max_int;
1404 if n != max_int then set n;
1406 let te = name ^ ": ", "", None, intentry, ondone in
1407 state.text <- "";
1408 Textentry (
1410 fun _ ->
1411 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1414 and boolp name get set =
1415 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1416 fun active first qsearch pan ->
1417 let v = get () in
1418 set (not v);
1419 Items (active, first, makeitems (), qsearch, pan, mode);
1421 and colorp name get set =
1422 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1423 fun active first _ pan ->
1424 let invalid = (nan, nan, nan) in
1425 let ondone s =
1426 let c =
1427 try color_of_string s
1428 with exn ->
1429 state.text <- Printf.sprintf "bad color `%s': %s"
1430 s (Printexc.to_string exn);
1431 invalid
1433 if c <> invalid
1434 then set c;
1436 let te = name ^ ": ", "", None, textentry, ondone in
1437 state.text <- "";
1438 Textentry (
1440 fun _ ->
1441 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1446 let birdseye = isbirdseye mode in
1447 let items = [
1448 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1450 boolp "presentation"
1451 (fun () -> conf.presentation)
1452 (fun v ->
1453 conf.presentation <- v;
1454 state.anchor <- getanchor ();
1455 represent ());
1457 boolp "ignore case in searches"
1458 (fun () -> conf.icase)
1459 (fun v -> conf.icase <- v);
1461 boolp "preload"
1462 (fun () -> conf.preload)
1463 (fun v -> conf.preload <- v);
1465 boolp "verbose"
1466 (fun () -> conf.verbose)
1467 (fun v -> conf.verbose <- v);
1469 boolp "max fit"
1470 (fun () -> conf.maxhfit)
1471 (fun v -> conf.maxhfit <- v);
1473 boolp "crop hack"
1474 (fun () -> conf.crophack)
1475 (fun v -> conf.crophack <- v);
1477 boolp "throttle"
1478 (fun () -> conf.showall)
1479 (fun v -> conf.showall <- v);
1481 boolp "highlight links"
1482 (fun () -> conf.hlinks)
1483 (fun v -> conf.hlinks <- v);
1485 boolp "under info"
1486 (fun () -> conf.underinfo)
1487 (fun v -> conf.underinfo <- v);
1488 boolp "persistent bookmarks"
1489 (fun () -> conf.savebmarks)
1490 (fun v -> conf.savebmarks <- v);
1492 boolp "proportional display"
1493 (fun () -> conf.proportional)
1494 (fun v -> reinit conf.angle v);
1496 boolp "persistent location"
1497 (fun () -> conf.jumpback)
1498 (fun v -> conf.jumpback <- v);
1500 "", 0, Noaction;
1502 intp "vertical margin"
1503 (fun () -> conf.interpagespace)
1504 (fun n ->
1505 conf.interpagespace <- n;
1506 let pageno, py =
1507 match state.layout with
1508 | [] -> 0, 0
1509 | l :: _ ->
1510 l.pageno, l.pagey
1512 state.maxy <- calcheight ();
1513 let y = getpagey pageno in
1514 gotoy (y + py)
1517 intp "page bias"
1518 (fun () -> conf.pagebias)
1519 (fun v -> conf.pagebias <- v);
1521 intp "scroll step"
1522 (fun () -> conf.scrollstep)
1523 (fun n -> conf.scrollstep <- n);
1525 intp "auto scroll step"
1526 (fun () ->
1527 if state.ascrollstep > 0
1528 then state.ascrollstep
1529 else conf.autoscrollstep)
1530 (fun n ->
1531 if state.ascrollstep > 0
1532 then state.ascrollstep <- n
1533 else conf.autoscrollstep <- n);
1535 intp "zoom"
1536 (fun () -> truncate (conf.zoom *. 100.))
1537 (fun v -> setzoom ((float v) /. 100.));
1539 intp "rotation"
1540 (fun () -> conf.angle)
1541 (fun v -> reinit v conf.proportional);
1543 intp "scroll bar width"
1544 (fun () -> state.scrollw)
1545 (fun v ->
1546 state.scrollw <- v;
1547 conf.scrollbw <- v;
1548 reshape conf.winw conf.winh;
1551 intp "scroll handle height"
1552 (fun () -> conf.scrollh)
1553 (fun v -> conf.scrollh <- v;);
1555 intp "thumbnail width"
1556 (fun () -> conf.thumbw)
1557 (fun v ->
1558 conf.thumbw <- min 1920 v;
1559 match mode with
1560 | Birdseye beye ->
1561 leavebirdseye beye false;
1562 enterbirdseye ()
1563 | _ -> ()
1566 colorp "background color"
1567 (fun () -> conf.bgcolor)
1568 (fun v -> conf.bgcolor <- v);
1570 "", 0, Noaction;
1571 "Presentation mode", 0, Noaction;
1573 boolp "scrollbar visible"
1574 (fun () -> conf.scrollbarinpm)
1575 (fun v ->
1576 if v != conf.scrollbarinpm
1577 then (
1578 conf.scrollbarinpm <- v;
1579 if conf.presentation
1580 then (
1581 state.scrollw <- if v then conf.scrollbw else 0;
1582 reshape conf.winw conf.winh;
1587 "", 0, Noaction;
1588 "Pixmap Cache", 0, Noaction;
1590 intp "size (advisory)"
1591 (fun () -> conf.memlimit)
1592 (fun v -> conf.memlimit <- v);
1593 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1597 let tailer =
1598 let trailer =
1599 ("", 0, Noaction)
1600 :: ("Document", 0, Noaction)
1601 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1603 if birdseye
1604 then trailer
1605 else (
1606 ("", 0, Noaction)
1607 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1608 (btos conf.bedefault),
1610 Action (
1611 fun active first qsearch pan ->
1612 conf.bedefault <- not conf.bedefault;
1613 Items (active, first, makeitems (), qsearch, pan, mode);
1615 ) :: trailer
1618 Array.of_list (items @ tailer)
1620 state.text <- "";
1621 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1622 Glut.postRedisplay ();
1625 let enterhelpmode () =
1626 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1627 Glut.postRedisplay ();
1630 let quickbookmark ?title () =
1631 match state.layout with
1632 | [] -> ()
1633 | l :: _ ->
1634 let title =
1635 match title with
1636 | None ->
1637 let sec = Unix.gettimeofday () in
1638 let tm = Unix.localtime sec in
1639 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1640 (l.pageno+1)
1641 tm.Unix.tm_mday
1642 tm.Unix.tm_mon
1643 (tm.Unix.tm_year + 1900)
1644 tm.Unix.tm_hour
1645 tm.Unix.tm_min
1646 | Some title -> title
1648 state.bookmarks <-
1649 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1650 :: state.bookmarks
1653 let doreshape w h =
1654 state.fullscreen <- None;
1655 Glut.reshapeWindow w h;
1658 let writeopen path password =
1659 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1660 writecmd state.csock "info";
1663 let opendoc path password =
1664 invalidate ();
1665 state.path <- path;
1666 state.password <- password;
1667 state.gen <- state.gen + 1;
1669 writeopen path password;
1670 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1671 wcmd "geometry" [`i state.w; `i conf.winh];
1674 let viewkeyboard key =
1675 let enttext te =
1676 let mode = state.mode in
1677 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1678 state.text <- "";
1679 enttext ();
1680 Glut.postRedisplay ()
1682 let c = Char.chr key in
1683 match c with
1684 | '\027' | 'q' -> (* escape *)
1685 exit 0
1687 | '\008' -> (* backspace *)
1688 let y = getnav ~-1 in
1689 gotoy_and_clear_text y
1691 | 'o' ->
1692 enteroutlinemode ()
1694 | 'u' ->
1695 state.rects <- [];
1696 state.text <- "";
1697 Glut.postRedisplay ()
1699 | '/' | '?' ->
1700 let ondone isforw s =
1701 cbput state.hists.pat s;
1702 state.searchpattern <- s;
1703 search s isforw
1705 let s = String.create 1 in
1706 s.[0] <- c;
1707 enttext (s, "", Some (onhist state.hists.pat),
1708 textentry, ondone (c ='/'))
1710 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1711 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1712 setzoom (min 2.2 (conf.zoom +. incr))
1714 | '+' ->
1715 let ondone s =
1716 let n =
1717 try int_of_string s with exc ->
1718 state.text <- Printf.sprintf "bad integer `%s': %s"
1719 s (Printexc.to_string exc);
1720 max_int
1722 if n != max_int
1723 then (
1724 conf.pagebias <- n;
1725 state.text <- "page bias is now " ^ string_of_int n;
1728 enttext ("page bias: ", "", None, intentry, ondone)
1730 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1731 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1732 setzoom (max 0.01 (conf.zoom -. decr))
1734 | '-' ->
1735 let ondone msg = state.text <- msg in
1736 enttext (
1737 "option [acfhilpstvAPRSZ]: ", "", None,
1738 optentry state.mode, ondone
1741 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1742 setzoom 1.0
1744 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1745 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1746 if zoom < 1.0
1747 then setzoom zoom
1749 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1750 togglebirdseye ()
1752 | '0' .. '9' ->
1753 let ondone s =
1754 let n =
1755 try int_of_string s with exc ->
1756 state.text <- Printf.sprintf "bad integer `%s': %s"
1757 s (Printexc.to_string exc);
1760 if n >= 0
1761 then (
1762 addnav ();
1763 cbput state.hists.pag (string_of_int n);
1764 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1767 let pageentry text key =
1768 match Char.unsafe_chr key with
1769 | 'g' -> TEdone text
1770 | _ -> intentry text key
1772 let text = "x" in text.[0] <- c;
1773 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1775 | 'b' ->
1776 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1777 reshape conf.winw conf.winh;
1779 | 'l' ->
1780 conf.hlinks <- not conf.hlinks;
1781 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1782 Glut.postRedisplay ()
1784 | 'a' ->
1785 if state.ascrollstep = 0
1786 then state.ascrollstep <- conf.autoscrollstep
1787 else (
1788 conf.autoscrollstep <- state.ascrollstep;
1789 state.ascrollstep <- 0;
1792 | 'P' ->
1793 conf.presentation <- not conf.presentation;
1794 if conf.presentation
1795 then (
1796 if not conf.scrollbarinpm
1797 then state.scrollw <- 0;
1799 else
1800 state.scrollw <- conf.scrollbw;
1802 showtext ' ' ("presentation mode " ^
1803 if conf.presentation then "on" else "off");
1804 state.anchor <- getanchor ();
1805 represent ()
1807 | 'f' ->
1808 begin match state.fullscreen with
1809 | None ->
1810 state.fullscreen <- Some (conf.winw, conf.winh);
1811 Glut.fullScreen ()
1812 | Some (w, h) ->
1813 state.fullscreen <- None;
1814 doreshape w h
1817 | 'g' ->
1818 gotoy_and_clear_text 0
1820 | 'n' ->
1821 search state.searchpattern true
1823 | 'p' | 'N' ->
1824 search state.searchpattern false
1826 | 't' ->
1827 begin match state.layout with
1828 | [] -> ()
1829 | l :: _ ->
1830 gotoy_and_clear_text (getpagey l.pageno)
1833 | ' ' ->
1834 begin match List.rev state.layout with
1835 | [] -> ()
1836 | l :: _ ->
1837 let pageno = min (l.pageno+1) (state.pagecount-1) in
1838 gotoy_and_clear_text (getpagey pageno)
1841 | '\127' -> (* del *)
1842 begin match state.layout with
1843 | [] -> ()
1844 | l :: _ ->
1845 let pageno = max 0 (l.pageno-1) in
1846 gotoy_and_clear_text (getpagey pageno)
1849 | '=' ->
1850 let f (fn, _) l =
1851 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1853 let fn, ln = List.fold_left f (-1, -1) state.layout in
1854 let s =
1855 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1856 let percent =
1857 if maxy <= 0
1858 then 100.
1859 else (100. *. (float state.y /. float maxy)) in
1860 if fn = ln
1861 then
1862 Printf.sprintf "Page %d of %d %.2f%%"
1863 (fn+1) state.pagecount percent
1864 else
1865 Printf.sprintf
1866 "Pages %d-%d of %d %.2f%%"
1867 (fn+1) (ln+1) state.pagecount percent
1869 showtext ' ' s;
1871 | 'w' ->
1872 begin match state.layout with
1873 | [] -> ()
1874 | l :: _ ->
1875 doreshape (l.pagew + state.scrollw) l.pageh;
1876 Glut.postRedisplay ();
1879 | '\'' ->
1880 enterbookmarkmode ()
1882 | 'h' ->
1883 enterhelpmode ()
1885 | 'i' ->
1886 enterinfomode ()
1888 | 'm' ->
1889 let ondone s =
1890 match state.layout with
1891 | l :: _ ->
1892 state.bookmarks <-
1893 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1894 :: state.bookmarks
1895 | _ -> ()
1897 enttext ("bookmark: ", "", None, textentry, ondone)
1899 | '~' ->
1900 quickbookmark ();
1901 showtext ' ' "Quick bookmark added";
1903 | 'z' ->
1904 begin match state.layout with
1905 | l :: _ ->
1906 let rect = getpdimrect l.pagedimno in
1907 let w, h =
1908 if conf.crophack
1909 then
1910 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1911 truncate (1.2 *. (rect.(3) -. rect.(0))))
1912 else
1913 (truncate (rect.(1) -. rect.(0)),
1914 truncate (rect.(3) -. rect.(0)))
1916 if w != 0 && h != 0
1917 then
1918 doreshape (w + state.scrollw) (h + conf.interpagespace)
1920 Glut.postRedisplay ();
1922 | [] -> ()
1925 | '<' | '>' ->
1926 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1928 | '[' | ']' ->
1929 state.colorscale <-
1930 max 0.0
1931 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1932 Glut.postRedisplay ()
1934 | 'k' ->
1935 begin match state.mode with
1936 | Birdseye beye -> upbirdseye beye
1937 | _ -> gotoy (clamp (-conf.scrollstep))
1940 | 'j' ->
1941 begin match state.mode with
1942 | Birdseye beye -> downbirdseye beye
1943 | _ -> gotoy (clamp conf.scrollstep)
1946 | 'r' ->
1947 state.anchor <- getanchor ();
1948 opendoc state.path state.password
1950 | _ ->
1951 vlog "huh? %d %c" key (Char.chr key);
1954 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1955 let enttext te =
1956 state.mode <- Textentry (te, onleave);
1957 state.text <- "";
1958 enttext ();
1959 Glut.postRedisplay ()
1961 match Char.unsafe_chr key with
1962 | '\008' -> (* backspace *)
1963 let len = String.length text in
1964 if len = 0
1965 then (
1966 onleave Cancel;
1967 Glut.postRedisplay ();
1969 else (
1970 let s = String.sub text 0 (len - 1) in
1971 enttext (c, s, opthist, onkey, ondone)
1974 | '\r' | '\n' ->
1975 ondone text;
1976 onleave Confirm;
1977 Glut.postRedisplay ()
1979 | '\027' -> (* escape *)
1980 begin match opthist with
1981 | None -> ()
1982 | Some (_, onhistcancel) -> onhistcancel ()
1983 end;
1984 onleave Cancel;
1985 Glut.postRedisplay ()
1987 | _ ->
1988 begin match onkey text key with
1989 | TEdone text ->
1990 onleave Confirm;
1991 ondone text;
1992 Glut.postRedisplay ()
1994 | TEcont text ->
1995 enttext (c, text, opthist, onkey, ondone);
1997 | TEstop ->
1998 onleave Cancel;
1999 Glut.postRedisplay ()
2001 | TEswitch te ->
2002 state.mode <- Textentry (te, onleave);
2003 Glut.postRedisplay ()
2004 end;
2007 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2008 match key with
2009 | 27 -> (* escape *)
2010 leavebirdseye beye true
2012 | 12 -> (* ctrl-l *)
2013 let y, h = getpageyh pageno in
2014 let top = (conf.winh - h) / 2 in
2015 gotoy (max 0 (y - top))
2017 | 13 -> (* enter *)
2018 leavebirdseye beye false
2020 | _ ->
2021 viewkeyboard key
2024 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2025 let set active first qsearch =
2026 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2028 let search active pattern incr =
2029 let dosearch re =
2030 let rec loop n =
2031 if n >= Array.length items || n < 0
2032 then None
2033 else
2034 let (s, _, _) = items.(n) in
2036 (try ignore (Str.search_forward re s 0); true
2037 with Not_found -> false)
2038 then Some n
2039 else loop (n + incr)
2041 loop active
2044 let re = Str.regexp_case_fold pattern in
2045 dosearch re
2046 with Failure s ->
2047 state.text <- s;
2048 None
2050 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2051 match key with
2052 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2053 let incr = if key = 18 then -1 else 1 in
2054 let active, first =
2055 match search (active + incr) qsearch incr with
2056 | None ->
2057 state.text <- qsearch ^ " [not found]";
2058 active, first
2059 | Some active ->
2060 state.text <- qsearch;
2061 active, firstof active
2063 set active first qsearch;
2064 Glut.postRedisplay ();
2066 | 8 -> (* backspace *)
2067 let len = String.length qsearch in
2068 if len = 0
2069 then ()
2070 else (
2071 if len = 1
2072 then (
2073 state.text <- "";
2074 set active first "";
2076 else
2077 let qsearch = String.sub qsearch 0 (len - 1) in
2078 let active, first =
2079 match search active qsearch ~-1 with
2080 | None ->
2081 state.text <- qsearch ^ " [not found]";
2082 active, first
2083 | Some active ->
2084 state.text <- qsearch;
2085 active, firstof active
2087 set active first qsearch
2089 Glut.postRedisplay ()
2091 | _ when key >= 32 && key < 127 ->
2092 let pattern = addchar qsearch (Char.chr key) in
2093 let active, first =
2094 match search active pattern 1 with
2095 | None ->
2096 state.text <- pattern ^ " [not found]";
2097 active, first
2098 | Some active ->
2099 state.text <- pattern;
2100 active, firstof active
2102 set active first pattern;
2103 Glut.postRedisplay ()
2105 | 27 -> (* escape *)
2106 state.text <- "";
2107 if String.length qsearch = 0
2108 then (
2109 state.mode <- oldmode;
2111 else (
2112 set active first "";
2114 Glut.postRedisplay ()
2116 | 13 -> (* enter *)
2117 if active >= 0 && active < Array.length items
2118 then (
2119 match items.(active) with
2120 | _, _, Action f ->
2121 state.mode <- f active first qsearch pan
2123 | _, _, Noaction ->
2124 state.text <- "";
2125 state.mode <- oldmode
2127 else (
2128 state.text <- "";
2129 state.mode <- oldmode
2131 Glut.postRedisplay ();
2133 | _ -> dolog "unknown key %d" key
2136 let outlinekeyboard key
2137 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2138 let narrow outlines pattern =
2139 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2140 match reopt with
2141 | None -> None
2142 | Some re ->
2143 let rec fold accu n =
2144 if n = -1
2145 then accu
2146 else
2147 let (s, _, _) as o = outlines.(n) in
2148 let accu =
2149 if (try ignore (Str.search_forward re s 0); true
2150 with Not_found -> false)
2151 then (o :: accu)
2152 else accu
2154 fold accu (n-1)
2156 let matched = fold [] (Array.length outlines - 1) in
2157 if matched = [] then None else Some (Array.of_list matched)
2159 let search active pattern incr =
2160 let dosearch re =
2161 let rec loop n =
2162 if n = Array.length outlines || n = -1
2163 then None
2164 else
2165 let (s, _, _) = outlines.(n) in
2167 (try ignore (Str.search_forward re s 0); true
2168 with Not_found -> false)
2169 then Some n
2170 else loop (n + incr)
2172 loop active
2175 let re = Str.regexp_case_fold pattern in
2176 dosearch re
2177 with Failure s ->
2178 state.text <- s;
2179 None
2181 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2182 match key with
2183 | 27 -> (* escape *)
2184 state.text <- "";
2185 if String.length qsearch = 0
2186 then (
2187 state.mode <- oldmode;
2189 else (
2190 state.mode <- Outline (
2191 allowdel, active, first, outlines, "", pan, oldmode
2194 Glut.postRedisplay ();
2196 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2197 let incr = if key = 18 then -1 else 1 in
2198 let active, first =
2199 match search (active + incr) qsearch incr with
2200 | None ->
2201 state.text <- qsearch ^ " [not found]";
2202 active, first
2203 | Some active ->
2204 state.text <- qsearch;
2205 active, firstof active
2207 state.mode <- Outline (
2208 allowdel, active, first, outlines, qsearch, pan, oldmode
2210 Glut.postRedisplay ();
2212 | 8 -> (* backspace *)
2213 let len = String.length qsearch in
2214 if len = 0
2215 then ()
2216 else (
2217 if len = 1
2218 then (
2219 state.text <- "";
2220 state.mode <- Outline (
2221 allowdel, active, first, outlines, "", pan, oldmode
2224 else
2225 let qsearch = String.sub qsearch 0 (len - 1) in
2226 let active, first =
2227 match search active qsearch ~-1 with
2228 | None ->
2229 state.text <- qsearch ^ " [not found]";
2230 active, first
2231 | Some active ->
2232 state.text <- qsearch;
2233 active, firstof active
2235 state.mode <- Outline (
2236 allowdel, active, first, outlines, qsearch, pan, oldmode
2239 Glut.postRedisplay ()
2241 | 13 -> (* enter *)
2242 if active < Array.length outlines
2243 then (
2244 let (_, _, anchor) = outlines.(active) in
2245 addnav ();
2246 gotoanchor anchor;
2248 state.text <- "";
2249 if allowdel then state.bookmarks <- Array.to_list outlines;
2250 state.mode <- oldmode;
2251 Glut.postRedisplay ();
2253 | _ when key >= 32 && key < 127 ->
2254 let pattern = addchar qsearch (Char.chr key) in
2255 let active, first =
2256 match search active pattern 1 with
2257 | None ->
2258 state.text <- pattern ^ " [not found]";
2259 active, first
2260 | Some active ->
2261 state.text <- pattern;
2262 active, firstof active
2264 state.mode <- Outline (
2265 allowdel, active, first, outlines, pattern, pan, oldmode
2267 Glut.postRedisplay ()
2269 | 14 when not allowdel -> (* ctrl-n *)
2270 if String.length qsearch > 0
2271 then (
2272 let optoutlines = narrow outlines qsearch in
2273 begin match optoutlines with
2274 | None -> state.text <- "can't narrow"
2275 | Some outlines ->
2276 state.mode <- Outline (
2277 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2279 match state.outlines with
2280 | Olist _ -> ()
2281 | Oarray a ->
2282 state.outlines <- Onarrow (qsearch, outlines, a)
2283 | Onarrow (_, _, b) ->
2284 state.outlines <- Onarrow (qsearch, outlines, b)
2285 end;
2287 Glut.postRedisplay ()
2289 | 21 when not allowdel -> (* ctrl-u *)
2290 let outline =
2291 match state.outlines with
2292 | Oarray a -> a
2293 | Olist l ->
2294 let a = Array.of_list (List.rev l) in
2295 state.outlines <- Oarray a;
2297 | Onarrow (_, _, b) ->
2298 state.outlines <- Oarray b;
2299 state.text <- "";
2302 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2303 Glut.postRedisplay ()
2305 | 12 -> (* ctrl-l *)
2306 state.mode <- Outline
2307 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2308 Glut.postRedisplay ()
2310 | 127 when allowdel -> (* delete *)
2311 let len = Array.length outlines - 1 in
2312 if len = 0
2313 then (
2314 state.mode <- View;
2315 state.bookmarks <- [];
2317 else (
2318 let bookmarks = Array.init len
2319 (fun i ->
2320 let i = if i >= active then i + 1 else i in
2321 outlines.(i)
2324 state.mode <-
2325 Outline (
2326 allowdel,
2327 min active (len-1),
2328 min first (len-1),
2329 bookmarks, qsearch,
2331 oldmode
2334 Glut.postRedisplay ()
2336 | _ -> dolog "unknown key %d" key
2339 let keyboard ~key ~x ~y =
2340 ignore x;
2341 ignore y;
2342 if key = 7 (* ctrl-g *)
2343 then
2344 wcmd "interrupt" []
2345 else
2346 match state.mode with
2347 | Outline outline -> outlinekeyboard key outline
2348 | Textentry textentry -> textentrykeyboard key textentry
2349 | Birdseye birdseye -> birdseyekeyboard key birdseye
2350 | View -> viewkeyboard key
2351 | Items items -> itemskeyboard key items
2354 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2355 match key with
2356 | Glut.KEY_UP -> upbirdseye beye
2357 | Glut.KEY_DOWN -> downbirdseye beye
2359 | Glut.KEY_PAGE_UP ->
2360 begin match state.layout with
2361 | l :: _ ->
2362 if l.pagey != 0
2363 then (
2364 state.mode <- Birdseye (
2365 conf, leftx, l.pageno, hooverpageno, anchor
2367 gotopage1 l.pageno 0;
2369 else (
2370 let layout = layout (state.y-conf.winh) conf.winh in
2371 match layout with
2372 | [] -> gotoy (clamp (-conf.winh))
2373 | l :: _ ->
2374 state.mode <- Birdseye (
2375 conf, leftx, l.pageno, hooverpageno, anchor
2377 gotopage1 l.pageno 0
2380 | [] -> gotoy (clamp (-conf.winh))
2381 end;
2383 | Glut.KEY_PAGE_DOWN ->
2384 begin match List.rev state.layout with
2385 | l :: _ ->
2386 let layout = layout (state.y + conf.winh) conf.winh in
2387 begin match layout with
2388 | [] ->
2389 let incr = l.pageh - l.pagevh in
2390 if incr = 0
2391 then (
2392 state.mode <-
2393 Birdseye (
2394 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2396 Glut.postRedisplay ();
2398 else gotoy (clamp (incr + conf.interpagespace*2));
2400 | l :: _ ->
2401 state.mode <-
2402 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2403 gotopage1 l.pageno 0;
2406 | [] -> gotoy (clamp conf.winh)
2407 end;
2409 | Glut.KEY_HOME ->
2410 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2411 gotopage1 0 0
2413 | Glut.KEY_END ->
2414 let pageno = state.pagecount - 1 in
2415 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2416 if not (pagevisible state.layout pageno)
2417 then
2418 let h =
2419 match List.rev state.pdims with
2420 | [] -> conf.winh
2421 | (_, _, h, _) :: _ -> h
2423 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2424 else Glut.postRedisplay ();
2425 | _ -> ()
2428 let setautoscrollspeed goingdown =
2429 let incr = max 1 (state.ascrollstep / 2) in
2430 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2431 state.ascrollstep <- astep;
2434 let special ~key ~x ~y =
2435 ignore x;
2436 ignore y;
2437 match state.mode with
2438 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2439 togglebirdseye ()
2441 | Birdseye vals ->
2442 birdseyespecial key vals
2444 | View when key = Glut.KEY_F1 ->
2445 enterhelpmode ()
2447 | View ->
2448 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2449 then setautoscrollspeed (key = Glut.KEY_DOWN)
2450 else
2451 let y =
2452 match key with
2453 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2454 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2455 | Glut.KEY_DOWN -> clamp conf.scrollstep
2456 | Glut.KEY_PAGE_UP ->
2457 if Glut.getModifiers () land Glut.active_ctrl != 0
2458 then
2459 match state.layout with
2460 | [] -> state.y
2461 | l :: _ -> state.y - l.pagey
2462 else
2463 clamp (-conf.winh)
2464 | Glut.KEY_PAGE_DOWN ->
2465 if Glut.getModifiers () land Glut.active_ctrl != 0
2466 then
2467 match List.rev state.layout with
2468 | [] -> state.y
2469 | l :: _ -> getpagey l.pageno
2470 else
2471 clamp conf.winh
2472 | Glut.KEY_HOME -> addnav (); 0
2473 | Glut.KEY_END ->
2474 addnav ();
2475 state.maxy - (if conf.maxhfit then conf.winh else 0)
2477 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2478 Glut.getModifiers () land Glut.active_alt != 0 ->
2479 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2481 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2482 state.x <- state.x - 10;
2483 state.y
2484 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2485 state.x <- state.x + 10;
2486 state.y
2488 | _ -> state.y
2490 gotoy_and_clear_text y
2492 | Textentry
2493 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2494 let s =
2495 match key with
2496 | Glut.KEY_UP -> action HCprev
2497 | Glut.KEY_DOWN -> action HCnext
2498 | Glut.KEY_HOME -> action HCfirst
2499 | Glut.KEY_END -> action HClast
2500 | _ -> state.text
2502 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2503 Glut.postRedisplay ()
2505 | Textentry _ -> ()
2507 | Items (active, first, items, qsearch, pan, oldmode) ->
2508 let maxrows = maxoutlinerows () in
2509 let itemcount = Array.length items in
2510 let hasaction = function
2511 | (_, _, Noaction) -> false
2512 | _ -> true
2514 let find start incr =
2515 let rec find i =
2516 if i = -1 || i = itemcount
2517 then -1
2518 else (
2519 if hasaction items.(i)
2520 then i
2521 else find (i + incr)
2524 find start
2526 let set active first =
2527 let first = max 0 (min first (itemcount - maxrows)) in
2528 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2530 let navigate incr =
2531 let isvisible first n = n >= first && n - first <= maxrows in
2532 let active, first =
2533 let incr1 = if incr > 0 then 1 else -1 in
2534 if isvisible first active
2535 then
2536 let next =
2537 let next = active + incr in
2538 let next =
2539 if next < 0 || next >= itemcount
2540 then -1
2541 else find next incr1
2543 if next = -1 || abs (active - next) > maxrows
2544 then -1
2545 else next
2547 if next = -1
2548 then
2549 let first = first + incr in
2550 let first = max 0 (min first (itemcount - 1)) in
2551 let next =
2552 let next = active + incr in
2553 let next = max 0 (min next (itemcount - 1)) in
2554 find next ~-incr1
2556 let active = if next = -1 then active else next in
2557 active, first
2558 else
2559 let first = min next first in
2560 next, first
2561 else
2562 let first = first + incr in
2563 let first = max 0 (min first (itemcount - 1)) in
2564 let active =
2565 let next = active + incr in
2566 let next = max 0 (min next (itemcount - 1)) in
2567 let next = find next incr1 in
2568 if next = -1 || abs (active - first) > maxrows
2569 then active
2570 else next
2572 active, first
2574 set active first;
2575 Glut.postRedisplay ()
2577 begin match key with
2578 | Glut.KEY_UP -> navigate ~-1
2579 | Glut.KEY_DOWN -> navigate 1
2580 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2581 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2583 | Glut.KEY_RIGHT ->
2584 state.mode <- Items (
2585 active, first, items, qsearch, min 0 (pan - 1), oldmode
2587 Glut.postRedisplay ()
2589 | Glut.KEY_LEFT ->
2590 state.mode <- Items (
2591 active, first, items, qsearch, min 0 (pan + 1), oldmode
2593 Glut.postRedisplay ()
2595 | Glut.KEY_HOME ->
2596 let active = find 0 1 in
2597 set active 0;
2598 Glut.postRedisplay ()
2600 | Glut.KEY_END ->
2601 let first = max 0 (itemcount - maxrows) in
2602 let active = find (itemcount - 1) ~-1 in
2603 set active first;
2604 Glut.postRedisplay ()
2606 | _ -> ()
2607 end;
2609 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2610 let maxrows = maxoutlinerows () in
2611 let calcfirst first active =
2612 if active > first
2613 then
2614 let rows = active - first in
2615 if rows > maxrows then active - maxrows else first
2616 else active
2618 let navigate incr =
2619 let active = active + incr in
2620 let active = max 0 (min active (Array.length outlines - 1)) in
2621 let first = calcfirst first active in
2622 state.mode <- Outline (
2623 allowdel, active, first, outlines, qsearch, pan, oldmode
2625 Glut.postRedisplay ()
2627 let updownlevel incr =
2628 let len = Array.length outlines in
2629 let (_, curlevel, _) = outlines.(active) in
2630 let rec flow i =
2631 if i = len then i-1 else if i = -1 then 0 else
2632 let (_, l, _) = outlines.(i) in
2633 if l != curlevel then i else flow (i+incr)
2635 let active = flow active in
2636 let first = calcfirst first active in
2637 state.mode <- Outline (
2638 allowdel, active, first, outlines, qsearch, pan, oldmode
2640 Glut.postRedisplay ()
2642 match key with
2643 | Glut.KEY_UP -> navigate ~-1
2644 | Glut.KEY_DOWN -> navigate 1
2645 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2646 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2648 | Glut.KEY_RIGHT ->
2649 if Glut.getModifiers () land Glut.active_ctrl != 0
2650 then (
2651 state.mode <- Outline (
2652 allowdel, active, first, outlines,
2653 qsearch, min 0 (pan + 1), oldmode
2655 Glut.postRedisplay ();
2657 else (
2658 if not allowdel
2659 then updownlevel 1
2662 | Glut.KEY_LEFT ->
2663 if Glut.getModifiers () land Glut.active_ctrl != 0
2664 then (
2665 state.mode <- Outline (
2666 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2668 Glut.postRedisplay ();
2670 else (
2671 if not allowdel
2672 then updownlevel ~-1
2675 | Glut.KEY_HOME ->
2676 state.mode <- Outline (
2677 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2679 Glut.postRedisplay ()
2681 | Glut.KEY_END ->
2682 let active = Array.length outlines - 1 in
2683 let first = max 0 (active - maxrows) in
2684 state.mode <- Outline (
2685 allowdel, active, first, outlines, qsearch, pan, oldmode
2687 Glut.postRedisplay ()
2689 | _ -> ()
2692 let drawplaceholder l =
2693 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2694 GlDraw.rect
2695 (float l.pagex, float l.pagedispy)
2696 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2698 let x = if margin < 0 then -margin else l.pagex
2699 and y = l.pagedispy + 13 in
2700 GlDraw.color (0.0, 0.0, 0.0);
2701 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2704 let now () = Unix.gettimeofday ();;
2706 let drawpage l =
2707 let color =
2708 match state.mode with
2709 | Textentry _ -> scalecolor 0.4
2710 | View | Outline _ | Items _ -> scalecolor 1.0
2711 | Birdseye (_, _, pageno, hooverpageno, _) ->
2712 if l.pageno = hooverpageno
2713 then scalecolor 0.9
2714 else (
2715 if l.pageno = pageno
2716 then scalecolor 1.0
2717 else scalecolor 0.8
2720 GlDraw.color color;
2721 begin match getopaque l.pageno with
2722 | Some (opaque, _) when validopaque opaque ->
2723 let a = now () in
2724 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2725 opaque;
2726 let b = now () in
2727 let d = b-.a in
2728 vlog "draw %d %f sec" l.pageno d;
2730 | _ ->
2731 drawplaceholder l;
2732 end;
2735 let scrollph y =
2736 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2737 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2738 let sh = float conf.winh /. sh in
2739 let sh = max sh (float conf.scrollh) in
2741 let percent =
2742 if state.y = state.maxy
2743 then 1.0
2744 else float y /. float maxy
2746 let position = (float conf.winh -. sh) *. percent in
2748 let position =
2749 if position +. sh > float conf.winh
2750 then float conf.winh -. sh
2751 else position
2753 position, sh;
2756 let scrollindicator () =
2757 GlDraw.color (0.64 , 0.64, 0.64);
2758 GlDraw.rect
2759 (float (conf.winw - state.scrollw), 0.)
2760 (float conf.winw, float conf.winh)
2762 GlDraw.color (0.0, 0.0, 0.0);
2764 let position, sh = scrollph state.y in
2765 GlDraw.rect
2766 (float (conf.winw - state.scrollw), position)
2767 (float conf.winw, position +. sh)
2771 let showsel margin =
2772 match state.mstate with
2773 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2776 | Msel ((x0, y0), (x1, y1)) ->
2777 let rec loop = function
2778 | l :: ls ->
2779 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2780 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2781 then
2782 match getopaque l.pageno with
2783 | Some (opaque, _) when validopaque opaque ->
2784 let oy = -l.pagey + l.pagedispy in
2785 seltext opaque
2786 (x0 - margin - state.x, y0,
2787 x1 - margin - state.x, y1) oy;
2789 | _ -> ()
2790 else loop ls
2791 | [] -> ()
2793 loop state.layout
2796 let showrects () =
2797 let panx = float state.x in
2798 Gl.enable `blend;
2799 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2800 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2801 List.iter
2802 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2803 List.iter (fun l ->
2804 if l.pageno = pageno
2805 then (
2806 let d = float (l.pagedispy - l.pagey) in
2807 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2808 GlDraw.begins `quads;
2810 GlDraw.vertex2 (x0+.panx, y0+.d);
2811 GlDraw.vertex2 (x1+.panx, y1+.d);
2812 GlDraw.vertex2 (x2+.panx, y2+.d);
2813 GlDraw.vertex2 (x3+.panx, y3+.d);
2815 GlDraw.ends ();
2817 ) state.layout
2818 ) state.rects
2820 Gl.disable `blend;
2823 let showstrings trusted active first pan strings =
2824 Gl.enable `blend;
2825 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2826 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2827 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2828 GlDraw.color (1., 1., 1.);
2829 Gl.enable `texture_2d;
2831 let wx = measurestr 15 "w" in
2832 let tabx = 30.0*.wx +. float (pan*15) in
2833 let rec loop row =
2834 if row = Array.length strings || (row - first) * 16 > conf.winh
2835 then ()
2836 else (
2837 let (s, level, _) = strings.(row) in
2838 let y = (row - first) * 16 in
2839 let x = 5 + 15*(max 0 (level+pan)) in
2840 if row = active
2841 then (
2842 Gl.disable `texture_2d;
2843 GlDraw.polygon_mode `both `line;
2844 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2845 GlDraw.rect (0., float (y + 1))
2846 (float (conf.winw - 1), float (y + 18));
2847 GlDraw.polygon_mode `both `fill;
2848 GlDraw.color (1., 1., 1.);
2849 Gl.enable `texture_2d;
2852 let drawtabularstring x s =
2853 let _ =
2854 if trusted
2855 then
2856 let tabpos = try String.index s '\t' with Not_found -> -1 in
2857 if tabpos > 0
2858 then
2859 let len = String.length s - tabpos - 1 in
2860 let s1 = String.sub s 0 tabpos
2861 and s2 = String.sub s (tabpos + 1) len in
2862 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2863 let x = truncate (max xx tabx) in
2864 drawstring1 15 x (y + 16) s2
2865 else
2866 drawstring1 15 x (y + 16) s
2867 else
2868 drawstring1 15 x (y + 16) s
2872 drawtabularstring (x + pan*15) s;
2873 loop (row+1)
2876 loop first;
2877 Gl.disable `blend;
2878 Gl.disable `texture_2d;
2881 let showoutline (_, active, first, outlines, _, pan, _) =
2882 showstrings false active first pan outlines;
2885 let showitems (active, first, items, _, pan, _) =
2886 showstrings true active first pan items;
2889 let display () =
2890 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2891 GlDraw.viewport margin 0 state.w conf.winh;
2892 pagematrix ();
2893 GlClear.color (scalecolor2 conf.bgcolor);
2894 GlClear.clear [`color];
2895 if conf.zoom > 1.0
2896 then (
2897 Gl.enable `scissor_test;
2898 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2900 List.iter drawpage state.layout;
2901 if conf.zoom > 1.0
2902 then
2903 Gl.disable `scissor_test
2905 if state.x != 0
2906 then (
2907 let x = -.float state.x in
2908 GlMat.translate ~x ();
2910 showrects ();
2911 showsel margin;
2912 GlDraw.viewport 0 0 conf.winw conf.winh;
2913 winmatrix ();
2914 scrollindicator ();
2915 begin match state.mode with
2916 | Items items -> showitems items
2917 | Outline outline -> showoutline outline
2918 | _ -> ()
2919 end;
2920 enttext ();
2921 Glut.swapBuffers ();
2924 let getunder x y =
2925 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2926 let x = x - margin - state.x in
2927 let rec f = function
2928 | l :: rest ->
2929 begin match getopaque l.pageno with
2930 | Some (opaque, _) when validopaque opaque ->
2931 let y = y - l.pagedispy in
2932 if y > 0
2933 then
2934 let y = l.pagey + y in
2935 let x = x - l.pagex in
2936 match whatsunder opaque x y with
2937 | Unone -> f rest
2938 | under -> under
2939 else
2940 f rest
2941 | _ ->
2942 f rest
2944 | [] -> Unone
2946 f state.layout
2949 let viewmouse button bstate x y =
2950 match button with
2951 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2952 if Glut.getModifiers () land Glut.active_ctrl != 0
2953 then (
2954 match state.mstate with
2955 | Mzoom (oldn, i) ->
2956 if oldn = n
2957 then (
2958 if i = 2
2959 then
2960 let incr =
2961 match n with
2962 | 4 ->
2963 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2964 | _ ->
2965 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2967 let zoom = conf.zoom +. incr in
2968 setzoom zoom;
2969 state.mstate <- Mzoom (n, 0);
2970 else
2971 state.mstate <- Mzoom (n, i+1);
2973 else state.mstate <- Mzoom (n, 0)
2975 | _ -> state.mstate <- Mzoom (n, 0)
2977 else (
2978 if state.ascrollstep > 0
2979 then
2980 setautoscrollspeed (n=4)
2981 else
2982 let incr =
2983 if n = 3
2984 then -conf.scrollstep
2985 else conf.scrollstep
2987 let incr = incr * 2 in
2988 let y = clamp incr in
2989 gotoy_and_clear_text y
2992 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2993 if bstate = Glut.DOWN
2994 then (
2995 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2996 state.mstate <- Mpan (x, y)
2998 else
2999 state.mstate <- Mnone
3001 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3002 if bstate = Glut.DOWN
3003 then
3004 let position, sh = scrollph state.y in
3005 if y > truncate position && y < truncate (position +. sh)
3006 then
3007 state.mstate <- Mscroll
3008 else
3009 let percent = float y /. float conf.winh in
3010 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3011 gotoy desty;
3012 state.mstate <- Mscroll
3013 else
3014 state.mstate <- Mnone
3016 | Glut.LEFT_BUTTON ->
3017 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3018 begin match dest with
3019 | Ulinkgoto (pageno, top) ->
3020 if pageno >= 0
3021 then (
3022 addnav ();
3023 gotopage1 pageno top;
3026 | Ulinkuri s ->
3027 print_endline s
3029 | Unone when bstate = Glut.DOWN ->
3030 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3031 state.mstate <- Mpan (x, y);
3033 | Unone | Utext _ ->
3034 if bstate = Glut.DOWN
3035 then (
3036 if conf.angle mod 360 = 0
3037 then (
3038 state.mstate <- Msel ((x, y), (x, y));
3039 Glut.postRedisplay ()
3042 else (
3043 match state.mstate with
3044 | Mnone -> ()
3046 | Mzoom _ | Mscroll ->
3047 state.mstate <- Mnone
3049 | Mpan _ ->
3050 Glut.setCursor Glut.CURSOR_INHERIT;
3051 state.mstate <- Mnone
3053 | Msel ((_, y0), (_, y1)) ->
3054 let f l =
3055 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3056 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3057 then
3058 match getopaque l.pageno with
3059 | Some (opaque, _) when validopaque opaque ->
3060 copysel opaque
3061 | _ -> ()
3063 List.iter f state.layout;
3064 copysel ""; (* ugly *)
3065 Glut.setCursor Glut.CURSOR_INHERIT;
3066 state.mstate <- Mnone;
3070 | _ -> ()
3073 let birdseyemouse button bstate x y
3074 (conf, leftx, _, hooverpageno, anchor) =
3075 match button with
3076 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3077 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3078 let rec loop = function
3079 | [] -> ()
3080 | l :: rest ->
3081 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3082 && x > margin && x < margin + l.pagew
3083 then (
3084 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3086 else loop rest
3088 loop state.layout
3089 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3090 | _ -> ()
3093 let mouse bstate button x y =
3094 match state.mode with
3095 | View -> viewmouse button bstate x y
3096 | Birdseye beye -> birdseyemouse button bstate x y beye
3097 | Textentry _ | Outline _ | Items _ -> ()
3100 let mouse ~button ~state ~x ~y = mouse state button x y;;
3102 let motion ~x ~y =
3103 match state.mode with
3104 | Outline _ -> ()
3105 | _ ->
3106 match state.mstate with
3107 | Mzoom _ | Mnone -> ()
3109 | Mpan (x0, y0) ->
3110 let dx = x - x0
3111 and dy = y0 - y in
3112 state.mstate <- Mpan (x, y);
3113 if conf.zoom > 1.0 then state.x <- state.x + dx;
3114 let y = clamp dy in
3115 gotoy_and_clear_text y
3117 | Msel (a, _) ->
3118 state.mstate <- Msel (a, (x, y));
3119 Glut.postRedisplay ()
3121 | Mscroll ->
3122 let y = min conf.winh (max 0 y) in
3123 let percent = float y /. float conf.winh in
3124 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3125 gotoy_and_clear_text y
3128 let pmotion ~x ~y =
3129 match state.mode with
3130 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3131 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3132 let rec loop = function
3133 | [] ->
3134 if hooverpageno != -1
3135 then (
3136 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3137 Glut.postRedisplay ();
3139 | l :: rest ->
3140 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3141 && x > margin && x < margin + l.pagew
3142 then (
3143 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3144 Glut.postRedisplay ();
3146 else loop rest
3148 loop state.layout
3150 | Outline _ | Items _ | Textentry _ -> ()
3151 | View ->
3152 match state.mstate with
3153 | Mnone ->
3154 begin match getunder x y with
3155 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3156 | Ulinkuri uri ->
3157 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3158 Glut.setCursor Glut.CURSOR_INFO
3159 | Ulinkgoto (page, _) ->
3160 if conf.underinfo
3161 then showtext 'p' ("age: " ^ string_of_int page);
3162 Glut.setCursor Glut.CURSOR_INFO
3163 | Utext s ->
3164 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3165 Glut.setCursor Glut.CURSOR_TEXT
3168 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3173 module State =
3174 struct
3175 open Parser
3177 let home =
3179 match Sys.os_type with
3180 | "Win32" -> Sys.getenv "HOMEPATH"
3181 | _ -> Sys.getenv "HOME"
3182 with exn ->
3183 prerr_endline
3184 ("Can not determine home directory location: " ^
3185 Printexc.to_string exn);
3189 let config_of c attrs =
3190 let apply c k v =
3192 match k with
3193 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3194 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3195 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3196 | "preload" -> { c with preload = bool_of_string v }
3197 | "page-bias" -> { c with pagebias = int_of_string v }
3198 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3199 | "auto-scroll-step" ->
3200 { c with autoscrollstep = max 0 (int_of_string v) }
3201 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3202 | "crop-hack" -> { c with crophack = bool_of_string v }
3203 | "throttle" -> { c with showall = bool_of_string v }
3204 | "highlight-links" -> { c with hlinks = bool_of_string v }
3205 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3206 | "vertical-margin" ->
3207 { c with interpagespace = max 0 (int_of_string v) }
3208 | "zoom" ->
3209 let zoom = float_of_string v /. 100. in
3210 let zoom = max 0.01 (min 2.2 zoom) in
3211 { c with zoom = zoom }
3212 | "presentation" -> { c with presentation = bool_of_string v }
3213 | "rotation-angle" -> { c with angle = int_of_string v }
3214 | "width" -> { c with winw = max 20 (int_of_string v) }
3215 | "height" -> { c with winh = max 20 (int_of_string v) }
3216 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3217 | "proportional-display" -> { c with proportional = bool_of_string v }
3218 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3219 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3220 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3221 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3222 | "persistent-location" -> { c with jumpback = bool_of_string v }
3223 | "background-color" -> { c with bgcolor = color_of_string v }
3224 | "scrollbar-in-presentation" ->
3225 { c with scrollbarinpm = bool_of_string v }
3226 | "ui-font" -> { c with uifont = v }
3227 | _ -> c
3228 with exn ->
3229 prerr_endline ("Error processing attribute (`" ^
3230 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3233 let rec fold c = function
3234 | [] -> c
3235 | (k, v) :: rest ->
3236 let c = apply c k v in
3237 fold c rest
3239 fold c attrs;
3242 let fromstring f pos n v d =
3243 try f v
3244 with exn ->
3245 dolog "Error processing attribute (%S=%S) at %d\n%s"
3246 n v pos (Printexc.to_string exn)
3251 let bookmark_of attrs =
3252 let rec fold title page rely = function
3253 | ("title", v) :: rest -> fold v page rely rest
3254 | ("page", v) :: rest -> fold title v rely rest
3255 | ("rely", v) :: rest -> fold title page v rest
3256 | _ :: rest -> fold title page rely rest
3257 | [] -> title, page, rely
3259 fold "invalid" "0" "0" attrs
3262 let doc_of attrs =
3263 let rec fold path page rely pan = function
3264 | ("path", v) :: rest -> fold v page rely pan rest
3265 | ("page", v) :: rest -> fold path v rely pan rest
3266 | ("rely", v) :: rest -> fold path page v pan rest
3267 | ("pan", v) :: rest -> fold path page rely v rest
3268 | _ :: rest -> fold path page rely pan rest
3269 | [] -> path, page, rely, pan
3271 fold "" "0" "0" "0" attrs
3274 let setconf dst src =
3275 dst.scrollbw <- src.scrollbw;
3276 dst.scrollh <- src.scrollh;
3277 dst.icase <- src.icase;
3278 dst.preload <- src.preload;
3279 dst.pagebias <- src.pagebias;
3280 dst.verbose <- src.verbose;
3281 dst.scrollstep <- src.scrollstep;
3282 dst.maxhfit <- src.maxhfit;
3283 dst.crophack <- src.crophack;
3284 dst.autoscrollstep <- src.autoscrollstep;
3285 dst.showall <- src.showall;
3286 dst.hlinks <- src.hlinks;
3287 dst.underinfo <- src.underinfo;
3288 dst.interpagespace <- src.interpagespace;
3289 dst.zoom <- src.zoom;
3290 dst.presentation <- src.presentation;
3291 dst.angle <- src.angle;
3292 dst.winw <- src.winw;
3293 dst.winh <- src.winh;
3294 dst.savebmarks <- src.savebmarks;
3295 dst.memlimit <- src.memlimit;
3296 dst.proportional <- src.proportional;
3297 dst.texcount <- src.texcount;
3298 dst.sliceheight <- src.sliceheight;
3299 dst.thumbw <- src.thumbw;
3300 dst.jumpback <- src.jumpback;
3301 dst.bgcolor <- src.bgcolor;
3302 dst.scrollbarinpm <- src.scrollbarinpm;
3303 dst.uifont <- src.uifont;
3306 let unent s =
3307 let l = String.length s in
3308 let b = Buffer.create l in
3309 unent b s 0 l;
3310 Buffer.contents b;
3313 let get s =
3314 let h = Hashtbl.create 10 in
3315 let dc = { defconf with angle = defconf.angle } in
3316 let rec toplevel v t spos _ =
3317 match t with
3318 | Vdata | Vcdata | Vend -> v
3319 | Vopen ("llppconfig", _, closed) ->
3320 if closed
3321 then v
3322 else { v with f = llppconfig }
3323 | Vopen _ ->
3324 error "unexpected subelement at top level" s spos
3325 | Vclose _ -> error "unexpected close at top level" s spos
3327 and llppconfig v t spos _ =
3328 match t with
3329 | Vdata | Vcdata | Vend -> v
3330 | Vopen ("defaults", attrs, closed) ->
3331 let c = config_of dc attrs in
3332 setconf dc c;
3333 if closed
3334 then v
3335 else { v with f = skip "defaults" (fun () -> v) }
3337 | Vopen ("doc", attrs, closed) ->
3338 let pathent, spage, srely, span = doc_of attrs in
3339 let path = unent pathent
3340 and pageno = fromstring int_of_string spos "page" spage 0
3341 and rely = fromstring float_of_string spos "rely" srely 0.0
3342 and pan = fromstring int_of_string spos "pan" span 0 in
3343 let c = config_of dc attrs in
3344 let anchor = (pageno, rely) in
3345 if closed
3346 then (Hashtbl.add h path (c, [], pan, anchor); v)
3347 else { v with f = doc path pan anchor c [] }
3349 | Vopen _ ->
3350 error "unexpected subelement in llppconfig" s spos
3352 | Vclose "llppconfig" -> { v with f = toplevel }
3353 | Vclose _ -> error "unexpected close in llppconfig" s spos
3355 and doc path pan anchor c bookmarks v t spos _ =
3356 match t with
3357 | Vdata | Vcdata -> v
3358 | Vend -> error "unexpected end of input in doc" s spos
3359 | Vopen ("bookmarks", _, closed) ->
3360 if closed
3361 then v
3362 else { v with f = pbookmarks path pan anchor c bookmarks }
3364 | Vopen (_, _, _) ->
3365 error "unexpected subelement in doc" s spos
3367 | Vclose "doc" ->
3368 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3369 { v with f = llppconfig }
3371 | Vclose _ -> error "unexpected close in doc" s spos
3373 and pbookmarks path pan anchor c bookmarks v t spos _ =
3374 match t with
3375 | Vdata | Vcdata -> v
3376 | Vend -> error "unexpected end of input in bookmarks" s spos
3377 | Vopen ("item", attrs, closed) ->
3378 let titleent, spage, srely = bookmark_of attrs in
3379 let page = fromstring int_of_string spos "page" spage 0
3380 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3381 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3382 if closed
3383 then { v with f = pbookmarks path pan anchor c bookmarks }
3384 else
3385 let f () = v in
3386 { v with f = skip "item" f }
3388 | Vopen _ ->
3389 error "unexpected subelement in bookmarks" s spos
3391 | Vclose "bookmarks" ->
3392 { v with f = doc path pan anchor c bookmarks }
3394 | Vclose _ -> error "unexpected close in bookmarks" s spos
3396 and skip tag f v t spos _ =
3397 match t with
3398 | Vdata | Vcdata -> v
3399 | Vend ->
3400 error ("unexpected end of input in skipped " ^ tag) s spos
3401 | Vopen (tag', _, closed) ->
3402 if closed
3403 then v
3404 else
3405 let f' () = { v with f = skip tag f } in
3406 { v with f = skip tag' f' }
3407 | Vclose ctag ->
3408 if tag = ctag
3409 then f ()
3410 else error ("unexpected close in skipped " ^ tag) s spos
3413 parse { f = toplevel; accu = () } s;
3414 h, dc;
3417 let do_load f ic =
3419 let len = in_channel_length ic in
3420 let s = String.create len in
3421 really_input ic s 0 len;
3422 f s;
3423 with
3424 | Parse_error (msg, s, pos) ->
3425 let subs = subs s pos in
3426 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3427 failwith ("parse error: " ^ s)
3429 | exn ->
3430 failwith ("config load error: " ^ Printexc.to_string exn)
3433 let path =
3434 let dir =
3436 let dir = Filename.concat home ".config" in
3437 if Sys.is_directory dir then dir else home
3438 with _ -> home
3440 Filename.concat dir "llpp.conf"
3443 let load1 f =
3444 if Sys.file_exists path
3445 then
3446 match
3447 (try Some (open_in_bin path)
3448 with exn ->
3449 prerr_endline
3450 ("Error opening configuation file `" ^ path ^ "': " ^
3451 Printexc.to_string exn);
3452 None
3454 with
3455 | Some ic ->
3456 begin try
3457 f (do_load get ic)
3458 with exn ->
3459 prerr_endline
3460 ("Error loading configuation from `" ^ path ^ "': " ^
3461 Printexc.to_string exn);
3462 end;
3463 close_in ic;
3465 | None -> ()
3466 else
3467 f (Hashtbl.create 0, defconf)
3470 let load () =
3471 let f (h, dc) =
3472 let pc, pb, px, pa =
3474 Hashtbl.find h (Filename.basename state.path)
3475 with Not_found -> dc, [], 0, (0, 0.0)
3477 setconf defconf dc;
3478 setconf conf pc;
3479 state.bookmarks <- pb;
3480 state.x <- px;
3481 state.scrollw <- conf.scrollbw;
3482 if conf.jumpback
3483 then state.anchor <- pa;
3484 cbput state.hists.nav pa;
3486 load1 f
3489 let add_attrs bb always dc c =
3490 let ob s a b =
3491 if always || a != b
3492 then Printf.bprintf bb "\n %s='%b'" s a
3493 and oi s a b =
3494 if always || a != b
3495 then Printf.bprintf bb "\n %s='%d'" s a
3496 and oz s a b =
3497 if always || a <> b
3498 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3499 and oc s a b =
3500 if always || a <> b
3501 then
3502 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3503 and os s a b =
3504 if always || a <> b
3505 then
3506 Printf.bprintf bb "\n %s='%s'" s a
3508 let w, h =
3509 if always
3510 then dc.winw, dc.winh
3511 else
3512 match state.fullscreen with
3513 | Some wh -> wh
3514 | None -> c.winw, c.winh
3516 let zoom, presentation, interpagespace, showall=
3517 if always
3518 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3519 else
3520 match state.mode with
3521 | Birdseye (bc, _, _, _, _) ->
3522 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3523 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3525 oi "width" w dc.winw;
3526 oi "height" h dc.winh;
3527 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3528 oi "scroll-handle-height" c.scrollh dc.scrollh;
3529 ob "case-insensitive-search" c.icase dc.icase;
3530 ob "preload" c.preload dc.preload;
3531 oi "page-bias" c.pagebias dc.pagebias;
3532 oi "scroll-step" c.scrollstep dc.scrollstep;
3533 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3534 ob "max-height-fit" c.maxhfit dc.maxhfit;
3535 ob "crop-hack" c.crophack dc.crophack;
3536 ob "throttle" showall dc.showall;
3537 ob "highlight-links" c.hlinks dc.hlinks;
3538 ob "under-cursor-info" c.underinfo dc.underinfo;
3539 oi "vertical-margin" interpagespace dc.interpagespace;
3540 oz "zoom" zoom dc.zoom;
3541 ob "presentation" presentation dc.presentation;
3542 oi "rotation-angle" c.angle dc.angle;
3543 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3544 ob "proportional-display" c.proportional dc.proportional;
3545 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3546 oi "texcount" c.texcount dc.texcount;
3547 oi "slice-height" c.sliceheight dc.sliceheight;
3548 oi "thumbnail-width" c.thumbw dc.thumbw;
3549 ob "persistent-location" c.jumpback dc.jumpback;
3550 oc "background-color" c.bgcolor dc.bgcolor;
3551 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3552 os "ui-font" c.uifont dc.uifont;
3555 let save () =
3556 let bb = Buffer.create 32768 in
3557 let f (h, dc) =
3558 let dc = if conf.bedefault then conf else dc in
3559 Buffer.add_string bb "<llppconfig>\n<defaults ";
3560 add_attrs bb true dc dc;
3561 Buffer.add_string bb "/>\n";
3563 let adddoc path pan anchor c bookmarks =
3564 if bookmarks == [] && c = dc && anchor = emptyanchor
3565 then ()
3566 else (
3567 Printf.bprintf bb "<doc path='%s'"
3568 (enent path 0 (String.length path));
3570 if anchor <> emptyanchor
3571 then (
3572 let n, y = anchor in
3573 Printf.bprintf bb " page='%d'" n;
3574 if y > 1e-6
3575 then
3576 Printf.bprintf bb " rely='%f'" y
3580 if pan != 0
3581 then Printf.bprintf bb " pan='%d'" pan;
3583 add_attrs bb false dc c;
3585 begin match bookmarks with
3586 | [] -> Buffer.add_string bb "/>\n"
3587 | _ ->
3588 Buffer.add_string bb ">\n<bookmarks>\n";
3589 List.iter (fun (title, _level, (page, rely)) ->
3590 Printf.bprintf bb
3591 "<item title='%s' page='%d'"
3592 (enent title 0 (String.length title))
3593 page
3595 if rely > 1e-6
3596 then
3597 Printf.bprintf bb " rely='%f'" rely
3599 Buffer.add_string bb "/>\n";
3600 ) bookmarks;
3601 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3602 end;
3606 let pan =
3607 match state.mode with
3608 | Birdseye (_, pan, _, _, _) -> pan
3609 | _ -> state.x
3611 let basename = Filename.basename state.path in
3612 adddoc basename pan (getanchor ())
3613 { conf with
3614 autoscrollstep =
3615 if state.ascrollstep > 0
3616 then state.ascrollstep
3617 else conf.autoscrollstep }
3618 (if conf.savebmarks then state.bookmarks else []);
3620 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3621 if basename <> path
3622 then adddoc path x y c bookmarks
3623 ) h;
3624 Buffer.add_string bb "</llppconfig>";
3626 load1 f;
3627 if Buffer.length bb > 0
3628 then
3630 let tmp = path ^ ".tmp" in
3631 let oc = open_out_bin tmp in
3632 Buffer.output_buffer oc bb;
3633 close_out oc;
3634 Sys.rename tmp path;
3635 with exn ->
3636 prerr_endline
3637 ("error while saving configuration: " ^ Printexc.to_string exn)
3639 end;;
3641 let () =
3642 Arg.parse
3643 (Arg.align
3644 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3645 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3646 " Print version and exit")]
3648 (fun s -> state.path <- s)
3649 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3651 if String.length state.path = 0
3652 then (prerr_endline "file name missing"; exit 1);
3654 State.load ();
3656 let _ = Glut.init Sys.argv in
3657 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3658 let () = Glut.initWindowSize conf.winw conf.winh in
3659 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3661 let csock, ssock =
3662 if Sys.os_type = "Unix"
3663 then
3664 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3665 else
3666 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3667 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3668 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3669 Unix.bind sock addr;
3670 Unix.listen sock 1;
3671 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3672 Unix.connect csock addr;
3673 let ssock, _ = Unix.accept sock in
3674 Unix.close sock;
3675 let opts sock =
3676 Unix.setsockopt sock Unix.TCP_NODELAY true;
3677 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3679 opts ssock;
3680 opts csock;
3681 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3682 ssock, csock
3685 let () = Glut.displayFunc display in
3686 let () = Glut.reshapeFunc reshape in
3687 let () = Glut.keyboardFunc keyboard in
3688 let () = Glut.specialFunc special in
3689 let () = Glut.idleFunc (Some idle) in
3690 let () = Glut.mouseFunc mouse in
3691 let () = Glut.motionFunc motion in
3692 let () = Glut.passiveMotionFunc pmotion in
3694 init ssock (
3695 conf.angle, conf.proportional, conf.texcount,
3696 conf.sliceheight, conf.uifont
3698 state.csock <- csock;
3699 state.ssock <- ssock;
3700 state.text <- "Opening " ^ state.path;
3701 writeopen state.path state.password;
3703 at_exit State.save;
3705 let rec handlelablglutbug () =
3707 Glut.mainLoop ();
3708 with Glut.BadEnum "key in special_of_int" ->
3709 showtext '!' " LablGlut bug: special key not recognized";
3710 handlelablglutbug ()
3712 handlelablglutbug ();