Make builtin font loading work
[llpp.git] / main.ml
blob85c4eabbd775abaecbbebdba25845d98c2cd838b
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params = angle * proportional * texcount * sliceheight * fontpath
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
25 and fontpath = string
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstring : int -> int -> int -> string -> unit = "ml_draw_string";;
38 type mpos = int * int
39 and mstate =
40 | Msel of (mpos * mpos)
41 | Mpan of mpos
42 | Mscroll
43 | Mzoom of (int * int)
44 | Mnone
47 type textentry = string * string * onhist option * onkey * ondone
48 and onkey = string -> int -> te
49 and ondone = string -> unit
50 and histcancel = unit -> unit
51 and onhist = ((histcmd -> string) * histcancel)
52 and histcmd = HCnext | HCprev | HCfirst | HClast
53 and te =
54 | TEstop
55 | TEdone of string
56 | TEcont of string
57 | TEswitch of textentry
60 type 'a circbuf =
61 { store : 'a array
62 ; mutable rc : int
63 ; mutable wc : int
64 ; mutable len : int
68 let cbnew n v =
69 { store = Array.create n v
70 ; rc = 0
71 ; wc = 0
72 ; len = 0
76 let cbcap b = Array.length b.store;;
78 let cbput b v =
79 let cap = cbcap b in
80 b.store.(b.wc) <- v;
81 b.wc <- (b.wc + 1) mod cap;
82 b.rc <- b.wc;
83 b.len <- min (b.len + 1) cap;
86 let cbempty b = b.len = 0;;
88 let cbgetg b circular dir =
89 if cbempty b
90 then b.store.(0)
91 else
92 let rc = b.rc + dir in
93 let rc =
94 if circular
95 then (
96 if rc = -1
97 then b.len-1
98 else (
99 if rc = b.len
100 then 0
101 else rc
104 else max 0 (min rc (b.len-1))
106 b.rc <- rc;
107 b.store.(rc);
110 let cbget b = cbgetg b false;;
111 let cbgetc b = cbgetg b true;;
113 let cbpeek b =
114 let rc = b.wc - b.len in
115 let rc = if rc < 0 then cbcap b + rc else rc in
116 b.store.(rc);
119 let cbdecr b = b.len <- b.len - 1;;
121 type layout =
122 { pageno : int
123 ; pagedimno : int
124 ; pagew : int
125 ; pageh : int
126 ; pagedispy : int
127 ; pagey : int
128 ; pagevh : int
129 ; pagex : int
133 type conf =
134 { mutable scrollbw : int
135 ; mutable scrollh : int
136 ; mutable icase : bool
137 ; mutable preload : bool
138 ; mutable pagebias : int
139 ; mutable verbose : bool
140 ; mutable scrollstep : int
141 ; mutable maxhfit : bool
142 ; mutable crophack : bool
143 ; mutable autoscrollstep : int
144 ; mutable showall : bool
145 ; mutable hlinks : bool
146 ; mutable underinfo : bool
147 ; mutable interpagespace : interpagespace
148 ; mutable zoom : float
149 ; mutable presentation : bool
150 ; mutable angle : angle
151 ; mutable winw : int
152 ; mutable winh : int
153 ; mutable savebmarks : bool
154 ; mutable proportional : proportional
155 ; mutable memlimit : int
156 ; mutable texcount : texcount
157 ; mutable sliceheight : sliceheight
158 ; mutable thumbw : width
159 ; mutable jumpback : bool
160 ; mutable bgcolor : float * float * float
161 ; mutable bedefault : bool
162 ; mutable scrollbarinpm : bool
163 ; mutable uifont : string
167 type anchor = pageno * top;;
169 type outline = string * int * anchor
170 and outlines =
171 | Oarray of outline array
172 | Olist of outline list
173 | Onarrow of string * outline array * outline array
176 type rect = float * float * float * float * float * float * float * float;;
178 type pagemapkey = pageno * width * angle * proportional * gen;;
180 let emptyanchor = (0, 0.0);;
181 let initialanchor = (-1, nan);;
183 type mode =
184 | Birdseye of (conf * leftx * pageno * pageno * anchor)
185 | Outline of (bool * int * int * outline array * string * int * mode)
186 | Items of (int * int * item array * string * int * mode)
187 | Textentry of (textentry * onleave)
188 | View
189 and onleave = leavetextentrystatus -> unit
190 and leavetextentrystatus = | Cancel | Confirm
191 and item = string * int * action
192 and action =
193 | Noaction
194 | Action of (int -> int -> string -> int -> mode)
197 let isbirdseye = function Birdseye _ -> true | _ -> false;;
198 let istextentry = function Textentry _ -> true | _ -> false;;
200 type state =
201 { mutable csock : Unix.file_descr
202 ; mutable ssock : Unix.file_descr
203 ; mutable w : int
204 ; mutable x : int
205 ; mutable y : int
206 ; mutable scrollw : int
207 ; mutable anchor : anchor
208 ; mutable maxy : int
209 ; mutable layout : layout list
210 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
211 ; mutable pdims : (pageno * width * height * leftx) list
212 ; mutable pagecount : int
213 ; pagecache : string circbuf
214 ; mutable rendering : bool
215 ; mutable mstate : mstate
216 ; mutable searchpattern : string
217 ; mutable rects : (pageno * recttype * rect) list
218 ; mutable rects1 : (pageno * recttype * rect) list
219 ; mutable text : string
220 ; mutable fullscreen : (width * height) option
221 ; mutable mode : mode
222 ; mutable outlines : outlines
223 ; mutable bookmarks : outline list
224 ; mutable path : string
225 ; mutable password : string
226 ; mutable invalidated : int
227 ; mutable colorscale : float
228 ; mutable memused : int
229 ; mutable gen : gen
230 ; mutable throttle : layout list option
231 ; mutable ascrollstep : int
232 ; mutable help : item array
233 ; mutable docinfo : (int * string) list
234 ; hists : hists
236 and hists =
237 { pat : string circbuf
238 ; pag : string circbuf
239 ; nav : anchor circbuf
243 let defconf =
244 { scrollbw = 7
245 ; scrollh = 12
246 ; icase = true
247 ; preload = true
248 ; pagebias = 0
249 ; verbose = false
250 ; scrollstep = 24
251 ; maxhfit = true
252 ; crophack = false
253 ; autoscrollstep = 24
254 ; showall = false
255 ; hlinks = false
256 ; underinfo = false
257 ; interpagespace = 2
258 ; zoom = 1.0
259 ; presentation = false
260 ; angle = 0
261 ; winw = 900
262 ; winh = 900
263 ; savebmarks = true
264 ; proportional = true
265 ; memlimit = 32*1024*1024
266 ; texcount = 256
267 ; sliceheight = 24
268 ; thumbw = 76
269 ; jumpback = false
270 ; bgcolor = (0.5, 0.5, 0.5)
271 ; bedefault = false
272 ; scrollbarinpm = true
273 ; uifont = ""
277 let conf = { defconf with angle = defconf.angle };;
279 let makehelp () =
280 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
281 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
284 let state =
285 { csock = Unix.stdin
286 ; ssock = Unix.stdin
287 ; x = 0
288 ; y = 0
289 ; w = 0
290 ; scrollw = 0
291 ; anchor = initialanchor
292 ; layout = []
293 ; maxy = max_int
294 ; pagemap = Hashtbl.create 10
295 ; pagecache = cbnew 100 ""
296 ; pdims = []
297 ; pagecount = 0
298 ; rendering = false
299 ; mstate = Mnone
300 ; rects = []
301 ; rects1 = []
302 ; text = ""
303 ; mode = View
304 ; fullscreen = None
305 ; searchpattern = ""
306 ; outlines = Olist []
307 ; bookmarks = []
308 ; path = ""
309 ; password = ""
310 ; invalidated = 0
311 ; hists =
312 { nav = cbnew 100 (0, 0.0)
313 ; pat = cbnew 20 ""
314 ; pag = cbnew 10 ""
316 ; colorscale = 1.0
317 ; memused = 0
318 ; gen = 0
319 ; throttle = None
320 ; ascrollstep = 0
321 ; help = makehelp ()
322 ; docinfo = []
326 let vlog fmt =
327 if conf.verbose
328 then
329 Printf.kprintf prerr_endline fmt
330 else
331 Printf.kprintf ignore fmt
334 let writecmd fd s =
335 let len = String.length s in
336 let n = 4 + len in
337 let b = Buffer.create n in
338 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
339 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
340 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
341 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
342 Buffer.add_string b s;
343 let s' = Buffer.contents b in
344 let n' = Unix.write fd s' 0 n in
345 if n' != n then failwith "write failed";
348 let readcmd fd =
349 let s = "xxxx" in
350 let n = Unix.read fd s 0 4 in
351 if n != 4 then failwith "incomplete read(len)";
352 let len = 0
353 lor (Char.code s.[0] lsl 24)
354 lor (Char.code s.[1] lsl 16)
355 lor (Char.code s.[2] lsl 8)
356 lor (Char.code s.[3] lsl 0)
358 let s = String.create len in
359 let n = Unix.read fd s 0 len in
360 if n != len then failwith "incomplete read(data)";
364 let makecmd s l =
365 let b = Buffer.create 10 in
366 Buffer.add_string b s;
367 let rec combine = function
368 | [] -> b
369 | x :: xs ->
370 Buffer.add_char b ' ';
371 let s =
372 match x with
373 | `b b -> if b then "1" else "0"
374 | `s s -> s
375 | `i i -> string_of_int i
376 | `f f -> string_of_float f
377 | `I f -> string_of_int (truncate f)
379 Buffer.add_string b s;
380 combine xs;
382 combine l;
385 let wcmd s l =
386 let cmd = Buffer.contents (makecmd s l) in
387 writecmd state.csock cmd;
390 let calcips h =
391 if conf.presentation
392 then
393 let d = conf.winh - h in
394 max 0 ((d + 1) / 2)
395 else
396 conf.interpagespace
399 let calcheight () =
400 let rec f pn ph pi fh l =
401 match l with
402 | (n, _, h, _) :: rest ->
403 let ips = calcips h in
404 let fh =
405 if conf.presentation
406 then fh+ips
407 else (
408 if isbirdseye state.mode && pn = 0
409 then fh + ips
410 else fh
413 let fh = fh + ((n - pn) * (ph + pi)) in
414 f n h ips fh rest;
416 | [] ->
417 let inc =
418 if conf.presentation || (isbirdseye state.mode && pn = 0)
419 then 0
420 else -pi
422 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
423 max 0 fh
425 let fh = f 0 0 0 0 state.pdims in
429 let getpageyh pageno =
430 let rec f pn ph pi y l =
431 match l with
432 | (n, _, h, _) :: rest ->
433 let ips = calcips h in
434 if n >= pageno
435 then
436 let h = if n = pageno then h else ph in
437 if conf.presentation && n = pageno
438 then
439 y + (pageno - pn) * (ph + pi) + pi, h
440 else
441 y + (pageno - pn) * (ph + pi), h
442 else
443 let y = y + (if conf.presentation then pi else 0) in
444 let y = y + (n - pn) * (ph + pi) in
445 f n h ips y rest
447 | [] ->
448 y + (pageno - pn) * (ph + pi), ph
450 f 0 0 0 0 state.pdims
453 let getpagey pageno = fst (getpageyh pageno);;
455 let layout y sh =
456 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
457 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
458 match pdims with
459 | (pageno', w, h, x) :: rest when pageno' = pageno ->
460 let ips = calcips h in
461 let yinc =
462 if conf.presentation || (isbirdseye state.mode && pageno = 0)
463 then ips
464 else 0
466 (w, h, ips, x), rest, pdimno + 1, yinc
467 | _ ->
468 prev, pdims, pdimno, 0
470 let dy = dy + yinc in
471 let py = py + yinc in
472 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
473 then
474 accu
475 else
476 let vy = y + dy in
477 if py + h <= vy - yinc
478 then
479 let py = py + h + ips in
480 let dy = max 0 (py - y) in
481 f ~pageno:(pageno+1)
482 ~pdimno
483 ~prev:curr
486 ~pdims:rest
487 ~cacheleft
488 ~accu
489 else
490 let pagey = vy - py in
491 let pagevh = h - pagey in
492 let pagevh = min (sh - dy) pagevh in
493 let off = if yinc > 0 then py - vy else 0 in
494 let py = py + h + ips in
495 let e =
496 { pageno = pageno
497 ; pagedimno = pdimno
498 ; pagew = w
499 ; pageh = h
500 ; pagedispy = dy + off
501 ; pagey = pagey + off
502 ; pagevh = pagevh - off
503 ; pagex = x
506 let accu = e :: accu in
507 f ~pageno:(pageno+1)
508 ~pdimno
509 ~prev:curr
511 ~dy:(dy+pagevh+ips)
512 ~pdims:rest
513 ~cacheleft:(cacheleft-1)
514 ~accu
516 if state.invalidated = 0
517 then (
518 let accu =
520 ~pageno:0
521 ~pdimno:~-1
522 ~prev:(0,0,0,0)
523 ~py:0
524 ~dy:0
525 ~pdims:state.pdims
526 ~cacheleft:(cbcap state.pagecache)
527 ~accu:[]
529 List.rev accu
531 else
535 let clamp incr =
536 let y = state.y + incr in
537 let y = max 0 y in
538 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
542 let getopaque pageno =
543 try Some (Hashtbl.find state.pagemap
544 (pageno, state.w, conf.angle, conf.proportional, state.gen))
545 with Not_found -> None
548 let cache pageno opaque =
549 Hashtbl.replace state.pagemap
550 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
553 let validopaque opaque = String.length opaque > 0;;
555 let render l =
556 match getopaque l.pageno with
557 | None when not state.rendering ->
558 state.rendering <- true;
559 cache l.pageno ("", -1);
560 wcmd "render" [`i (l.pageno + 1)
561 ;`i l.pagedimno
562 ;`i l.pagew
563 ;`i l.pageh];
564 | _ -> ()
567 let loadlayout layout =
568 let rec f all = function
569 | l :: ls ->
570 begin match getopaque l.pageno with
571 | None -> render l; f false ls
572 | Some (opaque, _) -> f (all && validopaque opaque) ls
574 | [] -> all
576 f (layout <> []) layout;
579 let findpageforopaque opaque =
580 Hashtbl.fold
581 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
582 state.pagemap None
585 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
587 let preload () =
588 let oktopreload =
589 if conf.preload
590 then
591 let memleft = conf.memlimit - state.memused in
592 if memleft < 0
593 then
594 let opaque = cbpeek state.pagecache in
595 match findpageforopaque opaque with
596 | Some ((n, _, _, _, _), size) ->
597 memleft + size >= 0 && not (pagevisible state.layout n)
598 | None -> false
599 else true
600 else false
602 if oktopreload
603 then
604 let presentation = conf.presentation in
605 let interpagespace = conf.interpagespace in
606 let maxy = state.maxy in
607 conf.presentation <- false;
608 conf.interpagespace <- 0;
609 state.maxy <- calcheight ();
610 let y =
611 match state.layout with
612 | [] -> 0
613 | l :: _ -> getpagey l.pageno + l.pagey
615 let y = if y < conf.winh then 0 else y - conf.winh in
616 let pages = layout y (conf.winh*3) in
617 List.iter render pages;
618 conf.presentation <- presentation;
619 conf.interpagespace <- interpagespace;
620 state.maxy <- maxy;
623 let gotoy y =
624 let y = max 0 y in
625 let y = min state.maxy y in
626 let pages = layout y conf.winh in
627 let ready = loadlayout pages in
628 if conf.showall
629 then (
630 if ready
631 then (
632 state.y <- y;
633 state.layout <- pages;
634 state.throttle <- None;
635 Glut.postRedisplay ();
637 else (
638 state.throttle <- Some pages;
641 else (
642 state.y <- y;
643 state.layout <- pages;
644 state.throttle <- None;
645 Glut.postRedisplay ();
647 begin match state.mode with
648 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
649 if not (pagevisible pages pageno)
650 then (
651 match state.layout with
652 | [] -> ()
653 | l :: _ ->
654 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
656 | _ -> ()
657 end;
658 preload ();
661 let gotoy_and_clear_text y =
662 gotoy y;
663 if not conf.verbose then state.text <- "";
666 let getanchor () =
667 match state.layout with
668 | [] -> emptyanchor
669 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
672 let getanchory (n, top) =
673 let y, h = getpageyh n in
674 y + (truncate (top *. float h));
677 let gotoanchor anchor =
678 gotoy (getanchory anchor);
681 let addnav () =
682 cbput state.hists.nav (getanchor ());
685 let getnav dir =
686 let anchor = cbgetc state.hists.nav dir in
687 getanchory anchor;
690 let gotopage n top =
691 let y, h = getpageyh n in
692 gotoy_and_clear_text (y + (truncate (top *. float h)));
695 let gotopage1 n top =
696 let y = getpagey n in
697 gotoy_and_clear_text (y + top);
700 let invalidate () =
701 state.layout <- [];
702 state.pdims <- [];
703 state.rects <- [];
704 state.rects1 <- [];
705 state.invalidated <- state.invalidated + 1;
708 let scalecolor c =
709 let c = c *. state.colorscale in
710 (c, c, c);
713 let scalecolor2 (r, g, b) =
714 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
717 let represent () =
718 state.maxy <- calcheight ();
719 match state.mode with
720 | Birdseye (_, _, pageno, _, _) ->
721 let y, h = getpageyh pageno in
722 let top = (conf.winh - h) / 2 in
723 gotoy (max 0 (y - top))
724 | _ -> gotoanchor state.anchor
727 let pagematrix () =
728 GlMat.mode `projection;
729 GlMat.load_identity ();
730 GlMat.rotate ~x:1.0 ~angle:180.0 ();
731 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
732 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
733 if state.x != 0
734 then (
735 GlMat.translate ~x:(float state.x) ();
739 let winmatrix () =
740 GlMat.mode `projection;
741 GlMat.load_identity ();
742 GlMat.rotate ~x:1.0 ~angle:180.0 ();
743 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
744 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
747 let reshape ~w ~h =
748 if state.invalidated = 0 && state.anchor == initialanchor
749 then state.anchor <- getanchor ();
751 conf.winw <- w;
752 let w = truncate (float w *. conf.zoom) - state.scrollw in
753 let w = max w 2 in
754 state.w <- w;
755 conf.winh <- h;
756 GlMat.mode `modelview;
757 GlMat.load_identity ();
758 GlClear.color (scalecolor 1.0);
759 GlClear.clear [`color];
761 invalidate ();
762 wcmd "geometry" [`i w; `i h];
765 let enttext () =
766 let drawstring s =
767 GlDraw.color (0.0, 0.0, 0.0);
768 GlDraw.rect
769 (0.0, float (conf.winh - 18))
770 (float (conf.winw - state.scrollw - 1), float conf.winh)
772 GlDraw.color (1.0, 1.0, 1.0);
773 drawstring 14 8 (conf.winh - 5) s;
775 let len = String.length state.text in
776 match state.mode with
777 | Textentry ((prefix, text, _, _, _), _) ->
778 let s =
779 match String.length prefix with
780 | 0 | 1 ->
781 if len > 0
782 then
783 Printf.sprintf "%s%s\226–� [%s]" prefix text state.text
784 else
785 Printf.sprintf "%s%s\226–�" prefix text
787 | _ ->
788 if len > 0
789 then
790 Printf.sprintf "%s: %s\226–� [%s]" prefix text state.text
791 else
792 Printf.sprintf "%s: %s\226–�" prefix text
794 drawstring s
796 | _ ->
797 if len > 0 then drawstring state.text
800 let showtext c s =
801 state.text <- Printf.sprintf "%c%s" c s;
802 Glut.postRedisplay ();
805 let act cmd =
806 match cmd.[0] with
807 | 'c' ->
808 state.pdims <- [];
810 | 'D' ->
811 state.rects <- state.rects1;
812 Glut.postRedisplay ()
814 | 'C' ->
815 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
816 state.pagecount <- n;
817 state.invalidated <- state.invalidated - 1;
818 if state.invalidated = 0
819 then represent ()
821 | 't' ->
822 let s = Scanf.sscanf cmd "t %n"
823 (fun n -> String.sub cmd n (String.length cmd - n))
825 Glut.setWindowTitle s
827 | 'T' ->
828 let s = Scanf.sscanf cmd "T %n"
829 (fun n -> String.sub cmd n (String.length cmd - n))
831 if istextentry state.mode
832 then (
833 state.text <- s;
834 showtext ' ' s;
836 else (
837 state.text <- s;
838 Glut.postRedisplay ();
841 | 'V' ->
842 if conf.verbose
843 then
844 let s = Scanf.sscanf cmd "V %n"
845 (fun n -> String.sub cmd n (String.length cmd - n))
847 state.text <- s;
848 showtext ' ' s;
850 | 'F' ->
851 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
852 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
853 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
854 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
856 let y = (getpagey pageno) + truncate y0 in
857 addnav ();
858 gotoy y;
859 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
861 | 'R' ->
862 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
863 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
864 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
865 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
867 state.rects1 <-
868 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
870 | 'r' ->
871 let n, w, _h, r, l, s, p =
872 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
873 (fun n w h r l s p ->
874 (n-1, w, h, r, l != 0, s, p))
877 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
878 state.memused <- state.memused + s;
880 let layout =
881 match state.throttle with
882 | None -> state.layout
883 | Some layout -> layout
886 let rec gc () =
887 if (state.memused <= conf.memlimit) || cbempty state.pagecache
888 then ()
889 else (
890 let evictedopaque = cbpeek state.pagecache in
891 match findpageforopaque evictedopaque with
892 | None -> failwith "bug in gc"
893 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
894 if state.gen != gen || not (pagevisible layout evictedn)
895 then (
896 wcmd "free" [`s evictedopaque];
897 state.memused <- state.memused - evictedsize;
898 Hashtbl.remove state.pagemap k;
899 cbdecr state.pagecache;
900 gc ();
904 gc ();
906 cbput state.pagecache p;
907 state.rendering <- false;
909 begin match state.throttle with
910 | None ->
911 if pagevisible state.layout n
912 then gotoy state.y
913 else (
914 let allvisible = loadlayout state.layout in
915 if allvisible then preload ();
918 | Some layout ->
919 match layout with
920 | [] -> ()
921 | l :: _ ->
922 let y = getpagey l.pageno + l.pagey in
923 gotoy y
926 | 'l' ->
927 let pdim =
928 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
930 state.pdims <- pdim :: state.pdims
932 | 'o' ->
933 let (l, n, t, h, pos) =
934 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
936 let s = String.sub cmd pos (String.length cmd - pos) in
937 let outline = (s, l, (n, float t /. float h)) in
938 let outlines =
939 match state.outlines with
940 | Olist outlines -> Olist (outline :: outlines)
941 | Oarray _ -> Olist [outline]
942 | Onarrow _ -> Olist [outline]
944 state.outlines <- outlines
947 | 'i' ->
948 let s = Scanf.sscanf cmd "i %n"
949 (fun n -> String.sub cmd n (String.length cmd - n))
951 let len = String.length s in
952 let rec fold accu pos =
953 let eolpos =
954 try String.index_from s pos '\n' with Not_found -> len
956 if eolpos = len
957 then List.rev accu
958 else
959 let line = String.sub s pos (eolpos - pos) in
960 fold ((1, line)::accu) (eolpos+1)
962 state.docinfo <- fold state.docinfo 0
964 | _ ->
965 dolog "unknown cmd `%S'" cmd
968 let now = Unix.gettimeofday;;
970 let idle () =
971 let rec loop delay =
972 let r, _, _ = Unix.select [state.csock] [] [] delay in
973 begin match r with
974 | [] ->
975 if state.ascrollstep > 0
976 then begin
977 let y = state.y + state.ascrollstep in
978 let y = if y >= state.maxy then 0 else y in
979 gotoy y;
980 state.text <- "";
981 end;
983 | _ ->
984 let cmd = readcmd state.csock in
985 act cmd;
986 loop 0.0
987 end;
988 in loop 0.001
991 let onhist cb =
992 let rc = cb.rc in
993 let action = function
994 | HCprev -> cbget cb ~-1
995 | HCnext -> cbget cb 1
996 | HCfirst -> cbget cb ~-(cb.rc)
997 | HClast -> cbget cb (cb.len - 1 - cb.rc)
998 and cancel () = cb.rc <- rc
999 in (action, cancel)
1002 let search pattern forward =
1003 if String.length pattern > 0
1004 then
1005 let pn, py =
1006 match state.layout with
1007 | [] -> 0, 0
1008 | l :: _ ->
1009 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1011 let cmd =
1012 let b = makecmd "search"
1013 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1015 Buffer.add_char b ',';
1016 Buffer.add_string b pattern;
1017 Buffer.add_char b '\000';
1018 Buffer.contents b;
1020 writecmd state.csock cmd;
1023 let intentry text key =
1024 let c = Char.unsafe_chr key in
1025 match c with
1026 | '0' .. '9' ->
1027 let s = "x" in s.[0] <- c;
1028 let text = text ^ s in
1029 TEcont text
1031 | _ ->
1032 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1033 TEcont text
1036 let addchar s c =
1037 let b = Buffer.create (String.length s + 1) in
1038 Buffer.add_string b s;
1039 Buffer.add_char b c;
1040 Buffer.contents b;
1043 let textentry text key =
1044 let c = Char.unsafe_chr key in
1045 match c with
1046 | _ when key >= 32 && key < 127 ->
1047 let text = addchar text c in
1048 TEcont text
1050 | _ ->
1051 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1052 TEcont text
1055 let reinit angle proportional =
1056 conf.angle <- angle;
1057 conf.proportional <- proportional;
1058 invalidate ();
1059 wcmd "reinit" [`i angle; `b proportional];
1062 let setzoom zoom =
1063 let zoom = max 0.01 (min 2.2 zoom) in
1064 if zoom <> conf.zoom
1065 then (
1066 if zoom <= 1.0
1067 then state.x <- 0;
1068 conf.zoom <- zoom;
1069 if state.invalidated = 0
1070 then
1071 state.anchor <- getanchor ()
1073 reshape conf.winw conf.winh;
1074 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1078 let enterbirdseye () =
1079 let zoom = float conf.thumbw /. float conf.winw in
1080 let birdseyepageno =
1081 let cy = conf.winh / 2 in
1082 let fold = function
1083 | [] -> 0
1084 | l :: rest ->
1085 let rec fold best = function
1086 | [] -> best.pageno
1087 | l :: rest ->
1088 let d = cy - (l.pagedispy + l.pagevh/2)
1089 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1090 if abs d < abs dbest
1091 then fold l rest
1092 else best.pageno
1093 in fold l rest
1095 fold state.layout
1097 state.mode <- Birdseye (
1098 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1100 conf.zoom <- zoom;
1101 conf.presentation <- false;
1102 conf.interpagespace <- 10;
1103 conf.hlinks <- false;
1104 state.x <- 0;
1105 state.mstate <- Mnone;
1106 conf.showall <- false;
1107 Glut.setCursor Glut.CURSOR_INHERIT;
1108 if conf.verbose
1109 then
1110 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1111 (100.0*.zoom)
1112 else
1113 state.text <- ""
1115 reshape conf.winw conf.winh;
1118 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1119 state.mode <- View;
1120 conf.zoom <- c.zoom;
1121 conf.presentation <- c.presentation;
1122 conf.interpagespace <- c.interpagespace;
1123 conf.showall <- c.showall;
1124 conf.hlinks <- c.hlinks;
1125 state.x <- leftx;
1126 if conf.verbose
1127 then
1128 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1129 (100.0*.conf.zoom)
1131 reshape conf.winw conf.winh;
1132 state.anchor <- if goback then anchor else (pageno, 0.0);
1135 let togglebirdseye () =
1136 match state.mode with
1137 | Birdseye vals -> leavebirdseye vals true
1138 | View | Outline _ -> enterbirdseye ()
1139 | _ -> ()
1142 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1143 let pageno = max 0 (pageno - 1) in
1144 let rec loop = function
1145 | [] -> gotopage1 pageno 0
1146 | l :: _ when l.pageno = pageno ->
1147 if l.pagedispy >= 0 && l.pagey = 0
1148 then Glut.postRedisplay ()
1149 else gotopage1 pageno 0
1150 | _ :: rest -> loop rest
1152 loop state.layout;
1153 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1156 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1157 let pageno = min (state.pagecount - 1) (pageno + 1) in
1158 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1159 let rec loop = function
1160 | [] ->
1161 let y, h = getpageyh pageno in
1162 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1163 gotoy (clamp dy)
1164 | l :: _ when l.pageno = pageno ->
1165 if l.pagevh != l.pageh
1166 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1167 else Glut.postRedisplay ()
1168 | _ :: rest -> loop rest
1170 loop state.layout
1173 let optentry mode _ key =
1174 let btos b = if b then "on" else "off" in
1175 let c = Char.unsafe_chr key in
1176 match c with
1177 | 's' ->
1178 let ondone s =
1179 try conf.scrollstep <- int_of_string s with exc ->
1180 state.text <- Printf.sprintf "bad integer `%s': %s"
1181 s (Printexc.to_string exc)
1183 TEswitch ("scroll step", "", None, intentry, ondone)
1185 | 'A' ->
1186 let ondone s =
1188 conf.autoscrollstep <- int_of_string s;
1189 if state.ascrollstep > 0
1190 then state.ascrollstep <- conf.autoscrollstep;
1191 with exc ->
1192 state.text <- Printf.sprintf "bad integer `%s': %s"
1193 s (Printexc.to_string exc)
1195 TEswitch ("auto scroll step", "", None, intentry, ondone)
1197 | 'Z' ->
1198 let ondone s =
1200 let zoom = float (int_of_string s) /. 100.0 in
1201 setzoom zoom
1202 with exc ->
1203 state.text <- Printf.sprintf "bad integer `%s': %s"
1204 s (Printexc.to_string exc)
1206 TEswitch ("zoom", "", None, intentry, ondone)
1208 | 't' ->
1209 let ondone s =
1211 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1212 state.text <-
1213 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1214 begin match mode with
1215 | Birdseye beye ->
1216 leavebirdseye beye false;
1217 enterbirdseye ();
1218 | _ -> ();
1220 with exc ->
1221 state.text <- Printf.sprintf "bad integer `%s': %s"
1222 s (Printexc.to_string exc)
1224 TEswitch ("thumbnail width", "", None, intentry, ondone)
1226 | 'R' ->
1227 let ondone s =
1228 match try
1229 Some (int_of_string s)
1230 with exc ->
1231 state.text <- Printf.sprintf "bad integer `%s': %s"
1232 s (Printexc.to_string exc);
1233 None
1234 with
1235 | Some angle -> reinit angle conf.proportional
1236 | None -> ()
1238 TEswitch ("rotation", "", None, intentry, ondone)
1240 | 'i' ->
1241 conf.icase <- not conf.icase;
1242 TEdone ("case insensitive search " ^ (btos conf.icase))
1244 | 'p' ->
1245 conf.preload <- not conf.preload;
1246 gotoy state.y;
1247 TEdone ("preload " ^ (btos conf.preload))
1249 | 'v' ->
1250 conf.verbose <- not conf.verbose;
1251 TEdone ("verbose " ^ (btos conf.verbose))
1253 | 'h' ->
1254 conf.maxhfit <- not conf.maxhfit;
1255 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1256 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1258 | 'c' ->
1259 conf.crophack <- not conf.crophack;
1260 TEdone ("crophack " ^ btos conf.crophack)
1262 | 'a' ->
1263 conf.showall <- not conf.showall;
1264 TEdone ("throttle " ^ btos conf.showall)
1266 | 'f' ->
1267 conf.underinfo <- not conf.underinfo;
1268 TEdone ("underinfo " ^ btos conf.underinfo)
1270 | 'P' ->
1271 conf.savebmarks <- not conf.savebmarks;
1272 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1274 | 'S' ->
1275 let ondone s =
1277 let pageno, py =
1278 match state.layout with
1279 | [] -> 0, 0
1280 | l :: _ ->
1281 l.pageno, l.pagey
1283 conf.interpagespace <- int_of_string s;
1284 state.maxy <- calcheight ();
1285 let y = getpagey pageno in
1286 gotoy (y + py)
1287 with exc ->
1288 state.text <- Printf.sprintf "bad integer `%s': %s"
1289 s (Printexc.to_string exc)
1291 TEswitch ("vertical margin", "", None, intentry, ondone)
1293 | 'l' ->
1294 reinit conf.angle (not conf.proportional);
1295 TEdone ("proprortional display " ^ btos conf.proportional)
1297 | _ ->
1298 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1299 TEstop
1302 let maxoutlinerows () = (conf.winh - 31) / 16;;
1304 let enterselector allowdel outlines errmsg msg =
1305 if Array.length outlines = 0
1306 then (
1307 showtext ' ' errmsg;
1309 else (
1310 state.text <- msg;
1311 Glut.setCursor Glut.CURSOR_INHERIT;
1312 let pageno =
1313 match state.layout with
1314 | [] -> -1
1315 | {pageno=pageno} :: _ -> pageno
1317 let active =
1318 let rec loop n =
1319 if n = Array.length outlines
1320 then 0
1321 else
1322 let (_, _, (outlinepageno, _)) = outlines.(n) in
1323 if outlinepageno >= pageno then n else loop (n+1)
1325 loop 0
1327 state.mode <- Outline
1328 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1329 state.mode);
1330 Glut.postRedisplay ();
1334 let enteroutlinemode () =
1335 let outlines, msg =
1336 match state.outlines with
1337 | Oarray a -> a, ""
1338 | Olist l ->
1339 let a = Array.of_list (List.rev l) in
1340 state.outlines <- Oarray a;
1341 a, ""
1342 | Onarrow (pat, a, _) ->
1343 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1345 enterselector false outlines "Document has no outline" msg;
1348 let enterbookmarkmode () =
1349 let bookmarks = Array.of_list state.bookmarks in
1350 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1353 let mode_to_string mode =
1354 let b = Buffer.create 10 in
1355 let rec f = function
1356 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1357 | View -> Buffer.add_string b "View"
1358 | Birdseye _ -> Buffer.add_string b "Birdseye"
1359 | Items _ -> Buffer.add_string b "Items"
1360 | Outline _ -> Buffer.add_string b "Outline"
1362 f mode;
1363 Buffer.contents b;
1366 let color_of_string s =
1367 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1368 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1372 let color_to_string (r, g, b) =
1373 let r = truncate (r *. 256.0)
1374 and g = truncate (g *. 256.0)
1375 and b = truncate (b *. 256.0) in
1376 Printf.sprintf "%d/%d/%d" r g b
1379 let enterinfomode () =
1380 let btos = function true -> "on" | _ -> "off" in
1381 let mode = state.mode in
1382 let rec makeitems () =
1383 let intp name get set =
1384 Printf.sprintf "%-24s %d" name (get ()), 1, Action (
1385 fun active first _ pan ->
1386 let ondone s =
1387 let n =
1388 try int_of_string s
1389 with exn ->
1390 state.text <- Printf.sprintf "bad integer `%s': %s"
1391 s (Printexc.to_string exn);
1392 max_int;
1394 if n != max_int then set n;
1396 let te = name, "", None, intentry, ondone in
1397 state.text <- "";
1398 Textentry (
1400 fun _ ->
1401 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1404 and boolp name get set =
1405 Printf.sprintf "%-24s %s" name (btos (get ())), 1, Action (
1406 fun active first qsearch pan ->
1407 let v = get () in
1408 set (not v);
1409 Items (active, first, makeitems (), qsearch, pan, mode);
1411 and colorp name get set =
1412 Printf.sprintf "%-24s %s" name (color_to_string (get ())), 1, Action (
1413 fun active first _ pan ->
1414 let invalid = (nan, nan, nan) in
1415 let ondone s =
1416 let c =
1417 try color_of_string s
1418 with exn ->
1419 state.text <- Printf.sprintf "bad color `%s': %s"
1420 s (Printexc.to_string exn);
1421 invalid
1423 if c <> invalid
1424 then set c;
1426 let te = name, "", None, textentry, ondone in
1427 state.text <- "";
1428 Textentry (
1430 fun _ ->
1431 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1436 let birdseye = isbirdseye mode in
1437 let items = [
1438 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1440 boolp "presentation"
1441 (fun () -> conf.presentation)
1442 (fun v ->
1443 conf.presentation <- v;
1444 state.anchor <- getanchor ();
1445 represent ());
1447 boolp "ignore case in searches"
1448 (fun () -> conf.icase)
1449 (fun v -> conf.icase <- v);
1451 boolp "preload"
1452 (fun () -> conf.preload)
1453 (fun v -> conf.preload <- v);
1455 boolp "verbose"
1456 (fun () -> conf.verbose)
1457 (fun v -> conf.verbose <- v);
1459 boolp "max fit"
1460 (fun () -> conf.maxhfit)
1461 (fun v -> conf.maxhfit <- v);
1463 boolp "crop hack"
1464 (fun () -> conf.crophack)
1465 (fun v -> conf.crophack <- v);
1467 boolp "throttle"
1468 (fun () -> conf.showall)
1469 (fun v -> conf.showall <- v);
1471 boolp "highlight links"
1472 (fun () -> conf.hlinks)
1473 (fun v -> conf.hlinks <- v);
1475 boolp "under info"
1476 (fun () -> conf.underinfo)
1477 (fun v -> conf.underinfo <- v);
1478 boolp "persistent bookmarks"
1479 (fun () -> conf.savebmarks)
1480 (fun v -> conf.savebmarks <- v);
1482 boolp "proportional display"
1483 (fun () -> conf.proportional)
1484 (fun v -> reinit conf.angle v);
1486 boolp "persistent location"
1487 (fun () -> conf.jumpback)
1488 (fun v -> conf.jumpback <- v);
1490 "", 0, Noaction;
1492 intp "vertical margin"
1493 (fun () -> conf.interpagespace)
1494 (fun n ->
1495 conf.interpagespace <- n;
1496 let pageno, py =
1497 match state.layout with
1498 | [] -> 0, 0
1499 | l :: _ ->
1500 l.pageno, l.pagey
1502 state.maxy <- calcheight ();
1503 let y = getpagey pageno in
1504 gotoy (y + py)
1507 intp "page bias"
1508 (fun () -> conf.pagebias)
1509 (fun v -> conf.pagebias <- v);
1511 intp "scroll step"
1512 (fun () -> conf.scrollstep)
1513 (fun n -> conf.scrollstep <- n);
1515 intp "auto scroll step"
1516 (fun () ->
1517 if state.ascrollstep > 0
1518 then state.ascrollstep
1519 else conf.autoscrollstep)
1520 (fun n ->
1521 if state.ascrollstep > 0
1522 then state.ascrollstep <- n
1523 else conf.autoscrollstep <- n);
1525 intp "zoom"
1526 (fun () -> truncate (conf.zoom *. 100.))
1527 (fun v -> setzoom ((float v) /. 100.));
1529 intp "rotation"
1530 (fun () -> conf.angle)
1531 (fun v -> reinit v conf.proportional);
1533 intp "scroll bar width"
1534 (fun () -> state.scrollw)
1535 (fun v ->
1536 state.scrollw <- v;
1537 conf.scrollbw <- v;
1538 reshape conf.winw conf.winh;
1541 intp "scroll handle height"
1542 (fun () -> conf.scrollh)
1543 (fun v -> conf.scrollh <- v;);
1545 intp "thumbnail width"
1546 (fun () -> conf.thumbw)
1547 (fun v ->
1548 conf.thumbw <- min 1920 v;
1549 match mode with
1550 | Birdseye beye ->
1551 leavebirdseye beye false;
1552 enterbirdseye ()
1553 | _ -> ()
1556 colorp "background color"
1557 (fun () -> conf.bgcolor)
1558 (fun v -> conf.bgcolor <- v);
1560 "", 0, Noaction;
1561 "Presentation mode", 0, Noaction;
1563 boolp "scrollbar visible"
1564 (fun () -> conf.scrollbarinpm)
1565 (fun v ->
1566 if v != conf.scrollbarinpm
1567 then (
1568 conf.scrollbarinpm <- v;
1569 if conf.presentation
1570 then (
1571 state.scrollw <- if v then conf.scrollbw else 0;
1572 reshape conf.winw conf.winh;
1577 "", 0, Noaction;
1578 "Pixmap Cache", 0, Noaction;
1580 intp "size (advisory)"
1581 (fun () -> conf.memlimit)
1582 (fun v -> conf.memlimit <- v);
1583 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1587 let tailer =
1588 let trailer =
1589 ("", 0, Noaction)
1590 :: ("Document", 0, Noaction)
1591 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1593 if birdseye
1594 then trailer
1595 else (
1596 ("", 0, Noaction)
1597 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1598 (btos conf.bedefault),
1600 Action (
1601 fun active first qsearch pan ->
1602 conf.bedefault <- not conf.bedefault;
1603 Items (active, first, makeitems (), qsearch, pan, mode);
1605 ) :: trailer
1608 Array.of_list (items @ tailer)
1610 state.text <- "";
1611 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1612 Glut.postRedisplay ();
1615 let enterhelpmode () =
1616 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1617 Glut.postRedisplay ();
1620 let quickbookmark ?title () =
1621 match state.layout with
1622 | [] -> ()
1623 | l :: _ ->
1624 let title =
1625 match title with
1626 | None ->
1627 let sec = Unix.gettimeofday () in
1628 let tm = Unix.localtime sec in
1629 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1630 (l.pageno+1)
1631 tm.Unix.tm_mday
1632 tm.Unix.tm_mon
1633 (tm.Unix.tm_year + 1900)
1634 tm.Unix.tm_hour
1635 tm.Unix.tm_min
1636 | Some title -> title
1638 state.bookmarks <-
1639 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1640 :: state.bookmarks
1643 let doreshape w h =
1644 state.fullscreen <- None;
1645 Glut.reshapeWindow w h;
1648 let writeopen path password =
1649 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1650 writecmd state.csock "info";
1653 let opendoc path password =
1654 invalidate ();
1655 state.path <- path;
1656 state.password <- password;
1657 state.gen <- state.gen + 1;
1659 writeopen path password;
1660 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1661 wcmd "geometry" [`i state.w; `i conf.winh];
1664 let viewkeyboard key =
1665 let enttext te =
1666 let mode = state.mode in
1667 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1668 state.text <- "";
1669 enttext ();
1670 Glut.postRedisplay ()
1672 let c = Char.chr key in
1673 match c with
1674 | '\027' | 'q' -> (* escape *)
1675 exit 0
1677 | '\008' -> (* backspace *)
1678 let y = getnav ~-1 in
1679 gotoy_and_clear_text y
1681 | 'o' ->
1682 enteroutlinemode ()
1684 | 'u' ->
1685 state.rects <- [];
1686 state.text <- "";
1687 Glut.postRedisplay ()
1689 | '/' | '?' ->
1690 let ondone isforw s =
1691 cbput state.hists.pat s;
1692 state.searchpattern <- s;
1693 search s isforw
1695 let s = String.create 1 in
1696 s.[0] <- c;
1697 enttext (s, "", Some (onhist state.hists.pat),
1698 textentry, ondone (c ='/'))
1700 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1701 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1702 setzoom (min 2.2 (conf.zoom +. incr))
1704 | '+' ->
1705 let ondone s =
1706 let n =
1707 try int_of_string s with exc ->
1708 state.text <- Printf.sprintf "bad integer `%s': %s"
1709 s (Printexc.to_string exc);
1710 max_int
1712 if n != max_int
1713 then (
1714 conf.pagebias <- n;
1715 state.text <- "page bias is now " ^ string_of_int n;
1718 enttext ("page bias", "", None, intentry, ondone)
1720 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1721 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1722 setzoom (max 0.01 (conf.zoom -. decr))
1724 | '-' ->
1725 let ondone msg = state.text <- msg in
1726 enttext (
1727 "option [acfhilpstvAPRSZ]", "", None,
1728 optentry state.mode, ondone
1731 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1732 setzoom 1.0
1734 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1735 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1736 if zoom < 1.0
1737 then setzoom zoom
1739 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1740 togglebirdseye ()
1742 | '0' .. '9' ->
1743 let ondone s =
1744 let n =
1745 try int_of_string s with exc ->
1746 state.text <- Printf.sprintf "bad integer `%s': %s"
1747 s (Printexc.to_string exc);
1750 if n >= 0
1751 then (
1752 addnav ();
1753 cbput state.hists.pag (string_of_int n);
1754 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1757 let pageentry text key =
1758 match Char.unsafe_chr key with
1759 | 'g' -> TEdone text
1760 | _ -> intentry text key
1762 let text = "x" in text.[0] <- c;
1763 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1765 | 'b' ->
1766 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1767 reshape conf.winw conf.winh;
1769 | 'l' ->
1770 conf.hlinks <- not conf.hlinks;
1771 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1772 Glut.postRedisplay ()
1774 | 'a' ->
1775 if state.ascrollstep = 0
1776 then state.ascrollstep <- conf.autoscrollstep
1777 else (
1778 conf.autoscrollstep <- state.ascrollstep;
1779 state.ascrollstep <- 0;
1782 | 'P' ->
1783 conf.presentation <- not conf.presentation;
1784 if conf.presentation
1785 then (
1786 if not conf.scrollbarinpm
1787 then state.scrollw <- 0;
1789 else
1790 state.scrollw <- conf.scrollbw;
1792 showtext ' ' ("presentation mode " ^
1793 if conf.presentation then "on" else "off");
1794 state.anchor <- getanchor ();
1795 represent ()
1797 | 'f' ->
1798 begin match state.fullscreen with
1799 | None ->
1800 state.fullscreen <- Some (conf.winw, conf.winh);
1801 Glut.fullScreen ()
1802 | Some (w, h) ->
1803 state.fullscreen <- None;
1804 doreshape w h
1807 | 'g' ->
1808 gotoy_and_clear_text 0
1810 | 'n' ->
1811 search state.searchpattern true
1813 | 'p' | 'N' ->
1814 search state.searchpattern false
1816 | 't' ->
1817 begin match state.layout with
1818 | [] -> ()
1819 | l :: _ ->
1820 gotoy_and_clear_text (getpagey l.pageno)
1823 | ' ' ->
1824 begin match List.rev state.layout with
1825 | [] -> ()
1826 | l :: _ ->
1827 let pageno = min (l.pageno+1) (state.pagecount-1) in
1828 gotoy_and_clear_text (getpagey pageno)
1831 | '\127' -> (* delte *)
1832 begin match state.layout with
1833 | [] -> ()
1834 | l :: _ ->
1835 let pageno = max 0 (l.pageno-1) in
1836 gotoy_and_clear_text (getpagey pageno)
1839 | '=' ->
1840 let f (fn, _) l =
1841 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1843 let fn, ln = List.fold_left f (-1, -1) state.layout in
1844 let s =
1845 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1846 let percent =
1847 if maxy <= 0
1848 then 100.
1849 else (100. *. (float state.y /. float maxy)) in
1850 if fn = ln
1851 then
1852 Printf.sprintf "Page %d of %d %.2f%%"
1853 (fn+1) state.pagecount percent
1854 else
1855 Printf.sprintf
1856 "Pages %d-%d of %d %.2f%%"
1857 (fn+1) (ln+1) state.pagecount percent
1859 showtext ' ' s;
1861 | 'w' ->
1862 begin match state.layout with
1863 | [] -> ()
1864 | l :: _ ->
1865 doreshape (l.pagew + state.scrollw) l.pageh;
1866 Glut.postRedisplay ();
1869 | '\'' ->
1870 enterbookmarkmode ()
1872 | 'h' ->
1873 enterhelpmode ()
1875 | 'i' ->
1876 enterinfomode ()
1878 | 'm' ->
1879 let ondone s =
1880 match state.layout with
1881 | l :: _ ->
1882 state.bookmarks <-
1883 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1884 :: state.bookmarks
1885 | _ -> ()
1887 enttext ("bookmark", "", None, textentry, ondone)
1889 | '~' ->
1890 quickbookmark ();
1891 showtext ' ' "Quick bookmark added";
1893 | 'z' ->
1894 begin match state.layout with
1895 | l :: _ ->
1896 let rect = getpdimrect l.pagedimno in
1897 let w, h =
1898 if conf.crophack
1899 then
1900 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1901 truncate (1.2 *. (rect.(3) -. rect.(0))))
1902 else
1903 (truncate (rect.(1) -. rect.(0)),
1904 truncate (rect.(3) -. rect.(0)))
1906 if w != 0 && h != 0
1907 then
1908 doreshape (w + state.scrollw) (h + conf.interpagespace)
1910 Glut.postRedisplay ();
1912 | [] -> ()
1915 | '<' | '>' ->
1916 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1918 | '[' | ']' ->
1919 state.colorscale <-
1920 max 0.0
1921 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1922 Glut.postRedisplay ()
1924 | 'k' ->
1925 begin match state.mode with
1926 | Birdseye beye -> upbirdseye beye
1927 | _ -> gotoy (clamp (-conf.scrollstep))
1930 | 'j' ->
1931 begin match state.mode with
1932 | Birdseye beye -> downbirdseye beye
1933 | _ -> gotoy (clamp conf.scrollstep)
1936 | 'r' ->
1937 state.anchor <- getanchor ();
1938 opendoc state.path state.password
1940 | _ ->
1941 vlog "huh? %d %c" key (Char.chr key);
1944 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1945 let enttext te =
1946 state.mode <- Textentry (te, onleave);
1947 state.text <- "";
1948 enttext ();
1949 Glut.postRedisplay ()
1951 match Char.unsafe_chr key with
1952 | '\008' -> (* backspace *)
1953 let len = String.length text in
1954 if len = 0
1955 then (
1956 onleave Cancel;
1957 Glut.postRedisplay ();
1959 else (
1960 let s = String.sub text 0 (len - 1) in
1961 enttext (c, s, opthist, onkey, ondone)
1964 | '\r' | '\n' ->
1965 ondone text;
1966 onleave Confirm;
1967 Glut.postRedisplay ()
1969 | '\027' -> (* escape *)
1970 begin match opthist with
1971 | None -> ()
1972 | Some (_, onhistcancel) -> onhistcancel ()
1973 end;
1974 onleave Cancel;
1975 Glut.postRedisplay ()
1977 | _ ->
1978 begin match onkey text key with
1979 | TEdone text ->
1980 onleave Confirm;
1981 ondone text;
1982 Glut.postRedisplay ()
1984 | TEcont text ->
1985 enttext (c, text, opthist, onkey, ondone);
1987 | TEstop ->
1988 onleave Cancel;
1989 Glut.postRedisplay ()
1991 | TEswitch te ->
1992 state.mode <- Textentry (te, onleave);
1993 Glut.postRedisplay ()
1994 end;
1997 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
1998 match key with
1999 | 27 -> (* escape *)
2000 leavebirdseye beye true
2002 | 12 -> (* ctrl-l *)
2003 let y, h = getpageyh pageno in
2004 let top = (conf.winh - h) / 2 in
2005 gotoy (max 0 (y - top))
2007 | 13 -> (* enter *)
2008 leavebirdseye beye false
2010 | _ ->
2011 viewkeyboard key
2014 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2015 let set active first qsearch =
2016 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2018 let search active pattern incr =
2019 let dosearch re =
2020 let rec loop n =
2021 if n = Array.length items || n = -1
2022 then None
2023 else
2024 let (s, _, _) = items.(n) in
2026 (try ignore (Str.search_forward re s 0); true
2027 with Not_found -> false)
2028 then Some n
2029 else loop (n + incr)
2031 loop active
2034 let re = Str.regexp_case_fold pattern in
2035 dosearch re
2036 with Failure s ->
2037 state.text <- s;
2038 None
2040 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2041 match key with
2042 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2043 let incr = if key = 18 then -1 else 1 in
2044 let active, first =
2045 match search (active + incr) qsearch incr with
2046 | None ->
2047 state.text <- qsearch ^ " [not found]";
2048 active, first
2049 | Some active ->
2050 state.text <- qsearch;
2051 active, firstof active
2053 set active first qsearch;
2054 Glut.postRedisplay ();
2056 | 8 -> (* backspace *)
2057 let len = String.length qsearch in
2058 if len = 0
2059 then ()
2060 else (
2061 if len = 1
2062 then (
2063 state.text <- "";
2064 set active first "";
2066 else
2067 let qsearch = String.sub qsearch 0 (len - 1) in
2068 let active, first =
2069 match search active qsearch ~-1 with
2070 | None ->
2071 state.text <- qsearch ^ " [not found]";
2072 active, first
2073 | Some active ->
2074 state.text <- qsearch;
2075 active, firstof active
2077 set active first qsearch
2079 Glut.postRedisplay ()
2081 | _ when key >= 32 && key < 127 ->
2082 let pattern = addchar qsearch (Char.chr key) in
2083 let active, first =
2084 match search active pattern 1 with
2085 | None ->
2086 state.text <- pattern ^ " [not found]";
2087 active, first
2088 | Some active ->
2089 state.text <- pattern;
2090 active, firstof active
2092 set active first pattern;
2093 Glut.postRedisplay ()
2095 | 27 -> (* escape *)
2096 state.text <- "";
2097 state.mode <- oldmode;
2098 Glut.postRedisplay ();
2100 | 13 -> (* enter *)
2101 if active >= 0 && active < Array.length items
2102 then (
2103 match items.(active) with
2104 | _, _, Action f ->
2105 state.mode <- f active first qsearch pan
2107 | _, _, Noaction ->
2108 state.text <- "";
2109 state.mode <- oldmode
2111 else (
2112 state.text <- "";
2113 state.mode <- oldmode
2115 Glut.postRedisplay ();
2117 | _ -> dolog "unknown key %d" key
2120 let outlinekeyboard key
2121 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2122 let narrow outlines pattern =
2123 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2124 match reopt with
2125 | None -> None
2126 | Some re ->
2127 let rec fold accu n =
2128 if n = -1
2129 then accu
2130 else
2131 let (s, _, _) as o = outlines.(n) in
2132 let accu =
2133 if (try ignore (Str.search_forward re s 0); true
2134 with Not_found -> false)
2135 then (o :: accu)
2136 else accu
2138 fold accu (n-1)
2140 let matched = fold [] (Array.length outlines - 1) in
2141 if matched = [] then None else Some (Array.of_list matched)
2143 let search active pattern incr =
2144 let dosearch re =
2145 let rec loop n =
2146 if n = Array.length outlines || n = -1
2147 then None
2148 else
2149 let (s, _, _) = outlines.(n) in
2151 (try ignore (Str.search_forward re s 0); true
2152 with Not_found -> false)
2153 then Some n
2154 else loop (n + incr)
2156 loop active
2159 let re = Str.regexp_case_fold pattern in
2160 dosearch re
2161 with Failure s ->
2162 state.text <- s;
2163 None
2165 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2166 match key with
2167 | 27 -> (* escape *)
2168 if String.length qsearch = 0
2169 then (
2170 state.text <- "";
2171 state.mode <- oldmode;
2172 Glut.postRedisplay ();
2174 else (
2175 state.text <- "";
2176 state.mode <- Outline (
2177 allowdel, active, first, outlines, "", pan, oldmode
2179 Glut.postRedisplay ();
2182 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2183 let incr = if key = 18 then -1 else 1 in
2184 let active, first =
2185 match search (active + incr) qsearch incr with
2186 | None ->
2187 state.text <- qsearch ^ " [not found]";
2188 active, first
2189 | Some active ->
2190 state.text <- qsearch;
2191 active, firstof active
2193 state.mode <- Outline (
2194 allowdel, active, first, outlines, qsearch, pan, oldmode
2196 Glut.postRedisplay ();
2198 | 8 -> (* backspace *)
2199 let len = String.length qsearch in
2200 if len = 0
2201 then ()
2202 else (
2203 if len = 1
2204 then (
2205 state.text <- "";
2206 state.mode <- Outline (
2207 allowdel, active, first, outlines, "", pan, oldmode
2210 else
2211 let qsearch = String.sub qsearch 0 (len - 1) in
2212 let active, first =
2213 match search active qsearch ~-1 with
2214 | None ->
2215 state.text <- qsearch ^ " [not found]";
2216 active, first
2217 | Some active ->
2218 state.text <- qsearch;
2219 active, firstof active
2221 state.mode <- Outline (
2222 allowdel, active, first, outlines, qsearch, pan, oldmode
2225 Glut.postRedisplay ()
2227 | 13 -> (* enter *)
2228 if active < Array.length outlines
2229 then (
2230 let (_, _, anchor) = outlines.(active) in
2231 addnav ();
2232 gotoanchor anchor;
2234 state.text <- "";
2235 if allowdel then state.bookmarks <- Array.to_list outlines;
2236 state.mode <- oldmode;
2237 Glut.postRedisplay ();
2239 | _ when key >= 32 && key < 127 ->
2240 let pattern = addchar qsearch (Char.chr key) in
2241 let active, first =
2242 match search active pattern 1 with
2243 | None ->
2244 state.text <- pattern ^ " [not found]";
2245 active, first
2246 | Some active ->
2247 state.text <- pattern;
2248 active, firstof active
2250 state.mode <- Outline (
2251 allowdel, active, first, outlines, pattern, pan, oldmode
2253 Glut.postRedisplay ()
2255 | 14 when not allowdel -> (* ctrl-n *)
2256 if String.length qsearch > 0
2257 then (
2258 let optoutlines = narrow outlines qsearch in
2259 begin match optoutlines with
2260 | None -> state.text <- "can't narrow"
2261 | Some outlines ->
2262 state.mode <- Outline (
2263 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2265 match state.outlines with
2266 | Olist _ -> ()
2267 | Oarray a ->
2268 state.outlines <- Onarrow (qsearch, outlines, a)
2269 | Onarrow (_, _, b) ->
2270 state.outlines <- Onarrow (qsearch, outlines, b)
2271 end;
2273 Glut.postRedisplay ()
2275 | 21 when not allowdel -> (* ctrl-u *)
2276 let outline =
2277 match state.outlines with
2278 | Oarray a -> a
2279 | Olist l ->
2280 let a = Array.of_list (List.rev l) in
2281 state.outlines <- Oarray a;
2283 | Onarrow (_, _, b) ->
2284 state.outlines <- Oarray b;
2285 state.text <- "";
2288 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2289 Glut.postRedisplay ()
2291 | 12 -> (* ctrl-l *)
2292 state.mode <- Outline
2293 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2294 Glut.postRedisplay ()
2296 | 127 when allowdel -> (* delete *)
2297 let len = Array.length outlines - 1 in
2298 if len = 0
2299 then (
2300 state.mode <- View;
2301 state.bookmarks <- [];
2303 else (
2304 let bookmarks = Array.init len
2305 (fun i ->
2306 let i = if i >= active then i + 1 else i in
2307 outlines.(i)
2310 state.mode <-
2311 Outline (
2312 allowdel,
2313 min active (len-1),
2314 min first (len-1),
2315 bookmarks, qsearch,
2317 oldmode
2320 Glut.postRedisplay ()
2322 | _ -> dolog "unknown key %d" key
2325 let keyboard ~key ~x ~y =
2326 ignore x;
2327 ignore y;
2328 if key = 7 (* ctrl-g *)
2329 then
2330 wcmd "interrupt" []
2331 else
2332 match state.mode with
2333 | Outline outline -> outlinekeyboard key outline
2334 | Textentry textentry -> textentrykeyboard key textentry
2335 | Birdseye birdseye -> birdseyekeyboard key birdseye
2336 | View -> viewkeyboard key
2337 | Items items -> itemskeyboard key items
2340 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2341 match key with
2342 | Glut.KEY_UP -> upbirdseye beye
2343 | Glut.KEY_DOWN -> downbirdseye beye
2345 | Glut.KEY_PAGE_UP ->
2346 begin match state.layout with
2347 | l :: _ ->
2348 if l.pagey != 0
2349 then (
2350 state.mode <- Birdseye (
2351 conf, leftx, l.pageno, hooverpageno, anchor
2353 gotopage1 l.pageno 0;
2355 else (
2356 let layout = layout (state.y-conf.winh) conf.winh in
2357 match layout with
2358 | [] -> gotoy (clamp (-conf.winh))
2359 | l :: _ ->
2360 state.mode <- Birdseye (
2361 conf, leftx, l.pageno, hooverpageno, anchor
2363 gotopage1 l.pageno 0
2366 | [] -> gotoy (clamp (-conf.winh))
2367 end;
2369 | Glut.KEY_PAGE_DOWN ->
2370 begin match List.rev state.layout with
2371 | l :: _ ->
2372 let layout = layout (state.y + conf.winh) conf.winh in
2373 begin match layout with
2374 | [] ->
2375 let incr = l.pageh - l.pagevh in
2376 if incr = 0
2377 then (
2378 state.mode <-
2379 Birdseye (
2380 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2382 Glut.postRedisplay ();
2384 else gotoy (clamp (incr + conf.interpagespace*2));
2386 | l :: _ ->
2387 state.mode <-
2388 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2389 gotopage1 l.pageno 0;
2392 | [] -> gotoy (clamp conf.winh)
2393 end;
2395 | Glut.KEY_HOME ->
2396 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2397 gotopage1 0 0
2399 | Glut.KEY_END ->
2400 let pageno = state.pagecount - 1 in
2401 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2402 if not (pagevisible state.layout pageno)
2403 then
2404 let h =
2405 match List.rev state.pdims with
2406 | [] -> conf.winh
2407 | (_, _, h, _) :: _ -> h
2409 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2410 else Glut.postRedisplay ();
2411 | _ -> ()
2414 let setautoscrollspeed goingdown =
2415 let incr = max 1 (state.ascrollstep / 2) in
2416 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2417 state.ascrollstep <- astep;
2420 let special ~key ~x ~y =
2421 ignore x;
2422 ignore y;
2423 match state.mode with
2424 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2425 togglebirdseye ()
2427 | Birdseye vals ->
2428 birdseyespecial key vals
2430 | View when key = Glut.KEY_F1 ->
2431 enterhelpmode ()
2433 | View ->
2434 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2435 then setautoscrollspeed (key = Glut.KEY_DOWN)
2436 else
2437 let y =
2438 match key with
2439 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2440 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2441 | Glut.KEY_DOWN -> clamp conf.scrollstep
2442 | Glut.KEY_PAGE_UP ->
2443 if Glut.getModifiers () land Glut.active_ctrl != 0
2444 then
2445 match state.layout with
2446 | [] -> state.y
2447 | l :: _ -> state.y - l.pagey
2448 else
2449 clamp (-conf.winh)
2450 | Glut.KEY_PAGE_DOWN ->
2451 if Glut.getModifiers () land Glut.active_ctrl != 0
2452 then
2453 match List.rev state.layout with
2454 | [] -> state.y
2455 | l :: _ -> getpagey l.pageno
2456 else
2457 clamp conf.winh
2458 | Glut.KEY_HOME -> addnav (); 0
2459 | Glut.KEY_END ->
2460 addnav ();
2461 state.maxy - (if conf.maxhfit then conf.winh else 0)
2463 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2464 Glut.getModifiers () land Glut.active_alt != 0 ->
2465 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2467 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2468 state.x <- state.x - 10;
2469 state.y
2470 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2471 state.x <- state.x + 10;
2472 state.y
2474 | _ -> state.y
2476 gotoy_and_clear_text y
2478 | Textentry
2479 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2480 let s =
2481 match key with
2482 | Glut.KEY_UP -> action HCprev
2483 | Glut.KEY_DOWN -> action HCnext
2484 | Glut.KEY_HOME -> action HCfirst
2485 | Glut.KEY_END -> action HClast
2486 | _ -> state.text
2488 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2489 Glut.postRedisplay ()
2491 | Textentry _ -> ()
2493 | Items (active, first, items, qsearch, pan, oldmode) ->
2494 let maxrows = maxoutlinerows () in
2495 let itemcount = Array.length items in
2496 let hasaction = function
2497 | (_, _, Noaction) -> false
2498 | _ -> true
2500 let find start incr =
2501 let rec find i =
2502 if i = -1 || i = itemcount
2503 then -1
2504 else (
2505 if hasaction items.(i)
2506 then i
2507 else find (i + incr)
2510 find start
2512 let set active first =
2513 let first = max 0 (min first (itemcount - maxrows)) in
2514 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2516 let navigate incr =
2517 let isvisible first n = n >= first && n - first <= maxrows in
2518 let active, first =
2519 let incr1 = if incr > 0 then 1 else -1 in
2520 if isvisible first active
2521 then
2522 let next =
2523 let next = active + incr in
2524 let next =
2525 if next < 0 || next >= itemcount
2526 then -1
2527 else find next incr1
2529 if next = -1 || abs (active - next) > maxrows
2530 then -1
2531 else next
2533 if next = -1
2534 then
2535 let first = first + incr in
2536 let first = max 0 (min first (itemcount - 1)) in
2537 let next =
2538 let next = active + incr in
2539 let next = max 0 (min next (itemcount - 1)) in
2540 find next ~-incr1
2542 let active = if next = -1 then active else next in
2543 active, first
2544 else
2545 let first = min next first in
2546 next, first
2547 else
2548 let first = first + incr in
2549 let first = max 0 (min first (itemcount - 1)) in
2550 let active =
2551 let next = active + incr in
2552 let next = max 0 (min next (itemcount - 1)) in
2553 let next = find next incr1 in
2554 if next = -1 || abs (active - first) > maxrows
2555 then active
2556 else next
2558 active, first
2560 set active first;
2561 Glut.postRedisplay ()
2563 begin match key with
2564 | Glut.KEY_UP -> navigate ~-1
2565 | Glut.KEY_DOWN -> navigate 1
2566 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2567 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2569 | Glut.KEY_RIGHT ->
2570 state.mode <- Items (
2571 active, first, items, qsearch, min 0 (pan - 1), oldmode
2573 Glut.postRedisplay ()
2575 | Glut.KEY_LEFT ->
2576 state.mode <- Items (
2577 active, first, items, qsearch, min 0 (pan + 1), oldmode
2579 Glut.postRedisplay ()
2581 | Glut.KEY_HOME ->
2582 let active = find 0 1 in
2583 set active 0;
2584 Glut.postRedisplay ()
2586 | Glut.KEY_END ->
2587 let first = max 0 (itemcount - maxrows) in
2588 let active = find (itemcount - 1) ~-1 in
2589 set active first;
2590 Glut.postRedisplay ()
2592 | _ -> ()
2593 end;
2595 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2596 let maxrows = maxoutlinerows () in
2597 let calcfirst first active =
2598 if active > first
2599 then
2600 let rows = active - first in
2601 if rows > maxrows then active - maxrows else first
2602 else active
2604 let navigate incr =
2605 let active = active + incr in
2606 let active = max 0 (min active (Array.length outlines - 1)) in
2607 let first = calcfirst first active in
2608 state.mode <- Outline (
2609 allowdel, active, first, outlines, qsearch, pan, oldmode
2611 Glut.postRedisplay ()
2613 let updownlevel incr =
2614 let len = Array.length outlines in
2615 let (_, curlevel, _) = outlines.(active) in
2616 let rec flow i =
2617 if i = len then i-1 else if i = -1 then 0 else
2618 let (_, l, _) = outlines.(i) in
2619 if l != curlevel then i else flow (i+incr)
2621 let active = flow active in
2622 let first = calcfirst first active in
2623 state.mode <- Outline (
2624 allowdel, active, first, outlines, qsearch, pan, oldmode
2626 Glut.postRedisplay ()
2628 match key with
2629 | Glut.KEY_UP -> navigate ~-1
2630 | Glut.KEY_DOWN -> navigate 1
2631 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2632 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2634 | Glut.KEY_RIGHT ->
2635 if Glut.getModifiers () land Glut.active_ctrl != 0
2636 then (
2637 state.mode <- Outline (
2638 allowdel, active, first, outlines,
2639 qsearch, min 0 (pan + 1), oldmode
2641 Glut.postRedisplay ();
2643 else (
2644 if not allowdel
2645 then updownlevel 1
2648 | Glut.KEY_LEFT ->
2649 if Glut.getModifiers () land Glut.active_ctrl != 0
2650 then (
2651 state.mode <- Outline (
2652 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2654 Glut.postRedisplay ();
2656 else (
2657 if not allowdel
2658 then updownlevel ~-1
2661 | Glut.KEY_HOME ->
2662 state.mode <- Outline (
2663 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2665 Glut.postRedisplay ()
2667 | Glut.KEY_END ->
2668 let active = Array.length outlines - 1 in
2669 let first = max 0 (active - maxrows) in
2670 state.mode <- Outline (
2671 allowdel, active, first, outlines, qsearch, pan, oldmode
2673 Glut.postRedisplay ()
2675 | _ -> ()
2678 let drawplaceholder l =
2679 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2680 GlDraw.rect
2681 (float l.pagex, float l.pagedispy)
2682 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2684 let x = if margin < 0 then -margin else l.pagex
2685 and y = l.pagedispy + 13 in
2686 GlDraw.color (0.0, 0.0, 0.0);
2687 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2690 let now () = Unix.gettimeofday ();;
2692 let drawpage l =
2693 let color =
2694 match state.mode with
2695 | Textentry _ -> scalecolor 0.4
2696 | View | Outline _ | Items _ -> scalecolor 1.0
2697 | Birdseye (_, _, pageno, hooverpageno, _) ->
2698 if l.pageno = hooverpageno
2699 then scalecolor 0.9
2700 else (
2701 if l.pageno = pageno
2702 then scalecolor 1.0
2703 else scalecolor 0.8
2706 GlDraw.color color;
2707 begin match getopaque l.pageno with
2708 | Some (opaque, _) when validopaque opaque ->
2709 let a = now () in
2710 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2711 opaque;
2712 let b = now () in
2713 let d = b-.a in
2714 vlog "draw %d %f sec" l.pageno d;
2716 | _ ->
2717 drawplaceholder l;
2718 end;
2721 let scrollph y =
2722 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2723 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2724 let sh = float conf.winh /. sh in
2725 let sh = max sh (float conf.scrollh) in
2727 let percent =
2728 if state.y = state.maxy
2729 then 1.0
2730 else float y /. float maxy
2732 let position = (float conf.winh -. sh) *. percent in
2734 let position =
2735 if position +. sh > float conf.winh
2736 then float conf.winh -. sh
2737 else position
2739 position, sh;
2742 let scrollindicator () =
2743 GlDraw.color (0.64 , 0.64, 0.64);
2744 GlDraw.rect
2745 (float (conf.winw - state.scrollw), 0.)
2746 (float conf.winw, float conf.winh)
2748 GlDraw.color (0.0, 0.0, 0.0);
2750 let position, sh = scrollph state.y in
2751 GlDraw.rect
2752 (float (conf.winw - state.scrollw), position)
2753 (float conf.winw, position +. sh)
2757 let showsel margin =
2758 match state.mstate with
2759 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2762 | Msel ((x0, y0), (x1, y1)) ->
2763 let rec loop = function
2764 | l :: ls ->
2765 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2766 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2767 then
2768 match getopaque l.pageno with
2769 | Some (opaque, _) when validopaque opaque ->
2770 let oy = -l.pagey + l.pagedispy in
2771 seltext opaque
2772 (x0 - margin - state.x, y0,
2773 x1 - margin - state.x, y1) oy;
2775 | _ -> ()
2776 else loop ls
2777 | [] -> ()
2779 loop state.layout
2782 let showrects () =
2783 let panx = float state.x in
2784 Gl.enable `blend;
2785 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2786 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2787 List.iter
2788 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2789 List.iter (fun l ->
2790 if l.pageno = pageno
2791 then (
2792 let d = float (l.pagedispy - l.pagey) in
2793 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2794 GlDraw.begins `quads;
2796 GlDraw.vertex2 (x0+.panx, y0+.d);
2797 GlDraw.vertex2 (x1+.panx, y1+.d);
2798 GlDraw.vertex2 (x2+.panx, y2+.d);
2799 GlDraw.vertex2 (x3+.panx, y3+.d);
2801 GlDraw.ends ();
2803 ) state.layout
2804 ) state.rects
2806 Gl.disable `blend;
2809 let showstrings active first pan strings =
2810 Gl.enable `blend;
2811 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2812 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2813 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2814 Gl.disable `blend;
2816 GlDraw.color (1., 1., 1.);
2817 let rec loop row =
2818 if row = Array.length strings || (row - first) * 16 > conf.winh
2819 then ()
2820 else (
2821 let (s, level, _) = strings.(row) in
2822 let y = (row - first) * 16 in
2823 let x = 5 + 15*(max 0 (level+pan)) in
2824 if row = active
2825 then (
2826 Gl.enable `blend;
2827 GlDraw.polygon_mode `both `line;
2828 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2829 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2830 GlDraw.rect (0., float (y + 1))
2831 (float (conf.winw - 1), float (y + 18));
2832 GlDraw.polygon_mode `both `fill;
2833 Gl.disable `blend;
2834 GlDraw.color (1., 1., 1.);
2836 let draw_string s =
2837 let l = String.length s in
2838 if pan < 0
2839 then (
2840 let pan = pan * 2 in
2841 let pos = pan + level in
2842 let left = l + pos in
2843 if left > 0
2844 then
2845 let s =
2846 if left > l
2847 then s
2848 else String.sub s (-pos) left
2850 drawstring 14 x (y + 16) s
2852 else
2853 drawstring 14 (x + pan*15) (y + 16) s
2855 draw_string s;
2856 loop (row+1)
2859 loop first
2862 let showoutline (_, active, first, outlines, _, pan, _) =
2863 showstrings active first pan outlines;
2866 let showitems (active, first, items, _, pan, _) =
2867 showstrings active first pan items;
2870 let display () =
2871 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2872 GlDraw.viewport margin 0 state.w conf.winh;
2873 pagematrix ();
2874 GlClear.color (scalecolor2 conf.bgcolor);
2875 GlClear.clear [`color];
2876 if conf.zoom > 1.0
2877 then (
2878 Gl.enable `scissor_test;
2879 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2881 List.iter drawpage state.layout;
2882 if conf.zoom > 1.0
2883 then
2884 Gl.disable `scissor_test
2886 if state.x != 0
2887 then (
2888 let x = -.float state.x in
2889 GlMat.translate ~x ();
2891 showrects ();
2892 showsel margin;
2893 GlDraw.viewport 0 0 conf.winw conf.winh;
2894 winmatrix ();
2895 scrollindicator ();
2896 begin match state.mode with
2897 | Items items -> showitems items
2898 | Outline outline -> showoutline outline
2899 | _ -> ()
2900 end;
2901 enttext ();
2902 Glut.swapBuffers ();
2905 let getunder x y =
2906 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2907 let x = x - margin - state.x in
2908 let rec f = function
2909 | l :: rest ->
2910 begin match getopaque l.pageno with
2911 | Some (opaque, _) when validopaque opaque ->
2912 let y = y - l.pagedispy in
2913 if y > 0
2914 then
2915 let y = l.pagey + y in
2916 let x = x - l.pagex in
2917 match whatsunder opaque x y with
2918 | Unone -> f rest
2919 | under -> under
2920 else
2921 f rest
2922 | _ ->
2923 f rest
2925 | [] -> Unone
2927 f state.layout
2930 let viewmouse button bstate x y =
2931 match button with
2932 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2933 if Glut.getModifiers () land Glut.active_ctrl != 0
2934 then (
2935 match state.mstate with
2936 | Mzoom (oldn, i) ->
2937 if oldn = n
2938 then (
2939 if i = 2
2940 then
2941 let incr =
2942 match n with
2943 | 4 ->
2944 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2945 | _ ->
2946 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2948 let zoom = conf.zoom +. incr in
2949 setzoom zoom;
2950 state.mstate <- Mzoom (n, 0);
2951 else
2952 state.mstate <- Mzoom (n, i+1);
2954 else state.mstate <- Mzoom (n, 0)
2956 | _ -> state.mstate <- Mzoom (n, 0)
2958 else (
2959 if state.ascrollstep > 0
2960 then
2961 setautoscrollspeed (n=4)
2962 else
2963 let incr =
2964 if n = 3
2965 then -conf.scrollstep
2966 else conf.scrollstep
2968 let incr = incr * 2 in
2969 let y = clamp incr in
2970 gotoy_and_clear_text y
2973 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2974 if bstate = Glut.DOWN
2975 then (
2976 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2977 state.mstate <- Mpan (x, y)
2979 else
2980 state.mstate <- Mnone
2982 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
2983 if bstate = Glut.DOWN
2984 then
2985 let position, sh = scrollph state.y in
2986 if y > truncate position && y < truncate (position +. sh)
2987 then
2988 state.mstate <- Mscroll
2989 else
2990 let percent = float y /. float conf.winh in
2991 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2992 gotoy desty;
2993 state.mstate <- Mscroll
2994 else
2995 state.mstate <- Mnone
2997 | Glut.LEFT_BUTTON ->
2998 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2999 begin match dest with
3000 | Ulinkgoto (pageno, top) ->
3001 if pageno >= 0
3002 then (
3003 addnav ();
3004 gotopage1 pageno top;
3007 | Ulinkuri s ->
3008 print_endline s
3010 | Unone when bstate = Glut.DOWN ->
3011 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3012 state.mstate <- Mpan (x, y);
3014 | Unone | Utext _ ->
3015 if bstate = Glut.DOWN
3016 then (
3017 if conf.angle mod 360 = 0
3018 then (
3019 state.mstate <- Msel ((x, y), (x, y));
3020 Glut.postRedisplay ()
3023 else (
3024 match state.mstate with
3025 | Mnone -> ()
3027 | Mzoom _ | Mscroll ->
3028 state.mstate <- Mnone
3030 | Mpan _ ->
3031 Glut.setCursor Glut.CURSOR_INHERIT;
3032 state.mstate <- Mnone
3034 | Msel ((_, y0), (_, y1)) ->
3035 let f l =
3036 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3037 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3038 then
3039 match getopaque l.pageno with
3040 | Some (opaque, _) when validopaque opaque ->
3041 copysel opaque
3042 | _ -> ()
3044 List.iter f state.layout;
3045 copysel ""; (* ugly *)
3046 Glut.setCursor Glut.CURSOR_INHERIT;
3047 state.mstate <- Mnone;
3051 | _ -> ()
3054 let birdseyemouse button bstate x y
3055 (conf, leftx, _, hooverpageno, anchor) =
3056 match button with
3057 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3058 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3059 let rec loop = function
3060 | [] -> ()
3061 | l :: rest ->
3062 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3063 && x > margin && x < margin + l.pagew
3064 then (
3065 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3067 else loop rest
3069 loop state.layout
3070 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3071 | _ -> ()
3074 let mouse bstate button x y =
3075 match state.mode with
3076 | View -> viewmouse button bstate x y
3077 | Birdseye beye -> birdseyemouse button bstate x y beye
3078 | Textentry _ | Outline _ | Items _ -> ()
3081 let mouse ~button ~state ~x ~y = mouse state button x y;;
3083 let motion ~x ~y =
3084 match state.mode with
3085 | Outline _ -> ()
3086 | _ ->
3087 match state.mstate with
3088 | Mzoom _ | Mnone -> ()
3090 | Mpan (x0, y0) ->
3091 let dx = x - x0
3092 and dy = y0 - y in
3093 state.mstate <- Mpan (x, y);
3094 if conf.zoom > 1.0 then state.x <- state.x + dx;
3095 let y = clamp dy in
3096 gotoy_and_clear_text y
3098 | Msel (a, _) ->
3099 state.mstate <- Msel (a, (x, y));
3100 Glut.postRedisplay ()
3102 | Mscroll ->
3103 let y = min conf.winh (max 0 y) in
3104 let percent = float y /. float conf.winh in
3105 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3106 gotoy_and_clear_text y
3109 let pmotion ~x ~y =
3110 match state.mode with
3111 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3112 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3113 let rec loop = function
3114 | [] ->
3115 if hooverpageno != -1
3116 then (
3117 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3118 Glut.postRedisplay ();
3120 | l :: rest ->
3121 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3122 && x > margin && x < margin + l.pagew
3123 then (
3124 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3125 Glut.postRedisplay ();
3127 else loop rest
3129 loop state.layout
3131 | Outline _ | Items _ | Textentry _ -> ()
3132 | View ->
3133 match state.mstate with
3134 | Mnone ->
3135 begin match getunder x y with
3136 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3137 | Ulinkuri uri ->
3138 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3139 Glut.setCursor Glut.CURSOR_INFO
3140 | Ulinkgoto (page, _) ->
3141 if conf.underinfo
3142 then showtext 'p' ("age: " ^ string_of_int page);
3143 Glut.setCursor Glut.CURSOR_INFO
3144 | Utext s ->
3145 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3146 Glut.setCursor Glut.CURSOR_TEXT
3149 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3154 module State =
3155 struct
3156 open Parser
3158 let home =
3160 match Sys.os_type with
3161 | "Win32" -> Sys.getenv "HOMEPATH"
3162 | _ -> Sys.getenv "HOME"
3163 with exn ->
3164 prerr_endline
3165 ("Can not determine home directory location: " ^
3166 Printexc.to_string exn);
3170 let config_of c attrs =
3171 let apply c k v =
3173 match k with
3174 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3175 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3176 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3177 | "preload" -> { c with preload = bool_of_string v }
3178 | "page-bias" -> { c with pagebias = int_of_string v }
3179 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3180 | "auto-scroll-step" ->
3181 { c with autoscrollstep = max 0 (int_of_string v) }
3182 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3183 | "crop-hack" -> { c with crophack = bool_of_string v }
3184 | "throttle" -> { c with showall = bool_of_string v }
3185 | "highlight-links" -> { c with hlinks = bool_of_string v }
3186 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3187 | "vertical-margin" ->
3188 { c with interpagespace = max 0 (int_of_string v) }
3189 | "zoom" ->
3190 let zoom = float_of_string v /. 100. in
3191 let zoom = max 0.01 (min 2.2 zoom) in
3192 { c with zoom = zoom }
3193 | "presentation" -> { c with presentation = bool_of_string v }
3194 | "rotation-angle" -> { c with angle = int_of_string v }
3195 | "width" -> { c with winw = max 20 (int_of_string v) }
3196 | "height" -> { c with winh = max 20 (int_of_string v) }
3197 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3198 | "proportional-display" -> { c with proportional = bool_of_string v }
3199 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3200 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3201 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3202 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3203 | "persistent-location" -> { c with jumpback = bool_of_string v }
3204 | "background-color" -> { c with bgcolor = color_of_string v }
3205 | "scrollbar-in-presentation" ->
3206 { c with scrollbarinpm = bool_of_string v }
3207 | "ui-font" -> { c with uifont = v }
3208 | _ -> c
3209 with exn ->
3210 prerr_endline ("Error processing attribute (`" ^
3211 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3214 let rec fold c = function
3215 | [] -> c
3216 | (k, v) :: rest ->
3217 let c = apply c k v in
3218 fold c rest
3220 fold c attrs;
3223 let fromstring f pos n v d =
3224 try f v
3225 with exn ->
3226 dolog "Error processing attribute (%S=%S) at %d\n%s"
3227 n v pos (Printexc.to_string exn)
3232 let bookmark_of attrs =
3233 let rec fold title page rely = function
3234 | ("title", v) :: rest -> fold v page rely rest
3235 | ("page", v) :: rest -> fold title v rely rest
3236 | ("rely", v) :: rest -> fold title page v rest
3237 | _ :: rest -> fold title page rely rest
3238 | [] -> title, page, rely
3240 fold "invalid" "0" "0" attrs
3243 let doc_of attrs =
3244 let rec fold path page rely pan = function
3245 | ("path", v) :: rest -> fold v page rely pan rest
3246 | ("page", v) :: rest -> fold path v rely pan rest
3247 | ("rely", v) :: rest -> fold path page v pan rest
3248 | ("pan", v) :: rest -> fold path page rely v rest
3249 | _ :: rest -> fold path page rely pan rest
3250 | [] -> path, page, rely, pan
3252 fold "" "0" "0" "0" attrs
3255 let setconf dst src =
3256 dst.scrollbw <- src.scrollbw;
3257 dst.scrollh <- src.scrollh;
3258 dst.icase <- src.icase;
3259 dst.preload <- src.preload;
3260 dst.pagebias <- src.pagebias;
3261 dst.verbose <- src.verbose;
3262 dst.scrollstep <- src.scrollstep;
3263 dst.maxhfit <- src.maxhfit;
3264 dst.crophack <- src.crophack;
3265 dst.autoscrollstep <- src.autoscrollstep;
3266 dst.showall <- src.showall;
3267 dst.hlinks <- src.hlinks;
3268 dst.underinfo <- src.underinfo;
3269 dst.interpagespace <- src.interpagespace;
3270 dst.zoom <- src.zoom;
3271 dst.presentation <- src.presentation;
3272 dst.angle <- src.angle;
3273 dst.winw <- src.winw;
3274 dst.winh <- src.winh;
3275 dst.savebmarks <- src.savebmarks;
3276 dst.memlimit <- src.memlimit;
3277 dst.proportional <- src.proportional;
3278 dst.texcount <- src.texcount;
3279 dst.sliceheight <- src.sliceheight;
3280 dst.thumbw <- src.thumbw;
3281 dst.jumpback <- src.jumpback;
3282 dst.bgcolor <- src.bgcolor;
3283 dst.scrollbarinpm <- src.scrollbarinpm;
3284 dst.uifont <- src.uifont;
3287 let unent s =
3288 let l = String.length s in
3289 let b = Buffer.create l in
3290 unent b s 0 l;
3291 Buffer.contents b;
3294 let get s =
3295 let h = Hashtbl.create 10 in
3296 let dc = { defconf with angle = defconf.angle } in
3297 let rec toplevel v t spos _ =
3298 match t with
3299 | Vdata | Vcdata | Vend -> v
3300 | Vopen ("llppconfig", _, closed) ->
3301 if closed
3302 then v
3303 else { v with f = llppconfig }
3304 | Vopen _ ->
3305 error "unexpected subelement at top level" s spos
3306 | Vclose _ -> error "unexpected close at top level" s spos
3308 and llppconfig v t spos _ =
3309 match t with
3310 | Vdata | Vcdata | Vend -> v
3311 | Vopen ("defaults", attrs, closed) ->
3312 let c = config_of dc attrs in
3313 setconf dc c;
3314 if closed
3315 then v
3316 else { v with f = skip "defaults" (fun () -> v) }
3318 | Vopen ("doc", attrs, closed) ->
3319 let pathent, spage, srely, span = doc_of attrs in
3320 let path = unent pathent
3321 and pageno = fromstring int_of_string spos "page" spage 0
3322 and rely = fromstring float_of_string spos "rely" srely 0.0
3323 and pan = fromstring int_of_string spos "pan" span 0 in
3324 let c = config_of dc attrs in
3325 let anchor = (pageno, rely) in
3326 if closed
3327 then (Hashtbl.add h path (c, [], pan, anchor); v)
3328 else { v with f = doc path pan anchor c [] }
3330 | Vopen _ ->
3331 error "unexpected subelement in llppconfig" s spos
3333 | Vclose "llppconfig" -> { v with f = toplevel }
3334 | Vclose _ -> error "unexpected close in llppconfig" s spos
3336 and doc path pan anchor c bookmarks v t spos _ =
3337 match t with
3338 | Vdata | Vcdata -> v
3339 | Vend -> error "unexpected end of input in doc" s spos
3340 | Vopen ("bookmarks", _, closed) ->
3341 if closed
3342 then v
3343 else { v with f = pbookmarks path pan anchor c bookmarks }
3345 | Vopen (_, _, _) ->
3346 error "unexpected subelement in doc" s spos
3348 | Vclose "doc" ->
3349 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3350 { v with f = llppconfig }
3352 | Vclose _ -> error "unexpected close in doc" s spos
3354 and pbookmarks path pan anchor c bookmarks v t spos _ =
3355 match t with
3356 | Vdata | Vcdata -> v
3357 | Vend -> error "unexpected end of input in bookmarks" s spos
3358 | Vopen ("item", attrs, closed) ->
3359 let titleent, spage, srely = bookmark_of attrs in
3360 let page = fromstring int_of_string spos "page" spage 0
3361 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3362 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3363 if closed
3364 then { v with f = pbookmarks path pan anchor c bookmarks }
3365 else
3366 let f () = v in
3367 { v with f = skip "item" f }
3369 | Vopen _ ->
3370 error "unexpected subelement in bookmarks" s spos
3372 | Vclose "bookmarks" ->
3373 { v with f = doc path pan anchor c bookmarks }
3375 | Vclose _ -> error "unexpected close in bookmarks" s spos
3377 and skip tag f v t spos _ =
3378 match t with
3379 | Vdata | Vcdata -> v
3380 | Vend ->
3381 error ("unexpected end of input in skipped " ^ tag) s spos
3382 | Vopen (tag', _, closed) ->
3383 if closed
3384 then v
3385 else
3386 let f' () = { v with f = skip tag f } in
3387 { v with f = skip tag' f' }
3388 | Vclose ctag ->
3389 if tag = ctag
3390 then f ()
3391 else error ("unexpected close in skipped " ^ tag) s spos
3394 parse { f = toplevel; accu = () } s;
3395 h, dc;
3398 let do_load f ic =
3400 let len = in_channel_length ic in
3401 let s = String.create len in
3402 really_input ic s 0 len;
3403 f s;
3404 with
3405 | Parse_error (msg, s, pos) ->
3406 let subs = subs s pos in
3407 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3408 failwith ("parse error: " ^ s)
3410 | exn ->
3411 failwith ("config load error: " ^ Printexc.to_string exn)
3414 let path =
3415 let dir =
3417 let dir = Filename.concat home ".config" in
3418 if Sys.is_directory dir then dir else home
3419 with _ -> home
3421 Filename.concat dir "llpp.conf"
3424 let load1 f =
3425 if Sys.file_exists path
3426 then
3427 match
3428 (try Some (open_in_bin path)
3429 with exn ->
3430 prerr_endline
3431 ("Error opening configuation file `" ^ path ^ "': " ^
3432 Printexc.to_string exn);
3433 None
3435 with
3436 | Some ic ->
3437 begin try
3438 f (do_load get ic)
3439 with exn ->
3440 prerr_endline
3441 ("Error loading configuation from `" ^ path ^ "': " ^
3442 Printexc.to_string exn);
3443 end;
3444 close_in ic;
3446 | None -> ()
3447 else
3448 f (Hashtbl.create 0, defconf)
3451 let load () =
3452 let f (h, dc) =
3453 let pc, pb, px, pa =
3455 Hashtbl.find h (Filename.basename state.path)
3456 with Not_found -> dc, [], 0, (0, 0.0)
3458 setconf defconf dc;
3459 setconf conf pc;
3460 state.bookmarks <- pb;
3461 state.x <- px;
3462 state.scrollw <- conf.scrollbw;
3463 if conf.jumpback
3464 then state.anchor <- pa;
3465 cbput state.hists.nav pa;
3467 load1 f
3470 let add_attrs bb always dc c =
3471 let ob s a b =
3472 if always || a != b
3473 then Printf.bprintf bb "\n %s='%b'" s a
3474 and oi s a b =
3475 if always || a != b
3476 then Printf.bprintf bb "\n %s='%d'" s a
3477 and oz s a b =
3478 if always || a <> b
3479 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3480 and oc s a b =
3481 if always || a <> b
3482 then
3483 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3484 and os s a b =
3485 if always || a <> b
3486 then
3487 Printf.bprintf bb "\n %s='%s'" s a
3489 let w, h =
3490 if always
3491 then dc.winw, dc.winh
3492 else
3493 match state.fullscreen with
3494 | Some wh -> wh
3495 | None -> c.winw, c.winh
3497 let zoom, presentation, interpagespace, showall=
3498 if always
3499 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3500 else
3501 match state.mode with
3502 | Birdseye (bc, _, _, _, _) ->
3503 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3504 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3506 oi "width" w dc.winw;
3507 oi "height" h dc.winh;
3508 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3509 oi "scroll-handle-height" c.scrollh dc.scrollh;
3510 ob "case-insensitive-search" c.icase dc.icase;
3511 ob "preload" c.preload dc.preload;
3512 oi "page-bias" c.pagebias dc.pagebias;
3513 oi "scroll-step" c.scrollstep dc.scrollstep;
3514 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3515 ob "max-height-fit" c.maxhfit dc.maxhfit;
3516 ob "crop-hack" c.crophack dc.crophack;
3517 ob "throttle" showall dc.showall;
3518 ob "highlight-links" c.hlinks dc.hlinks;
3519 ob "under-cursor-info" c.underinfo dc.underinfo;
3520 oi "vertical-margin" interpagespace dc.interpagespace;
3521 oz "zoom" zoom dc.zoom;
3522 ob "presentation" presentation dc.presentation;
3523 oi "rotation-angle" c.angle dc.angle;
3524 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3525 ob "proportional-display" c.proportional dc.proportional;
3526 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3527 oi "texcount" c.texcount dc.texcount;
3528 oi "slice-height" c.sliceheight dc.sliceheight;
3529 oi "thumbnail-width" c.thumbw dc.thumbw;
3530 ob "persistent-location" c.jumpback dc.jumpback;
3531 oc "background-color" c.bgcolor dc.bgcolor;
3532 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3533 os "ui-font" c.uifont dc.uifont;
3536 let save () =
3537 let bb = Buffer.create 32768 in
3538 let f (h, dc) =
3539 let dc = if conf.bedefault then conf else dc in
3540 Buffer.add_string bb "<llppconfig>\n<defaults ";
3541 add_attrs bb true dc dc;
3542 Buffer.add_string bb "/>\n";
3544 let adddoc path pan anchor c bookmarks =
3545 if bookmarks == [] && c = dc && anchor = emptyanchor
3546 then ()
3547 else (
3548 Printf.bprintf bb "<doc path='%s'"
3549 (enent path 0 (String.length path));
3551 if anchor <> emptyanchor
3552 then (
3553 let n, y = anchor in
3554 Printf.bprintf bb " page='%d'" n;
3555 if y > 1e-6
3556 then
3557 Printf.bprintf bb " rely='%f'" y
3561 if pan != 0
3562 then Printf.bprintf bb " pan='%d'" pan;
3564 add_attrs bb false dc c;
3566 begin match bookmarks with
3567 | [] -> Buffer.add_string bb "/>\n"
3568 | _ ->
3569 Buffer.add_string bb ">\n<bookmarks>\n";
3570 List.iter (fun (title, _level, (page, rely)) ->
3571 Printf.bprintf bb
3572 "<item title='%s' page='%d'"
3573 (enent title 0 (String.length title))
3574 page
3576 if rely > 1e-6
3577 then
3578 Printf.bprintf bb " rely='%f'" rely
3580 Buffer.add_string bb "/>\n";
3581 ) bookmarks;
3582 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3583 end;
3587 let pan =
3588 match state.mode with
3589 | Birdseye (_, pan, _, _, _) -> pan
3590 | _ -> state.x
3592 let basename = Filename.basename state.path in
3593 adddoc basename pan (getanchor ())
3594 { conf with
3595 autoscrollstep =
3596 if state.ascrollstep > 0
3597 then state.ascrollstep
3598 else conf.autoscrollstep }
3599 (if conf.savebmarks then state.bookmarks else []);
3601 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3602 if basename <> path
3603 then adddoc path x y c bookmarks
3604 ) h;
3605 Buffer.add_string bb "</llppconfig>";
3607 load1 f;
3608 if Buffer.length bb > 0
3609 then
3611 let tmp = path ^ ".tmp" in
3612 let oc = open_out_bin tmp in
3613 Buffer.output_buffer oc bb;
3614 close_out oc;
3615 Sys.rename tmp path;
3616 with exn ->
3617 prerr_endline
3618 ("error while saving configuration: " ^ Printexc.to_string exn)
3620 end;;
3622 let () =
3623 Arg.parse
3624 (Arg.align
3625 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3626 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3627 " Print version and exit")]
3629 (fun s -> state.path <- s)
3630 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3632 if String.length state.path = 0
3633 then (prerr_endline "file name missing"; exit 1);
3635 State.load ();
3637 let _ = Glut.init Sys.argv in
3638 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3639 let () = Glut.initWindowSize conf.winw conf.winh in
3640 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3642 let csock, ssock =
3643 if Sys.os_type = "Unix"
3644 then
3645 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3646 else
3647 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3648 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3649 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3650 Unix.bind sock addr;
3651 Unix.listen sock 1;
3652 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3653 Unix.connect csock addr;
3654 let ssock, _ = Unix.accept sock in
3655 Unix.close sock;
3656 let opts sock =
3657 Unix.setsockopt sock Unix.TCP_NODELAY true;
3658 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3660 opts ssock;
3661 opts csock;
3662 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3663 ssock, csock
3666 let () = Glut.displayFunc display in
3667 let () = Glut.reshapeFunc reshape in
3668 let () = Glut.keyboardFunc keyboard in
3669 let () = Glut.specialFunc special in
3670 let () = Glut.idleFunc (Some idle) in
3671 let () = Glut.mouseFunc mouse in
3672 let () = Glut.motionFunc motion in
3673 let () = Glut.passiveMotionFunc pmotion in
3675 init ssock (
3676 conf.angle, conf.proportional, conf.texcount,
3677 conf.sliceheight, conf.uifont
3679 state.csock <- csock;
3680 state.ssock <- ssock;
3681 state.text <- "Opening " ^ state.path;
3682 writeopen state.path state.password;
3684 at_exit State.save;
3686 let rec handlelablglutbug () =
3688 Glut.mainLoop ();
3689 with Glut.BadEnum "key in special_of_int" ->
3690 showtext '!' " LablGlut bug: special key not recognized";
3691 handlelablglutbug ()
3693 handlelablglutbug ();