Add -v command line switch
[llpp.git] / main.ml
blobd857bcf4cc1b87edc8cc75a5eceda3cd330b4cee
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
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
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mzoom of (int * int)
42 | Mnone
45 type textentry = (char * string * onhist * onkey * ondone)
46 and onkey = string -> int -> te
47 and ondone = string -> unit
48 and histcancel = unit -> unit
49 and onhist = ((histcmd -> string) * histcancel) option
50 and histcmd = HCnext | HCprev | HCfirst | HClast
51 and te =
52 | TEstop
53 | TEdone of string
54 | TEcont of string
55 | TEswitch of textentry
58 type 'a circbuf =
59 { store : 'a array
60 ; mutable rc : int
61 ; mutable wc : int
62 ; mutable len : int
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollstep : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscrollstep : int
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable thumbw : width
160 type outline = string * int * int * float;;
161 type outlines =
162 | Oarray of outline array
163 | Olist of outline list
164 | Onarrow of string * outline array * outline array
167 type rect = (float * float * float * float * float * float * float * float);;
169 type pagemapkey = (pageno * width * angle * proportional * gen);;
171 type anchor = pageno * top;;
173 type mode =
174 | Birdseye of (conf * leftx * pageno * pageno * anchor)
175 | Outline of (bool * int * int * outline array * string)
176 | Textentry of (textentry * mode)
177 | View
180 let isbirdseye = function Birdseye _ -> true | _ -> false;;
181 let istextentry = function Textentry _ -> true | _ -> false;;
183 type state =
184 { mutable csock : Unix.file_descr
185 ; mutable ssock : Unix.file_descr
186 ; mutable w : int
187 ; mutable x : int
188 ; mutable y : int
189 ; mutable anchor : anchor
190 ; mutable maxy : int
191 ; mutable layout : layout list
192 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
193 ; mutable pdims : (pageno * width * height * leftx) list
194 ; mutable pagecount : int
195 ; pagecache : string circbuf
196 ; mutable rendering : bool
197 ; mutable mstate : mstate
198 ; mutable searchpattern : string
199 ; mutable rects : (pageno * recttype * rect) list
200 ; mutable rects1 : (pageno * recttype * rect) list
201 ; mutable text : string
202 ; mutable fullscreen : (width * height) option
203 ; mutable mode : mode
204 ; mutable outlines : outlines
205 ; mutable bookmarks : outline list
206 ; mutable path : string
207 ; mutable password : string
208 ; mutable invalidated : int
209 ; mutable colorscale : float
210 ; mutable memused : int
211 ; mutable gen : gen
212 ; mutable throttle : layout list option
213 ; mutable ascrollstep : int
214 ; mutable help : string list
215 ; hists : hists
217 and hists =
218 { pat : string circbuf
219 ; pag : string circbuf
220 ; nav : anchor circbuf
224 let defconf =
225 { scrollw = 7
226 ; scrollh = 12
227 ; icase = true
228 ; preload = true
229 ; pagebias = 0
230 ; verbose = false
231 ; scrollstep = 24
232 ; maxhfit = true
233 ; crophack = false
234 ; autoscrollstep = 24
235 ; showall = false
236 ; hlinks = false
237 ; underinfo = false
238 ; interpagespace = 2
239 ; zoom = 1.0
240 ; presentation = false
241 ; angle = 0
242 ; winw = 900
243 ; winh = 900
244 ; savebmarks = true
245 ; proportional = true
246 ; memlimit = 32*1024*1024
247 ; texcount = 256
248 ; sliceheight = 24
249 ; thumbw = 76
253 let conf = { defconf with angle = defconf.angle };;
255 let state =
256 { csock = Unix.stdin
257 ; ssock = Unix.stdin
258 ; x = 0
259 ; y = 0
260 ; anchor = (0, 0.0)
261 ; w = 0
262 ; layout = []
263 ; maxy = max_int
264 ; pagemap = Hashtbl.create 10
265 ; pagecache = cbnew 100 ""
266 ; pdims = []
267 ; pagecount = 0
268 ; rendering = false
269 ; mstate = Mnone
270 ; rects = []
271 ; rects1 = []
272 ; text = ""
273 ; mode = View
274 ; fullscreen = None
275 ; searchpattern = ""
276 ; outlines = Olist []
277 ; bookmarks = []
278 ; path = ""
279 ; password = ""
280 ; invalidated = 0
281 ; hists =
282 { nav = cbnew 100 (0, 0.0)
283 ; pat = cbnew 20 ""
284 ; pag = cbnew 10 ""
286 ; colorscale = 1.0
287 ; memused = 0
288 ; gen = 0
289 ; throttle = None
290 ; ascrollstep = 0
291 ; help = Help.keys
295 let vlog fmt =
296 if conf.verbose
297 then
298 Printf.kprintf prerr_endline fmt
299 else
300 Printf.kprintf ignore fmt
303 let writecmd fd s =
304 let len = String.length s in
305 let n = 4 + len in
306 let b = Buffer.create n in
307 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
308 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
309 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
310 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
311 Buffer.add_string b s;
312 let s' = Buffer.contents b in
313 let n' = Unix.write fd s' 0 n in
314 if n' != n then failwith "write failed";
317 let readcmd fd =
318 let s = "xxxx" in
319 let n = Unix.read fd s 0 4 in
320 if n != 4 then failwith "incomplete read(len)";
321 let len = 0
322 lor (Char.code s.[0] lsl 24)
323 lor (Char.code s.[1] lsl 16)
324 lor (Char.code s.[2] lsl 8)
325 lor (Char.code s.[3] lsl 0)
327 let s = String.create len in
328 let n = Unix.read fd s 0 len in
329 if n != len then failwith "incomplete read(data)";
333 let makecmd s l =
334 let b = Buffer.create 10 in
335 Buffer.add_string b s;
336 let rec combine = function
337 | [] -> b
338 | x :: xs ->
339 Buffer.add_char b ' ';
340 let s =
341 match x with
342 | `b b -> if b then "1" else "0"
343 | `s s -> s
344 | `i i -> string_of_int i
345 | `f f -> string_of_float f
346 | `I f -> string_of_int (truncate f)
348 Buffer.add_string b s;
349 combine xs;
351 combine l;
354 let wcmd s l =
355 let cmd = Buffer.contents (makecmd s l) in
356 writecmd state.csock cmd;
359 let calcips h =
360 if conf.presentation
361 then
362 let d = conf.winh - h in
363 max 0 ((d + 1) / 2)
364 else
365 conf.interpagespace
368 let calcheight () =
369 let rec f pn ph pi fh l =
370 match l with
371 | (n, _, h, _) :: rest ->
372 let ips = calcips h in
373 let fh =
374 if conf.presentation
375 then fh+ips
376 else (
377 if isbirdseye state.mode && pn = 0
378 then fh + ips
379 else fh
382 let fh = fh + ((n - pn) * (ph + pi)) in
383 f n h ips fh rest;
385 | [] ->
386 let inc =
387 if conf.presentation || (isbirdseye state.mode && pn = 0)
388 then 0
389 else -pi
391 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
392 max 0 fh
394 let fh = f 0 0 0 0 state.pdims in
398 let getpageyh pageno =
399 let rec f pn ph pi y l =
400 match l with
401 | (n, _, h, _) :: rest ->
402 let ips = calcips h in
403 if n >= pageno
404 then
405 let h = if n = pageno then h else ph in
406 if conf.presentation && n = pageno
407 then
408 y + (pageno - pn) * (ph + pi) + pi, h
409 else
410 y + (pageno - pn) * (ph + pi), h
411 else
412 let y = y + (if conf.presentation then pi else 0) in
413 let y = y + (n - pn) * (ph + pi) in
414 f n h ips y rest
416 | [] ->
417 y + (pageno - pn) * (ph + pi), ph
419 f 0 0 0 0 state.pdims
422 let getpagey pageno = fst (getpageyh pageno);;
424 let layout y sh =
425 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
426 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
427 match pdims with
428 | (pageno', w, h, x) :: rest when pageno' = pageno ->
429 let ips = calcips h in
430 let yinc =
431 if conf.presentation || (isbirdseye state.mode && pageno = 0)
432 then ips
433 else 0
435 (w, h, ips, x), rest, pdimno + 1, yinc
436 | _ ->
437 prev, pdims, pdimno, 0
439 let dy = dy + yinc in
440 let py = py + yinc in
441 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
442 then
443 accu
444 else
445 let vy = y + dy in
446 if py + h <= vy - yinc
447 then
448 let py = py + h + ips in
449 let dy = max 0 (py - y) in
450 f ~pageno:(pageno+1)
451 ~pdimno
452 ~prev:curr
455 ~pdims:rest
456 ~cacheleft
457 ~accu
458 else
459 let pagey = vy - py in
460 let pagevh = h - pagey in
461 let pagevh = min (sh - dy) pagevh in
462 let off = if yinc > 0 then py - vy else 0 in
463 let py = py + h + ips in
464 let e =
465 { pageno = pageno
466 ; pagedimno = pdimno
467 ; pagew = w
468 ; pageh = h
469 ; pagedispy = dy + off
470 ; pagey = pagey + off
471 ; pagevh = pagevh - off
472 ; pagex = x
475 let accu = e :: accu in
476 f ~pageno:(pageno+1)
477 ~pdimno
478 ~prev:curr
480 ~dy:(dy+pagevh+ips)
481 ~pdims:rest
482 ~cacheleft:(cacheleft-1)
483 ~accu
485 if state.invalidated = 0
486 then (
487 let accu =
489 ~pageno:0
490 ~pdimno:~-1
491 ~prev:(0,0,0,0)
492 ~py:0
493 ~dy:0
494 ~pdims:state.pdims
495 ~cacheleft:(cbcap state.pagecache)
496 ~accu:[]
498 List.rev accu
500 else
504 let clamp incr =
505 let y = state.y + incr in
506 let y = max 0 y in
507 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
511 let getopaque pageno =
512 try Some (Hashtbl.find state.pagemap
513 (pageno, state.w, conf.angle, conf.proportional, state.gen))
514 with Not_found -> None
517 let cache pageno opaque =
518 Hashtbl.replace state.pagemap
519 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
522 let validopaque opaque = String.length opaque > 0;;
524 let render l =
525 match getopaque l.pageno with
526 | None when not state.rendering ->
527 state.rendering <- true;
528 cache l.pageno ("", -1);
529 wcmd "render" [`i (l.pageno + 1)
530 ;`i l.pagedimno
531 ;`i l.pagew
532 ;`i l.pageh];
533 | _ -> ()
536 let loadlayout layout =
537 let rec f all = function
538 | l :: ls ->
539 begin match getopaque l.pageno with
540 | None -> render l; f false ls
541 | Some (opaque, _) -> f (all && validopaque opaque) ls
543 | [] -> all
545 f (layout <> []) layout;
548 let findpageforopaque opaque =
549 Hashtbl.fold
550 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
551 state.pagemap None
554 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
556 let preload () =
557 let oktopreload =
558 if conf.preload
559 then
560 let memleft = conf.memlimit - state.memused in
561 if memleft < 0
562 then
563 let opaque = cbpeek state.pagecache in
564 match findpageforopaque opaque with
565 | Some ((n, _, _, _, _), size) ->
566 memleft + size >= 0 && not (pagevisible state.layout n)
567 | None -> false
568 else true
569 else false
571 if oktopreload
572 then
573 let presentation = conf.presentation in
574 let interpagespace = conf.interpagespace in
575 let maxy = state.maxy in
576 conf.presentation <- false;
577 conf.interpagespace <- 0;
578 state.maxy <- calcheight ();
579 let y =
580 match state.layout with
581 | [] -> 0
582 | l :: _ -> getpagey l.pageno + l.pagey
584 let y = if y < conf.winh then 0 else y - conf.winh in
585 let pages = layout y (conf.winh*3) in
586 List.iter render pages;
587 conf.presentation <- presentation;
588 conf.interpagespace <- interpagespace;
589 state.maxy <- maxy;
592 let gotoy y =
593 let y = max 0 y in
594 let y = min state.maxy y in
595 let pages = layout y conf.winh in
596 let ready = loadlayout pages in
597 if conf.showall
598 then (
599 if ready
600 then (
601 state.y <- y;
602 state.layout <- pages;
603 state.throttle <- None;
604 Glut.postRedisplay ();
606 else (
607 state.throttle <- Some pages;
610 else (
611 state.y <- y;
612 state.layout <- pages;
613 state.throttle <- None;
614 Glut.postRedisplay ();
616 begin match state.mode with
617 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
618 if not (pagevisible pages pageno)
619 then (
620 match state.layout with
621 | [] -> ()
622 | l :: _ ->
623 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
625 | _ -> ()
626 end;
627 preload ();
630 let gotoy_and_clear_text y =
631 gotoy y;
632 if not conf.verbose then state.text <- "";
635 let emptyanchor = (0, 0.0);;
637 let getanchor () =
638 match state.layout with
639 | [] -> emptyanchor
640 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
643 let getanchory (n, top) =
644 let y, h = getpageyh n in
645 y + (truncate (top *. float h));
648 let gotoanchor anchor =
649 gotoy (getanchory anchor);
652 let addnav () =
653 cbput state.hists.nav (getanchor ());
656 let getnav () =
657 let anchor = cbgetc state.hists.nav ~-1 in
658 getanchory anchor;
661 let gotopage n top =
662 let y, h = getpageyh n in
663 gotoy_and_clear_text (y + (truncate (top *. float h)));
666 let gotopage1 n top =
667 let y = getpagey n in
668 gotoy_and_clear_text (y + top);
671 let invalidate () =
672 state.layout <- [];
673 state.pdims <- [];
674 state.rects <- [];
675 state.rects1 <- [];
676 state.invalidated <- state.invalidated + 1;
679 let scalecolor c =
680 let c = c *. state.colorscale in
681 (c, c, c);
684 let represent () =
685 state.maxy <- calcheight ();
686 match state.mode with
687 | Birdseye (_, _, pageno, _, _) ->
688 let y, h = getpageyh pageno in
689 let top = (conf.winh - h) / 2 in
690 gotoy (max 0 (y - top))
691 | _ -> gotoanchor state.anchor
694 let pagematrix () =
695 GlMat.mode `projection;
696 GlMat.load_identity ();
697 GlMat.rotate ~x:1.0 ~angle:180.0 ();
698 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
699 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
700 if state.x != 0
701 then (
702 GlMat.translate ~x:(float state.x) ();
706 let winmatrix () =
707 GlMat.mode `projection;
708 GlMat.load_identity ();
709 GlMat.rotate ~x:1.0 ~angle:180.0 ();
710 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
711 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
714 let reshape ~w ~h =
715 if state.invalidated = 0
716 then state.anchor <- getanchor ();
718 conf.winw <- w;
719 let w = truncate (float w *. conf.zoom) - conf.scrollw in
720 let w = max w 2 in
721 state.w <- w;
722 conf.winh <- h;
723 GlMat.mode `modelview;
724 GlMat.load_identity ();
725 GlClear.color (scalecolor 1.0);
726 GlClear.clear [`color];
728 invalidate ();
729 wcmd "geometry" [`i w; `i h];
732 let showtext c s =
733 GlDraw.color (0.0, 0.0, 0.0);
734 GlDraw.rect
735 (0.0, float (conf.winh - 18))
736 (float (conf.winw - conf.scrollw - 1), float conf.winh)
738 let font = Glut.BITMAP_8_BY_13 in
739 GlDraw.color (1.0, 1.0, 1.0);
740 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
741 Glut.bitmapCharacter ~font ~c:(Char.code c);
742 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
745 let enttext () =
746 let len = String.length state.text in
747 match state.mode with
748 | Textentry ((c, text, _, _, _), _) ->
749 let s =
750 if len > 0
751 then
752 text ^ " [" ^ state.text ^ "]"
753 else
754 text
756 showtext c s;
758 | _ ->
759 if len > 0 then showtext ' ' state.text
762 let showtext c s =
763 state.text <- Printf.sprintf "%c%s" c s;
764 Glut.postRedisplay ();
767 let act cmd =
768 match cmd.[0] with
769 | 'c' ->
770 state.pdims <- [];
772 | 'D' ->
773 state.rects <- state.rects1;
774 Glut.postRedisplay ()
776 | 'C' ->
777 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
778 state.pagecount <- n;
779 state.invalidated <- state.invalidated - 1;
780 if state.invalidated = 0
781 then represent ()
783 | 't' ->
784 let s = Scanf.sscanf cmd "t %n"
785 (fun n -> String.sub cmd n (String.length cmd - n))
787 Glut.setWindowTitle s
789 | 'T' ->
790 let s = Scanf.sscanf cmd "T %n"
791 (fun n -> String.sub cmd n (String.length cmd - n))
793 if istextentry state.mode
794 then (
795 state.text <- s;
796 showtext ' ' s;
798 else (
799 state.text <- s;
800 Glut.postRedisplay ();
803 | 'V' ->
804 if conf.verbose
805 then
806 let s = Scanf.sscanf cmd "V %n"
807 (fun n -> String.sub cmd n (String.length cmd - n))
809 state.text <- s;
810 showtext ' ' s;
812 | 'F' ->
813 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
814 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
815 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
816 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
818 let y = (getpagey pageno) + truncate y0 in
819 addnav ();
820 gotoy y;
821 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
823 | 'R' ->
824 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
825 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
826 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
827 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
829 state.rects1 <-
830 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
832 | 'r' ->
833 let n, w, h, r, l, s, p =
834 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
835 (fun n w h r l s p ->
836 (n-1, w, h, r, l != 0, s, p))
839 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
840 state.memused <- state.memused + s;
842 let layout =
843 match state.throttle with
844 | None -> state.layout
845 | Some layout -> layout
848 let rec gc () =
849 if (state.memused <= conf.memlimit) || cbempty state.pagecache
850 then ()
851 else (
852 let evictedopaque = cbpeek state.pagecache in
853 match findpageforopaque evictedopaque with
854 | None -> failwith "bug in gc"
855 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
856 if state.gen != gen || not (pagevisible layout evictedn)
857 then (
858 wcmd "free" [`s evictedopaque];
859 state.memused <- state.memused - evictedsize;
860 Hashtbl.remove state.pagemap k;
861 cbdecr state.pagecache;
862 gc ();
866 gc ();
868 cbput state.pagecache p;
869 state.rendering <- false;
871 begin match state.throttle with
872 | None ->
873 if pagevisible state.layout n
874 then gotoy state.y
875 else (
876 let allvisible = loadlayout state.layout in
877 if allvisible then preload ();
880 | Some layout ->
881 match layout with
882 | [] -> ()
883 | l :: _ ->
884 let y = getpagey l.pageno + l.pagey in
885 gotoy y
888 | 'l' ->
889 let (n, w, h, x) as pdim =
890 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
892 state.pdims <- pdim :: state.pdims
894 | 'o' ->
895 let (l, n, t, h, pos) =
896 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
898 let s = String.sub cmd pos (String.length cmd - pos) in
899 let s =
900 let l = String.length s in
901 let b = Buffer.create (String.length s) in
902 let rec loop pc2 i =
903 if i = l
904 then ()
905 else
906 let pc2 =
907 match s.[i] with
908 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
909 | '\xc2' -> true
910 | c ->
911 let c = if Char.code c land 0x80 = 0 then c else '?' in
912 Buffer.add_char b c;
913 false
915 loop pc2 (i+1)
917 loop false 0;
918 Buffer.contents b
920 let outline = (s, l, n, float t /. float h) in
921 let outlines =
922 match state.outlines with
923 | Olist outlines -> Olist (outline :: outlines)
924 | Oarray _ -> Olist [outline]
925 | Onarrow _ -> Olist [outline]
927 state.outlines <- outlines
929 | _ ->
930 dolog "unknown cmd `%S'" cmd
933 let now = Unix.gettimeofday;;
935 let idle () =
936 let rec loop delay =
937 let r, _, _ = Unix.select [state.csock] [] [] delay in
938 begin match r with
939 | [] ->
940 if state.ascrollstep > 0
941 then begin
942 let y = state.y + state.ascrollstep in
943 let y = if y >= state.maxy then 0 else y in
944 gotoy y;
945 state.text <- "";
946 end;
948 | _ ->
949 let cmd = readcmd state.csock in
950 act cmd;
951 loop 0.0
952 end;
953 in loop 0.001
956 let onhist cb =
957 let rc = cb.rc in
958 let action = function
959 | HCprev -> cbget cb ~-1
960 | HCnext -> cbget cb 1
961 | HCfirst -> cbget cb ~-(cb.rc)
962 | HClast -> cbget cb (cb.len - 1 - cb.rc)
963 and cancel () = cb.rc <- rc
964 in (action, cancel)
967 let search pattern forward =
968 if String.length pattern > 0
969 then
970 let pn, py =
971 match state.layout with
972 | [] -> 0, 0
973 | l :: _ ->
974 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
976 let cmd =
977 let b = makecmd "search"
978 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
980 Buffer.add_char b ',';
981 Buffer.add_string b pattern;
982 Buffer.add_char b '\000';
983 Buffer.contents b;
985 writecmd state.csock cmd;
988 let intentry text key =
989 let c = Char.unsafe_chr key in
990 match c with
991 | '0' .. '9' ->
992 let s = "x" in s.[0] <- c;
993 let text = text ^ s in
994 TEcont text
996 | _ ->
997 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
998 TEcont text
1001 let addchar s c =
1002 let b = Buffer.create (String.length s + 1) in
1003 Buffer.add_string b s;
1004 Buffer.add_char b c;
1005 Buffer.contents b;
1008 let textentry text key =
1009 let c = Char.unsafe_chr key in
1010 match c with
1011 | _ when key >= 32 && key < 127 ->
1012 let text = addchar text c in
1013 TEcont text
1015 | _ ->
1016 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1017 TEcont text
1020 let reinit angle proportional =
1021 conf.angle <- angle;
1022 conf.proportional <- proportional;
1023 invalidate ();
1024 wcmd "reinit" [`i angle; `b proportional];
1027 let setzoom zoom =
1028 let zoom = max 0.01 (min 2.2 zoom) in
1029 if zoom <> conf.zoom
1030 then (
1031 if zoom <= 1.0
1032 then state.x <- 0;
1033 conf.zoom <- zoom;
1034 reshape conf.winw conf.winh;
1035 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1039 let enterbirdseye () =
1040 let zoom = float conf.thumbw /. float conf.winw in
1041 let birdseyepageno =
1042 let rec fold = function
1043 | [] -> 0
1044 | l :: _ when l.pagey = 0 -> l.pageno
1045 | _ :: rest -> fold rest
1047 fold state.layout
1049 state.mode <- Birdseye (
1050 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1052 conf.zoom <- zoom;
1053 conf.presentation <- false;
1054 conf.interpagespace <- 10;
1055 conf.hlinks <- false;
1056 state.x <- 0;
1057 state.mstate <- Mnone;
1058 conf.showall <- false;
1059 Glut.setCursor Glut.CURSOR_INHERIT;
1060 if conf.verbose
1061 then
1062 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1063 (100.0*.zoom)
1064 else
1065 state.text <- ""
1067 reshape conf.winw conf.winh;
1070 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1071 state.mode <- View;
1072 conf.zoom <- c.zoom;
1073 conf.presentation <- c.presentation;
1074 conf.interpagespace <- c.interpagespace;
1075 conf.showall <- c.showall;
1076 conf.hlinks <- c.hlinks;
1077 state.x <- leftx;
1078 if conf.verbose
1079 then
1080 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1081 (100.0*.conf.zoom)
1083 reshape conf.winw conf.winh;
1084 state.anchor <- if goback then anchor else (pageno, 0.0);
1087 let togglebirdseye () =
1088 match state.mode with
1089 | Birdseye vals -> leavebirdseye vals true
1090 | View | Outline _ -> enterbirdseye ()
1091 | _ -> ()
1094 let optentry text key =
1095 let btos b = if b then "on" else "off" in
1096 let c = Char.unsafe_chr key in
1097 match c with
1098 | 's' ->
1099 let ondone s =
1100 try conf.scrollstep <- int_of_string s with exc ->
1101 state.text <- Printf.sprintf "bad integer `%s': %s"
1102 s (Printexc.to_string exc)
1104 TEswitch ('#', "", None, intentry, ondone)
1106 | 'A' ->
1107 let ondone s =
1109 conf.autoscrollstep <- int_of_string s;
1110 if state.ascrollstep > 0
1111 then state.ascrollstep <- conf.autoscrollstep;
1112 with exc ->
1113 state.text <- Printf.sprintf "bad integer `%s': %s"
1114 s (Printexc.to_string exc)
1116 TEswitch ('*', "", None, intentry, ondone)
1118 | 'Z' ->
1119 let ondone s =
1121 let zoom = float (int_of_string s) /. 100.0 in
1122 setzoom zoom
1123 with exc ->
1124 state.text <- Printf.sprintf "bad integer `%s': %s"
1125 s (Printexc.to_string exc)
1127 TEswitch ('@', "", None, intentry, ondone)
1129 | 't' ->
1130 let ondone s =
1132 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1133 state.text <-
1134 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1135 begin match state.mode with
1136 | Textentry (_, Birdseye beye) ->
1137 leavebirdseye beye false;
1138 enterbirdseye ()
1139 | _ -> ()
1140 end;
1141 with exc ->
1142 state.text <- Printf.sprintf "bad integer `%s': %s"
1143 s (Printexc.to_string exc)
1145 TEswitch ('$', "", None, intentry, ondone)
1147 | 'R' ->
1148 let ondone s =
1149 match try
1150 Some (int_of_string s)
1151 with exc ->
1152 state.text <- Printf.sprintf "bad integer `%s': %s"
1153 s (Printexc.to_string exc);
1154 None
1155 with
1156 | Some angle -> reinit angle conf.proportional
1157 | None -> ()
1159 TEswitch ('^', "", None, intentry, ondone)
1161 | 'i' ->
1162 conf.icase <- not conf.icase;
1163 TEdone ("case insensitive search " ^ (btos conf.icase))
1165 | 'p' ->
1166 conf.preload <- not conf.preload;
1167 gotoy state.y;
1168 TEdone ("preload " ^ (btos conf.preload))
1170 | 'v' ->
1171 conf.verbose <- not conf.verbose;
1172 TEdone ("verbose " ^ (btos conf.verbose))
1174 | 'h' ->
1175 conf.maxhfit <- not conf.maxhfit;
1176 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1177 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1179 | 'c' ->
1180 conf.crophack <- not conf.crophack;
1181 TEdone ("crophack " ^ btos conf.crophack)
1183 | 'a' ->
1184 conf.showall <- not conf.showall;
1185 TEdone ("showall " ^ btos conf.showall)
1187 | 'f' ->
1188 conf.underinfo <- not conf.underinfo;
1189 TEdone ("underinfo " ^ btos conf.underinfo)
1191 | 'P' ->
1192 conf.savebmarks <- not conf.savebmarks;
1193 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1195 | 'S' ->
1196 let ondone s =
1198 let pageno, py =
1199 match state.layout with
1200 | [] -> 0, 0
1201 | l :: _ ->
1202 l.pageno, l.pagey
1204 conf.interpagespace <- int_of_string s;
1205 state.maxy <- calcheight ();
1206 let y = getpagey pageno in
1207 gotoy (y + py)
1208 with exc ->
1209 state.text <- Printf.sprintf "bad integer `%s': %s"
1210 s (Printexc.to_string exc)
1212 TEswitch ('%', "", None, intentry, ondone)
1214 | 'l' ->
1215 reinit conf.angle (not conf.proportional);
1216 TEdone ("proprortional display " ^ btos conf.proportional)
1218 | _ ->
1219 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1220 TEstop
1223 let maxoutlinerows () = (conf.winh - 31) / 16;;
1225 let enterselector allowdel outlines errmsg msg =
1226 if Array.length outlines = 0
1227 then (
1228 showtext ' ' errmsg;
1230 else (
1231 state.text <- msg;
1232 Glut.setCursor Glut.CURSOR_INHERIT;
1233 let pageno =
1234 match state.layout with
1235 | [] -> -1
1236 | {pageno=pageno} :: rest -> pageno
1238 let active =
1239 let rec loop n =
1240 if n = Array.length outlines
1241 then 0
1242 else
1243 let (_, _, outlinepageno, _) = outlines.(n) in
1244 if outlinepageno >= pageno then n else loop (n+1)
1246 loop 0
1248 state.mode <- Outline
1249 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1250 Glut.postRedisplay ();
1254 let enteroutlinemode () =
1255 let outlines, msg =
1256 match state.outlines with
1257 | Oarray a -> a, ""
1258 | Olist l ->
1259 let a = Array.of_list (List.rev l) in
1260 state.outlines <- Oarray a;
1261 a, ""
1262 | Onarrow (pat, a, b) ->
1263 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1265 enterselector false outlines "Document has no outline" msg;
1268 let enterbookmarkmode () =
1269 let bookmarks = Array.of_list state.bookmarks in
1270 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1273 let enterinfomode () =
1274 let btos = function true -> "on" | _ -> "off" in
1275 let pageno, top = getanchor () in
1276 let help =
1277 let autoscrollstep =
1278 if state.ascrollstep > 0
1279 then state.ascrollstep
1280 else conf.autoscrollstep
1282 ("Current parameters")
1283 :: ("version " ^ Help.version)
1284 :: ("presentation mode " ^ btos conf.presentation)
1285 :: ("case insensitive search " ^ btos conf.icase)
1286 :: ("preload " ^ btos conf.preload)
1287 :: ("page bias " ^ string_of_int conf.pagebias)
1288 :: ("verbose " ^ btos conf.verbose)
1289 :: ("scroll step " ^ string_of_int conf.scrollstep)
1290 :: ("max fit " ^ btos conf.maxhfit)
1291 :: ("crop hack " ^ btos conf.crophack)
1292 :: ("autoscroll step " ^ string_of_int autoscrollstep)
1293 :: ("throttle " ^ btos conf.showall)
1294 :: ("highlight links " ^ btos conf.hlinks)
1295 :: ("under info " ^ btos conf.underinfo)
1296 :: ("veritcal margin " ^ string_of_int conf.interpagespace)
1297 :: ("zoom " ^ Printf.sprintf "%-5.1f" (conf.zoom*.100.))
1298 :: ("rotation " ^ string_of_int conf.angle)
1299 :: ("persistent bookmarks " ^ btos conf.savebmarks)
1300 :: ("proportional display " ^ btos conf.proportional)
1301 :: ("pixmap cache size " ^ string_of_int conf.memlimit)
1302 :: ("pixmap cache used " ^ string_of_int state.memused)
1303 :: ("thumbnail width " ^ string_of_int conf.thumbw)
1304 :: (Printf.sprintf "window dimensions %dx%d " conf.winw conf.winh)
1305 :: []
1307 let o =
1308 let o = Array.create (List.length help) ("", 0, pageno, top) in
1309 let rec iteri i = function
1310 | [] -> ()
1311 | s :: rest ->
1312 o.(i) <- (s, (if i = 0 then 0 else 1), pageno, top);
1313 iteri (i+1) rest
1315 iteri 0 help;
1318 enterselector false o "Info not available" "";
1321 let enterhelpmode () =
1322 let pageno, top = getanchor () in
1323 let o =
1324 let help = ("Keys for llpp " ^ Help.version) ::state.help in
1325 let o = Array.create (List.length help) ("", 0, pageno, top) in
1326 let rec iteri i = function
1327 | [] -> ()
1328 | s :: rest ->
1329 o.(i) <- (s, (if i = 0 then 0 else 1), pageno, top);
1330 iteri (i+1) rest
1332 iteri 0 help;
1335 enterselector false o "Help not available" "";
1338 let quickbookmark ?title () =
1339 match state.layout with
1340 | [] -> ()
1341 | l :: _ ->
1342 let title =
1343 match title with
1344 | None ->
1345 let sec = Unix.gettimeofday () in
1346 let tm = Unix.localtime sec in
1347 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1348 (l.pageno+1)
1349 tm.Unix.tm_mday
1350 tm.Unix.tm_mon
1351 (tm.Unix.tm_year + 1900)
1352 tm.Unix.tm_hour
1353 tm.Unix.tm_min
1354 | Some title -> title
1356 state.bookmarks <-
1357 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1360 let doreshape w h =
1361 state.fullscreen <- None;
1362 Glut.reshapeWindow w h;
1365 let writeopen path password =
1366 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1369 let opendoc path password =
1370 invalidate ();
1371 state.path <- path;
1372 state.password <- password;
1373 state.gen <- state.gen + 1;
1375 writeopen path password;
1376 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1377 wcmd "geometry" [`i state.w; `i conf.winh];
1380 let viewkeyboard ~key ~x ~y =
1381 let enttext te =
1382 state.mode <- Textentry (te, state.mode);
1383 state.text <- "";
1384 enttext ();
1385 Glut.postRedisplay ()
1387 let c = Char.chr key in
1388 match c with
1389 | '\027' | 'q' ->
1390 exit 0
1392 | '\008' ->
1393 let y = getnav () in
1394 gotoy_and_clear_text y
1396 | 'o' ->
1397 enteroutlinemode ()
1399 | 'u' ->
1400 state.rects <- [];
1401 state.text <- "";
1402 Glut.postRedisplay ()
1404 | '/' | '?' ->
1405 let ondone isforw s =
1406 cbput state.hists.pat s;
1407 state.searchpattern <- s;
1408 search s isforw
1410 enttext (c, "", Some (onhist state.hists.pat),
1411 textentry, ondone (c ='/'))
1413 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1414 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1415 setzoom (min 2.2 (conf.zoom +. incr))
1417 | '+' ->
1418 let ondone s =
1419 let n =
1420 try int_of_string s with exc ->
1421 state.text <- Printf.sprintf "bad integer `%s': %s"
1422 s (Printexc.to_string exc);
1423 max_int
1425 if n != max_int
1426 then (
1427 conf.pagebias <- n;
1428 state.text <- "page bias is now " ^ string_of_int n;
1431 enttext ('+', "", None, intentry, ondone)
1433 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1434 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1435 setzoom (max 0.01 (conf.zoom -. decr))
1437 | '-' ->
1438 let ondone msg =
1439 state.text <- msg;
1441 enttext ('-', "", None, optentry, ondone)
1443 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1444 setzoom 1.0
1446 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1447 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1448 if zoom < 1.0
1449 then setzoom zoom
1451 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1452 togglebirdseye ()
1454 | '0' .. '9' ->
1455 let ondone s =
1456 let n =
1457 try int_of_string s with exc ->
1458 state.text <- Printf.sprintf "bad integer `%s': %s"
1459 s (Printexc.to_string exc);
1462 if n >= 0
1463 then (
1464 addnav ();
1465 cbput state.hists.pag (string_of_int n);
1466 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1469 let pageentry text key =
1470 match Char.unsafe_chr key with
1471 | 'g' -> TEdone text
1472 | _ -> intentry text key
1474 let text = "x" in text.[0] <- c;
1475 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1477 | 'b' ->
1478 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1479 reshape conf.winw conf.winh;
1481 | 'l' ->
1482 conf.hlinks <- not conf.hlinks;
1483 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1484 Glut.postRedisplay ()
1486 | 'a' ->
1487 if state.ascrollstep = 0
1488 then state.ascrollstep <- conf.autoscrollstep
1489 else (
1490 conf.autoscrollstep <- state.ascrollstep;
1491 state.ascrollstep <- 0;
1494 | 'P' ->
1495 conf.presentation <- not conf.presentation;
1496 showtext ' ' ("presentation mode " ^
1497 if conf.presentation then "on" else "off");
1498 represent ()
1500 | 'f' ->
1501 begin match state.fullscreen with
1502 | None ->
1503 state.fullscreen <- Some (conf.winw, conf.winh);
1504 Glut.fullScreen ()
1505 | Some (w, h) ->
1506 state.fullscreen <- None;
1507 doreshape w h
1510 | 'g' ->
1511 gotoy_and_clear_text 0
1513 | 'n' ->
1514 search state.searchpattern true
1516 | 'p' | 'N' ->
1517 search state.searchpattern false
1519 | 't' ->
1520 begin match state.layout with
1521 | [] -> ()
1522 | l :: _ ->
1523 gotoy_and_clear_text (getpagey l.pageno)
1526 | ' ' ->
1527 begin match List.rev state.layout with
1528 | [] -> ()
1529 | l :: _ ->
1530 let pageno = min (l.pageno+1) (state.pagecount-1) in
1531 gotoy_and_clear_text (getpagey pageno)
1534 | '\127' ->
1535 begin match state.layout with
1536 | [] -> ()
1537 | l :: _ ->
1538 let pageno = max 0 (l.pageno-1) in
1539 gotoy_and_clear_text (getpagey pageno)
1542 | '=' ->
1543 let f (fn, ln) l =
1544 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1546 let fn, ln = List.fold_left f (-1, -1) state.layout in
1547 let s =
1548 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1549 let percent =
1550 if maxy <= 0
1551 then 100.
1552 else (100. *. (float state.y /. float maxy)) in
1553 if fn = ln
1554 then
1555 Printf.sprintf "Page %d of %d %.2f%%"
1556 (fn+1) state.pagecount percent
1557 else
1558 Printf.sprintf
1559 "Pages %d-%d of %d %.2f%%"
1560 (fn+1) (ln+1) state.pagecount percent
1562 showtext ' ' s;
1564 | 'w' ->
1565 begin match state.layout with
1566 | [] -> ()
1567 | l :: _ ->
1568 doreshape (l.pagew + conf.scrollw) l.pageh;
1569 Glut.postRedisplay ();
1572 | '\'' ->
1573 enterbookmarkmode ()
1575 | 'h' ->
1576 enterhelpmode ()
1578 | 'i' ->
1579 enterinfomode ()
1581 | 'm' ->
1582 let ondone s =
1583 match state.layout with
1584 | l :: _ ->
1585 state.bookmarks <-
1586 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1587 :: state.bookmarks
1588 | _ -> ()
1590 enttext ('~', "", None, textentry, ondone)
1592 | '~' ->
1593 quickbookmark ();
1594 showtext ' ' "Quick bookmark added";
1596 | 'z' ->
1597 begin match state.layout with
1598 | l :: _ ->
1599 let rect = getpdimrect l.pagedimno in
1600 let w, h =
1601 if conf.crophack
1602 then
1603 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1604 truncate (1.2 *. (rect.(3) -. rect.(0))))
1605 else
1606 (truncate (rect.(1) -. rect.(0)),
1607 truncate (rect.(3) -. rect.(0)))
1609 if w != 0 && h != 0
1610 then
1611 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1613 Glut.postRedisplay ();
1615 | [] -> ()
1618 | '<' | '>' ->
1619 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1621 | '[' | ']' ->
1622 state.colorscale <-
1623 max 0.0
1624 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1625 Glut.postRedisplay ()
1627 | 'k' -> gotoy (clamp (-conf.scrollstep))
1628 | 'j' -> gotoy (clamp conf.scrollstep)
1630 | 'r' -> opendoc state.path state.password
1632 | _ ->
1633 vlog "huh? %d %c" key (Char.chr key);
1636 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1637 let enttext te =
1638 state.mode <- Textentry (te, mode);
1639 state.text <- "";
1640 enttext ();
1641 Glut.postRedisplay ()
1643 match Char.unsafe_chr key with
1644 | '\008' ->
1645 let len = String.length text in
1646 if len = 0
1647 then (
1648 state.mode <- mode;
1649 Glut.postRedisplay ();
1651 else (
1652 let s = String.sub text 0 (len - 1) in
1653 enttext (c, s, opthist, onkey, ondone)
1656 | '\r' | '\n' ->
1657 ondone text;
1658 state.mode <- mode;
1659 Glut.postRedisplay ()
1661 | '\027' ->
1662 begin match opthist with
1663 | None -> ()
1664 | Some (_, onhistcancel) -> onhistcancel ()
1665 end;
1666 state.mode <- View;
1667 Glut.postRedisplay ()
1669 | _ ->
1670 begin match onkey text key with
1671 | TEdone text ->
1672 state.mode <- mode;
1673 ondone text;
1674 Glut.postRedisplay ()
1676 | TEcont text ->
1677 enttext (c, text, opthist, onkey, ondone);
1679 | TEstop ->
1680 state.mode <- mode;
1681 Glut.postRedisplay ()
1683 | TEswitch te ->
1684 state.mode <- Textentry (te, mode);
1685 Glut.postRedisplay ()
1686 end;
1689 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1690 match key with
1691 | 27 ->
1692 leavebirdseye beye true
1694 | 12 ->
1695 let y, h = getpageyh pageno in
1696 let top = (conf.winh - h) / 2 in
1697 gotoy (max 0 (y - top))
1699 | 13 ->
1700 leavebirdseye beye false
1702 | _ ->
1703 viewkeyboard ~key ~x ~y
1706 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1707 let narrow outlines pattern =
1708 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1709 match reopt with
1710 | None -> None
1711 | Some re ->
1712 let rec fold accu n =
1713 if n = -1
1714 then accu
1715 else
1716 let (s, _, _, _) as o = outlines.(n) in
1717 let accu =
1718 if (try ignore (Str.search_forward re s 0); true
1719 with Not_found -> false)
1720 then (o :: accu)
1721 else accu
1723 fold accu (n-1)
1725 let matched = fold [] (Array.length outlines - 1) in
1726 if matched = [] then None else Some (Array.of_list matched)
1728 let search active pattern incr =
1729 let dosearch re =
1730 let rec loop n =
1731 if n = Array.length outlines || n = -1
1732 then None
1733 else
1734 let (s, _, _, _) = outlines.(n) in
1736 (try ignore (Str.search_forward re s 0); true
1737 with Not_found -> false)
1738 then Some n
1739 else loop (n + incr)
1741 loop active
1744 let re = Str.regexp_case_fold pattern in
1745 dosearch re
1746 with Failure s ->
1747 state.text <- s;
1748 None
1750 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1751 match key with
1752 | 27 ->
1753 if String.length qsearch = 0
1754 then (
1755 state.text <- "";
1756 state.mode <- View;
1757 Glut.postRedisplay ();
1759 else (
1760 state.text <- "";
1761 state.mode <- Outline (allowdel, active, first, outlines, "");
1762 Glut.postRedisplay ();
1765 | 18 | 19 ->
1766 let incr = if key = 18 then -1 else 1 in
1767 let active, first =
1768 match search (active + incr) qsearch incr with
1769 | None ->
1770 state.text <- qsearch ^ " [not found]";
1771 active, first
1772 | Some active ->
1773 state.text <- qsearch;
1774 active, firstof active
1776 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1777 Glut.postRedisplay ();
1779 | 8 ->
1780 let len = String.length qsearch in
1781 if len = 0
1782 then ()
1783 else (
1784 if len = 1
1785 then (
1786 state.text <- "";
1787 state.mode <- Outline (allowdel, active, first, outlines, "");
1789 else
1790 let qsearch = String.sub qsearch 0 (len - 1) in
1791 let active, first =
1792 match search active qsearch ~-1 with
1793 | None ->
1794 state.text <- qsearch ^ " [not found]";
1795 active, first
1796 | Some active ->
1797 state.text <- qsearch;
1798 active, firstof active
1800 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1802 Glut.postRedisplay ()
1804 | 13 ->
1805 if active < Array.length outlines
1806 then (
1807 let (_, _, n, t) = outlines.(active) in
1808 addnav ();
1809 gotopage n t;
1811 state.text <- "";
1812 if allowdel then state.bookmarks <- Array.to_list outlines;
1813 state.mode <- View;
1814 Glut.postRedisplay ();
1816 | _ when key >= 32 && key < 127 ->
1817 let pattern = addchar qsearch (Char.chr key) in
1818 let active, first =
1819 match search active pattern 1 with
1820 | None ->
1821 state.text <- pattern ^ " [not found]";
1822 active, first
1823 | Some active ->
1824 state.text <- pattern;
1825 active, firstof active
1827 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1828 Glut.postRedisplay ()
1830 | 14 when not allowdel -> (* ctrl-n *)
1831 if String.length qsearch > 0
1832 then (
1833 let optoutlines = narrow outlines qsearch in
1834 begin match optoutlines with
1835 | None -> state.text <- "can't narrow"
1836 | Some outlines ->
1837 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1838 match state.outlines with
1839 | Olist l -> ()
1840 | Oarray a ->
1841 state.outlines <- Onarrow (qsearch, outlines, a)
1842 | Onarrow (pat, a, b) ->
1843 state.outlines <- Onarrow (qsearch, outlines, b)
1844 end;
1846 Glut.postRedisplay ()
1848 | 21 when not allowdel -> (* ctrl-u *)
1849 let outline =
1850 match state.outlines with
1851 | Oarray a -> a
1852 | Olist l ->
1853 let a = Array.of_list (List.rev l) in
1854 state.outlines <- Oarray a;
1856 | Onarrow (pat, a, b) ->
1857 state.outlines <- Oarray b;
1858 state.text <- "";
1861 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1862 Glut.postRedisplay ()
1864 | 12 ->
1865 state.mode <- Outline
1866 (allowdel, active, firstof active, outlines, qsearch);
1867 Glut.postRedisplay ()
1869 | 127 when allowdel ->
1870 let len = Array.length outlines - 1 in
1871 if len = 0
1872 then (
1873 state.mode <- View;
1874 state.bookmarks <- [];
1876 else (
1877 let bookmarks = Array.init len
1878 (fun i ->
1879 let i = if i >= active then i + 1 else i in
1880 outlines.(i)
1883 state.mode <-
1884 Outline (
1885 allowdel,
1886 min active (len-1),
1887 min first (len-1),
1888 bookmarks, qsearch
1891 Glut.postRedisplay ()
1893 | _ -> dolog "unknown key %d" key
1896 let keyboard ~key ~x ~y =
1897 if key = 7
1898 then
1899 wcmd "interrupt" []
1900 else
1901 match state.mode with
1902 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1903 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1904 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1905 | View -> viewkeyboard ~key ~x ~y
1908 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1909 match key with
1910 | Glut.KEY_UP ->
1911 let pageno = max 0 (pageno - 1) in
1912 let rec loop = function
1913 | [] -> gotopage1 pageno 0
1914 | l :: _ when l.pageno = pageno ->
1915 if l.pagedispy >= 0 && l.pagey = 0
1916 then Glut.postRedisplay ()
1917 else gotopage1 pageno 0
1918 | _ :: rest -> loop rest
1920 loop state.layout;
1921 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1923 | Glut.KEY_DOWN ->
1924 let pageno = min (state.pagecount - 1) (pageno + 1) in
1925 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1926 let rec loop = function
1927 | [] ->
1928 let y, h = getpageyh pageno in
1929 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1930 gotoy (clamp dy)
1931 | l :: rest when l.pageno = pageno ->
1932 if l.pagevh != l.pageh
1933 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1934 else Glut.postRedisplay ()
1935 | l :: rest -> loop rest
1937 loop state.layout
1939 | Glut.KEY_PAGE_UP ->
1940 begin match state.layout with
1941 | l :: _ ->
1942 if l.pagey != 0
1943 then (
1944 state.mode <- Birdseye (
1945 conf, leftx, l.pageno, hooverpageno, anchor
1947 gotopage1 l.pageno 0;
1949 else (
1950 let layout = layout (state.y-conf.winh) conf.winh in
1951 match layout with
1952 | [] -> gotoy (clamp (-conf.winh))
1953 | l :: _ ->
1954 state.mode <- Birdseye (
1955 conf, leftx, l.pageno, hooverpageno, anchor
1957 gotopage1 l.pageno 0
1960 | [] -> gotoy (clamp (-conf.winh))
1961 end;
1963 | Glut.KEY_PAGE_DOWN ->
1964 begin match List.rev state.layout with
1965 | l :: _ ->
1966 let layout = layout (state.y + conf.winh) conf.winh in
1967 begin match layout with
1968 | [] ->
1969 let incr = l.pageh - l.pagevh in
1970 if incr = 0
1971 then (
1972 state.mode <-
1973 Birdseye (
1974 conf, leftx, state.pagecount - 1, hooverpageno, anchor
1976 Glut.postRedisplay ();
1978 else gotoy (clamp (incr + conf.interpagespace*2));
1980 | l :: _ ->
1981 state.mode <-
1982 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
1983 gotopage1 l.pageno 0;
1986 | [] -> gotoy (clamp conf.winh)
1987 end;
1989 | Glut.KEY_HOME ->
1990 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
1991 gotopage1 0 0
1993 | Glut.KEY_END ->
1994 let pageno = state.pagecount - 1 in
1995 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1996 if not (pagevisible state.layout pageno)
1997 then
1998 let h =
1999 match List.rev state.pdims with
2000 | [] -> conf.winh
2001 | (_, _, h, _) :: _ -> h
2003 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2004 else Glut.postRedisplay ();
2005 | _ -> ()
2008 let setautoscrollspeed goingdown =
2009 let incr = max 1 (state.ascrollstep / 2) in
2010 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2011 state.ascrollstep <- astep;
2014 let special ~key ~x ~y =
2015 match state.mode with
2016 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2017 togglebirdseye ()
2019 | Birdseye vals ->
2020 birdseyespecial key x y vals
2022 | View ->
2023 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2024 then setautoscrollspeed (key = Glut.KEY_DOWN)
2025 else
2026 let y =
2027 match key with
2028 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2029 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2030 | Glut.KEY_DOWN -> clamp conf.scrollstep
2031 | Glut.KEY_PAGE_UP ->
2032 if Glut.getModifiers () land Glut.active_ctrl != 0
2033 then
2034 match state.layout with
2035 | [] -> state.y
2036 | l :: _ -> state.y - l.pagey
2037 else
2038 clamp (-conf.winh)
2039 | Glut.KEY_PAGE_DOWN ->
2040 if Glut.getModifiers () land Glut.active_ctrl != 0
2041 then
2042 match List.rev state.layout with
2043 | [] -> state.y
2044 | l :: _ -> getpagey l.pageno
2045 else
2046 clamp conf.winh
2047 | Glut.KEY_HOME -> addnav (); 0
2048 | Glut.KEY_END ->
2049 addnav ();
2050 state.maxy - (if conf.maxhfit then conf.winh else 0)
2052 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2053 state.x <- state.x - 10;
2054 state.y
2055 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2056 state.x <- state.x + 10;
2057 state.y
2059 | _ -> state.y
2061 gotoy_and_clear_text y
2063 | Textentry
2064 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
2065 let s =
2066 match key with
2067 | Glut.KEY_UP -> action HCprev
2068 | Glut.KEY_DOWN -> action HCnext
2069 | Glut.KEY_HOME -> action HCfirst
2070 | Glut.KEY_END -> action HClast
2071 | _ -> state.text
2073 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2074 Glut.postRedisplay ()
2076 | Textentry _ -> ()
2078 | Outline (allowdel, active, first, outlines, qsearch) ->
2079 let maxrows = maxoutlinerows () in
2080 let calcfirst first active =
2081 if active > first
2082 then
2083 let rows = active - first in
2084 if rows > maxrows then active - maxrows else first
2085 else active
2087 let navigate incr =
2088 let active = active + incr in
2089 let active = max 0 (min active (Array.length outlines - 1)) in
2090 let first = calcfirst first active in
2091 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2092 Glut.postRedisplay ()
2094 let updownlevel incr =
2095 let len = Array.length outlines in
2096 let (_, curlevel, _, _) = outlines.(active) in
2097 let rec flow i =
2098 if i = len then i-1 else if i = -1 then 0 else
2099 let (_, l, _, _) = outlines.(i) in
2100 if l != curlevel then i else flow (i+incr)
2102 let active = flow active in
2103 let first = calcfirst first active in
2104 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2105 Glut.postRedisplay ()
2107 match key with
2108 | Glut.KEY_UP -> navigate ~-1
2109 | Glut.KEY_DOWN -> navigate 1
2110 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2111 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2113 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
2114 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
2116 | Glut.KEY_HOME ->
2117 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
2118 Glut.postRedisplay ()
2120 | Glut.KEY_END ->
2121 let active = Array.length outlines - 1 in
2122 let first = max 0 (active - maxrows) in
2123 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2124 Glut.postRedisplay ()
2126 | _ -> ()
2129 let drawplaceholder l =
2130 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2131 GlDraw.rect
2132 (float l.pagex, float l.pagedispy)
2133 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2135 let x = float (if margin < 0 then -margin else l.pagex)
2136 and y = float (l.pagedispy + 13) in
2137 let font = Glut.BITMAP_8_BY_13 in
2138 GlDraw.color (0.0, 0.0, 0.0);
2139 GlPix.raster_pos ~x ~y ();
2140 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2141 ("Loading " ^ string_of_int (l.pageno + 1));
2144 let now () = Unix.gettimeofday ();;
2146 let drawpage l =
2147 let color =
2148 match state.mode with
2149 | Textentry _ -> scalecolor 0.4
2150 | View | Outline _ -> scalecolor 1.0
2151 | Birdseye (_, _, pageno, hooverpageno, _) ->
2152 if l.pageno = pageno
2153 then scalecolor 1.0
2154 else (
2155 if l.pageno = hooverpageno
2156 then scalecolor 0.9
2157 else scalecolor 0.8
2160 GlDraw.color color;
2161 begin match getopaque l.pageno with
2162 | Some (opaque, _) when validopaque opaque ->
2163 let a = now () in
2164 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2165 opaque;
2166 let b = now () in
2167 let d = b-.a in
2168 vlog "draw %d %f sec" l.pageno d;
2170 | _ ->
2171 drawplaceholder l;
2172 end;
2175 let scrollph y =
2176 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2177 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2178 let sh = float conf.winh /. sh in
2179 let sh = max sh (float conf.scrollh) in
2181 let percent =
2182 if state.y = state.maxy
2183 then 1.0
2184 else float y /. float maxy
2186 let position = (float conf.winh -. sh) *. percent in
2188 let position =
2189 if position +. sh > float conf.winh
2190 then float conf.winh -. sh
2191 else position
2193 position, sh;
2196 let scrollindicator () =
2197 GlDraw.color (0.64 , 0.64, 0.64);
2198 GlDraw.rect
2199 (float (conf.winw - conf.scrollw), 0.)
2200 (float conf.winw, float conf.winh)
2202 GlDraw.color (0.0, 0.0, 0.0);
2204 let position, sh = scrollph state.y in
2205 GlDraw.rect
2206 (float (conf.winw - conf.scrollw), position)
2207 (float conf.winw, position +. sh)
2211 let showsel margin =
2212 match state.mstate with
2213 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2216 | Msel ((x0, y0), (x1, y1)) ->
2217 let rec loop = function
2218 | l :: ls ->
2219 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2220 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2221 then
2222 match getopaque l.pageno with
2223 | Some (opaque, _) when validopaque opaque ->
2224 let oy = -l.pagey + l.pagedispy in
2225 seltext opaque
2226 (x0 - margin - state.x, y0,
2227 x1 - margin - state.x, y1) oy;
2229 | _ -> ()
2230 else loop ls
2231 | [] -> ()
2233 loop state.layout
2236 let showrects () =
2237 let panx = float state.x in
2238 Gl.enable `blend;
2239 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2240 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2241 List.iter
2242 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2243 List.iter (fun l ->
2244 if l.pageno = pageno
2245 then (
2246 let d = float (l.pagedispy - l.pagey) in
2247 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2248 GlDraw.begins `quads;
2250 GlDraw.vertex2 (x0+.panx, y0+.d);
2251 GlDraw.vertex2 (x1+.panx, y1+.d);
2252 GlDraw.vertex2 (x2+.panx, y2+.d);
2253 GlDraw.vertex2 (x3+.panx, y3+.d);
2255 GlDraw.ends ();
2257 ) state.layout
2258 ) state.rects
2260 Gl.disable `blend;
2263 let showoutline () =
2264 match state.mode with
2265 | Outline (allowdel, active, first, outlines, qsearch) ->
2266 Gl.enable `blend;
2267 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2268 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2269 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2270 Gl.disable `blend;
2272 GlDraw.color (1., 1., 1.);
2273 let font = Glut.BITMAP_9_BY_15 in
2274 let draw_string x y s =
2275 GlPix.raster_pos ~x ~y ();
2276 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2278 let rec loop row =
2279 if row = Array.length outlines || (row - first) * 16 > conf.winh
2280 then ()
2281 else (
2282 let (s, l, _, _) = outlines.(row) in
2283 let y = (row - first) * 16 in
2284 let x = 5 + 15*l in
2285 if row = active
2286 then (
2287 Gl.enable `blend;
2288 GlDraw.polygon_mode `both `line;
2289 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2290 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2291 GlDraw.rect (0., float (y + 1))
2292 (float (conf.winw - 1), float (y + 18));
2293 GlDraw.polygon_mode `both `fill;
2294 Gl.disable `blend;
2295 GlDraw.color (1., 1., 1.);
2297 draw_string (float x) (float (y + 16)) s;
2298 loop (row+1)
2301 loop first
2303 | _ -> ()
2306 let display () =
2307 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2308 GlDraw.viewport margin 0 state.w conf.winh;
2309 pagematrix ();
2310 GlClear.color (scalecolor 0.5);
2311 GlClear.clear [`color];
2312 if conf.zoom > 1.0
2313 then (
2314 Gl.enable `scissor_test;
2315 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2317 List.iter drawpage state.layout;
2318 if conf.zoom > 1.0
2319 then
2320 Gl.disable `scissor_test
2322 if state.x != 0
2323 then (
2324 let x = -.float state.x in
2325 GlMat.translate ~x ();
2327 showrects ();
2328 showsel margin;
2329 GlDraw.viewport 0 0 conf.winw conf.winh;
2330 winmatrix ();
2331 scrollindicator ();
2332 showoutline ();
2333 enttext ();
2334 Glut.swapBuffers ();
2337 let getunder x y =
2338 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2339 let x = x - margin - state.x in
2340 let rec f = function
2341 | l :: rest ->
2342 begin match getopaque l.pageno with
2343 | Some (opaque, _) when validopaque opaque ->
2344 let y = y - l.pagedispy in
2345 if y > 0
2346 then
2347 let y = l.pagey + y in
2348 let x = x - l.pagex in
2349 match whatsunder opaque x y with
2350 | Unone -> f rest
2351 | under -> under
2352 else
2353 f rest
2354 | _ ->
2355 f rest
2357 | [] -> Unone
2359 f state.layout
2362 let viewmouse button bstate x y =
2363 match button with
2364 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2365 if Glut.getModifiers () land Glut.active_ctrl != 0
2366 then (
2367 match state.mstate with
2368 | Mzoom (oldn, i) ->
2369 if oldn = n
2370 then (
2371 if i = 2
2372 then
2373 let incr =
2374 match n with
2375 | 4 ->
2376 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2377 | _ ->
2378 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2380 let zoom = conf.zoom +. incr in
2381 setzoom zoom;
2382 state.mstate <- Mzoom (n, 0);
2383 else
2384 state.mstate <- Mzoom (n, i+1);
2386 else state.mstate <- Mzoom (n, 0)
2388 | _ -> state.mstate <- Mzoom (n, 0)
2390 else (
2391 if state.ascrollstep > 0
2392 then
2393 setautoscrollspeed (n=4)
2394 else
2395 let incr =
2396 if n = 3
2397 then -conf.scrollstep
2398 else conf.scrollstep
2400 let incr = incr * 2 in
2401 let y = clamp incr in
2402 gotoy_and_clear_text y
2405 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2406 if bstate = Glut.DOWN
2407 then (
2408 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2409 state.mstate <- Mpan (x, y)
2411 else
2412 state.mstate <- Mnone
2414 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2415 if bstate = Glut.DOWN
2416 then
2417 let position, sh = scrollph state.y in
2418 if y > truncate position && y < truncate (position +. sh)
2419 then
2420 state.mstate <- Mscroll
2421 else
2422 let percent = float y /. float conf.winh in
2423 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2424 gotoy desty;
2425 state.mstate <- Mscroll
2426 else
2427 state.mstate <- Mnone
2429 | Glut.LEFT_BUTTON ->
2430 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2431 begin match dest with
2432 | Ulinkgoto (pageno, top) ->
2433 if pageno >= 0
2434 then (
2435 addnav ();
2436 gotopage1 pageno top;
2439 | Ulinkuri s ->
2440 print_endline s
2442 | Unone when bstate = Glut.DOWN ->
2443 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2444 state.mstate <- Mpan (x, y);
2446 | Unone | Utext _ ->
2447 if bstate = Glut.DOWN
2448 then (
2449 if conf.angle mod 360 = 0
2450 then (
2451 state.mstate <- Msel ((x, y), (x, y));
2452 Glut.postRedisplay ()
2455 else (
2456 match state.mstate with
2457 | Mnone -> ()
2459 | Mzoom _ | Mscroll ->
2460 state.mstate <- Mnone
2462 | Mpan _ ->
2463 Glut.setCursor Glut.CURSOR_INHERIT;
2464 state.mstate <- Mnone
2466 | Msel ((x0, y0), (x1, y1)) ->
2467 let f l =
2468 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2469 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2470 then
2471 match getopaque l.pageno with
2472 | Some (opaque, _) when validopaque opaque ->
2473 copysel opaque
2474 | _ -> ()
2476 List.iter f state.layout;
2477 copysel ""; (* ugly *)
2478 Glut.setCursor Glut.CURSOR_INHERIT;
2479 state.mstate <- Mnone;
2483 | _ -> ()
2486 let birdseyemouse button bstate x y
2487 (conf, leftx, pageno, hooverpageno, anchor) =
2488 match button with
2489 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2490 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2491 let rec loop = function
2492 | [] -> ()
2493 | l :: rest ->
2494 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2495 && x > margin && x < margin + l.pagew
2496 then (
2497 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
2499 else loop rest
2501 loop state.layout
2502 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2503 | _ -> ()
2506 let mouse bstate button x y =
2507 match state.mode with
2508 | View -> viewmouse button bstate x y
2509 | Birdseye beye -> birdseyemouse button bstate x y beye
2510 | Textentry _ -> ()
2511 | Outline _ -> ()
2514 let mouse ~button ~state ~x ~y = mouse state button x y;;
2516 let motion ~x ~y =
2517 match state.mode with
2518 | Outline _ -> ()
2519 | _ ->
2520 match state.mstate with
2521 | Mzoom _ | Mnone -> ()
2523 | Mpan (x0, y0) ->
2524 let dx = x - x0
2525 and dy = y0 - y in
2526 state.mstate <- Mpan (x, y);
2527 if conf.zoom > 1.0 then state.x <- state.x + dx;
2528 let y = clamp dy in
2529 gotoy_and_clear_text y
2531 | Msel (a, _) ->
2532 state.mstate <- Msel (a, (x, y));
2533 Glut.postRedisplay ()
2535 | Mscroll ->
2536 let y = min conf.winh (max 0 y) in
2537 let percent = float y /. float conf.winh in
2538 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2539 gotoy_and_clear_text y
2542 let pmotion ~x ~y =
2543 match state.mode with
2544 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2545 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2546 let rec loop = function
2547 | [] ->
2548 if hooverpageno != -1
2549 then (
2550 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2551 Glut.postRedisplay ();
2553 | l :: rest ->
2554 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2555 && x > margin && x < margin + l.pagew
2556 then (
2557 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2558 Glut.postRedisplay ();
2560 else loop rest
2562 loop state.layout
2564 | Outline _ -> ()
2565 | _ ->
2566 match state.mstate with
2567 | Mnone ->
2568 begin match getunder x y with
2569 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2570 | Ulinkuri uri ->
2571 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2572 Glut.setCursor Glut.CURSOR_INFO
2573 | Ulinkgoto (page, y) ->
2574 if conf.underinfo
2575 then showtext 'p' ("age: " ^ string_of_int page);
2576 Glut.setCursor Glut.CURSOR_INFO
2577 | Utext s ->
2578 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2579 Glut.setCursor Glut.CURSOR_TEXT
2582 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
2587 module State =
2588 struct
2589 open Parser
2591 let home =
2593 match Sys.os_type with
2594 | "Win32" -> Sys.getenv "HOMEPATH"
2595 | _ -> Sys.getenv "HOME"
2596 with exn ->
2597 prerr_endline
2598 ("Can not determine home directory location: " ^
2599 Printexc.to_string exn);
2603 let config_of c attrs =
2604 let apply c k v =
2606 match k with
2607 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2608 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2609 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2610 | "preload" -> { c with preload = bool_of_string v }
2611 | "page-bias" -> { c with pagebias = int_of_string v }
2612 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2613 | "auto-scroll-step" ->
2614 { c with autoscrollstep = max 0 (int_of_string v) }
2615 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2616 | "crop-hack" -> { c with crophack = bool_of_string v }
2617 | "throttle" -> { c with showall = bool_of_string v }
2618 | "highlight-links" -> { c with hlinks = bool_of_string v }
2619 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2620 | "vertical-margin" ->
2621 { c with interpagespace = max 0 (int_of_string v) }
2622 | "zoom" ->
2623 let zoom = float_of_string v /. 100. in
2624 let zoom = max 0.01 (min 2.2 zoom) in
2625 { c with zoom = zoom }
2626 | "presentation" -> { c with presentation = bool_of_string v }
2627 | "rotation-angle" -> { c with angle = int_of_string v }
2628 | "width" -> { c with winw = max 20 (int_of_string v) }
2629 | "height" -> { c with winh = max 20 (int_of_string v) }
2630 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2631 | "proportional-display" -> { c with proportional = bool_of_string v }
2632 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2633 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2634 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2635 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2636 | _ -> c
2637 with exn ->
2638 prerr_endline ("Error processing attribute (`" ^
2639 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2642 let rec fold c = function
2643 | [] -> c
2644 | (k, v) :: rest ->
2645 let c = apply c k v in
2646 fold c rest
2648 fold c attrs;
2651 let fromstring f pos n v d =
2652 try f v
2653 with exn ->
2654 dolog "Error processing attribute (%S=%S) at %d\n%s"
2655 n v pos (Printexc.to_string exn)
2660 let bookmark_of attrs =
2661 let rec fold title page rely = function
2662 | ("title", v) :: rest -> fold v page rely rest
2663 | ("page", v) :: rest -> fold title v rely rest
2664 | ("rely", v) :: rest -> fold title page v rest
2665 | _ :: rest -> fold title page rely rest
2666 | [] -> title, page, rely
2668 fold "invalid" "0" "0" attrs
2671 let doc_of attrs =
2672 let rec fold path page rely pan = function
2673 | ("path", v) :: rest -> fold v page rely pan rest
2674 | ("page", v) :: rest -> fold path v rely pan rest
2675 | ("rely", v) :: rest -> fold path page v pan rest
2676 | ("pan", v) :: rest -> fold path page rely v rest
2677 | _ :: rest -> fold path page rely pan rest
2678 | [] -> path, page, rely, pan
2680 fold "" "0" "0" "0" attrs
2683 let setconf dst src =
2684 dst.scrollw <- src.scrollw;
2685 dst.scrollh <- src.scrollh;
2686 dst.icase <- src.icase;
2687 dst.preload <- src.preload;
2688 dst.pagebias <- src.pagebias;
2689 dst.verbose <- src.verbose;
2690 dst.scrollstep <- src.scrollstep;
2691 dst.maxhfit <- src.maxhfit;
2692 dst.crophack <- src.crophack;
2693 dst.autoscrollstep <- src.autoscrollstep;
2694 dst.showall <- src.showall;
2695 dst.hlinks <- src.hlinks;
2696 dst.underinfo <- src.underinfo;
2697 dst.interpagespace <- src.interpagespace;
2698 dst.zoom <- src.zoom;
2699 dst.presentation <- src.presentation;
2700 dst.angle <- src.angle;
2701 dst.winw <- src.winw;
2702 dst.winh <- src.winh;
2703 dst.savebmarks <- src.savebmarks;
2704 dst.memlimit <- src.memlimit;
2705 dst.proportional <- src.proportional;
2706 dst.texcount <- src.texcount;
2707 dst.sliceheight <- src.sliceheight;
2708 dst.thumbw <- src.thumbw;
2711 let unent s =
2712 let l = String.length s in
2713 let b = Buffer.create l in
2714 unent b s 0 l;
2715 Buffer.contents b;
2718 let get s =
2719 let h = Hashtbl.create 10 in
2720 let dc = { defconf with angle = defconf.angle } in
2721 let rec toplevel v t spos epos =
2722 match t with
2723 | Vdata | Vcdata | Vend -> v
2724 | Vopen ("llppconfig", attrs, closed) ->
2725 if closed
2726 then v
2727 else { v with f = llppconfig }
2728 | Vopen _ ->
2729 error "unexpected subelement at top level" s spos
2730 | Vclose tag -> error "unexpected close at top level" s spos
2732 and llppconfig v t spos epos =
2733 match t with
2734 | Vdata | Vcdata | Vend -> v
2735 | Vopen ("defaults", attrs, closed) ->
2736 let c = config_of dc attrs in
2737 setconf dc c;
2738 if closed
2739 then v
2740 else { v with f = skip "defaults" (fun () -> v) }
2742 | Vopen ("doc", attrs, closed) ->
2743 let pathent, spage, srely, span = doc_of attrs in
2744 let path = unent pathent
2745 and pageno = fromstring int_of_string spos "page" spage 0
2746 and rely = fromstring float_of_string spos "rely" srely 0.0
2747 and pan = fromstring int_of_string spos "pan" span 0 in
2748 let c = config_of dc attrs in
2749 let anchor = (pageno, rely) in
2750 if closed
2751 then (Hashtbl.add h path (c, [], pan, anchor); v)
2752 else { v with f = doc path pan anchor c [] }
2754 | Vopen (tag, _, closed) ->
2755 error "unexpected subelement in llppconfig" s spos
2757 | Vclose "llppconfig" -> { v with f = toplevel }
2758 | Vclose tag -> error "unexpected close in llppconfig" s spos
2760 and doc path pan anchor c bookmarks v t spos epos =
2761 match t with
2762 | Vdata | Vcdata -> v
2763 | Vend -> error "unexpected end of input in doc" s spos
2764 | Vopen ("bookmarks", attrs, closed) ->
2765 { v with f = pbookmarks path pan anchor c bookmarks }
2767 | Vopen (tag, _, _) ->
2768 error "unexpected subelement in doc" s spos
2770 | Vclose "doc" ->
2771 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2772 { v with f = llppconfig }
2774 | Vclose tag -> error "unexpected close in doc" s spos
2776 and pbookmarks path pan anchor c bookmarks v t spos epos =
2777 match t with
2778 | Vdata | Vcdata -> v
2779 | Vend -> error "unexpected end of input in bookmarks" s spos
2780 | Vopen ("item", attrs, closed) ->
2781 let titleent, spage, srely = bookmark_of attrs in
2782 let page = fromstring int_of_string spos "page" spage 0
2783 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2784 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2785 if closed
2786 then { v with f = pbookmarks path pan anchor c bookmarks }
2787 else
2788 let f () = v in
2789 { v with f = skip "item" f }
2791 | Vopen _ ->
2792 error "unexpected subelement in bookmarks" s spos
2794 | Vclose "bookmarks" ->
2795 { v with f = doc path pan anchor c bookmarks }
2797 | Vclose tag -> error "unexpected close in bookmarks" s spos
2799 and skip tag f v t spos epos =
2800 match t with
2801 | Vdata | Vcdata -> v
2802 | Vend ->
2803 error ("unexpected end of input in skipped " ^ tag) s spos
2804 | Vopen (tag', _, closed) ->
2805 if closed
2806 then v
2807 else
2808 let f' () = { v with f = skip tag f } in
2809 { v with f = skip tag' f' }
2810 | Vclose ctag ->
2811 if tag = ctag
2812 then f ()
2813 else error ("unexpected close in skipped " ^ tag) s spos
2816 parse { f = toplevel; accu = () } s;
2817 h, dc;
2820 let do_load f ic =
2822 let len = in_channel_length ic in
2823 let s = String.create len in
2824 really_input ic s 0 len;
2825 f s;
2826 with
2827 | Parse_error (msg, s, pos) ->
2828 let subs = subs s pos in
2829 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2830 failwith ("parse error: " ^ s)
2832 | exn ->
2833 failwith ("config load error: " ^ Printexc.to_string exn)
2836 let path =
2837 let dir =
2839 let dir = Filename.concat home ".config" in
2840 if Sys.is_directory dir then dir else home
2841 with _ -> home
2843 Filename.concat dir "llpp.conf"
2846 let load1 f =
2847 if Sys.file_exists path
2848 then
2849 match
2850 (try Some (open_in_bin path)
2851 with exn ->
2852 prerr_endline
2853 ("Error opening configuation file `" ^ path ^ "': " ^
2854 Printexc.to_string exn);
2855 None
2857 with
2858 | Some ic ->
2859 begin try
2860 f (do_load get ic)
2861 with exn ->
2862 prerr_endline
2863 ("Error loading configuation from `" ^ path ^ "': " ^
2864 Printexc.to_string exn);
2865 end;
2866 close_in ic;
2868 | None -> ()
2869 else
2870 f (Hashtbl.create 0, defconf)
2873 let load () =
2874 let f (h, dc) =
2875 let pc, pb, px, pa =
2877 Hashtbl.find h (Filename.basename state.path)
2878 with Not_found -> dc, [], 0, (0, 0.0)
2880 setconf defconf dc;
2881 setconf conf pc;
2882 state.bookmarks <- pb;
2883 state.x <- px;
2884 cbput state.hists.nav pa;
2886 load1 f
2889 let add_attrs bb always dc c =
2890 let ob s a b =
2891 if always || a != b
2892 then Printf.bprintf bb "\n %s='%b'" s a
2893 and oi s a b =
2894 if always || a != b
2895 then Printf.bprintf bb "\n %s='%d'" s a
2896 and oz s a b =
2897 if always || a <> b
2898 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2900 let w, h =
2901 if always
2902 then dc.winw, dc.winh
2903 else
2904 match state.fullscreen with
2905 | Some wh -> wh
2906 | None -> c.winw, c.winh
2908 let zoom, presentation, interpagespace, showall=
2909 if always
2910 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2911 else
2912 match state.mode with
2913 | Birdseye (bc, _, _, _, _) ->
2914 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2915 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2917 oi "width" w dc.winw;
2918 oi "height" h dc.winh;
2919 oi "scroll-bar-width" c.scrollw dc.scrollw;
2920 oi "scroll-handle-height" c.scrollh dc.scrollh;
2921 ob "case-insensitive-search" c.icase dc.icase;
2922 ob "preload" c.preload dc.preload;
2923 oi "page-bias" c.pagebias dc.pagebias;
2924 oi "scroll-step" c.scrollstep dc.scrollstep;
2925 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
2926 ob "max-height-fit" c.maxhfit dc.maxhfit;
2927 ob "crop-hack" c.crophack dc.crophack;
2928 ob "throttle" showall dc.showall;
2929 ob "highlight-links" c.hlinks dc.hlinks;
2930 ob "under-cursor-info" c.underinfo dc.underinfo;
2931 oi "vertical-margin" interpagespace dc.interpagespace;
2932 oz "zoom" zoom dc.zoom;
2933 ob "presentation" presentation dc.presentation;
2934 oi "rotation-angle" c.angle dc.angle;
2935 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2936 ob "proportional-display" c.proportional dc.proportional;
2937 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2938 oi "texcount" c.texcount dc.texcount;
2939 oi "slice-height" c.sliceheight dc.sliceheight;
2940 oi "thumbnail-width" c.thumbw dc.thumbw;
2943 let save () =
2944 let bb = Buffer.create 32768 in
2945 let f (h, dc) =
2946 Buffer.add_string bb "<llppconfig>\n<defaults ";
2947 add_attrs bb true dc dc;
2948 Buffer.add_string bb "/>\n";
2950 let adddoc path pan anchor c bookmarks =
2951 if bookmarks == [] && c = dc && anchor = emptyanchor
2952 then ()
2953 else (
2954 Printf.bprintf bb "<doc path='%s'"
2955 (enent path 0 (String.length path));
2957 if anchor <> emptyanchor
2958 then (
2959 let n, y = anchor in
2960 Printf.bprintf bb " page='%d'" n;
2961 Printf.bprintf bb " rely='%f'" y;
2964 if pan != 0
2965 then Printf.bprintf bb " pan='%d'" pan;
2967 add_attrs bb false dc c;
2969 begin match bookmarks with
2970 | [] -> Buffer.add_string bb "/>\n"
2971 | _ ->
2972 Buffer.add_string bb ">\n<bookmarks>\n";
2973 List.iter (fun (title, _level, page, rely) ->
2974 Printf.bprintf bb
2975 "<item title='%s' page='%d' rely='%f'/>\n"
2976 (enent title 0 (String.length title))
2977 page
2978 rely
2979 ) bookmarks;
2980 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2981 end;
2985 let pan =
2986 match state.mode with
2987 | Birdseye (_, pan, _, _, _) -> pan
2988 | _ -> state.x
2990 let basename = Filename.basename state.path in
2991 adddoc basename pan (getanchor ())
2992 { conf with
2993 autoscrollstep =
2994 if state.ascrollstep > 0
2995 then state.ascrollstep
2996 else conf.autoscrollstep }
2997 (if conf.savebmarks then state.bookmarks else []);
2999 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3000 if basename <> path
3001 then adddoc path x y c bookmarks
3002 ) h;
3003 Buffer.add_string bb "</llppconfig>";
3005 load1 f;
3006 if Buffer.length bb > 0
3007 then
3009 let tmp = path ^ ".tmp" in
3010 let oc = open_out_bin tmp in
3011 Buffer.output_buffer oc bb;
3012 close_out oc;
3013 Sys.rename tmp path;
3014 with exn ->
3015 prerr_endline
3016 ("error while saving configuration: " ^ Printexc.to_string exn)
3018 end;;
3020 let () =
3021 Arg.parse
3022 ["-p", Arg.String (fun s -> state.password <- s) , "password"
3023 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3024 "print version")]
3025 (fun s -> state.path <- s)
3026 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
3028 if String.length state.path = 0
3029 then (prerr_endline "filename missing"; exit 1);
3031 State.load ();
3033 let _ = Glut.init Sys.argv in
3034 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3035 let () = Glut.initWindowSize conf.winw conf.winh in
3036 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3038 let csock, ssock =
3039 if Sys.os_type = "Unix"
3040 then
3041 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3042 else
3043 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3044 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3045 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3046 Unix.bind sock addr;
3047 Unix.listen sock 1;
3048 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3049 Unix.connect csock addr;
3050 let ssock, _ = Unix.accept sock in
3051 Unix.close sock;
3052 let opts sock =
3053 Unix.setsockopt sock Unix.TCP_NODELAY true;
3054 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3056 opts ssock;
3057 opts csock;
3058 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3059 ssock, csock
3062 let () = Glut.displayFunc display in
3063 let () = Glut.reshapeFunc reshape in
3064 let () = Glut.keyboardFunc keyboard in
3065 let () = Glut.specialFunc special in
3066 let () = Glut.idleFunc (Some idle) in
3067 let () = Glut.mouseFunc mouse in
3068 let () = Glut.motionFunc motion in
3069 let () = Glut.passiveMotionFunc pmotion in
3071 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3072 state.csock <- csock;
3073 state.ssock <- ssock;
3074 state.text <- "Opening " ^ state.path;
3075 writeopen state.path state.password;
3077 at_exit State.save;
3079 let rec handlelablglutbug () =
3081 Glut.mainLoop ();
3082 with Glut.BadEnum "key in special_of_int" ->
3083 showtext '!' " LablGlut bug: special key not recognized";
3084 handlelablglutbug ()
3086 handlelablglutbug ();