Anchor when reloading
[llpp.git] / main.ml
blobccf566086251b1774a9ada7f58ffd316ff18013a
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 then (
1584 conf.scrollbarinpm <- v;
1585 if conf.presentation
1586 then (
1587 state.scrollw <- 0;
1588 reshape conf.winw conf.winh;
1593 "", 0, Noaction;
1594 "Pixmap Cache", 0, Noaction;
1596 intp "size (advisory)"
1597 (fun () -> conf.memlimit)
1598 (fun v -> conf.memlimit <- v);
1599 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1603 let tailer =
1604 let trailer =
1605 ("", 0, Noaction)
1606 :: ("Document", 0, Noaction)
1607 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1609 if birdseye
1610 then trailer
1611 else (
1612 ("", 0, Noaction)
1613 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1614 (btos conf.bedefault),
1616 Action (
1617 fun active first qsearch pan ->
1618 conf.bedefault <- not conf.bedefault;
1619 Items (active, first, makeitems (), qsearch, pan, mode);
1621 ) :: trailer
1624 Array.of_list (items @ tailer)
1626 state.text <- "";
1627 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1628 Glut.postRedisplay ();
1631 let enterhelpmode () =
1632 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1633 Glut.postRedisplay ();
1636 let quickbookmark ?title () =
1637 match state.layout with
1638 | [] -> ()
1639 | l :: _ ->
1640 let title =
1641 match title with
1642 | None ->
1643 let sec = Unix.gettimeofday () in
1644 let tm = Unix.localtime sec in
1645 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1646 (l.pageno+1)
1647 tm.Unix.tm_mday
1648 tm.Unix.tm_mon
1649 (tm.Unix.tm_year + 1900)
1650 tm.Unix.tm_hour
1651 tm.Unix.tm_min
1652 | Some title -> title
1654 state.bookmarks <-
1655 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1656 :: state.bookmarks
1659 let doreshape w h =
1660 state.fullscreen <- None;
1661 Glut.reshapeWindow w h;
1664 let writeopen path password =
1665 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1666 writecmd state.csock "info";
1669 let opendoc path password =
1670 invalidate ();
1671 state.path <- path;
1672 state.password <- password;
1673 state.gen <- state.gen + 1;
1675 writeopen path password;
1676 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1677 wcmd "geometry" [`i state.w; `i conf.winh];
1680 let viewkeyboard key =
1681 let enttext te =
1682 let mode = state.mode in
1683 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1684 state.text <- "";
1685 enttext ();
1686 Glut.postRedisplay ()
1688 let c = Char.chr key in
1689 match c with
1690 | '\027' | 'q' -> (* escape *)
1691 exit 0
1693 | '\008' -> (* backspace *)
1694 let y = getnav ~-1 in
1695 gotoy_and_clear_text y
1697 | 'o' ->
1698 enteroutlinemode ()
1700 | 'u' ->
1701 state.rects <- [];
1702 state.text <- "";
1703 Glut.postRedisplay ()
1705 | '/' | '?' ->
1706 let ondone isforw s =
1707 cbput state.hists.pat s;
1708 state.searchpattern <- s;
1709 search s isforw
1711 let s = String.create 1 in
1712 s.[0] <- c;
1713 enttext (s, "", Some (onhist state.hists.pat),
1714 textentry, ondone (c ='/'))
1716 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1717 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1718 setzoom (min 2.2 (conf.zoom +. incr))
1720 | '+' ->
1721 let ondone s =
1722 let n =
1723 try int_of_string s with exc ->
1724 state.text <- Printf.sprintf "bad integer `%s': %s"
1725 s (Printexc.to_string exc);
1726 max_int
1728 if n != max_int
1729 then (
1730 conf.pagebias <- n;
1731 state.text <- "page bias is now " ^ string_of_int n;
1734 enttext ("page bias", "", None, intentry, ondone)
1736 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1737 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1738 setzoom (max 0.01 (conf.zoom -. decr))
1740 | '-' ->
1741 let ondone msg = state.text <- msg in
1742 enttext (
1743 "option [acfhilpstvAPRSZ]", "", None,
1744 optentry state.mode, ondone
1747 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1748 setzoom 1.0
1750 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1751 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1752 if zoom < 1.0
1753 then setzoom zoom
1755 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1756 togglebirdseye ()
1758 | '0' .. '9' ->
1759 let ondone s =
1760 let n =
1761 try int_of_string s with exc ->
1762 state.text <- Printf.sprintf "bad integer `%s': %s"
1763 s (Printexc.to_string exc);
1766 if n >= 0
1767 then (
1768 addnav ();
1769 cbput state.hists.pag (string_of_int n);
1770 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1773 let pageentry text key =
1774 match Char.unsafe_chr key with
1775 | 'g' -> TEdone text
1776 | _ -> intentry text key
1778 let text = "x" in text.[0] <- c;
1779 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1781 | 'b' ->
1782 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1783 reshape conf.winw conf.winh;
1785 | 'l' ->
1786 conf.hlinks <- not conf.hlinks;
1787 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1788 Glut.postRedisplay ()
1790 | 'a' ->
1791 if state.ascrollstep = 0
1792 then state.ascrollstep <- conf.autoscrollstep
1793 else (
1794 conf.autoscrollstep <- state.ascrollstep;
1795 state.ascrollstep <- 0;
1798 | 'P' ->
1799 conf.presentation <- not conf.presentation;
1800 if conf.presentation
1801 then (
1802 if not conf.scrollbarinpm
1803 then state.scrollw <- 0;
1805 else
1806 state.scrollw <- conf.scrollbw;
1808 showtext ' ' ("presentation mode " ^
1809 if conf.presentation then "on" else "off");
1810 state.anchor <- getanchor ();
1811 represent ()
1813 | 'f' ->
1814 begin match state.fullscreen with
1815 | None ->
1816 state.fullscreen <- Some (conf.winw, conf.winh);
1817 Glut.fullScreen ()
1818 | Some (w, h) ->
1819 state.fullscreen <- None;
1820 doreshape w h
1823 | 'g' ->
1824 gotoy_and_clear_text 0
1826 | 'n' ->
1827 search state.searchpattern true
1829 | 'p' | 'N' ->
1830 search state.searchpattern false
1832 | 't' ->
1833 begin match state.layout with
1834 | [] -> ()
1835 | l :: _ ->
1836 gotoy_and_clear_text (getpagey l.pageno)
1839 | ' ' ->
1840 begin match List.rev state.layout with
1841 | [] -> ()
1842 | l :: _ ->
1843 let pageno = min (l.pageno+1) (state.pagecount-1) in
1844 gotoy_and_clear_text (getpagey pageno)
1847 | '\127' -> (* delte *)
1848 begin match state.layout with
1849 | [] -> ()
1850 | l :: _ ->
1851 let pageno = max 0 (l.pageno-1) in
1852 gotoy_and_clear_text (getpagey pageno)
1855 | '=' ->
1856 let f (fn, _) l =
1857 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1859 let fn, ln = List.fold_left f (-1, -1) state.layout in
1860 let s =
1861 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1862 let percent =
1863 if maxy <= 0
1864 then 100.
1865 else (100. *. (float state.y /. float maxy)) in
1866 if fn = ln
1867 then
1868 Printf.sprintf "Page %d of %d %.2f%%"
1869 (fn+1) state.pagecount percent
1870 else
1871 Printf.sprintf
1872 "Pages %d-%d of %d %.2f%%"
1873 (fn+1) (ln+1) state.pagecount percent
1875 showtext ' ' s;
1877 | 'w' ->
1878 begin match state.layout with
1879 | [] -> ()
1880 | l :: _ ->
1881 doreshape (l.pagew + state.scrollw) l.pageh;
1882 Glut.postRedisplay ();
1885 | '\'' ->
1886 enterbookmarkmode ()
1888 | 'h' ->
1889 enterhelpmode ()
1891 | 'i' ->
1892 enterinfomode ()
1894 | 'm' ->
1895 let ondone s =
1896 match state.layout with
1897 | l :: _ ->
1898 state.bookmarks <-
1899 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1900 :: state.bookmarks
1901 | _ -> ()
1903 enttext ("bookmark", "", None, textentry, ondone)
1905 | '~' ->
1906 quickbookmark ();
1907 showtext ' ' "Quick bookmark added";
1909 | 'z' ->
1910 begin match state.layout with
1911 | l :: _ ->
1912 let rect = getpdimrect l.pagedimno in
1913 let w, h =
1914 if conf.crophack
1915 then
1916 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1917 truncate (1.2 *. (rect.(3) -. rect.(0))))
1918 else
1919 (truncate (rect.(1) -. rect.(0)),
1920 truncate (rect.(3) -. rect.(0)))
1922 if w != 0 && h != 0
1923 then
1924 doreshape (w + state.scrollw) (h + conf.interpagespace)
1926 Glut.postRedisplay ();
1928 | [] -> ()
1931 | 'x' ->
1932 state.text <- "zoom is reset to 100";
1933 Glut.reshapeWindow (state.w + state.scrollw)
1934 (truncate (float conf.winh *. conf.zoom));
1935 conf.zoom <- 1.0;
1936 Glut.postRedisplay ()
1938 | '<' | '>' ->
1939 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1941 | '[' | ']' ->
1942 state.colorscale <-
1943 max 0.0
1944 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1945 Glut.postRedisplay ()
1947 | 'k' ->
1948 begin match state.mode with
1949 | Birdseye beye -> upbirdseye beye
1950 | _ -> gotoy (clamp (-conf.scrollstep))
1953 | 'j' ->
1954 begin match state.mode with
1955 | Birdseye beye -> downbirdseye beye
1956 | _ -> gotoy (clamp conf.scrollstep)
1959 | 'r' ->
1960 state.anchor <- getanchor ();
1961 opendoc state.path state.password
1963 | _ ->
1964 vlog "huh? %d %c" key (Char.chr key);
1967 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1968 let enttext te =
1969 state.mode <- Textentry (te, onleave);
1970 state.text <- "";
1971 enttext ();
1972 Glut.postRedisplay ()
1974 match Char.unsafe_chr key with
1975 | '\008' -> (* backspace *)
1976 let len = String.length text in
1977 if len = 0
1978 then (
1979 onleave Cancel;
1980 Glut.postRedisplay ();
1982 else (
1983 let s = String.sub text 0 (len - 1) in
1984 enttext (c, s, opthist, onkey, ondone)
1987 | '\r' | '\n' ->
1988 ondone text;
1989 onleave Confirm;
1990 Glut.postRedisplay ()
1992 | '\027' -> (* escape *)
1993 begin match opthist with
1994 | None -> ()
1995 | Some (_, onhistcancel) -> onhistcancel ()
1996 end;
1997 onleave Cancel;
1998 Glut.postRedisplay ()
2000 | _ ->
2001 begin match onkey text key with
2002 | TEdone text ->
2003 onleave Confirm;
2004 ondone text;
2005 Glut.postRedisplay ()
2007 | TEcont text ->
2008 enttext (c, text, opthist, onkey, ondone);
2010 | TEstop ->
2011 onleave Cancel;
2012 Glut.postRedisplay ()
2014 | TEswitch te ->
2015 state.mode <- Textentry (te, onleave);
2016 Glut.postRedisplay ()
2017 end;
2020 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2021 match key with
2022 | 27 -> (* escape *)
2023 leavebirdseye beye true
2025 | 12 -> (* ctrl-l *)
2026 let y, h = getpageyh pageno in
2027 let top = (conf.winh - h) / 2 in
2028 gotoy (max 0 (y - top))
2030 | 13 -> (* enter *)
2031 leavebirdseye beye false
2033 | _ ->
2034 viewkeyboard key
2037 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2038 let set active first qsearch =
2039 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2041 let search active pattern incr =
2042 let dosearch re =
2043 let rec loop n =
2044 if n = Array.length items || n = -1
2045 then None
2046 else
2047 let (s, _, _) = items.(n) in
2049 (try ignore (Str.search_forward re s 0); true
2050 with Not_found -> false)
2051 then Some n
2052 else loop (n + incr)
2054 loop active
2057 let re = Str.regexp_case_fold pattern in
2058 dosearch re
2059 with Failure s ->
2060 state.text <- s;
2061 None
2063 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2064 match key with
2065 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2066 let incr = if key = 18 then -1 else 1 in
2067 let active, first =
2068 match search (active + incr) qsearch incr with
2069 | None ->
2070 state.text <- qsearch ^ " [not found]";
2071 active, first
2072 | Some active ->
2073 state.text <- qsearch;
2074 active, firstof active
2076 set active first qsearch;
2077 Glut.postRedisplay ();
2079 | 8 -> (* backspace *)
2080 let len = String.length qsearch in
2081 if len = 0
2082 then ()
2083 else (
2084 if len = 1
2085 then (
2086 state.text <- "";
2087 set active first "";
2089 else
2090 let qsearch = String.sub qsearch 0 (len - 1) in
2091 let active, first =
2092 match search active qsearch ~-1 with
2093 | None ->
2094 state.text <- qsearch ^ " [not found]";
2095 active, first
2096 | Some active ->
2097 state.text <- qsearch;
2098 active, firstof active
2100 set active first qsearch
2102 Glut.postRedisplay ()
2104 | _ when key >= 32 && key < 127 ->
2105 let pattern = addchar qsearch (Char.chr key) in
2106 let active, first =
2107 match search active pattern 1 with
2108 | None ->
2109 state.text <- pattern ^ " [not found]";
2110 active, first
2111 | Some active ->
2112 state.text <- pattern;
2113 active, firstof active
2115 set active first pattern;
2116 Glut.postRedisplay ()
2118 | 27 -> (* escape *)
2119 state.text <- "";
2120 state.mode <- oldmode;
2121 Glut.postRedisplay ();
2123 | 13 -> (* enter *)
2124 if active < Array.length items
2125 then (
2126 match items.(active) with
2127 | _, _, Action f ->
2128 state.mode <- f active first qsearch pan
2130 | _, _, Noaction ->
2131 state.text <- "";
2132 state.mode <- oldmode
2134 Glut.postRedisplay ();
2136 | _ -> dolog "unknown key %d" key
2139 let outlinekeyboard key
2140 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2141 let narrow outlines pattern =
2142 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2143 match reopt with
2144 | None -> None
2145 | Some re ->
2146 let rec fold accu n =
2147 if n = -1
2148 then accu
2149 else
2150 let (s, _, _) as o = outlines.(n) in
2151 let accu =
2152 if (try ignore (Str.search_forward re s 0); true
2153 with Not_found -> false)
2154 then (o :: accu)
2155 else accu
2157 fold accu (n-1)
2159 let matched = fold [] (Array.length outlines - 1) in
2160 if matched = [] then None else Some (Array.of_list matched)
2162 let search active pattern incr =
2163 let dosearch re =
2164 let rec loop n =
2165 if n = Array.length outlines || n = -1
2166 then None
2167 else
2168 let (s, _, _) = outlines.(n) in
2170 (try ignore (Str.search_forward re s 0); true
2171 with Not_found -> false)
2172 then Some n
2173 else loop (n + incr)
2175 loop active
2178 let re = Str.regexp_case_fold pattern in
2179 dosearch re
2180 with Failure s ->
2181 state.text <- s;
2182 None
2184 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2185 match key with
2186 | 27 -> (* escape *)
2187 if String.length qsearch = 0
2188 then (
2189 state.text <- "";
2190 state.mode <- oldmode;
2191 Glut.postRedisplay ();
2193 else (
2194 state.text <- "";
2195 state.mode <- Outline (
2196 allowdel, active, first, outlines, "", pan, oldmode
2198 Glut.postRedisplay ();
2201 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2202 let incr = if key = 18 then -1 else 1 in
2203 let active, first =
2204 match search (active + incr) qsearch incr with
2205 | None ->
2206 state.text <- qsearch ^ " [not found]";
2207 active, first
2208 | Some active ->
2209 state.text <- qsearch;
2210 active, firstof active
2212 state.mode <- Outline (
2213 allowdel, active, first, outlines, qsearch, pan, oldmode
2215 Glut.postRedisplay ();
2217 | 8 -> (* backspace *)
2218 let len = String.length qsearch in
2219 if len = 0
2220 then ()
2221 else (
2222 if len = 1
2223 then (
2224 state.text <- "";
2225 state.mode <- Outline (
2226 allowdel, active, first, outlines, "", pan, oldmode
2229 else
2230 let qsearch = String.sub qsearch 0 (len - 1) in
2231 let active, first =
2232 match search active qsearch ~-1 with
2233 | None ->
2234 state.text <- qsearch ^ " [not found]";
2235 active, first
2236 | Some active ->
2237 state.text <- qsearch;
2238 active, firstof active
2240 state.mode <- Outline (
2241 allowdel, active, first, outlines, qsearch, pan, oldmode
2244 Glut.postRedisplay ()
2246 | 13 -> (* enter *)
2247 if active < Array.length outlines
2248 then (
2249 let (_, _, anchor) = outlines.(active) in
2250 addnav ();
2251 gotoanchor anchor;
2253 state.text <- "";
2254 if allowdel then state.bookmarks <- Array.to_list outlines;
2255 state.mode <- oldmode;
2256 Glut.postRedisplay ();
2258 | _ when key >= 32 && key < 127 ->
2259 let pattern = addchar qsearch (Char.chr key) in
2260 let active, first =
2261 match search active pattern 1 with
2262 | None ->
2263 state.text <- pattern ^ " [not found]";
2264 active, first
2265 | Some active ->
2266 state.text <- pattern;
2267 active, firstof active
2269 state.mode <- Outline (
2270 allowdel, active, first, outlines, pattern, pan, oldmode
2272 Glut.postRedisplay ()
2274 | 14 when not allowdel -> (* ctrl-n *)
2275 if String.length qsearch > 0
2276 then (
2277 let optoutlines = narrow outlines qsearch in
2278 begin match optoutlines with
2279 | None -> state.text <- "can't narrow"
2280 | Some outlines ->
2281 state.mode <- Outline (
2282 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2284 match state.outlines with
2285 | Olist _ -> ()
2286 | Oarray a ->
2287 state.outlines <- Onarrow (qsearch, outlines, a)
2288 | Onarrow (_, _, b) ->
2289 state.outlines <- Onarrow (qsearch, outlines, b)
2290 end;
2292 Glut.postRedisplay ()
2294 | 21 when not allowdel -> (* ctrl-u *)
2295 let outline =
2296 match state.outlines with
2297 | Oarray a -> a
2298 | Olist l ->
2299 let a = Array.of_list (List.rev l) in
2300 state.outlines <- Oarray a;
2302 | Onarrow (_, _, b) ->
2303 state.outlines <- Oarray b;
2304 state.text <- "";
2307 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2308 Glut.postRedisplay ()
2310 | 12 -> (* ctrl-l *)
2311 state.mode <- Outline
2312 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2313 Glut.postRedisplay ()
2315 | 127 when allowdel -> (* delete *)
2316 let len = Array.length outlines - 1 in
2317 if len = 0
2318 then (
2319 state.mode <- View;
2320 state.bookmarks <- [];
2322 else (
2323 let bookmarks = Array.init len
2324 (fun i ->
2325 let i = if i >= active then i + 1 else i in
2326 outlines.(i)
2329 state.mode <-
2330 Outline (
2331 allowdel,
2332 min active (len-1),
2333 min first (len-1),
2334 bookmarks, qsearch,
2336 oldmode
2339 Glut.postRedisplay ()
2341 | _ -> dolog "unknown key %d" key
2344 let keyboard ~key ~x ~y =
2345 ignore x;
2346 ignore y;
2347 if key = 7 (* ctrl-g *)
2348 then
2349 wcmd "interrupt" []
2350 else
2351 match state.mode with
2352 | Outline outline -> outlinekeyboard key outline
2353 | Textentry textentry -> textentrykeyboard key textentry
2354 | Birdseye birdseye -> birdseyekeyboard key birdseye
2355 | View -> viewkeyboard key
2356 | Items items -> itemskeyboard key items
2359 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2360 match key with
2361 | Glut.KEY_UP -> upbirdseye beye
2362 | Glut.KEY_DOWN -> downbirdseye beye
2364 | Glut.KEY_PAGE_UP ->
2365 begin match state.layout with
2366 | l :: _ ->
2367 if l.pagey != 0
2368 then (
2369 state.mode <- Birdseye (
2370 conf, leftx, l.pageno, hooverpageno, anchor
2372 gotopage1 l.pageno 0;
2374 else (
2375 let layout = layout (state.y-conf.winh) conf.winh in
2376 match layout with
2377 | [] -> gotoy (clamp (-conf.winh))
2378 | l :: _ ->
2379 state.mode <- Birdseye (
2380 conf, leftx, l.pageno, hooverpageno, anchor
2382 gotopage1 l.pageno 0
2385 | [] -> gotoy (clamp (-conf.winh))
2386 end;
2388 | Glut.KEY_PAGE_DOWN ->
2389 begin match List.rev state.layout with
2390 | l :: _ ->
2391 let layout = layout (state.y + conf.winh) conf.winh in
2392 begin match layout with
2393 | [] ->
2394 let incr = l.pageh - l.pagevh in
2395 if incr = 0
2396 then (
2397 state.mode <-
2398 Birdseye (
2399 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2401 Glut.postRedisplay ();
2403 else gotoy (clamp (incr + conf.interpagespace*2));
2405 | l :: _ ->
2406 state.mode <-
2407 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2408 gotopage1 l.pageno 0;
2411 | [] -> gotoy (clamp conf.winh)
2412 end;
2414 | Glut.KEY_HOME ->
2415 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2416 gotopage1 0 0
2418 | Glut.KEY_END ->
2419 let pageno = state.pagecount - 1 in
2420 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2421 if not (pagevisible state.layout pageno)
2422 then
2423 let h =
2424 match List.rev state.pdims with
2425 | [] -> conf.winh
2426 | (_, _, h, _) :: _ -> h
2428 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2429 else Glut.postRedisplay ();
2430 | _ -> ()
2433 let setautoscrollspeed goingdown =
2434 let incr = max 1 (state.ascrollstep / 2) in
2435 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2436 state.ascrollstep <- astep;
2439 let special ~key ~x ~y =
2440 ignore x;
2441 ignore y;
2442 match state.mode with
2443 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2444 togglebirdseye ()
2446 | Birdseye vals ->
2447 birdseyespecial key vals
2449 | View when key = Glut.KEY_F1 ->
2450 enterhelpmode ()
2452 | View ->
2453 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2454 then setautoscrollspeed (key = Glut.KEY_DOWN)
2455 else
2456 let y =
2457 match key with
2458 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2459 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2460 | Glut.KEY_DOWN -> clamp conf.scrollstep
2461 | Glut.KEY_PAGE_UP ->
2462 if Glut.getModifiers () land Glut.active_ctrl != 0
2463 then
2464 match state.layout with
2465 | [] -> state.y
2466 | l :: _ -> state.y - l.pagey
2467 else
2468 clamp (-conf.winh)
2469 | Glut.KEY_PAGE_DOWN ->
2470 if Glut.getModifiers () land Glut.active_ctrl != 0
2471 then
2472 match List.rev state.layout with
2473 | [] -> state.y
2474 | l :: _ -> getpagey l.pageno
2475 else
2476 clamp conf.winh
2477 | Glut.KEY_HOME -> addnav (); 0
2478 | Glut.KEY_END ->
2479 addnav ();
2480 state.maxy - (if conf.maxhfit then conf.winh else 0)
2482 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2483 Glut.getModifiers () land Glut.active_alt != 0 ->
2484 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2486 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2487 state.x <- state.x - 10;
2488 state.y
2489 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2490 state.x <- state.x + 10;
2491 state.y
2493 | _ -> state.y
2495 gotoy_and_clear_text y
2497 | Textentry
2498 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2499 let s =
2500 match key with
2501 | Glut.KEY_UP -> action HCprev
2502 | Glut.KEY_DOWN -> action HCnext
2503 | Glut.KEY_HOME -> action HCfirst
2504 | Glut.KEY_END -> action HClast
2505 | _ -> state.text
2507 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2508 Glut.postRedisplay ()
2510 | Textentry _ -> ()
2512 | Items (active, first, items, qsearch, pan, oldmode) ->
2513 let maxrows = maxoutlinerows () in
2514 let itemcount = Array.length items in
2515 let hasaction = function
2516 | (_, _, Noaction) -> false
2517 | _ -> true
2519 let find start incr =
2520 let rec find i =
2521 if i = -1 || i = itemcount
2522 then -1
2523 else (
2524 if hasaction items.(i)
2525 then i
2526 else find (i + incr)
2529 find start
2531 let set active first =
2532 let first = max 0 (min first (itemcount - maxrows)) in
2533 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2535 let navigate incr =
2536 let isvisible first n = n >= first && n - first <= maxrows in
2537 let active, first =
2538 let incr1 = if incr > 0 then 1 else -1 in
2539 if isvisible first active
2540 then
2541 let next =
2542 let next = active + incr in
2543 let next =
2544 if next < 0 || next >= itemcount
2545 then -1
2546 else find next incr1
2548 if next = -1 || abs (active - next) > maxrows
2549 then -1
2550 else next
2552 if next = -1
2553 then
2554 let first = first + incr in
2555 let first = max 0 (min first (itemcount - 1)) in
2556 let next =
2557 let next = active + incr in
2558 let next = max 0 (min next (itemcount - 1)) in
2559 find next ~-incr1
2561 let active = if next = -1 then active else next in
2562 active, first
2563 else
2564 let first = min next first in
2565 next, first
2566 else
2567 let first = first + incr in
2568 let first = max 0 (min first (itemcount - 1)) in
2569 let active =
2570 let next = active + incr in
2571 let next = max 0 (min next (itemcount - 1)) in
2572 let next = find next incr1 in
2573 if next = -1 || abs (active - first) > maxrows
2574 then active
2575 else next
2577 active, first
2579 set active first;
2580 Glut.postRedisplay ()
2582 begin match key with
2583 | Glut.KEY_UP -> navigate ~-1
2584 | Glut.KEY_DOWN -> navigate 1
2585 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2586 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2588 | Glut.KEY_RIGHT ->
2589 state.mode <- Items (
2590 active, first, items, qsearch, min 0 (pan - 1), oldmode
2592 Glut.postRedisplay ()
2594 | Glut.KEY_LEFT ->
2595 state.mode <- Items (
2596 active, first, items, qsearch, min 0 (pan + 1), oldmode
2598 Glut.postRedisplay ()
2600 | Glut.KEY_HOME ->
2601 let active = find 0 1 in
2602 set active 0;
2603 Glut.postRedisplay ()
2605 | Glut.KEY_END ->
2606 let first = max 0 (itemcount - maxrows) in
2607 let active = find (itemcount - 1) ~-1 in
2608 set active first;
2609 Glut.postRedisplay ()
2611 | _ -> ()
2612 end;
2614 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2615 let maxrows = maxoutlinerows () in
2616 let calcfirst first active =
2617 if active > first
2618 then
2619 let rows = active - first in
2620 if rows > maxrows then active - maxrows else first
2621 else active
2623 let navigate incr =
2624 let active = active + incr in
2625 let active = max 0 (min active (Array.length outlines - 1)) in
2626 let first = calcfirst first active in
2627 state.mode <- Outline (
2628 allowdel, active, first, outlines, qsearch, pan, oldmode
2630 Glut.postRedisplay ()
2632 let updownlevel incr =
2633 let len = Array.length outlines in
2634 let (_, curlevel, _) = outlines.(active) in
2635 let rec flow i =
2636 if i = len then i-1 else if i = -1 then 0 else
2637 let (_, l, _) = outlines.(i) in
2638 if l != curlevel then i else flow (i+incr)
2640 let active = flow active in
2641 let first = calcfirst first active in
2642 state.mode <- Outline (
2643 allowdel, active, first, outlines, qsearch, pan, oldmode
2645 Glut.postRedisplay ()
2647 match key with
2648 | Glut.KEY_UP -> navigate ~-1
2649 | Glut.KEY_DOWN -> navigate 1
2650 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2651 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2653 | Glut.KEY_RIGHT ->
2654 if Glut.getModifiers () land Glut.active_ctrl != 0
2655 then (
2656 state.mode <- Outline (
2657 allowdel, active, first, outlines,
2658 qsearch, min 0 (pan + 1), oldmode
2660 Glut.postRedisplay ();
2662 else (
2663 if not allowdel
2664 then updownlevel 1
2667 | Glut.KEY_LEFT ->
2668 if Glut.getModifiers () land Glut.active_ctrl != 0
2669 then (
2670 state.mode <- Outline (
2671 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2673 Glut.postRedisplay ();
2675 else (
2676 if not allowdel
2677 then updownlevel ~-1
2680 | Glut.KEY_HOME ->
2681 state.mode <- Outline (
2682 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2684 Glut.postRedisplay ()
2686 | Glut.KEY_END ->
2687 let active = Array.length outlines - 1 in
2688 let first = max 0 (active - maxrows) in
2689 state.mode <- Outline (
2690 allowdel, active, first, outlines, qsearch, pan, oldmode
2692 Glut.postRedisplay ()
2694 | _ -> ()
2697 let drawplaceholder l =
2698 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2699 GlDraw.rect
2700 (float l.pagex, float l.pagedispy)
2701 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2703 let x = float (if margin < 0 then -margin else l.pagex)
2704 and y = float (l.pagedispy + 13) in
2705 let font = Glut.BITMAP_8_BY_13 in
2706 GlDraw.color (0.0, 0.0, 0.0);
2707 GlPix.raster_pos ~x ~y ();
2708 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2709 ("Loading " ^ string_of_int (l.pageno + 1));
2712 let now () = Unix.gettimeofday ();;
2714 let drawpage l =
2715 let color =
2716 match state.mode with
2717 | Textentry _ -> scalecolor 0.4
2718 | View | Outline _ | Items _ -> scalecolor 1.0
2719 | Birdseye (_, _, pageno, hooverpageno, _) ->
2720 if l.pageno = hooverpageno
2721 then scalecolor 0.9
2722 else (
2723 if l.pageno = pageno
2724 then scalecolor 1.0
2725 else scalecolor 0.8
2728 GlDraw.color color;
2729 begin match getopaque l.pageno with
2730 | Some (opaque, _) when validopaque opaque ->
2731 let a = now () in
2732 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2733 opaque;
2734 let b = now () in
2735 let d = b-.a in
2736 vlog "draw %d %f sec" l.pageno d;
2738 | _ ->
2739 drawplaceholder l;
2740 end;
2743 let scrollph y =
2744 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2745 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2746 let sh = float conf.winh /. sh in
2747 let sh = max sh (float conf.scrollh) in
2749 let percent =
2750 if state.y = state.maxy
2751 then 1.0
2752 else float y /. float maxy
2754 let position = (float conf.winh -. sh) *. percent in
2756 let position =
2757 if position +. sh > float conf.winh
2758 then float conf.winh -. sh
2759 else position
2761 position, sh;
2764 let scrollindicator () =
2765 GlDraw.color (0.64 , 0.64, 0.64);
2766 GlDraw.rect
2767 (float (conf.winw - state.scrollw), 0.)
2768 (float conf.winw, float conf.winh)
2770 GlDraw.color (0.0, 0.0, 0.0);
2772 let position, sh = scrollph state.y in
2773 GlDraw.rect
2774 (float (conf.winw - state.scrollw), position)
2775 (float conf.winw, position +. sh)
2779 let showsel margin =
2780 match state.mstate with
2781 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2784 | Msel ((x0, y0), (x1, y1)) ->
2785 let rec loop = function
2786 | l :: ls ->
2787 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2788 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2789 then
2790 match getopaque l.pageno with
2791 | Some (opaque, _) when validopaque opaque ->
2792 let oy = -l.pagey + l.pagedispy in
2793 seltext opaque
2794 (x0 - margin - state.x, y0,
2795 x1 - margin - state.x, y1) oy;
2797 | _ -> ()
2798 else loop ls
2799 | [] -> ()
2801 loop state.layout
2804 let showrects () =
2805 let panx = float state.x in
2806 Gl.enable `blend;
2807 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2808 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2809 List.iter
2810 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2811 List.iter (fun l ->
2812 if l.pageno = pageno
2813 then (
2814 let d = float (l.pagedispy - l.pagey) in
2815 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2816 GlDraw.begins `quads;
2818 GlDraw.vertex2 (x0+.panx, y0+.d);
2819 GlDraw.vertex2 (x1+.panx, y1+.d);
2820 GlDraw.vertex2 (x2+.panx, y2+.d);
2821 GlDraw.vertex2 (x3+.panx, y3+.d);
2823 GlDraw.ends ();
2825 ) state.layout
2826 ) state.rects
2828 Gl.disable `blend;
2831 let showstrings active first pan strings =
2832 Gl.enable `blend;
2833 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2834 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2835 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2836 Gl.disable `blend;
2838 GlDraw.color (1., 1., 1.);
2839 let font = Glut.BITMAP_9_BY_15 in
2840 let draw_string x y s =
2841 GlPix.raster_pos ~x ~y ();
2842 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2844 let rec loop row =
2845 if row = Array.length strings || (row - first) * 16 > conf.winh
2846 then ()
2847 else (
2848 let (s, level, _) = strings.(row) in
2849 let y = (row - first) * 16 in
2850 let x = 5 + 15*(max 0 (level+pan)) in
2851 if row = active
2852 then (
2853 Gl.enable `blend;
2854 GlDraw.polygon_mode `both `line;
2855 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2856 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2857 GlDraw.rect (0., float (y + 1))
2858 (float (conf.winw - 1), float (y + 18));
2859 GlDraw.polygon_mode `both `fill;
2860 Gl.disable `blend;
2861 GlDraw.color (1., 1., 1.);
2863 let draw_string s =
2864 let l = String.length s in
2865 if pan < 0
2866 then (
2867 let pan = pan * 2 in
2868 let pos = pan + level in
2869 let left = l + pos in
2870 if left > 0
2871 then
2872 let s =
2873 if left > l
2874 then s
2875 else String.sub s (-pos) left
2877 draw_string (float x) (float (y + 16)) s
2879 else
2880 draw_string (float (x + pan*15)) (float (y + 16)) s
2882 draw_string s;
2883 loop (row+1)
2886 loop first
2889 let showoutline (_, active, first, outlines, _, pan, _) =
2890 showstrings active first pan outlines;
2893 let showitems (active, first, items, _, pan, _) =
2894 showstrings active first pan items;
2897 let display () =
2898 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2899 GlDraw.viewport margin 0 state.w conf.winh;
2900 pagematrix ();
2901 GlClear.color (scalecolor2 conf.bgcolor);
2902 GlClear.clear [`color];
2903 if conf.zoom > 1.0
2904 then (
2905 Gl.enable `scissor_test;
2906 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2908 List.iter drawpage state.layout;
2909 if conf.zoom > 1.0
2910 then
2911 Gl.disable `scissor_test
2913 if state.x != 0
2914 then (
2915 let x = -.float state.x in
2916 GlMat.translate ~x ();
2918 showrects ();
2919 showsel margin;
2920 GlDraw.viewport 0 0 conf.winw conf.winh;
2921 winmatrix ();
2922 scrollindicator ();
2923 begin match state.mode with
2924 | Items items -> showitems items
2925 | Outline outline -> showoutline outline
2926 | _ -> ()
2927 end;
2928 enttext ();
2929 Glut.swapBuffers ();
2932 let getunder x y =
2933 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2934 let x = x - margin - state.x in
2935 let rec f = function
2936 | l :: rest ->
2937 begin match getopaque l.pageno with
2938 | Some (opaque, _) when validopaque opaque ->
2939 let y = y - l.pagedispy in
2940 if y > 0
2941 then
2942 let y = l.pagey + y in
2943 let x = x - l.pagex in
2944 match whatsunder opaque x y with
2945 | Unone -> f rest
2946 | under -> under
2947 else
2948 f rest
2949 | _ ->
2950 f rest
2952 | [] -> Unone
2954 f state.layout
2957 let viewmouse button bstate x y =
2958 match button with
2959 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2960 if Glut.getModifiers () land Glut.active_ctrl != 0
2961 then (
2962 match state.mstate with
2963 | Mzoom (oldn, i) ->
2964 if oldn = n
2965 then (
2966 if i = 2
2967 then
2968 let incr =
2969 match n with
2970 | 4 ->
2971 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2972 | _ ->
2973 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2975 let zoom = conf.zoom +. incr in
2976 setzoom zoom;
2977 state.mstate <- Mzoom (n, 0);
2978 else
2979 state.mstate <- Mzoom (n, i+1);
2981 else state.mstate <- Mzoom (n, 0)
2983 | _ -> state.mstate <- Mzoom (n, 0)
2985 else (
2986 if state.ascrollstep > 0
2987 then
2988 setautoscrollspeed (n=4)
2989 else
2990 let incr =
2991 if n = 3
2992 then -conf.scrollstep
2993 else conf.scrollstep
2995 let incr = incr * 2 in
2996 let y = clamp incr in
2997 gotoy_and_clear_text y
3000 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3001 if bstate = Glut.DOWN
3002 then (
3003 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3004 state.mstate <- Mpan (x, y)
3006 else
3007 state.mstate <- Mnone
3009 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3010 if bstate = Glut.DOWN
3011 then
3012 let position, sh = scrollph state.y in
3013 if y > truncate position && y < truncate (position +. sh)
3014 then
3015 state.mstate <- Mscroll
3016 else
3017 let percent = float y /. float conf.winh in
3018 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3019 gotoy desty;
3020 state.mstate <- Mscroll
3021 else
3022 state.mstate <- Mnone
3024 | Glut.LEFT_BUTTON ->
3025 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3026 begin match dest with
3027 | Ulinkgoto (pageno, top) ->
3028 if pageno >= 0
3029 then (
3030 addnav ();
3031 gotopage1 pageno top;
3034 | Ulinkuri s ->
3035 print_endline s
3037 | Unone when bstate = Glut.DOWN ->
3038 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3039 state.mstate <- Mpan (x, y);
3041 | Unone | Utext _ ->
3042 if bstate = Glut.DOWN
3043 then (
3044 if conf.angle mod 360 = 0
3045 then (
3046 state.mstate <- Msel ((x, y), (x, y));
3047 Glut.postRedisplay ()
3050 else (
3051 match state.mstate with
3052 | Mnone -> ()
3054 | Mzoom _ | Mscroll ->
3055 state.mstate <- Mnone
3057 | Mpan _ ->
3058 Glut.setCursor Glut.CURSOR_INHERIT;
3059 state.mstate <- Mnone
3061 | Msel ((_, y0), (_, y1)) ->
3062 let f l =
3063 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3064 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3065 then
3066 match getopaque l.pageno with
3067 | Some (opaque, _) when validopaque opaque ->
3068 copysel opaque
3069 | _ -> ()
3071 List.iter f state.layout;
3072 copysel ""; (* ugly *)
3073 Glut.setCursor Glut.CURSOR_INHERIT;
3074 state.mstate <- Mnone;
3078 | _ -> ()
3081 let birdseyemouse button bstate x y
3082 (conf, leftx, _, hooverpageno, anchor) =
3083 match button with
3084 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3085 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3086 let rec loop = function
3087 | [] -> ()
3088 | l :: rest ->
3089 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3090 && x > margin && x < margin + l.pagew
3091 then (
3092 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3094 else loop rest
3096 loop state.layout
3097 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3098 | _ -> ()
3101 let mouse bstate button x y =
3102 match state.mode with
3103 | View -> viewmouse button bstate x y
3104 | Birdseye beye -> birdseyemouse button bstate x y beye
3105 | Textentry _ | Outline _ | Items _ -> ()
3108 let mouse ~button ~state ~x ~y = mouse state button x y;;
3110 let motion ~x ~y =
3111 match state.mode with
3112 | Outline _ -> ()
3113 | _ ->
3114 match state.mstate with
3115 | Mzoom _ | Mnone -> ()
3117 | Mpan (x0, y0) ->
3118 let dx = x - x0
3119 and dy = y0 - y in
3120 state.mstate <- Mpan (x, y);
3121 if conf.zoom > 1.0 then state.x <- state.x + dx;
3122 let y = clamp dy in
3123 gotoy_and_clear_text y
3125 | Msel (a, _) ->
3126 state.mstate <- Msel (a, (x, y));
3127 Glut.postRedisplay ()
3129 | Mscroll ->
3130 let y = min conf.winh (max 0 y) in
3131 let percent = float y /. float conf.winh in
3132 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3133 gotoy_and_clear_text y
3136 let pmotion ~x ~y =
3137 match state.mode with
3138 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3139 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3140 let rec loop = function
3141 | [] ->
3142 if hooverpageno != -1
3143 then (
3144 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3145 Glut.postRedisplay ();
3147 | l :: rest ->
3148 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3149 && x > margin && x < margin + l.pagew
3150 then (
3151 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3152 Glut.postRedisplay ();
3154 else loop rest
3156 loop state.layout
3158 | Outline _ | Items _ | Textentry _ -> ()
3159 | View ->
3160 match state.mstate with
3161 | Mnone ->
3162 begin match getunder x y with
3163 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3164 | Ulinkuri uri ->
3165 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3166 Glut.setCursor Glut.CURSOR_INFO
3167 | Ulinkgoto (page, _) ->
3168 if conf.underinfo
3169 then showtext 'p' ("age: " ^ string_of_int page);
3170 Glut.setCursor Glut.CURSOR_INFO
3171 | Utext s ->
3172 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3173 Glut.setCursor Glut.CURSOR_TEXT
3176 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3181 module State =
3182 struct
3183 open Parser
3185 let home =
3187 match Sys.os_type with
3188 | "Win32" -> Sys.getenv "HOMEPATH"
3189 | _ -> Sys.getenv "HOME"
3190 with exn ->
3191 prerr_endline
3192 ("Can not determine home directory location: " ^
3193 Printexc.to_string exn);
3197 let config_of c attrs =
3198 let apply c k v =
3200 match k with
3201 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3202 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3203 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3204 | "preload" -> { c with preload = bool_of_string v }
3205 | "page-bias" -> { c with pagebias = int_of_string v }
3206 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3207 | "auto-scroll-step" ->
3208 { c with autoscrollstep = max 0 (int_of_string v) }
3209 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3210 | "crop-hack" -> { c with crophack = bool_of_string v }
3211 | "throttle" -> { c with showall = bool_of_string v }
3212 | "highlight-links" -> { c with hlinks = bool_of_string v }
3213 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3214 | "vertical-margin" ->
3215 { c with interpagespace = max 0 (int_of_string v) }
3216 | "zoom" ->
3217 let zoom = float_of_string v /. 100. in
3218 let zoom = max 0.01 (min 2.2 zoom) in
3219 { c with zoom = zoom }
3220 | "presentation" -> { c with presentation = bool_of_string v }
3221 | "rotation-angle" -> { c with angle = int_of_string v }
3222 | "width" -> { c with winw = max 20 (int_of_string v) }
3223 | "height" -> { c with winh = max 20 (int_of_string v) }
3224 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3225 | "proportional-display" -> { c with proportional = bool_of_string v }
3226 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3227 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3228 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3229 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3230 | "persistent-location" -> { c with jumpback = bool_of_string v }
3231 | "background-color" -> { c with bgcolor = color_of_string v }
3232 | "scrollbar-in-presentation" ->
3233 { c with scrollbarinpm = bool_of_string v }
3234 | _ -> c
3235 with exn ->
3236 prerr_endline ("Error processing attribute (`" ^
3237 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3240 let rec fold c = function
3241 | [] -> c
3242 | (k, v) :: rest ->
3243 let c = apply c k v in
3244 fold c rest
3246 fold c attrs;
3249 let fromstring f pos n v d =
3250 try f v
3251 with exn ->
3252 dolog "Error processing attribute (%S=%S) at %d\n%s"
3253 n v pos (Printexc.to_string exn)
3258 let bookmark_of attrs =
3259 let rec fold title page rely = function
3260 | ("title", v) :: rest -> fold v page rely rest
3261 | ("page", v) :: rest -> fold title v rely rest
3262 | ("rely", v) :: rest -> fold title page v rest
3263 | _ :: rest -> fold title page rely rest
3264 | [] -> title, page, rely
3266 fold "invalid" "0" "0" attrs
3269 let doc_of attrs =
3270 let rec fold path page rely pan = function
3271 | ("path", v) :: rest -> fold v page rely pan rest
3272 | ("page", v) :: rest -> fold path v rely pan rest
3273 | ("rely", v) :: rest -> fold path page v pan rest
3274 | ("pan", v) :: rest -> fold path page rely v rest
3275 | _ :: rest -> fold path page rely pan rest
3276 | [] -> path, page, rely, pan
3278 fold "" "0" "0" "0" attrs
3281 let setconf dst src =
3282 dst.scrollbw <- src.scrollbw;
3283 dst.scrollh <- src.scrollh;
3284 dst.icase <- src.icase;
3285 dst.preload <- src.preload;
3286 dst.pagebias <- src.pagebias;
3287 dst.verbose <- src.verbose;
3288 dst.scrollstep <- src.scrollstep;
3289 dst.maxhfit <- src.maxhfit;
3290 dst.crophack <- src.crophack;
3291 dst.autoscrollstep <- src.autoscrollstep;
3292 dst.showall <- src.showall;
3293 dst.hlinks <- src.hlinks;
3294 dst.underinfo <- src.underinfo;
3295 dst.interpagespace <- src.interpagespace;
3296 dst.zoom <- src.zoom;
3297 dst.presentation <- src.presentation;
3298 dst.angle <- src.angle;
3299 dst.winw <- src.winw;
3300 dst.winh <- src.winh;
3301 dst.savebmarks <- src.savebmarks;
3302 dst.memlimit <- src.memlimit;
3303 dst.proportional <- src.proportional;
3304 dst.texcount <- src.texcount;
3305 dst.sliceheight <- src.sliceheight;
3306 dst.thumbw <- src.thumbw;
3307 dst.jumpback <- src.jumpback;
3308 dst.bgcolor <- src.bgcolor;
3309 dst.scrollbarinpm <- src.scrollbarinpm;
3312 let unent s =
3313 let l = String.length s in
3314 let b = Buffer.create l in
3315 unent b s 0 l;
3316 Buffer.contents b;
3319 let get s =
3320 let h = Hashtbl.create 10 in
3321 let dc = { defconf with angle = defconf.angle } in
3322 let rec toplevel v t spos _ =
3323 match t with
3324 | Vdata | Vcdata | Vend -> v
3325 | Vopen ("llppconfig", _, closed) ->
3326 if closed
3327 then v
3328 else { v with f = llppconfig }
3329 | Vopen _ ->
3330 error "unexpected subelement at top level" s spos
3331 | Vclose _ -> error "unexpected close at top level" s spos
3333 and llppconfig v t spos _ =
3334 match t with
3335 | Vdata | Vcdata | Vend -> v
3336 | Vopen ("defaults", attrs, closed) ->
3337 let c = config_of dc attrs in
3338 setconf dc c;
3339 if closed
3340 then v
3341 else { v with f = skip "defaults" (fun () -> v) }
3343 | Vopen ("doc", attrs, closed) ->
3344 let pathent, spage, srely, span = doc_of attrs in
3345 let path = unent pathent
3346 and pageno = fromstring int_of_string spos "page" spage 0
3347 and rely = fromstring float_of_string spos "rely" srely 0.0
3348 and pan = fromstring int_of_string spos "pan" span 0 in
3349 let c = config_of dc attrs in
3350 let anchor = (pageno, rely) in
3351 if closed
3352 then (Hashtbl.add h path (c, [], pan, anchor); v)
3353 else { v with f = doc path pan anchor c [] }
3355 | Vopen _ ->
3356 error "unexpected subelement in llppconfig" s spos
3358 | Vclose "llppconfig" -> { v with f = toplevel }
3359 | Vclose _ -> error "unexpected close in llppconfig" s spos
3361 and doc path pan anchor c bookmarks v t spos _ =
3362 match t with
3363 | Vdata | Vcdata -> v
3364 | Vend -> error "unexpected end of input in doc" s spos
3365 | Vopen ("bookmarks", _, closed) ->
3366 if closed
3367 then v
3368 else { v with f = pbookmarks path pan anchor c bookmarks }
3370 | Vopen (_, _, _) ->
3371 error "unexpected subelement in doc" s spos
3373 | Vclose "doc" ->
3374 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3375 { v with f = llppconfig }
3377 | Vclose _ -> error "unexpected close in doc" s spos
3379 and pbookmarks path pan anchor c bookmarks v t spos _ =
3380 match t with
3381 | Vdata | Vcdata -> v
3382 | Vend -> error "unexpected end of input in bookmarks" s spos
3383 | Vopen ("item", attrs, closed) ->
3384 let titleent, spage, srely = bookmark_of attrs in
3385 let page = fromstring int_of_string spos "page" spage 0
3386 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3387 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3388 if closed
3389 then { v with f = pbookmarks path pan anchor c bookmarks }
3390 else
3391 let f () = v in
3392 { v with f = skip "item" f }
3394 | Vopen _ ->
3395 error "unexpected subelement in bookmarks" s spos
3397 | Vclose "bookmarks" ->
3398 { v with f = doc path pan anchor c bookmarks }
3400 | Vclose _ -> error "unexpected close in bookmarks" s spos
3402 and skip tag f v t spos _ =
3403 match t with
3404 | Vdata | Vcdata -> v
3405 | Vend ->
3406 error ("unexpected end of input in skipped " ^ tag) s spos
3407 | Vopen (tag', _, closed) ->
3408 if closed
3409 then v
3410 else
3411 let f' () = { v with f = skip tag f } in
3412 { v with f = skip tag' f' }
3413 | Vclose ctag ->
3414 if tag = ctag
3415 then f ()
3416 else error ("unexpected close in skipped " ^ tag) s spos
3419 parse { f = toplevel; accu = () } s;
3420 h, dc;
3423 let do_load f ic =
3425 let len = in_channel_length ic in
3426 let s = String.create len in
3427 really_input ic s 0 len;
3428 f s;
3429 with
3430 | Parse_error (msg, s, pos) ->
3431 let subs = subs s pos in
3432 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3433 failwith ("parse error: " ^ s)
3435 | exn ->
3436 failwith ("config load error: " ^ Printexc.to_string exn)
3439 let path =
3440 let dir =
3442 let dir = Filename.concat home ".config" in
3443 if Sys.is_directory dir then dir else home
3444 with _ -> home
3446 Filename.concat dir "llpp.conf"
3449 let load1 f =
3450 if Sys.file_exists path
3451 then
3452 match
3453 (try Some (open_in_bin path)
3454 with exn ->
3455 prerr_endline
3456 ("Error opening configuation file `" ^ path ^ "': " ^
3457 Printexc.to_string exn);
3458 None
3460 with
3461 | Some ic ->
3462 begin try
3463 f (do_load get ic)
3464 with exn ->
3465 prerr_endline
3466 ("Error loading configuation from `" ^ path ^ "': " ^
3467 Printexc.to_string exn);
3468 end;
3469 close_in ic;
3471 | None -> ()
3472 else
3473 f (Hashtbl.create 0, defconf)
3476 let load () =
3477 let f (h, dc) =
3478 let pc, pb, px, pa =
3480 Hashtbl.find h (Filename.basename state.path)
3481 with Not_found -> dc, [], 0, (0, 0.0)
3483 setconf defconf dc;
3484 setconf conf pc;
3485 state.bookmarks <- pb;
3486 state.x <- px;
3487 state.scrollw <- conf.scrollbw;
3488 if conf.jumpback
3489 then state.anchor <- pa;
3490 cbput state.hists.nav pa;
3492 load1 f
3495 let add_attrs bb always dc c =
3496 let ob s a b =
3497 if always || a != b
3498 then Printf.bprintf bb "\n %s='%b'" s a
3499 and oi s a b =
3500 if always || a != b
3501 then Printf.bprintf bb "\n %s='%d'" s a
3502 and oz s a b =
3503 if always || a <> b
3504 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3505 and oc s a b =
3506 if always || a <> b
3507 then
3508 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3510 let w, h =
3511 if always
3512 then dc.winw, dc.winh
3513 else
3514 match state.fullscreen with
3515 | Some wh -> wh
3516 | None -> c.winw, c.winh
3518 let zoom, presentation, interpagespace, showall=
3519 if always
3520 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3521 else
3522 match state.mode with
3523 | Birdseye (bc, _, _, _, _) ->
3524 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3525 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3527 oi "width" w dc.winw;
3528 oi "height" h dc.winh;
3529 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3530 oi "scroll-handle-height" c.scrollh dc.scrollh;
3531 ob "case-insensitive-search" c.icase dc.icase;
3532 ob "preload" c.preload dc.preload;
3533 oi "page-bias" c.pagebias dc.pagebias;
3534 oi "scroll-step" c.scrollstep dc.scrollstep;
3535 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3536 ob "max-height-fit" c.maxhfit dc.maxhfit;
3537 ob "crop-hack" c.crophack dc.crophack;
3538 ob "throttle" showall dc.showall;
3539 ob "highlight-links" c.hlinks dc.hlinks;
3540 ob "under-cursor-info" c.underinfo dc.underinfo;
3541 oi "vertical-margin" interpagespace dc.interpagespace;
3542 oz "zoom" zoom dc.zoom;
3543 ob "presentation" presentation dc.presentation;
3544 oi "rotation-angle" c.angle dc.angle;
3545 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3546 ob "proportional-display" c.proportional dc.proportional;
3547 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3548 oi "texcount" c.texcount dc.texcount;
3549 oi "slice-height" c.sliceheight dc.sliceheight;
3550 oi "thumbnail-width" c.thumbw dc.thumbw;
3551 ob "persistent-location" c.jumpback dc.jumpback;
3552 oc "background-color" c.bgcolor dc.bgcolor;
3553 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3556 let save () =
3557 let bb = Buffer.create 32768 in
3558 let f (h, dc) =
3559 let dc = if conf.bedefault then conf else dc in
3560 Buffer.add_string bb "<llppconfig>\n<defaults ";
3561 add_attrs bb true dc dc;
3562 Buffer.add_string bb "/>\n";
3564 let adddoc path pan anchor c bookmarks =
3565 if bookmarks == [] && c = dc && anchor = emptyanchor
3566 then ()
3567 else (
3568 Printf.bprintf bb "<doc path='%s'"
3569 (enent path 0 (String.length path));
3571 if anchor <> emptyanchor
3572 then (
3573 let n, y = anchor in
3574 Printf.bprintf bb " page='%d'" n;
3575 if y > 1e-6
3576 then
3577 Printf.bprintf bb " rely='%f'" y
3581 if pan != 0
3582 then Printf.bprintf bb " pan='%d'" pan;
3584 add_attrs bb false dc c;
3586 begin match bookmarks with
3587 | [] -> Buffer.add_string bb "/>\n"
3588 | _ ->
3589 Buffer.add_string bb ">\n<bookmarks>\n";
3590 List.iter (fun (title, _level, (page, rely)) ->
3591 Printf.bprintf bb
3592 "<item title='%s' page='%d'"
3593 (enent title 0 (String.length title))
3594 page
3596 if rely > 1e-6
3597 then
3598 Printf.bprintf bb " rely='%f'" rely
3600 Buffer.add_string bb "/>\n";
3601 ) bookmarks;
3602 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3603 end;
3607 let pan =
3608 match state.mode with
3609 | Birdseye (_, pan, _, _, _) -> pan
3610 | _ -> state.x
3612 let basename = Filename.basename state.path in
3613 adddoc basename pan (getanchor ())
3614 { conf with
3615 autoscrollstep =
3616 if state.ascrollstep > 0
3617 then state.ascrollstep
3618 else conf.autoscrollstep }
3619 (if conf.savebmarks then state.bookmarks else []);
3621 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3622 if basename <> path
3623 then adddoc path x y c bookmarks
3624 ) h;
3625 Buffer.add_string bb "</llppconfig>";
3627 load1 f;
3628 if Buffer.length bb > 0
3629 then
3631 let tmp = path ^ ".tmp" in
3632 let oc = open_out_bin tmp in
3633 Buffer.output_buffer oc bb;
3634 close_out oc;
3635 Sys.rename tmp path;
3636 with exn ->
3637 prerr_endline
3638 ("error while saving configuration: " ^ Printexc.to_string exn)
3640 end;;
3642 let () =
3643 Arg.parse
3644 (Arg.align
3645 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3646 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3647 " Print version and exit")]
3649 (fun s -> state.path <- s)
3650 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3652 if String.length state.path = 0
3653 then (prerr_endline "file name missing"; exit 1);
3655 State.load ();
3657 let _ = Glut.init Sys.argv in
3658 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3659 let () = Glut.initWindowSize conf.winw conf.winh in
3660 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3662 let csock, ssock =
3663 if Sys.os_type = "Unix"
3664 then
3665 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3666 else
3667 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3668 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3669 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3670 Unix.bind sock addr;
3671 Unix.listen sock 1;
3672 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3673 Unix.connect csock addr;
3674 let ssock, _ = Unix.accept sock in
3675 Unix.close sock;
3676 let opts sock =
3677 Unix.setsockopt sock Unix.TCP_NODELAY true;
3678 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3680 opts ssock;
3681 opts csock;
3682 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3683 ssock, csock
3686 let () = Glut.displayFunc display in
3687 let () = Glut.reshapeFunc reshape in
3688 let () = Glut.keyboardFunc keyboard in
3689 let () = Glut.specialFunc special in
3690 let () = Glut.idleFunc (Some idle) in
3691 let () = Glut.mouseFunc mouse in
3692 let () = Glut.motionFunc motion in
3693 let () = Glut.passiveMotionFunc pmotion in
3695 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3696 state.csock <- csock;
3697 state.ssock <- ssock;
3698 state.text <- "Opening " ^ state.path;
3699 writeopen state.path state.password;
3701 at_exit State.save;
3703 let rec handlelablglutbug () =
3705 Glut.mainLoop ();
3706 with Glut.BadEnum "key in special_of_int" ->
3707 showtext '!' " LablGlut bug: special key not recognized";
3708 handlelablglutbug ()
3710 handlelablglutbug ();