Remove bitrotten line
[llpp.git] / main.ml
blob20395feae6811cf60979f4d4dc190fa326efca46
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 exception Quit;;
12 type params =
13 angle * proportional * texcount * sliceheight * blockwidth * fontpath
14 and pageno = int
15 and width = int
16 and height = int
17 and leftx = int
18 and opaque = string
19 and recttype = int
20 and pixmapsize = int
21 and angle = int
22 and proportional = bool
23 and interpagespace = int
24 and texcount = int
25 and sliceheight = int
26 and blockwidth = int
27 and gen = int
28 and top = float
29 and fontpath = string
32 external init : Unix.file_descr -> params -> unit = "ml_init";;
33 external draw : (int * int * int * bool) -> string -> unit = "ml_draw";;
34 external seltext : string -> (int * int * int * int) -> int -> unit =
35 "ml_seltext";;
36 external copysel : string -> unit = "ml_copysel";;
37 external getpdimrect : int -> float array = "ml_getpdimrect";;
38 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
39 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
40 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
41 external measurestr : int -> string -> float = "ml_measure_string";;
42 external getmaxw : unit -> float = "ml_getmaxw";;
44 type mpos = int * int
45 and mstate =
46 | Msel of (mpos * mpos)
47 | Mpan of mpos
48 | Mscroll
49 | Mzoom of (int * int)
50 | Mnone
53 type textentry = string * string * onhist option * onkey * ondone
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel)
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
66 type 'a circbuf =
67 { store : 'a array
68 ; mutable rc : int
69 ; mutable wc : int
70 ; mutable len : int
74 let cbnew n v =
75 { store = Array.create n v
76 ; rc = 0
77 ; wc = 0
78 ; len = 0
82 let cbcap b = Array.length b.store;;
84 let cbput b v =
85 let cap = cbcap b in
86 b.store.(b.wc) <- v;
87 b.wc <- (b.wc + 1) mod cap;
88 b.rc <- b.wc;
89 b.len <- min (b.len + 1) cap;
92 let cbempty b = b.len = 0;;
94 let cbgetg b circular dir =
95 if cbempty b
96 then b.store.(0)
97 else
98 let rc = b.rc + dir in
99 let rc =
100 if circular
101 then (
102 if rc = -1
103 then b.len-1
104 else (
105 if rc = b.len
106 then 0
107 else rc
110 else max 0 (min rc (b.len-1))
112 b.rc <- rc;
113 b.store.(rc);
116 let cbget b = cbgetg b false;;
117 let cbgetc b = cbgetg b true;;
119 let cbpeek b =
120 let rc = b.wc - b.len in
121 let rc = if rc < 0 then cbcap b + rc else rc in
122 b.store.(rc);
125 let cbdecr b = b.len <- b.len - 1;;
127 type layout =
128 { pageno : int
129 ; pagedimno : int
130 ; pagew : int
131 ; pageh : int
132 ; pagedispy : int
133 ; pagey : int
134 ; pagevh : int
135 ; pagex : int
139 type conf =
140 { mutable scrollbw : int
141 ; mutable scrollh : int
142 ; mutable icase : bool
143 ; mutable preload : bool
144 ; mutable pagebias : int
145 ; mutable verbose : bool
146 ; mutable scrollstep : int
147 ; mutable maxhfit : bool
148 ; mutable crophack : bool
149 ; mutable autoscrollstep : int
150 ; mutable showall : bool
151 ; mutable hlinks : bool
152 ; mutable underinfo : bool
153 ; mutable interpagespace : interpagespace
154 ; mutable zoom : float
155 ; mutable presentation : bool
156 ; mutable angle : angle
157 ; mutable winw : int
158 ; mutable winh : int
159 ; mutable savebmarks : bool
160 ; mutable proportional : proportional
161 ; mutable memlimit : int
162 ; mutable texcount : texcount
163 ; mutable sliceheight : sliceheight
164 ; mutable blockwidth : blockwidth
165 ; mutable thumbw : width
166 ; mutable jumpback : bool
167 ; mutable bgcolor : float * float * float
168 ; mutable bedefault : bool
169 ; mutable scrollbarinpm : bool
173 type anchor = pageno * top;;
175 type outline = string * int * anchor
176 and outlines =
177 | Oarray of outline array
178 | Olist of outline list
179 | Onarrow of string * outline array * outline array
182 type rect = float * float * float * float * float * float * float * float;;
184 type pagemapkey = pageno * width * angle * proportional * gen;;
186 let emptyanchor = (0, 0.0);;
188 type mode =
189 | Birdseye of (conf * leftx * pageno * pageno * anchor)
190 | Outline of (bool * int * int * outline array * string * int * mode)
191 | Items of (int * int * item array * string * int * mode)
192 | Textentry of (textentry * onleave)
193 | View
194 and onleave = leavetextentrystatus -> unit
195 and leavetextentrystatus = | Cancel | Confirm
196 and item = string * int * action
197 and action =
198 | Noaction
199 | Action of (int -> int -> string -> int -> mode)
202 let isbirdseye = function Birdseye _ -> true | _ -> false;;
203 let istextentry = function Textentry _ -> true | _ -> false;;
205 type state =
206 { mutable csock : Unix.file_descr
207 ; mutable ssock : Unix.file_descr
208 ; mutable w : int
209 ; mutable x : int
210 ; mutable y : int
211 ; mutable scrollw : int
212 ; mutable anchor : anchor
213 ; mutable maxy : int
214 ; mutable layout : layout list
215 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
216 ; mutable pdims : (pageno * width * height * leftx) list
217 ; mutable pagecount : int
218 ; pagecache : string circbuf
219 ; mutable rendering : bool
220 ; mutable mstate : mstate
221 ; mutable searchpattern : string
222 ; mutable rects : (pageno * recttype * rect) list
223 ; mutable rects1 : (pageno * recttype * rect) list
224 ; mutable text : string
225 ; mutable fullscreen : (width * height) option
226 ; mutable mode : mode
227 ; mutable outlines : outlines
228 ; mutable bookmarks : outline list
229 ; mutable path : string
230 ; mutable password : string
231 ; mutable invalidated : int
232 ; mutable colorscale : float
233 ; mutable memused : int
234 ; mutable gen : gen
235 ; mutable throttle : layout list option
236 ; mutable autoscroll :int option
237 ; mutable help : item array
238 ; mutable docinfo : (int * string) list
239 ; mutable deadline : float
240 ; hists : hists
242 and hists =
243 { pat : string circbuf
244 ; pag : string circbuf
245 ; nav : anchor circbuf
249 let defconf =
250 { scrollbw = 7
251 ; scrollh = 12
252 ; icase = true
253 ; preload = true
254 ; pagebias = 0
255 ; verbose = false
256 ; scrollstep = 24
257 ; maxhfit = true
258 ; crophack = false
259 ; autoscrollstep = 2
260 ; showall = false
261 ; hlinks = false
262 ; underinfo = false
263 ; interpagespace = 2
264 ; zoom = 1.0
265 ; presentation = false
266 ; angle = 0
267 ; winw = 900
268 ; winh = 900
269 ; savebmarks = true
270 ; proportional = true
271 ; memlimit = 32*1024*1024
272 ; texcount = 256
273 ; sliceheight = 24
274 ; blockwidth = 2048
275 ; thumbw = 76
276 ; jumpback = false
277 ; bgcolor = (0.5, 0.5, 0.5)
278 ; bedefault = false
279 ; scrollbarinpm = true
283 let conf = { defconf with angle = defconf.angle };;
285 let makehelp () =
286 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
287 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
290 let state =
291 { csock = Unix.stdin
292 ; ssock = Unix.stdin
293 ; x = 0
294 ; y = 0
295 ; w = 0
296 ; scrollw = 0
297 ; anchor = emptyanchor
298 ; layout = []
299 ; maxy = max_int
300 ; pagemap = Hashtbl.create 10
301 ; pagecache = cbnew 100 ""
302 ; pdims = []
303 ; pagecount = 0
304 ; rendering = false
305 ; mstate = Mnone
306 ; rects = []
307 ; rects1 = []
308 ; text = ""
309 ; mode = View
310 ; fullscreen = None
311 ; searchpattern = ""
312 ; outlines = Olist []
313 ; bookmarks = []
314 ; path = ""
315 ; password = ""
316 ; invalidated = 0
317 ; hists =
318 { nav = cbnew 100 (0, 0.0)
319 ; pat = cbnew 20 ""
320 ; pag = cbnew 10 ""
322 ; colorscale = 1.0
323 ; memused = 0
324 ; gen = 0
325 ; throttle = None
326 ; autoscroll = None
327 ; help = makehelp ()
328 ; docinfo = []
329 ; deadline = nan
333 let vlog fmt =
334 if conf.verbose
335 then
336 Printf.kprintf prerr_endline fmt
337 else
338 Printf.kprintf ignore fmt
341 let writecmd fd s =
342 let len = String.length s in
343 let n = 4 + len in
344 let b = Buffer.create n in
345 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
346 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
347 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
348 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
349 Buffer.add_string b s;
350 let s' = Buffer.contents b in
351 let n' = Unix.write fd s' 0 n in
352 if n' != n then failwith "write failed";
355 let readcmd fd =
356 let s = "xxxx" in
357 let n = Unix.read fd s 0 4 in
358 if n != 4 then failwith "incomplete read(len)";
359 let len = 0
360 lor (Char.code s.[0] lsl 24)
361 lor (Char.code s.[1] lsl 16)
362 lor (Char.code s.[2] lsl 8)
363 lor (Char.code s.[3] lsl 0)
365 let s = String.create len in
366 let n = Unix.read fd s 0 len in
367 if n != len then failwith "incomplete read(data)";
371 let makecmd s l =
372 let b = Buffer.create 10 in
373 Buffer.add_string b s;
374 let rec combine = function
375 | [] -> b
376 | x :: xs ->
377 Buffer.add_char b ' ';
378 let s =
379 match x with
380 | `b b -> if b then "1" else "0"
381 | `s s -> s
382 | `i i -> string_of_int i
383 | `f f -> string_of_float f
384 | `I f -> string_of_int (truncate f)
386 Buffer.add_string b s;
387 combine xs;
389 combine l;
392 let wcmd s l =
393 let cmd = Buffer.contents (makecmd s l) in
394 writecmd state.csock cmd;
397 let calcips h =
398 if conf.presentation
399 then
400 let d = conf.winh - h in
401 max 0 ((d + 1) / 2)
402 else
403 conf.interpagespace
406 let calcheight () =
407 let rec f pn ph pi fh l =
408 match l with
409 | (n, _, h, _) :: rest ->
410 let ips = calcips h in
411 let fh =
412 if conf.presentation
413 then fh+ips
414 else (
415 if isbirdseye state.mode && pn = 0
416 then fh + ips
417 else fh
420 let fh = fh + ((n - pn) * (ph + pi)) in
421 f n h ips fh rest;
423 | [] ->
424 let inc =
425 if conf.presentation || (isbirdseye state.mode && pn = 0)
426 then 0
427 else -pi
429 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
430 max 0 fh
432 let fh = f 0 0 0 0 state.pdims in
436 let getpageyh pageno =
437 let rec f pn ph pi y l =
438 match l with
439 | (n, _, h, _) :: rest ->
440 let ips = calcips h in
441 if n >= pageno
442 then
443 let h = if n = pageno then h else ph in
444 if conf.presentation && n = pageno
445 then
446 y + (pageno - pn) * (ph + pi) + pi, h
447 else
448 y + (pageno - pn) * (ph + pi), h
449 else
450 let y = y + (if conf.presentation then pi else 0) in
451 let y = y + (n - pn) * (ph + pi) in
452 f n h ips y rest
454 | [] ->
455 y + (pageno - pn) * (ph + pi), ph
457 f 0 0 0 0 state.pdims
460 let getpagey pageno = fst (getpageyh pageno);;
462 let layout y sh =
463 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
464 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
465 match pdims with
466 | (pageno', w, h, x) :: rest when pageno' = pageno ->
467 let ips = calcips h in
468 let yinc =
469 if conf.presentation || (isbirdseye state.mode && pageno = 0)
470 then ips
471 else 0
473 (w, h, ips, x), rest, pdimno + 1, yinc
474 | _ ->
475 prev, pdims, pdimno, 0
477 let dy = dy + yinc in
478 let py = py + yinc in
479 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
480 then
481 accu
482 else
483 let vy = y + dy in
484 if py + h <= vy - yinc
485 then
486 let py = py + h + ips in
487 let dy = max 0 (py - y) in
488 f ~pageno:(pageno+1)
489 ~pdimno
490 ~prev:curr
493 ~pdims:rest
494 ~cacheleft
495 ~accu
496 else
497 let pagey = vy - py in
498 let pagevh = h - pagey in
499 let pagevh = min (sh - dy) pagevh in
500 let off = if yinc > 0 then py - vy else 0 in
501 let py = py + h + ips in
502 let e =
503 { pageno = pageno
504 ; pagedimno = pdimno
505 ; pagew = w
506 ; pageh = h
507 ; pagedispy = dy + off
508 ; pagey = pagey + off
509 ; pagevh = pagevh - off
510 ; pagex = x
513 let accu = e :: accu in
514 f ~pageno:(pageno+1)
515 ~pdimno
516 ~prev:curr
518 ~dy:(dy+pagevh+ips)
519 ~pdims:rest
520 ~cacheleft:(cacheleft-1)
521 ~accu
523 if state.invalidated = 0
524 then (
525 let accu =
527 ~pageno:0
528 ~pdimno:~-1
529 ~prev:(0,0,0,0)
530 ~py:0
531 ~dy:0
532 ~pdims:state.pdims
533 ~cacheleft:(cbcap state.pagecache)
534 ~accu:[]
536 List.rev accu
538 else
542 let clamp incr =
543 let y = state.y + incr in
544 let y = max 0 y in
545 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
549 let getopaque pageno =
550 try Some (Hashtbl.find state.pagemap
551 (pageno, state.w, conf.angle, conf.proportional, state.gen))
552 with Not_found -> None
555 let cache pageno opaque =
556 Hashtbl.replace state.pagemap
557 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
560 let validopaque opaque = String.length opaque > 0;;
562 let render l =
563 match getopaque l.pageno with
564 | None when not state.rendering ->
565 state.rendering <- true;
566 cache l.pageno ("", -1);
567 wcmd "render" [`i (l.pageno + 1)
568 ;`i l.pagedimno
569 ;`i l.pagew
570 ;`i l.pageh];
571 | _ -> ()
574 let loadlayout layout =
575 let rec f all = function
576 | l :: ls ->
577 begin match getopaque l.pageno with
578 | None -> render l; f false ls
579 | Some (opaque, _) -> f (all && validopaque opaque) ls
581 | [] -> all
583 f (layout <> []) layout;
586 let findpageforopaque opaque =
587 Hashtbl.fold
588 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
589 state.pagemap None
592 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
594 let preload () =
595 let oktopreload =
596 if conf.preload
597 then
598 let memleft = conf.memlimit - state.memused in
599 if memleft < 0
600 then
601 let opaque = cbpeek state.pagecache in
602 match findpageforopaque opaque with
603 | Some ((n, _, _, _, _), size) ->
604 memleft + size >= 0 && not (pagevisible state.layout n)
605 | None -> false
606 else true
607 else false
609 if oktopreload
610 then
611 let presentation = conf.presentation in
612 let interpagespace = conf.interpagespace in
613 let maxy = state.maxy in
614 conf.presentation <- false;
615 conf.interpagespace <- 0;
616 state.maxy <- calcheight ();
617 let y =
618 match state.layout with
619 | [] -> 0
620 | l :: _ -> getpagey l.pageno + l.pagey
622 let y = if y < conf.winh then 0 else y - conf.winh in
623 let h = state.y - y + conf.winh*3 in
624 let pages = layout y h in
625 List.iter render pages;
626 conf.presentation <- presentation;
627 conf.interpagespace <- interpagespace;
628 state.maxy <- maxy;
631 let gotoy y =
632 let y = max 0 y in
633 let y = min state.maxy y in
634 let pages = layout y conf.winh in
635 let ready = loadlayout pages in
636 if conf.showall
637 then (
638 if ready
639 then (
640 state.y <- y;
641 state.layout <- pages;
642 state.throttle <- None;
643 Glut.postRedisplay ();
645 else (
646 state.throttle <- Some pages;
649 else (
650 state.y <- y;
651 state.layout <- pages;
652 state.throttle <- None;
653 Glut.postRedisplay ();
655 begin match state.mode with
656 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
657 if not (pagevisible pages pageno)
658 then (
659 match state.layout with
660 | [] -> ()
661 | l :: _ ->
662 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
664 | _ -> ()
665 end;
666 preload ();
669 let gotoy_and_clear_text y =
670 gotoy y;
671 if not conf.verbose then state.text <- "";
674 let getanchor () =
675 match state.layout with
676 | [] -> emptyanchor
677 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
680 let getanchory (n, top) =
681 let y, h = getpageyh n in
682 y + (truncate (top *. float h));
685 let gotoanchor anchor =
686 gotoy (getanchory anchor);
689 let addnav () =
690 cbput state.hists.nav (getanchor ());
693 let getnav dir =
694 let anchor = cbgetc state.hists.nav dir in
695 getanchory anchor;
698 let gotopage n top =
699 let y, h = getpageyh n in
700 gotoy_and_clear_text (y + (truncate (top *. float h)));
703 let gotopage1 n top =
704 let y = getpagey n in
705 gotoy_and_clear_text (y + top);
708 let invalidate () =
709 state.layout <- [];
710 state.pdims <- [];
711 state.rects <- [];
712 state.rects1 <- [];
713 state.invalidated <- state.invalidated + 1;
716 let scalecolor c =
717 let c = c *. state.colorscale in
718 (c, c, c);
721 let scalecolor2 (r, g, b) =
722 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
725 let represent () =
726 state.maxy <- calcheight ();
727 match state.mode with
728 | Birdseye (_, _, pageno, _, _) ->
729 let y, h = getpageyh pageno in
730 let top = (conf.winh - h) / 2 in
731 gotoy (max 0 (y - top))
732 | _ -> gotoanchor state.anchor
735 let pagematrix () =
736 GlMat.mode `projection;
737 GlMat.load_identity ();
738 GlMat.rotate ~x:1.0 ~angle:180.0 ();
739 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
740 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
741 if state.x != 0
742 then (
743 GlMat.translate ~x:(float state.x) ();
747 let winmatrix () =
748 GlMat.mode `projection;
749 GlMat.load_identity ();
750 GlMat.rotate ~x:1.0 ~angle:180.0 ();
751 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
752 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
755 let reshape =
756 let firsttime = ref true in
757 fun ~w ~h ->
758 if state.invalidated = 0 && not !firsttime
759 then state.anchor <- getanchor ();
761 firsttime := false;
762 conf.winw <- w;
763 let w = truncate (float w *. conf.zoom) - state.scrollw in
764 let w = max w 2 in
765 state.w <- w;
766 conf.winh <- h;
767 GlMat.mode `modelview;
768 GlMat.load_identity ();
769 GlClear.color (scalecolor 1.0);
770 GlClear.clear [`color];
772 invalidate ();
773 wcmd "geometry" [`i w; `i h];
776 let drawstring size x y s =
777 Gl.enable `blend;
778 Gl.enable `texture_2d;
779 ignore (drawstr size x y s);
780 Gl.disable `blend;
781 Gl.disable `texture_2d;
784 let drawstring1 size x y s =
785 drawstr size x y s;
788 let enttext () =
789 let len = String.length state.text in
790 let drawstring s =
791 GlDraw.color (0.0, 0.0, 0.0);
792 GlDraw.rect
793 (0.0, float (conf.winh - 18))
794 (float (conf.winw - state.scrollw - 1), float conf.winh)
796 GlDraw.color (1.0, 1.0, 1.0);
797 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
799 match state.mode with
800 | Textentry ((prefix, text, _, _, _), _) ->
801 let s =
802 if len > 0
803 then
804 Printf.sprintf "%s%s_ [%s]" prefix text state.text
805 else
806 Printf.sprintf "%s%s_" prefix text
808 drawstring s
810 | _ ->
811 if len > 0 then drawstring state.text
814 let showtext c s =
815 state.text <- Printf.sprintf "%c%s" c s;
816 Glut.postRedisplay ();
819 let act cmd =
820 match cmd.[0] with
821 | 'c' ->
822 state.pdims <- [];
824 | 'D' ->
825 state.rects <- state.rects1;
826 Glut.postRedisplay ()
828 | 'C' ->
829 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
830 state.pagecount <- n;
831 state.invalidated <- state.invalidated - 1;
832 if state.invalidated = 0
833 then represent ()
835 | 't' ->
836 let s = Scanf.sscanf cmd "t %n"
837 (fun n -> String.sub cmd n (String.length cmd - n))
839 Glut.setWindowTitle s
841 | 'T' ->
842 let s = Scanf.sscanf cmd "T %n"
843 (fun n -> String.sub cmd n (String.length cmd - n))
845 if istextentry state.mode
846 then (
847 state.text <- s;
848 showtext ' ' s;
850 else (
851 state.text <- s;
852 Glut.postRedisplay ();
855 | 'V' ->
856 if conf.verbose
857 then
858 let s = Scanf.sscanf cmd "V %n"
859 (fun n -> String.sub cmd n (String.length cmd - n))
861 state.text <- s;
862 showtext ' ' s;
864 | 'F' ->
865 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
866 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
867 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
868 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
870 let y = (getpagey pageno) + truncate y0 in
871 addnav ();
872 gotoy y;
873 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
875 | 'R' ->
876 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
877 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
878 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
879 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
881 state.rects1 <-
882 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
884 | 'r' ->
885 let n, w, _h, r, l, s, p =
886 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
887 (fun n w h r l s p ->
888 (n-1, w, h, r, l != 0, s, p))
891 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
892 state.memused <- state.memused + s;
894 let layout =
895 match state.throttle with
896 | None -> state.layout
897 | Some layout -> layout
900 let rec gc () =
901 if (state.memused <= conf.memlimit) || cbempty state.pagecache
902 then ()
903 else (
904 let evictedopaque = cbpeek state.pagecache in
905 match findpageforopaque evictedopaque with
906 | None -> failwith "bug in gc"
907 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
908 if state.gen != gen || not (pagevisible layout evictedn)
909 then (
910 wcmd "free" [`s evictedopaque];
911 state.memused <- state.memused - evictedsize;
912 Hashtbl.remove state.pagemap k;
913 cbdecr state.pagecache;
914 gc ();
918 gc ();
920 cbput state.pagecache p;
921 state.rendering <- false;
923 begin match state.throttle with
924 | None ->
925 if pagevisible state.layout n
926 then gotoy state.y
927 else (
928 let allvisible = loadlayout state.layout in
929 if allvisible then preload ();
932 | Some layout ->
933 match layout with
934 | [] -> ()
935 | l :: _ ->
936 let y = getpagey l.pageno + l.pagey in
937 gotoy y
940 | 'l' ->
941 let pdim =
942 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
944 state.pdims <- pdim :: state.pdims
946 | 'o' ->
947 let (l, n, t, h, pos) =
948 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
950 let s = String.sub cmd pos (String.length cmd - pos) in
951 let outline = (s, l, (n, float t /. float h)) in
952 let outlines =
953 match state.outlines with
954 | Olist outlines -> Olist (outline :: outlines)
955 | Oarray _ -> Olist [outline]
956 | Onarrow _ -> Olist [outline]
958 state.outlines <- outlines
961 | 'i' ->
962 if String.length cmd > 1 && cmd.[1] = 'e'
963 then
964 state.docinfo <- List.rev state.docinfo
965 else
966 let s = Scanf.sscanf cmd "i %n"
967 (fun n -> String.sub cmd n (String.length cmd - n))
969 state.docinfo <- (1, s) :: state.docinfo
971 | _ ->
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday;;
977 let idle () =
978 if state.deadline == nan then state.deadline <- now ();
979 let rec loop delay =
980 let timeout =
981 if delay > 0.0
982 then max 0.0 (state.deadline -. now ())
983 else 0.0
985 let r, _, _ = Unix.select [state.csock] [] [] timeout in
986 state.deadline <- state.deadline +. delay;
987 begin match r with
988 | [] ->
989 begin match state.autoscroll with
990 | Some step when step != 0 ->
991 let y = state.y + step in
992 let y =
993 if y < 0
994 then state.maxy
995 else if y >= state.maxy then 0 else y
997 gotoy y;
998 if state.mode = View
999 then state.text <- "";
1000 | _ -> ()
1001 end;
1003 | _ ->
1004 let cmd = readcmd state.csock in
1005 act cmd;
1006 loop 0.0
1007 end;
1008 in loop 0.007
1011 let onhist cb =
1012 let rc = cb.rc in
1013 let action = function
1014 | HCprev -> cbget cb ~-1
1015 | HCnext -> cbget cb 1
1016 | HCfirst -> cbget cb ~-(cb.rc)
1017 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1018 and cancel () = cb.rc <- rc
1019 in (action, cancel)
1022 let search pattern forward =
1023 if String.length pattern > 0
1024 then
1025 let pn, py =
1026 match state.layout with
1027 | [] -> 0, 0
1028 | l :: _ ->
1029 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1031 let cmd =
1032 let b = makecmd "search"
1033 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1035 Buffer.add_char b ',';
1036 Buffer.add_string b pattern;
1037 Buffer.add_char b '\000';
1038 Buffer.contents b;
1040 writecmd state.csock cmd;
1043 let intentry text key =
1044 let c = Char.unsafe_chr key in
1045 match c with
1046 | '0' .. '9' ->
1047 let s = "x" in s.[0] <- c;
1048 let text = text ^ s in
1049 TEcont text
1051 | _ ->
1052 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1053 TEcont text
1056 let addchar s c =
1057 let b = Buffer.create (String.length s + 1) in
1058 Buffer.add_string b s;
1059 Buffer.add_char b c;
1060 Buffer.contents b;
1063 let textentry text key =
1064 let c = Char.unsafe_chr key in
1065 match c with
1066 | _ when key >= 32 && key < 127 ->
1067 let text = addchar text c in
1068 TEcont text
1070 | _ ->
1071 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1072 TEcont text
1075 let reinit angle proportional =
1076 conf.angle <- angle;
1077 conf.proportional <- proportional;
1078 invalidate ();
1079 wcmd "reinit" [`i angle; `b proportional];
1082 let setzoom zoom =
1083 let zoom = max 0.01 zoom in
1084 if zoom <> conf.zoom
1085 then (
1086 if zoom <= 1.0
1087 then state.x <- 0;
1088 conf.zoom <- zoom;
1089 reshape conf.winw conf.winh;
1090 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1094 let enterbirdseye () =
1095 let zoom = float conf.thumbw /. float conf.winw in
1096 let birdseyepageno =
1097 let cy = conf.winh / 2 in
1098 let fold = function
1099 | [] -> 0
1100 | l :: rest ->
1101 let rec fold best = function
1102 | [] -> best.pageno
1103 | l :: rest ->
1104 let d = cy - (l.pagedispy + l.pagevh/2)
1105 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1106 if abs d < abs dbest
1107 then fold l rest
1108 else best.pageno
1109 in fold l rest
1111 fold state.layout
1113 state.mode <- Birdseye (
1114 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1116 conf.zoom <- zoom;
1117 conf.presentation <- false;
1118 conf.interpagespace <- 10;
1119 conf.hlinks <- false;
1120 state.x <- 0;
1121 state.mstate <- Mnone;
1122 conf.showall <- false;
1123 Glut.setCursor Glut.CURSOR_INHERIT;
1124 if conf.verbose
1125 then
1126 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1127 (100.0*.zoom)
1128 else
1129 state.text <- ""
1131 reshape conf.winw conf.winh;
1134 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1135 state.mode <- View;
1136 conf.zoom <- c.zoom;
1137 conf.presentation <- c.presentation;
1138 conf.interpagespace <- c.interpagespace;
1139 conf.showall <- c.showall;
1140 conf.hlinks <- c.hlinks;
1141 state.x <- leftx;
1142 if conf.verbose
1143 then
1144 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1145 (100.0*.conf.zoom)
1147 reshape conf.winw conf.winh;
1148 state.anchor <- if goback then anchor else (pageno, 0.0);
1151 let togglebirdseye () =
1152 match state.mode with
1153 | Birdseye vals -> leavebirdseye vals true
1154 | View | Outline _ -> enterbirdseye ()
1155 | _ -> ()
1158 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1159 let pageno = max 0 (pageno - 1) in
1160 let rec loop = function
1161 | [] -> gotopage1 pageno 0
1162 | l :: _ when l.pageno = pageno ->
1163 if l.pagedispy >= 0 && l.pagey = 0
1164 then Glut.postRedisplay ()
1165 else gotopage1 pageno 0
1166 | _ :: rest -> loop rest
1168 loop state.layout;
1169 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1172 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1173 let pageno = min (state.pagecount - 1) (pageno + 1) in
1174 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1175 let rec loop = function
1176 | [] ->
1177 let y, h = getpageyh pageno in
1178 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1179 gotoy (clamp dy)
1180 | l :: _ when l.pageno = pageno ->
1181 if l.pagevh != l.pageh
1182 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1183 else Glut.postRedisplay ()
1184 | _ :: rest -> loop rest
1186 loop state.layout
1189 let optentry mode _ key =
1190 let btos b = if b then "on" else "off" in
1191 let c = Char.unsafe_chr key in
1192 match c with
1193 | 's' ->
1194 let ondone s =
1195 try conf.scrollstep <- int_of_string s with exc ->
1196 state.text <- Printf.sprintf "bad integer `%s': %s"
1197 s (Printexc.to_string exc)
1199 TEswitch ("scroll step: ", "", None, intentry, ondone)
1201 | 'A' ->
1202 let ondone s =
1204 conf.autoscrollstep <- int_of_string s;
1205 if state.autoscroll <> None
1206 then state.autoscroll <- Some conf.autoscrollstep
1207 with exc ->
1208 state.text <- Printf.sprintf "bad integer `%s': %s"
1209 s (Printexc.to_string exc)
1211 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1213 | 'Z' ->
1214 let ondone s =
1216 let zoom = float (int_of_string s) /. 100.0 in
1217 setzoom zoom
1218 with exc ->
1219 state.text <- Printf.sprintf "bad integer `%s': %s"
1220 s (Printexc.to_string exc)
1222 TEswitch ("zoom: ", "", None, intentry, ondone)
1224 | 't' ->
1225 let ondone s =
1227 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1228 state.text <-
1229 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1230 begin match mode with
1231 | Birdseye beye ->
1232 leavebirdseye beye false;
1233 enterbirdseye ();
1234 | _ -> ();
1236 with exc ->
1237 state.text <- Printf.sprintf "bad integer `%s': %s"
1238 s (Printexc.to_string exc)
1240 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1242 | 'R' ->
1243 let ondone s =
1244 match try
1245 Some (int_of_string s)
1246 with exc ->
1247 state.text <- Printf.sprintf "bad integer `%s': %s"
1248 s (Printexc.to_string exc);
1249 None
1250 with
1251 | Some angle -> reinit angle conf.proportional
1252 | None -> ()
1254 TEswitch ("rotation: ", "", None, intentry, ondone)
1256 | 'i' ->
1257 conf.icase <- not conf.icase;
1258 TEdone ("case insensitive search " ^ (btos conf.icase))
1260 | 'p' ->
1261 conf.preload <- not conf.preload;
1262 gotoy state.y;
1263 TEdone ("preload " ^ (btos conf.preload))
1265 | 'v' ->
1266 conf.verbose <- not conf.verbose;
1267 TEdone ("verbose " ^ (btos conf.verbose))
1269 | 'h' ->
1270 conf.maxhfit <- not conf.maxhfit;
1271 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1272 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1274 | 'c' ->
1275 conf.crophack <- not conf.crophack;
1276 TEdone ("crophack " ^ btos conf.crophack)
1278 | 'a' ->
1279 conf.showall <- not conf.showall;
1280 TEdone ("throttle " ^ btos conf.showall)
1282 | 'f' ->
1283 conf.underinfo <- not conf.underinfo;
1284 TEdone ("underinfo " ^ btos conf.underinfo)
1286 | 'P' ->
1287 conf.savebmarks <- not conf.savebmarks;
1288 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1290 | 'S' ->
1291 let ondone s =
1293 let pageno, py =
1294 match state.layout with
1295 | [] -> 0, 0
1296 | l :: _ ->
1297 l.pageno, l.pagey
1299 conf.interpagespace <- int_of_string s;
1300 state.maxy <- calcheight ();
1301 let y = getpagey pageno in
1302 gotoy (y + py)
1303 with exc ->
1304 state.text <- Printf.sprintf "bad integer `%s': %s"
1305 s (Printexc.to_string exc)
1307 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1309 | 'l' ->
1310 reinit conf.angle (not conf.proportional);
1311 TEdone ("proprortional display " ^ btos conf.proportional)
1313 | _ ->
1314 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1315 TEstop
1318 let maxoutlinerows () = (conf.winh - 31) / 16;;
1320 let enterselector allowdel outlines errmsg msg =
1321 if Array.length outlines = 0
1322 then (
1323 showtext ' ' errmsg;
1325 else (
1326 state.text <- msg;
1327 Glut.setCursor Glut.CURSOR_INHERIT;
1328 let pageno =
1329 match state.layout with
1330 | [] -> -1
1331 | {pageno=pageno} :: _ -> pageno
1333 let active =
1334 let rec loop n =
1335 if n = Array.length outlines
1336 then 0
1337 else
1338 let (_, _, (outlinepageno, _)) = outlines.(n) in
1339 if outlinepageno >= pageno then n else loop (n+1)
1341 loop 0
1343 state.mode <- Outline
1344 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1345 state.mode);
1346 Glut.postRedisplay ();
1350 let enteroutlinemode () =
1351 let outlines, msg =
1352 match state.outlines with
1353 | Oarray a -> a, ""
1354 | Olist l ->
1355 let a = Array.of_list (List.rev l) in
1356 state.outlines <- Oarray a;
1357 a, ""
1358 | Onarrow (pat, a, _) ->
1359 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1361 enterselector false outlines "Document has no outline" msg;
1364 let enterbookmarkmode () =
1365 let bookmarks = Array.of_list state.bookmarks in
1366 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1369 let mode_to_string mode =
1370 let b = Buffer.create 10 in
1371 let rec f = function
1372 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1373 | View -> Buffer.add_string b "View"
1374 | Birdseye _ -> Buffer.add_string b "Birdseye"
1375 | Items _ -> Buffer.add_string b "Items"
1376 | Outline _ -> Buffer.add_string b "Outline"
1378 f mode;
1379 Buffer.contents b;
1382 let color_of_string s =
1383 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1384 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1388 let color_to_string (r, g, b) =
1389 let r = truncate (r *. 256.0)
1390 and g = truncate (g *. 256.0)
1391 and b = truncate (b *. 256.0) in
1392 Printf.sprintf "%d/%d/%d" r g b
1395 let enterinfomode () =
1396 let btos = function true -> "on" | _ -> "off" in
1397 let mode = state.mode in
1398 let rec makeitems () =
1399 let intp name get set =
1400 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1401 fun active first _ pan ->
1402 let ondone s =
1403 let n =
1404 try int_of_string s
1405 with exn ->
1406 state.text <- Printf.sprintf "bad integer `%s': %s"
1407 s (Printexc.to_string exn);
1408 max_int;
1410 if n != max_int then set n;
1412 let te = name ^ ": ", "", None, intentry, ondone in
1413 state.text <- "";
1414 Textentry (
1416 fun _ ->
1417 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1420 and boolp name get set =
1421 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1422 fun active first qsearch pan ->
1423 let v = get () in
1424 set (not v);
1425 Items (active, first, makeitems (), qsearch, pan, mode);
1427 and colorp name get set =
1428 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1429 fun active first _ pan ->
1430 let invalid = (nan, nan, nan) in
1431 let ondone s =
1432 let c =
1433 try color_of_string s
1434 with exn ->
1435 state.text <- Printf.sprintf "bad color `%s': %s"
1436 s (Printexc.to_string exn);
1437 invalid
1439 if c <> invalid
1440 then set c;
1442 let te = name ^ ": ", "", None, textentry, ondone in
1443 state.text <- "";
1444 Textentry (
1446 fun _ ->
1447 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1452 let birdseye = isbirdseye mode in
1453 let items = [
1454 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1456 boolp "presentation"
1457 (fun () -> conf.presentation)
1458 (fun v ->
1459 conf.presentation <- v;
1460 state.anchor <- getanchor ();
1461 represent ());
1463 boolp "ignore case in searches"
1464 (fun () -> conf.icase)
1465 (fun v -> conf.icase <- v);
1467 boolp "preload"
1468 (fun () -> conf.preload)
1469 (fun v -> conf.preload <- v);
1471 boolp "verbose"
1472 (fun () -> conf.verbose)
1473 (fun v -> conf.verbose <- v);
1475 boolp "max fit"
1476 (fun () -> conf.maxhfit)
1477 (fun v -> conf.maxhfit <- v);
1479 boolp "crop hack"
1480 (fun () -> conf.crophack)
1481 (fun v -> conf.crophack <- v);
1483 boolp "throttle"
1484 (fun () -> conf.showall)
1485 (fun v -> conf.showall <- v);
1487 boolp "highlight links"
1488 (fun () -> conf.hlinks)
1489 (fun v -> conf.hlinks <- v);
1491 boolp "under info"
1492 (fun () -> conf.underinfo)
1493 (fun v -> conf.underinfo <- v);
1494 boolp "persistent bookmarks"
1495 (fun () -> conf.savebmarks)
1496 (fun v -> conf.savebmarks <- v);
1498 boolp "proportional display"
1499 (fun () -> conf.proportional)
1500 (fun v -> reinit conf.angle v);
1502 boolp "persistent location"
1503 (fun () -> conf.jumpback)
1504 (fun v -> conf.jumpback <- v);
1506 "", 0, Noaction;
1508 intp "vertical margin"
1509 (fun () -> conf.interpagespace)
1510 (fun n ->
1511 conf.interpagespace <- n;
1512 let pageno, py =
1513 match state.layout with
1514 | [] -> 0, 0
1515 | l :: _ ->
1516 l.pageno, l.pagey
1518 state.maxy <- calcheight ();
1519 let y = getpagey pageno in
1520 gotoy (y + py)
1523 intp "page bias"
1524 (fun () -> conf.pagebias)
1525 (fun v -> conf.pagebias <- v);
1527 intp "scroll step"
1528 (fun () -> conf.scrollstep)
1529 (fun n -> conf.scrollstep <- n);
1531 intp "auto scroll step"
1532 (fun () ->
1533 match state.autoscroll with
1534 | Some step -> step
1535 | _ -> conf.autoscrollstep)
1536 (fun n ->
1537 if state.autoscroll <> None
1538 then state.autoscroll <- Some n;
1539 conf.autoscrollstep <- n);
1541 intp "zoom"
1542 (fun () -> truncate (conf.zoom *. 100.))
1543 (fun v -> setzoom ((float v) /. 100.));
1545 intp "rotation"
1546 (fun () -> conf.angle)
1547 (fun v -> reinit v conf.proportional);
1549 intp "scroll bar width"
1550 (fun () -> state.scrollw)
1551 (fun v ->
1552 state.scrollw <- v;
1553 conf.scrollbw <- v;
1554 reshape conf.winw conf.winh;
1557 intp "scroll handle height"
1558 (fun () -> conf.scrollh)
1559 (fun v -> conf.scrollh <- v;);
1561 intp "thumbnail width"
1562 (fun () -> conf.thumbw)
1563 (fun v ->
1564 conf.thumbw <- min 1920 v;
1565 match mode with
1566 | Birdseye beye ->
1567 leavebirdseye beye false;
1568 enterbirdseye ()
1569 | _ -> ()
1572 colorp "background color"
1573 (fun () -> conf.bgcolor)
1574 (fun v -> conf.bgcolor <- v);
1576 "", 0, Noaction;
1577 "Presentation mode", 0, Noaction;
1579 boolp "scrollbar visible"
1580 (fun () -> conf.scrollbarinpm)
1581 (fun v ->
1582 if v != conf.scrollbarinpm
1583 then (
1584 conf.scrollbarinpm <- v;
1585 if conf.presentation
1586 then (
1587 state.scrollw <- if v then conf.scrollbw else 0;
1588 reshape conf.winw conf.winh;
1593 "", 0, Noaction;
1594 "Pixmap Cache", 0, Noaction;
1596 intp "size (advisory)"
1597 (fun () -> conf.memlimit)
1598 (fun v -> conf.memlimit <- v);
1599 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1603 let tailer =
1604 let trailer =
1605 ("", 0, Noaction)
1606 :: ("Document", 0, Noaction)
1607 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1609 if birdseye
1610 then trailer
1611 else (
1612 ("", 0, Noaction)
1613 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1614 (btos conf.bedefault),
1616 Action (
1617 fun active first qsearch pan ->
1618 conf.bedefault <- not conf.bedefault;
1619 Items (active, first, makeitems (), qsearch, pan, mode);
1621 ) :: trailer
1624 Array.of_list (items @ tailer)
1626 state.text <- "";
1627 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1628 Glut.postRedisplay ();
1631 let enterhelpmode () =
1632 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1633 Glut.postRedisplay ();
1636 let quickbookmark ?title () =
1637 match state.layout with
1638 | [] -> ()
1639 | l :: _ ->
1640 let title =
1641 match title with
1642 | None ->
1643 let sec = Unix.gettimeofday () in
1644 let tm = Unix.localtime sec in
1645 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1646 (l.pageno+1)
1647 tm.Unix.tm_mday
1648 tm.Unix.tm_mon
1649 (tm.Unix.tm_year + 1900)
1650 tm.Unix.tm_hour
1651 tm.Unix.tm_min
1652 | Some title -> title
1654 state.bookmarks <-
1655 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1656 :: state.bookmarks
1659 let doreshape w h =
1660 state.fullscreen <- None;
1661 Glut.reshapeWindow w h;
1664 let writeopen path password =
1665 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1668 let opendoc path password =
1669 invalidate ();
1670 state.path <- path;
1671 state.password <- password;
1672 state.gen <- state.gen + 1;
1673 state.docinfo <- [];
1675 writeopen path password;
1676 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1677 wcmd "geometry" [`i state.w; `i conf.winh];
1680 let viewkeyboard key =
1681 let enttext te =
1682 let mode = state.mode in
1683 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1684 state.text <- "";
1685 enttext ();
1686 Glut.postRedisplay ()
1688 let c = Char.chr key in
1689 match c with
1690 | '\027' -> (* escape *)
1691 if String.length state.text > 0
1692 then (
1693 state.text <- "";
1694 Glut.postRedisplay ();
1696 else raise Quit;
1698 | 'q' ->
1699 raise Quit
1701 | '\008' -> (* backspace *)
1702 let y = getnav ~-1 in
1703 gotoy_and_clear_text y
1705 | 'o' ->
1706 enteroutlinemode ()
1708 | 'u' ->
1709 state.rects <- [];
1710 state.text <- "";
1711 Glut.postRedisplay ()
1713 | '/' | '?' ->
1714 let ondone isforw s =
1715 cbput state.hists.pat s;
1716 state.searchpattern <- s;
1717 search s isforw
1719 let s = String.create 1 in
1720 s.[0] <- c;
1721 enttext (s, "", Some (onhist state.hists.pat),
1722 textentry, ondone (c ='/'))
1724 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1725 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1726 setzoom (conf.zoom +. incr)
1728 | '+' ->
1729 let ondone s =
1730 let n =
1731 try int_of_string s with exc ->
1732 state.text <- Printf.sprintf "bad integer `%s': %s"
1733 s (Printexc.to_string exc);
1734 max_int
1736 if n != max_int
1737 then (
1738 conf.pagebias <- n;
1739 state.text <- "page bias is now " ^ string_of_int n;
1742 enttext ("page bias: ", "", None, intentry, ondone)
1744 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1745 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1746 setzoom (max 0.01 (conf.zoom -. decr))
1748 | '-' ->
1749 let ondone msg = state.text <- msg in
1750 enttext (
1751 "option [acfhilpstvAPRSZ]: ", "", None,
1752 optentry state.mode, ondone
1755 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1756 setzoom 1.0
1758 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1759 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1760 if zoom < 1.0
1761 then setzoom zoom
1763 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1764 togglebirdseye ()
1766 | '0' .. '9' ->
1767 let ondone s =
1768 let n =
1769 try int_of_string s with exc ->
1770 state.text <- Printf.sprintf "bad integer `%s': %s"
1771 s (Printexc.to_string exc);
1774 if n >= 0
1775 then (
1776 addnav ();
1777 cbput state.hists.pag (string_of_int n);
1778 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1781 let pageentry text key =
1782 match Char.unsafe_chr key with
1783 | 'g' -> TEdone text
1784 | _ -> intentry text key
1786 let text = "x" in text.[0] <- c;
1787 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1789 | 'b' ->
1790 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1791 reshape conf.winw conf.winh;
1793 | 'l' ->
1794 conf.hlinks <- not conf.hlinks;
1795 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1796 Glut.postRedisplay ()
1798 | 'a' ->
1799 begin match state.autoscroll with
1800 | Some step ->
1801 conf.autoscrollstep <- step;
1802 state.autoscroll <- None
1803 | None ->
1804 if conf.autoscrollstep = 0
1805 then state.autoscroll <- Some 1
1806 else state.autoscroll <- Some conf.autoscrollstep
1809 | 'P' ->
1810 conf.presentation <- not conf.presentation;
1811 if conf.presentation
1812 then (
1813 if not conf.scrollbarinpm
1814 then state.scrollw <- 0;
1816 else
1817 state.scrollw <- conf.scrollbw;
1819 showtext ' ' ("presentation mode " ^
1820 if conf.presentation then "on" else "off");
1821 state.anchor <- getanchor ();
1822 represent ()
1824 | 'f' ->
1825 begin match state.fullscreen with
1826 | None ->
1827 state.fullscreen <- Some (conf.winw, conf.winh);
1828 Glut.fullScreen ()
1829 | Some (w, h) ->
1830 state.fullscreen <- None;
1831 doreshape w h
1834 | 'g' ->
1835 gotoy_and_clear_text 0
1837 | 'n' ->
1838 search state.searchpattern true
1840 | 'p' | 'N' ->
1841 search state.searchpattern false
1843 | 't' ->
1844 begin match state.layout with
1845 | [] -> ()
1846 | l :: _ ->
1847 gotoy_and_clear_text (getpagey l.pageno)
1850 | ' ' ->
1851 begin match List.rev state.layout with
1852 | [] -> ()
1853 | l :: _ ->
1854 let pageno = min (l.pageno+1) (state.pagecount-1) in
1855 gotoy_and_clear_text (getpagey pageno)
1858 | '\127' -> (* del *)
1859 begin match state.layout with
1860 | [] -> ()
1861 | l :: _ ->
1862 let pageno = max 0 (l.pageno-1) in
1863 gotoy_and_clear_text (getpagey pageno)
1866 | '=' ->
1867 let f (fn, _) l =
1868 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1870 let fn, ln = List.fold_left f (-1, -1) state.layout in
1871 let s =
1872 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1873 let percent =
1874 if maxy <= 0
1875 then 100.
1876 else (100. *. (float state.y /. float maxy)) in
1877 if fn = ln
1878 then
1879 Printf.sprintf "Page %d of %d %.2f%%"
1880 (fn+1) state.pagecount percent
1881 else
1882 Printf.sprintf
1883 "Pages %d-%d of %d %.2f%%"
1884 (fn+1) (ln+1) state.pagecount percent
1886 showtext ' ' s;
1888 | 'w' ->
1889 begin match state.layout with
1890 | [] -> ()
1891 | l :: _ ->
1892 doreshape (l.pagew + state.scrollw) l.pageh;
1893 Glut.postRedisplay ();
1896 | '\'' ->
1897 enterbookmarkmode ()
1899 | 'h' ->
1900 enterhelpmode ()
1902 | 'i' ->
1903 enterinfomode ()
1905 | 'm' ->
1906 let ondone s =
1907 match state.layout with
1908 | l :: _ ->
1909 state.bookmarks <-
1910 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1911 :: state.bookmarks
1912 | _ -> ()
1914 enttext ("bookmark: ", "", None, textentry, ondone)
1916 | '~' ->
1917 quickbookmark ();
1918 showtext ' ' "Quick bookmark added";
1920 | 'z' ->
1921 begin match state.layout with
1922 | l :: _ ->
1923 let rect = getpdimrect l.pagedimno in
1924 let w, h =
1925 if conf.crophack
1926 then
1927 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1928 truncate (1.2 *. (rect.(3) -. rect.(0))))
1929 else
1930 (truncate (rect.(1) -. rect.(0)),
1931 truncate (rect.(3) -. rect.(0)))
1933 if w != 0 && h != 0
1934 then
1935 doreshape (w + state.scrollw) (h + conf.interpagespace)
1937 Glut.postRedisplay ();
1939 | [] -> ()
1942 | '\000' -> (* ctrl-2 *)
1943 let maxw = getmaxw () in
1944 if maxw > 0.0
1945 then setzoom (maxw /. float conf.winw)
1947 | '<' | '>' ->
1948 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1950 | '[' | ']' ->
1951 state.colorscale <-
1952 max 0.0
1953 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1954 Glut.postRedisplay ()
1956 | 'k' ->
1957 begin match state.mode with
1958 | Birdseye beye -> upbirdseye beye
1959 | _ -> gotoy (clamp (-conf.scrollstep))
1962 | 'j' ->
1963 begin match state.mode with
1964 | Birdseye beye -> downbirdseye beye
1965 | _ -> gotoy (clamp conf.scrollstep)
1968 | 'r' ->
1969 state.anchor <- getanchor ();
1970 opendoc state.path state.password
1972 | _ ->
1973 vlog "huh? %d %c" key (Char.chr key);
1976 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1977 let enttext te =
1978 state.mode <- Textentry (te, onleave);
1979 state.text <- "";
1980 enttext ();
1981 Glut.postRedisplay ()
1983 match Char.unsafe_chr key with
1984 | '\008' -> (* backspace *)
1985 let len = String.length text in
1986 if len = 0
1987 then (
1988 onleave Cancel;
1989 Glut.postRedisplay ();
1991 else (
1992 let s = String.sub text 0 (len - 1) in
1993 enttext (c, s, opthist, onkey, ondone)
1996 | '\r' | '\n' ->
1997 ondone text;
1998 onleave Confirm;
1999 Glut.postRedisplay ()
2001 | '\027' -> (* escape *)
2002 begin match opthist with
2003 | None -> ()
2004 | Some (_, onhistcancel) -> onhistcancel ()
2005 end;
2006 onleave Cancel;
2007 Glut.postRedisplay ()
2009 | _ ->
2010 begin match onkey text key with
2011 | TEdone text ->
2012 onleave Confirm;
2013 ondone text;
2014 Glut.postRedisplay ()
2016 | TEcont text ->
2017 enttext (c, text, opthist, onkey, ondone);
2019 | TEstop ->
2020 onleave Cancel;
2021 Glut.postRedisplay ()
2023 | TEswitch te ->
2024 state.mode <- Textentry (te, onleave);
2025 Glut.postRedisplay ()
2026 end;
2029 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2030 match key with
2031 | 27 -> (* escape *)
2032 leavebirdseye beye true
2034 | 12 -> (* ctrl-l *)
2035 let y, h = getpageyh pageno in
2036 let top = (conf.winh - h) / 2 in
2037 gotoy (max 0 (y - top))
2039 | 13 -> (* enter *)
2040 leavebirdseye beye false
2042 | _ ->
2043 viewkeyboard key
2046 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2047 let set active first qsearch =
2048 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2050 let search active pattern incr =
2051 let dosearch re =
2052 let rec loop n =
2053 if n >= Array.length items || n < 0
2054 then None
2055 else
2056 let (s, _, _) = items.(n) in
2058 (try ignore (Str.search_forward re s 0); true
2059 with Not_found -> false)
2060 then Some n
2061 else loop (n + incr)
2063 loop active
2066 let re = Str.regexp_case_fold pattern in
2067 dosearch re
2068 with Failure s ->
2069 state.text <- s;
2070 None
2072 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2073 match key with
2074 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2075 let incr = if key = 18 then -1 else 1 in
2076 let active, first =
2077 match search (active + incr) qsearch incr with
2078 | None ->
2079 state.text <- qsearch ^ " [not found]";
2080 active, first
2081 | Some active ->
2082 state.text <- qsearch;
2083 active, firstof active
2085 set active first qsearch;
2086 Glut.postRedisplay ();
2088 | 8 -> (* backspace *)
2089 let len = String.length qsearch in
2090 if len = 0
2091 then ()
2092 else (
2093 if len = 1
2094 then (
2095 state.text <- "";
2096 set active first "";
2098 else
2099 let qsearch = String.sub qsearch 0 (len - 1) in
2100 let active, first =
2101 match search active qsearch ~-1 with
2102 | None ->
2103 state.text <- qsearch ^ " [not found]";
2104 active, first
2105 | Some active ->
2106 state.text <- qsearch;
2107 active, firstof active
2109 set active first qsearch
2111 Glut.postRedisplay ()
2113 | _ when key >= 32 && key < 127 ->
2114 let pattern = addchar qsearch (Char.chr key) in
2115 let active, first =
2116 match search active pattern 1 with
2117 | None ->
2118 state.text <- pattern ^ " [not found]";
2119 active, first
2120 | Some active ->
2121 state.text <- pattern;
2122 active, firstof active
2124 set active first pattern;
2125 Glut.postRedisplay ()
2127 | 27 -> (* escape *)
2128 state.text <- "";
2129 if String.length qsearch = 0
2130 then (
2131 state.mode <- oldmode;
2133 else (
2134 set active first "";
2136 Glut.postRedisplay ()
2138 | 13 -> (* enter *)
2139 if active >= 0 && active < Array.length items
2140 then (
2141 match items.(active) with
2142 | _, _, Action f ->
2143 state.mode <- f active first qsearch pan
2145 | _, _, Noaction ->
2146 state.text <- "";
2147 state.mode <- oldmode
2149 else (
2150 state.text <- "";
2151 state.mode <- oldmode
2153 Glut.postRedisplay ();
2155 | _ -> dolog "unknown key %d" key
2158 let outlinekeyboard key
2159 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2160 let narrow outlines pattern =
2161 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2162 match reopt with
2163 | None -> None
2164 | Some re ->
2165 let rec fold accu n =
2166 if n = -1
2167 then accu
2168 else
2169 let (s, _, _) as o = outlines.(n) in
2170 let accu =
2171 if (try ignore (Str.search_forward re s 0); true
2172 with Not_found -> false)
2173 then (o :: accu)
2174 else accu
2176 fold accu (n-1)
2178 let matched = fold [] (Array.length outlines - 1) in
2179 if matched = [] then None else Some (Array.of_list matched)
2181 let search active pattern incr =
2182 let dosearch re =
2183 let rec loop n =
2184 if n = Array.length outlines || n = -1
2185 then None
2186 else
2187 let (s, _, _) = outlines.(n) in
2189 (try ignore (Str.search_forward re s 0); true
2190 with Not_found -> false)
2191 then Some n
2192 else loop (n + incr)
2194 loop active
2197 let re = Str.regexp_case_fold pattern in
2198 dosearch re
2199 with Failure s ->
2200 state.text <- s;
2201 None
2203 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2204 match key with
2205 | 27 -> (* escape *)
2206 state.text <- "";
2207 if String.length qsearch = 0
2208 then (
2209 state.mode <- oldmode;
2211 else (
2212 state.mode <- Outline (
2213 allowdel, active, first, outlines, "", pan, oldmode
2216 Glut.postRedisplay ();
2218 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2219 let incr = if key = 18 then -1 else 1 in
2220 let active, first =
2221 match search (active + incr) qsearch incr with
2222 | None ->
2223 state.text <- qsearch ^ " [not found]";
2224 active, first
2225 | Some active ->
2226 state.text <- qsearch;
2227 active, firstof active
2229 state.mode <- Outline (
2230 allowdel, active, first, outlines, qsearch, pan, oldmode
2232 Glut.postRedisplay ();
2234 | 8 -> (* backspace *)
2235 let len = String.length qsearch in
2236 if len = 0
2237 then ()
2238 else (
2239 if len = 1
2240 then (
2241 state.text <- "";
2242 state.mode <- Outline (
2243 allowdel, active, first, outlines, "", pan, oldmode
2246 else
2247 let qsearch = String.sub qsearch 0 (len - 1) in
2248 let active, first =
2249 match search active qsearch ~-1 with
2250 | None ->
2251 state.text <- qsearch ^ " [not found]";
2252 active, first
2253 | Some active ->
2254 state.text <- qsearch;
2255 active, firstof active
2257 state.mode <- Outline (
2258 allowdel, active, first, outlines, qsearch, pan, oldmode
2261 Glut.postRedisplay ()
2263 | 13 -> (* enter *)
2264 if active < Array.length outlines
2265 then (
2266 let (_, _, anchor) = outlines.(active) in
2267 addnav ();
2268 gotoanchor anchor;
2270 state.text <- "";
2271 if allowdel then state.bookmarks <- Array.to_list outlines;
2272 state.mode <- oldmode;
2273 Glut.postRedisplay ();
2275 | _ when key >= 32 && key < 127 ->
2276 let pattern = addchar qsearch (Char.chr key) in
2277 let active, first =
2278 match search active pattern 1 with
2279 | None ->
2280 state.text <- pattern ^ " [not found]";
2281 active, first
2282 | Some active ->
2283 state.text <- pattern;
2284 active, firstof active
2286 state.mode <- Outline (
2287 allowdel, active, first, outlines, pattern, pan, oldmode
2289 Glut.postRedisplay ()
2291 | 14 when not allowdel -> (* ctrl-n *)
2292 if String.length qsearch > 0
2293 then (
2294 let optoutlines = narrow outlines qsearch in
2295 begin match optoutlines with
2296 | None -> state.text <- "can't narrow"
2297 | Some outlines ->
2298 state.mode <- Outline (
2299 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2301 match state.outlines with
2302 | Olist _ -> ()
2303 | Oarray a ->
2304 state.outlines <- Onarrow (qsearch, outlines, a)
2305 | Onarrow (_, _, b) ->
2306 state.outlines <- Onarrow (qsearch, outlines, b)
2307 end;
2309 Glut.postRedisplay ()
2311 | 21 when not allowdel -> (* ctrl-u *)
2312 let outline =
2313 match state.outlines with
2314 | Oarray a -> a
2315 | Olist l ->
2316 let a = Array.of_list (List.rev l) in
2317 state.outlines <- Oarray a;
2319 | Onarrow (_, _, b) ->
2320 state.outlines <- Oarray b;
2321 state.text <- "";
2324 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2325 Glut.postRedisplay ()
2327 | 12 -> (* ctrl-l *)
2328 state.mode <- Outline
2329 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2330 Glut.postRedisplay ()
2332 | 127 when allowdel -> (* delete *)
2333 let len = Array.length outlines - 1 in
2334 if len = 0
2335 then (
2336 state.mode <- View;
2337 state.bookmarks <- [];
2339 else (
2340 let bookmarks = Array.init len
2341 (fun i ->
2342 let i = if i >= active then i + 1 else i in
2343 outlines.(i)
2346 state.mode <-
2347 Outline (
2348 allowdel,
2349 min active (len-1),
2350 min first (len-1),
2351 bookmarks, qsearch,
2353 oldmode
2356 Glut.postRedisplay ()
2358 | _ -> dolog "unknown key %d" key
2361 let keyboard ~key ~x ~y =
2362 ignore x;
2363 ignore y;
2364 if key = 7 (* ctrl-g *)
2365 then
2366 wcmd "interrupt" []
2367 else
2368 match state.mode with
2369 | Outline outline -> outlinekeyboard key outline
2370 | Textentry textentry -> textentrykeyboard key textentry
2371 | Birdseye birdseye -> birdseyekeyboard key birdseye
2372 | View -> viewkeyboard key
2373 | Items items -> itemskeyboard key items
2376 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2377 match key with
2378 | Glut.KEY_UP -> upbirdseye beye
2379 | Glut.KEY_DOWN -> downbirdseye beye
2381 | Glut.KEY_PAGE_UP ->
2382 begin match state.layout with
2383 | l :: _ ->
2384 if l.pagey != 0
2385 then (
2386 state.mode <- Birdseye (
2387 conf, leftx, l.pageno, hooverpageno, anchor
2389 gotopage1 l.pageno 0;
2391 else (
2392 let layout = layout (state.y-conf.winh) conf.winh in
2393 match layout with
2394 | [] -> gotoy (clamp (-conf.winh))
2395 | l :: _ ->
2396 state.mode <- Birdseye (
2397 conf, leftx, l.pageno, hooverpageno, anchor
2399 gotopage1 l.pageno 0
2402 | [] -> gotoy (clamp (-conf.winh))
2403 end;
2405 | Glut.KEY_PAGE_DOWN ->
2406 begin match List.rev state.layout with
2407 | l :: _ ->
2408 let layout = layout (state.y + conf.winh) conf.winh in
2409 begin match layout with
2410 | [] ->
2411 let incr = l.pageh - l.pagevh in
2412 if incr = 0
2413 then (
2414 state.mode <-
2415 Birdseye (
2416 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2418 Glut.postRedisplay ();
2420 else gotoy (clamp (incr + conf.interpagespace*2));
2422 | l :: _ ->
2423 state.mode <-
2424 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2425 gotopage1 l.pageno 0;
2428 | [] -> gotoy (clamp conf.winh)
2429 end;
2431 | Glut.KEY_HOME ->
2432 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2433 gotopage1 0 0
2435 | Glut.KEY_END ->
2436 let pageno = state.pagecount - 1 in
2437 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2438 if not (pagevisible state.layout pageno)
2439 then
2440 let h =
2441 match List.rev state.pdims with
2442 | [] -> conf.winh
2443 | (_, _, h, _) :: _ -> h
2445 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2446 else Glut.postRedisplay ();
2447 | _ -> ()
2450 let setautoscrollspeed step goingdown =
2451 let incr = max 1 ((abs step) / 2) in
2452 let incr = if goingdown then incr else -incr in
2453 let astep = step + incr in
2454 state.autoscroll <- Some astep;
2457 let special ~key ~x ~y =
2458 ignore x;
2459 ignore y;
2460 match state.mode with
2461 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2462 togglebirdseye ()
2464 | Birdseye vals ->
2465 birdseyespecial key vals
2467 | View when key = Glut.KEY_F1 ->
2468 enterhelpmode ()
2470 | View ->
2471 begin match state.autoscroll with
2472 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2473 setautoscrollspeed step (key = Glut.KEY_DOWN)
2475 | _ ->
2476 let y =
2477 match key with
2478 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2479 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2480 | Glut.KEY_DOWN -> clamp conf.scrollstep
2481 | Glut.KEY_PAGE_UP ->
2482 if Glut.getModifiers () land Glut.active_ctrl != 0
2483 then
2484 match state.layout with
2485 | [] -> state.y
2486 | l :: _ -> state.y - l.pagey
2487 else
2488 clamp (-conf.winh)
2489 | Glut.KEY_PAGE_DOWN ->
2490 if Glut.getModifiers () land Glut.active_ctrl != 0
2491 then
2492 match List.rev state.layout with
2493 | [] -> state.y
2494 | l :: _ -> getpagey l.pageno
2495 else
2496 clamp conf.winh
2497 | Glut.KEY_HOME -> addnav (); 0
2498 | Glut.KEY_END ->
2499 addnav ();
2500 state.maxy - (if conf.maxhfit then conf.winh else 0)
2502 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2503 Glut.getModifiers () land Glut.active_alt != 0 ->
2504 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2506 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2507 state.x <- state.x - 10;
2508 state.y
2509 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2510 state.x <- state.x + 10;
2511 state.y
2513 | _ -> state.y
2515 gotoy_and_clear_text y
2518 | Textentry
2519 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2520 let s =
2521 match key with
2522 | Glut.KEY_UP -> action HCprev
2523 | Glut.KEY_DOWN -> action HCnext
2524 | Glut.KEY_HOME -> action HCfirst
2525 | Glut.KEY_END -> action HClast
2526 | _ -> state.text
2528 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2529 Glut.postRedisplay ()
2531 | Textentry _ -> ()
2533 | Items (active, first, items, qsearch, pan, oldmode) ->
2534 let maxrows = maxoutlinerows () in
2535 let itemcount = Array.length items in
2536 let hasaction = function
2537 | (_, _, Noaction) -> false
2538 | _ -> true
2540 let find start incr =
2541 let rec find i =
2542 if i = -1 || i = itemcount
2543 then -1
2544 else (
2545 if hasaction items.(i)
2546 then i
2547 else find (i + incr)
2550 find start
2552 let set active first =
2553 let first = max 0 (min first (itemcount - maxrows)) in
2554 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2556 let navigate incr =
2557 let isvisible first n = n >= first && n - first <= maxrows in
2558 let active, first =
2559 let incr1 = if incr > 0 then 1 else -1 in
2560 if isvisible first active
2561 then
2562 let next =
2563 let next = active + incr in
2564 let next =
2565 if next < 0 || next >= itemcount
2566 then -1
2567 else find next incr1
2569 if next = -1 || abs (active - next) > maxrows
2570 then -1
2571 else next
2573 if next = -1
2574 then
2575 let first = first + incr in
2576 let first = max 0 (min first (itemcount - 1)) in
2577 let next =
2578 let next = active + incr in
2579 let next = max 0 (min next (itemcount - 1)) in
2580 find next ~-incr1
2582 let active = if next = -1 then active else next in
2583 active, first
2584 else
2585 let first = min next first in
2586 next, first
2587 else
2588 let first = first + incr in
2589 let first = max 0 (min first (itemcount - 1)) in
2590 let active =
2591 let next = active + incr in
2592 let next = max 0 (min next (itemcount - 1)) in
2593 let next = find next incr1 in
2594 if next = -1 || abs (active - first) > maxrows
2595 then active
2596 else next
2598 active, first
2600 set active first;
2601 Glut.postRedisplay ()
2603 begin match key with
2604 | Glut.KEY_UP -> navigate ~-1
2605 | Glut.KEY_DOWN -> navigate 1
2606 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2607 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2609 | Glut.KEY_RIGHT ->
2610 state.mode <- Items (
2611 active, first, items, qsearch, min 0 (pan - 1), oldmode
2613 Glut.postRedisplay ()
2615 | Glut.KEY_LEFT ->
2616 state.mode <- Items (
2617 active, first, items, qsearch, min 0 (pan + 1), oldmode
2619 Glut.postRedisplay ()
2621 | Glut.KEY_HOME ->
2622 let active = find 0 1 in
2623 set active 0;
2624 Glut.postRedisplay ()
2626 | Glut.KEY_END ->
2627 let first = max 0 (itemcount - maxrows) in
2628 let active = find (itemcount - 1) ~-1 in
2629 set active first;
2630 Glut.postRedisplay ()
2632 | _ -> ()
2633 end;
2635 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2636 let maxrows = maxoutlinerows () in
2637 let calcfirst first active =
2638 if active > first
2639 then
2640 let rows = active - first in
2641 if rows > maxrows then active - maxrows else first
2642 else active
2644 let navigate incr =
2645 let active = active + incr in
2646 let active = max 0 (min active (Array.length outlines - 1)) in
2647 let first = calcfirst first active in
2648 state.mode <- Outline (
2649 allowdel, active, first, outlines, qsearch, pan, oldmode
2651 Glut.postRedisplay ()
2653 let updownlevel incr =
2654 let len = Array.length outlines in
2655 let (_, curlevel, _) = outlines.(active) in
2656 let rec flow i =
2657 if i = len then i-1 else if i = -1 then 0 else
2658 let (_, l, _) = outlines.(i) in
2659 if l != curlevel then i else flow (i+incr)
2661 let active = flow active in
2662 let first = calcfirst first active in
2663 state.mode <- Outline (
2664 allowdel, active, first, outlines, qsearch, pan, oldmode
2666 Glut.postRedisplay ()
2668 match key with
2669 | Glut.KEY_UP -> navigate ~-1
2670 | Glut.KEY_DOWN -> navigate 1
2671 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2672 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2674 | Glut.KEY_RIGHT ->
2675 if Glut.getModifiers () land Glut.active_ctrl != 0
2676 then (
2677 state.mode <- Outline (
2678 allowdel, active, first, outlines,
2679 qsearch, min 0 (pan + 1), oldmode
2681 Glut.postRedisplay ();
2683 else (
2684 if not allowdel
2685 then updownlevel 1
2688 | Glut.KEY_LEFT ->
2689 if Glut.getModifiers () land Glut.active_ctrl != 0
2690 then (
2691 state.mode <- Outline (
2692 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2694 Glut.postRedisplay ();
2696 else (
2697 if not allowdel
2698 then updownlevel ~-1
2701 | Glut.KEY_HOME ->
2702 state.mode <- Outline (
2703 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2705 Glut.postRedisplay ()
2707 | Glut.KEY_END ->
2708 let active = Array.length outlines - 1 in
2709 let first = max 0 (active - maxrows) in
2710 state.mode <- Outline (
2711 allowdel, active, first, outlines, qsearch, pan, oldmode
2713 Glut.postRedisplay ()
2715 | _ -> ()
2718 let drawplaceholder l =
2719 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2720 GlDraw.rect
2721 (float l.pagex, float l.pagedispy)
2722 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2724 let x = if margin < 0 then -margin else l.pagex
2725 and y = l.pagedispy + 13 in
2726 GlDraw.color (0.0, 0.0, 0.0);
2727 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2730 let now () = Unix.gettimeofday ();;
2732 let drawpage l =
2733 let color =
2734 match state.mode with
2735 | Textentry _ -> scalecolor 0.4
2736 | View | Outline _ | Items _ -> scalecolor 1.0
2737 | Birdseye (_, _, pageno, hooverpageno, _) ->
2738 if l.pageno = hooverpageno
2739 then scalecolor 0.9
2740 else (
2741 if l.pageno = pageno
2742 then scalecolor 1.0
2743 else scalecolor 0.8
2746 GlDraw.color color;
2747 begin match getopaque l.pageno with
2748 | Some (opaque, _) when validopaque opaque ->
2749 let a = now () in
2750 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2751 let b = now () in
2752 let d = b-.a in
2753 vlog "draw %d %f sec" l.pageno d;
2755 | _ ->
2756 drawplaceholder l;
2757 end;
2760 let scrollph y =
2761 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2762 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2763 let sh = float conf.winh /. sh in
2764 let sh = max sh (float conf.scrollh) in
2766 let percent =
2767 if state.y = state.maxy
2768 then 1.0
2769 else float y /. float maxy
2771 let position = (float conf.winh -. sh) *. percent in
2773 let position =
2774 if position +. sh > float conf.winh
2775 then float conf.winh -. sh
2776 else position
2778 position, sh;
2781 let scrollindicator () =
2782 GlDraw.color (0.64 , 0.64, 0.64);
2783 GlDraw.rect
2784 (float (conf.winw - state.scrollw), 0.)
2785 (float conf.winw, float conf.winh)
2787 GlDraw.color (0.0, 0.0, 0.0);
2789 let position, sh = scrollph state.y in
2790 GlDraw.rect
2791 (float (conf.winw - state.scrollw), position)
2792 (float conf.winw, position +. sh)
2796 let showsel margin =
2797 match state.mstate with
2798 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2801 | Msel ((x0, y0), (x1, y1)) ->
2802 let rec loop = function
2803 | l :: ls ->
2804 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2805 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2806 then
2807 match getopaque l.pageno with
2808 | Some (opaque, _) when validopaque opaque ->
2809 let oy = -l.pagey + l.pagedispy in
2810 seltext opaque
2811 (x0 - margin - state.x, y0,
2812 x1 - margin - state.x, y1) oy;
2814 | _ -> ()
2815 else loop ls
2816 | [] -> ()
2818 loop state.layout
2821 let showrects () =
2822 let panx = float state.x in
2823 Gl.enable `blend;
2824 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2825 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2826 List.iter
2827 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2828 List.iter (fun l ->
2829 if l.pageno = pageno
2830 then (
2831 let d = float (l.pagedispy - l.pagey) in
2832 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2833 GlDraw.begins `quads;
2835 GlDraw.vertex2 (x0+.panx, y0+.d);
2836 GlDraw.vertex2 (x1+.panx, y1+.d);
2837 GlDraw.vertex2 (x2+.panx, y2+.d);
2838 GlDraw.vertex2 (x3+.panx, y3+.d);
2840 GlDraw.ends ();
2842 ) state.layout
2843 ) state.rects
2845 Gl.disable `blend;
2848 let showstrings trusted active first pan strings =
2849 Gl.enable `blend;
2850 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2851 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2852 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2853 GlDraw.color (1., 1., 1.);
2854 Gl.enable `texture_2d;
2856 let wx = measurestr 15 "w" in
2857 let tabx = 30.0*.wx +. float (pan*15) in
2858 let rec loop row =
2859 if row = Array.length strings || (row - first) * 16 > conf.winh
2860 then ()
2861 else (
2862 let (s, level, _) = strings.(row) in
2863 let y = (row - first) * 16 in
2864 let x = 5 + 15*(max 0 (level+pan)) in
2865 if row = active
2866 then (
2867 Gl.disable `texture_2d;
2868 GlDraw.polygon_mode `both `line;
2869 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2870 GlDraw.rect (0., float (y + 1))
2871 (float (conf.winw - 1), float (y + 18));
2872 GlDraw.polygon_mode `both `fill;
2873 GlDraw.color (1., 1., 1.);
2874 Gl.enable `texture_2d;
2877 let drawtabularstring x s =
2878 let _ =
2879 if trusted
2880 then
2881 let tabpos = try String.index s '\t' with Not_found -> -1 in
2882 if tabpos > 0
2883 then
2884 let len = String.length s - tabpos - 1 in
2885 let s1 = String.sub s 0 tabpos
2886 and s2 = String.sub s (tabpos + 1) len in
2887 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2888 let x = truncate (max xx tabx) in
2889 drawstring1 15 x (y + 16) s2
2890 else
2891 drawstring1 15 x (y + 16) s
2892 else
2893 drawstring1 15 x (y + 16) s
2897 drawtabularstring (x + pan*15) s;
2898 loop (row+1)
2901 loop first;
2902 Gl.disable `blend;
2903 Gl.disable `texture_2d;
2906 let showoutline (_, active, first, outlines, _, pan, _) =
2907 showstrings false active first pan outlines;
2910 let showitems (active, first, items, _, pan, _) =
2911 showstrings true active first pan items;
2914 let display () =
2915 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2916 GlDraw.viewport margin 0 state.w conf.winh;
2917 pagematrix ();
2918 GlClear.color (scalecolor2 conf.bgcolor);
2919 GlClear.clear [`color];
2920 if conf.zoom > 1.0
2921 then (
2922 Gl.enable `scissor_test;
2923 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2925 List.iter drawpage state.layout;
2926 if conf.zoom > 1.0
2927 then
2928 Gl.disable `scissor_test
2930 if state.x != 0
2931 then (
2932 let x = -.float state.x in
2933 GlMat.translate ~x ();
2935 showrects ();
2936 showsel margin;
2937 GlDraw.viewport 0 0 conf.winw conf.winh;
2938 winmatrix ();
2939 scrollindicator ();
2940 begin match state.mode with
2941 | Items items -> showitems items
2942 | Outline outline -> showoutline outline
2943 | _ -> ()
2944 end;
2945 enttext ();
2946 Glut.swapBuffers ();
2949 let getunder x y =
2950 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2951 let x = x - margin - state.x in
2952 let rec f = function
2953 | l :: rest ->
2954 begin match getopaque l.pageno with
2955 | Some (opaque, _) when validopaque opaque ->
2956 let y = y - l.pagedispy in
2957 if y > 0
2958 then
2959 let y = l.pagey + y in
2960 let x = x - l.pagex in
2961 match whatsunder opaque x y with
2962 | Unone -> f rest
2963 | under -> under
2964 else
2965 f rest
2966 | _ ->
2967 f rest
2969 | [] -> Unone
2971 f state.layout
2974 let viewmouse button bstate x y =
2975 match button with
2976 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2977 if Glut.getModifiers () land Glut.active_ctrl != 0
2978 then (
2979 match state.mstate with
2980 | Mzoom (oldn, i) ->
2981 if oldn = n
2982 then (
2983 if i = 2
2984 then
2985 let incr =
2986 match n with
2987 | 4 ->
2988 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2989 | _ ->
2990 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2992 let zoom = conf.zoom +. incr in
2993 setzoom zoom;
2994 state.mstate <- Mzoom (n, 0);
2995 else
2996 state.mstate <- Mzoom (n, i+1);
2998 else state.mstate <- Mzoom (n, 0)
3000 | _ -> state.mstate <- Mzoom (n, 0)
3002 else (
3003 match state.autoscroll with
3004 | Some step -> setautoscrollspeed step (n=4)
3005 | None ->
3006 let incr =
3007 if n = 3
3008 then -conf.scrollstep
3009 else conf.scrollstep
3011 let incr = incr * 2 in
3012 let y = clamp incr in
3013 gotoy_and_clear_text y
3016 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3017 if bstate = Glut.DOWN
3018 then (
3019 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3020 state.mstate <- Mpan (x, y)
3022 else
3023 state.mstate <- Mnone
3025 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3026 if bstate = Glut.DOWN
3027 then
3028 let position, sh = scrollph state.y in
3029 if y > truncate position && y < truncate (position +. sh)
3030 then
3031 state.mstate <- Mscroll
3032 else
3033 let percent = float y /. float conf.winh in
3034 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3035 gotoy desty;
3036 state.mstate <- Mscroll
3037 else
3038 state.mstate <- Mnone
3040 | Glut.LEFT_BUTTON ->
3041 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3042 begin match dest with
3043 | Ulinkgoto (pageno, top) ->
3044 if pageno >= 0
3045 then (
3046 addnav ();
3047 gotopage1 pageno top;
3050 | Ulinkuri s ->
3051 print_endline s
3053 | Unone when bstate = Glut.DOWN ->
3054 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3055 state.mstate <- Mpan (x, y);
3057 | Unone | Utext _ ->
3058 if bstate = Glut.DOWN
3059 then (
3060 if conf.angle mod 360 = 0
3061 then (
3062 state.mstate <- Msel ((x, y), (x, y));
3063 Glut.postRedisplay ()
3066 else (
3067 match state.mstate with
3068 | Mnone -> ()
3070 | Mzoom _ | Mscroll ->
3071 state.mstate <- Mnone
3073 | Mpan _ ->
3074 Glut.setCursor Glut.CURSOR_INHERIT;
3075 state.mstate <- Mnone
3077 | Msel ((_, y0), (_, y1)) ->
3078 let f l =
3079 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3080 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3081 then
3082 match getopaque l.pageno with
3083 | Some (opaque, _) when validopaque opaque ->
3084 copysel opaque
3085 | _ -> ()
3087 List.iter f state.layout;
3088 copysel ""; (* ugly *)
3089 Glut.setCursor Glut.CURSOR_INHERIT;
3090 state.mstate <- Mnone;
3094 | _ -> ()
3097 let birdseyemouse button bstate x y
3098 (conf, leftx, _, hooverpageno, anchor) =
3099 match button with
3100 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3101 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3102 let rec loop = function
3103 | [] -> ()
3104 | l :: rest ->
3105 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3106 && x > margin && x < margin + l.pagew
3107 then (
3108 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3110 else loop rest
3112 loop state.layout
3113 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3114 | _ -> ()
3117 let mouse bstate button x y =
3118 match state.mode with
3119 | View -> viewmouse button bstate x y
3120 | Birdseye beye -> birdseyemouse button bstate x y beye
3121 | Textentry _ | Outline _ | Items _ -> ()
3124 let mouse ~button ~state ~x ~y = mouse state button x y;;
3126 let motion ~x ~y =
3127 match state.mode with
3128 | Outline _ -> ()
3129 | _ ->
3130 match state.mstate with
3131 | Mzoom _ | Mnone -> ()
3133 | Mpan (x0, y0) ->
3134 let dx = x - x0
3135 and dy = y0 - y in
3136 state.mstate <- Mpan (x, y);
3137 if conf.zoom > 1.0 then state.x <- state.x + dx;
3138 let y = clamp dy in
3139 gotoy_and_clear_text y
3141 | Msel (a, _) ->
3142 state.mstate <- Msel (a, (x, y));
3143 Glut.postRedisplay ()
3145 | Mscroll ->
3146 let y = min conf.winh (max 0 y) in
3147 let percent = float y /. float conf.winh in
3148 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3149 gotoy_and_clear_text y
3152 let pmotion ~x ~y =
3153 match state.mode with
3154 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3155 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3156 let rec loop = function
3157 | [] ->
3158 if hooverpageno != -1
3159 then (
3160 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3161 Glut.postRedisplay ();
3163 | l :: rest ->
3164 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3165 && x > margin && x < margin + l.pagew
3166 then (
3167 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3168 Glut.postRedisplay ();
3170 else loop rest
3172 loop state.layout
3174 | Outline _ | Items _ | Textentry _ -> ()
3175 | View ->
3176 match state.mstate with
3177 | Mnone ->
3178 begin match getunder x y with
3179 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3180 | Ulinkuri uri ->
3181 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3182 Glut.setCursor Glut.CURSOR_INFO
3183 | Ulinkgoto (page, _) ->
3184 if conf.underinfo
3185 then showtext 'p' ("age: " ^ string_of_int page);
3186 Glut.setCursor Glut.CURSOR_INFO
3187 | Utext s ->
3188 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3189 Glut.setCursor Glut.CURSOR_TEXT
3192 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3197 module State =
3198 struct
3199 open Parser
3201 let fontpath = ref "";;
3203 let home =
3205 match Sys.os_type with
3206 | "Win32" -> Sys.getenv "HOMEPATH"
3207 | _ -> Sys.getenv "HOME"
3208 with exn ->
3209 prerr_endline
3210 ("Can not determine home directory location: " ^
3211 Printexc.to_string exn);
3215 let config_of c attrs =
3216 let apply c k v =
3218 match k with
3219 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3220 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3221 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3222 | "preload" -> { c with preload = bool_of_string v }
3223 | "page-bias" -> { c with pagebias = int_of_string v }
3224 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3225 | "auto-scroll-step" ->
3226 { c with autoscrollstep = max 0 (int_of_string v) }
3227 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3228 | "crop-hack" -> { c with crophack = bool_of_string v }
3229 | "throttle" -> { c with showall = bool_of_string v }
3230 | "highlight-links" -> { c with hlinks = bool_of_string v }
3231 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3232 | "vertical-margin" ->
3233 { c with interpagespace = max 0 (int_of_string v) }
3234 | "zoom" ->
3235 let zoom = float_of_string v /. 100. in
3236 let zoom = max 0.01 zoom in
3237 { c with zoom = zoom }
3238 | "presentation" -> { c with presentation = bool_of_string v }
3239 | "rotation-angle" -> { c with angle = int_of_string v }
3240 | "width" -> { c with winw = max 20 (int_of_string v) }
3241 | "height" -> { c with winh = max 20 (int_of_string v) }
3242 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3243 | "proportional-display" -> { c with proportional = bool_of_string v }
3244 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3245 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3246 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3247 | "block-width" -> { c with blockwidth = max 2 (int_of_string v) }
3248 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3249 | "persistent-location" -> { c with jumpback = bool_of_string v }
3250 | "background-color" -> { c with bgcolor = color_of_string v }
3251 | "scrollbar-in-presentation" ->
3252 { c with scrollbarinpm = bool_of_string v }
3253 | _ -> c
3254 with exn ->
3255 prerr_endline ("Error processing attribute (`" ^
3256 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3259 let rec fold c = function
3260 | [] -> c
3261 | (k, v) :: rest ->
3262 let c = apply c k v in
3263 fold c rest
3265 fold c attrs;
3268 let fromstring f pos n v d =
3269 try f v
3270 with exn ->
3271 dolog "Error processing attribute (%S=%S) at %d\n%s"
3272 n v pos (Printexc.to_string exn)
3277 let bookmark_of attrs =
3278 let rec fold title page rely = function
3279 | ("title", v) :: rest -> fold v page rely rest
3280 | ("page", v) :: rest -> fold title v rely rest
3281 | ("rely", v) :: rest -> fold title page v rest
3282 | _ :: rest -> fold title page rely rest
3283 | [] -> title, page, rely
3285 fold "invalid" "0" "0" attrs
3288 let doc_of attrs =
3289 let rec fold path page rely pan = function
3290 | ("path", v) :: rest -> fold v page rely pan rest
3291 | ("page", v) :: rest -> fold path v rely pan rest
3292 | ("rely", v) :: rest -> fold path page v pan rest
3293 | ("pan", v) :: rest -> fold path page rely v rest
3294 | _ :: rest -> fold path page rely pan rest
3295 | [] -> path, page, rely, pan
3297 fold "" "0" "0" "0" attrs
3300 let setconf dst src =
3301 dst.scrollbw <- src.scrollbw;
3302 dst.scrollh <- src.scrollh;
3303 dst.icase <- src.icase;
3304 dst.preload <- src.preload;
3305 dst.pagebias <- src.pagebias;
3306 dst.verbose <- src.verbose;
3307 dst.scrollstep <- src.scrollstep;
3308 dst.maxhfit <- src.maxhfit;
3309 dst.crophack <- src.crophack;
3310 dst.autoscrollstep <- src.autoscrollstep;
3311 dst.showall <- src.showall;
3312 dst.hlinks <- src.hlinks;
3313 dst.underinfo <- src.underinfo;
3314 dst.interpagespace <- src.interpagespace;
3315 dst.zoom <- src.zoom;
3316 dst.presentation <- src.presentation;
3317 dst.angle <- src.angle;
3318 dst.winw <- src.winw;
3319 dst.winh <- src.winh;
3320 dst.savebmarks <- src.savebmarks;
3321 dst.memlimit <- src.memlimit;
3322 dst.proportional <- src.proportional;
3323 dst.texcount <- src.texcount;
3324 dst.sliceheight <- src.sliceheight;
3325 dst.blockwidth <- src.blockwidth;
3326 dst.thumbw <- src.thumbw;
3327 dst.jumpback <- src.jumpback;
3328 dst.bgcolor <- src.bgcolor;
3329 dst.scrollbarinpm <- src.scrollbarinpm;
3332 let unent s =
3333 let l = String.length s in
3334 let b = Buffer.create l in
3335 unent b s 0 l;
3336 Buffer.contents b;
3339 let get s =
3340 let h = Hashtbl.create 10 in
3341 let dc = { defconf with angle = defconf.angle } in
3342 let rec toplevel v t spos _ =
3343 match t with
3344 | Vdata | Vcdata | Vend -> v
3345 | Vopen ("llppconfig", _, closed) ->
3346 if closed
3347 then v
3348 else { v with f = llppconfig }
3349 | Vopen _ ->
3350 error "unexpected subelement at top level" s spos
3351 | Vclose _ -> error "unexpected close at top level" s spos
3353 and llppconfig v t spos _ =
3354 match t with
3355 | Vdata | Vcdata -> v
3356 | Vend -> error "unexpected end of input in llppconfig" s spos
3357 | Vopen ("defaults", attrs, closed) ->
3358 let c = config_of dc attrs in
3359 setconf dc c;
3360 if closed
3361 then v
3362 else { v with f = skip "defaults" (fun () -> v) }
3364 | Vopen ("ui-font", _, closed) ->
3365 if closed
3366 then v
3367 else { v with f = uifont (Buffer.create 10) }
3369 | Vopen ("doc", attrs, closed) ->
3370 let pathent, spage, srely, span = doc_of attrs in
3371 let path = unent pathent
3372 and pageno = fromstring int_of_string spos "page" spage 0
3373 and rely = fromstring float_of_string spos "rely" srely 0.0
3374 and pan = fromstring int_of_string spos "pan" span 0 in
3375 let c = config_of dc attrs in
3376 let anchor = (pageno, rely) in
3377 if closed
3378 then (Hashtbl.add h path (c, [], pan, anchor); v)
3379 else { v with f = doc path pan anchor c [] }
3381 | Vopen _ ->
3382 error "unexpected subelement in llppconfig" s spos
3384 | Vclose "llppconfig" -> { v with f = toplevel }
3385 | Vclose _ -> error "unexpected close in llppconfig" s spos
3387 and uifont b v t spos epos =
3388 match t with
3389 | Vdata | Vcdata ->
3390 Buffer.add_substring b s spos (epos - spos);
3392 | Vopen (_, _, _) ->
3393 error "unexpected subelement in ui-font" s spos
3394 | Vclose "ui-font" ->
3395 if String.length !fontpath = 0
3396 then fontpath := Buffer.contents b;
3397 { v with f = llppconfig }
3398 | Vclose _ -> error "unexpected close in ui-font" s spos
3399 | Vend -> error "unexpected end of input in ui-font" s spos
3401 and doc path pan anchor c bookmarks v t spos _ =
3402 match t with
3403 | Vdata | Vcdata -> v
3404 | Vend -> error "unexpected end of input in doc" s spos
3405 | Vopen ("bookmarks", _, closed) ->
3406 if closed
3407 then v
3408 else { v with f = pbookmarks path pan anchor c bookmarks }
3410 | Vopen (_, _, _) ->
3411 error "unexpected subelement in doc" s spos
3413 | Vclose "doc" ->
3414 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3415 { v with f = llppconfig }
3417 | Vclose _ -> error "unexpected close in doc" s spos
3419 and pbookmarks path pan anchor c bookmarks v t spos _ =
3420 match t with
3421 | Vdata | Vcdata -> v
3422 | Vend -> error "unexpected end of input in bookmarks" s spos
3423 | Vopen ("item", attrs, closed) ->
3424 let titleent, spage, srely = bookmark_of attrs in
3425 let page = fromstring int_of_string spos "page" spage 0
3426 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3427 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3428 if closed
3429 then { v with f = pbookmarks path pan anchor c bookmarks }
3430 else
3431 let f () = v in
3432 { v with f = skip "item" f }
3434 | Vopen _ ->
3435 error "unexpected subelement in bookmarks" s spos
3437 | Vclose "bookmarks" ->
3438 { v with f = doc path pan anchor c bookmarks }
3440 | Vclose _ -> error "unexpected close in bookmarks" s spos
3442 and skip tag f v t spos _ =
3443 match t with
3444 | Vdata | Vcdata -> v
3445 | Vend ->
3446 error ("unexpected end of input in skipped " ^ tag) s spos
3447 | Vopen (tag', _, closed) ->
3448 if closed
3449 then v
3450 else
3451 let f' () = { v with f = skip tag f } in
3452 { v with f = skip tag' f' }
3453 | Vclose ctag ->
3454 if tag = ctag
3455 then f ()
3456 else error ("unexpected close in skipped " ^ tag) s spos
3459 parse { f = toplevel; accu = () } s;
3460 h, dc;
3463 let do_load f ic =
3465 let len = in_channel_length ic in
3466 let s = String.create len in
3467 really_input ic s 0 len;
3468 f s;
3469 with
3470 | Parse_error (msg, s, pos) ->
3471 let subs = subs s pos in
3472 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3473 failwith ("parse error: " ^ s)
3475 | exn ->
3476 failwith ("config load error: " ^ Printexc.to_string exn)
3479 let defconfpath =
3480 let dir =
3482 let dir = Filename.concat home ".config" in
3483 if Sys.is_directory dir then dir else home
3484 with _ -> home
3486 Filename.concat dir "llpp.conf"
3489 let confpath = ref defconfpath;;
3491 let load1 f =
3492 if Sys.file_exists !confpath
3493 then
3494 match
3495 (try Some (open_in_bin !confpath)
3496 with exn ->
3497 prerr_endline
3498 ("Error opening configuation file `" ^ !confpath ^ "': " ^
3499 Printexc.to_string exn);
3500 None
3502 with
3503 | Some ic ->
3504 begin try
3505 f (do_load get ic)
3506 with exn ->
3507 prerr_endline
3508 ("Error loading configuation from `" ^ !confpath ^ "': " ^
3509 Printexc.to_string exn);
3510 end;
3511 close_in ic;
3513 | None -> ()
3514 else
3515 f (Hashtbl.create 0, defconf)
3518 let load () =
3519 let f (h, dc) =
3520 let pc, pb, px, pa =
3522 Hashtbl.find h (Filename.basename state.path)
3523 with Not_found -> dc, [], 0, (0, 0.0)
3525 setconf defconf dc;
3526 setconf conf pc;
3527 state.bookmarks <- pb;
3528 state.x <- px;
3529 state.scrollw <- conf.scrollbw;
3530 if conf.jumpback
3531 then state.anchor <- pa;
3532 cbput state.hists.nav pa;
3534 load1 f
3537 let add_attrs bb always dc c =
3538 let ob s a b =
3539 if always || a != b
3540 then Printf.bprintf bb "\n %s='%b'" s a
3541 and oi s a b =
3542 if always || a != b
3543 then Printf.bprintf bb "\n %s='%d'" s a
3544 and oz s a b =
3545 if always || a <> b
3546 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3547 and oc s a b =
3548 if always || a <> b
3549 then
3550 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3552 let w, h =
3553 if always
3554 then dc.winw, dc.winh
3555 else
3556 match state.fullscreen with
3557 | Some wh -> wh
3558 | None -> c.winw, c.winh
3560 let zoom, presentation, interpagespace, showall=
3561 if always
3562 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3563 else
3564 match state.mode with
3565 | Birdseye (bc, _, _, _, _) ->
3566 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3567 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3569 oi "width" w dc.winw;
3570 oi "height" h dc.winh;
3571 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3572 oi "scroll-handle-height" c.scrollh dc.scrollh;
3573 ob "case-insensitive-search" c.icase dc.icase;
3574 ob "preload" c.preload dc.preload;
3575 oi "page-bias" c.pagebias dc.pagebias;
3576 oi "scroll-step" c.scrollstep dc.scrollstep;
3577 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3578 ob "max-height-fit" c.maxhfit dc.maxhfit;
3579 ob "crop-hack" c.crophack dc.crophack;
3580 ob "throttle" showall dc.showall;
3581 ob "highlight-links" c.hlinks dc.hlinks;
3582 ob "under-cursor-info" c.underinfo dc.underinfo;
3583 oi "vertical-margin" interpagespace dc.interpagespace;
3584 oz "zoom" zoom dc.zoom;
3585 ob "presentation" presentation dc.presentation;
3586 oi "rotation-angle" c.angle dc.angle;
3587 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3588 ob "proportional-display" c.proportional dc.proportional;
3589 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3590 oi "tex-count" c.texcount dc.texcount;
3591 oi "slice-height" c.sliceheight dc.sliceheight;
3592 oi "block-width" c.blockwidth dc.blockwidth;
3593 oi "thumbnail-width" c.thumbw dc.thumbw;
3594 ob "persistent-location" c.jumpback dc.jumpback;
3595 oc "background-color" c.bgcolor dc.bgcolor;
3596 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3599 let save () =
3600 let bb = Buffer.create 32768 in
3601 let f (h, dc) =
3602 let dc = if conf.bedefault then conf else dc in
3603 Buffer.add_string bb "<llppconfig>\n";
3605 if String.length !fontpath > 0
3606 then Printf.bprintf bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3608 Buffer.add_string bb "<defaults ";
3609 add_attrs bb true dc dc;
3610 Buffer.add_string bb "/>\n";
3612 let adddoc path pan anchor c bookmarks =
3613 if bookmarks == [] && c = dc && anchor = emptyanchor
3614 then ()
3615 else (
3616 Printf.bprintf bb "<doc path='%s'"
3617 (enent path 0 (String.length path));
3619 if anchor <> emptyanchor
3620 then (
3621 let n, y = anchor in
3622 Printf.bprintf bb " page='%d'" n;
3623 if y > 1e-6
3624 then
3625 Printf.bprintf bb " rely='%f'" y
3629 if pan != 0
3630 then Printf.bprintf bb " pan='%d'" pan;
3632 add_attrs bb false dc c;
3634 begin match bookmarks with
3635 | [] -> Buffer.add_string bb "/>\n"
3636 | _ ->
3637 Buffer.add_string bb ">\n<bookmarks>\n";
3638 List.iter (fun (title, _level, (page, rely)) ->
3639 Printf.bprintf bb
3640 "<item title='%s' page='%d'"
3641 (enent title 0 (String.length title))
3642 page
3644 if rely > 1e-6
3645 then
3646 Printf.bprintf bb " rely='%f'" rely
3648 Buffer.add_string bb "/>\n";
3649 ) bookmarks;
3650 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3651 end;
3655 let pan =
3656 match state.mode with
3657 | Birdseye (_, pan, _, _, _) -> pan
3658 | _ -> state.x
3660 let basename = Filename.basename state.path in
3661 adddoc basename pan (getanchor ())
3662 { conf with
3663 autoscrollstep =
3664 match state.autoscroll with
3665 | Some step -> step
3666 | None -> conf.autoscrollstep }
3667 (if conf.savebmarks then state.bookmarks else []);
3669 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3670 if basename <> path
3671 then adddoc path x y c bookmarks
3672 ) h;
3673 Buffer.add_string bb "</llppconfig>";
3675 load1 f;
3676 if Buffer.length bb > 0
3677 then
3679 let tmp = !confpath ^ ".tmp" in
3680 let oc = open_out_bin tmp in
3681 Buffer.output_buffer oc bb;
3682 close_out oc;
3683 Sys.rename tmp !confpath;
3684 with exn ->
3685 prerr_endline
3686 ("error while saving configuration: " ^ Printexc.to_string exn)
3688 end;;
3690 let () =
3691 Arg.parse
3692 (Arg.align
3693 [("-p", Arg.String (fun s -> state.password <- s) ,
3694 "<password> Set password");
3696 ("-f", Arg.String (fun s -> State.fontpath := s),
3697 "<path> Set path to the user interface font");
3699 ("-c", Arg.String (fun s -> State.confpath := s),
3700 "<path> Set path to the configuration file");
3702 ("-v", Arg.Unit (fun () ->
3703 Printf.printf
3704 "%s\nconfiguration path: %s\n"
3705 Help.version
3706 State.defconfpath
3708 exit 0), " Print version and exit");
3711 (fun s -> state.path <- s)
3712 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3714 if String.length state.path = 0
3715 then (prerr_endline "file name missing"; exit 1);
3717 State.load ();
3719 let _ = Glut.init Sys.argv in
3720 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3721 let () = Glut.initWindowSize conf.winw conf.winh in
3722 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3724 let csock, ssock =
3725 if Sys.os_type = "Unix"
3726 then
3727 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3728 else
3729 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3730 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3731 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3732 Unix.bind sock addr;
3733 Unix.listen sock 1;
3734 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3735 Unix.connect csock addr;
3736 let ssock, _ = Unix.accept sock in
3737 Unix.close sock;
3738 let opts sock =
3739 Unix.setsockopt sock Unix.TCP_NODELAY true;
3740 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3742 opts ssock;
3743 opts csock;
3744 ssock, csock
3747 let () = Glut.displayFunc display in
3748 let () = Glut.reshapeFunc reshape in
3749 let () = Glut.keyboardFunc keyboard in
3750 let () = Glut.specialFunc special in
3751 let () = Glut.idleFunc (Some idle) in
3752 let () = Glut.mouseFunc mouse in
3753 let () = Glut.motionFunc motion in
3754 let () = Glut.passiveMotionFunc pmotion in
3756 init ssock (
3757 conf.angle, conf.proportional, conf.texcount,
3758 conf.sliceheight, conf.blockwidth, !State.fontpath
3760 state.csock <- csock;
3761 state.ssock <- ssock;
3762 state.text <- "Opening " ^ state.path;
3763 writeopen state.path state.password;
3765 let rec handlelablglutbug () =
3767 Glut.mainLoop ();
3768 with
3769 | Glut.BadEnum "key in special_of_int" ->
3770 showtext '!' " LablGlut bug: special key not recognized";
3771 handlelablglutbug ()
3773 | Quit->
3774 if Sys.os_type <> "Unix"
3775 then Unix.shutdown ssock Unix.SHUTDOWN_ALL;
3776 State.save ()
3778 handlelablglutbug ();