Refactoring #4
[llpp.git] / main.ml
blobeb49c97fa3b937ccb7c569e8fb1e3a35104f15dd
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 type params = angle * proportional * texcount * sliceheight
12 and pageno = int
13 and width = int
14 and height = int
15 and leftx = int
16 and opaque = string
17 and recttype = int
18 and pixmapsize = int
19 and angle = int
20 and proportional = bool
21 and interpagespace = int
22 and texcount = int
23 and sliceheight = int
24 and gen = int
25 and top = float
28 external init : Unix.file_descr -> params -> unit = "ml_init";;
29 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext : string -> (int * int * int * int) -> int -> unit =
31 "ml_seltext";;
32 external copysel : string -> unit = "ml_copysel";;
33 external getpdimrect : int -> float array = "ml_getpdimrect";;
34 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
35 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
37 type mpos = int * int
38 and mstate =
39 | Msel of (mpos * mpos)
40 | Mpan of mpos
41 | Mscroll
42 | Mnone
45 type 'a circbuf =
46 { store : 'a array
47 ; mutable rc : int
48 ; mutable wc : int
49 ; mutable len : int
53 type textentry = (char * string * onhist * onkey * ondone)
54 and onkey = string -> int -> te
55 and ondone = string -> unit
56 and histcancel = unit -> unit
57 and onhist = ((histcmd -> string) * histcancel) option
58 and histcmd = HCnext | HCprev | HCfirst | HClast
59 and te =
60 | TEstop
61 | TEdone of string
62 | TEcont of string
63 | TEswitch of textentry
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 scrollincr : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscroll : bool
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)
175 | Outline of (bool * int * int * outline array * string)
176 | Textentry of textentry
177 | View
180 let isbirdseye = function Birdseye _ -> true | _ -> false;;
181 let istextentry = function Textentry _ -> true | _ -> false;;
182 let isoutline = function Outline _ -> true | _ -> false;;
184 type state =
185 { mutable csock : Unix.file_descr
186 ; mutable ssock : Unix.file_descr
187 ; mutable w : int
188 ; mutable x : int
189 ; mutable y : int
190 ; mutable anchor : anchor
191 ; mutable maxy : int
192 ; mutable layout : layout list
193 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
194 ; mutable pdims : (pageno * width * height * leftx) list
195 ; mutable pagecount : int
196 ; pagecache : string circbuf
197 ; mutable rendering : bool
198 ; mutable mstate : mstate
199 ; mutable searchpattern : string
200 ; mutable rects : (pageno * recttype * rect) list
201 ; mutable rects1 : (pageno * recttype * rect) list
202 ; mutable text : string
203 ; mutable fullscreen : (width * height) option
204 ; mutable mode : mode
205 ; mutable outlines : outlines
206 ; mutable bookmarks : outline list
207 ; mutable path : string
208 ; mutable password : string
209 ; mutable invalidated : int
210 ; mutable colorscale : float
211 ; mutable memused : int
212 ; mutable gen : gen
213 ; mutable throttle : layout list option
214 ; hists : hists
216 and hists =
217 { pat : string circbuf
218 ; pag : string circbuf
219 ; nav : anchor circbuf
223 let defconf =
224 { scrollw = 7
225 ; scrollh = 12
226 ; icase = true
227 ; preload = true
228 ; pagebias = 0
229 ; verbose = false
230 ; scrollincr = 24
231 ; maxhfit = true
232 ; crophack = false
233 ; autoscroll = false
234 ; showall = false
235 ; hlinks = false
236 ; underinfo = false
237 ; interpagespace = 2
238 ; zoom = 1.0
239 ; presentation = false
240 ; angle = 0
241 ; winw = 900
242 ; winh = 900
243 ; savebmarks = true
244 ; proportional = true
245 ; memlimit = 32*1024*1024
246 ; texcount = 256
247 ; sliceheight = 24
248 ; thumbw = 76
252 let conf = { defconf with angle = defconf.angle };;
254 let state =
255 { csock = Unix.stdin
256 ; ssock = Unix.stdin
257 ; x = 0
258 ; y = 0
259 ; anchor = (0, 0.0)
260 ; w = 0
261 ; layout = []
262 ; maxy = max_int
263 ; pagemap = Hashtbl.create 10
264 ; pagecache = cbnew 100 ""
265 ; pdims = []
266 ; pagecount = 0
267 ; rendering = false
268 ; mstate = Mnone
269 ; rects = []
270 ; rects1 = []
271 ; text = ""
272 ; mode = View
273 ; fullscreen = None
274 ; searchpattern = ""
275 ; outlines = Olist []
276 ; bookmarks = []
277 ; path = ""
278 ; password = ""
279 ; invalidated = 0
280 ; hists =
281 { nav = cbnew 100 (0, 0.0)
282 ; pat = cbnew 20 ""
283 ; pag = cbnew 10 ""
285 ; colorscale = 1.0
286 ; memused = 0
287 ; gen = 0
288 ; throttle = None
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];
531 | _ -> ()
534 let loadlayout layout =
535 let rec f all = function
536 | l :: ls ->
537 begin match getopaque l.pageno with
538 | None -> render l; f false ls
539 | Some (opaque, _) -> f (all && validopaque opaque) ls
541 | [] -> all
543 f (layout <> []) layout;
546 let findpageforopaque opaque =
547 Hashtbl.fold
548 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
549 state.pagemap None
552 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
554 let preload () =
555 let oktopreload =
556 if conf.preload
557 then
558 let memleft = conf.memlimit - state.memused in
559 if memleft < 0
560 then
561 let opaque = cbpeek state.pagecache in
562 match findpageforopaque opaque with
563 | Some ((n, _, _, _, _), size) ->
564 memleft + size >= 0 && not (pagevisible state.layout n)
565 | None -> false
566 else true
567 else false
569 if oktopreload
570 then
571 let presentation = conf.presentation in
572 let interpagespace = conf.interpagespace in
573 let maxy = state.maxy in
574 conf.presentation <- false;
575 conf.interpagespace <- 0;
576 state.maxy <- calcheight ();
577 let y =
578 match state.layout with
579 | [] -> 0
580 | l :: _ -> getpagey l.pageno
582 let y = if y < conf.winh then 0 else y - conf.winh in
583 let pages = layout y (conf.winh*3) in
584 List.iter render pages;
585 conf.presentation <- presentation;
586 conf.interpagespace <- interpagespace;
587 state.maxy <- maxy;
590 let gotoy y =
591 let y = max 0 y in
592 let y = min state.maxy y in
593 let pages = layout y conf.winh in
594 let ready = loadlayout pages in
595 if conf.showall
596 then (
597 if ready
598 then (
599 state.y <- y;
600 state.layout <- pages;
601 state.throttle <- None;
602 Glut.postRedisplay ();
604 else (
605 state.throttle <- Some pages;
608 else (
609 state.y <- y;
610 state.layout <- pages;
611 state.throttle <- None;
612 Glut.postRedisplay ();
614 begin match state.mode with
615 | Birdseye (conf, leftx, pageno, hooverpageno) ->
616 if not (pagevisible pages pageno)
617 then (
618 match state.layout with
619 | [] -> ()
620 | l :: _ ->
621 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno)
623 | _ -> ()
624 end;
625 preload ();
628 let gotoy_and_clear_text y =
629 gotoy y;
630 if not conf.verbose then state.text <- "";
633 let emptyanchor = (0, 0.0);;
635 let getanchor () =
636 match state.layout with
637 | [] -> emptyanchor
638 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
641 let getanchory (n, top) =
642 let y, h = getpageyh n in
643 y + (truncate (top *. float h));
646 let gotoanchor anchor =
647 gotoy (getanchory anchor);
650 let addnav () =
651 cbput state.hists.nav (getanchor ());
654 let getnav () =
655 let anchor = cbgetc state.hists.nav ~-1 in
656 getanchory anchor;
659 let gotopagenonav n top =
660 let y, h = getpageyh n in
661 gotoy_and_clear_text (y + (truncate (top *. float h)));
664 let gotopage1nonav n top =
665 let y, h = getpageyh n in
666 addnav ();
667 gotoy_and_clear_text (y + top);
670 let gotopage n top =
671 let y, h = getpageyh n in
672 addnav ();
673 gotoy_and_clear_text (y + (truncate (top *. float h)));
676 let gotopage1 n top =
677 let y = getpagey n in
678 addnav ();
679 gotoy_and_clear_text (y + top);
682 let invalidate () =
683 state.layout <- [];
684 state.pdims <- [];
685 state.rects <- [];
686 state.rects1 <- [];
687 state.invalidated <- state.invalidated + 1;
690 let scalecolor c =
691 let c = c *. state.colorscale in
692 (c, c, c);
695 let represent () =
696 state.maxy <- calcheight ();
697 match state.mode with
698 | Birdseye (_, _, pageno, _) ->
699 let y, h = getpageyh pageno in
700 let top = (conf.winh - h) / 2 in
701 gotoy (max 0 (y - top))
702 | _ -> gotoanchor state.anchor
705 let pagematrix () =
706 GlMat.mode `projection;
707 GlMat.load_identity ();
708 GlMat.rotate ~x:1.0 ~angle:180.0 ();
709 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
710 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
713 let winmatrix () =
714 GlMat.mode `projection;
715 GlMat.load_identity ();
716 GlMat.rotate ~x:1.0 ~angle:180.0 ();
717 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
718 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
721 let reshape ~w ~h =
722 if state.invalidated = 0
723 then state.anchor <- getanchor ();
725 conf.winw <- w;
726 let w = truncate (float w *. conf.zoom) - conf.scrollw in
727 let w = max w 2 in
728 state.w <- w;
729 conf.winh <- h;
730 GlMat.mode `modelview;
731 GlMat.load_identity ();
732 GlClear.color (scalecolor 1.0);
733 GlClear.clear [`color];
735 invalidate ();
736 wcmd "geometry" [`i w; `i h];
739 let showtext c s =
740 GlDraw.color (0.0, 0.0, 0.0);
741 GlDraw.rect
742 (0.0, float (conf.winh - 18))
743 (float (conf.winw - conf.scrollw - 1), float conf.winh)
745 let font = Glut.BITMAP_8_BY_13 in
746 GlDraw.color (1.0, 1.0, 1.0);
747 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
748 Glut.bitmapCharacter ~font ~c:(Char.code c);
749 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
752 let enttext () =
753 let len = String.length state.text in
754 match state.mode with
755 | Textentry (c, text, _, _, _) ->
756 let s =
757 if len > 0
758 then
759 text ^ " [" ^ state.text ^ "]"
760 else
761 text
763 showtext c s;
765 | _ ->
766 if len > 0 then showtext ' ' state.text
769 let showtext c s =
770 if true
771 then (
772 state.text <- Printf.sprintf "%c%s" c s;
773 Glut.postRedisplay ();
775 else (
776 showtext c s;
777 Glut.swapBuffers ();
781 let act cmd =
782 match cmd.[0] with
783 | 'c' ->
784 state.pdims <- [];
786 | 'D' ->
787 state.rects <- state.rects1;
788 Glut.postRedisplay ()
790 | 'C' ->
791 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
792 state.pagecount <- n;
793 state.invalidated <- state.invalidated - 1;
794 if state.invalidated = 0
795 then represent ()
797 | 't' ->
798 let s = Scanf.sscanf cmd "t %n"
799 (fun n -> String.sub cmd n (String.length cmd - n))
801 Glut.setWindowTitle s
803 | 'T' ->
804 let s = Scanf.sscanf cmd "T %n"
805 (fun n -> String.sub cmd n (String.length cmd - n))
807 if istextentry state.mode
808 then (
809 state.text <- s;
810 showtext ' ' s;
812 else (
813 state.text <- s;
814 Glut.postRedisplay ();
817 | 'V' ->
818 if conf.verbose
819 then
820 let s = Scanf.sscanf cmd "V %n"
821 (fun n -> String.sub cmd n (String.length cmd - n))
823 state.text <- s;
824 showtext ' ' s;
826 | 'F' ->
827 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
828 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
829 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
830 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
832 let y = (getpagey pageno) + truncate y0 in
833 addnav ();
834 gotoy y;
835 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
837 | 'R' ->
838 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
839 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
840 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
841 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
843 state.rects1 <-
844 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
846 | 'r' ->
847 let n, w, h, r, l, s, p =
848 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
849 (fun n w h r l s p ->
850 (n-1, w, h, r, l != 0, s, p))
853 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
854 state.memused <- state.memused + s;
856 let layout =
857 match state.throttle with
858 | None -> state.layout
859 | Some layout -> layout
862 let rec gc () =
863 if (state.memused <= conf.memlimit) || cbempty state.pagecache
864 then ()
865 else (
866 let evictedopaque = cbpeek state.pagecache in
867 match findpageforopaque evictedopaque with
868 | None -> failwith "bug in gc"
869 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
870 if state.gen != gen || not (pagevisible layout evictedn)
871 then (
872 wcmd "free" [`s evictedopaque];
873 state.memused <- state.memused - evictedsize;
874 Hashtbl.remove state.pagemap k;
875 cbdecr state.pagecache;
876 gc ();
880 gc ();
882 cbput state.pagecache p;
883 state.rendering <- false;
885 begin match state.throttle with
886 | None ->
887 if pagevisible state.layout n
888 then gotoy state.y
889 else (
890 let allvisible = loadlayout state.layout in
891 if allvisible then preload ();
894 | Some layout ->
895 match layout with
896 | [] -> ()
897 | l :: _ ->
898 let y = getpagey l.pageno + l.pagey in
899 gotoy y
902 | 'l' ->
903 let (n, w, h, x) as pdim =
904 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
906 state.pdims <- pdim :: state.pdims
908 | 'o' ->
909 let (l, n, t, h, pos) =
910 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
912 let s = String.sub cmd pos (String.length cmd - pos) in
913 let s =
914 let l = String.length s in
915 let b = Buffer.create (String.length s) in
916 let rec loop pc2 i =
917 if i = l
918 then ()
919 else
920 let pc2 =
921 match s.[i] with
922 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
923 | '\xc2' -> true
924 | c ->
925 let c = if Char.code c land 0x80 = 0 then c else '?' in
926 Buffer.add_char b c;
927 false
929 loop pc2 (i+1)
931 loop false 0;
932 Buffer.contents b
934 let outline = (s, l, n, float t /. float h) in
935 let outlines =
936 match state.outlines with
937 | Olist outlines -> Olist (outline :: outlines)
938 | Oarray _ -> Olist [outline]
939 | Onarrow _ -> Olist [outline]
941 state.outlines <- outlines
943 | _ ->
944 log "unknown cmd `%S'" cmd
947 let now = Unix.gettimeofday;;
949 let idle () =
950 let rec loop delay =
951 let r, _, _ = Unix.select [state.csock] [] [] delay in
952 begin match r with
953 | [] ->
954 if conf.autoscroll && conf.scrollincr != 0
955 then begin
956 let y = state.y + conf.scrollincr in
957 let y = if y >= state.maxy then 0 else y in
958 gotoy y;
959 state.text <- "";
960 end;
962 | _ ->
963 let cmd = readcmd state.csock in
964 act cmd;
965 loop 0.0
966 end;
967 in loop 0.001
970 let onhist cb =
971 let rc = cb.rc in
972 let action = function
973 | HCprev -> cbget cb ~-1
974 | HCnext -> cbget cb 1
975 | HCfirst -> cbget cb ~-(cb.rc)
976 | HClast -> cbget cb (cb.len - 1 - cb.rc)
977 and cancel () = cb.rc <- rc
978 in (action, cancel)
981 let search pattern forward =
982 if String.length pattern > 0
983 then
984 let pn, py =
985 match state.layout with
986 | [] -> 0, 0
987 | l :: _ ->
988 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
990 let cmd =
991 let b = makecmd "search"
992 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
994 Buffer.add_char b ',';
995 Buffer.add_string b pattern;
996 Buffer.add_char b '\000';
997 Buffer.contents b;
999 writecmd state.csock cmd;
1002 let intentry text key =
1003 let c = Char.unsafe_chr key in
1004 match c with
1005 | '0' .. '9' ->
1006 let s = "x" in s.[0] <- c;
1007 let text = text ^ s in
1008 TEcont text
1010 | _ ->
1011 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1012 TEcont text
1015 let addchar s c =
1016 let b = Buffer.create (String.length s + 1) in
1017 Buffer.add_string b s;
1018 Buffer.add_char b c;
1019 Buffer.contents b;
1022 let textentry text key =
1023 let c = Char.unsafe_chr key in
1024 match c with
1025 | _ when key >= 32 && key < 127 ->
1026 let text = addchar text c in
1027 TEcont text
1029 | _ ->
1030 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1031 TEcont text
1034 let reinit angle proportional =
1035 conf.angle <- angle;
1036 conf.proportional <- proportional;
1037 invalidate ();
1038 wcmd "reinit" [`i angle; `b proportional];
1041 let optentry text key =
1042 let btos b = if b then "on" else "off" in
1043 let c = Char.unsafe_chr key in
1044 match c with
1045 | 's' ->
1046 let ondone s =
1047 try conf.scrollincr <- int_of_string s with exc ->
1048 state.text <- Printf.sprintf "bad integer `%s': %s"
1049 s (Printexc.to_string exc)
1051 TEswitch ('#', "", None, intentry, ondone)
1053 | 'R' ->
1054 let ondone s =
1055 match try
1056 Some (int_of_string s)
1057 with exc ->
1058 state.text <- Printf.sprintf "bad integer `%s': %s"
1059 s (Printexc.to_string exc);
1060 None
1061 with
1062 | Some angle -> reinit angle conf.proportional
1063 | None -> ()
1065 TEswitch ('^', "", None, intentry, ondone)
1067 | 'i' ->
1068 conf.icase <- not conf.icase;
1069 TEdone ("case insensitive search " ^ (btos conf.icase))
1071 | 'p' ->
1072 conf.preload <- not conf.preload;
1073 gotoy state.y;
1074 TEdone ("preload " ^ (btos conf.preload))
1076 | 'v' ->
1077 conf.verbose <- not conf.verbose;
1078 TEdone ("verbose " ^ (btos conf.verbose))
1080 | 'h' ->
1081 conf.maxhfit <- not conf.maxhfit;
1082 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1083 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1085 | 'c' ->
1086 conf.crophack <- not conf.crophack;
1087 TEdone ("crophack " ^ btos conf.crophack)
1089 | 'a' ->
1090 conf.showall <- not conf.showall;
1091 TEdone ("showall " ^ btos conf.showall)
1093 | 'f' ->
1094 conf.underinfo <- not conf.underinfo;
1095 TEdone ("underinfo " ^ btos conf.underinfo)
1097 | 'P' ->
1098 conf.savebmarks <- not conf.savebmarks;
1099 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1101 | 'S' ->
1102 let ondone s =
1104 let pageno, py =
1105 match state.layout with
1106 | [] -> 0, 0
1107 | l :: _ ->
1108 l.pageno, l.pagey
1110 conf.interpagespace <- int_of_string s;
1111 state.maxy <- calcheight ();
1112 let y = getpagey pageno in
1113 gotoy (y + py)
1114 with exc ->
1115 state.text <- Printf.sprintf "bad integer `%s': %s"
1116 s (Printexc.to_string exc)
1118 TEswitch ('%', "", None, intentry, ondone)
1120 | 'l' ->
1121 reinit conf.angle (not conf.proportional);
1122 TEdone ("proprortional display " ^ btos conf.proportional)
1124 | _ ->
1125 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1126 TEstop
1129 let maxoutlinerows () = (conf.winh - 31) / 16;;
1131 let enterselector allowdel outlines errmsg msg =
1132 if Array.length outlines = 0
1133 then (
1134 showtext ' ' errmsg;
1136 else (
1137 state.text <- msg;
1138 Glut.setCursor Glut.CURSOR_INHERIT;
1139 let pageno =
1140 match state.layout with
1141 | [] -> -1
1142 | {pageno=pageno} :: rest -> pageno
1144 let active =
1145 let rec loop n =
1146 if n = Array.length outlines
1147 then 0
1148 else
1149 let (_, _, outlinepageno, _) = outlines.(n) in
1150 if outlinepageno >= pageno then n else loop (n+1)
1152 loop 0
1154 state.mode <- Outline
1155 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "");
1156 Glut.postRedisplay ();
1160 let enteroutlinemode () =
1161 let outlines, msg =
1162 match state.outlines with
1163 | Oarray a -> a, ""
1164 | Olist l ->
1165 let a = Array.of_list (List.rev l) in
1166 state.outlines <- Oarray a;
1167 a, ""
1168 | Onarrow (pat, a, b) ->
1169 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1171 enterselector false outlines "Document has no outline" msg;
1174 let enterbookmarkmode () =
1175 let bookmarks = Array.of_list state.bookmarks in
1176 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1179 let quickbookmark ?title () =
1180 match state.layout with
1181 | [] -> ()
1182 | l :: _ ->
1183 let title =
1184 match title with
1185 | None ->
1186 let sec = Unix.gettimeofday () in
1187 let tm = Unix.localtime sec in
1188 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1189 (l.pageno+1)
1190 tm.Unix.tm_mday
1191 tm.Unix.tm_mon
1192 (tm.Unix.tm_year + 1900)
1193 tm.Unix.tm_hour
1194 tm.Unix.tm_min
1195 | Some title -> title
1197 state.bookmarks <-
1198 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1201 let doreshape w h =
1202 state.fullscreen <- None;
1203 Glut.reshapeWindow w h;
1206 let writeopen path password =
1207 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1210 let opendoc path password =
1211 invalidate ();
1212 state.path <- path;
1213 state.password <- password;
1214 state.gen <- state.gen + 1;
1216 writeopen path password;
1217 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1218 wcmd "geometry" [`i state.w; `i conf.winh];
1221 let birdseyeon () =
1222 let zoom = float conf.thumbw /. float conf.winw in
1223 let (birdseyepageno, _) as anchor = getanchor () in
1224 state.mode <-
1225 Birdseye ({ conf with zoom = conf.zoom }, state.x, birdseyepageno, -1);
1226 conf.zoom <- zoom;
1227 conf.presentation <- false;
1228 conf.interpagespace <- 10;
1229 conf.hlinks <- false;
1230 state.x <- 0;
1231 state.mstate <- Mnone;
1232 conf.showall <- false;
1233 Glut.setCursor Glut.CURSOR_INHERIT;
1234 if conf.verbose
1235 then
1236 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1237 (100.0*.zoom)
1238 else
1239 state.text <- ""
1243 let birdseyeoff (c, leftx, pageno, _) =
1244 state.mode <- View;
1245 conf.zoom <- c.zoom;
1246 conf.presentation <- c.presentation;
1247 conf.interpagespace <- c.interpagespace;
1248 conf.showall <- c.showall;
1249 conf.hlinks <- c.hlinks;
1250 state.x <- leftx;
1251 if conf.verbose
1252 then
1253 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1254 (100.0*.conf.zoom)
1258 let togglebirdseye () =
1259 match state.mode with
1260 | Birdseye vals -> birdseyeoff vals
1261 | View | Outline _ -> birdseyeon ()
1262 | _ -> ()
1265 let viewkeyboard ~key ~x ~y =
1266 let enttext te =
1267 state.mode <- Textentry te;
1268 state.text <- "";
1269 enttext ();
1270 Glut.postRedisplay ()
1272 let c = Char.chr key in
1273 match c with
1274 | '\027' | 'q' ->
1275 exit 0
1277 | '\008' ->
1278 let y = getnav () in
1279 gotoy_and_clear_text y
1281 | 'o' ->
1282 enteroutlinemode ()
1284 | 'u' ->
1285 state.rects <- [];
1286 state.text <- "";
1287 Glut.postRedisplay ()
1289 | '/' | '?' ->
1290 let ondone isforw s =
1291 cbput state.hists.pat s;
1292 state.searchpattern <- s;
1293 search s isforw
1295 enttext (c, "", Some (onhist state.hists.pat),
1296 textentry, ondone (c ='/'))
1298 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1299 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1300 conf.zoom <- min 2.2 (conf.zoom +. incr);
1301 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1302 reshape conf.winw conf.winh
1304 | '+' ->
1305 let ondone s =
1306 let n =
1307 try int_of_string s with exc ->
1308 state.text <- Printf.sprintf "bad integer `%s': %s"
1309 s (Printexc.to_string exc);
1310 max_int
1312 if n != max_int
1313 then (
1314 conf.pagebias <- n;
1315 state.text <- "page bias is now " ^ string_of_int n;
1318 enttext ('+', "", None, intentry, ondone)
1320 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1321 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1322 conf.zoom <- max 0.01 (conf.zoom -. decr);
1323 if conf.zoom <= 1.0 then state.x <- 0;
1324 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1325 reshape conf.winw conf.winh;
1327 | '-' ->
1328 let ondone msg =
1329 state.text <- msg;
1331 enttext ('-', "", None, optentry, ondone)
1333 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1334 state.x <- 0;
1335 conf.zoom <- 1.0;
1336 state.text <- "zoom is 100%";
1337 reshape conf.winw conf.winh
1339 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1340 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1341 if zoom < 1.0
1342 then (
1343 conf.zoom <- zoom;
1344 state.x <- 0;
1345 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1346 reshape conf.winw conf.winh;
1349 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1350 togglebirdseye ();
1351 reshape conf.winw conf.winh;
1353 | '0' .. '9' ->
1354 let ondone s =
1355 let n =
1356 try int_of_string s with exc ->
1357 state.text <- Printf.sprintf "bad integer `%s': %s"
1358 s (Printexc.to_string exc);
1361 if n >= 0
1362 then (
1363 addnav ();
1364 cbput state.hists.pag (string_of_int n);
1365 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1368 let pageentry text key =
1369 match Char.unsafe_chr key with
1370 | 'g' -> TEdone text
1371 | _ -> intentry text key
1373 let text = "x" in text.[0] <- c;
1374 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1376 | 'b' ->
1377 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1378 reshape conf.winw conf.winh;
1380 | 'l' ->
1381 conf.hlinks <- not conf.hlinks;
1382 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1383 Glut.postRedisplay ()
1385 | 'a' ->
1386 conf.autoscroll <- not conf.autoscroll
1388 | 'P' ->
1389 conf.presentation <- not conf.presentation;
1390 showtext ' ' ("presentation mode " ^
1391 if conf.presentation then "on" else "off");
1392 represent ()
1394 | 'f' ->
1395 begin match state.fullscreen with
1396 | None ->
1397 state.fullscreen <- Some (conf.winw, conf.winh);
1398 Glut.fullScreen ()
1399 | Some (w, h) ->
1400 state.fullscreen <- None;
1401 doreshape w h
1404 | 'g' ->
1405 gotoy_and_clear_text 0
1407 | 'n' ->
1408 search state.searchpattern true
1410 | 'p' | 'N' ->
1411 search state.searchpattern false
1413 | 't' ->
1414 begin match state.layout with
1415 | [] -> ()
1416 | l :: _ ->
1417 gotoy_and_clear_text (getpagey l.pageno)
1420 | ' ' ->
1421 begin match List.rev state.layout with
1422 | [] -> ()
1423 | l :: _ ->
1424 let pageno = min (l.pageno+1) (state.pagecount-1) in
1425 gotoy_and_clear_text (getpagey pageno)
1428 | '\127' ->
1429 begin match state.layout with
1430 | [] -> ()
1431 | l :: _ ->
1432 let pageno = max 0 (l.pageno-1) in
1433 gotoy_and_clear_text (getpagey pageno)
1436 | '=' ->
1437 let f (fn, ln) l =
1438 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1440 let fn, ln = List.fold_left f (-1, -1) state.layout in
1441 let s =
1442 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1443 let percent =
1444 if maxy <= 0
1445 then 100.
1446 else (100. *. (float state.y /. float maxy)) in
1447 if fn = ln
1448 then
1449 Printf.sprintf "Page %d of %d %.2f%%"
1450 (fn+1) state.pagecount percent
1451 else
1452 Printf.sprintf
1453 "Pages %d-%d of %d %.2f%%"
1454 (fn+1) (ln+1) state.pagecount percent
1456 showtext ' ' s;
1458 | 'w' ->
1459 begin match state.layout with
1460 | [] -> ()
1461 | l :: _ ->
1462 doreshape (l.pagew + conf.scrollw) l.pageh;
1463 Glut.postRedisplay ();
1466 | '\'' ->
1467 enterbookmarkmode ()
1469 | 'm' ->
1470 let ondone s =
1471 match state.layout with
1472 | l :: _ ->
1473 state.bookmarks <-
1474 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1475 :: state.bookmarks
1476 | _ -> ()
1478 enttext ('~', "", None, textentry, ondone)
1480 | '~' ->
1481 quickbookmark ();
1482 showtext ' ' "Quick bookmark added";
1484 | 'z' ->
1485 begin match state.layout with
1486 | l :: _ ->
1487 let rect = getpdimrect l.pagedimno in
1488 let w, h =
1489 if conf.crophack
1490 then
1491 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1492 truncate (1.2 *. (rect.(3) -. rect.(0))))
1493 else
1494 (truncate (rect.(1) -. rect.(0)),
1495 truncate (rect.(3) -. rect.(0)))
1497 if w != 0 && h != 0
1498 then
1499 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1501 Glut.postRedisplay ();
1503 | [] -> ()
1506 | '<' | '>' ->
1507 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1509 | '[' | ']' ->
1510 state.colorscale <-
1511 max 0.0
1512 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1513 Glut.postRedisplay ()
1515 | 'k' -> gotoy (clamp (-conf.scrollincr))
1516 | 'j' -> gotoy (clamp conf.scrollincr)
1518 | 'r' -> opendoc state.path state.password
1520 | _ ->
1521 vlog "huh? %d %c" key (Char.chr key);
1524 let textentrykeyboard ~key ~x ~y (c, text, opthist, onkey, ondone) =
1525 let enttext te =
1526 state.mode <- Textentry te;
1527 state.text <- "";
1528 enttext ();
1529 Glut.postRedisplay ()
1531 match Char.unsafe_chr key with
1532 | '\008' ->
1533 let len = String.length text in
1534 if len = 0
1535 then (
1536 state.mode <- View;
1537 Glut.postRedisplay ();
1539 else (
1540 let s = String.sub text 0 (len - 1) in
1541 enttext (c, s, opthist, onkey, ondone)
1544 | '\r' | '\n' ->
1545 ondone text;
1546 state.mode <- View;
1547 Glut.postRedisplay ()
1549 | '\027' ->
1550 begin match opthist with
1551 | None -> ()
1552 | Some (_, onhistcancel) -> onhistcancel ()
1553 end;
1554 state.mode <- View;
1555 Glut.postRedisplay ()
1557 | _ ->
1558 begin match onkey text key with
1559 | TEdone text ->
1560 state.mode <- View;
1561 ondone text;
1562 Glut.postRedisplay ()
1564 | TEcont text ->
1565 enttext (c, text, opthist, onkey, ondone);
1567 | TEstop ->
1568 state.mode <- View;
1569 Glut.postRedisplay ()
1571 | TEswitch te ->
1572 state.mode <- Textentry te;
1573 Glut.postRedisplay ()
1574 end;
1577 let birdseyekeyboard ~key ~x ~y ((c, leftx, pageno, hooverpageno) as beye) =
1578 match key with
1579 | 27 ->
1580 birdseyeoff beye;
1581 reshape conf.winw conf.winh
1583 | 12 ->
1584 let y, h = getpageyh pageno in
1585 let top = (conf.winh - h) / 2 in
1586 gotoy (max 0 (y - top))
1588 | 13 ->
1589 addnav ();
1590 birdseyeoff beye;
1591 reshape conf.winw conf.winh;
1592 state.anchor <- (pageno, 0.0);
1594 | 0x39 when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1595 birdseyeon ();
1596 reshape conf.winw conf.winh;
1598 | _ ->
1599 viewkeyboard ~key ~x ~y
1602 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1603 let narrow outlines pattern =
1604 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1605 match reopt with
1606 | None -> None
1607 | Some re ->
1608 let rec fold accu n =
1609 if n = -1
1610 then accu
1611 else
1612 let (s, _, _, _) as o = outlines.(n) in
1613 let accu =
1614 if (try ignore (Str.search_forward re s 0); true
1615 with Not_found -> false)
1616 then (o :: accu)
1617 else accu
1619 fold accu (n-1)
1621 let matched = fold [] (Array.length outlines - 1) in
1622 if matched = [] then None else Some (Array.of_list matched)
1624 let search active pattern incr =
1625 let dosearch re =
1626 let rec loop n =
1627 if n = Array.length outlines || n = -1
1628 then None
1629 else
1630 let (s, _, _, _) = outlines.(n) in
1632 (try ignore (Str.search_forward re s 0); true
1633 with Not_found -> false)
1634 then Some n
1635 else loop (n + incr)
1637 loop active
1640 let re = Str.regexp_case_fold pattern in
1641 dosearch re
1642 with Failure s ->
1643 state.text <- s;
1644 None
1646 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1647 match key with
1648 | 27 ->
1649 if String.length qsearch = 0
1650 then (
1651 state.text <- "";
1652 state.mode <- View;
1653 Glut.postRedisplay ();
1655 else (
1656 state.text <- "";
1657 state.mode <- Outline (allowdel, active, first, outlines, "");
1658 Glut.postRedisplay ();
1661 | 18 | 19 ->
1662 let incr = if key = 18 then -1 else 1 in
1663 let active, first =
1664 match search (active + incr) qsearch incr with
1665 | None ->
1666 state.text <- qsearch ^ " [not found]";
1667 active, first
1668 | Some active ->
1669 state.text <- qsearch;
1670 active, firstof active
1672 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1673 Glut.postRedisplay ();
1675 | 8 ->
1676 let len = String.length qsearch in
1677 if len = 0
1678 then ()
1679 else (
1680 if len = 1
1681 then (
1682 state.text <- "";
1683 state.mode <- Outline (allowdel, active, first, outlines, "");
1685 else
1686 let qsearch = String.sub qsearch 0 (len - 1) in
1687 let active, first =
1688 match search active qsearch ~-1 with
1689 | None ->
1690 state.text <- qsearch ^ " [not found]";
1691 active, first
1692 | Some active ->
1693 state.text <- qsearch;
1694 active, firstof active
1696 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1698 Glut.postRedisplay ()
1700 | 13 ->
1701 if active < Array.length outlines
1702 then (
1703 let (_, _, n, t) = outlines.(active) in
1704 gotopage n t;
1706 state.text <- "";
1707 if allowdel then state.bookmarks <- Array.to_list outlines;
1708 state.mode <- View;
1709 Glut.postRedisplay ();
1711 | _ when key >= 32 && key < 127 ->
1712 let pattern = addchar qsearch (Char.chr key) in
1713 let active, first =
1714 match search active pattern 1 with
1715 | None ->
1716 state.text <- pattern ^ " [not found]";
1717 active, first
1718 | Some active ->
1719 state.text <- pattern;
1720 active, firstof active
1722 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1723 Glut.postRedisplay ()
1725 | 14 when not allowdel -> (* ctrl-n *)
1726 if String.length qsearch > 0
1727 then (
1728 let optoutlines = narrow outlines qsearch in
1729 begin match optoutlines with
1730 | None -> state.text <- "can't narrow"
1731 | Some outlines ->
1732 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1733 match state.outlines with
1734 | Olist l -> ()
1735 | Oarray a ->
1736 state.outlines <- Onarrow (qsearch, outlines, a)
1737 | Onarrow (pat, a, b) ->
1738 state.outlines <- Onarrow (qsearch, outlines, b)
1739 end;
1741 Glut.postRedisplay ()
1743 | 21 when not allowdel -> (* ctrl-u *)
1744 let outline =
1745 match state.outlines with
1746 | Oarray a -> a
1747 | Olist l ->
1748 let a = Array.of_list (List.rev l) in
1749 state.outlines <- Oarray a;
1751 | Onarrow (pat, a, b) ->
1752 state.outlines <- Oarray b;
1753 state.text <- "";
1756 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1757 Glut.postRedisplay ()
1759 | 12 ->
1760 state.mode <- Outline
1761 (allowdel, active, firstof active, outlines, qsearch);
1762 Glut.postRedisplay ()
1764 | 127 when allowdel ->
1765 let len = Array.length outlines - 1 in
1766 if len = 0
1767 then (
1768 state.mode <- View;
1769 state.bookmarks <- [];
1771 else (
1772 let bookmarks = Array.init len
1773 (fun i ->
1774 let i = if i >= active then i + 1 else i in
1775 outlines.(i)
1778 state.mode <-
1779 Outline (
1780 allowdel,
1781 min active (len-1),
1782 min first (len-1),
1783 bookmarks, qsearch
1786 Glut.postRedisplay ()
1788 | _ -> log "unknown key %d" key
1791 let keyboard ~key ~x ~y =
1792 if key = 7
1793 then
1794 wcmd "interrupt" []
1795 else
1796 match state.mode with
1797 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1798 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1799 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1800 | View -> viewkeyboard ~key ~x ~y
1803 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno) =
1804 match key with
1805 | Glut.KEY_UP ->
1806 let pageno = max 0 (pageno - 1) in
1807 let rec loop = function
1808 | [] -> gotopage1nonav pageno 0
1809 | l :: _ when l.pageno = pageno ->
1810 if l.pagedispy >= 0 && l.pagey = 0
1811 then Glut.postRedisplay ()
1812 else gotopage1nonav pageno 0
1813 | _ :: rest -> loop rest
1815 loop state.layout;
1816 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno)
1818 | Glut.KEY_DOWN ->
1819 let pageno = min (state.pagecount - 1) (pageno + 1) in
1820 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno);
1821 let rec loop = function
1822 | [] ->
1823 let y, h = getpageyh pageno in
1824 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1825 gotoy (clamp dy)
1826 | l :: rest when l.pageno = pageno ->
1827 if l.pagevh != l.pageh
1828 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1829 else Glut.postRedisplay ()
1830 | l :: rest -> loop rest
1832 loop state.layout
1834 | Glut.KEY_PAGE_UP ->
1835 begin match state.layout with
1836 | l :: _ ->
1837 if l.pagey != 0
1838 then (
1839 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1840 gotopage1nonav l.pageno 0;
1842 else (
1843 let layout = layout (state.y-conf.winh) conf.winh in
1844 match layout with
1845 | [] -> gotoy (clamp (-conf.winh))
1846 | l :: _ ->
1847 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1848 gotopage1nonav l.pageno 0
1851 | [] -> gotoy (clamp (-conf.winh))
1852 end;
1854 | Glut.KEY_PAGE_DOWN ->
1855 begin match List.rev state.layout with
1856 | l :: _ ->
1857 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno);
1858 gotoy (clamp (l.pagedispy + l.pageh))
1859 | [] -> gotoy (clamp conf.winh)
1860 end;
1862 | Glut.KEY_HOME ->
1863 state.mode <- Birdseye (conf, leftx, 0, hooverpageno);
1864 gotopage1nonav 0 0
1866 | Glut.KEY_END ->
1867 let pageno = state.pagecount - 1 in
1868 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno);
1869 if not (pagevisible state.layout pageno)
1870 then
1871 let h =
1872 match List.rev state.pdims with
1873 | [] -> conf.winh
1874 | (_, _, h, _) :: _ -> h
1876 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
1877 else Glut.postRedisplay ();
1878 | _ -> ()
1881 let special ~key ~x ~y =
1882 match state.mode with
1883 | View | (Birdseye _) when key = Glut.KEY_F9 ->
1884 togglebirdseye ();
1885 reshape conf.winw conf.winh;
1887 | Birdseye vals ->
1888 birdseyespecial key x y vals
1890 | View | Textentry _ ->
1891 begin match state.mode with
1892 | View ->
1893 let y =
1894 match key with
1895 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1896 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1897 | Glut.KEY_DOWN -> clamp conf.scrollincr
1898 | Glut.KEY_PAGE_UP ->
1899 if Glut.getModifiers () land Glut.active_ctrl != 0
1900 then
1901 match state.layout with
1902 | [] -> state.y
1903 | l :: _ -> state.y - l.pagey
1904 else
1905 clamp (-conf.winh)
1906 | Glut.KEY_PAGE_DOWN ->
1907 if Glut.getModifiers () land Glut.active_ctrl != 0
1908 then
1909 match List.rev state.layout with
1910 | [] -> state.y
1911 | l :: _ -> getpagey l.pageno
1912 else
1913 clamp conf.winh
1914 | Glut.KEY_HOME -> addnav (); 0
1915 | Glut.KEY_END ->
1916 addnav ();
1917 state.maxy - (if conf.maxhfit then conf.winh else 0)
1919 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1920 state.x <- state.x - 10;
1921 state.y
1922 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1923 state.x <- state.x + 10;
1924 state.y
1926 | _ -> state.y
1928 gotoy_and_clear_text y
1930 | Textentry (c, s, (Some (action, _) as onhist), onkey, ondone) ->
1931 let s =
1932 match key with
1933 | Glut.KEY_UP -> action HCprev
1934 | Glut.KEY_DOWN -> action HCnext
1935 | Glut.KEY_HOME -> action HCfirst
1936 | Glut.KEY_END -> action HClast
1937 | _ -> state.text
1939 state.mode <- Textentry (c, s, onhist, onkey, ondone);
1940 Glut.postRedisplay ()
1942 | _ -> ()
1945 | Outline (allowdel, active, first, outlines, qsearch) ->
1946 let maxrows = maxoutlinerows () in
1947 let calcfirst first active =
1948 if active > first
1949 then
1950 let rows = active - first in
1951 if rows > maxrows then active - maxrows else first
1952 else active
1954 let navigate incr =
1955 let active = active + incr in
1956 let active = max 0 (min active (Array.length outlines - 1)) in
1957 let first = calcfirst first active in
1958 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1959 Glut.postRedisplay ()
1961 let updownlevel incr =
1962 let len = Array.length outlines in
1963 let (_, curlevel, _, _) = outlines.(active) in
1964 let rec flow i =
1965 if i = len then i-1 else if i = -1 then 0 else
1966 let (_, l, _, _) = outlines.(i) in
1967 if l != curlevel then i else flow (i+incr)
1969 let active = flow active in
1970 let first = calcfirst first active in
1971 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1972 Glut.postRedisplay ()
1974 match key with
1975 | Glut.KEY_UP -> navigate ~-1
1976 | Glut.KEY_DOWN -> navigate 1
1977 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1978 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1980 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
1981 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
1983 | Glut.KEY_HOME ->
1984 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1985 Glut.postRedisplay ()
1987 | Glut.KEY_END ->
1988 let active = Array.length outlines - 1 in
1989 let first = max 0 (active - maxrows) in
1990 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1991 Glut.postRedisplay ()
1993 | _ -> ()
1996 let drawplaceholder l =
1997 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
1998 GlDraw.rect
1999 (float l.pagex, float l.pagedispy)
2000 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2002 let x = float (if margin < 0 then -margin else l.pagex)
2003 and y = float (l.pagedispy + 13) in
2004 let font = Glut.BITMAP_8_BY_13 in
2005 GlDraw.color (0.0, 0.0, 0.0);
2006 GlPix.raster_pos ~x ~y ();
2007 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2008 ("Loading " ^ string_of_int (l.pageno + 1));
2011 let now () = Unix.gettimeofday ();;
2013 let drawpage l =
2014 let color =
2015 match state.mode with
2016 | Textentry _ -> scalecolor 0.4
2017 | View | Outline _ -> scalecolor 1.0
2018 | Birdseye (_, _, pageno, hooverpageno) ->
2019 if l.pageno = pageno
2020 then scalecolor 1.0
2021 else (
2022 if l.pageno = hooverpageno
2023 then scalecolor 0.9
2024 else scalecolor 0.8
2027 GlDraw.color color;
2028 begin match getopaque l.pageno with
2029 | Some (opaque, _) when validopaque opaque ->
2030 let a = now () in
2031 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2032 opaque;
2033 let b = now () in
2034 let d = b-.a in
2035 vlog "draw %d %f sec" l.pageno d;
2037 | _ ->
2038 drawplaceholder l;
2039 end;
2042 let scrollph y =
2043 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2044 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2045 let sh = float conf.winh /. sh in
2046 let sh = max sh (float conf.scrollh) in
2048 let percent =
2049 if state.y = state.maxy
2050 then 1.0
2051 else float y /. float maxy
2053 let position = (float conf.winh -. sh) *. percent in
2055 let position =
2056 if position +. sh > float conf.winh
2057 then float conf.winh -. sh
2058 else position
2060 position, sh;
2063 let scrollindicator () =
2064 GlDraw.color (0.64 , 0.64, 0.64);
2065 GlDraw.rect
2066 (float (conf.winw - conf.scrollw), 0.)
2067 (float conf.winw, float conf.winh)
2069 GlDraw.color (0.0, 0.0, 0.0);
2071 let position, sh = scrollph state.y in
2072 GlDraw.rect
2073 (float (conf.winw - conf.scrollw), position)
2074 (float conf.winw, position +. sh)
2078 let showsel margin =
2079 match state.mstate with
2080 | Mnone | Mscroll _ | Mpan _ ->
2083 | Msel ((x0, y0), (x1, y1)) ->
2084 let rec loop = function
2085 | l :: ls ->
2086 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2087 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2088 then
2089 match getopaque l.pageno with
2090 | Some (opaque, _) when validopaque opaque ->
2091 let oy = -l.pagey + l.pagedispy in
2092 seltext opaque
2093 (x0 - margin - state.x, y0,
2094 x1 - margin - state.x, y1) oy;
2096 | _ -> ()
2097 else loop ls
2098 | [] -> ()
2100 loop state.layout
2103 let showrects () =
2104 let panx = float state.x in
2105 Gl.enable `blend;
2106 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2107 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2108 List.iter
2109 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2110 List.iter (fun l ->
2111 if l.pageno = pageno
2112 then (
2113 let d = float (l.pagedispy - l.pagey) in
2114 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2115 GlDraw.begins `quads;
2117 GlDraw.vertex2 (x0+.panx, y0+.d);
2118 GlDraw.vertex2 (x1+.panx, y1+.d);
2119 GlDraw.vertex2 (x2+.panx, y2+.d);
2120 GlDraw.vertex2 (x3+.panx, y3+.d);
2122 GlDraw.ends ();
2124 ) state.layout
2125 ) state.rects
2127 Gl.disable `blend;
2130 let showoutline = function
2131 | Outline (allowdel, active, first, outlines, qsearch) ->
2132 Gl.enable `blend;
2133 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2134 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2135 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2136 Gl.disable `blend;
2138 GlDraw.color (1., 1., 1.);
2139 let font = Glut.BITMAP_9_BY_15 in
2140 let draw_string x y s =
2141 GlPix.raster_pos ~x ~y ();
2142 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2144 let rec loop row =
2145 if row = Array.length outlines || (row - first) * 16 > conf.winh
2146 then ()
2147 else (
2148 let (s, l, _, _) = outlines.(row) in
2149 let y = (row - first) * 16 in
2150 let x = 5 + 15*l in
2151 if row = active
2152 then (
2153 Gl.enable `blend;
2154 GlDraw.polygon_mode `both `line;
2155 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2156 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2157 GlDraw.rect (0., float (y + 1))
2158 (float (conf.winw - 1), float (y + 18));
2159 GlDraw.polygon_mode `both `fill;
2160 Gl.disable `blend;
2161 GlDraw.color (1., 1., 1.);
2163 draw_string (float x) (float (y + 16)) s;
2164 loop (row+1)
2167 loop first
2169 | _ -> ()
2172 let display () =
2173 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2174 GlDraw.viewport margin 0 state.w conf.winh;
2175 pagematrix ();
2176 GlClear.color (scalecolor 0.5);
2177 GlClear.clear [`color];
2178 if state.x != 0
2179 then (
2180 let x = float state.x in
2181 GlMat.translate ~x ();
2183 if conf.zoom > 1.0
2184 then (
2185 Gl.enable `scissor_test;
2186 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2188 List.iter drawpage state.layout;
2189 if conf.zoom > 1.0
2190 then
2191 Gl.disable `scissor_test
2193 if state.x != 0
2194 then (
2195 let x = -.float state.x in
2196 GlMat.translate ~x ();
2198 showrects ();
2199 showsel margin;
2200 GlDraw.viewport 0 0 conf.winw conf.winh;
2201 winmatrix ();
2202 scrollindicator ();
2203 showoutline state.mode;
2204 enttext ();
2205 Glut.swapBuffers ();
2208 let getunder x y =
2209 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2210 let x = x - margin - state.x in
2211 let rec f = function
2212 | l :: rest ->
2213 begin match getopaque l.pageno with
2214 | Some (opaque, _) when validopaque opaque ->
2215 let y = y - l.pagedispy in
2216 if y > 0
2217 then
2218 let y = l.pagey + y in
2219 let x = x - l.pagex in
2220 match whatsunder opaque x y with
2221 | Unone -> f rest
2222 | under -> under
2223 else
2224 f rest
2225 | _ ->
2226 f rest
2228 | [] -> Unone
2230 f state.layout
2233 let mouse ~button ~bstate ~x ~y =
2234 match button with
2235 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2236 let incr =
2237 if n = 3
2238 then
2239 -conf.scrollincr
2240 else
2241 conf.scrollincr
2243 let incr = incr * 2 in
2244 let y = clamp incr in
2245 gotoy_and_clear_text y
2247 | Glut.LEFT_BUTTON when not (isoutline state.mode)
2248 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
2249 if bstate = Glut.DOWN
2250 then (
2251 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2252 state.mstate <- Mpan (x, y)
2254 else
2255 state.mstate <- Mnone
2257 | Glut.LEFT_BUTTON
2258 when not (isoutline state.mode) && x > conf.winw - conf.scrollw ->
2259 if bstate = Glut.DOWN
2260 then
2261 let position, sh = scrollph state.y in
2262 if y > truncate position && y < truncate (position +. sh)
2263 then
2264 state.mstate <- Mscroll
2265 else
2266 let percent = float y /. float conf.winh in
2267 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2268 gotoy desty;
2269 state.mstate <- Mscroll
2270 else
2271 state.mstate <- Mnone
2273 | Glut.LEFT_BUTTON
2274 when not (isoutline state.mode) && (isbirdseye state.mode) ->
2275 begin match state.mode with
2276 | Birdseye (conf, leftx, pageno, hooverpageno) ->
2277 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2278 let rec loop = function
2279 | [] -> ()
2280 | l :: rest ->
2281 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2282 && x > margin && x < margin + l.pagew
2283 then (
2284 birdseyeoff (conf, leftx, l.pageno, hooverpageno);
2285 reshape conf.winw conf.winh;
2286 state.anchor <- (l.pageno, 0.0);
2288 else loop rest
2290 loop state.layout;
2291 | _ -> () (* impossible *)
2294 | Glut.LEFT_BUTTON when not (isoutline state.mode) ->
2295 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2296 begin match dest with
2297 | Ulinkgoto (pageno, top) ->
2298 if pageno >= 0
2299 then
2300 gotopage1 pageno top
2302 | Ulinkuri s ->
2303 print_endline s
2305 | Unone when bstate = Glut.DOWN ->
2306 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2307 state.mstate <- Mpan (x, y);
2309 | Unone | Utext _ ->
2310 if bstate = Glut.DOWN
2311 then (
2312 if conf.angle mod 360 = 0
2313 then (
2314 state.mstate <- Msel ((x, y), (x, y));
2315 Glut.postRedisplay ()
2318 else (
2319 match state.mstate with
2320 | Mnone -> ()
2322 | Mscroll ->
2323 state.mstate <- Mnone
2325 | Mpan _ ->
2326 Glut.setCursor Glut.CURSOR_INHERIT;
2327 state.mstate <- Mnone
2329 | Msel ((x0, y0), (x1, y1)) ->
2330 let f l =
2331 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2332 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2333 then
2334 match getopaque l.pageno with
2335 | Some (opaque, _) when validopaque opaque ->
2336 copysel opaque
2337 | _ -> ()
2339 List.iter f state.layout;
2340 copysel ""; (* ugly *)
2341 Glut.setCursor Glut.CURSOR_INHERIT;
2342 state.mstate <- Mnone;
2346 | _ ->
2349 let mouse ~button ~state ~x ~y = mouse button state x y;;
2351 let motion ~x ~y =
2352 match state.mode with
2353 | Outline _ -> ()
2354 | _ ->
2355 match state.mstate with
2356 | Mnone -> ()
2358 | Mpan (x0, y0) ->
2359 let dx = x - x0
2360 and dy = y0 - y in
2361 state.mstate <- Mpan (x, y);
2362 if conf.zoom > 1.0 then state.x <- state.x + dx;
2363 let y = clamp dy in
2364 gotoy_and_clear_text y
2366 | Msel (a, _) ->
2367 state.mstate <- Msel (a, (x, y));
2368 Glut.postRedisplay ()
2370 | Mscroll ->
2371 let y = min conf.winh (max 0 y) in
2372 let percent = float y /. float conf.winh in
2373 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2374 gotoy_and_clear_text y
2377 let pmotion ~x ~y =
2378 match state.mode with
2379 | Birdseye (conf, leftx, pageno, hooverpageno) ->
2380 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2381 let rec loop = function
2382 | [] ->
2383 if hooverpageno != -1
2384 then (
2385 state.mode <- Birdseye (conf, leftx, pageno, -1);
2386 Glut.postRedisplay ();
2388 | l :: rest ->
2389 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2390 && x > margin && x < margin + l.pagew
2391 then (
2392 state.mode <- Birdseye (conf, leftx, pageno, l.pageno);
2393 Glut.postRedisplay ();
2395 else loop rest
2397 loop state.layout
2399 | Outline _ -> ()
2400 | _ ->
2401 match state.mstate with
2402 | Mnone ->
2403 begin match getunder x y with
2404 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2405 | Ulinkuri uri ->
2406 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2407 Glut.setCursor Glut.CURSOR_INFO
2408 | Ulinkgoto (page, y) ->
2409 if conf.underinfo
2410 then showtext 'p' ("age: " ^ string_of_int page);
2411 Glut.setCursor Glut.CURSOR_INFO
2412 | Utext s ->
2413 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2414 Glut.setCursor Glut.CURSOR_TEXT
2417 | Mpan _ | Msel _ | Mscroll ->
2422 module State =
2423 struct
2424 open Parser
2426 let home =
2428 match Sys.os_type with
2429 | "Win32" -> Sys.getenv "HOMEPATH"
2430 | _ -> Sys.getenv "HOME"
2431 with exn ->
2432 prerr_endline
2433 ("Can not determine home directory location: " ^
2434 Printexc.to_string exn);
2438 let config_of c attrs =
2439 let apply c k v =
2441 match k with
2442 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2443 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2444 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2445 | "preload" -> { c with preload = bool_of_string v }
2446 | "page-bias" -> { c with pagebias = int_of_string v }
2447 | "scroll-step" -> { c with scrollincr = max 1 (int_of_string v) }
2448 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2449 | "crop-hack" -> { c with crophack = bool_of_string v }
2450 | "throttle" -> { c with showall = bool_of_string v }
2451 | "highlight-links" -> { c with hlinks = bool_of_string v }
2452 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2453 | "vertical-margin" -> { c with interpagespace = max 0 (int_of_string v) }
2454 | "zoom" ->
2455 let zoom = float_of_string v /. 100. in
2456 let zoom = max 0.01 (min 2.2 zoom) in
2457 { c with zoom = zoom }
2458 | "presentation" -> { c with presentation = bool_of_string v }
2459 | "rotation-angle" -> { c with angle = int_of_string v }
2460 | "width" -> { c with winw = max 20 (int_of_string v) }
2461 | "height" -> { c with winh = max 20 (int_of_string v) }
2462 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2463 | "proportional-display" -> { c with proportional = bool_of_string v }
2464 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2465 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2466 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2467 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2468 | _ -> c
2469 with exn ->
2470 prerr_endline ("Error processing attribute (`" ^
2471 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2474 let rec fold c = function
2475 | [] -> c
2476 | (k, v) :: rest ->
2477 let c = apply c k v in
2478 fold c rest
2480 fold c attrs;
2483 let bookmark_of attrs =
2484 let rec fold title page rely = function
2485 | ("title", v) :: rest -> fold v page rely rest
2486 | ("page", v) :: rest -> fold title v rely rest
2487 | ("rely", v) :: rest -> fold title page v rest
2488 | _ :: rest -> fold title page rely rest
2489 | [] -> title, page, rely
2491 fold "invalid" "0" "0" attrs
2494 let setconf dst src =
2495 dst.scrollw <- src.scrollw;
2496 dst.scrollh <- src.scrollh;
2497 dst.icase <- src.icase;
2498 dst.preload <- src.preload;
2499 dst.pagebias <- src.pagebias;
2500 dst.verbose <- src.verbose;
2501 dst.scrollincr <- src.scrollincr;
2502 dst.maxhfit <- src.maxhfit;
2503 dst.crophack <- src.crophack;
2504 dst.autoscroll <- src.autoscroll;
2505 dst.showall <- src.showall;
2506 dst.hlinks <- src.hlinks;
2507 dst.underinfo <- src.underinfo;
2508 dst.interpagespace <- src.interpagespace;
2509 dst.zoom <- src.zoom;
2510 dst.presentation <- src.presentation;
2511 dst.angle <- src.angle;
2512 dst.winw <- src.winw;
2513 dst.winh <- src.winh;
2514 dst.savebmarks <- src.savebmarks;
2515 dst.memlimit <- src.memlimit;
2516 dst.proportional <- src.proportional;
2517 dst.texcount <- src.texcount;
2518 dst.sliceheight <- src.sliceheight;
2519 dst.thumbw <- src.thumbw;
2522 let unent s =
2523 let l = String.length s in
2524 let b = Buffer.create l in
2525 unent b s 0 l;
2526 Buffer.contents b;
2529 let get s =
2530 let h = Hashtbl.create 10 in
2531 let dc = { defconf with angle = defconf.angle } in
2532 let rec toplevel v t spos epos =
2533 match t with
2534 | Vdata | Vcdata | Vend -> v
2535 | Vopen ("llppconfig", attrs, closed) ->
2536 if closed
2537 then v
2538 else { v with f = llppconfig }
2539 | Vopen _ ->
2540 error "unexpected subelement at top level" s spos
2541 | Vclose tag -> error "unexpected close at top level" s spos
2543 and llppconfig v t spos epos =
2544 match t with
2545 | Vdata | Vcdata | Vend -> v
2546 | Vopen ("defaults", attrs, closed) ->
2547 let c = config_of dc attrs in
2548 setconf dc c;
2549 if closed
2550 then v
2551 else { v with f = skip "defaults" (fun () -> v) }
2553 | Vopen ("doc", attrs, closed) ->
2554 let pathent =
2556 List.assoc "path" attrs
2557 with Not_found -> error "doc is missing path attribute" s spos
2559 let path = unent pathent in
2560 let c = config_of dc attrs in
2561 let pageno, rely, x =
2562 let safef f n v d =
2563 try f v
2564 with exn ->
2565 dolog "error accessing %s (%S) at postion %d:\n %s"
2566 n v spos (Printexc.to_string exn);
2569 let rec fold pageno rely x = function
2570 | [] -> pageno, rely, x
2571 | ("rely", v) :: rest ->
2572 fold pageno (safef float_of_string "rely" v 0.0) x rest
2573 | ("page", v) :: rest ->
2574 fold (safef int_of_string "page" v 0) rely x rest
2575 | ("x", v) :: rest ->
2576 fold pageno rely (safef int_of_string "x" v 0) rest
2577 | _ :: rest ->
2578 fold pageno rely x rest
2580 fold 0 0.0 0 attrs
2582 let anchor = (pageno, rely) in
2583 if closed
2584 then (Hashtbl.add h path (c, [], x, anchor); v)
2585 else { v with f = doc path x anchor c [] }
2587 | Vopen (tag, _, closed) ->
2588 error "unexpected subelement in llppconfig" s spos
2590 | Vclose "llppconfig" -> { v with f = toplevel }
2591 | Vclose tag -> error "unexpected close in llppconfig" s spos
2593 and doc path x anchor c bookmarks v t spos epos =
2594 match t with
2595 | Vdata | Vcdata -> v
2596 | Vend -> error "unexpected end of input in doc" s spos
2597 | Vopen ("bookmarks", attrs, closed) ->
2598 { v with f = pbookmarks path x anchor c bookmarks }
2600 | Vopen (tag, _, _) ->
2601 error "unexpected subelement in doc" s spos
2603 | Vclose "doc" ->
2604 Hashtbl.add h path (c, List.rev bookmarks, x, anchor);
2605 { v with f = llppconfig }
2607 | Vclose tag -> error "unexpected close in doc" s spos
2609 and pbookmarks path x anchor c bookmarks v t spos epos =
2610 match t with
2611 | Vdata | Vcdata -> v
2612 | Vend -> error "unexpected end of input in bookmarks" s spos
2613 | Vopen ("item", attrs, closed) ->
2614 let titleent, spage, srely = bookmark_of attrs in
2615 let page =
2617 int_of_string spage
2618 with exn ->
2619 dolog "Failed to convert page %S to integer: %s"
2620 spage (Printexc.to_string exn);
2623 let rely =
2625 float_of_string srely
2626 with exn ->
2627 dolog "Failed to convert rely %S to real: %s"
2628 srely (Printexc.to_string exn);
2631 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2632 if closed
2633 then { v with f = pbookmarks path x anchor c bookmarks }
2634 else
2635 let f () = v in
2636 { v with f = skip "item" f }
2638 | Vopen _ ->
2639 error "unexpected subelement in bookmarks" s spos
2641 | Vclose "bookmarks" ->
2642 { v with f = doc path x anchor c bookmarks }
2644 | Vclose tag -> error "unexpected close in bookmarks" s spos
2646 and skip tag f v t spos epos =
2647 match t with
2648 | Vdata | Vcdata -> v
2649 | Vend ->
2650 error ("unexpected end of input in skipped " ^ tag) s spos
2651 | Vopen (tag', _, closed) ->
2652 if closed
2653 then v
2654 else
2655 let f' () = { v with f = skip tag f } in
2656 { v with f = skip tag' f' }
2657 | Vclose ctag ->
2658 if tag = ctag
2659 then f ()
2660 else error ("unexpected close in skipped " ^ tag) s spos
2663 parse { f = toplevel; accu = () } s;
2664 h, dc;
2667 let do_load f ic =
2669 let len = in_channel_length ic in
2670 let s = String.create len in
2671 really_input ic s 0 len;
2672 f s;
2673 with
2674 | Parse_error (msg, s, pos) ->
2675 let subs = subs s pos in
2676 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2677 failwith ("parse error: " ^ s)
2679 | exn ->
2680 failwith ("config load error: " ^ Printexc.to_string exn)
2683 let path =
2684 let dir =
2686 let dir = Filename.concat home ".config" in
2687 if Sys.is_directory dir then dir else home
2688 with _ -> home
2690 Filename.concat dir "llpp.conf"
2693 let load1 f =
2694 if Sys.file_exists path
2695 then
2696 match
2697 (try Some (open_in_bin path)
2698 with exn ->
2699 prerr_endline
2700 ("Error opening configuation file `" ^ path ^ "': " ^
2701 Printexc.to_string exn);
2702 None
2704 with
2705 | Some ic ->
2706 begin try
2707 f (do_load get ic)
2708 with exn ->
2709 prerr_endline
2710 ("Error loading configuation from `" ^ path ^ "': " ^
2711 Printexc.to_string exn);
2712 end;
2713 close_in ic;
2715 | None -> ()
2716 else
2717 f (Hashtbl.create 0, defconf)
2720 let load () =
2721 let f (h, dc) =
2722 let pc, pb, px, pa =
2724 Hashtbl.find h (Filename.basename state.path)
2725 with Not_found -> dc, [], 0, (0, 0.0)
2727 setconf defconf dc;
2728 setconf conf pc;
2729 state.bookmarks <- pb;
2730 state.x <- px;
2731 cbput state.hists.nav pa;
2733 load1 f
2736 let add_attrs bb always dc c =
2737 let ob s a b =
2738 if always || a != b
2739 then Printf.bprintf bb "\n %s='%b'" s a
2740 and oi s a b =
2741 if always || a != b
2742 then Printf.bprintf bb "\n %s='%d'" s a
2743 and oz s a b =
2744 if always || a <> b
2745 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2747 let w, h =
2748 if always
2749 then dc.winw, dc.winh
2750 else
2751 match state.fullscreen with
2752 | Some wh -> wh
2753 | None -> c.winw, c.winh
2755 let zoom, presentation, interpagespace, showall=
2756 if always
2757 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2758 else
2759 match state.mode with
2760 | Birdseye (bc, _, _, _) ->
2761 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2762 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2764 oi "width" w dc.winw;
2765 oi "height" h dc.winh;
2766 oi "scroll-bar-width" c.scrollw dc.scrollw;
2767 oi "scroll-handle-height" c.scrollh dc.scrollh;
2768 ob "case-insensitive-search" c.icase dc.icase;
2769 ob "preload" c.preload dc.preload;
2770 oi "page-bias" c.pagebias dc.pagebias;
2771 oi "scroll-step" c.scrollincr dc.scrollincr;
2772 ob "max-height-fit" c.maxhfit dc.maxhfit;
2773 ob "crop-hack" c.crophack dc.crophack;
2774 ob "throttle" showall dc.showall;
2775 ob "highlight-links" c.hlinks dc.hlinks;
2776 ob "under-cursor-info" c.underinfo dc.underinfo;
2777 oi "vertical-margin" interpagespace dc.interpagespace;
2778 oz "zoom" zoom dc.zoom;
2779 ob "presentation" presentation dc.presentation;
2780 oi "rotation-angle" c.angle dc.angle;
2781 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2782 ob "proportional-display" c.proportional dc.proportional;
2783 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2784 oi "texcount" c.texcount dc.texcount;
2785 oi "slice-height" c.sliceheight dc.sliceheight;
2786 oi "thumbnail-width" c.thumbw dc.thumbw;
2789 let save () =
2790 let bb = Buffer.create 32768 in
2791 let f (h, dc) =
2792 Buffer.add_string bb "<llppconfig>\n<defaults ";
2793 add_attrs bb true dc dc;
2794 Buffer.add_string bb "/>\n";
2796 let adddoc path x anchor c bookmarks =
2797 if bookmarks == [] && c = dc && anchor = emptyanchor
2798 then ()
2799 else (
2800 Printf.bprintf bb "<doc path='%s'"
2801 (enent path 0 (String.length path));
2803 if anchor <> emptyanchor
2804 then (
2805 let n, y = anchor in
2806 Printf.bprintf bb " page='%d'" n;
2807 Printf.bprintf bb " rely='%f'" y;
2810 if x != 0
2811 then Printf.bprintf bb " pan='%d'" x;
2813 add_attrs bb false dc c;
2815 begin match bookmarks with
2816 | [] -> Buffer.add_string bb "/>\n"
2817 | _ ->
2818 Buffer.add_string bb ">\n<bookmarks>\n";
2819 List.iter (fun (title, _level, page, rely) ->
2820 Printf.bprintf bb
2821 "<item title='%s' page='%d' rely='%f'/>\n"
2822 (enent title 0 (String.length title))
2823 page
2824 rely
2825 ) bookmarks;
2826 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2827 end;
2831 let x =
2832 match state.mode with
2833 | Birdseye (_, x, _, _) -> x
2834 | _ -> state.x
2836 let basename = Filename.basename state.path in
2837 adddoc basename x (getanchor ()) conf
2838 (if conf.savebmarks then state.bookmarks else []);
2840 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2841 if basename <> path
2842 then adddoc path x y c bookmarks
2843 ) h;
2844 Buffer.add_string bb "</llppconfig>";
2846 load1 f;
2847 if Buffer.length bb > 0
2848 then
2850 let tmp = path ^ ".tmp" in
2851 let oc = open_out_bin tmp in
2852 Buffer.output_buffer oc bb;
2853 close_out oc;
2854 Sys.rename tmp path;
2855 with exn ->
2856 prerr_endline
2857 ("error while saving configuration: " ^ Printexc.to_string exn)
2859 end;;
2861 let () =
2862 Arg.parse
2863 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2864 (fun s -> state.path <- s)
2865 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2867 if String.length state.path = 0
2868 then (prerr_endline "filename missing"; exit 1);
2870 State.load ();
2872 let _ = Glut.init Sys.argv in
2873 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2874 let () = Glut.initWindowSize conf.winw conf.winh in
2875 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
2877 let csock, ssock =
2878 if Sys.os_type = "Unix"
2879 then
2880 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2881 else
2882 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2883 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2884 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2885 Unix.bind sock addr;
2886 Unix.listen sock 1;
2887 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2888 Unix.connect csock addr;
2889 let ssock, _ = Unix.accept sock in
2890 Unix.close sock;
2891 let opts sock =
2892 Unix.setsockopt sock Unix.TCP_NODELAY true;
2893 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2895 opts ssock;
2896 opts csock;
2897 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2898 ssock, csock
2901 let () = Glut.displayFunc display in
2902 let () = Glut.reshapeFunc reshape in
2903 let () = Glut.keyboardFunc keyboard in
2904 let () = Glut.specialFunc special in
2905 let () = Glut.idleFunc (Some idle) in
2906 let () = Glut.mouseFunc mouse in
2907 let () = Glut.motionFunc motion in
2908 let () = Glut.passiveMotionFunc pmotion in
2910 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
2911 state.csock <- csock;
2912 state.ssock <- ssock;
2913 state.text <- "Opening " ^ state.path;
2914 writeopen state.path state.password;
2916 at_exit State.save;
2918 let rec handlelablglutbug () =
2920 Glut.mainLoop ();
2921 with Glut.BadEnum "key in special_of_int" ->
2922 showtext '!' " LablGlut bug: special key not recognized";
2923 handlelablglutbug ()
2925 handlelablglutbug ();