Increase items text size by a point
[llpp.git] / main.ml
blob23eb5564971ecab76b849e59889f4c4bb41348bd
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params = angle * proportional * texcount * sliceheight * fontpath
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
25 and fontpath = string
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
37 external measurestr : int -> string -> float = "ml_measure_string";;
39 type mpos = int * int
40 and mstate =
41 | Msel of (mpos * mpos)
42 | Mpan of mpos
43 | Mscroll
44 | Mzoom of (int * int)
45 | Mnone
48 type textentry = string * string * onhist option * onkey * ondone
49 and onkey = string -> int -> te
50 and ondone = string -> unit
51 and histcancel = unit -> unit
52 and onhist = ((histcmd -> string) * histcancel)
53 and histcmd = HCnext | HCprev | HCfirst | HClast
54 and te =
55 | TEstop
56 | TEdone of string
57 | TEcont of string
58 | TEswitch of textentry
61 type 'a circbuf =
62 { store : 'a array
63 ; mutable rc : int
64 ; mutable wc : int
65 ; mutable len : int
69 let cbnew n v =
70 { store = Array.create n v
71 ; rc = 0
72 ; wc = 0
73 ; len = 0
77 let cbcap b = Array.length b.store;;
79 let cbput b v =
80 let cap = cbcap b in
81 b.store.(b.wc) <- v;
82 b.wc <- (b.wc + 1) mod cap;
83 b.rc <- b.wc;
84 b.len <- min (b.len + 1) cap;
87 let cbempty b = b.len = 0;;
89 let cbgetg b circular dir =
90 if cbempty b
91 then b.store.(0)
92 else
93 let rc = b.rc + dir in
94 let rc =
95 if circular
96 then (
97 if rc = -1
98 then b.len-1
99 else (
100 if rc = b.len
101 then 0
102 else rc
105 else max 0 (min rc (b.len-1))
107 b.rc <- rc;
108 b.store.(rc);
111 let cbget b = cbgetg b false;;
112 let cbgetc b = cbgetg b true;;
114 let cbpeek b =
115 let rc = b.wc - b.len in
116 let rc = if rc < 0 then cbcap b + rc else rc in
117 b.store.(rc);
120 let cbdecr b = b.len <- b.len - 1;;
122 type layout =
123 { pageno : int
124 ; pagedimno : int
125 ; pagew : int
126 ; pageh : int
127 ; pagedispy : int
128 ; pagey : int
129 ; pagevh : int
130 ; pagex : int
134 type conf =
135 { mutable scrollbw : int
136 ; mutable scrollh : int
137 ; mutable icase : bool
138 ; mutable preload : bool
139 ; mutable pagebias : int
140 ; mutable verbose : bool
141 ; mutable scrollstep : int
142 ; mutable maxhfit : bool
143 ; mutable crophack : bool
144 ; mutable autoscrollstep : int
145 ; mutable showall : bool
146 ; mutable hlinks : bool
147 ; mutable underinfo : bool
148 ; mutable interpagespace : interpagespace
149 ; mutable zoom : float
150 ; mutable presentation : bool
151 ; mutable angle : angle
152 ; mutable winw : int
153 ; mutable winh : int
154 ; mutable savebmarks : bool
155 ; mutable proportional : proportional
156 ; mutable memlimit : int
157 ; mutable texcount : texcount
158 ; mutable sliceheight : sliceheight
159 ; mutable thumbw : width
160 ; mutable jumpback : bool
161 ; mutable bgcolor : float * float * float
162 ; mutable bedefault : bool
163 ; mutable scrollbarinpm : bool
164 ; mutable uifont : string
168 type anchor = pageno * top;;
170 type outline = string * int * anchor
171 and outlines =
172 | Oarray of outline array
173 | Olist of outline list
174 | Onarrow of string * outline array * outline array
177 type rect = float * float * float * float * float * float * float * float;;
179 type pagemapkey = pageno * width * angle * proportional * gen;;
181 let emptyanchor = (0, 0.0);;
182 let initialanchor = (-1, nan);;
184 type mode =
185 | Birdseye of (conf * leftx * pageno * pageno * anchor)
186 | Outline of (bool * int * int * outline array * string * int * mode)
187 | Items of (int * int * item array * string * int * mode)
188 | Textentry of (textentry * onleave)
189 | View
190 and onleave = leavetextentrystatus -> unit
191 and leavetextentrystatus = | Cancel | Confirm
192 and item = string * int * action
193 and action =
194 | Noaction
195 | Action of (int -> int -> string -> int -> mode)
198 let isbirdseye = function Birdseye _ -> true | _ -> false;;
199 let istextentry = function Textentry _ -> true | _ -> false;;
201 type state =
202 { mutable csock : Unix.file_descr
203 ; mutable ssock : Unix.file_descr
204 ; mutable w : int
205 ; mutable x : int
206 ; mutable y : int
207 ; mutable scrollw : int
208 ; mutable anchor : anchor
209 ; mutable maxy : int
210 ; mutable layout : layout list
211 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
212 ; mutable pdims : (pageno * width * height * leftx) list
213 ; mutable pagecount : int
214 ; pagecache : string circbuf
215 ; mutable rendering : bool
216 ; mutable mstate : mstate
217 ; mutable searchpattern : string
218 ; mutable rects : (pageno * recttype * rect) list
219 ; mutable rects1 : (pageno * recttype * rect) list
220 ; mutable text : string
221 ; mutable fullscreen : (width * height) option
222 ; mutable mode : mode
223 ; mutable outlines : outlines
224 ; mutable bookmarks : outline list
225 ; mutable path : string
226 ; mutable password : string
227 ; mutable invalidated : int
228 ; mutable colorscale : float
229 ; mutable memused : int
230 ; mutable gen : gen
231 ; mutable throttle : layout list option
232 ; mutable ascrollstep : int
233 ; mutable help : item array
234 ; mutable docinfo : (int * string) list
235 ; hists : hists
237 and hists =
238 { pat : string circbuf
239 ; pag : string circbuf
240 ; nav : anchor circbuf
244 let defconf =
245 { scrollbw = 7
246 ; scrollh = 12
247 ; icase = true
248 ; preload = true
249 ; pagebias = 0
250 ; verbose = false
251 ; scrollstep = 24
252 ; maxhfit = true
253 ; crophack = false
254 ; autoscrollstep = 24
255 ; showall = false
256 ; hlinks = false
257 ; underinfo = false
258 ; interpagespace = 2
259 ; zoom = 1.0
260 ; presentation = false
261 ; angle = 0
262 ; winw = 900
263 ; winh = 900
264 ; savebmarks = true
265 ; proportional = true
266 ; memlimit = 32*1024*1024
267 ; texcount = 256
268 ; sliceheight = 24
269 ; thumbw = 76
270 ; jumpback = false
271 ; bgcolor = (0.5, 0.5, 0.5)
272 ; bedefault = false
273 ; scrollbarinpm = true
274 ; uifont = ""
278 let conf = { defconf with angle = defconf.angle };;
280 let makehelp () =
281 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
282 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
285 let state =
286 { csock = Unix.stdin
287 ; ssock = Unix.stdin
288 ; x = 0
289 ; y = 0
290 ; w = 0
291 ; scrollw = 0
292 ; anchor = initialanchor
293 ; layout = []
294 ; maxy = max_int
295 ; pagemap = Hashtbl.create 10
296 ; pagecache = cbnew 100 ""
297 ; pdims = []
298 ; pagecount = 0
299 ; rendering = false
300 ; mstate = Mnone
301 ; rects = []
302 ; rects1 = []
303 ; text = ""
304 ; mode = View
305 ; fullscreen = None
306 ; searchpattern = ""
307 ; outlines = Olist []
308 ; bookmarks = []
309 ; path = ""
310 ; password = ""
311 ; invalidated = 0
312 ; hists =
313 { nav = cbnew 100 (0, 0.0)
314 ; pat = cbnew 20 ""
315 ; pag = cbnew 10 ""
317 ; colorscale = 1.0
318 ; memused = 0
319 ; gen = 0
320 ; throttle = None
321 ; ascrollstep = 0
322 ; help = makehelp ()
323 ; docinfo = []
327 let vlog fmt =
328 if conf.verbose
329 then
330 Printf.kprintf prerr_endline fmt
331 else
332 Printf.kprintf ignore fmt
335 let writecmd fd s =
336 let len = String.length s in
337 let n = 4 + len in
338 let b = Buffer.create n in
339 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
340 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
341 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
342 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
343 Buffer.add_string b s;
344 let s' = Buffer.contents b in
345 let n' = Unix.write fd s' 0 n in
346 if n' != n then failwith "write failed";
349 let readcmd fd =
350 let s = "xxxx" in
351 let n = Unix.read fd s 0 4 in
352 if n != 4 then failwith "incomplete read(len)";
353 let len = 0
354 lor (Char.code s.[0] lsl 24)
355 lor (Char.code s.[1] lsl 16)
356 lor (Char.code s.[2] lsl 8)
357 lor (Char.code s.[3] lsl 0)
359 let s = String.create len in
360 let n = Unix.read fd s 0 len in
361 if n != len then failwith "incomplete read(data)";
365 let makecmd s l =
366 let b = Buffer.create 10 in
367 Buffer.add_string b s;
368 let rec combine = function
369 | [] -> b
370 | x :: xs ->
371 Buffer.add_char b ' ';
372 let s =
373 match x with
374 | `b b -> if b then "1" else "0"
375 | `s s -> s
376 | `i i -> string_of_int i
377 | `f f -> string_of_float f
378 | `I f -> string_of_int (truncate f)
380 Buffer.add_string b s;
381 combine xs;
383 combine l;
386 let wcmd s l =
387 let cmd = Buffer.contents (makecmd s l) in
388 writecmd state.csock cmd;
391 let calcips h =
392 if conf.presentation
393 then
394 let d = conf.winh - h in
395 max 0 ((d + 1) / 2)
396 else
397 conf.interpagespace
400 let calcheight () =
401 let rec f pn ph pi fh l =
402 match l with
403 | (n, _, h, _) :: rest ->
404 let ips = calcips h in
405 let fh =
406 if conf.presentation
407 then fh+ips
408 else (
409 if isbirdseye state.mode && pn = 0
410 then fh + ips
411 else fh
414 let fh = fh + ((n - pn) * (ph + pi)) in
415 f n h ips fh rest;
417 | [] ->
418 let inc =
419 if conf.presentation || (isbirdseye state.mode && pn = 0)
420 then 0
421 else -pi
423 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
424 max 0 fh
426 let fh = f 0 0 0 0 state.pdims in
430 let getpageyh pageno =
431 let rec f pn ph pi y l =
432 match l with
433 | (n, _, h, _) :: rest ->
434 let ips = calcips h in
435 if n >= pageno
436 then
437 let h = if n = pageno then h else ph in
438 if conf.presentation && n = pageno
439 then
440 y + (pageno - pn) * (ph + pi) + pi, h
441 else
442 y + (pageno - pn) * (ph + pi), h
443 else
444 let y = y + (if conf.presentation then pi else 0) in
445 let y = y + (n - pn) * (ph + pi) in
446 f n h ips y rest
448 | [] ->
449 y + (pageno - pn) * (ph + pi), ph
451 f 0 0 0 0 state.pdims
454 let getpagey pageno = fst (getpageyh pageno);;
456 let layout y sh =
457 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
458 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
459 match pdims with
460 | (pageno', w, h, x) :: rest when pageno' = pageno ->
461 let ips = calcips h in
462 let yinc =
463 if conf.presentation || (isbirdseye state.mode && pageno = 0)
464 then ips
465 else 0
467 (w, h, ips, x), rest, pdimno + 1, yinc
468 | _ ->
469 prev, pdims, pdimno, 0
471 let dy = dy + yinc in
472 let py = py + yinc in
473 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
474 then
475 accu
476 else
477 let vy = y + dy in
478 if py + h <= vy - yinc
479 then
480 let py = py + h + ips in
481 let dy = max 0 (py - y) in
482 f ~pageno:(pageno+1)
483 ~pdimno
484 ~prev:curr
487 ~pdims:rest
488 ~cacheleft
489 ~accu
490 else
491 let pagey = vy - py in
492 let pagevh = h - pagey in
493 let pagevh = min (sh - dy) pagevh in
494 let off = if yinc > 0 then py - vy else 0 in
495 let py = py + h + ips in
496 let e =
497 { pageno = pageno
498 ; pagedimno = pdimno
499 ; pagew = w
500 ; pageh = h
501 ; pagedispy = dy + off
502 ; pagey = pagey + off
503 ; pagevh = pagevh - off
504 ; pagex = x
507 let accu = e :: accu in
508 f ~pageno:(pageno+1)
509 ~pdimno
510 ~prev:curr
512 ~dy:(dy+pagevh+ips)
513 ~pdims:rest
514 ~cacheleft:(cacheleft-1)
515 ~accu
517 if state.invalidated = 0
518 then (
519 let accu =
521 ~pageno:0
522 ~pdimno:~-1
523 ~prev:(0,0,0,0)
524 ~py:0
525 ~dy:0
526 ~pdims:state.pdims
527 ~cacheleft:(cbcap state.pagecache)
528 ~accu:[]
530 List.rev accu
532 else
536 let clamp incr =
537 let y = state.y + incr in
538 let y = max 0 y in
539 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
543 let getopaque pageno =
544 try Some (Hashtbl.find state.pagemap
545 (pageno, state.w, conf.angle, conf.proportional, state.gen))
546 with Not_found -> None
549 let cache pageno opaque =
550 Hashtbl.replace state.pagemap
551 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
554 let validopaque opaque = String.length opaque > 0;;
556 let render l =
557 match getopaque l.pageno with
558 | None when not state.rendering ->
559 state.rendering <- true;
560 cache l.pageno ("", -1);
561 wcmd "render" [`i (l.pageno + 1)
562 ;`i l.pagedimno
563 ;`i l.pagew
564 ;`i l.pageh];
565 | _ -> ()
568 let loadlayout layout =
569 let rec f all = function
570 | l :: ls ->
571 begin match getopaque l.pageno with
572 | None -> render l; f false ls
573 | Some (opaque, _) -> f (all && validopaque opaque) ls
575 | [] -> all
577 f (layout <> []) layout;
580 let findpageforopaque opaque =
581 Hashtbl.fold
582 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
583 state.pagemap None
586 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
588 let preload () =
589 let oktopreload =
590 if conf.preload
591 then
592 let memleft = conf.memlimit - state.memused in
593 if memleft < 0
594 then
595 let opaque = cbpeek state.pagecache in
596 match findpageforopaque opaque with
597 | Some ((n, _, _, _, _), size) ->
598 memleft + size >= 0 && not (pagevisible state.layout n)
599 | None -> false
600 else true
601 else false
603 if oktopreload
604 then
605 let presentation = conf.presentation in
606 let interpagespace = conf.interpagespace in
607 let maxy = state.maxy in
608 conf.presentation <- false;
609 conf.interpagespace <- 0;
610 state.maxy <- calcheight ();
611 let y =
612 match state.layout with
613 | [] -> 0
614 | l :: _ -> getpagey l.pageno + l.pagey
616 let y = if y < conf.winh then 0 else y - conf.winh in
617 let pages = layout y (conf.winh*3) in
618 List.iter render pages;
619 conf.presentation <- presentation;
620 conf.interpagespace <- interpagespace;
621 state.maxy <- maxy;
624 let gotoy y =
625 let y = max 0 y in
626 let y = min state.maxy y in
627 let pages = layout y conf.winh in
628 let ready = loadlayout pages in
629 if conf.showall
630 then (
631 if ready
632 then (
633 state.y <- y;
634 state.layout <- pages;
635 state.throttle <- None;
636 Glut.postRedisplay ();
638 else (
639 state.throttle <- Some pages;
642 else (
643 state.y <- y;
644 state.layout <- pages;
645 state.throttle <- None;
646 Glut.postRedisplay ();
648 begin match state.mode with
649 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
650 if not (pagevisible pages pageno)
651 then (
652 match state.layout with
653 | [] -> ()
654 | l :: _ ->
655 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
657 | _ -> ()
658 end;
659 preload ();
662 let gotoy_and_clear_text y =
663 gotoy y;
664 if not conf.verbose then state.text <- "";
667 let getanchor () =
668 match state.layout with
669 | [] -> emptyanchor
670 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
673 let getanchory (n, top) =
674 let y, h = getpageyh n in
675 y + (truncate (top *. float h));
678 let gotoanchor anchor =
679 gotoy (getanchory anchor);
682 let addnav () =
683 cbput state.hists.nav (getanchor ());
686 let getnav dir =
687 let anchor = cbgetc state.hists.nav dir in
688 getanchory anchor;
691 let gotopage n top =
692 let y, h = getpageyh n in
693 gotoy_and_clear_text (y + (truncate (top *. float h)));
696 let gotopage1 n top =
697 let y = getpagey n in
698 gotoy_and_clear_text (y + top);
701 let invalidate () =
702 state.layout <- [];
703 state.pdims <- [];
704 state.rects <- [];
705 state.rects1 <- [];
706 state.invalidated <- state.invalidated + 1;
709 let scalecolor c =
710 let c = c *. state.colorscale in
711 (c, c, c);
714 let scalecolor2 (r, g, b) =
715 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
718 let represent () =
719 state.maxy <- calcheight ();
720 match state.mode with
721 | Birdseye (_, _, pageno, _, _) ->
722 let y, h = getpageyh pageno in
723 let top = (conf.winh - h) / 2 in
724 gotoy (max 0 (y - top))
725 | _ -> gotoanchor state.anchor
728 let pagematrix () =
729 GlMat.mode `projection;
730 GlMat.load_identity ();
731 GlMat.rotate ~x:1.0 ~angle:180.0 ();
732 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
733 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
734 if state.x != 0
735 then (
736 GlMat.translate ~x:(float state.x) ();
740 let winmatrix () =
741 GlMat.mode `projection;
742 GlMat.load_identity ();
743 GlMat.rotate ~x:1.0 ~angle:180.0 ();
744 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
745 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
748 let reshape ~w ~h =
749 if state.invalidated = 0 && state.anchor == initialanchor
750 then state.anchor <- getanchor ();
752 conf.winw <- w;
753 let w = truncate (float w *. conf.zoom) - state.scrollw in
754 let w = max w 2 in
755 state.w <- w;
756 conf.winh <- h;
757 GlMat.mode `modelview;
758 GlMat.load_identity ();
759 GlClear.color (scalecolor 1.0);
760 GlClear.clear [`color];
762 invalidate ();
763 wcmd "geometry" [`i w; `i h];
766 let drawstring size x y s =
767 Gl.enable `blend;
768 Gl.enable `texture_2d;
769 ignore (drawstr size x y s);
770 Gl.disable `blend;
771 Gl.disable `texture_2d;
774 let drawstring1 size x y s =
775 drawstr size x y s;
778 let enttext () =
779 let drawstring s =
780 GlDraw.color (0.0, 0.0, 0.0);
781 GlDraw.rect
782 (0.0, float (conf.winh - 18))
783 (float (conf.winw - state.scrollw - 1), float conf.winh)
785 GlDraw.color (1.0, 1.0, 1.0);
786 drawstring 14 8 (conf.winh - 5) s;
788 let len = String.length state.text in
789 match state.mode with
790 | Textentry ((prefix, text, _, _, _), _) ->
791 let s =
792 match String.length prefix with
793 | 0 | 1 ->
794 if len > 0
795 then
796 Printf.sprintf "%s%s_ [%s]" prefix text state.text
797 else
798 Printf.sprintf "%s%s_" prefix text
800 | _ ->
801 if len > 0
802 then
803 Printf.sprintf "%s: %s_ [%s]" prefix text state.text
804 else
805 Printf.sprintf "%s: %s_" prefix text
807 drawstring s
809 | _ ->
810 if len > 0 then drawstring state.text
813 let showtext c s =
814 state.text <- Printf.sprintf "%c%s" c s;
815 Glut.postRedisplay ();
818 let act cmd =
819 match cmd.[0] with
820 | 'c' ->
821 state.pdims <- [];
823 | 'D' ->
824 state.rects <- state.rects1;
825 Glut.postRedisplay ()
827 | 'C' ->
828 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
829 state.pagecount <- n;
830 state.invalidated <- state.invalidated - 1;
831 if state.invalidated = 0
832 then represent ()
834 | 't' ->
835 let s = Scanf.sscanf cmd "t %n"
836 (fun n -> String.sub cmd n (String.length cmd - n))
838 Glut.setWindowTitle s
840 | 'T' ->
841 let s = Scanf.sscanf cmd "T %n"
842 (fun n -> String.sub cmd n (String.length cmd - n))
844 if istextentry state.mode
845 then (
846 state.text <- s;
847 showtext ' ' s;
849 else (
850 state.text <- s;
851 Glut.postRedisplay ();
854 | 'V' ->
855 if conf.verbose
856 then
857 let s = Scanf.sscanf cmd "V %n"
858 (fun n -> String.sub cmd n (String.length cmd - n))
860 state.text <- s;
861 showtext ' ' s;
863 | 'F' ->
864 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
865 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
866 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
867 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
869 let y = (getpagey pageno) + truncate y0 in
870 addnav ();
871 gotoy y;
872 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
874 | 'R' ->
875 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
876 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
877 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
878 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
880 state.rects1 <-
881 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
883 | 'r' ->
884 let n, w, _h, r, l, s, p =
885 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
886 (fun n w h r l s p ->
887 (n-1, w, h, r, l != 0, s, p))
890 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
891 state.memused <- state.memused + s;
893 let layout =
894 match state.throttle with
895 | None -> state.layout
896 | Some layout -> layout
899 let rec gc () =
900 if (state.memused <= conf.memlimit) || cbempty state.pagecache
901 then ()
902 else (
903 let evictedopaque = cbpeek state.pagecache in
904 match findpageforopaque evictedopaque with
905 | None -> failwith "bug in gc"
906 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
907 if state.gen != gen || not (pagevisible layout evictedn)
908 then (
909 wcmd "free" [`s evictedopaque];
910 state.memused <- state.memused - evictedsize;
911 Hashtbl.remove state.pagemap k;
912 cbdecr state.pagecache;
913 gc ();
917 gc ();
919 cbput state.pagecache p;
920 state.rendering <- false;
922 begin match state.throttle with
923 | None ->
924 if pagevisible state.layout n
925 then gotoy state.y
926 else (
927 let allvisible = loadlayout state.layout in
928 if allvisible then preload ();
931 | Some layout ->
932 match layout with
933 | [] -> ()
934 | l :: _ ->
935 let y = getpagey l.pageno + l.pagey in
936 gotoy y
939 | 'l' ->
940 let pdim =
941 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
943 state.pdims <- pdim :: state.pdims
945 | 'o' ->
946 let (l, n, t, h, pos) =
947 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
949 let s = String.sub cmd pos (String.length cmd - pos) in
950 let outline = (s, l, (n, float t /. float h)) in
951 let outlines =
952 match state.outlines with
953 | Olist outlines -> Olist (outline :: outlines)
954 | Oarray _ -> Olist [outline]
955 | Onarrow _ -> Olist [outline]
957 state.outlines <- outlines
960 | 'i' ->
961 let s = Scanf.sscanf cmd "i %n"
962 (fun n -> String.sub cmd n (String.length cmd - n))
964 let len = String.length s in
965 let rec fold accu pos =
966 let eolpos =
967 try String.index_from s pos '\n' with Not_found -> len
969 if eolpos = len
970 then List.rev accu
971 else
972 let line = String.sub s pos (eolpos - pos) in
973 fold ((1, line)::accu) (eolpos+1)
975 state.docinfo <- fold state.docinfo 0
977 | _ ->
978 dolog "unknown cmd `%S'" cmd
981 let now = Unix.gettimeofday;;
983 let idle () =
984 let rec loop delay =
985 let r, _, _ = Unix.select [state.csock] [] [] delay in
986 begin match r with
987 | [] ->
988 if state.ascrollstep > 0
989 then begin
990 let y = state.y + state.ascrollstep in
991 let y = if y >= state.maxy then 0 else y in
992 gotoy y;
993 state.text <- "";
994 end;
996 | _ ->
997 let cmd = readcmd state.csock in
998 act cmd;
999 loop 0.0
1000 end;
1001 in loop 0.001
1004 let onhist cb =
1005 let rc = cb.rc in
1006 let action = function
1007 | HCprev -> cbget cb ~-1
1008 | HCnext -> cbget cb 1
1009 | HCfirst -> cbget cb ~-(cb.rc)
1010 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1011 and cancel () = cb.rc <- rc
1012 in (action, cancel)
1015 let search pattern forward =
1016 if String.length pattern > 0
1017 then
1018 let pn, py =
1019 match state.layout with
1020 | [] -> 0, 0
1021 | l :: _ ->
1022 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1024 let cmd =
1025 let b = makecmd "search"
1026 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1028 Buffer.add_char b ',';
1029 Buffer.add_string b pattern;
1030 Buffer.add_char b '\000';
1031 Buffer.contents b;
1033 writecmd state.csock cmd;
1036 let intentry text key =
1037 let c = Char.unsafe_chr key in
1038 match c with
1039 | '0' .. '9' ->
1040 let s = "x" in s.[0] <- c;
1041 let text = text ^ s in
1042 TEcont text
1044 | _ ->
1045 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1046 TEcont text
1049 let addchar s c =
1050 let b = Buffer.create (String.length s + 1) in
1051 Buffer.add_string b s;
1052 Buffer.add_char b c;
1053 Buffer.contents b;
1056 let textentry text key =
1057 let c = Char.unsafe_chr key in
1058 match c with
1059 | _ when key >= 32 && key < 127 ->
1060 let text = addchar text c in
1061 TEcont text
1063 | _ ->
1064 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1065 TEcont text
1068 let reinit angle proportional =
1069 conf.angle <- angle;
1070 conf.proportional <- proportional;
1071 invalidate ();
1072 wcmd "reinit" [`i angle; `b proportional];
1075 let setzoom zoom =
1076 let zoom = max 0.01 (min 2.2 zoom) in
1077 if zoom <> conf.zoom
1078 then (
1079 if zoom <= 1.0
1080 then state.x <- 0;
1081 conf.zoom <- zoom;
1082 if state.invalidated = 0
1083 then
1084 state.anchor <- getanchor ()
1086 reshape conf.winw conf.winh;
1087 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1091 let enterbirdseye () =
1092 let zoom = float conf.thumbw /. float conf.winw in
1093 let birdseyepageno =
1094 let cy = conf.winh / 2 in
1095 let fold = function
1096 | [] -> 0
1097 | l :: rest ->
1098 let rec fold best = function
1099 | [] -> best.pageno
1100 | l :: rest ->
1101 let d = cy - (l.pagedispy + l.pagevh/2)
1102 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1103 if abs d < abs dbest
1104 then fold l rest
1105 else best.pageno
1106 in fold l rest
1108 fold state.layout
1110 state.mode <- Birdseye (
1111 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1113 conf.zoom <- zoom;
1114 conf.presentation <- false;
1115 conf.interpagespace <- 10;
1116 conf.hlinks <- false;
1117 state.x <- 0;
1118 state.mstate <- Mnone;
1119 conf.showall <- false;
1120 Glut.setCursor Glut.CURSOR_INHERIT;
1121 if conf.verbose
1122 then
1123 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1124 (100.0*.zoom)
1125 else
1126 state.text <- ""
1128 reshape conf.winw conf.winh;
1131 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1132 state.mode <- View;
1133 conf.zoom <- c.zoom;
1134 conf.presentation <- c.presentation;
1135 conf.interpagespace <- c.interpagespace;
1136 conf.showall <- c.showall;
1137 conf.hlinks <- c.hlinks;
1138 state.x <- leftx;
1139 if conf.verbose
1140 then
1141 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1142 (100.0*.conf.zoom)
1144 reshape conf.winw conf.winh;
1145 state.anchor <- if goback then anchor else (pageno, 0.0);
1148 let togglebirdseye () =
1149 match state.mode with
1150 | Birdseye vals -> leavebirdseye vals true
1151 | View | Outline _ -> enterbirdseye ()
1152 | _ -> ()
1155 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1156 let pageno = max 0 (pageno - 1) in
1157 let rec loop = function
1158 | [] -> gotopage1 pageno 0
1159 | l :: _ when l.pageno = pageno ->
1160 if l.pagedispy >= 0 && l.pagey = 0
1161 then Glut.postRedisplay ()
1162 else gotopage1 pageno 0
1163 | _ :: rest -> loop rest
1165 loop state.layout;
1166 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1169 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1170 let pageno = min (state.pagecount - 1) (pageno + 1) in
1171 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1172 let rec loop = function
1173 | [] ->
1174 let y, h = getpageyh pageno in
1175 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1176 gotoy (clamp dy)
1177 | l :: _ when l.pageno = pageno ->
1178 if l.pagevh != l.pageh
1179 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1180 else Glut.postRedisplay ()
1181 | _ :: rest -> loop rest
1183 loop state.layout
1186 let optentry mode _ key =
1187 let btos b = if b then "on" else "off" in
1188 let c = Char.unsafe_chr key in
1189 match c with
1190 | 's' ->
1191 let ondone s =
1192 try conf.scrollstep <- int_of_string s with exc ->
1193 state.text <- Printf.sprintf "bad integer `%s': %s"
1194 s (Printexc.to_string exc)
1196 TEswitch ("scroll step", "", None, intentry, ondone)
1198 | 'A' ->
1199 let ondone s =
1201 conf.autoscrollstep <- int_of_string s;
1202 if state.ascrollstep > 0
1203 then state.ascrollstep <- conf.autoscrollstep;
1204 with exc ->
1205 state.text <- Printf.sprintf "bad integer `%s': %s"
1206 s (Printexc.to_string exc)
1208 TEswitch ("auto scroll step", "", None, intentry, ondone)
1210 | 'Z' ->
1211 let ondone s =
1213 let zoom = float (int_of_string s) /. 100.0 in
1214 setzoom zoom
1215 with exc ->
1216 state.text <- Printf.sprintf "bad integer `%s': %s"
1217 s (Printexc.to_string exc)
1219 TEswitch ("zoom", "", None, intentry, ondone)
1221 | 't' ->
1222 let ondone s =
1224 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1225 state.text <-
1226 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1227 begin match mode with
1228 | Birdseye beye ->
1229 leavebirdseye beye false;
1230 enterbirdseye ();
1231 | _ -> ();
1233 with exc ->
1234 state.text <- Printf.sprintf "bad integer `%s': %s"
1235 s (Printexc.to_string exc)
1237 TEswitch ("thumbnail width", "", None, intentry, ondone)
1239 | 'R' ->
1240 let ondone s =
1241 match try
1242 Some (int_of_string s)
1243 with exc ->
1244 state.text <- Printf.sprintf "bad integer `%s': %s"
1245 s (Printexc.to_string exc);
1246 None
1247 with
1248 | Some angle -> reinit angle conf.proportional
1249 | None -> ()
1251 TEswitch ("rotation", "", None, intentry, ondone)
1253 | 'i' ->
1254 conf.icase <- not conf.icase;
1255 TEdone ("case insensitive search " ^ (btos conf.icase))
1257 | 'p' ->
1258 conf.preload <- not conf.preload;
1259 gotoy state.y;
1260 TEdone ("preload " ^ (btos conf.preload))
1262 | 'v' ->
1263 conf.verbose <- not conf.verbose;
1264 TEdone ("verbose " ^ (btos conf.verbose))
1266 | 'h' ->
1267 conf.maxhfit <- not conf.maxhfit;
1268 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1269 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1271 | 'c' ->
1272 conf.crophack <- not conf.crophack;
1273 TEdone ("crophack " ^ btos conf.crophack)
1275 | 'a' ->
1276 conf.showall <- not conf.showall;
1277 TEdone ("throttle " ^ btos conf.showall)
1279 | 'f' ->
1280 conf.underinfo <- not conf.underinfo;
1281 TEdone ("underinfo " ^ btos conf.underinfo)
1283 | 'P' ->
1284 conf.savebmarks <- not conf.savebmarks;
1285 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1287 | 'S' ->
1288 let ondone s =
1290 let pageno, py =
1291 match state.layout with
1292 | [] -> 0, 0
1293 | l :: _ ->
1294 l.pageno, l.pagey
1296 conf.interpagespace <- int_of_string s;
1297 state.maxy <- calcheight ();
1298 let y = getpagey pageno in
1299 gotoy (y + py)
1300 with exc ->
1301 state.text <- Printf.sprintf "bad integer `%s': %s"
1302 s (Printexc.to_string exc)
1304 TEswitch ("vertical margin", "", None, intentry, ondone)
1306 | 'l' ->
1307 reinit conf.angle (not conf.proportional);
1308 TEdone ("proprortional display " ^ btos conf.proportional)
1310 | _ ->
1311 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1312 TEstop
1315 let maxoutlinerows () = (conf.winh - 31) / 16;;
1317 let enterselector allowdel outlines errmsg msg =
1318 if Array.length outlines = 0
1319 then (
1320 showtext ' ' errmsg;
1322 else (
1323 state.text <- msg;
1324 Glut.setCursor Glut.CURSOR_INHERIT;
1325 let pageno =
1326 match state.layout with
1327 | [] -> -1
1328 | {pageno=pageno} :: _ -> pageno
1330 let active =
1331 let rec loop n =
1332 if n = Array.length outlines
1333 then 0
1334 else
1335 let (_, _, (outlinepageno, _)) = outlines.(n) in
1336 if outlinepageno >= pageno then n else loop (n+1)
1338 loop 0
1340 state.mode <- Outline
1341 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1342 state.mode);
1343 Glut.postRedisplay ();
1347 let enteroutlinemode () =
1348 let outlines, msg =
1349 match state.outlines with
1350 | Oarray a -> a, ""
1351 | Olist l ->
1352 let a = Array.of_list (List.rev l) in
1353 state.outlines <- Oarray a;
1354 a, ""
1355 | Onarrow (pat, a, _) ->
1356 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1358 enterselector false outlines "Document has no outline" msg;
1361 let enterbookmarkmode () =
1362 let bookmarks = Array.of_list state.bookmarks in
1363 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1366 let mode_to_string mode =
1367 let b = Buffer.create 10 in
1368 let rec f = function
1369 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1370 | View -> Buffer.add_string b "View"
1371 | Birdseye _ -> Buffer.add_string b "Birdseye"
1372 | Items _ -> Buffer.add_string b "Items"
1373 | Outline _ -> Buffer.add_string b "Outline"
1375 f mode;
1376 Buffer.contents b;
1379 let color_of_string s =
1380 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1381 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1385 let color_to_string (r, g, b) =
1386 let r = truncate (r *. 256.0)
1387 and g = truncate (g *. 256.0)
1388 and b = truncate (b *. 256.0) in
1389 Printf.sprintf "%d/%d/%d" r g b
1392 let enterinfomode () =
1393 let btos = function true -> "on" | _ -> "off" in
1394 let mode = state.mode in
1395 let rec makeitems () =
1396 let intp name get set =
1397 Printf.sprintf "%s\t%d" name (get ()), 1, Action (
1398 fun active first _ pan ->
1399 let ondone s =
1400 let n =
1401 try int_of_string s
1402 with exn ->
1403 state.text <- Printf.sprintf "bad integer `%s': %s"
1404 s (Printexc.to_string exn);
1405 max_int;
1407 if n != max_int then set n;
1409 let te = name, "", None, intentry, ondone in
1410 state.text <- "";
1411 Textentry (
1413 fun _ ->
1414 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1417 and boolp name get set =
1418 Printf.sprintf "%s\t%s" name (btos (get ())), 1, Action (
1419 fun active first qsearch pan ->
1420 let v = get () in
1421 set (not v);
1422 Items (active, first, makeitems (), qsearch, pan, mode);
1424 and colorp name get set =
1425 Printf.sprintf "%s\t%s" name (color_to_string (get ())), 1, Action (
1426 fun active first _ pan ->
1427 let invalid = (nan, nan, nan) in
1428 let ondone s =
1429 let c =
1430 try color_of_string s
1431 with exn ->
1432 state.text <- Printf.sprintf "bad color `%s': %s"
1433 s (Printexc.to_string exn);
1434 invalid
1436 if c <> invalid
1437 then set c;
1439 let te = name, "", None, textentry, ondone in
1440 state.text <- "";
1441 Textentry (
1443 fun _ ->
1444 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1449 let birdseye = isbirdseye mode in
1450 let items = [
1451 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1453 boolp "presentation"
1454 (fun () -> conf.presentation)
1455 (fun v ->
1456 conf.presentation <- v;
1457 state.anchor <- getanchor ();
1458 represent ());
1460 boolp "ignore case in searches"
1461 (fun () -> conf.icase)
1462 (fun v -> conf.icase <- v);
1464 boolp "preload"
1465 (fun () -> conf.preload)
1466 (fun v -> conf.preload <- v);
1468 boolp "verbose"
1469 (fun () -> conf.verbose)
1470 (fun v -> conf.verbose <- v);
1472 boolp "max fit"
1473 (fun () -> conf.maxhfit)
1474 (fun v -> conf.maxhfit <- v);
1476 boolp "crop hack"
1477 (fun () -> conf.crophack)
1478 (fun v -> conf.crophack <- v);
1480 boolp "throttle"
1481 (fun () -> conf.showall)
1482 (fun v -> conf.showall <- v);
1484 boolp "highlight links"
1485 (fun () -> conf.hlinks)
1486 (fun v -> conf.hlinks <- v);
1488 boolp "under info"
1489 (fun () -> conf.underinfo)
1490 (fun v -> conf.underinfo <- v);
1491 boolp "persistent bookmarks"
1492 (fun () -> conf.savebmarks)
1493 (fun v -> conf.savebmarks <- v);
1495 boolp "proportional display"
1496 (fun () -> conf.proportional)
1497 (fun v -> reinit conf.angle v);
1499 boolp "persistent location"
1500 (fun () -> conf.jumpback)
1501 (fun v -> conf.jumpback <- v);
1503 "", 0, Noaction;
1505 intp "vertical margin"
1506 (fun () -> conf.interpagespace)
1507 (fun n ->
1508 conf.interpagespace <- n;
1509 let pageno, py =
1510 match state.layout with
1511 | [] -> 0, 0
1512 | l :: _ ->
1513 l.pageno, l.pagey
1515 state.maxy <- calcheight ();
1516 let y = getpagey pageno in
1517 gotoy (y + py)
1520 intp "page bias"
1521 (fun () -> conf.pagebias)
1522 (fun v -> conf.pagebias <- v);
1524 intp "scroll step"
1525 (fun () -> conf.scrollstep)
1526 (fun n -> conf.scrollstep <- n);
1528 intp "auto scroll step"
1529 (fun () ->
1530 if state.ascrollstep > 0
1531 then state.ascrollstep
1532 else conf.autoscrollstep)
1533 (fun n ->
1534 if state.ascrollstep > 0
1535 then state.ascrollstep <- n
1536 else conf.autoscrollstep <- n);
1538 intp "zoom"
1539 (fun () -> truncate (conf.zoom *. 100.))
1540 (fun v -> setzoom ((float v) /. 100.));
1542 intp "rotation"
1543 (fun () -> conf.angle)
1544 (fun v -> reinit v conf.proportional);
1546 intp "scroll bar width"
1547 (fun () -> state.scrollw)
1548 (fun v ->
1549 state.scrollw <- v;
1550 conf.scrollbw <- v;
1551 reshape conf.winw conf.winh;
1554 intp "scroll handle height"
1555 (fun () -> conf.scrollh)
1556 (fun v -> conf.scrollh <- v;);
1558 intp "thumbnail width"
1559 (fun () -> conf.thumbw)
1560 (fun v ->
1561 conf.thumbw <- min 1920 v;
1562 match mode with
1563 | Birdseye beye ->
1564 leavebirdseye beye false;
1565 enterbirdseye ()
1566 | _ -> ()
1569 colorp "background color"
1570 (fun () -> conf.bgcolor)
1571 (fun v -> conf.bgcolor <- v);
1573 "", 0, Noaction;
1574 "Presentation mode", 0, Noaction;
1576 boolp "scrollbar visible"
1577 (fun () -> conf.scrollbarinpm)
1578 (fun v ->
1579 if v != conf.scrollbarinpm
1580 then (
1581 conf.scrollbarinpm <- v;
1582 if conf.presentation
1583 then (
1584 state.scrollw <- if v then conf.scrollbw else 0;
1585 reshape conf.winw conf.winh;
1590 "", 0, Noaction;
1591 "Pixmap Cache", 0, Noaction;
1593 intp "size (advisory)"
1594 (fun () -> conf.memlimit)
1595 (fun v -> conf.memlimit <- v);
1596 Printf.sprintf "%s\t%d" "used" state.memused, 1, Noaction;
1600 let tailer =
1601 let trailer =
1602 ("", 0, Noaction)
1603 :: ("Document", 0, Noaction)
1604 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1606 if birdseye
1607 then trailer
1608 else (
1609 ("", 0, Noaction)
1610 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1611 (btos conf.bedefault),
1613 Action (
1614 fun active first qsearch pan ->
1615 conf.bedefault <- not conf.bedefault;
1616 Items (active, first, makeitems (), qsearch, pan, mode);
1618 ) :: trailer
1621 Array.of_list (items @ tailer)
1623 state.text <- "";
1624 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1625 Glut.postRedisplay ();
1628 let enterhelpmode () =
1629 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1630 Glut.postRedisplay ();
1633 let quickbookmark ?title () =
1634 match state.layout with
1635 | [] -> ()
1636 | l :: _ ->
1637 let title =
1638 match title with
1639 | None ->
1640 let sec = Unix.gettimeofday () in
1641 let tm = Unix.localtime sec in
1642 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1643 (l.pageno+1)
1644 tm.Unix.tm_mday
1645 tm.Unix.tm_mon
1646 (tm.Unix.tm_year + 1900)
1647 tm.Unix.tm_hour
1648 tm.Unix.tm_min
1649 | Some title -> title
1651 state.bookmarks <-
1652 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1653 :: state.bookmarks
1656 let doreshape w h =
1657 state.fullscreen <- None;
1658 Glut.reshapeWindow w h;
1661 let writeopen path password =
1662 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1663 writecmd state.csock "info";
1666 let opendoc path password =
1667 invalidate ();
1668 state.path <- path;
1669 state.password <- password;
1670 state.gen <- state.gen + 1;
1672 writeopen path password;
1673 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1674 wcmd "geometry" [`i state.w; `i conf.winh];
1677 let viewkeyboard key =
1678 let enttext te =
1679 let mode = state.mode in
1680 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1681 state.text <- "";
1682 enttext ();
1683 Glut.postRedisplay ()
1685 let c = Char.chr key in
1686 match c with
1687 | '\027' | 'q' -> (* escape *)
1688 exit 0
1690 | '\008' -> (* backspace *)
1691 let y = getnav ~-1 in
1692 gotoy_and_clear_text y
1694 | 'o' ->
1695 enteroutlinemode ()
1697 | 'u' ->
1698 state.rects <- [];
1699 state.text <- "";
1700 Glut.postRedisplay ()
1702 | '/' | '?' ->
1703 let ondone isforw s =
1704 cbput state.hists.pat s;
1705 state.searchpattern <- s;
1706 search s isforw
1708 let s = String.create 1 in
1709 s.[0] <- c;
1710 enttext (s, "", Some (onhist state.hists.pat),
1711 textentry, ondone (c ='/'))
1713 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1714 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1715 setzoom (min 2.2 (conf.zoom +. incr))
1717 | '+' ->
1718 let ondone s =
1719 let n =
1720 try int_of_string s with exc ->
1721 state.text <- Printf.sprintf "bad integer `%s': %s"
1722 s (Printexc.to_string exc);
1723 max_int
1725 if n != max_int
1726 then (
1727 conf.pagebias <- n;
1728 state.text <- "page bias is now " ^ string_of_int n;
1731 enttext ("page bias", "", None, intentry, ondone)
1733 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1734 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1735 setzoom (max 0.01 (conf.zoom -. decr))
1737 | '-' ->
1738 let ondone msg = state.text <- msg in
1739 enttext (
1740 "option [acfhilpstvAPRSZ]", "", None,
1741 optentry state.mode, ondone
1744 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1745 setzoom 1.0
1747 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1748 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1749 if zoom < 1.0
1750 then setzoom zoom
1752 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1753 togglebirdseye ()
1755 | '0' .. '9' ->
1756 let ondone s =
1757 let n =
1758 try int_of_string s with exc ->
1759 state.text <- Printf.sprintf "bad integer `%s': %s"
1760 s (Printexc.to_string exc);
1763 if n >= 0
1764 then (
1765 addnav ();
1766 cbput state.hists.pag (string_of_int n);
1767 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1770 let pageentry text key =
1771 match Char.unsafe_chr key with
1772 | 'g' -> TEdone text
1773 | _ -> intentry text key
1775 let text = "x" in text.[0] <- c;
1776 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1778 | 'b' ->
1779 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1780 reshape conf.winw conf.winh;
1782 | 'l' ->
1783 conf.hlinks <- not conf.hlinks;
1784 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1785 Glut.postRedisplay ()
1787 | 'a' ->
1788 if state.ascrollstep = 0
1789 then state.ascrollstep <- conf.autoscrollstep
1790 else (
1791 conf.autoscrollstep <- state.ascrollstep;
1792 state.ascrollstep <- 0;
1795 | 'P' ->
1796 conf.presentation <- not conf.presentation;
1797 if conf.presentation
1798 then (
1799 if not conf.scrollbarinpm
1800 then state.scrollw <- 0;
1802 else
1803 state.scrollw <- conf.scrollbw;
1805 showtext ' ' ("presentation mode " ^
1806 if conf.presentation then "on" else "off");
1807 state.anchor <- getanchor ();
1808 represent ()
1810 | 'f' ->
1811 begin match state.fullscreen with
1812 | None ->
1813 state.fullscreen <- Some (conf.winw, conf.winh);
1814 Glut.fullScreen ()
1815 | Some (w, h) ->
1816 state.fullscreen <- None;
1817 doreshape w h
1820 | 'g' ->
1821 gotoy_and_clear_text 0
1823 | 'n' ->
1824 search state.searchpattern true
1826 | 'p' | 'N' ->
1827 search state.searchpattern false
1829 | 't' ->
1830 begin match state.layout with
1831 | [] -> ()
1832 | l :: _ ->
1833 gotoy_and_clear_text (getpagey l.pageno)
1836 | ' ' ->
1837 begin match List.rev state.layout with
1838 | [] -> ()
1839 | l :: _ ->
1840 let pageno = min (l.pageno+1) (state.pagecount-1) in
1841 gotoy_and_clear_text (getpagey pageno)
1844 | '\127' -> (* delte *)
1845 begin match state.layout with
1846 | [] -> ()
1847 | l :: _ ->
1848 let pageno = max 0 (l.pageno-1) in
1849 gotoy_and_clear_text (getpagey pageno)
1852 | '=' ->
1853 let f (fn, _) l =
1854 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1856 let fn, ln = List.fold_left f (-1, -1) state.layout in
1857 let s =
1858 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1859 let percent =
1860 if maxy <= 0
1861 then 100.
1862 else (100. *. (float state.y /. float maxy)) in
1863 if fn = ln
1864 then
1865 Printf.sprintf "Page %d of %d %.2f%%"
1866 (fn+1) state.pagecount percent
1867 else
1868 Printf.sprintf
1869 "Pages %d-%d of %d %.2f%%"
1870 (fn+1) (ln+1) state.pagecount percent
1872 showtext ' ' s;
1874 | 'w' ->
1875 begin match state.layout with
1876 | [] -> ()
1877 | l :: _ ->
1878 doreshape (l.pagew + state.scrollw) l.pageh;
1879 Glut.postRedisplay ();
1882 | '\'' ->
1883 enterbookmarkmode ()
1885 | 'h' ->
1886 enterhelpmode ()
1888 | 'i' ->
1889 enterinfomode ()
1891 | 'm' ->
1892 let ondone s =
1893 match state.layout with
1894 | l :: _ ->
1895 state.bookmarks <-
1896 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1897 :: state.bookmarks
1898 | _ -> ()
1900 enttext ("bookmark", "", None, textentry, ondone)
1902 | '~' ->
1903 quickbookmark ();
1904 showtext ' ' "Quick bookmark added";
1906 | 'z' ->
1907 begin match state.layout with
1908 | l :: _ ->
1909 let rect = getpdimrect l.pagedimno in
1910 let w, h =
1911 if conf.crophack
1912 then
1913 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1914 truncate (1.2 *. (rect.(3) -. rect.(0))))
1915 else
1916 (truncate (rect.(1) -. rect.(0)),
1917 truncate (rect.(3) -. rect.(0)))
1919 if w != 0 && h != 0
1920 then
1921 doreshape (w + state.scrollw) (h + conf.interpagespace)
1923 Glut.postRedisplay ();
1925 | [] -> ()
1928 | '<' | '>' ->
1929 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1931 | '[' | ']' ->
1932 state.colorscale <-
1933 max 0.0
1934 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1935 Glut.postRedisplay ()
1937 | 'k' ->
1938 begin match state.mode with
1939 | Birdseye beye -> upbirdseye beye
1940 | _ -> gotoy (clamp (-conf.scrollstep))
1943 | 'j' ->
1944 begin match state.mode with
1945 | Birdseye beye -> downbirdseye beye
1946 | _ -> gotoy (clamp conf.scrollstep)
1949 | 'r' ->
1950 state.anchor <- getanchor ();
1951 opendoc state.path state.password
1953 | _ ->
1954 vlog "huh? %d %c" key (Char.chr key);
1957 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1958 let enttext te =
1959 state.mode <- Textentry (te, onleave);
1960 state.text <- "";
1961 enttext ();
1962 Glut.postRedisplay ()
1964 match Char.unsafe_chr key with
1965 | '\008' -> (* backspace *)
1966 let len = String.length text in
1967 if len = 0
1968 then (
1969 onleave Cancel;
1970 Glut.postRedisplay ();
1972 else (
1973 let s = String.sub text 0 (len - 1) in
1974 enttext (c, s, opthist, onkey, ondone)
1977 | '\r' | '\n' ->
1978 ondone text;
1979 onleave Confirm;
1980 Glut.postRedisplay ()
1982 | '\027' -> (* escape *)
1983 begin match opthist with
1984 | None -> ()
1985 | Some (_, onhistcancel) -> onhistcancel ()
1986 end;
1987 onleave Cancel;
1988 Glut.postRedisplay ()
1990 | _ ->
1991 begin match onkey text key with
1992 | TEdone text ->
1993 onleave Confirm;
1994 ondone text;
1995 Glut.postRedisplay ()
1997 | TEcont text ->
1998 enttext (c, text, opthist, onkey, ondone);
2000 | TEstop ->
2001 onleave Cancel;
2002 Glut.postRedisplay ()
2004 | TEswitch te ->
2005 state.mode <- Textentry (te, onleave);
2006 Glut.postRedisplay ()
2007 end;
2010 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2011 match key with
2012 | 27 -> (* escape *)
2013 leavebirdseye beye true
2015 | 12 -> (* ctrl-l *)
2016 let y, h = getpageyh pageno in
2017 let top = (conf.winh - h) / 2 in
2018 gotoy (max 0 (y - top))
2020 | 13 -> (* enter *)
2021 leavebirdseye beye false
2023 | _ ->
2024 viewkeyboard key
2027 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2028 let set active first qsearch =
2029 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2031 let search active pattern incr =
2032 let dosearch re =
2033 let rec loop n =
2034 if n >= Array.length items || n < 0
2035 then None
2036 else
2037 let (s, _, _) = items.(n) in
2039 (try ignore (Str.search_forward re s 0); true
2040 with Not_found -> false)
2041 then Some n
2042 else loop (n + incr)
2044 loop active
2047 let re = Str.regexp_case_fold pattern in
2048 dosearch re
2049 with Failure s ->
2050 state.text <- s;
2051 None
2053 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2054 match key with
2055 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2056 let incr = if key = 18 then -1 else 1 in
2057 let active, first =
2058 match search (active + incr) qsearch incr with
2059 | None ->
2060 state.text <- qsearch ^ " [not found]";
2061 active, first
2062 | Some active ->
2063 state.text <- qsearch;
2064 active, firstof active
2066 set active first qsearch;
2067 Glut.postRedisplay ();
2069 | 8 -> (* backspace *)
2070 let len = String.length qsearch in
2071 if len = 0
2072 then ()
2073 else (
2074 if len = 1
2075 then (
2076 state.text <- "";
2077 set active first "";
2079 else
2080 let qsearch = String.sub qsearch 0 (len - 1) in
2081 let active, first =
2082 match search active qsearch ~-1 with
2083 | None ->
2084 state.text <- qsearch ^ " [not found]";
2085 active, first
2086 | Some active ->
2087 state.text <- qsearch;
2088 active, firstof active
2090 set active first qsearch
2092 Glut.postRedisplay ()
2094 | _ when key >= 32 && key < 127 ->
2095 let pattern = addchar qsearch (Char.chr key) in
2096 let active, first =
2097 match search active pattern 1 with
2098 | None ->
2099 state.text <- pattern ^ " [not found]";
2100 active, first
2101 | Some active ->
2102 state.text <- pattern;
2103 active, firstof active
2105 set active first pattern;
2106 Glut.postRedisplay ()
2108 | 27 -> (* escape *)
2109 state.text <- "";
2110 if String.length qsearch = 0
2111 then (
2112 state.mode <- oldmode;
2114 else (
2115 set active first "";
2117 Glut.postRedisplay ()
2119 | 13 -> (* enter *)
2120 if active >= 0 && active < Array.length items
2121 then (
2122 match items.(active) with
2123 | _, _, Action f ->
2124 state.mode <- f active first qsearch pan
2126 | _, _, Noaction ->
2127 state.text <- "";
2128 state.mode <- oldmode
2130 else (
2131 state.text <- "";
2132 state.mode <- oldmode
2134 Glut.postRedisplay ();
2136 | _ -> dolog "unknown key %d" key
2139 let outlinekeyboard key
2140 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2141 let narrow outlines pattern =
2142 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2143 match reopt with
2144 | None -> None
2145 | Some re ->
2146 let rec fold accu n =
2147 if n = -1
2148 then accu
2149 else
2150 let (s, _, _) as o = outlines.(n) in
2151 let accu =
2152 if (try ignore (Str.search_forward re s 0); true
2153 with Not_found -> false)
2154 then (o :: accu)
2155 else accu
2157 fold accu (n-1)
2159 let matched = fold [] (Array.length outlines - 1) in
2160 if matched = [] then None else Some (Array.of_list matched)
2162 let search active pattern incr =
2163 let dosearch re =
2164 let rec loop n =
2165 if n = Array.length outlines || n = -1
2166 then None
2167 else
2168 let (s, _, _) = outlines.(n) in
2170 (try ignore (Str.search_forward re s 0); true
2171 with Not_found -> false)
2172 then Some n
2173 else loop (n + incr)
2175 loop active
2178 let re = Str.regexp_case_fold pattern in
2179 dosearch re
2180 with Failure s ->
2181 state.text <- s;
2182 None
2184 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2185 match key with
2186 | 27 -> (* escape *)
2187 state.text <- "";
2188 if String.length qsearch = 0
2189 then (
2190 state.mode <- oldmode;
2192 else (
2193 state.mode <- Outline (
2194 allowdel, active, first, outlines, "", pan, oldmode
2197 Glut.postRedisplay ();
2199 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2200 let incr = if key = 18 then -1 else 1 in
2201 let active, first =
2202 match search (active + incr) qsearch incr with
2203 | None ->
2204 state.text <- qsearch ^ " [not found]";
2205 active, first
2206 | Some active ->
2207 state.text <- qsearch;
2208 active, firstof active
2210 state.mode <- Outline (
2211 allowdel, active, first, outlines, qsearch, pan, oldmode
2213 Glut.postRedisplay ();
2215 | 8 -> (* backspace *)
2216 let len = String.length qsearch in
2217 if len = 0
2218 then ()
2219 else (
2220 if len = 1
2221 then (
2222 state.text <- "";
2223 state.mode <- Outline (
2224 allowdel, active, first, outlines, "", pan, oldmode
2227 else
2228 let qsearch = String.sub qsearch 0 (len - 1) in
2229 let active, first =
2230 match search active qsearch ~-1 with
2231 | None ->
2232 state.text <- qsearch ^ " [not found]";
2233 active, first
2234 | Some active ->
2235 state.text <- qsearch;
2236 active, firstof active
2238 state.mode <- Outline (
2239 allowdel, active, first, outlines, qsearch, pan, oldmode
2242 Glut.postRedisplay ()
2244 | 13 -> (* enter *)
2245 if active < Array.length outlines
2246 then (
2247 let (_, _, anchor) = outlines.(active) in
2248 addnav ();
2249 gotoanchor anchor;
2251 state.text <- "";
2252 if allowdel then state.bookmarks <- Array.to_list outlines;
2253 state.mode <- oldmode;
2254 Glut.postRedisplay ();
2256 | _ when key >= 32 && key < 127 ->
2257 let pattern = addchar qsearch (Char.chr key) in
2258 let active, first =
2259 match search active pattern 1 with
2260 | None ->
2261 state.text <- pattern ^ " [not found]";
2262 active, first
2263 | Some active ->
2264 state.text <- pattern;
2265 active, firstof active
2267 state.mode <- Outline (
2268 allowdel, active, first, outlines, pattern, pan, oldmode
2270 Glut.postRedisplay ()
2272 | 14 when not allowdel -> (* ctrl-n *)
2273 if String.length qsearch > 0
2274 then (
2275 let optoutlines = narrow outlines qsearch in
2276 begin match optoutlines with
2277 | None -> state.text <- "can't narrow"
2278 | Some outlines ->
2279 state.mode <- Outline (
2280 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2282 match state.outlines with
2283 | Olist _ -> ()
2284 | Oarray a ->
2285 state.outlines <- Onarrow (qsearch, outlines, a)
2286 | Onarrow (_, _, b) ->
2287 state.outlines <- Onarrow (qsearch, outlines, b)
2288 end;
2290 Glut.postRedisplay ()
2292 | 21 when not allowdel -> (* ctrl-u *)
2293 let outline =
2294 match state.outlines with
2295 | Oarray a -> a
2296 | Olist l ->
2297 let a = Array.of_list (List.rev l) in
2298 state.outlines <- Oarray a;
2300 | Onarrow (_, _, b) ->
2301 state.outlines <- Oarray b;
2302 state.text <- "";
2305 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2306 Glut.postRedisplay ()
2308 | 12 -> (* ctrl-l *)
2309 state.mode <- Outline
2310 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2311 Glut.postRedisplay ()
2313 | 127 when allowdel -> (* delete *)
2314 let len = Array.length outlines - 1 in
2315 if len = 0
2316 then (
2317 state.mode <- View;
2318 state.bookmarks <- [];
2320 else (
2321 let bookmarks = Array.init len
2322 (fun i ->
2323 let i = if i >= active then i + 1 else i in
2324 outlines.(i)
2327 state.mode <-
2328 Outline (
2329 allowdel,
2330 min active (len-1),
2331 min first (len-1),
2332 bookmarks, qsearch,
2334 oldmode
2337 Glut.postRedisplay ()
2339 | _ -> dolog "unknown key %d" key
2342 let keyboard ~key ~x ~y =
2343 ignore x;
2344 ignore y;
2345 if key = 7 (* ctrl-g *)
2346 then
2347 wcmd "interrupt" []
2348 else
2349 match state.mode with
2350 | Outline outline -> outlinekeyboard key outline
2351 | Textentry textentry -> textentrykeyboard key textentry
2352 | Birdseye birdseye -> birdseyekeyboard key birdseye
2353 | View -> viewkeyboard key
2354 | Items items -> itemskeyboard key items
2357 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2358 match key with
2359 | Glut.KEY_UP -> upbirdseye beye
2360 | Glut.KEY_DOWN -> downbirdseye beye
2362 | Glut.KEY_PAGE_UP ->
2363 begin match state.layout with
2364 | l :: _ ->
2365 if l.pagey != 0
2366 then (
2367 state.mode <- Birdseye (
2368 conf, leftx, l.pageno, hooverpageno, anchor
2370 gotopage1 l.pageno 0;
2372 else (
2373 let layout = layout (state.y-conf.winh) conf.winh in
2374 match layout with
2375 | [] -> gotoy (clamp (-conf.winh))
2376 | l :: _ ->
2377 state.mode <- Birdseye (
2378 conf, leftx, l.pageno, hooverpageno, anchor
2380 gotopage1 l.pageno 0
2383 | [] -> gotoy (clamp (-conf.winh))
2384 end;
2386 | Glut.KEY_PAGE_DOWN ->
2387 begin match List.rev state.layout with
2388 | l :: _ ->
2389 let layout = layout (state.y + conf.winh) conf.winh in
2390 begin match layout with
2391 | [] ->
2392 let incr = l.pageh - l.pagevh in
2393 if incr = 0
2394 then (
2395 state.mode <-
2396 Birdseye (
2397 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2399 Glut.postRedisplay ();
2401 else gotoy (clamp (incr + conf.interpagespace*2));
2403 | l :: _ ->
2404 state.mode <-
2405 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2406 gotopage1 l.pageno 0;
2409 | [] -> gotoy (clamp conf.winh)
2410 end;
2412 | Glut.KEY_HOME ->
2413 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2414 gotopage1 0 0
2416 | Glut.KEY_END ->
2417 let pageno = state.pagecount - 1 in
2418 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2419 if not (pagevisible state.layout pageno)
2420 then
2421 let h =
2422 match List.rev state.pdims with
2423 | [] -> conf.winh
2424 | (_, _, h, _) :: _ -> h
2426 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2427 else Glut.postRedisplay ();
2428 | _ -> ()
2431 let setautoscrollspeed goingdown =
2432 let incr = max 1 (state.ascrollstep / 2) in
2433 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2434 state.ascrollstep <- astep;
2437 let special ~key ~x ~y =
2438 ignore x;
2439 ignore y;
2440 match state.mode with
2441 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2442 togglebirdseye ()
2444 | Birdseye vals ->
2445 birdseyespecial key vals
2447 | View when key = Glut.KEY_F1 ->
2448 enterhelpmode ()
2450 | View ->
2451 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2452 then setautoscrollspeed (key = Glut.KEY_DOWN)
2453 else
2454 let y =
2455 match key with
2456 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2457 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2458 | Glut.KEY_DOWN -> clamp conf.scrollstep
2459 | Glut.KEY_PAGE_UP ->
2460 if Glut.getModifiers () land Glut.active_ctrl != 0
2461 then
2462 match state.layout with
2463 | [] -> state.y
2464 | l :: _ -> state.y - l.pagey
2465 else
2466 clamp (-conf.winh)
2467 | Glut.KEY_PAGE_DOWN ->
2468 if Glut.getModifiers () land Glut.active_ctrl != 0
2469 then
2470 match List.rev state.layout with
2471 | [] -> state.y
2472 | l :: _ -> getpagey l.pageno
2473 else
2474 clamp conf.winh
2475 | Glut.KEY_HOME -> addnav (); 0
2476 | Glut.KEY_END ->
2477 addnav ();
2478 state.maxy - (if conf.maxhfit then conf.winh else 0)
2480 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2481 Glut.getModifiers () land Glut.active_alt != 0 ->
2482 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2484 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2485 state.x <- state.x - 10;
2486 state.y
2487 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2488 state.x <- state.x + 10;
2489 state.y
2491 | _ -> state.y
2493 gotoy_and_clear_text y
2495 | Textentry
2496 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2497 let s =
2498 match key with
2499 | Glut.KEY_UP -> action HCprev
2500 | Glut.KEY_DOWN -> action HCnext
2501 | Glut.KEY_HOME -> action HCfirst
2502 | Glut.KEY_END -> action HClast
2503 | _ -> state.text
2505 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2506 Glut.postRedisplay ()
2508 | Textentry _ -> ()
2510 | Items (active, first, items, qsearch, pan, oldmode) ->
2511 let maxrows = maxoutlinerows () in
2512 let itemcount = Array.length items in
2513 let hasaction = function
2514 | (_, _, Noaction) -> false
2515 | _ -> true
2517 let find start incr =
2518 let rec find i =
2519 if i = -1 || i = itemcount
2520 then -1
2521 else (
2522 if hasaction items.(i)
2523 then i
2524 else find (i + incr)
2527 find start
2529 let set active first =
2530 let first = max 0 (min first (itemcount - maxrows)) in
2531 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2533 let navigate incr =
2534 let isvisible first n = n >= first && n - first <= maxrows in
2535 let active, first =
2536 let incr1 = if incr > 0 then 1 else -1 in
2537 if isvisible first active
2538 then
2539 let next =
2540 let next = active + incr in
2541 let next =
2542 if next < 0 || next >= itemcount
2543 then -1
2544 else find next incr1
2546 if next = -1 || abs (active - next) > maxrows
2547 then -1
2548 else next
2550 if next = -1
2551 then
2552 let first = first + incr in
2553 let first = max 0 (min first (itemcount - 1)) in
2554 let next =
2555 let next = active + incr in
2556 let next = max 0 (min next (itemcount - 1)) in
2557 find next ~-incr1
2559 let active = if next = -1 then active else next in
2560 active, first
2561 else
2562 let first = min next first in
2563 next, first
2564 else
2565 let first = first + incr in
2566 let first = max 0 (min first (itemcount - 1)) in
2567 let active =
2568 let next = active + incr in
2569 let next = max 0 (min next (itemcount - 1)) in
2570 let next = find next incr1 in
2571 if next = -1 || abs (active - first) > maxrows
2572 then active
2573 else next
2575 active, first
2577 set active first;
2578 Glut.postRedisplay ()
2580 begin match key with
2581 | Glut.KEY_UP -> navigate ~-1
2582 | Glut.KEY_DOWN -> navigate 1
2583 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2584 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2586 | Glut.KEY_RIGHT ->
2587 state.mode <- Items (
2588 active, first, items, qsearch, min 0 (pan - 1), oldmode
2590 Glut.postRedisplay ()
2592 | Glut.KEY_LEFT ->
2593 state.mode <- Items (
2594 active, first, items, qsearch, min 0 (pan + 1), oldmode
2596 Glut.postRedisplay ()
2598 | Glut.KEY_HOME ->
2599 let active = find 0 1 in
2600 set active 0;
2601 Glut.postRedisplay ()
2603 | Glut.KEY_END ->
2604 let first = max 0 (itemcount - maxrows) in
2605 let active = find (itemcount - 1) ~-1 in
2606 set active first;
2607 Glut.postRedisplay ()
2609 | _ -> ()
2610 end;
2612 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2613 let maxrows = maxoutlinerows () in
2614 let calcfirst first active =
2615 if active > first
2616 then
2617 let rows = active - first in
2618 if rows > maxrows then active - maxrows else first
2619 else active
2621 let navigate incr =
2622 let active = active + incr in
2623 let active = max 0 (min active (Array.length outlines - 1)) in
2624 let first = calcfirst first active in
2625 state.mode <- Outline (
2626 allowdel, active, first, outlines, qsearch, pan, oldmode
2628 Glut.postRedisplay ()
2630 let updownlevel incr =
2631 let len = Array.length outlines in
2632 let (_, curlevel, _) = outlines.(active) in
2633 let rec flow i =
2634 if i = len then i-1 else if i = -1 then 0 else
2635 let (_, l, _) = outlines.(i) in
2636 if l != curlevel then i else flow (i+incr)
2638 let active = flow active in
2639 let first = calcfirst first active in
2640 state.mode <- Outline (
2641 allowdel, active, first, outlines, qsearch, pan, oldmode
2643 Glut.postRedisplay ()
2645 match key with
2646 | Glut.KEY_UP -> navigate ~-1
2647 | Glut.KEY_DOWN -> navigate 1
2648 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2649 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2651 | Glut.KEY_RIGHT ->
2652 if Glut.getModifiers () land Glut.active_ctrl != 0
2653 then (
2654 state.mode <- Outline (
2655 allowdel, active, first, outlines,
2656 qsearch, min 0 (pan + 1), oldmode
2658 Glut.postRedisplay ();
2660 else (
2661 if not allowdel
2662 then updownlevel 1
2665 | Glut.KEY_LEFT ->
2666 if Glut.getModifiers () land Glut.active_ctrl != 0
2667 then (
2668 state.mode <- Outline (
2669 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2671 Glut.postRedisplay ();
2673 else (
2674 if not allowdel
2675 then updownlevel ~-1
2678 | Glut.KEY_HOME ->
2679 state.mode <- Outline (
2680 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2682 Glut.postRedisplay ()
2684 | Glut.KEY_END ->
2685 let active = Array.length outlines - 1 in
2686 let first = max 0 (active - maxrows) in
2687 state.mode <- Outline (
2688 allowdel, active, first, outlines, qsearch, pan, oldmode
2690 Glut.postRedisplay ()
2692 | _ -> ()
2695 let drawplaceholder l =
2696 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2697 GlDraw.rect
2698 (float l.pagex, float l.pagedispy)
2699 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2701 let x = if margin < 0 then -margin else l.pagex
2702 and y = l.pagedispy + 13 in
2703 GlDraw.color (0.0, 0.0, 0.0);
2704 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2707 let now () = Unix.gettimeofday ();;
2709 let drawpage l =
2710 let color =
2711 match state.mode with
2712 | Textentry _ -> scalecolor 0.4
2713 | View | Outline _ | Items _ -> scalecolor 1.0
2714 | Birdseye (_, _, pageno, hooverpageno, _) ->
2715 if l.pageno = hooverpageno
2716 then scalecolor 0.9
2717 else (
2718 if l.pageno = pageno
2719 then scalecolor 1.0
2720 else scalecolor 0.8
2723 GlDraw.color color;
2724 begin match getopaque l.pageno with
2725 | Some (opaque, _) when validopaque opaque ->
2726 let a = now () in
2727 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2728 opaque;
2729 let b = now () in
2730 let d = b-.a in
2731 vlog "draw %d %f sec" l.pageno d;
2733 | _ ->
2734 drawplaceholder l;
2735 end;
2738 let scrollph y =
2739 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2740 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2741 let sh = float conf.winh /. sh in
2742 let sh = max sh (float conf.scrollh) in
2744 let percent =
2745 if state.y = state.maxy
2746 then 1.0
2747 else float y /. float maxy
2749 let position = (float conf.winh -. sh) *. percent in
2751 let position =
2752 if position +. sh > float conf.winh
2753 then float conf.winh -. sh
2754 else position
2756 position, sh;
2759 let scrollindicator () =
2760 GlDraw.color (0.64 , 0.64, 0.64);
2761 GlDraw.rect
2762 (float (conf.winw - state.scrollw), 0.)
2763 (float conf.winw, float conf.winh)
2765 GlDraw.color (0.0, 0.0, 0.0);
2767 let position, sh = scrollph state.y in
2768 GlDraw.rect
2769 (float (conf.winw - state.scrollw), position)
2770 (float conf.winw, position +. sh)
2774 let showsel margin =
2775 match state.mstate with
2776 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2779 | Msel ((x0, y0), (x1, y1)) ->
2780 let rec loop = function
2781 | l :: ls ->
2782 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2783 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2784 then
2785 match getopaque l.pageno with
2786 | Some (opaque, _) when validopaque opaque ->
2787 let oy = -l.pagey + l.pagedispy in
2788 seltext opaque
2789 (x0 - margin - state.x, y0,
2790 x1 - margin - state.x, y1) oy;
2792 | _ -> ()
2793 else loop ls
2794 | [] -> ()
2796 loop state.layout
2799 let showrects () =
2800 let panx = float state.x in
2801 Gl.enable `blend;
2802 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2803 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2804 List.iter
2805 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2806 List.iter (fun l ->
2807 if l.pageno = pageno
2808 then (
2809 let d = float (l.pagedispy - l.pagey) in
2810 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2811 GlDraw.begins `quads;
2813 GlDraw.vertex2 (x0+.panx, y0+.d);
2814 GlDraw.vertex2 (x1+.panx, y1+.d);
2815 GlDraw.vertex2 (x2+.panx, y2+.d);
2816 GlDraw.vertex2 (x3+.panx, y3+.d);
2818 GlDraw.ends ();
2820 ) state.layout
2821 ) state.rects
2823 Gl.disable `blend;
2826 let showstrings trusted active first pan strings =
2827 Gl.enable `blend;
2828 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2829 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2830 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2831 GlDraw.color (1., 1., 1.);
2832 Gl.enable `texture_2d;
2834 let wx = measurestr 15 "w" in
2835 let tabx = 30.0*.wx +. float (pan*15) in
2836 let rec loop row =
2837 if row = Array.length strings || (row - first) * 16 > conf.winh
2838 then ()
2839 else (
2840 let (s, level, _) = strings.(row) in
2841 let y = (row - first) * 16 in
2842 let x = 5 + 15*(max 0 (level+pan)) in
2843 if row = active
2844 then (
2845 Gl.disable `texture_2d;
2846 GlDraw.polygon_mode `both `line;
2847 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2848 GlDraw.rect (0., float (y + 1))
2849 (float (conf.winw - 1), float (y + 18));
2850 GlDraw.polygon_mode `both `fill;
2851 GlDraw.color (1., 1., 1.);
2852 Gl.enable `texture_2d;
2855 let drawtabularstring x s =
2856 let _ =
2857 if trusted
2858 then
2859 let tabpos = try String.index s '\t' with Not_found -> -1 in
2860 if tabpos > 0
2861 then
2862 let len = String.length s - tabpos - 1 in
2863 let s1 = String.sub s 0 tabpos
2864 and s2 = String.sub s (tabpos + 1) len in
2865 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2866 let x = truncate (max xx tabx) in
2867 drawstring1 15 x (y + 16) s2
2868 else
2869 drawstring1 15 x (y + 16) s
2870 else
2871 drawstring1 15 x (y + 16) s
2876 let draw_string s =
2877 let l = String.length s in
2878 if pan < 0
2879 then (
2880 let pan = pan * 2 in
2881 let pos = pan + level in
2882 let left = l + pos in
2883 if left > 0
2884 then
2885 let s =
2886 if left > l
2887 then s
2888 else String.sub s (-pos) left
2890 drawtabularstring x s
2892 else
2893 drawtabularstring (x + pan*15) s
2895 draw_string s;
2896 loop (row+1)
2899 loop first;
2900 Gl.disable `blend;
2901 Gl.disable `texture_2d;
2904 let showoutline (_, active, first, outlines, _, pan, _) =
2905 showstrings false active first pan outlines;
2908 let showitems (active, first, items, _, pan, _) =
2909 showstrings true active first pan items;
2912 let display () =
2913 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2914 GlDraw.viewport margin 0 state.w conf.winh;
2915 pagematrix ();
2916 GlClear.color (scalecolor2 conf.bgcolor);
2917 GlClear.clear [`color];
2918 if conf.zoom > 1.0
2919 then (
2920 Gl.enable `scissor_test;
2921 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2923 List.iter drawpage state.layout;
2924 if conf.zoom > 1.0
2925 then
2926 Gl.disable `scissor_test
2928 if state.x != 0
2929 then (
2930 let x = -.float state.x in
2931 GlMat.translate ~x ();
2933 showrects ();
2934 showsel margin;
2935 GlDraw.viewport 0 0 conf.winw conf.winh;
2936 winmatrix ();
2937 scrollindicator ();
2938 begin match state.mode with
2939 | Items items -> showitems items
2940 | Outline outline -> showoutline outline
2941 | _ -> ()
2942 end;
2943 enttext ();
2944 Glut.swapBuffers ();
2947 let getunder x y =
2948 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2949 let x = x - margin - state.x in
2950 let rec f = function
2951 | l :: rest ->
2952 begin match getopaque l.pageno with
2953 | Some (opaque, _) when validopaque opaque ->
2954 let y = y - l.pagedispy in
2955 if y > 0
2956 then
2957 let y = l.pagey + y in
2958 let x = x - l.pagex in
2959 match whatsunder opaque x y with
2960 | Unone -> f rest
2961 | under -> under
2962 else
2963 f rest
2964 | _ ->
2965 f rest
2967 | [] -> Unone
2969 f state.layout
2972 let viewmouse button bstate x y =
2973 match button with
2974 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2975 if Glut.getModifiers () land Glut.active_ctrl != 0
2976 then (
2977 match state.mstate with
2978 | Mzoom (oldn, i) ->
2979 if oldn = n
2980 then (
2981 if i = 2
2982 then
2983 let incr =
2984 match n with
2985 | 4 ->
2986 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2987 | _ ->
2988 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2990 let zoom = conf.zoom +. incr in
2991 setzoom zoom;
2992 state.mstate <- Mzoom (n, 0);
2993 else
2994 state.mstate <- Mzoom (n, i+1);
2996 else state.mstate <- Mzoom (n, 0)
2998 | _ -> state.mstate <- Mzoom (n, 0)
3000 else (
3001 if state.ascrollstep > 0
3002 then
3003 setautoscrollspeed (n=4)
3004 else
3005 let incr =
3006 if n = 3
3007 then -conf.scrollstep
3008 else conf.scrollstep
3010 let incr = incr * 2 in
3011 let y = clamp incr in
3012 gotoy_and_clear_text y
3015 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
3016 if bstate = Glut.DOWN
3017 then (
3018 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3019 state.mstate <- Mpan (x, y)
3021 else
3022 state.mstate <- Mnone
3024 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
3025 if bstate = Glut.DOWN
3026 then
3027 let position, sh = scrollph state.y in
3028 if y > truncate position && y < truncate (position +. sh)
3029 then
3030 state.mstate <- Mscroll
3031 else
3032 let percent = float y /. float conf.winh in
3033 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3034 gotoy desty;
3035 state.mstate <- Mscroll
3036 else
3037 state.mstate <- Mnone
3039 | Glut.LEFT_BUTTON ->
3040 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3041 begin match dest with
3042 | Ulinkgoto (pageno, top) ->
3043 if pageno >= 0
3044 then (
3045 addnav ();
3046 gotopage1 pageno top;
3049 | Ulinkuri s ->
3050 print_endline s
3052 | Unone when bstate = Glut.DOWN ->
3053 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3054 state.mstate <- Mpan (x, y);
3056 | Unone | Utext _ ->
3057 if bstate = Glut.DOWN
3058 then (
3059 if conf.angle mod 360 = 0
3060 then (
3061 state.mstate <- Msel ((x, y), (x, y));
3062 Glut.postRedisplay ()
3065 else (
3066 match state.mstate with
3067 | Mnone -> ()
3069 | Mzoom _ | Mscroll ->
3070 state.mstate <- Mnone
3072 | Mpan _ ->
3073 Glut.setCursor Glut.CURSOR_INHERIT;
3074 state.mstate <- Mnone
3076 | Msel ((_, y0), (_, y1)) ->
3077 let f l =
3078 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3079 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3080 then
3081 match getopaque l.pageno with
3082 | Some (opaque, _) when validopaque opaque ->
3083 copysel opaque
3084 | _ -> ()
3086 List.iter f state.layout;
3087 copysel ""; (* ugly *)
3088 Glut.setCursor Glut.CURSOR_INHERIT;
3089 state.mstate <- Mnone;
3093 | _ -> ()
3096 let birdseyemouse button bstate x y
3097 (conf, leftx, _, hooverpageno, anchor) =
3098 match button with
3099 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3100 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3101 let rec loop = function
3102 | [] -> ()
3103 | l :: rest ->
3104 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3105 && x > margin && x < margin + l.pagew
3106 then (
3107 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3109 else loop rest
3111 loop state.layout
3112 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3113 | _ -> ()
3116 let mouse bstate button x y =
3117 match state.mode with
3118 | View -> viewmouse button bstate x y
3119 | Birdseye beye -> birdseyemouse button bstate x y beye
3120 | Textentry _ | Outline _ | Items _ -> ()
3123 let mouse ~button ~state ~x ~y = mouse state button x y;;
3125 let motion ~x ~y =
3126 match state.mode with
3127 | Outline _ -> ()
3128 | _ ->
3129 match state.mstate with
3130 | Mzoom _ | Mnone -> ()
3132 | Mpan (x0, y0) ->
3133 let dx = x - x0
3134 and dy = y0 - y in
3135 state.mstate <- Mpan (x, y);
3136 if conf.zoom > 1.0 then state.x <- state.x + dx;
3137 let y = clamp dy in
3138 gotoy_and_clear_text y
3140 | Msel (a, _) ->
3141 state.mstate <- Msel (a, (x, y));
3142 Glut.postRedisplay ()
3144 | Mscroll ->
3145 let y = min conf.winh (max 0 y) in
3146 let percent = float y /. float conf.winh in
3147 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3148 gotoy_and_clear_text y
3151 let pmotion ~x ~y =
3152 match state.mode with
3153 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3154 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3155 let rec loop = function
3156 | [] ->
3157 if hooverpageno != -1
3158 then (
3159 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3160 Glut.postRedisplay ();
3162 | l :: rest ->
3163 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3164 && x > margin && x < margin + l.pagew
3165 then (
3166 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3167 Glut.postRedisplay ();
3169 else loop rest
3171 loop state.layout
3173 | Outline _ | Items _ | Textentry _ -> ()
3174 | View ->
3175 match state.mstate with
3176 | Mnone ->
3177 begin match getunder x y with
3178 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3179 | Ulinkuri uri ->
3180 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3181 Glut.setCursor Glut.CURSOR_INFO
3182 | Ulinkgoto (page, _) ->
3183 if conf.underinfo
3184 then showtext 'p' ("age: " ^ string_of_int page);
3185 Glut.setCursor Glut.CURSOR_INFO
3186 | Utext s ->
3187 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3188 Glut.setCursor Glut.CURSOR_TEXT
3191 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3196 module State =
3197 struct
3198 open Parser
3200 let home =
3202 match Sys.os_type with
3203 | "Win32" -> Sys.getenv "HOMEPATH"
3204 | _ -> Sys.getenv "HOME"
3205 with exn ->
3206 prerr_endline
3207 ("Can not determine home directory location: " ^
3208 Printexc.to_string exn);
3212 let config_of c attrs =
3213 let apply c k v =
3215 match k with
3216 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3217 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3218 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3219 | "preload" -> { c with preload = bool_of_string v }
3220 | "page-bias" -> { c with pagebias = int_of_string v }
3221 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3222 | "auto-scroll-step" ->
3223 { c with autoscrollstep = max 0 (int_of_string v) }
3224 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3225 | "crop-hack" -> { c with crophack = bool_of_string v }
3226 | "throttle" -> { c with showall = bool_of_string v }
3227 | "highlight-links" -> { c with hlinks = bool_of_string v }
3228 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3229 | "vertical-margin" ->
3230 { c with interpagespace = max 0 (int_of_string v) }
3231 | "zoom" ->
3232 let zoom = float_of_string v /. 100. in
3233 let zoom = max 0.01 (min 2.2 zoom) in
3234 { c with zoom = zoom }
3235 | "presentation" -> { c with presentation = bool_of_string v }
3236 | "rotation-angle" -> { c with angle = int_of_string v }
3237 | "width" -> { c with winw = max 20 (int_of_string v) }
3238 | "height" -> { c with winh = max 20 (int_of_string v) }
3239 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3240 | "proportional-display" -> { c with proportional = bool_of_string v }
3241 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3242 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3243 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3244 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3245 | "persistent-location" -> { c with jumpback = bool_of_string v }
3246 | "background-color" -> { c with bgcolor = color_of_string v }
3247 | "scrollbar-in-presentation" ->
3248 { c with scrollbarinpm = bool_of_string v }
3249 | "ui-font" -> { c with uifont = v }
3250 | _ -> c
3251 with exn ->
3252 prerr_endline ("Error processing attribute (`" ^
3253 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3256 let rec fold c = function
3257 | [] -> c
3258 | (k, v) :: rest ->
3259 let c = apply c k v in
3260 fold c rest
3262 fold c attrs;
3265 let fromstring f pos n v d =
3266 try f v
3267 with exn ->
3268 dolog "Error processing attribute (%S=%S) at %d\n%s"
3269 n v pos (Printexc.to_string exn)
3274 let bookmark_of attrs =
3275 let rec fold title page rely = function
3276 | ("title", v) :: rest -> fold v page rely rest
3277 | ("page", v) :: rest -> fold title v rely rest
3278 | ("rely", v) :: rest -> fold title page v rest
3279 | _ :: rest -> fold title page rely rest
3280 | [] -> title, page, rely
3282 fold "invalid" "0" "0" attrs
3285 let doc_of attrs =
3286 let rec fold path page rely pan = function
3287 | ("path", v) :: rest -> fold v page rely pan rest
3288 | ("page", v) :: rest -> fold path v rely pan rest
3289 | ("rely", v) :: rest -> fold path page v pan rest
3290 | ("pan", v) :: rest -> fold path page rely v rest
3291 | _ :: rest -> fold path page rely pan rest
3292 | [] -> path, page, rely, pan
3294 fold "" "0" "0" "0" attrs
3297 let setconf dst src =
3298 dst.scrollbw <- src.scrollbw;
3299 dst.scrollh <- src.scrollh;
3300 dst.icase <- src.icase;
3301 dst.preload <- src.preload;
3302 dst.pagebias <- src.pagebias;
3303 dst.verbose <- src.verbose;
3304 dst.scrollstep <- src.scrollstep;
3305 dst.maxhfit <- src.maxhfit;
3306 dst.crophack <- src.crophack;
3307 dst.autoscrollstep <- src.autoscrollstep;
3308 dst.showall <- src.showall;
3309 dst.hlinks <- src.hlinks;
3310 dst.underinfo <- src.underinfo;
3311 dst.interpagespace <- src.interpagespace;
3312 dst.zoom <- src.zoom;
3313 dst.presentation <- src.presentation;
3314 dst.angle <- src.angle;
3315 dst.winw <- src.winw;
3316 dst.winh <- src.winh;
3317 dst.savebmarks <- src.savebmarks;
3318 dst.memlimit <- src.memlimit;
3319 dst.proportional <- src.proportional;
3320 dst.texcount <- src.texcount;
3321 dst.sliceheight <- src.sliceheight;
3322 dst.thumbw <- src.thumbw;
3323 dst.jumpback <- src.jumpback;
3324 dst.bgcolor <- src.bgcolor;
3325 dst.scrollbarinpm <- src.scrollbarinpm;
3326 dst.uifont <- src.uifont;
3329 let unent s =
3330 let l = String.length s in
3331 let b = Buffer.create l in
3332 unent b s 0 l;
3333 Buffer.contents b;
3336 let get s =
3337 let h = Hashtbl.create 10 in
3338 let dc = { defconf with angle = defconf.angle } in
3339 let rec toplevel v t spos _ =
3340 match t with
3341 | Vdata | Vcdata | Vend -> v
3342 | Vopen ("llppconfig", _, closed) ->
3343 if closed
3344 then v
3345 else { v with f = llppconfig }
3346 | Vopen _ ->
3347 error "unexpected subelement at top level" s spos
3348 | Vclose _ -> error "unexpected close at top level" s spos
3350 and llppconfig v t spos _ =
3351 match t with
3352 | Vdata | Vcdata | Vend -> v
3353 | Vopen ("defaults", attrs, closed) ->
3354 let c = config_of dc attrs in
3355 setconf dc c;
3356 if closed
3357 then v
3358 else { v with f = skip "defaults" (fun () -> v) }
3360 | Vopen ("doc", attrs, closed) ->
3361 let pathent, spage, srely, span = doc_of attrs in
3362 let path = unent pathent
3363 and pageno = fromstring int_of_string spos "page" spage 0
3364 and rely = fromstring float_of_string spos "rely" srely 0.0
3365 and pan = fromstring int_of_string spos "pan" span 0 in
3366 let c = config_of dc attrs in
3367 let anchor = (pageno, rely) in
3368 if closed
3369 then (Hashtbl.add h path (c, [], pan, anchor); v)
3370 else { v with f = doc path pan anchor c [] }
3372 | Vopen _ ->
3373 error "unexpected subelement in llppconfig" s spos
3375 | Vclose "llppconfig" -> { v with f = toplevel }
3376 | Vclose _ -> error "unexpected close in llppconfig" s spos
3378 and doc path pan anchor c bookmarks v t spos _ =
3379 match t with
3380 | Vdata | Vcdata -> v
3381 | Vend -> error "unexpected end of input in doc" s spos
3382 | Vopen ("bookmarks", _, closed) ->
3383 if closed
3384 then v
3385 else { v with f = pbookmarks path pan anchor c bookmarks }
3387 | Vopen (_, _, _) ->
3388 error "unexpected subelement in doc" s spos
3390 | Vclose "doc" ->
3391 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3392 { v with f = llppconfig }
3394 | Vclose _ -> error "unexpected close in doc" s spos
3396 and pbookmarks path pan anchor c bookmarks v t spos _ =
3397 match t with
3398 | Vdata | Vcdata -> v
3399 | Vend -> error "unexpected end of input in bookmarks" s spos
3400 | Vopen ("item", attrs, closed) ->
3401 let titleent, spage, srely = bookmark_of attrs in
3402 let page = fromstring int_of_string spos "page" spage 0
3403 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3404 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3405 if closed
3406 then { v with f = pbookmarks path pan anchor c bookmarks }
3407 else
3408 let f () = v in
3409 { v with f = skip "item" f }
3411 | Vopen _ ->
3412 error "unexpected subelement in bookmarks" s spos
3414 | Vclose "bookmarks" ->
3415 { v with f = doc path pan anchor c bookmarks }
3417 | Vclose _ -> error "unexpected close in bookmarks" s spos
3419 and skip tag f v t spos _ =
3420 match t with
3421 | Vdata | Vcdata -> v
3422 | Vend ->
3423 error ("unexpected end of input in skipped " ^ tag) s spos
3424 | Vopen (tag', _, closed) ->
3425 if closed
3426 then v
3427 else
3428 let f' () = { v with f = skip tag f } in
3429 { v with f = skip tag' f' }
3430 | Vclose ctag ->
3431 if tag = ctag
3432 then f ()
3433 else error ("unexpected close in skipped " ^ tag) s spos
3436 parse { f = toplevel; accu = () } s;
3437 h, dc;
3440 let do_load f ic =
3442 let len = in_channel_length ic in
3443 let s = String.create len in
3444 really_input ic s 0 len;
3445 f s;
3446 with
3447 | Parse_error (msg, s, pos) ->
3448 let subs = subs s pos in
3449 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3450 failwith ("parse error: " ^ s)
3452 | exn ->
3453 failwith ("config load error: " ^ Printexc.to_string exn)
3456 let path =
3457 let dir =
3459 let dir = Filename.concat home ".config" in
3460 if Sys.is_directory dir then dir else home
3461 with _ -> home
3463 Filename.concat dir "llpp.conf"
3466 let load1 f =
3467 if Sys.file_exists path
3468 then
3469 match
3470 (try Some (open_in_bin path)
3471 with exn ->
3472 prerr_endline
3473 ("Error opening configuation file `" ^ path ^ "': " ^
3474 Printexc.to_string exn);
3475 None
3477 with
3478 | Some ic ->
3479 begin try
3480 f (do_load get ic)
3481 with exn ->
3482 prerr_endline
3483 ("Error loading configuation from `" ^ path ^ "': " ^
3484 Printexc.to_string exn);
3485 end;
3486 close_in ic;
3488 | None -> ()
3489 else
3490 f (Hashtbl.create 0, defconf)
3493 let load () =
3494 let f (h, dc) =
3495 let pc, pb, px, pa =
3497 Hashtbl.find h (Filename.basename state.path)
3498 with Not_found -> dc, [], 0, (0, 0.0)
3500 setconf defconf dc;
3501 setconf conf pc;
3502 state.bookmarks <- pb;
3503 state.x <- px;
3504 state.scrollw <- conf.scrollbw;
3505 if conf.jumpback
3506 then state.anchor <- pa;
3507 cbput state.hists.nav pa;
3509 load1 f
3512 let add_attrs bb always dc c =
3513 let ob s a b =
3514 if always || a != b
3515 then Printf.bprintf bb "\n %s='%b'" s a
3516 and oi s a b =
3517 if always || a != b
3518 then Printf.bprintf bb "\n %s='%d'" s a
3519 and oz s a b =
3520 if always || a <> b
3521 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3522 and oc s a b =
3523 if always || a <> b
3524 then
3525 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3526 and os s a b =
3527 if always || a <> b
3528 then
3529 Printf.bprintf bb "\n %s='%s'" s a
3531 let w, h =
3532 if always
3533 then dc.winw, dc.winh
3534 else
3535 match state.fullscreen with
3536 | Some wh -> wh
3537 | None -> c.winw, c.winh
3539 let zoom, presentation, interpagespace, showall=
3540 if always
3541 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3542 else
3543 match state.mode with
3544 | Birdseye (bc, _, _, _, _) ->
3545 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3546 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3548 oi "width" w dc.winw;
3549 oi "height" h dc.winh;
3550 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3551 oi "scroll-handle-height" c.scrollh dc.scrollh;
3552 ob "case-insensitive-search" c.icase dc.icase;
3553 ob "preload" c.preload dc.preload;
3554 oi "page-bias" c.pagebias dc.pagebias;
3555 oi "scroll-step" c.scrollstep dc.scrollstep;
3556 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3557 ob "max-height-fit" c.maxhfit dc.maxhfit;
3558 ob "crop-hack" c.crophack dc.crophack;
3559 ob "throttle" showall dc.showall;
3560 ob "highlight-links" c.hlinks dc.hlinks;
3561 ob "under-cursor-info" c.underinfo dc.underinfo;
3562 oi "vertical-margin" interpagespace dc.interpagespace;
3563 oz "zoom" zoom dc.zoom;
3564 ob "presentation" presentation dc.presentation;
3565 oi "rotation-angle" c.angle dc.angle;
3566 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3567 ob "proportional-display" c.proportional dc.proportional;
3568 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3569 oi "texcount" c.texcount dc.texcount;
3570 oi "slice-height" c.sliceheight dc.sliceheight;
3571 oi "thumbnail-width" c.thumbw dc.thumbw;
3572 ob "persistent-location" c.jumpback dc.jumpback;
3573 oc "background-color" c.bgcolor dc.bgcolor;
3574 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3575 os "ui-font" c.uifont dc.uifont;
3578 let save () =
3579 let bb = Buffer.create 32768 in
3580 let f (h, dc) =
3581 let dc = if conf.bedefault then conf else dc in
3582 Buffer.add_string bb "<llppconfig>\n<defaults ";
3583 add_attrs bb true dc dc;
3584 Buffer.add_string bb "/>\n";
3586 let adddoc path pan anchor c bookmarks =
3587 if bookmarks == [] && c = dc && anchor = emptyanchor
3588 then ()
3589 else (
3590 Printf.bprintf bb "<doc path='%s'"
3591 (enent path 0 (String.length path));
3593 if anchor <> emptyanchor
3594 then (
3595 let n, y = anchor in
3596 Printf.bprintf bb " page='%d'" n;
3597 if y > 1e-6
3598 then
3599 Printf.bprintf bb " rely='%f'" y
3603 if pan != 0
3604 then Printf.bprintf bb " pan='%d'" pan;
3606 add_attrs bb false dc c;
3608 begin match bookmarks with
3609 | [] -> Buffer.add_string bb "/>\n"
3610 | _ ->
3611 Buffer.add_string bb ">\n<bookmarks>\n";
3612 List.iter (fun (title, _level, (page, rely)) ->
3613 Printf.bprintf bb
3614 "<item title='%s' page='%d'"
3615 (enent title 0 (String.length title))
3616 page
3618 if rely > 1e-6
3619 then
3620 Printf.bprintf bb " rely='%f'" rely
3622 Buffer.add_string bb "/>\n";
3623 ) bookmarks;
3624 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3625 end;
3629 let pan =
3630 match state.mode with
3631 | Birdseye (_, pan, _, _, _) -> pan
3632 | _ -> state.x
3634 let basename = Filename.basename state.path in
3635 adddoc basename pan (getanchor ())
3636 { conf with
3637 autoscrollstep =
3638 if state.ascrollstep > 0
3639 then state.ascrollstep
3640 else conf.autoscrollstep }
3641 (if conf.savebmarks then state.bookmarks else []);
3643 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3644 if basename <> path
3645 then adddoc path x y c bookmarks
3646 ) h;
3647 Buffer.add_string bb "</llppconfig>";
3649 load1 f;
3650 if Buffer.length bb > 0
3651 then
3653 let tmp = path ^ ".tmp" in
3654 let oc = open_out_bin tmp in
3655 Buffer.output_buffer oc bb;
3656 close_out oc;
3657 Sys.rename tmp path;
3658 with exn ->
3659 prerr_endline
3660 ("error while saving configuration: " ^ Printexc.to_string exn)
3662 end;;
3664 let () =
3665 Arg.parse
3666 (Arg.align
3667 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3668 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3669 " Print version and exit")]
3671 (fun s -> state.path <- s)
3672 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3674 if String.length state.path = 0
3675 then (prerr_endline "file name missing"; exit 1);
3677 State.load ();
3679 let _ = Glut.init Sys.argv in
3680 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3681 let () = Glut.initWindowSize conf.winw conf.winh in
3682 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3684 let csock, ssock =
3685 if Sys.os_type = "Unix"
3686 then
3687 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3688 else
3689 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3690 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3691 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3692 Unix.bind sock addr;
3693 Unix.listen sock 1;
3694 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3695 Unix.connect csock addr;
3696 let ssock, _ = Unix.accept sock in
3697 Unix.close sock;
3698 let opts sock =
3699 Unix.setsockopt sock Unix.TCP_NODELAY true;
3700 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3702 opts ssock;
3703 opts csock;
3704 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3705 ssock, csock
3708 let () = Glut.displayFunc display in
3709 let () = Glut.reshapeFunc reshape in
3710 let () = Glut.keyboardFunc keyboard in
3711 let () = Glut.specialFunc special in
3712 let () = Glut.idleFunc (Some idle) in
3713 let () = Glut.mouseFunc mouse in
3714 let () = Glut.motionFunc motion in
3715 let () = Glut.passiveMotionFunc pmotion in
3717 init ssock (
3718 conf.angle, conf.proportional, conf.texcount,
3719 conf.sliceheight, conf.uifont
3721 state.csock <- csock;
3722 state.ssock <- ssock;
3723 state.text <- "Opening " ^ state.path;
3724 writeopen state.path state.password;
3726 at_exit State.save;
3728 let rec handlelablglutbug () =
3730 Glut.mainLoop ();
3731 with Glut.BadEnum "key in special_of_int" ->
3732 showtext '!' " LablGlut bug: special key not recognized";
3733 handlelablglutbug ()
3735 handlelablglutbug ();