Cosmetics
[llpp.git] / main.ml
blob308e070bb3772914c484ec73e2d735fbb2827f27
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 scrollw : 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
163 type outline = string * int * int * float;;
164 type outlines =
165 | Oarray of outline array
166 | Olist of outline list
167 | Onarrow of string * outline array * outline array
170 type rect = float * float * float * float * float * float * float * float;;
172 type pagemapkey = pageno * width * angle * proportional * gen;;
174 type anchor = pageno * top;;
176 let emptyanchor = (0, 0.0);;
177 let initialanchor = (-1, nan);;
179 type mode =
180 | Birdseye of (conf * leftx * pageno * pageno * anchor)
181 | Outline of (bool * int * int * outline array * string * int * mode)
182 | Items of (int * int * item array * string * int * mode)
183 | Textentry of (textentry * onleave)
184 | View
185 and onleave = leavetextentrystatus -> unit
186 and leavetextentrystatus = | Cancel | Confirm
187 and item = string * int * action
188 and action =
189 | Noaction
190 | Action of (int -> int -> string -> int -> mode)
193 let isbirdseye = function Birdseye _ -> true | _ -> false;;
194 let istextentry = function Textentry _ -> true | _ -> false;;
196 type state =
197 { mutable csock : Unix.file_descr
198 ; mutable ssock : Unix.file_descr
199 ; mutable w : int
200 ; mutable x : int
201 ; mutable y : int
202 ; mutable anchor : anchor
203 ; mutable maxy : int
204 ; mutable layout : layout list
205 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
206 ; mutable pdims : (pageno * width * height * leftx) list
207 ; mutable pagecount : int
208 ; pagecache : string circbuf
209 ; mutable rendering : bool
210 ; mutable mstate : mstate
211 ; mutable searchpattern : string
212 ; mutable rects : (pageno * recttype * rect) list
213 ; mutable rects1 : (pageno * recttype * rect) list
214 ; mutable text : string
215 ; mutable fullscreen : (width * height) option
216 ; mutable mode : mode
217 ; mutable outlines : outlines
218 ; mutable bookmarks : outline list
219 ; mutable path : string
220 ; mutable password : string
221 ; mutable invalidated : int
222 ; mutable colorscale : float
223 ; mutable memused : int
224 ; mutable gen : gen
225 ; mutable throttle : layout list option
226 ; mutable ascrollstep : int
227 ; mutable help : item array
228 ; mutable docinfo : (int * string) list
229 ; hists : hists
231 and hists =
232 { pat : string circbuf
233 ; pag : string circbuf
234 ; nav : anchor circbuf
238 let defconf =
239 { scrollw = 7
240 ; scrollh = 12
241 ; icase = true
242 ; preload = true
243 ; pagebias = 0
244 ; verbose = false
245 ; scrollstep = 24
246 ; maxhfit = true
247 ; crophack = false
248 ; autoscrollstep = 24
249 ; showall = false
250 ; hlinks = false
251 ; underinfo = false
252 ; interpagespace = 2
253 ; zoom = 1.0
254 ; presentation = false
255 ; angle = 0
256 ; winw = 900
257 ; winh = 900
258 ; savebmarks = true
259 ; proportional = true
260 ; memlimit = 32*1024*1024
261 ; texcount = 256
262 ; sliceheight = 24
263 ; thumbw = 76
264 ; jumpback = false
265 ; bgcolor = (0.5, 0.5, 0.5)
266 ; bedefault = false
270 let conf = { defconf with angle = defconf.angle };;
272 let makehelp () =
273 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
274 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
277 let state =
278 { csock = Unix.stdin
279 ; ssock = Unix.stdin
280 ; x = 0
281 ; y = 0
282 ; anchor = initialanchor
283 ; w = 0
284 ; layout = []
285 ; maxy = max_int
286 ; pagemap = Hashtbl.create 10
287 ; pagecache = cbnew 100 ""
288 ; pdims = []
289 ; pagecount = 0
290 ; rendering = false
291 ; mstate = Mnone
292 ; rects = []
293 ; rects1 = []
294 ; text = ""
295 ; mode = View
296 ; fullscreen = None
297 ; searchpattern = ""
298 ; outlines = Olist []
299 ; bookmarks = []
300 ; path = ""
301 ; password = ""
302 ; invalidated = 0
303 ; hists =
304 { nav = cbnew 100 (0, 0.0)
305 ; pat = cbnew 20 ""
306 ; pag = cbnew 10 ""
308 ; colorscale = 1.0
309 ; memused = 0
310 ; gen = 0
311 ; throttle = None
312 ; ascrollstep = 0
313 ; help = makehelp ()
314 ; docinfo = []
318 let vlog fmt =
319 if conf.verbose
320 then
321 Printf.kprintf prerr_endline fmt
322 else
323 Printf.kprintf ignore fmt
326 let writecmd fd s =
327 let len = String.length s in
328 let n = 4 + len in
329 let b = Buffer.create n in
330 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
331 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
332 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
333 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
334 Buffer.add_string b s;
335 let s' = Buffer.contents b in
336 let n' = Unix.write fd s' 0 n in
337 if n' != n then failwith "write failed";
340 let readcmd fd =
341 let s = "xxxx" in
342 let n = Unix.read fd s 0 4 in
343 if n != 4 then failwith "incomplete read(len)";
344 let len = 0
345 lor (Char.code s.[0] lsl 24)
346 lor (Char.code s.[1] lsl 16)
347 lor (Char.code s.[2] lsl 8)
348 lor (Char.code s.[3] lsl 0)
350 let s = String.create len in
351 let n = Unix.read fd s 0 len in
352 if n != len then failwith "incomplete read(data)";
356 let makecmd s l =
357 let b = Buffer.create 10 in
358 Buffer.add_string b s;
359 let rec combine = function
360 | [] -> b
361 | x :: xs ->
362 Buffer.add_char b ' ';
363 let s =
364 match x with
365 | `b b -> if b then "1" else "0"
366 | `s s -> s
367 | `i i -> string_of_int i
368 | `f f -> string_of_float f
369 | `I f -> string_of_int (truncate f)
371 Buffer.add_string b s;
372 combine xs;
374 combine l;
377 let wcmd s l =
378 let cmd = Buffer.contents (makecmd s l) in
379 writecmd state.csock cmd;
382 let calcips h =
383 if conf.presentation
384 then
385 let d = conf.winh - h in
386 max 0 ((d + 1) / 2)
387 else
388 conf.interpagespace
391 let calcheight () =
392 let rec f pn ph pi fh l =
393 match l with
394 | (n, _, h, _) :: rest ->
395 let ips = calcips h in
396 let fh =
397 if conf.presentation
398 then fh+ips
399 else (
400 if isbirdseye state.mode && pn = 0
401 then fh + ips
402 else fh
405 let fh = fh + ((n - pn) * (ph + pi)) in
406 f n h ips fh rest;
408 | [] ->
409 let inc =
410 if conf.presentation || (isbirdseye state.mode && pn = 0)
411 then 0
412 else -pi
414 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
415 max 0 fh
417 let fh = f 0 0 0 0 state.pdims in
421 let getpageyh pageno =
422 let rec f pn ph pi y l =
423 match l with
424 | (n, _, h, _) :: rest ->
425 let ips = calcips h in
426 if n >= pageno
427 then
428 let h = if n = pageno then h else ph in
429 if conf.presentation && n = pageno
430 then
431 y + (pageno - pn) * (ph + pi) + pi, h
432 else
433 y + (pageno - pn) * (ph + pi), h
434 else
435 let y = y + (if conf.presentation then pi else 0) in
436 let y = y + (n - pn) * (ph + pi) in
437 f n h ips y rest
439 | [] ->
440 y + (pageno - pn) * (ph + pi), ph
442 f 0 0 0 0 state.pdims
445 let getpagey pageno = fst (getpageyh pageno);;
447 let layout y sh =
448 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
449 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
450 match pdims with
451 | (pageno', w, h, x) :: rest when pageno' = pageno ->
452 let ips = calcips h in
453 let yinc =
454 if conf.presentation || (isbirdseye state.mode && pageno = 0)
455 then ips
456 else 0
458 (w, h, ips, x), rest, pdimno + 1, yinc
459 | _ ->
460 prev, pdims, pdimno, 0
462 let dy = dy + yinc in
463 let py = py + yinc in
464 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
465 then
466 accu
467 else
468 let vy = y + dy in
469 if py + h <= vy - yinc
470 then
471 let py = py + h + ips in
472 let dy = max 0 (py - y) in
473 f ~pageno:(pageno+1)
474 ~pdimno
475 ~prev:curr
478 ~pdims:rest
479 ~cacheleft
480 ~accu
481 else
482 let pagey = vy - py in
483 let pagevh = h - pagey in
484 let pagevh = min (sh - dy) pagevh in
485 let off = if yinc > 0 then py - vy else 0 in
486 let py = py + h + ips in
487 let e =
488 { pageno = pageno
489 ; pagedimno = pdimno
490 ; pagew = w
491 ; pageh = h
492 ; pagedispy = dy + off
493 ; pagey = pagey + off
494 ; pagevh = pagevh - off
495 ; pagex = x
498 let accu = e :: accu in
499 f ~pageno:(pageno+1)
500 ~pdimno
501 ~prev:curr
503 ~dy:(dy+pagevh+ips)
504 ~pdims:rest
505 ~cacheleft:(cacheleft-1)
506 ~accu
508 if state.invalidated = 0
509 then (
510 let accu =
512 ~pageno:0
513 ~pdimno:~-1
514 ~prev:(0,0,0,0)
515 ~py:0
516 ~dy:0
517 ~pdims:state.pdims
518 ~cacheleft:(cbcap state.pagecache)
519 ~accu:[]
521 List.rev accu
523 else
527 let clamp incr =
528 let y = state.y + incr in
529 let y = max 0 y in
530 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
534 let getopaque pageno =
535 try Some (Hashtbl.find state.pagemap
536 (pageno, state.w, conf.angle, conf.proportional, state.gen))
537 with Not_found -> None
540 let cache pageno opaque =
541 Hashtbl.replace state.pagemap
542 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
545 let validopaque opaque = String.length opaque > 0;;
547 let render l =
548 match getopaque l.pageno with
549 | None when not state.rendering ->
550 state.rendering <- true;
551 cache l.pageno ("", -1);
552 wcmd "render" [`i (l.pageno + 1)
553 ;`i l.pagedimno
554 ;`i l.pagew
555 ;`i l.pageh];
556 | _ -> ()
559 let loadlayout layout =
560 let rec f all = function
561 | l :: ls ->
562 begin match getopaque l.pageno with
563 | None -> render l; f false ls
564 | Some (opaque, _) -> f (all && validopaque opaque) ls
566 | [] -> all
568 f (layout <> []) layout;
571 let findpageforopaque opaque =
572 Hashtbl.fold
573 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
574 state.pagemap None
577 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
579 let preload () =
580 let oktopreload =
581 if conf.preload
582 then
583 let memleft = conf.memlimit - state.memused in
584 if memleft < 0
585 then
586 let opaque = cbpeek state.pagecache in
587 match findpageforopaque opaque with
588 | Some ((n, _, _, _, _), size) ->
589 memleft + size >= 0 && not (pagevisible state.layout n)
590 | None -> false
591 else true
592 else false
594 if oktopreload
595 then
596 let presentation = conf.presentation in
597 let interpagespace = conf.interpagespace in
598 let maxy = state.maxy in
599 conf.presentation <- false;
600 conf.interpagespace <- 0;
601 state.maxy <- calcheight ();
602 let y =
603 match state.layout with
604 | [] -> 0
605 | l :: _ -> getpagey l.pageno + l.pagey
607 let y = if y < conf.winh then 0 else y - conf.winh in
608 let pages = layout y (conf.winh*3) in
609 List.iter render pages;
610 conf.presentation <- presentation;
611 conf.interpagespace <- interpagespace;
612 state.maxy <- maxy;
615 let gotoy y =
616 let y = max 0 y in
617 let y = min state.maxy y in
618 let pages = layout y conf.winh in
619 let ready = loadlayout pages in
620 if conf.showall
621 then (
622 if ready
623 then (
624 state.y <- y;
625 state.layout <- pages;
626 state.throttle <- None;
627 Glut.postRedisplay ();
629 else (
630 state.throttle <- Some pages;
633 else (
634 state.y <- y;
635 state.layout <- pages;
636 state.throttle <- None;
637 Glut.postRedisplay ();
639 begin match state.mode with
640 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
641 if not (pagevisible pages pageno)
642 then (
643 match state.layout with
644 | [] -> ()
645 | l :: _ ->
646 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
648 | _ -> ()
649 end;
650 preload ();
653 let gotoy_and_clear_text y =
654 gotoy y;
655 if not conf.verbose then state.text <- "";
658 let getanchor () =
659 match state.layout with
660 | [] -> emptyanchor
661 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
664 let getanchory (n, top) =
665 let y, h = getpageyh n in
666 y + (truncate (top *. float h));
669 let gotoanchor anchor =
670 gotoy (getanchory anchor);
673 let addnav () =
674 cbput state.hists.nav (getanchor ());
677 let getnav dir =
678 let anchor = cbgetc state.hists.nav dir in
679 getanchory anchor;
682 let gotopage n top =
683 let y, h = getpageyh n in
684 gotoy_and_clear_text (y + (truncate (top *. float h)));
687 let gotopage1 n top =
688 let y = getpagey n in
689 gotoy_and_clear_text (y + top);
692 let invalidate () =
693 state.layout <- [];
694 state.pdims <- [];
695 state.rects <- [];
696 state.rects1 <- [];
697 state.invalidated <- state.invalidated + 1;
700 let scalecolor c =
701 let c = c *. state.colorscale in
702 (c, c, c);
705 let scalecolor2 (r, g, b) =
706 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
709 let represent () =
710 state.maxy <- calcheight ();
711 match state.mode with
712 | Birdseye (_, _, pageno, _, _) ->
713 let y, h = getpageyh pageno in
714 let top = (conf.winh - h) / 2 in
715 gotoy (max 0 (y - top))
716 | _ -> gotoanchor state.anchor
719 let pagematrix () =
720 GlMat.mode `projection;
721 GlMat.load_identity ();
722 GlMat.rotate ~x:1.0 ~angle:180.0 ();
723 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
724 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
725 if state.x != 0
726 then (
727 GlMat.translate ~x:(float state.x) ();
731 let winmatrix () =
732 GlMat.mode `projection;
733 GlMat.load_identity ();
734 GlMat.rotate ~x:1.0 ~angle:180.0 ();
735 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
736 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
739 let reshape ~w ~h =
740 if state.invalidated = 0 && state.anchor == initialanchor
741 then state.anchor <- getanchor ();
743 conf.winw <- w;
744 let w = truncate (float w *. conf.zoom) - conf.scrollw in
745 let w = max w 2 in
746 state.w <- w;
747 conf.winh <- h;
748 GlMat.mode `modelview;
749 GlMat.load_identity ();
750 GlClear.color (scalecolor 1.0);
751 GlClear.clear [`color];
753 invalidate ();
754 wcmd "geometry" [`i w; `i h];
757 let showtext s =
758 GlDraw.color (0.0, 0.0, 0.0);
759 GlDraw.rect
760 (0.0, float (conf.winh - 18))
761 (float (conf.winw - conf.scrollw - 1), float conf.winh)
763 let font = Glut.BITMAP_8_BY_13 in
764 GlDraw.color (1.0, 1.0, 1.0);
765 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
766 Glut.bitmapCharacter ~font ~c:(Char.code ' ');
767 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
770 let enttext () =
771 let len = String.length state.text in
772 match state.mode with
773 | Textentry ((prefix, text, _, _, _), _) ->
774 let s =
775 match String.length prefix with
776 | 0 | 1 ->
777 if len > 0
778 then
779 Printf.sprintf "%s%s^ [%s]" prefix text state.text
780 else
781 Printf.sprintf "%s%s^" prefix text
783 | _ ->
784 if len > 0
785 then
786 Printf.sprintf "%s: %s^ [%s]" prefix text state.text
787 else
788 Printf.sprintf "%s: %s^" prefix text
790 showtext s;
792 | _ ->
793 if len > 0 then showtext state.text
796 let showtext c s =
797 state.text <- Printf.sprintf "%c%s" c s;
798 Glut.postRedisplay ();
801 let act cmd =
802 match cmd.[0] with
803 | 'c' ->
804 state.pdims <- [];
806 | 'D' ->
807 state.rects <- state.rects1;
808 Glut.postRedisplay ()
810 | 'C' ->
811 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
812 state.pagecount <- n;
813 state.invalidated <- state.invalidated - 1;
814 if state.invalidated = 0
815 then represent ()
817 | 't' ->
818 let s = Scanf.sscanf cmd "t %n"
819 (fun n -> String.sub cmd n (String.length cmd - n))
821 Glut.setWindowTitle s
823 | 'T' ->
824 let s = Scanf.sscanf cmd "T %n"
825 (fun n -> String.sub cmd n (String.length cmd - n))
827 if istextentry state.mode
828 then (
829 state.text <- s;
830 showtext ' ' s;
832 else (
833 state.text <- s;
834 Glut.postRedisplay ();
837 | 'V' ->
838 if conf.verbose
839 then
840 let s = Scanf.sscanf cmd "V %n"
841 (fun n -> String.sub cmd n (String.length cmd - n))
843 state.text <- s;
844 showtext ' ' s;
846 | 'F' ->
847 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
848 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
849 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
850 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
852 let y = (getpagey pageno) + truncate y0 in
853 addnav ();
854 gotoy y;
855 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
857 | 'R' ->
858 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
859 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
860 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
861 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
863 state.rects1 <-
864 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
866 | 'r' ->
867 let n, w, _h, r, l, s, p =
868 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
869 (fun n w h r l s p ->
870 (n-1, w, h, r, l != 0, s, p))
873 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
874 state.memused <- state.memused + s;
876 let layout =
877 match state.throttle with
878 | None -> state.layout
879 | Some layout -> layout
882 let rec gc () =
883 if (state.memused <= conf.memlimit) || cbempty state.pagecache
884 then ()
885 else (
886 let evictedopaque = cbpeek state.pagecache in
887 match findpageforopaque evictedopaque with
888 | None -> failwith "bug in gc"
889 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
890 if state.gen != gen || not (pagevisible layout evictedn)
891 then (
892 wcmd "free" [`s evictedopaque];
893 state.memused <- state.memused - evictedsize;
894 Hashtbl.remove state.pagemap k;
895 cbdecr state.pagecache;
896 gc ();
900 gc ();
902 cbput state.pagecache p;
903 state.rendering <- false;
905 begin match state.throttle with
906 | None ->
907 if pagevisible state.layout n
908 then gotoy state.y
909 else (
910 let allvisible = loadlayout state.layout in
911 if allvisible then preload ();
914 | Some layout ->
915 match layout with
916 | [] -> ()
917 | l :: _ ->
918 let y = getpagey l.pageno + l.pagey in
919 gotoy y
922 | 'l' ->
923 let pdim =
924 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
926 state.pdims <- pdim :: state.pdims
928 | 'o' ->
929 let (l, n, t, h, pos) =
930 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
932 let s = String.sub cmd pos (String.length cmd - pos) in
933 let s =
934 let l = String.length s in
935 let b = Buffer.create (String.length s) in
936 let rec loop pc2 i =
937 if i = l
938 then ()
939 else
940 let pc2 =
941 match s.[i] with
942 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
943 | '\xc2' -> true
944 | c ->
945 let c = if Char.code c land 0x80 = 0 then c else '?' in
946 Buffer.add_char b c;
947 false
949 loop pc2 (i+1)
951 loop false 0;
952 Buffer.contents b
954 let outline = (s, l, n, float t /. float h) in
955 let outlines =
956 match state.outlines with
957 | Olist outlines -> Olist (outline :: outlines)
958 | Oarray _ -> Olist [outline]
959 | Onarrow _ -> Olist [outline]
961 state.outlines <- outlines
964 | 'i' ->
965 let s = Scanf.sscanf cmd "i %n"
966 (fun n -> String.sub cmd n (String.length cmd - n))
968 let len = String.length s in
969 let rec fold accu pos =
970 let eolpos =
971 try String.index_from s pos '\n' with Not_found -> len
973 if eolpos = len
974 then List.rev accu
975 else
976 let line = String.sub s pos (eolpos - pos) in
977 fold ((1, line)::accu) (eolpos+1)
979 state.docinfo <- fold state.docinfo 0
981 | _ ->
982 dolog "unknown cmd `%S'" cmd
985 let now = Unix.gettimeofday;;
987 let idle () =
988 let rec loop delay =
989 let r, _, _ = Unix.select [state.csock] [] [] delay in
990 begin match r with
991 | [] ->
992 if state.ascrollstep > 0
993 then begin
994 let y = state.y + state.ascrollstep in
995 let y = if y >= state.maxy then 0 else y in
996 gotoy y;
997 state.text <- "";
998 end;
1000 | _ ->
1001 let cmd = readcmd state.csock in
1002 act cmd;
1003 loop 0.0
1004 end;
1005 in loop 0.001
1008 let onhist cb =
1009 let rc = cb.rc in
1010 let action = function
1011 | HCprev -> cbget cb ~-1
1012 | HCnext -> cbget cb 1
1013 | HCfirst -> cbget cb ~-(cb.rc)
1014 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1015 and cancel () = cb.rc <- rc
1016 in (action, cancel)
1019 let search pattern forward =
1020 if String.length pattern > 0
1021 then
1022 let pn, py =
1023 match state.layout with
1024 | [] -> 0, 0
1025 | l :: _ ->
1026 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1028 let cmd =
1029 let b = makecmd "search"
1030 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1032 Buffer.add_char b ',';
1033 Buffer.add_string b pattern;
1034 Buffer.add_char b '\000';
1035 Buffer.contents b;
1037 writecmd state.csock cmd;
1040 let intentry text key =
1041 let c = Char.unsafe_chr key in
1042 match c with
1043 | '0' .. '9' ->
1044 let s = "x" in s.[0] <- c;
1045 let text = text ^ s in
1046 TEcont text
1048 | _ ->
1049 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1050 TEcont text
1053 let addchar s c =
1054 let b = Buffer.create (String.length s + 1) in
1055 Buffer.add_string b s;
1056 Buffer.add_char b c;
1057 Buffer.contents b;
1060 let textentry text key =
1061 let c = Char.unsafe_chr key in
1062 match c with
1063 | _ when key >= 32 && key < 127 ->
1064 let text = addchar text c in
1065 TEcont text
1067 | _ ->
1068 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1069 TEcont text
1072 let reinit angle proportional =
1073 conf.angle <- angle;
1074 conf.proportional <- proportional;
1075 invalidate ();
1076 wcmd "reinit" [`i angle; `b proportional];
1079 let setzoom zoom =
1080 let zoom = max 0.01 (min 2.2 zoom) in
1081 if zoom <> conf.zoom
1082 then (
1083 if zoom <= 1.0
1084 then state.x <- 0;
1085 conf.zoom <- zoom;
1086 state.anchor <- getanchor ();
1087 reshape conf.winw conf.winh;
1088 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1092 let enterbirdseye () =
1093 let zoom = float conf.thumbw /. float conf.winw in
1094 let birdseyepageno =
1095 let rec fold candidate = function
1096 | [] -> candidate
1097 | l :: _ when l.pagey = 0 -> l.pageno
1098 | l :: rest -> fold l.pageno rest
1100 fold 0 state.layout
1102 state.mode <- Birdseye (
1103 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1105 conf.zoom <- zoom;
1106 conf.presentation <- false;
1107 conf.interpagespace <- 10;
1108 conf.hlinks <- false;
1109 state.x <- 0;
1110 state.mstate <- Mnone;
1111 conf.showall <- false;
1112 Glut.setCursor Glut.CURSOR_INHERIT;
1113 if conf.verbose
1114 then
1115 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1116 (100.0*.zoom)
1117 else
1118 state.text <- ""
1120 reshape conf.winw conf.winh;
1123 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1124 state.mode <- View;
1125 conf.zoom <- c.zoom;
1126 conf.presentation <- c.presentation;
1127 conf.interpagespace <- c.interpagespace;
1128 conf.showall <- c.showall;
1129 conf.hlinks <- c.hlinks;
1130 state.x <- leftx;
1131 if conf.verbose
1132 then
1133 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1134 (100.0*.conf.zoom)
1136 reshape conf.winw conf.winh;
1137 state.anchor <- if goback then anchor else (pageno, 0.0);
1140 let togglebirdseye () =
1141 match state.mode with
1142 | Birdseye vals -> leavebirdseye vals true
1143 | View | Outline _ -> enterbirdseye ()
1144 | _ -> ()
1147 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1148 let pageno = max 0 (pageno - 1) in
1149 let rec loop = function
1150 | [] -> gotopage1 pageno 0
1151 | l :: _ when l.pageno = pageno ->
1152 if l.pagedispy >= 0 && l.pagey = 0
1153 then Glut.postRedisplay ()
1154 else gotopage1 pageno 0
1155 | _ :: rest -> loop rest
1157 loop state.layout;
1158 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1161 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1162 let pageno = min (state.pagecount - 1) (pageno + 1) in
1163 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1164 let rec loop = function
1165 | [] ->
1166 let y, h = getpageyh pageno in
1167 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1168 gotoy (clamp dy)
1169 | l :: _ when l.pageno = pageno ->
1170 if l.pagevh != l.pageh
1171 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1172 else Glut.postRedisplay ()
1173 | _ :: rest -> loop rest
1175 loop state.layout
1178 let optentry mode _ key =
1179 let btos b = if b then "on" else "off" in
1180 let c = Char.unsafe_chr key in
1181 match c with
1182 | 's' ->
1183 let ondone s =
1184 try conf.scrollstep <- int_of_string s with exc ->
1185 state.text <- Printf.sprintf "bad integer `%s': %s"
1186 s (Printexc.to_string exc)
1188 TEswitch ("scroll step", "", None, intentry, ondone)
1190 | 'A' ->
1191 let ondone s =
1193 conf.autoscrollstep <- int_of_string s;
1194 if state.ascrollstep > 0
1195 then state.ascrollstep <- conf.autoscrollstep;
1196 with exc ->
1197 state.text <- Printf.sprintf "bad integer `%s': %s"
1198 s (Printexc.to_string exc)
1200 TEswitch ("auto scroll step", "", None, intentry, ondone)
1202 | 'Z' ->
1203 let ondone s =
1205 let zoom = float (int_of_string s) /. 100.0 in
1206 setzoom zoom
1207 with exc ->
1208 state.text <- Printf.sprintf "bad integer `%s': %s"
1209 s (Printexc.to_string exc)
1211 TEswitch ("zoom", "", None, intentry, ondone)
1213 | 't' ->
1214 let ondone s =
1216 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1217 state.text <-
1218 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1219 begin match mode with
1220 | Birdseye beye ->
1221 leavebirdseye beye false;
1222 enterbirdseye ();
1223 | _ -> ();
1225 with exc ->
1226 state.text <- Printf.sprintf "bad integer `%s': %s"
1227 s (Printexc.to_string exc)
1229 TEswitch ("thumbnail width", "", None, intentry, ondone)
1231 | 'R' ->
1232 let ondone s =
1233 match try
1234 Some (int_of_string s)
1235 with exc ->
1236 state.text <- Printf.sprintf "bad integer `%s': %s"
1237 s (Printexc.to_string exc);
1238 None
1239 with
1240 | Some angle -> reinit angle conf.proportional
1241 | None -> ()
1243 TEswitch ("rotation", "", None, intentry, ondone)
1245 | 'i' ->
1246 conf.icase <- not conf.icase;
1247 TEdone ("case insensitive search " ^ (btos conf.icase))
1249 | 'p' ->
1250 conf.preload <- not conf.preload;
1251 gotoy state.y;
1252 TEdone ("preload " ^ (btos conf.preload))
1254 | 'v' ->
1255 conf.verbose <- not conf.verbose;
1256 TEdone ("verbose " ^ (btos conf.verbose))
1258 | 'h' ->
1259 conf.maxhfit <- not conf.maxhfit;
1260 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1261 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1263 | 'c' ->
1264 conf.crophack <- not conf.crophack;
1265 TEdone ("crophack " ^ btos conf.crophack)
1267 | 'a' ->
1268 conf.showall <- not conf.showall;
1269 TEdone ("throttle " ^ btos conf.showall)
1271 | 'f' ->
1272 conf.underinfo <- not conf.underinfo;
1273 TEdone ("underinfo " ^ btos conf.underinfo)
1275 | 'P' ->
1276 conf.savebmarks <- not conf.savebmarks;
1277 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1279 | 'S' ->
1280 let ondone s =
1282 let pageno, py =
1283 match state.layout with
1284 | [] -> 0, 0
1285 | l :: _ ->
1286 l.pageno, l.pagey
1288 conf.interpagespace <- int_of_string s;
1289 state.maxy <- calcheight ();
1290 let y = getpagey pageno in
1291 gotoy (y + py)
1292 with exc ->
1293 state.text <- Printf.sprintf "bad integer `%s': %s"
1294 s (Printexc.to_string exc)
1296 TEswitch ("vertical margin", "", None, intentry, ondone)
1298 | 'l' ->
1299 reinit conf.angle (not conf.proportional);
1300 TEdone ("proprortional display " ^ btos conf.proportional)
1302 | _ ->
1303 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1304 TEstop
1307 let maxoutlinerows () = (conf.winh - 31) / 16;;
1309 let enterselector allowdel outlines errmsg msg =
1310 if Array.length outlines = 0
1311 then (
1312 showtext ' ' errmsg;
1314 else (
1315 state.text <- msg;
1316 Glut.setCursor Glut.CURSOR_INHERIT;
1317 let pageno =
1318 match state.layout with
1319 | [] -> -1
1320 | {pageno=pageno} :: _ -> pageno
1322 let active =
1323 let rec loop n =
1324 if n = Array.length outlines
1325 then 0
1326 else
1327 let (_, _, outlinepageno, _) = outlines.(n) in
1328 if outlinepageno >= pageno then n else loop (n+1)
1330 loop 0
1332 state.mode <- Outline
1333 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1334 state.mode);
1335 Glut.postRedisplay ();
1339 let enteroutlinemode () =
1340 let outlines, msg =
1341 match state.outlines with
1342 | Oarray a -> a, ""
1343 | Olist l ->
1344 let a = Array.of_list (List.rev l) in
1345 state.outlines <- Oarray a;
1346 a, ""
1347 | Onarrow (pat, a, _) ->
1348 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1350 enterselector false outlines "Document has no outline" msg;
1353 let enterbookmarkmode () =
1354 let bookmarks = Array.of_list state.bookmarks in
1355 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1358 let mode_to_string mode =
1359 let b = Buffer.create 10 in
1360 let rec f = function
1361 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1362 | View -> Buffer.add_string b "View"
1363 | Birdseye _ -> Buffer.add_string b "Birdseye"
1364 | Items _ -> Buffer.add_string b "Items"
1365 | Outline _ -> Buffer.add_string b "Outline"
1367 f mode;
1368 Buffer.contents b;
1371 let enterinfomode () =
1372 let btos = function true -> "on" | _ -> "off" in
1373 let mode = state.mode in
1374 let rec makeitems () =
1375 let intp name get set =
1376 Printf.sprintf "%-24s %d" name (get ()), 1, Action (
1377 fun active first _qsearch pan ->
1378 let ondone s =
1379 let n =
1380 try int_of_string s
1381 with exn ->
1382 state.text <- Printf.sprintf "bad integer `%s': %s"
1383 s (Printexc.to_string exn);
1384 max_int;
1386 if n != max_int then set n;
1388 let te = name, "", None, intentry, ondone in
1389 state.text <- "";
1390 Textentry (
1392 fun _ ->
1393 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1396 and boolp name get set =
1397 Printf.sprintf "%-24s %s" name (btos (get ())), 1, Action (
1398 fun active first qsearch pan ->
1399 let v = get () in
1400 set (not v);
1401 Items (active, first, makeitems (), qsearch, pan, mode);
1405 let birdseye = isbirdseye mode in
1406 let items = [
1407 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1409 boolp "presentation"
1410 (fun () -> conf.presentation)
1411 (fun v ->
1412 conf.presentation <- v;
1413 state.anchor <- getanchor ();
1414 represent ());
1416 boolp "ignore case in searches"
1417 (fun () -> conf.icase)
1418 (fun v -> conf.icase <- v);
1420 boolp "preload"
1421 (fun () -> conf.preload)
1422 (fun v -> conf.preload <- v);
1424 boolp "verbose"
1425 (fun () -> conf.verbose)
1426 (fun v -> conf.verbose <- v);
1428 boolp "max fit"
1429 (fun () -> conf.maxhfit)
1430 (fun v -> conf.maxhfit <- v);
1432 boolp "crop hack"
1433 (fun () -> conf.crophack)
1434 (fun v -> conf.crophack <- v);
1436 boolp "throttle"
1437 (fun () -> conf.showall)
1438 (fun v -> conf.showall <- v);
1440 boolp "highlight links"
1441 (fun () -> conf.hlinks)
1442 (fun v -> conf.hlinks <- v);
1444 boolp "under info"
1445 (fun () -> conf.underinfo)
1446 (fun v -> conf.underinfo <- v);
1447 boolp "persistent bookmarks"
1448 (fun () -> conf.savebmarks)
1449 (fun v -> conf.savebmarks <- v);
1451 boolp "proportional display"
1452 (fun () -> conf.proportional)
1453 (fun v -> reinit conf.angle v);
1455 boolp "persistent location"
1456 (fun () -> conf.jumpback)
1457 (fun v -> conf.jumpback <- v);
1459 "", 0, Noaction;
1461 intp "vertical margin"
1462 (fun () -> conf.interpagespace)
1463 (fun n ->
1464 conf.interpagespace <- n;
1465 let pageno, py =
1466 match state.layout with
1467 | [] -> 0, 0
1468 | l :: _ ->
1469 l.pageno, l.pagey
1471 state.maxy <- calcheight ();
1472 let y = getpagey pageno in
1473 gotoy (y + py)
1476 intp "page bias"
1477 (fun () -> conf.pagebias)
1478 (fun v -> conf.pagebias <- v);
1480 intp "scroll step"
1481 (fun () -> conf.scrollstep)
1482 (fun n -> conf.scrollstep <- n);
1484 intp "auto scroll step"
1485 (fun () ->
1486 if state.ascrollstep > 0
1487 then state.ascrollstep
1488 else conf.autoscrollstep)
1489 (fun n ->
1490 if state.ascrollstep > 0
1491 then state.ascrollstep <- n
1492 else conf.autoscrollstep <- n);
1494 intp "zoom"
1495 (fun () -> truncate (conf.zoom *. 100.))
1496 (fun v -> setzoom ((float v) /. 100.));
1498 intp "rotation"
1499 (fun () -> conf.angle)
1500 (fun v -> reinit v conf.proportional);
1502 intp "scroll bar width"
1503 (fun () -> conf.scrollw)
1504 (fun v ->
1505 conf.scrollw <- v;
1506 reshape conf.winw conf.winh;
1509 intp "scroll handle height"
1510 (fun () -> conf.scrollh)
1511 (fun v -> conf.scrollh <- v;);
1513 intp "thumbnail width"
1514 (fun () -> conf.thumbw)
1515 (fun v ->
1516 conf.thumbw <- min 1920 v;
1517 match mode with
1518 | Birdseye beye ->
1519 leavebirdseye beye false;
1520 enterbirdseye ()
1521 | _ -> ()
1524 "", 0, Noaction;
1525 "Pixmap Cache", 0, Noaction;
1527 intp "size (advisory)"
1528 (fun () -> conf.memlimit)
1529 (fun v -> conf.memlimit <- v);
1530 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1534 let tailer =
1535 let trailer =
1536 ("", 0, Noaction)
1537 :: ("Document", 0, Noaction)
1538 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1540 if birdseye
1541 then trailer
1542 else (
1543 ("", 0, Noaction)
1544 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1545 (btos conf.bedefault),
1547 Action (
1548 fun active first qsearch pan ->
1549 conf.bedefault <- not conf.bedefault;
1550 Items (active, first, makeitems (), qsearch, pan, mode);
1552 ) :: trailer
1555 Array.of_list (items @ tailer)
1557 state.text <- "";
1558 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1559 Glut.postRedisplay ();
1562 let enterhelpmode () =
1563 state.mode <- Items (0, 0, state.help, "", 0, state.mode);
1564 Glut.postRedisplay ();
1567 let quickbookmark ?title () =
1568 match state.layout with
1569 | [] -> ()
1570 | l :: _ ->
1571 let title =
1572 match title with
1573 | None ->
1574 let sec = Unix.gettimeofday () in
1575 let tm = Unix.localtime sec in
1576 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1577 (l.pageno+1)
1578 tm.Unix.tm_mday
1579 tm.Unix.tm_mon
1580 (tm.Unix.tm_year + 1900)
1581 tm.Unix.tm_hour
1582 tm.Unix.tm_min
1583 | Some title -> title
1585 state.bookmarks <-
1586 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1589 let doreshape w h =
1590 state.fullscreen <- None;
1591 Glut.reshapeWindow w h;
1594 let writeopen path password =
1595 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1596 writecmd state.csock "info";
1599 let opendoc path password =
1600 invalidate ();
1601 state.path <- path;
1602 state.password <- password;
1603 state.gen <- state.gen + 1;
1605 writeopen path password;
1606 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1607 wcmd "geometry" [`i state.w; `i conf.winh];
1610 let viewkeyboard ~key ~x ~y =
1611 ignore x;
1612 ignore y;
1613 let enttext te =
1614 let mode = state.mode in
1615 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1616 state.text <- "";
1617 enttext ();
1618 Glut.postRedisplay ()
1620 let c = Char.chr key in
1621 match c with
1622 | '\027' | 'q' -> (* escape *)
1623 exit 0
1625 | '\008' -> (* backspace *)
1626 let y = getnav ~-1 in
1627 gotoy_and_clear_text y
1629 | 'o' ->
1630 enteroutlinemode ()
1632 | 'u' ->
1633 state.rects <- [];
1634 state.text <- "";
1635 Glut.postRedisplay ()
1637 | '/' | '?' ->
1638 let ondone isforw s =
1639 cbput state.hists.pat s;
1640 state.searchpattern <- s;
1641 search s isforw
1643 let s = String.create 1 in
1644 s.[0] <- c;
1645 enttext (s, "", Some (onhist state.hists.pat),
1646 textentry, ondone (c ='/'))
1648 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1649 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1650 setzoom (min 2.2 (conf.zoom +. incr))
1652 | '+' ->
1653 let ondone s =
1654 let n =
1655 try int_of_string s with exc ->
1656 state.text <- Printf.sprintf "bad integer `%s': %s"
1657 s (Printexc.to_string exc);
1658 max_int
1660 if n != max_int
1661 then (
1662 conf.pagebias <- n;
1663 state.text <- "page bias is now " ^ string_of_int n;
1666 enttext ("page bias", "", None, intentry, ondone)
1668 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1669 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1670 setzoom (max 0.01 (conf.zoom -. decr))
1672 | '-' ->
1673 let ondone msg = state.text <- msg in
1674 enttext (
1675 "option [acfhilpstvAPRSZ]", "", None,
1676 optentry state.mode, ondone
1679 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1680 setzoom 1.0
1682 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1683 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1684 if zoom < 1.0
1685 then setzoom zoom
1687 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1688 togglebirdseye ()
1690 | '0' .. '9' ->
1691 let ondone s =
1692 let n =
1693 try int_of_string s with exc ->
1694 state.text <- Printf.sprintf "bad integer `%s': %s"
1695 s (Printexc.to_string exc);
1698 if n >= 0
1699 then (
1700 addnav ();
1701 cbput state.hists.pag (string_of_int n);
1702 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1705 let pageentry text key =
1706 match Char.unsafe_chr key with
1707 | 'g' -> TEdone text
1708 | _ -> intentry text key
1710 let text = "x" in text.[0] <- c;
1711 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1713 | 'b' ->
1714 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1715 reshape conf.winw conf.winh;
1717 | 'l' ->
1718 conf.hlinks <- not conf.hlinks;
1719 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1720 Glut.postRedisplay ()
1722 | 'a' ->
1723 if state.ascrollstep = 0
1724 then state.ascrollstep <- conf.autoscrollstep
1725 else (
1726 conf.autoscrollstep <- state.ascrollstep;
1727 state.ascrollstep <- 0;
1730 | 'P' ->
1731 conf.presentation <- not conf.presentation;
1732 showtext ' ' ("presentation mode " ^
1733 if conf.presentation then "on" else "off");
1734 state.anchor <- getanchor ();
1735 represent ()
1737 | 'f' ->
1738 begin match state.fullscreen with
1739 | None ->
1740 state.fullscreen <- Some (conf.winw, conf.winh);
1741 Glut.fullScreen ()
1742 | Some (w, h) ->
1743 state.fullscreen <- None;
1744 doreshape w h
1747 | 'g' ->
1748 gotoy_and_clear_text 0
1750 | 'n' ->
1751 search state.searchpattern true
1753 | 'p' | 'N' ->
1754 search state.searchpattern false
1756 | 't' ->
1757 begin match state.layout with
1758 | [] -> ()
1759 | l :: _ ->
1760 gotoy_and_clear_text (getpagey l.pageno)
1763 | ' ' ->
1764 begin match List.rev state.layout with
1765 | [] -> ()
1766 | l :: _ ->
1767 let pageno = min (l.pageno+1) (state.pagecount-1) in
1768 gotoy_and_clear_text (getpagey pageno)
1771 | '\127' -> (* delte *)
1772 begin match state.layout with
1773 | [] -> ()
1774 | l :: _ ->
1775 let pageno = max 0 (l.pageno-1) in
1776 gotoy_and_clear_text (getpagey pageno)
1779 | '=' ->
1780 let f (fn, _) l =
1781 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1783 let fn, ln = List.fold_left f (-1, -1) state.layout in
1784 let s =
1785 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1786 let percent =
1787 if maxy <= 0
1788 then 100.
1789 else (100. *. (float state.y /. float maxy)) in
1790 if fn = ln
1791 then
1792 Printf.sprintf "Page %d of %d %.2f%%"
1793 (fn+1) state.pagecount percent
1794 else
1795 Printf.sprintf
1796 "Pages %d-%d of %d %.2f%%"
1797 (fn+1) (ln+1) state.pagecount percent
1799 showtext ' ' s;
1801 | 'w' ->
1802 begin match state.layout with
1803 | [] -> ()
1804 | l :: _ ->
1805 doreshape (l.pagew + conf.scrollw) l.pageh;
1806 Glut.postRedisplay ();
1809 | '\'' ->
1810 enterbookmarkmode ()
1812 | 'h' ->
1813 enterhelpmode ()
1815 | 'i' ->
1816 enterinfomode ()
1818 | 'm' ->
1819 let ondone s =
1820 match state.layout with
1821 | l :: _ ->
1822 state.bookmarks <-
1823 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1824 :: state.bookmarks
1825 | _ -> ()
1827 enttext ("bookmark", "", None, textentry, ondone)
1829 | '~' ->
1830 quickbookmark ();
1831 showtext ' ' "Quick bookmark added";
1833 | 'z' ->
1834 begin match state.layout with
1835 | l :: _ ->
1836 let rect = getpdimrect l.pagedimno in
1837 let w, h =
1838 if conf.crophack
1839 then
1840 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1841 truncate (1.2 *. (rect.(3) -. rect.(0))))
1842 else
1843 (truncate (rect.(1) -. rect.(0)),
1844 truncate (rect.(3) -. rect.(0)))
1846 if w != 0 && h != 0
1847 then
1848 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1850 Glut.postRedisplay ();
1852 | [] -> ()
1855 | '<' | '>' ->
1856 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1858 | '[' | ']' ->
1859 state.colorscale <-
1860 max 0.0
1861 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1862 Glut.postRedisplay ()
1864 | 'k' ->
1865 begin match state.mode with
1866 | Birdseye beye -> upbirdseye beye
1867 | _ -> gotoy (clamp (-conf.scrollstep))
1870 | 'j' ->
1871 begin match state.mode with
1872 | Birdseye beye -> downbirdseye beye
1873 | _ -> gotoy (clamp conf.scrollstep)
1876 | 'r' -> opendoc state.path state.password
1878 | _ ->
1879 vlog "huh? %d %c" key (Char.chr key);
1882 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), onleave) =
1883 ignore x;
1884 ignore y;
1885 let enttext te =
1886 state.mode <- Textentry (te, onleave);
1887 state.text <- "";
1888 enttext ();
1889 Glut.postRedisplay ()
1891 match Char.unsafe_chr key with
1892 | '\008' -> (* backspace *)
1893 let len = String.length text in
1894 if len = 0
1895 then (
1896 onleave Cancel;
1897 Glut.postRedisplay ();
1899 else (
1900 let s = String.sub text 0 (len - 1) in
1901 enttext (c, s, opthist, onkey, ondone)
1904 | '\r' | '\n' ->
1905 ondone text;
1906 onleave Confirm;
1907 Glut.postRedisplay ()
1909 | '\027' -> (* escape *)
1910 begin match opthist with
1911 | None -> ()
1912 | Some (_, onhistcancel) -> onhistcancel ()
1913 end;
1914 onleave Cancel;
1915 Glut.postRedisplay ()
1917 | _ ->
1918 begin match onkey text key with
1919 | TEdone text ->
1920 onleave Confirm;
1921 ondone text;
1922 Glut.postRedisplay ()
1924 | TEcont text ->
1925 enttext (c, text, opthist, onkey, ondone);
1927 | TEstop ->
1928 onleave Cancel;
1929 Glut.postRedisplay ()
1931 | TEswitch te ->
1932 state.mode <- Textentry (te, onleave);
1933 Glut.postRedisplay ()
1934 end;
1937 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, _) as beye) =
1938 match key with
1939 | 27 -> (* escape *)
1940 leavebirdseye beye true
1942 | 12 -> (* ctrl-l *)
1943 let y, h = getpageyh pageno in
1944 let top = (conf.winh - h) / 2 in
1945 gotoy (max 0 (y - top))
1947 | 13 -> (* enter *)
1948 leavebirdseye beye false
1950 | _ ->
1951 viewkeyboard ~key ~x ~y
1954 let itemskeyboard ~key ~x ~y (active, first, items, qsearch, pan, oldmode) =
1955 ignore x;
1956 ignore y;
1957 let set active first qsearch =
1958 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
1960 let search active pattern incr =
1961 let dosearch re =
1962 let rec loop n =
1963 if n = Array.length items || n = -1
1964 then None
1965 else
1966 let (s, _, _) = items.(n) in
1968 (try ignore (Str.search_forward re s 0); true
1969 with Not_found -> false)
1970 then Some n
1971 else loop (n + incr)
1973 loop active
1976 let re = Str.regexp_case_fold pattern in
1977 dosearch re
1978 with Failure s ->
1979 state.text <- s;
1980 None
1982 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1983 match key with
1984 | 18 | 19 -> (* ctrl-r/ctlr-s *)
1985 let incr = if key = 18 then -1 else 1 in
1986 let active, first =
1987 match search (active + incr) qsearch incr with
1988 | None ->
1989 state.text <- qsearch ^ " [not found]";
1990 active, first
1991 | Some active ->
1992 state.text <- qsearch;
1993 active, firstof active
1995 set active first qsearch;
1996 Glut.postRedisplay ();
1998 | 8 -> (* backspace *)
1999 let len = String.length qsearch in
2000 if len = 0
2001 then ()
2002 else (
2003 if len = 1
2004 then (
2005 state.text <- "";
2006 set active first "";
2008 else
2009 let qsearch = String.sub qsearch 0 (len - 1) in
2010 let active, first =
2011 match search active qsearch ~-1 with
2012 | None ->
2013 state.text <- qsearch ^ " [not found]";
2014 active, first
2015 | Some active ->
2016 state.text <- qsearch;
2017 active, firstof active
2019 set active first qsearch
2021 Glut.postRedisplay ()
2023 | _ when key >= 32 && key < 127 ->
2024 let pattern = addchar qsearch (Char.chr key) in
2025 let active, first =
2026 match search active pattern 1 with
2027 | None ->
2028 state.text <- pattern ^ " [not found]";
2029 active, first
2030 | Some active ->
2031 state.text <- pattern;
2032 active, firstof active
2034 set active first pattern;
2035 Glut.postRedisplay ()
2037 | 27 -> (* escape *)
2038 state.text <- "";
2039 state.mode <- oldmode;
2040 Glut.postRedisplay ();
2042 | 13 -> (* enter *)
2043 if active < Array.length items
2044 then (
2045 match items.(active) with
2046 | _, _, Action f ->
2047 state.mode <- f active first qsearch pan
2049 | _, _, Noaction ->
2050 state.text <- "";
2051 state.mode <- oldmode
2053 Glut.postRedisplay ();
2055 | _ -> dolog "unknown key %d" key
2058 let outlinekeyboard ~key ~x ~y
2059 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2060 ignore x;
2061 ignore y;
2062 let narrow outlines pattern =
2063 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2064 match reopt with
2065 | None -> None
2066 | Some re ->
2067 let rec fold accu n =
2068 if n = -1
2069 then accu
2070 else
2071 let (s, _, _, _) as o = outlines.(n) in
2072 let accu =
2073 if (try ignore (Str.search_forward re s 0); true
2074 with Not_found -> false)
2075 then (o :: accu)
2076 else accu
2078 fold accu (n-1)
2080 let matched = fold [] (Array.length outlines - 1) in
2081 if matched = [] then None else Some (Array.of_list matched)
2083 let search active pattern incr =
2084 let dosearch re =
2085 let rec loop n =
2086 if n = Array.length outlines || n = -1
2087 then None
2088 else
2089 let (s, _, _, _) = outlines.(n) in
2091 (try ignore (Str.search_forward re s 0); true
2092 with Not_found -> false)
2093 then Some n
2094 else loop (n + incr)
2096 loop active
2099 let re = Str.regexp_case_fold pattern in
2100 dosearch re
2101 with Failure s ->
2102 state.text <- s;
2103 None
2105 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2106 match key with
2107 | 27 -> (* escape *)
2108 if String.length qsearch = 0
2109 then (
2110 state.text <- "";
2111 state.mode <- oldmode;
2112 Glut.postRedisplay ();
2114 else (
2115 state.text <- "";
2116 state.mode <- Outline (
2117 allowdel, active, first, outlines, "", pan, oldmode
2119 Glut.postRedisplay ();
2122 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2123 let incr = if key = 18 then -1 else 1 in
2124 let active, first =
2125 match search (active + incr) qsearch incr with
2126 | None ->
2127 state.text <- qsearch ^ " [not found]";
2128 active, first
2129 | Some active ->
2130 state.text <- qsearch;
2131 active, firstof active
2133 state.mode <- Outline (
2134 allowdel, active, first, outlines, qsearch, pan, oldmode
2136 Glut.postRedisplay ();
2138 | 8 -> (* backspace *)
2139 let len = String.length qsearch in
2140 if len = 0
2141 then ()
2142 else (
2143 if len = 1
2144 then (
2145 state.text <- "";
2146 state.mode <- Outline (
2147 allowdel, active, first, outlines, "", pan, oldmode
2150 else
2151 let qsearch = String.sub qsearch 0 (len - 1) in
2152 let active, first =
2153 match search active qsearch ~-1 with
2154 | None ->
2155 state.text <- qsearch ^ " [not found]";
2156 active, first
2157 | Some active ->
2158 state.text <- qsearch;
2159 active, firstof active
2161 state.mode <- Outline (
2162 allowdel, active, first, outlines, qsearch, pan, oldmode
2165 Glut.postRedisplay ()
2167 | 13 -> (* enter *)
2168 if active < Array.length outlines
2169 then (
2170 let (_, _, n, t) = outlines.(active) in
2171 addnav ();
2172 gotopage n t;
2174 state.text <- "";
2175 if allowdel then state.bookmarks <- Array.to_list outlines;
2176 state.mode <- oldmode;
2177 Glut.postRedisplay ();
2179 | _ when key >= 32 && key < 127 ->
2180 let pattern = addchar qsearch (Char.chr key) in
2181 let active, first =
2182 match search active pattern 1 with
2183 | None ->
2184 state.text <- pattern ^ " [not found]";
2185 active, first
2186 | Some active ->
2187 state.text <- pattern;
2188 active, firstof active
2190 state.mode <- Outline (
2191 allowdel, active, first, outlines, pattern, pan, oldmode
2193 Glut.postRedisplay ()
2195 | 14 when not allowdel -> (* ctrl-n *)
2196 if String.length qsearch > 0
2197 then (
2198 let optoutlines = narrow outlines qsearch in
2199 begin match optoutlines with
2200 | None -> state.text <- "can't narrow"
2201 | Some outlines ->
2202 state.mode <- Outline (
2203 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2205 match state.outlines with
2206 | Olist _ -> ()
2207 | Oarray a ->
2208 state.outlines <- Onarrow (qsearch, outlines, a)
2209 | Onarrow (_, _, b) ->
2210 state.outlines <- Onarrow (qsearch, outlines, b)
2211 end;
2213 Glut.postRedisplay ()
2215 | 21 when not allowdel -> (* ctrl-u *)
2216 let outline =
2217 match state.outlines with
2218 | Oarray a -> a
2219 | Olist l ->
2220 let a = Array.of_list (List.rev l) in
2221 state.outlines <- Oarray a;
2223 | Onarrow (_, _, b) ->
2224 state.outlines <- Oarray b;
2225 state.text <- "";
2228 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2229 Glut.postRedisplay ()
2231 | 12 -> (* ctrl-l *)
2232 state.mode <- Outline
2233 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2234 Glut.postRedisplay ()
2236 | 127 when allowdel -> (* delete *)
2237 let len = Array.length outlines - 1 in
2238 if len = 0
2239 then (
2240 state.mode <- View;
2241 state.bookmarks <- [];
2243 else (
2244 let bookmarks = Array.init len
2245 (fun i ->
2246 let i = if i >= active then i + 1 else i in
2247 outlines.(i)
2250 state.mode <-
2251 Outline (
2252 allowdel,
2253 min active (len-1),
2254 min first (len-1),
2255 bookmarks, qsearch,
2257 oldmode
2260 Glut.postRedisplay ()
2262 | _ -> dolog "unknown key %d" key
2265 let keyboard ~key ~x ~y =
2266 if key = 7
2267 then
2268 wcmd "interrupt" []
2269 else
2270 match state.mode with
2271 | Outline outline -> outlinekeyboard ~key ~x ~y outline
2272 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
2273 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
2274 | View -> viewkeyboard ~key ~x ~y
2275 | Items items -> itemskeyboard ~key ~x ~y items
2278 let birdseyespecial key _ _
2279 ((conf, leftx, _, hooverpageno, anchor) as beye) =
2280 match key with
2281 | Glut.KEY_UP -> upbirdseye beye
2282 | Glut.KEY_DOWN -> downbirdseye beye
2284 | Glut.KEY_PAGE_UP ->
2285 begin match state.layout with
2286 | l :: _ ->
2287 if l.pagey != 0
2288 then (
2289 state.mode <- Birdseye (
2290 conf, leftx, l.pageno, hooverpageno, anchor
2292 gotopage1 l.pageno 0;
2294 else (
2295 let layout = layout (state.y-conf.winh) conf.winh in
2296 match layout with
2297 | [] -> gotoy (clamp (-conf.winh))
2298 | l :: _ ->
2299 state.mode <- Birdseye (
2300 conf, leftx, l.pageno, hooverpageno, anchor
2302 gotopage1 l.pageno 0
2305 | [] -> gotoy (clamp (-conf.winh))
2306 end;
2308 | Glut.KEY_PAGE_DOWN ->
2309 begin match List.rev state.layout with
2310 | l :: _ ->
2311 let layout = layout (state.y + conf.winh) conf.winh in
2312 begin match layout with
2313 | [] ->
2314 let incr = l.pageh - l.pagevh in
2315 if incr = 0
2316 then (
2317 state.mode <-
2318 Birdseye (
2319 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2321 Glut.postRedisplay ();
2323 else gotoy (clamp (incr + conf.interpagespace*2));
2325 | l :: _ ->
2326 state.mode <-
2327 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2328 gotopage1 l.pageno 0;
2331 | [] -> gotoy (clamp conf.winh)
2332 end;
2334 | Glut.KEY_HOME ->
2335 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2336 gotopage1 0 0
2338 | Glut.KEY_END ->
2339 let pageno = state.pagecount - 1 in
2340 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2341 if not (pagevisible state.layout pageno)
2342 then
2343 let h =
2344 match List.rev state.pdims with
2345 | [] -> conf.winh
2346 | (_, _, h, _) :: _ -> h
2348 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2349 else Glut.postRedisplay ();
2350 | _ -> ()
2353 let setautoscrollspeed goingdown =
2354 let incr = max 1 (state.ascrollstep / 2) in
2355 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2356 state.ascrollstep <- astep;
2359 let special ~key ~x ~y =
2360 match state.mode with
2361 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2362 togglebirdseye ()
2364 | Birdseye vals ->
2365 birdseyespecial key x y vals
2367 | View when key = Glut.KEY_F1 ->
2368 enterhelpmode ()
2370 | View ->
2371 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2372 then setautoscrollspeed (key = Glut.KEY_DOWN)
2373 else
2374 let y =
2375 match key with
2376 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2377 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2378 | Glut.KEY_DOWN -> clamp conf.scrollstep
2379 | Glut.KEY_PAGE_UP ->
2380 if Glut.getModifiers () land Glut.active_ctrl != 0
2381 then
2382 match state.layout with
2383 | [] -> state.y
2384 | l :: _ -> state.y - l.pagey
2385 else
2386 clamp (-conf.winh)
2387 | Glut.KEY_PAGE_DOWN ->
2388 if Glut.getModifiers () land Glut.active_ctrl != 0
2389 then
2390 match List.rev state.layout with
2391 | [] -> state.y
2392 | l :: _ -> getpagey l.pageno
2393 else
2394 clamp conf.winh
2395 | Glut.KEY_HOME -> addnav (); 0
2396 | Glut.KEY_END ->
2397 addnav ();
2398 state.maxy - (if conf.maxhfit then conf.winh else 0)
2400 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2401 Glut.getModifiers () land Glut.active_alt != 0 ->
2402 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2404 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2405 state.x <- state.x - 10;
2406 state.y
2407 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2408 state.x <- state.x + 10;
2409 state.y
2411 | _ -> state.y
2413 gotoy_and_clear_text y
2415 | Textentry
2416 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2417 let s =
2418 match key with
2419 | Glut.KEY_UP -> action HCprev
2420 | Glut.KEY_DOWN -> action HCnext
2421 | Glut.KEY_HOME -> action HCfirst
2422 | Glut.KEY_END -> action HClast
2423 | _ -> state.text
2425 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2426 Glut.postRedisplay ()
2428 | Textentry _ -> ()
2430 | Items (active, first, items, qsearch, pan, oldmode) ->
2431 let maxrows = maxoutlinerows () in
2432 let itemcount = Array.length items in
2433 let hasaction = function
2434 | (_, _, Noaction) -> false
2435 | _ -> true
2437 let find start incr =
2438 let rec find i =
2439 if i = -1 || i = itemcount
2440 then -1
2441 else (
2442 if hasaction items.(i)
2443 then i
2444 else find (i + incr)
2447 find start
2449 let set active first =
2450 let first = max 0 (min first (itemcount - maxrows)) in
2451 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2453 let navigate incr =
2454 let isvisible first n = n >= first && n - first <= maxrows in
2455 let active, first =
2456 let incr1 = if incr > 0 then 1 else -1 in
2457 if isvisible first active
2458 then
2459 let next =
2460 let next = active + incr in
2461 let next =
2462 if next < 0 || next >= itemcount
2463 then -1
2464 else find next incr1
2466 if next = -1 || abs (active - next) > maxrows
2467 then -1
2468 else next
2470 if next = -1
2471 then
2472 let first = first + incr in
2473 let first = max 0 (min first (itemcount - 1)) in
2474 let next =
2475 let next = active + incr in
2476 let next = max 0 (min next (itemcount - 1)) in
2477 find next ~-incr1
2479 let active = if next = -1 then active else next in
2480 active, first
2481 else
2482 let first = min next first in
2483 next, first
2484 else
2485 let first = first + incr in
2486 let first = max 0 (min first (itemcount - 1)) in
2487 let active =
2488 let next = active + incr in
2489 let next = max 0 (min next (itemcount - 1)) in
2490 let next = find next incr1 in
2491 if next = -1 || abs (active - first) > maxrows
2492 then active
2493 else next
2495 active, first
2497 set active first;
2498 Glut.postRedisplay ()
2500 begin match key with
2501 | Glut.KEY_UP -> navigate ~-1
2502 | Glut.KEY_DOWN -> navigate 1
2503 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2504 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2506 | Glut.KEY_RIGHT ->
2507 state.mode <- Items (
2508 active, first, items, qsearch, min 0 (pan - 1), oldmode
2510 Glut.postRedisplay ()
2512 | Glut.KEY_LEFT ->
2513 state.mode <- Items (
2514 active, first, items, qsearch, min 0 (pan + 1), oldmode
2516 Glut.postRedisplay ()
2518 | Glut.KEY_HOME ->
2519 let active = find 0 1 in
2520 set active 0;
2521 Glut.postRedisplay ()
2523 | Glut.KEY_END ->
2524 let first = max 0 (itemcount - maxrows) in
2525 let active = find (itemcount - 1) ~-1 in
2526 set active first;
2527 Glut.postRedisplay ()
2529 | _ -> ()
2530 end;
2532 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2533 let maxrows = maxoutlinerows () in
2534 let calcfirst first active =
2535 if active > first
2536 then
2537 let rows = active - first in
2538 if rows > maxrows then active - maxrows else first
2539 else active
2541 let navigate incr =
2542 let active = active + incr in
2543 let active = max 0 (min active (Array.length outlines - 1)) in
2544 let first = calcfirst first active in
2545 state.mode <- Outline (
2546 allowdel, active, first, outlines, qsearch, pan, oldmode
2548 Glut.postRedisplay ()
2550 let updownlevel incr =
2551 let len = Array.length outlines in
2552 let (_, curlevel, _, _) = outlines.(active) in
2553 let rec flow i =
2554 if i = len then i-1 else if i = -1 then 0 else
2555 let (_, l, _, _) = outlines.(i) in
2556 if l != curlevel then i else flow (i+incr)
2558 let active = flow active in
2559 let first = calcfirst first active in
2560 state.mode <- Outline (
2561 allowdel, active, first, outlines, qsearch, pan, oldmode
2563 Glut.postRedisplay ()
2565 match key with
2566 | Glut.KEY_UP -> navigate ~-1
2567 | Glut.KEY_DOWN -> navigate 1
2568 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2569 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2571 | Glut.KEY_RIGHT ->
2572 if Glut.getModifiers () land Glut.active_ctrl != 0
2573 then (
2574 state.mode <- Outline (
2575 allowdel, active, first, outlines,
2576 qsearch, min 0 (pan - 1), oldmode
2578 Glut.postRedisplay ();
2580 else (
2581 if not allowdel
2582 then updownlevel 1
2585 | Glut.KEY_LEFT ->
2586 if Glut.getModifiers () land Glut.active_ctrl != 0
2587 then (
2588 state.mode <- Outline (
2589 allowdel, active, first, outlines, qsearch, pan + 1, oldmode
2591 Glut.postRedisplay ();
2593 else (
2594 if not allowdel
2595 then updownlevel ~-1
2598 | Glut.KEY_HOME ->
2599 state.mode <- Outline (
2600 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2602 Glut.postRedisplay ()
2604 | Glut.KEY_END ->
2605 let active = Array.length outlines - 1 in
2606 let first = max 0 (active - maxrows) in
2607 state.mode <- Outline (
2608 allowdel, active, first, outlines, qsearch, pan, oldmode
2610 Glut.postRedisplay ()
2612 | _ -> ()
2615 let drawplaceholder l =
2616 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2617 GlDraw.rect
2618 (float l.pagex, float l.pagedispy)
2619 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2621 let x = float (if margin < 0 then -margin else l.pagex)
2622 and y = float (l.pagedispy + 13) in
2623 let font = Glut.BITMAP_8_BY_13 in
2624 GlDraw.color (0.0, 0.0, 0.0);
2625 GlPix.raster_pos ~x ~y ();
2626 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2627 ("Loading " ^ string_of_int (l.pageno + 1));
2630 let now () = Unix.gettimeofday ();;
2632 let drawpage l =
2633 let color =
2634 match state.mode with
2635 | Textentry _ -> scalecolor 0.4
2636 | View | Outline _ | Items _ -> scalecolor 1.0
2637 | Birdseye (_, _, pageno, hooverpageno, _) ->
2638 if l.pageno = hooverpageno
2639 then scalecolor 0.9
2640 else (
2641 if l.pageno = pageno
2642 then scalecolor 1.0
2643 else scalecolor 0.8
2646 GlDraw.color color;
2647 begin match getopaque l.pageno with
2648 | Some (opaque, _) when validopaque opaque ->
2649 let a = now () in
2650 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2651 opaque;
2652 let b = now () in
2653 let d = b-.a in
2654 vlog "draw %d %f sec" l.pageno d;
2656 | _ ->
2657 drawplaceholder l;
2658 end;
2661 let scrollph y =
2662 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2663 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2664 let sh = float conf.winh /. sh in
2665 let sh = max sh (float conf.scrollh) in
2667 let percent =
2668 if state.y = state.maxy
2669 then 1.0
2670 else float y /. float maxy
2672 let position = (float conf.winh -. sh) *. percent in
2674 let position =
2675 if position +. sh > float conf.winh
2676 then float conf.winh -. sh
2677 else position
2679 position, sh;
2682 let scrollindicator () =
2683 GlDraw.color (0.64 , 0.64, 0.64);
2684 GlDraw.rect
2685 (float (conf.winw - conf.scrollw), 0.)
2686 (float conf.winw, float conf.winh)
2688 GlDraw.color (0.0, 0.0, 0.0);
2690 let position, sh = scrollph state.y in
2691 GlDraw.rect
2692 (float (conf.winw - conf.scrollw), position)
2693 (float conf.winw, position +. sh)
2697 let showsel margin =
2698 match state.mstate with
2699 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2702 | Msel ((x0, y0), (x1, y1)) ->
2703 let rec loop = function
2704 | l :: ls ->
2705 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2706 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2707 then
2708 match getopaque l.pageno with
2709 | Some (opaque, _) when validopaque opaque ->
2710 let oy = -l.pagey + l.pagedispy in
2711 seltext opaque
2712 (x0 - margin - state.x, y0,
2713 x1 - margin - state.x, y1) oy;
2715 | _ -> ()
2716 else loop ls
2717 | [] -> ()
2719 loop state.layout
2722 let showrects () =
2723 let panx = float state.x in
2724 Gl.enable `blend;
2725 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2726 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2727 List.iter
2728 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2729 List.iter (fun l ->
2730 if l.pageno = pageno
2731 then (
2732 let d = float (l.pagedispy - l.pagey) in
2733 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2734 GlDraw.begins `quads;
2736 GlDraw.vertex2 (x0+.panx, y0+.d);
2737 GlDraw.vertex2 (x1+.panx, y1+.d);
2738 GlDraw.vertex2 (x2+.panx, y2+.d);
2739 GlDraw.vertex2 (x3+.panx, y3+.d);
2741 GlDraw.ends ();
2743 ) state.layout
2744 ) state.rects
2746 Gl.disable `blend;
2749 let showoutline (_, active, first, outlines, _, pan, _) =
2750 Gl.enable `blend;
2751 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2752 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2753 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2754 Gl.disable `blend;
2756 GlDraw.color (1., 1., 1.);
2757 let font = Glut.BITMAP_9_BY_15 in
2758 let draw_string x y s =
2759 GlPix.raster_pos ~x ~y ();
2760 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2762 let rec loop row =
2763 if row = Array.length outlines || (row - first) * 16 > conf.winh
2764 then ()
2765 else (
2766 let (s, level, _, _) = outlines.(row) in
2767 let y = (row - first) * 16 in
2768 let x = 5 + 15*(max 0 (level+pan)) in
2769 if row = active
2770 then (
2771 Gl.enable `blend;
2772 GlDraw.polygon_mode `both `line;
2773 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2774 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2775 GlDraw.rect (0., float (y + 1))
2776 (float (conf.winw - 1), float (y + 18));
2777 GlDraw.polygon_mode `both `fill;
2778 Gl.disable `blend;
2779 GlDraw.color (1., 1., 1.);
2781 let draw_string s =
2782 let l = String.length s in
2783 if pan < 0
2784 then (
2785 let pan = pan * 2 in
2786 let left = l + pan in
2787 if left > 0
2788 then
2789 let s = String.sub s (-pan) left in
2790 draw_string (float x) (float (y + 16)) s
2792 else
2793 draw_string (float (x + pan*15)) (float (y + 16)) s
2795 draw_string s;
2796 loop (row+1)
2799 loop first
2802 let showitems (active, first, items, _, pan, _) =
2803 Gl.enable `blend;
2804 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2805 GlDraw.color (0., 0., 0.) ~alpha:0.90;
2806 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2807 Gl.disable `blend;
2809 GlDraw.color (1., 1., 1.);
2810 let font = Glut.BITMAP_9_BY_15 in
2811 let draw_string x y s =
2812 GlPix.raster_pos ~x ~y ();
2813 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2815 let rec loop row =
2816 if row = Array.length items || (row - first) * 16 > conf.winh
2817 then ()
2818 else (
2819 let (s, l, a) = items.(row) in
2820 let y = (row - first) * 16 in
2821 let x = 5 + (max 0 (l+pan))*15 in
2822 if row = active && a <> Noaction
2823 then (
2824 Gl.enable `blend;
2825 GlDraw.polygon_mode `both `line;
2826 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2827 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2828 GlDraw.rect (0., float (y + 1))
2829 (float (conf.winw - 1), float (y + 18));
2830 GlDraw.polygon_mode `both `fill;
2831 Gl.disable `blend;
2832 GlDraw.color (1., 1., 1.);
2834 let draw_string s =
2835 let l = String.length s in
2836 if pan < 0
2837 then (
2838 let pan = pan * 2 in
2839 let left = l + pan in
2840 if left > 0
2841 then
2842 let s = String.sub s (-pan) left in
2843 draw_string (float x) (float (y + 16)) s
2845 else
2846 draw_string (float (x + pan*15)) (float (y + 16)) s
2848 draw_string s;
2849 loop (row+1)
2852 loop first
2855 let display () =
2856 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2857 GlDraw.viewport margin 0 state.w conf.winh;
2858 pagematrix ();
2859 GlClear.color (scalecolor2 conf.bgcolor);
2860 GlClear.clear [`color];
2861 if conf.zoom > 1.0
2862 then (
2863 Gl.enable `scissor_test;
2864 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2866 List.iter drawpage state.layout;
2867 if conf.zoom > 1.0
2868 then
2869 Gl.disable `scissor_test
2871 if state.x != 0
2872 then (
2873 let x = -.float state.x in
2874 GlMat.translate ~x ();
2876 showrects ();
2877 showsel margin;
2878 GlDraw.viewport 0 0 conf.winw conf.winh;
2879 winmatrix ();
2880 scrollindicator ();
2881 begin match state.mode with
2882 | Items items -> showitems items
2883 | Outline outline -> showoutline outline
2884 | _ -> ()
2885 end;
2886 enttext ();
2887 Glut.swapBuffers ();
2890 let getunder x y =
2891 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2892 let x = x - margin - state.x in
2893 let rec f = function
2894 | l :: rest ->
2895 begin match getopaque l.pageno with
2896 | Some (opaque, _) when validopaque opaque ->
2897 let y = y - l.pagedispy in
2898 if y > 0
2899 then
2900 let y = l.pagey + y in
2901 let x = x - l.pagex in
2902 match whatsunder opaque x y with
2903 | Unone -> f rest
2904 | under -> under
2905 else
2906 f rest
2907 | _ ->
2908 f rest
2910 | [] -> Unone
2912 f state.layout
2915 let viewmouse button bstate x y =
2916 match button with
2917 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2918 if Glut.getModifiers () land Glut.active_ctrl != 0
2919 then (
2920 match state.mstate with
2921 | Mzoom (oldn, i) ->
2922 if oldn = n
2923 then (
2924 if i = 2
2925 then
2926 let incr =
2927 match n with
2928 | 4 ->
2929 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2930 | _ ->
2931 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2933 let zoom = conf.zoom +. incr in
2934 setzoom zoom;
2935 state.mstate <- Mzoom (n, 0);
2936 else
2937 state.mstate <- Mzoom (n, i+1);
2939 else state.mstate <- Mzoom (n, 0)
2941 | _ -> state.mstate <- Mzoom (n, 0)
2943 else (
2944 if state.ascrollstep > 0
2945 then
2946 setautoscrollspeed (n=4)
2947 else
2948 let incr =
2949 if n = 3
2950 then -conf.scrollstep
2951 else conf.scrollstep
2953 let incr = incr * 2 in
2954 let y = clamp incr in
2955 gotoy_and_clear_text y
2958 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2959 if bstate = Glut.DOWN
2960 then (
2961 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2962 state.mstate <- Mpan (x, y)
2964 else
2965 state.mstate <- Mnone
2967 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2968 if bstate = Glut.DOWN
2969 then
2970 let position, sh = scrollph state.y in
2971 if y > truncate position && y < truncate (position +. sh)
2972 then
2973 state.mstate <- Mscroll
2974 else
2975 let percent = float y /. float conf.winh in
2976 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2977 gotoy desty;
2978 state.mstate <- Mscroll
2979 else
2980 state.mstate <- Mnone
2982 | Glut.LEFT_BUTTON ->
2983 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2984 begin match dest with
2985 | Ulinkgoto (pageno, top) ->
2986 if pageno >= 0
2987 then (
2988 addnav ();
2989 gotopage1 pageno top;
2992 | Ulinkuri s ->
2993 print_endline s
2995 | Unone when bstate = Glut.DOWN ->
2996 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2997 state.mstate <- Mpan (x, y);
2999 | Unone | Utext _ ->
3000 if bstate = Glut.DOWN
3001 then (
3002 if conf.angle mod 360 = 0
3003 then (
3004 state.mstate <- Msel ((x, y), (x, y));
3005 Glut.postRedisplay ()
3008 else (
3009 match state.mstate with
3010 | Mnone -> ()
3012 | Mzoom _ | Mscroll ->
3013 state.mstate <- Mnone
3015 | Mpan _ ->
3016 Glut.setCursor Glut.CURSOR_INHERIT;
3017 state.mstate <- Mnone
3019 | Msel ((_, y0), (_, y1)) ->
3020 let f l =
3021 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3022 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3023 then
3024 match getopaque l.pageno with
3025 | Some (opaque, _) when validopaque opaque ->
3026 copysel opaque
3027 | _ -> ()
3029 List.iter f state.layout;
3030 copysel ""; (* ugly *)
3031 Glut.setCursor Glut.CURSOR_INHERIT;
3032 state.mstate <- Mnone;
3036 | _ -> ()
3039 let birdseyemouse button bstate x y
3040 (conf, leftx, _, hooverpageno, anchor) =
3041 match button with
3042 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3043 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
3044 let rec loop = function
3045 | [] -> ()
3046 | l :: rest ->
3047 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3048 && x > margin && x < margin + l.pagew
3049 then (
3050 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3052 else loop rest
3054 loop state.layout
3055 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3056 | _ -> ()
3059 let mouse bstate button x y =
3060 match state.mode with
3061 | View -> viewmouse button bstate x y
3062 | Birdseye beye -> birdseyemouse button bstate x y beye
3063 | Textentry _ | Outline _ | Items _ -> ()
3066 let mouse ~button ~state ~x ~y = mouse state button x y;;
3068 let motion ~x ~y =
3069 match state.mode with
3070 | Outline _ -> ()
3071 | _ ->
3072 match state.mstate with
3073 | Mzoom _ | Mnone -> ()
3075 | Mpan (x0, y0) ->
3076 let dx = x - x0
3077 and dy = y0 - y in
3078 state.mstate <- Mpan (x, y);
3079 if conf.zoom > 1.0 then state.x <- state.x + dx;
3080 let y = clamp dy in
3081 gotoy_and_clear_text y
3083 | Msel (a, _) ->
3084 state.mstate <- Msel (a, (x, y));
3085 Glut.postRedisplay ()
3087 | Mscroll ->
3088 let y = min conf.winh (max 0 y) in
3089 let percent = float y /. float conf.winh in
3090 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3091 gotoy_and_clear_text y
3094 let pmotion ~x ~y =
3095 match state.mode with
3096 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3097 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
3098 let rec loop = function
3099 | [] ->
3100 if hooverpageno != -1
3101 then (
3102 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3103 Glut.postRedisplay ();
3105 | l :: rest ->
3106 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3107 && x > margin && x < margin + l.pagew
3108 then (
3109 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3110 Glut.postRedisplay ();
3112 else loop rest
3114 loop state.layout
3116 | Outline _ | Items _ | Textentry _ -> ()
3117 | View ->
3118 match state.mstate with
3119 | Mnone ->
3120 begin match getunder x y with
3121 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3122 | Ulinkuri uri ->
3123 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3124 Glut.setCursor Glut.CURSOR_INFO
3125 | Ulinkgoto (page, _) ->
3126 if conf.underinfo
3127 then showtext 'p' ("age: " ^ string_of_int page);
3128 Glut.setCursor Glut.CURSOR_INFO
3129 | Utext s ->
3130 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3131 Glut.setCursor Glut.CURSOR_TEXT
3134 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3139 module State =
3140 struct
3141 open Parser
3143 let home =
3145 match Sys.os_type with
3146 | "Win32" -> Sys.getenv "HOMEPATH"
3147 | _ -> Sys.getenv "HOME"
3148 with exn ->
3149 prerr_endline
3150 ("Can not determine home directory location: " ^
3151 Printexc.to_string exn);
3155 let color_of_string s =
3156 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3157 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3161 let config_of c attrs =
3162 let apply c k v =
3164 match k with
3165 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
3166 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3167 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3168 | "preload" -> { c with preload = bool_of_string v }
3169 | "page-bias" -> { c with pagebias = int_of_string v }
3170 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3171 | "auto-scroll-step" ->
3172 { c with autoscrollstep = max 0 (int_of_string v) }
3173 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3174 | "crop-hack" -> { c with crophack = bool_of_string v }
3175 | "throttle" -> { c with showall = bool_of_string v }
3176 | "highlight-links" -> { c with hlinks = bool_of_string v }
3177 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3178 | "vertical-margin" ->
3179 { c with interpagespace = max 0 (int_of_string v) }
3180 | "zoom" ->
3181 let zoom = float_of_string v /. 100. in
3182 let zoom = max 0.01 (min 2.2 zoom) in
3183 { c with zoom = zoom }
3184 | "presentation" -> { c with presentation = bool_of_string v }
3185 | "rotation-angle" -> { c with angle = int_of_string v }
3186 | "width" -> { c with winw = max 20 (int_of_string v) }
3187 | "height" -> { c with winh = max 20 (int_of_string v) }
3188 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3189 | "proportional-display" -> { c with proportional = bool_of_string v }
3190 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3191 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3192 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3193 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3194 | "persistent-location" -> { c with jumpback = bool_of_string v }
3195 | "background-color" -> { c with bgcolor = color_of_string v }
3196 | _ -> c
3197 with exn ->
3198 prerr_endline ("Error processing attribute (`" ^
3199 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3202 let rec fold c = function
3203 | [] -> c
3204 | (k, v) :: rest ->
3205 let c = apply c k v in
3206 fold c rest
3208 fold c attrs;
3211 let fromstring f pos n v d =
3212 try f v
3213 with exn ->
3214 dolog "Error processing attribute (%S=%S) at %d\n%s"
3215 n v pos (Printexc.to_string exn)
3220 let bookmark_of attrs =
3221 let rec fold title page rely = function
3222 | ("title", v) :: rest -> fold v page rely rest
3223 | ("page", v) :: rest -> fold title v rely rest
3224 | ("rely", v) :: rest -> fold title page v rest
3225 | _ :: rest -> fold title page rely rest
3226 | [] -> title, page, rely
3228 fold "invalid" "0" "0" attrs
3231 let doc_of attrs =
3232 let rec fold path page rely pan = function
3233 | ("path", v) :: rest -> fold v page rely pan rest
3234 | ("page", v) :: rest -> fold path v rely pan rest
3235 | ("rely", v) :: rest -> fold path page v pan rest
3236 | ("pan", v) :: rest -> fold path page rely v rest
3237 | _ :: rest -> fold path page rely pan rest
3238 | [] -> path, page, rely, pan
3240 fold "" "0" "0" "0" attrs
3243 let setconf dst src =
3244 dst.scrollw <- src.scrollw;
3245 dst.scrollh <- src.scrollh;
3246 dst.icase <- src.icase;
3247 dst.preload <- src.preload;
3248 dst.pagebias <- src.pagebias;
3249 dst.verbose <- src.verbose;
3250 dst.scrollstep <- src.scrollstep;
3251 dst.maxhfit <- src.maxhfit;
3252 dst.crophack <- src.crophack;
3253 dst.autoscrollstep <- src.autoscrollstep;
3254 dst.showall <- src.showall;
3255 dst.hlinks <- src.hlinks;
3256 dst.underinfo <- src.underinfo;
3257 dst.interpagespace <- src.interpagespace;
3258 dst.zoom <- src.zoom;
3259 dst.presentation <- src.presentation;
3260 dst.angle <- src.angle;
3261 dst.winw <- src.winw;
3262 dst.winh <- src.winh;
3263 dst.savebmarks <- src.savebmarks;
3264 dst.memlimit <- src.memlimit;
3265 dst.proportional <- src.proportional;
3266 dst.texcount <- src.texcount;
3267 dst.sliceheight <- src.sliceheight;
3268 dst.thumbw <- src.thumbw;
3269 dst.jumpback <- src.jumpback;
3270 dst.bgcolor <- src.bgcolor;
3273 let unent s =
3274 let l = String.length s in
3275 let b = Buffer.create l in
3276 unent b s 0 l;
3277 Buffer.contents b;
3280 let get s =
3281 let h = Hashtbl.create 10 in
3282 let dc = { defconf with angle = defconf.angle } in
3283 let rec toplevel v t spos _ =
3284 match t with
3285 | Vdata | Vcdata | Vend -> v
3286 | Vopen ("llppconfig", _, closed) ->
3287 if closed
3288 then v
3289 else { v with f = llppconfig }
3290 | Vopen _ ->
3291 error "unexpected subelement at top level" s spos
3292 | Vclose _ -> error "unexpected close at top level" s spos
3294 and llppconfig v t spos _ =
3295 match t with
3296 | Vdata | Vcdata | Vend -> v
3297 | Vopen ("defaults", attrs, closed) ->
3298 let c = config_of dc attrs in
3299 setconf dc c;
3300 if closed
3301 then v
3302 else { v with f = skip "defaults" (fun () -> v) }
3304 | Vopen ("doc", attrs, closed) ->
3305 let pathent, spage, srely, span = doc_of attrs in
3306 let path = unent pathent
3307 and pageno = fromstring int_of_string spos "page" spage 0
3308 and rely = fromstring float_of_string spos "rely" srely 0.0
3309 and pan = fromstring int_of_string spos "pan" span 0 in
3310 let c = config_of dc attrs in
3311 let anchor = (pageno, rely) in
3312 if closed
3313 then (Hashtbl.add h path (c, [], pan, anchor); v)
3314 else { v with f = doc path pan anchor c [] }
3316 | Vopen _ ->
3317 error "unexpected subelement in llppconfig" s spos
3319 | Vclose "llppconfig" -> { v with f = toplevel }
3320 | Vclose _ -> error "unexpected close in llppconfig" s spos
3322 and doc path pan anchor c bookmarks v t spos _ =
3323 match t with
3324 | Vdata | Vcdata -> v
3325 | Vend -> error "unexpected end of input in doc" s spos
3326 | Vopen ("bookmarks", _, closed) ->
3327 if closed
3328 then v
3329 else { v with f = pbookmarks path pan anchor c bookmarks }
3331 | Vopen (_, _, _) ->
3332 error "unexpected subelement in doc" s spos
3334 | Vclose "doc" ->
3335 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3336 { v with f = llppconfig }
3338 | Vclose _ -> error "unexpected close in doc" s spos
3340 and pbookmarks path pan anchor c bookmarks v t spos _ =
3341 match t with
3342 | Vdata | Vcdata -> v
3343 | Vend -> error "unexpected end of input in bookmarks" s spos
3344 | Vopen ("item", attrs, closed) ->
3345 let titleent, spage, srely = bookmark_of attrs in
3346 let page = fromstring int_of_string spos "page" spage 0
3347 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3348 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
3349 if closed
3350 then { v with f = pbookmarks path pan anchor c bookmarks }
3351 else
3352 let f () = v in
3353 { v with f = skip "item" f }
3355 | Vopen _ ->
3356 error "unexpected subelement in bookmarks" s spos
3358 | Vclose "bookmarks" ->
3359 { v with f = doc path pan anchor c bookmarks }
3361 | Vclose _ -> error "unexpected close in bookmarks" s spos
3363 and skip tag f v t spos _ =
3364 match t with
3365 | Vdata | Vcdata -> v
3366 | Vend ->
3367 error ("unexpected end of input in skipped " ^ tag) s spos
3368 | Vopen (tag', _, closed) ->
3369 if closed
3370 then v
3371 else
3372 let f' () = { v with f = skip tag f } in
3373 { v with f = skip tag' f' }
3374 | Vclose ctag ->
3375 if tag = ctag
3376 then f ()
3377 else error ("unexpected close in skipped " ^ tag) s spos
3380 parse { f = toplevel; accu = () } s;
3381 h, dc;
3384 let do_load f ic =
3386 let len = in_channel_length ic in
3387 let s = String.create len in
3388 really_input ic s 0 len;
3389 f s;
3390 with
3391 | Parse_error (msg, s, pos) ->
3392 let subs = subs s pos in
3393 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3394 failwith ("parse error: " ^ s)
3396 | exn ->
3397 failwith ("config load error: " ^ Printexc.to_string exn)
3400 let path =
3401 let dir =
3403 let dir = Filename.concat home ".config" in
3404 if Sys.is_directory dir then dir else home
3405 with _ -> home
3407 Filename.concat dir "llpp.conf"
3410 let load1 f =
3411 if Sys.file_exists path
3412 then
3413 match
3414 (try Some (open_in_bin path)
3415 with exn ->
3416 prerr_endline
3417 ("Error opening configuation file `" ^ path ^ "': " ^
3418 Printexc.to_string exn);
3419 None
3421 with
3422 | Some ic ->
3423 begin try
3424 f (do_load get ic)
3425 with exn ->
3426 prerr_endline
3427 ("Error loading configuation from `" ^ path ^ "': " ^
3428 Printexc.to_string exn);
3429 end;
3430 close_in ic;
3432 | None -> ()
3433 else
3434 f (Hashtbl.create 0, defconf)
3437 let load () =
3438 let f (h, dc) =
3439 let pc, pb, px, pa =
3441 Hashtbl.find h (Filename.basename state.path)
3442 with Not_found -> dc, [], 0, (0, 0.0)
3444 setconf defconf dc;
3445 setconf conf pc;
3446 state.bookmarks <- pb;
3447 state.x <- px;
3448 if conf.jumpback
3449 then state.anchor <- pa;
3450 cbput state.hists.nav pa;
3452 load1 f
3455 let add_attrs bb always dc c =
3456 let ob s a b =
3457 if always || a != b
3458 then Printf.bprintf bb "\n %s='%b'" s a
3459 and oi s a b =
3460 if always || a != b
3461 then Printf.bprintf bb "\n %s='%d'" s a
3462 and oz s a b =
3463 if always || a <> b
3464 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3465 and oc s a b =
3466 if always || a <> b
3467 then
3468 let r, g, b = a in
3469 let r = truncate (r *. 256.0)
3470 and g = truncate (g *. 256.0)
3471 and b = truncate (b *. 256.0) in
3472 Printf.bprintf bb "\n %s='%d/%d/%d'" s r g b
3474 let w, h =
3475 if always
3476 then dc.winw, dc.winh
3477 else
3478 match state.fullscreen with
3479 | Some wh -> wh
3480 | None -> c.winw, c.winh
3482 let zoom, presentation, interpagespace, showall=
3483 if always
3484 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3485 else
3486 match state.mode with
3487 | Birdseye (bc, _, _, _, _) ->
3488 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3489 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3491 oi "width" w dc.winw;
3492 oi "height" h dc.winh;
3493 oi "scroll-bar-width" c.scrollw dc.scrollw;
3494 oi "scroll-handle-height" c.scrollh dc.scrollh;
3495 ob "case-insensitive-search" c.icase dc.icase;
3496 ob "preload" c.preload dc.preload;
3497 oi "page-bias" c.pagebias dc.pagebias;
3498 oi "scroll-step" c.scrollstep dc.scrollstep;
3499 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3500 ob "max-height-fit" c.maxhfit dc.maxhfit;
3501 ob "crop-hack" c.crophack dc.crophack;
3502 ob "throttle" showall dc.showall;
3503 ob "highlight-links" c.hlinks dc.hlinks;
3504 ob "under-cursor-info" c.underinfo dc.underinfo;
3505 oi "vertical-margin" interpagespace dc.interpagespace;
3506 oz "zoom" zoom dc.zoom;
3507 ob "presentation" presentation dc.presentation;
3508 oi "rotation-angle" c.angle dc.angle;
3509 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3510 ob "proportional-display" c.proportional dc.proportional;
3511 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3512 oi "texcount" c.texcount dc.texcount;
3513 oi "slice-height" c.sliceheight dc.sliceheight;
3514 oi "thumbnail-width" c.thumbw dc.thumbw;
3515 ob "persistent-location" c.jumpback dc.jumpback;
3516 oc "background-color" c.bgcolor dc.bgcolor;
3519 let save () =
3520 let bb = Buffer.create 32768 in
3521 let f (h, dc) =
3522 let dc = if conf.bedefault then conf else dc in
3523 Buffer.add_string bb "<llppconfig>\n<defaults ";
3524 add_attrs bb true dc dc;
3525 Buffer.add_string bb "/>\n";
3527 let adddoc path pan anchor c bookmarks =
3528 if bookmarks == [] && c = dc && anchor = emptyanchor
3529 then ()
3530 else (
3531 Printf.bprintf bb "<doc path='%s'"
3532 (enent path 0 (String.length path));
3534 if anchor <> emptyanchor
3535 then (
3536 let n, y = anchor in
3537 Printf.bprintf bb " page='%d'" n;
3538 if y > 1e-6
3539 then
3540 Printf.bprintf bb " rely='%f'" y
3544 if pan != 0
3545 then Printf.bprintf bb " pan='%d'" pan;
3547 add_attrs bb false dc c;
3549 begin match bookmarks with
3550 | [] -> Buffer.add_string bb "/>\n"
3551 | _ ->
3552 Buffer.add_string bb ">\n<bookmarks>\n";
3553 List.iter (fun (title, _level, page, rely) ->
3554 Printf.bprintf bb
3555 "<item title='%s' page='%d'"
3556 (enent title 0 (String.length title))
3557 page
3559 if rely > 1e-6
3560 then
3561 Printf.bprintf bb " rely='%f'" rely
3563 Buffer.add_string bb "/>\n";
3564 ) bookmarks;
3565 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3566 end;
3570 let pan =
3571 match state.mode with
3572 | Birdseye (_, pan, _, _, _) -> pan
3573 | _ -> state.x
3575 let basename = Filename.basename state.path in
3576 adddoc basename pan (getanchor ())
3577 { conf with
3578 autoscrollstep =
3579 if state.ascrollstep > 0
3580 then state.ascrollstep
3581 else conf.autoscrollstep }
3582 (if conf.savebmarks then state.bookmarks else []);
3584 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3585 if basename <> path
3586 then adddoc path x y c bookmarks
3587 ) h;
3588 Buffer.add_string bb "</llppconfig>";
3590 load1 f;
3591 if Buffer.length bb > 0
3592 then
3594 let tmp = path ^ ".tmp" in
3595 let oc = open_out_bin tmp in
3596 Buffer.output_buffer oc bb;
3597 close_out oc;
3598 Sys.rename tmp path;
3599 with exn ->
3600 prerr_endline
3601 ("error while saving configuration: " ^ Printexc.to_string exn)
3603 end;;
3605 let () =
3606 Arg.parse
3607 (Arg.align
3608 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3609 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3610 " Print version and exit")]
3612 (fun s -> state.path <- s)
3613 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3615 if String.length state.path = 0
3616 then (prerr_endline "file name missing"; exit 1);
3618 State.load ();
3620 let _ = Glut.init Sys.argv in
3621 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3622 let () = Glut.initWindowSize conf.winw conf.winh in
3623 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3625 let csock, ssock =
3626 if Sys.os_type = "Unix"
3627 then
3628 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3629 else
3630 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3631 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3632 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3633 Unix.bind sock addr;
3634 Unix.listen sock 1;
3635 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3636 Unix.connect csock addr;
3637 let ssock, _ = Unix.accept sock in
3638 Unix.close sock;
3639 let opts sock =
3640 Unix.setsockopt sock Unix.TCP_NODELAY true;
3641 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3643 opts ssock;
3644 opts csock;
3645 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3646 ssock, csock
3649 let () = Glut.displayFunc display in
3650 let () = Glut.reshapeFunc reshape in
3651 let () = Glut.keyboardFunc keyboard in
3652 let () = Glut.specialFunc special in
3653 let () = Glut.idleFunc (Some idle) in
3654 let () = Glut.mouseFunc mouse in
3655 let () = Glut.motionFunc motion in
3656 let () = Glut.passiveMotionFunc pmotion in
3658 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3659 state.csock <- csock;
3660 state.ssock <- ssock;
3661 state.text <- "Opening " ^ state.path;
3662 writeopen state.path state.password;
3664 at_exit State.save;
3666 let rec handlelablglutbug () =
3668 Glut.mainLoop ();
3669 with Glut.BadEnum "key in special_of_int" ->
3670 showtext '!' " LablGlut bug: special key not recognized";
3671 handlelablglutbug ()
3673 handlelablglutbug ();