Switch beack to GL_NEAREST, otherwise Nimbus Mono looks very bad
[llpp.git] / main.ml
blobe87d2d8142a4dd4f43baee07377b381ba5f77f83
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params = angle * proportional * texcount * sliceheight * fontpath
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
25 and fontpath = string
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstr : int -> int -> int -> string -> unit = "ml_draw_string";;
38 type mpos = int * int
39 and mstate =
40 | Msel of (mpos * mpos)
41 | Mpan of mpos
42 | Mscroll
43 | Mzoom of (int * int)
44 | Mnone
47 type textentry = string * string * onhist option * onkey * ondone
48 and onkey = string -> int -> te
49 and ondone = string -> unit
50 and histcancel = unit -> unit
51 and onhist = ((histcmd -> string) * histcancel)
52 and histcmd = HCnext | HCprev | HCfirst | HClast
53 and te =
54 | TEstop
55 | TEdone of string
56 | TEcont of string
57 | TEswitch of textentry
60 type 'a circbuf =
61 { store : 'a array
62 ; mutable rc : int
63 ; mutable wc : int
64 ; mutable len : int
68 let cbnew n v =
69 { store = Array.create n v
70 ; rc = 0
71 ; wc = 0
72 ; len = 0
76 let cbcap b = Array.length b.store;;
78 let cbput b v =
79 let cap = cbcap b in
80 b.store.(b.wc) <- v;
81 b.wc <- (b.wc + 1) mod cap;
82 b.rc <- b.wc;
83 b.len <- min (b.len + 1) cap;
86 let cbempty b = b.len = 0;;
88 let cbgetg b circular dir =
89 if cbempty b
90 then b.store.(0)
91 else
92 let rc = b.rc + dir in
93 let rc =
94 if circular
95 then (
96 if rc = -1
97 then b.len-1
98 else (
99 if rc = b.len
100 then 0
101 else rc
104 else max 0 (min rc (b.len-1))
106 b.rc <- rc;
107 b.store.(rc);
110 let cbget b = cbgetg b false;;
111 let cbgetc b = cbgetg b true;;
113 let cbpeek b =
114 let rc = b.wc - b.len in
115 let rc = if rc < 0 then cbcap b + rc else rc in
116 b.store.(rc);
119 let cbdecr b = b.len <- b.len - 1;;
121 type layout =
122 { pageno : int
123 ; pagedimno : int
124 ; pagew : int
125 ; pageh : int
126 ; pagedispy : int
127 ; pagey : int
128 ; pagevh : int
129 ; pagex : int
133 type conf =
134 { mutable scrollbw : int
135 ; mutable scrollh : int
136 ; mutable icase : bool
137 ; mutable preload : bool
138 ; mutable pagebias : int
139 ; mutable verbose : bool
140 ; mutable scrollstep : int
141 ; mutable maxhfit : bool
142 ; mutable crophack : bool
143 ; mutable autoscrollstep : int
144 ; mutable showall : bool
145 ; mutable hlinks : bool
146 ; mutable underinfo : bool
147 ; mutable interpagespace : interpagespace
148 ; mutable zoom : float
149 ; mutable presentation : bool
150 ; mutable angle : angle
151 ; mutable winw : int
152 ; mutable winh : int
153 ; mutable savebmarks : bool
154 ; mutable proportional : proportional
155 ; mutable memlimit : int
156 ; mutable texcount : texcount
157 ; mutable sliceheight : sliceheight
158 ; mutable thumbw : width
159 ; mutable jumpback : bool
160 ; mutable bgcolor : float * float * float
161 ; mutable bedefault : bool
162 ; mutable scrollbarinpm : bool
163 ; mutable uifont : string
167 type anchor = pageno * top;;
169 type outline = string * int * anchor
170 and outlines =
171 | Oarray of outline array
172 | Olist of outline list
173 | Onarrow of string * outline array * outline array
176 type rect = float * float * float * float * float * float * float * float;;
178 type pagemapkey = pageno * width * angle * proportional * gen;;
180 let emptyanchor = (0, 0.0);;
181 let initialanchor = (-1, nan);;
183 type mode =
184 | Birdseye of (conf * leftx * pageno * pageno * anchor)
185 | Outline of (bool * int * int * outline array * string * int * mode)
186 | Items of (int * int * item array * string * int * mode)
187 | Textentry of (textentry * onleave)
188 | View
189 and onleave = leavetextentrystatus -> unit
190 and leavetextentrystatus = | Cancel | Confirm
191 and item = string * int * action
192 and action =
193 | Noaction
194 | Action of (int -> int -> string -> int -> mode)
197 let isbirdseye = function Birdseye _ -> true | _ -> false;;
198 let istextentry = function Textentry _ -> true | _ -> false;;
200 type state =
201 { mutable csock : Unix.file_descr
202 ; mutable ssock : Unix.file_descr
203 ; mutable w : int
204 ; mutable x : int
205 ; mutable y : int
206 ; mutable scrollw : int
207 ; mutable anchor : anchor
208 ; mutable maxy : int
209 ; mutable layout : layout list
210 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
211 ; mutable pdims : (pageno * width * height * leftx) list
212 ; mutable pagecount : int
213 ; pagecache : string circbuf
214 ; mutable rendering : bool
215 ; mutable mstate : mstate
216 ; mutable searchpattern : string
217 ; mutable rects : (pageno * recttype * rect) list
218 ; mutable rects1 : (pageno * recttype * rect) list
219 ; mutable text : string
220 ; mutable fullscreen : (width * height) option
221 ; mutable mode : mode
222 ; mutable outlines : outlines
223 ; mutable bookmarks : outline list
224 ; mutable path : string
225 ; mutable password : string
226 ; mutable invalidated : int
227 ; mutable colorscale : float
228 ; mutable memused : int
229 ; mutable gen : gen
230 ; mutable throttle : layout list option
231 ; mutable ascrollstep : int
232 ; mutable help : item array
233 ; mutable docinfo : (int * string) list
234 ; hists : hists
236 and hists =
237 { pat : string circbuf
238 ; pag : string circbuf
239 ; nav : anchor circbuf
243 let defconf =
244 { scrollbw = 7
245 ; scrollh = 12
246 ; icase = true
247 ; preload = true
248 ; pagebias = 0
249 ; verbose = false
250 ; scrollstep = 24
251 ; maxhfit = true
252 ; crophack = false
253 ; autoscrollstep = 24
254 ; showall = false
255 ; hlinks = false
256 ; underinfo = false
257 ; interpagespace = 2
258 ; zoom = 1.0
259 ; presentation = false
260 ; angle = 0
261 ; winw = 900
262 ; winh = 900
263 ; savebmarks = true
264 ; proportional = true
265 ; memlimit = 32*1024*1024
266 ; texcount = 256
267 ; sliceheight = 24
268 ; thumbw = 76
269 ; jumpback = false
270 ; bgcolor = (0.5, 0.5, 0.5)
271 ; bedefault = false
272 ; scrollbarinpm = true
273 ; uifont = ""
277 let conf = { defconf with angle = defconf.angle };;
279 let makehelp () =
280 let strings = ("llpp version " ^ Help.version) :: "" :: Help.keys in
281 Array.of_list (List.map (fun s -> s, 0, Noaction) strings);
284 let state =
285 { csock = Unix.stdin
286 ; ssock = Unix.stdin
287 ; x = 0
288 ; y = 0
289 ; w = 0
290 ; scrollw = 0
291 ; anchor = initialanchor
292 ; layout = []
293 ; maxy = max_int
294 ; pagemap = Hashtbl.create 10
295 ; pagecache = cbnew 100 ""
296 ; pdims = []
297 ; pagecount = 0
298 ; rendering = false
299 ; mstate = Mnone
300 ; rects = []
301 ; rects1 = []
302 ; text = ""
303 ; mode = View
304 ; fullscreen = None
305 ; searchpattern = ""
306 ; outlines = Olist []
307 ; bookmarks = []
308 ; path = ""
309 ; password = ""
310 ; invalidated = 0
311 ; hists =
312 { nav = cbnew 100 (0, 0.0)
313 ; pat = cbnew 20 ""
314 ; pag = cbnew 10 ""
316 ; colorscale = 1.0
317 ; memused = 0
318 ; gen = 0
319 ; throttle = None
320 ; ascrollstep = 0
321 ; help = makehelp ()
322 ; docinfo = []
326 let vlog fmt =
327 if conf.verbose
328 then
329 Printf.kprintf prerr_endline fmt
330 else
331 Printf.kprintf ignore fmt
334 let writecmd fd s =
335 let len = String.length s in
336 let n = 4 + len in
337 let b = Buffer.create n in
338 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
339 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
340 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
341 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
342 Buffer.add_string b s;
343 let s' = Buffer.contents b in
344 let n' = Unix.write fd s' 0 n in
345 if n' != n then failwith "write failed";
348 let readcmd fd =
349 let s = "xxxx" in
350 let n = Unix.read fd s 0 4 in
351 if n != 4 then failwith "incomplete read(len)";
352 let len = 0
353 lor (Char.code s.[0] lsl 24)
354 lor (Char.code s.[1] lsl 16)
355 lor (Char.code s.[2] lsl 8)
356 lor (Char.code s.[3] lsl 0)
358 let s = String.create len in
359 let n = Unix.read fd s 0 len in
360 if n != len then failwith "incomplete read(data)";
364 let makecmd s l =
365 let b = Buffer.create 10 in
366 Buffer.add_string b s;
367 let rec combine = function
368 | [] -> b
369 | x :: xs ->
370 Buffer.add_char b ' ';
371 let s =
372 match x with
373 | `b b -> if b then "1" else "0"
374 | `s s -> s
375 | `i i -> string_of_int i
376 | `f f -> string_of_float f
377 | `I f -> string_of_int (truncate f)
379 Buffer.add_string b s;
380 combine xs;
382 combine l;
385 let wcmd s l =
386 let cmd = Buffer.contents (makecmd s l) in
387 writecmd state.csock cmd;
390 let calcips h =
391 if conf.presentation
392 then
393 let d = conf.winh - h in
394 max 0 ((d + 1) / 2)
395 else
396 conf.interpagespace
399 let calcheight () =
400 let rec f pn ph pi fh l =
401 match l with
402 | (n, _, h, _) :: rest ->
403 let ips = calcips h in
404 let fh =
405 if conf.presentation
406 then fh+ips
407 else (
408 if isbirdseye state.mode && pn = 0
409 then fh + ips
410 else fh
413 let fh = fh + ((n - pn) * (ph + pi)) in
414 f n h ips fh rest;
416 | [] ->
417 let inc =
418 if conf.presentation || (isbirdseye state.mode && pn = 0)
419 then 0
420 else -pi
422 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
423 max 0 fh
425 let fh = f 0 0 0 0 state.pdims in
429 let getpageyh pageno =
430 let rec f pn ph pi y l =
431 match l with
432 | (n, _, h, _) :: rest ->
433 let ips = calcips h in
434 if n >= pageno
435 then
436 let h = if n = pageno then h else ph in
437 if conf.presentation && n = pageno
438 then
439 y + (pageno - pn) * (ph + pi) + pi, h
440 else
441 y + (pageno - pn) * (ph + pi), h
442 else
443 let y = y + (if conf.presentation then pi else 0) in
444 let y = y + (n - pn) * (ph + pi) in
445 f n h ips y rest
447 | [] ->
448 y + (pageno - pn) * (ph + pi), ph
450 f 0 0 0 0 state.pdims
453 let getpagey pageno = fst (getpageyh pageno);;
455 let layout y sh =
456 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
457 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
458 match pdims with
459 | (pageno', w, h, x) :: rest when pageno' = pageno ->
460 let ips = calcips h in
461 let yinc =
462 if conf.presentation || (isbirdseye state.mode && pageno = 0)
463 then ips
464 else 0
466 (w, h, ips, x), rest, pdimno + 1, yinc
467 | _ ->
468 prev, pdims, pdimno, 0
470 let dy = dy + yinc in
471 let py = py + yinc in
472 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
473 then
474 accu
475 else
476 let vy = y + dy in
477 if py + h <= vy - yinc
478 then
479 let py = py + h + ips in
480 let dy = max 0 (py - y) in
481 f ~pageno:(pageno+1)
482 ~pdimno
483 ~prev:curr
486 ~pdims:rest
487 ~cacheleft
488 ~accu
489 else
490 let pagey = vy - py in
491 let pagevh = h - pagey in
492 let pagevh = min (sh - dy) pagevh in
493 let off = if yinc > 0 then py - vy else 0 in
494 let py = py + h + ips in
495 let e =
496 { pageno = pageno
497 ; pagedimno = pdimno
498 ; pagew = w
499 ; pageh = h
500 ; pagedispy = dy + off
501 ; pagey = pagey + off
502 ; pagevh = pagevh - off
503 ; pagex = x
506 let accu = e :: accu in
507 f ~pageno:(pageno+1)
508 ~pdimno
509 ~prev:curr
511 ~dy:(dy+pagevh+ips)
512 ~pdims:rest
513 ~cacheleft:(cacheleft-1)
514 ~accu
516 if state.invalidated = 0
517 then (
518 let accu =
520 ~pageno:0
521 ~pdimno:~-1
522 ~prev:(0,0,0,0)
523 ~py:0
524 ~dy:0
525 ~pdims:state.pdims
526 ~cacheleft:(cbcap state.pagecache)
527 ~accu:[]
529 List.rev accu
531 else
535 let clamp incr =
536 let y = state.y + incr in
537 let y = max 0 y in
538 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
542 let getopaque pageno =
543 try Some (Hashtbl.find state.pagemap
544 (pageno, state.w, conf.angle, conf.proportional, state.gen))
545 with Not_found -> None
548 let cache pageno opaque =
549 Hashtbl.replace state.pagemap
550 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
553 let validopaque opaque = String.length opaque > 0;;
555 let render l =
556 match getopaque l.pageno with
557 | None when not state.rendering ->
558 state.rendering <- true;
559 cache l.pageno ("", -1);
560 wcmd "render" [`i (l.pageno + 1)
561 ;`i l.pagedimno
562 ;`i l.pagew
563 ;`i l.pageh];
564 | _ -> ()
567 let loadlayout layout =
568 let rec f all = function
569 | l :: ls ->
570 begin match getopaque l.pageno with
571 | None -> render l; f false ls
572 | Some (opaque, _) -> f (all && validopaque opaque) ls
574 | [] -> all
576 f (layout <> []) layout;
579 let findpageforopaque opaque =
580 Hashtbl.fold
581 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
582 state.pagemap None
585 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
587 let preload () =
588 let oktopreload =
589 if conf.preload
590 then
591 let memleft = conf.memlimit - state.memused in
592 if memleft < 0
593 then
594 let opaque = cbpeek state.pagecache in
595 match findpageforopaque opaque with
596 | Some ((n, _, _, _, _), size) ->
597 memleft + size >= 0 && not (pagevisible state.layout n)
598 | None -> false
599 else true
600 else false
602 if oktopreload
603 then
604 let presentation = conf.presentation in
605 let interpagespace = conf.interpagespace in
606 let maxy = state.maxy in
607 conf.presentation <- false;
608 conf.interpagespace <- 0;
609 state.maxy <- calcheight ();
610 let y =
611 match state.layout with
612 | [] -> 0
613 | l :: _ -> getpagey l.pageno + l.pagey
615 let y = if y < conf.winh then 0 else y - conf.winh in
616 let pages = layout y (conf.winh*3) in
617 List.iter render pages;
618 conf.presentation <- presentation;
619 conf.interpagespace <- interpagespace;
620 state.maxy <- maxy;
623 let gotoy y =
624 let y = max 0 y in
625 let y = min state.maxy y in
626 let pages = layout y conf.winh in
627 let ready = loadlayout pages in
628 if conf.showall
629 then (
630 if ready
631 then (
632 state.y <- y;
633 state.layout <- pages;
634 state.throttle <- None;
635 Glut.postRedisplay ();
637 else (
638 state.throttle <- Some pages;
641 else (
642 state.y <- y;
643 state.layout <- pages;
644 state.throttle <- None;
645 Glut.postRedisplay ();
647 begin match state.mode with
648 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
649 if not (pagevisible pages pageno)
650 then (
651 match state.layout with
652 | [] -> ()
653 | l :: _ ->
654 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
656 | _ -> ()
657 end;
658 preload ();
661 let gotoy_and_clear_text y =
662 gotoy y;
663 if not conf.verbose then state.text <- "";
666 let getanchor () =
667 match state.layout with
668 | [] -> emptyanchor
669 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
672 let getanchory (n, top) =
673 let y, h = getpageyh n in
674 y + (truncate (top *. float h));
677 let gotoanchor anchor =
678 gotoy (getanchory anchor);
681 let addnav () =
682 cbput state.hists.nav (getanchor ());
685 let getnav dir =
686 let anchor = cbgetc state.hists.nav dir in
687 getanchory anchor;
690 let gotopage n top =
691 let y, h = getpageyh n in
692 gotoy_and_clear_text (y + (truncate (top *. float h)));
695 let gotopage1 n top =
696 let y = getpagey n in
697 gotoy_and_clear_text (y + top);
700 let invalidate () =
701 state.layout <- [];
702 state.pdims <- [];
703 state.rects <- [];
704 state.rects1 <- [];
705 state.invalidated <- state.invalidated + 1;
708 let scalecolor c =
709 let c = c *. state.colorscale in
710 (c, c, c);
713 let scalecolor2 (r, g, b) =
714 (r *. state.colorscale, g *. state.colorscale, b *. state.colorscale);
717 let represent () =
718 state.maxy <- calcheight ();
719 match state.mode with
720 | Birdseye (_, _, pageno, _, _) ->
721 let y, h = getpageyh pageno in
722 let top = (conf.winh - h) / 2 in
723 gotoy (max 0 (y - top))
724 | _ -> gotoanchor state.anchor
727 let pagematrix () =
728 GlMat.mode `projection;
729 GlMat.load_identity ();
730 GlMat.rotate ~x:1.0 ~angle:180.0 ();
731 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
732 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
733 if state.x != 0
734 then (
735 GlMat.translate ~x:(float state.x) ();
739 let winmatrix () =
740 GlMat.mode `projection;
741 GlMat.load_identity ();
742 GlMat.rotate ~x:1.0 ~angle:180.0 ();
743 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
744 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
747 let reshape ~w ~h =
748 if state.invalidated = 0 && state.anchor == initialanchor
749 then state.anchor <- getanchor ();
751 conf.winw <- w;
752 let w = truncate (float w *. conf.zoom) - state.scrollw in
753 let w = max w 2 in
754 state.w <- w;
755 conf.winh <- h;
756 GlMat.mode `modelview;
757 GlMat.load_identity ();
758 GlClear.color (scalecolor 1.0);
759 GlClear.clear [`color];
761 invalidate ();
762 wcmd "geometry" [`i w; `i h];
765 let drawstring size x y s =
766 Gl.enable `blend;
767 Gl.enable `texture_2d;
768 drawstr size x y s;
769 Gl.disable `blend;
770 Gl.disable `texture_2d;
773 let drawstring1 size x y s =
774 drawstr size x y s;
777 let enttext () =
778 let drawstring s =
779 GlDraw.color (0.0, 0.0, 0.0);
780 GlDraw.rect
781 (0.0, float (conf.winh - 18))
782 (float (conf.winw - state.scrollw - 1), float conf.winh)
784 GlDraw.color (1.0, 1.0, 1.0);
785 drawstring 14 8 (conf.winh - 5) s;
787 let len = String.length state.text in
788 match state.mode with
789 | Textentry ((prefix, text, _, _, _), _) ->
790 let s =
791 match String.length prefix with
792 | 0 | 1 ->
793 if len > 0
794 then
795 Printf.sprintf "%s%s\226–� [%s]" prefix text state.text
796 else
797 Printf.sprintf "%s%s\226–�" prefix text
799 | _ ->
800 if len > 0
801 then
802 Printf.sprintf "%s: %s\226–� [%s]" prefix text state.text
803 else
804 Printf.sprintf "%s: %s\226–�" prefix text
806 drawstring s
808 | _ ->
809 if len > 0 then drawstring state.text
812 let showtext c s =
813 state.text <- Printf.sprintf "%c%s" c s;
814 Glut.postRedisplay ();
817 let act cmd =
818 match cmd.[0] with
819 | 'c' ->
820 state.pdims <- [];
822 | 'D' ->
823 state.rects <- state.rects1;
824 Glut.postRedisplay ()
826 | 'C' ->
827 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
828 state.pagecount <- n;
829 state.invalidated <- state.invalidated - 1;
830 if state.invalidated = 0
831 then represent ()
833 | 't' ->
834 let s = Scanf.sscanf cmd "t %n"
835 (fun n -> String.sub cmd n (String.length cmd - n))
837 Glut.setWindowTitle s
839 | 'T' ->
840 let s = Scanf.sscanf cmd "T %n"
841 (fun n -> String.sub cmd n (String.length cmd - n))
843 if istextentry state.mode
844 then (
845 state.text <- s;
846 showtext ' ' s;
848 else (
849 state.text <- s;
850 Glut.postRedisplay ();
853 | 'V' ->
854 if conf.verbose
855 then
856 let s = Scanf.sscanf cmd "V %n"
857 (fun n -> String.sub cmd n (String.length cmd - n))
859 state.text <- s;
860 showtext ' ' s;
862 | 'F' ->
863 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
864 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
865 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
866 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
868 let y = (getpagey pageno) + truncate y0 in
869 addnav ();
870 gotoy y;
871 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
873 | 'R' ->
874 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
875 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
876 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
877 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
879 state.rects1 <-
880 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
882 | 'r' ->
883 let n, w, _h, r, l, s, p =
884 Scanf.sscanf cmd "r %u %u %u %d %d %u %s"
885 (fun n w h r l s p ->
886 (n-1, w, h, r, l != 0, s, p))
889 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
890 state.memused <- state.memused + s;
892 let layout =
893 match state.throttle with
894 | None -> state.layout
895 | Some layout -> layout
898 let rec gc () =
899 if (state.memused <= conf.memlimit) || cbempty state.pagecache
900 then ()
901 else (
902 let evictedopaque = cbpeek state.pagecache in
903 match findpageforopaque evictedopaque with
904 | None -> failwith "bug in gc"
905 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
906 if state.gen != gen || not (pagevisible layout evictedn)
907 then (
908 wcmd "free" [`s evictedopaque];
909 state.memused <- state.memused - evictedsize;
910 Hashtbl.remove state.pagemap k;
911 cbdecr state.pagecache;
912 gc ();
916 gc ();
918 cbput state.pagecache p;
919 state.rendering <- false;
921 begin match state.throttle with
922 | None ->
923 if pagevisible state.layout n
924 then gotoy state.y
925 else (
926 let allvisible = loadlayout state.layout in
927 if allvisible then preload ();
930 | Some layout ->
931 match layout with
932 | [] -> ()
933 | l :: _ ->
934 let y = getpagey l.pageno + l.pagey in
935 gotoy y
938 | 'l' ->
939 let pdim =
940 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
942 state.pdims <- pdim :: state.pdims
944 | 'o' ->
945 let (l, n, t, h, pos) =
946 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
948 let s = String.sub cmd pos (String.length cmd - pos) in
949 let outline = (s, l, (n, float t /. float h)) in
950 let outlines =
951 match state.outlines with
952 | Olist outlines -> Olist (outline :: outlines)
953 | Oarray _ -> Olist [outline]
954 | Onarrow _ -> Olist [outline]
956 state.outlines <- outlines
959 | 'i' ->
960 let s = Scanf.sscanf cmd "i %n"
961 (fun n -> String.sub cmd n (String.length cmd - n))
963 let len = String.length s in
964 let rec fold accu pos =
965 let eolpos =
966 try String.index_from s pos '\n' with Not_found -> len
968 if eolpos = len
969 then List.rev accu
970 else
971 let line = String.sub s pos (eolpos - pos) in
972 fold ((1, line)::accu) (eolpos+1)
974 state.docinfo <- fold state.docinfo 0
976 | _ ->
977 dolog "unknown cmd `%S'" cmd
980 let now = Unix.gettimeofday;;
982 let idle () =
983 let rec loop delay =
984 let r, _, _ = Unix.select [state.csock] [] [] delay in
985 begin match r with
986 | [] ->
987 if state.ascrollstep > 0
988 then begin
989 let y = state.y + state.ascrollstep in
990 let y = if y >= state.maxy then 0 else y in
991 gotoy y;
992 state.text <- "";
993 end;
995 | _ ->
996 let cmd = readcmd state.csock in
997 act cmd;
998 loop 0.0
999 end;
1000 in loop 0.001
1003 let onhist cb =
1004 let rc = cb.rc in
1005 let action = function
1006 | HCprev -> cbget cb ~-1
1007 | HCnext -> cbget cb 1
1008 | HCfirst -> cbget cb ~-(cb.rc)
1009 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1010 and cancel () = cb.rc <- rc
1011 in (action, cancel)
1014 let search pattern forward =
1015 if String.length pattern > 0
1016 then
1017 let pn, py =
1018 match state.layout with
1019 | [] -> 0, 0
1020 | l :: _ ->
1021 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1023 let cmd =
1024 let b = makecmd "search"
1025 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1027 Buffer.add_char b ',';
1028 Buffer.add_string b pattern;
1029 Buffer.add_char b '\000';
1030 Buffer.contents b;
1032 writecmd state.csock cmd;
1035 let intentry text key =
1036 let c = Char.unsafe_chr key in
1037 match c with
1038 | '0' .. '9' ->
1039 let s = "x" in s.[0] <- c;
1040 let text = text ^ s in
1041 TEcont text
1043 | _ ->
1044 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1045 TEcont text
1048 let addchar s c =
1049 let b = Buffer.create (String.length s + 1) in
1050 Buffer.add_string b s;
1051 Buffer.add_char b c;
1052 Buffer.contents b;
1055 let textentry text key =
1056 let c = Char.unsafe_chr key in
1057 match c with
1058 | _ when key >= 32 && key < 127 ->
1059 let text = addchar text c in
1060 TEcont text
1062 | _ ->
1063 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1064 TEcont text
1067 let reinit angle proportional =
1068 conf.angle <- angle;
1069 conf.proportional <- proportional;
1070 invalidate ();
1071 wcmd "reinit" [`i angle; `b proportional];
1074 let setzoom zoom =
1075 let zoom = max 0.01 (min 2.2 zoom) in
1076 if zoom <> conf.zoom
1077 then (
1078 if zoom <= 1.0
1079 then state.x <- 0;
1080 conf.zoom <- zoom;
1081 if state.invalidated = 0
1082 then
1083 state.anchor <- getanchor ()
1085 reshape conf.winw conf.winh;
1086 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1090 let enterbirdseye () =
1091 let zoom = float conf.thumbw /. float conf.winw in
1092 let birdseyepageno =
1093 let cy = conf.winh / 2 in
1094 let fold = function
1095 | [] -> 0
1096 | l :: rest ->
1097 let rec fold best = function
1098 | [] -> best.pageno
1099 | l :: rest ->
1100 let d = cy - (l.pagedispy + l.pagevh/2)
1101 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1102 if abs d < abs dbest
1103 then fold l rest
1104 else best.pageno
1105 in fold l rest
1107 fold state.layout
1109 state.mode <- Birdseye (
1110 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1112 conf.zoom <- zoom;
1113 conf.presentation <- false;
1114 conf.interpagespace <- 10;
1115 conf.hlinks <- false;
1116 state.x <- 0;
1117 state.mstate <- Mnone;
1118 conf.showall <- false;
1119 Glut.setCursor Glut.CURSOR_INHERIT;
1120 if conf.verbose
1121 then
1122 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1123 (100.0*.zoom)
1124 else
1125 state.text <- ""
1127 reshape conf.winw conf.winh;
1130 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1131 state.mode <- View;
1132 conf.zoom <- c.zoom;
1133 conf.presentation <- c.presentation;
1134 conf.interpagespace <- c.interpagespace;
1135 conf.showall <- c.showall;
1136 conf.hlinks <- c.hlinks;
1137 state.x <- leftx;
1138 if conf.verbose
1139 then
1140 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1141 (100.0*.conf.zoom)
1143 reshape conf.winw conf.winh;
1144 state.anchor <- if goback then anchor else (pageno, 0.0);
1147 let togglebirdseye () =
1148 match state.mode with
1149 | Birdseye vals -> leavebirdseye vals true
1150 | View | Outline _ -> enterbirdseye ()
1151 | _ -> ()
1154 let upbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1155 let pageno = max 0 (pageno - 1) in
1156 let rec loop = function
1157 | [] -> gotopage1 pageno 0
1158 | l :: _ when l.pageno = pageno ->
1159 if l.pagedispy >= 0 && l.pagey = 0
1160 then Glut.postRedisplay ()
1161 else gotopage1 pageno 0
1162 | _ :: rest -> loop rest
1164 loop state.layout;
1165 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1168 let downbirdseye (conf, leftx, pageno, hooverpageno, anchor) =
1169 let pageno = min (state.pagecount - 1) (pageno + 1) in
1170 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1171 let rec loop = function
1172 | [] ->
1173 let y, h = getpageyh pageno in
1174 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1175 gotoy (clamp dy)
1176 | l :: _ when l.pageno = pageno ->
1177 if l.pagevh != l.pageh
1178 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1179 else Glut.postRedisplay ()
1180 | _ :: rest -> loop rest
1182 loop state.layout
1185 let optentry mode _ key =
1186 let btos b = if b then "on" else "off" in
1187 let c = Char.unsafe_chr key in
1188 match c with
1189 | 's' ->
1190 let ondone s =
1191 try conf.scrollstep <- int_of_string s with exc ->
1192 state.text <- Printf.sprintf "bad integer `%s': %s"
1193 s (Printexc.to_string exc)
1195 TEswitch ("scroll step", "", None, intentry, ondone)
1197 | 'A' ->
1198 let ondone s =
1200 conf.autoscrollstep <- int_of_string s;
1201 if state.ascrollstep > 0
1202 then state.ascrollstep <- conf.autoscrollstep;
1203 with exc ->
1204 state.text <- Printf.sprintf "bad integer `%s': %s"
1205 s (Printexc.to_string exc)
1207 TEswitch ("auto scroll step", "", None, intentry, ondone)
1209 | 'Z' ->
1210 let ondone s =
1212 let zoom = float (int_of_string s) /. 100.0 in
1213 setzoom zoom
1214 with exc ->
1215 state.text <- Printf.sprintf "bad integer `%s': %s"
1216 s (Printexc.to_string exc)
1218 TEswitch ("zoom", "", None, intentry, ondone)
1220 | 't' ->
1221 let ondone s =
1223 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1224 state.text <-
1225 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1226 begin match mode with
1227 | Birdseye beye ->
1228 leavebirdseye beye false;
1229 enterbirdseye ();
1230 | _ -> ();
1232 with exc ->
1233 state.text <- Printf.sprintf "bad integer `%s': %s"
1234 s (Printexc.to_string exc)
1236 TEswitch ("thumbnail width", "", None, intentry, ondone)
1238 | 'R' ->
1239 let ondone s =
1240 match try
1241 Some (int_of_string s)
1242 with exc ->
1243 state.text <- Printf.sprintf "bad integer `%s': %s"
1244 s (Printexc.to_string exc);
1245 None
1246 with
1247 | Some angle -> reinit angle conf.proportional
1248 | None -> ()
1250 TEswitch ("rotation", "", None, intentry, ondone)
1252 | 'i' ->
1253 conf.icase <- not conf.icase;
1254 TEdone ("case insensitive search " ^ (btos conf.icase))
1256 | 'p' ->
1257 conf.preload <- not conf.preload;
1258 gotoy state.y;
1259 TEdone ("preload " ^ (btos conf.preload))
1261 | 'v' ->
1262 conf.verbose <- not conf.verbose;
1263 TEdone ("verbose " ^ (btos conf.verbose))
1265 | 'h' ->
1266 conf.maxhfit <- not conf.maxhfit;
1267 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1268 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1270 | 'c' ->
1271 conf.crophack <- not conf.crophack;
1272 TEdone ("crophack " ^ btos conf.crophack)
1274 | 'a' ->
1275 conf.showall <- not conf.showall;
1276 TEdone ("throttle " ^ btos conf.showall)
1278 | 'f' ->
1279 conf.underinfo <- not conf.underinfo;
1280 TEdone ("underinfo " ^ btos conf.underinfo)
1282 | 'P' ->
1283 conf.savebmarks <- not conf.savebmarks;
1284 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1286 | 'S' ->
1287 let ondone s =
1289 let pageno, py =
1290 match state.layout with
1291 | [] -> 0, 0
1292 | l :: _ ->
1293 l.pageno, l.pagey
1295 conf.interpagespace <- int_of_string s;
1296 state.maxy <- calcheight ();
1297 let y = getpagey pageno in
1298 gotoy (y + py)
1299 with exc ->
1300 state.text <- Printf.sprintf "bad integer `%s': %s"
1301 s (Printexc.to_string exc)
1303 TEswitch ("vertical margin", "", None, intentry, ondone)
1305 | 'l' ->
1306 reinit conf.angle (not conf.proportional);
1307 TEdone ("proprortional display " ^ btos conf.proportional)
1309 | _ ->
1310 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1311 TEstop
1314 let maxoutlinerows () = (conf.winh - 31) / 16;;
1316 let enterselector allowdel outlines errmsg msg =
1317 if Array.length outlines = 0
1318 then (
1319 showtext ' ' errmsg;
1321 else (
1322 state.text <- msg;
1323 Glut.setCursor Glut.CURSOR_INHERIT;
1324 let pageno =
1325 match state.layout with
1326 | [] -> -1
1327 | {pageno=pageno} :: _ -> pageno
1329 let active =
1330 let rec loop n =
1331 if n = Array.length outlines
1332 then 0
1333 else
1334 let (_, _, (outlinepageno, _)) = outlines.(n) in
1335 if outlinepageno >= pageno then n else loop (n+1)
1337 loop 0
1339 state.mode <- Outline
1340 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0,
1341 state.mode);
1342 Glut.postRedisplay ();
1346 let enteroutlinemode () =
1347 let outlines, msg =
1348 match state.outlines with
1349 | Oarray a -> a, ""
1350 | Olist l ->
1351 let a = Array.of_list (List.rev l) in
1352 state.outlines <- Oarray a;
1353 a, ""
1354 | Onarrow (pat, a, _) ->
1355 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1357 enterselector false outlines "Document has no outline" msg;
1360 let enterbookmarkmode () =
1361 let bookmarks = Array.of_list state.bookmarks in
1362 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1365 let mode_to_string mode =
1366 let b = Buffer.create 10 in
1367 let rec f = function
1368 | Textentry (_, _) -> Buffer.add_string b "Textentry ";
1369 | View -> Buffer.add_string b "View"
1370 | Birdseye _ -> Buffer.add_string b "Birdseye"
1371 | Items _ -> Buffer.add_string b "Items"
1372 | Outline _ -> Buffer.add_string b "Outline"
1374 f mode;
1375 Buffer.contents b;
1378 let color_of_string s =
1379 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
1380 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
1384 let color_to_string (r, g, b) =
1385 let r = truncate (r *. 256.0)
1386 and g = truncate (g *. 256.0)
1387 and b = truncate (b *. 256.0) in
1388 Printf.sprintf "%d/%d/%d" r g b
1391 let enterinfomode () =
1392 let btos = function true -> "on" | _ -> "off" in
1393 let mode = state.mode in
1394 let rec makeitems () =
1395 let intp name get set =
1396 Printf.sprintf "%-24s %d" name (get ()), 1, Action (
1397 fun active first _ pan ->
1398 let ondone s =
1399 let n =
1400 try int_of_string s
1401 with exn ->
1402 state.text <- Printf.sprintf "bad integer `%s': %s"
1403 s (Printexc.to_string exn);
1404 max_int;
1406 if n != max_int then set n;
1408 let te = name, "", None, intentry, ondone in
1409 state.text <- "";
1410 Textentry (
1412 fun _ ->
1413 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1416 and boolp name get set =
1417 Printf.sprintf "%-24s %s" name (btos (get ())), 1, Action (
1418 fun active first qsearch pan ->
1419 let v = get () in
1420 set (not v);
1421 Items (active, first, makeitems (), qsearch, pan, mode);
1423 and colorp name get set =
1424 Printf.sprintf "%-24s %s" name (color_to_string (get ())), 1, Action (
1425 fun active first _ pan ->
1426 let invalid = (nan, nan, nan) in
1427 let ondone s =
1428 let c =
1429 try color_of_string s
1430 with exn ->
1431 state.text <- Printf.sprintf "bad color `%s': %s"
1432 s (Printexc.to_string exn);
1433 invalid
1435 if c <> invalid
1436 then set c;
1438 let te = name, "", None, textentry, ondone in
1439 state.text <- "";
1440 Textentry (
1442 fun _ ->
1443 state.mode <- Items (active, first, makeitems (), "", pan, mode)
1448 let birdseye = isbirdseye mode in
1449 let items = [
1450 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction;
1452 boolp "presentation"
1453 (fun () -> conf.presentation)
1454 (fun v ->
1455 conf.presentation <- v;
1456 state.anchor <- getanchor ();
1457 represent ());
1459 boolp "ignore case in searches"
1460 (fun () -> conf.icase)
1461 (fun v -> conf.icase <- v);
1463 boolp "preload"
1464 (fun () -> conf.preload)
1465 (fun v -> conf.preload <- v);
1467 boolp "verbose"
1468 (fun () -> conf.verbose)
1469 (fun v -> conf.verbose <- v);
1471 boolp "max fit"
1472 (fun () -> conf.maxhfit)
1473 (fun v -> conf.maxhfit <- v);
1475 boolp "crop hack"
1476 (fun () -> conf.crophack)
1477 (fun v -> conf.crophack <- v);
1479 boolp "throttle"
1480 (fun () -> conf.showall)
1481 (fun v -> conf.showall <- v);
1483 boolp "highlight links"
1484 (fun () -> conf.hlinks)
1485 (fun v -> conf.hlinks <- v);
1487 boolp "under info"
1488 (fun () -> conf.underinfo)
1489 (fun v -> conf.underinfo <- v);
1490 boolp "persistent bookmarks"
1491 (fun () -> conf.savebmarks)
1492 (fun v -> conf.savebmarks <- v);
1494 boolp "proportional display"
1495 (fun () -> conf.proportional)
1496 (fun v -> reinit conf.angle v);
1498 boolp "persistent location"
1499 (fun () -> conf.jumpback)
1500 (fun v -> conf.jumpback <- v);
1502 "", 0, Noaction;
1504 intp "vertical margin"
1505 (fun () -> conf.interpagespace)
1506 (fun n ->
1507 conf.interpagespace <- n;
1508 let pageno, py =
1509 match state.layout with
1510 | [] -> 0, 0
1511 | l :: _ ->
1512 l.pageno, l.pagey
1514 state.maxy <- calcheight ();
1515 let y = getpagey pageno in
1516 gotoy (y + py)
1519 intp "page bias"
1520 (fun () -> conf.pagebias)
1521 (fun v -> conf.pagebias <- v);
1523 intp "scroll step"
1524 (fun () -> conf.scrollstep)
1525 (fun n -> conf.scrollstep <- n);
1527 intp "auto scroll step"
1528 (fun () ->
1529 if state.ascrollstep > 0
1530 then state.ascrollstep
1531 else conf.autoscrollstep)
1532 (fun n ->
1533 if state.ascrollstep > 0
1534 then state.ascrollstep <- n
1535 else conf.autoscrollstep <- n);
1537 intp "zoom"
1538 (fun () -> truncate (conf.zoom *. 100.))
1539 (fun v -> setzoom ((float v) /. 100.));
1541 intp "rotation"
1542 (fun () -> conf.angle)
1543 (fun v -> reinit v conf.proportional);
1545 intp "scroll bar width"
1546 (fun () -> state.scrollw)
1547 (fun v ->
1548 state.scrollw <- v;
1549 conf.scrollbw <- v;
1550 reshape conf.winw conf.winh;
1553 intp "scroll handle height"
1554 (fun () -> conf.scrollh)
1555 (fun v -> conf.scrollh <- v;);
1557 intp "thumbnail width"
1558 (fun () -> conf.thumbw)
1559 (fun v ->
1560 conf.thumbw <- min 1920 v;
1561 match mode with
1562 | Birdseye beye ->
1563 leavebirdseye beye false;
1564 enterbirdseye ()
1565 | _ -> ()
1568 colorp "background color"
1569 (fun () -> conf.bgcolor)
1570 (fun v -> conf.bgcolor <- v);
1572 "", 0, Noaction;
1573 "Presentation mode", 0, Noaction;
1575 boolp "scrollbar visible"
1576 (fun () -> conf.scrollbarinpm)
1577 (fun v ->
1578 if v != conf.scrollbarinpm
1579 then (
1580 conf.scrollbarinpm <- v;
1581 if conf.presentation
1582 then (
1583 state.scrollw <- if v then conf.scrollbw else 0;
1584 reshape conf.winw conf.winh;
1589 "", 0, Noaction;
1590 "Pixmap Cache", 0, Noaction;
1592 intp "size (advisory)"
1593 (fun () -> conf.memlimit)
1594 (fun v -> conf.memlimit <- v);
1595 Printf.sprintf "%-24s %d" "used" state.memused, 1, Noaction;
1599 let tailer =
1600 let trailer =
1601 ("", 0, Noaction)
1602 :: ("Document", 0, Noaction)
1603 :: List.map (fun (_, s) -> (s, 1, Noaction)) state.docinfo
1605 if birdseye
1606 then trailer
1607 else (
1608 ("", 0, Noaction)
1609 :: (Printf.sprintf "Save these parameters as defaults at exit (%s)"
1610 (btos conf.bedefault),
1612 Action (
1613 fun active first qsearch pan ->
1614 conf.bedefault <- not conf.bedefault;
1615 Items (active, first, makeitems (), qsearch, pan, mode);
1617 ) :: trailer
1620 Array.of_list (items @ tailer)
1622 state.text <- "";
1623 state.mode <- Items (1, 0, makeitems (), "", 0, mode);
1624 Glut.postRedisplay ();
1627 let enterhelpmode () =
1628 state.mode <- Items (-1, 0, state.help, "", 0, state.mode);
1629 Glut.postRedisplay ();
1632 let quickbookmark ?title () =
1633 match state.layout with
1634 | [] -> ()
1635 | l :: _ ->
1636 let title =
1637 match title with
1638 | None ->
1639 let sec = Unix.gettimeofday () in
1640 let tm = Unix.localtime sec in
1641 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1642 (l.pageno+1)
1643 tm.Unix.tm_mday
1644 tm.Unix.tm_mon
1645 (tm.Unix.tm_year + 1900)
1646 tm.Unix.tm_hour
1647 tm.Unix.tm_min
1648 | Some title -> title
1650 state.bookmarks <-
1651 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
1652 :: state.bookmarks
1655 let doreshape w h =
1656 state.fullscreen <- None;
1657 Glut.reshapeWindow w h;
1660 let writeopen path password =
1661 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1662 writecmd state.csock "info";
1665 let opendoc path password =
1666 invalidate ();
1667 state.path <- path;
1668 state.password <- password;
1669 state.gen <- state.gen + 1;
1671 writeopen path password;
1672 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1673 wcmd "geometry" [`i state.w; `i conf.winh];
1676 let viewkeyboard key =
1677 let enttext te =
1678 let mode = state.mode in
1679 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
1680 state.text <- "";
1681 enttext ();
1682 Glut.postRedisplay ()
1684 let c = Char.chr key in
1685 match c with
1686 | '\027' | 'q' -> (* escape *)
1687 exit 0
1689 | '\008' -> (* backspace *)
1690 let y = getnav ~-1 in
1691 gotoy_and_clear_text y
1693 | 'o' ->
1694 enteroutlinemode ()
1696 | 'u' ->
1697 state.rects <- [];
1698 state.text <- "";
1699 Glut.postRedisplay ()
1701 | '/' | '?' ->
1702 let ondone isforw s =
1703 cbput state.hists.pat s;
1704 state.searchpattern <- s;
1705 search s isforw
1707 let s = String.create 1 in
1708 s.[0] <- c;
1709 enttext (s, "", Some (onhist state.hists.pat),
1710 textentry, ondone (c ='/'))
1712 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1713 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1714 setzoom (min 2.2 (conf.zoom +. incr))
1716 | '+' ->
1717 let ondone s =
1718 let n =
1719 try int_of_string s with exc ->
1720 state.text <- Printf.sprintf "bad integer `%s': %s"
1721 s (Printexc.to_string exc);
1722 max_int
1724 if n != max_int
1725 then (
1726 conf.pagebias <- n;
1727 state.text <- "page bias is now " ^ string_of_int n;
1730 enttext ("page bias", "", None, intentry, ondone)
1732 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1733 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1734 setzoom (max 0.01 (conf.zoom -. decr))
1736 | '-' ->
1737 let ondone msg = state.text <- msg in
1738 enttext (
1739 "option [acfhilpstvAPRSZ]", "", None,
1740 optentry state.mode, ondone
1743 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1744 setzoom 1.0
1746 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1747 let zoom = zoomforh conf.winw conf.winh state.scrollw in
1748 if zoom < 1.0
1749 then setzoom zoom
1751 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1752 togglebirdseye ()
1754 | '0' .. '9' ->
1755 let ondone s =
1756 let n =
1757 try int_of_string s with exc ->
1758 state.text <- Printf.sprintf "bad integer `%s': %s"
1759 s (Printexc.to_string exc);
1762 if n >= 0
1763 then (
1764 addnav ();
1765 cbput state.hists.pag (string_of_int n);
1766 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1769 let pageentry text key =
1770 match Char.unsafe_chr key with
1771 | 'g' -> TEdone text
1772 | _ -> intentry text key
1774 let text = "x" in text.[0] <- c;
1775 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
1777 | 'b' ->
1778 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
1779 reshape conf.winw conf.winh;
1781 | 'l' ->
1782 conf.hlinks <- not conf.hlinks;
1783 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1784 Glut.postRedisplay ()
1786 | 'a' ->
1787 if state.ascrollstep = 0
1788 then state.ascrollstep <- conf.autoscrollstep
1789 else (
1790 conf.autoscrollstep <- state.ascrollstep;
1791 state.ascrollstep <- 0;
1794 | 'P' ->
1795 conf.presentation <- not conf.presentation;
1796 if conf.presentation
1797 then (
1798 if not conf.scrollbarinpm
1799 then state.scrollw <- 0;
1801 else
1802 state.scrollw <- conf.scrollbw;
1804 showtext ' ' ("presentation mode " ^
1805 if conf.presentation then "on" else "off");
1806 state.anchor <- getanchor ();
1807 represent ()
1809 | 'f' ->
1810 begin match state.fullscreen with
1811 | None ->
1812 state.fullscreen <- Some (conf.winw, conf.winh);
1813 Glut.fullScreen ()
1814 | Some (w, h) ->
1815 state.fullscreen <- None;
1816 doreshape w h
1819 | 'g' ->
1820 gotoy_and_clear_text 0
1822 | 'n' ->
1823 search state.searchpattern true
1825 | 'p' | 'N' ->
1826 search state.searchpattern false
1828 | 't' ->
1829 begin match state.layout with
1830 | [] -> ()
1831 | l :: _ ->
1832 gotoy_and_clear_text (getpagey l.pageno)
1835 | ' ' ->
1836 begin match List.rev state.layout with
1837 | [] -> ()
1838 | l :: _ ->
1839 let pageno = min (l.pageno+1) (state.pagecount-1) in
1840 gotoy_and_clear_text (getpagey pageno)
1843 | '\127' -> (* delte *)
1844 begin match state.layout with
1845 | [] -> ()
1846 | l :: _ ->
1847 let pageno = max 0 (l.pageno-1) in
1848 gotoy_and_clear_text (getpagey pageno)
1851 | '=' ->
1852 let f (fn, _) l =
1853 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1855 let fn, ln = List.fold_left f (-1, -1) state.layout in
1856 let s =
1857 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1858 let percent =
1859 if maxy <= 0
1860 then 100.
1861 else (100. *. (float state.y /. float maxy)) in
1862 if fn = ln
1863 then
1864 Printf.sprintf "Page %d of %d %.2f%%"
1865 (fn+1) state.pagecount percent
1866 else
1867 Printf.sprintf
1868 "Pages %d-%d of %d %.2f%%"
1869 (fn+1) (ln+1) state.pagecount percent
1871 showtext ' ' s;
1873 | 'w' ->
1874 begin match state.layout with
1875 | [] -> ()
1876 | l :: _ ->
1877 doreshape (l.pagew + state.scrollw) l.pageh;
1878 Glut.postRedisplay ();
1881 | '\'' ->
1882 enterbookmarkmode ()
1884 | 'h' ->
1885 enterhelpmode ()
1887 | 'i' ->
1888 enterinfomode ()
1890 | 'm' ->
1891 let ondone s =
1892 match state.layout with
1893 | l :: _ ->
1894 state.bookmarks <-
1895 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
1896 :: state.bookmarks
1897 | _ -> ()
1899 enttext ("bookmark", "", None, textentry, ondone)
1901 | '~' ->
1902 quickbookmark ();
1903 showtext ' ' "Quick bookmark added";
1905 | 'z' ->
1906 begin match state.layout with
1907 | l :: _ ->
1908 let rect = getpdimrect l.pagedimno in
1909 let w, h =
1910 if conf.crophack
1911 then
1912 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1913 truncate (1.2 *. (rect.(3) -. rect.(0))))
1914 else
1915 (truncate (rect.(1) -. rect.(0)),
1916 truncate (rect.(3) -. rect.(0)))
1918 if w != 0 && h != 0
1919 then
1920 doreshape (w + state.scrollw) (h + conf.interpagespace)
1922 Glut.postRedisplay ();
1924 | [] -> ()
1927 | '<' | '>' ->
1928 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1930 | '[' | ']' ->
1931 state.colorscale <-
1932 max 0.0
1933 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1934 Glut.postRedisplay ()
1936 | 'k' ->
1937 begin match state.mode with
1938 | Birdseye beye -> upbirdseye beye
1939 | _ -> gotoy (clamp (-conf.scrollstep))
1942 | 'j' ->
1943 begin match state.mode with
1944 | Birdseye beye -> downbirdseye beye
1945 | _ -> gotoy (clamp conf.scrollstep)
1948 | 'r' ->
1949 state.anchor <- getanchor ();
1950 opendoc state.path state.password
1952 | _ ->
1953 vlog "huh? %d %c" key (Char.chr key);
1956 let textentrykeyboard key ((c, text, opthist, onkey, ondone), onleave) =
1957 let enttext te =
1958 state.mode <- Textentry (te, onleave);
1959 state.text <- "";
1960 enttext ();
1961 Glut.postRedisplay ()
1963 match Char.unsafe_chr key with
1964 | '\008' -> (* backspace *)
1965 let len = String.length text in
1966 if len = 0
1967 then (
1968 onleave Cancel;
1969 Glut.postRedisplay ();
1971 else (
1972 let s = String.sub text 0 (len - 1) in
1973 enttext (c, s, opthist, onkey, ondone)
1976 | '\r' | '\n' ->
1977 ondone text;
1978 onleave Confirm;
1979 Glut.postRedisplay ()
1981 | '\027' -> (* escape *)
1982 begin match opthist with
1983 | None -> ()
1984 | Some (_, onhistcancel) -> onhistcancel ()
1985 end;
1986 onleave Cancel;
1987 Glut.postRedisplay ()
1989 | _ ->
1990 begin match onkey text key with
1991 | TEdone text ->
1992 onleave Confirm;
1993 ondone text;
1994 Glut.postRedisplay ()
1996 | TEcont text ->
1997 enttext (c, text, opthist, onkey, ondone);
1999 | TEstop ->
2000 onleave Cancel;
2001 Glut.postRedisplay ()
2003 | TEswitch te ->
2004 state.mode <- Textentry (te, onleave);
2005 Glut.postRedisplay ()
2006 end;
2009 let birdseyekeyboard key ((_, _, pageno, _, _) as beye) =
2010 match key with
2011 | 27 -> (* escape *)
2012 leavebirdseye beye true
2014 | 12 -> (* ctrl-l *)
2015 let y, h = getpageyh pageno in
2016 let top = (conf.winh - h) / 2 in
2017 gotoy (max 0 (y - top))
2019 | 13 -> (* enter *)
2020 leavebirdseye beye false
2022 | _ ->
2023 viewkeyboard key
2026 let itemskeyboard key (active, first, items, qsearch, pan, oldmode) =
2027 let set active first qsearch =
2028 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2030 let search active pattern incr =
2031 let dosearch re =
2032 let rec loop n =
2033 if n = Array.length items || n = -1
2034 then None
2035 else
2036 let (s, _, _) = items.(n) in
2038 (try ignore (Str.search_forward re s 0); true
2039 with Not_found -> false)
2040 then Some n
2041 else loop (n + incr)
2043 loop active
2046 let re = Str.regexp_case_fold pattern in
2047 dosearch re
2048 with Failure s ->
2049 state.text <- s;
2050 None
2052 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2053 match key with
2054 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2055 let incr = if key = 18 then -1 else 1 in
2056 let active, first =
2057 match search (active + incr) qsearch incr with
2058 | None ->
2059 state.text <- qsearch ^ " [not found]";
2060 active, first
2061 | Some active ->
2062 state.text <- qsearch;
2063 active, firstof active
2065 set active first qsearch;
2066 Glut.postRedisplay ();
2068 | 8 -> (* backspace *)
2069 let len = String.length qsearch in
2070 if len = 0
2071 then ()
2072 else (
2073 if len = 1
2074 then (
2075 state.text <- "";
2076 set active first "";
2078 else
2079 let qsearch = String.sub qsearch 0 (len - 1) in
2080 let active, first =
2081 match search active qsearch ~-1 with
2082 | None ->
2083 state.text <- qsearch ^ " [not found]";
2084 active, first
2085 | Some active ->
2086 state.text <- qsearch;
2087 active, firstof active
2089 set active first qsearch
2091 Glut.postRedisplay ()
2093 | _ when key >= 32 && key < 127 ->
2094 let pattern = addchar qsearch (Char.chr key) in
2095 let active, first =
2096 match search active pattern 1 with
2097 | None ->
2098 state.text <- pattern ^ " [not found]";
2099 active, first
2100 | Some active ->
2101 state.text <- pattern;
2102 active, firstof active
2104 set active first pattern;
2105 Glut.postRedisplay ()
2107 | 27 -> (* escape *)
2108 state.text <- "";
2109 state.mode <- oldmode;
2110 Glut.postRedisplay ();
2112 | 13 -> (* enter *)
2113 if active >= 0 && active < Array.length items
2114 then (
2115 match items.(active) with
2116 | _, _, Action f ->
2117 state.mode <- f active first qsearch pan
2119 | _, _, Noaction ->
2120 state.text <- "";
2121 state.mode <- oldmode
2123 else (
2124 state.text <- "";
2125 state.mode <- oldmode
2127 Glut.postRedisplay ();
2129 | _ -> dolog "unknown key %d" key
2132 let outlinekeyboard key
2133 (allowdel, active, first, outlines, qsearch, pan, oldmode) =
2134 let narrow outlines pattern =
2135 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
2136 match reopt with
2137 | None -> None
2138 | Some re ->
2139 let rec fold accu n =
2140 if n = -1
2141 then accu
2142 else
2143 let (s, _, _) as o = outlines.(n) in
2144 let accu =
2145 if (try ignore (Str.search_forward re s 0); true
2146 with Not_found -> false)
2147 then (o :: accu)
2148 else accu
2150 fold accu (n-1)
2152 let matched = fold [] (Array.length outlines - 1) in
2153 if matched = [] then None else Some (Array.of_list matched)
2155 let search active pattern incr =
2156 let dosearch re =
2157 let rec loop n =
2158 if n = Array.length outlines || n = -1
2159 then None
2160 else
2161 let (s, _, _) = outlines.(n) in
2163 (try ignore (Str.search_forward re s 0); true
2164 with Not_found -> false)
2165 then Some n
2166 else loop (n + incr)
2168 loop active
2171 let re = Str.regexp_case_fold pattern in
2172 dosearch re
2173 with Failure s ->
2174 state.text <- s;
2175 None
2177 let firstof active = max 0 (active - maxoutlinerows () / 2) in
2178 match key with
2179 | 27 -> (* escape *)
2180 if String.length qsearch = 0
2181 then (
2182 state.text <- "";
2183 state.mode <- oldmode;
2184 Glut.postRedisplay ();
2186 else (
2187 state.text <- "";
2188 state.mode <- Outline (
2189 allowdel, active, first, outlines, "", pan, oldmode
2191 Glut.postRedisplay ();
2194 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2195 let incr = if key = 18 then -1 else 1 in
2196 let active, first =
2197 match search (active + incr) qsearch incr with
2198 | None ->
2199 state.text <- qsearch ^ " [not found]";
2200 active, first
2201 | Some active ->
2202 state.text <- qsearch;
2203 active, firstof active
2205 state.mode <- Outline (
2206 allowdel, active, first, outlines, qsearch, pan, oldmode
2208 Glut.postRedisplay ();
2210 | 8 -> (* backspace *)
2211 let len = String.length qsearch in
2212 if len = 0
2213 then ()
2214 else (
2215 if len = 1
2216 then (
2217 state.text <- "";
2218 state.mode <- Outline (
2219 allowdel, active, first, outlines, "", pan, oldmode
2222 else
2223 let qsearch = String.sub qsearch 0 (len - 1) in
2224 let active, first =
2225 match search active qsearch ~-1 with
2226 | None ->
2227 state.text <- qsearch ^ " [not found]";
2228 active, first
2229 | Some active ->
2230 state.text <- qsearch;
2231 active, firstof active
2233 state.mode <- Outline (
2234 allowdel, active, first, outlines, qsearch, pan, oldmode
2237 Glut.postRedisplay ()
2239 | 13 -> (* enter *)
2240 if active < Array.length outlines
2241 then (
2242 let (_, _, anchor) = outlines.(active) in
2243 addnav ();
2244 gotoanchor anchor;
2246 state.text <- "";
2247 if allowdel then state.bookmarks <- Array.to_list outlines;
2248 state.mode <- oldmode;
2249 Glut.postRedisplay ();
2251 | _ when key >= 32 && key < 127 ->
2252 let pattern = addchar qsearch (Char.chr key) in
2253 let active, first =
2254 match search active pattern 1 with
2255 | None ->
2256 state.text <- pattern ^ " [not found]";
2257 active, first
2258 | Some active ->
2259 state.text <- pattern;
2260 active, firstof active
2262 state.mode <- Outline (
2263 allowdel, active, first, outlines, pattern, pan, oldmode
2265 Glut.postRedisplay ()
2267 | 14 when not allowdel -> (* ctrl-n *)
2268 if String.length qsearch > 0
2269 then (
2270 let optoutlines = narrow outlines qsearch in
2271 begin match optoutlines with
2272 | None -> state.text <- "can't narrow"
2273 | Some outlines ->
2274 state.mode <- Outline (
2275 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2277 match state.outlines with
2278 | Olist _ -> ()
2279 | Oarray a ->
2280 state.outlines <- Onarrow (qsearch, outlines, a)
2281 | Onarrow (_, _, b) ->
2282 state.outlines <- Onarrow (qsearch, outlines, b)
2283 end;
2285 Glut.postRedisplay ()
2287 | 21 when not allowdel -> (* ctrl-u *)
2288 let outline =
2289 match state.outlines with
2290 | Oarray a -> a
2291 | Olist l ->
2292 let a = Array.of_list (List.rev l) in
2293 state.outlines <- Oarray a;
2295 | Onarrow (_, _, b) ->
2296 state.outlines <- Oarray b;
2297 state.text <- "";
2300 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan, oldmode);
2301 Glut.postRedisplay ()
2303 | 12 -> (* ctrl-l *)
2304 state.mode <- Outline
2305 (allowdel, active, firstof active, outlines, qsearch, pan, oldmode);
2306 Glut.postRedisplay ()
2308 | 127 when allowdel -> (* delete *)
2309 let len = Array.length outlines - 1 in
2310 if len = 0
2311 then (
2312 state.mode <- View;
2313 state.bookmarks <- [];
2315 else (
2316 let bookmarks = Array.init len
2317 (fun i ->
2318 let i = if i >= active then i + 1 else i in
2319 outlines.(i)
2322 state.mode <-
2323 Outline (
2324 allowdel,
2325 min active (len-1),
2326 min first (len-1),
2327 bookmarks, qsearch,
2329 oldmode
2332 Glut.postRedisplay ()
2334 | _ -> dolog "unknown key %d" key
2337 let keyboard ~key ~x ~y =
2338 ignore x;
2339 ignore y;
2340 if key = 7 (* ctrl-g *)
2341 then
2342 wcmd "interrupt" []
2343 else
2344 match state.mode with
2345 | Outline outline -> outlinekeyboard key outline
2346 | Textentry textentry -> textentrykeyboard key textentry
2347 | Birdseye birdseye -> birdseyekeyboard key birdseye
2348 | View -> viewkeyboard key
2349 | Items items -> itemskeyboard key items
2352 let birdseyespecial key ((conf, leftx, _, hooverpageno, anchor) as beye) =
2353 match key with
2354 | Glut.KEY_UP -> upbirdseye beye
2355 | Glut.KEY_DOWN -> downbirdseye beye
2357 | Glut.KEY_PAGE_UP ->
2358 begin match state.layout with
2359 | l :: _ ->
2360 if l.pagey != 0
2361 then (
2362 state.mode <- Birdseye (
2363 conf, leftx, l.pageno, hooverpageno, anchor
2365 gotopage1 l.pageno 0;
2367 else (
2368 let layout = layout (state.y-conf.winh) conf.winh in
2369 match layout with
2370 | [] -> gotoy (clamp (-conf.winh))
2371 | l :: _ ->
2372 state.mode <- Birdseye (
2373 conf, leftx, l.pageno, hooverpageno, anchor
2375 gotopage1 l.pageno 0
2378 | [] -> gotoy (clamp (-conf.winh))
2379 end;
2381 | Glut.KEY_PAGE_DOWN ->
2382 begin match List.rev state.layout with
2383 | l :: _ ->
2384 let layout = layout (state.y + conf.winh) conf.winh in
2385 begin match layout with
2386 | [] ->
2387 let incr = l.pageh - l.pagevh in
2388 if incr = 0
2389 then (
2390 state.mode <-
2391 Birdseye (
2392 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2394 Glut.postRedisplay ();
2396 else gotoy (clamp (incr + conf.interpagespace*2));
2398 | l :: _ ->
2399 state.mode <-
2400 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2401 gotopage1 l.pageno 0;
2404 | [] -> gotoy (clamp conf.winh)
2405 end;
2407 | Glut.KEY_HOME ->
2408 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2409 gotopage1 0 0
2411 | Glut.KEY_END ->
2412 let pageno = state.pagecount - 1 in
2413 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2414 if not (pagevisible state.layout pageno)
2415 then
2416 let h =
2417 match List.rev state.pdims with
2418 | [] -> conf.winh
2419 | (_, _, h, _) :: _ -> h
2421 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2422 else Glut.postRedisplay ();
2423 | _ -> ()
2426 let setautoscrollspeed goingdown =
2427 let incr = max 1 (state.ascrollstep / 2) in
2428 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2429 state.ascrollstep <- astep;
2432 let special ~key ~x ~y =
2433 ignore x;
2434 ignore y;
2435 match state.mode with
2436 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2437 togglebirdseye ()
2439 | Birdseye vals ->
2440 birdseyespecial key vals
2442 | View when key = Glut.KEY_F1 ->
2443 enterhelpmode ()
2445 | View ->
2446 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2447 then setautoscrollspeed (key = Glut.KEY_DOWN)
2448 else
2449 let y =
2450 match key with
2451 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2452 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2453 | Glut.KEY_DOWN -> clamp conf.scrollstep
2454 | Glut.KEY_PAGE_UP ->
2455 if Glut.getModifiers () land Glut.active_ctrl != 0
2456 then
2457 match state.layout with
2458 | [] -> state.y
2459 | l :: _ -> state.y - l.pagey
2460 else
2461 clamp (-conf.winh)
2462 | Glut.KEY_PAGE_DOWN ->
2463 if Glut.getModifiers () land Glut.active_ctrl != 0
2464 then
2465 match List.rev state.layout with
2466 | [] -> state.y
2467 | l :: _ -> getpagey l.pageno
2468 else
2469 clamp conf.winh
2470 | Glut.KEY_HOME -> addnav (); 0
2471 | Glut.KEY_END ->
2472 addnav ();
2473 state.maxy - (if conf.maxhfit then conf.winh else 0)
2475 | (Glut.KEY_RIGHT | Glut.KEY_LEFT) when
2476 Glut.getModifiers () land Glut.active_alt != 0 ->
2477 getnav (if key = Glut.KEY_LEFT then 1 else -1)
2479 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2480 state.x <- state.x - 10;
2481 state.y
2482 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2483 state.x <- state.x + 10;
2484 state.y
2486 | _ -> state.y
2488 gotoy_and_clear_text y
2490 | Textentry
2491 ((c, _, (Some (action, _) as onhist), onkey, ondone), mode) ->
2492 let s =
2493 match key with
2494 | Glut.KEY_UP -> action HCprev
2495 | Glut.KEY_DOWN -> action HCnext
2496 | Glut.KEY_HOME -> action HCfirst
2497 | Glut.KEY_END -> action HClast
2498 | _ -> state.text
2500 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2501 Glut.postRedisplay ()
2503 | Textentry _ -> ()
2505 | Items (active, first, items, qsearch, pan, oldmode) ->
2506 let maxrows = maxoutlinerows () in
2507 let itemcount = Array.length items in
2508 let hasaction = function
2509 | (_, _, Noaction) -> false
2510 | _ -> true
2512 let find start incr =
2513 let rec find i =
2514 if i = -1 || i = itemcount
2515 then -1
2516 else (
2517 if hasaction items.(i)
2518 then i
2519 else find (i + incr)
2522 find start
2524 let set active first =
2525 let first = max 0 (min first (itemcount - maxrows)) in
2526 state.mode <- Items (active, first, items, qsearch, pan, oldmode)
2528 let navigate incr =
2529 let isvisible first n = n >= first && n - first <= maxrows in
2530 let active, first =
2531 let incr1 = if incr > 0 then 1 else -1 in
2532 if isvisible first active
2533 then
2534 let next =
2535 let next = active + incr in
2536 let next =
2537 if next < 0 || next >= itemcount
2538 then -1
2539 else find next incr1
2541 if next = -1 || abs (active - next) > maxrows
2542 then -1
2543 else next
2545 if next = -1
2546 then
2547 let first = first + incr in
2548 let first = max 0 (min first (itemcount - 1)) in
2549 let next =
2550 let next = active + incr in
2551 let next = max 0 (min next (itemcount - 1)) in
2552 find next ~-incr1
2554 let active = if next = -1 then active else next in
2555 active, first
2556 else
2557 let first = min next first in
2558 next, first
2559 else
2560 let first = first + incr in
2561 let first = max 0 (min first (itemcount - 1)) in
2562 let active =
2563 let next = active + incr in
2564 let next = max 0 (min next (itemcount - 1)) in
2565 let next = find next incr1 in
2566 if next = -1 || abs (active - first) > maxrows
2567 then active
2568 else next
2570 active, first
2572 set active first;
2573 Glut.postRedisplay ()
2575 begin match key with
2576 | Glut.KEY_UP -> navigate ~-1
2577 | Glut.KEY_DOWN -> navigate 1
2578 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2579 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2581 | Glut.KEY_RIGHT ->
2582 state.mode <- Items (
2583 active, first, items, qsearch, min 0 (pan - 1), oldmode
2585 Glut.postRedisplay ()
2587 | Glut.KEY_LEFT ->
2588 state.mode <- Items (
2589 active, first, items, qsearch, min 0 (pan + 1), oldmode
2591 Glut.postRedisplay ()
2593 | Glut.KEY_HOME ->
2594 let active = find 0 1 in
2595 set active 0;
2596 Glut.postRedisplay ()
2598 | Glut.KEY_END ->
2599 let first = max 0 (itemcount - maxrows) in
2600 let active = find (itemcount - 1) ~-1 in
2601 set active first;
2602 Glut.postRedisplay ()
2604 | _ -> ()
2605 end;
2607 | Outline (allowdel, active, first, outlines, qsearch, pan, oldmode) ->
2608 let maxrows = maxoutlinerows () in
2609 let calcfirst first active =
2610 if active > first
2611 then
2612 let rows = active - first in
2613 if rows > maxrows then active - maxrows else first
2614 else active
2616 let navigate incr =
2617 let active = active + incr in
2618 let active = max 0 (min active (Array.length outlines - 1)) in
2619 let first = calcfirst first active in
2620 state.mode <- Outline (
2621 allowdel, active, first, outlines, qsearch, pan, oldmode
2623 Glut.postRedisplay ()
2625 let updownlevel incr =
2626 let len = Array.length outlines in
2627 let (_, curlevel, _) = outlines.(active) in
2628 let rec flow i =
2629 if i = len then i-1 else if i = -1 then 0 else
2630 let (_, l, _) = outlines.(i) in
2631 if l != curlevel then i else flow (i+incr)
2633 let active = flow active in
2634 let first = calcfirst first active in
2635 state.mode <- Outline (
2636 allowdel, active, first, outlines, qsearch, pan, oldmode
2638 Glut.postRedisplay ()
2640 match key with
2641 | Glut.KEY_UP -> navigate ~-1
2642 | Glut.KEY_DOWN -> navigate 1
2643 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2644 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2646 | Glut.KEY_RIGHT ->
2647 if Glut.getModifiers () land Glut.active_ctrl != 0
2648 then (
2649 state.mode <- Outline (
2650 allowdel, active, first, outlines,
2651 qsearch, min 0 (pan + 1), oldmode
2653 Glut.postRedisplay ();
2655 else (
2656 if not allowdel
2657 then updownlevel 1
2660 | Glut.KEY_LEFT ->
2661 if Glut.getModifiers () land Glut.active_ctrl != 0
2662 then (
2663 state.mode <- Outline (
2664 allowdel, active, first, outlines, qsearch, pan - 1, oldmode
2666 Glut.postRedisplay ();
2668 else (
2669 if not allowdel
2670 then updownlevel ~-1
2673 | Glut.KEY_HOME ->
2674 state.mode <- Outline (
2675 allowdel, 0, 0, outlines, qsearch, pan, oldmode
2677 Glut.postRedisplay ()
2679 | Glut.KEY_END ->
2680 let active = Array.length outlines - 1 in
2681 let first = max 0 (active - maxrows) in
2682 state.mode <- Outline (
2683 allowdel, active, first, outlines, qsearch, pan, oldmode
2685 Glut.postRedisplay ()
2687 | _ -> ()
2690 let drawplaceholder l =
2691 let margin = state.x + (conf.winw - (state.w + state.scrollw)) / 2 in
2692 GlDraw.rect
2693 (float l.pagex, float l.pagedispy)
2694 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2696 let x = if margin < 0 then -margin else l.pagex
2697 and y = l.pagedispy + 13 in
2698 GlDraw.color (0.0, 0.0, 0.0);
2699 drawstring 13 x y ("Loading " ^ string_of_int (l.pageno + 1))
2702 let now () = Unix.gettimeofday ();;
2704 let drawpage l =
2705 let color =
2706 match state.mode with
2707 | Textentry _ -> scalecolor 0.4
2708 | View | Outline _ | Items _ -> scalecolor 1.0
2709 | Birdseye (_, _, pageno, hooverpageno, _) ->
2710 if l.pageno = hooverpageno
2711 then scalecolor 0.9
2712 else (
2713 if l.pageno = pageno
2714 then scalecolor 1.0
2715 else scalecolor 0.8
2718 GlDraw.color color;
2719 begin match getopaque l.pageno with
2720 | Some (opaque, _) when validopaque opaque ->
2721 let a = now () in
2722 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2723 opaque;
2724 let b = now () in
2725 let d = b-.a in
2726 vlog "draw %d %f sec" l.pageno d;
2728 | _ ->
2729 drawplaceholder l;
2730 end;
2733 let scrollph y =
2734 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2735 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2736 let sh = float conf.winh /. sh in
2737 let sh = max sh (float conf.scrollh) in
2739 let percent =
2740 if state.y = state.maxy
2741 then 1.0
2742 else float y /. float maxy
2744 let position = (float conf.winh -. sh) *. percent in
2746 let position =
2747 if position +. sh > float conf.winh
2748 then float conf.winh -. sh
2749 else position
2751 position, sh;
2754 let scrollindicator () =
2755 GlDraw.color (0.64 , 0.64, 0.64);
2756 GlDraw.rect
2757 (float (conf.winw - state.scrollw), 0.)
2758 (float conf.winw, float conf.winh)
2760 GlDraw.color (0.0, 0.0, 0.0);
2762 let position, sh = scrollph state.y in
2763 GlDraw.rect
2764 (float (conf.winw - state.scrollw), position)
2765 (float conf.winw, position +. sh)
2769 let showsel margin =
2770 match state.mstate with
2771 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2774 | Msel ((x0, y0), (x1, y1)) ->
2775 let rec loop = function
2776 | l :: ls ->
2777 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2778 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2779 then
2780 match getopaque l.pageno with
2781 | Some (opaque, _) when validopaque opaque ->
2782 let oy = -l.pagey + l.pagedispy in
2783 seltext opaque
2784 (x0 - margin - state.x, y0,
2785 x1 - margin - state.x, y1) oy;
2787 | _ -> ()
2788 else loop ls
2789 | [] -> ()
2791 loop state.layout
2794 let showrects () =
2795 let panx = float state.x in
2796 Gl.enable `blend;
2797 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2798 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2799 List.iter
2800 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2801 List.iter (fun l ->
2802 if l.pageno = pageno
2803 then (
2804 let d = float (l.pagedispy - l.pagey) in
2805 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2806 GlDraw.begins `quads;
2808 GlDraw.vertex2 (x0+.panx, y0+.d);
2809 GlDraw.vertex2 (x1+.panx, y1+.d);
2810 GlDraw.vertex2 (x2+.panx, y2+.d);
2811 GlDraw.vertex2 (x3+.panx, y3+.d);
2813 GlDraw.ends ();
2815 ) state.layout
2816 ) state.rects
2818 Gl.disable `blend;
2821 let showstrings active first pan strings =
2822 Gl.enable `blend;
2823 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2824 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2825 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2826 GlDraw.color (1., 1., 1.);
2827 Gl.enable `texture_2d;
2829 let rec loop row =
2830 if row = Array.length strings || (row - first) * 16 > conf.winh
2831 then ()
2832 else (
2833 let (s, level, _) = strings.(row) in
2834 let y = (row - first) * 16 in
2835 let x = 5 + 15*(max 0 (level+pan)) in
2836 if row = active
2837 then (
2838 Gl.disable `texture_2d;
2839 GlDraw.polygon_mode `both `line;
2840 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2841 GlDraw.rect (0., float (y + 1))
2842 (float (conf.winw - 1), float (y + 18));
2843 GlDraw.polygon_mode `both `fill;
2844 GlDraw.color (1., 1., 1.);
2845 Gl.enable `texture_2d;
2847 let draw_string s =
2848 let l = String.length s in
2849 if pan < 0
2850 then (
2851 let pan = pan * 2 in
2852 let pos = pan + level in
2853 let left = l + pos in
2854 if left > 0
2855 then
2856 let s =
2857 if left > l
2858 then s
2859 else String.sub s (-pos) left
2861 drawstring1 14 x (y + 16) s
2863 else
2864 drawstring1 14 (x + pan*15) (y + 16) s
2866 draw_string s;
2867 loop (row+1)
2870 loop first;
2871 Gl.disable `blend;
2872 Gl.disable `texture_2d;
2875 let showoutline (_, active, first, outlines, _, pan, _) =
2876 showstrings active first pan outlines;
2879 let showitems (active, first, items, _, pan, _) =
2880 showstrings active first pan items;
2883 let display () =
2884 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2885 GlDraw.viewport margin 0 state.w conf.winh;
2886 pagematrix ();
2887 GlClear.color (scalecolor2 conf.bgcolor);
2888 GlClear.clear [`color];
2889 if conf.zoom > 1.0
2890 then (
2891 Gl.enable `scissor_test;
2892 GlMisc.scissor 0 0 (conf.winw - state.scrollw) conf.winh;
2894 List.iter drawpage state.layout;
2895 if conf.zoom > 1.0
2896 then
2897 Gl.disable `scissor_test
2899 if state.x != 0
2900 then (
2901 let x = -.float state.x in
2902 GlMat.translate ~x ();
2904 showrects ();
2905 showsel margin;
2906 GlDraw.viewport 0 0 conf.winw conf.winh;
2907 winmatrix ();
2908 scrollindicator ();
2909 begin match state.mode with
2910 | Items items -> showitems items
2911 | Outline outline -> showoutline outline
2912 | _ -> ()
2913 end;
2914 enttext ();
2915 Glut.swapBuffers ();
2918 let getunder x y =
2919 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
2920 let x = x - margin - state.x in
2921 let rec f = function
2922 | l :: rest ->
2923 begin match getopaque l.pageno with
2924 | Some (opaque, _) when validopaque opaque ->
2925 let y = y - l.pagedispy in
2926 if y > 0
2927 then
2928 let y = l.pagey + y in
2929 let x = x - l.pagex in
2930 match whatsunder opaque x y with
2931 | Unone -> f rest
2932 | under -> under
2933 else
2934 f rest
2935 | _ ->
2936 f rest
2938 | [] -> Unone
2940 f state.layout
2943 let viewmouse button bstate x y =
2944 match button with
2945 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2946 if Glut.getModifiers () land Glut.active_ctrl != 0
2947 then (
2948 match state.mstate with
2949 | Mzoom (oldn, i) ->
2950 if oldn = n
2951 then (
2952 if i = 2
2953 then
2954 let incr =
2955 match n with
2956 | 4 ->
2957 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2958 | _ ->
2959 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2961 let zoom = conf.zoom +. incr in
2962 setzoom zoom;
2963 state.mstate <- Mzoom (n, 0);
2964 else
2965 state.mstate <- Mzoom (n, i+1);
2967 else state.mstate <- Mzoom (n, 0)
2969 | _ -> state.mstate <- Mzoom (n, 0)
2971 else (
2972 if state.ascrollstep > 0
2973 then
2974 setautoscrollspeed (n=4)
2975 else
2976 let incr =
2977 if n = 3
2978 then -conf.scrollstep
2979 else conf.scrollstep
2981 let incr = incr * 2 in
2982 let y = clamp incr in
2983 gotoy_and_clear_text y
2986 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2987 if bstate = Glut.DOWN
2988 then (
2989 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2990 state.mstate <- Mpan (x, y)
2992 else
2993 state.mstate <- Mnone
2995 | Glut.LEFT_BUTTON when x > conf.winw - state.scrollw ->
2996 if bstate = Glut.DOWN
2997 then
2998 let position, sh = scrollph state.y in
2999 if y > truncate position && y < truncate (position +. sh)
3000 then
3001 state.mstate <- Mscroll
3002 else
3003 let percent = float y /. float conf.winh in
3004 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
3005 gotoy desty;
3006 state.mstate <- Mscroll
3007 else
3008 state.mstate <- Mnone
3010 | Glut.LEFT_BUTTON ->
3011 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
3012 begin match dest with
3013 | Ulinkgoto (pageno, top) ->
3014 if pageno >= 0
3015 then (
3016 addnav ();
3017 gotopage1 pageno top;
3020 | Ulinkuri s ->
3021 print_endline s
3023 | Unone when bstate = Glut.DOWN ->
3024 Glut.setCursor Glut.CURSOR_CROSSHAIR;
3025 state.mstate <- Mpan (x, y);
3027 | Unone | Utext _ ->
3028 if bstate = Glut.DOWN
3029 then (
3030 if conf.angle mod 360 = 0
3031 then (
3032 state.mstate <- Msel ((x, y), (x, y));
3033 Glut.postRedisplay ()
3036 else (
3037 match state.mstate with
3038 | Mnone -> ()
3040 | Mzoom _ | Mscroll ->
3041 state.mstate <- Mnone
3043 | Mpan _ ->
3044 Glut.setCursor Glut.CURSOR_INHERIT;
3045 state.mstate <- Mnone
3047 | Msel ((_, y0), (_, y1)) ->
3048 let f l =
3049 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
3050 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
3051 then
3052 match getopaque l.pageno with
3053 | Some (opaque, _) when validopaque opaque ->
3054 copysel opaque
3055 | _ -> ()
3057 List.iter f state.layout;
3058 copysel ""; (* ugly *)
3059 Glut.setCursor Glut.CURSOR_INHERIT;
3060 state.mstate <- Mnone;
3064 | _ -> ()
3067 let birdseyemouse button bstate x y
3068 (conf, leftx, _, hooverpageno, anchor) =
3069 match button with
3070 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
3071 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3072 let rec loop = function
3073 | [] -> ()
3074 | l :: rest ->
3075 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3076 && x > margin && x < margin + l.pagew
3077 then (
3078 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
3080 else loop rest
3082 loop state.layout
3083 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
3084 | _ -> ()
3087 let mouse bstate button x y =
3088 match state.mode with
3089 | View -> viewmouse button bstate x y
3090 | Birdseye beye -> birdseyemouse button bstate x y beye
3091 | Textentry _ | Outline _ | Items _ -> ()
3094 let mouse ~button ~state ~x ~y = mouse state button x y;;
3096 let motion ~x ~y =
3097 match state.mode with
3098 | Outline _ -> ()
3099 | _ ->
3100 match state.mstate with
3101 | Mzoom _ | Mnone -> ()
3103 | Mpan (x0, y0) ->
3104 let dx = x - x0
3105 and dy = y0 - y in
3106 state.mstate <- Mpan (x, y);
3107 if conf.zoom > 1.0 then state.x <- state.x + dx;
3108 let y = clamp dy in
3109 gotoy_and_clear_text y
3111 | Msel (a, _) ->
3112 state.mstate <- Msel (a, (x, y));
3113 Glut.postRedisplay ()
3115 | Mscroll ->
3116 let y = min conf.winh (max 0 y) in
3117 let percent = float y /. float conf.winh in
3118 let y = truncate (float (state.maxy - conf.winh) *. percent) in
3119 gotoy_and_clear_text y
3122 let pmotion ~x ~y =
3123 match state.mode with
3124 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
3125 let margin = (conf.winw - (state.w + state.scrollw)) / 2 in
3126 let rec loop = function
3127 | [] ->
3128 if hooverpageno != -1
3129 then (
3130 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
3131 Glut.postRedisplay ();
3133 | l :: rest ->
3134 if y > l.pagedispy && y < l.pagedispy + l.pagevh
3135 && x > margin && x < margin + l.pagew
3136 then (
3137 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
3138 Glut.postRedisplay ();
3140 else loop rest
3142 loop state.layout
3144 | Outline _ | Items _ | Textentry _ -> ()
3145 | View ->
3146 match state.mstate with
3147 | Mnone ->
3148 begin match getunder x y with
3149 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
3150 | Ulinkuri uri ->
3151 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
3152 Glut.setCursor Glut.CURSOR_INFO
3153 | Ulinkgoto (page, _) ->
3154 if conf.underinfo
3155 then showtext 'p' ("age: " ^ string_of_int page);
3156 Glut.setCursor Glut.CURSOR_INFO
3157 | Utext s ->
3158 if conf.underinfo then showtext 'f' ("ont: " ^ s);
3159 Glut.setCursor Glut.CURSOR_TEXT
3162 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
3167 module State =
3168 struct
3169 open Parser
3171 let home =
3173 match Sys.os_type with
3174 | "Win32" -> Sys.getenv "HOMEPATH"
3175 | _ -> Sys.getenv "HOME"
3176 with exn ->
3177 prerr_endline
3178 ("Can not determine home directory location: " ^
3179 Printexc.to_string exn);
3183 let config_of c attrs =
3184 let apply c k v =
3186 match k with
3187 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
3188 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
3189 | "case-insensitive-search" -> { c with icase = bool_of_string v }
3190 | "preload" -> { c with preload = bool_of_string v }
3191 | "page-bias" -> { c with pagebias = int_of_string v }
3192 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
3193 | "auto-scroll-step" ->
3194 { c with autoscrollstep = max 0 (int_of_string v) }
3195 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
3196 | "crop-hack" -> { c with crophack = bool_of_string v }
3197 | "throttle" -> { c with showall = bool_of_string v }
3198 | "highlight-links" -> { c with hlinks = bool_of_string v }
3199 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
3200 | "vertical-margin" ->
3201 { c with interpagespace = max 0 (int_of_string v) }
3202 | "zoom" ->
3203 let zoom = float_of_string v /. 100. in
3204 let zoom = max 0.01 (min 2.2 zoom) in
3205 { c with zoom = zoom }
3206 | "presentation" -> { c with presentation = bool_of_string v }
3207 | "rotation-angle" -> { c with angle = int_of_string v }
3208 | "width" -> { c with winw = max 20 (int_of_string v) }
3209 | "height" -> { c with winh = max 20 (int_of_string v) }
3210 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
3211 | "proportional-display" -> { c with proportional = bool_of_string v }
3212 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
3213 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
3214 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
3215 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
3216 | "persistent-location" -> { c with jumpback = bool_of_string v }
3217 | "background-color" -> { c with bgcolor = color_of_string v }
3218 | "scrollbar-in-presentation" ->
3219 { c with scrollbarinpm = bool_of_string v }
3220 | "ui-font" -> { c with uifont = v }
3221 | _ -> c
3222 with exn ->
3223 prerr_endline ("Error processing attribute (`" ^
3224 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
3227 let rec fold c = function
3228 | [] -> c
3229 | (k, v) :: rest ->
3230 let c = apply c k v in
3231 fold c rest
3233 fold c attrs;
3236 let fromstring f pos n v d =
3237 try f v
3238 with exn ->
3239 dolog "Error processing attribute (%S=%S) at %d\n%s"
3240 n v pos (Printexc.to_string exn)
3245 let bookmark_of attrs =
3246 let rec fold title page rely = function
3247 | ("title", v) :: rest -> fold v page rely rest
3248 | ("page", v) :: rest -> fold title v rely rest
3249 | ("rely", v) :: rest -> fold title page v rest
3250 | _ :: rest -> fold title page rely rest
3251 | [] -> title, page, rely
3253 fold "invalid" "0" "0" attrs
3256 let doc_of attrs =
3257 let rec fold path page rely pan = function
3258 | ("path", v) :: rest -> fold v page rely pan rest
3259 | ("page", v) :: rest -> fold path v rely pan rest
3260 | ("rely", v) :: rest -> fold path page v pan rest
3261 | ("pan", v) :: rest -> fold path page rely v rest
3262 | _ :: rest -> fold path page rely pan rest
3263 | [] -> path, page, rely, pan
3265 fold "" "0" "0" "0" attrs
3268 let setconf dst src =
3269 dst.scrollbw <- src.scrollbw;
3270 dst.scrollh <- src.scrollh;
3271 dst.icase <- src.icase;
3272 dst.preload <- src.preload;
3273 dst.pagebias <- src.pagebias;
3274 dst.verbose <- src.verbose;
3275 dst.scrollstep <- src.scrollstep;
3276 dst.maxhfit <- src.maxhfit;
3277 dst.crophack <- src.crophack;
3278 dst.autoscrollstep <- src.autoscrollstep;
3279 dst.showall <- src.showall;
3280 dst.hlinks <- src.hlinks;
3281 dst.underinfo <- src.underinfo;
3282 dst.interpagespace <- src.interpagespace;
3283 dst.zoom <- src.zoom;
3284 dst.presentation <- src.presentation;
3285 dst.angle <- src.angle;
3286 dst.winw <- src.winw;
3287 dst.winh <- src.winh;
3288 dst.savebmarks <- src.savebmarks;
3289 dst.memlimit <- src.memlimit;
3290 dst.proportional <- src.proportional;
3291 dst.texcount <- src.texcount;
3292 dst.sliceheight <- src.sliceheight;
3293 dst.thumbw <- src.thumbw;
3294 dst.jumpback <- src.jumpback;
3295 dst.bgcolor <- src.bgcolor;
3296 dst.scrollbarinpm <- src.scrollbarinpm;
3297 dst.uifont <- src.uifont;
3300 let unent s =
3301 let l = String.length s in
3302 let b = Buffer.create l in
3303 unent b s 0 l;
3304 Buffer.contents b;
3307 let get s =
3308 let h = Hashtbl.create 10 in
3309 let dc = { defconf with angle = defconf.angle } in
3310 let rec toplevel v t spos _ =
3311 match t with
3312 | Vdata | Vcdata | Vend -> v
3313 | Vopen ("llppconfig", _, closed) ->
3314 if closed
3315 then v
3316 else { v with f = llppconfig }
3317 | Vopen _ ->
3318 error "unexpected subelement at top level" s spos
3319 | Vclose _ -> error "unexpected close at top level" s spos
3321 and llppconfig v t spos _ =
3322 match t with
3323 | Vdata | Vcdata | Vend -> v
3324 | Vopen ("defaults", attrs, closed) ->
3325 let c = config_of dc attrs in
3326 setconf dc c;
3327 if closed
3328 then v
3329 else { v with f = skip "defaults" (fun () -> v) }
3331 | Vopen ("doc", attrs, closed) ->
3332 let pathent, spage, srely, span = doc_of attrs in
3333 let path = unent pathent
3334 and pageno = fromstring int_of_string spos "page" spage 0
3335 and rely = fromstring float_of_string spos "rely" srely 0.0
3336 and pan = fromstring int_of_string spos "pan" span 0 in
3337 let c = config_of dc attrs in
3338 let anchor = (pageno, rely) in
3339 if closed
3340 then (Hashtbl.add h path (c, [], pan, anchor); v)
3341 else { v with f = doc path pan anchor c [] }
3343 | Vopen _ ->
3344 error "unexpected subelement in llppconfig" s spos
3346 | Vclose "llppconfig" -> { v with f = toplevel }
3347 | Vclose _ -> error "unexpected close in llppconfig" s spos
3349 and doc path pan anchor c bookmarks v t spos _ =
3350 match t with
3351 | Vdata | Vcdata -> v
3352 | Vend -> error "unexpected end of input in doc" s spos
3353 | Vopen ("bookmarks", _, closed) ->
3354 if closed
3355 then v
3356 else { v with f = pbookmarks path pan anchor c bookmarks }
3358 | Vopen (_, _, _) ->
3359 error "unexpected subelement in doc" s spos
3361 | Vclose "doc" ->
3362 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
3363 { v with f = llppconfig }
3365 | Vclose _ -> error "unexpected close in doc" s spos
3367 and pbookmarks path pan anchor c bookmarks v t spos _ =
3368 match t with
3369 | Vdata | Vcdata -> v
3370 | Vend -> error "unexpected end of input in bookmarks" s spos
3371 | Vopen ("item", attrs, closed) ->
3372 let titleent, spage, srely = bookmark_of attrs in
3373 let page = fromstring int_of_string spos "page" spage 0
3374 and rely = fromstring float_of_string spos "rely" srely 0.0 in
3375 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
3376 if closed
3377 then { v with f = pbookmarks path pan anchor c bookmarks }
3378 else
3379 let f () = v in
3380 { v with f = skip "item" f }
3382 | Vopen _ ->
3383 error "unexpected subelement in bookmarks" s spos
3385 | Vclose "bookmarks" ->
3386 { v with f = doc path pan anchor c bookmarks }
3388 | Vclose _ -> error "unexpected close in bookmarks" s spos
3390 and skip tag f v t spos _ =
3391 match t with
3392 | Vdata | Vcdata -> v
3393 | Vend ->
3394 error ("unexpected end of input in skipped " ^ tag) s spos
3395 | Vopen (tag', _, closed) ->
3396 if closed
3397 then v
3398 else
3399 let f' () = { v with f = skip tag f } in
3400 { v with f = skip tag' f' }
3401 | Vclose ctag ->
3402 if tag = ctag
3403 then f ()
3404 else error ("unexpected close in skipped " ^ tag) s spos
3407 parse { f = toplevel; accu = () } s;
3408 h, dc;
3411 let do_load f ic =
3413 let len = in_channel_length ic in
3414 let s = String.create len in
3415 really_input ic s 0 len;
3416 f s;
3417 with
3418 | Parse_error (msg, s, pos) ->
3419 let subs = subs s pos in
3420 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
3421 failwith ("parse error: " ^ s)
3423 | exn ->
3424 failwith ("config load error: " ^ Printexc.to_string exn)
3427 let path =
3428 let dir =
3430 let dir = Filename.concat home ".config" in
3431 if Sys.is_directory dir then dir else home
3432 with _ -> home
3434 Filename.concat dir "llpp.conf"
3437 let load1 f =
3438 if Sys.file_exists path
3439 then
3440 match
3441 (try Some (open_in_bin path)
3442 with exn ->
3443 prerr_endline
3444 ("Error opening configuation file `" ^ path ^ "': " ^
3445 Printexc.to_string exn);
3446 None
3448 with
3449 | Some ic ->
3450 begin try
3451 f (do_load get ic)
3452 with exn ->
3453 prerr_endline
3454 ("Error loading configuation from `" ^ path ^ "': " ^
3455 Printexc.to_string exn);
3456 end;
3457 close_in ic;
3459 | None -> ()
3460 else
3461 f (Hashtbl.create 0, defconf)
3464 let load () =
3465 let f (h, dc) =
3466 let pc, pb, px, pa =
3468 Hashtbl.find h (Filename.basename state.path)
3469 with Not_found -> dc, [], 0, (0, 0.0)
3471 setconf defconf dc;
3472 setconf conf pc;
3473 state.bookmarks <- pb;
3474 state.x <- px;
3475 state.scrollw <- conf.scrollbw;
3476 if conf.jumpback
3477 then state.anchor <- pa;
3478 cbput state.hists.nav pa;
3480 load1 f
3483 let add_attrs bb always dc c =
3484 let ob s a b =
3485 if always || a != b
3486 then Printf.bprintf bb "\n %s='%b'" s a
3487 and oi s a b =
3488 if always || a != b
3489 then Printf.bprintf bb "\n %s='%d'" s a
3490 and oz s a b =
3491 if always || a <> b
3492 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
3493 and oc s a b =
3494 if always || a <> b
3495 then
3496 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
3497 and os s a b =
3498 if always || a <> b
3499 then
3500 Printf.bprintf bb "\n %s='%s'" s a
3502 let w, h =
3503 if always
3504 then dc.winw, dc.winh
3505 else
3506 match state.fullscreen with
3507 | Some wh -> wh
3508 | None -> c.winw, c.winh
3510 let zoom, presentation, interpagespace, showall=
3511 if always
3512 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3513 else
3514 match state.mode with
3515 | Birdseye (bc, _, _, _, _) ->
3516 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
3517 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3519 oi "width" w dc.winw;
3520 oi "height" h dc.winh;
3521 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
3522 oi "scroll-handle-height" c.scrollh dc.scrollh;
3523 ob "case-insensitive-search" c.icase dc.icase;
3524 ob "preload" c.preload dc.preload;
3525 oi "page-bias" c.pagebias dc.pagebias;
3526 oi "scroll-step" c.scrollstep dc.scrollstep;
3527 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
3528 ob "max-height-fit" c.maxhfit dc.maxhfit;
3529 ob "crop-hack" c.crophack dc.crophack;
3530 ob "throttle" showall dc.showall;
3531 ob "highlight-links" c.hlinks dc.hlinks;
3532 ob "under-cursor-info" c.underinfo dc.underinfo;
3533 oi "vertical-margin" interpagespace dc.interpagespace;
3534 oz "zoom" zoom dc.zoom;
3535 ob "presentation" presentation dc.presentation;
3536 oi "rotation-angle" c.angle dc.angle;
3537 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3538 ob "proportional-display" c.proportional dc.proportional;
3539 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3540 oi "texcount" c.texcount dc.texcount;
3541 oi "slice-height" c.sliceheight dc.sliceheight;
3542 oi "thumbnail-width" c.thumbw dc.thumbw;
3543 ob "persistent-location" c.jumpback dc.jumpback;
3544 oc "background-color" c.bgcolor dc.bgcolor;
3545 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
3546 os "ui-font" c.uifont dc.uifont;
3549 let save () =
3550 let bb = Buffer.create 32768 in
3551 let f (h, dc) =
3552 let dc = if conf.bedefault then conf else dc in
3553 Buffer.add_string bb "<llppconfig>\n<defaults ";
3554 add_attrs bb true dc dc;
3555 Buffer.add_string bb "/>\n";
3557 let adddoc path pan anchor c bookmarks =
3558 if bookmarks == [] && c = dc && anchor = emptyanchor
3559 then ()
3560 else (
3561 Printf.bprintf bb "<doc path='%s'"
3562 (enent path 0 (String.length path));
3564 if anchor <> emptyanchor
3565 then (
3566 let n, y = anchor in
3567 Printf.bprintf bb " page='%d'" n;
3568 if y > 1e-6
3569 then
3570 Printf.bprintf bb " rely='%f'" y
3574 if pan != 0
3575 then Printf.bprintf bb " pan='%d'" pan;
3577 add_attrs bb false dc c;
3579 begin match bookmarks with
3580 | [] -> Buffer.add_string bb "/>\n"
3581 | _ ->
3582 Buffer.add_string bb ">\n<bookmarks>\n";
3583 List.iter (fun (title, _level, (page, rely)) ->
3584 Printf.bprintf bb
3585 "<item title='%s' page='%d'"
3586 (enent title 0 (String.length title))
3587 page
3589 if rely > 1e-6
3590 then
3591 Printf.bprintf bb " rely='%f'" rely
3593 Buffer.add_string bb "/>\n";
3594 ) bookmarks;
3595 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3596 end;
3600 let pan =
3601 match state.mode with
3602 | Birdseye (_, pan, _, _, _) -> pan
3603 | _ -> state.x
3605 let basename = Filename.basename state.path in
3606 adddoc basename pan (getanchor ())
3607 { conf with
3608 autoscrollstep =
3609 if state.ascrollstep > 0
3610 then state.ascrollstep
3611 else conf.autoscrollstep }
3612 (if conf.savebmarks then state.bookmarks else []);
3614 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3615 if basename <> path
3616 then adddoc path x y c bookmarks
3617 ) h;
3618 Buffer.add_string bb "</llppconfig>";
3620 load1 f;
3621 if Buffer.length bb > 0
3622 then
3624 let tmp = path ^ ".tmp" in
3625 let oc = open_out_bin tmp in
3626 Buffer.output_buffer oc bb;
3627 close_out oc;
3628 Sys.rename tmp path;
3629 with exn ->
3630 prerr_endline
3631 ("error while saving configuration: " ^ Printexc.to_string exn)
3633 end;;
3635 let () =
3636 Arg.parse
3637 (Arg.align
3638 [("-p", Arg.String (fun s -> state.password <- s) , " Set password")
3639 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3640 " Print version and exit")]
3642 (fun s -> state.path <- s)
3643 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
3645 if String.length state.path = 0
3646 then (prerr_endline "file name missing"; exit 1);
3648 State.load ();
3650 let _ = Glut.init Sys.argv in
3651 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3652 let () = Glut.initWindowSize conf.winw conf.winh in
3653 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3655 let csock, ssock =
3656 if Sys.os_type = "Unix"
3657 then
3658 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3659 else
3660 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3661 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3662 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3663 Unix.bind sock addr;
3664 Unix.listen sock 1;
3665 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3666 Unix.connect csock addr;
3667 let ssock, _ = Unix.accept sock in
3668 Unix.close sock;
3669 let opts sock =
3670 Unix.setsockopt sock Unix.TCP_NODELAY true;
3671 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3673 opts ssock;
3674 opts csock;
3675 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3676 ssock, csock
3679 let () = Glut.displayFunc display in
3680 let () = Glut.reshapeFunc reshape in
3681 let () = Glut.keyboardFunc keyboard in
3682 let () = Glut.specialFunc special in
3683 let () = Glut.idleFunc (Some idle) in
3684 let () = Glut.mouseFunc mouse in
3685 let () = Glut.motionFunc motion in
3686 let () = Glut.passiveMotionFunc pmotion in
3688 init ssock (
3689 conf.angle, conf.proportional, conf.texcount,
3690 conf.sliceheight, conf.uifont
3692 state.csock <- csock;
3693 state.ssock <- ssock;
3694 state.text <- "Opening " ^ state.path;
3695 writeopen state.path state.password;
3697 at_exit State.save;
3699 let rec handlelablglutbug () =
3701 Glut.mainLoop ();
3702 with Glut.BadEnum "key in special_of_int" ->
3703 showtext '!' " LablGlut bug: special key not recognized";
3704 handlelablglutbug ()
3706 handlelablglutbug ();