The only robust way i can think of that will make persistent-location work
[llpp.git] / main.ml
blobadb5defbe5c6d36ea10246a32f4823a762fc9fa3
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 -> float = "ml_draw_string";;
37 external measurestr : int -> string -> float = "ml_measure_string";;
39 type mpos = int * int
40 and mstate =
41 | Msel of (mpos * mpos)
42 | Mpan of mpos
43 | Mscroll
44 | Mzoom of (int * int)
45 | Mnone
48 type textentry = string * string * onhist option * onkey * ondone
49 and onkey = string -> int -> te
50 and ondone = string -> unit
51 and histcancel = unit -> unit
52 and onhist = ((histcmd -> string) * histcancel)
53 and histcmd = HCnext | HCprev | HCfirst | HClast
54 and te =
55 | TEstop
56 | TEdone of string
57 | TEcont of string
58 | TEswitch of textentry
61 type 'a circbuf =
62 { store : 'a array
63 ; mutable rc : int
64 ; mutable wc : int
65 ; mutable len : int
69 let cbnew n v =
70 { store = Array.create n v
71 ; rc = 0
72 ; wc = 0
73 ; len = 0
77 let cbcap b = Array.length b.store;;
79 let cbput b v =
80 let cap = cbcap b in
81 b.store.(b.wc) <- v;
82 b.wc <- (b.wc + 1) mod cap;
83 b.rc <- b.wc;
84 b.len <- min (b.len + 1) cap;
87 let cbempty b = b.len = 0;;
89 let cbgetg b circular dir =
90 if cbempty b
91 then b.store.(0)
92 else
93 let rc = b.rc + dir in
94 let rc =
95 if circular
96 then (
97 if rc = -1
98 then b.len-1
99 else (
100 if rc = b.len
101 then 0
102 else rc
105 else max 0 (min rc (b.len-1))
107 b.rc <- rc;
108 b.store.(rc);
111 let cbget b = cbgetg b false;;
112 let cbgetc b = cbgetg b true;;
114 let cbpeek b =
115 let rc = b.wc - b.len in
116 let rc = if rc < 0 then cbcap b + rc else rc in
117 b.store.(rc);
120 let cbdecr b = b.len <- b.len - 1;;
122 type layout =
123 { pageno : int
124 ; pagedimno : int
125 ; pagew : int
126 ; pageh : int
127 ; pagedispy : int
128 ; pagey : int
129 ; pagevh : int
130 ; pagex : int
134 type conf =
135 { mutable scrollbw : int
136 ; mutable scrollh : int
137 ; mutable icase : bool
138 ; mutable preload : bool
139 ; mutable pagebias : int
140 ; mutable verbose : bool
141 ; mutable scrollstep : int
142 ; mutable maxhfit : bool
143 ; mutable crophack : bool
144 ; mutable autoscrollstep : int
145 ; mutable showall : bool
146 ; mutable hlinks : bool
147 ; mutable underinfo : bool
148 ; mutable interpagespace : interpagespace
149 ; mutable zoom : float
150 ; mutable presentation : bool
151 ; mutable angle : angle
152 ; mutable winw : int
153 ; mutable winh : int
154 ; mutable savebmarks : bool
155 ; mutable proportional : proportional
156 ; mutable memlimit : int
157 ; mutable texcount : texcount
158 ; mutable sliceheight : sliceheight
159 ; mutable thumbw : width
160 ; mutable jumpback : bool
161 ; mutable bgcolor : float * float * float
162 ; mutable bedefault : bool
163 ; mutable scrollbarinpm : bool
164 ; mutable uifont : string
168 type anchor = pageno * top;;
170 type outline = string * int * anchor
171 and outlines =
172 | Oarray of outline array
173 | Olist of outline list
174 | Onarrow of string * outline array * outline array
177 type rect = float * float * float * float * float * float * float * float;;
179 type pagemapkey = pageno * width * angle * proportional * gen;;
181 let emptyanchor = (0, 0.0);;
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 ; mutable deadline : float
235 ; hists : hists
237 and hists =
238 { pat : string circbuf
239 ; pag : string circbuf
240 ; nav : anchor circbuf
244 let defconf =
245 { scrollbw = 7
246 ; scrollh = 12
247 ; icase = true
248 ; preload = true
249 ; pagebias = 0
250 ; verbose = false
251 ; scrollstep = 24
252 ; maxhfit = true
253 ; crophack = false
254 ; autoscrollstep = 24
255 ; showall = false
256 ; hlinks = false
257 ; underinfo = false
258 ; interpagespace = 2
259 ; zoom = 1.0
260 ; presentation = false
261 ; angle = 0
262 ; winw = 900
263 ; winh = 900
264 ; savebmarks = true
265 ; proportional = true
266 ; memlimit = 32*1024*1024
267 ; texcount = 256
268 ; sliceheight = 24
269 ; thumbw = 76
270 ; jumpback = false
271 ; bgcolor = (0.5, 0.5, 0.5)
272 ; bedefault = false
273 ; scrollbarinpm = true
274 ; uifont = ""
278 let conf = { defconf with angle = defconf.angle };;
280 let makehelp () =
281 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
282 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
285 let state =
286 { csock = Unix.stdin
287 ; ssock = Unix.stdin
288 ; x = 0
289 ; y = 0
290 ; w = 0
291 ; scrollw = 0
292 ; anchor = emptyanchor
293 ; layout = []
294 ; maxy = max_int
295 ; pagemap = Hashtbl.create 10
296 ; pagecache = cbnew 100 ""
297 ; pdims = []
298 ; pagecount = 0
299 ; rendering = false
300 ; mstate = Mnone
301 ; rects = []
302 ; rects1 = []
303 ; text = ""
304 ; mode = View
305 ; fullscreen = None
306 ; searchpattern = ""
307 ; outlines = Olist []
308 ; bookmarks = []
309 ; path = ""
310 ; password = ""
311 ; invalidated = 0
312 ; hists =
313 { nav = cbnew 100 (0, 0.0)
314 ; pat = cbnew 20 ""
315 ; pag = cbnew 10 ""
317 ; colorscale = 1.0
318 ; memused = 0
319 ; gen = 0
320 ; throttle = None
321 ; ascrollstep = 0
322 ; help = makehelp ()
323 ; docinfo = []
324 ; deadline = nan
328 let vlog fmt =
329 if conf.verbose
330 then
331 Printf.kprintf prerr_endline fmt
332 else
333 Printf.kprintf ignore fmt
336 let writecmd fd s =
337 let len = String.length s in
338 let n = 4 + len in
339 let b = Buffer.create n in
340 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
341 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
342 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
343 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
344 Buffer.add_string b s;
345 let s' = Buffer.contents b in
346 let n' = Unix.write fd s' 0 n in
347 if n' != n then failwith "write failed";
350 let readcmd fd =
351 let s = "xxxx" in
352 let n = Unix.read fd s 0 4 in
353 if n != 4 then failwith "incomplete read(len)";
354 let len = 0
355 lor (Char.code s.[0] lsl 24)
356 lor (Char.code s.[1] lsl 16)
357 lor (Char.code s.[2] lsl 8)
358 lor (Char.code s.[3] lsl 0)
360 let s = String.create len in
361 let n = Unix.read fd s 0 len in
362 if n != len then failwith "incomplete read(data)";
366 let makecmd s l =
367 let b = Buffer.create 10 in
368 Buffer.add_string b s;
369 let rec combine = function
370 | [] -> b
371 | x :: xs ->
372 Buffer.add_char b ' ';
373 let s =
374 match x with
375 | `b b -> if b then "1" else "0"
376 | `s s -> s
377 | `i i -> string_of_int i
378 | `f f -> string_of_float f
379 | `I f -> string_of_int (truncate f)
381 Buffer.add_string b s;
382 combine xs;
384 combine l;
387 let wcmd s l =
388 let cmd = Buffer.contents (makecmd s l) in
389 writecmd state.csock cmd;
392 let calcips h =
393 if conf.presentation
394 then
395 let d = conf.winh - h in
396 max 0 ((d + 1) / 2)
397 else
398 conf.interpagespace
401 let calcheight () =
402 let rec f pn ph pi fh l =
403 match l with
404 | (n, _, h, _) :: rest ->
405 let ips = calcips h in
406 let fh =
407 if conf.presentation
408 then fh+ips
409 else (
410 if isbirdseye state.mode && pn = 0
411 then fh + ips
412 else fh
415 let fh = fh + ((n - pn) * (ph + pi)) in
416 f n h ips fh rest;
418 | [] ->
419 let inc =
420 if conf.presentation || (isbirdseye state.mode && pn = 0)
421 then 0
422 else -pi
424 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
425 max 0 fh
427 let fh = f 0 0 0 0 state.pdims in
431 let getpageyh pageno =
432 let rec f pn ph pi y l =
433 match l with
434 | (n, _, h, _) :: rest ->
435 let ips = calcips h in
436 if n >= pageno
437 then
438 let h = if n = pageno then h else ph in
439 if conf.presentation && n = pageno
440 then
441 y + (pageno - pn) * (ph + pi) + pi, h
442 else
443 y + (pageno - pn) * (ph + pi), h
444 else
445 let y = y + (if conf.presentation then pi else 0) in
446 let y = y + (n - pn) * (ph + pi) in
447 f n h ips y rest
449 | [] ->
450 y + (pageno - pn) * (ph + pi), ph
452 f 0 0 0 0 state.pdims
455 let getpagey pageno = fst (getpageyh pageno);;
457 let layout y sh =
458 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
459 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
460 match pdims with
461 | (pageno', w, h, x) :: rest when pageno' = pageno ->
462 let ips = calcips h in
463 let yinc =
464 if conf.presentation || (isbirdseye state.mode && pageno = 0)
465 then ips
466 else 0
468 (w, h, ips, x), rest, pdimno + 1, yinc
469 | _ ->
470 prev, pdims, pdimno, 0
472 let dy = dy + yinc in
473 let py = py + yinc in
474 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
475 then
476 accu
477 else
478 let vy = y + dy in
479 if py + h <= vy - yinc
480 then
481 let py = py + h + ips in
482 let dy = max 0 (py - y) in
483 f ~pageno:(pageno+1)
484 ~pdimno
485 ~prev:curr
488 ~pdims:rest
489 ~cacheleft
490 ~accu
491 else
492 let pagey = vy - py in
493 let pagevh = h - pagey in
494 let pagevh = min (sh - dy) pagevh in
495 let off = if yinc > 0 then py - vy else 0 in
496 let py = py + h + ips in
497 let e =
498 { pageno = pageno
499 ; pagedimno = pdimno
500 ; pagew = w
501 ; pageh = h
502 ; pagedispy = dy + off
503 ; pagey = pagey + off
504 ; pagevh = pagevh - off
505 ; pagex = x
508 let accu = e :: accu in
509 f ~pageno:(pageno+1)
510 ~pdimno
511 ~prev:curr
513 ~dy:(dy+pagevh+ips)
514 ~pdims:rest
515 ~cacheleft:(cacheleft-1)
516 ~accu
518 if state.invalidated = 0
519 then (
520 let accu =
522 ~pageno:0
523 ~pdimno:~-1
524 ~prev:(0,0,0,0)
525 ~py:0
526 ~dy:0
527 ~pdims:state.pdims
528 ~cacheleft:(cbcap state.pagecache)
529 ~accu:[]
531 List.rev accu
533 else
537 let clamp incr =
538 let y = state.y + incr in
539 let y = max 0 y in
540 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
544 let getopaque pageno =
545 try Some (Hashtbl.find state.pagemap
546 (pageno, state.w, conf.angle, conf.proportional, state.gen))
547 with Not_found -> None
550 let cache pageno opaque =
551 Hashtbl.replace state.pagemap
552 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
555 let validopaque opaque = String.length opaque > 0;;
557 let render l =
558 match getopaque l.pageno with
559 | None when not state.rendering ->
560 state.rendering <- true;
561 cache l.pageno ("", -1);
562 wcmd "render" [`i (l.pageno + 1)
563 ;`i l.pagedimno
564 ;`i l.pagew
565 ;`i l.pageh];
566 | _ -> ()
569 let loadlayout layout =
570 let rec f all = function
571 | l :: ls ->
572 begin match getopaque l.pageno with
573 | None -> render l; f false ls
574 | Some (opaque, _) -> f (all && validopaque opaque) ls
576 | [] -> all
578 f (layout <> []) layout;
581 let findpageforopaque opaque =
582 Hashtbl.fold
583 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
584 state.pagemap None
587 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
589 let preload () =
590 let oktopreload =
591 if conf.preload
592 then
593 let memleft = conf.memlimit - state.memused in
594 if memleft < 0
595 then
596 let opaque = cbpeek state.pagecache in
597 match findpageforopaque opaque with
598 | Some ((n, _, _, _, _), size) ->
599 memleft + size >= 0 && not (pagevisible state.layout n)
600 | None -> false
601 else true
602 else false
604 if oktopreload
605 then
606 let presentation = conf.presentation in
607 let interpagespace = conf.interpagespace in
608 let maxy = state.maxy in
609 conf.presentation <- false;
610 conf.interpagespace <- 0;
611 state.maxy <- calcheight ();
612 let y =
613 match state.layout with
614 | [] -> 0
615 | l :: _ -> getpagey l.pageno + l.pagey
617 let y = if y < conf.winh then 0 else y - conf.winh in
618 let pages = layout y (conf.winh*3) in
619 List.iter render pages;
620 conf.presentation <- presentation;
621 conf.interpagespace <- interpagespace;
622 state.maxy <- maxy;
625 let gotoy y =
626 let y = max 0 y in
627 let y = min state.maxy y in
628 let pages = layout y conf.winh in
629 let ready = loadlayout pages in
630 if conf.showall
631 then (
632 if ready
633 then (
634 state.y <- y;
635 state.layout <- pages;
636 state.throttle <- None;
637 Glut.postRedisplay ();
639 else (
640 state.throttle <- Some pages;
643 else (
644 state.y <- y;
645 state.layout <- pages;
646 state.throttle <- None;
647 Glut.postRedisplay ();
649 begin match state.mode with
650 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
651 if not (pagevisible pages pageno)
652 then (
653 match state.layout with
654 | [] -> ()
655 | l :: _ ->
656 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
658 | _ -> ()
659 end;
660 preload ();
663 let gotoy_and_clear_text y =
664 gotoy y;
665 if not conf.verbose then state.text <- "";
668 let getanchor () =
669 match state.layout with
670 | [] -> emptyanchor
671 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
674 let getanchory (n, top) =
675 let y, h = getpageyh n in
676 y + (truncate (top *. float h));
679 let gotoanchor anchor =
680 gotoy (getanchory anchor);
683 let addnav () =
684 cbput state.hists.nav (getanchor ());
687 let getnav dir =
688 let anchor = cbgetc state.hists.nav dir in
689 getanchory anchor;
692 let gotopage n top =
693 let y, h = getpageyh n in
694 gotoy_and_clear_text (y + (truncate (top *. float h)));
697 let gotopage1 n top =
698 let y = getpagey n in
699 gotoy_and_clear_text (y + top);
702 let invalidate () =
703 state.layout <- [];
704 state.pdims <- [];
705 state.rects <- [];
706 state.rects1 <- [];
707 state.invalidated <- state.invalidated + 1;
710 let scalecolor c =
711 let c = c *. state.colorscale in
712 (c, c, c);
715 let scalecolor2 (r, g, b) =
716 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
719 let represent () =
720 state.maxy <- calcheight ();
721 match state.mode with
722 | Birdseye (_, _, pageno, _, _) ->
723 let y, h = getpageyh pageno in
724 let top = (conf.winh - h) / 2 in
725 gotoy (max 0 (y - top))
726 | _ -> gotoanchor state.anchor
729 let pagematrix () =
730 GlMat.mode `projection;
731 GlMat.load_identity ();
732 GlMat.rotate ~x:1.0 ~angle:180.0 ();
733 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
734 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
735 if state.x != 0
736 then (
737 GlMat.translate ~x:(float state.x) ();
741 let winmatrix () =
742 GlMat.mode `projection;
743 GlMat.load_identity ();
744 GlMat.rotate ~x:1.0 ~angle:180.0 ();
745 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
746 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
749 let reshape =
750 let firsttime = ref true in
751 fun ~w ~h ->
752 if state.invalidated = 0 && not !firsttime
753 then (state.anchor <- getanchor (); firsttime := false);
755 conf.winw <- w;
756 let w = truncate (float w *. conf.zoom) - state.scrollw in
757 let w = max w 2 in
758 state.w <- w;
759 conf.winh <- h;
760 GlMat.mode `modelview;
761 GlMat.load_identity ();
762 GlClear.color (scalecolor 1.0);
763 GlClear.clear [`color];
765 invalidate ();
766 wcmd "geometry" [`i w; `i h];
769 let drawstring size x y s =
770 Gl.enable `blend;
771 Gl.enable `texture_2d;
772 ignore (drawstr size x y s);
773 Gl.disable `blend;
774 Gl.disable `texture_2d;
777 let drawstring1 size x y s =
778 drawstr size x y s;
781 let enttext () =
782 let len = String.length state.text in
783 let drawstring s =
784 GlDraw.color (0.0, 0.0, 0.0);
785 GlDraw.rect
786 (0.0, float (conf.winh - 18))
787 (float (conf.winw - state.scrollw - 1), float conf.winh)
789 GlDraw.color (1.0, 1.0, 1.0);
790 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
792 match state.mode with
793 | Textentry ((prefix, text, _, _, _), _) ->
794 let s =
795 match String.length prefix with
796 | 0 | 1 ->
797 if len > 0
798 then
799 Printf.sprintf "%s%s_ [%s]" prefix text state.text
800 else
801 Printf.sprintf "%s%s_" prefix text
803 | _ ->
804 if len > 0
805 then
806 Printf.sprintf "%s: %s_ [%s]" prefix text state.text
807 else
808 Printf.sprintf "%s: %s_" prefix text
810 drawstring s
812 | _ ->
813 if len > 0 then drawstring state.text
816 let showtext c s =
817 state.text <- Printf.sprintf "%c%s" c s;
818 Glut.postRedisplay ();
821 let act cmd =
822 match cmd.[0] with
823 | 'c' ->
824 state.pdims <- [];
826 | 'D' ->
827 state.rects <- state.rects1;
828 Glut.postRedisplay ()
830 | 'C' ->
831 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
832 state.pagecount <- n;
833 state.invalidated <- state.invalidated - 1;
834 if state.invalidated = 0
835 then represent ()
837 | 't' ->
838 let s = Scanf.sscanf cmd "t %n"
839 (fun n -> String.sub cmd n (String.length cmd - n))
841 Glut.setWindowTitle s
843 | 'T' ->
844 let s = Scanf.sscanf cmd "T %n"
845 (fun n -> String.sub cmd n (String.length cmd - n))
847 if istextentry state.mode
848 then (
849 state.text <- s;
850 showtext ' ' s;
852 else (
853 state.text <- s;
854 Glut.postRedisplay ();
857 | 'V' ->
858 if conf.verbose
859 then
860 let s = Scanf.sscanf cmd "V %n"
861 (fun n -> String.sub cmd n (String.length cmd - n))
863 state.text <- s;
864 showtext ' ' s;
866 | 'F' ->
867 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
868 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
869 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
870 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
872 let y = (getpagey pageno) + truncate y0 in
873 addnav ();
874 gotoy y;
875 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
877 | 'R' ->
878 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
879 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
880 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
881 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
883 state.rects1 <-
884 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
886 | 'r' ->
887 let n, w, _h, r, l, s, p =
888 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
889 (fun n w h r l s p ->
890 (n-1, w, h, r, l != 0, s, p))
893 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
894 state.memused <- state.memused + s;
896 let layout =
897 match state.throttle with
898 | None -> state.layout
899 | Some layout -> layout
902 let rec gc () =
903 if (state.memused <= conf.memlimit) || cbempty state.pagecache
904 then ()
905 else (
906 let evictedopaque = cbpeek state.pagecache in
907 match findpageforopaque evictedopaque with
908 | None -> failwith "bug in gc"
909 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
910 if state.gen != gen || not (pagevisible layout evictedn)
911 then (
912 wcmd "free" [`s evictedopaque];
913 state.memused <- state.memused - evictedsize;
914 Hashtbl.remove state.pagemap k;
915 cbdecr state.pagecache;
916 gc ();
920 gc ();
922 cbput state.pagecache p;
923 state.rendering <- false;
925 begin match state.throttle with
926 | None ->
927 if pagevisible state.layout n
928 then gotoy state.y
929 else (
930 let allvisible = loadlayout state.layout in
931 if allvisible then preload ();
934 | Some layout ->
935 match layout with
936 | [] -> ()
937 | l :: _ ->
938 let y = getpagey l.pageno + l.pagey in
939 gotoy y
942 | 'l' ->
943 let pdim =
944 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
946 state.pdims <- pdim :: state.pdims
948 | 'o' ->
949 let (l, n, t, h, pos) =
950 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
952 let s = String.sub cmd pos (String.length cmd - pos) in
953 let outline = (s, l, (n, float t /. float h)) in
954 let outlines =
955 match state.outlines with
956 | Olist outlines -> Olist (outline :: outlines)
957 | Oarray _ -> Olist [outline]
958 | Onarrow _ -> Olist [outline]
960 state.outlines <- outlines
963 | 'i' ->
964 let s = Scanf.sscanf cmd "i %n"
965 (fun n -> String.sub cmd n (String.length cmd - n))
967 let len = String.length s in
968 let rec fold accu pos =
969 let eolpos =
970 try String.index_from s pos '\n' with Not_found -> len
972 if eolpos = len
973 then List.rev accu
974 else
975 let line = String.sub s pos (eolpos - pos) in
976 fold ((1, line)::accu) (eolpos+1)
978 state.docinfo <- fold state.docinfo 0
980 | _ ->
981 dolog "unknown cmd `%S'" cmd
984 let now = Unix.gettimeofday;;
986 let idle () =
987 if state.deadline == nan then state.deadline <- now ();
988 let rec loop delay =
989 let timeout =
990 if delay > 0.0
991 then max 0.0 (state.deadline -. now ())
992 else 0.0
994 let r, _, _ = Unix.select [state.csock] [] [] timeout in
995 state.deadline <- state.deadline +. delay;
996 begin match r with
997 | [] ->
998 if state.ascrollstep > 0
999 then begin
1000 let y = state.y + state.ascrollstep in
1001 let y = if y >= state.maxy then 0 else y in
1002 gotoy y;
1003 state.text <- "";
1004 end;
1006 | _ ->
1007 let cmd = readcmd state.csock in
1008 act cmd;
1009 loop 0.0
1010 end;
1011 in loop 0.007
1014 let onhist cb =
1015 let rc = cb.rc in
1016 let action = function
1017 | HCprev -> cbget cb ~-1
1018 | HCnext -> cbget cb 1
1019 | HCfirst -> cbget cb ~-(cb.rc)
1020 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1021 and cancel () = cb.rc <- rc
1022 in (action, cancel)
1025 let search pattern forward =
1026 if String.length pattern > 0
1027 then
1028 let pn, py =
1029 match state.layout with
1030 | [] -> 0, 0
1031 | l :: _ ->
1032 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1034 let cmd =
1035 let b = makecmd "search"
1036 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1038 Buffer.add_char b ',';
1039 Buffer.add_string b pattern;
1040 Buffer.add_char b '\000';
1041 Buffer.contents b;
1043 writecmd state.csock cmd;
1046 let intentry text key =
1047 let c = Char.unsafe_chr key in
1048 match c with
1049 | '0' .. '9' ->
1050 let s = "x" in s.[0] <- c;
1051 let text = text ^ s in
1052 TEcont text
1054 | _ ->
1055 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1056 TEcont text
1059 let addchar s c =
1060 let b = Buffer.create (String.length s + 1) in
1061 Buffer.add_string b s;
1062 Buffer.add_char b c;
1063 Buffer.contents b;
1066 let textentry text key =
1067 let c = Char.unsafe_chr key in
1068 match c with
1069 | _ when key >= 32 && key < 127 ->
1070 let text = addchar text c in
1071 TEcont text
1073 | _ ->
1074 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1075 TEcont text
1078 let reinit angle proportional =
1079 conf.angle <- angle;
1080 conf.proportional <- proportional;
1081 invalidate ();
1082 wcmd "reinit" [`i angle; `b proportional];
1085 let setzoom zoom =
1086 let zoom = max 0.01 (min 2.2 zoom) in
1087 if zoom <> conf.zoom
1088 then (
1089 if zoom <= 1.0
1090 then state.x <- 0;
1091 conf.zoom <- zoom;
1092 reshape conf.winw conf.winh;
1093 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1097 let enterbirdseye () =
1098 let zoom = float conf.thumbw /. float conf.winw in
1099 let birdseyepageno =
1100 let cy = conf.winh / 2 in
1101 let fold = function
1102 | [] -> 0
1103 | l :: rest ->
1104 let rec fold best = function
1105 | [] -> best.pageno
1106 | l :: rest ->
1107 let d = cy - (l.pagedispy + l.pagevh/2)
1108 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1109 if abs d < abs dbest
1110 then fold l rest
1111 else best.pageno
1112 in fold l rest
1114 fold state.layout
1116 state.mode <- Birdseye (
1117 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1119 conf.zoom <- zoom;
1120 conf.presentation <- false;
1121 conf.interpagespace <- 10;
1122 conf.hlinks <- false;
1123 state.x <- 0;
1124 state.mstate <- Mnone;
1125 conf.showall <- false;
1126 Glut.setCursor Glut.CURSOR_INHERIT;
1127 if conf.verbose
1128 then
1129 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1130 (100.0*.zoom)
1131 else
1132 state.text <- ""
1134 reshape conf.winw conf.winh;
1137 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1138 state.mode <- View;
1139 conf.zoom <- c.zoom;
1140 conf.presentation <- c.presentation;
1141 conf.interpagespace <- c.interpagespace;
1142 conf.showall <- c.showall;
1143 conf.hlinks <- c.hlinks;
1144 state.x <- leftx;
1145 if conf.verbose
1146 then
1147 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1148 (100.0*.conf.zoom)
1150 reshape conf.winw conf.winh;
1151 state.anchor <- if goback then anchor else (pageno, 0.0);
1154 let togglebirdseye () =
1155 match state.mode with
1156 | Birdseye vals -> leavebirdseye vals true
1157 | View | Outline _ -> enterbirdseye ()
1158 | _ -> ()
1161 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1162 let pageno = max 0 (pageno - 1) in
1163 let rec loop = function
1164 | [] -> gotopage1 pageno 0
1165 | l :: _ when l.pageno = pageno ->
1166 if l.pagedispy >= 0 && l.pagey = 0
1167 then Glut.postRedisplay ()
1168 else gotopage1 pageno 0
1169 | _ :: rest -> loop rest
1171 loop state.layout;
1172 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1175 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1176 let pageno = min (state.pagecount - 1) (pageno + 1) in
1177 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1178 let rec loop = function
1179 | [] ->
1180 let y, h = getpageyh pageno in
1181 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1182 gotoy (clamp dy)
1183 | l :: _ when l.pageno = pageno ->
1184 if l.pagevh != l.pageh
1185 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1186 else Glut.postRedisplay ()
1187 | _ :: rest -> loop rest
1189 loop state.layout
1192 let optentry mode _ key =
1193 let btos b = if b then "on" else "off" in
1194 let c = Char.unsafe_chr key in
1195 match c with
1196 | 's' ->
1197 let ondone s =
1198 try conf.scrollstep <- int_of_string s with exc ->
1199 state.text <- Printf.sprintf "bad integer `%s': %s"
1200 s (Printexc.to_string exc)
1202 TEswitch ("scroll step", "", None, intentry, ondone)
1204 | 'A' ->
1205 let ondone s =
1207 conf.autoscrollstep <- int_of_string s;
1208 if state.ascrollstep > 0
1209 then state.ascrollstep <- conf.autoscrollstep;
1210 with exc ->
1211 state.text <- Printf.sprintf "bad integer `%s': %s"
1212 s (Printexc.to_string exc)
1214 TEswitch ("auto scroll step", "", None, intentry, ondone)
1216 | 'Z' ->
1217 let ondone s =
1219 let zoom = float (int_of_string s) /. 100.0 in
1220 setzoom zoom
1221 with exc ->
1222 state.text <- Printf.sprintf "bad integer `%s': %s"
1223 s (Printexc.to_string exc)
1225 TEswitch ("zoom", "", None, intentry, ondone)
1227 | 't' ->
1228 let ondone s =
1230 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1231 state.text <-
1232 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1233 begin match mode with
1234 | Birdseye beye ->
1235 leavebirdseye beye false;
1236 enterbirdseye ();
1237 | _ -> ();
1239 with exc ->
1240 state.text <- Printf.sprintf "bad integer `%s': %s"
1241 s (Printexc.to_string exc)
1243 TEswitch ("thumbnail width", "", None, intentry, ondone)
1245 | 'R' ->
1246 let ondone s =
1247 match try
1248 Some (int_of_string s)
1249 with exc ->
1250 state.text <- Printf.sprintf "bad integer `%s': %s"
1251 s (Printexc.to_string exc);
1252 None
1253 with
1254 | Some angle -> reinit angle conf.proportional
1255 | None -> ()
1257 TEswitch ("rotation", "", None, intentry, ondone)
1259 | 'i' ->
1260 conf.icase <- not conf.icase;
1261 TEdone ("case insensitive search " ^ (btos conf.icase))
1263 | 'p' ->
1264 conf.preload <- not conf.preload;
1265 gotoy state.y;
1266 TEdone ("preload " ^ (btos conf.preload))
1268 | 'v' ->
1269 conf.verbose <- not conf.verbose;
1270 TEdone ("verbose " ^ (btos conf.verbose))
1272 | 'h' ->
1273 conf.maxhfit <- not conf.maxhfit;
1274 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1275 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1277 | 'c' ->
1278 conf.crophack <- not conf.crophack;
1279 TEdone ("crophack " ^ btos conf.crophack)
1281 | 'a' ->
1282 conf.showall <- not conf.showall;
1283 TEdone ("throttle " ^ btos conf.showall)
1285 | 'f' ->
1286 conf.underinfo <- not conf.underinfo;
1287 TEdone ("underinfo " ^ btos conf.underinfo)
1289 | 'P' ->
1290 conf.savebmarks <- not conf.savebmarks;
1291 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1293 | 'S' ->
1294 let ondone s =
1296 let pageno, py =
1297 match state.layout with
1298 | [] -> 0, 0
1299 | l :: _ ->
1300 l.pageno, l.pagey
1302 conf.interpagespace <- int_of_string s;
1303 state.maxy <- calcheight ();
1304 let y = getpagey pageno in
1305 gotoy (y + py)
1306 with exc ->
1307 state.text <- Printf.sprintf "bad integer `%s': %s"
1308 s (Printexc.to_string exc)
1310 TEswitch ("vertical margin", "", None, intentry, ondone)
1312 | 'l' ->
1313 reinit conf.angle (not conf.proportional);
1314 TEdone ("proprortional display " ^ btos conf.proportional)
1316 | _ ->
1317 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1318 TEstop
1321 let maxoutlinerows () = (conf.winh - 31) / 16;;
1323 let enterselector allowdel outlines errmsg msg =
1324 if Array.length outlines = 0
1325 then (
1326 showtext ' ' errmsg;
1328 else (
1329 state.text <- msg;
1330 Glut.setCursor Glut.CURSOR_INHERIT;
1331 let pageno =
1332 match state.layout with
1333 | [] -> -1
1334 | {pageno=pageno} :: _ -> pageno
1336 let active =
1337 let rec loop n =
1338 if n = Array.length outlines
1339 then 0
1340 else
1341 let (_, _, (outlinepageno, _)) = outlines.(n) in
1342 if outlinepageno >= pageno then n else loop (n+1)
1344 loop 0
1346 state.mode <- Outline
1347 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1348 state.mode);
1349 Glut.postRedisplay ();
1353 let enteroutlinemode () =
1354 let outlines, msg =
1355 match state.outlines with
1356 | Oarray a -> a, ""
1357 | Olist l ->
1358 let a = Array.of_list (List.rev l) in
1359 state.outlines <- Oarray a;
1360 a, ""
1361 | Onarrow (pat, a, _) ->
1362 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1364 enterselector false outlines "Document has no outline" msg;
1367 let enterbookmarkmode () =
1368 let bookmarks = Array.of_list state.bookmarks in
1369 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1372 let mode_to_string mode =
1373 let b = Buffer.create 10 in
1374 let rec f = function
1375 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1376 | View -> Buffer.add_string b "View"
1377 | Birdseye _ -> Buffer.add_string b "Birdseye"
1378 | Items _ -> Buffer.add_string b "Items"
1379 | Outline _ -> Buffer.add_string b "Outline"
1381 f mode;
1382 Buffer.contents b;
1385 let color_of_string s =
1386 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1387 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1391 let color_to_string (r, g, b) =
1392 let r = truncate (r *. 256.0)
1393 and g = truncate (g *. 256.0)
1394 and b = truncate (b *. 256.0) in
1395 Printf.sprintf "%d/%d/%d" r g b
1398 let enterinfomode () =
1399 let btos = function true -> "on" | _ -> "off" in
1400 let mode = state.mode in
1401 let rec makeitems () =
1402 let intp name get set =
1403 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1404 fun active first _ pan ->
1405 let ondone s =
1406 let n =
1407 try int_of_string s
1408 with exn ->
1409 state.text <- Printf.sprintf "bad integer `%s': %s"
1410 s (Printexc.to_string exn);
1411 max_int;
1413 if n != max_int then set n;
1415 let te = name, "", None, intentry, ondone in
1416 state.text <- "";
1417 Textentry (
1419 fun _ ->
1420 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1423 and boolp name get set =
1424 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1425 fun active first qsearch pan ->
1426 let v = get () in
1427 set (not v);
1428 Items (active, first, makeitems (), qsearch, pan, mode);
1430 and colorp name get set =
1431 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1432 fun active first _ pan ->
1433 let invalid = (nan, nan, nan) in
1434 let ondone s =
1435 let c =
1436 try color_of_string s
1437 with exn ->
1438 state.text <- Printf.sprintf "bad color `%s': %s"
1439 s (Printexc.to_string exn);
1440 invalid
1442 if c <> invalid
1443 then set c;
1445 let te = name, "", None, textentry, ondone in
1446 state.text <- "";
1447 Textentry (
1449 fun _ ->
1450 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1455 let birdseye = isbirdseye mode in
1456 let items = [
1457 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1459 boolp "presentation"
1460 (fun () -> conf.presentation)
1461 (fun v ->
1462 conf.presentation <- v;
1463 state.anchor <- getanchor ();
1464 represent ());
1466 boolp "ignore case in searches"
1467 (fun () -> conf.icase)
1468 (fun v -> conf.icase <- v);
1470 boolp "preload"
1471 (fun () -> conf.preload)
1472 (fun v -> conf.preload <- v);
1474 boolp "verbose"
1475 (fun () -> conf.verbose)
1476 (fun v -> conf.verbose <- v);
1478 boolp "max fit"
1479 (fun () -> conf.maxhfit)
1480 (fun v -> conf.maxhfit <- v);
1482 boolp "crop hack"
1483 (fun () -> conf.crophack)
1484 (fun v -> conf.crophack <- v);
1486 boolp "throttle"
1487 (fun () -> conf.showall)
1488 (fun v -> conf.showall <- v);
1490 boolp "highlight links"
1491 (fun () -> conf.hlinks)
1492 (fun v -> conf.hlinks <- v);
1494 boolp "under info"
1495 (fun () -> conf.underinfo)
1496 (fun v -> conf.underinfo <- v);
1497 boolp "persistent bookmarks"
1498 (fun () -> conf.savebmarks)
1499 (fun v -> conf.savebmarks <- v);
1501 boolp "proportional display"
1502 (fun () -> conf.proportional)
1503 (fun v -> reinit conf.angle v);
1505 boolp "persistent location"
1506 (fun () -> conf.jumpback)
1507 (fun v -> conf.jumpback <- v);
1509 "", 0, Noaction;
1511 intp "vertical margin"
1512 (fun () -> conf.interpagespace)
1513 (fun n ->
1514 conf.interpagespace <- n;
1515 let pageno, py =
1516 match state.layout with
1517 | [] -> 0, 0
1518 | l :: _ ->
1519 l.pageno, l.pagey
1521 state.maxy <- calcheight ();
1522 let y = getpagey pageno in
1523 gotoy (y + py)
1526 intp "page bias"
1527 (fun () -> conf.pagebias)
1528 (fun v -> conf.pagebias <- v);
1530 intp "scroll step"
1531 (fun () -> conf.scrollstep)
1532 (fun n -> conf.scrollstep <- n);
1534 intp "auto scroll step"
1535 (fun () ->
1536 if state.ascrollstep > 0
1537 then state.ascrollstep
1538 else conf.autoscrollstep)
1539 (fun n ->
1540 if state.ascrollstep > 0
1541 then state.ascrollstep <- n
1542 else conf.autoscrollstep <- n);
1544 intp "zoom"
1545 (fun () -> truncate (conf.zoom *. 100.))
1546 (fun v -> setzoom ((float v) /. 100.));
1548 intp "rotation"
1549 (fun () -> conf.angle)
1550 (fun v -> reinit v conf.proportional);
1552 intp "scroll bar width"
1553 (fun () -> state.scrollw)
1554 (fun v ->
1555 state.scrollw <- v;
1556 conf.scrollbw <- v;
1557 reshape conf.winw conf.winh;
1560 intp "scroll handle height"
1561 (fun () -> conf.scrollh)
1562 (fun v -> conf.scrollh <- v;);
1564 intp "thumbnail width"
1565 (fun () -> conf.thumbw)
1566 (fun v ->
1567 conf.thumbw <- min 1920 v;
1568 match mode with
1569 | Birdseye beye ->
1570 leavebirdseye beye false;
1571 enterbirdseye ()
1572 | _ -> ()
1575 colorp "background color"
1576 (fun () -> conf.bgcolor)
1577 (fun v -> conf.bgcolor <- v);
1579 "", 0, Noaction;
1580 "Presentation mode", 0, Noaction;
1582 boolp "scrollbar visible"
1583 (fun () -> conf.scrollbarinpm)
1584 (fun v ->
1585 if v != conf.scrollbarinpm
1586 then (
1587 conf.scrollbarinpm <- v;
1588 if conf.presentation
1589 then (
1590 state.scrollw <- if v then conf.scrollbw else 0;
1591 reshape conf.winw conf.winh;
1596 "", 0, Noaction;
1597 "Pixmap Cache", 0, Noaction;
1599 intp "size (advisory)"
1600 (fun () -> conf.memlimit)
1601 (fun v -> conf.memlimit <- v);
1602 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1606 let tailer =
1607 let trailer =
1608 ("", 0, Noaction)
1609 :: ("Document", 0, Noaction)
1610 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1612 if birdseye
1613 then trailer
1614 else (
1615 ("", 0, Noaction)
1616 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1617 (btos conf.bedefault),
1619 Action (
1620 fun active first qsearch pan ->
1621 conf.bedefault <- not conf.bedefault;
1622 Items (active, first, makeitems (), qsearch, pan, mode);
1624 ) :: trailer
1627 Array.of_list (items @ tailer)
1629 state.text <- "";
1630 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1631 Glut.postRedisplay ();
1634 let enterhelpmode () =
1635 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1636 Glut.postRedisplay ();
1639 let quickbookmark ?title () =
1640 match state.layout with
1641 | [] -> ()
1642 | l :: _ ->
1643 let title =
1644 match title with
1645 | None ->
1646 let sec = Unix.gettimeofday () in
1647 let tm = Unix.localtime sec in
1648 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1649 (l.pageno+1)
1650 tm.Unix.tm_mday
1651 tm.Unix.tm_mon
1652 (tm.Unix.tm_year + 1900)
1653 tm.Unix.tm_hour
1654 tm.Unix.tm_min
1655 | Some title -> title
1657 state.bookmarks <-
1658 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1659 :: state.bookmarks
1662 let doreshape w h =
1663 state.fullscreen <- None;
1664 Glut.reshapeWindow w h;
1667 let writeopen path password =
1668 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1669 writecmd state.csock "info";
1672 let opendoc path password =
1673 invalidate ();
1674 state.path <- path;
1675 state.password <- password;
1676 state.gen <- state.gen + 1;
1678 writeopen path password;
1679 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1680 wcmd "geometry" [`i state.w; `i conf.winh];
1683 let viewkeyboard key =
1684 let enttext te =
1685 let mode = state.mode in
1686 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1687 state.text <- "";
1688 enttext ();
1689 Glut.postRedisplay ()
1691 let c = Char.chr key in
1692 match c with
1693 | '\027' | 'q' -> (* escape *)
1694 exit 0
1696 | '\008' -> (* backspace *)
1697 let y = getnav ~-1 in
1698 gotoy_and_clear_text y
1700 | 'o' ->
1701 enteroutlinemode ()
1703 | 'u' ->
1704 state.rects <- [];
1705 state.text <- "";
1706 Glut.postRedisplay ()
1708 | '/' | '?' ->
1709 let ondone isforw s =
1710 cbput state.hists.pat s;
1711 state.searchpattern <- s;
1712 search s isforw
1714 let s = String.create 1 in
1715 s.[0] <- c;
1716 enttext (s, "", Some (onhist state.hists.pat),
1717 textentry, ondone (c ='/'))
1719 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1720 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1721 setzoom (min 2.2 (conf.zoom +. incr))
1723 | '+' ->
1724 let ondone s =
1725 let n =
1726 try int_of_string s with exc ->
1727 state.text <- Printf.sprintf "bad integer `%s': %s"
1728 s (Printexc.to_string exc);
1729 max_int
1731 if n != max_int
1732 then (
1733 conf.pagebias <- n;
1734 state.text <- "page bias is now " ^ string_of_int n;
1737 enttext ("page bias", "", None, intentry, ondone)
1739 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1740 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1741 setzoom (max 0.01 (conf.zoom -. decr))
1743 | '-' ->
1744 let ondone msg = state.text <- msg in
1745 enttext (
1746 "option [acfhilpstvAPRSZ]", "", None,
1747 optentry state.mode, ondone
1750 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1751 setzoom 1.0
1753 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1754 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1755 if zoom < 1.0
1756 then setzoom zoom
1758 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1759 togglebirdseye ()
1761 | '0' .. '9' ->
1762 let ondone s =
1763 let n =
1764 try int_of_string s with exc ->
1765 state.text <- Printf.sprintf "bad integer `%s': %s"
1766 s (Printexc.to_string exc);
1769 if n >= 0
1770 then (
1771 addnav ();
1772 cbput state.hists.pag (string_of_int n);
1773 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1776 let pageentry text key =
1777 match Char.unsafe_chr key with
1778 | 'g' -> TEdone text
1779 | _ -> intentry text key
1781 let text = "x" in text.[0] <- c;
1782 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1784 | 'b' ->
1785 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1786 reshape conf.winw conf.winh;
1788 | 'l' ->
1789 conf.hlinks <- not conf.hlinks;
1790 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1791 Glut.postRedisplay ()
1793 | 'a' ->
1794 if state.ascrollstep = 0
1795 then state.ascrollstep <- conf.autoscrollstep
1796 else (
1797 conf.autoscrollstep <- state.ascrollstep;
1798 state.ascrollstep <- 0;
1801 | 'P' ->
1802 conf.presentation <- not conf.presentation;
1803 if conf.presentation
1804 then (
1805 if not conf.scrollbarinpm
1806 then state.scrollw <- 0;
1808 else
1809 state.scrollw <- conf.scrollbw;
1811 showtext ' ' ("presentation mode " ^
1812 if conf.presentation then "on" else "off");
1813 state.anchor <- getanchor ();
1814 represent ()
1816 | 'f' ->
1817 begin match state.fullscreen with
1818 | None ->
1819 state.fullscreen <- Some (conf.winw, conf.winh);
1820 Glut.fullScreen ()
1821 | Some (w, h) ->
1822 state.fullscreen <- None;
1823 doreshape w h
1826 | 'g' ->
1827 gotoy_and_clear_text 0
1829 | 'n' ->
1830 search state.searchpattern true
1832 | 'p' | 'N' ->
1833 search state.searchpattern false
1835 | 't' ->
1836 begin match state.layout with
1837 | [] -> ()
1838 | l :: _ ->
1839 gotoy_and_clear_text (getpagey l.pageno)
1842 | ' ' ->
1843 begin match List.rev state.layout with
1844 | [] -> ()
1845 | l :: _ ->
1846 let pageno = min (l.pageno+1) (state.pagecount-1) in
1847 gotoy_and_clear_text (getpagey pageno)
1850 | '\127' -> (* del *)
1851 begin match state.layout with
1852 | [] -> ()
1853 | l :: _ ->
1854 let pageno = max 0 (l.pageno-1) in
1855 gotoy_and_clear_text (getpagey pageno)
1858 | '=' ->
1859 let f (fn, _) l =
1860 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1862 let fn, ln = List.fold_left f (-1, -1) state.layout in
1863 let s =
1864 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1865 let percent =
1866 if maxy <= 0
1867 then 100.
1868 else (100. *. (float state.y /. float maxy)) in
1869 if fn = ln
1870 then
1871 Printf.sprintf "Page %d of %d %.2f%%"
1872 (fn+1) state.pagecount percent
1873 else
1874 Printf.sprintf
1875 "Pages %d-%d of %d %.2f%%"
1876 (fn+1) (ln+1) state.pagecount percent
1878 showtext ' ' s;
1880 | 'w' ->
1881 begin match state.layout with
1882 | [] -> ()
1883 | l :: _ ->
1884 doreshape (l.pagew + state.scrollw) l.pageh;
1885 Glut.postRedisplay ();
1888 | '\'' ->
1889 enterbookmarkmode ()
1891 | 'h' ->
1892 enterhelpmode ()
1894 | 'i' ->
1895 enterinfomode ()
1897 | 'm' ->
1898 let ondone s =
1899 match state.layout with
1900 | l :: _ ->
1901 state.bookmarks <-
1902 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1903 :: state.bookmarks
1904 | _ -> ()
1906 enttext ("bookmark", "", None, textentry, ondone)
1908 | '~' ->
1909 quickbookmark ();
1910 showtext ' ' "Quick bookmark added";
1912 | 'z' ->
1913 begin match state.layout with
1914 | l :: _ ->
1915 let rect = getpdimrect l.pagedimno in
1916 let w, h =
1917 if conf.crophack
1918 then
1919 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1920 truncate (1.2 *. (rect.(3) -. rect.(0))))
1921 else
1922 (truncate (rect.(1) -. rect.(0)),
1923 truncate (rect.(3) -. rect.(0)))
1925 if w != 0 && h != 0
1926 then
1927 doreshape (w + state.scrollw) (h + conf.interpagespace)
1929 Glut.postRedisplay ();
1931 | [] -> ()
1934 | '<' | '>' ->
1935 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1937 | '[' | ']' ->
1938 state.colorscale <-
1939 max 0.0
1940 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1941 Glut.postRedisplay ()
1943 | 'k' ->
1944 begin match state.mode with
1945 | Birdseye beye -> upbirdseye beye
1946 | _ -> gotoy (clamp (-conf.scrollstep))
1949 | 'j' ->
1950 begin match state.mode with
1951 | Birdseye beye -> downbirdseye beye
1952 | _ -> gotoy (clamp conf.scrollstep)
1955 | 'r' ->
1956 state.anchor <- getanchor ();
1957 opendoc state.path state.password
1959 | _ ->
1960 vlog "huh? %d %c" key (Char.chr key);
1963 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1964 let enttext te =
1965 state.mode <- Textentry (te, onleave);
1966 state.text <- "";
1967 enttext ();
1968 Glut.postRedisplay ()
1970 match Char.unsafe_chr key with
1971 | '\008' -> (* backspace *)
1972 let len = String.length text in
1973 if len = 0
1974 then (
1975 onleave Cancel;
1976 Glut.postRedisplay ();
1978 else (
1979 let s = String.sub text 0 (len - 1) in
1980 enttext (c, s, opthist, onkey, ondone)
1983 | '\r' | '\n' ->
1984 ondone text;
1985 onleave Confirm;
1986 Glut.postRedisplay ()
1988 | '\027' -> (* escape *)
1989 begin match opthist with
1990 | None -> ()
1991 | Some (_, onhistcancel) -> onhistcancel ()
1992 end;
1993 onleave Cancel;
1994 Glut.postRedisplay ()
1996 | _ ->
1997 begin match onkey text key with
1998 | TEdone text ->
1999 onleave Confirm;
2000 ondone text;
2001 Glut.postRedisplay ()
2003 | TEcont text ->
2004 enttext (c, text, opthist, onkey, ondone);
2006 | TEstop ->
2007 onleave Cancel;
2008 Glut.postRedisplay ()
2010 | TEswitch te ->
2011 state.mode <- Textentry (te, onleave);
2012 Glut.postRedisplay ()
2013 end;
2016 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2017 match key with
2018 | 27 -> (* escape *)
2019 leavebirdseye beye true
2021 | 12 -> (* ctrl-l *)
2022 let y, h = getpageyh pageno in
2023 let top = (conf.winh - h) / 2 in
2024 gotoy (max 0 (y - top))
2026 | 13 -> (* enter *)
2027 leavebirdseye beye false
2029 | _ ->
2030 viewkeyboard key
2033 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2034 let set active first qsearch =
2035 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2037 let search active pattern incr =
2038 let dosearch re =
2039 let rec loop n =
2040 if n >= Array.length items || n < 0
2041 then None
2042 else
2043 let (s, _, _) = items.(n) in
2045 (try ignore (Str.search_forward re s 0); true
2046 with Not_found -> false)
2047 then Some n
2048 else loop (n + incr)
2050 loop active
2053 let re = Str.regexp_case_fold pattern in
2054 dosearch re
2055 with Failure s ->
2056 state.text <- s;
2057 None
2059 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2060 match key with
2061 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2062 let incr = if key = 18 then -1 else 1 in
2063 let active, first =
2064 match search (active + incr) qsearch incr with
2065 | None ->
2066 state.text <- qsearch ^ " [not found]";
2067 active, first
2068 | Some active ->
2069 state.text <- qsearch;
2070 active, firstof active
2072 set active first qsearch;
2073 Glut.postRedisplay ();
2075 | 8 -> (* backspace *)
2076 let len = String.length qsearch in
2077 if len = 0
2078 then ()
2079 else (
2080 if len = 1
2081 then (
2082 state.text <- "";
2083 set active first "";
2085 else
2086 let qsearch = String.sub qsearch 0 (len - 1) in
2087 let active, first =
2088 match search active qsearch ~-1 with
2089 | None ->
2090 state.text <- qsearch ^ " [not found]";
2091 active, first
2092 | Some active ->
2093 state.text <- qsearch;
2094 active, firstof active
2096 set active first qsearch
2098 Glut.postRedisplay ()
2100 | _ when key >= 32 && key < 127 ->
2101 let pattern = addchar qsearch (Char.chr key) in
2102 let active, first =
2103 match search active pattern 1 with
2104 | None ->
2105 state.text <- pattern ^ " [not found]";
2106 active, first
2107 | Some active ->
2108 state.text <- pattern;
2109 active, firstof active
2111 set active first pattern;
2112 Glut.postRedisplay ()
2114 | 27 -> (* escape *)
2115 state.text <- "";
2116 if String.length qsearch = 0
2117 then (
2118 state.mode <- oldmode;
2120 else (
2121 set active first "";
2123 Glut.postRedisplay ()
2125 | 13 -> (* enter *)
2126 if active >= 0 && active < Array.length items
2127 then (
2128 match items.(active) with
2129 | _, _, Action f ->
2130 state.mode <- f active first qsearch pan
2132 | _, _, Noaction ->
2133 state.text <- "";
2134 state.mode <- oldmode
2136 else (
2137 state.text <- "";
2138 state.mode <- oldmode
2140 Glut.postRedisplay ();
2142 | _ -> dolog "unknown key %d" key
2145 let outlinekeyboard key
2146 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2147 let narrow outlines pattern =
2148 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2149 match reopt with
2150 | None -> None
2151 | Some re ->
2152 let rec fold accu n =
2153 if n = -1
2154 then accu
2155 else
2156 let (s, _, _) as o = outlines.(n) in
2157 let accu =
2158 if (try ignore (Str.search_forward re s 0); true
2159 with Not_found -> false)
2160 then (o :: accu)
2161 else accu
2163 fold accu (n-1)
2165 let matched = fold [] (Array.length outlines - 1) in
2166 if matched = [] then None else Some (Array.of_list matched)
2168 let search active pattern incr =
2169 let dosearch re =
2170 let rec loop n =
2171 if n = Array.length outlines || n = -1
2172 then None
2173 else
2174 let (s, _, _) = outlines.(n) in
2176 (try ignore (Str.search_forward re s 0); true
2177 with Not_found -> false)
2178 then Some n
2179 else loop (n + incr)
2181 loop active
2184 let re = Str.regexp_case_fold pattern in
2185 dosearch re
2186 with Failure s ->
2187 state.text <- s;
2188 None
2190 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2191 match key with
2192 | 27 -> (* escape *)
2193 state.text <- "";
2194 if String.length qsearch = 0
2195 then (
2196 state.mode <- oldmode;
2198 else (
2199 state.mode <- Outline (
2200 allowdel, active, first, outlines, "", pan, oldmode
2203 Glut.postRedisplay ();
2205 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2206 let incr = if key = 18 then -1 else 1 in
2207 let active, first =
2208 match search (active + incr) qsearch incr with
2209 | None ->
2210 state.text <- qsearch ^ " [not found]";
2211 active, first
2212 | Some active ->
2213 state.text <- qsearch;
2214 active, firstof active
2216 state.mode <- Outline (
2217 allowdel, active, first, outlines, qsearch, pan, oldmode
2219 Glut.postRedisplay ();
2221 | 8 -> (* backspace *)
2222 let len = String.length qsearch in
2223 if len = 0
2224 then ()
2225 else (
2226 if len = 1
2227 then (
2228 state.text <- "";
2229 state.mode <- Outline (
2230 allowdel, active, first, outlines, "", pan, oldmode
2233 else
2234 let qsearch = String.sub qsearch 0 (len - 1) in
2235 let active, first =
2236 match search active qsearch ~-1 with
2237 | None ->
2238 state.text <- qsearch ^ " [not found]";
2239 active, first
2240 | Some active ->
2241 state.text <- qsearch;
2242 active, firstof active
2244 state.mode <- Outline (
2245 allowdel, active, first, outlines, qsearch, pan, oldmode
2248 Glut.postRedisplay ()
2250 | 13 -> (* enter *)
2251 if active < Array.length outlines
2252 then (
2253 let (_, _, anchor) = outlines.(active) in
2254 addnav ();
2255 gotoanchor anchor;
2257 state.text <- "";
2258 if allowdel then state.bookmarks <- Array.to_list outlines;
2259 state.mode <- oldmode;
2260 Glut.postRedisplay ();
2262 | _ when key >= 32 && key < 127 ->
2263 let pattern = addchar qsearch (Char.chr key) in
2264 let active, first =
2265 match search active pattern 1 with
2266 | None ->
2267 state.text <- pattern ^ " [not found]";
2268 active, first
2269 | Some active ->
2270 state.text <- pattern;
2271 active, firstof active
2273 state.mode <- Outline (
2274 allowdel, active, first, outlines, pattern, pan, oldmode
2276 Glut.postRedisplay ()
2278 | 14 when not allowdel -> (* ctrl-n *)
2279 if String.length qsearch > 0
2280 then (
2281 let optoutlines = narrow outlines qsearch in
2282 begin match optoutlines with
2283 | None -> state.text <- "can't narrow"
2284 | Some outlines ->
2285 state.mode <- Outline (
2286 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2288 match state.outlines with
2289 | Olist _ -> ()
2290 | Oarray a ->
2291 state.outlines <- Onarrow (qsearch, outlines, a)
2292 | Onarrow (_, _, b) ->
2293 state.outlines <- Onarrow (qsearch, outlines, b)
2294 end;
2296 Glut.postRedisplay ()
2298 | 21 when not allowdel -> (* ctrl-u *)
2299 let outline =
2300 match state.outlines with
2301 | Oarray a -> a
2302 | Olist l ->
2303 let a = Array.of_list (List.rev l) in
2304 state.outlines <- Oarray a;
2306 | Onarrow (_, _, b) ->
2307 state.outlines <- Oarray b;
2308 state.text <- "";
2311 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2312 Glut.postRedisplay ()
2314 | 12 -> (* ctrl-l *)
2315 state.mode <- Outline
2316 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2317 Glut.postRedisplay ()
2319 | 127 when allowdel -> (* delete *)
2320 let len = Array.length outlines - 1 in
2321 if len = 0
2322 then (
2323 state.mode <- View;
2324 state.bookmarks <- [];
2326 else (
2327 let bookmarks = Array.init len
2328 (fun i ->
2329 let i = if i >= active then i + 1 else i in
2330 outlines.(i)
2333 state.mode <-
2334 Outline (
2335 allowdel,
2336 min active (len-1),
2337 min first (len-1),
2338 bookmarks, qsearch,
2340 oldmode
2343 Glut.postRedisplay ()
2345 | _ -> dolog "unknown key %d" key
2348 let keyboard ~key ~x ~y =
2349 ignore x;
2350 ignore y;
2351 if key = 7 (* ctrl-g *)
2352 then
2353 wcmd "interrupt" []
2354 else
2355 match state.mode with
2356 | Outline outline -> outlinekeyboard key outline
2357 | Textentry textentry -> textentrykeyboard key textentry
2358 | Birdseye birdseye -> birdseyekeyboard key birdseye
2359 | View -> viewkeyboard key
2360 | Items items -> itemskeyboard key items
2363 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2364 match key with
2365 | Glut.KEY_UP -> upbirdseye beye
2366 | Glut.KEY_DOWN -> downbirdseye beye
2368 | Glut.KEY_PAGE_UP ->
2369 begin match state.layout with
2370 | l :: _ ->
2371 if l.pagey != 0
2372 then (
2373 state.mode <- Birdseye (
2374 conf, leftx, l.pageno, hooverpageno, anchor
2376 gotopage1 l.pageno 0;
2378 else (
2379 let layout = layout (state.y-conf.winh) conf.winh in
2380 match layout with
2381 | [] -> gotoy (clamp (-conf.winh))
2382 | l :: _ ->
2383 state.mode <- Birdseye (
2384 conf, leftx, l.pageno, hooverpageno, anchor
2386 gotopage1 l.pageno 0
2389 | [] -> gotoy (clamp (-conf.winh))
2390 end;
2392 | Glut.KEY_PAGE_DOWN ->
2393 begin match List.rev state.layout with
2394 | l :: _ ->
2395 let layout = layout (state.y + conf.winh) conf.winh in
2396 begin match layout with
2397 | [] ->
2398 let incr = l.pageh - l.pagevh in
2399 if incr = 0
2400 then (
2401 state.mode <-
2402 Birdseye (
2403 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2405 Glut.postRedisplay ();
2407 else gotoy (clamp (incr + conf.interpagespace*2));
2409 | l :: _ ->
2410 state.mode <-
2411 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2412 gotopage1 l.pageno 0;
2415 | [] -> gotoy (clamp conf.winh)
2416 end;
2418 | Glut.KEY_HOME ->
2419 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2420 gotopage1 0 0
2422 | Glut.KEY_END ->
2423 let pageno = state.pagecount - 1 in
2424 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2425 if not (pagevisible state.layout pageno)
2426 then
2427 let h =
2428 match List.rev state.pdims with
2429 | [] -> conf.winh
2430 | (_, _, h, _) :: _ -> h
2432 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2433 else Glut.postRedisplay ();
2434 | _ -> ()
2437 let setautoscrollspeed goingdown =
2438 let incr = max 1 (state.ascrollstep / 2) in
2439 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2440 state.ascrollstep <- astep;
2443 let special ~key ~x ~y =
2444 ignore x;
2445 ignore y;
2446 match state.mode with
2447 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2448 togglebirdseye ()
2450 | Birdseye vals ->
2451 birdseyespecial key vals
2453 | View when key = Glut.KEY_F1 ->
2454 enterhelpmode ()
2456 | View ->
2457 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2458 then setautoscrollspeed (key = Glut.KEY_DOWN)
2459 else
2460 let y =
2461 match key with
2462 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2463 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2464 | Glut.KEY_DOWN -> clamp conf.scrollstep
2465 | Glut.KEY_PAGE_UP ->
2466 if Glut.getModifiers () land Glut.active_ctrl != 0
2467 then
2468 match state.layout with
2469 | [] -> state.y
2470 | l :: _ -> state.y - l.pagey
2471 else
2472 clamp (-conf.winh)
2473 | Glut.KEY_PAGE_DOWN ->
2474 if Glut.getModifiers () land Glut.active_ctrl != 0
2475 then
2476 match List.rev state.layout with
2477 | [] -> state.y
2478 | l :: _ -> getpagey l.pageno
2479 else
2480 clamp conf.winh
2481 | Glut.KEY_HOME -> addnav (); 0
2482 | Glut.KEY_END ->
2483 addnav ();
2484 state.maxy - (if conf.maxhfit then conf.winh else 0)
2486 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2487 Glut.getModifiers () land Glut.active_alt != 0 ->
2488 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2490 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2491 state.x <- state.x - 10;
2492 state.y
2493 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2494 state.x <- state.x + 10;
2495 state.y
2497 | _ -> state.y
2499 gotoy_and_clear_text y
2501 | Textentry
2502 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2503 let s =
2504 match key with
2505 | Glut.KEY_UP -> action HCprev
2506 | Glut.KEY_DOWN -> action HCnext
2507 | Glut.KEY_HOME -> action HCfirst
2508 | Glut.KEY_END -> action HClast
2509 | _ -> state.text
2511 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2512 Glut.postRedisplay ()
2514 | Textentry _ -> ()
2516 | Items (active, first, items, qsearch, pan, oldmode) ->
2517 let maxrows = maxoutlinerows () in
2518 let itemcount = Array.length items in
2519 let hasaction = function
2520 | (_, _, Noaction) -> false
2521 | _ -> true
2523 let find start incr =
2524 let rec find i =
2525 if i = -1 || i = itemcount
2526 then -1
2527 else (
2528 if hasaction items.(i)
2529 then i
2530 else find (i + incr)
2533 find start
2535 let set active first =
2536 let first = max 0 (min first (itemcount - maxrows)) in
2537 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2539 let navigate incr =
2540 let isvisible first n = n >= first && n - first <= maxrows in
2541 let active, first =
2542 let incr1 = if incr > 0 then 1 else -1 in
2543 if isvisible first active
2544 then
2545 let next =
2546 let next = active + incr in
2547 let next =
2548 if next < 0 || next >= itemcount
2549 then -1
2550 else find next incr1
2552 if next = -1 || abs (active - next) > maxrows
2553 then -1
2554 else next
2556 if next = -1
2557 then
2558 let first = first + incr in
2559 let first = max 0 (min first (itemcount - 1)) in
2560 let next =
2561 let next = active + incr in
2562 let next = max 0 (min next (itemcount - 1)) in
2563 find next ~-incr1
2565 let active = if next = -1 then active else next in
2566 active, first
2567 else
2568 let first = min next first in
2569 next, first
2570 else
2571 let first = first + incr in
2572 let first = max 0 (min first (itemcount - 1)) in
2573 let active =
2574 let next = active + incr in
2575 let next = max 0 (min next (itemcount - 1)) in
2576 let next = find next incr1 in
2577 if next = -1 || abs (active - first) > maxrows
2578 then active
2579 else next
2581 active, first
2583 set active first;
2584 Glut.postRedisplay ()
2586 begin match key with
2587 | Glut.KEY_UP -> navigate ~-1
2588 | Glut.KEY_DOWN -> navigate 1
2589 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2590 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2592 | Glut.KEY_RIGHT ->
2593 state.mode <- Items (
2594 active, first, items, qsearch, min 0 (pan - 1), oldmode
2596 Glut.postRedisplay ()
2598 | Glut.KEY_LEFT ->
2599 state.mode <- Items (
2600 active, first, items, qsearch, min 0 (pan + 1), oldmode
2602 Glut.postRedisplay ()
2604 | Glut.KEY_HOME ->
2605 let active = find 0 1 in
2606 set active 0;
2607 Glut.postRedisplay ()
2609 | Glut.KEY_END ->
2610 let first = max 0 (itemcount - maxrows) in
2611 let active = find (itemcount - 1) ~-1 in
2612 set active first;
2613 Glut.postRedisplay ()
2615 | _ -> ()
2616 end;
2618 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2619 let maxrows = maxoutlinerows () in
2620 let calcfirst first active =
2621 if active > first
2622 then
2623 let rows = active - first in
2624 if rows > maxrows then active - maxrows else first
2625 else active
2627 let navigate incr =
2628 let active = active + incr in
2629 let active = max 0 (min active (Array.length outlines - 1)) in
2630 let first = calcfirst first active in
2631 state.mode <- Outline (
2632 allowdel, active, first, outlines, qsearch, pan, oldmode
2634 Glut.postRedisplay ()
2636 let updownlevel incr =
2637 let len = Array.length outlines in
2638 let (_, curlevel, _) = outlines.(active) in
2639 let rec flow i =
2640 if i = len then i-1 else if i = -1 then 0 else
2641 let (_, l, _) = outlines.(i) in
2642 if l != curlevel then i else flow (i+incr)
2644 let active = flow active in
2645 let first = calcfirst first active in
2646 state.mode <- Outline (
2647 allowdel, active, first, outlines, qsearch, pan, oldmode
2649 Glut.postRedisplay ()
2651 match key with
2652 | Glut.KEY_UP -> navigate ~-1
2653 | Glut.KEY_DOWN -> navigate 1
2654 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2655 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2657 | Glut.KEY_RIGHT ->
2658 if Glut.getModifiers () land Glut.active_ctrl != 0
2659 then (
2660 state.mode <- Outline (
2661 allowdel, active, first, outlines,
2662 qsearch, min 0 (pan + 1), oldmode
2664 Glut.postRedisplay ();
2666 else (
2667 if not allowdel
2668 then updownlevel 1
2671 | Glut.KEY_LEFT ->
2672 if Glut.getModifiers () land Glut.active_ctrl != 0
2673 then (
2674 state.mode <- Outline (
2675 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2677 Glut.postRedisplay ();
2679 else (
2680 if not allowdel
2681 then updownlevel ~-1
2684 | Glut.KEY_HOME ->
2685 state.mode <- Outline (
2686 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2688 Glut.postRedisplay ()
2690 | Glut.KEY_END ->
2691 let active = Array.length outlines - 1 in
2692 let first = max 0 (active - maxrows) in
2693 state.mode <- Outline (
2694 allowdel, active, first, outlines, qsearch, pan, oldmode
2696 Glut.postRedisplay ()
2698 | _ -> ()
2701 let drawplaceholder l =
2702 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2703 GlDraw.rect
2704 (float l.pagex, float l.pagedispy)
2705 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2707 let x = if margin < 0 then -margin else l.pagex
2708 and y = l.pagedispy + 13 in
2709 GlDraw.color (0.0, 0.0, 0.0);
2710 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2713 let now () = Unix.gettimeofday ();;
2715 let drawpage l =
2716 let color =
2717 match state.mode with
2718 | Textentry _ -> scalecolor 0.4
2719 | View | Outline _ | Items _ -> scalecolor 1.0
2720 | Birdseye (_, _, pageno, hooverpageno, _) ->
2721 if l.pageno = hooverpageno
2722 then scalecolor 0.9
2723 else (
2724 if l.pageno = pageno
2725 then scalecolor 1.0
2726 else scalecolor 0.8
2729 GlDraw.color color;
2730 begin match getopaque l.pageno with
2731 | Some (opaque, _) when validopaque opaque ->
2732 let a = now () in
2733 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2734 opaque;
2735 let b = now () in
2736 let d = b-.a in
2737 vlog "draw %d %f sec" l.pageno d;
2739 | _ ->
2740 drawplaceholder l;
2741 end;
2744 let scrollph y =
2745 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2746 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2747 let sh = float conf.winh /. sh in
2748 let sh = max sh (float conf.scrollh) in
2750 let percent =
2751 if state.y = state.maxy
2752 then 1.0
2753 else float y /. float maxy
2755 let position = (float conf.winh -. sh) *. percent in
2757 let position =
2758 if position +. sh > float conf.winh
2759 then float conf.winh -. sh
2760 else position
2762 position, sh;
2765 let scrollindicator () =
2766 GlDraw.color (0.64 , 0.64, 0.64);
2767 GlDraw.rect
2768 (float (conf.winw - state.scrollw), 0.)
2769 (float conf.winw, float conf.winh)
2771 GlDraw.color (0.0, 0.0, 0.0);
2773 let position, sh = scrollph state.y in
2774 GlDraw.rect
2775 (float (conf.winw - state.scrollw), position)
2776 (float conf.winw, position +. sh)
2780 let showsel margin =
2781 match state.mstate with
2782 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2785 | Msel ((x0, y0), (x1, y1)) ->
2786 let rec loop = function
2787 | l :: ls ->
2788 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2789 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2790 then
2791 match getopaque l.pageno with
2792 | Some (opaque, _) when validopaque opaque ->
2793 let oy = -l.pagey + l.pagedispy in
2794 seltext opaque
2795 (x0 - margin - state.x, y0,
2796 x1 - margin - state.x, y1) oy;
2798 | _ -> ()
2799 else loop ls
2800 | [] -> ()
2802 loop state.layout
2805 let showrects () =
2806 let panx = float state.x in
2807 Gl.enable `blend;
2808 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2809 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2810 List.iter
2811 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2812 List.iter (fun l ->
2813 if l.pageno = pageno
2814 then (
2815 let d = float (l.pagedispy - l.pagey) in
2816 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2817 GlDraw.begins `quads;
2819 GlDraw.vertex2 (x0+.panx, y0+.d);
2820 GlDraw.vertex2 (x1+.panx, y1+.d);
2821 GlDraw.vertex2 (x2+.panx, y2+.d);
2822 GlDraw.vertex2 (x3+.panx, y3+.d);
2824 GlDraw.ends ();
2826 ) state.layout
2827 ) state.rects
2829 Gl.disable `blend;
2832 let showstrings trusted active first pan strings =
2833 Gl.enable `blend;
2834 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2835 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2836 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2837 GlDraw.color (1., 1., 1.);
2838 Gl.enable `texture_2d;
2840 let wx = measurestr 15 "w" in
2841 let tabx = 30.0*.wx +. float (pan*15) in
2842 let rec loop row =
2843 if row = Array.length strings || (row - first) * 16 > conf.winh
2844 then ()
2845 else (
2846 let (s, level, _) = strings.(row) in
2847 let y = (row - first) * 16 in
2848 let x = 5 + 15*(max 0 (level+pan)) in
2849 if row = active
2850 then (
2851 Gl.disable `texture_2d;
2852 GlDraw.polygon_mode `both `line;
2853 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2854 GlDraw.rect (0., float (y + 1))
2855 (float (conf.winw - 1), float (y + 18));
2856 GlDraw.polygon_mode `both `fill;
2857 GlDraw.color (1., 1., 1.);
2858 Gl.enable `texture_2d;
2861 let drawtabularstring x s =
2862 let _ =
2863 if trusted
2864 then
2865 let tabpos = try String.index s '\t' with Not_found -> -1 in
2866 if tabpos > 0
2867 then
2868 let len = String.length s - tabpos - 1 in
2869 let s1 = String.sub s 0 tabpos
2870 and s2 = String.sub s (tabpos + 1) len in
2871 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2872 let x = truncate (max xx tabx) in
2873 drawstring1 15 x (y + 16) s2
2874 else
2875 drawstring1 15 x (y + 16) s
2876 else
2877 drawstring1 15 x (y + 16) s
2881 drawtabularstring (x + pan*15) s;
2882 loop (row+1)
2885 loop first;
2886 Gl.disable `blend;
2887 Gl.disable `texture_2d;
2890 let showoutline (_, active, first, outlines, _, pan, _) =
2891 showstrings false active first pan outlines;
2894 let showitems (active, first, items, _, pan, _) =
2895 showstrings true active first pan items;
2898 let display () =
2899 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2900 GlDraw.viewport margin 0 state.w conf.winh;
2901 pagematrix ();
2902 GlClear.color (scalecolor2 conf.bgcolor);
2903 GlClear.clear [`color];
2904 if conf.zoom > 1.0
2905 then (
2906 Gl.enable `scissor_test;
2907 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2909 List.iter drawpage state.layout;
2910 if conf.zoom > 1.0
2911 then
2912 Gl.disable `scissor_test
2914 if state.x != 0
2915 then (
2916 let x = -.float state.x in
2917 GlMat.translate ~x ();
2919 showrects ();
2920 showsel margin;
2921 GlDraw.viewport 0 0 conf.winw conf.winh;
2922 winmatrix ();
2923 scrollindicator ();
2924 begin match state.mode with
2925 | Items items -> showitems items
2926 | Outline outline -> showoutline outline
2927 | _ -> ()
2928 end;
2929 enttext ();
2930 Glut.swapBuffers ();
2933 let getunder x y =
2934 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2935 let x = x - margin - state.x in
2936 let rec f = function
2937 | l :: rest ->
2938 begin match getopaque l.pageno with
2939 | Some (opaque, _) when validopaque opaque ->
2940 let y = y - l.pagedispy in
2941 if y > 0
2942 then
2943 let y = l.pagey + y in
2944 let x = x - l.pagex in
2945 match whatsunder opaque x y with
2946 | Unone -> f rest
2947 | under -> under
2948 else
2949 f rest
2950 | _ ->
2951 f rest
2953 | [] -> Unone
2955 f state.layout
2958 let viewmouse button bstate x y =
2959 match button with
2960 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2961 if Glut.getModifiers () land Glut.active_ctrl != 0
2962 then (
2963 match state.mstate with
2964 | Mzoom (oldn, i) ->
2965 if oldn = n
2966 then (
2967 if i = 2
2968 then
2969 let incr =
2970 match n with
2971 | 4 ->
2972 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2973 | _ ->
2974 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2976 let zoom = conf.zoom +. incr in
2977 setzoom zoom;
2978 state.mstate <- Mzoom (n, 0);
2979 else
2980 state.mstate <- Mzoom (n, i+1);
2982 else state.mstate <- Mzoom (n, 0)
2984 | _ -> state.mstate <- Mzoom (n, 0)
2986 else (
2987 if state.ascrollstep > 0
2988 then
2989 setautoscrollspeed (n=4)
2990 else
2991 let incr =
2992 if n = 3
2993 then -conf.scrollstep
2994 else conf.scrollstep
2996 let incr = incr * 2 in
2997 let y = clamp incr in
2998 gotoy_and_clear_text y
3001 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3002 if bstate = Glut.DOWN
3003 then (
3004 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3005 state.mstate <- Mpan (x, y)
3007 else
3008 state.mstate <- Mnone
3010 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3011 if bstate = Glut.DOWN
3012 then
3013 let position, sh = scrollph state.y in
3014 if y > truncate position && y < truncate (position +. sh)
3015 then
3016 state.mstate <- Mscroll
3017 else
3018 let percent = float y /. float conf.winh in
3019 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3020 gotoy desty;
3021 state.mstate <- Mscroll
3022 else
3023 state.mstate <- Mnone
3025 | Glut.LEFT_BUTTON ->
3026 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3027 begin match dest with
3028 | Ulinkgoto (pageno, top) ->
3029 if pageno >= 0
3030 then (
3031 addnav ();
3032 gotopage1 pageno top;
3035 | Ulinkuri s ->
3036 print_endline s
3038 | Unone when bstate = Glut.DOWN ->
3039 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3040 state.mstate <- Mpan (x, y);
3042 | Unone | Utext _ ->
3043 if bstate = Glut.DOWN
3044 then (
3045 if conf.angle mod 360 = 0
3046 then (
3047 state.mstate <- Msel ((x, y), (x, y));
3048 Glut.postRedisplay ()
3051 else (
3052 match state.mstate with
3053 | Mnone -> ()
3055 | Mzoom _ | Mscroll ->
3056 state.mstate <- Mnone
3058 | Mpan _ ->
3059 Glut.setCursor Glut.CURSOR_INHERIT;
3060 state.mstate <- Mnone
3062 | Msel ((_, y0), (_, y1)) ->
3063 let f l =
3064 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3065 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3066 then
3067 match getopaque l.pageno with
3068 | Some (opaque, _) when validopaque opaque ->
3069 copysel opaque
3070 | _ -> ()
3072 List.iter f state.layout;
3073 copysel ""; (* ugly *)
3074 Glut.setCursor Glut.CURSOR_INHERIT;
3075 state.mstate <- Mnone;
3079 | _ -> ()
3082 let birdseyemouse button bstate x y
3083 (conf, leftx, _, hooverpageno, anchor) =
3084 match button with
3085 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3086 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3087 let rec loop = function
3088 | [] -> ()
3089 | l :: rest ->
3090 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3091 && x > margin && x < margin + l.pagew
3092 then (
3093 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3095 else loop rest
3097 loop state.layout
3098 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3099 | _ -> ()
3102 let mouse bstate button x y =
3103 match state.mode with
3104 | View -> viewmouse button bstate x y
3105 | Birdseye beye -> birdseyemouse button bstate x y beye
3106 | Textentry _ | Outline _ | Items _ -> ()
3109 let mouse ~button ~state ~x ~y = mouse state button x y;;
3111 let motion ~x ~y =
3112 match state.mode with
3113 | Outline _ -> ()
3114 | _ ->
3115 match state.mstate with
3116 | Mzoom _ | Mnone -> ()
3118 | Mpan (x0, y0) ->
3119 let dx = x - x0
3120 and dy = y0 - y in
3121 state.mstate <- Mpan (x, y);
3122 if conf.zoom > 1.0 then state.x <- state.x + dx;
3123 let y = clamp dy in
3124 gotoy_and_clear_text y
3126 | Msel (a, _) ->
3127 state.mstate <- Msel (a, (x, y));
3128 Glut.postRedisplay ()
3130 | Mscroll ->
3131 let y = min conf.winh (max 0 y) in
3132 let percent = float y /. float conf.winh in
3133 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3134 gotoy_and_clear_text y
3137 let pmotion ~x ~y =
3138 match state.mode with
3139 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3140 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3141 let rec loop = function
3142 | [] ->
3143 if hooverpageno != -1
3144 then (
3145 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3146 Glut.postRedisplay ();
3148 | l :: rest ->
3149 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3150 && x > margin && x < margin + l.pagew
3151 then (
3152 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3153 Glut.postRedisplay ();
3155 else loop rest
3157 loop state.layout
3159 | Outline _ | Items _ | Textentry _ -> ()
3160 | View ->
3161 match state.mstate with
3162 | Mnone ->
3163 begin match getunder x y with
3164 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3165 | Ulinkuri uri ->
3166 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3167 Glut.setCursor Glut.CURSOR_INFO
3168 | Ulinkgoto (page, _) ->
3169 if conf.underinfo
3170 then showtext 'p' ("age: " ^ string_of_int page);
3171 Glut.setCursor Glut.CURSOR_INFO
3172 | Utext s ->
3173 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3174 Glut.setCursor Glut.CURSOR_TEXT
3177 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3182 module State =
3183 struct
3184 open Parser
3186 let home =
3188 match Sys.os_type with
3189 | "Win32" -> Sys.getenv "HOMEPATH"
3190 | _ -> Sys.getenv "HOME"
3191 with exn ->
3192 prerr_endline
3193 ("Can not determine home directory location: " ^
3194 Printexc.to_string exn);
3198 let config_of c attrs =
3199 let apply c k v =
3201 match k with
3202 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3203 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3204 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3205 | "preload" -> { c with preload = bool_of_string v }
3206 | "page-bias" -> { c with pagebias = int_of_string v }
3207 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3208 | "auto-scroll-step" ->
3209 { c with autoscrollstep = max 0 (int_of_string v) }
3210 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3211 | "crop-hack" -> { c with crophack = bool_of_string v }
3212 | "throttle" -> { c with showall = bool_of_string v }
3213 | "highlight-links" -> { c with hlinks = bool_of_string v }
3214 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3215 | "vertical-margin" ->
3216 { c with interpagespace = max 0 (int_of_string v) }
3217 | "zoom" ->
3218 let zoom = float_of_string v /. 100. in
3219 let zoom = max 0.01 (min 2.2 zoom) in
3220 { c with zoom = zoom }
3221 | "presentation" -> { c with presentation = bool_of_string v }
3222 | "rotation-angle" -> { c with angle = int_of_string v }
3223 | "width" -> { c with winw = max 20 (int_of_string v) }
3224 | "height" -> { c with winh = max 20 (int_of_string v) }
3225 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3226 | "proportional-display" -> { c with proportional = bool_of_string v }
3227 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3228 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3229 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3230 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3231 | "persistent-location" -> { c with jumpback = bool_of_string v }
3232 | "background-color" -> { c with bgcolor = color_of_string v }
3233 | "scrollbar-in-presentation" ->
3234 { c with scrollbarinpm = bool_of_string v }
3235 | "ui-font" -> { c with uifont = v }
3236 | _ -> c
3237 with exn ->
3238 prerr_endline ("Error processing attribute (`" ^
3239 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3242 let rec fold c = function
3243 | [] -> c
3244 | (k, v) :: rest ->
3245 let c = apply c k v in
3246 fold c rest
3248 fold c attrs;
3251 let fromstring f pos n v d =
3252 try f v
3253 with exn ->
3254 dolog "Error processing attribute (%S=%S) at %d\n%s"
3255 n v pos (Printexc.to_string exn)
3260 let bookmark_of attrs =
3261 let rec fold title page rely = function
3262 | ("title", v) :: rest -> fold v page rely rest
3263 | ("page", v) :: rest -> fold title v rely rest
3264 | ("rely", v) :: rest -> fold title page v rest
3265 | _ :: rest -> fold title page rely rest
3266 | [] -> title, page, rely
3268 fold "invalid" "0" "0" attrs
3271 let doc_of attrs =
3272 let rec fold path page rely pan = function
3273 | ("path", v) :: rest -> fold v page rely pan rest
3274 | ("page", v) :: rest -> fold path v rely pan rest
3275 | ("rely", v) :: rest -> fold path page v pan rest
3276 | ("pan", v) :: rest -> fold path page rely v rest
3277 | _ :: rest -> fold path page rely pan rest
3278 | [] -> path, page, rely, pan
3280 fold "" "0" "0" "0" attrs
3283 let setconf dst src =
3284 dst.scrollbw <- src.scrollbw;
3285 dst.scrollh <- src.scrollh;
3286 dst.icase <- src.icase;
3287 dst.preload <- src.preload;
3288 dst.pagebias <- src.pagebias;
3289 dst.verbose <- src.verbose;
3290 dst.scrollstep <- src.scrollstep;
3291 dst.maxhfit <- src.maxhfit;
3292 dst.crophack <- src.crophack;
3293 dst.autoscrollstep <- src.autoscrollstep;
3294 dst.showall <- src.showall;
3295 dst.hlinks <- src.hlinks;
3296 dst.underinfo <- src.underinfo;
3297 dst.interpagespace <- src.interpagespace;
3298 dst.zoom <- src.zoom;
3299 dst.presentation <- src.presentation;
3300 dst.angle <- src.angle;
3301 dst.winw <- src.winw;
3302 dst.winh <- src.winh;
3303 dst.savebmarks <- src.savebmarks;
3304 dst.memlimit <- src.memlimit;
3305 dst.proportional <- src.proportional;
3306 dst.texcount <- src.texcount;
3307 dst.sliceheight <- src.sliceheight;
3308 dst.thumbw <- src.thumbw;
3309 dst.jumpback <- src.jumpback;
3310 dst.bgcolor <- src.bgcolor;
3311 dst.scrollbarinpm <- src.scrollbarinpm;
3312 dst.uifont <- src.uifont;
3315 let unent s =
3316 let l = String.length s in
3317 let b = Buffer.create l in
3318 unent b s 0 l;
3319 Buffer.contents b;
3322 let get s =
3323 let h = Hashtbl.create 10 in
3324 let dc = { defconf with angle = defconf.angle } in
3325 let rec toplevel v t spos _ =
3326 match t with
3327 | Vdata | Vcdata | Vend -> v
3328 | Vopen ("llppconfig", _, closed) ->
3329 if closed
3330 then v
3331 else { v with f = llppconfig }
3332 | Vopen _ ->
3333 error "unexpected subelement at top level" s spos
3334 | Vclose _ -> error "unexpected close at top level" s spos
3336 and llppconfig v t spos _ =
3337 match t with
3338 | Vdata | Vcdata | Vend -> v
3339 | Vopen ("defaults", attrs, closed) ->
3340 let c = config_of dc attrs in
3341 setconf dc c;
3342 if closed
3343 then v
3344 else { v with f = skip "defaults" (fun () -> v) }
3346 | Vopen ("doc", attrs, closed) ->
3347 let pathent, spage, srely, span = doc_of attrs in
3348 let path = unent pathent
3349 and pageno = fromstring int_of_string spos "page" spage 0
3350 and rely = fromstring float_of_string spos "rely" srely 0.0
3351 and pan = fromstring int_of_string spos "pan" span 0 in
3352 let c = config_of dc attrs in
3353 let anchor = (pageno, rely) in
3354 if closed
3355 then (Hashtbl.add h path (c, [], pan, anchor); v)
3356 else { v with f = doc path pan anchor c [] }
3358 | Vopen _ ->
3359 error "unexpected subelement in llppconfig" s spos
3361 | Vclose "llppconfig" -> { v with f = toplevel }
3362 | Vclose _ -> error "unexpected close in llppconfig" s spos
3364 and doc path pan anchor c bookmarks v t spos _ =
3365 match t with
3366 | Vdata | Vcdata -> v
3367 | Vend -> error "unexpected end of input in doc" s spos
3368 | Vopen ("bookmarks", _, closed) ->
3369 if closed
3370 then v
3371 else { v with f = pbookmarks path pan anchor c bookmarks }
3373 | Vopen (_, _, _) ->
3374 error "unexpected subelement in doc" s spos
3376 | Vclose "doc" ->
3377 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3378 { v with f = llppconfig }
3380 | Vclose _ -> error "unexpected close in doc" s spos
3382 and pbookmarks path pan anchor c bookmarks v t spos _ =
3383 match t with
3384 | Vdata | Vcdata -> v
3385 | Vend -> error "unexpected end of input in bookmarks" s spos
3386 | Vopen ("item", attrs, closed) ->
3387 let titleent, spage, srely = bookmark_of attrs in
3388 let page = fromstring int_of_string spos "page" spage 0
3389 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3390 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3391 if closed
3392 then { v with f = pbookmarks path pan anchor c bookmarks }
3393 else
3394 let f () = v in
3395 { v with f = skip "item" f }
3397 | Vopen _ ->
3398 error "unexpected subelement in bookmarks" s spos
3400 | Vclose "bookmarks" ->
3401 { v with f = doc path pan anchor c bookmarks }
3403 | Vclose _ -> error "unexpected close in bookmarks" s spos
3405 and skip tag f v t spos _ =
3406 match t with
3407 | Vdata | Vcdata -> v
3408 | Vend ->
3409 error ("unexpected end of input in skipped " ^ tag) s spos
3410 | Vopen (tag', _, closed) ->
3411 if closed
3412 then v
3413 else
3414 let f' () = { v with f = skip tag f } in
3415 { v with f = skip tag' f' }
3416 | Vclose ctag ->
3417 if tag = ctag
3418 then f ()
3419 else error ("unexpected close in skipped " ^ tag) s spos
3422 parse { f = toplevel; accu = () } s;
3423 h, dc;
3426 let do_load f ic =
3428 let len = in_channel_length ic in
3429 let s = String.create len in
3430 really_input ic s 0 len;
3431 f s;
3432 with
3433 | Parse_error (msg, s, pos) ->
3434 let subs = subs s pos in
3435 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3436 failwith ("parse error: " ^ s)
3438 | exn ->
3439 failwith ("config load error: " ^ Printexc.to_string exn)
3442 let path =
3443 let dir =
3445 let dir = Filename.concat home ".config" in
3446 if Sys.is_directory dir then dir else home
3447 with _ -> home
3449 Filename.concat dir "llpp.conf"
3452 let load1 f =
3453 if Sys.file_exists path
3454 then
3455 match
3456 (try Some (open_in_bin path)
3457 with exn ->
3458 prerr_endline
3459 ("Error opening configuation file `" ^ path ^ "': " ^
3460 Printexc.to_string exn);
3461 None
3463 with
3464 | Some ic ->
3465 begin try
3466 f (do_load get ic)
3467 with exn ->
3468 prerr_endline
3469 ("Error loading configuation from `" ^ path ^ "': " ^
3470 Printexc.to_string exn);
3471 end;
3472 close_in ic;
3474 | None -> ()
3475 else
3476 f (Hashtbl.create 0, defconf)
3479 let load () =
3480 let f (h, dc) =
3481 let pc, pb, px, pa =
3483 Hashtbl.find h (Filename.basename state.path)
3484 with Not_found -> dc, [], 0, (0, 0.0)
3486 setconf defconf dc;
3487 setconf conf pc;
3488 state.bookmarks <- pb;
3489 state.x <- px;
3490 state.scrollw <- conf.scrollbw;
3491 if conf.jumpback
3492 then state.anchor <- pa;
3493 cbput state.hists.nav pa;
3495 load1 f
3498 let add_attrs bb always dc c =
3499 let ob s a b =
3500 if always || a != b
3501 then Printf.bprintf bb "\n %s='%b'" s a
3502 and oi s a b =
3503 if always || a != b
3504 then Printf.bprintf bb "\n %s='%d'" s a
3505 and oz s a b =
3506 if always || a <> b
3507 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3508 and oc s a b =
3509 if always || a <> b
3510 then
3511 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3512 and os s a b =
3513 if always || a <> b
3514 then
3515 Printf.bprintf bb "\n %s='%s'" s a
3517 let w, h =
3518 if always
3519 then dc.winw, dc.winh
3520 else
3521 match state.fullscreen with
3522 | Some wh -> wh
3523 | None -> c.winw, c.winh
3525 let zoom, presentation, interpagespace, showall=
3526 if always
3527 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3528 else
3529 match state.mode with
3530 | Birdseye (bc, _, _, _, _) ->
3531 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3532 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3534 oi "width" w dc.winw;
3535 oi "height" h dc.winh;
3536 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3537 oi "scroll-handle-height" c.scrollh dc.scrollh;
3538 ob "case-insensitive-search" c.icase dc.icase;
3539 ob "preload" c.preload dc.preload;
3540 oi "page-bias" c.pagebias dc.pagebias;
3541 oi "scroll-step" c.scrollstep dc.scrollstep;
3542 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3543 ob "max-height-fit" c.maxhfit dc.maxhfit;
3544 ob "crop-hack" c.crophack dc.crophack;
3545 ob "throttle" showall dc.showall;
3546 ob "highlight-links" c.hlinks dc.hlinks;
3547 ob "under-cursor-info" c.underinfo dc.underinfo;
3548 oi "vertical-margin" interpagespace dc.interpagespace;
3549 oz "zoom" zoom dc.zoom;
3550 ob "presentation" presentation dc.presentation;
3551 oi "rotation-angle" c.angle dc.angle;
3552 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3553 ob "proportional-display" c.proportional dc.proportional;
3554 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3555 oi "texcount" c.texcount dc.texcount;
3556 oi "slice-height" c.sliceheight dc.sliceheight;
3557 oi "thumbnail-width" c.thumbw dc.thumbw;
3558 ob "persistent-location" c.jumpback dc.jumpback;
3559 oc "background-color" c.bgcolor dc.bgcolor;
3560 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3561 os "ui-font" c.uifont dc.uifont;
3564 let save () =
3565 let bb = Buffer.create 32768 in
3566 let f (h, dc) =
3567 let dc = if conf.bedefault then conf else dc in
3568 Buffer.add_string bb "<llppconfig>\n<defaults ";
3569 add_attrs bb true dc dc;
3570 Buffer.add_string bb "/>\n";
3572 let adddoc path pan anchor c bookmarks =
3573 if bookmarks == [] && c = dc && anchor = emptyanchor
3574 then ()
3575 else (
3576 Printf.bprintf bb "<doc path='%s'"
3577 (enent path 0 (String.length path));
3579 if anchor <> emptyanchor
3580 then (
3581 let n, y = anchor in
3582 Printf.bprintf bb " page='%d'" n;
3583 if y > 1e-6
3584 then
3585 Printf.bprintf bb " rely='%f'" y
3589 if pan != 0
3590 then Printf.bprintf bb " pan='%d'" pan;
3592 add_attrs bb false dc c;
3594 begin match bookmarks with
3595 | [] -> Buffer.add_string bb "/>\n"
3596 | _ ->
3597 Buffer.add_string bb ">\n<bookmarks>\n";
3598 List.iter (fun (title, _level, (page, rely)) ->
3599 Printf.bprintf bb
3600 "<item title='%s' page='%d'"
3601 (enent title 0 (String.length title))
3602 page
3604 if rely > 1e-6
3605 then
3606 Printf.bprintf bb " rely='%f'" rely
3608 Buffer.add_string bb "/>\n";
3609 ) bookmarks;
3610 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3611 end;
3615 let pan =
3616 match state.mode with
3617 | Birdseye (_, pan, _, _, _) -> pan
3618 | _ -> state.x
3620 let basename = Filename.basename state.path in
3621 adddoc basename pan (getanchor ())
3622 { conf with
3623 autoscrollstep =
3624 if state.ascrollstep > 0
3625 then state.ascrollstep
3626 else conf.autoscrollstep }
3627 (if conf.savebmarks then state.bookmarks else []);
3629 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3630 if basename <> path
3631 then adddoc path x y c bookmarks
3632 ) h;
3633 Buffer.add_string bb "</llppconfig>";
3635 load1 f;
3636 if Buffer.length bb > 0
3637 then
3639 let tmp = path ^ ".tmp" in
3640 let oc = open_out_bin tmp in
3641 Buffer.output_buffer oc bb;
3642 close_out oc;
3643 Sys.rename tmp path;
3644 with exn ->
3645 prerr_endline
3646 ("error while saving configuration: " ^ Printexc.to_string exn)
3648 end;;
3650 let () =
3651 Arg.parse
3652 (Arg.align
3653 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3654 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3655 " Print version and exit")]
3657 (fun s -> state.path <- s)
3658 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3660 if String.length state.path = 0
3661 then (prerr_endline "file name missing"; exit 1);
3663 State.load ();
3665 let _ = Glut.init Sys.argv in
3666 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3667 let () = Glut.initWindowSize conf.winw conf.winh in
3668 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3670 let csock, ssock =
3671 if Sys.os_type = "Unix"
3672 then
3673 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3674 else
3675 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3676 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3677 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3678 Unix.bind sock addr;
3679 Unix.listen sock 1;
3680 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3681 Unix.connect csock addr;
3682 let ssock, _ = Unix.accept sock in
3683 Unix.close sock;
3684 let opts sock =
3685 Unix.setsockopt sock Unix.TCP_NODELAY true;
3686 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3688 opts ssock;
3689 opts csock;
3690 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3691 ssock, csock
3694 let () = Glut.displayFunc display in
3695 let () = Glut.reshapeFunc reshape in
3696 let () = Glut.keyboardFunc keyboard in
3697 let () = Glut.specialFunc special in
3698 let () = Glut.idleFunc (Some idle) in
3699 let () = Glut.mouseFunc mouse in
3700 let () = Glut.motionFunc motion in
3701 let () = Glut.passiveMotionFunc pmotion in
3703 init ssock (
3704 conf.angle, conf.proportional, conf.texcount,
3705 conf.sliceheight, conf.uifont
3707 state.csock <- csock;
3708 state.ssock <- ssock;
3709 state.text <- "Opening " ^ state.path;
3710 writeopen state.path state.password;
3712 at_exit State.save;
3714 let rec handlelablglutbug () =
3716 Glut.mainLoop ();
3717 with Glut.BadEnum "key in special_of_int" ->
3718 showtext '!' " LablGlut bug: special key not recognized";
3719 handlelablglutbug ()
3721 handlelablglutbug ();