Allow setting zoom level and auto scroll step from keyboard
[llpp.git] / main.ml
blob21169076b27aa04a43a5ea11721898f08c3f6fd6
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 | Mnone
44 type textentry = (char * string * onhist * onkey * ondone)
45 and onkey = string -> int -> te
46 and ondone = string -> unit
47 and histcancel = unit -> unit
48 and onhist = ((histcmd -> string) * histcancel) option
49 and histcmd = HCnext | HCprev | HCfirst | HClast
50 and te =
51 | TEstop
52 | TEdone of string
53 | TEcont of string
54 | TEswitch of textentry
57 type 'a circbuf =
58 { store : 'a array
59 ; mutable rc : int
60 ; mutable wc : int
61 ; mutable len : int
65 let cbnew n v =
66 { store = Array.create n v
67 ; rc = 0
68 ; wc = 0
69 ; len = 0
73 let cbcap b = Array.length b.store;;
75 let cbput b v =
76 let cap = cbcap b in
77 b.store.(b.wc) <- v;
78 b.wc <- (b.wc + 1) mod cap;
79 b.rc <- b.wc;
80 b.len <- min (b.len + 1) cap;
83 let cbempty b = b.len = 0;;
85 let cbgetg b circular dir =
86 if cbempty b
87 then b.store.(0)
88 else
89 let rc = b.rc + dir in
90 let rc =
91 if circular
92 then (
93 if rc = -1
94 then b.len-1
95 else (
96 if rc = b.len
97 then 0
98 else rc
101 else max 0 (min rc (b.len-1))
103 b.rc <- rc;
104 b.store.(rc);
107 let cbget b = cbgetg b false;;
108 let cbgetc b = cbgetg b true;;
110 let cbpeek b =
111 let rc = b.wc - b.len in
112 let rc = if rc < 0 then cbcap b + rc else rc in
113 b.store.(rc);
116 let cbdecr b = b.len <- b.len - 1;;
118 type layout =
119 { pageno : int
120 ; pagedimno : int
121 ; pagew : int
122 ; pageh : int
123 ; pagedispy : int
124 ; pagey : int
125 ; pagevh : int
126 ; pagex : int
130 type conf =
131 { mutable scrollw : int
132 ; mutable scrollh : int
133 ; mutable icase : bool
134 ; mutable preload : bool
135 ; mutable pagebias : int
136 ; mutable verbose : bool
137 ; mutable scrollstep : int
138 ; mutable maxhfit : bool
139 ; mutable crophack : bool
140 ; mutable autoscrollstep : int
141 ; mutable showall : bool
142 ; mutable hlinks : bool
143 ; mutable underinfo : bool
144 ; mutable interpagespace : interpagespace
145 ; mutable zoom : float
146 ; mutable presentation : bool
147 ; mutable angle : angle
148 ; mutable winw : int
149 ; mutable winh : int
150 ; mutable savebmarks : bool
151 ; mutable proportional : proportional
152 ; mutable memlimit : int
153 ; mutable texcount : texcount
154 ; mutable sliceheight : sliceheight
155 ; mutable thumbw : width
159 type outline = string * int * int * float;;
160 type outlines =
161 | Oarray of outline array
162 | Olist of outline list
163 | Onarrow of string * outline array * outline array
166 type rect = (float * float * float * float * float * float * float * float);;
168 type pagemapkey = (pageno * width * angle * proportional * gen);;
170 type anchor = pageno * top;;
172 type mode =
173 | Birdseye of (conf * leftx * pageno * pageno * anchor)
174 | Outline of (bool * int * int * outline array * string)
175 | Textentry of (textentry * mode)
176 | View
179 let isbirdseye = function Birdseye _ -> true | _ -> false;;
180 let istextentry = function Textentry _ -> true | _ -> false;;
182 type state =
183 { mutable csock : Unix.file_descr
184 ; mutable ssock : Unix.file_descr
185 ; mutable w : int
186 ; mutable x : int
187 ; mutable y : int
188 ; mutable anchor : anchor
189 ; mutable maxy : int
190 ; mutable layout : layout list
191 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
192 ; mutable pdims : (pageno * width * height * leftx) list
193 ; mutable pagecount : int
194 ; pagecache : string circbuf
195 ; mutable rendering : bool
196 ; mutable mstate : mstate
197 ; mutable searchpattern : string
198 ; mutable rects : (pageno * recttype * rect) list
199 ; mutable rects1 : (pageno * recttype * rect) list
200 ; mutable text : string
201 ; mutable fullscreen : (width * height) option
202 ; mutable mode : mode
203 ; mutable outlines : outlines
204 ; mutable bookmarks : outline list
205 ; mutable path : string
206 ; mutable password : string
207 ; mutable invalidated : int
208 ; mutable colorscale : float
209 ; mutable memused : int
210 ; mutable gen : gen
211 ; mutable throttle : layout list option
212 ; mutable ascrollstep : int
213 ; hists : hists
215 and hists =
216 { pat : string circbuf
217 ; pag : string circbuf
218 ; nav : anchor circbuf
222 let defconf =
223 { scrollw = 7
224 ; scrollh = 12
225 ; icase = true
226 ; preload = true
227 ; pagebias = 0
228 ; verbose = false
229 ; scrollstep = 24
230 ; maxhfit = true
231 ; crophack = false
232 ; autoscrollstep = 24
233 ; showall = false
234 ; hlinks = false
235 ; underinfo = false
236 ; interpagespace = 2
237 ; zoom = 1.0
238 ; presentation = false
239 ; angle = 0
240 ; winw = 900
241 ; winh = 900
242 ; savebmarks = true
243 ; proportional = true
244 ; memlimit = 32*1024*1024
245 ; texcount = 256
246 ; sliceheight = 24
247 ; thumbw = 76
251 let conf = { defconf with angle = defconf.angle };;
253 let state =
254 { csock = Unix.stdin
255 ; ssock = Unix.stdin
256 ; x = 0
257 ; y = 0
258 ; anchor = (0, 0.0)
259 ; w = 0
260 ; layout = []
261 ; maxy = max_int
262 ; pagemap = Hashtbl.create 10
263 ; pagecache = cbnew 100 ""
264 ; pdims = []
265 ; pagecount = 0
266 ; rendering = false
267 ; mstate = Mnone
268 ; rects = []
269 ; rects1 = []
270 ; text = ""
271 ; mode = View
272 ; fullscreen = None
273 ; searchpattern = ""
274 ; outlines = Olist []
275 ; bookmarks = []
276 ; path = ""
277 ; password = ""
278 ; invalidated = 0
279 ; hists =
280 { nav = cbnew 100 (0, 0.0)
281 ; pat = cbnew 20 ""
282 ; pag = cbnew 10 ""
284 ; colorscale = 1.0
285 ; memused = 0
286 ; gen = 0
287 ; throttle = None
288 ; ascrollstep = 0
292 let vlog fmt =
293 if conf.verbose
294 then
295 Printf.kprintf prerr_endline fmt
296 else
297 Printf.kprintf ignore fmt
300 let writecmd fd s =
301 let len = String.length s in
302 let n = 4 + len in
303 let b = Buffer.create n in
304 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
305 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
306 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
307 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
308 Buffer.add_string b s;
309 let s' = Buffer.contents b in
310 let n' = Unix.write fd s' 0 n in
311 if n' != n then failwith "write failed";
314 let readcmd fd =
315 let s = "xxxx" in
316 let n = Unix.read fd s 0 4 in
317 if n != 4 then failwith "incomplete read(len)";
318 let len = 0
319 lor (Char.code s.[0] lsl 24)
320 lor (Char.code s.[1] lsl 16)
321 lor (Char.code s.[2] lsl 8)
322 lor (Char.code s.[3] lsl 0)
324 let s = String.create len in
325 let n = Unix.read fd s 0 len in
326 if n != len then failwith "incomplete read(data)";
330 let makecmd s l =
331 let b = Buffer.create 10 in
332 Buffer.add_string b s;
333 let rec combine = function
334 | [] -> b
335 | x :: xs ->
336 Buffer.add_char b ' ';
337 let s =
338 match x with
339 | `b b -> if b then "1" else "0"
340 | `s s -> s
341 | `i i -> string_of_int i
342 | `f f -> string_of_float f
343 | `I f -> string_of_int (truncate f)
345 Buffer.add_string b s;
346 combine xs;
348 combine l;
351 let wcmd s l =
352 let cmd = Buffer.contents (makecmd s l) in
353 writecmd state.csock cmd;
356 let calcips h =
357 if conf.presentation
358 then
359 let d = conf.winh - h in
360 max 0 ((d + 1) / 2)
361 else
362 conf.interpagespace
365 let calcheight () =
366 let rec f pn ph pi fh l =
367 match l with
368 | (n, _, h, _) :: rest ->
369 let ips = calcips h in
370 let fh =
371 if conf.presentation
372 then fh+ips
373 else (
374 if isbirdseye state.mode && pn = 0
375 then fh + ips
376 else fh
379 let fh = fh + ((n - pn) * (ph + pi)) in
380 f n h ips fh rest;
382 | [] ->
383 let inc =
384 if conf.presentation || (isbirdseye state.mode && pn = 0)
385 then 0
386 else -pi
388 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
389 max 0 fh
391 let fh = f 0 0 0 0 state.pdims in
395 let getpageyh pageno =
396 let rec f pn ph pi y l =
397 match l with
398 | (n, _, h, _) :: rest ->
399 let ips = calcips h in
400 if n >= pageno
401 then
402 let h = if n = pageno then h else ph in
403 if conf.presentation && n = pageno
404 then
405 y + (pageno - pn) * (ph + pi) + pi, h
406 else
407 y + (pageno - pn) * (ph + pi), h
408 else
409 let y = y + (if conf.presentation then pi else 0) in
410 let y = y + (n - pn) * (ph + pi) in
411 f n h ips y rest
413 | [] ->
414 y + (pageno - pn) * (ph + pi), ph
416 f 0 0 0 0 state.pdims
419 let getpagey pageno = fst (getpageyh pageno);;
421 let layout y sh =
422 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
423 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
424 match pdims with
425 | (pageno', w, h, x) :: rest when pageno' = pageno ->
426 let ips = calcips h in
427 let yinc =
428 if conf.presentation || (isbirdseye state.mode && pageno = 0)
429 then ips
430 else 0
432 (w, h, ips, x), rest, pdimno + 1, yinc
433 | _ ->
434 prev, pdims, pdimno, 0
436 let dy = dy + yinc in
437 let py = py + yinc in
438 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
439 then
440 accu
441 else
442 let vy = y + dy in
443 if py + h <= vy - yinc
444 then
445 let py = py + h + ips in
446 let dy = max 0 (py - y) in
447 f ~pageno:(pageno+1)
448 ~pdimno
449 ~prev:curr
452 ~pdims:rest
453 ~cacheleft
454 ~accu
455 else
456 let pagey = vy - py in
457 let pagevh = h - pagey in
458 let pagevh = min (sh - dy) pagevh in
459 let off = if yinc > 0 then py - vy else 0 in
460 let py = py + h + ips in
461 let e =
462 { pageno = pageno
463 ; pagedimno = pdimno
464 ; pagew = w
465 ; pageh = h
466 ; pagedispy = dy + off
467 ; pagey = pagey + off
468 ; pagevh = pagevh - off
469 ; pagex = x
472 let accu = e :: accu in
473 f ~pageno:(pageno+1)
474 ~pdimno
475 ~prev:curr
477 ~dy:(dy+pagevh+ips)
478 ~pdims:rest
479 ~cacheleft:(cacheleft-1)
480 ~accu
482 if state.invalidated = 0
483 then (
484 let accu =
486 ~pageno:0
487 ~pdimno:~-1
488 ~prev:(0,0,0,0)
489 ~py:0
490 ~dy:0
491 ~pdims:state.pdims
492 ~cacheleft:(cbcap state.pagecache)
493 ~accu:[]
495 List.rev accu
497 else
501 let clamp incr =
502 let y = state.y + incr in
503 let y = max 0 y in
504 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
508 let getopaque pageno =
509 try Some (Hashtbl.find state.pagemap
510 (pageno, state.w, conf.angle, conf.proportional, state.gen))
511 with Not_found -> None
514 let cache pageno opaque =
515 Hashtbl.replace state.pagemap
516 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
519 let validopaque opaque = String.length opaque > 0;;
521 let render l =
522 match getopaque l.pageno with
523 | None when not state.rendering ->
524 state.rendering <- true;
525 cache l.pageno ("", -1);
526 wcmd "render" [`i (l.pageno + 1)
527 ;`i l.pagedimno
528 ;`i l.pagew
529 ;`i l.pageh];
530 | _ -> ()
533 let loadlayout layout =
534 let rec f all = function
535 | l :: ls ->
536 begin match getopaque l.pageno with
537 | None -> render l; f false ls
538 | Some (opaque, _) -> f (all && validopaque opaque) ls
540 | [] -> all
542 f (layout <> []) layout;
545 let findpageforopaque opaque =
546 Hashtbl.fold
547 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
548 state.pagemap None
551 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
553 let preload () =
554 let oktopreload =
555 if conf.preload
556 then
557 let memleft = conf.memlimit - state.memused in
558 if memleft < 0
559 then
560 let opaque = cbpeek state.pagecache in
561 match findpageforopaque opaque with
562 | Some ((n, _, _, _, _), size) ->
563 memleft + size >= 0 && not (pagevisible state.layout n)
564 | None -> false
565 else true
566 else false
568 if oktopreload
569 then
570 let presentation = conf.presentation in
571 let interpagespace = conf.interpagespace in
572 let maxy = state.maxy in
573 conf.presentation <- false;
574 conf.interpagespace <- 0;
575 state.maxy <- calcheight ();
576 let y =
577 match state.layout with
578 | [] -> 0
579 | l :: _ -> getpagey l.pageno + l.pagey
581 let y = if y < conf.winh then 0 else y - conf.winh in
582 let pages = layout y (conf.winh*3) in
583 List.iter render pages;
584 conf.presentation <- presentation;
585 conf.interpagespace <- interpagespace;
586 state.maxy <- maxy;
589 let gotoy y =
590 let y = max 0 y in
591 let y = min state.maxy y in
592 let pages = layout y conf.winh in
593 let ready = loadlayout pages in
594 if conf.showall
595 then (
596 if ready
597 then (
598 state.y <- y;
599 state.layout <- pages;
600 state.throttle <- None;
601 Glut.postRedisplay ();
603 else (
604 state.throttle <- Some pages;
607 else (
608 state.y <- y;
609 state.layout <- pages;
610 state.throttle <- None;
611 Glut.postRedisplay ();
613 begin match state.mode with
614 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
615 if not (pagevisible pages pageno)
616 then (
617 match state.layout with
618 | [] -> ()
619 | l :: _ ->
620 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
622 | _ -> ()
623 end;
624 preload ();
627 let gotoy_and_clear_text y =
628 gotoy y;
629 if not conf.verbose then state.text <- "";
632 let emptyanchor = (0, 0.0);;
634 let getanchor () =
635 match state.layout with
636 | [] -> emptyanchor
637 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
640 let getanchory (n, top) =
641 let y, h = getpageyh n in
642 y + (truncate (top *. float h));
645 let gotoanchor anchor =
646 gotoy (getanchory anchor);
649 let addnav () =
650 cbput state.hists.nav (getanchor ());
653 let getnav () =
654 let anchor = cbgetc state.hists.nav ~-1 in
655 getanchory anchor;
658 let gotopage n top =
659 let y, h = getpageyh n in
660 gotoy_and_clear_text (y + (truncate (top *. float h)));
663 let gotopage1 n top =
664 let y = getpagey n in
665 gotoy_and_clear_text (y + top);
668 let invalidate () =
669 state.layout <- [];
670 state.pdims <- [];
671 state.rects <- [];
672 state.rects1 <- [];
673 state.invalidated <- state.invalidated + 1;
676 let scalecolor c =
677 let c = c *. state.colorscale in
678 (c, c, c);
681 let represent () =
682 state.maxy <- calcheight ();
683 match state.mode with
684 | Birdseye (_, _, pageno, _, _) ->
685 let y, h = getpageyh pageno in
686 let top = (conf.winh - h) / 2 in
687 gotoy (max 0 (y - top))
688 | _ -> gotoanchor state.anchor
691 let pagematrix () =
692 GlMat.mode `projection;
693 GlMat.load_identity ();
694 GlMat.rotate ~x:1.0 ~angle:180.0 ();
695 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
696 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
697 if state.x != 0
698 then (
699 GlMat.translate ~x:(float state.x) ();
703 let winmatrix () =
704 GlMat.mode `projection;
705 GlMat.load_identity ();
706 GlMat.rotate ~x:1.0 ~angle:180.0 ();
707 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
708 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
711 let reshape ~w ~h =
712 if state.invalidated = 0
713 then state.anchor <- getanchor ();
715 conf.winw <- w;
716 let w = truncate (float w *. conf.zoom) - conf.scrollw in
717 let w = max w 2 in
718 state.w <- w;
719 conf.winh <- h;
720 GlMat.mode `modelview;
721 GlMat.load_identity ();
722 GlClear.color (scalecolor 1.0);
723 GlClear.clear [`color];
725 invalidate ();
726 wcmd "geometry" [`i w; `i h];
729 let showtext c s =
730 GlDraw.color (0.0, 0.0, 0.0);
731 GlDraw.rect
732 (0.0, float (conf.winh - 18))
733 (float (conf.winw - conf.scrollw - 1), float conf.winh)
735 let font = Glut.BITMAP_8_BY_13 in
736 GlDraw.color (1.0, 1.0, 1.0);
737 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
738 Glut.bitmapCharacter ~font ~c:(Char.code c);
739 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
742 let enttext () =
743 let len = String.length state.text in
744 match state.mode with
745 | Textentry ((c, text, _, _, _), _) ->
746 let s =
747 if len > 0
748 then
749 text ^ " [" ^ state.text ^ "]"
750 else
751 text
753 showtext c s;
755 | _ ->
756 if len > 0 then showtext ' ' state.text
759 let showtext c s =
760 state.text <- Printf.sprintf "%c%s" c s;
761 Glut.postRedisplay ();
764 let act cmd =
765 match cmd.[0] with
766 | 'c' ->
767 state.pdims <- [];
769 | 'D' ->
770 state.rects <- state.rects1;
771 Glut.postRedisplay ()
773 | 'C' ->
774 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
775 state.pagecount <- n;
776 state.invalidated <- state.invalidated - 1;
777 if state.invalidated = 0
778 then represent ()
780 | 't' ->
781 let s = Scanf.sscanf cmd "t %n"
782 (fun n -> String.sub cmd n (String.length cmd - n))
784 Glut.setWindowTitle s
786 | 'T' ->
787 let s = Scanf.sscanf cmd "T %n"
788 (fun n -> String.sub cmd n (String.length cmd - n))
790 if istextentry state.mode
791 then (
792 state.text <- s;
793 showtext ' ' s;
795 else (
796 state.text <- s;
797 Glut.postRedisplay ();
800 | 'V' ->
801 if conf.verbose
802 then
803 let s = Scanf.sscanf cmd "V %n"
804 (fun n -> String.sub cmd n (String.length cmd - n))
806 state.text <- s;
807 showtext ' ' s;
809 | 'F' ->
810 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
811 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
812 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
813 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
815 let y = (getpagey pageno) + truncate y0 in
816 addnav ();
817 gotoy y;
818 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
820 | 'R' ->
821 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
822 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
823 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
824 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
826 state.rects1 <-
827 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
829 | 'r' ->
830 let n, w, h, r, l, s, p =
831 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
832 (fun n w h r l s p ->
833 (n-1, w, h, r, l != 0, s, p))
836 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
837 state.memused <- state.memused + s;
839 let layout =
840 match state.throttle with
841 | None -> state.layout
842 | Some layout -> layout
845 let rec gc () =
846 if (state.memused <= conf.memlimit) || cbempty state.pagecache
847 then ()
848 else (
849 let evictedopaque = cbpeek state.pagecache in
850 match findpageforopaque evictedopaque with
851 | None -> failwith "bug in gc"
852 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
853 if state.gen != gen || not (pagevisible layout evictedn)
854 then (
855 wcmd "free" [`s evictedopaque];
856 state.memused <- state.memused - evictedsize;
857 Hashtbl.remove state.pagemap k;
858 cbdecr state.pagecache;
859 gc ();
863 gc ();
865 cbput state.pagecache p;
866 state.rendering <- false;
868 begin match state.throttle with
869 | None ->
870 if pagevisible state.layout n
871 then gotoy state.y
872 else (
873 let allvisible = loadlayout state.layout in
874 if allvisible then preload ();
877 | Some layout ->
878 match layout with
879 | [] -> ()
880 | l :: _ ->
881 let y = getpagey l.pageno + l.pagey in
882 gotoy y
885 | 'l' ->
886 let (n, w, h, x) as pdim =
887 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
889 state.pdims <- pdim :: state.pdims
891 | 'o' ->
892 let (l, n, t, h, pos) =
893 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
895 let s = String.sub cmd pos (String.length cmd - pos) in
896 let s =
897 let l = String.length s in
898 let b = Buffer.create (String.length s) in
899 let rec loop pc2 i =
900 if i = l
901 then ()
902 else
903 let pc2 =
904 match s.[i] with
905 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
906 | '\xc2' -> true
907 | c ->
908 let c = if Char.code c land 0x80 = 0 then c else '?' in
909 Buffer.add_char b c;
910 false
912 loop pc2 (i+1)
914 loop false 0;
915 Buffer.contents b
917 let outline = (s, l, n, float t /. float h) in
918 let outlines =
919 match state.outlines with
920 | Olist outlines -> Olist (outline :: outlines)
921 | Oarray _ -> Olist [outline]
922 | Onarrow _ -> Olist [outline]
924 state.outlines <- outlines
926 | _ ->
927 dolog "unknown cmd `%S'" cmd
930 let now = Unix.gettimeofday;;
932 let idle () =
933 let rec loop delay =
934 let r, _, _ = Unix.select [state.csock] [] [] delay in
935 begin match r with
936 | [] ->
937 if state.ascrollstep > 0
938 then begin
939 let y = state.y + state.ascrollstep in
940 let y = if y >= state.maxy then 0 else y in
941 gotoy y;
942 state.text <- "";
943 end;
945 | _ ->
946 let cmd = readcmd state.csock in
947 act cmd;
948 loop 0.0
949 end;
950 in loop 0.001
953 let onhist cb =
954 let rc = cb.rc in
955 let action = function
956 | HCprev -> cbget cb ~-1
957 | HCnext -> cbget cb 1
958 | HCfirst -> cbget cb ~-(cb.rc)
959 | HClast -> cbget cb (cb.len - 1 - cb.rc)
960 and cancel () = cb.rc <- rc
961 in (action, cancel)
964 let search pattern forward =
965 if String.length pattern > 0
966 then
967 let pn, py =
968 match state.layout with
969 | [] -> 0, 0
970 | l :: _ ->
971 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
973 let cmd =
974 let b = makecmd "search"
975 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
977 Buffer.add_char b ',';
978 Buffer.add_string b pattern;
979 Buffer.add_char b '\000';
980 Buffer.contents b;
982 writecmd state.csock cmd;
985 let intentry text key =
986 let c = Char.unsafe_chr key in
987 match c with
988 | '0' .. '9' ->
989 let s = "x" in s.[0] <- c;
990 let text = text ^ s in
991 TEcont text
993 | _ ->
994 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
995 TEcont text
998 let addchar s c =
999 let b = Buffer.create (String.length s + 1) in
1000 Buffer.add_string b s;
1001 Buffer.add_char b c;
1002 Buffer.contents b;
1005 let textentry text key =
1006 let c = Char.unsafe_chr key in
1007 match c with
1008 | _ when key >= 32 && key < 127 ->
1009 let text = addchar text c in
1010 TEcont text
1012 | _ ->
1013 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1014 TEcont text
1017 let reinit angle proportional =
1018 conf.angle <- angle;
1019 conf.proportional <- proportional;
1020 invalidate ();
1021 wcmd "reinit" [`i angle; `b proportional];
1024 let optentry text key =
1025 let btos b = if b then "on" else "off" in
1026 let c = Char.unsafe_chr key in
1027 match c with
1028 | 's' ->
1029 let ondone s =
1030 try conf.scrollstep <- int_of_string s with exc ->
1031 state.text <- Printf.sprintf "bad integer `%s': %s"
1032 s (Printexc.to_string exc)
1034 TEswitch ('#', "", None, intentry, ondone)
1036 | 'A' ->
1037 let ondone s =
1039 conf.autoscrollstep <- int_of_string s;
1040 if state.ascrollstep > 0
1041 then state.ascrollstep <- conf.autoscrollstep;
1042 with exc ->
1043 state.text <- Printf.sprintf "bad integer `%s': %s"
1044 s (Printexc.to_string exc)
1046 TEswitch ('*', "", None, intentry, ondone)
1048 | 'Z' ->
1049 let ondone s =
1051 let zoom = float (int_of_string s) /. 100.0 in
1052 let zoom = max 0.01 (min 2.2 zoom) in
1053 conf.zoom <- zoom;
1054 if zoom <= 1.0
1055 then state.x <- 0;
1056 reshape conf.winh conf.winw;
1057 state.text <- Printf.sprintf "zoom is now %f" (zoom *. 100.0);
1058 with exc ->
1059 state.text <- Printf.sprintf "bad integer `%s': %s"
1060 s (Printexc.to_string exc)
1062 TEswitch ('@', "", None, intentry, ondone)
1064 | 'R' ->
1065 let ondone s =
1066 match try
1067 Some (int_of_string s)
1068 with exc ->
1069 state.text <- Printf.sprintf "bad integer `%s': %s"
1070 s (Printexc.to_string exc);
1071 None
1072 with
1073 | Some angle -> reinit angle conf.proportional
1074 | None -> ()
1076 TEswitch ('^', "", None, intentry, ondone)
1078 | 'i' ->
1079 conf.icase <- not conf.icase;
1080 TEdone ("case insensitive search " ^ (btos conf.icase))
1082 | 'p' ->
1083 conf.preload <- not conf.preload;
1084 gotoy state.y;
1085 TEdone ("preload " ^ (btos conf.preload))
1087 | 'v' ->
1088 conf.verbose <- not conf.verbose;
1089 TEdone ("verbose " ^ (btos conf.verbose))
1091 | 'h' ->
1092 conf.maxhfit <- not conf.maxhfit;
1093 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1094 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1096 | 'c' ->
1097 conf.crophack <- not conf.crophack;
1098 TEdone ("crophack " ^ btos conf.crophack)
1100 | 'a' ->
1101 conf.showall <- not conf.showall;
1102 TEdone ("showall " ^ btos conf.showall)
1104 | 'f' ->
1105 conf.underinfo <- not conf.underinfo;
1106 TEdone ("underinfo " ^ btos conf.underinfo)
1108 | 'P' ->
1109 conf.savebmarks <- not conf.savebmarks;
1110 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1112 | 'S' ->
1113 let ondone s =
1115 let pageno, py =
1116 match state.layout with
1117 | [] -> 0, 0
1118 | l :: _ ->
1119 l.pageno, l.pagey
1121 conf.interpagespace <- int_of_string s;
1122 state.maxy <- calcheight ();
1123 let y = getpagey pageno in
1124 gotoy (y + py)
1125 with exc ->
1126 state.text <- Printf.sprintf "bad integer `%s': %s"
1127 s (Printexc.to_string exc)
1129 TEswitch ('%', "", None, intentry, ondone)
1131 | 'l' ->
1132 reinit conf.angle (not conf.proportional);
1133 TEdone ("proprortional display " ^ btos conf.proportional)
1135 | _ ->
1136 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1137 TEstop
1140 let maxoutlinerows () = (conf.winh - 31) / 16;;
1142 let enterselector allowdel outlines errmsg msg =
1143 if Array.length outlines = 0
1144 then (
1145 showtext ' ' errmsg;
1147 else (
1148 state.text <- msg;
1149 Glut.setCursor Glut.CURSOR_INHERIT;
1150 let pageno =
1151 match state.layout with
1152 | [] -> -1
1153 | {pageno=pageno} :: rest -> pageno
1155 let active =
1156 let rec loop n =
1157 if n = Array.length outlines
1158 then 0
1159 else
1160 let (_, _, outlinepageno, _) = outlines.(n) in
1161 if outlinepageno >= pageno then n else loop (n+1)
1163 loop 0
1165 state.mode <- Outline
1166 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1167 Glut.postRedisplay ();
1171 let enteroutlinemode () =
1172 let outlines, msg =
1173 match state.outlines with
1174 | Oarray a -> a, ""
1175 | Olist l ->
1176 let a = Array.of_list (List.rev l) in
1177 state.outlines <- Oarray a;
1178 a, ""
1179 | Onarrow (pat, a, b) ->
1180 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1182 enterselector false outlines "Document has no outline" msg;
1185 let enterbookmarkmode () =
1186 let bookmarks = Array.of_list state.bookmarks in
1187 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1190 let quickbookmark ?title () =
1191 match state.layout with
1192 | [] -> ()
1193 | l :: _ ->
1194 let title =
1195 match title with
1196 | None ->
1197 let sec = Unix.gettimeofday () in
1198 let tm = Unix.localtime sec in
1199 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1200 (l.pageno+1)
1201 tm.Unix.tm_mday
1202 tm.Unix.tm_mon
1203 (tm.Unix.tm_year + 1900)
1204 tm.Unix.tm_hour
1205 tm.Unix.tm_min
1206 | Some title -> title
1208 state.bookmarks <-
1209 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1212 let doreshape w h =
1213 state.fullscreen <- None;
1214 Glut.reshapeWindow w h;
1217 let writeopen path password =
1218 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1221 let opendoc path password =
1222 invalidate ();
1223 state.path <- path;
1224 state.password <- password;
1225 state.gen <- state.gen + 1;
1227 writeopen path password;
1228 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1229 wcmd "geometry" [`i state.w; `i conf.winh];
1232 let birdseyeon () =
1233 let zoom = float conf.thumbw /. float conf.winw in
1234 let birdseyepageno =
1235 let rec fold = function
1236 | [] -> 0
1237 | l :: _ when l.pagey = 0 -> l.pageno
1238 | _ :: rest -> fold rest
1240 fold state.layout
1242 state.mode <- Birdseye (
1243 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1245 conf.zoom <- zoom;
1246 conf.presentation <- false;
1247 conf.interpagespace <- 10;
1248 conf.hlinks <- false;
1249 state.x <- 0;
1250 state.mstate <- Mnone;
1251 conf.showall <- false;
1252 Glut.setCursor Glut.CURSOR_INHERIT;
1253 if conf.verbose
1254 then
1255 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1256 (100.0*.zoom)
1257 else
1258 state.text <- ""
1260 reshape conf.winw conf.winh;
1263 let birdseyeoff (c, leftx, pageno, _, anchor) goback =
1264 state.mode <- View;
1265 conf.zoom <- c.zoom;
1266 conf.presentation <- c.presentation;
1267 conf.interpagespace <- c.interpagespace;
1268 conf.showall <- c.showall;
1269 conf.hlinks <- c.hlinks;
1270 state.x <- leftx;
1271 if conf.verbose
1272 then
1273 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1274 (100.0*.conf.zoom)
1276 reshape conf.winw conf.winh;
1277 state.anchor <- if goback then anchor else (pageno, 0.0);
1280 let togglebirdseye () =
1281 match state.mode with
1282 | Birdseye vals -> birdseyeoff vals true
1283 | View | Outline _ -> birdseyeon ()
1284 | _ -> ()
1287 let viewkeyboard ~key ~x ~y =
1288 let enttext te =
1289 state.mode <- Textentry (te, state.mode);
1290 state.text <- "";
1291 enttext ();
1292 Glut.postRedisplay ()
1294 let c = Char.chr key in
1295 match c with
1296 | '\027' | 'q' ->
1297 exit 0
1299 | '\008' ->
1300 let y = getnav () in
1301 gotoy_and_clear_text y
1303 | 'o' ->
1304 enteroutlinemode ()
1306 | 'u' ->
1307 state.rects <- [];
1308 state.text <- "";
1309 Glut.postRedisplay ()
1311 | '/' | '?' ->
1312 let ondone isforw s =
1313 cbput state.hists.pat s;
1314 state.searchpattern <- s;
1315 search s isforw
1317 enttext (c, "", Some (onhist state.hists.pat),
1318 textentry, ondone (c ='/'))
1320 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1321 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1322 conf.zoom <- min 2.2 (conf.zoom +. incr);
1323 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1324 reshape conf.winw conf.winh
1326 | '+' ->
1327 let ondone s =
1328 let n =
1329 try int_of_string s with exc ->
1330 state.text <- Printf.sprintf "bad integer `%s': %s"
1331 s (Printexc.to_string exc);
1332 max_int
1334 if n != max_int
1335 then (
1336 conf.pagebias <- n;
1337 state.text <- "page bias is now " ^ string_of_int n;
1340 enttext ('+', "", None, intentry, ondone)
1342 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1343 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1344 conf.zoom <- max 0.01 (conf.zoom -. decr);
1345 if conf.zoom <= 1.0 then state.x <- 0;
1346 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1347 reshape conf.winw conf.winh;
1349 | '-' ->
1350 let ondone msg =
1351 state.text <- msg;
1353 enttext ('-', "", None, optentry, ondone)
1355 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1356 state.x <- 0;
1357 conf.zoom <- 1.0;
1358 state.text <- "zoom is 100%";
1359 reshape conf.winw conf.winh
1361 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1362 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1363 if zoom < 1.0
1364 then (
1365 conf.zoom <- zoom;
1366 state.x <- 0;
1367 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1368 reshape conf.winw conf.winh;
1371 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1372 togglebirdseye ()
1374 | '0' .. '9' ->
1375 let ondone s =
1376 let n =
1377 try int_of_string s with exc ->
1378 state.text <- Printf.sprintf "bad integer `%s': %s"
1379 s (Printexc.to_string exc);
1382 if n >= 0
1383 then (
1384 addnav ();
1385 cbput state.hists.pag (string_of_int n);
1386 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1389 let pageentry text key =
1390 match Char.unsafe_chr key with
1391 | 'g' -> TEdone text
1392 | _ -> intentry text key
1394 let text = "x" in text.[0] <- c;
1395 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1397 | 'b' ->
1398 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1399 reshape conf.winw conf.winh;
1401 | 'l' ->
1402 conf.hlinks <- not conf.hlinks;
1403 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1404 Glut.postRedisplay ()
1406 | 'a' ->
1407 if state.ascrollstep = 0
1408 then state.ascrollstep <- conf.autoscrollstep
1409 else (
1410 conf.autoscrollstep <- state.ascrollstep;
1411 state.ascrollstep <- 0;
1414 | 'P' ->
1415 conf.presentation <- not conf.presentation;
1416 showtext ' ' ("presentation mode " ^
1417 if conf.presentation then "on" else "off");
1418 represent ()
1420 | 'f' ->
1421 begin match state.fullscreen with
1422 | None ->
1423 state.fullscreen <- Some (conf.winw, conf.winh);
1424 Glut.fullScreen ()
1425 | Some (w, h) ->
1426 state.fullscreen <- None;
1427 doreshape w h
1430 | 'g' ->
1431 gotoy_and_clear_text 0
1433 | 'n' ->
1434 search state.searchpattern true
1436 | 'p' | 'N' ->
1437 search state.searchpattern false
1439 | 't' ->
1440 begin match state.layout with
1441 | [] -> ()
1442 | l :: _ ->
1443 gotoy_and_clear_text (getpagey l.pageno)
1446 | ' ' ->
1447 begin match List.rev state.layout with
1448 | [] -> ()
1449 | l :: _ ->
1450 let pageno = min (l.pageno+1) (state.pagecount-1) in
1451 gotoy_and_clear_text (getpagey pageno)
1454 | '\127' ->
1455 begin match state.layout with
1456 | [] -> ()
1457 | l :: _ ->
1458 let pageno = max 0 (l.pageno-1) in
1459 gotoy_and_clear_text (getpagey pageno)
1462 | '=' ->
1463 let f (fn, ln) l =
1464 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1466 let fn, ln = List.fold_left f (-1, -1) state.layout in
1467 let s =
1468 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1469 let percent =
1470 if maxy <= 0
1471 then 100.
1472 else (100. *. (float state.y /. float maxy)) in
1473 if fn = ln
1474 then
1475 Printf.sprintf "Page %d of %d %.2f%%"
1476 (fn+1) state.pagecount percent
1477 else
1478 Printf.sprintf
1479 "Pages %d-%d of %d %.2f%%"
1480 (fn+1) (ln+1) state.pagecount percent
1482 showtext ' ' s;
1484 | 'w' ->
1485 begin match state.layout with
1486 | [] -> ()
1487 | l :: _ ->
1488 doreshape (l.pagew + conf.scrollw) l.pageh;
1489 Glut.postRedisplay ();
1492 | '\'' ->
1493 enterbookmarkmode ()
1495 | 'm' ->
1496 let ondone s =
1497 match state.layout with
1498 | l :: _ ->
1499 state.bookmarks <-
1500 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1501 :: state.bookmarks
1502 | _ -> ()
1504 enttext ('~', "", None, textentry, ondone)
1506 | '~' ->
1507 quickbookmark ();
1508 showtext ' ' "Quick bookmark added";
1510 | 'z' ->
1511 begin match state.layout with
1512 | l :: _ ->
1513 let rect = getpdimrect l.pagedimno in
1514 let w, h =
1515 if conf.crophack
1516 then
1517 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1518 truncate (1.2 *. (rect.(3) -. rect.(0))))
1519 else
1520 (truncate (rect.(1) -. rect.(0)),
1521 truncate (rect.(3) -. rect.(0)))
1523 if w != 0 && h != 0
1524 then
1525 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1527 Glut.postRedisplay ();
1529 | [] -> ()
1532 | '<' | '>' ->
1533 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1535 | '[' | ']' ->
1536 state.colorscale <-
1537 max 0.0
1538 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1539 Glut.postRedisplay ()
1541 | 'k' -> gotoy (clamp (-conf.scrollstep))
1542 | 'j' -> gotoy (clamp conf.scrollstep)
1544 | 'r' -> opendoc state.path state.password
1546 | _ ->
1547 vlog "huh? %d %c" key (Char.chr key);
1550 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1551 let enttext te =
1552 state.mode <- Textentry (te, mode);
1553 state.text <- "";
1554 enttext ();
1555 Glut.postRedisplay ()
1557 match Char.unsafe_chr key with
1558 | '\008' ->
1559 let len = String.length text in
1560 if len = 0
1561 then (
1562 state.mode <- mode;
1563 Glut.postRedisplay ();
1565 else (
1566 let s = String.sub text 0 (len - 1) in
1567 enttext (c, s, opthist, onkey, ondone)
1570 | '\r' | '\n' ->
1571 ondone text;
1572 state.mode <- mode;
1573 Glut.postRedisplay ()
1575 | '\027' ->
1576 begin match opthist with
1577 | None -> ()
1578 | Some (_, onhistcancel) -> onhistcancel ()
1579 end;
1580 state.mode <- View;
1581 Glut.postRedisplay ()
1583 | _ ->
1584 begin match onkey text key with
1585 | TEdone text ->
1586 state.mode <- mode;
1587 ondone text;
1588 Glut.postRedisplay ()
1590 | TEcont text ->
1591 enttext (c, text, opthist, onkey, ondone);
1593 | TEstop ->
1594 state.mode <- mode;
1595 Glut.postRedisplay ()
1597 | TEswitch te ->
1598 state.mode <- Textentry (te, mode);
1599 Glut.postRedisplay ()
1600 end;
1603 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1604 match key with
1605 | 27 ->
1606 birdseyeoff beye true
1608 | 12 ->
1609 let y, h = getpageyh pageno in
1610 let top = (conf.winh - h) / 2 in
1611 gotoy (max 0 (y - top))
1613 | 13 ->
1614 birdseyeoff beye false
1616 | _ ->
1617 viewkeyboard ~key ~x ~y
1620 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1621 let narrow outlines pattern =
1622 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1623 match reopt with
1624 | None -> None
1625 | Some re ->
1626 let rec fold accu n =
1627 if n = -1
1628 then accu
1629 else
1630 let (s, _, _, _) as o = outlines.(n) in
1631 let accu =
1632 if (try ignore (Str.search_forward re s 0); true
1633 with Not_found -> false)
1634 then (o :: accu)
1635 else accu
1637 fold accu (n-1)
1639 let matched = fold [] (Array.length outlines - 1) in
1640 if matched = [] then None else Some (Array.of_list matched)
1642 let search active pattern incr =
1643 let dosearch re =
1644 let rec loop n =
1645 if n = Array.length outlines || n = -1
1646 then None
1647 else
1648 let (s, _, _, _) = outlines.(n) in
1650 (try ignore (Str.search_forward re s 0); true
1651 with Not_found -> false)
1652 then Some n
1653 else loop (n + incr)
1655 loop active
1658 let re = Str.regexp_case_fold pattern in
1659 dosearch re
1660 with Failure s ->
1661 state.text <- s;
1662 None
1664 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1665 match key with
1666 | 27 ->
1667 if String.length qsearch = 0
1668 then (
1669 state.text <- "";
1670 state.mode <- View;
1671 Glut.postRedisplay ();
1673 else (
1674 state.text <- "";
1675 state.mode <- Outline (allowdel, active, first, outlines, "");
1676 Glut.postRedisplay ();
1679 | 18 | 19 ->
1680 let incr = if key = 18 then -1 else 1 in
1681 let active, first =
1682 match search (active + incr) qsearch incr with
1683 | None ->
1684 state.text <- qsearch ^ " [not found]";
1685 active, first
1686 | Some active ->
1687 state.text <- qsearch;
1688 active, firstof active
1690 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1691 Glut.postRedisplay ();
1693 | 8 ->
1694 let len = String.length qsearch in
1695 if len = 0
1696 then ()
1697 else (
1698 if len = 1
1699 then (
1700 state.text <- "";
1701 state.mode <- Outline (allowdel, active, first, outlines, "");
1703 else
1704 let qsearch = String.sub qsearch 0 (len - 1) in
1705 let active, first =
1706 match search active qsearch ~-1 with
1707 | None ->
1708 state.text <- qsearch ^ " [not found]";
1709 active, first
1710 | Some active ->
1711 state.text <- qsearch;
1712 active, firstof active
1714 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1716 Glut.postRedisplay ()
1718 | 13 ->
1719 if active < Array.length outlines
1720 then (
1721 let (_, _, n, t) = outlines.(active) in
1722 addnav ();
1723 gotopage n t;
1725 state.text <- "";
1726 if allowdel then state.bookmarks <- Array.to_list outlines;
1727 state.mode <- View;
1728 Glut.postRedisplay ();
1730 | _ when key >= 32 && key < 127 ->
1731 let pattern = addchar qsearch (Char.chr key) in
1732 let active, first =
1733 match search active pattern 1 with
1734 | None ->
1735 state.text <- pattern ^ " [not found]";
1736 active, first
1737 | Some active ->
1738 state.text <- pattern;
1739 active, firstof active
1741 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1742 Glut.postRedisplay ()
1744 | 14 when not allowdel -> (* ctrl-n *)
1745 if String.length qsearch > 0
1746 then (
1747 let optoutlines = narrow outlines qsearch in
1748 begin match optoutlines with
1749 | None -> state.text <- "can't narrow"
1750 | Some outlines ->
1751 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1752 match state.outlines with
1753 | Olist l -> ()
1754 | Oarray a ->
1755 state.outlines <- Onarrow (qsearch, outlines, a)
1756 | Onarrow (pat, a, b) ->
1757 state.outlines <- Onarrow (qsearch, outlines, b)
1758 end;
1760 Glut.postRedisplay ()
1762 | 21 when not allowdel -> (* ctrl-u *)
1763 let outline =
1764 match state.outlines with
1765 | Oarray a -> a
1766 | Olist l ->
1767 let a = Array.of_list (List.rev l) in
1768 state.outlines <- Oarray a;
1770 | Onarrow (pat, a, b) ->
1771 state.outlines <- Oarray b;
1772 state.text <- "";
1775 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1776 Glut.postRedisplay ()
1778 | 12 ->
1779 state.mode <- Outline
1780 (allowdel, active, firstof active, outlines, qsearch);
1781 Glut.postRedisplay ()
1783 | 127 when allowdel ->
1784 let len = Array.length outlines - 1 in
1785 if len = 0
1786 then (
1787 state.mode <- View;
1788 state.bookmarks <- [];
1790 else (
1791 let bookmarks = Array.init len
1792 (fun i ->
1793 let i = if i >= active then i + 1 else i in
1794 outlines.(i)
1797 state.mode <-
1798 Outline (
1799 allowdel,
1800 min active (len-1),
1801 min first (len-1),
1802 bookmarks, qsearch
1805 Glut.postRedisplay ()
1807 | _ -> dolog "unknown key %d" key
1810 let keyboard ~key ~x ~y =
1811 if key = 7
1812 then
1813 wcmd "interrupt" []
1814 else
1815 match state.mode with
1816 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1817 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1818 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1819 | View -> viewkeyboard ~key ~x ~y
1822 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1823 match key with
1824 | Glut.KEY_UP ->
1825 let pageno = max 0 (pageno - 1) in
1826 let rec loop = function
1827 | [] -> gotopage1 pageno 0
1828 | l :: _ when l.pageno = pageno ->
1829 if l.pagedispy >= 0 && l.pagey = 0
1830 then Glut.postRedisplay ()
1831 else gotopage1 pageno 0
1832 | _ :: rest -> loop rest
1834 loop state.layout;
1835 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1837 | Glut.KEY_DOWN ->
1838 let pageno = min (state.pagecount - 1) (pageno + 1) in
1839 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1840 let rec loop = function
1841 | [] ->
1842 let y, h = getpageyh pageno in
1843 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1844 gotoy (clamp dy)
1845 | l :: rest when l.pageno = pageno ->
1846 if l.pagevh != l.pageh
1847 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1848 else Glut.postRedisplay ()
1849 | l :: rest -> loop rest
1851 loop state.layout
1853 | Glut.KEY_PAGE_UP ->
1854 begin match state.layout with
1855 | l :: _ ->
1856 if l.pagey != 0
1857 then (
1858 state.mode <- Birdseye (
1859 conf, leftx, l.pageno, hooverpageno, anchor
1861 gotopage1 l.pageno 0;
1863 else (
1864 let layout = layout (state.y-conf.winh) conf.winh in
1865 match layout with
1866 | [] -> gotoy (clamp (-conf.winh))
1867 | l :: _ ->
1868 state.mode <- Birdseye (
1869 conf, leftx, l.pageno, hooverpageno, anchor
1871 gotopage1 l.pageno 0
1874 | [] -> gotoy (clamp (-conf.winh))
1875 end;
1877 | Glut.KEY_PAGE_DOWN ->
1878 begin match List.rev state.layout with
1879 | l :: _ ->
1880 let layout = layout (state.y + conf.winh) conf.winh in
1881 begin match layout with
1882 | [] ->
1883 let incr = l.pageh - l.pagevh in
1884 if incr = 0
1885 then (
1886 state.mode <-
1887 Birdseye (
1888 conf, leftx, state.pagecount - 1, hooverpageno, anchor
1890 Glut.postRedisplay ();
1892 else gotoy (clamp (incr + conf.interpagespace*2));
1894 | l :: _ ->
1895 state.mode <-
1896 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
1897 gotopage1 l.pageno 0;
1900 | [] -> gotoy (clamp conf.winh)
1901 end;
1903 | Glut.KEY_HOME ->
1904 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
1905 gotopage1 0 0
1907 | Glut.KEY_END ->
1908 let pageno = state.pagecount - 1 in
1909 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1910 if not (pagevisible state.layout pageno)
1911 then
1912 let h =
1913 match List.rev state.pdims with
1914 | [] -> conf.winh
1915 | (_, _, h, _) :: _ -> h
1917 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1918 else Glut.postRedisplay ();
1919 | _ -> ()
1922 let setautoscrollspeed goingdown =
1923 let incr = max 1 (state.ascrollstep / 2) in
1924 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
1925 state.ascrollstep <- astep;
1928 let special ~key ~x ~y =
1929 match state.mode with
1930 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1931 togglebirdseye ()
1933 | Birdseye vals ->
1934 birdseyespecial key x y vals
1936 | View ->
1937 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
1938 then setautoscrollspeed (key = Glut.KEY_DOWN)
1939 else
1940 let y =
1941 match key with
1942 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1943 | Glut.KEY_UP -> clamp (-conf.scrollstep)
1944 | Glut.KEY_DOWN -> clamp conf.scrollstep
1945 | Glut.KEY_PAGE_UP ->
1946 if Glut.getModifiers () land Glut.active_ctrl != 0
1947 then
1948 match state.layout with
1949 | [] -> state.y
1950 | l :: _ -> state.y - l.pagey
1951 else
1952 clamp (-conf.winh)
1953 | Glut.KEY_PAGE_DOWN ->
1954 if Glut.getModifiers () land Glut.active_ctrl != 0
1955 then
1956 match List.rev state.layout with
1957 | [] -> state.y
1958 | l :: _ -> getpagey l.pageno
1959 else
1960 clamp conf.winh
1961 | Glut.KEY_HOME -> addnav (); 0
1962 | Glut.KEY_END ->
1963 addnav ();
1964 state.maxy - (if conf.maxhfit then conf.winh else 0)
1966 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1967 state.x <- state.x - 10;
1968 state.y
1969 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1970 state.x <- state.x + 10;
1971 state.y
1973 | _ -> state.y
1975 gotoy_and_clear_text y
1977 | Textentry
1978 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
1979 let s =
1980 match key with
1981 | Glut.KEY_UP -> action HCprev
1982 | Glut.KEY_DOWN -> action HCnext
1983 | Glut.KEY_HOME -> action HCfirst
1984 | Glut.KEY_END -> action HClast
1985 | _ -> state.text
1987 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
1988 Glut.postRedisplay ()
1990 | Textentry _ -> ()
1992 | Outline (allowdel, active, first, outlines, qsearch) ->
1993 let maxrows = maxoutlinerows () in
1994 let calcfirst first active =
1995 if active > first
1996 then
1997 let rows = active - first in
1998 if rows > maxrows then active - maxrows else first
1999 else active
2001 let navigate incr =
2002 let active = active + incr in
2003 let active = max 0 (min active (Array.length outlines - 1)) in
2004 let first = calcfirst first active in
2005 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2006 Glut.postRedisplay ()
2008 let updownlevel incr =
2009 let len = Array.length outlines in
2010 let (_, curlevel, _, _) = outlines.(active) in
2011 let rec flow i =
2012 if i = len then i-1 else if i = -1 then 0 else
2013 let (_, l, _, _) = outlines.(i) in
2014 if l != curlevel then i else flow (i+incr)
2016 let active = flow active in
2017 let first = calcfirst first active in
2018 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2019 Glut.postRedisplay ()
2021 match key with
2022 | Glut.KEY_UP -> navigate ~-1
2023 | Glut.KEY_DOWN -> navigate 1
2024 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2025 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2027 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
2028 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
2030 | Glut.KEY_HOME ->
2031 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
2032 Glut.postRedisplay ()
2034 | Glut.KEY_END ->
2035 let active = Array.length outlines - 1 in
2036 let first = max 0 (active - maxrows) in
2037 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2038 Glut.postRedisplay ()
2040 | _ -> ()
2043 let drawplaceholder l =
2044 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2045 GlDraw.rect
2046 (float l.pagex, float l.pagedispy)
2047 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2049 let x = float (if margin < 0 then -margin else l.pagex)
2050 and y = float (l.pagedispy + 13) in
2051 let font = Glut.BITMAP_8_BY_13 in
2052 GlDraw.color (0.0, 0.0, 0.0);
2053 GlPix.raster_pos ~x ~y ();
2054 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2055 ("Loading " ^ string_of_int (l.pageno + 1));
2058 let now () = Unix.gettimeofday ();;
2060 let drawpage l =
2061 let color =
2062 match state.mode with
2063 | Textentry _ -> scalecolor 0.4
2064 | View | Outline _ -> scalecolor 1.0
2065 | Birdseye (_, _, pageno, hooverpageno, _) ->
2066 if l.pageno = pageno
2067 then scalecolor 1.0
2068 else (
2069 if l.pageno = hooverpageno
2070 then scalecolor 0.9
2071 else scalecolor 0.8
2074 GlDraw.color color;
2075 begin match getopaque l.pageno with
2076 | Some (opaque, _) when validopaque opaque ->
2077 let a = now () in
2078 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2079 opaque;
2080 let b = now () in
2081 let d = b-.a in
2082 vlog "draw %d %f sec" l.pageno d;
2084 | _ ->
2085 drawplaceholder l;
2086 end;
2089 let scrollph y =
2090 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2091 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2092 let sh = float conf.winh /. sh in
2093 let sh = max sh (float conf.scrollh) in
2095 let percent =
2096 if state.y = state.maxy
2097 then 1.0
2098 else float y /. float maxy
2100 let position = (float conf.winh -. sh) *. percent in
2102 let position =
2103 if position +. sh > float conf.winh
2104 then float conf.winh -. sh
2105 else position
2107 position, sh;
2110 let scrollindicator () =
2111 GlDraw.color (0.64 , 0.64, 0.64);
2112 GlDraw.rect
2113 (float (conf.winw - conf.scrollw), 0.)
2114 (float conf.winw, float conf.winh)
2116 GlDraw.color (0.0, 0.0, 0.0);
2118 let position, sh = scrollph state.y in
2119 GlDraw.rect
2120 (float (conf.winw - conf.scrollw), position)
2121 (float conf.winw, position +. sh)
2125 let showsel margin =
2126 match state.mstate with
2127 | Mnone | Mscroll _ | Mpan _ ->
2130 | Msel ((x0, y0), (x1, y1)) ->
2131 let rec loop = function
2132 | l :: ls ->
2133 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2134 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2135 then
2136 match getopaque l.pageno with
2137 | Some (opaque, _) when validopaque opaque ->
2138 let oy = -l.pagey + l.pagedispy in
2139 seltext opaque
2140 (x0 - margin - state.x, y0,
2141 x1 - margin - state.x, y1) oy;
2143 | _ -> ()
2144 else loop ls
2145 | [] -> ()
2147 loop state.layout
2150 let showrects () =
2151 let panx = float state.x in
2152 Gl.enable `blend;
2153 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2154 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2155 List.iter
2156 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2157 List.iter (fun l ->
2158 if l.pageno = pageno
2159 then (
2160 let d = float (l.pagedispy - l.pagey) in
2161 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2162 GlDraw.begins `quads;
2164 GlDraw.vertex2 (x0+.panx, y0+.d);
2165 GlDraw.vertex2 (x1+.panx, y1+.d);
2166 GlDraw.vertex2 (x2+.panx, y2+.d);
2167 GlDraw.vertex2 (x3+.panx, y3+.d);
2169 GlDraw.ends ();
2171 ) state.layout
2172 ) state.rects
2174 Gl.disable `blend;
2177 let showoutline () =
2178 match state.mode with
2179 | Outline (allowdel, active, first, outlines, qsearch) ->
2180 Gl.enable `blend;
2181 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2182 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2183 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2184 Gl.disable `blend;
2186 GlDraw.color (1., 1., 1.);
2187 let font = Glut.BITMAP_9_BY_15 in
2188 let draw_string x y s =
2189 GlPix.raster_pos ~x ~y ();
2190 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2192 let rec loop row =
2193 if row = Array.length outlines || (row - first) * 16 > conf.winh
2194 then ()
2195 else (
2196 let (s, l, _, _) = outlines.(row) in
2197 let y = (row - first) * 16 in
2198 let x = 5 + 15*l in
2199 if row = active
2200 then (
2201 Gl.enable `blend;
2202 GlDraw.polygon_mode `both `line;
2203 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2204 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2205 GlDraw.rect (0., float (y + 1))
2206 (float (conf.winw - 1), float (y + 18));
2207 GlDraw.polygon_mode `both `fill;
2208 Gl.disable `blend;
2209 GlDraw.color (1., 1., 1.);
2211 draw_string (float x) (float (y + 16)) s;
2212 loop (row+1)
2215 loop first
2217 | _ -> ()
2220 let display () =
2221 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2222 GlDraw.viewport margin 0 state.w conf.winh;
2223 pagematrix ();
2224 GlClear.color (scalecolor 0.5);
2225 GlClear.clear [`color];
2226 if conf.zoom > 1.0
2227 then (
2228 Gl.enable `scissor_test;
2229 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2231 List.iter drawpage state.layout;
2232 if conf.zoom > 1.0
2233 then
2234 Gl.disable `scissor_test
2236 if state.x != 0
2237 then (
2238 let x = -.float state.x in
2239 GlMat.translate ~x ();
2241 showrects ();
2242 showsel margin;
2243 GlDraw.viewport 0 0 conf.winw conf.winh;
2244 winmatrix ();
2245 scrollindicator ();
2246 showoutline ();
2247 enttext ();
2248 Glut.swapBuffers ();
2251 let getunder x y =
2252 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2253 let x = x - margin - state.x in
2254 let rec f = function
2255 | l :: rest ->
2256 begin match getopaque l.pageno with
2257 | Some (opaque, _) when validopaque opaque ->
2258 let y = y - l.pagedispy in
2259 if y > 0
2260 then
2261 let y = l.pagey + y in
2262 let x = x - l.pagex in
2263 match whatsunder opaque x y with
2264 | Unone -> f rest
2265 | under -> under
2266 else
2267 f rest
2268 | _ ->
2269 f rest
2271 | [] -> Unone
2273 f state.layout
2276 let viewmouse button bstate x y =
2277 match button with
2278 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2279 if state.ascrollstep > 0
2280 then
2281 setautoscrollspeed (n=4)
2282 else
2283 let incr =
2284 if n = 3
2285 then -conf.scrollstep
2286 else conf.scrollstep
2288 let incr = incr * 2 in
2289 let y = clamp incr in
2290 gotoy_and_clear_text y
2292 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2293 if bstate = Glut.DOWN
2294 then (
2295 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2296 state.mstate <- Mpan (x, y)
2298 else
2299 state.mstate <- Mnone
2301 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2302 if bstate = Glut.DOWN
2303 then
2304 let position, sh = scrollph state.y in
2305 if y > truncate position && y < truncate (position +. sh)
2306 then
2307 state.mstate <- Mscroll
2308 else
2309 let percent = float y /. float conf.winh in
2310 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2311 gotoy desty;
2312 state.mstate <- Mscroll
2313 else
2314 state.mstate <- Mnone
2316 | Glut.LEFT_BUTTON ->
2317 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2318 begin match dest with
2319 | Ulinkgoto (pageno, top) ->
2320 if pageno >= 0
2321 then (
2322 addnav ();
2323 gotopage1 pageno top;
2326 | Ulinkuri s ->
2327 print_endline s
2329 | Unone when bstate = Glut.DOWN ->
2330 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2331 state.mstate <- Mpan (x, y);
2333 | Unone | Utext _ ->
2334 if bstate = Glut.DOWN
2335 then (
2336 if conf.angle mod 360 = 0
2337 then (
2338 state.mstate <- Msel ((x, y), (x, y));
2339 Glut.postRedisplay ()
2342 else (
2343 match state.mstate with
2344 | Mnone -> ()
2346 | Mscroll ->
2347 state.mstate <- Mnone
2349 | Mpan _ ->
2350 Glut.setCursor Glut.CURSOR_INHERIT;
2351 state.mstate <- Mnone
2353 | Msel ((x0, y0), (x1, y1)) ->
2354 let f l =
2355 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2356 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2357 then
2358 match getopaque l.pageno with
2359 | Some (opaque, _) when validopaque opaque ->
2360 copysel opaque
2361 | _ -> ()
2363 List.iter f state.layout;
2364 copysel ""; (* ugly *)
2365 Glut.setCursor Glut.CURSOR_INHERIT;
2366 state.mstate <- Mnone;
2370 | _ -> ()
2373 let birdseyemouse button bstate x y
2374 (conf, leftx, pageno, hooverpageno, anchor) =
2375 match button with
2376 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2377 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2378 let rec loop = function
2379 | [] -> ()
2380 | l :: rest ->
2381 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2382 && x > margin && x < margin + l.pagew
2383 then (
2384 birdseyeoff (conf, leftx, l.pageno, hooverpageno, anchor) false;
2386 else loop rest
2388 loop state.layout
2389 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2390 | _ -> ()
2393 let mouse bstate button x y =
2394 match state.mode with
2395 | View -> viewmouse button bstate x y
2396 | Birdseye beye -> birdseyemouse button bstate x y beye
2397 | Textentry _ -> ()
2398 | Outline _ -> ()
2401 let mouse ~button ~state ~x ~y = mouse state button x y;;
2403 let motion ~x ~y =
2404 match state.mode with
2405 | Outline _ -> ()
2406 | _ ->
2407 match state.mstate with
2408 | Mnone -> ()
2410 | Mpan (x0, y0) ->
2411 let dx = x - x0
2412 and dy = y0 - y in
2413 state.mstate <- Mpan (x, y);
2414 if conf.zoom > 1.0 then state.x <- state.x + dx;
2415 let y = clamp dy in
2416 gotoy_and_clear_text y
2418 | Msel (a, _) ->
2419 state.mstate <- Msel (a, (x, y));
2420 Glut.postRedisplay ()
2422 | Mscroll ->
2423 let y = min conf.winh (max 0 y) in
2424 let percent = float y /. float conf.winh in
2425 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2426 gotoy_and_clear_text y
2429 let pmotion ~x ~y =
2430 match state.mode with
2431 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2432 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2433 let rec loop = function
2434 | [] ->
2435 if hooverpageno != -1
2436 then (
2437 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2438 Glut.postRedisplay ();
2440 | l :: rest ->
2441 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2442 && x > margin && x < margin + l.pagew
2443 then (
2444 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2445 Glut.postRedisplay ();
2447 else loop rest
2449 loop state.layout
2451 | Outline _ -> ()
2452 | _ ->
2453 match state.mstate with
2454 | Mnone ->
2455 begin match getunder x y with
2456 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2457 | Ulinkuri uri ->
2458 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2459 Glut.setCursor Glut.CURSOR_INFO
2460 | Ulinkgoto (page, y) ->
2461 if conf.underinfo
2462 then showtext 'p' ("age: " ^ string_of_int page);
2463 Glut.setCursor Glut.CURSOR_INFO
2464 | Utext s ->
2465 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2466 Glut.setCursor Glut.CURSOR_TEXT
2469 | Mpan _ | Msel _ | Mscroll ->
2474 module State =
2475 struct
2476 open Parser
2478 let home =
2480 match Sys.os_type with
2481 | "Win32" -> Sys.getenv "HOMEPATH"
2482 | _ -> Sys.getenv "HOME"
2483 with exn ->
2484 prerr_endline
2485 ("Can not determine home directory location: " ^
2486 Printexc.to_string exn);
2490 let config_of c attrs =
2491 let apply c k v =
2493 match k with
2494 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2495 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2496 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2497 | "preload" -> { c with preload = bool_of_string v }
2498 | "page-bias" -> { c with pagebias = int_of_string v }
2499 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2500 | "auto-scroll-step" ->
2501 { c with autoscrollstep = max 0 (int_of_string v) }
2502 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2503 | "crop-hack" -> { c with crophack = bool_of_string v }
2504 | "throttle" -> { c with showall = bool_of_string v }
2505 | "highlight-links" -> { c with hlinks = bool_of_string v }
2506 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2507 | "vertical-margin" ->
2508 { c with interpagespace = max 0 (int_of_string v) }
2509 | "zoom" ->
2510 let zoom = float_of_string v /. 100. in
2511 let zoom = max 0.01 (min 2.2 zoom) in
2512 { c with zoom = zoom }
2513 | "presentation" -> { c with presentation = bool_of_string v }
2514 | "rotation-angle" -> { c with angle = int_of_string v }
2515 | "width" -> { c with winw = max 20 (int_of_string v) }
2516 | "height" -> { c with winh = max 20 (int_of_string v) }
2517 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2518 | "proportional-display" -> { c with proportional = bool_of_string v }
2519 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2520 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2521 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2522 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2523 | _ -> c
2524 with exn ->
2525 prerr_endline ("Error processing attribute (`" ^
2526 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2529 let rec fold c = function
2530 | [] -> c
2531 | (k, v) :: rest ->
2532 let c = apply c k v in
2533 fold c rest
2535 fold c attrs;
2538 let fromstring f pos n v d =
2539 try f v
2540 with exn ->
2541 dolog "Error processing attribute (%S=%S) at %d\n%s"
2542 n v pos (Printexc.to_string exn)
2547 let bookmark_of attrs =
2548 let rec fold title page rely = function
2549 | ("title", v) :: rest -> fold v page rely rest
2550 | ("page", v) :: rest -> fold title v rely rest
2551 | ("rely", v) :: rest -> fold title page v rest
2552 | _ :: rest -> fold title page rely rest
2553 | [] -> title, page, rely
2555 fold "invalid" "0" "0" attrs
2558 let doc_of attrs =
2559 let rec fold path page rely pan = function
2560 | ("path", v) :: rest -> fold v page rely pan rest
2561 | ("page", v) :: rest -> fold path v rely pan rest
2562 | ("rely", v) :: rest -> fold path page v pan rest
2563 | ("pan", v) :: rest -> fold path page rely v rest
2564 | _ :: rest -> fold path page rely pan rest
2565 | [] -> path, page, rely, pan
2567 fold "" "0" "0" "0" attrs
2570 let setconf dst src =
2571 dst.scrollw <- src.scrollw;
2572 dst.scrollh <- src.scrollh;
2573 dst.icase <- src.icase;
2574 dst.preload <- src.preload;
2575 dst.pagebias <- src.pagebias;
2576 dst.verbose <- src.verbose;
2577 dst.scrollstep <- src.scrollstep;
2578 dst.maxhfit <- src.maxhfit;
2579 dst.crophack <- src.crophack;
2580 dst.autoscrollstep <- src.autoscrollstep;
2581 dst.showall <- src.showall;
2582 dst.hlinks <- src.hlinks;
2583 dst.underinfo <- src.underinfo;
2584 dst.interpagespace <- src.interpagespace;
2585 dst.zoom <- src.zoom;
2586 dst.presentation <- src.presentation;
2587 dst.angle <- src.angle;
2588 dst.winw <- src.winw;
2589 dst.winh <- src.winh;
2590 dst.savebmarks <- src.savebmarks;
2591 dst.memlimit <- src.memlimit;
2592 dst.proportional <- src.proportional;
2593 dst.texcount <- src.texcount;
2594 dst.sliceheight <- src.sliceheight;
2595 dst.thumbw <- src.thumbw;
2598 let unent s =
2599 let l = String.length s in
2600 let b = Buffer.create l in
2601 unent b s 0 l;
2602 Buffer.contents b;
2605 let get s =
2606 let h = Hashtbl.create 10 in
2607 let dc = { defconf with angle = defconf.angle } in
2608 let rec toplevel v t spos epos =
2609 match t with
2610 | Vdata | Vcdata | Vend -> v
2611 | Vopen ("llppconfig", attrs, closed) ->
2612 if closed
2613 then v
2614 else { v with f = llppconfig }
2615 | Vopen _ ->
2616 error "unexpected subelement at top level" s spos
2617 | Vclose tag -> error "unexpected close at top level" s spos
2619 and llppconfig v t spos epos =
2620 match t with
2621 | Vdata | Vcdata | Vend -> v
2622 | Vopen ("defaults", attrs, closed) ->
2623 let c = config_of dc attrs in
2624 setconf dc c;
2625 if closed
2626 then v
2627 else { v with f = skip "defaults" (fun () -> v) }
2629 | Vopen ("doc", attrs, closed) ->
2630 let pathent, spage, srely, span = doc_of attrs in
2631 let path = unent pathent
2632 and pageno = fromstring int_of_string spos "page" spage 0
2633 and rely = fromstring float_of_string spos "rely" srely 0.0
2634 and pan = fromstring int_of_string spos "pan" span 0 in
2635 let c = config_of dc attrs in
2636 let anchor = (pageno, rely) in
2637 if closed
2638 then (Hashtbl.add h path (c, [], pan, anchor); v)
2639 else { v with f = doc path pan anchor c [] }
2641 | Vopen (tag, _, closed) ->
2642 error "unexpected subelement in llppconfig" s spos
2644 | Vclose "llppconfig" -> { v with f = toplevel }
2645 | Vclose tag -> error "unexpected close in llppconfig" s spos
2647 and doc path pan anchor c bookmarks v t spos epos =
2648 match t with
2649 | Vdata | Vcdata -> v
2650 | Vend -> error "unexpected end of input in doc" s spos
2651 | Vopen ("bookmarks", attrs, closed) ->
2652 { v with f = pbookmarks path pan anchor c bookmarks }
2654 | Vopen (tag, _, _) ->
2655 error "unexpected subelement in doc" s spos
2657 | Vclose "doc" ->
2658 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2659 { v with f = llppconfig }
2661 | Vclose tag -> error "unexpected close in doc" s spos
2663 and pbookmarks path pan anchor c bookmarks v t spos epos =
2664 match t with
2665 | Vdata | Vcdata -> v
2666 | Vend -> error "unexpected end of input in bookmarks" s spos
2667 | Vopen ("item", attrs, closed) ->
2668 let titleent, spage, srely = bookmark_of attrs in
2669 let page = fromstring int_of_string spos "page" spage 0
2670 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2671 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2672 if closed
2673 then { v with f = pbookmarks path pan anchor c bookmarks }
2674 else
2675 let f () = v in
2676 { v with f = skip "item" f }
2678 | Vopen _ ->
2679 error "unexpected subelement in bookmarks" s spos
2681 | Vclose "bookmarks" ->
2682 { v with f = doc path pan anchor c bookmarks }
2684 | Vclose tag -> error "unexpected close in bookmarks" s spos
2686 and skip tag f v t spos epos =
2687 match t with
2688 | Vdata | Vcdata -> v
2689 | Vend ->
2690 error ("unexpected end of input in skipped " ^ tag) s spos
2691 | Vopen (tag', _, closed) ->
2692 if closed
2693 then v
2694 else
2695 let f' () = { v with f = skip tag f } in
2696 { v with f = skip tag' f' }
2697 | Vclose ctag ->
2698 if tag = ctag
2699 then f ()
2700 else error ("unexpected close in skipped " ^ tag) s spos
2703 parse { f = toplevel; accu = () } s;
2704 h, dc;
2707 let do_load f ic =
2709 let len = in_channel_length ic in
2710 let s = String.create len in
2711 really_input ic s 0 len;
2712 f s;
2713 with
2714 | Parse_error (msg, s, pos) ->
2715 let subs = subs s pos in
2716 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2717 failwith ("parse error: " ^ s)
2719 | exn ->
2720 failwith ("config load error: " ^ Printexc.to_string exn)
2723 let path =
2724 let dir =
2726 let dir = Filename.concat home ".config" in
2727 if Sys.is_directory dir then dir else home
2728 with _ -> home
2730 Filename.concat dir "llpp.conf"
2733 let load1 f =
2734 if Sys.file_exists path
2735 then
2736 match
2737 (try Some (open_in_bin path)
2738 with exn ->
2739 prerr_endline
2740 ("Error opening configuation file `" ^ path ^ "': " ^
2741 Printexc.to_string exn);
2742 None
2744 with
2745 | Some ic ->
2746 begin try
2747 f (do_load get ic)
2748 with exn ->
2749 prerr_endline
2750 ("Error loading configuation from `" ^ path ^ "': " ^
2751 Printexc.to_string exn);
2752 end;
2753 close_in ic;
2755 | None -> ()
2756 else
2757 f (Hashtbl.create 0, defconf)
2760 let load () =
2761 let f (h, dc) =
2762 let pc, pb, px, pa =
2764 Hashtbl.find h (Filename.basename state.path)
2765 with Not_found -> dc, [], 0, (0, 0.0)
2767 setconf defconf dc;
2768 setconf conf pc;
2769 state.bookmarks <- pb;
2770 state.x <- px;
2771 cbput state.hists.nav pa;
2773 load1 f
2776 let add_attrs bb always dc c =
2777 let ob s a b =
2778 if always || a != b
2779 then Printf.bprintf bb "\n %s='%b'" s a
2780 and oi s a b =
2781 if always || a != b
2782 then Printf.bprintf bb "\n %s='%d'" s a
2783 and oz s a b =
2784 if always || a <> b
2785 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2787 let w, h =
2788 if always
2789 then dc.winw, dc.winh
2790 else
2791 match state.fullscreen with
2792 | Some wh -> wh
2793 | None -> c.winw, c.winh
2795 let zoom, presentation, interpagespace, showall=
2796 if always
2797 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2798 else
2799 match state.mode with
2800 | Birdseye (bc, _, _, _, _) ->
2801 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2802 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2804 oi "width" w dc.winw;
2805 oi "height" h dc.winh;
2806 oi "scroll-bar-width" c.scrollw dc.scrollw;
2807 oi "scroll-handle-height" c.scrollh dc.scrollh;
2808 ob "case-insensitive-search" c.icase dc.icase;
2809 ob "preload" c.preload dc.preload;
2810 oi "page-bias" c.pagebias dc.pagebias;
2811 oi "scroll-step" c.scrollstep dc.scrollstep;
2812 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
2813 ob "max-height-fit" c.maxhfit dc.maxhfit;
2814 ob "crop-hack" c.crophack dc.crophack;
2815 ob "throttle" showall dc.showall;
2816 ob "highlight-links" c.hlinks dc.hlinks;
2817 ob "under-cursor-info" c.underinfo dc.underinfo;
2818 oi "vertical-margin" interpagespace dc.interpagespace;
2819 oz "zoom" zoom dc.zoom;
2820 ob "presentation" presentation dc.presentation;
2821 oi "rotation-angle" c.angle dc.angle;
2822 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2823 ob "proportional-display" c.proportional dc.proportional;
2824 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2825 oi "texcount" c.texcount dc.texcount;
2826 oi "slice-height" c.sliceheight dc.sliceheight;
2827 oi "thumbnail-width" c.thumbw dc.thumbw;
2830 let save () =
2831 let bb = Buffer.create 32768 in
2832 let f (h, dc) =
2833 Buffer.add_string bb "<llppconfig>\n<defaults ";
2834 add_attrs bb true dc dc;
2835 Buffer.add_string bb "/>\n";
2837 let adddoc path pan anchor c bookmarks =
2838 if bookmarks == [] && c = dc && anchor = emptyanchor
2839 then ()
2840 else (
2841 Printf.bprintf bb "<doc path='%s'"
2842 (enent path 0 (String.length path));
2844 if anchor <> emptyanchor
2845 then (
2846 let n, y = anchor in
2847 Printf.bprintf bb " page='%d'" n;
2848 Printf.bprintf bb " rely='%f'" y;
2851 if pan != 0
2852 then Printf.bprintf bb " pan='%d'" pan;
2854 add_attrs bb false dc c;
2856 begin match bookmarks with
2857 | [] -> Buffer.add_string bb "/>\n"
2858 | _ ->
2859 Buffer.add_string bb ">\n<bookmarks>\n";
2860 List.iter (fun (title, _level, page, rely) ->
2861 Printf.bprintf bb
2862 "<item title='%s' page='%d' rely='%f'/>\n"
2863 (enent title 0 (String.length title))
2864 page
2865 rely
2866 ) bookmarks;
2867 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2868 end;
2872 let pan =
2873 match state.mode with
2874 | Birdseye (_, pan, _, _, _) -> pan
2875 | _ -> state.x
2877 let basename = Filename.basename state.path in
2878 adddoc basename pan (getanchor ())
2879 { conf with
2880 autoscrollstep =
2881 if state.ascrollstep > 0
2882 then state.ascrollstep
2883 else conf.autoscrollstep }
2884 (if conf.savebmarks then state.bookmarks else []);
2886 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2887 if basename <> path
2888 then adddoc path x y c bookmarks
2889 ) h;
2890 Buffer.add_string bb "</llppconfig>";
2892 load1 f;
2893 if Buffer.length bb > 0
2894 then
2896 let tmp = path ^ ".tmp" in
2897 let oc = open_out_bin tmp in
2898 Buffer.output_buffer oc bb;
2899 close_out oc;
2900 Sys.rename tmp path;
2901 with exn ->
2902 prerr_endline
2903 ("error while saving configuration: " ^ Printexc.to_string exn)
2905 end;;
2907 let () =
2908 Arg.parse
2909 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2910 (fun s -> state.path <- s)
2911 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2913 if String.length state.path = 0
2914 then (prerr_endline "filename missing"; exit 1);
2916 State.load ();
2918 let _ = Glut.init Sys.argv in
2919 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2920 let () = Glut.initWindowSize conf.winw conf.winh in
2921 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2923 let csock, ssock =
2924 if Sys.os_type = "Unix"
2925 then
2926 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2927 else
2928 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2929 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2930 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2931 Unix.bind sock addr;
2932 Unix.listen sock 1;
2933 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2934 Unix.connect csock addr;
2935 let ssock, _ = Unix.accept sock in
2936 Unix.close sock;
2937 let opts sock =
2938 Unix.setsockopt sock Unix.TCP_NODELAY true;
2939 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2941 opts ssock;
2942 opts csock;
2943 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2944 ssock, csock
2947 let () = Glut.displayFunc display in
2948 let () = Glut.reshapeFunc reshape in
2949 let () = Glut.keyboardFunc keyboard in
2950 let () = Glut.specialFunc special in
2951 let () = Glut.idleFunc (Some idle) in
2952 let () = Glut.mouseFunc mouse in
2953 let () = Glut.motionFunc motion in
2954 let () = Glut.passiveMotionFunc pmotion in
2956 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2957 state.csock <- csock;
2958 state.ssock <- ssock;
2959 state.text <- "Opening " ^ state.path;
2960 writeopen state.path state.password;
2962 at_exit State.save;
2964 let rec handlelablglutbug () =
2966 Glut.mainLoop ();
2967 with Glut.BadEnum "key in special_of_int" ->
2968 showtext '!' " LablGlut bug: special key not recognized";
2969 handlelablglutbug ()
2971 handlelablglutbug ();