Make maximal texture width user configurable
[llpp.git] / main.ml
blob6a0d321868358992d19b74f30b384f3a5c8b2bf1
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params =
11 angle * proportional * texcount * sliceheight * blockwidth * fontpath
12 and pageno = int
13 and width = int
14 and height = int
15 and leftx = int
16 and opaque = string
17 and recttype = int
18 and pixmapsize = int
19 and angle = int
20 and proportional = bool
21 and interpagespace = int
22 and texcount = int
23 and sliceheight = int
24 and blockwidth = int
25 and gen = int
26 and top = float
27 and fontpath = string
30 external init : Unix.file_descr -> params -> unit = "ml_init";;
31 external draw : (int * int * int * bool) -> string -> unit = "ml_draw";;
32 external seltext : string -> (int * int * int * int) -> int -> unit =
33 "ml_seltext";;
34 external copysel : string -> unit = "ml_copysel";;
35 external getpdimrect : int -> float array = "ml_getpdimrect";;
36 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
37 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
38 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
39 external measurestr : int -> string -> float = "ml_measure_string";;
41 type mpos = int * int
42 and mstate =
43 | Msel of (mpos * mpos)
44 | Mpan of mpos
45 | Mscroll
46 | Mzoom of (int * int)
47 | Mnone
50 type textentry = string * string * onhist option * onkey * ondone
51 and onkey = string -> int -> te
52 and ondone = string -> unit
53 and histcancel = unit -> unit
54 and onhist = ((histcmd -> string) * histcancel)
55 and histcmd = HCnext | HCprev | HCfirst | HClast
56 and te =
57 | TEstop
58 | TEdone of string
59 | TEcont of string
60 | TEswitch of textentry
63 type 'a circbuf =
64 { store : 'a array
65 ; mutable rc : int
66 ; mutable wc : int
67 ; mutable len : int
71 let cbnew n v =
72 { store = Array.create n v
73 ; rc = 0
74 ; wc = 0
75 ; len = 0
79 let cbcap b = Array.length b.store;;
81 let cbput b v =
82 let cap = cbcap b in
83 b.store.(b.wc) <- v;
84 b.wc <- (b.wc + 1) mod cap;
85 b.rc <- b.wc;
86 b.len <- min (b.len + 1) cap;
89 let cbempty b = b.len = 0;;
91 let cbgetg b circular dir =
92 if cbempty b
93 then b.store.(0)
94 else
95 let rc = b.rc + dir in
96 let rc =
97 if circular
98 then (
99 if rc = -1
100 then b.len-1
101 else (
102 if rc = b.len
103 then 0
104 else rc
107 else max 0 (min rc (b.len-1))
109 b.rc <- rc;
110 b.store.(rc);
113 let cbget b = cbgetg b false;;
114 let cbgetc b = cbgetg b true;;
116 let cbpeek b =
117 let rc = b.wc - b.len in
118 let rc = if rc < 0 then cbcap b + rc else rc in
119 b.store.(rc);
122 let cbdecr b = b.len <- b.len - 1;;
124 type layout =
125 { pageno : int
126 ; pagedimno : int
127 ; pagew : int
128 ; pageh : int
129 ; pagedispy : int
130 ; pagey : int
131 ; pagevh : int
132 ; pagex : int
136 type conf =
137 { mutable scrollbw : int
138 ; mutable scrollh : int
139 ; mutable icase : bool
140 ; mutable preload : bool
141 ; mutable pagebias : int
142 ; mutable verbose : bool
143 ; mutable scrollstep : int
144 ; mutable maxhfit : bool
145 ; mutable crophack : bool
146 ; mutable autoscrollstep : int
147 ; mutable showall : bool
148 ; mutable hlinks : bool
149 ; mutable underinfo : bool
150 ; mutable interpagespace : interpagespace
151 ; mutable zoom : float
152 ; mutable presentation : bool
153 ; mutable angle : angle
154 ; mutable winw : int
155 ; mutable winh : int
156 ; mutable savebmarks : bool
157 ; mutable proportional : proportional
158 ; mutable memlimit : int
159 ; mutable texcount : texcount
160 ; mutable sliceheight : sliceheight
161 ; mutable blockwidth : blockwidth
162 ; mutable thumbw : width
163 ; mutable jumpback : bool
164 ; mutable bgcolor : float * float * float
165 ; mutable bedefault : bool
166 ; mutable scrollbarinpm : bool
167 ; mutable uifont : string
171 type anchor = pageno * top;;
173 type outline = string * int * anchor
174 and outlines =
175 | Oarray of outline array
176 | Olist of outline list
177 | Onarrow of string * outline array * outline array
180 type rect = float * float * float * float * float * float * float * float;;
182 type pagemapkey = pageno * width * angle * proportional * gen;;
184 let emptyanchor = (0, 0.0);;
186 type mode =
187 | Birdseye of (conf * leftx * pageno * pageno * anchor)
188 | Outline of (bool * int * int * outline array * string * int * mode)
189 | Items of (int * int * item array * string * int * mode)
190 | Textentry of (textentry * onleave)
191 | View
192 and onleave = leavetextentrystatus -> unit
193 and leavetextentrystatus = | Cancel | Confirm
194 and item = string * int * action
195 and action =
196 | Noaction
197 | Action of (int -> int -> string -> int -> mode)
200 let isbirdseye = function Birdseye _ -> true | _ -> false;;
201 let istextentry = function Textentry _ -> true | _ -> false;;
203 type state =
204 { mutable csock : Unix.file_descr
205 ; mutable ssock : Unix.file_descr
206 ; mutable w : int
207 ; mutable x : int
208 ; mutable y : int
209 ; mutable scrollw : int
210 ; mutable anchor : anchor
211 ; mutable maxy : int
212 ; mutable layout : layout list
213 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
214 ; mutable pdims : (pageno * width * height * leftx) list
215 ; mutable pagecount : int
216 ; pagecache : string circbuf
217 ; mutable rendering : bool
218 ; mutable mstate : mstate
219 ; mutable searchpattern : string
220 ; mutable rects : (pageno * recttype * rect) list
221 ; mutable rects1 : (pageno * recttype * rect) list
222 ; mutable text : string
223 ; mutable fullscreen : (width * height) option
224 ; mutable mode : mode
225 ; mutable outlines : outlines
226 ; mutable bookmarks : outline list
227 ; mutable path : string
228 ; mutable password : string
229 ; mutable invalidated : int
230 ; mutable colorscale : float
231 ; mutable memused : int
232 ; mutable gen : gen
233 ; mutable throttle : layout list option
234 ; mutable autoscroll :int option
235 ; mutable help : item array
236 ; mutable docinfo : (int * string) list
237 ; mutable deadline : float
238 ; hists : hists
240 and hists =
241 { pat : string circbuf
242 ; pag : string circbuf
243 ; nav : anchor circbuf
247 let defconf =
248 { scrollbw = 7
249 ; scrollh = 12
250 ; icase = true
251 ; preload = true
252 ; pagebias = 0
253 ; verbose = false
254 ; scrollstep = 24
255 ; maxhfit = true
256 ; crophack = false
257 ; autoscrollstep = 24
258 ; showall = false
259 ; hlinks = false
260 ; underinfo = false
261 ; interpagespace = 2
262 ; zoom = 1.0
263 ; presentation = false
264 ; angle = 0
265 ; winw = 900
266 ; winh = 900
267 ; savebmarks = true
268 ; proportional = true
269 ; memlimit = 32*1024*1024
270 ; texcount = 256
271 ; sliceheight = 24
272 ; blockwidth = 2048
273 ; thumbw = 76
274 ; jumpback = false
275 ; bgcolor = (0.5, 0.5, 0.5)
276 ; bedefault = false
277 ; scrollbarinpm = true
278 ; uifont = ""
282 let conf = { defconf with angle = defconf.angle };;
284 let makehelp () =
285 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
286 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
289 let state =
290 { csock = Unix.stdin
291 ; ssock = Unix.stdin
292 ; x = 0
293 ; y = 0
294 ; w = 0
295 ; scrollw = 0
296 ; anchor = emptyanchor
297 ; layout = []
298 ; maxy = max_int
299 ; pagemap = Hashtbl.create 10
300 ; pagecache = cbnew 100 ""
301 ; pdims = []
302 ; pagecount = 0
303 ; rendering = false
304 ; mstate = Mnone
305 ; rects = []
306 ; rects1 = []
307 ; text = ""
308 ; mode = View
309 ; fullscreen = None
310 ; searchpattern = ""
311 ; outlines = Olist []
312 ; bookmarks = []
313 ; path = ""
314 ; password = ""
315 ; invalidated = 0
316 ; hists =
317 { nav = cbnew 100 (0, 0.0)
318 ; pat = cbnew 20 ""
319 ; pag = cbnew 10 ""
321 ; colorscale = 1.0
322 ; memused = 0
323 ; gen = 0
324 ; throttle = None
325 ; autoscroll = None
326 ; help = makehelp ()
327 ; docinfo = []
328 ; deadline = nan
332 let vlog fmt =
333 if conf.verbose
334 then
335 Printf.kprintf prerr_endline fmt
336 else
337 Printf.kprintf ignore fmt
340 let writecmd fd s =
341 let len = String.length s in
342 let n = 4 + len in
343 let b = Buffer.create n in
344 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
345 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
346 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
347 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
348 Buffer.add_string b s;
349 let s' = Buffer.contents b in
350 let n' = Unix.write fd s' 0 n in
351 if n' != n then failwith "write failed";
354 let readcmd fd =
355 let s = "xxxx" in
356 let n = Unix.read fd s 0 4 in
357 if n != 4 then failwith "incomplete read(len)";
358 let len = 0
359 lor (Char.code s.[0] lsl 24)
360 lor (Char.code s.[1] lsl 16)
361 lor (Char.code s.[2] lsl 8)
362 lor (Char.code s.[3] lsl 0)
364 let s = String.create len in
365 let n = Unix.read fd s 0 len in
366 if n != len then failwith "incomplete read(data)";
370 let makecmd s l =
371 let b = Buffer.create 10 in
372 Buffer.add_string b s;
373 let rec combine = function
374 | [] -> b
375 | x :: xs ->
376 Buffer.add_char b ' ';
377 let s =
378 match x with
379 | `b b -> if b then "1" else "0"
380 | `s s -> s
381 | `i i -> string_of_int i
382 | `f f -> string_of_float f
383 | `I f -> string_of_int (truncate f)
385 Buffer.add_string b s;
386 combine xs;
388 combine l;
391 let wcmd s l =
392 let cmd = Buffer.contents (makecmd s l) in
393 writecmd state.csock cmd;
396 let calcips h =
397 if conf.presentation
398 then
399 let d = conf.winh - h in
400 max 0 ((d + 1) / 2)
401 else
402 conf.interpagespace
405 let calcheight () =
406 let rec f pn ph pi fh l =
407 match l with
408 | (n, _, h, _) :: rest ->
409 let ips = calcips h in
410 let fh =
411 if conf.presentation
412 then fh+ips
413 else (
414 if isbirdseye state.mode && pn = 0
415 then fh + ips
416 else fh
419 let fh = fh + ((n - pn) * (ph + pi)) in
420 f n h ips fh rest;
422 | [] ->
423 let inc =
424 if conf.presentation || (isbirdseye state.mode && pn = 0)
425 then 0
426 else -pi
428 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
429 max 0 fh
431 let fh = f 0 0 0 0 state.pdims in
435 let getpageyh pageno =
436 let rec f pn ph pi y l =
437 match l with
438 | (n, _, h, _) :: rest ->
439 let ips = calcips h in
440 if n >= pageno
441 then
442 let h = if n = pageno then h else ph in
443 if conf.presentation && n = pageno
444 then
445 y + (pageno - pn) * (ph + pi) + pi, h
446 else
447 y + (pageno - pn) * (ph + pi), h
448 else
449 let y = y + (if conf.presentation then pi else 0) in
450 let y = y + (n - pn) * (ph + pi) in
451 f n h ips y rest
453 | [] ->
454 y + (pageno - pn) * (ph + pi), ph
456 f 0 0 0 0 state.pdims
459 let getpagey pageno = fst (getpageyh pageno);;
461 let layout y sh =
462 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
463 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
464 match pdims with
465 | (pageno', w, h, x) :: rest when pageno' = pageno ->
466 let ips = calcips h in
467 let yinc =
468 if conf.presentation || (isbirdseye state.mode && pageno = 0)
469 then ips
470 else 0
472 (w, h, ips, x), rest, pdimno + 1, yinc
473 | _ ->
474 prev, pdims, pdimno, 0
476 let dy = dy + yinc in
477 let py = py + yinc in
478 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
479 then
480 accu
481 else
482 let vy = y + dy in
483 if py + h <= vy - yinc
484 then
485 let py = py + h + ips in
486 let dy = max 0 (py - y) in
487 f ~pageno:(pageno+1)
488 ~pdimno
489 ~prev:curr
492 ~pdims:rest
493 ~cacheleft
494 ~accu
495 else
496 let pagey = vy - py in
497 let pagevh = h - pagey in
498 let pagevh = min (sh - dy) pagevh in
499 let off = if yinc > 0 then py - vy else 0 in
500 let py = py + h + ips in
501 let e =
502 { pageno = pageno
503 ; pagedimno = pdimno
504 ; pagew = w
505 ; pageh = h
506 ; pagedispy = dy + off
507 ; pagey = pagey + off
508 ; pagevh = pagevh - off
509 ; pagex = x
512 let accu = e :: accu in
513 f ~pageno:(pageno+1)
514 ~pdimno
515 ~prev:curr
517 ~dy:(dy+pagevh+ips)
518 ~pdims:rest
519 ~cacheleft:(cacheleft-1)
520 ~accu
522 if state.invalidated = 0
523 then (
524 let accu =
526 ~pageno:0
527 ~pdimno:~-1
528 ~prev:(0,0,0,0)
529 ~py:0
530 ~dy:0
531 ~pdims:state.pdims
532 ~cacheleft:(cbcap state.pagecache)
533 ~accu:[]
535 List.rev accu
537 else
541 let clamp incr =
542 let y = state.y + incr in
543 let y = max 0 y in
544 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
548 let getopaque pageno =
549 try Some (Hashtbl.find state.pagemap
550 (pageno, state.w, conf.angle, conf.proportional, state.gen))
551 with Not_found -> None
554 let cache pageno opaque =
555 Hashtbl.replace state.pagemap
556 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
559 let validopaque opaque = String.length opaque > 0;;
561 let render l =
562 match getopaque l.pageno with
563 | None when not state.rendering ->
564 state.rendering <- true;
565 cache l.pageno ("", -1);
566 wcmd "render" [`i (l.pageno + 1)
567 ;`i l.pagedimno
568 ;`i l.pagew
569 ;`i l.pageh];
570 | _ -> ()
573 let loadlayout layout =
574 let rec f all = function
575 | l :: ls ->
576 begin match getopaque l.pageno with
577 | None -> render l; f false ls
578 | Some (opaque, _) -> f (all && validopaque opaque) ls
580 | [] -> all
582 f (layout <> []) layout;
585 let findpageforopaque opaque =
586 Hashtbl.fold
587 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
588 state.pagemap None
591 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
593 let preload () =
594 let oktopreload =
595 if conf.preload
596 then
597 let memleft = conf.memlimit - state.memused in
598 if memleft < 0
599 then
600 let opaque = cbpeek state.pagecache in
601 match findpageforopaque opaque with
602 | Some ((n, _, _, _, _), size) ->
603 memleft + size >= 0 && not (pagevisible state.layout n)
604 | None -> false
605 else true
606 else false
608 if oktopreload
609 then
610 let presentation = conf.presentation in
611 let interpagespace = conf.interpagespace in
612 let maxy = state.maxy in
613 conf.presentation <- false;
614 conf.interpagespace <- 0;
615 state.maxy <- calcheight ();
616 let y =
617 match state.layout with
618 | [] -> 0
619 | l :: _ -> getpagey l.pageno + l.pagey
621 let y = if y < conf.winh then 0 else y - conf.winh in
622 let pages = layout y (conf.winh*3) in
623 List.iter render pages;
624 conf.presentation <- presentation;
625 conf.interpagespace <- interpagespace;
626 state.maxy <- maxy;
629 let gotoy y =
630 let y = max 0 y in
631 let y = min state.maxy y in
632 let pages = layout y conf.winh in
633 let ready = loadlayout pages in
634 if conf.showall
635 then (
636 if ready
637 then (
638 state.y <- y;
639 state.layout <- pages;
640 state.throttle <- None;
641 Glut.postRedisplay ();
643 else (
644 state.throttle <- Some pages;
647 else (
648 state.y <- y;
649 state.layout <- pages;
650 state.throttle <- None;
651 Glut.postRedisplay ();
653 begin match state.mode with
654 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
655 if not (pagevisible pages pageno)
656 then (
657 match state.layout with
658 | [] -> ()
659 | l :: _ ->
660 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
662 | _ -> ()
663 end;
664 preload ();
667 let gotoy_and_clear_text y =
668 gotoy y;
669 if not conf.verbose then state.text <- "";
672 let getanchor () =
673 match state.layout with
674 | [] -> emptyanchor
675 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
678 let getanchory (n, top) =
679 let y, h = getpageyh n in
680 y + (truncate (top *. float h));
683 let gotoanchor anchor =
684 gotoy (getanchory anchor);
687 let addnav () =
688 cbput state.hists.nav (getanchor ());
691 let getnav dir =
692 let anchor = cbgetc state.hists.nav dir in
693 getanchory anchor;
696 let gotopage n top =
697 let y, h = getpageyh n in
698 gotoy_and_clear_text (y + (truncate (top *. float h)));
701 let gotopage1 n top =
702 let y = getpagey n in
703 gotoy_and_clear_text (y + top);
706 let invalidate () =
707 state.layout <- [];
708 state.pdims <- [];
709 state.rects <- [];
710 state.rects1 <- [];
711 state.invalidated <- state.invalidated + 1;
714 let scalecolor c =
715 let c = c *. state.colorscale in
716 (c, c, c);
719 let scalecolor2 (r, g, b) =
720 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
723 let represent () =
724 state.maxy <- calcheight ();
725 match state.mode with
726 | Birdseye (_, _, pageno, _, _) ->
727 let y, h = getpageyh pageno in
728 let top = (conf.winh - h) / 2 in
729 gotoy (max 0 (y - top))
730 | _ -> gotoanchor state.anchor
733 let pagematrix () =
734 GlMat.mode `projection;
735 GlMat.load_identity ();
736 GlMat.rotate ~x:1.0 ~angle:180.0 ();
737 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
738 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
739 if state.x != 0
740 then (
741 GlMat.translate ~x:(float state.x) ();
745 let winmatrix () =
746 GlMat.mode `projection;
747 GlMat.load_identity ();
748 GlMat.rotate ~x:1.0 ~angle:180.0 ();
749 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
750 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
753 let reshape =
754 let firsttime = ref true in
755 fun ~w ~h ->
756 if state.invalidated = 0 && not !firsttime
757 then state.anchor <- getanchor ();
759 firsttime := false;
760 conf.winw <- w;
761 let w = truncate (float w *. conf.zoom) - state.scrollw in
762 let w = max w 2 in
763 state.w <- w;
764 conf.winh <- h;
765 GlMat.mode `modelview;
766 GlMat.load_identity ();
767 GlClear.color (scalecolor 1.0);
768 GlClear.clear [`color];
770 invalidate ();
771 wcmd "geometry" [`i w; `i h];
774 let drawstring size x y s =
775 Gl.enable `blend;
776 Gl.enable `texture_2d;
777 ignore (drawstr size x y s);
778 Gl.disable `blend;
779 Gl.disable `texture_2d;
782 let drawstring1 size x y s =
783 drawstr size x y s;
786 let enttext () =
787 let len = String.length state.text in
788 let drawstring s =
789 GlDraw.color (0.0, 0.0, 0.0);
790 GlDraw.rect
791 (0.0, float (conf.winh - 18))
792 (float (conf.winw - state.scrollw - 1), float conf.winh)
794 GlDraw.color (1.0, 1.0, 1.0);
795 drawstring 14 (if len > 0 then 8 else 2) (conf.winh - 5) s;
797 match state.mode with
798 | Textentry ((prefix, text, _, _, _), _) ->
799 let s =
800 if len > 0
801 then
802 Printf.sprintf "%s%s_ [%s]" prefix text state.text
803 else
804 Printf.sprintf "%s%s_" prefix text
806 drawstring s
808 | _ ->
809 if len > 0 then drawstring state.text
812 let showtext c s =
813 state.text <- Printf.sprintf "%c%s" c s;
814 Glut.postRedisplay ();
817 let act cmd =
818 match cmd.[0] with
819 | 'c' ->
820 state.pdims <- [];
822 | 'D' ->
823 state.rects <- state.rects1;
824 Glut.postRedisplay ()
826 | 'C' ->
827 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
828 state.pagecount <- n;
829 state.invalidated <- state.invalidated - 1;
830 if state.invalidated = 0
831 then represent ()
833 | 't' ->
834 let s = Scanf.sscanf cmd "t %n"
835 (fun n -> String.sub cmd n (String.length cmd - n))
837 Glut.setWindowTitle s
839 | 'T' ->
840 let s = Scanf.sscanf cmd "T %n"
841 (fun n -> String.sub cmd n (String.length cmd - n))
843 if istextentry state.mode
844 then (
845 state.text <- s;
846 showtext ' ' s;
848 else (
849 state.text <- s;
850 Glut.postRedisplay ();
853 | 'V' ->
854 if conf.verbose
855 then
856 let s = Scanf.sscanf cmd "V %n"
857 (fun n -> String.sub cmd n (String.length cmd - n))
859 state.text <- s;
860 showtext ' ' s;
862 | 'F' ->
863 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
864 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
865 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
866 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
868 let y = (getpagey pageno) + truncate y0 in
869 addnav ();
870 gotoy y;
871 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
873 | 'R' ->
874 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
875 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
876 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
877 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
879 state.rects1 <-
880 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
882 | 'r' ->
883 let n, w, _h, r, l, s, p =
884 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
885 (fun n w h r l s p ->
886 (n-1, w, h, r, l != 0, s, p))
889 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
890 state.memused <- state.memused + s;
892 let layout =
893 match state.throttle with
894 | None -> state.layout
895 | Some layout -> layout
898 let rec gc () =
899 if (state.memused <= conf.memlimit) || cbempty state.pagecache
900 then ()
901 else (
902 let evictedopaque = cbpeek state.pagecache in
903 match findpageforopaque evictedopaque with
904 | None -> failwith "bug in gc"
905 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
906 if state.gen != gen || not (pagevisible layout evictedn)
907 then (
908 wcmd "free" [`s evictedopaque];
909 state.memused <- state.memused - evictedsize;
910 Hashtbl.remove state.pagemap k;
911 cbdecr state.pagecache;
912 gc ();
916 gc ();
918 cbput state.pagecache p;
919 state.rendering <- false;
921 begin match state.throttle with
922 | None ->
923 if pagevisible state.layout n
924 then gotoy state.y
925 else (
926 let allvisible = loadlayout state.layout in
927 if allvisible then preload ();
930 | Some layout ->
931 match layout with
932 | [] -> ()
933 | l :: _ ->
934 let y = getpagey l.pageno + l.pagey in
935 gotoy y
938 | 'l' ->
939 let pdim =
940 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
942 state.pdims <- pdim :: state.pdims
944 | 'o' ->
945 let (l, n, t, h, pos) =
946 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
948 let s = String.sub cmd pos (String.length cmd - pos) in
949 let outline = (s, l, (n, float t /. float h)) in
950 let outlines =
951 match state.outlines with
952 | Olist outlines -> Olist (outline :: outlines)
953 | Oarray _ -> Olist [outline]
954 | Onarrow _ -> Olist [outline]
956 state.outlines <- outlines
959 | 'i' ->
960 if String.length cmd > 1 && cmd.[1] = 'e'
961 then
962 state.docinfo <- List.rev state.docinfo
963 else
964 let s = Scanf.sscanf cmd "i %n"
965 (fun n -> String.sub cmd n (String.length cmd - n))
967 state.docinfo <- (1, s) :: state.docinfo
969 | _ ->
970 dolog "unknown cmd `%S'" cmd
973 let now = Unix.gettimeofday;;
975 let idle () =
976 if state.deadline == nan then state.deadline <- now ();
977 let rec loop delay =
978 let timeout =
979 if delay > 0.0
980 then max 0.0 (state.deadline -. now ())
981 else 0.0
983 let r, _, _ = Unix.select [state.csock] [] [] timeout in
984 state.deadline <- state.deadline +. delay;
985 begin match r with
986 | [] ->
987 begin match state.autoscroll with
988 | Some step when step != 0 ->
989 let y = state.y + step in
990 let y =
991 if y < 0
992 then state.maxy
993 else if y >= state.maxy then 0 else y
995 gotoy y;
996 if state.mode = View
997 then state.text <- "";
998 | _ -> ()
999 end;
1001 | _ ->
1002 let cmd = readcmd state.csock in
1003 act cmd;
1004 loop 0.0
1005 end;
1006 in loop 0.007
1009 let onhist cb =
1010 let rc = cb.rc in
1011 let action = function
1012 | HCprev -> cbget cb ~-1
1013 | HCnext -> cbget cb 1
1014 | HCfirst -> cbget cb ~-(cb.rc)
1015 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1016 and cancel () = cb.rc <- rc
1017 in (action, cancel)
1020 let search pattern forward =
1021 if String.length pattern > 0
1022 then
1023 let pn, py =
1024 match state.layout with
1025 | [] -> 0, 0
1026 | l :: _ ->
1027 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1029 let cmd =
1030 let b = makecmd "search"
1031 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1033 Buffer.add_char b ',';
1034 Buffer.add_string b pattern;
1035 Buffer.add_char b '\000';
1036 Buffer.contents b;
1038 writecmd state.csock cmd;
1041 let intentry text key =
1042 let c = Char.unsafe_chr key in
1043 match c with
1044 | '0' .. '9' ->
1045 let s = "x" in s.[0] <- c;
1046 let text = text ^ s in
1047 TEcont text
1049 | _ ->
1050 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1051 TEcont text
1054 let addchar s c =
1055 let b = Buffer.create (String.length s + 1) in
1056 Buffer.add_string b s;
1057 Buffer.add_char b c;
1058 Buffer.contents b;
1061 let textentry text key =
1062 let c = Char.unsafe_chr key in
1063 match c with
1064 | _ when key >= 32 && key < 127 ->
1065 let text = addchar text c in
1066 TEcont text
1068 | _ ->
1069 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1070 TEcont text
1073 let reinit angle proportional =
1074 conf.angle <- angle;
1075 conf.proportional <- proportional;
1076 invalidate ();
1077 wcmd "reinit" [`i angle; `b proportional];
1080 let setzoom zoom =
1081 let zoom = max 0.01 zoom in
1082 if zoom <> conf.zoom
1083 then (
1084 if zoom <= 1.0
1085 then state.x <- 0;
1086 conf.zoom <- zoom;
1087 reshape conf.winw conf.winh;
1088 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1092 let enterbirdseye () =
1093 let zoom = float conf.thumbw /. float conf.winw in
1094 let birdseyepageno =
1095 let cy = conf.winh / 2 in
1096 let fold = function
1097 | [] -> 0
1098 | l :: rest ->
1099 let rec fold best = function
1100 | [] -> best.pageno
1101 | l :: rest ->
1102 let d = cy - (l.pagedispy + l.pagevh/2)
1103 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1104 if abs d < abs dbest
1105 then fold l rest
1106 else best.pageno
1107 in fold l rest
1109 fold state.layout
1111 state.mode <- Birdseye (
1112 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1114 conf.zoom <- zoom;
1115 conf.presentation <- false;
1116 conf.interpagespace <- 10;
1117 conf.hlinks <- false;
1118 state.x <- 0;
1119 state.mstate <- Mnone;
1120 conf.showall <- false;
1121 Glut.setCursor Glut.CURSOR_INHERIT;
1122 if conf.verbose
1123 then
1124 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1125 (100.0*.zoom)
1126 else
1127 state.text <- ""
1129 reshape conf.winw conf.winh;
1132 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1133 state.mode <- View;
1134 conf.zoom <- c.zoom;
1135 conf.presentation <- c.presentation;
1136 conf.interpagespace <- c.interpagespace;
1137 conf.showall <- c.showall;
1138 conf.hlinks <- c.hlinks;
1139 state.x <- leftx;
1140 if conf.verbose
1141 then
1142 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1143 (100.0*.conf.zoom)
1145 reshape conf.winw conf.winh;
1146 state.anchor <- if goback then anchor else (pageno, 0.0);
1149 let togglebirdseye () =
1150 match state.mode with
1151 | Birdseye vals -> leavebirdseye vals true
1152 | View | Outline _ -> enterbirdseye ()
1153 | _ -> ()
1156 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1157 let pageno = max 0 (pageno - 1) in
1158 let rec loop = function
1159 | [] -> gotopage1 pageno 0
1160 | l :: _ when l.pageno = pageno ->
1161 if l.pagedispy >= 0 && l.pagey = 0
1162 then Glut.postRedisplay ()
1163 else gotopage1 pageno 0
1164 | _ :: rest -> loop rest
1166 loop state.layout;
1167 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1170 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1171 let pageno = min (state.pagecount - 1) (pageno + 1) in
1172 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1173 let rec loop = function
1174 | [] ->
1175 let y, h = getpageyh pageno in
1176 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1177 gotoy (clamp dy)
1178 | l :: _ when l.pageno = pageno ->
1179 if l.pagevh != l.pageh
1180 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1181 else Glut.postRedisplay ()
1182 | _ :: rest -> loop rest
1184 loop state.layout
1187 let optentry mode _ key =
1188 let btos b = if b then "on" else "off" in
1189 let c = Char.unsafe_chr key in
1190 match c with
1191 | 's' ->
1192 let ondone s =
1193 try conf.scrollstep <- int_of_string s with exc ->
1194 state.text <- Printf.sprintf "bad integer `%s': %s"
1195 s (Printexc.to_string exc)
1197 TEswitch ("scroll step: ", "", None, intentry, ondone)
1199 | 'A' ->
1200 let ondone s =
1202 conf.autoscrollstep <- int_of_string s;
1203 if state.autoscroll <> None
1204 then state.autoscroll <- Some conf.autoscrollstep
1205 with exc ->
1206 state.text <- Printf.sprintf "bad integer `%s': %s"
1207 s (Printexc.to_string exc)
1209 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
1211 | 'Z' ->
1212 let ondone s =
1214 let zoom = float (int_of_string s) /. 100.0 in
1215 setzoom zoom
1216 with exc ->
1217 state.text <- Printf.sprintf "bad integer `%s': %s"
1218 s (Printexc.to_string exc)
1220 TEswitch ("zoom: ", "", None, intentry, ondone)
1222 | 't' ->
1223 let ondone s =
1225 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1226 state.text <-
1227 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1228 begin match mode with
1229 | Birdseye beye ->
1230 leavebirdseye beye false;
1231 enterbirdseye ();
1232 | _ -> ();
1234 with exc ->
1235 state.text <- Printf.sprintf "bad integer `%s': %s"
1236 s (Printexc.to_string exc)
1238 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
1240 | 'R' ->
1241 let ondone s =
1242 match try
1243 Some (int_of_string s)
1244 with exc ->
1245 state.text <- Printf.sprintf "bad integer `%s': %s"
1246 s (Printexc.to_string exc);
1247 None
1248 with
1249 | Some angle -> reinit angle conf.proportional
1250 | None -> ()
1252 TEswitch ("rotation: ", "", None, intentry, ondone)
1254 | 'i' ->
1255 conf.icase <- not conf.icase;
1256 TEdone ("case insensitive search " ^ (btos conf.icase))
1258 | 'p' ->
1259 conf.preload <- not conf.preload;
1260 gotoy state.y;
1261 TEdone ("preload " ^ (btos conf.preload))
1263 | 'v' ->
1264 conf.verbose <- not conf.verbose;
1265 TEdone ("verbose " ^ (btos conf.verbose))
1267 | 'h' ->
1268 conf.maxhfit <- not conf.maxhfit;
1269 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1270 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1272 | 'c' ->
1273 conf.crophack <- not conf.crophack;
1274 TEdone ("crophack " ^ btos conf.crophack)
1276 | 'a' ->
1277 conf.showall <- not conf.showall;
1278 TEdone ("throttle " ^ btos conf.showall)
1280 | 'f' ->
1281 conf.underinfo <- not conf.underinfo;
1282 TEdone ("underinfo " ^ btos conf.underinfo)
1284 | 'P' ->
1285 conf.savebmarks <- not conf.savebmarks;
1286 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1288 | 'S' ->
1289 let ondone s =
1291 let pageno, py =
1292 match state.layout with
1293 | [] -> 0, 0
1294 | l :: _ ->
1295 l.pageno, l.pagey
1297 conf.interpagespace <- int_of_string s;
1298 state.maxy <- calcheight ();
1299 let y = getpagey pageno in
1300 gotoy (y + py)
1301 with exc ->
1302 state.text <- Printf.sprintf "bad integer `%s': %s"
1303 s (Printexc.to_string exc)
1305 TEswitch ("vertical margin: ", "", None, intentry, ondone)
1307 | 'l' ->
1308 reinit conf.angle (not conf.proportional);
1309 TEdone ("proprortional display " ^ btos conf.proportional)
1311 | _ ->
1312 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1313 TEstop
1316 let maxoutlinerows () = (conf.winh - 31) / 16;;
1318 let enterselector allowdel outlines errmsg msg =
1319 if Array.length outlines = 0
1320 then (
1321 showtext ' ' errmsg;
1323 else (
1324 state.text <- msg;
1325 Glut.setCursor Glut.CURSOR_INHERIT;
1326 let pageno =
1327 match state.layout with
1328 | [] -> -1
1329 | {pageno=pageno} :: _ -> pageno
1331 let active =
1332 let rec loop n =
1333 if n = Array.length outlines
1334 then 0
1335 else
1336 let (_, _, (outlinepageno, _)) = outlines.(n) in
1337 if outlinepageno >= pageno then n else loop (n+1)
1339 loop 0
1341 state.mode <- Outline
1342 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1343 state.mode);
1344 Glut.postRedisplay ();
1348 let enteroutlinemode () =
1349 let outlines, msg =
1350 match state.outlines with
1351 | Oarray a -> a, ""
1352 | Olist l ->
1353 let a = Array.of_list (List.rev l) in
1354 state.outlines <- Oarray a;
1355 a, ""
1356 | Onarrow (pat, a, _) ->
1357 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1359 enterselector false outlines "Document has no outline" msg;
1362 let enterbookmarkmode () =
1363 let bookmarks = Array.of_list state.bookmarks in
1364 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1367 let mode_to_string mode =
1368 let b = Buffer.create 10 in
1369 let rec f = function
1370 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1371 | View -> Buffer.add_string b "View"
1372 | Birdseye _ -> Buffer.add_string b "Birdseye"
1373 | Items _ -> Buffer.add_string b "Items"
1374 | Outline _ -> Buffer.add_string b "Outline"
1376 f mode;
1377 Buffer.contents b;
1380 let color_of_string s =
1381 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1382 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1386 let color_to_string (r, g, b) =
1387 let r = truncate (r *. 256.0)
1388 and g = truncate (g *. 256.0)
1389 and b = truncate (b *. 256.0) in
1390 Printf.sprintf "%d/%d/%d" r g b
1393 let enterinfomode () =
1394 let btos = function true -> "on" | _ -> "off" in
1395 let mode = state.mode in
1396 let rec makeitems () =
1397 let intp name get set =
1398 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1399 fun active first _ pan ->
1400 let ondone s =
1401 let n =
1402 try int_of_string s
1403 with exn ->
1404 state.text <- Printf.sprintf "bad integer `%s': %s"
1405 s (Printexc.to_string exn);
1406 max_int;
1408 if n != max_int then set n;
1410 let te = name ^ ": ", "", None, intentry, ondone in
1411 state.text <- "";
1412 Textentry (
1414 fun _ ->
1415 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1418 and boolp name get set =
1419 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1420 fun active first qsearch pan ->
1421 let v = get () in
1422 set (not v);
1423 Items (active, first, makeitems (), qsearch, pan, mode);
1425 and colorp name get set =
1426 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1427 fun active first _ pan ->
1428 let invalid = (nan, nan, nan) in
1429 let ondone s =
1430 let c =
1431 try color_of_string s
1432 with exn ->
1433 state.text <- Printf.sprintf "bad color `%s': %s"
1434 s (Printexc.to_string exn);
1435 invalid
1437 if c <> invalid
1438 then set c;
1440 let te = name ^ ": ", "", None, textentry, ondone in
1441 state.text <- "";
1442 Textentry (
1444 fun _ ->
1445 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1450 let birdseye = isbirdseye mode in
1451 let items = [
1452 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1454 boolp "presentation"
1455 (fun () -> conf.presentation)
1456 (fun v ->
1457 conf.presentation <- v;
1458 state.anchor <- getanchor ();
1459 represent ());
1461 boolp "ignore case in searches"
1462 (fun () -> conf.icase)
1463 (fun v -> conf.icase <- v);
1465 boolp "preload"
1466 (fun () -> conf.preload)
1467 (fun v -> conf.preload <- v);
1469 boolp "verbose"
1470 (fun () -> conf.verbose)
1471 (fun v -> conf.verbose <- v);
1473 boolp "max fit"
1474 (fun () -> conf.maxhfit)
1475 (fun v -> conf.maxhfit <- v);
1477 boolp "crop hack"
1478 (fun () -> conf.crophack)
1479 (fun v -> conf.crophack <- v);
1481 boolp "throttle"
1482 (fun () -> conf.showall)
1483 (fun v -> conf.showall <- v);
1485 boolp "highlight links"
1486 (fun () -> conf.hlinks)
1487 (fun v -> conf.hlinks <- v);
1489 boolp "under info"
1490 (fun () -> conf.underinfo)
1491 (fun v -> conf.underinfo <- v);
1492 boolp "persistent bookmarks"
1493 (fun () -> conf.savebmarks)
1494 (fun v -> conf.savebmarks <- v);
1496 boolp "proportional display"
1497 (fun () -> conf.proportional)
1498 (fun v -> reinit conf.angle v);
1500 boolp "persistent location"
1501 (fun () -> conf.jumpback)
1502 (fun v -> conf.jumpback <- v);
1504 "", 0, Noaction;
1506 intp "vertical margin"
1507 (fun () -> conf.interpagespace)
1508 (fun n ->
1509 conf.interpagespace <- n;
1510 let pageno, py =
1511 match state.layout with
1512 | [] -> 0, 0
1513 | l :: _ ->
1514 l.pageno, l.pagey
1516 state.maxy <- calcheight ();
1517 let y = getpagey pageno in
1518 gotoy (y + py)
1521 intp "page bias"
1522 (fun () -> conf.pagebias)
1523 (fun v -> conf.pagebias <- v);
1525 intp "scroll step"
1526 (fun () -> conf.scrollstep)
1527 (fun n -> conf.scrollstep <- n);
1529 intp "auto scroll step"
1530 (fun () ->
1531 match state.autoscroll with
1532 | Some step -> step
1533 | _ -> conf.autoscrollstep)
1534 (fun n ->
1535 if state.autoscroll <> None
1536 then state.autoscroll <- Some n;
1537 conf.autoscrollstep <- n);
1539 intp "zoom"
1540 (fun () -> truncate (conf.zoom *. 100.))
1541 (fun v -> setzoom ((float v) /. 100.));
1543 intp "rotation"
1544 (fun () -> conf.angle)
1545 (fun v -> reinit v conf.proportional);
1547 intp "scroll bar width"
1548 (fun () -> state.scrollw)
1549 (fun v ->
1550 state.scrollw <- v;
1551 conf.scrollbw <- v;
1552 reshape conf.winw conf.winh;
1555 intp "scroll handle height"
1556 (fun () -> conf.scrollh)
1557 (fun v -> conf.scrollh <- v;);
1559 intp "thumbnail width"
1560 (fun () -> conf.thumbw)
1561 (fun v ->
1562 conf.thumbw <- min 1920 v;
1563 match mode with
1564 | Birdseye beye ->
1565 leavebirdseye beye false;
1566 enterbirdseye ()
1567 | _ -> ()
1570 colorp "background color"
1571 (fun () -> conf.bgcolor)
1572 (fun v -> conf.bgcolor <- v);
1574 "", 0, Noaction;
1575 "Presentation mode", 0, Noaction;
1577 boolp "scrollbar visible"
1578 (fun () -> conf.scrollbarinpm)
1579 (fun v ->
1580 if v != conf.scrollbarinpm
1581 then (
1582 conf.scrollbarinpm <- v;
1583 if conf.presentation
1584 then (
1585 state.scrollw <- if v then conf.scrollbw else 0;
1586 reshape conf.winw conf.winh;
1591 "", 0, Noaction;
1592 "Pixmap Cache", 0, Noaction;
1594 intp "size (advisory)"
1595 (fun () -> conf.memlimit)
1596 (fun v -> conf.memlimit <- v);
1597 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1601 let tailer =
1602 let trailer =
1603 ("", 0, Noaction)
1604 :: ("Document", 0, Noaction)
1605 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1607 if birdseye
1608 then trailer
1609 else (
1610 ("", 0, Noaction)
1611 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1612 (btos conf.bedefault),
1614 Action (
1615 fun active first qsearch pan ->
1616 conf.bedefault <- not conf.bedefault;
1617 Items (active, first, makeitems (), qsearch, pan, mode);
1619 ) :: trailer
1622 Array.of_list (items @ tailer)
1624 state.text <- "";
1625 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1626 Glut.postRedisplay ();
1629 let enterhelpmode () =
1630 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1631 Glut.postRedisplay ();
1634 let quickbookmark ?title () =
1635 match state.layout with
1636 | [] -> ()
1637 | l :: _ ->
1638 let title =
1639 match title with
1640 | None ->
1641 let sec = Unix.gettimeofday () in
1642 let tm = Unix.localtime sec in
1643 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1644 (l.pageno+1)
1645 tm.Unix.tm_mday
1646 tm.Unix.tm_mon
1647 (tm.Unix.tm_year + 1900)
1648 tm.Unix.tm_hour
1649 tm.Unix.tm_min
1650 | Some title -> title
1652 state.bookmarks <-
1653 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1654 :: state.bookmarks
1657 let doreshape w h =
1658 state.fullscreen <- None;
1659 Glut.reshapeWindow w h;
1662 let writeopen path password =
1663 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1666 let opendoc path password =
1667 invalidate ();
1668 state.path <- path;
1669 state.password <- password;
1670 state.gen <- state.gen + 1;
1671 state.docinfo <- [];
1673 writeopen path password;
1674 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1675 wcmd "geometry" [`i state.w; `i conf.winh];
1678 let viewkeyboard key =
1679 let enttext te =
1680 let mode = state.mode in
1681 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1682 state.text <- "";
1683 enttext ();
1684 Glut.postRedisplay ()
1686 let c = Char.chr key in
1687 match c with
1688 | '\027' -> (* escape *)
1689 if String.length state.text > 0
1690 then (
1691 state.text <- "";
1692 Glut.postRedisplay ();
1694 else
1695 exit 0
1697 | 'q' ->
1698 exit 0
1700 | '\008' -> (* backspace *)
1701 let y = getnav ~-1 in
1702 gotoy_and_clear_text y
1704 | 'o' ->
1705 enteroutlinemode ()
1707 | 'u' ->
1708 state.rects <- [];
1709 state.text <- "";
1710 Glut.postRedisplay ()
1712 | '/' | '?' ->
1713 let ondone isforw s =
1714 cbput state.hists.pat s;
1715 state.searchpattern <- s;
1716 search s isforw
1718 let s = String.create 1 in
1719 s.[0] <- c;
1720 enttext (s, "", Some (onhist state.hists.pat),
1721 textentry, ondone (c ='/'))
1723 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1724 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1725 setzoom (conf.zoom +. incr)
1727 | '+' ->
1728 let ondone s =
1729 let n =
1730 try int_of_string s with exc ->
1731 state.text <- Printf.sprintf "bad integer `%s': %s"
1732 s (Printexc.to_string exc);
1733 max_int
1735 if n != max_int
1736 then (
1737 conf.pagebias <- n;
1738 state.text <- "page bias is now " ^ string_of_int n;
1741 enttext ("page bias: ", "", None, intentry, ondone)
1743 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1744 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1745 setzoom (max 0.01 (conf.zoom -. decr))
1747 | '-' ->
1748 let ondone msg = state.text <- msg in
1749 enttext (
1750 "option [acfhilpstvAPRSZ]: ", "", None,
1751 optentry state.mode, ondone
1754 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1755 setzoom 1.0
1757 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1758 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1759 if zoom < 1.0
1760 then setzoom zoom
1762 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1763 togglebirdseye ()
1765 | '0' .. '9' ->
1766 let ondone s =
1767 let n =
1768 try int_of_string s with exc ->
1769 state.text <- Printf.sprintf "bad integer `%s': %s"
1770 s (Printexc.to_string exc);
1773 if n >= 0
1774 then (
1775 addnav ();
1776 cbput state.hists.pag (string_of_int n);
1777 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1780 let pageentry text key =
1781 match Char.unsafe_chr key with
1782 | 'g' -> TEdone text
1783 | _ -> intentry text key
1785 let text = "x" in text.[0] <- c;
1786 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1788 | 'b' ->
1789 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1790 reshape conf.winw conf.winh;
1792 | 'l' ->
1793 conf.hlinks <- not conf.hlinks;
1794 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1795 Glut.postRedisplay ()
1797 | 'a' ->
1798 begin match state.autoscroll with
1799 | Some step ->
1800 conf.autoscrollstep <- step;
1801 state.autoscroll <- None
1802 | None ->
1803 if conf.autoscrollstep = 0
1804 then state.autoscroll <- Some 1
1805 else state.autoscroll <- Some conf.autoscrollstep
1808 | 'P' ->
1809 conf.presentation <- not conf.presentation;
1810 if conf.presentation
1811 then (
1812 if not conf.scrollbarinpm
1813 then state.scrollw <- 0;
1815 else
1816 state.scrollw <- conf.scrollbw;
1818 showtext ' ' ("presentation mode " ^
1819 if conf.presentation then "on" else "off");
1820 state.anchor <- getanchor ();
1821 represent ()
1823 | 'f' ->
1824 begin match state.fullscreen with
1825 | None ->
1826 state.fullscreen <- Some (conf.winw, conf.winh);
1827 Glut.fullScreen ()
1828 | Some (w, h) ->
1829 state.fullscreen <- None;
1830 doreshape w h
1833 | 'g' ->
1834 gotoy_and_clear_text 0
1836 | 'n' ->
1837 search state.searchpattern true
1839 | 'p' | 'N' ->
1840 search state.searchpattern false
1842 | 't' ->
1843 begin match state.layout with
1844 | [] -> ()
1845 | l :: _ ->
1846 gotoy_and_clear_text (getpagey l.pageno)
1849 | ' ' ->
1850 begin match List.rev state.layout with
1851 | [] -> ()
1852 | l :: _ ->
1853 let pageno = min (l.pageno+1) (state.pagecount-1) in
1854 gotoy_and_clear_text (getpagey pageno)
1857 | '\127' -> (* del *)
1858 begin match state.layout with
1859 | [] -> ()
1860 | l :: _ ->
1861 let pageno = max 0 (l.pageno-1) in
1862 gotoy_and_clear_text (getpagey pageno)
1865 | '=' ->
1866 let f (fn, _) l =
1867 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1869 let fn, ln = List.fold_left f (-1, -1) state.layout in
1870 let s =
1871 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1872 let percent =
1873 if maxy <= 0
1874 then 100.
1875 else (100. *. (float state.y /. float maxy)) in
1876 if fn = ln
1877 then
1878 Printf.sprintf "Page %d of %d %.2f%%"
1879 (fn+1) state.pagecount percent
1880 else
1881 Printf.sprintf
1882 "Pages %d-%d of %d %.2f%%"
1883 (fn+1) (ln+1) state.pagecount percent
1885 showtext ' ' s;
1887 | 'w' ->
1888 begin match state.layout with
1889 | [] -> ()
1890 | l :: _ ->
1891 doreshape (l.pagew + state.scrollw) l.pageh;
1892 Glut.postRedisplay ();
1895 | '\'' ->
1896 enterbookmarkmode ()
1898 | 'h' ->
1899 enterhelpmode ()
1901 | 'i' ->
1902 enterinfomode ()
1904 | 'm' ->
1905 let ondone s =
1906 match state.layout with
1907 | l :: _ ->
1908 state.bookmarks <-
1909 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1910 :: state.bookmarks
1911 | _ -> ()
1913 enttext ("bookmark: ", "", None, textentry, ondone)
1915 | '~' ->
1916 quickbookmark ();
1917 showtext ' ' "Quick bookmark added";
1919 | 'z' ->
1920 begin match state.layout with
1921 | l :: _ ->
1922 let rect = getpdimrect l.pagedimno in
1923 let w, h =
1924 if conf.crophack
1925 then
1926 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1927 truncate (1.2 *. (rect.(3) -. rect.(0))))
1928 else
1929 (truncate (rect.(1) -. rect.(0)),
1930 truncate (rect.(3) -. rect.(0)))
1932 if w != 0 && h != 0
1933 then
1934 doreshape (w + state.scrollw) (h + conf.interpagespace)
1936 Glut.postRedisplay ();
1938 | [] -> ()
1941 | '<' | '>' ->
1942 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1944 | '[' | ']' ->
1945 state.colorscale <-
1946 max 0.0
1947 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1948 Glut.postRedisplay ()
1950 | 'k' ->
1951 begin match state.mode with
1952 | Birdseye beye -> upbirdseye beye
1953 | _ -> gotoy (clamp (-conf.scrollstep))
1956 | 'j' ->
1957 begin match state.mode with
1958 | Birdseye beye -> downbirdseye beye
1959 | _ -> gotoy (clamp conf.scrollstep)
1962 | 'r' ->
1963 state.anchor <- getanchor ();
1964 opendoc state.path state.password
1966 | _ ->
1967 vlog "huh? %d %c" key (Char.chr key);
1970 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1971 let enttext te =
1972 state.mode <- Textentry (te, onleave);
1973 state.text <- "";
1974 enttext ();
1975 Glut.postRedisplay ()
1977 match Char.unsafe_chr key with
1978 | '\008' -> (* backspace *)
1979 let len = String.length text in
1980 if len = 0
1981 then (
1982 onleave Cancel;
1983 Glut.postRedisplay ();
1985 else (
1986 let s = String.sub text 0 (len - 1) in
1987 enttext (c, s, opthist, onkey, ondone)
1990 | '\r' | '\n' ->
1991 ondone text;
1992 onleave Confirm;
1993 Glut.postRedisplay ()
1995 | '\027' -> (* escape *)
1996 begin match opthist with
1997 | None -> ()
1998 | Some (_, onhistcancel) -> onhistcancel ()
1999 end;
2000 onleave Cancel;
2001 Glut.postRedisplay ()
2003 | _ ->
2004 begin match onkey text key with
2005 | TEdone text ->
2006 onleave Confirm;
2007 ondone text;
2008 Glut.postRedisplay ()
2010 | TEcont text ->
2011 enttext (c, text, opthist, onkey, ondone);
2013 | TEstop ->
2014 onleave Cancel;
2015 Glut.postRedisplay ()
2017 | TEswitch te ->
2018 state.mode <- Textentry (te, onleave);
2019 Glut.postRedisplay ()
2020 end;
2023 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2024 match key with
2025 | 27 -> (* escape *)
2026 leavebirdseye beye true
2028 | 12 -> (* ctrl-l *)
2029 let y, h = getpageyh pageno in
2030 let top = (conf.winh - h) / 2 in
2031 gotoy (max 0 (y - top))
2033 | 13 -> (* enter *)
2034 leavebirdseye beye false
2036 | _ ->
2037 viewkeyboard key
2040 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2041 let set active first qsearch =
2042 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2044 let search active pattern incr =
2045 let dosearch re =
2046 let rec loop n =
2047 if n >= Array.length items || n < 0
2048 then None
2049 else
2050 let (s, _, _) = items.(n) in
2052 (try ignore (Str.search_forward re s 0); true
2053 with Not_found -> false)
2054 then Some n
2055 else loop (n + incr)
2057 loop active
2060 let re = Str.regexp_case_fold pattern in
2061 dosearch re
2062 with Failure s ->
2063 state.text <- s;
2064 None
2066 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2067 match key with
2068 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2069 let incr = if key = 18 then -1 else 1 in
2070 let active, first =
2071 match search (active + incr) qsearch incr with
2072 | None ->
2073 state.text <- qsearch ^ " [not found]";
2074 active, first
2075 | Some active ->
2076 state.text <- qsearch;
2077 active, firstof active
2079 set active first qsearch;
2080 Glut.postRedisplay ();
2082 | 8 -> (* backspace *)
2083 let len = String.length qsearch in
2084 if len = 0
2085 then ()
2086 else (
2087 if len = 1
2088 then (
2089 state.text <- "";
2090 set active first "";
2092 else
2093 let qsearch = String.sub qsearch 0 (len - 1) in
2094 let active, first =
2095 match search active qsearch ~-1 with
2096 | None ->
2097 state.text <- qsearch ^ " [not found]";
2098 active, first
2099 | Some active ->
2100 state.text <- qsearch;
2101 active, firstof active
2103 set active first qsearch
2105 Glut.postRedisplay ()
2107 | _ when key >= 32 && key < 127 ->
2108 let pattern = addchar qsearch (Char.chr key) in
2109 let active, first =
2110 match search active pattern 1 with
2111 | None ->
2112 state.text <- pattern ^ " [not found]";
2113 active, first
2114 | Some active ->
2115 state.text <- pattern;
2116 active, firstof active
2118 set active first pattern;
2119 Glut.postRedisplay ()
2121 | 27 -> (* escape *)
2122 state.text <- "";
2123 if String.length qsearch = 0
2124 then (
2125 state.mode <- oldmode;
2127 else (
2128 set active first "";
2130 Glut.postRedisplay ()
2132 | 13 -> (* enter *)
2133 if active >= 0 && active < Array.length items
2134 then (
2135 match items.(active) with
2136 | _, _, Action f ->
2137 state.mode <- f active first qsearch pan
2139 | _, _, Noaction ->
2140 state.text <- "";
2141 state.mode <- oldmode
2143 else (
2144 state.text <- "";
2145 state.mode <- oldmode
2147 Glut.postRedisplay ();
2149 | _ -> dolog "unknown key %d" key
2152 let outlinekeyboard key
2153 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2154 let narrow outlines pattern =
2155 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2156 match reopt with
2157 | None -> None
2158 | Some re ->
2159 let rec fold accu n =
2160 if n = -1
2161 then accu
2162 else
2163 let (s, _, _) as o = outlines.(n) in
2164 let accu =
2165 if (try ignore (Str.search_forward re s 0); true
2166 with Not_found -> false)
2167 then (o :: accu)
2168 else accu
2170 fold accu (n-1)
2172 let matched = fold [] (Array.length outlines - 1) in
2173 if matched = [] then None else Some (Array.of_list matched)
2175 let search active pattern incr =
2176 let dosearch re =
2177 let rec loop n =
2178 if n = Array.length outlines || n = -1
2179 then None
2180 else
2181 let (s, _, _) = outlines.(n) in
2183 (try ignore (Str.search_forward re s 0); true
2184 with Not_found -> false)
2185 then Some n
2186 else loop (n + incr)
2188 loop active
2191 let re = Str.regexp_case_fold pattern in
2192 dosearch re
2193 with Failure s ->
2194 state.text <- s;
2195 None
2197 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2198 match key with
2199 | 27 -> (* escape *)
2200 state.text <- "";
2201 if String.length qsearch = 0
2202 then (
2203 state.mode <- oldmode;
2205 else (
2206 state.mode <- Outline (
2207 allowdel, active, first, outlines, "", pan, oldmode
2210 Glut.postRedisplay ();
2212 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2213 let incr = if key = 18 then -1 else 1 in
2214 let active, first =
2215 match search (active + incr) qsearch incr with
2216 | None ->
2217 state.text <- qsearch ^ " [not found]";
2218 active, first
2219 | Some active ->
2220 state.text <- qsearch;
2221 active, firstof active
2223 state.mode <- Outline (
2224 allowdel, active, first, outlines, qsearch, pan, oldmode
2226 Glut.postRedisplay ();
2228 | 8 -> (* backspace *)
2229 let len = String.length qsearch in
2230 if len = 0
2231 then ()
2232 else (
2233 if len = 1
2234 then (
2235 state.text <- "";
2236 state.mode <- Outline (
2237 allowdel, active, first, outlines, "", pan, oldmode
2240 else
2241 let qsearch = String.sub qsearch 0 (len - 1) in
2242 let active, first =
2243 match search active qsearch ~-1 with
2244 | None ->
2245 state.text <- qsearch ^ " [not found]";
2246 active, first
2247 | Some active ->
2248 state.text <- qsearch;
2249 active, firstof active
2251 state.mode <- Outline (
2252 allowdel, active, first, outlines, qsearch, pan, oldmode
2255 Glut.postRedisplay ()
2257 | 13 -> (* enter *)
2258 if active < Array.length outlines
2259 then (
2260 let (_, _, anchor) = outlines.(active) in
2261 addnav ();
2262 gotoanchor anchor;
2264 state.text <- "";
2265 if allowdel then state.bookmarks <- Array.to_list outlines;
2266 state.mode <- oldmode;
2267 Glut.postRedisplay ();
2269 | _ when key >= 32 && key < 127 ->
2270 let pattern = addchar qsearch (Char.chr key) in
2271 let active, first =
2272 match search active pattern 1 with
2273 | None ->
2274 state.text <- pattern ^ " [not found]";
2275 active, first
2276 | Some active ->
2277 state.text <- pattern;
2278 active, firstof active
2280 state.mode <- Outline (
2281 allowdel, active, first, outlines, pattern, pan, oldmode
2283 Glut.postRedisplay ()
2285 | 14 when not allowdel -> (* ctrl-n *)
2286 if String.length qsearch > 0
2287 then (
2288 let optoutlines = narrow outlines qsearch in
2289 begin match optoutlines with
2290 | None -> state.text <- "can't narrow"
2291 | Some outlines ->
2292 state.mode <- Outline (
2293 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2295 match state.outlines with
2296 | Olist _ -> ()
2297 | Oarray a ->
2298 state.outlines <- Onarrow (qsearch, outlines, a)
2299 | Onarrow (_, _, b) ->
2300 state.outlines <- Onarrow (qsearch, outlines, b)
2301 end;
2303 Glut.postRedisplay ()
2305 | 21 when not allowdel -> (* ctrl-u *)
2306 let outline =
2307 match state.outlines with
2308 | Oarray a -> a
2309 | Olist l ->
2310 let a = Array.of_list (List.rev l) in
2311 state.outlines <- Oarray a;
2313 | Onarrow (_, _, b) ->
2314 state.outlines <- Oarray b;
2315 state.text <- "";
2318 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2319 Glut.postRedisplay ()
2321 | 12 -> (* ctrl-l *)
2322 state.mode <- Outline
2323 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2324 Glut.postRedisplay ()
2326 | 127 when allowdel -> (* delete *)
2327 let len = Array.length outlines - 1 in
2328 if len = 0
2329 then (
2330 state.mode <- View;
2331 state.bookmarks <- [];
2333 else (
2334 let bookmarks = Array.init len
2335 (fun i ->
2336 let i = if i >= active then i + 1 else i in
2337 outlines.(i)
2340 state.mode <-
2341 Outline (
2342 allowdel,
2343 min active (len-1),
2344 min first (len-1),
2345 bookmarks, qsearch,
2347 oldmode
2350 Glut.postRedisplay ()
2352 | _ -> dolog "unknown key %d" key
2355 let keyboard ~key ~x ~y =
2356 ignore x;
2357 ignore y;
2358 if key = 7 (* ctrl-g *)
2359 then
2360 wcmd "interrupt" []
2361 else
2362 match state.mode with
2363 | Outline outline -> outlinekeyboard key outline
2364 | Textentry textentry -> textentrykeyboard key textentry
2365 | Birdseye birdseye -> birdseyekeyboard key birdseye
2366 | View -> viewkeyboard key
2367 | Items items -> itemskeyboard key items
2370 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2371 match key with
2372 | Glut.KEY_UP -> upbirdseye beye
2373 | Glut.KEY_DOWN -> downbirdseye beye
2375 | Glut.KEY_PAGE_UP ->
2376 begin match state.layout with
2377 | l :: _ ->
2378 if l.pagey != 0
2379 then (
2380 state.mode <- Birdseye (
2381 conf, leftx, l.pageno, hooverpageno, anchor
2383 gotopage1 l.pageno 0;
2385 else (
2386 let layout = layout (state.y-conf.winh) conf.winh in
2387 match layout with
2388 | [] -> gotoy (clamp (-conf.winh))
2389 | l :: _ ->
2390 state.mode <- Birdseye (
2391 conf, leftx, l.pageno, hooverpageno, anchor
2393 gotopage1 l.pageno 0
2396 | [] -> gotoy (clamp (-conf.winh))
2397 end;
2399 | Glut.KEY_PAGE_DOWN ->
2400 begin match List.rev state.layout with
2401 | l :: _ ->
2402 let layout = layout (state.y + conf.winh) conf.winh in
2403 begin match layout with
2404 | [] ->
2405 let incr = l.pageh - l.pagevh in
2406 if incr = 0
2407 then (
2408 state.mode <-
2409 Birdseye (
2410 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2412 Glut.postRedisplay ();
2414 else gotoy (clamp (incr + conf.interpagespace*2));
2416 | l :: _ ->
2417 state.mode <-
2418 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2419 gotopage1 l.pageno 0;
2422 | [] -> gotoy (clamp conf.winh)
2423 end;
2425 | Glut.KEY_HOME ->
2426 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2427 gotopage1 0 0
2429 | Glut.KEY_END ->
2430 let pageno = state.pagecount - 1 in
2431 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2432 if not (pagevisible state.layout pageno)
2433 then
2434 let h =
2435 match List.rev state.pdims with
2436 | [] -> conf.winh
2437 | (_, _, h, _) :: _ -> h
2439 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2440 else Glut.postRedisplay ();
2441 | _ -> ()
2444 let setautoscrollspeed step goingdown =
2445 let incr = max 1 ((abs step) / 2) in
2446 let incr = if goingdown then incr else -incr in
2447 let astep = step + incr in
2448 state.autoscroll <- Some astep;
2451 let special ~key ~x ~y =
2452 ignore x;
2453 ignore y;
2454 match state.mode with
2455 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2456 togglebirdseye ()
2458 | Birdseye vals ->
2459 birdseyespecial key vals
2461 | View when key = Glut.KEY_F1 ->
2462 enterhelpmode ()
2464 | View ->
2465 begin match state.autoscroll with
2466 | Some step when key = Glut.KEY_DOWN || key = Glut.KEY_UP ->
2467 setautoscrollspeed step (key = Glut.KEY_DOWN)
2469 | _ ->
2470 let y =
2471 match key with
2472 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2473 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2474 | Glut.KEY_DOWN -> clamp conf.scrollstep
2475 | Glut.KEY_PAGE_UP ->
2476 if Glut.getModifiers () land Glut.active_ctrl != 0
2477 then
2478 match state.layout with
2479 | [] -> state.y
2480 | l :: _ -> state.y - l.pagey
2481 else
2482 clamp (-conf.winh)
2483 | Glut.KEY_PAGE_DOWN ->
2484 if Glut.getModifiers () land Glut.active_ctrl != 0
2485 then
2486 match List.rev state.layout with
2487 | [] -> state.y
2488 | l :: _ -> getpagey l.pageno
2489 else
2490 clamp conf.winh
2491 | Glut.KEY_HOME -> addnav (); 0
2492 | Glut.KEY_END ->
2493 addnav ();
2494 state.maxy - (if conf.maxhfit then conf.winh else 0)
2496 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2497 Glut.getModifiers () land Glut.active_alt != 0 ->
2498 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2500 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2501 state.x <- state.x - 10;
2502 state.y
2503 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2504 state.x <- state.x + 10;
2505 state.y
2507 | _ -> state.y
2509 gotoy_and_clear_text y
2512 | Textentry
2513 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2514 let s =
2515 match key with
2516 | Glut.KEY_UP -> action HCprev
2517 | Glut.KEY_DOWN -> action HCnext
2518 | Glut.KEY_HOME -> action HCfirst
2519 | Glut.KEY_END -> action HClast
2520 | _ -> state.text
2522 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2523 Glut.postRedisplay ()
2525 | Textentry _ -> ()
2527 | Items (active, first, items, qsearch, pan, oldmode) ->
2528 let maxrows = maxoutlinerows () in
2529 let itemcount = Array.length items in
2530 let hasaction = function
2531 | (_, _, Noaction) -> false
2532 | _ -> true
2534 let find start incr =
2535 let rec find i =
2536 if i = -1 || i = itemcount
2537 then -1
2538 else (
2539 if hasaction items.(i)
2540 then i
2541 else find (i + incr)
2544 find start
2546 let set active first =
2547 let first = max 0 (min first (itemcount - maxrows)) in
2548 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2550 let navigate incr =
2551 let isvisible first n = n >= first && n - first <= maxrows in
2552 let active, first =
2553 let incr1 = if incr > 0 then 1 else -1 in
2554 if isvisible first active
2555 then
2556 let next =
2557 let next = active + incr in
2558 let next =
2559 if next < 0 || next >= itemcount
2560 then -1
2561 else find next incr1
2563 if next = -1 || abs (active - next) > maxrows
2564 then -1
2565 else next
2567 if next = -1
2568 then
2569 let first = first + incr in
2570 let first = max 0 (min first (itemcount - 1)) in
2571 let next =
2572 let next = active + incr in
2573 let next = max 0 (min next (itemcount - 1)) in
2574 find next ~-incr1
2576 let active = if next = -1 then active else next in
2577 active, first
2578 else
2579 let first = min next first in
2580 next, first
2581 else
2582 let first = first + incr in
2583 let first = max 0 (min first (itemcount - 1)) in
2584 let active =
2585 let next = active + incr in
2586 let next = max 0 (min next (itemcount - 1)) in
2587 let next = find next incr1 in
2588 if next = -1 || abs (active - first) > maxrows
2589 then active
2590 else next
2592 active, first
2594 set active first;
2595 Glut.postRedisplay ()
2597 begin match key with
2598 | Glut.KEY_UP -> navigate ~-1
2599 | Glut.KEY_DOWN -> navigate 1
2600 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2601 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2603 | Glut.KEY_RIGHT ->
2604 state.mode <- Items (
2605 active, first, items, qsearch, min 0 (pan - 1), oldmode
2607 Glut.postRedisplay ()
2609 | Glut.KEY_LEFT ->
2610 state.mode <- Items (
2611 active, first, items, qsearch, min 0 (pan + 1), oldmode
2613 Glut.postRedisplay ()
2615 | Glut.KEY_HOME ->
2616 let active = find 0 1 in
2617 set active 0;
2618 Glut.postRedisplay ()
2620 | Glut.KEY_END ->
2621 let first = max 0 (itemcount - maxrows) in
2622 let active = find (itemcount - 1) ~-1 in
2623 set active first;
2624 Glut.postRedisplay ()
2626 | _ -> ()
2627 end;
2629 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2630 let maxrows = maxoutlinerows () in
2631 let calcfirst first active =
2632 if active > first
2633 then
2634 let rows = active - first in
2635 if rows > maxrows then active - maxrows else first
2636 else active
2638 let navigate incr =
2639 let active = active + incr in
2640 let active = max 0 (min active (Array.length outlines - 1)) in
2641 let first = calcfirst first active in
2642 state.mode <- Outline (
2643 allowdel, active, first, outlines, qsearch, pan, oldmode
2645 Glut.postRedisplay ()
2647 let updownlevel incr =
2648 let len = Array.length outlines in
2649 let (_, curlevel, _) = outlines.(active) in
2650 let rec flow i =
2651 if i = len then i-1 else if i = -1 then 0 else
2652 let (_, l, _) = outlines.(i) in
2653 if l != curlevel then i else flow (i+incr)
2655 let active = flow active in
2656 let first = calcfirst first active in
2657 state.mode <- Outline (
2658 allowdel, active, first, outlines, qsearch, pan, oldmode
2660 Glut.postRedisplay ()
2662 match key with
2663 | Glut.KEY_UP -> navigate ~-1
2664 | Glut.KEY_DOWN -> navigate 1
2665 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2666 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2668 | Glut.KEY_RIGHT ->
2669 if Glut.getModifiers () land Glut.active_ctrl != 0
2670 then (
2671 state.mode <- Outline (
2672 allowdel, active, first, outlines,
2673 qsearch, min 0 (pan + 1), oldmode
2675 Glut.postRedisplay ();
2677 else (
2678 if not allowdel
2679 then updownlevel 1
2682 | Glut.KEY_LEFT ->
2683 if Glut.getModifiers () land Glut.active_ctrl != 0
2684 then (
2685 state.mode <- Outline (
2686 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2688 Glut.postRedisplay ();
2690 else (
2691 if not allowdel
2692 then updownlevel ~-1
2695 | Glut.KEY_HOME ->
2696 state.mode <- Outline (
2697 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2699 Glut.postRedisplay ()
2701 | Glut.KEY_END ->
2702 let active = Array.length outlines - 1 in
2703 let first = max 0 (active - maxrows) in
2704 state.mode <- Outline (
2705 allowdel, active, first, outlines, qsearch, pan, oldmode
2707 Glut.postRedisplay ()
2709 | _ -> ()
2712 let drawplaceholder l =
2713 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2714 GlDraw.rect
2715 (float l.pagex, float l.pagedispy)
2716 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2718 let x = if margin < 0 then -margin else l.pagex
2719 and y = l.pagedispy + 13 in
2720 GlDraw.color (0.0, 0.0, 0.0);
2721 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2724 let now () = Unix.gettimeofday ();;
2726 let drawpage l =
2727 let color =
2728 match state.mode with
2729 | Textentry _ -> scalecolor 0.4
2730 | View | Outline _ | Items _ -> scalecolor 1.0
2731 | Birdseye (_, _, pageno, hooverpageno, _) ->
2732 if l.pageno = hooverpageno
2733 then scalecolor 0.9
2734 else (
2735 if l.pageno = pageno
2736 then scalecolor 1.0
2737 else scalecolor 0.8
2740 GlDraw.color color;
2741 begin match getopaque l.pageno with
2742 | Some (opaque, _) when validopaque opaque ->
2743 let a = now () in
2744 draw (l.pagedispy, l.pagevh, l.pagey, conf.hlinks) opaque;
2745 let b = now () in
2746 let d = b-.a in
2747 vlog "draw %d %f sec" l.pageno d;
2749 | _ ->
2750 drawplaceholder l;
2751 end;
2754 let scrollph y =
2755 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2756 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2757 let sh = float conf.winh /. sh in
2758 let sh = max sh (float conf.scrollh) in
2760 let percent =
2761 if state.y = state.maxy
2762 then 1.0
2763 else float y /. float maxy
2765 let position = (float conf.winh -. sh) *. percent in
2767 let position =
2768 if position +. sh > float conf.winh
2769 then float conf.winh -. sh
2770 else position
2772 position, sh;
2775 let scrollindicator () =
2776 GlDraw.color (0.64 , 0.64, 0.64);
2777 GlDraw.rect
2778 (float (conf.winw - state.scrollw), 0.)
2779 (float conf.winw, float conf.winh)
2781 GlDraw.color (0.0, 0.0, 0.0);
2783 let position, sh = scrollph state.y in
2784 GlDraw.rect
2785 (float (conf.winw - state.scrollw), position)
2786 (float conf.winw, position +. sh)
2790 let showsel margin =
2791 match state.mstate with
2792 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2795 | Msel ((x0, y0), (x1, y1)) ->
2796 let rec loop = function
2797 | l :: ls ->
2798 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2799 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2800 then
2801 match getopaque l.pageno with
2802 | Some (opaque, _) when validopaque opaque ->
2803 let oy = -l.pagey + l.pagedispy in
2804 seltext opaque
2805 (x0 - margin - state.x, y0,
2806 x1 - margin - state.x, y1) oy;
2808 | _ -> ()
2809 else loop ls
2810 | [] -> ()
2812 loop state.layout
2815 let showrects () =
2816 let panx = float state.x in
2817 Gl.enable `blend;
2818 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2819 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2820 List.iter
2821 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2822 List.iter (fun l ->
2823 if l.pageno = pageno
2824 then (
2825 let d = float (l.pagedispy - l.pagey) in
2826 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2827 GlDraw.begins `quads;
2829 GlDraw.vertex2 (x0+.panx, y0+.d);
2830 GlDraw.vertex2 (x1+.panx, y1+.d);
2831 GlDraw.vertex2 (x2+.panx, y2+.d);
2832 GlDraw.vertex2 (x3+.panx, y3+.d);
2834 GlDraw.ends ();
2836 ) state.layout
2837 ) state.rects
2839 Gl.disable `blend;
2842 let showstrings trusted active first pan strings =
2843 Gl.enable `blend;
2844 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2845 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2846 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2847 GlDraw.color (1., 1., 1.);
2848 Gl.enable `texture_2d;
2850 let wx = measurestr 15 "w" in
2851 let tabx = 30.0*.wx +. float (pan*15) in
2852 let rec loop row =
2853 if row = Array.length strings || (row - first) * 16 > conf.winh
2854 then ()
2855 else (
2856 let (s, level, _) = strings.(row) in
2857 let y = (row - first) * 16 in
2858 let x = 5 + 15*(max 0 (level+pan)) in
2859 if row = active
2860 then (
2861 Gl.disable `texture_2d;
2862 GlDraw.polygon_mode `both `line;
2863 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2864 GlDraw.rect (0., float (y + 1))
2865 (float (conf.winw - 1), float (y + 18));
2866 GlDraw.polygon_mode `both `fill;
2867 GlDraw.color (1., 1., 1.);
2868 Gl.enable `texture_2d;
2871 let drawtabularstring x s =
2872 let _ =
2873 if trusted
2874 then
2875 let tabpos = try String.index s '\t' with Not_found -> -1 in
2876 if tabpos > 0
2877 then
2878 let len = String.length s - tabpos - 1 in
2879 let s1 = String.sub s 0 tabpos
2880 and s2 = String.sub s (tabpos + 1) len in
2881 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2882 let x = truncate (max xx tabx) in
2883 drawstring1 15 x (y + 16) s2
2884 else
2885 drawstring1 15 x (y + 16) s
2886 else
2887 drawstring1 15 x (y + 16) s
2891 drawtabularstring (x + pan*15) s;
2892 loop (row+1)
2895 loop first;
2896 Gl.disable `blend;
2897 Gl.disable `texture_2d;
2900 let showoutline (_, active, first, outlines, _, pan, _) =
2901 showstrings false active first pan outlines;
2904 let showitems (active, first, items, _, pan, _) =
2905 showstrings true active first pan items;
2908 let display () =
2909 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2910 GlDraw.viewport margin 0 state.w conf.winh;
2911 pagematrix ();
2912 GlClear.color (scalecolor2 conf.bgcolor);
2913 GlClear.clear [`color];
2914 if conf.zoom > 1.0
2915 then (
2916 Gl.enable `scissor_test;
2917 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2919 List.iter drawpage state.layout;
2920 if conf.zoom > 1.0
2921 then
2922 Gl.disable `scissor_test
2924 if state.x != 0
2925 then (
2926 let x = -.float state.x in
2927 GlMat.translate ~x ();
2929 showrects ();
2930 showsel margin;
2931 GlDraw.viewport 0 0 conf.winw conf.winh;
2932 winmatrix ();
2933 scrollindicator ();
2934 begin match state.mode with
2935 | Items items -> showitems items
2936 | Outline outline -> showoutline outline
2937 | _ -> ()
2938 end;
2939 enttext ();
2940 Glut.swapBuffers ();
2943 let getunder x y =
2944 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2945 let x = x - margin - state.x in
2946 let rec f = function
2947 | l :: rest ->
2948 begin match getopaque l.pageno with
2949 | Some (opaque, _) when validopaque opaque ->
2950 let y = y - l.pagedispy in
2951 if y > 0
2952 then
2953 let y = l.pagey + y in
2954 let x = x - l.pagex in
2955 match whatsunder opaque x y with
2956 | Unone -> f rest
2957 | under -> under
2958 else
2959 f rest
2960 | _ ->
2961 f rest
2963 | [] -> Unone
2965 f state.layout
2968 let viewmouse button bstate x y =
2969 match button with
2970 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2971 if Glut.getModifiers () land Glut.active_ctrl != 0
2972 then (
2973 match state.mstate with
2974 | Mzoom (oldn, i) ->
2975 if oldn = n
2976 then (
2977 if i = 2
2978 then
2979 let incr =
2980 match n with
2981 | 4 ->
2982 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2983 | _ ->
2984 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2986 let zoom = conf.zoom +. incr in
2987 setzoom zoom;
2988 state.mstate <- Mzoom (n, 0);
2989 else
2990 state.mstate <- Mzoom (n, i+1);
2992 else state.mstate <- Mzoom (n, 0)
2994 | _ -> state.mstate <- Mzoom (n, 0)
2996 else (
2997 match state.autoscroll with
2998 | Some step -> setautoscrollspeed step (n=4)
2999 | None ->
3000 let incr =
3001 if n = 3
3002 then -conf.scrollstep
3003 else conf.scrollstep
3005 let incr = incr * 2 in
3006 let y = clamp incr in
3007 gotoy_and_clear_text y
3010 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3011 if bstate = Glut.DOWN
3012 then (
3013 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3014 state.mstate <- Mpan (x, y)
3016 else
3017 state.mstate <- Mnone
3019 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3020 if bstate = Glut.DOWN
3021 then
3022 let position, sh = scrollph state.y in
3023 if y > truncate position && y < truncate (position +. sh)
3024 then
3025 state.mstate <- Mscroll
3026 else
3027 let percent = float y /. float conf.winh in
3028 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3029 gotoy desty;
3030 state.mstate <- Mscroll
3031 else
3032 state.mstate <- Mnone
3034 | Glut.LEFT_BUTTON ->
3035 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3036 begin match dest with
3037 | Ulinkgoto (pageno, top) ->
3038 if pageno >= 0
3039 then (
3040 addnav ();
3041 gotopage1 pageno top;
3044 | Ulinkuri s ->
3045 print_endline s
3047 | Unone when bstate = Glut.DOWN ->
3048 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3049 state.mstate <- Mpan (x, y);
3051 | Unone | Utext _ ->
3052 if bstate = Glut.DOWN
3053 then (
3054 if conf.angle mod 360 = 0
3055 then (
3056 state.mstate <- Msel ((x, y), (x, y));
3057 Glut.postRedisplay ()
3060 else (
3061 match state.mstate with
3062 | Mnone -> ()
3064 | Mzoom _ | Mscroll ->
3065 state.mstate <- Mnone
3067 | Mpan _ ->
3068 Glut.setCursor Glut.CURSOR_INHERIT;
3069 state.mstate <- Mnone
3071 | Msel ((_, y0), (_, y1)) ->
3072 let f l =
3073 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3074 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3075 then
3076 match getopaque l.pageno with
3077 | Some (opaque, _) when validopaque opaque ->
3078 copysel opaque
3079 | _ -> ()
3081 List.iter f state.layout;
3082 copysel ""; (* ugly *)
3083 Glut.setCursor Glut.CURSOR_INHERIT;
3084 state.mstate <- Mnone;
3088 | _ -> ()
3091 let birdseyemouse button bstate x y
3092 (conf, leftx, _, hooverpageno, anchor) =
3093 match button with
3094 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3095 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3096 let rec loop = function
3097 | [] -> ()
3098 | l :: rest ->
3099 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3100 && x > margin && x < margin + l.pagew
3101 then (
3102 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3104 else loop rest
3106 loop state.layout
3107 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3108 | _ -> ()
3111 let mouse bstate button x y =
3112 match state.mode with
3113 | View -> viewmouse button bstate x y
3114 | Birdseye beye -> birdseyemouse button bstate x y beye
3115 | Textentry _ | Outline _ | Items _ -> ()
3118 let mouse ~button ~state ~x ~y = mouse state button x y;;
3120 let motion ~x ~y =
3121 match state.mode with
3122 | Outline _ -> ()
3123 | _ ->
3124 match state.mstate with
3125 | Mzoom _ | Mnone -> ()
3127 | Mpan (x0, y0) ->
3128 let dx = x - x0
3129 and dy = y0 - y in
3130 state.mstate <- Mpan (x, y);
3131 if conf.zoom > 1.0 then state.x <- state.x + dx;
3132 let y = clamp dy in
3133 gotoy_and_clear_text y
3135 | Msel (a, _) ->
3136 state.mstate <- Msel (a, (x, y));
3137 Glut.postRedisplay ()
3139 | Mscroll ->
3140 let y = min conf.winh (max 0 y) in
3141 let percent = float y /. float conf.winh in
3142 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3143 gotoy_and_clear_text y
3146 let pmotion ~x ~y =
3147 match state.mode with
3148 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3149 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3150 let rec loop = function
3151 | [] ->
3152 if hooverpageno != -1
3153 then (
3154 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3155 Glut.postRedisplay ();
3157 | l :: rest ->
3158 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3159 && x > margin && x < margin + l.pagew
3160 then (
3161 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3162 Glut.postRedisplay ();
3164 else loop rest
3166 loop state.layout
3168 | Outline _ | Items _ | Textentry _ -> ()
3169 | View ->
3170 match state.mstate with
3171 | Mnone ->
3172 begin match getunder x y with
3173 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3174 | Ulinkuri uri ->
3175 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3176 Glut.setCursor Glut.CURSOR_INFO
3177 | Ulinkgoto (page, _) ->
3178 if conf.underinfo
3179 then showtext 'p' ("age: " ^ string_of_int page);
3180 Glut.setCursor Glut.CURSOR_INFO
3181 | Utext s ->
3182 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3183 Glut.setCursor Glut.CURSOR_TEXT
3186 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3191 module State =
3192 struct
3193 open Parser
3195 let home =
3197 match Sys.os_type with
3198 | "Win32" -> Sys.getenv "HOMEPATH"
3199 | _ -> Sys.getenv "HOME"
3200 with exn ->
3201 prerr_endline
3202 ("Can not determine home directory location: " ^
3203 Printexc.to_string exn);
3207 let config_of c attrs =
3208 let apply c k v =
3210 match k with
3211 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3212 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3213 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3214 | "preload" -> { c with preload = bool_of_string v }
3215 | "page-bias" -> { c with pagebias = int_of_string v }
3216 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3217 | "auto-scroll-step" ->
3218 { c with autoscrollstep = max 0 (int_of_string v) }
3219 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3220 | "crop-hack" -> { c with crophack = bool_of_string v }
3221 | "throttle" -> { c with showall = bool_of_string v }
3222 | "highlight-links" -> { c with hlinks = bool_of_string v }
3223 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3224 | "vertical-margin" ->
3225 { c with interpagespace = max 0 (int_of_string v) }
3226 | "zoom" ->
3227 let zoom = float_of_string v /. 100. in
3228 let zoom = max 0.01 zoom in
3229 { c with zoom = zoom }
3230 | "presentation" -> { c with presentation = bool_of_string v }
3231 | "rotation-angle" -> { c with angle = int_of_string v }
3232 | "width" -> { c with winw = max 20 (int_of_string v) }
3233 | "height" -> { c with winh = max 20 (int_of_string v) }
3234 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3235 | "proportional-display" -> { c with proportional = bool_of_string v }
3236 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3237 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3238 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3239 | "max-tex-width" -> { c with blockwidth = max 2 (int_of_string v) }
3240 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3241 | "persistent-location" -> { c with jumpback = bool_of_string v }
3242 | "background-color" -> { c with bgcolor = color_of_string v }
3243 | "scrollbar-in-presentation" ->
3244 { c with scrollbarinpm = bool_of_string v }
3245 | "ui-font" -> { c with uifont = v }
3246 | _ -> c
3247 with exn ->
3248 prerr_endline ("Error processing attribute (`" ^
3249 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3252 let rec fold c = function
3253 | [] -> c
3254 | (k, v) :: rest ->
3255 let c = apply c k v in
3256 fold c rest
3258 fold c attrs;
3261 let fromstring f pos n v d =
3262 try f v
3263 with exn ->
3264 dolog "Error processing attribute (%S=%S) at %d\n%s"
3265 n v pos (Printexc.to_string exn)
3270 let bookmark_of attrs =
3271 let rec fold title page rely = function
3272 | ("title", v) :: rest -> fold v page rely rest
3273 | ("page", v) :: rest -> fold title v rely rest
3274 | ("rely", v) :: rest -> fold title page v rest
3275 | _ :: rest -> fold title page rely rest
3276 | [] -> title, page, rely
3278 fold "invalid" "0" "0" attrs
3281 let doc_of attrs =
3282 let rec fold path page rely pan = function
3283 | ("path", v) :: rest -> fold v page rely pan rest
3284 | ("page", v) :: rest -> fold path v rely pan rest
3285 | ("rely", v) :: rest -> fold path page v pan rest
3286 | ("pan", v) :: rest -> fold path page rely v rest
3287 | _ :: rest -> fold path page rely pan rest
3288 | [] -> path, page, rely, pan
3290 fold "" "0" "0" "0" attrs
3293 let setconf dst src =
3294 dst.scrollbw <- src.scrollbw;
3295 dst.scrollh <- src.scrollh;
3296 dst.icase <- src.icase;
3297 dst.preload <- src.preload;
3298 dst.pagebias <- src.pagebias;
3299 dst.verbose <- src.verbose;
3300 dst.scrollstep <- src.scrollstep;
3301 dst.maxhfit <- src.maxhfit;
3302 dst.crophack <- src.crophack;
3303 dst.autoscrollstep <- src.autoscrollstep;
3304 dst.showall <- src.showall;
3305 dst.hlinks <- src.hlinks;
3306 dst.underinfo <- src.underinfo;
3307 dst.interpagespace <- src.interpagespace;
3308 dst.zoom <- src.zoom;
3309 dst.presentation <- src.presentation;
3310 dst.angle <- src.angle;
3311 dst.winw <- src.winw;
3312 dst.winh <- src.winh;
3313 dst.savebmarks <- src.savebmarks;
3314 dst.memlimit <- src.memlimit;
3315 dst.proportional <- src.proportional;
3316 dst.texcount <- src.texcount;
3317 dst.sliceheight <- src.sliceheight;
3318 dst.blockwidth <- src.blockwidth;
3319 dst.thumbw <- src.thumbw;
3320 dst.jumpback <- src.jumpback;
3321 dst.bgcolor <- src.bgcolor;
3322 dst.scrollbarinpm <- src.scrollbarinpm;
3323 dst.uifont <- src.uifont;
3326 let unent s =
3327 let l = String.length s in
3328 let b = Buffer.create l in
3329 unent b s 0 l;
3330 Buffer.contents b;
3333 let get s =
3334 let h = Hashtbl.create 10 in
3335 let dc = { defconf with angle = defconf.angle } in
3336 let rec toplevel v t spos _ =
3337 match t with
3338 | Vdata | Vcdata | Vend -> v
3339 | Vopen ("llppconfig", _, closed) ->
3340 if closed
3341 then v
3342 else { v with f = llppconfig }
3343 | Vopen _ ->
3344 error "unexpected subelement at top level" s spos
3345 | Vclose _ -> error "unexpected close at top level" s spos
3347 and llppconfig v t spos _ =
3348 match t with
3349 | Vdata | Vcdata | Vend -> v
3350 | Vopen ("defaults", attrs, closed) ->
3351 let c = config_of dc attrs in
3352 setconf dc c;
3353 if closed
3354 then v
3355 else { v with f = skip "defaults" (fun () -> v) }
3357 | Vopen ("doc", attrs, closed) ->
3358 let pathent, spage, srely, span = doc_of attrs in
3359 let path = unent pathent
3360 and pageno = fromstring int_of_string spos "page" spage 0
3361 and rely = fromstring float_of_string spos "rely" srely 0.0
3362 and pan = fromstring int_of_string spos "pan" span 0 in
3363 let c = config_of dc attrs in
3364 let anchor = (pageno, rely) in
3365 if closed
3366 then (Hashtbl.add h path (c, [], pan, anchor); v)
3367 else { v with f = doc path pan anchor c [] }
3369 | Vopen _ ->
3370 error "unexpected subelement in llppconfig" s spos
3372 | Vclose "llppconfig" -> { v with f = toplevel }
3373 | Vclose _ -> error "unexpected close in llppconfig" s spos
3375 and doc path pan anchor c bookmarks v t spos _ =
3376 match t with
3377 | Vdata | Vcdata -> v
3378 | Vend -> error "unexpected end of input in doc" s spos
3379 | Vopen ("bookmarks", _, closed) ->
3380 if closed
3381 then v
3382 else { v with f = pbookmarks path pan anchor c bookmarks }
3384 | Vopen (_, _, _) ->
3385 error "unexpected subelement in doc" s spos
3387 | Vclose "doc" ->
3388 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3389 { v with f = llppconfig }
3391 | Vclose _ -> error "unexpected close in doc" s spos
3393 and pbookmarks path pan anchor c bookmarks v t spos _ =
3394 match t with
3395 | Vdata | Vcdata -> v
3396 | Vend -> error "unexpected end of input in bookmarks" s spos
3397 | Vopen ("item", attrs, closed) ->
3398 let titleent, spage, srely = bookmark_of attrs in
3399 let page = fromstring int_of_string spos "page" spage 0
3400 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3401 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3402 if closed
3403 then { v with f = pbookmarks path pan anchor c bookmarks }
3404 else
3405 let f () = v in
3406 { v with f = skip "item" f }
3408 | Vopen _ ->
3409 error "unexpected subelement in bookmarks" s spos
3411 | Vclose "bookmarks" ->
3412 { v with f = doc path pan anchor c bookmarks }
3414 | Vclose _ -> error "unexpected close in bookmarks" s spos
3416 and skip tag f v t spos _ =
3417 match t with
3418 | Vdata | Vcdata -> v
3419 | Vend ->
3420 error ("unexpected end of input in skipped " ^ tag) s spos
3421 | Vopen (tag', _, closed) ->
3422 if closed
3423 then v
3424 else
3425 let f' () = { v with f = skip tag f } in
3426 { v with f = skip tag' f' }
3427 | Vclose ctag ->
3428 if tag = ctag
3429 then f ()
3430 else error ("unexpected close in skipped " ^ tag) s spos
3433 parse { f = toplevel; accu = () } s;
3434 h, dc;
3437 let do_load f ic =
3439 let len = in_channel_length ic in
3440 let s = String.create len in
3441 really_input ic s 0 len;
3442 f s;
3443 with
3444 | Parse_error (msg, s, pos) ->
3445 let subs = subs s pos in
3446 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3447 failwith ("parse error: " ^ s)
3449 | exn ->
3450 failwith ("config load error: " ^ Printexc.to_string exn)
3453 let path =
3454 let dir =
3456 let dir = Filename.concat home ".config" in
3457 if Sys.is_directory dir then dir else home
3458 with _ -> home
3460 Filename.concat dir "llpp.conf"
3463 let load1 f =
3464 if Sys.file_exists path
3465 then
3466 match
3467 (try Some (open_in_bin path)
3468 with exn ->
3469 prerr_endline
3470 ("Error opening configuation file `" ^ path ^ "': " ^
3471 Printexc.to_string exn);
3472 None
3474 with
3475 | Some ic ->
3476 begin try
3477 f (do_load get ic)
3478 with exn ->
3479 prerr_endline
3480 ("Error loading configuation from `" ^ path ^ "': " ^
3481 Printexc.to_string exn);
3482 end;
3483 close_in ic;
3485 | None -> ()
3486 else
3487 f (Hashtbl.create 0, defconf)
3490 let load () =
3491 let f (h, dc) =
3492 let pc, pb, px, pa =
3494 Hashtbl.find h (Filename.basename state.path)
3495 with Not_found -> dc, [], 0, (0, 0.0)
3497 setconf defconf dc;
3498 setconf conf pc;
3499 state.bookmarks <- pb;
3500 state.x <- px;
3501 state.scrollw <- conf.scrollbw;
3502 if conf.jumpback
3503 then state.anchor <- pa;
3504 cbput state.hists.nav pa;
3506 load1 f
3509 let add_attrs bb always dc c =
3510 let ob s a b =
3511 if always || a != b
3512 then Printf.bprintf bb "\n %s='%b'" s a
3513 and oi s a b =
3514 if always || a != b
3515 then Printf.bprintf bb "\n %s='%d'" s a
3516 and oz s a b =
3517 if always || a <> b
3518 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3519 and oc s a b =
3520 if always || a <> b
3521 then
3522 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3523 and os s a b =
3524 if always || a <> b
3525 then
3526 Printf.bprintf bb "\n %s='%s'" s a
3528 let w, h =
3529 if always
3530 then dc.winw, dc.winh
3531 else
3532 match state.fullscreen with
3533 | Some wh -> wh
3534 | None -> c.winw, c.winh
3536 let zoom, presentation, interpagespace, showall=
3537 if always
3538 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3539 else
3540 match state.mode with
3541 | Birdseye (bc, _, _, _, _) ->
3542 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3543 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3545 oi "width" w dc.winw;
3546 oi "height" h dc.winh;
3547 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3548 oi "scroll-handle-height" c.scrollh dc.scrollh;
3549 ob "case-insensitive-search" c.icase dc.icase;
3550 ob "preload" c.preload dc.preload;
3551 oi "page-bias" c.pagebias dc.pagebias;
3552 oi "scroll-step" c.scrollstep dc.scrollstep;
3553 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3554 ob "max-height-fit" c.maxhfit dc.maxhfit;
3555 ob "crop-hack" c.crophack dc.crophack;
3556 ob "throttle" showall dc.showall;
3557 ob "highlight-links" c.hlinks dc.hlinks;
3558 ob "under-cursor-info" c.underinfo dc.underinfo;
3559 oi "vertical-margin" interpagespace dc.interpagespace;
3560 oz "zoom" zoom dc.zoom;
3561 ob "presentation" presentation dc.presentation;
3562 oi "rotation-angle" c.angle dc.angle;
3563 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3564 ob "proportional-display" c.proportional dc.proportional;
3565 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3566 oi "texcount" c.texcount dc.texcount;
3567 oi "slice-height" c.sliceheight dc.sliceheight;
3568 oi "max-tex-width" c.blockwidth dc.blockwidth;
3569 oi "thumbnail-width" c.thumbw dc.thumbw;
3570 ob "persistent-location" c.jumpback dc.jumpback;
3571 oc "background-color" c.bgcolor dc.bgcolor;
3572 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3573 os "ui-font" c.uifont dc.uifont;
3576 let save () =
3577 let bb = Buffer.create 32768 in
3578 let f (h, dc) =
3579 let dc = if conf.bedefault then conf else dc in
3580 Buffer.add_string bb "<llppconfig>\n<defaults ";
3581 add_attrs bb true dc dc;
3582 Buffer.add_string bb "/>\n";
3584 let adddoc path pan anchor c bookmarks =
3585 if bookmarks == [] && c = dc && anchor = emptyanchor
3586 then ()
3587 else (
3588 Printf.bprintf bb "<doc path='%s'"
3589 (enent path 0 (String.length path));
3591 if anchor <> emptyanchor
3592 then (
3593 let n, y = anchor in
3594 Printf.bprintf bb " page='%d'" n;
3595 if y > 1e-6
3596 then
3597 Printf.bprintf bb " rely='%f'" y
3601 if pan != 0
3602 then Printf.bprintf bb " pan='%d'" pan;
3604 add_attrs bb false dc c;
3606 begin match bookmarks with
3607 | [] -> Buffer.add_string bb "/>\n"
3608 | _ ->
3609 Buffer.add_string bb ">\n<bookmarks>\n";
3610 List.iter (fun (title, _level, (page, rely)) ->
3611 Printf.bprintf bb
3612 "<item title='%s' page='%d'"
3613 (enent title 0 (String.length title))
3614 page
3616 if rely > 1e-6
3617 then
3618 Printf.bprintf bb " rely='%f'" rely
3620 Buffer.add_string bb "/>\n";
3621 ) bookmarks;
3622 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3623 end;
3627 let pan =
3628 match state.mode with
3629 | Birdseye (_, pan, _, _, _) -> pan
3630 | _ -> state.x
3632 let basename = Filename.basename state.path in
3633 adddoc basename pan (getanchor ())
3634 { conf with
3635 autoscrollstep =
3636 match state.autoscroll with
3637 | Some step -> step
3638 | None -> conf.autoscrollstep }
3639 (if conf.savebmarks then state.bookmarks else []);
3641 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3642 if basename <> path
3643 then adddoc path x y c bookmarks
3644 ) h;
3645 Buffer.add_string bb "</llppconfig>";
3647 load1 f;
3648 if Buffer.length bb > 0
3649 then
3651 let tmp = path ^ ".tmp" in
3652 let oc = open_out_bin tmp in
3653 Buffer.output_buffer oc bb;
3654 close_out oc;
3655 Sys.rename tmp path;
3656 with exn ->
3657 prerr_endline
3658 ("error while saving configuration: " ^ Printexc.to_string exn)
3660 end;;
3662 let () =
3663 Arg.parse
3664 (Arg.align
3665 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3666 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3667 " Print version and exit")]
3669 (fun s -> state.path <- s)
3670 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3672 if String.length state.path = 0
3673 then (prerr_endline "file name missing"; exit 1);
3675 State.load ();
3677 let _ = Glut.init Sys.argv in
3678 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3679 let () = Glut.initWindowSize conf.winw conf.winh in
3680 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3682 let csock, ssock =
3683 if Sys.os_type = "Unix"
3684 then
3685 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3686 else
3687 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3688 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3689 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3690 Unix.bind sock addr;
3691 Unix.listen sock 1;
3692 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3693 Unix.connect csock addr;
3694 let ssock, _ = Unix.accept sock in
3695 Unix.close sock;
3696 let opts sock =
3697 Unix.setsockopt sock Unix.TCP_NODELAY true;
3698 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3700 opts ssock;
3701 opts csock;
3702 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3703 ssock, csock
3706 let () = Glut.displayFunc display in
3707 let () = Glut.reshapeFunc reshape in
3708 let () = Glut.keyboardFunc keyboard in
3709 let () = Glut.specialFunc special in
3710 let () = Glut.idleFunc (Some idle) in
3711 let () = Glut.mouseFunc mouse in
3712 let () = Glut.motionFunc motion in
3713 let () = Glut.passiveMotionFunc pmotion in
3715 init ssock (
3716 conf.angle, conf.proportional, conf.texcount,
3717 conf.sliceheight, conf.blockwidth, conf.uifont
3719 state.csock <- csock;
3720 state.ssock <- ssock;
3721 state.text <- "Opening " ^ state.path;
3722 writeopen state.path state.password;
3724 at_exit State.save;
3726 let rec handlelablglutbug () =
3728 Glut.mainLoop ();
3729 with Glut.BadEnum "key in special_of_int" ->
3730 showtext '!' " LablGlut bug: special key not recognized";
3731 handlelablglutbug ()
3733 handlelablglutbug ();