Do not fiddle with atexit
[llpp.git] / main.ml
blobd5b7c087d4045e0e0537b603ef32067802153b43
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 exception Quit;;
12 type params =
13 angle * proportional * texcount * sliceheight * blockwidth * fontpath
14 and pageno = int
15 and width = int
16 and height = int
17 and leftx = int
18 and opaque = string
19 and recttype = int
20 and pixmapsize = int
21 and angle = int
22 and proportional = bool
23 and interpagespace = int
24 and texcount = int
25 and sliceheight = int
26 and blockwidth = int
27 and gen = int
28 and top = float
29 and fontpath = string
32 external init : Unix.file_descr -> params -> unit = "ml_init";;
33 external draw : (int * int * int * bool) -> string -> unit = "ml_draw";;
34 external seltext : string -> (int * int * int * int) -> int -> unit =
35 "ml_seltext";;
36 external copysel : string -> unit = "ml_copysel";;
37 external getpdimrect : int -> float array = "ml_getpdimrect";;
38 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
39 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
40 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
41 external measurestr : int -> string -> float = "ml_measure_string";;
42 external getmaxw : unit -> float = "ml_getmaxw";;
44 type mpos = int * int
45 and mstate =
46 | Msel of (mpos * mpos)
47 | Mpan of mpos
48 | Mscroll
49 | Mzoom of (int * int)
50 | Mnone
53 type textentry = string * string * onhist option * onkey * ondone
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel)
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
66 type 'a circbuf =
67 { store : 'a array
68 ; mutable rc : int
69 ; mutable wc : int
70 ; mutable len : int
74 let cbnew n v =
75 { store = Array.create n v
76 ; rc = 0
77 ; wc = 0
78 ; len = 0
82 let cbcap b = Array.length b.store;;
84 let cbput b v =
85 let cap = cbcap b in
86 b.store.(b.wc) <- v;
87 b.wc <- (b.wc + 1) mod cap;
88 b.rc <- b.wc;
89 b.len <- min (b.len + 1) cap;
92 let cbempty b = b.len = 0;;
94 let cbgetg b circular dir =
95 if cbempty b
96 then b.store.(0)
97 else
98 let rc = b.rc + dir in
99 let rc =
100 if circular
101 then (
102 if rc = -1
103 then b.len-1
104 else (
105 if rc = b.len
106 then 0
107 else rc
110 else max 0 (min rc (b.len-1))
112 b.rc <- rc;
113 b.store.(rc);
116 let cbget b = cbgetg b false;;
117 let cbgetc b = cbgetg b true;;
119 let cbpeek b =
120 let rc = b.wc - b.len in
121 let rc = if rc < 0 then cbcap b + rc else rc in
122 b.store.(rc);
125 let cbdecr b = b.len <- b.len - 1;;
127 type layout =
128 { pageno : int
129 ; pagedimno : int
130 ; pagew : int
131 ; pageh : int
132 ; pagedispy : int
133 ; pagey : int
134 ; pagevh : int
135 ; pagex : int
139 type conf =
140 { mutable scrollbw : int
141 ; mutable scrollh : int
142 ; mutable icase : bool
143 ; mutable preload : bool
144 ; mutable pagebias : int
145 ; mutable verbose : bool
146 ; mutable scrollstep : int
147 ; mutable maxhfit : bool
148 ; mutable crophack : bool
149 ; mutable autoscrollstep : int
150 ; mutable showall : bool
151 ; mutable hlinks : bool
152 ; mutable underinfo : bool
153 ; mutable interpagespace : interpagespace
154 ; mutable zoom : float
155 ; mutable presentation : bool
156 ; mutable angle : angle
157 ; mutable winw : int
158 ; mutable winh : int
159 ; mutable savebmarks : bool
160 ; mutable proportional : proportional
161 ; mutable memlimit : int
162 ; mutable texcount : texcount
163 ; mutable sliceheight : sliceheight
164 ; mutable blockwidth : blockwidth
165 ; mutable thumbw : width
166 ; mutable jumpback : bool
167 ; mutable bgcolor : float * float * float
168 ; mutable bedefault : bool
169 ; mutable scrollbarinpm : bool
170 ; mutable uifont : string
174 type anchor = pageno * top;;
176 type outline = string * int * anchor
177 and outlines =
178 | Oarray of outline array
179 | Olist of outline list
180 | Onarrow of string * outline array * outline array
183 type rect = float * float * float * float * float * float * float * float;;
185 type pagemapkey = pageno * width * angle * proportional * gen;;
187 let emptyanchor = (0, 0.0);;
189 type mode =
190 | Birdseye of (conf * leftx * pageno * pageno * anchor)
191 | Outline of (bool * int * int * outline array * string * int * mode)
192 | Items of (int * int * item array * string * int * mode)
193 | Textentry of (textentry * onleave)
194 | View
195 and onleave = leavetextentrystatus -> unit
196 and leavetextentrystatus = | Cancel | Confirm
197 and item = string * int * action
198 and action =
199 | Noaction
200 | Action of (int -> int -> string -> int -> mode)
203 let isbirdseye = function Birdseye _ -> true | _ -> false;;
204 let istextentry = function Textentry _ -> true | _ -> false;;
206 type state =
207 { mutable csock : Unix.file_descr
208 ; mutable ssock : Unix.file_descr
209 ; mutable w : int
210 ; mutable x : int
211 ; mutable y : int
212 ; mutable scrollw : int
213 ; mutable anchor : anchor
214 ; mutable maxy : int
215 ; mutable layout : layout list
216 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
217 ; mutable pdims : (pageno * width * height * leftx) list
218 ; mutable pagecount : int
219 ; pagecache : string circbuf
220 ; mutable rendering : bool
221 ; mutable mstate : mstate
222 ; mutable searchpattern : string
223 ; mutable rects : (pageno * recttype * rect) list
224 ; mutable rects1 : (pageno * recttype * rect) list
225 ; mutable text : string
226 ; mutable fullscreen : (width * height) option
227 ; mutable mode : mode
228 ; mutable outlines : outlines
229 ; mutable bookmarks : outline list
230 ; mutable path : string
231 ; mutable password : string
232 ; mutable invalidated : int
233 ; mutable colorscale : float
234 ; mutable memused : int
235 ; mutable gen : gen
236 ; mutable throttle : layout list option
237 ; mutable autoscroll :int option
238 ; mutable help : item array
239 ; mutable docinfo : (int * string) list
240 ; mutable deadline : float
241 ; hists : hists
243 and hists =
244 { pat : string circbuf
245 ; pag : string circbuf
246 ; nav : anchor circbuf
250 let defconf =
251 { scrollbw = 7
252 ; scrollh = 12
253 ; icase = true
254 ; preload = true
255 ; pagebias = 0
256 ; verbose = false
257 ; scrollstep = 24
258 ; maxhfit = true
259 ; crophack = false
260 ; autoscrollstep = 2
261 ; showall = false
262 ; hlinks = false
263 ; underinfo = false
264 ; interpagespace = 2
265 ; zoom = 1.0
266 ; presentation = false
267 ; angle = 0
268 ; winw = 900
269 ; winh = 900
270 ; savebmarks = true
271 ; proportional = true
272 ; memlimit = 32*1024*1024
273 ; texcount = 256
274 ; sliceheight = 24
275 ; blockwidth = 2048
276 ; thumbw = 76
277 ; jumpback = false
278 ; bgcolor = (0.5, 0.5, 0.5)
279 ; bedefault = false
280 ; scrollbarinpm = true
281 ; uifont = ""
285 let conf = { defconf with angle = defconf.angle };;
287 let makehelp () =
288 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
289 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
292 let state =
293 { csock = Unix.stdin
294 ; ssock = Unix.stdin
295 ; x = 0
296 ; y = 0
297 ; w = 0
298 ; scrollw = 0
299 ; anchor = emptyanchor
300 ; layout = []
301 ; maxy = max_int
302 ; pagemap = Hashtbl.create 10
303 ; pagecache = cbnew 100 ""
304 ; pdims = []
305 ; pagecount = 0
306 ; rendering = false
307 ; mstate = Mnone
308 ; rects = []
309 ; rects1 = []
310 ; text = ""
311 ; mode = View
312 ; fullscreen = None
313 ; searchpattern = ""
314 ; outlines = Olist []
315 ; bookmarks = []
316 ; path = ""
317 ; password = ""
318 ; invalidated = 0
319 ; hists =
320 { nav = cbnew 100 (0, 0.0)
321 ; pat = cbnew 20 ""
322 ; pag = cbnew 10 ""
324 ; colorscale = 1.0
325 ; memused = 0
326 ; gen = 0
327 ; throttle = None
328 ; autoscroll = None
329 ; help = makehelp ()
330 ; docinfo = []
331 ; deadline = nan
335 let vlog fmt =
336 if conf.verbose
337 then
338 Printf.kprintf prerr_endline fmt
339 else
340 Printf.kprintf ignore fmt
343 let writecmd fd s =
344 let len = String.length s in
345 let n = 4 + len in
346 let b = Buffer.create n in
347 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
348 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
349 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
350 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
351 Buffer.add_string b s;
352 let s' = Buffer.contents b in
353 let n' = Unix.write fd s' 0 n in
354 if n' != n then failwith "write failed";
357 let readcmd fd =
358 let s = "xxxx" in
359 let n = Unix.read fd s 0 4 in
360 if n != 4 then failwith "incomplete read(len)";
361 let len = 0
362 lor (Char.code s.[0] lsl 24)
363 lor (Char.code s.[1] lsl 16)
364 lor (Char.code s.[2] lsl 8)
365 lor (Char.code s.[3] lsl 0)
367 let s = String.create len in
368 let n = Unix.read fd s 0 len in
369 if n != len then failwith "incomplete read(data)";
373 let makecmd s l =
374 let b = Buffer.create 10 in
375 Buffer.add_string b s;
376 let rec combine = function
377 | [] -> b
378 | x :: xs ->
379 Buffer.add_char b ' ';
380 let s =
381 match x with
382 | `b b -> if b then "1" else "0"
383 | `s s -> s
384 | `i i -> string_of_int i
385 | `f f -> string_of_float f
386 | `I f -> string_of_int (truncate f)
388 Buffer.add_string b s;
389 combine xs;
391 combine l;
394 let wcmd s l =
395 let cmd = Buffer.contents (makecmd s l) in
396 writecmd state.csock cmd;
399 let calcips h =
400 if conf.presentation
401 then
402 let d = conf.winh - h in
403 max 0 ((d + 1) / 2)
404 else
405 conf.interpagespace
408 let calcheight () =
409 let rec f pn ph pi fh l =
410 match l with
411 | (n, _, h, _) :: rest ->
412 let ips = calcips h in
413 let fh =
414 if conf.presentation
415 then fh+ips
416 else (
417 if isbirdseye state.mode && pn = 0
418 then fh + ips
419 else fh
422 let fh = fh + ((n - pn) * (ph + pi)) in
423 f n h ips fh rest;
425 | [] ->
426 let inc =
427 if conf.presentation || (isbirdseye state.mode && pn = 0)
428 then 0
429 else -pi
431 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
432 max 0 fh
434 let fh = f 0 0 0 0 state.pdims in
438 let getpageyh pageno =
439 let rec f pn ph pi y l =
440 match l with
441 | (n, _, h, _) :: rest ->
442 let ips = calcips h in
443 if n >= pageno
444 then
445 let h = if n = pageno then h else ph in
446 if conf.presentation && n = pageno
447 then
448 y + (pageno - pn) * (ph + pi) + pi, h
449 else
450 y + (pageno - pn) * (ph + pi), h
451 else
452 let y = y + (if conf.presentation then pi else 0) in
453 let y = y + (n - pn) * (ph + pi) in
454 f n h ips y rest
456 | [] ->
457 y + (pageno - pn) * (ph + pi), ph
459 f 0 0 0 0 state.pdims
462 let getpagey pageno = fst (getpageyh pageno);;
464 let layout y sh =
465 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
466 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
467 match pdims with
468 | (pageno', w, h, x) :: rest when pageno' = pageno ->
469 let ips = calcips h in
470 let yinc =
471 if conf.presentation || (isbirdseye state.mode && pageno = 0)
472 then ips
473 else 0
475 (w, h, ips, x), rest, pdimno + 1, yinc
476 | _ ->
477 prev, pdims, pdimno, 0
479 let dy = dy + yinc in
480 let py = py + yinc in
481 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
482 then
483 accu
484 else
485 let vy = y + dy in
486 if py + h <= vy - yinc
487 then
488 let py = py + h + ips in
489 let dy = max 0 (py - y) in
490 f ~pageno:(pageno+1)
491 ~pdimno
492 ~prev:curr
495 ~pdims:rest
496 ~cacheleft
497 ~accu
498 else
499 let pagey = vy - py in
500 let pagevh = h - pagey in
501 let pagevh = min (sh - dy) pagevh in
502 let off = if yinc > 0 then py - vy else 0 in
503 let py = py + h + ips in
504 let e =
505 { pageno = pageno
506 ; pagedimno = pdimno
507 ; pagew = w
508 ; pageh = h
509 ; pagedispy = dy + off
510 ; pagey = pagey + off
511 ; pagevh = pagevh - off
512 ; pagex = x
515 let accu = e :: accu in
516 f ~pageno:(pageno+1)
517 ~pdimno
518 ~prev:curr
520 ~dy:(dy+pagevh+ips)
521 ~pdims:rest
522 ~cacheleft:(cacheleft-1)
523 ~accu
525 if state.invalidated = 0
526 then (
527 let accu =
529 ~pageno:0
530 ~pdimno:~-1
531 ~prev:(0,0,0,0)
532 ~py:0
533 ~dy:0
534 ~pdims:state.pdims
535 ~cacheleft:(cbcap state.pagecache)
536 ~accu:[]
538 List.rev accu
540 else
544 let clamp incr =
545 let y = state.y + incr in
546 let y = max 0 y in
547 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
551 let getopaque pageno =
552 try Some (Hashtbl.find state.pagemap
553 (pageno, state.w, conf.angle, conf.proportional, state.gen))
554 with Not_found -> None
557 let cache pageno opaque =
558 Hashtbl.replace state.pagemap
559 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
562 let validopaque opaque = String.length opaque > 0;;
564 let render l =
565 match getopaque l.pageno with
566 | None when not state.rendering ->
567 state.rendering <- true;
568 cache l.pageno ("", -1);
569 wcmd "render" [`i (l.pageno + 1)
570 ;`i l.pagedimno
571 ;`i l.pagew
572 ;`i l.pageh];
573 | _ -> ()
576 let loadlayout layout =
577 let rec f all = function
578 | l :: ls ->
579 begin match getopaque l.pageno with
580 | None -> render l; f false ls
581 | Some (opaque, _) -> f (all && validopaque opaque) ls
583 | [] -> all
585 f (layout <> []) layout;
588 let findpageforopaque opaque =
589 Hashtbl.fold
590 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
591 state.pagemap None
594 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
596 let preload () =
597 let oktopreload =
598 if conf.preload
599 then
600 let memleft = conf.memlimit - state.memused in
601 if memleft < 0
602 then
603 let opaque = cbpeek state.pagecache in
604 match findpageforopaque opaque with
605 | Some ((n, _, _, _, _), size) ->
606 memleft + size >= 0 && not (pagevisible state.layout n)
607 | None -> false
608 else true
609 else false
611 if oktopreload
612 then
613 let presentation = conf.presentation in
614 let interpagespace = conf.interpagespace in
615 let maxy = state.maxy in
616 conf.presentation <- false;
617 conf.interpagespace <- 0;
618 state.maxy <- calcheight ();
619 let y =
620 match state.layout with
621 | [] -> 0
622 | l :: _ -> getpagey l.pageno + l.pagey
624 let y = if y < conf.winh then 0 else y - conf.winh in
625 let h = state.y - y + conf.winh*3 in
626 let pages = layout y h in
627 List.iter render pages;
628 conf.presentation <- presentation;
629 conf.interpagespace <- interpagespace;
630 state.maxy <- maxy;
633 let gotoy y =
634 let y = max 0 y in
635 let y = min state.maxy y in
636 let pages = layout y conf.winh in
637 let ready = loadlayout pages in
638 if conf.showall
639 then (
640 if ready
641 then (
642 state.y <- y;
643 state.layout <- pages;
644 state.throttle <- None;
645 Glut.postRedisplay ();
647 else (
648 state.throttle <- Some pages;
651 else (
652 state.y <- y;
653 state.layout <- pages;
654 state.throttle <- None;
655 Glut.postRedisplay ();
657 begin match state.mode with
658 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
659 if not (pagevisible pages pageno)
660 then (
661 match state.layout with
662 | [] -> ()
663 | l :: _ ->
664 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
666 | _ -> ()
667 end;
668 preload ();
671 let gotoy_and_clear_text y =
672 gotoy y;
673 if not conf.verbose then state.text <- "";
676 let getanchor () =
677 match state.layout with
678 | [] -> emptyanchor
679 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
682 let getanchory (n, top) =
683 let y, h = getpageyh n in
684 y + (truncate (top *. float h));
687 let gotoanchor anchor =
688 gotoy (getanchory anchor);
691 let addnav () =
692 cbput state.hists.nav (getanchor ());
695 let getnav dir =
696 let anchor = cbgetc state.hists.nav dir in
697 getanchory anchor;
700 let gotopage n top =
701 let y, h = getpageyh n in
702 gotoy_and_clear_text (y + (truncate (top *. float h)));
705 let gotopage1 n top =
706 let y = getpagey n in
707 gotoy_and_clear_text (y + top);
710 let invalidate () =
711 state.layout <- [];
712 state.pdims <- [];
713 state.rects <- [];
714 state.rects1 <- [];
715 state.invalidated <- state.invalidated + 1;
718 let scalecolor c =
719 let c = c *. state.colorscale in
720 (c, c, c);
723 let scalecolor2 (r, g, b) =
724 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
727 let represent () =
728 state.maxy <- calcheight ();
729 match state.mode with
730 | Birdseye (_, _, pageno, _, _) ->
731 let y, h = getpageyh pageno in
732 let top = (conf.winh - h) / 2 in
733 gotoy (max 0 (y - top))
734 | _ -> gotoanchor state.anchor
737 let pagematrix () =
738 GlMat.mode `projection;
739 GlMat.load_identity ();
740 GlMat.rotate ~x:1.0 ~angle:180.0 ();
741 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
742 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
743 if state.x != 0
744 then (
745 GlMat.translate ~x:(float state.x) ();
749 let winmatrix () =
750 GlMat.mode `projection;
751 GlMat.load_identity ();
752 GlMat.rotate ~x:1.0 ~angle:180.0 ();
753 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
754 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
757 let reshape =
758 let firsttime = ref true in
759 fun ~w ~h ->
760 if state.invalidated = 0 && not !firsttime
761 then state.anchor <- getanchor ();
763 firsttime := false;
764 conf.winw <- w;
765 let w = truncate (float w *. conf.zoom) - state.scrollw in
766 let w = max w 2 in
767 state.w <- w;
768 conf.winh <- h;
769 GlMat.mode `modelview;
770 GlMat.load_identity ();
771 GlClear.color (scalecolor 1.0);
772 GlClear.clear [`color];
774 invalidate ();
775 wcmd "geometry" [`i w; `i h];
778 let drawstring size x y s =
779 Gl.enable `blend;
780 Gl.enable `texture_2d;
781 ignore (drawstr size x y s);
782 Gl.disable `blend;
783 Gl.disable `texture_2d;
786 let drawstring1 size x y s =
787 drawstr size x y s;
790 let enttext () =
791 let len = String.length state.text in
792 let drawstring s =
793 GlDraw.color (0.0, 0.0, 0.0);
794 GlDraw.rect
795 (0.0, float (conf.winh - 18))
796 (float (conf.winw - state.scrollw - 1), float conf.winh)
798 GlDraw.color (1.0, 1.0, 1.0);
799 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
801 match state.mode with
802 | Textentry ((prefix, text, _, _, _), _) ->
803 let s =
804 if len > 0
805 then
806 Printf.sprintf "%s%s_ [%s]" prefix text state.text
807 else
808 Printf.sprintf "%s%s_" prefix text
810 drawstring s
812 | _ ->
813 if len > 0 then drawstring state.text
816 let showtext c s =
817 state.text <- Printf.sprintf "%c%s" c s;
818 Glut.postRedisplay ();
821 let act cmd =
822 match cmd.[0] with
823 | 'c' ->
824 state.pdims <- [];
826 | 'D' ->
827 state.rects <- state.rects1;
828 Glut.postRedisplay ()
830 | 'C' ->
831 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
832 state.pagecount <- n;
833 state.invalidated <- state.invalidated - 1;
834 if state.invalidated = 0
835 then represent ()
837 | 't' ->
838 let s = Scanf.sscanf cmd "t %n"
839 (fun n -> String.sub cmd n (String.length cmd - n))
841 Glut.setWindowTitle s
843 | 'T' ->
844 let s = Scanf.sscanf cmd "T %n"
845 (fun n -> String.sub cmd n (String.length cmd - n))
847 if istextentry state.mode
848 then (
849 state.text <- s;
850 showtext ' ' s;
852 else (
853 state.text <- s;
854 Glut.postRedisplay ();
857 | 'V' ->
858 if conf.verbose
859 then
860 let s = Scanf.sscanf cmd "V %n"
861 (fun n -> String.sub cmd n (String.length cmd - n))
863 state.text <- s;
864 showtext ' ' s;
866 | 'F' ->
867 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
868 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
869 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
870 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
872 let y = (getpagey pageno) + truncate y0 in
873 addnav ();
874 gotoy y;
875 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
877 | 'R' ->
878 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
879 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
880 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
881 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
883 state.rects1 <-
884 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
886 | 'r' ->
887 let n, w, _h, r, l, s, p =
888 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
889 (fun n w h r l s p ->
890 (n-1, w, h, r, l != 0, s, p))
893 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
894 state.memused <- state.memused + s;
896 let layout =
897 match state.throttle with
898 | None -> state.layout
899 | Some layout -> layout
902 let rec gc () =
903 if (state.memused <= conf.memlimit) || cbempty state.pagecache
904 then ()
905 else (
906 let evictedopaque = cbpeek state.pagecache in
907 match findpageforopaque evictedopaque with
908 | None -> failwith "bug in gc"
909 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
910 if state.gen != gen || not (pagevisible layout evictedn)
911 then (
912 wcmd "free" [`s evictedopaque];
913 state.memused <- state.memused - evictedsize;
914 Hashtbl.remove state.pagemap k;
915 cbdecr state.pagecache;
916 gc ();
920 gc ();
922 cbput state.pagecache p;
923 state.rendering <- false;
925 begin match state.throttle with
926 | None ->
927 if pagevisible state.layout n
928 then gotoy state.y
929 else (
930 let allvisible = loadlayout state.layout in
931 if allvisible then preload ();
934 | Some layout ->
935 match layout with
936 | [] -> ()
937 | l :: _ ->
938 let y = getpagey l.pageno + l.pagey in
939 gotoy y
942 | 'l' ->
943 let pdim =
944 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
946 state.pdims <- pdim :: state.pdims
948 | 'o' ->
949 let (l, n, t, h, pos) =
950 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
952 let s = String.sub cmd pos (String.length cmd - pos) in
953 let outline = (s, l, (n, float t /. float h)) in
954 let outlines =
955 match state.outlines with
956 | Olist outlines -> Olist (outline :: outlines)
957 | Oarray _ -> Olist [outline]
958 | Onarrow _ -> Olist [outline]
960 state.outlines <- outlines
963 | 'i' ->
964 if String.length cmd > 1 && cmd.[1] = 'e'
965 then
966 state.docinfo <- List.rev state.docinfo
967 else
968 let s = Scanf.sscanf cmd "i %n"
969 (fun n -> String.sub cmd n (String.length cmd - n))
971 state.docinfo <- (1, s) :: state.docinfo
973 | _ ->
974 dolog "unknown cmd `%S'" cmd
977 let now = Unix.gettimeofday;;
979 let idle () =
980 if state.deadline == nan then state.deadline <- now ();
981 let rec loop delay =
982 let timeout =
983 if delay > 0.0
984 then max 0.0 (state.deadline -. now ())
985 else 0.0
987 let r, _, _ = Unix.select [state.csock] [] [] timeout in
988 state.deadline <- state.deadline +. delay;
989 begin match r with
990 | [] ->
991 begin match state.autoscroll with
992 | Some step when step != 0 ->
993 let y = state.y + step in
994 let y =
995 if y < 0
996 then state.maxy
997 else if y >= state.maxy then 0 else y
999 gotoy y;
1000 if state.mode = View
1001 then state.text <- "";
1002 | _ -> ()
1003 end;
1005 | _ ->
1006 let cmd = readcmd state.csock in
1007 act cmd;
1008 loop 0.0
1009 end;
1010 in loop 0.007
1013 let onhist cb =
1014 let rc = cb.rc in
1015 let action = function
1016 | HCprev -> cbget cb ~-1
1017 | HCnext -> cbget cb 1
1018 | HCfirst -> cbget cb ~-(cb.rc)
1019 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1020 and cancel () = cb.rc <- rc
1021 in (action, cancel)
1024 let search pattern forward =
1025 if String.length pattern > 0
1026 then
1027 let pn, py =
1028 match state.layout with
1029 | [] -> 0, 0
1030 | l :: _ ->
1031 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1033 let cmd =
1034 let b = makecmd "search"
1035 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1037 Buffer.add_char b ',';
1038 Buffer.add_string b pattern;
1039 Buffer.add_char b '\000';
1040 Buffer.contents b;
1042 writecmd state.csock cmd;
1045 let intentry text key =
1046 let c = Char.unsafe_chr key in
1047 match c with
1048 | '0' .. '9' ->
1049 let s = "x" in s.[0] <- c;
1050 let text = text ^ s in
1051 TEcont text
1053 | _ ->
1054 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1055 TEcont text
1058 let addchar s c =
1059 let b = Buffer.create (String.length s + 1) in
1060 Buffer.add_string b s;
1061 Buffer.add_char b c;
1062 Buffer.contents b;
1065 let textentry text key =
1066 let c = Char.unsafe_chr key in
1067 match c with
1068 | _ when key >= 32 && key < 127 ->
1069 let text = addchar text c in
1070 TEcont text
1072 | _ ->
1073 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1074 TEcont text
1077 let reinit angle proportional =
1078 conf.angle <- angle;
1079 conf.proportional <- proportional;
1080 invalidate ();
1081 wcmd "reinit" [`i angle; `b proportional];
1084 let setzoom zoom =
1085 let zoom = max 0.01 zoom in
1086 if zoom <> conf.zoom
1087 then (
1088 if zoom <= 1.0
1089 then state.x <- 0;
1090 conf.zoom <- zoom;
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.autoscroll <> None
1208 then state.autoscroll <- Some 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 "%s\t%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 "%s\t%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 "%s\t%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 match state.autoscroll with
1536 | Some step -> step
1537 | _ -> conf.autoscrollstep)
1538 (fun n ->
1539 if state.autoscroll <> None
1540 then state.autoscroll <- Some n;
1541 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 conf.scrollbw <- v;
1556 reshape conf.winw conf.winh;
1559 intp "scroll handle height"
1560 (fun () -> conf.scrollh)
1561 (fun v -> conf.scrollh <- v;);
1563 intp "thumbnail width"
1564 (fun () -> conf.thumbw)
1565 (fun v ->
1566 conf.thumbw <- min 1920 v;
1567 match mode with
1568 | Birdseye beye ->
1569 leavebirdseye beye false;
1570 enterbirdseye ()
1571 | _ -> ()
1574 colorp "background color"
1575 (fun () -> conf.bgcolor)
1576 (fun v -> conf.bgcolor <- v);
1578 "", 0, Noaction;
1579 "Presentation mode", 0, Noaction;
1581 boolp "scrollbar visible"
1582 (fun () -> conf.scrollbarinpm)
1583 (fun v ->
1584 if v != conf.scrollbarinpm
1585 then (
1586 conf.scrollbarinpm <- v;
1587 if conf.presentation
1588 then (
1589 state.scrollw <- if v then conf.scrollbw else 0;
1590 reshape conf.winw conf.winh;
1595 "", 0, Noaction;
1596 "Pixmap Cache", 0, Noaction;
1598 intp "size (advisory)"
1599 (fun () -> conf.memlimit)
1600 (fun v -> conf.memlimit <- v);
1601 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1605 let tailer =
1606 let trailer =
1607 ("", 0, Noaction)
1608 :: ("Document", 0, Noaction)
1609 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1611 if birdseye
1612 then trailer
1613 else (
1614 ("", 0, Noaction)
1615 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1616 (btos conf.bedefault),
1618 Action (
1619 fun active first qsearch pan ->
1620 conf.bedefault <- not conf.bedefault;
1621 Items (active, first, makeitems (), qsearch, pan, mode);
1623 ) :: trailer
1626 Array.of_list (items @ tailer)
1628 state.text <- "";
1629 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1630 Glut.postRedisplay ();
1633 let enterhelpmode () =
1634 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1635 Glut.postRedisplay ();
1638 let quickbookmark ?title () =
1639 match state.layout with
1640 | [] -> ()
1641 | l :: _ ->
1642 let title =
1643 match title with
1644 | None ->
1645 let sec = Unix.gettimeofday () in
1646 let tm = Unix.localtime sec in
1647 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1648 (l.pageno+1)
1649 tm.Unix.tm_mday
1650 tm.Unix.tm_mon
1651 (tm.Unix.tm_year + 1900)
1652 tm.Unix.tm_hour
1653 tm.Unix.tm_min
1654 | Some title -> title
1656 state.bookmarks <-
1657 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1658 :: state.bookmarks
1661 let doreshape w h =
1662 state.fullscreen <- None;
1663 Glut.reshapeWindow w h;
1666 let writeopen path password =
1667 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1670 let opendoc path password =
1671 invalidate ();
1672 state.path <- path;
1673 state.password <- password;
1674 state.gen <- state.gen + 1;
1675 state.docinfo <- [];
1677 writeopen path password;
1678 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1679 wcmd "geometry" [`i state.w; `i conf.winh];
1682 let viewkeyboard key =
1683 let enttext te =
1684 let mode = state.mode in
1685 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1686 state.text <- "";
1687 enttext ();
1688 Glut.postRedisplay ()
1690 let c = Char.chr key in
1691 match c with
1692 | '\027' -> (* escape *)
1693 if String.length state.text > 0
1694 then (
1695 state.text <- "";
1696 Glut.postRedisplay ();
1698 else raise Quit;
1700 | 'q' ->
1701 raise Quit
1703 | '\008' -> (* backspace *)
1704 let y = getnav ~-1 in
1705 gotoy_and_clear_text y
1707 | 'o' ->
1708 enteroutlinemode ()
1710 | 'u' ->
1711 state.rects <- [];
1712 state.text <- "";
1713 Glut.postRedisplay ()
1715 | '/' | '?' ->
1716 let ondone isforw s =
1717 cbput state.hists.pat s;
1718 state.searchpattern <- s;
1719 search s isforw
1721 let s = String.create 1 in
1722 s.[0] <- c;
1723 enttext (s, "", Some (onhist state.hists.pat),
1724 textentry, ondone (c ='/'))
1726 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1727 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1728 setzoom (conf.zoom +. incr)
1730 | '+' ->
1731 let ondone s =
1732 let n =
1733 try int_of_string s with exc ->
1734 state.text <- Printf.sprintf "bad integer `%s': %s"
1735 s (Printexc.to_string exc);
1736 max_int
1738 if n != max_int
1739 then (
1740 conf.pagebias <- n;
1741 state.text <- "page bias is now " ^ string_of_int n;
1744 enttext ("page bias: ", "", None, intentry, ondone)
1746 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1747 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1748 setzoom (max 0.01 (conf.zoom -. decr))
1750 | '-' ->
1751 let ondone msg = state.text <- msg in
1752 enttext (
1753 "option [acfhilpstvAPRSZ]: ", "", None,
1754 optentry state.mode, ondone
1757 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1758 setzoom 1.0
1760 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1761 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1762 if zoom < 1.0
1763 then setzoom zoom
1765 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1766 togglebirdseye ()
1768 | '0' .. '9' ->
1769 let ondone s =
1770 let n =
1771 try int_of_string s with exc ->
1772 state.text <- Printf.sprintf "bad integer `%s': %s"
1773 s (Printexc.to_string exc);
1776 if n >= 0
1777 then (
1778 addnav ();
1779 cbput state.hists.pag (string_of_int n);
1780 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1783 let pageentry text key =
1784 match Char.unsafe_chr key with
1785 | 'g' -> TEdone text
1786 | _ -> intentry text key
1788 let text = "x" in text.[0] <- c;
1789 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1791 | 'b' ->
1792 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1793 reshape conf.winw conf.winh;
1795 | 'l' ->
1796 conf.hlinks <- not conf.hlinks;
1797 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1798 Glut.postRedisplay ()
1800 | 'a' ->
1801 begin match state.autoscroll with
1802 | Some step ->
1803 conf.autoscrollstep <- step;
1804 state.autoscroll <- None
1805 | None ->
1806 if conf.autoscrollstep = 0
1807 then state.autoscroll <- Some 1
1808 else state.autoscroll <- Some conf.autoscrollstep
1811 | 'P' ->
1812 conf.presentation <- not conf.presentation;
1813 if conf.presentation
1814 then (
1815 if not conf.scrollbarinpm
1816 then state.scrollw <- 0;
1818 else
1819 state.scrollw <- conf.scrollbw;
1821 showtext ' ' ("presentation mode " ^
1822 if conf.presentation then "on" else "off");
1823 state.anchor <- getanchor ();
1824 represent ()
1826 | 'f' ->
1827 begin match state.fullscreen with
1828 | None ->
1829 state.fullscreen <- Some (conf.winw, conf.winh);
1830 Glut.fullScreen ()
1831 | Some (w, h) ->
1832 state.fullscreen <- None;
1833 doreshape w h
1836 | 'g' ->
1837 gotoy_and_clear_text 0
1839 | 'n' ->
1840 search state.searchpattern true
1842 | 'p' | 'N' ->
1843 search state.searchpattern false
1845 | 't' ->
1846 begin match state.layout with
1847 | [] -> ()
1848 | l :: _ ->
1849 gotoy_and_clear_text (getpagey l.pageno)
1852 | ' ' ->
1853 begin match List.rev state.layout with
1854 | [] -> ()
1855 | l :: _ ->
1856 let pageno = min (l.pageno+1) (state.pagecount-1) in
1857 gotoy_and_clear_text (getpagey pageno)
1860 | '\127' -> (* del *)
1861 begin match state.layout with
1862 | [] -> ()
1863 | l :: _ ->
1864 let pageno = max 0 (l.pageno-1) in
1865 gotoy_and_clear_text (getpagey pageno)
1868 | '=' ->
1869 let f (fn, _) l =
1870 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1872 let fn, ln = List.fold_left f (-1, -1) state.layout in
1873 let s =
1874 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1875 let percent =
1876 if maxy <= 0
1877 then 100.
1878 else (100. *. (float state.y /. float maxy)) in
1879 if fn = ln
1880 then
1881 Printf.sprintf "Page %d of %d %.2f%%"
1882 (fn+1) state.pagecount percent
1883 else
1884 Printf.sprintf
1885 "Pages %d-%d of %d %.2f%%"
1886 (fn+1) (ln+1) state.pagecount percent
1888 showtext ' ' s;
1890 | 'w' ->
1891 begin match state.layout with
1892 | [] -> ()
1893 | l :: _ ->
1894 doreshape (l.pagew + state.scrollw) l.pageh;
1895 Glut.postRedisplay ();
1898 | '\'' ->
1899 enterbookmarkmode ()
1901 | 'h' ->
1902 enterhelpmode ()
1904 | 'i' ->
1905 enterinfomode ()
1907 | 'm' ->
1908 let ondone s =
1909 match state.layout with
1910 | l :: _ ->
1911 state.bookmarks <-
1912 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1913 :: state.bookmarks
1914 | _ -> ()
1916 enttext ("bookmark: ", "", None, textentry, ondone)
1918 | '~' ->
1919 quickbookmark ();
1920 showtext ' ' "Quick bookmark added";
1922 | 'z' ->
1923 begin match state.layout with
1924 | l :: _ ->
1925 let rect = getpdimrect l.pagedimno in
1926 let w, h =
1927 if conf.crophack
1928 then
1929 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1930 truncate (1.2 *. (rect.(3) -. rect.(0))))
1931 else
1932 (truncate (rect.(1) -. rect.(0)),
1933 truncate (rect.(3) -. rect.(0)))
1935 if w != 0 && h != 0
1936 then
1937 doreshape (w + state.scrollw) (h + conf.interpagespace)
1939 Glut.postRedisplay ();
1941 | [] -> ()
1944 | '\000' -> (* ctrl-2 *)
1945 let maxw = getmaxw () in
1946 if maxw > 0.0
1947 then setzoom (maxw /. float conf.winw)
1949 | '<' | '>' ->
1950 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1952 | '[' | ']' ->
1953 state.colorscale <-
1954 max 0.0
1955 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1956 Glut.postRedisplay ()
1958 | 'k' ->
1959 begin match state.mode with
1960 | Birdseye beye -> upbirdseye beye
1961 | _ -> gotoy (clamp (-conf.scrollstep))
1964 | 'j' ->
1965 begin match state.mode with
1966 | Birdseye beye -> downbirdseye beye
1967 | _ -> gotoy (clamp conf.scrollstep)
1970 | 'r' ->
1971 state.anchor <- getanchor ();
1972 opendoc state.path state.password
1974 | _ ->
1975 vlog "huh? %d %c" key (Char.chr key);
1978 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1979 let enttext te =
1980 state.mode <- Textentry (te, onleave);
1981 state.text <- "";
1982 enttext ();
1983 Glut.postRedisplay ()
1985 match Char.unsafe_chr key with
1986 | '\008' -> (* backspace *)
1987 let len = String.length text in
1988 if len = 0
1989 then (
1990 onleave Cancel;
1991 Glut.postRedisplay ();
1993 else (
1994 let s = String.sub text 0 (len - 1) in
1995 enttext (c, s, opthist, onkey, ondone)
1998 | '\r' | '\n' ->
1999 ondone text;
2000 onleave Confirm;
2001 Glut.postRedisplay ()
2003 | '\027' -> (* escape *)
2004 begin match opthist with
2005 | None -> ()
2006 | Some (_, onhistcancel) -> onhistcancel ()
2007 end;
2008 onleave Cancel;
2009 Glut.postRedisplay ()
2011 | _ ->
2012 begin match onkey text key with
2013 | TEdone text ->
2014 onleave Confirm;
2015 ondone text;
2016 Glut.postRedisplay ()
2018 | TEcont text ->
2019 enttext (c, text, opthist, onkey, ondone);
2021 | TEstop ->
2022 onleave Cancel;
2023 Glut.postRedisplay ()
2025 | TEswitch te ->
2026 state.mode <- Textentry (te, onleave);
2027 Glut.postRedisplay ()
2028 end;
2031 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2032 match key with
2033 | 27 -> (* escape *)
2034 leavebirdseye beye true
2036 | 12 -> (* ctrl-l *)
2037 let y, h = getpageyh pageno in
2038 let top = (conf.winh - h) / 2 in
2039 gotoy (max 0 (y - top))
2041 | 13 -> (* enter *)
2042 leavebirdseye beye false
2044 | _ ->
2045 viewkeyboard key
2048 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2049 let set active first qsearch =
2050 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2052 let search active pattern incr =
2053 let dosearch re =
2054 let rec loop n =
2055 if n >= Array.length items || n < 0
2056 then None
2057 else
2058 let (s, _, _) = items.(n) in
2060 (try ignore (Str.search_forward re s 0); true
2061 with Not_found -> false)
2062 then Some n
2063 else loop (n + incr)
2065 loop active
2068 let re = Str.regexp_case_fold pattern in
2069 dosearch re
2070 with Failure s ->
2071 state.text <- s;
2072 None
2074 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2075 match key with
2076 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2077 let incr = if key = 18 then -1 else 1 in
2078 let active, first =
2079 match search (active + incr) qsearch incr with
2080 | None ->
2081 state.text <- qsearch ^ " [not found]";
2082 active, first
2083 | Some active ->
2084 state.text <- qsearch;
2085 active, firstof active
2087 set active first qsearch;
2088 Glut.postRedisplay ();
2090 | 8 -> (* backspace *)
2091 let len = String.length qsearch in
2092 if len = 0
2093 then ()
2094 else (
2095 if len = 1
2096 then (
2097 state.text <- "";
2098 set active first "";
2100 else
2101 let qsearch = String.sub qsearch 0 (len - 1) in
2102 let active, first =
2103 match search active qsearch ~-1 with
2104 | None ->
2105 state.text <- qsearch ^ " [not found]";
2106 active, first
2107 | Some active ->
2108 state.text <- qsearch;
2109 active, firstof active
2111 set active first qsearch
2113 Glut.postRedisplay ()
2115 | _ when key >= 32 && key < 127 ->
2116 let pattern = addchar qsearch (Char.chr key) in
2117 let active, first =
2118 match search active pattern 1 with
2119 | None ->
2120 state.text <- pattern ^ " [not found]";
2121 active, first
2122 | Some active ->
2123 state.text <- pattern;
2124 active, firstof active
2126 set active first pattern;
2127 Glut.postRedisplay ()
2129 | 27 -> (* escape *)
2130 state.text <- "";
2131 if String.length qsearch = 0
2132 then (
2133 state.mode <- oldmode;
2135 else (
2136 set active first "";
2138 Glut.postRedisplay ()
2140 | 13 -> (* enter *)
2141 if active >= 0 && active < Array.length items
2142 then (
2143 match items.(active) with
2144 | _, _, Action f ->
2145 state.mode <- f active first qsearch pan
2147 | _, _, Noaction ->
2148 state.text <- "";
2149 state.mode <- oldmode
2151 else (
2152 state.text <- "";
2153 state.mode <- oldmode
2155 Glut.postRedisplay ();
2157 | _ -> dolog "unknown key %d" key
2160 let outlinekeyboard key
2161 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2162 let narrow outlines pattern =
2163 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2164 match reopt with
2165 | None -> None
2166 | Some re ->
2167 let rec fold accu n =
2168 if n = -1
2169 then accu
2170 else
2171 let (s, _, _) as o = outlines.(n) in
2172 let accu =
2173 if (try ignore (Str.search_forward re s 0); true
2174 with Not_found -> false)
2175 then (o :: accu)
2176 else accu
2178 fold accu (n-1)
2180 let matched = fold [] (Array.length outlines - 1) in
2181 if matched = [] then None else Some (Array.of_list matched)
2183 let search active pattern incr =
2184 let dosearch re =
2185 let rec loop n =
2186 if n = Array.length outlines || n = -1
2187 then None
2188 else
2189 let (s, _, _) = outlines.(n) in
2191 (try ignore (Str.search_forward re s 0); true
2192 with Not_found -> false)
2193 then Some n
2194 else loop (n + incr)
2196 loop active
2199 let re = Str.regexp_case_fold pattern in
2200 dosearch re
2201 with Failure s ->
2202 state.text <- s;
2203 None
2205 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2206 match key with
2207 | 27 -> (* escape *)
2208 state.text <- "";
2209 if String.length qsearch = 0
2210 then (
2211 state.mode <- oldmode;
2213 else (
2214 state.mode <- Outline (
2215 allowdel, active, first, outlines, "", pan, oldmode
2218 Glut.postRedisplay ();
2220 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2221 let incr = if key = 18 then -1 else 1 in
2222 let active, first =
2223 match search (active + incr) qsearch incr with
2224 | None ->
2225 state.text <- qsearch ^ " [not found]";
2226 active, first
2227 | Some active ->
2228 state.text <- qsearch;
2229 active, firstof active
2231 state.mode <- Outline (
2232 allowdel, active, first, outlines, qsearch, pan, oldmode
2234 Glut.postRedisplay ();
2236 | 8 -> (* backspace *)
2237 let len = String.length qsearch in
2238 if len = 0
2239 then ()
2240 else (
2241 if len = 1
2242 then (
2243 state.text <- "";
2244 state.mode <- Outline (
2245 allowdel, active, first, outlines, "", pan, oldmode
2248 else
2249 let qsearch = String.sub qsearch 0 (len - 1) in
2250 let active, first =
2251 match search active qsearch ~-1 with
2252 | None ->
2253 state.text <- qsearch ^ " [not found]";
2254 active, first
2255 | Some active ->
2256 state.text <- qsearch;
2257 active, firstof active
2259 state.mode <- Outline (
2260 allowdel, active, first, outlines, qsearch, pan, oldmode
2263 Glut.postRedisplay ()
2265 | 13 -> (* enter *)
2266 if active < Array.length outlines
2267 then (
2268 let (_, _, anchor) = outlines.(active) in
2269 addnav ();
2270 gotoanchor anchor;
2272 state.text <- "";
2273 if allowdel then state.bookmarks <- Array.to_list outlines;
2274 state.mode <- oldmode;
2275 Glut.postRedisplay ();
2277 | _ when key >= 32 && key < 127 ->
2278 let pattern = addchar qsearch (Char.chr key) in
2279 let active, first =
2280 match search active pattern 1 with
2281 | None ->
2282 state.text <- pattern ^ " [not found]";
2283 active, first
2284 | Some active ->
2285 state.text <- pattern;
2286 active, firstof active
2288 state.mode <- Outline (
2289 allowdel, active, first, outlines, pattern, pan, oldmode
2291 Glut.postRedisplay ()
2293 | 14 when not allowdel -> (* ctrl-n *)
2294 if String.length qsearch > 0
2295 then (
2296 let optoutlines = narrow outlines qsearch in
2297 begin match optoutlines with
2298 | None -> state.text <- "can't narrow"
2299 | Some outlines ->
2300 state.mode <- Outline (
2301 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2303 match state.outlines with
2304 | Olist _ -> ()
2305 | Oarray a ->
2306 state.outlines <- Onarrow (qsearch, outlines, a)
2307 | Onarrow (_, _, b) ->
2308 state.outlines <- Onarrow (qsearch, outlines, b)
2309 end;
2311 Glut.postRedisplay ()
2313 | 21 when not allowdel -> (* ctrl-u *)
2314 let outline =
2315 match state.outlines with
2316 | Oarray a -> a
2317 | Olist l ->
2318 let a = Array.of_list (List.rev l) in
2319 state.outlines <- Oarray a;
2321 | Onarrow (_, _, b) ->
2322 state.outlines <- Oarray b;
2323 state.text <- "";
2326 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2327 Glut.postRedisplay ()
2329 | 12 -> (* ctrl-l *)
2330 state.mode <- Outline
2331 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2332 Glut.postRedisplay ()
2334 | 127 when allowdel -> (* delete *)
2335 let len = Array.length outlines - 1 in
2336 if len = 0
2337 then (
2338 state.mode <- View;
2339 state.bookmarks <- [];
2341 else (
2342 let bookmarks = Array.init len
2343 (fun i ->
2344 let i = if i >= active then i + 1 else i in
2345 outlines.(i)
2348 state.mode <-
2349 Outline (
2350 allowdel,
2351 min active (len-1),
2352 min first (len-1),
2353 bookmarks, qsearch,
2355 oldmode
2358 Glut.postRedisplay ()
2360 | _ -> dolog "unknown key %d" key
2363 let keyboard ~key ~x ~y =
2364 ignore x;
2365 ignore y;
2366 if key = 7 (* ctrl-g *)
2367 then
2368 wcmd "interrupt" []
2369 else
2370 match state.mode with
2371 | Outline outline -> outlinekeyboard key outline
2372 | Textentry textentry -> textentrykeyboard key textentry
2373 | Birdseye birdseye -> birdseyekeyboard key birdseye
2374 | View -> viewkeyboard key
2375 | Items items -> itemskeyboard key items
2378 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2379 match key with
2380 | Glut.KEY_UP -> upbirdseye beye
2381 | Glut.KEY_DOWN -> downbirdseye beye
2383 | Glut.KEY_PAGE_UP ->
2384 begin match state.layout with
2385 | l :: _ ->
2386 if l.pagey != 0
2387 then (
2388 state.mode <- Birdseye (
2389 conf, leftx, l.pageno, hooverpageno, anchor
2391 gotopage1 l.pageno 0;
2393 else (
2394 let layout = layout (state.y-conf.winh) conf.winh in
2395 match layout with
2396 | [] -> gotoy (clamp (-conf.winh))
2397 | l :: _ ->
2398 state.mode <- Birdseye (
2399 conf, leftx, l.pageno, hooverpageno, anchor
2401 gotopage1 l.pageno 0
2404 | [] -> gotoy (clamp (-conf.winh))
2405 end;
2407 | Glut.KEY_PAGE_DOWN ->
2408 begin match List.rev state.layout with
2409 | l :: _ ->
2410 let layout = layout (state.y + conf.winh) conf.winh in
2411 begin match layout with
2412 | [] ->
2413 let incr = l.pageh - l.pagevh in
2414 if incr = 0
2415 then (
2416 state.mode <-
2417 Birdseye (
2418 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2420 Glut.postRedisplay ();
2422 else gotoy (clamp (incr + conf.interpagespace*2));
2424 | l :: _ ->
2425 state.mode <-
2426 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2427 gotopage1 l.pageno 0;
2430 | [] -> gotoy (clamp conf.winh)
2431 end;
2433 | Glut.KEY_HOME ->
2434 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2435 gotopage1 0 0
2437 | Glut.KEY_END ->
2438 let pageno = state.pagecount - 1 in
2439 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2440 if not (pagevisible state.layout pageno)
2441 then
2442 let h =
2443 match List.rev state.pdims with
2444 | [] -> conf.winh
2445 | (_, _, h, _) :: _ -> h
2447 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2448 else Glut.postRedisplay ();
2449 | _ -> ()
2452 let setautoscrollspeed step goingdown =
2453 let incr = max 1 ((abs step) / 2) in
2454 let incr = if goingdown then incr else -incr in
2455 let astep = step + incr in
2456 state.autoscroll <- Some astep;
2459 let special ~key ~x ~y =
2460 ignore x;
2461 ignore y;
2462 match state.mode with
2463 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2464 togglebirdseye ()
2466 | Birdseye vals ->
2467 birdseyespecial key vals
2469 | View when key = Glut.KEY_F1 ->
2470 enterhelpmode ()
2472 | View ->
2473 begin match state.autoscroll with
2474 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2475 setautoscrollspeed step (key = Glut.KEY_DOWN)
2477 | _ ->
2478 let y =
2479 match key with
2480 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2481 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2482 | Glut.KEY_DOWN -> clamp conf.scrollstep
2483 | Glut.KEY_PAGE_UP ->
2484 if Glut.getModifiers () land Glut.active_ctrl != 0
2485 then
2486 match state.layout with
2487 | [] -> state.y
2488 | l :: _ -> state.y - l.pagey
2489 else
2490 clamp (-conf.winh)
2491 | Glut.KEY_PAGE_DOWN ->
2492 if Glut.getModifiers () land Glut.active_ctrl != 0
2493 then
2494 match List.rev state.layout with
2495 | [] -> state.y
2496 | l :: _ -> getpagey l.pageno
2497 else
2498 clamp conf.winh
2499 | Glut.KEY_HOME -> addnav (); 0
2500 | Glut.KEY_END ->
2501 addnav ();
2502 state.maxy - (if conf.maxhfit then conf.winh else 0)
2504 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2505 Glut.getModifiers () land Glut.active_alt != 0 ->
2506 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2508 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2509 state.x <- state.x - 10;
2510 state.y
2511 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2512 state.x <- state.x + 10;
2513 state.y
2515 | _ -> state.y
2517 gotoy_and_clear_text y
2520 | Textentry
2521 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2522 let s =
2523 match key with
2524 | Glut.KEY_UP -> action HCprev
2525 | Glut.KEY_DOWN -> action HCnext
2526 | Glut.KEY_HOME -> action HCfirst
2527 | Glut.KEY_END -> action HClast
2528 | _ -> state.text
2530 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2531 Glut.postRedisplay ()
2533 | Textentry _ -> ()
2535 | Items (active, first, items, qsearch, pan, oldmode) ->
2536 let maxrows = maxoutlinerows () in
2537 let itemcount = Array.length items in
2538 let hasaction = function
2539 | (_, _, Noaction) -> false
2540 | _ -> true
2542 let find start incr =
2543 let rec find i =
2544 if i = -1 || i = itemcount
2545 then -1
2546 else (
2547 if hasaction items.(i)
2548 then i
2549 else find (i + incr)
2552 find start
2554 let set active first =
2555 let first = max 0 (min first (itemcount - maxrows)) in
2556 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2558 let navigate incr =
2559 let isvisible first n = n >= first && n - first <= maxrows in
2560 let active, first =
2561 let incr1 = if incr > 0 then 1 else -1 in
2562 if isvisible first active
2563 then
2564 let next =
2565 let next = active + incr in
2566 let next =
2567 if next < 0 || next >= itemcount
2568 then -1
2569 else find next incr1
2571 if next = -1 || abs (active - next) > maxrows
2572 then -1
2573 else next
2575 if next = -1
2576 then
2577 let first = first + incr in
2578 let first = max 0 (min first (itemcount - 1)) in
2579 let next =
2580 let next = active + incr in
2581 let next = max 0 (min next (itemcount - 1)) in
2582 find next ~-incr1
2584 let active = if next = -1 then active else next in
2585 active, first
2586 else
2587 let first = min next first in
2588 next, first
2589 else
2590 let first = first + incr in
2591 let first = max 0 (min first (itemcount - 1)) in
2592 let active =
2593 let next = active + incr in
2594 let next = max 0 (min next (itemcount - 1)) in
2595 let next = find next incr1 in
2596 if next = -1 || abs (active - first) > maxrows
2597 then active
2598 else next
2600 active, first
2602 set active first;
2603 Glut.postRedisplay ()
2605 begin match key with
2606 | Glut.KEY_UP -> navigate ~-1
2607 | Glut.KEY_DOWN -> navigate 1
2608 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2609 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2611 | Glut.KEY_RIGHT ->
2612 state.mode <- Items (
2613 active, first, items, qsearch, min 0 (pan - 1), oldmode
2615 Glut.postRedisplay ()
2617 | Glut.KEY_LEFT ->
2618 state.mode <- Items (
2619 active, first, items, qsearch, min 0 (pan + 1), oldmode
2621 Glut.postRedisplay ()
2623 | Glut.KEY_HOME ->
2624 let active = find 0 1 in
2625 set active 0;
2626 Glut.postRedisplay ()
2628 | Glut.KEY_END ->
2629 let first = max 0 (itemcount - maxrows) in
2630 let active = find (itemcount - 1) ~-1 in
2631 set active first;
2632 Glut.postRedisplay ()
2634 | _ -> ()
2635 end;
2637 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2638 let maxrows = maxoutlinerows () in
2639 let calcfirst first active =
2640 if active > first
2641 then
2642 let rows = active - first in
2643 if rows > maxrows then active - maxrows else first
2644 else active
2646 let navigate incr =
2647 let active = active + incr in
2648 let active = max 0 (min active (Array.length outlines - 1)) in
2649 let first = calcfirst first active in
2650 state.mode <- Outline (
2651 allowdel, active, first, outlines, qsearch, pan, oldmode
2653 Glut.postRedisplay ()
2655 let updownlevel incr =
2656 let len = Array.length outlines in
2657 let (_, curlevel, _) = outlines.(active) in
2658 let rec flow i =
2659 if i = len then i-1 else if i = -1 then 0 else
2660 let (_, l, _) = outlines.(i) in
2661 if l != curlevel then i else flow (i+incr)
2663 let active = flow active in
2664 let first = calcfirst first active in
2665 state.mode <- Outline (
2666 allowdel, active, first, outlines, qsearch, pan, oldmode
2668 Glut.postRedisplay ()
2670 match key with
2671 | Glut.KEY_UP -> navigate ~-1
2672 | Glut.KEY_DOWN -> navigate 1
2673 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2674 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2676 | Glut.KEY_RIGHT ->
2677 if Glut.getModifiers () land Glut.active_ctrl != 0
2678 then (
2679 state.mode <- Outline (
2680 allowdel, active, first, outlines,
2681 qsearch, min 0 (pan + 1), oldmode
2683 Glut.postRedisplay ();
2685 else (
2686 if not allowdel
2687 then updownlevel 1
2690 | Glut.KEY_LEFT ->
2691 if Glut.getModifiers () land Glut.active_ctrl != 0
2692 then (
2693 state.mode <- Outline (
2694 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2696 Glut.postRedisplay ();
2698 else (
2699 if not allowdel
2700 then updownlevel ~-1
2703 | Glut.KEY_HOME ->
2704 state.mode <- Outline (
2705 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2707 Glut.postRedisplay ()
2709 | Glut.KEY_END ->
2710 let active = Array.length outlines - 1 in
2711 let first = max 0 (active - maxrows) in
2712 state.mode <- Outline (
2713 allowdel, active, first, outlines, qsearch, pan, oldmode
2715 Glut.postRedisplay ()
2717 | _ -> ()
2720 let drawplaceholder l =
2721 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2722 GlDraw.rect
2723 (float l.pagex, float l.pagedispy)
2724 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2726 let x = if margin < 0 then -margin else l.pagex
2727 and y = l.pagedispy + 13 in
2728 GlDraw.color (0.0, 0.0, 0.0);
2729 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2732 let now () = Unix.gettimeofday ();;
2734 let drawpage l =
2735 let color =
2736 match state.mode with
2737 | Textentry _ -> scalecolor 0.4
2738 | View | Outline _ | Items _ -> scalecolor 1.0
2739 | Birdseye (_, _, pageno, hooverpageno, _) ->
2740 if l.pageno = hooverpageno
2741 then scalecolor 0.9
2742 else (
2743 if l.pageno = pageno
2744 then scalecolor 1.0
2745 else scalecolor 0.8
2748 GlDraw.color color;
2749 begin match getopaque l.pageno with
2750 | Some (opaque, _) when validopaque opaque ->
2751 let a = now () in
2752 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2753 let b = now () in
2754 let d = b-.a in
2755 vlog "draw %d %f sec" l.pageno d;
2757 | _ ->
2758 drawplaceholder l;
2759 end;
2762 let scrollph y =
2763 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2764 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2765 let sh = float conf.winh /. sh in
2766 let sh = max sh (float conf.scrollh) in
2768 let percent =
2769 if state.y = state.maxy
2770 then 1.0
2771 else float y /. float maxy
2773 let position = (float conf.winh -. sh) *. percent in
2775 let position =
2776 if position +. sh > float conf.winh
2777 then float conf.winh -. sh
2778 else position
2780 position, sh;
2783 let scrollindicator () =
2784 GlDraw.color (0.64 , 0.64, 0.64);
2785 GlDraw.rect
2786 (float (conf.winw - state.scrollw), 0.)
2787 (float conf.winw, float conf.winh)
2789 GlDraw.color (0.0, 0.0, 0.0);
2791 let position, sh = scrollph state.y in
2792 GlDraw.rect
2793 (float (conf.winw - state.scrollw), position)
2794 (float conf.winw, position +. sh)
2798 let showsel margin =
2799 match state.mstate with
2800 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2803 | Msel ((x0, y0), (x1, y1)) ->
2804 let rec loop = function
2805 | l :: ls ->
2806 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2807 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2808 then
2809 match getopaque l.pageno with
2810 | Some (opaque, _) when validopaque opaque ->
2811 let oy = -l.pagey + l.pagedispy in
2812 seltext opaque
2813 (x0 - margin - state.x, y0,
2814 x1 - margin - state.x, y1) oy;
2816 | _ -> ()
2817 else loop ls
2818 | [] -> ()
2820 loop state.layout
2823 let showrects () =
2824 let panx = float state.x in
2825 Gl.enable `blend;
2826 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2827 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2828 List.iter
2829 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2830 List.iter (fun l ->
2831 if l.pageno = pageno
2832 then (
2833 let d = float (l.pagedispy - l.pagey) in
2834 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2835 GlDraw.begins `quads;
2837 GlDraw.vertex2 (x0+.panx, y0+.d);
2838 GlDraw.vertex2 (x1+.panx, y1+.d);
2839 GlDraw.vertex2 (x2+.panx, y2+.d);
2840 GlDraw.vertex2 (x3+.panx, y3+.d);
2842 GlDraw.ends ();
2844 ) state.layout
2845 ) state.rects
2847 Gl.disable `blend;
2850 let showstrings trusted active first pan strings =
2851 Gl.enable `blend;
2852 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2853 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2854 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2855 GlDraw.color (1., 1., 1.);
2856 Gl.enable `texture_2d;
2858 let wx = measurestr 15 "w" in
2859 let tabx = 30.0*.wx +. float (pan*15) in
2860 let rec loop row =
2861 if row = Array.length strings || (row - first) * 16 > conf.winh
2862 then ()
2863 else (
2864 let (s, level, _) = strings.(row) in
2865 let y = (row - first) * 16 in
2866 let x = 5 + 15*(max 0 (level+pan)) in
2867 if row = active
2868 then (
2869 Gl.disable `texture_2d;
2870 GlDraw.polygon_mode `both `line;
2871 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2872 GlDraw.rect (0., float (y + 1))
2873 (float (conf.winw - 1), float (y + 18));
2874 GlDraw.polygon_mode `both `fill;
2875 GlDraw.color (1., 1., 1.);
2876 Gl.enable `texture_2d;
2879 let drawtabularstring x s =
2880 let _ =
2881 if trusted
2882 then
2883 let tabpos = try String.index s '\t' with Not_found -> -1 in
2884 if tabpos > 0
2885 then
2886 let len = String.length s - tabpos - 1 in
2887 let s1 = String.sub s 0 tabpos
2888 and s2 = String.sub s (tabpos + 1) len in
2889 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2890 let x = truncate (max xx tabx) in
2891 drawstring1 15 x (y + 16) s2
2892 else
2893 drawstring1 15 x (y + 16) s
2894 else
2895 drawstring1 15 x (y + 16) s
2899 drawtabularstring (x + pan*15) s;
2900 loop (row+1)
2903 loop first;
2904 Gl.disable `blend;
2905 Gl.disable `texture_2d;
2908 let showoutline (_, active, first, outlines, _, pan, _) =
2909 showstrings false active first pan outlines;
2912 let showitems (active, first, items, _, pan, _) =
2913 showstrings true active first pan items;
2916 let display () =
2917 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2918 GlDraw.viewport margin 0 state.w conf.winh;
2919 pagematrix ();
2920 GlClear.color (scalecolor2 conf.bgcolor);
2921 GlClear.clear [`color];
2922 if conf.zoom > 1.0
2923 then (
2924 Gl.enable `scissor_test;
2925 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2927 List.iter drawpage state.layout;
2928 if conf.zoom > 1.0
2929 then
2930 Gl.disable `scissor_test
2932 if state.x != 0
2933 then (
2934 let x = -.float state.x in
2935 GlMat.translate ~x ();
2937 showrects ();
2938 showsel margin;
2939 GlDraw.viewport 0 0 conf.winw conf.winh;
2940 winmatrix ();
2941 scrollindicator ();
2942 begin match state.mode with
2943 | Items items -> showitems items
2944 | Outline outline -> showoutline outline
2945 | _ -> ()
2946 end;
2947 enttext ();
2948 Glut.swapBuffers ();
2951 let getunder x y =
2952 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2953 let x = x - margin - state.x in
2954 let rec f = function
2955 | l :: rest ->
2956 begin match getopaque l.pageno with
2957 | Some (opaque, _) when validopaque opaque ->
2958 let y = y - l.pagedispy in
2959 if y > 0
2960 then
2961 let y = l.pagey + y in
2962 let x = x - l.pagex in
2963 match whatsunder opaque x y with
2964 | Unone -> f rest
2965 | under -> under
2966 else
2967 f rest
2968 | _ ->
2969 f rest
2971 | [] -> Unone
2973 f state.layout
2976 let viewmouse button bstate x y =
2977 match button with
2978 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2979 if Glut.getModifiers () land Glut.active_ctrl != 0
2980 then (
2981 match state.mstate with
2982 | Mzoom (oldn, i) ->
2983 if oldn = n
2984 then (
2985 if i = 2
2986 then
2987 let incr =
2988 match n with
2989 | 4 ->
2990 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2991 | _ ->
2992 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2994 let zoom = conf.zoom +. incr in
2995 setzoom zoom;
2996 state.mstate <- Mzoom (n, 0);
2997 else
2998 state.mstate <- Mzoom (n, i+1);
3000 else state.mstate <- Mzoom (n, 0)
3002 | _ -> state.mstate <- Mzoom (n, 0)
3004 else (
3005 match state.autoscroll with
3006 | Some step -> setautoscrollspeed step (n=4)
3007 | None ->
3008 let incr =
3009 if n = 3
3010 then -conf.scrollstep
3011 else conf.scrollstep
3013 let incr = incr * 2 in
3014 let y = clamp incr in
3015 gotoy_and_clear_text y
3018 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3019 if bstate = Glut.DOWN
3020 then (
3021 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3022 state.mstate <- Mpan (x, y)
3024 else
3025 state.mstate <- Mnone
3027 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3028 if bstate = Glut.DOWN
3029 then
3030 let position, sh = scrollph state.y in
3031 if y > truncate position && y < truncate (position +. sh)
3032 then
3033 state.mstate <- Mscroll
3034 else
3035 let percent = float y /. float conf.winh in
3036 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3037 gotoy desty;
3038 state.mstate <- Mscroll
3039 else
3040 state.mstate <- Mnone
3042 | Glut.LEFT_BUTTON ->
3043 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3044 begin match dest with
3045 | Ulinkgoto (pageno, top) ->
3046 if pageno >= 0
3047 then (
3048 addnav ();
3049 gotopage1 pageno top;
3052 | Ulinkuri s ->
3053 print_endline s
3055 | Unone when bstate = Glut.DOWN ->
3056 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3057 state.mstate <- Mpan (x, y);
3059 | Unone | Utext _ ->
3060 if bstate = Glut.DOWN
3061 then (
3062 if conf.angle mod 360 = 0
3063 then (
3064 state.mstate <- Msel ((x, y), (x, y));
3065 Glut.postRedisplay ()
3068 else (
3069 match state.mstate with
3070 | Mnone -> ()
3072 | Mzoom _ | Mscroll ->
3073 state.mstate <- Mnone
3075 | Mpan _ ->
3076 Glut.setCursor Glut.CURSOR_INHERIT;
3077 state.mstate <- Mnone
3079 | Msel ((_, y0), (_, y1)) ->
3080 let f l =
3081 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3082 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3083 then
3084 match getopaque l.pageno with
3085 | Some (opaque, _) when validopaque opaque ->
3086 copysel opaque
3087 | _ -> ()
3089 List.iter f state.layout;
3090 copysel ""; (* ugly *)
3091 Glut.setCursor Glut.CURSOR_INHERIT;
3092 state.mstate <- Mnone;
3096 | _ -> ()
3099 let birdseyemouse button bstate x y
3100 (conf, leftx, _, hooverpageno, anchor) =
3101 match button with
3102 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3103 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3104 let rec loop = function
3105 | [] -> ()
3106 | l :: rest ->
3107 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3108 && x > margin && x < margin + l.pagew
3109 then (
3110 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3112 else loop rest
3114 loop state.layout
3115 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3116 | _ -> ()
3119 let mouse bstate button x y =
3120 match state.mode with
3121 | View -> viewmouse button bstate x y
3122 | Birdseye beye -> birdseyemouse button bstate x y beye
3123 | Textentry _ | Outline _ | Items _ -> ()
3126 let mouse ~button ~state ~x ~y = mouse state button x y;;
3128 let motion ~x ~y =
3129 match state.mode with
3130 | Outline _ -> ()
3131 | _ ->
3132 match state.mstate with
3133 | Mzoom _ | Mnone -> ()
3135 | Mpan (x0, y0) ->
3136 let dx = x - x0
3137 and dy = y0 - y in
3138 state.mstate <- Mpan (x, y);
3139 if conf.zoom > 1.0 then state.x <- state.x + dx;
3140 let y = clamp dy in
3141 gotoy_and_clear_text y
3143 | Msel (a, _) ->
3144 state.mstate <- Msel (a, (x, y));
3145 Glut.postRedisplay ()
3147 | Mscroll ->
3148 let y = min conf.winh (max 0 y) in
3149 let percent = float y /. float conf.winh in
3150 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3151 gotoy_and_clear_text y
3154 let pmotion ~x ~y =
3155 match state.mode with
3156 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3157 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3158 let rec loop = function
3159 | [] ->
3160 if hooverpageno != -1
3161 then (
3162 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3163 Glut.postRedisplay ();
3165 | l :: rest ->
3166 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3167 && x > margin && x < margin + l.pagew
3168 then (
3169 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3170 Glut.postRedisplay ();
3172 else loop rest
3174 loop state.layout
3176 | Outline _ | Items _ | Textentry _ -> ()
3177 | View ->
3178 match state.mstate with
3179 | Mnone ->
3180 begin match getunder x y with
3181 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3182 | Ulinkuri uri ->
3183 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3184 Glut.setCursor Glut.CURSOR_INFO
3185 | Ulinkgoto (page, _) ->
3186 if conf.underinfo
3187 then showtext 'p' ("age: " ^ string_of_int page);
3188 Glut.setCursor Glut.CURSOR_INFO
3189 | Utext s ->
3190 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3191 Glut.setCursor Glut.CURSOR_TEXT
3194 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3199 module State =
3200 struct
3201 open Parser
3203 let home =
3205 match Sys.os_type with
3206 | "Win32" -> Sys.getenv "HOMEPATH"
3207 | _ -> Sys.getenv "HOME"
3208 with exn ->
3209 prerr_endline
3210 ("Can not determine home directory location: " ^
3211 Printexc.to_string exn);
3215 let config_of c attrs =
3216 let apply c k v =
3218 match k with
3219 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3220 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3221 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3222 | "preload" -> { c with preload = bool_of_string v }
3223 | "page-bias" -> { c with pagebias = int_of_string v }
3224 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3225 | "auto-scroll-step" ->
3226 { c with autoscrollstep = max 0 (int_of_string v) }
3227 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3228 | "crop-hack" -> { c with crophack = bool_of_string v }
3229 | "throttle" -> { c with showall = bool_of_string v }
3230 | "highlight-links" -> { c with hlinks = bool_of_string v }
3231 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3232 | "vertical-margin" ->
3233 { c with interpagespace = max 0 (int_of_string v) }
3234 | "zoom" ->
3235 let zoom = float_of_string v /. 100. in
3236 let zoom = max 0.01 zoom in
3237 { c with zoom = zoom }
3238 | "presentation" -> { c with presentation = bool_of_string v }
3239 | "rotation-angle" -> { c with angle = int_of_string v }
3240 | "width" -> { c with winw = max 20 (int_of_string v) }
3241 | "height" -> { c with winh = max 20 (int_of_string v) }
3242 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3243 | "proportional-display" -> { c with proportional = bool_of_string v }
3244 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3245 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3246 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3247 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3248 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3249 | "persistent-location" -> { c with jumpback = bool_of_string v }
3250 | "background-color" -> { c with bgcolor = color_of_string v }
3251 | "scrollbar-in-presentation" ->
3252 { c with scrollbarinpm = bool_of_string v }
3253 | "ui-font" -> { c with uifont = v }
3254 | _ -> c
3255 with exn ->
3256 prerr_endline ("Error processing attribute (`" ^
3257 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3260 let rec fold c = function
3261 | [] -> c
3262 | (k, v) :: rest ->
3263 let c = apply c k v in
3264 fold c rest
3266 fold c attrs;
3269 let fromstring f pos n v d =
3270 try f v
3271 with exn ->
3272 dolog "Error processing attribute (%S=%S) at %d\n%s"
3273 n v pos (Printexc.to_string exn)
3278 let bookmark_of attrs =
3279 let rec fold title page rely = function
3280 | ("title", v) :: rest -> fold v page rely rest
3281 | ("page", v) :: rest -> fold title v rely rest
3282 | ("rely", v) :: rest -> fold title page v rest
3283 | _ :: rest -> fold title page rely rest
3284 | [] -> title, page, rely
3286 fold "invalid" "0" "0" attrs
3289 let doc_of attrs =
3290 let rec fold path page rely pan = function
3291 | ("path", v) :: rest -> fold v page rely pan rest
3292 | ("page", v) :: rest -> fold path v rely pan rest
3293 | ("rely", v) :: rest -> fold path page v pan rest
3294 | ("pan", v) :: rest -> fold path page rely v rest
3295 | _ :: rest -> fold path page rely pan rest
3296 | [] -> path, page, rely, pan
3298 fold "" "0" "0" "0" attrs
3301 let setconf dst src =
3302 dst.scrollbw <- src.scrollbw;
3303 dst.scrollh <- src.scrollh;
3304 dst.icase <- src.icase;
3305 dst.preload <- src.preload;
3306 dst.pagebias <- src.pagebias;
3307 dst.verbose <- src.verbose;
3308 dst.scrollstep <- src.scrollstep;
3309 dst.maxhfit <- src.maxhfit;
3310 dst.crophack <- src.crophack;
3311 dst.autoscrollstep <- src.autoscrollstep;
3312 dst.showall <- src.showall;
3313 dst.hlinks <- src.hlinks;
3314 dst.underinfo <- src.underinfo;
3315 dst.interpagespace <- src.interpagespace;
3316 dst.zoom <- src.zoom;
3317 dst.presentation <- src.presentation;
3318 dst.angle <- src.angle;
3319 dst.winw <- src.winw;
3320 dst.winh <- src.winh;
3321 dst.savebmarks <- src.savebmarks;
3322 dst.memlimit <- src.memlimit;
3323 dst.proportional <- src.proportional;
3324 dst.texcount <- src.texcount;
3325 dst.sliceheight <- src.sliceheight;
3326 dst.blockwidth <- src.blockwidth;
3327 dst.thumbw <- src.thumbw;
3328 dst.jumpback <- src.jumpback;
3329 dst.bgcolor <- src.bgcolor;
3330 dst.scrollbarinpm <- src.scrollbarinpm;
3331 dst.uifont <- src.uifont;
3334 let unent s =
3335 let l = String.length s in
3336 let b = Buffer.create l in
3337 unent b s 0 l;
3338 Buffer.contents b;
3341 let get s =
3342 let h = Hashtbl.create 10 in
3343 let dc = { defconf with angle = defconf.angle } in
3344 let rec toplevel v t spos _ =
3345 match t with
3346 | Vdata | Vcdata | Vend -> v
3347 | Vopen ("llppconfig", _, closed) ->
3348 if closed
3349 then v
3350 else { v with f = llppconfig }
3351 | Vopen _ ->
3352 error "unexpected subelement at top level" s spos
3353 | Vclose _ -> error "unexpected close at top level" s spos
3355 and llppconfig v t spos _ =
3356 match t with
3357 | Vdata | Vcdata | Vend -> v
3358 | Vopen ("defaults", attrs, closed) ->
3359 let c = config_of dc attrs in
3360 setconf dc c;
3361 if closed
3362 then v
3363 else { v with f = skip "defaults" (fun () -> v) }
3365 | Vopen ("doc", attrs, closed) ->
3366 let pathent, spage, srely, span = doc_of attrs in
3367 let path = unent pathent
3368 and pageno = fromstring int_of_string spos "page" spage 0
3369 and rely = fromstring float_of_string spos "rely" srely 0.0
3370 and pan = fromstring int_of_string spos "pan" span 0 in
3371 let c = config_of dc attrs in
3372 let anchor = (pageno, rely) in
3373 if closed
3374 then (Hashtbl.add h path (c, [], pan, anchor); v)
3375 else { v with f = doc path pan anchor c [] }
3377 | Vopen _ ->
3378 error "unexpected subelement in llppconfig" s spos
3380 | Vclose "llppconfig" -> { v with f = toplevel }
3381 | Vclose _ -> error "unexpected close in llppconfig" s spos
3383 and doc path pan anchor c bookmarks v t spos _ =
3384 match t with
3385 | Vdata | Vcdata -> v
3386 | Vend -> error "unexpected end of input in doc" s spos
3387 | Vopen ("bookmarks", _, closed) ->
3388 if closed
3389 then v
3390 else { v with f = pbookmarks path pan anchor c bookmarks }
3392 | Vopen (_, _, _) ->
3393 error "unexpected subelement in doc" s spos
3395 | Vclose "doc" ->
3396 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3397 { v with f = llppconfig }
3399 | Vclose _ -> error "unexpected close in doc" s spos
3401 and pbookmarks path pan anchor c bookmarks v t spos _ =
3402 match t with
3403 | Vdata | Vcdata -> v
3404 | Vend -> error "unexpected end of input in bookmarks" s spos
3405 | Vopen ("item", attrs, closed) ->
3406 let titleent, spage, srely = bookmark_of attrs in
3407 let page = fromstring int_of_string spos "page" spage 0
3408 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3409 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3410 if closed
3411 then { v with f = pbookmarks path pan anchor c bookmarks }
3412 else
3413 let f () = v in
3414 { v with f = skip "item" f }
3416 | Vopen _ ->
3417 error "unexpected subelement in bookmarks" s spos
3419 | Vclose "bookmarks" ->
3420 { v with f = doc path pan anchor c bookmarks }
3422 | Vclose _ -> error "unexpected close in bookmarks" s spos
3424 and skip tag f v t spos _ =
3425 match t with
3426 | Vdata | Vcdata -> v
3427 | Vend ->
3428 error ("unexpected end of input in skipped " ^ tag) s spos
3429 | Vopen (tag', _, closed) ->
3430 if closed
3431 then v
3432 else
3433 let f' () = { v with f = skip tag f } in
3434 { v with f = skip tag' f' }
3435 | Vclose ctag ->
3436 if tag = ctag
3437 then f ()
3438 else error ("unexpected close in skipped " ^ tag) s spos
3441 parse { f = toplevel; accu = () } s;
3442 h, dc;
3445 let do_load f ic =
3447 let len = in_channel_length ic in
3448 let s = String.create len in
3449 really_input ic s 0 len;
3450 f s;
3451 with
3452 | Parse_error (msg, s, pos) ->
3453 let subs = subs s pos in
3454 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3455 failwith ("parse error: " ^ s)
3457 | exn ->
3458 failwith ("config load error: " ^ Printexc.to_string exn)
3461 let path =
3462 let dir =
3464 let dir = Filename.concat home ".config" in
3465 if Sys.is_directory dir then dir else home
3466 with _ -> home
3468 Filename.concat dir "llpp.conf"
3471 let load1 f =
3472 if Sys.file_exists path
3473 then
3474 match
3475 (try Some (open_in_bin path)
3476 with exn ->
3477 prerr_endline
3478 ("Error opening configuation file `" ^ path ^ "': " ^
3479 Printexc.to_string exn);
3480 None
3482 with
3483 | Some ic ->
3484 begin try
3485 f (do_load get ic)
3486 with exn ->
3487 prerr_endline
3488 ("Error loading configuation from `" ^ path ^ "': " ^
3489 Printexc.to_string exn);
3490 end;
3491 close_in ic;
3493 | None -> ()
3494 else
3495 f (Hashtbl.create 0, defconf)
3498 let load () =
3499 let f (h, dc) =
3500 let pc, pb, px, pa =
3502 Hashtbl.find h (Filename.basename state.path)
3503 with Not_found -> dc, [], 0, (0, 0.0)
3505 setconf defconf dc;
3506 setconf conf pc;
3507 state.bookmarks <- pb;
3508 state.x <- px;
3509 state.scrollw <- conf.scrollbw;
3510 if conf.jumpback
3511 then state.anchor <- pa;
3512 cbput state.hists.nav pa;
3514 load1 f
3517 let add_attrs bb always dc c =
3518 let ob s a b =
3519 if always || a != b
3520 then Printf.bprintf bb "\n %s='%b'" s a
3521 and oi s a b =
3522 if always || a != b
3523 then Printf.bprintf bb "\n %s='%d'" s a
3524 and oz s a b =
3525 if always || a <> b
3526 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3527 and oc s a b =
3528 if always || a <> b
3529 then
3530 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3531 and os s a b =
3532 if always || a <> b
3533 then
3534 Printf.bprintf bb "\n %s='%s'" s a
3536 let w, h =
3537 if always
3538 then dc.winw, dc.winh
3539 else
3540 match state.fullscreen with
3541 | Some wh -> wh
3542 | None -> c.winw, c.winh
3544 let zoom, presentation, interpagespace, showall=
3545 if always
3546 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3547 else
3548 match state.mode with
3549 | Birdseye (bc, _, _, _, _) ->
3550 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3551 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3553 oi "width" w dc.winw;
3554 oi "height" h dc.winh;
3555 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3556 oi "scroll-handle-height" c.scrollh dc.scrollh;
3557 ob "case-insensitive-search" c.icase dc.icase;
3558 ob "preload" c.preload dc.preload;
3559 oi "page-bias" c.pagebias dc.pagebias;
3560 oi "scroll-step" c.scrollstep dc.scrollstep;
3561 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3562 ob "max-height-fit" c.maxhfit dc.maxhfit;
3563 ob "crop-hack" c.crophack dc.crophack;
3564 ob "throttle" showall dc.showall;
3565 ob "highlight-links" c.hlinks dc.hlinks;
3566 ob "under-cursor-info" c.underinfo dc.underinfo;
3567 oi "vertical-margin" interpagespace dc.interpagespace;
3568 oz "zoom" zoom dc.zoom;
3569 ob "presentation" presentation dc.presentation;
3570 oi "rotation-angle" c.angle dc.angle;
3571 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3572 ob "proportional-display" c.proportional dc.proportional;
3573 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3574 oi "tex-count" c.texcount dc.texcount;
3575 oi "slice-height" c.sliceheight dc.sliceheight;
3576 oi "block-width" c.blockwidth dc.blockwidth;
3577 oi "thumbnail-width" c.thumbw dc.thumbw;
3578 ob "persistent-location" c.jumpback dc.jumpback;
3579 oc "background-color" c.bgcolor dc.bgcolor;
3580 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3581 os "ui-font" c.uifont dc.uifont;
3584 let save () =
3585 let bb = Buffer.create 32768 in
3586 let f (h, dc) =
3587 let dc = if conf.bedefault then conf else dc in
3588 Buffer.add_string bb "<llppconfig>\n<defaults ";
3589 add_attrs bb true dc dc;
3590 Buffer.add_string bb "/>\n";
3592 let adddoc path pan anchor c bookmarks =
3593 if bookmarks == [] && c = dc && anchor = emptyanchor
3594 then ()
3595 else (
3596 Printf.bprintf bb "<doc path='%s'"
3597 (enent path 0 (String.length path));
3599 if anchor <> emptyanchor
3600 then (
3601 let n, y = anchor in
3602 Printf.bprintf bb " page='%d'" n;
3603 if y > 1e-6
3604 then
3605 Printf.bprintf bb " rely='%f'" y
3609 if pan != 0
3610 then Printf.bprintf bb " pan='%d'" pan;
3612 add_attrs bb false dc c;
3614 begin match bookmarks with
3615 | [] -> Buffer.add_string bb "/>\n"
3616 | _ ->
3617 Buffer.add_string bb ">\n<bookmarks>\n";
3618 List.iter (fun (title, _level, (page, rely)) ->
3619 Printf.bprintf bb
3620 "<item title='%s' page='%d'"
3621 (enent title 0 (String.length title))
3622 page
3624 if rely > 1e-6
3625 then
3626 Printf.bprintf bb " rely='%f'" rely
3628 Buffer.add_string bb "/>\n";
3629 ) bookmarks;
3630 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3631 end;
3635 let pan =
3636 match state.mode with
3637 | Birdseye (_, pan, _, _, _) -> pan
3638 | _ -> state.x
3640 let basename = Filename.basename state.path in
3641 adddoc basename pan (getanchor ())
3642 { conf with
3643 autoscrollstep =
3644 match state.autoscroll with
3645 | Some step -> step
3646 | None -> conf.autoscrollstep }
3647 (if conf.savebmarks then state.bookmarks else []);
3649 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3650 if basename <> path
3651 then adddoc path x y c bookmarks
3652 ) h;
3653 Buffer.add_string bb "</llppconfig>";
3655 load1 f;
3656 if Buffer.length bb > 0
3657 then
3659 let tmp = path ^ ".tmp" in
3660 let oc = open_out_bin tmp in
3661 Buffer.output_buffer oc bb;
3662 close_out oc;
3663 Sys.rename tmp path;
3664 with exn ->
3665 prerr_endline
3666 ("error while saving configuration: " ^ Printexc.to_string exn)
3668 end;;
3670 let () =
3671 Arg.parse
3672 (Arg.align
3673 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3674 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3675 " Print version and exit")]
3677 (fun s -> state.path <- s)
3678 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3680 if String.length state.path = 0
3681 then (prerr_endline "file name missing"; exit 1);
3683 State.load ();
3685 let _ = Glut.init Sys.argv in
3686 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3687 let () = Glut.initWindowSize conf.winw conf.winh in
3688 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3690 let csock, ssock =
3691 if Sys.os_type = "Unix"
3692 then
3693 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3694 else
3695 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3696 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3697 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3698 Unix.bind sock addr;
3699 Unix.listen sock 1;
3700 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3701 Unix.connect csock addr;
3702 let ssock, _ = Unix.accept sock in
3703 Unix.close sock;
3704 let opts sock =
3705 Unix.setsockopt sock Unix.TCP_NODELAY true;
3706 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3708 opts ssock;
3709 opts csock;
3710 ssock, csock
3713 let () = Glut.displayFunc display in
3714 let () = Glut.reshapeFunc reshape in
3715 let () = Glut.keyboardFunc keyboard in
3716 let () = Glut.specialFunc special in
3717 let () = Glut.idleFunc (Some idle) in
3718 let () = Glut.mouseFunc mouse in
3719 let () = Glut.motionFunc motion in
3720 let () = Glut.passiveMotionFunc pmotion in
3722 init ssock (
3723 conf.angle, conf.proportional, conf.texcount,
3724 conf.sliceheight, conf.blockwidth, conf.uifont
3726 state.csock <- csock;
3727 state.ssock <- ssock;
3728 state.text <- "Opening " ^ state.path;
3729 writeopen state.path state.password;
3731 let rec handlelablglutbug () =
3733 Glut.mainLoop ();
3734 with
3735 | Glut.BadEnum "key in special_of_int" ->
3736 showtext '!' " LablGlut bug: special key not recognized";
3737 handlelablglutbug ()
3739 | Quit->
3740 if Sys.os_type <> "Unix"
3741 then Unix.shutdown ssock Unix.SHUTDOWN_ALL;
3742 State.save ()
3744 handlelablglutbug ();