Back out unintended change that kreeped into ddfb30313d397186e070d3e60bd6b7b597502fda
[llpp.git] / main.ml
blob6e1331f6889e8cda5f2e59c7bbdefcdd5eda5da8
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 | '<' | '>' ->
1932 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1934 | '[' | ']' ->
1935 state.colorscale <-
1936 max 0.0
1937 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1938 Glut.postRedisplay ()
1940 | 'k' ->
1941 begin match state.mode with
1942 | Birdseye beye -> upbirdseye beye
1943 | _ -> gotoy (clamp (-conf.scrollstep))
1946 | 'j' ->
1947 begin match state.mode with
1948 | Birdseye beye -> downbirdseye beye
1949 | _ -> gotoy (clamp conf.scrollstep)
1952 | 'r' ->
1953 state.anchor <- getanchor ();
1954 opendoc state.path state.password
1956 | _ ->
1957 vlog "huh? %d %c" key (Char.chr key);
1960 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1961 let enttext te =
1962 state.mode <- Textentry (te, onleave);
1963 state.text <- "";
1964 enttext ();
1965 Glut.postRedisplay ()
1967 match Char.unsafe_chr key with
1968 | '\008' -> (* backspace *)
1969 let len = String.length text in
1970 if len = 0
1971 then (
1972 onleave Cancel;
1973 Glut.postRedisplay ();
1975 else (
1976 let s = String.sub text 0 (len - 1) in
1977 enttext (c, s, opthist, onkey, ondone)
1980 | '\r' | '\n' ->
1981 ondone text;
1982 onleave Confirm;
1983 Glut.postRedisplay ()
1985 | '\027' -> (* escape *)
1986 begin match opthist with
1987 | None -> ()
1988 | Some (_, onhistcancel) -> onhistcancel ()
1989 end;
1990 onleave Cancel;
1991 Glut.postRedisplay ()
1993 | _ ->
1994 begin match onkey text key with
1995 | TEdone text ->
1996 onleave Confirm;
1997 ondone text;
1998 Glut.postRedisplay ()
2000 | TEcont text ->
2001 enttext (c, text, opthist, onkey, ondone);
2003 | TEstop ->
2004 onleave Cancel;
2005 Glut.postRedisplay ()
2007 | TEswitch te ->
2008 state.mode <- Textentry (te, onleave);
2009 Glut.postRedisplay ()
2010 end;
2013 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2014 match key with
2015 | 27 -> (* escape *)
2016 leavebirdseye beye true
2018 | 12 -> (* ctrl-l *)
2019 let y, h = getpageyh pageno in
2020 let top = (conf.winh - h) / 2 in
2021 gotoy (max 0 (y - top))
2023 | 13 -> (* enter *)
2024 leavebirdseye beye false
2026 | _ ->
2027 viewkeyboard key
2030 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2031 let set active first qsearch =
2032 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2034 let search active pattern incr =
2035 let dosearch re =
2036 let rec loop n =
2037 if n = Array.length items || n = -1
2038 then None
2039 else
2040 let (s, _, _) = items.(n) in
2042 (try ignore (Str.search_forward re s 0); true
2043 with Not_found -> false)
2044 then Some n
2045 else loop (n + incr)
2047 loop active
2050 let re = Str.regexp_case_fold pattern in
2051 dosearch re
2052 with Failure s ->
2053 state.text <- s;
2054 None
2056 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2057 match key with
2058 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2059 let incr = if key = 18 then -1 else 1 in
2060 let active, first =
2061 match search (active + incr) qsearch incr with
2062 | None ->
2063 state.text <- qsearch ^ " [not found]";
2064 active, first
2065 | Some active ->
2066 state.text <- qsearch;
2067 active, firstof active
2069 set active first qsearch;
2070 Glut.postRedisplay ();
2072 | 8 -> (* backspace *)
2073 let len = String.length qsearch in
2074 if len = 0
2075 then ()
2076 else (
2077 if len = 1
2078 then (
2079 state.text <- "";
2080 set active first "";
2082 else
2083 let qsearch = String.sub qsearch 0 (len - 1) in
2084 let active, first =
2085 match search active qsearch ~-1 with
2086 | None ->
2087 state.text <- qsearch ^ " [not found]";
2088 active, first
2089 | Some active ->
2090 state.text <- qsearch;
2091 active, firstof active
2093 set active first qsearch
2095 Glut.postRedisplay ()
2097 | _ when key >= 32 && key < 127 ->
2098 let pattern = addchar qsearch (Char.chr key) in
2099 let active, first =
2100 match search active pattern 1 with
2101 | None ->
2102 state.text <- pattern ^ " [not found]";
2103 active, first
2104 | Some active ->
2105 state.text <- pattern;
2106 active, firstof active
2108 set active first pattern;
2109 Glut.postRedisplay ()
2111 | 27 -> (* escape *)
2112 state.text <- "";
2113 state.mode <- oldmode;
2114 Glut.postRedisplay ();
2116 | 13 -> (* enter *)
2117 if active < Array.length items
2118 then (
2119 match items.(active) with
2120 | _, _, Action f ->
2121 state.mode <- f active first qsearch pan
2123 | _, _, Noaction ->
2124 state.text <- "";
2125 state.mode <- oldmode
2127 Glut.postRedisplay ();
2129 | _ -> dolog "unknown key %d" key
2132 let outlinekeyboard key
2133 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2134 let narrow outlines pattern =
2135 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2136 match reopt with
2137 | None -> None
2138 | Some re ->
2139 let rec fold accu n =
2140 if n = -1
2141 then accu
2142 else
2143 let (s, _, _) as o = outlines.(n) in
2144 let accu =
2145 if (try ignore (Str.search_forward re s 0); true
2146 with Not_found -> false)
2147 then (o :: accu)
2148 else accu
2150 fold accu (n-1)
2152 let matched = fold [] (Array.length outlines - 1) in
2153 if matched = [] then None else Some (Array.of_list matched)
2155 let search active pattern incr =
2156 let dosearch re =
2157 let rec loop n =
2158 if n = Array.length outlines || n = -1
2159 then None
2160 else
2161 let (s, _, _) = outlines.(n) in
2163 (try ignore (Str.search_forward re s 0); true
2164 with Not_found -> false)
2165 then Some n
2166 else loop (n + incr)
2168 loop active
2171 let re = Str.regexp_case_fold pattern in
2172 dosearch re
2173 with Failure s ->
2174 state.text <- s;
2175 None
2177 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2178 match key with
2179 | 27 -> (* escape *)
2180 if String.length qsearch = 0
2181 then (
2182 state.text <- "";
2183 state.mode <- oldmode;
2184 Glut.postRedisplay ();
2186 else (
2187 state.text <- "";
2188 state.mode <- Outline (
2189 allowdel, active, first, outlines, "", pan, oldmode
2191 Glut.postRedisplay ();
2194 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2195 let incr = if key = 18 then -1 else 1 in
2196 let active, first =
2197 match search (active + incr) qsearch incr with
2198 | None ->
2199 state.text <- qsearch ^ " [not found]";
2200 active, first
2201 | Some active ->
2202 state.text <- qsearch;
2203 active, firstof active
2205 state.mode <- Outline (
2206 allowdel, active, first, outlines, qsearch, pan, oldmode
2208 Glut.postRedisplay ();
2210 | 8 -> (* backspace *)
2211 let len = String.length qsearch in
2212 if len = 0
2213 then ()
2214 else (
2215 if len = 1
2216 then (
2217 state.text <- "";
2218 state.mode <- Outline (
2219 allowdel, active, first, outlines, "", pan, oldmode
2222 else
2223 let qsearch = String.sub qsearch 0 (len - 1) in
2224 let active, first =
2225 match search active qsearch ~-1 with
2226 | None ->
2227 state.text <- qsearch ^ " [not found]";
2228 active, first
2229 | Some active ->
2230 state.text <- qsearch;
2231 active, firstof active
2233 state.mode <- Outline (
2234 allowdel, active, first, outlines, qsearch, pan, oldmode
2237 Glut.postRedisplay ()
2239 | 13 -> (* enter *)
2240 if active < Array.length outlines
2241 then (
2242 let (_, _, anchor) = outlines.(active) in
2243 addnav ();
2244 gotoanchor anchor;
2246 state.text <- "";
2247 if allowdel then state.bookmarks <- Array.to_list outlines;
2248 state.mode <- oldmode;
2249 Glut.postRedisplay ();
2251 | _ when key >= 32 && key < 127 ->
2252 let pattern = addchar qsearch (Char.chr key) in
2253 let active, first =
2254 match search active pattern 1 with
2255 | None ->
2256 state.text <- pattern ^ " [not found]";
2257 active, first
2258 | Some active ->
2259 state.text <- pattern;
2260 active, firstof active
2262 state.mode <- Outline (
2263 allowdel, active, first, outlines, pattern, pan, oldmode
2265 Glut.postRedisplay ()
2267 | 14 when not allowdel -> (* ctrl-n *)
2268 if String.length qsearch > 0
2269 then (
2270 let optoutlines = narrow outlines qsearch in
2271 begin match optoutlines with
2272 | None -> state.text <- "can't narrow"
2273 | Some outlines ->
2274 state.mode <- Outline (
2275 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2277 match state.outlines with
2278 | Olist _ -> ()
2279 | Oarray a ->
2280 state.outlines <- Onarrow (qsearch, outlines, a)
2281 | Onarrow (_, _, b) ->
2282 state.outlines <- Onarrow (qsearch, outlines, b)
2283 end;
2285 Glut.postRedisplay ()
2287 | 21 when not allowdel -> (* ctrl-u *)
2288 let outline =
2289 match state.outlines with
2290 | Oarray a -> a
2291 | Olist l ->
2292 let a = Array.of_list (List.rev l) in
2293 state.outlines <- Oarray a;
2295 | Onarrow (_, _, b) ->
2296 state.outlines <- Oarray b;
2297 state.text <- "";
2300 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2301 Glut.postRedisplay ()
2303 | 12 -> (* ctrl-l *)
2304 state.mode <- Outline
2305 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2306 Glut.postRedisplay ()
2308 | 127 when allowdel -> (* delete *)
2309 let len = Array.length outlines - 1 in
2310 if len = 0
2311 then (
2312 state.mode <- View;
2313 state.bookmarks <- [];
2315 else (
2316 let bookmarks = Array.init len
2317 (fun i ->
2318 let i = if i >= active then i + 1 else i in
2319 outlines.(i)
2322 state.mode <-
2323 Outline (
2324 allowdel,
2325 min active (len-1),
2326 min first (len-1),
2327 bookmarks, qsearch,
2329 oldmode
2332 Glut.postRedisplay ()
2334 | _ -> dolog "unknown key %d" key
2337 let keyboard ~key ~x ~y =
2338 ignore x;
2339 ignore y;
2340 if key = 7 (* ctrl-g *)
2341 then
2342 wcmd "interrupt" []
2343 else
2344 match state.mode with
2345 | Outline outline -> outlinekeyboard key outline
2346 | Textentry textentry -> textentrykeyboard key textentry
2347 | Birdseye birdseye -> birdseyekeyboard key birdseye
2348 | View -> viewkeyboard key
2349 | Items items -> itemskeyboard key items
2352 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2353 match key with
2354 | Glut.KEY_UP -> upbirdseye beye
2355 | Glut.KEY_DOWN -> downbirdseye beye
2357 | Glut.KEY_PAGE_UP ->
2358 begin match state.layout with
2359 | l :: _ ->
2360 if l.pagey != 0
2361 then (
2362 state.mode <- Birdseye (
2363 conf, leftx, l.pageno, hooverpageno, anchor
2365 gotopage1 l.pageno 0;
2367 else (
2368 let layout = layout (state.y-conf.winh) conf.winh in
2369 match layout with
2370 | [] -> gotoy (clamp (-conf.winh))
2371 | l :: _ ->
2372 state.mode <- Birdseye (
2373 conf, leftx, l.pageno, hooverpageno, anchor
2375 gotopage1 l.pageno 0
2378 | [] -> gotoy (clamp (-conf.winh))
2379 end;
2381 | Glut.KEY_PAGE_DOWN ->
2382 begin match List.rev state.layout with
2383 | l :: _ ->
2384 let layout = layout (state.y + conf.winh) conf.winh in
2385 begin match layout with
2386 | [] ->
2387 let incr = l.pageh - l.pagevh in
2388 if incr = 0
2389 then (
2390 state.mode <-
2391 Birdseye (
2392 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2394 Glut.postRedisplay ();
2396 else gotoy (clamp (incr + conf.interpagespace*2));
2398 | l :: _ ->
2399 state.mode <-
2400 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2401 gotopage1 l.pageno 0;
2404 | [] -> gotoy (clamp conf.winh)
2405 end;
2407 | Glut.KEY_HOME ->
2408 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2409 gotopage1 0 0
2411 | Glut.KEY_END ->
2412 let pageno = state.pagecount - 1 in
2413 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2414 if not (pagevisible state.layout pageno)
2415 then
2416 let h =
2417 match List.rev state.pdims with
2418 | [] -> conf.winh
2419 | (_, _, h, _) :: _ -> h
2421 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2422 else Glut.postRedisplay ();
2423 | _ -> ()
2426 let setautoscrollspeed goingdown =
2427 let incr = max 1 (state.ascrollstep / 2) in
2428 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2429 state.ascrollstep <- astep;
2432 let special ~key ~x ~y =
2433 ignore x;
2434 ignore y;
2435 match state.mode with
2436 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2437 togglebirdseye ()
2439 | Birdseye vals ->
2440 birdseyespecial key vals
2442 | View when key = Glut.KEY_F1 ->
2443 enterhelpmode ()
2445 | View ->
2446 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2447 then setautoscrollspeed (key = Glut.KEY_DOWN)
2448 else
2449 let y =
2450 match key with
2451 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2452 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2453 | Glut.KEY_DOWN -> clamp conf.scrollstep
2454 | Glut.KEY_PAGE_UP ->
2455 if Glut.getModifiers () land Glut.active_ctrl != 0
2456 then
2457 match state.layout with
2458 | [] -> state.y
2459 | l :: _ -> state.y - l.pagey
2460 else
2461 clamp (-conf.winh)
2462 | Glut.KEY_PAGE_DOWN ->
2463 if Glut.getModifiers () land Glut.active_ctrl != 0
2464 then
2465 match List.rev state.layout with
2466 | [] -> state.y
2467 | l :: _ -> getpagey l.pageno
2468 else
2469 clamp conf.winh
2470 | Glut.KEY_HOME -> addnav (); 0
2471 | Glut.KEY_END ->
2472 addnav ();
2473 state.maxy - (if conf.maxhfit then conf.winh else 0)
2475 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2476 Glut.getModifiers () land Glut.active_alt != 0 ->
2477 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2479 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2480 state.x <- state.x - 10;
2481 state.y
2482 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2483 state.x <- state.x + 10;
2484 state.y
2486 | _ -> state.y
2488 gotoy_and_clear_text y
2490 | Textentry
2491 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2492 let s =
2493 match key with
2494 | Glut.KEY_UP -> action HCprev
2495 | Glut.KEY_DOWN -> action HCnext
2496 | Glut.KEY_HOME -> action HCfirst
2497 | Glut.KEY_END -> action HClast
2498 | _ -> state.text
2500 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2501 Glut.postRedisplay ()
2503 | Textentry _ -> ()
2505 | Items (active, first, items, qsearch, pan, oldmode) ->
2506 let maxrows = maxoutlinerows () in
2507 let itemcount = Array.length items in
2508 let hasaction = function
2509 | (_, _, Noaction) -> false
2510 | _ -> true
2512 let find start incr =
2513 let rec find i =
2514 if i = -1 || i = itemcount
2515 then -1
2516 else (
2517 if hasaction items.(i)
2518 then i
2519 else find (i + incr)
2522 find start
2524 let set active first =
2525 let first = max 0 (min first (itemcount - maxrows)) in
2526 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2528 let navigate incr =
2529 let isvisible first n = n >= first && n - first <= maxrows in
2530 let active, first =
2531 let incr1 = if incr > 0 then 1 else -1 in
2532 if isvisible first active
2533 then
2534 let next =
2535 let next = active + incr in
2536 let next =
2537 if next < 0 || next >= itemcount
2538 then -1
2539 else find next incr1
2541 if next = -1 || abs (active - next) > maxrows
2542 then -1
2543 else next
2545 if next = -1
2546 then
2547 let first = first + incr in
2548 let first = max 0 (min first (itemcount - 1)) in
2549 let next =
2550 let next = active + incr in
2551 let next = max 0 (min next (itemcount - 1)) in
2552 find next ~-incr1
2554 let active = if next = -1 then active else next in
2555 active, first
2556 else
2557 let first = min next first in
2558 next, first
2559 else
2560 let first = first + incr in
2561 let first = max 0 (min first (itemcount - 1)) in
2562 let active =
2563 let next = active + incr in
2564 let next = max 0 (min next (itemcount - 1)) in
2565 let next = find next incr1 in
2566 if next = -1 || abs (active - first) > maxrows
2567 then active
2568 else next
2570 active, first
2572 set active first;
2573 Glut.postRedisplay ()
2575 begin match key with
2576 | Glut.KEY_UP -> navigate ~-1
2577 | Glut.KEY_DOWN -> navigate 1
2578 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2579 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2581 | Glut.KEY_RIGHT ->
2582 state.mode <- Items (
2583 active, first, items, qsearch, min 0 (pan - 1), oldmode
2585 Glut.postRedisplay ()
2587 | Glut.KEY_LEFT ->
2588 state.mode <- Items (
2589 active, first, items, qsearch, min 0 (pan + 1), oldmode
2591 Glut.postRedisplay ()
2593 | Glut.KEY_HOME ->
2594 let active = find 0 1 in
2595 set active 0;
2596 Glut.postRedisplay ()
2598 | Glut.KEY_END ->
2599 let first = max 0 (itemcount - maxrows) in
2600 let active = find (itemcount - 1) ~-1 in
2601 set active first;
2602 Glut.postRedisplay ()
2604 | _ -> ()
2605 end;
2607 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2608 let maxrows = maxoutlinerows () in
2609 let calcfirst first active =
2610 if active > first
2611 then
2612 let rows = active - first in
2613 if rows > maxrows then active - maxrows else first
2614 else active
2616 let navigate incr =
2617 let active = active + incr in
2618 let active = max 0 (min active (Array.length outlines - 1)) in
2619 let first = calcfirst first active in
2620 state.mode <- Outline (
2621 allowdel, active, first, outlines, qsearch, pan, oldmode
2623 Glut.postRedisplay ()
2625 let updownlevel incr =
2626 let len = Array.length outlines in
2627 let (_, curlevel, _) = outlines.(active) in
2628 let rec flow i =
2629 if i = len then i-1 else if i = -1 then 0 else
2630 let (_, l, _) = outlines.(i) in
2631 if l != curlevel then i else flow (i+incr)
2633 let active = flow active in
2634 let first = calcfirst first active in
2635 state.mode <- Outline (
2636 allowdel, active, first, outlines, qsearch, pan, oldmode
2638 Glut.postRedisplay ()
2640 match key with
2641 | Glut.KEY_UP -> navigate ~-1
2642 | Glut.KEY_DOWN -> navigate 1
2643 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2644 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2646 | Glut.KEY_RIGHT ->
2647 if Glut.getModifiers () land Glut.active_ctrl != 0
2648 then (
2649 state.mode <- Outline (
2650 allowdel, active, first, outlines,
2651 qsearch, min 0 (pan + 1), oldmode
2653 Glut.postRedisplay ();
2655 else (
2656 if not allowdel
2657 then updownlevel 1
2660 | Glut.KEY_LEFT ->
2661 if Glut.getModifiers () land Glut.active_ctrl != 0
2662 then (
2663 state.mode <- Outline (
2664 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2666 Glut.postRedisplay ();
2668 else (
2669 if not allowdel
2670 then updownlevel ~-1
2673 | Glut.KEY_HOME ->
2674 state.mode <- Outline (
2675 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2677 Glut.postRedisplay ()
2679 | Glut.KEY_END ->
2680 let active = Array.length outlines - 1 in
2681 let first = max 0 (active - maxrows) in
2682 state.mode <- Outline (
2683 allowdel, active, first, outlines, qsearch, pan, oldmode
2685 Glut.postRedisplay ()
2687 | _ -> ()
2690 let drawplaceholder l =
2691 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2692 GlDraw.rect
2693 (float l.pagex, float l.pagedispy)
2694 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2696 let x = float (if margin < 0 then -margin else l.pagex)
2697 and y = float (l.pagedispy + 13) in
2698 let font = Glut.BITMAP_8_BY_13 in
2699 GlDraw.color (0.0, 0.0, 0.0);
2700 GlPix.raster_pos ~x ~y ();
2701 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2702 ("Loading " ^ string_of_int (l.pageno + 1));
2705 let now () = Unix.gettimeofday ();;
2707 let drawpage l =
2708 let color =
2709 match state.mode with
2710 | Textentry _ -> scalecolor 0.4
2711 | View | Outline _ | Items _ -> scalecolor 1.0
2712 | Birdseye (_, _, pageno, hooverpageno, _) ->
2713 if l.pageno = hooverpageno
2714 then scalecolor 0.9
2715 else (
2716 if l.pageno = pageno
2717 then scalecolor 1.0
2718 else scalecolor 0.8
2721 GlDraw.color color;
2722 begin match getopaque l.pageno with
2723 | Some (opaque, _) when validopaque opaque ->
2724 let a = now () in
2725 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2726 opaque;
2727 let b = now () in
2728 let d = b-.a in
2729 vlog "draw %d %f sec" l.pageno d;
2731 | _ ->
2732 drawplaceholder l;
2733 end;
2736 let scrollph y =
2737 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2738 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2739 let sh = float conf.winh /. sh in
2740 let sh = max sh (float conf.scrollh) in
2742 let percent =
2743 if state.y = state.maxy
2744 then 1.0
2745 else float y /. float maxy
2747 let position = (float conf.winh -. sh) *. percent in
2749 let position =
2750 if position +. sh > float conf.winh
2751 then float conf.winh -. sh
2752 else position
2754 position, sh;
2757 let scrollindicator () =
2758 GlDraw.color (0.64 , 0.64, 0.64);
2759 GlDraw.rect
2760 (float (conf.winw - state.scrollw), 0.)
2761 (float conf.winw, float conf.winh)
2763 GlDraw.color (0.0, 0.0, 0.0);
2765 let position, sh = scrollph state.y in
2766 GlDraw.rect
2767 (float (conf.winw - state.scrollw), position)
2768 (float conf.winw, position +. sh)
2772 let showsel margin =
2773 match state.mstate with
2774 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2777 | Msel ((x0, y0), (x1, y1)) ->
2778 let rec loop = function
2779 | l :: ls ->
2780 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2781 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2782 then
2783 match getopaque l.pageno with
2784 | Some (opaque, _) when validopaque opaque ->
2785 let oy = -l.pagey + l.pagedispy in
2786 seltext opaque
2787 (x0 - margin - state.x, y0,
2788 x1 - margin - state.x, y1) oy;
2790 | _ -> ()
2791 else loop ls
2792 | [] -> ()
2794 loop state.layout
2797 let showrects () =
2798 let panx = float state.x in
2799 Gl.enable `blend;
2800 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2801 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2802 List.iter
2803 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2804 List.iter (fun l ->
2805 if l.pageno = pageno
2806 then (
2807 let d = float (l.pagedispy - l.pagey) in
2808 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2809 GlDraw.begins `quads;
2811 GlDraw.vertex2 (x0+.panx, y0+.d);
2812 GlDraw.vertex2 (x1+.panx, y1+.d);
2813 GlDraw.vertex2 (x2+.panx, y2+.d);
2814 GlDraw.vertex2 (x3+.panx, y3+.d);
2816 GlDraw.ends ();
2818 ) state.layout
2819 ) state.rects
2821 Gl.disable `blend;
2824 let showstrings active first pan strings =
2825 Gl.enable `blend;
2826 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2827 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2828 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2829 Gl.disable `blend;
2831 GlDraw.color (1., 1., 1.);
2832 let font = Glut.BITMAP_9_BY_15 in
2833 let draw_string x y s =
2834 GlPix.raster_pos ~x ~y ();
2835 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2837 let rec loop row =
2838 if row = Array.length strings || (row - first) * 16 > conf.winh
2839 then ()
2840 else (
2841 let (s, level, _) = strings.(row) in
2842 let y = (row - first) * 16 in
2843 let x = 5 + 15*(max 0 (level+pan)) in
2844 if row = active
2845 then (
2846 Gl.enable `blend;
2847 GlDraw.polygon_mode `both `line;
2848 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2849 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2850 GlDraw.rect (0., float (y + 1))
2851 (float (conf.winw - 1), float (y + 18));
2852 GlDraw.polygon_mode `both `fill;
2853 Gl.disable `blend;
2854 GlDraw.color (1., 1., 1.);
2856 let draw_string s =
2857 let l = String.length s in
2858 if pan < 0
2859 then (
2860 let pan = pan * 2 in
2861 let pos = pan + level in
2862 let left = l + pos in
2863 if left > 0
2864 then
2865 let s =
2866 if left > l
2867 then s
2868 else String.sub s (-pos) left
2870 draw_string (float x) (float (y + 16)) s
2872 else
2873 draw_string (float (x + pan*15)) (float (y + 16)) s
2875 draw_string s;
2876 loop (row+1)
2879 loop first
2882 let showoutline (_, active, first, outlines, _, pan, _) =
2883 showstrings active first pan outlines;
2886 let showitems (active, first, items, _, pan, _) =
2887 showstrings active first pan items;
2890 let display () =
2891 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2892 GlDraw.viewport margin 0 state.w conf.winh;
2893 pagematrix ();
2894 GlClear.color (scalecolor2 conf.bgcolor);
2895 GlClear.clear [`color];
2896 if conf.zoom > 1.0
2897 then (
2898 Gl.enable `scissor_test;
2899 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2901 List.iter drawpage state.layout;
2902 if conf.zoom > 1.0
2903 then
2904 Gl.disable `scissor_test
2906 if state.x != 0
2907 then (
2908 let x = -.float state.x in
2909 GlMat.translate ~x ();
2911 showrects ();
2912 showsel margin;
2913 GlDraw.viewport 0 0 conf.winw conf.winh;
2914 winmatrix ();
2915 scrollindicator ();
2916 begin match state.mode with
2917 | Items items -> showitems items
2918 | Outline outline -> showoutline outline
2919 | _ -> ()
2920 end;
2921 enttext ();
2922 Glut.swapBuffers ();
2925 let getunder x y =
2926 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2927 let x = x - margin - state.x in
2928 let rec f = function
2929 | l :: rest ->
2930 begin match getopaque l.pageno with
2931 | Some (opaque, _) when validopaque opaque ->
2932 let y = y - l.pagedispy in
2933 if y > 0
2934 then
2935 let y = l.pagey + y in
2936 let x = x - l.pagex in
2937 match whatsunder opaque x y with
2938 | Unone -> f rest
2939 | under -> under
2940 else
2941 f rest
2942 | _ ->
2943 f rest
2945 | [] -> Unone
2947 f state.layout
2950 let viewmouse button bstate x y =
2951 match button with
2952 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2953 if Glut.getModifiers () land Glut.active_ctrl != 0
2954 then (
2955 match state.mstate with
2956 | Mzoom (oldn, i) ->
2957 if oldn = n
2958 then (
2959 if i = 2
2960 then
2961 let incr =
2962 match n with
2963 | 4 ->
2964 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2965 | _ ->
2966 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2968 let zoom = conf.zoom +. incr in
2969 setzoom zoom;
2970 state.mstate <- Mzoom (n, 0);
2971 else
2972 state.mstate <- Mzoom (n, i+1);
2974 else state.mstate <- Mzoom (n, 0)
2976 | _ -> state.mstate <- Mzoom (n, 0)
2978 else (
2979 if state.ascrollstep > 0
2980 then
2981 setautoscrollspeed (n=4)
2982 else
2983 let incr =
2984 if n = 3
2985 then -conf.scrollstep
2986 else conf.scrollstep
2988 let incr = incr * 2 in
2989 let y = clamp incr in
2990 gotoy_and_clear_text y
2993 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2994 if bstate = Glut.DOWN
2995 then (
2996 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2997 state.mstate <- Mpan (x, y)
2999 else
3000 state.mstate <- Mnone
3002 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3003 if bstate = Glut.DOWN
3004 then
3005 let position, sh = scrollph state.y in
3006 if y > truncate position && y < truncate (position +. sh)
3007 then
3008 state.mstate <- Mscroll
3009 else
3010 let percent = float y /. float conf.winh in
3011 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3012 gotoy desty;
3013 state.mstate <- Mscroll
3014 else
3015 state.mstate <- Mnone
3017 | Glut.LEFT_BUTTON ->
3018 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3019 begin match dest with
3020 | Ulinkgoto (pageno, top) ->
3021 if pageno >= 0
3022 then (
3023 addnav ();
3024 gotopage1 pageno top;
3027 | Ulinkuri s ->
3028 print_endline s
3030 | Unone when bstate = Glut.DOWN ->
3031 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3032 state.mstate <- Mpan (x, y);
3034 | Unone | Utext _ ->
3035 if bstate = Glut.DOWN
3036 then (
3037 if conf.angle mod 360 = 0
3038 then (
3039 state.mstate <- Msel ((x, y), (x, y));
3040 Glut.postRedisplay ()
3043 else (
3044 match state.mstate with
3045 | Mnone -> ()
3047 | Mzoom _ | Mscroll ->
3048 state.mstate <- Mnone
3050 | Mpan _ ->
3051 Glut.setCursor Glut.CURSOR_INHERIT;
3052 state.mstate <- Mnone
3054 | Msel ((_, y0), (_, y1)) ->
3055 let f l =
3056 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3057 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3058 then
3059 match getopaque l.pageno with
3060 | Some (opaque, _) when validopaque opaque ->
3061 copysel opaque
3062 | _ -> ()
3064 List.iter f state.layout;
3065 copysel ""; (* ugly *)
3066 Glut.setCursor Glut.CURSOR_INHERIT;
3067 state.mstate <- Mnone;
3071 | _ -> ()
3074 let birdseyemouse button bstate x y
3075 (conf, leftx, _, hooverpageno, anchor) =
3076 match button with
3077 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3078 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3079 let rec loop = function
3080 | [] -> ()
3081 | l :: rest ->
3082 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3083 && x > margin && x < margin + l.pagew
3084 then (
3085 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3087 else loop rest
3089 loop state.layout
3090 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3091 | _ -> ()
3094 let mouse bstate button x y =
3095 match state.mode with
3096 | View -> viewmouse button bstate x y
3097 | Birdseye beye -> birdseyemouse button bstate x y beye
3098 | Textentry _ | Outline _ | Items _ -> ()
3101 let mouse ~button ~state ~x ~y = mouse state button x y;;
3103 let motion ~x ~y =
3104 match state.mode with
3105 | Outline _ -> ()
3106 | _ ->
3107 match state.mstate with
3108 | Mzoom _ | Mnone -> ()
3110 | Mpan (x0, y0) ->
3111 let dx = x - x0
3112 and dy = y0 - y in
3113 state.mstate <- Mpan (x, y);
3114 if conf.zoom > 1.0 then state.x <- state.x + dx;
3115 let y = clamp dy in
3116 gotoy_and_clear_text y
3118 | Msel (a, _) ->
3119 state.mstate <- Msel (a, (x, y));
3120 Glut.postRedisplay ()
3122 | Mscroll ->
3123 let y = min conf.winh (max 0 y) in
3124 let percent = float y /. float conf.winh in
3125 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3126 gotoy_and_clear_text y
3129 let pmotion ~x ~y =
3130 match state.mode with
3131 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3132 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3133 let rec loop = function
3134 | [] ->
3135 if hooverpageno != -1
3136 then (
3137 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3138 Glut.postRedisplay ();
3140 | l :: rest ->
3141 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3142 && x > margin && x < margin + l.pagew
3143 then (
3144 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3145 Glut.postRedisplay ();
3147 else loop rest
3149 loop state.layout
3151 | Outline _ | Items _ | Textentry _ -> ()
3152 | View ->
3153 match state.mstate with
3154 | Mnone ->
3155 begin match getunder x y with
3156 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3157 | Ulinkuri uri ->
3158 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3159 Glut.setCursor Glut.CURSOR_INFO
3160 | Ulinkgoto (page, _) ->
3161 if conf.underinfo
3162 then showtext 'p' ("age: " ^ string_of_int page);
3163 Glut.setCursor Glut.CURSOR_INFO
3164 | Utext s ->
3165 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3166 Glut.setCursor Glut.CURSOR_TEXT
3169 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3174 module State =
3175 struct
3176 open Parser
3178 let home =
3180 match Sys.os_type with
3181 | "Win32" -> Sys.getenv "HOMEPATH"
3182 | _ -> Sys.getenv "HOME"
3183 with exn ->
3184 prerr_endline
3185 ("Can not determine home directory location: " ^
3186 Printexc.to_string exn);
3190 let config_of c attrs =
3191 let apply c k v =
3193 match k with
3194 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3195 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3196 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3197 | "preload" -> { c with preload = bool_of_string v }
3198 | "page-bias" -> { c with pagebias = int_of_string v }
3199 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3200 | "auto-scroll-step" ->
3201 { c with autoscrollstep = max 0 (int_of_string v) }
3202 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3203 | "crop-hack" -> { c with crophack = bool_of_string v }
3204 | "throttle" -> { c with showall = bool_of_string v }
3205 | "highlight-links" -> { c with hlinks = bool_of_string v }
3206 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3207 | "vertical-margin" ->
3208 { c with interpagespace = max 0 (int_of_string v) }
3209 | "zoom" ->
3210 let zoom = float_of_string v /. 100. in
3211 let zoom = max 0.01 (min 2.2 zoom) in
3212 { c with zoom = zoom }
3213 | "presentation" -> { c with presentation = bool_of_string v }
3214 | "rotation-angle" -> { c with angle = int_of_string v }
3215 | "width" -> { c with winw = max 20 (int_of_string v) }
3216 | "height" -> { c with winh = max 20 (int_of_string v) }
3217 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3218 | "proportional-display" -> { c with proportional = bool_of_string v }
3219 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3220 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3221 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3222 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3223 | "persistent-location" -> { c with jumpback = bool_of_string v }
3224 | "background-color" -> { c with bgcolor = color_of_string v }
3225 | "scrollbar-in-presentation" ->
3226 { c with scrollbarinpm = bool_of_string v }
3227 | _ -> c
3228 with exn ->
3229 prerr_endline ("Error processing attribute (`" ^
3230 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3233 let rec fold c = function
3234 | [] -> c
3235 | (k, v) :: rest ->
3236 let c = apply c k v in
3237 fold c rest
3239 fold c attrs;
3242 let fromstring f pos n v d =
3243 try f v
3244 with exn ->
3245 dolog "Error processing attribute (%S=%S) at %d\n%s"
3246 n v pos (Printexc.to_string exn)
3251 let bookmark_of attrs =
3252 let rec fold title page rely = function
3253 | ("title", v) :: rest -> fold v page rely rest
3254 | ("page", v) :: rest -> fold title v rely rest
3255 | ("rely", v) :: rest -> fold title page v rest
3256 | _ :: rest -> fold title page rely rest
3257 | [] -> title, page, rely
3259 fold "invalid" "0" "0" attrs
3262 let doc_of attrs =
3263 let rec fold path page rely pan = function
3264 | ("path", v) :: rest -> fold v page rely pan rest
3265 | ("page", v) :: rest -> fold path v rely pan rest
3266 | ("rely", v) :: rest -> fold path page v pan rest
3267 | ("pan", v) :: rest -> fold path page rely v rest
3268 | _ :: rest -> fold path page rely pan rest
3269 | [] -> path, page, rely, pan
3271 fold "" "0" "0" "0" attrs
3274 let setconf dst src =
3275 dst.scrollbw <- src.scrollbw;
3276 dst.scrollh <- src.scrollh;
3277 dst.icase <- src.icase;
3278 dst.preload <- src.preload;
3279 dst.pagebias <- src.pagebias;
3280 dst.verbose <- src.verbose;
3281 dst.scrollstep <- src.scrollstep;
3282 dst.maxhfit <- src.maxhfit;
3283 dst.crophack <- src.crophack;
3284 dst.autoscrollstep <- src.autoscrollstep;
3285 dst.showall <- src.showall;
3286 dst.hlinks <- src.hlinks;
3287 dst.underinfo <- src.underinfo;
3288 dst.interpagespace <- src.interpagespace;
3289 dst.zoom <- src.zoom;
3290 dst.presentation <- src.presentation;
3291 dst.angle <- src.angle;
3292 dst.winw <- src.winw;
3293 dst.winh <- src.winh;
3294 dst.savebmarks <- src.savebmarks;
3295 dst.memlimit <- src.memlimit;
3296 dst.proportional <- src.proportional;
3297 dst.texcount <- src.texcount;
3298 dst.sliceheight <- src.sliceheight;
3299 dst.thumbw <- src.thumbw;
3300 dst.jumpback <- src.jumpback;
3301 dst.bgcolor <- src.bgcolor;
3302 dst.scrollbarinpm <- src.scrollbarinpm;
3305 let unent s =
3306 let l = String.length s in
3307 let b = Buffer.create l in
3308 unent b s 0 l;
3309 Buffer.contents b;
3312 let get s =
3313 let h = Hashtbl.create 10 in
3314 let dc = { defconf with angle = defconf.angle } in
3315 let rec toplevel v t spos _ =
3316 match t with
3317 | Vdata | Vcdata | Vend -> v
3318 | Vopen ("llppconfig", _, closed) ->
3319 if closed
3320 then v
3321 else { v with f = llppconfig }
3322 | Vopen _ ->
3323 error "unexpected subelement at top level" s spos
3324 | Vclose _ -> error "unexpected close at top level" s spos
3326 and llppconfig v t spos _ =
3327 match t with
3328 | Vdata | Vcdata | Vend -> v
3329 | Vopen ("defaults", attrs, closed) ->
3330 let c = config_of dc attrs in
3331 setconf dc c;
3332 if closed
3333 then v
3334 else { v with f = skip "defaults" (fun () -> v) }
3336 | Vopen ("doc", attrs, closed) ->
3337 let pathent, spage, srely, span = doc_of attrs in
3338 let path = unent pathent
3339 and pageno = fromstring int_of_string spos "page" spage 0
3340 and rely = fromstring float_of_string spos "rely" srely 0.0
3341 and pan = fromstring int_of_string spos "pan" span 0 in
3342 let c = config_of dc attrs in
3343 let anchor = (pageno, rely) in
3344 if closed
3345 then (Hashtbl.add h path (c, [], pan, anchor); v)
3346 else { v with f = doc path pan anchor c [] }
3348 | Vopen _ ->
3349 error "unexpected subelement in llppconfig" s spos
3351 | Vclose "llppconfig" -> { v with f = toplevel }
3352 | Vclose _ -> error "unexpected close in llppconfig" s spos
3354 and doc path pan anchor c bookmarks v t spos _ =
3355 match t with
3356 | Vdata | Vcdata -> v
3357 | Vend -> error "unexpected end of input in doc" s spos
3358 | Vopen ("bookmarks", _, closed) ->
3359 if closed
3360 then v
3361 else { v with f = pbookmarks path pan anchor c bookmarks }
3363 | Vopen (_, _, _) ->
3364 error "unexpected subelement in doc" s spos
3366 | Vclose "doc" ->
3367 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3368 { v with f = llppconfig }
3370 | Vclose _ -> error "unexpected close in doc" s spos
3372 and pbookmarks path pan anchor c bookmarks v t spos _ =
3373 match t with
3374 | Vdata | Vcdata -> v
3375 | Vend -> error "unexpected end of input in bookmarks" s spos
3376 | Vopen ("item", attrs, closed) ->
3377 let titleent, spage, srely = bookmark_of attrs in
3378 let page = fromstring int_of_string spos "page" spage 0
3379 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3380 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3381 if closed
3382 then { v with f = pbookmarks path pan anchor c bookmarks }
3383 else
3384 let f () = v in
3385 { v with f = skip "item" f }
3387 | Vopen _ ->
3388 error "unexpected subelement in bookmarks" s spos
3390 | Vclose "bookmarks" ->
3391 { v with f = doc path pan anchor c bookmarks }
3393 | Vclose _ -> error "unexpected close in bookmarks" s spos
3395 and skip tag f v t spos _ =
3396 match t with
3397 | Vdata | Vcdata -> v
3398 | Vend ->
3399 error ("unexpected end of input in skipped " ^ tag) s spos
3400 | Vopen (tag', _, closed) ->
3401 if closed
3402 then v
3403 else
3404 let f' () = { v with f = skip tag f } in
3405 { v with f = skip tag' f' }
3406 | Vclose ctag ->
3407 if tag = ctag
3408 then f ()
3409 else error ("unexpected close in skipped " ^ tag) s spos
3412 parse { f = toplevel; accu = () } s;
3413 h, dc;
3416 let do_load f ic =
3418 let len = in_channel_length ic in
3419 let s = String.create len in
3420 really_input ic s 0 len;
3421 f s;
3422 with
3423 | Parse_error (msg, s, pos) ->
3424 let subs = subs s pos in
3425 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3426 failwith ("parse error: " ^ s)
3428 | exn ->
3429 failwith ("config load error: " ^ Printexc.to_string exn)
3432 let path =
3433 let dir =
3435 let dir = Filename.concat home ".config" in
3436 if Sys.is_directory dir then dir else home
3437 with _ -> home
3439 Filename.concat dir "llpp.conf"
3442 let load1 f =
3443 if Sys.file_exists path
3444 then
3445 match
3446 (try Some (open_in_bin path)
3447 with exn ->
3448 prerr_endline
3449 ("Error opening configuation file `" ^ path ^ "': " ^
3450 Printexc.to_string exn);
3451 None
3453 with
3454 | Some ic ->
3455 begin try
3456 f (do_load get ic)
3457 with exn ->
3458 prerr_endline
3459 ("Error loading configuation from `" ^ path ^ "': " ^
3460 Printexc.to_string exn);
3461 end;
3462 close_in ic;
3464 | None -> ()
3465 else
3466 f (Hashtbl.create 0, defconf)
3469 let load () =
3470 let f (h, dc) =
3471 let pc, pb, px, pa =
3473 Hashtbl.find h (Filename.basename state.path)
3474 with Not_found -> dc, [], 0, (0, 0.0)
3476 setconf defconf dc;
3477 setconf conf pc;
3478 state.bookmarks <- pb;
3479 state.x <- px;
3480 state.scrollw <- conf.scrollbw;
3481 if conf.jumpback
3482 then state.anchor <- pa;
3483 cbput state.hists.nav pa;
3485 load1 f
3488 let add_attrs bb always dc c =
3489 let ob s a b =
3490 if always || a != b
3491 then Printf.bprintf bb "\n %s='%b'" s a
3492 and oi s a b =
3493 if always || a != b
3494 then Printf.bprintf bb "\n %s='%d'" s a
3495 and oz s a b =
3496 if always || a <> b
3497 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3498 and oc s a b =
3499 if always || a <> b
3500 then
3501 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3503 let w, h =
3504 if always
3505 then dc.winw, dc.winh
3506 else
3507 match state.fullscreen with
3508 | Some wh -> wh
3509 | None -> c.winw, c.winh
3511 let zoom, presentation, interpagespace, showall=
3512 if always
3513 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3514 else
3515 match state.mode with
3516 | Birdseye (bc, _, _, _, _) ->
3517 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3518 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3520 oi "width" w dc.winw;
3521 oi "height" h dc.winh;
3522 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3523 oi "scroll-handle-height" c.scrollh dc.scrollh;
3524 ob "case-insensitive-search" c.icase dc.icase;
3525 ob "preload" c.preload dc.preload;
3526 oi "page-bias" c.pagebias dc.pagebias;
3527 oi "scroll-step" c.scrollstep dc.scrollstep;
3528 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3529 ob "max-height-fit" c.maxhfit dc.maxhfit;
3530 ob "crop-hack" c.crophack dc.crophack;
3531 ob "throttle" showall dc.showall;
3532 ob "highlight-links" c.hlinks dc.hlinks;
3533 ob "under-cursor-info" c.underinfo dc.underinfo;
3534 oi "vertical-margin" interpagespace dc.interpagespace;
3535 oz "zoom" zoom dc.zoom;
3536 ob "presentation" presentation dc.presentation;
3537 oi "rotation-angle" c.angle dc.angle;
3538 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3539 ob "proportional-display" c.proportional dc.proportional;
3540 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3541 oi "texcount" c.texcount dc.texcount;
3542 oi "slice-height" c.sliceheight dc.sliceheight;
3543 oi "thumbnail-width" c.thumbw dc.thumbw;
3544 ob "persistent-location" c.jumpback dc.jumpback;
3545 oc "background-color" c.bgcolor dc.bgcolor;
3546 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3549 let save () =
3550 let bb = Buffer.create 32768 in
3551 let f (h, dc) =
3552 let dc = if conf.bedefault then conf else dc in
3553 Buffer.add_string bb "<llppconfig>\n<defaults ";
3554 add_attrs bb true dc dc;
3555 Buffer.add_string bb "/>\n";
3557 let adddoc path pan anchor c bookmarks =
3558 if bookmarks == [] && c = dc && anchor = emptyanchor
3559 then ()
3560 else (
3561 Printf.bprintf bb "<doc path='%s'"
3562 (enent path 0 (String.length path));
3564 if anchor <> emptyanchor
3565 then (
3566 let n, y = anchor in
3567 Printf.bprintf bb " page='%d'" n;
3568 if y > 1e-6
3569 then
3570 Printf.bprintf bb " rely='%f'" y
3574 if pan != 0
3575 then Printf.bprintf bb " pan='%d'" pan;
3577 add_attrs bb false dc c;
3579 begin match bookmarks with
3580 | [] -> Buffer.add_string bb "/>\n"
3581 | _ ->
3582 Buffer.add_string bb ">\n<bookmarks>\n";
3583 List.iter (fun (title, _level, (page, rely)) ->
3584 Printf.bprintf bb
3585 "<item title='%s' page='%d'"
3586 (enent title 0 (String.length title))
3587 page
3589 if rely > 1e-6
3590 then
3591 Printf.bprintf bb " rely='%f'" rely
3593 Buffer.add_string bb "/>\n";
3594 ) bookmarks;
3595 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3596 end;
3600 let pan =
3601 match state.mode with
3602 | Birdseye (_, pan, _, _, _) -> pan
3603 | _ -> state.x
3605 let basename = Filename.basename state.path in
3606 adddoc basename pan (getanchor ())
3607 { conf with
3608 autoscrollstep =
3609 if state.ascrollstep > 0
3610 then state.ascrollstep
3611 else conf.autoscrollstep }
3612 (if conf.savebmarks then state.bookmarks else []);
3614 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3615 if basename <> path
3616 then adddoc path x y c bookmarks
3617 ) h;
3618 Buffer.add_string bb "</llppconfig>";
3620 load1 f;
3621 if Buffer.length bb > 0
3622 then
3624 let tmp = path ^ ".tmp" in
3625 let oc = open_out_bin tmp in
3626 Buffer.output_buffer oc bb;
3627 close_out oc;
3628 Sys.rename tmp path;
3629 with exn ->
3630 prerr_endline
3631 ("error while saving configuration: " ^ Printexc.to_string exn)
3633 end;;
3635 let () =
3636 Arg.parse
3637 (Arg.align
3638 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3639 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3640 " Print version and exit")]
3642 (fun s -> state.path <- s)
3643 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3645 if String.length state.path = 0
3646 then (prerr_endline "file name missing"; exit 1);
3648 State.load ();
3650 let _ = Glut.init Sys.argv in
3651 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3652 let () = Glut.initWindowSize conf.winw conf.winh in
3653 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3655 let csock, ssock =
3656 if Sys.os_type = "Unix"
3657 then
3658 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3659 else
3660 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3661 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3662 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3663 Unix.bind sock addr;
3664 Unix.listen sock 1;
3665 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3666 Unix.connect csock addr;
3667 let ssock, _ = Unix.accept sock in
3668 Unix.close sock;
3669 let opts sock =
3670 Unix.setsockopt sock Unix.TCP_NODELAY true;
3671 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3673 opts ssock;
3674 opts csock;
3675 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3676 ssock, csock
3679 let () = Glut.displayFunc display in
3680 let () = Glut.reshapeFunc reshape in
3681 let () = Glut.keyboardFunc keyboard in
3682 let () = Glut.specialFunc special in
3683 let () = Glut.idleFunc (Some idle) in
3684 let () = Glut.mouseFunc mouse in
3685 let () = Glut.motionFunc motion in
3686 let () = Glut.passiveMotionFunc pmotion in
3688 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3689 state.csock <- csock;
3690 state.ssock <- ssock;
3691 state.text <- "Opening " ^ state.path;
3692 writeopen state.path state.password;
3694 at_exit State.save;
3696 let rec handlelablglutbug () =
3698 Glut.mainLoop ();
3699 with Glut.BadEnum "key in special_of_int" ->
3700 showtext '!' " LablGlut bug: special key not recognized";
3701 handlelablglutbug ()
3703 handlelablglutbug ();