Always use our own err/errx implementations and avoid using exit
[llpp.git] / main.ml
blob91edc83ce68287cac05995a63cfe0fb0ff338ec7
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
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
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mzoom of (int * int)
42 | Mnone
45 type textentry = string * string * onhist option * onkey * ondone
46 and onkey = string -> int -> te
47 and ondone = string -> unit
48 and histcancel = unit -> unit
49 and onhist = ((histcmd -> string) * histcancel)
50 and histcmd = HCnext | HCprev | HCfirst | HClast
51 and te =
52 | TEstop
53 | TEdone of string
54 | TEcont of string
55 | TEswitch of textentry
58 type 'a circbuf =
59 { store : 'a array
60 ; mutable rc : int
61 ; mutable wc : int
62 ; mutable len : int
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollbw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollstep : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscrollstep : int
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable thumbw : width
157 ; mutable jumpback : bool
158 ; mutable bgcolor : float * float * float
159 ; mutable bedefault : bool
160 ; mutable scrollbarinpm : bool
164 type anchor = pageno * top;;
166 type outline = string * int * anchor
167 and outlines =
168 | Oarray of outline array
169 | Olist of outline list
170 | Onarrow of string * outline array * outline array
173 type rect = float * float * float * float * float * float * float * float;;
175 type pagemapkey = pageno * width * angle * proportional * gen;;
177 let emptyanchor = (0, 0.0);;
178 let initialanchor = (-1, nan);;
180 type mode =
181 | Birdseye of (conf * leftx * pageno * pageno * anchor)
182 | Outline of (bool * int * int * outline array * string * int * mode)
183 | Items of (int * int * item array * string * int * mode)
184 | Textentry of (textentry * onleave)
185 | View
186 and onleave = leavetextentrystatus -> unit
187 and leavetextentrystatus = | Cancel | Confirm
188 and item = string * int * action
189 and action =
190 | Noaction
191 | Action of (int -> int -> string -> int -> mode)
194 let isbirdseye = function Birdseye _ -> true | _ -> false;;
195 let istextentry = function Textentry _ -> true | _ -> false;;
197 type state =
198 { mutable csock : Unix.file_descr
199 ; mutable ssock : Unix.file_descr
200 ; mutable w : int
201 ; mutable x : int
202 ; mutable y : int
203 ; mutable scrollw : int
204 ; mutable anchor : anchor
205 ; mutable maxy : int
206 ; mutable layout : layout list
207 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
208 ; mutable pdims : (pageno * width * height * leftx) list
209 ; mutable pagecount : int
210 ; pagecache : string circbuf
211 ; mutable rendering : bool
212 ; mutable mstate : mstate
213 ; mutable searchpattern : string
214 ; mutable rects : (pageno * recttype * rect) list
215 ; mutable rects1 : (pageno * recttype * rect) list
216 ; mutable text : string
217 ; mutable fullscreen : (width * height) option
218 ; mutable mode : mode
219 ; mutable outlines : outlines
220 ; mutable bookmarks : outline list
221 ; mutable path : string
222 ; mutable password : string
223 ; mutable invalidated : int
224 ; mutable colorscale : float
225 ; mutable memused : int
226 ; mutable gen : gen
227 ; mutable throttle : layout list option
228 ; mutable ascrollstep : int
229 ; mutable help : item array
230 ; mutable docinfo : (int * string) list
231 ; hists : hists
233 and hists =
234 { pat : string circbuf
235 ; pag : string circbuf
236 ; nav : anchor circbuf
240 let defconf =
241 { scrollbw = 7
242 ; scrollh = 12
243 ; icase = true
244 ; preload = true
245 ; pagebias = 0
246 ; verbose = false
247 ; scrollstep = 24
248 ; maxhfit = true
249 ; crophack = false
250 ; autoscrollstep = 24
251 ; showall = false
252 ; hlinks = false
253 ; underinfo = false
254 ; interpagespace = 2
255 ; zoom = 1.0
256 ; presentation = false
257 ; angle = 0
258 ; winw = 900
259 ; winh = 900
260 ; savebmarks = true
261 ; proportional = true
262 ; memlimit = 32*1024*1024
263 ; texcount = 256
264 ; sliceheight = 24
265 ; thumbw = 76
266 ; jumpback = false
267 ; bgcolor = (0.5, 0.5, 0.5)
268 ; bedefault = false
269 ; scrollbarinpm = true
273 let conf = { defconf with angle = defconf.angle };;
275 let makehelp () =
276 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
277 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
280 let state =
281 { csock = Unix.stdin
282 ; ssock = Unix.stdin
283 ; x = 0
284 ; y = 0
285 ; w = 0
286 ; scrollw = 0
287 ; anchor = initialanchor
288 ; layout = []
289 ; maxy = max_int
290 ; pagemap = Hashtbl.create 10
291 ; pagecache = cbnew 100 ""
292 ; pdims = []
293 ; pagecount = 0
294 ; rendering = false
295 ; mstate = Mnone
296 ; rects = []
297 ; rects1 = []
298 ; text = ""
299 ; mode = View
300 ; fullscreen = None
301 ; searchpattern = ""
302 ; outlines = Olist []
303 ; bookmarks = []
304 ; path = ""
305 ; password = ""
306 ; invalidated = 0
307 ; hists =
308 { nav = cbnew 100 (0, 0.0)
309 ; pat = cbnew 20 ""
310 ; pag = cbnew 10 ""
312 ; colorscale = 1.0
313 ; memused = 0
314 ; gen = 0
315 ; throttle = None
316 ; ascrollstep = 0
317 ; help = makehelp ()
318 ; docinfo = []
322 let vlog fmt =
323 if conf.verbose
324 then
325 Printf.kprintf prerr_endline fmt
326 else
327 Printf.kprintf ignore fmt
330 let writecmd fd s =
331 let len = String.length s in
332 let n = 4 + len in
333 let b = Buffer.create n in
334 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
335 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
336 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
337 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
338 Buffer.add_string b s;
339 let s' = Buffer.contents b in
340 let n' = Unix.write fd s' 0 n in
341 if n' != n then failwith "write failed";
344 let readcmd fd =
345 let s = "xxxx" in
346 let n = Unix.read fd s 0 4 in
347 if n != 4 then failwith "incomplete read(len)";
348 let len = 0
349 lor (Char.code s.[0] lsl 24)
350 lor (Char.code s.[1] lsl 16)
351 lor (Char.code s.[2] lsl 8)
352 lor (Char.code s.[3] lsl 0)
354 let s = String.create len in
355 let n = Unix.read fd s 0 len in
356 if n != len then failwith "incomplete read(data)";
360 let makecmd s l =
361 let b = Buffer.create 10 in
362 Buffer.add_string b s;
363 let rec combine = function
364 | [] -> b
365 | x :: xs ->
366 Buffer.add_char b ' ';
367 let s =
368 match x with
369 | `b b -> if b then "1" else "0"
370 | `s s -> s
371 | `i i -> string_of_int i
372 | `f f -> string_of_float f
373 | `I f -> string_of_int (truncate f)
375 Buffer.add_string b s;
376 combine xs;
378 combine l;
381 let wcmd s l =
382 let cmd = Buffer.contents (makecmd s l) in
383 writecmd state.csock cmd;
386 let calcips h =
387 if conf.presentation
388 then
389 let d = conf.winh - h in
390 max 0 ((d + 1) / 2)
391 else
392 conf.interpagespace
395 let calcheight () =
396 let rec f pn ph pi fh l =
397 match l with
398 | (n, _, h, _) :: rest ->
399 let ips = calcips h in
400 let fh =
401 if conf.presentation
402 then fh+ips
403 else (
404 if isbirdseye state.mode && pn = 0
405 then fh + ips
406 else fh
409 let fh = fh + ((n - pn) * (ph + pi)) in
410 f n h ips fh rest;
412 | [] ->
413 let inc =
414 if conf.presentation || (isbirdseye state.mode && pn = 0)
415 then 0
416 else -pi
418 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
419 max 0 fh
421 let fh = f 0 0 0 0 state.pdims in
425 let getpageyh pageno =
426 let rec f pn ph pi y l =
427 match l with
428 | (n, _, h, _) :: rest ->
429 let ips = calcips h in
430 if n >= pageno
431 then
432 let h = if n = pageno then h else ph in
433 if conf.presentation && n = pageno
434 then
435 y + (pageno - pn) * (ph + pi) + pi, h
436 else
437 y + (pageno - pn) * (ph + pi), h
438 else
439 let y = y + (if conf.presentation then pi else 0) in
440 let y = y + (n - pn) * (ph + pi) in
441 f n h ips y rest
443 | [] ->
444 y + (pageno - pn) * (ph + pi), ph
446 f 0 0 0 0 state.pdims
449 let getpagey pageno = fst (getpageyh pageno);;
451 let layout y sh =
452 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
453 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
454 match pdims with
455 | (pageno', w, h, x) :: rest when pageno' = pageno ->
456 let ips = calcips h in
457 let yinc =
458 if conf.presentation || (isbirdseye state.mode && pageno = 0)
459 then ips
460 else 0
462 (w, h, ips, x), rest, pdimno + 1, yinc
463 | _ ->
464 prev, pdims, pdimno, 0
466 let dy = dy + yinc in
467 let py = py + yinc in
468 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
469 then
470 accu
471 else
472 let vy = y + dy in
473 if py + h <= vy - yinc
474 then
475 let py = py + h + ips in
476 let dy = max 0 (py - y) in
477 f ~pageno:(pageno+1)
478 ~pdimno
479 ~prev:curr
482 ~pdims:rest
483 ~cacheleft
484 ~accu
485 else
486 let pagey = vy - py in
487 let pagevh = h - pagey in
488 let pagevh = min (sh - dy) pagevh in
489 let off = if yinc > 0 then py - vy else 0 in
490 let py = py + h + ips in
491 let e =
492 { pageno = pageno
493 ; pagedimno = pdimno
494 ; pagew = w
495 ; pageh = h
496 ; pagedispy = dy + off
497 ; pagey = pagey + off
498 ; pagevh = pagevh - off
499 ; pagex = x
502 let accu = e :: accu in
503 f ~pageno:(pageno+1)
504 ~pdimno
505 ~prev:curr
507 ~dy:(dy+pagevh+ips)
508 ~pdims:rest
509 ~cacheleft:(cacheleft-1)
510 ~accu
512 if state.invalidated = 0
513 then (
514 let accu =
516 ~pageno:0
517 ~pdimno:~-1
518 ~prev:(0,0,0,0)
519 ~py:0
520 ~dy:0
521 ~pdims:state.pdims
522 ~cacheleft:(cbcap state.pagecache)
523 ~accu:[]
525 List.rev accu
527 else
531 let clamp incr =
532 let y = state.y + incr in
533 let y = max 0 y in
534 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
538 let getopaque pageno =
539 try Some (Hashtbl.find state.pagemap
540 (pageno, state.w, conf.angle, conf.proportional, state.gen))
541 with Not_found -> None
544 let cache pageno opaque =
545 Hashtbl.replace state.pagemap
546 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
549 let validopaque opaque = String.length opaque > 0;;
551 let render l =
552 match getopaque l.pageno with
553 | None when not state.rendering ->
554 state.rendering <- true;
555 cache l.pageno ("", -1);
556 wcmd "render" [`i (l.pageno + 1)
557 ;`i l.pagedimno
558 ;`i l.pagew
559 ;`i l.pageh];
560 | _ -> ()
563 let loadlayout layout =
564 let rec f all = function
565 | l :: ls ->
566 begin match getopaque l.pageno with
567 | None -> render l; f false ls
568 | Some (opaque, _) -> f (all && validopaque opaque) ls
570 | [] -> all
572 f (layout <> []) layout;
575 let findpageforopaque opaque =
576 Hashtbl.fold
577 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
578 state.pagemap None
581 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
583 let preload () =
584 let oktopreload =
585 if conf.preload
586 then
587 let memleft = conf.memlimit - state.memused in
588 if memleft < 0
589 then
590 let opaque = cbpeek state.pagecache in
591 match findpageforopaque opaque with
592 | Some ((n, _, _, _, _), size) ->
593 memleft + size >= 0 && not (pagevisible state.layout n)
594 | None -> false
595 else true
596 else false
598 if oktopreload
599 then
600 let presentation = conf.presentation in
601 let interpagespace = conf.interpagespace in
602 let maxy = state.maxy in
603 conf.presentation <- false;
604 conf.interpagespace <- 0;
605 state.maxy <- calcheight ();
606 let y =
607 match state.layout with
608 | [] -> 0
609 | l :: _ -> getpagey l.pageno + l.pagey
611 let y = if y < conf.winh then 0 else y - conf.winh in
612 let pages = layout y (conf.winh*3) in
613 List.iter render pages;
614 conf.presentation <- presentation;
615 conf.interpagespace <- interpagespace;
616 state.maxy <- maxy;
619 let gotoy y =
620 let y = max 0 y in
621 let y = min state.maxy y in
622 let pages = layout y conf.winh in
623 let ready = loadlayout pages in
624 if conf.showall
625 then (
626 if ready
627 then (
628 state.y <- y;
629 state.layout <- pages;
630 state.throttle <- None;
631 Glut.postRedisplay ();
633 else (
634 state.throttle <- Some pages;
637 else (
638 state.y <- y;
639 state.layout <- pages;
640 state.throttle <- None;
641 Glut.postRedisplay ();
643 begin match state.mode with
644 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
645 if not (pagevisible pages pageno)
646 then (
647 match state.layout with
648 | [] -> ()
649 | l :: _ ->
650 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
652 | _ -> ()
653 end;
654 preload ();
657 let gotoy_and_clear_text y =
658 gotoy y;
659 if not conf.verbose then state.text <- "";
662 let getanchor () =
663 match state.layout with
664 | [] -> emptyanchor
665 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
668 let getanchory (n, top) =
669 let y, h = getpageyh n in
670 y + (truncate (top *. float h));
673 let gotoanchor anchor =
674 gotoy (getanchory anchor);
677 let addnav () =
678 cbput state.hists.nav (getanchor ());
681 let getnav dir =
682 let anchor = cbgetc state.hists.nav dir in
683 getanchory anchor;
686 let gotopage n top =
687 let y, h = getpageyh n in
688 gotoy_and_clear_text (y + (truncate (top *. float h)));
691 let gotopage1 n top =
692 let y = getpagey n in
693 gotoy_and_clear_text (y + top);
696 let invalidate () =
697 state.layout <- [];
698 state.pdims <- [];
699 state.rects <- [];
700 state.rects1 <- [];
701 state.invalidated <- state.invalidated + 1;
704 let scalecolor c =
705 let c = c *. state.colorscale in
706 (c, c, c);
709 let scalecolor2 (r, g, b) =
710 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
713 let represent () =
714 state.maxy <- calcheight ();
715 match state.mode with
716 | Birdseye (_, _, pageno, _, _) ->
717 let y, h = getpageyh pageno in
718 let top = (conf.winh - h) / 2 in
719 gotoy (max 0 (y - top))
720 | _ -> gotoanchor state.anchor
723 let pagematrix () =
724 GlMat.mode `projection;
725 GlMat.load_identity ();
726 GlMat.rotate ~x:1.0 ~angle:180.0 ();
727 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
728 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
729 if state.x != 0
730 then (
731 GlMat.translate ~x:(float state.x) ();
735 let winmatrix () =
736 GlMat.mode `projection;
737 GlMat.load_identity ();
738 GlMat.rotate ~x:1.0 ~angle:180.0 ();
739 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
740 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
743 let reshape ~w ~h =
744 if state.invalidated = 0 && state.anchor == initialanchor
745 then state.anchor <- getanchor ();
747 conf.winw <- w;
748 let w = truncate (float w *. conf.zoom) - state.scrollw in
749 let w = max w 2 in
750 state.w <- w;
751 conf.winh <- h;
752 GlMat.mode `modelview;
753 GlMat.load_identity ();
754 GlClear.color (scalecolor 1.0);
755 GlClear.clear [`color];
757 invalidate ();
758 wcmd "geometry" [`i w; `i h];
761 let showtext s =
762 GlDraw.color (0.0, 0.0, 0.0);
763 GlDraw.rect
764 (0.0, float (conf.winh - 18))
765 (float (conf.winw - state.scrollw - 1), float conf.winh)
767 let font = Glut.BITMAP_8_BY_13 in
768 GlDraw.color (1.0, 1.0, 1.0);
769 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
770 Glut.bitmapCharacter ~font ~c:(Char.code ' ');
771 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
774 let enttext () =
775 let len = String.length state.text in
776 match state.mode with
777 | Textentry ((prefix, text, _, _, _), _) ->
778 let s =
779 match String.length prefix with
780 | 0 | 1 ->
781 if len > 0
782 then
783 Printf.sprintf "%s%s^ [%s]" prefix text state.text
784 else
785 Printf.sprintf "%s%s^" prefix text
787 | _ ->
788 if len > 0
789 then
790 Printf.sprintf "%s: %s^ [%s]" prefix text state.text
791 else
792 Printf.sprintf "%s: %s^" prefix text
794 showtext s;
796 | _ ->
797 if len > 0 then showtext state.text
800 let showtext c s =
801 state.text <- Printf.sprintf "%c%s" c s;
802 Glut.postRedisplay ();
805 let act cmd =
806 match cmd.[0] with
807 | 'c' ->
808 state.pdims <- [];
810 | 'D' ->
811 state.rects <- state.rects1;
812 Glut.postRedisplay ()
814 | 'C' ->
815 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
816 state.pagecount <- n;
817 state.invalidated <- state.invalidated - 1;
818 if state.invalidated = 0
819 then represent ()
821 | 't' ->
822 let s = Scanf.sscanf cmd "t %n"
823 (fun n -> String.sub cmd n (String.length cmd - n))
825 Glut.setWindowTitle s
827 | 'T' ->
828 let s = Scanf.sscanf cmd "T %n"
829 (fun n -> String.sub cmd n (String.length cmd - n))
831 if istextentry state.mode
832 then (
833 state.text <- s;
834 showtext ' ' s;
836 else (
837 state.text <- s;
838 Glut.postRedisplay ();
841 | 'V' ->
842 if conf.verbose
843 then
844 let s = Scanf.sscanf cmd "V %n"
845 (fun n -> String.sub cmd n (String.length cmd - n))
847 state.text <- s;
848 showtext ' ' s;
850 | 'F' ->
851 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
852 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
853 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
854 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
856 let y = (getpagey pageno) + truncate y0 in
857 addnav ();
858 gotoy y;
859 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
861 | 'R' ->
862 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
863 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
864 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
865 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
867 state.rects1 <-
868 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
870 | 'r' ->
871 let n, w, _h, r, l, s, p =
872 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
873 (fun n w h r l s p ->
874 (n-1, w, h, r, l != 0, s, p))
877 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
878 state.memused <- state.memused + s;
880 let layout =
881 match state.throttle with
882 | None -> state.layout
883 | Some layout -> layout
886 let rec gc () =
887 if (state.memused <= conf.memlimit) || cbempty state.pagecache
888 then ()
889 else (
890 let evictedopaque = cbpeek state.pagecache in
891 match findpageforopaque evictedopaque with
892 | None -> failwith "bug in gc"
893 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
894 if state.gen != gen || not (pagevisible layout evictedn)
895 then (
896 wcmd "free" [`s evictedopaque];
897 state.memused <- state.memused - evictedsize;
898 Hashtbl.remove state.pagemap k;
899 cbdecr state.pagecache;
900 gc ();
904 gc ();
906 cbput state.pagecache p;
907 state.rendering <- false;
909 begin match state.throttle with
910 | None ->
911 if pagevisible state.layout n
912 then gotoy state.y
913 else (
914 let allvisible = loadlayout state.layout in
915 if allvisible then preload ();
918 | Some layout ->
919 match layout with
920 | [] -> ()
921 | l :: _ ->
922 let y = getpagey l.pageno + l.pagey in
923 gotoy y
926 | 'l' ->
927 let pdim =
928 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
930 state.pdims <- pdim :: state.pdims
932 | 'o' ->
933 let (l, n, t, h, pos) =
934 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
936 let s = String.sub cmd pos (String.length cmd - pos) in
937 let s =
938 let l = String.length s in
939 let b = Buffer.create (String.length s) in
940 let rec loop pc2 i =
941 if i = l
942 then ()
943 else
944 let pc2 =
945 match s.[i] with
946 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
947 | '\xc2' -> true
948 | c ->
949 let c = if Char.code c land 0x80 = 0 then c else '?' in
950 Buffer.add_char b c;
951 false
953 loop pc2 (i+1)
955 loop false 0;
956 Buffer.contents b
958 let outline = (s, l, (n, float t /. float h)) in
959 let outlines =
960 match state.outlines with
961 | Olist outlines -> Olist (outline :: outlines)
962 | Oarray _ -> Olist [outline]
963 | Onarrow _ -> Olist [outline]
965 state.outlines <- outlines
968 | 'i' ->
969 let s = Scanf.sscanf cmd "i %n"
970 (fun n -> String.sub cmd n (String.length cmd - n))
972 let len = String.length s in
973 let rec fold accu pos =
974 let eolpos =
975 try String.index_from s pos '\n' with Not_found -> len
977 if eolpos = len
978 then List.rev accu
979 else
980 let line = String.sub s pos (eolpos - pos) in
981 fold ((1, line)::accu) (eolpos+1)
983 state.docinfo <- fold state.docinfo 0
985 | _ ->
986 dolog "unknown cmd `%S'" cmd
989 let now = Unix.gettimeofday;;
991 let idle () =
992 let rec loop delay =
993 let r, _, _ = Unix.select [state.csock] [] [] delay in
994 begin match r with
995 | [] ->
996 if state.ascrollstep > 0
997 then begin
998 let y = state.y + state.ascrollstep in
999 let y = if y >= state.maxy then 0 else y in
1000 gotoy y;
1001 state.text <- "";
1002 end;
1004 | _ ->
1005 let cmd = readcmd state.csock in
1006 act cmd;
1007 loop 0.0
1008 end;
1009 in loop 0.001
1012 let onhist cb =
1013 let rc = cb.rc in
1014 let action = function
1015 | HCprev -> cbget cb ~-1
1016 | HCnext -> cbget cb 1
1017 | HCfirst -> cbget cb ~-(cb.rc)
1018 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1019 and cancel () = cb.rc <- rc
1020 in (action, cancel)
1023 let search pattern forward =
1024 if String.length pattern > 0
1025 then
1026 let pn, py =
1027 match state.layout with
1028 | [] -> 0, 0
1029 | l :: _ ->
1030 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1032 let cmd =
1033 let b = makecmd "search"
1034 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1036 Buffer.add_char b ',';
1037 Buffer.add_string b pattern;
1038 Buffer.add_char b '\000';
1039 Buffer.contents b;
1041 writecmd state.csock cmd;
1044 let intentry text key =
1045 let c = Char.unsafe_chr key in
1046 match c with
1047 | '0' .. '9' ->
1048 let s = "x" in s.[0] <- c;
1049 let text = text ^ s in
1050 TEcont text
1052 | _ ->
1053 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1054 TEcont text
1057 let addchar s c =
1058 let b = Buffer.create (String.length s + 1) in
1059 Buffer.add_string b s;
1060 Buffer.add_char b c;
1061 Buffer.contents b;
1064 let textentry text key =
1065 let c = Char.unsafe_chr key in
1066 match c with
1067 | _ when key >= 32 && key < 127 ->
1068 let text = addchar text c in
1069 TEcont text
1071 | _ ->
1072 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1073 TEcont text
1076 let reinit angle proportional =
1077 conf.angle <- angle;
1078 conf.proportional <- proportional;
1079 invalidate ();
1080 wcmd "reinit" [`i angle; `b proportional];
1083 let setzoom zoom =
1084 let zoom = max 0.01 (min 2.2 zoom) in
1085 if zoom <> conf.zoom
1086 then (
1087 if zoom <= 1.0
1088 then state.x <- 0;
1089 conf.zoom <- zoom;
1090 state.anchor <- getanchor ();
1091 reshape conf.winw conf.winh;
1092 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1096 let enterbirdseye () =
1097 let zoom = float conf.thumbw /. float conf.winw in
1098 let birdseyepageno =
1099 let cy = conf.winh / 2 in
1100 let fold = function
1101 | [] -> 0
1102 | l :: rest ->
1103 let rec fold best = function
1104 | [] -> best.pageno
1105 | l :: rest ->
1106 let d = cy - (l.pagedispy + l.pagevh/2)
1107 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1108 if abs d < abs dbest
1109 then fold l rest
1110 else best.pageno
1111 in fold l rest
1113 fold state.layout
1115 state.mode <- Birdseye (
1116 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1118 conf.zoom <- zoom;
1119 conf.presentation <- false;
1120 conf.interpagespace <- 10;
1121 conf.hlinks <- false;
1122 state.x <- 0;
1123 state.mstate <- Mnone;
1124 conf.showall <- false;
1125 Glut.setCursor Glut.CURSOR_INHERIT;
1126 if conf.verbose
1127 then
1128 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1129 (100.0*.zoom)
1130 else
1131 state.text <- ""
1133 reshape conf.winw conf.winh;
1136 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1137 state.mode <- View;
1138 conf.zoom <- c.zoom;
1139 conf.presentation <- c.presentation;
1140 conf.interpagespace <- c.interpagespace;
1141 conf.showall <- c.showall;
1142 conf.hlinks <- c.hlinks;
1143 state.x <- leftx;
1144 if conf.verbose
1145 then
1146 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1147 (100.0*.conf.zoom)
1149 reshape conf.winw conf.winh;
1150 state.anchor <- if goback then anchor else (pageno, 0.0);
1153 let togglebirdseye () =
1154 match state.mode with
1155 | Birdseye vals -> leavebirdseye vals true
1156 | View | Outline _ -> enterbirdseye ()
1157 | _ -> ()
1160 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1161 let pageno = max 0 (pageno - 1) in
1162 let rec loop = function
1163 | [] -> gotopage1 pageno 0
1164 | l :: _ when l.pageno = pageno ->
1165 if l.pagedispy >= 0 && l.pagey = 0
1166 then Glut.postRedisplay ()
1167 else gotopage1 pageno 0
1168 | _ :: rest -> loop rest
1170 loop state.layout;
1171 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1174 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1175 let pageno = min (state.pagecount - 1) (pageno + 1) in
1176 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1177 let rec loop = function
1178 | [] ->
1179 let y, h = getpageyh pageno in
1180 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1181 gotoy (clamp dy)
1182 | l :: _ when l.pageno = pageno ->
1183 if l.pagevh != l.pageh
1184 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1185 else Glut.postRedisplay ()
1186 | _ :: rest -> loop rest
1188 loop state.layout
1191 let optentry mode _ key =
1192 let btos b = if b then "on" else "off" in
1193 let c = Char.unsafe_chr key in
1194 match c with
1195 | 's' ->
1196 let ondone s =
1197 try conf.scrollstep <- int_of_string s with exc ->
1198 state.text <- Printf.sprintf "bad integer `%s': %s"
1199 s (Printexc.to_string exc)
1201 TEswitch ("scroll step", "", None, intentry, ondone)
1203 | 'A' ->
1204 let ondone s =
1206 conf.autoscrollstep <- int_of_string s;
1207 if state.ascrollstep > 0
1208 then state.ascrollstep <- conf.autoscrollstep;
1209 with exc ->
1210 state.text <- Printf.sprintf "bad integer `%s': %s"
1211 s (Printexc.to_string exc)
1213 TEswitch ("auto scroll step", "", None, intentry, ondone)
1215 | 'Z' ->
1216 let ondone s =
1218 let zoom = float (int_of_string s) /. 100.0 in
1219 setzoom zoom
1220 with exc ->
1221 state.text <- Printf.sprintf "bad integer `%s': %s"
1222 s (Printexc.to_string exc)
1224 TEswitch ("zoom", "", None, intentry, ondone)
1226 | 't' ->
1227 let ondone s =
1229 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1230 state.text <-
1231 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1232 begin match mode with
1233 | Birdseye beye ->
1234 leavebirdseye beye false;
1235 enterbirdseye ();
1236 | _ -> ();
1238 with exc ->
1239 state.text <- Printf.sprintf "bad integer `%s': %s"
1240 s (Printexc.to_string exc)
1242 TEswitch ("thumbnail width", "", None, intentry, ondone)
1244 | 'R' ->
1245 let ondone s =
1246 match try
1247 Some (int_of_string s)
1248 with exc ->
1249 state.text <- Printf.sprintf "bad integer `%s': %s"
1250 s (Printexc.to_string exc);
1251 None
1252 with
1253 | Some angle -> reinit angle conf.proportional
1254 | None -> ()
1256 TEswitch ("rotation", "", None, intentry, ondone)
1258 | 'i' ->
1259 conf.icase <- not conf.icase;
1260 TEdone ("case insensitive search " ^ (btos conf.icase))
1262 | 'p' ->
1263 conf.preload <- not conf.preload;
1264 gotoy state.y;
1265 TEdone ("preload " ^ (btos conf.preload))
1267 | 'v' ->
1268 conf.verbose <- not conf.verbose;
1269 TEdone ("verbose " ^ (btos conf.verbose))
1271 | 'h' ->
1272 conf.maxhfit <- not conf.maxhfit;
1273 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1274 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1276 | 'c' ->
1277 conf.crophack <- not conf.crophack;
1278 TEdone ("crophack " ^ btos conf.crophack)
1280 | 'a' ->
1281 conf.showall <- not conf.showall;
1282 TEdone ("throttle " ^ btos conf.showall)
1284 | 'f' ->
1285 conf.underinfo <- not conf.underinfo;
1286 TEdone ("underinfo " ^ btos conf.underinfo)
1288 | 'P' ->
1289 conf.savebmarks <- not conf.savebmarks;
1290 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1292 | 'S' ->
1293 let ondone s =
1295 let pageno, py =
1296 match state.layout with
1297 | [] -> 0, 0
1298 | l :: _ ->
1299 l.pageno, l.pagey
1301 conf.interpagespace <- int_of_string s;
1302 state.maxy <- calcheight ();
1303 let y = getpagey pageno in
1304 gotoy (y + py)
1305 with exc ->
1306 state.text <- Printf.sprintf "bad integer `%s': %s"
1307 s (Printexc.to_string exc)
1309 TEswitch ("vertical margin", "", None, intentry, ondone)
1311 | 'l' ->
1312 reinit conf.angle (not conf.proportional);
1313 TEdone ("proprortional display " ^ btos conf.proportional)
1315 | _ ->
1316 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1317 TEstop
1320 let maxoutlinerows () = (conf.winh - 31) / 16;;
1322 let enterselector allowdel outlines errmsg msg =
1323 if Array.length outlines = 0
1324 then (
1325 showtext ' ' errmsg;
1327 else (
1328 state.text <- msg;
1329 Glut.setCursor Glut.CURSOR_INHERIT;
1330 let pageno =
1331 match state.layout with
1332 | [] -> -1
1333 | {pageno=pageno} :: _ -> pageno
1335 let active =
1336 let rec loop n =
1337 if n = Array.length outlines
1338 then 0
1339 else
1340 let (_, _, (outlinepageno, _)) = outlines.(n) in
1341 if outlinepageno >= pageno then n else loop (n+1)
1343 loop 0
1345 state.mode <- Outline
1346 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1347 state.mode);
1348 Glut.postRedisplay ();
1352 let enteroutlinemode () =
1353 let outlines, msg =
1354 match state.outlines with
1355 | Oarray a -> a, ""
1356 | Olist l ->
1357 let a = Array.of_list (List.rev l) in
1358 state.outlines <- Oarray a;
1359 a, ""
1360 | Onarrow (pat, a, _) ->
1361 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1363 enterselector false outlines "Document has no outline" msg;
1366 let enterbookmarkmode () =
1367 let bookmarks = Array.of_list state.bookmarks in
1368 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1371 let mode_to_string mode =
1372 let b = Buffer.create 10 in
1373 let rec f = function
1374 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1375 | View -> Buffer.add_string b "View"
1376 | Birdseye _ -> Buffer.add_string b "Birdseye"
1377 | Items _ -> Buffer.add_string b "Items"
1378 | Outline _ -> Buffer.add_string b "Outline"
1380 f mode;
1381 Buffer.contents b;
1384 let color_of_string s =
1385 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1386 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1390 let color_to_string (r, g, b) =
1391 let r = truncate (r *. 256.0)
1392 and g = truncate (g *. 256.0)
1393 and b = truncate (b *. 256.0) in
1394 Printf.sprintf "%d/%d/%d" r g b
1397 let enterinfomode () =
1398 let btos = function true -> "on" | _ -> "off" in
1399 let mode = state.mode in
1400 let rec makeitems () =
1401 let intp name get set =
1402 Printf.sprintf "%-24s %d" name (get ()), 1, Action (
1403 fun active first _ pan ->
1404 let ondone s =
1405 let n =
1406 try int_of_string s
1407 with exn ->
1408 state.text <- Printf.sprintf "bad integer `%s': %s"
1409 s (Printexc.to_string exn);
1410 max_int;
1412 if n != max_int then set n;
1414 let te = name, "", None, intentry, ondone in
1415 state.text <- "";
1416 Textentry (
1418 fun _ ->
1419 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1422 and boolp name get set =
1423 Printf.sprintf "%-24s %s" name (btos (get ())), 1, Action (
1424 fun active first qsearch pan ->
1425 let v = get () in
1426 set (not v);
1427 Items (active, first, makeitems (), qsearch, pan, mode);
1429 and colorp name get set =
1430 Printf.sprintf "%-24s %s" name (color_to_string (get ())), 1, Action (
1431 fun active first _ pan ->
1432 let invalid = (nan, nan, nan) in
1433 let ondone s =
1434 let c =
1435 try color_of_string s
1436 with exn ->
1437 state.text <- Printf.sprintf "bad color `%s': %s"
1438 s (Printexc.to_string exn);
1439 invalid
1441 if c <> invalid
1442 then set c;
1444 let te = name, "", None, textentry, ondone in
1445 state.text <- "";
1446 Textentry (
1448 fun _ ->
1449 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1454 let birdseye = isbirdseye mode in
1455 let items = [
1456 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1458 boolp "presentation"
1459 (fun () -> conf.presentation)
1460 (fun v ->
1461 conf.presentation <- v;
1462 state.anchor <- getanchor ();
1463 represent ());
1465 boolp "ignore case in searches"
1466 (fun () -> conf.icase)
1467 (fun v -> conf.icase <- v);
1469 boolp "preload"
1470 (fun () -> conf.preload)
1471 (fun v -> conf.preload <- v);
1473 boolp "verbose"
1474 (fun () -> conf.verbose)
1475 (fun v -> conf.verbose <- v);
1477 boolp "max fit"
1478 (fun () -> conf.maxhfit)
1479 (fun v -> conf.maxhfit <- v);
1481 boolp "crop hack"
1482 (fun () -> conf.crophack)
1483 (fun v -> conf.crophack <- v);
1485 boolp "throttle"
1486 (fun () -> conf.showall)
1487 (fun v -> conf.showall <- v);
1489 boolp "highlight links"
1490 (fun () -> conf.hlinks)
1491 (fun v -> conf.hlinks <- v);
1493 boolp "under info"
1494 (fun () -> conf.underinfo)
1495 (fun v -> conf.underinfo <- v);
1496 boolp "persistent bookmarks"
1497 (fun () -> conf.savebmarks)
1498 (fun v -> conf.savebmarks <- v);
1500 boolp "proportional display"
1501 (fun () -> conf.proportional)
1502 (fun v -> reinit conf.angle v);
1504 boolp "persistent location"
1505 (fun () -> conf.jumpback)
1506 (fun v -> conf.jumpback <- v);
1508 "", 0, Noaction;
1510 intp "vertical margin"
1511 (fun () -> conf.interpagespace)
1512 (fun n ->
1513 conf.interpagespace <- n;
1514 let pageno, py =
1515 match state.layout with
1516 | [] -> 0, 0
1517 | l :: _ ->
1518 l.pageno, l.pagey
1520 state.maxy <- calcheight ();
1521 let y = getpagey pageno in
1522 gotoy (y + py)
1525 intp "page bias"
1526 (fun () -> conf.pagebias)
1527 (fun v -> conf.pagebias <- v);
1529 intp "scroll step"
1530 (fun () -> conf.scrollstep)
1531 (fun n -> conf.scrollstep <- n);
1533 intp "auto scroll step"
1534 (fun () ->
1535 if state.ascrollstep > 0
1536 then state.ascrollstep
1537 else conf.autoscrollstep)
1538 (fun n ->
1539 if state.ascrollstep > 0
1540 then state.ascrollstep <- n
1541 else conf.autoscrollstep <- n);
1543 intp "zoom"
1544 (fun () -> truncate (conf.zoom *. 100.))
1545 (fun v -> setzoom ((float v) /. 100.));
1547 intp "rotation"
1548 (fun () -> conf.angle)
1549 (fun v -> reinit v conf.proportional);
1551 intp "scroll bar width"
1552 (fun () -> state.scrollw)
1553 (fun v ->
1554 state.scrollw <- v;
1555 reshape conf.winw conf.winh;
1558 intp "scroll handle height"
1559 (fun () -> conf.scrollh)
1560 (fun v -> conf.scrollh <- v;);
1562 intp "thumbnail width"
1563 (fun () -> conf.thumbw)
1564 (fun v ->
1565 conf.thumbw <- min 1920 v;
1566 match mode with
1567 | Birdseye beye ->
1568 leavebirdseye beye false;
1569 enterbirdseye ()
1570 | _ -> ()
1573 colorp "background color"
1574 (fun () -> conf.bgcolor)
1575 (fun v -> conf.bgcolor <- v);
1577 "", 0, Noaction;
1578 "Presentation mode", 0, Noaction;
1580 boolp "scrollbar visible"
1581 (fun () -> conf.scrollbarinpm)
1582 (fun v ->
1583 if v != conf.scrollbarinpm
1584 then (
1585 conf.scrollbarinpm <- v;
1586 if conf.presentation
1587 then (
1588 state.scrollw <- if v then conf.scrollbw else 0;
1589 reshape conf.winw conf.winh;
1594 "", 0, Noaction;
1595 "Pixmap Cache", 0, Noaction;
1597 intp "size (advisory)"
1598 (fun () -> conf.memlimit)
1599 (fun v -> conf.memlimit <- v);
1600 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1604 let tailer =
1605 let trailer =
1606 ("", 0, Noaction)
1607 :: ("Document", 0, Noaction)
1608 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1610 if birdseye
1611 then trailer
1612 else (
1613 ("", 0, Noaction)
1614 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1615 (btos conf.bedefault),
1617 Action (
1618 fun active first qsearch pan ->
1619 conf.bedefault <- not conf.bedefault;
1620 Items (active, first, makeitems (), qsearch, pan, mode);
1622 ) :: trailer
1625 Array.of_list (items @ tailer)
1627 state.text <- "";
1628 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1629 Glut.postRedisplay ();
1632 let enterhelpmode () =
1633 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1634 Glut.postRedisplay ();
1637 let quickbookmark ?title () =
1638 match state.layout with
1639 | [] -> ()
1640 | l :: _ ->
1641 let title =
1642 match title with
1643 | None ->
1644 let sec = Unix.gettimeofday () in
1645 let tm = Unix.localtime sec in
1646 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1647 (l.pageno+1)
1648 tm.Unix.tm_mday
1649 tm.Unix.tm_mon
1650 (tm.Unix.tm_year + 1900)
1651 tm.Unix.tm_hour
1652 tm.Unix.tm_min
1653 | Some title -> title
1655 state.bookmarks <-
1656 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1657 :: state.bookmarks
1660 let doreshape w h =
1661 state.fullscreen <- None;
1662 Glut.reshapeWindow w h;
1665 let writeopen path password =
1666 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1667 writecmd state.csock "info";
1670 let opendoc path password =
1671 invalidate ();
1672 state.path <- path;
1673 state.password <- password;
1674 state.gen <- state.gen + 1;
1676 writeopen path password;
1677 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1678 wcmd "geometry" [`i state.w; `i conf.winh];
1681 let viewkeyboard key =
1682 let enttext te =
1683 let mode = state.mode in
1684 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1685 state.text <- "";
1686 enttext ();
1687 Glut.postRedisplay ()
1689 let c = Char.chr key in
1690 match c with
1691 | '\027' | 'q' -> (* escape *)
1692 exit 0
1694 | '\008' -> (* backspace *)
1695 let y = getnav ~-1 in
1696 gotoy_and_clear_text y
1698 | 'o' ->
1699 enteroutlinemode ()
1701 | 'u' ->
1702 state.rects <- [];
1703 state.text <- "";
1704 Glut.postRedisplay ()
1706 | '/' | '?' ->
1707 let ondone isforw s =
1708 cbput state.hists.pat s;
1709 state.searchpattern <- s;
1710 search s isforw
1712 let s = String.create 1 in
1713 s.[0] <- c;
1714 enttext (s, "", Some (onhist state.hists.pat),
1715 textentry, ondone (c ='/'))
1717 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1718 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1719 setzoom (min 2.2 (conf.zoom +. incr))
1721 | '+' ->
1722 let ondone s =
1723 let n =
1724 try int_of_string s with exc ->
1725 state.text <- Printf.sprintf "bad integer `%s': %s"
1726 s (Printexc.to_string exc);
1727 max_int
1729 if n != max_int
1730 then (
1731 conf.pagebias <- n;
1732 state.text <- "page bias is now " ^ string_of_int n;
1735 enttext ("page bias", "", None, intentry, ondone)
1737 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1738 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1739 setzoom (max 0.01 (conf.zoom -. decr))
1741 | '-' ->
1742 let ondone msg = state.text <- msg in
1743 enttext (
1744 "option [acfhilpstvAPRSZ]", "", None,
1745 optentry state.mode, ondone
1748 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1749 setzoom 1.0
1751 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1752 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1753 if zoom < 1.0
1754 then setzoom zoom
1756 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1757 togglebirdseye ()
1759 | '0' .. '9' ->
1760 let ondone s =
1761 let n =
1762 try int_of_string s with exc ->
1763 state.text <- Printf.sprintf "bad integer `%s': %s"
1764 s (Printexc.to_string exc);
1767 if n >= 0
1768 then (
1769 addnav ();
1770 cbput state.hists.pag (string_of_int n);
1771 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1774 let pageentry text key =
1775 match Char.unsafe_chr key with
1776 | 'g' -> TEdone text
1777 | _ -> intentry text key
1779 let text = "x" in text.[0] <- c;
1780 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1782 | 'b' ->
1783 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1784 reshape conf.winw conf.winh;
1786 | 'l' ->
1787 conf.hlinks <- not conf.hlinks;
1788 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1789 Glut.postRedisplay ()
1791 | 'a' ->
1792 if state.ascrollstep = 0
1793 then state.ascrollstep <- conf.autoscrollstep
1794 else (
1795 conf.autoscrollstep <- state.ascrollstep;
1796 state.ascrollstep <- 0;
1799 | 'P' ->
1800 conf.presentation <- not conf.presentation;
1801 if conf.presentation
1802 then (
1803 if not conf.scrollbarinpm
1804 then state.scrollw <- 0;
1806 else
1807 state.scrollw <- conf.scrollbw;
1809 showtext ' ' ("presentation mode " ^
1810 if conf.presentation then "on" else "off");
1811 state.anchor <- getanchor ();
1812 represent ()
1814 | 'f' ->
1815 begin match state.fullscreen with
1816 | None ->
1817 state.fullscreen <- Some (conf.winw, conf.winh);
1818 Glut.fullScreen ()
1819 | Some (w, h) ->
1820 state.fullscreen <- None;
1821 doreshape w h
1824 | 'g' ->
1825 gotoy_and_clear_text 0
1827 | 'n' ->
1828 search state.searchpattern true
1830 | 'p' | 'N' ->
1831 search state.searchpattern false
1833 | 't' ->
1834 begin match state.layout with
1835 | [] -> ()
1836 | l :: _ ->
1837 gotoy_and_clear_text (getpagey l.pageno)
1840 | ' ' ->
1841 begin match List.rev state.layout with
1842 | [] -> ()
1843 | l :: _ ->
1844 let pageno = min (l.pageno+1) (state.pagecount-1) in
1845 gotoy_and_clear_text (getpagey pageno)
1848 | '\127' -> (* delte *)
1849 begin match state.layout with
1850 | [] -> ()
1851 | l :: _ ->
1852 let pageno = max 0 (l.pageno-1) in
1853 gotoy_and_clear_text (getpagey pageno)
1856 | '=' ->
1857 let f (fn, _) l =
1858 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1860 let fn, ln = List.fold_left f (-1, -1) state.layout in
1861 let s =
1862 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1863 let percent =
1864 if maxy <= 0
1865 then 100.
1866 else (100. *. (float state.y /. float maxy)) in
1867 if fn = ln
1868 then
1869 Printf.sprintf "Page %d of %d %.2f%%"
1870 (fn+1) state.pagecount percent
1871 else
1872 Printf.sprintf
1873 "Pages %d-%d of %d %.2f%%"
1874 (fn+1) (ln+1) state.pagecount percent
1876 showtext ' ' s;
1878 | 'w' ->
1879 begin match state.layout with
1880 | [] -> ()
1881 | l :: _ ->
1882 doreshape (l.pagew + state.scrollw) l.pageh;
1883 Glut.postRedisplay ();
1886 | '\'' ->
1887 enterbookmarkmode ()
1889 | 'h' ->
1890 enterhelpmode ()
1892 | 'i' ->
1893 enterinfomode ()
1895 | 'm' ->
1896 let ondone s =
1897 match state.layout with
1898 | l :: _ ->
1899 state.bookmarks <-
1900 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1901 :: state.bookmarks
1902 | _ -> ()
1904 enttext ("bookmark", "", None, textentry, ondone)
1906 | '~' ->
1907 quickbookmark ();
1908 showtext ' ' "Quick bookmark added";
1910 | 'z' ->
1911 begin match state.layout with
1912 | l :: _ ->
1913 let rect = getpdimrect l.pagedimno in
1914 let w, h =
1915 if conf.crophack
1916 then
1917 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1918 truncate (1.2 *. (rect.(3) -. rect.(0))))
1919 else
1920 (truncate (rect.(1) -. rect.(0)),
1921 truncate (rect.(3) -. rect.(0)))
1923 if w != 0 && h != 0
1924 then
1925 doreshape (w + state.scrollw) (h + conf.interpagespace)
1927 Glut.postRedisplay ();
1929 | [] -> ()
1932 | '<' | '>' ->
1933 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1935 | '[' | ']' ->
1936 state.colorscale <-
1937 max 0.0
1938 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1939 Glut.postRedisplay ()
1941 | 'k' ->
1942 begin match state.mode with
1943 | Birdseye beye -> upbirdseye beye
1944 | _ -> gotoy (clamp (-conf.scrollstep))
1947 | 'j' ->
1948 begin match state.mode with
1949 | Birdseye beye -> downbirdseye beye
1950 | _ -> gotoy (clamp conf.scrollstep)
1953 | 'r' ->
1954 state.anchor <- getanchor ();
1955 opendoc state.path state.password
1957 | _ ->
1958 vlog "huh? %d %c" key (Char.chr key);
1961 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1962 let enttext te =
1963 state.mode <- Textentry (te, onleave);
1964 state.text <- "";
1965 enttext ();
1966 Glut.postRedisplay ()
1968 match Char.unsafe_chr key with
1969 | '\008' -> (* backspace *)
1970 let len = String.length text in
1971 if len = 0
1972 then (
1973 onleave Cancel;
1974 Glut.postRedisplay ();
1976 else (
1977 let s = String.sub text 0 (len - 1) in
1978 enttext (c, s, opthist, onkey, ondone)
1981 | '\r' | '\n' ->
1982 ondone text;
1983 onleave Confirm;
1984 Glut.postRedisplay ()
1986 | '\027' -> (* escape *)
1987 begin match opthist with
1988 | None -> ()
1989 | Some (_, onhistcancel) -> onhistcancel ()
1990 end;
1991 onleave Cancel;
1992 Glut.postRedisplay ()
1994 | _ ->
1995 begin match onkey text key with
1996 | TEdone text ->
1997 onleave Confirm;
1998 ondone text;
1999 Glut.postRedisplay ()
2001 | TEcont text ->
2002 enttext (c, text, opthist, onkey, ondone);
2004 | TEstop ->
2005 onleave Cancel;
2006 Glut.postRedisplay ()
2008 | TEswitch te ->
2009 state.mode <- Textentry (te, onleave);
2010 Glut.postRedisplay ()
2011 end;
2014 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2015 match key with
2016 | 27 -> (* escape *)
2017 leavebirdseye beye true
2019 | 12 -> (* ctrl-l *)
2020 let y, h = getpageyh pageno in
2021 let top = (conf.winh - h) / 2 in
2022 gotoy (max 0 (y - top))
2024 | 13 -> (* enter *)
2025 leavebirdseye beye false
2027 | _ ->
2028 viewkeyboard key
2031 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2032 let set active first qsearch =
2033 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2035 let search active pattern incr =
2036 let dosearch re =
2037 let rec loop n =
2038 if n = Array.length items || n = -1
2039 then None
2040 else
2041 let (s, _, _) = items.(n) in
2043 (try ignore (Str.search_forward re s 0); true
2044 with Not_found -> false)
2045 then Some n
2046 else loop (n + incr)
2048 loop active
2051 let re = Str.regexp_case_fold pattern in
2052 dosearch re
2053 with Failure s ->
2054 state.text <- s;
2055 None
2057 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2058 match key with
2059 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2060 let incr = if key = 18 then -1 else 1 in
2061 let active, first =
2062 match search (active + incr) qsearch incr with
2063 | None ->
2064 state.text <- qsearch ^ " [not found]";
2065 active, first
2066 | Some active ->
2067 state.text <- qsearch;
2068 active, firstof active
2070 set active first qsearch;
2071 Glut.postRedisplay ();
2073 | 8 -> (* backspace *)
2074 let len = String.length qsearch in
2075 if len = 0
2076 then ()
2077 else (
2078 if len = 1
2079 then (
2080 state.text <- "";
2081 set active first "";
2083 else
2084 let qsearch = String.sub qsearch 0 (len - 1) in
2085 let active, first =
2086 match search active qsearch ~-1 with
2087 | None ->
2088 state.text <- qsearch ^ " [not found]";
2089 active, first
2090 | Some active ->
2091 state.text <- qsearch;
2092 active, firstof active
2094 set active first qsearch
2096 Glut.postRedisplay ()
2098 | _ when key >= 32 && key < 127 ->
2099 let pattern = addchar qsearch (Char.chr key) in
2100 let active, first =
2101 match search active pattern 1 with
2102 | None ->
2103 state.text <- pattern ^ " [not found]";
2104 active, first
2105 | Some active ->
2106 state.text <- pattern;
2107 active, firstof active
2109 set active first pattern;
2110 Glut.postRedisplay ()
2112 | 27 -> (* escape *)
2113 state.text <- "";
2114 state.mode <- oldmode;
2115 Glut.postRedisplay ();
2117 | 13 -> (* enter *)
2118 if active < Array.length items
2119 then (
2120 match items.(active) with
2121 | _, _, Action f ->
2122 state.mode <- f active first qsearch pan
2124 | _, _, Noaction ->
2125 state.text <- "";
2126 state.mode <- oldmode
2128 Glut.postRedisplay ();
2130 | _ -> dolog "unknown key %d" key
2133 let outlinekeyboard key
2134 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2135 let narrow outlines pattern =
2136 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2137 match reopt with
2138 | None -> None
2139 | Some re ->
2140 let rec fold accu n =
2141 if n = -1
2142 then accu
2143 else
2144 let (s, _, _) as o = outlines.(n) in
2145 let accu =
2146 if (try ignore (Str.search_forward re s 0); true
2147 with Not_found -> false)
2148 then (o :: accu)
2149 else accu
2151 fold accu (n-1)
2153 let matched = fold [] (Array.length outlines - 1) in
2154 if matched = [] then None else Some (Array.of_list matched)
2156 let search active pattern incr =
2157 let dosearch re =
2158 let rec loop n =
2159 if n = Array.length outlines || n = -1
2160 then None
2161 else
2162 let (s, _, _) = outlines.(n) in
2164 (try ignore (Str.search_forward re s 0); true
2165 with Not_found -> false)
2166 then Some n
2167 else loop (n + incr)
2169 loop active
2172 let re = Str.regexp_case_fold pattern in
2173 dosearch re
2174 with Failure s ->
2175 state.text <- s;
2176 None
2178 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2179 match key with
2180 | 27 -> (* escape *)
2181 if String.length qsearch = 0
2182 then (
2183 state.text <- "";
2184 state.mode <- oldmode;
2185 Glut.postRedisplay ();
2187 else (
2188 state.text <- "";
2189 state.mode <- Outline (
2190 allowdel, active, first, outlines, "", pan, oldmode
2192 Glut.postRedisplay ();
2195 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2196 let incr = if key = 18 then -1 else 1 in
2197 let active, first =
2198 match search (active + incr) qsearch incr with
2199 | None ->
2200 state.text <- qsearch ^ " [not found]";
2201 active, first
2202 | Some active ->
2203 state.text <- qsearch;
2204 active, firstof active
2206 state.mode <- Outline (
2207 allowdel, active, first, outlines, qsearch, pan, oldmode
2209 Glut.postRedisplay ();
2211 | 8 -> (* backspace *)
2212 let len = String.length qsearch in
2213 if len = 0
2214 then ()
2215 else (
2216 if len = 1
2217 then (
2218 state.text <- "";
2219 state.mode <- Outline (
2220 allowdel, active, first, outlines, "", pan, oldmode
2223 else
2224 let qsearch = String.sub qsearch 0 (len - 1) in
2225 let active, first =
2226 match search active qsearch ~-1 with
2227 | None ->
2228 state.text <- qsearch ^ " [not found]";
2229 active, first
2230 | Some active ->
2231 state.text <- qsearch;
2232 active, firstof active
2234 state.mode <- Outline (
2235 allowdel, active, first, outlines, qsearch, pan, oldmode
2238 Glut.postRedisplay ()
2240 | 13 -> (* enter *)
2241 if active < Array.length outlines
2242 then (
2243 let (_, _, anchor) = outlines.(active) in
2244 addnav ();
2245 gotoanchor anchor;
2247 state.text <- "";
2248 if allowdel then state.bookmarks <- Array.to_list outlines;
2249 state.mode <- oldmode;
2250 Glut.postRedisplay ();
2252 | _ when key >= 32 && key < 127 ->
2253 let pattern = addchar qsearch (Char.chr key) in
2254 let active, first =
2255 match search active pattern 1 with
2256 | None ->
2257 state.text <- pattern ^ " [not found]";
2258 active, first
2259 | Some active ->
2260 state.text <- pattern;
2261 active, firstof active
2263 state.mode <- Outline (
2264 allowdel, active, first, outlines, pattern, pan, oldmode
2266 Glut.postRedisplay ()
2268 | 14 when not allowdel -> (* ctrl-n *)
2269 if String.length qsearch > 0
2270 then (
2271 let optoutlines = narrow outlines qsearch in
2272 begin match optoutlines with
2273 | None -> state.text <- "can't narrow"
2274 | Some outlines ->
2275 state.mode <- Outline (
2276 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2278 match state.outlines with
2279 | Olist _ -> ()
2280 | Oarray a ->
2281 state.outlines <- Onarrow (qsearch, outlines, a)
2282 | Onarrow (_, _, b) ->
2283 state.outlines <- Onarrow (qsearch, outlines, b)
2284 end;
2286 Glut.postRedisplay ()
2288 | 21 when not allowdel -> (* ctrl-u *)
2289 let outline =
2290 match state.outlines with
2291 | Oarray a -> a
2292 | Olist l ->
2293 let a = Array.of_list (List.rev l) in
2294 state.outlines <- Oarray a;
2296 | Onarrow (_, _, b) ->
2297 state.outlines <- Oarray b;
2298 state.text <- "";
2301 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2302 Glut.postRedisplay ()
2304 | 12 -> (* ctrl-l *)
2305 state.mode <- Outline
2306 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2307 Glut.postRedisplay ()
2309 | 127 when allowdel -> (* delete *)
2310 let len = Array.length outlines - 1 in
2311 if len = 0
2312 then (
2313 state.mode <- View;
2314 state.bookmarks <- [];
2316 else (
2317 let bookmarks = Array.init len
2318 (fun i ->
2319 let i = if i >= active then i + 1 else i in
2320 outlines.(i)
2323 state.mode <-
2324 Outline (
2325 allowdel,
2326 min active (len-1),
2327 min first (len-1),
2328 bookmarks, qsearch,
2330 oldmode
2333 Glut.postRedisplay ()
2335 | _ -> dolog "unknown key %d" key
2338 let keyboard ~key ~x ~y =
2339 ignore x;
2340 ignore y;
2341 if key = 7 (* ctrl-g *)
2342 then
2343 wcmd "interrupt" []
2344 else
2345 match state.mode with
2346 | Outline outline -> outlinekeyboard key outline
2347 | Textentry textentry -> textentrykeyboard key textentry
2348 | Birdseye birdseye -> birdseyekeyboard key birdseye
2349 | View -> viewkeyboard key
2350 | Items items -> itemskeyboard key items
2353 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2354 match key with
2355 | Glut.KEY_UP -> upbirdseye beye
2356 | Glut.KEY_DOWN -> downbirdseye beye
2358 | Glut.KEY_PAGE_UP ->
2359 begin match state.layout with
2360 | l :: _ ->
2361 if l.pagey != 0
2362 then (
2363 state.mode <- Birdseye (
2364 conf, leftx, l.pageno, hooverpageno, anchor
2366 gotopage1 l.pageno 0;
2368 else (
2369 let layout = layout (state.y-conf.winh) conf.winh in
2370 match layout with
2371 | [] -> gotoy (clamp (-conf.winh))
2372 | l :: _ ->
2373 state.mode <- Birdseye (
2374 conf, leftx, l.pageno, hooverpageno, anchor
2376 gotopage1 l.pageno 0
2379 | [] -> gotoy (clamp (-conf.winh))
2380 end;
2382 | Glut.KEY_PAGE_DOWN ->
2383 begin match List.rev state.layout with
2384 | l :: _ ->
2385 let layout = layout (state.y + conf.winh) conf.winh in
2386 begin match layout with
2387 | [] ->
2388 let incr = l.pageh - l.pagevh in
2389 if incr = 0
2390 then (
2391 state.mode <-
2392 Birdseye (
2393 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2395 Glut.postRedisplay ();
2397 else gotoy (clamp (incr + conf.interpagespace*2));
2399 | l :: _ ->
2400 state.mode <-
2401 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2402 gotopage1 l.pageno 0;
2405 | [] -> gotoy (clamp conf.winh)
2406 end;
2408 | Glut.KEY_HOME ->
2409 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2410 gotopage1 0 0
2412 | Glut.KEY_END ->
2413 let pageno = state.pagecount - 1 in
2414 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2415 if not (pagevisible state.layout pageno)
2416 then
2417 let h =
2418 match List.rev state.pdims with
2419 | [] -> conf.winh
2420 | (_, _, h, _) :: _ -> h
2422 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2423 else Glut.postRedisplay ();
2424 | _ -> ()
2427 let setautoscrollspeed goingdown =
2428 let incr = max 1 (state.ascrollstep / 2) in
2429 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2430 state.ascrollstep <- astep;
2433 let special ~key ~x ~y =
2434 ignore x;
2435 ignore y;
2436 match state.mode with
2437 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2438 togglebirdseye ()
2440 | Birdseye vals ->
2441 birdseyespecial key vals
2443 | View when key = Glut.KEY_F1 ->
2444 enterhelpmode ()
2446 | View ->
2447 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2448 then setautoscrollspeed (key = Glut.KEY_DOWN)
2449 else
2450 let y =
2451 match key with
2452 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2453 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2454 | Glut.KEY_DOWN -> clamp conf.scrollstep
2455 | Glut.KEY_PAGE_UP ->
2456 if Glut.getModifiers () land Glut.active_ctrl != 0
2457 then
2458 match state.layout with
2459 | [] -> state.y
2460 | l :: _ -> state.y - l.pagey
2461 else
2462 clamp (-conf.winh)
2463 | Glut.KEY_PAGE_DOWN ->
2464 if Glut.getModifiers () land Glut.active_ctrl != 0
2465 then
2466 match List.rev state.layout with
2467 | [] -> state.y
2468 | l :: _ -> getpagey l.pageno
2469 else
2470 clamp conf.winh
2471 | Glut.KEY_HOME -> addnav (); 0
2472 | Glut.KEY_END ->
2473 addnav ();
2474 state.maxy - (if conf.maxhfit then conf.winh else 0)
2476 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2477 Glut.getModifiers () land Glut.active_alt != 0 ->
2478 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2480 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2481 state.x <- state.x - 10;
2482 state.y
2483 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2484 state.x <- state.x + 10;
2485 state.y
2487 | _ -> state.y
2489 gotoy_and_clear_text y
2491 | Textentry
2492 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2493 let s =
2494 match key with
2495 | Glut.KEY_UP -> action HCprev
2496 | Glut.KEY_DOWN -> action HCnext
2497 | Glut.KEY_HOME -> action HCfirst
2498 | Glut.KEY_END -> action HClast
2499 | _ -> state.text
2501 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2502 Glut.postRedisplay ()
2504 | Textentry _ -> ()
2506 | Items (active, first, items, qsearch, pan, oldmode) ->
2507 let maxrows = maxoutlinerows () in
2508 let itemcount = Array.length items in
2509 let hasaction = function
2510 | (_, _, Noaction) -> false
2511 | _ -> true
2513 let find start incr =
2514 let rec find i =
2515 if i = -1 || i = itemcount
2516 then -1
2517 else (
2518 if hasaction items.(i)
2519 then i
2520 else find (i + incr)
2523 find start
2525 let set active first =
2526 let first = max 0 (min first (itemcount - maxrows)) in
2527 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2529 let navigate incr =
2530 let isvisible first n = n >= first && n - first <= maxrows in
2531 let active, first =
2532 let incr1 = if incr > 0 then 1 else -1 in
2533 if isvisible first active
2534 then
2535 let next =
2536 let next = active + incr in
2537 let next =
2538 if next < 0 || next >= itemcount
2539 then -1
2540 else find next incr1
2542 if next = -1 || abs (active - next) > maxrows
2543 then -1
2544 else next
2546 if next = -1
2547 then
2548 let first = first + incr in
2549 let first = max 0 (min first (itemcount - 1)) in
2550 let next =
2551 let next = active + incr in
2552 let next = max 0 (min next (itemcount - 1)) in
2553 find next ~-incr1
2555 let active = if next = -1 then active else next in
2556 active, first
2557 else
2558 let first = min next first in
2559 next, first
2560 else
2561 let first = first + incr in
2562 let first = max 0 (min first (itemcount - 1)) in
2563 let active =
2564 let next = active + incr in
2565 let next = max 0 (min next (itemcount - 1)) in
2566 let next = find next incr1 in
2567 if next = -1 || abs (active - first) > maxrows
2568 then active
2569 else next
2571 active, first
2573 set active first;
2574 Glut.postRedisplay ()
2576 begin match key with
2577 | Glut.KEY_UP -> navigate ~-1
2578 | Glut.KEY_DOWN -> navigate 1
2579 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2580 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2582 | Glut.KEY_RIGHT ->
2583 state.mode <- Items (
2584 active, first, items, qsearch, min 0 (pan - 1), oldmode
2586 Glut.postRedisplay ()
2588 | Glut.KEY_LEFT ->
2589 state.mode <- Items (
2590 active, first, items, qsearch, min 0 (pan + 1), oldmode
2592 Glut.postRedisplay ()
2594 | Glut.KEY_HOME ->
2595 let active = find 0 1 in
2596 set active 0;
2597 Glut.postRedisplay ()
2599 | Glut.KEY_END ->
2600 let first = max 0 (itemcount - maxrows) in
2601 let active = find (itemcount - 1) ~-1 in
2602 set active first;
2603 Glut.postRedisplay ()
2605 | _ -> ()
2606 end;
2608 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2609 let maxrows = maxoutlinerows () in
2610 let calcfirst first active =
2611 if active > first
2612 then
2613 let rows = active - first in
2614 if rows > maxrows then active - maxrows else first
2615 else active
2617 let navigate incr =
2618 let active = active + incr in
2619 let active = max 0 (min active (Array.length outlines - 1)) in
2620 let first = calcfirst first active in
2621 state.mode <- Outline (
2622 allowdel, active, first, outlines, qsearch, pan, oldmode
2624 Glut.postRedisplay ()
2626 let updownlevel incr =
2627 let len = Array.length outlines in
2628 let (_, curlevel, _) = outlines.(active) in
2629 let rec flow i =
2630 if i = len then i-1 else if i = -1 then 0 else
2631 let (_, l, _) = outlines.(i) in
2632 if l != curlevel then i else flow (i+incr)
2634 let active = flow active in
2635 let first = calcfirst first active in
2636 state.mode <- Outline (
2637 allowdel, active, first, outlines, qsearch, pan, oldmode
2639 Glut.postRedisplay ()
2641 match key with
2642 | Glut.KEY_UP -> navigate ~-1
2643 | Glut.KEY_DOWN -> navigate 1
2644 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2645 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2647 | Glut.KEY_RIGHT ->
2648 if Glut.getModifiers () land Glut.active_ctrl != 0
2649 then (
2650 state.mode <- Outline (
2651 allowdel, active, first, outlines,
2652 qsearch, min 0 (pan + 1), oldmode
2654 Glut.postRedisplay ();
2656 else (
2657 if not allowdel
2658 then updownlevel 1
2661 | Glut.KEY_LEFT ->
2662 if Glut.getModifiers () land Glut.active_ctrl != 0
2663 then (
2664 state.mode <- Outline (
2665 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2667 Glut.postRedisplay ();
2669 else (
2670 if not allowdel
2671 then updownlevel ~-1
2674 | Glut.KEY_HOME ->
2675 state.mode <- Outline (
2676 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2678 Glut.postRedisplay ()
2680 | Glut.KEY_END ->
2681 let active = Array.length outlines - 1 in
2682 let first = max 0 (active - maxrows) in
2683 state.mode <- Outline (
2684 allowdel, active, first, outlines, qsearch, pan, oldmode
2686 Glut.postRedisplay ()
2688 | _ -> ()
2691 let drawplaceholder l =
2692 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2693 GlDraw.rect
2694 (float l.pagex, float l.pagedispy)
2695 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2697 let x = float (if margin < 0 then -margin else l.pagex)
2698 and y = float (l.pagedispy + 13) in
2699 let font = Glut.BITMAP_8_BY_13 in
2700 GlDraw.color (0.0, 0.0, 0.0);
2701 GlPix.raster_pos ~x ~y ();
2702 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2703 ("Loading " ^ string_of_int (l.pageno + 1));
2706 let now () = Unix.gettimeofday ();;
2708 let drawpage l =
2709 let color =
2710 match state.mode with
2711 | Textentry _ -> scalecolor 0.4
2712 | View | Outline _ | Items _ -> scalecolor 1.0
2713 | Birdseye (_, _, pageno, hooverpageno, _) ->
2714 if l.pageno = hooverpageno
2715 then scalecolor 0.9
2716 else (
2717 if l.pageno = pageno
2718 then scalecolor 1.0
2719 else scalecolor 0.8
2722 GlDraw.color color;
2723 begin match getopaque l.pageno with
2724 | Some (opaque, _) when validopaque opaque ->
2725 let a = now () in
2726 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2727 opaque;
2728 let b = now () in
2729 let d = b-.a in
2730 vlog "draw %d %f sec" l.pageno d;
2732 | _ ->
2733 drawplaceholder l;
2734 end;
2737 let scrollph y =
2738 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2739 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2740 let sh = float conf.winh /. sh in
2741 let sh = max sh (float conf.scrollh) in
2743 let percent =
2744 if state.y = state.maxy
2745 then 1.0
2746 else float y /. float maxy
2748 let position = (float conf.winh -. sh) *. percent in
2750 let position =
2751 if position +. sh > float conf.winh
2752 then float conf.winh -. sh
2753 else position
2755 position, sh;
2758 let scrollindicator () =
2759 GlDraw.color (0.64 , 0.64, 0.64);
2760 GlDraw.rect
2761 (float (conf.winw - state.scrollw), 0.)
2762 (float conf.winw, float conf.winh)
2764 GlDraw.color (0.0, 0.0, 0.0);
2766 let position, sh = scrollph state.y in
2767 GlDraw.rect
2768 (float (conf.winw - state.scrollw), position)
2769 (float conf.winw, position +. sh)
2773 let showsel margin =
2774 match state.mstate with
2775 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2778 | Msel ((x0, y0), (x1, y1)) ->
2779 let rec loop = function
2780 | l :: ls ->
2781 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2782 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2783 then
2784 match getopaque l.pageno with
2785 | Some (opaque, _) when validopaque opaque ->
2786 let oy = -l.pagey + l.pagedispy in
2787 seltext opaque
2788 (x0 - margin - state.x, y0,
2789 x1 - margin - state.x, y1) oy;
2791 | _ -> ()
2792 else loop ls
2793 | [] -> ()
2795 loop state.layout
2798 let showrects () =
2799 let panx = float state.x in
2800 Gl.enable `blend;
2801 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2802 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2803 List.iter
2804 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2805 List.iter (fun l ->
2806 if l.pageno = pageno
2807 then (
2808 let d = float (l.pagedispy - l.pagey) in
2809 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2810 GlDraw.begins `quads;
2812 GlDraw.vertex2 (x0+.panx, y0+.d);
2813 GlDraw.vertex2 (x1+.panx, y1+.d);
2814 GlDraw.vertex2 (x2+.panx, y2+.d);
2815 GlDraw.vertex2 (x3+.panx, y3+.d);
2817 GlDraw.ends ();
2819 ) state.layout
2820 ) state.rects
2822 Gl.disable `blend;
2825 let showstrings active first pan strings =
2826 Gl.enable `blend;
2827 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2828 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2829 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2830 Gl.disable `blend;
2832 GlDraw.color (1., 1., 1.);
2833 let font = Glut.BITMAP_9_BY_15 in
2834 let draw_string x y s =
2835 GlPix.raster_pos ~x ~y ();
2836 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2838 let rec loop row =
2839 if row = Array.length strings || (row - first) * 16 > conf.winh
2840 then ()
2841 else (
2842 let (s, level, _) = strings.(row) in
2843 let y = (row - first) * 16 in
2844 let x = 5 + 15*(max 0 (level+pan)) in
2845 if row = active
2846 then (
2847 Gl.enable `blend;
2848 GlDraw.polygon_mode `both `line;
2849 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2850 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2851 GlDraw.rect (0., float (y + 1))
2852 (float (conf.winw - 1), float (y + 18));
2853 GlDraw.polygon_mode `both `fill;
2854 Gl.disable `blend;
2855 GlDraw.color (1., 1., 1.);
2857 let draw_string s =
2858 let l = String.length s in
2859 if pan < 0
2860 then (
2861 let pan = pan * 2 in
2862 let pos = pan + level in
2863 let left = l + pos in
2864 if left > 0
2865 then
2866 let s =
2867 if left > l
2868 then s
2869 else String.sub s (-pos) left
2871 draw_string (float x) (float (y + 16)) s
2873 else
2874 draw_string (float (x + pan*15)) (float (y + 16)) s
2876 draw_string s;
2877 loop (row+1)
2880 loop first
2883 let showoutline (_, active, first, outlines, _, pan, _) =
2884 showstrings active first pan outlines;
2887 let showitems (active, first, items, _, pan, _) =
2888 showstrings active first pan items;
2891 let display () =
2892 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2893 GlDraw.viewport margin 0 state.w conf.winh;
2894 pagematrix ();
2895 GlClear.color (scalecolor2 conf.bgcolor);
2896 GlClear.clear [`color];
2897 if conf.zoom > 1.0
2898 then (
2899 Gl.enable `scissor_test;
2900 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2902 List.iter drawpage state.layout;
2903 if conf.zoom > 1.0
2904 then
2905 Gl.disable `scissor_test
2907 if state.x != 0
2908 then (
2909 let x = -.float state.x in
2910 GlMat.translate ~x ();
2912 showrects ();
2913 showsel margin;
2914 GlDraw.viewport 0 0 conf.winw conf.winh;
2915 winmatrix ();
2916 scrollindicator ();
2917 begin match state.mode with
2918 | Items items -> showitems items
2919 | Outline outline -> showoutline outline
2920 | _ -> ()
2921 end;
2922 enttext ();
2923 Glut.swapBuffers ();
2926 let getunder x y =
2927 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2928 let x = x - margin - state.x in
2929 let rec f = function
2930 | l :: rest ->
2931 begin match getopaque l.pageno with
2932 | Some (opaque, _) when validopaque opaque ->
2933 let y = y - l.pagedispy in
2934 if y > 0
2935 then
2936 let y = l.pagey + y in
2937 let x = x - l.pagex in
2938 match whatsunder opaque x y with
2939 | Unone -> f rest
2940 | under -> under
2941 else
2942 f rest
2943 | _ ->
2944 f rest
2946 | [] -> Unone
2948 f state.layout
2951 let viewmouse button bstate x y =
2952 match button with
2953 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2954 if Glut.getModifiers () land Glut.active_ctrl != 0
2955 then (
2956 match state.mstate with
2957 | Mzoom (oldn, i) ->
2958 if oldn = n
2959 then (
2960 if i = 2
2961 then
2962 let incr =
2963 match n with
2964 | 4 ->
2965 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2966 | _ ->
2967 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2969 let zoom = conf.zoom +. incr in
2970 setzoom zoom;
2971 state.mstate <- Mzoom (n, 0);
2972 else
2973 state.mstate <- Mzoom (n, i+1);
2975 else state.mstate <- Mzoom (n, 0)
2977 | _ -> state.mstate <- Mzoom (n, 0)
2979 else (
2980 if state.ascrollstep > 0
2981 then
2982 setautoscrollspeed (n=4)
2983 else
2984 let incr =
2985 if n = 3
2986 then -conf.scrollstep
2987 else conf.scrollstep
2989 let incr = incr * 2 in
2990 let y = clamp incr in
2991 gotoy_and_clear_text y
2994 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2995 if bstate = Glut.DOWN
2996 then (
2997 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2998 state.mstate <- Mpan (x, y)
3000 else
3001 state.mstate <- Mnone
3003 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3004 if bstate = Glut.DOWN
3005 then
3006 let position, sh = scrollph state.y in
3007 if y > truncate position && y < truncate (position +. sh)
3008 then
3009 state.mstate <- Mscroll
3010 else
3011 let percent = float y /. float conf.winh in
3012 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3013 gotoy desty;
3014 state.mstate <- Mscroll
3015 else
3016 state.mstate <- Mnone
3018 | Glut.LEFT_BUTTON ->
3019 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3020 begin match dest with
3021 | Ulinkgoto (pageno, top) ->
3022 if pageno >= 0
3023 then (
3024 addnav ();
3025 gotopage1 pageno top;
3028 | Ulinkuri s ->
3029 print_endline s
3031 | Unone when bstate = Glut.DOWN ->
3032 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3033 state.mstate <- Mpan (x, y);
3035 | Unone | Utext _ ->
3036 if bstate = Glut.DOWN
3037 then (
3038 if conf.angle mod 360 = 0
3039 then (
3040 state.mstate <- Msel ((x, y), (x, y));
3041 Glut.postRedisplay ()
3044 else (
3045 match state.mstate with
3046 | Mnone -> ()
3048 | Mzoom _ | Mscroll ->
3049 state.mstate <- Mnone
3051 | Mpan _ ->
3052 Glut.setCursor Glut.CURSOR_INHERIT;
3053 state.mstate <- Mnone
3055 | Msel ((_, y0), (_, y1)) ->
3056 let f l =
3057 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3058 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3059 then
3060 match getopaque l.pageno with
3061 | Some (opaque, _) when validopaque opaque ->
3062 copysel opaque
3063 | _ -> ()
3065 List.iter f state.layout;
3066 copysel ""; (* ugly *)
3067 Glut.setCursor Glut.CURSOR_INHERIT;
3068 state.mstate <- Mnone;
3072 | _ -> ()
3075 let birdseyemouse button bstate x y
3076 (conf, leftx, _, hooverpageno, anchor) =
3077 match button with
3078 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3079 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3080 let rec loop = function
3081 | [] -> ()
3082 | l :: rest ->
3083 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3084 && x > margin && x < margin + l.pagew
3085 then (
3086 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3088 else loop rest
3090 loop state.layout
3091 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3092 | _ -> ()
3095 let mouse bstate button x y =
3096 match state.mode with
3097 | View -> viewmouse button bstate x y
3098 | Birdseye beye -> birdseyemouse button bstate x y beye
3099 | Textentry _ | Outline _ | Items _ -> ()
3102 let mouse ~button ~state ~x ~y = mouse state button x y;;
3104 let motion ~x ~y =
3105 match state.mode with
3106 | Outline _ -> ()
3107 | _ ->
3108 match state.mstate with
3109 | Mzoom _ | Mnone -> ()
3111 | Mpan (x0, y0) ->
3112 let dx = x - x0
3113 and dy = y0 - y in
3114 state.mstate <- Mpan (x, y);
3115 if conf.zoom > 1.0 then state.x <- state.x + dx;
3116 let y = clamp dy in
3117 gotoy_and_clear_text y
3119 | Msel (a, _) ->
3120 state.mstate <- Msel (a, (x, y));
3121 Glut.postRedisplay ()
3123 | Mscroll ->
3124 let y = min conf.winh (max 0 y) in
3125 let percent = float y /. float conf.winh in
3126 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3127 gotoy_and_clear_text y
3130 let pmotion ~x ~y =
3131 match state.mode with
3132 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3133 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3134 let rec loop = function
3135 | [] ->
3136 if hooverpageno != -1
3137 then (
3138 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3139 Glut.postRedisplay ();
3141 | l :: rest ->
3142 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3143 && x > margin && x < margin + l.pagew
3144 then (
3145 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3146 Glut.postRedisplay ();
3148 else loop rest
3150 loop state.layout
3152 | Outline _ | Items _ | Textentry _ -> ()
3153 | View ->
3154 match state.mstate with
3155 | Mnone ->
3156 begin match getunder x y with
3157 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3158 | Ulinkuri uri ->
3159 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3160 Glut.setCursor Glut.CURSOR_INFO
3161 | Ulinkgoto (page, _) ->
3162 if conf.underinfo
3163 then showtext 'p' ("age: " ^ string_of_int page);
3164 Glut.setCursor Glut.CURSOR_INFO
3165 | Utext s ->
3166 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3167 Glut.setCursor Glut.CURSOR_TEXT
3170 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3175 module State =
3176 struct
3177 open Parser
3179 let home =
3181 match Sys.os_type with
3182 | "Win32" -> Sys.getenv "HOMEPATH"
3183 | _ -> Sys.getenv "HOME"
3184 with exn ->
3185 prerr_endline
3186 ("Can not determine home directory location: " ^
3187 Printexc.to_string exn);
3191 let config_of c attrs =
3192 let apply c k v =
3194 match k with
3195 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3196 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3197 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3198 | "preload" -> { c with preload = bool_of_string v }
3199 | "page-bias" -> { c with pagebias = int_of_string v }
3200 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3201 | "auto-scroll-step" ->
3202 { c with autoscrollstep = max 0 (int_of_string v) }
3203 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3204 | "crop-hack" -> { c with crophack = bool_of_string v }
3205 | "throttle" -> { c with showall = bool_of_string v }
3206 | "highlight-links" -> { c with hlinks = bool_of_string v }
3207 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3208 | "vertical-margin" ->
3209 { c with interpagespace = max 0 (int_of_string v) }
3210 | "zoom" ->
3211 let zoom = float_of_string v /. 100. in
3212 let zoom = max 0.01 (min 2.2 zoom) in
3213 { c with zoom = zoom }
3214 | "presentation" -> { c with presentation = bool_of_string v }
3215 | "rotation-angle" -> { c with angle = int_of_string v }
3216 | "width" -> { c with winw = max 20 (int_of_string v) }
3217 | "height" -> { c with winh = max 20 (int_of_string v) }
3218 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3219 | "proportional-display" -> { c with proportional = bool_of_string v }
3220 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3221 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3222 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3223 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3224 | "persistent-location" -> { c with jumpback = bool_of_string v }
3225 | "background-color" -> { c with bgcolor = color_of_string v }
3226 | "scrollbar-in-presentation" ->
3227 { c with scrollbarinpm = bool_of_string v }
3228 | _ -> c
3229 with exn ->
3230 prerr_endline ("Error processing attribute (`" ^
3231 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3234 let rec fold c = function
3235 | [] -> c
3236 | (k, v) :: rest ->
3237 let c = apply c k v in
3238 fold c rest
3240 fold c attrs;
3243 let fromstring f pos n v d =
3244 try f v
3245 with exn ->
3246 dolog "Error processing attribute (%S=%S) at %d\n%s"
3247 n v pos (Printexc.to_string exn)
3252 let bookmark_of attrs =
3253 let rec fold title page rely = function
3254 | ("title", v) :: rest -> fold v page rely rest
3255 | ("page", v) :: rest -> fold title v rely rest
3256 | ("rely", v) :: rest -> fold title page v rest
3257 | _ :: rest -> fold title page rely rest
3258 | [] -> title, page, rely
3260 fold "invalid" "0" "0" attrs
3263 let doc_of attrs =
3264 let rec fold path page rely pan = function
3265 | ("path", v) :: rest -> fold v page rely pan rest
3266 | ("page", v) :: rest -> fold path v rely pan rest
3267 | ("rely", v) :: rest -> fold path page v pan rest
3268 | ("pan", v) :: rest -> fold path page rely v rest
3269 | _ :: rest -> fold path page rely pan rest
3270 | [] -> path, page, rely, pan
3272 fold "" "0" "0" "0" attrs
3275 let setconf dst src =
3276 dst.scrollbw <- src.scrollbw;
3277 dst.scrollh <- src.scrollh;
3278 dst.icase <- src.icase;
3279 dst.preload <- src.preload;
3280 dst.pagebias <- src.pagebias;
3281 dst.verbose <- src.verbose;
3282 dst.scrollstep <- src.scrollstep;
3283 dst.maxhfit <- src.maxhfit;
3284 dst.crophack <- src.crophack;
3285 dst.autoscrollstep <- src.autoscrollstep;
3286 dst.showall <- src.showall;
3287 dst.hlinks <- src.hlinks;
3288 dst.underinfo <- src.underinfo;
3289 dst.interpagespace <- src.interpagespace;
3290 dst.zoom <- src.zoom;
3291 dst.presentation <- src.presentation;
3292 dst.angle <- src.angle;
3293 dst.winw <- src.winw;
3294 dst.winh <- src.winh;
3295 dst.savebmarks <- src.savebmarks;
3296 dst.memlimit <- src.memlimit;
3297 dst.proportional <- src.proportional;
3298 dst.texcount <- src.texcount;
3299 dst.sliceheight <- src.sliceheight;
3300 dst.thumbw <- src.thumbw;
3301 dst.jumpback <- src.jumpback;
3302 dst.bgcolor <- src.bgcolor;
3303 dst.scrollbarinpm <- src.scrollbarinpm;
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)
3504 let w, h =
3505 if always
3506 then dc.winw, dc.winh
3507 else
3508 match state.fullscreen with
3509 | Some wh -> wh
3510 | None -> c.winw, c.winh
3512 let zoom, presentation, interpagespace, showall=
3513 if always
3514 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3515 else
3516 match state.mode with
3517 | Birdseye (bc, _, _, _, _) ->
3518 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3519 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3521 oi "width" w dc.winw;
3522 oi "height" h dc.winh;
3523 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3524 oi "scroll-handle-height" c.scrollh dc.scrollh;
3525 ob "case-insensitive-search" c.icase dc.icase;
3526 ob "preload" c.preload dc.preload;
3527 oi "page-bias" c.pagebias dc.pagebias;
3528 oi "scroll-step" c.scrollstep dc.scrollstep;
3529 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3530 ob "max-height-fit" c.maxhfit dc.maxhfit;
3531 ob "crop-hack" c.crophack dc.crophack;
3532 ob "throttle" showall dc.showall;
3533 ob "highlight-links" c.hlinks dc.hlinks;
3534 ob "under-cursor-info" c.underinfo dc.underinfo;
3535 oi "vertical-margin" interpagespace dc.interpagespace;
3536 oz "zoom" zoom dc.zoom;
3537 ob "presentation" presentation dc.presentation;
3538 oi "rotation-angle" c.angle dc.angle;
3539 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3540 ob "proportional-display" c.proportional dc.proportional;
3541 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3542 oi "texcount" c.texcount dc.texcount;
3543 oi "slice-height" c.sliceheight dc.sliceheight;
3544 oi "thumbnail-width" c.thumbw dc.thumbw;
3545 ob "persistent-location" c.jumpback dc.jumpback;
3546 oc "background-color" c.bgcolor dc.bgcolor;
3547 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3550 let save () =
3551 let bb = Buffer.create 32768 in
3552 let f (h, dc) =
3553 let dc = if conf.bedefault then conf else dc in
3554 Buffer.add_string bb "<llppconfig>\n<defaults ";
3555 add_attrs bb true dc dc;
3556 Buffer.add_string bb "/>\n";
3558 let adddoc path pan anchor c bookmarks =
3559 if bookmarks == [] && c = dc && anchor = emptyanchor
3560 then ()
3561 else (
3562 Printf.bprintf bb "<doc path='%s'"
3563 (enent path 0 (String.length path));
3565 if anchor <> emptyanchor
3566 then (
3567 let n, y = anchor in
3568 Printf.bprintf bb " page='%d'" n;
3569 if y > 1e-6
3570 then
3571 Printf.bprintf bb " rely='%f'" y
3575 if pan != 0
3576 then Printf.bprintf bb " pan='%d'" pan;
3578 add_attrs bb false dc c;
3580 begin match bookmarks with
3581 | [] -> Buffer.add_string bb "/>\n"
3582 | _ ->
3583 Buffer.add_string bb ">\n<bookmarks>\n";
3584 List.iter (fun (title, _level, (page, rely)) ->
3585 Printf.bprintf bb
3586 "<item title='%s' page='%d'"
3587 (enent title 0 (String.length title))
3588 page
3590 if rely > 1e-6
3591 then
3592 Printf.bprintf bb " rely='%f'" rely
3594 Buffer.add_string bb "/>\n";
3595 ) bookmarks;
3596 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3597 end;
3601 let pan =
3602 match state.mode with
3603 | Birdseye (_, pan, _, _, _) -> pan
3604 | _ -> state.x
3606 let basename = Filename.basename state.path in
3607 adddoc basename pan (getanchor ())
3608 { conf with
3609 autoscrollstep =
3610 if state.ascrollstep > 0
3611 then state.ascrollstep
3612 else conf.autoscrollstep }
3613 (if conf.savebmarks then state.bookmarks else []);
3615 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3616 if basename <> path
3617 then adddoc path x y c bookmarks
3618 ) h;
3619 Buffer.add_string bb "</llppconfig>";
3621 load1 f;
3622 if Buffer.length bb > 0
3623 then
3625 let tmp = path ^ ".tmp" in
3626 let oc = open_out_bin tmp in
3627 Buffer.output_buffer oc bb;
3628 close_out oc;
3629 Sys.rename tmp path;
3630 with exn ->
3631 prerr_endline
3632 ("error while saving configuration: " ^ Printexc.to_string exn)
3634 end;;
3636 let () =
3637 Arg.parse
3638 (Arg.align
3639 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3640 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3641 " Print version and exit")]
3643 (fun s -> state.path <- s)
3644 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3646 if String.length state.path = 0
3647 then (prerr_endline "file name missing"; exit 1);
3649 State.load ();
3651 let _ = Glut.init Sys.argv in
3652 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3653 let () = Glut.initWindowSize conf.winw conf.winh in
3654 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3656 let csock, ssock =
3657 if Sys.os_type = "Unix"
3658 then
3659 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3660 else
3661 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3662 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3663 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3664 Unix.bind sock addr;
3665 Unix.listen sock 1;
3666 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3667 Unix.connect csock addr;
3668 let ssock, _ = Unix.accept sock in
3669 Unix.close sock;
3670 let opts sock =
3671 Unix.setsockopt sock Unix.TCP_NODELAY true;
3672 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3674 opts ssock;
3675 opts csock;
3676 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3677 ssock, csock
3680 let () = Glut.displayFunc display in
3681 let () = Glut.reshapeFunc reshape in
3682 let () = Glut.keyboardFunc keyboard in
3683 let () = Glut.specialFunc special in
3684 let () = Glut.idleFunc (Some idle) in
3685 let () = Glut.mouseFunc mouse in
3686 let () = Glut.motionFunc motion in
3687 let () = Glut.passiveMotionFunc pmotion in
3689 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3690 state.csock <- csock;
3691 state.ssock <- ssock;
3692 state.text <- "Opening " ^ state.path;
3693 writeopen state.path state.password;
3695 at_exit State.save;
3697 let rec handlelablglutbug () =
3699 Glut.mainLoop ();
3700 with Glut.BadEnum "key in special_of_int" ->
3701 showtext '!' " LablGlut bug: special key not recognized";
3702 handlelablglutbug ()
3704 handlelablglutbug ();