Refactor
[llpp.git] / main.ml
blobd418ca7ea1a215d758c35cbea194677136ba4ae7
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 drawstr : 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 drawstring size x y s =
766 Gl.enable `blend;
767 Gl.enable `texture_2d;
768 drawstr size x y s;
769 Gl.disable `blend;
770 Gl.disable `texture_2d;
773 let drawstring1 size x y s =
774 drawstr size x y s;
777 let enttext () =
778 let drawstring s =
779 GlDraw.color (0.0, 0.0, 0.0);
780 GlDraw.rect
781 (0.0, float (conf.winh - 18))
782 (float (conf.winw - state.scrollw - 1), float conf.winh)
784 GlDraw.color (1.0, 1.0, 1.0);
785 drawstring 14 8 (conf.winh - 5) s;
787 let len = String.length state.text in
788 match state.mode with
789 | Textentry ((prefix, text, _, _, _), _) ->
790 let s =
791 match String.length prefix with
792 | 0 | 1 ->
793 if len > 0
794 then
795 Printf.sprintf "%s%s\226–� [%s]" prefix text state.text
796 else
797 Printf.sprintf "%s%s\226–�" prefix text
799 | _ ->
800 if len > 0
801 then
802 Printf.sprintf "%s: %s\226–� [%s]" prefix text state.text
803 else
804 Printf.sprintf "%s: %s\226–�" prefix text
806 drawstring s
808 | _ ->
809 if len > 0 then drawstring state.text
812 let showtext c s =
813 state.text <- Printf.sprintf "%c%s" c s;
814 Glut.postRedisplay ();
817 let act cmd =
818 match cmd.[0] with
819 | 'c' ->
820 state.pdims <- [];
822 | 'D' ->
823 state.rects <- state.rects1;
824 Glut.postRedisplay ()
826 | 'C' ->
827 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
828 state.pagecount <- n;
829 state.invalidated <- state.invalidated - 1;
830 if state.invalidated = 0
831 then represent ()
833 | 't' ->
834 let s = Scanf.sscanf cmd "t %n"
835 (fun n -> String.sub cmd n (String.length cmd - n))
837 Glut.setWindowTitle s
839 | 'T' ->
840 let s = Scanf.sscanf cmd "T %n"
841 (fun n -> String.sub cmd n (String.length cmd - n))
843 if istextentry state.mode
844 then (
845 state.text <- s;
846 showtext ' ' s;
848 else (
849 state.text <- s;
850 Glut.postRedisplay ();
853 | 'V' ->
854 if conf.verbose
855 then
856 let s = Scanf.sscanf cmd "V %n"
857 (fun n -> String.sub cmd n (String.length cmd - n))
859 state.text <- s;
860 showtext ' ' s;
862 | 'F' ->
863 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
864 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
865 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
866 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
868 let y = (getpagey pageno) + truncate y0 in
869 addnav ();
870 gotoy y;
871 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
873 | 'R' ->
874 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
875 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
876 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
877 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
879 state.rects1 <-
880 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
882 | 'r' ->
883 let n, w, _h, r, l, s, p =
884 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
885 (fun n w h r l s p ->
886 (n-1, w, h, r, l != 0, s, p))
889 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
890 state.memused <- state.memused + s;
892 let layout =
893 match state.throttle with
894 | None -> state.layout
895 | Some layout -> layout
898 let rec gc () =
899 if (state.memused <= conf.memlimit) || cbempty state.pagecache
900 then ()
901 else (
902 let evictedopaque = cbpeek state.pagecache in
903 match findpageforopaque evictedopaque with
904 | None -> failwith "bug in gc"
905 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
906 if state.gen != gen || not (pagevisible layout evictedn)
907 then (
908 wcmd "free" [`s evictedopaque];
909 state.memused <- state.memused - evictedsize;
910 Hashtbl.remove state.pagemap k;
911 cbdecr state.pagecache;
912 gc ();
916 gc ();
918 cbput state.pagecache p;
919 state.rendering <- false;
921 begin match state.throttle with
922 | None ->
923 if pagevisible state.layout n
924 then gotoy state.y
925 else (
926 let allvisible = loadlayout state.layout in
927 if allvisible then preload ();
930 | Some layout ->
931 match layout with
932 | [] -> ()
933 | l :: _ ->
934 let y = getpagey l.pageno + l.pagey in
935 gotoy y
938 | 'l' ->
939 let pdim =
940 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
942 state.pdims <- pdim :: state.pdims
944 | 'o' ->
945 let (l, n, t, h, pos) =
946 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
948 let s = String.sub cmd pos (String.length cmd - pos) in
949 let outline = (s, l, (n, float t /. float h)) in
950 let outlines =
951 match state.outlines with
952 | Olist outlines -> Olist (outline :: outlines)
953 | Oarray _ -> Olist [outline]
954 | Onarrow _ -> Olist [outline]
956 state.outlines <- outlines
959 | 'i' ->
960 let s = Scanf.sscanf cmd "i %n"
961 (fun n -> String.sub cmd n (String.length cmd - n))
963 let len = String.length s in
964 let rec fold accu pos =
965 let eolpos =
966 try String.index_from s pos '\n' with Not_found -> len
968 if eolpos = len
969 then List.rev accu
970 else
971 let line = String.sub s pos (eolpos - pos) in
972 fold ((1, line)::accu) (eolpos+1)
974 state.docinfo <- fold state.docinfo 0
976 | _ ->
977 dolog "unknown cmd `%S'" cmd
980 let now = Unix.gettimeofday;;
982 let idle () =
983 let rec loop delay =
984 let r, _, _ = Unix.select [state.csock] [] [] delay in
985 begin match r with
986 | [] ->
987 if state.ascrollstep > 0
988 then begin
989 let y = state.y + state.ascrollstep in
990 let y = if y >= state.maxy then 0 else y in
991 gotoy y;
992 state.text <- "";
993 end;
995 | _ ->
996 let cmd = readcmd state.csock in
997 act cmd;
998 loop 0.0
999 end;
1000 in loop 0.001
1003 let onhist cb =
1004 let rc = cb.rc in
1005 let action = function
1006 | HCprev -> cbget cb ~-1
1007 | HCnext -> cbget cb 1
1008 | HCfirst -> cbget cb ~-(cb.rc)
1009 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1010 and cancel () = cb.rc <- rc
1011 in (action, cancel)
1014 let search pattern forward =
1015 if String.length pattern > 0
1016 then
1017 let pn, py =
1018 match state.layout with
1019 | [] -> 0, 0
1020 | l :: _ ->
1021 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1023 let cmd =
1024 let b = makecmd "search"
1025 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1027 Buffer.add_char b ',';
1028 Buffer.add_string b pattern;
1029 Buffer.add_char b '\000';
1030 Buffer.contents b;
1032 writecmd state.csock cmd;
1035 let intentry text key =
1036 let c = Char.unsafe_chr key in
1037 match c with
1038 | '0' .. '9' ->
1039 let s = "x" in s.[0] <- c;
1040 let text = text ^ s in
1041 TEcont text
1043 | _ ->
1044 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1045 TEcont text
1048 let addchar s c =
1049 let b = Buffer.create (String.length s + 1) in
1050 Buffer.add_string b s;
1051 Buffer.add_char b c;
1052 Buffer.contents b;
1055 let textentry text key =
1056 let c = Char.unsafe_chr key in
1057 match c with
1058 | _ when key >= 32 && key < 127 ->
1059 let text = addchar text c in
1060 TEcont text
1062 | _ ->
1063 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1064 TEcont text
1067 let reinit angle proportional =
1068 conf.angle <- angle;
1069 conf.proportional <- proportional;
1070 invalidate ();
1071 wcmd "reinit" [`i angle; `b proportional];
1074 let setzoom zoom =
1075 let zoom = max 0.01 (min 2.2 zoom) in
1076 if zoom <> conf.zoom
1077 then (
1078 if zoom <= 1.0
1079 then state.x <- 0;
1080 conf.zoom <- zoom;
1081 if state.invalidated = 0
1082 then
1083 state.anchor <- getanchor ()
1085 reshape conf.winw conf.winh;
1086 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1090 let enterbirdseye () =
1091 let zoom = float conf.thumbw /. float conf.winw in
1092 let birdseyepageno =
1093 let cy = conf.winh / 2 in
1094 let fold = function
1095 | [] -> 0
1096 | l :: rest ->
1097 let rec fold best = function
1098 | [] -> best.pageno
1099 | l :: rest ->
1100 let d = cy - (l.pagedispy + l.pagevh/2)
1101 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1102 if abs d < abs dbest
1103 then fold l rest
1104 else best.pageno
1105 in fold l rest
1107 fold state.layout
1109 state.mode <- Birdseye (
1110 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1112 conf.zoom <- zoom;
1113 conf.presentation <- false;
1114 conf.interpagespace <- 10;
1115 conf.hlinks <- false;
1116 state.x <- 0;
1117 state.mstate <- Mnone;
1118 conf.showall <- false;
1119 Glut.setCursor Glut.CURSOR_INHERIT;
1120 if conf.verbose
1121 then
1122 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1123 (100.0*.zoom)
1124 else
1125 state.text <- ""
1127 reshape conf.winw conf.winh;
1130 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1131 state.mode <- View;
1132 conf.zoom <- c.zoom;
1133 conf.presentation <- c.presentation;
1134 conf.interpagespace <- c.interpagespace;
1135 conf.showall <- c.showall;
1136 conf.hlinks <- c.hlinks;
1137 state.x <- leftx;
1138 if conf.verbose
1139 then
1140 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1141 (100.0*.conf.zoom)
1143 reshape conf.winw conf.winh;
1144 state.anchor <- if goback then anchor else (pageno, 0.0);
1147 let togglebirdseye () =
1148 match state.mode with
1149 | Birdseye vals -> leavebirdseye vals true
1150 | View | Outline _ -> enterbirdseye ()
1151 | _ -> ()
1154 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1155 let pageno = max 0 (pageno - 1) in
1156 let rec loop = function
1157 | [] -> gotopage1 pageno 0
1158 | l :: _ when l.pageno = pageno ->
1159 if l.pagedispy >= 0 && l.pagey = 0
1160 then Glut.postRedisplay ()
1161 else gotopage1 pageno 0
1162 | _ :: rest -> loop rest
1164 loop state.layout;
1165 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1168 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1169 let pageno = min (state.pagecount - 1) (pageno + 1) in
1170 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1171 let rec loop = function
1172 | [] ->
1173 let y, h = getpageyh pageno in
1174 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1175 gotoy (clamp dy)
1176 | l :: _ when l.pageno = pageno ->
1177 if l.pagevh != l.pageh
1178 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1179 else Glut.postRedisplay ()
1180 | _ :: rest -> loop rest
1182 loop state.layout
1185 let optentry mode _ key =
1186 let btos b = if b then "on" else "off" in
1187 let c = Char.unsafe_chr key in
1188 match c with
1189 | 's' ->
1190 let ondone s =
1191 try conf.scrollstep <- int_of_string s with exc ->
1192 state.text <- Printf.sprintf "bad integer `%s': %s"
1193 s (Printexc.to_string exc)
1195 TEswitch ("scroll step", "", None, intentry, ondone)
1197 | 'A' ->
1198 let ondone s =
1200 conf.autoscrollstep <- int_of_string s;
1201 if state.ascrollstep > 0
1202 then state.ascrollstep <- conf.autoscrollstep;
1203 with exc ->
1204 state.text <- Printf.sprintf "bad integer `%s': %s"
1205 s (Printexc.to_string exc)
1207 TEswitch ("auto scroll step", "", None, intentry, ondone)
1209 | 'Z' ->
1210 let ondone s =
1212 let zoom = float (int_of_string s) /. 100.0 in
1213 setzoom zoom
1214 with exc ->
1215 state.text <- Printf.sprintf "bad integer `%s': %s"
1216 s (Printexc.to_string exc)
1218 TEswitch ("zoom", "", None, intentry, ondone)
1220 | 't' ->
1221 let ondone s =
1223 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1224 state.text <-
1225 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1226 begin match mode with
1227 | Birdseye beye ->
1228 leavebirdseye beye false;
1229 enterbirdseye ();
1230 | _ -> ();
1232 with exc ->
1233 state.text <- Printf.sprintf "bad integer `%s': %s"
1234 s (Printexc.to_string exc)
1236 TEswitch ("thumbnail width", "", None, intentry, ondone)
1238 | 'R' ->
1239 let ondone s =
1240 match try
1241 Some (int_of_string s)
1242 with exc ->
1243 state.text <- Printf.sprintf "bad integer `%s': %s"
1244 s (Printexc.to_string exc);
1245 None
1246 with
1247 | Some angle -> reinit angle conf.proportional
1248 | None -> ()
1250 TEswitch ("rotation", "", None, intentry, ondone)
1252 | 'i' ->
1253 conf.icase <- not conf.icase;
1254 TEdone ("case insensitive search " ^ (btos conf.icase))
1256 | 'p' ->
1257 conf.preload <- not conf.preload;
1258 gotoy state.y;
1259 TEdone ("preload " ^ (btos conf.preload))
1261 | 'v' ->
1262 conf.verbose <- not conf.verbose;
1263 TEdone ("verbose " ^ (btos conf.verbose))
1265 | 'h' ->
1266 conf.maxhfit <- not conf.maxhfit;
1267 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1268 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1270 | 'c' ->
1271 conf.crophack <- not conf.crophack;
1272 TEdone ("crophack " ^ btos conf.crophack)
1274 | 'a' ->
1275 conf.showall <- not conf.showall;
1276 TEdone ("throttle " ^ btos conf.showall)
1278 | 'f' ->
1279 conf.underinfo <- not conf.underinfo;
1280 TEdone ("underinfo " ^ btos conf.underinfo)
1282 | 'P' ->
1283 conf.savebmarks <- not conf.savebmarks;
1284 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1286 | 'S' ->
1287 let ondone s =
1289 let pageno, py =
1290 match state.layout with
1291 | [] -> 0, 0
1292 | l :: _ ->
1293 l.pageno, l.pagey
1295 conf.interpagespace <- int_of_string s;
1296 state.maxy <- calcheight ();
1297 let y = getpagey pageno in
1298 gotoy (y + py)
1299 with exc ->
1300 state.text <- Printf.sprintf "bad integer `%s': %s"
1301 s (Printexc.to_string exc)
1303 TEswitch ("vertical margin", "", None, intentry, ondone)
1305 | 'l' ->
1306 reinit conf.angle (not conf.proportional);
1307 TEdone ("proprortional display " ^ btos conf.proportional)
1309 | _ ->
1310 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1311 TEstop
1314 let maxoutlinerows () = (conf.winh - 31) / 16;;
1316 let enterselector allowdel outlines errmsg msg =
1317 if Array.length outlines = 0
1318 then (
1319 showtext ' ' errmsg;
1321 else (
1322 state.text <- msg;
1323 Glut.setCursor Glut.CURSOR_INHERIT;
1324 let pageno =
1325 match state.layout with
1326 | [] -> -1
1327 | {pageno=pageno} :: _ -> pageno
1329 let active =
1330 let rec loop n =
1331 if n = Array.length outlines
1332 then 0
1333 else
1334 let (_, _, (outlinepageno, _)) = outlines.(n) in
1335 if outlinepageno >= pageno then n else loop (n+1)
1337 loop 0
1339 state.mode <- Outline
1340 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1341 state.mode);
1342 Glut.postRedisplay ();
1346 let enteroutlinemode () =
1347 let outlines, msg =
1348 match state.outlines with
1349 | Oarray a -> a, ""
1350 | Olist l ->
1351 let a = Array.of_list (List.rev l) in
1352 state.outlines <- Oarray a;
1353 a, ""
1354 | Onarrow (pat, a, _) ->
1355 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1357 enterselector false outlines "Document has no outline" msg;
1360 let enterbookmarkmode () =
1361 let bookmarks = Array.of_list state.bookmarks in
1362 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1365 let mode_to_string mode =
1366 let b = Buffer.create 10 in
1367 let rec f = function
1368 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1369 | View -> Buffer.add_string b "View"
1370 | Birdseye _ -> Buffer.add_string b "Birdseye"
1371 | Items _ -> Buffer.add_string b "Items"
1372 | Outline _ -> Buffer.add_string b "Outline"
1374 f mode;
1375 Buffer.contents b;
1378 let color_of_string s =
1379 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1380 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1384 let color_to_string (r, g, b) =
1385 let r = truncate (r *. 256.0)
1386 and g = truncate (g *. 256.0)
1387 and b = truncate (b *. 256.0) in
1388 Printf.sprintf "%d/%d/%d" r g b
1391 let enterinfomode () =
1392 let btos = function true -> "on" | _ -> "off" in
1393 let mode = state.mode in
1394 let rec makeitems () =
1395 let intp name get set =
1396 Printf.sprintf "%-24s %d" name (get ()), 1, Action (
1397 fun active first _ pan ->
1398 let ondone s =
1399 let n =
1400 try int_of_string s
1401 with exn ->
1402 state.text <- Printf.sprintf "bad integer `%s': %s"
1403 s (Printexc.to_string exn);
1404 max_int;
1406 if n != max_int then set n;
1408 let te = name, "", None, intentry, ondone in
1409 state.text <- "";
1410 Textentry (
1412 fun _ ->
1413 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1416 and boolp name get set =
1417 Printf.sprintf "%-24s %s" name (btos (get ())), 1, Action (
1418 fun active first qsearch pan ->
1419 let v = get () in
1420 set (not v);
1421 Items (active, first, makeitems (), qsearch, pan, mode);
1423 and colorp name get set =
1424 Printf.sprintf "%-24s %s" name (color_to_string (get ())), 1, Action (
1425 fun active first _ pan ->
1426 let invalid = (nan, nan, nan) in
1427 let ondone s =
1428 let c =
1429 try color_of_string s
1430 with exn ->
1431 state.text <- Printf.sprintf "bad color `%s': %s"
1432 s (Printexc.to_string exn);
1433 invalid
1435 if c <> invalid
1436 then set c;
1438 let te = name, "", None, textentry, ondone in
1439 state.text <- "";
1440 Textentry (
1442 fun _ ->
1443 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1448 let birdseye = isbirdseye mode in
1449 let items = [
1450 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1452 boolp "presentation"
1453 (fun () -> conf.presentation)
1454 (fun v ->
1455 conf.presentation <- v;
1456 state.anchor <- getanchor ();
1457 represent ());
1459 boolp "ignore case in searches"
1460 (fun () -> conf.icase)
1461 (fun v -> conf.icase <- v);
1463 boolp "preload"
1464 (fun () -> conf.preload)
1465 (fun v -> conf.preload <- v);
1467 boolp "verbose"
1468 (fun () -> conf.verbose)
1469 (fun v -> conf.verbose <- v);
1471 boolp "max fit"
1472 (fun () -> conf.maxhfit)
1473 (fun v -> conf.maxhfit <- v);
1475 boolp "crop hack"
1476 (fun () -> conf.crophack)
1477 (fun v -> conf.crophack <- v);
1479 boolp "throttle"
1480 (fun () -> conf.showall)
1481 (fun v -> conf.showall <- v);
1483 boolp "highlight links"
1484 (fun () -> conf.hlinks)
1485 (fun v -> conf.hlinks <- v);
1487 boolp "under info"
1488 (fun () -> conf.underinfo)
1489 (fun v -> conf.underinfo <- v);
1490 boolp "persistent bookmarks"
1491 (fun () -> conf.savebmarks)
1492 (fun v -> conf.savebmarks <- v);
1494 boolp "proportional display"
1495 (fun () -> conf.proportional)
1496 (fun v -> reinit conf.angle v);
1498 boolp "persistent location"
1499 (fun () -> conf.jumpback)
1500 (fun v -> conf.jumpback <- v);
1502 "", 0, Noaction;
1504 intp "vertical margin"
1505 (fun () -> conf.interpagespace)
1506 (fun n ->
1507 conf.interpagespace <- n;
1508 let pageno, py =
1509 match state.layout with
1510 | [] -> 0, 0
1511 | l :: _ ->
1512 l.pageno, l.pagey
1514 state.maxy <- calcheight ();
1515 let y = getpagey pageno in
1516 gotoy (y + py)
1519 intp "page bias"
1520 (fun () -> conf.pagebias)
1521 (fun v -> conf.pagebias <- v);
1523 intp "scroll step"
1524 (fun () -> conf.scrollstep)
1525 (fun n -> conf.scrollstep <- n);
1527 intp "auto scroll step"
1528 (fun () ->
1529 if state.ascrollstep > 0
1530 then state.ascrollstep
1531 else conf.autoscrollstep)
1532 (fun n ->
1533 if state.ascrollstep > 0
1534 then state.ascrollstep <- n
1535 else conf.autoscrollstep <- n);
1537 intp "zoom"
1538 (fun () -> truncate (conf.zoom *. 100.))
1539 (fun v -> setzoom ((float v) /. 100.));
1541 intp "rotation"
1542 (fun () -> conf.angle)
1543 (fun v -> reinit v conf.proportional);
1545 intp "scroll bar width"
1546 (fun () -> state.scrollw)
1547 (fun v ->
1548 state.scrollw <- v;
1549 conf.scrollbw <- v;
1550 reshape conf.winw conf.winh;
1553 intp "scroll handle height"
1554 (fun () -> conf.scrollh)
1555 (fun v -> conf.scrollh <- v;);
1557 intp "thumbnail width"
1558 (fun () -> conf.thumbw)
1559 (fun v ->
1560 conf.thumbw <- min 1920 v;
1561 match mode with
1562 | Birdseye beye ->
1563 leavebirdseye beye false;
1564 enterbirdseye ()
1565 | _ -> ()
1568 colorp "background color"
1569 (fun () -> conf.bgcolor)
1570 (fun v -> conf.bgcolor <- v);
1572 "", 0, Noaction;
1573 "Presentation mode", 0, Noaction;
1575 boolp "scrollbar visible"
1576 (fun () -> conf.scrollbarinpm)
1577 (fun v ->
1578 if v != conf.scrollbarinpm
1579 then (
1580 conf.scrollbarinpm <- v;
1581 if conf.presentation
1582 then (
1583 state.scrollw <- if v then conf.scrollbw else 0;
1584 reshape conf.winw conf.winh;
1589 "", 0, Noaction;
1590 "Pixmap Cache", 0, Noaction;
1592 intp "size (advisory)"
1593 (fun () -> conf.memlimit)
1594 (fun v -> conf.memlimit <- v);
1595 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1599 let tailer =
1600 let trailer =
1601 ("", 0, Noaction)
1602 :: ("Document", 0, Noaction)
1603 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1605 if birdseye
1606 then trailer
1607 else (
1608 ("", 0, Noaction)
1609 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1610 (btos conf.bedefault),
1612 Action (
1613 fun active first qsearch pan ->
1614 conf.bedefault <- not conf.bedefault;
1615 Items (active, first, makeitems (), qsearch, pan, mode);
1617 ) :: trailer
1620 Array.of_list (items @ tailer)
1622 state.text <- "";
1623 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1624 Glut.postRedisplay ();
1627 let enterhelpmode () =
1628 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1629 Glut.postRedisplay ();
1632 let quickbookmark ?title () =
1633 match state.layout with
1634 | [] -> ()
1635 | l :: _ ->
1636 let title =
1637 match title with
1638 | None ->
1639 let sec = Unix.gettimeofday () in
1640 let tm = Unix.localtime sec in
1641 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1642 (l.pageno+1)
1643 tm.Unix.tm_mday
1644 tm.Unix.tm_mon
1645 (tm.Unix.tm_year + 1900)
1646 tm.Unix.tm_hour
1647 tm.Unix.tm_min
1648 | Some title -> title
1650 state.bookmarks <-
1651 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1652 :: state.bookmarks
1655 let doreshape w h =
1656 state.fullscreen <- None;
1657 Glut.reshapeWindow w h;
1660 let writeopen path password =
1661 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1662 writecmd state.csock "info";
1665 let opendoc path password =
1666 invalidate ();
1667 state.path <- path;
1668 state.password <- password;
1669 state.gen <- state.gen + 1;
1671 writeopen path password;
1672 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1673 wcmd "geometry" [`i state.w; `i conf.winh];
1676 let viewkeyboard key =
1677 let enttext te =
1678 let mode = state.mode in
1679 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1680 state.text <- "";
1681 enttext ();
1682 Glut.postRedisplay ()
1684 let c = Char.chr key in
1685 match c with
1686 | '\027' | 'q' -> (* escape *)
1687 exit 0
1689 | '\008' -> (* backspace *)
1690 let y = getnav ~-1 in
1691 gotoy_and_clear_text y
1693 | 'o' ->
1694 enteroutlinemode ()
1696 | 'u' ->
1697 state.rects <- [];
1698 state.text <- "";
1699 Glut.postRedisplay ()
1701 | '/' | '?' ->
1702 let ondone isforw s =
1703 cbput state.hists.pat s;
1704 state.searchpattern <- s;
1705 search s isforw
1707 let s = String.create 1 in
1708 s.[0] <- c;
1709 enttext (s, "", Some (onhist state.hists.pat),
1710 textentry, ondone (c ='/'))
1712 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1713 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1714 setzoom (min 2.2 (conf.zoom +. incr))
1716 | '+' ->
1717 let ondone s =
1718 let n =
1719 try int_of_string s with exc ->
1720 state.text <- Printf.sprintf "bad integer `%s': %s"
1721 s (Printexc.to_string exc);
1722 max_int
1724 if n != max_int
1725 then (
1726 conf.pagebias <- n;
1727 state.text <- "page bias is now " ^ string_of_int n;
1730 enttext ("page bias", "", None, intentry, ondone)
1732 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1733 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1734 setzoom (max 0.01 (conf.zoom -. decr))
1736 | '-' ->
1737 let ondone msg = state.text <- msg in
1738 enttext (
1739 "option [acfhilpstvAPRSZ]", "", None,
1740 optentry state.mode, ondone
1743 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1744 setzoom 1.0
1746 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1747 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1748 if zoom < 1.0
1749 then setzoom zoom
1751 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1752 togglebirdseye ()
1754 | '0' .. '9' ->
1755 let ondone s =
1756 let n =
1757 try int_of_string s with exc ->
1758 state.text <- Printf.sprintf "bad integer `%s': %s"
1759 s (Printexc.to_string exc);
1762 if n >= 0
1763 then (
1764 addnav ();
1765 cbput state.hists.pag (string_of_int n);
1766 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1769 let pageentry text key =
1770 match Char.unsafe_chr key with
1771 | 'g' -> TEdone text
1772 | _ -> intentry text key
1774 let text = "x" in text.[0] <- c;
1775 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1777 | 'b' ->
1778 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1779 reshape conf.winw conf.winh;
1781 | 'l' ->
1782 conf.hlinks <- not conf.hlinks;
1783 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1784 Glut.postRedisplay ()
1786 | 'a' ->
1787 if state.ascrollstep = 0
1788 then state.ascrollstep <- conf.autoscrollstep
1789 else (
1790 conf.autoscrollstep <- state.ascrollstep;
1791 state.ascrollstep <- 0;
1794 | 'P' ->
1795 conf.presentation <- not conf.presentation;
1796 if conf.presentation
1797 then (
1798 if not conf.scrollbarinpm
1799 then state.scrollw <- 0;
1801 else
1802 state.scrollw <- conf.scrollbw;
1804 showtext ' ' ("presentation mode " ^
1805 if conf.presentation then "on" else "off");
1806 state.anchor <- getanchor ();
1807 represent ()
1809 | 'f' ->
1810 begin match state.fullscreen with
1811 | None ->
1812 state.fullscreen <- Some (conf.winw, conf.winh);
1813 Glut.fullScreen ()
1814 | Some (w, h) ->
1815 state.fullscreen <- None;
1816 doreshape w h
1819 | 'g' ->
1820 gotoy_and_clear_text 0
1822 | 'n' ->
1823 search state.searchpattern true
1825 | 'p' | 'N' ->
1826 search state.searchpattern false
1828 | 't' ->
1829 begin match state.layout with
1830 | [] -> ()
1831 | l :: _ ->
1832 gotoy_and_clear_text (getpagey l.pageno)
1835 | ' ' ->
1836 begin match List.rev state.layout with
1837 | [] -> ()
1838 | l :: _ ->
1839 let pageno = min (l.pageno+1) (state.pagecount-1) in
1840 gotoy_and_clear_text (getpagey pageno)
1843 | '\127' -> (* delte *)
1844 begin match state.layout with
1845 | [] -> ()
1846 | l :: _ ->
1847 let pageno = max 0 (l.pageno-1) in
1848 gotoy_and_clear_text (getpagey pageno)
1851 | '=' ->
1852 let f (fn, _) l =
1853 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1855 let fn, ln = List.fold_left f (-1, -1) state.layout in
1856 let s =
1857 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1858 let percent =
1859 if maxy <= 0
1860 then 100.
1861 else (100. *. (float state.y /. float maxy)) in
1862 if fn = ln
1863 then
1864 Printf.sprintf "Page %d of %d %.2f%%"
1865 (fn+1) state.pagecount percent
1866 else
1867 Printf.sprintf
1868 "Pages %d-%d of %d %.2f%%"
1869 (fn+1) (ln+1) state.pagecount percent
1871 showtext ' ' s;
1873 | 'w' ->
1874 begin match state.layout with
1875 | [] -> ()
1876 | l :: _ ->
1877 doreshape (l.pagew + state.scrollw) l.pageh;
1878 Glut.postRedisplay ();
1881 | '\'' ->
1882 enterbookmarkmode ()
1884 | 'h' ->
1885 enterhelpmode ()
1887 | 'i' ->
1888 enterinfomode ()
1890 | 'm' ->
1891 let ondone s =
1892 match state.layout with
1893 | l :: _ ->
1894 state.bookmarks <-
1895 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1896 :: state.bookmarks
1897 | _ -> ()
1899 enttext ("bookmark", "", None, textentry, ondone)
1901 | '~' ->
1902 quickbookmark ();
1903 showtext ' ' "Quick bookmark added";
1905 | 'z' ->
1906 begin match state.layout with
1907 | l :: _ ->
1908 let rect = getpdimrect l.pagedimno in
1909 let w, h =
1910 if conf.crophack
1911 then
1912 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1913 truncate (1.2 *. (rect.(3) -. rect.(0))))
1914 else
1915 (truncate (rect.(1) -. rect.(0)),
1916 truncate (rect.(3) -. rect.(0)))
1918 if w != 0 && h != 0
1919 then
1920 doreshape (w + state.scrollw) (h + conf.interpagespace)
1922 Glut.postRedisplay ();
1924 | [] -> ()
1927 | '<' | '>' ->
1928 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1930 | '[' | ']' ->
1931 state.colorscale <-
1932 max 0.0
1933 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1934 Glut.postRedisplay ()
1936 | 'k' ->
1937 begin match state.mode with
1938 | Birdseye beye -> upbirdseye beye
1939 | _ -> gotoy (clamp (-conf.scrollstep))
1942 | 'j' ->
1943 begin match state.mode with
1944 | Birdseye beye -> downbirdseye beye
1945 | _ -> gotoy (clamp conf.scrollstep)
1948 | 'r' ->
1949 state.anchor <- getanchor ();
1950 opendoc state.path state.password
1952 | _ ->
1953 vlog "huh? %d %c" key (Char.chr key);
1956 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1957 let enttext te =
1958 state.mode <- Textentry (te, onleave);
1959 state.text <- "";
1960 enttext ();
1961 Glut.postRedisplay ()
1963 match Char.unsafe_chr key with
1964 | '\008' -> (* backspace *)
1965 let len = String.length text in
1966 if len = 0
1967 then (
1968 onleave Cancel;
1969 Glut.postRedisplay ();
1971 else (
1972 let s = String.sub text 0 (len - 1) in
1973 enttext (c, s, opthist, onkey, ondone)
1976 | '\r' | '\n' ->
1977 ondone text;
1978 onleave Confirm;
1979 Glut.postRedisplay ()
1981 | '\027' -> (* escape *)
1982 begin match opthist with
1983 | None -> ()
1984 | Some (_, onhistcancel) -> onhistcancel ()
1985 end;
1986 onleave Cancel;
1987 Glut.postRedisplay ()
1989 | _ ->
1990 begin match onkey text key with
1991 | TEdone text ->
1992 onleave Confirm;
1993 ondone text;
1994 Glut.postRedisplay ()
1996 | TEcont text ->
1997 enttext (c, text, opthist, onkey, ondone);
1999 | TEstop ->
2000 onleave Cancel;
2001 Glut.postRedisplay ()
2003 | TEswitch te ->
2004 state.mode <- Textentry (te, onleave);
2005 Glut.postRedisplay ()
2006 end;
2009 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2010 match key with
2011 | 27 -> (* escape *)
2012 leavebirdseye beye true
2014 | 12 -> (* ctrl-l *)
2015 let y, h = getpageyh pageno in
2016 let top = (conf.winh - h) / 2 in
2017 gotoy (max 0 (y - top))
2019 | 13 -> (* enter *)
2020 leavebirdseye beye false
2022 | _ ->
2023 viewkeyboard key
2026 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2027 let set active first qsearch =
2028 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2030 let search active pattern incr =
2031 let dosearch re =
2032 let rec loop n =
2033 if n = Array.length items || n = -1
2034 then None
2035 else
2036 let (s, _, _) = items.(n) in
2038 (try ignore (Str.search_forward re s 0); true
2039 with Not_found -> false)
2040 then Some n
2041 else loop (n + incr)
2043 loop active
2046 let re = Str.regexp_case_fold pattern in
2047 dosearch re
2048 with Failure s ->
2049 state.text <- s;
2050 None
2052 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2053 match key with
2054 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2055 let incr = if key = 18 then -1 else 1 in
2056 let active, first =
2057 match search (active + incr) qsearch incr with
2058 | None ->
2059 state.text <- qsearch ^ " [not found]";
2060 active, first
2061 | Some active ->
2062 state.text <- qsearch;
2063 active, firstof active
2065 set active first qsearch;
2066 Glut.postRedisplay ();
2068 | 8 -> (* backspace *)
2069 let len = String.length qsearch in
2070 if len = 0
2071 then ()
2072 else (
2073 if len = 1
2074 then (
2075 state.text <- "";
2076 set active first "";
2078 else
2079 let qsearch = String.sub qsearch 0 (len - 1) in
2080 let active, first =
2081 match search active qsearch ~-1 with
2082 | None ->
2083 state.text <- qsearch ^ " [not found]";
2084 active, first
2085 | Some active ->
2086 state.text <- qsearch;
2087 active, firstof active
2089 set active first qsearch
2091 Glut.postRedisplay ()
2093 | _ when key >= 32 && key < 127 ->
2094 let pattern = addchar qsearch (Char.chr key) in
2095 let active, first =
2096 match search active pattern 1 with
2097 | None ->
2098 state.text <- pattern ^ " [not found]";
2099 active, first
2100 | Some active ->
2101 state.text <- pattern;
2102 active, firstof active
2104 set active first pattern;
2105 Glut.postRedisplay ()
2107 | 27 -> (* escape *)
2108 state.text <- "";
2109 if String.length qsearch = 0
2110 then (
2111 state.mode <- oldmode;
2113 else (
2114 set active first "";
2116 Glut.postRedisplay ()
2118 | 13 -> (* enter *)
2119 if active >= 0 && active < Array.length items
2120 then (
2121 match items.(active) with
2122 | _, _, Action f ->
2123 state.mode <- f active first qsearch pan
2125 | _, _, Noaction ->
2126 state.text <- "";
2127 state.mode <- oldmode
2129 else (
2130 state.text <- "";
2131 state.mode <- oldmode
2133 Glut.postRedisplay ();
2135 | _ -> dolog "unknown key %d" key
2138 let outlinekeyboard key
2139 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2140 let narrow outlines pattern =
2141 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2142 match reopt with
2143 | None -> None
2144 | Some re ->
2145 let rec fold accu n =
2146 if n = -1
2147 then accu
2148 else
2149 let (s, _, _) as o = outlines.(n) in
2150 let accu =
2151 if (try ignore (Str.search_forward re s 0); true
2152 with Not_found -> false)
2153 then (o :: accu)
2154 else accu
2156 fold accu (n-1)
2158 let matched = fold [] (Array.length outlines - 1) in
2159 if matched = [] then None else Some (Array.of_list matched)
2161 let search active pattern incr =
2162 let dosearch re =
2163 let rec loop n =
2164 if n = Array.length outlines || n = -1
2165 then None
2166 else
2167 let (s, _, _) = outlines.(n) in
2169 (try ignore (Str.search_forward re s 0); true
2170 with Not_found -> false)
2171 then Some n
2172 else loop (n + incr)
2174 loop active
2177 let re = Str.regexp_case_fold pattern in
2178 dosearch re
2179 with Failure s ->
2180 state.text <- s;
2181 None
2183 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2184 match key with
2185 | 27 -> (* escape *)
2186 state.text <- "";
2187 if String.length qsearch = 0
2188 then (
2189 state.mode <- oldmode;
2191 else (
2192 state.mode <- Outline (
2193 allowdel, active, first, outlines, "", pan, oldmode
2196 Glut.postRedisplay ();
2198 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2199 let incr = if key = 18 then -1 else 1 in
2200 let active, first =
2201 match search (active + incr) qsearch incr with
2202 | None ->
2203 state.text <- qsearch ^ " [not found]";
2204 active, first
2205 | Some active ->
2206 state.text <- qsearch;
2207 active, firstof active
2209 state.mode <- Outline (
2210 allowdel, active, first, outlines, qsearch, pan, oldmode
2212 Glut.postRedisplay ();
2214 | 8 -> (* backspace *)
2215 let len = String.length qsearch in
2216 if len = 0
2217 then ()
2218 else (
2219 if len = 1
2220 then (
2221 state.text <- "";
2222 state.mode <- Outline (
2223 allowdel, active, first, outlines, "", pan, oldmode
2226 else
2227 let qsearch = String.sub qsearch 0 (len - 1) in
2228 let active, first =
2229 match search active qsearch ~-1 with
2230 | None ->
2231 state.text <- qsearch ^ " [not found]";
2232 active, first
2233 | Some active ->
2234 state.text <- qsearch;
2235 active, firstof active
2237 state.mode <- Outline (
2238 allowdel, active, first, outlines, qsearch, pan, oldmode
2241 Glut.postRedisplay ()
2243 | 13 -> (* enter *)
2244 if active < Array.length outlines
2245 then (
2246 let (_, _, anchor) = outlines.(active) in
2247 addnav ();
2248 gotoanchor anchor;
2250 state.text <- "";
2251 if allowdel then state.bookmarks <- Array.to_list outlines;
2252 state.mode <- oldmode;
2253 Glut.postRedisplay ();
2255 | _ when key >= 32 && key < 127 ->
2256 let pattern = addchar qsearch (Char.chr key) in
2257 let active, first =
2258 match search active pattern 1 with
2259 | None ->
2260 state.text <- pattern ^ " [not found]";
2261 active, first
2262 | Some active ->
2263 state.text <- pattern;
2264 active, firstof active
2266 state.mode <- Outline (
2267 allowdel, active, first, outlines, pattern, pan, oldmode
2269 Glut.postRedisplay ()
2271 | 14 when not allowdel -> (* ctrl-n *)
2272 if String.length qsearch > 0
2273 then (
2274 let optoutlines = narrow outlines qsearch in
2275 begin match optoutlines with
2276 | None -> state.text <- "can't narrow"
2277 | Some outlines ->
2278 state.mode <- Outline (
2279 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2281 match state.outlines with
2282 | Olist _ -> ()
2283 | Oarray a ->
2284 state.outlines <- Onarrow (qsearch, outlines, a)
2285 | Onarrow (_, _, b) ->
2286 state.outlines <- Onarrow (qsearch, outlines, b)
2287 end;
2289 Glut.postRedisplay ()
2291 | 21 when not allowdel -> (* ctrl-u *)
2292 let outline =
2293 match state.outlines with
2294 | Oarray a -> a
2295 | Olist l ->
2296 let a = Array.of_list (List.rev l) in
2297 state.outlines <- Oarray a;
2299 | Onarrow (_, _, b) ->
2300 state.outlines <- Oarray b;
2301 state.text <- "";
2304 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2305 Glut.postRedisplay ()
2307 | 12 -> (* ctrl-l *)
2308 state.mode <- Outline
2309 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2310 Glut.postRedisplay ()
2312 | 127 when allowdel -> (* delete *)
2313 let len = Array.length outlines - 1 in
2314 if len = 0
2315 then (
2316 state.mode <- View;
2317 state.bookmarks <- [];
2319 else (
2320 let bookmarks = Array.init len
2321 (fun i ->
2322 let i = if i >= active then i + 1 else i in
2323 outlines.(i)
2326 state.mode <-
2327 Outline (
2328 allowdel,
2329 min active (len-1),
2330 min first (len-1),
2331 bookmarks, qsearch,
2333 oldmode
2336 Glut.postRedisplay ()
2338 | _ -> dolog "unknown key %d" key
2341 let keyboard ~key ~x ~y =
2342 ignore x;
2343 ignore y;
2344 if key = 7 (* ctrl-g *)
2345 then
2346 wcmd "interrupt" []
2347 else
2348 match state.mode with
2349 | Outline outline -> outlinekeyboard key outline
2350 | Textentry textentry -> textentrykeyboard key textentry
2351 | Birdseye birdseye -> birdseyekeyboard key birdseye
2352 | View -> viewkeyboard key
2353 | Items items -> itemskeyboard key items
2356 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2357 match key with
2358 | Glut.KEY_UP -> upbirdseye beye
2359 | Glut.KEY_DOWN -> downbirdseye beye
2361 | Glut.KEY_PAGE_UP ->
2362 begin match state.layout with
2363 | l :: _ ->
2364 if l.pagey != 0
2365 then (
2366 state.mode <- Birdseye (
2367 conf, leftx, l.pageno, hooverpageno, anchor
2369 gotopage1 l.pageno 0;
2371 else (
2372 let layout = layout (state.y-conf.winh) conf.winh in
2373 match layout with
2374 | [] -> gotoy (clamp (-conf.winh))
2375 | l :: _ ->
2376 state.mode <- Birdseye (
2377 conf, leftx, l.pageno, hooverpageno, anchor
2379 gotopage1 l.pageno 0
2382 | [] -> gotoy (clamp (-conf.winh))
2383 end;
2385 | Glut.KEY_PAGE_DOWN ->
2386 begin match List.rev state.layout with
2387 | l :: _ ->
2388 let layout = layout (state.y + conf.winh) conf.winh in
2389 begin match layout with
2390 | [] ->
2391 let incr = l.pageh - l.pagevh in
2392 if incr = 0
2393 then (
2394 state.mode <-
2395 Birdseye (
2396 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2398 Glut.postRedisplay ();
2400 else gotoy (clamp (incr + conf.interpagespace*2));
2402 | l :: _ ->
2403 state.mode <-
2404 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2405 gotopage1 l.pageno 0;
2408 | [] -> gotoy (clamp conf.winh)
2409 end;
2411 | Glut.KEY_HOME ->
2412 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2413 gotopage1 0 0
2415 | Glut.KEY_END ->
2416 let pageno = state.pagecount - 1 in
2417 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2418 if not (pagevisible state.layout pageno)
2419 then
2420 let h =
2421 match List.rev state.pdims with
2422 | [] -> conf.winh
2423 | (_, _, h, _) :: _ -> h
2425 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2426 else Glut.postRedisplay ();
2427 | _ -> ()
2430 let setautoscrollspeed goingdown =
2431 let incr = max 1 (state.ascrollstep / 2) in
2432 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2433 state.ascrollstep <- astep;
2436 let special ~key ~x ~y =
2437 ignore x;
2438 ignore y;
2439 match state.mode with
2440 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2441 togglebirdseye ()
2443 | Birdseye vals ->
2444 birdseyespecial key vals
2446 | View when key = Glut.KEY_F1 ->
2447 enterhelpmode ()
2449 | View ->
2450 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2451 then setautoscrollspeed (key = Glut.KEY_DOWN)
2452 else
2453 let y =
2454 match key with
2455 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2456 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2457 | Glut.KEY_DOWN -> clamp conf.scrollstep
2458 | Glut.KEY_PAGE_UP ->
2459 if Glut.getModifiers () land Glut.active_ctrl != 0
2460 then
2461 match state.layout with
2462 | [] -> state.y
2463 | l :: _ -> state.y - l.pagey
2464 else
2465 clamp (-conf.winh)
2466 | Glut.KEY_PAGE_DOWN ->
2467 if Glut.getModifiers () land Glut.active_ctrl != 0
2468 then
2469 match List.rev state.layout with
2470 | [] -> state.y
2471 | l :: _ -> getpagey l.pageno
2472 else
2473 clamp conf.winh
2474 | Glut.KEY_HOME -> addnav (); 0
2475 | Glut.KEY_END ->
2476 addnav ();
2477 state.maxy - (if conf.maxhfit then conf.winh else 0)
2479 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2480 Glut.getModifiers () land Glut.active_alt != 0 ->
2481 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2483 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2484 state.x <- state.x - 10;
2485 state.y
2486 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2487 state.x <- state.x + 10;
2488 state.y
2490 | _ -> state.y
2492 gotoy_and_clear_text y
2494 | Textentry
2495 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2496 let s =
2497 match key with
2498 | Glut.KEY_UP -> action HCprev
2499 | Glut.KEY_DOWN -> action HCnext
2500 | Glut.KEY_HOME -> action HCfirst
2501 | Glut.KEY_END -> action HClast
2502 | _ -> state.text
2504 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2505 Glut.postRedisplay ()
2507 | Textentry _ -> ()
2509 | Items (active, first, items, qsearch, pan, oldmode) ->
2510 let maxrows = maxoutlinerows () in
2511 let itemcount = Array.length items in
2512 let hasaction = function
2513 | (_, _, Noaction) -> false
2514 | _ -> true
2516 let find start incr =
2517 let rec find i =
2518 if i = -1 || i = itemcount
2519 then -1
2520 else (
2521 if hasaction items.(i)
2522 then i
2523 else find (i + incr)
2526 find start
2528 let set active first =
2529 let first = max 0 (min first (itemcount - maxrows)) in
2530 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2532 let navigate incr =
2533 let isvisible first n = n >= first && n - first <= maxrows in
2534 let active, first =
2535 let incr1 = if incr > 0 then 1 else -1 in
2536 if isvisible first active
2537 then
2538 let next =
2539 let next = active + incr in
2540 let next =
2541 if next < 0 || next >= itemcount
2542 then -1
2543 else find next incr1
2545 if next = -1 || abs (active - next) > maxrows
2546 then -1
2547 else next
2549 if next = -1
2550 then
2551 let first = first + incr in
2552 let first = max 0 (min first (itemcount - 1)) in
2553 let next =
2554 let next = active + incr in
2555 let next = max 0 (min next (itemcount - 1)) in
2556 find next ~-incr1
2558 let active = if next = -1 then active else next in
2559 active, first
2560 else
2561 let first = min next first in
2562 next, first
2563 else
2564 let first = first + incr in
2565 let first = max 0 (min first (itemcount - 1)) in
2566 let active =
2567 let next = active + incr in
2568 let next = max 0 (min next (itemcount - 1)) in
2569 let next = find next incr1 in
2570 if next = -1 || abs (active - first) > maxrows
2571 then active
2572 else next
2574 active, first
2576 set active first;
2577 Glut.postRedisplay ()
2579 begin match key with
2580 | Glut.KEY_UP -> navigate ~-1
2581 | Glut.KEY_DOWN -> navigate 1
2582 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2583 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2585 | Glut.KEY_RIGHT ->
2586 state.mode <- Items (
2587 active, first, items, qsearch, min 0 (pan - 1), oldmode
2589 Glut.postRedisplay ()
2591 | Glut.KEY_LEFT ->
2592 state.mode <- Items (
2593 active, first, items, qsearch, min 0 (pan + 1), oldmode
2595 Glut.postRedisplay ()
2597 | Glut.KEY_HOME ->
2598 let active = find 0 1 in
2599 set active 0;
2600 Glut.postRedisplay ()
2602 | Glut.KEY_END ->
2603 let first = max 0 (itemcount - maxrows) in
2604 let active = find (itemcount - 1) ~-1 in
2605 set active first;
2606 Glut.postRedisplay ()
2608 | _ -> ()
2609 end;
2611 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2612 let maxrows = maxoutlinerows () in
2613 let calcfirst first active =
2614 if active > first
2615 then
2616 let rows = active - first in
2617 if rows > maxrows then active - maxrows else first
2618 else active
2620 let navigate incr =
2621 let active = active + incr in
2622 let active = max 0 (min active (Array.length outlines - 1)) in
2623 let first = calcfirst first active in
2624 state.mode <- Outline (
2625 allowdel, active, first, outlines, qsearch, pan, oldmode
2627 Glut.postRedisplay ()
2629 let updownlevel incr =
2630 let len = Array.length outlines in
2631 let (_, curlevel, _) = outlines.(active) in
2632 let rec flow i =
2633 if i = len then i-1 else if i = -1 then 0 else
2634 let (_, l, _) = outlines.(i) in
2635 if l != curlevel then i else flow (i+incr)
2637 let active = flow active in
2638 let first = calcfirst first active in
2639 state.mode <- Outline (
2640 allowdel, active, first, outlines, qsearch, pan, oldmode
2642 Glut.postRedisplay ()
2644 match key with
2645 | Glut.KEY_UP -> navigate ~-1
2646 | Glut.KEY_DOWN -> navigate 1
2647 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2648 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2650 | Glut.KEY_RIGHT ->
2651 if Glut.getModifiers () land Glut.active_ctrl != 0
2652 then (
2653 state.mode <- Outline (
2654 allowdel, active, first, outlines,
2655 qsearch, min 0 (pan + 1), oldmode
2657 Glut.postRedisplay ();
2659 else (
2660 if not allowdel
2661 then updownlevel 1
2664 | Glut.KEY_LEFT ->
2665 if Glut.getModifiers () land Glut.active_ctrl != 0
2666 then (
2667 state.mode <- Outline (
2668 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2670 Glut.postRedisplay ();
2672 else (
2673 if not allowdel
2674 then updownlevel ~-1
2677 | Glut.KEY_HOME ->
2678 state.mode <- Outline (
2679 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2681 Glut.postRedisplay ()
2683 | Glut.KEY_END ->
2684 let active = Array.length outlines - 1 in
2685 let first = max 0 (active - maxrows) in
2686 state.mode <- Outline (
2687 allowdel, active, first, outlines, qsearch, pan, oldmode
2689 Glut.postRedisplay ()
2691 | _ -> ()
2694 let drawplaceholder l =
2695 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2696 GlDraw.rect
2697 (float l.pagex, float l.pagedispy)
2698 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2700 let x = if margin < 0 then -margin else l.pagex
2701 and y = l.pagedispy + 13 in
2702 GlDraw.color (0.0, 0.0, 0.0);
2703 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2706 let now () = Unix.gettimeofday ();;
2708 let drawpage l =
2709 let color =
2710 match state.mode with
2711 | Textentry _ -> scalecolor 0.4
2712 | View | Outline _ | Items _ -> scalecolor 1.0
2713 | Birdseye (_, _, pageno, hooverpageno, _) ->
2714 if l.pageno = hooverpageno
2715 then scalecolor 0.9
2716 else (
2717 if l.pageno = pageno
2718 then scalecolor 1.0
2719 else scalecolor 0.8
2722 GlDraw.color color;
2723 begin match getopaque l.pageno with
2724 | Some (opaque, _) when validopaque opaque ->
2725 let a = now () in
2726 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2727 opaque;
2728 let b = now () in
2729 let d = b-.a in
2730 vlog "draw %d %f sec" l.pageno d;
2732 | _ ->
2733 drawplaceholder l;
2734 end;
2737 let scrollph y =
2738 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2739 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2740 let sh = float conf.winh /. sh in
2741 let sh = max sh (float conf.scrollh) in
2743 let percent =
2744 if state.y = state.maxy
2745 then 1.0
2746 else float y /. float maxy
2748 let position = (float conf.winh -. sh) *. percent in
2750 let position =
2751 if position +. sh > float conf.winh
2752 then float conf.winh -. sh
2753 else position
2755 position, sh;
2758 let scrollindicator () =
2759 GlDraw.color (0.64 , 0.64, 0.64);
2760 GlDraw.rect
2761 (float (conf.winw - state.scrollw), 0.)
2762 (float conf.winw, float conf.winh)
2764 GlDraw.color (0.0, 0.0, 0.0);
2766 let position, sh = scrollph state.y in
2767 GlDraw.rect
2768 (float (conf.winw - state.scrollw), position)
2769 (float conf.winw, position +. sh)
2773 let showsel margin =
2774 match state.mstate with
2775 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2778 | Msel ((x0, y0), (x1, y1)) ->
2779 let rec loop = function
2780 | l :: ls ->
2781 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2782 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2783 then
2784 match getopaque l.pageno with
2785 | Some (opaque, _) when validopaque opaque ->
2786 let oy = -l.pagey + l.pagedispy in
2787 seltext opaque
2788 (x0 - margin - state.x, y0,
2789 x1 - margin - state.x, y1) oy;
2791 | _ -> ()
2792 else loop ls
2793 | [] -> ()
2795 loop state.layout
2798 let showrects () =
2799 let panx = float state.x in
2800 Gl.enable `blend;
2801 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2802 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2803 List.iter
2804 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2805 List.iter (fun l ->
2806 if l.pageno = pageno
2807 then (
2808 let d = float (l.pagedispy - l.pagey) in
2809 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2810 GlDraw.begins `quads;
2812 GlDraw.vertex2 (x0+.panx, y0+.d);
2813 GlDraw.vertex2 (x1+.panx, y1+.d);
2814 GlDraw.vertex2 (x2+.panx, y2+.d);
2815 GlDraw.vertex2 (x3+.panx, y3+.d);
2817 GlDraw.ends ();
2819 ) state.layout
2820 ) state.rects
2822 Gl.disable `blend;
2825 let showstrings active first pan strings =
2826 Gl.enable `blend;
2827 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2828 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2829 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2830 GlDraw.color (1., 1., 1.);
2831 Gl.enable `texture_2d;
2833 let rec loop row =
2834 if row = Array.length strings || (row - first) * 16 > conf.winh
2835 then ()
2836 else (
2837 let (s, level, _) = strings.(row) in
2838 let y = (row - first) * 16 in
2839 let x = 5 + 15*(max 0 (level+pan)) in
2840 if row = active
2841 then (
2842 Gl.disable `texture_2d;
2843 GlDraw.polygon_mode `both `line;
2844 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2845 GlDraw.rect (0., float (y + 1))
2846 (float (conf.winw - 1), float (y + 18));
2847 GlDraw.polygon_mode `both `fill;
2848 GlDraw.color (1., 1., 1.);
2849 Gl.enable `texture_2d;
2851 let draw_string s =
2852 let l = String.length s in
2853 if pan < 0
2854 then (
2855 let pan = pan * 2 in
2856 let pos = pan + level in
2857 let left = l + pos in
2858 if left > 0
2859 then
2860 let s =
2861 if left > l
2862 then s
2863 else String.sub s (-pos) left
2865 drawstring1 14 x (y + 16) s
2867 else
2868 drawstring1 14 (x + pan*15) (y + 16) s
2870 draw_string s;
2871 loop (row+1)
2874 loop first;
2875 Gl.disable `blend;
2876 Gl.disable `texture_2d;
2879 let showoutline (_, active, first, outlines, _, pan, _) =
2880 showstrings active first pan outlines;
2883 let showitems (active, first, items, _, pan, _) =
2884 showstrings active first pan items;
2887 let display () =
2888 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2889 GlDraw.viewport margin 0 state.w conf.winh;
2890 pagematrix ();
2891 GlClear.color (scalecolor2 conf.bgcolor);
2892 GlClear.clear [`color];
2893 if conf.zoom > 1.0
2894 then (
2895 Gl.enable `scissor_test;
2896 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2898 List.iter drawpage state.layout;
2899 if conf.zoom > 1.0
2900 then
2901 Gl.disable `scissor_test
2903 if state.x != 0
2904 then (
2905 let x = -.float state.x in
2906 GlMat.translate ~x ();
2908 showrects ();
2909 showsel margin;
2910 GlDraw.viewport 0 0 conf.winw conf.winh;
2911 winmatrix ();
2912 scrollindicator ();
2913 begin match state.mode with
2914 | Items items -> showitems items
2915 | Outline outline -> showoutline outline
2916 | _ -> ()
2917 end;
2918 enttext ();
2919 Glut.swapBuffers ();
2922 let getunder x y =
2923 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2924 let x = x - margin - state.x in
2925 let rec f = function
2926 | l :: rest ->
2927 begin match getopaque l.pageno with
2928 | Some (opaque, _) when validopaque opaque ->
2929 let y = y - l.pagedispy in
2930 if y > 0
2931 then
2932 let y = l.pagey + y in
2933 let x = x - l.pagex in
2934 match whatsunder opaque x y with
2935 | Unone -> f rest
2936 | under -> under
2937 else
2938 f rest
2939 | _ ->
2940 f rest
2942 | [] -> Unone
2944 f state.layout
2947 let viewmouse button bstate x y =
2948 match button with
2949 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2950 if Glut.getModifiers () land Glut.active_ctrl != 0
2951 then (
2952 match state.mstate with
2953 | Mzoom (oldn, i) ->
2954 if oldn = n
2955 then (
2956 if i = 2
2957 then
2958 let incr =
2959 match n with
2960 | 4 ->
2961 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2962 | _ ->
2963 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2965 let zoom = conf.zoom +. incr in
2966 setzoom zoom;
2967 state.mstate <- Mzoom (n, 0);
2968 else
2969 state.mstate <- Mzoom (n, i+1);
2971 else state.mstate <- Mzoom (n, 0)
2973 | _ -> state.mstate <- Mzoom (n, 0)
2975 else (
2976 if state.ascrollstep > 0
2977 then
2978 setautoscrollspeed (n=4)
2979 else
2980 let incr =
2981 if n = 3
2982 then -conf.scrollstep
2983 else conf.scrollstep
2985 let incr = incr * 2 in
2986 let y = clamp incr in
2987 gotoy_and_clear_text y
2990 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2991 if bstate = Glut.DOWN
2992 then (
2993 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2994 state.mstate <- Mpan (x, y)
2996 else
2997 state.mstate <- Mnone
2999 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3000 if bstate = Glut.DOWN
3001 then
3002 let position, sh = scrollph state.y in
3003 if y > truncate position && y < truncate (position +. sh)
3004 then
3005 state.mstate <- Mscroll
3006 else
3007 let percent = float y /. float conf.winh in
3008 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3009 gotoy desty;
3010 state.mstate <- Mscroll
3011 else
3012 state.mstate <- Mnone
3014 | Glut.LEFT_BUTTON ->
3015 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3016 begin match dest with
3017 | Ulinkgoto (pageno, top) ->
3018 if pageno >= 0
3019 then (
3020 addnav ();
3021 gotopage1 pageno top;
3024 | Ulinkuri s ->
3025 print_endline s
3027 | Unone when bstate = Glut.DOWN ->
3028 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3029 state.mstate <- Mpan (x, y);
3031 | Unone | Utext _ ->
3032 if bstate = Glut.DOWN
3033 then (
3034 if conf.angle mod 360 = 0
3035 then (
3036 state.mstate <- Msel ((x, y), (x, y));
3037 Glut.postRedisplay ()
3040 else (
3041 match state.mstate with
3042 | Mnone -> ()
3044 | Mzoom _ | Mscroll ->
3045 state.mstate <- Mnone
3047 | Mpan _ ->
3048 Glut.setCursor Glut.CURSOR_INHERIT;
3049 state.mstate <- Mnone
3051 | Msel ((_, y0), (_, y1)) ->
3052 let f l =
3053 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3054 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3055 then
3056 match getopaque l.pageno with
3057 | Some (opaque, _) when validopaque opaque ->
3058 copysel opaque
3059 | _ -> ()
3061 List.iter f state.layout;
3062 copysel ""; (* ugly *)
3063 Glut.setCursor Glut.CURSOR_INHERIT;
3064 state.mstate <- Mnone;
3068 | _ -> ()
3071 let birdseyemouse button bstate x y
3072 (conf, leftx, _, hooverpageno, anchor) =
3073 match button with
3074 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3075 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3076 let rec loop = function
3077 | [] -> ()
3078 | l :: rest ->
3079 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3080 && x > margin && x < margin + l.pagew
3081 then (
3082 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3084 else loop rest
3086 loop state.layout
3087 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3088 | _ -> ()
3091 let mouse bstate button x y =
3092 match state.mode with
3093 | View -> viewmouse button bstate x y
3094 | Birdseye beye -> birdseyemouse button bstate x y beye
3095 | Textentry _ | Outline _ | Items _ -> ()
3098 let mouse ~button ~state ~x ~y = mouse state button x y;;
3100 let motion ~x ~y =
3101 match state.mode with
3102 | Outline _ -> ()
3103 | _ ->
3104 match state.mstate with
3105 | Mzoom _ | Mnone -> ()
3107 | Mpan (x0, y0) ->
3108 let dx = x - x0
3109 and dy = y0 - y in
3110 state.mstate <- Mpan (x, y);
3111 if conf.zoom > 1.0 then state.x <- state.x + dx;
3112 let y = clamp dy in
3113 gotoy_and_clear_text y
3115 | Msel (a, _) ->
3116 state.mstate <- Msel (a, (x, y));
3117 Glut.postRedisplay ()
3119 | Mscroll ->
3120 let y = min conf.winh (max 0 y) in
3121 let percent = float y /. float conf.winh in
3122 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3123 gotoy_and_clear_text y
3126 let pmotion ~x ~y =
3127 match state.mode with
3128 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3129 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3130 let rec loop = function
3131 | [] ->
3132 if hooverpageno != -1
3133 then (
3134 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3135 Glut.postRedisplay ();
3137 | l :: rest ->
3138 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3139 && x > margin && x < margin + l.pagew
3140 then (
3141 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3142 Glut.postRedisplay ();
3144 else loop rest
3146 loop state.layout
3148 | Outline _ | Items _ | Textentry _ -> ()
3149 | View ->
3150 match state.mstate with
3151 | Mnone ->
3152 begin match getunder x y with
3153 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3154 | Ulinkuri uri ->
3155 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3156 Glut.setCursor Glut.CURSOR_INFO
3157 | Ulinkgoto (page, _) ->
3158 if conf.underinfo
3159 then showtext 'p' ("age: " ^ string_of_int page);
3160 Glut.setCursor Glut.CURSOR_INFO
3161 | Utext s ->
3162 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3163 Glut.setCursor Glut.CURSOR_TEXT
3166 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3171 module State =
3172 struct
3173 open Parser
3175 let home =
3177 match Sys.os_type with
3178 | "Win32" -> Sys.getenv "HOMEPATH"
3179 | _ -> Sys.getenv "HOME"
3180 with exn ->
3181 prerr_endline
3182 ("Can not determine home directory location: " ^
3183 Printexc.to_string exn);
3187 let config_of c attrs =
3188 let apply c k v =
3190 match k with
3191 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3192 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3193 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3194 | "preload" -> { c with preload = bool_of_string v }
3195 | "page-bias" -> { c with pagebias = int_of_string v }
3196 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3197 | "auto-scroll-step" ->
3198 { c with autoscrollstep = max 0 (int_of_string v) }
3199 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3200 | "crop-hack" -> { c with crophack = bool_of_string v }
3201 | "throttle" -> { c with showall = bool_of_string v }
3202 | "highlight-links" -> { c with hlinks = bool_of_string v }
3203 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3204 | "vertical-margin" ->
3205 { c with interpagespace = max 0 (int_of_string v) }
3206 | "zoom" ->
3207 let zoom = float_of_string v /. 100. in
3208 let zoom = max 0.01 (min 2.2 zoom) in
3209 { c with zoom = zoom }
3210 | "presentation" -> { c with presentation = bool_of_string v }
3211 | "rotation-angle" -> { c with angle = int_of_string v }
3212 | "width" -> { c with winw = max 20 (int_of_string v) }
3213 | "height" -> { c with winh = max 20 (int_of_string v) }
3214 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3215 | "proportional-display" -> { c with proportional = bool_of_string v }
3216 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3217 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3218 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3219 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3220 | "persistent-location" -> { c with jumpback = bool_of_string v }
3221 | "background-color" -> { c with bgcolor = color_of_string v }
3222 | "scrollbar-in-presentation" ->
3223 { c with scrollbarinpm = bool_of_string v }
3224 | "ui-font" -> { c with uifont = v }
3225 | _ -> c
3226 with exn ->
3227 prerr_endline ("Error processing attribute (`" ^
3228 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3231 let rec fold c = function
3232 | [] -> c
3233 | (k, v) :: rest ->
3234 let c = apply c k v in
3235 fold c rest
3237 fold c attrs;
3240 let fromstring f pos n v d =
3241 try f v
3242 with exn ->
3243 dolog "Error processing attribute (%S=%S) at %d\n%s"
3244 n v pos (Printexc.to_string exn)
3249 let bookmark_of attrs =
3250 let rec fold title page rely = function
3251 | ("title", v) :: rest -> fold v page rely rest
3252 | ("page", v) :: rest -> fold title v rely rest
3253 | ("rely", v) :: rest -> fold title page v rest
3254 | _ :: rest -> fold title page rely rest
3255 | [] -> title, page, rely
3257 fold "invalid" "0" "0" attrs
3260 let doc_of attrs =
3261 let rec fold path page rely pan = function
3262 | ("path", v) :: rest -> fold v page rely pan rest
3263 | ("page", v) :: rest -> fold path v rely pan rest
3264 | ("rely", v) :: rest -> fold path page v pan rest
3265 | ("pan", v) :: rest -> fold path page rely v rest
3266 | _ :: rest -> fold path page rely pan rest
3267 | [] -> path, page, rely, pan
3269 fold "" "0" "0" "0" attrs
3272 let setconf dst src =
3273 dst.scrollbw <- src.scrollbw;
3274 dst.scrollh <- src.scrollh;
3275 dst.icase <- src.icase;
3276 dst.preload <- src.preload;
3277 dst.pagebias <- src.pagebias;
3278 dst.verbose <- src.verbose;
3279 dst.scrollstep <- src.scrollstep;
3280 dst.maxhfit <- src.maxhfit;
3281 dst.crophack <- src.crophack;
3282 dst.autoscrollstep <- src.autoscrollstep;
3283 dst.showall <- src.showall;
3284 dst.hlinks <- src.hlinks;
3285 dst.underinfo <- src.underinfo;
3286 dst.interpagespace <- src.interpagespace;
3287 dst.zoom <- src.zoom;
3288 dst.presentation <- src.presentation;
3289 dst.angle <- src.angle;
3290 dst.winw <- src.winw;
3291 dst.winh <- src.winh;
3292 dst.savebmarks <- src.savebmarks;
3293 dst.memlimit <- src.memlimit;
3294 dst.proportional <- src.proportional;
3295 dst.texcount <- src.texcount;
3296 dst.sliceheight <- src.sliceheight;
3297 dst.thumbw <- src.thumbw;
3298 dst.jumpback <- src.jumpback;
3299 dst.bgcolor <- src.bgcolor;
3300 dst.scrollbarinpm <- src.scrollbarinpm;
3301 dst.uifont <- src.uifont;
3304 let unent s =
3305 let l = String.length s in
3306 let b = Buffer.create l in
3307 unent b s 0 l;
3308 Buffer.contents b;
3311 let get s =
3312 let h = Hashtbl.create 10 in
3313 let dc = { defconf with angle = defconf.angle } in
3314 let rec toplevel v t spos _ =
3315 match t with
3316 | Vdata | Vcdata | Vend -> v
3317 | Vopen ("llppconfig", _, closed) ->
3318 if closed
3319 then v
3320 else { v with f = llppconfig }
3321 | Vopen _ ->
3322 error "unexpected subelement at top level" s spos
3323 | Vclose _ -> error "unexpected close at top level" s spos
3325 and llppconfig v t spos _ =
3326 match t with
3327 | Vdata | Vcdata | Vend -> v
3328 | Vopen ("defaults", attrs, closed) ->
3329 let c = config_of dc attrs in
3330 setconf dc c;
3331 if closed
3332 then v
3333 else { v with f = skip "defaults" (fun () -> v) }
3335 | Vopen ("doc", attrs, closed) ->
3336 let pathent, spage, srely, span = doc_of attrs in
3337 let path = unent pathent
3338 and pageno = fromstring int_of_string spos "page" spage 0
3339 and rely = fromstring float_of_string spos "rely" srely 0.0
3340 and pan = fromstring int_of_string spos "pan" span 0 in
3341 let c = config_of dc attrs in
3342 let anchor = (pageno, rely) in
3343 if closed
3344 then (Hashtbl.add h path (c, [], pan, anchor); v)
3345 else { v with f = doc path pan anchor c [] }
3347 | Vopen _ ->
3348 error "unexpected subelement in llppconfig" s spos
3350 | Vclose "llppconfig" -> { v with f = toplevel }
3351 | Vclose _ -> error "unexpected close in llppconfig" s spos
3353 and doc path pan anchor c bookmarks v t spos _ =
3354 match t with
3355 | Vdata | Vcdata -> v
3356 | Vend -> error "unexpected end of input in doc" s spos
3357 | Vopen ("bookmarks", _, closed) ->
3358 if closed
3359 then v
3360 else { v with f = pbookmarks path pan anchor c bookmarks }
3362 | Vopen (_, _, _) ->
3363 error "unexpected subelement in doc" s spos
3365 | Vclose "doc" ->
3366 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3367 { v with f = llppconfig }
3369 | Vclose _ -> error "unexpected close in doc" s spos
3371 and pbookmarks path pan anchor c bookmarks v t spos _ =
3372 match t with
3373 | Vdata | Vcdata -> v
3374 | Vend -> error "unexpected end of input in bookmarks" s spos
3375 | Vopen ("item", attrs, closed) ->
3376 let titleent, spage, srely = bookmark_of attrs in
3377 let page = fromstring int_of_string spos "page" spage 0
3378 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3379 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3380 if closed
3381 then { v with f = pbookmarks path pan anchor c bookmarks }
3382 else
3383 let f () = v in
3384 { v with f = skip "item" f }
3386 | Vopen _ ->
3387 error "unexpected subelement in bookmarks" s spos
3389 | Vclose "bookmarks" ->
3390 { v with f = doc path pan anchor c bookmarks }
3392 | Vclose _ -> error "unexpected close in bookmarks" s spos
3394 and skip tag f v t spos _ =
3395 match t with
3396 | Vdata | Vcdata -> v
3397 | Vend ->
3398 error ("unexpected end of input in skipped " ^ tag) s spos
3399 | Vopen (tag', _, closed) ->
3400 if closed
3401 then v
3402 else
3403 let f' () = { v with f = skip tag f } in
3404 { v with f = skip tag' f' }
3405 | Vclose ctag ->
3406 if tag = ctag
3407 then f ()
3408 else error ("unexpected close in skipped " ^ tag) s spos
3411 parse { f = toplevel; accu = () } s;
3412 h, dc;
3415 let do_load f ic =
3417 let len = in_channel_length ic in
3418 let s = String.create len in
3419 really_input ic s 0 len;
3420 f s;
3421 with
3422 | Parse_error (msg, s, pos) ->
3423 let subs = subs s pos in
3424 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3425 failwith ("parse error: " ^ s)
3427 | exn ->
3428 failwith ("config load error: " ^ Printexc.to_string exn)
3431 let path =
3432 let dir =
3434 let dir = Filename.concat home ".config" in
3435 if Sys.is_directory dir then dir else home
3436 with _ -> home
3438 Filename.concat dir "llpp.conf"
3441 let load1 f =
3442 if Sys.file_exists path
3443 then
3444 match
3445 (try Some (open_in_bin path)
3446 with exn ->
3447 prerr_endline
3448 ("Error opening configuation file `" ^ path ^ "': " ^
3449 Printexc.to_string exn);
3450 None
3452 with
3453 | Some ic ->
3454 begin try
3455 f (do_load get ic)
3456 with exn ->
3457 prerr_endline
3458 ("Error loading configuation from `" ^ path ^ "': " ^
3459 Printexc.to_string exn);
3460 end;
3461 close_in ic;
3463 | None -> ()
3464 else
3465 f (Hashtbl.create 0, defconf)
3468 let load () =
3469 let f (h, dc) =
3470 let pc, pb, px, pa =
3472 Hashtbl.find h (Filename.basename state.path)
3473 with Not_found -> dc, [], 0, (0, 0.0)
3475 setconf defconf dc;
3476 setconf conf pc;
3477 state.bookmarks <- pb;
3478 state.x <- px;
3479 state.scrollw <- conf.scrollbw;
3480 if conf.jumpback
3481 then state.anchor <- pa;
3482 cbput state.hists.nav pa;
3484 load1 f
3487 let add_attrs bb always dc c =
3488 let ob s a b =
3489 if always || a != b
3490 then Printf.bprintf bb "\n %s='%b'" s a
3491 and oi s a b =
3492 if always || a != b
3493 then Printf.bprintf bb "\n %s='%d'" s a
3494 and oz s a b =
3495 if always || a <> b
3496 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3497 and oc s a b =
3498 if always || a <> b
3499 then
3500 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3501 and os s a b =
3502 if always || a <> b
3503 then
3504 Printf.bprintf bb "\n %s='%s'" s a
3506 let w, h =
3507 if always
3508 then dc.winw, dc.winh
3509 else
3510 match state.fullscreen with
3511 | Some wh -> wh
3512 | None -> c.winw, c.winh
3514 let zoom, presentation, interpagespace, showall=
3515 if always
3516 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3517 else
3518 match state.mode with
3519 | Birdseye (bc, _, _, _, _) ->
3520 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3521 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3523 oi "width" w dc.winw;
3524 oi "height" h dc.winh;
3525 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3526 oi "scroll-handle-height" c.scrollh dc.scrollh;
3527 ob "case-insensitive-search" c.icase dc.icase;
3528 ob "preload" c.preload dc.preload;
3529 oi "page-bias" c.pagebias dc.pagebias;
3530 oi "scroll-step" c.scrollstep dc.scrollstep;
3531 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3532 ob "max-height-fit" c.maxhfit dc.maxhfit;
3533 ob "crop-hack" c.crophack dc.crophack;
3534 ob "throttle" showall dc.showall;
3535 ob "highlight-links" c.hlinks dc.hlinks;
3536 ob "under-cursor-info" c.underinfo dc.underinfo;
3537 oi "vertical-margin" interpagespace dc.interpagespace;
3538 oz "zoom" zoom dc.zoom;
3539 ob "presentation" presentation dc.presentation;
3540 oi "rotation-angle" c.angle dc.angle;
3541 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3542 ob "proportional-display" c.proportional dc.proportional;
3543 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3544 oi "texcount" c.texcount dc.texcount;
3545 oi "slice-height" c.sliceheight dc.sliceheight;
3546 oi "thumbnail-width" c.thumbw dc.thumbw;
3547 ob "persistent-location" c.jumpback dc.jumpback;
3548 oc "background-color" c.bgcolor dc.bgcolor;
3549 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3550 os "ui-font" c.uifont dc.uifont;
3553 let save () =
3554 let bb = Buffer.create 32768 in
3555 let f (h, dc) =
3556 let dc = if conf.bedefault then conf else dc in
3557 Buffer.add_string bb "<llppconfig>\n<defaults ";
3558 add_attrs bb true dc dc;
3559 Buffer.add_string bb "/>\n";
3561 let adddoc path pan anchor c bookmarks =
3562 if bookmarks == [] && c = dc && anchor = emptyanchor
3563 then ()
3564 else (
3565 Printf.bprintf bb "<doc path='%s'"
3566 (enent path 0 (String.length path));
3568 if anchor <> emptyanchor
3569 then (
3570 let n, y = anchor in
3571 Printf.bprintf bb " page='%d'" n;
3572 if y > 1e-6
3573 then
3574 Printf.bprintf bb " rely='%f'" y
3578 if pan != 0
3579 then Printf.bprintf bb " pan='%d'" pan;
3581 add_attrs bb false dc c;
3583 begin match bookmarks with
3584 | [] -> Buffer.add_string bb "/>\n"
3585 | _ ->
3586 Buffer.add_string bb ">\n<bookmarks>\n";
3587 List.iter (fun (title, _level, (page, rely)) ->
3588 Printf.bprintf bb
3589 "<item title='%s' page='%d'"
3590 (enent title 0 (String.length title))
3591 page
3593 if rely > 1e-6
3594 then
3595 Printf.bprintf bb " rely='%f'" rely
3597 Buffer.add_string bb "/>\n";
3598 ) bookmarks;
3599 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3600 end;
3604 let pan =
3605 match state.mode with
3606 | Birdseye (_, pan, _, _, _) -> pan
3607 | _ -> state.x
3609 let basename = Filename.basename state.path in
3610 adddoc basename pan (getanchor ())
3611 { conf with
3612 autoscrollstep =
3613 if state.ascrollstep > 0
3614 then state.ascrollstep
3615 else conf.autoscrollstep }
3616 (if conf.savebmarks then state.bookmarks else []);
3618 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3619 if basename <> path
3620 then adddoc path x y c bookmarks
3621 ) h;
3622 Buffer.add_string bb "</llppconfig>";
3624 load1 f;
3625 if Buffer.length bb > 0
3626 then
3628 let tmp = path ^ ".tmp" in
3629 let oc = open_out_bin tmp in
3630 Buffer.output_buffer oc bb;
3631 close_out oc;
3632 Sys.rename tmp path;
3633 with exn ->
3634 prerr_endline
3635 ("error while saving configuration: " ^ Printexc.to_string exn)
3637 end;;
3639 let () =
3640 Arg.parse
3641 (Arg.align
3642 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3643 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3644 " Print version and exit")]
3646 (fun s -> state.path <- s)
3647 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3649 if String.length state.path = 0
3650 then (prerr_endline "file name missing"; exit 1);
3652 State.load ();
3654 let _ = Glut.init Sys.argv in
3655 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3656 let () = Glut.initWindowSize conf.winw conf.winh in
3657 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3659 let csock, ssock =
3660 if Sys.os_type = "Unix"
3661 then
3662 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3663 else
3664 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3665 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3666 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3667 Unix.bind sock addr;
3668 Unix.listen sock 1;
3669 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3670 Unix.connect csock addr;
3671 let ssock, _ = Unix.accept sock in
3672 Unix.close sock;
3673 let opts sock =
3674 Unix.setsockopt sock Unix.TCP_NODELAY true;
3675 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3677 opts ssock;
3678 opts csock;
3679 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3680 ssock, csock
3683 let () = Glut.displayFunc display in
3684 let () = Glut.reshapeFunc reshape in
3685 let () = Glut.keyboardFunc keyboard in
3686 let () = Glut.specialFunc special in
3687 let () = Glut.idleFunc (Some idle) in
3688 let () = Glut.mouseFunc mouse in
3689 let () = Glut.motionFunc motion in
3690 let () = Glut.passiveMotionFunc pmotion in
3692 init ssock (
3693 conf.angle, conf.proportional, conf.texcount,
3694 conf.sliceheight, conf.uifont
3696 state.csock <- csock;
3697 state.ssock <- ssock;
3698 state.text <- "Opening " ^ state.path;
3699 writeopen state.path state.password;
3701 at_exit State.save;
3703 let rec handlelablglutbug () =
3705 Glut.mainLoop ();
3706 with Glut.BadEnum "key in special_of_int" ->
3707 showtext '!' " LablGlut bug: special key not recognized";
3708 handlelablglutbug ()
3710 handlelablglutbug ();