Fix showrects when page was panned
[llpp.git] / main.ml
blob6ae2fcfebb12a845dec0a165adc9da3bbd73903b
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 external init : Unix.file_descr -> unit = "ml_init";;
12 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext : string -> (int * int * int * int) -> int -> unit =
14 "ml_seltext";;
15 external copysel : string -> unit = "ml_copysel";;
16 external getpagewh : int -> float array = "ml_getpagewh";;
17 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
19 type mpos = int * int
20 type mstate = Msel of (mpos * mpos) | Mpan of mpos | Mnone;;
22 type 'a circbuf =
23 { store : 'a array
24 ; mutable rc : int
25 ; mutable wc : int
26 ; mutable len : int
30 type textentry = (char * string * onhist option * onkey * ondone)
31 and onkey = string -> int -> te
32 and ondone = string -> unit
33 and onhist = histcmd -> string
34 and histcmd = HCnext | HCprev | HCfirst | HClast
35 and te =
36 | TEstop
37 | TEdone of string
38 | TEcont of string
39 | TEswitch of textentry
42 let cbnew n v =
43 { store = Array.create n v
44 ; rc = 0
45 ; wc = 0
46 ; len = 0
50 let cblen b = Array.length b.store;;
52 let cbput b v =
53 let len = cblen b in
54 b.store.(b.wc) <- v;
55 b.wc <- (b.wc + 1) mod len;
56 b.len <- min (b.len + 1) len;
59 let cbpeekw b = b.store.(b.wc);;
61 let cbget b dir =
62 if b.len = 0
63 then b.store.(0)
64 else
65 let rc = b.rc + dir in
66 let rc = if rc = -1 then b.len - 1 else rc in
67 let rc = if rc = b.len then 0 else rc in
68 b.rc <- rc;
69 b.store.(rc);
72 let cbrfollowlen b =
73 b.rc <- b.len;
76 let cbclear b v =
77 b.len <- 0;
78 Array.fill b.store 0 (Array.length b.store) v;
81 type layout =
82 { pageno : int
83 ; pagedimno : int
84 ; pagew : int
85 ; pageh : int
86 ; pagedispy : int
87 ; pagey : int
88 ; pagevh : int
92 type conf =
93 { mutable scrollw : int
94 ; mutable scrollh : int
95 ; mutable icase : bool
96 ; mutable preload : bool
97 ; mutable pagebias : int
98 ; mutable verbose : bool
99 ; mutable scrollincr : int
100 ; mutable maxhfit : bool
101 ; mutable crophack : bool
102 ; mutable autoscroll : bool
103 ; mutable showall : bool
104 ; mutable hlinks : bool
105 ; mutable underinfo : bool
106 ; mutable interpagespace : int
107 ; mutable zoom : float
108 ; mutable presentation : bool
112 type outline = string * int * int * float;;
113 type outlines =
114 | Oarray of outline array
115 | Olist of outline list
116 | Onarrow of outline array * outline array
119 type rect = (float * float * float * float * float * float * float * float);;
121 type state =
122 { mutable csock : Unix.file_descr
123 ; mutable ssock : Unix.file_descr
124 ; mutable w : int
125 ; mutable h : int
126 ; mutable winw : int
127 ; mutable rotate : int
128 ; mutable x : int
129 ; mutable y : int
130 ; mutable ty : float
131 ; mutable maxy : int
132 ; mutable layout : layout list
133 ; pagemap : ((int * int * int), string) Hashtbl.t
134 ; mutable pages : (int * int * int) list
135 ; mutable pagecount : int
136 ; pagecache : string circbuf
137 ; mutable rendering : bool
138 ; mutable mstate : mstate
139 ; mutable searchpattern : string
140 ; mutable rects : (int * int * rect) list
141 ; mutable rects1 : (int * int * rect) list
142 ; mutable text : string
143 ; mutable fullscreen : (int * int) option
144 ; mutable textentry : textentry option
145 ; mutable outlines : outlines
146 ; mutable outline : (bool * int * int * outline array * string) option
147 ; mutable bookmarks : outline list
148 ; mutable path : string
149 ; mutable password : string
150 ; mutable invalidated : int
151 ; mutable colorscale : float
152 ; hists : hists
154 and hists =
155 { pat : string circbuf
156 ; pag : string circbuf
157 ; nav : float circbuf
161 let conf =
162 { scrollw = 5
163 ; scrollh = 12
164 ; icase = true
165 ; preload = true
166 ; pagebias = 0
167 ; verbose = false
168 ; scrollincr = 24
169 ; maxhfit = true
170 ; crophack = false
171 ; autoscroll = false
172 ; showall = false
173 ; hlinks = false
174 ; underinfo = false
175 ; interpagespace = 2
176 ; zoom = 1.0
177 ; presentation = false
181 let state =
182 { csock = Unix.stdin
183 ; ssock = Unix.stdin
184 ; w = 900
185 ; h = 900
186 ; winw = 900
187 ; rotate = 0
188 ; y = 0
189 ; x = 0
190 ; ty = 0.0
191 ; layout = []
192 ; maxy = max_int
193 ; pagemap = Hashtbl.create 10
194 ; pagecache = cbnew 10 ""
195 ; pages = []
196 ; pagecount = 0
197 ; rendering = false
198 ; mstate = Mnone
199 ; rects = []
200 ; rects1 = []
201 ; text = ""
202 ; fullscreen = None
203 ; textentry = None
204 ; searchpattern = ""
205 ; outlines = Olist []
206 ; outline = None
207 ; bookmarks = []
208 ; path = ""
209 ; password = ""
210 ; invalidated = 0
211 ; hists =
212 { nav = cbnew 100 0.0
213 ; pat = cbnew 20 ""
214 ; pag = cbnew 10 ""
216 ; colorscale = 1.0
220 let vlog fmt =
221 if conf.verbose
222 then
223 Printf.kprintf prerr_endline fmt
224 else
225 Printf.kprintf ignore fmt
228 let writecmd fd s =
229 let len = String.length s in
230 let n = 4 + len in
231 let b = Buffer.create n in
232 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
233 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
234 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
235 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
236 Buffer.add_string b s;
237 let s' = Buffer.contents b in
238 let n' = Unix.write fd s' 0 n in
239 if n' != n then failwith "write failed";
242 let readcmd fd =
243 let s = "xxxx" in
244 let n = Unix.read fd s 0 4 in
245 if n != 4 then failwith "incomplete read(len)";
246 let len = 0
247 lor (Char.code s.[0] lsl 24)
248 lor (Char.code s.[1] lsl 16)
249 lor (Char.code s.[2] lsl 8)
250 lor (Char.code s.[3] lsl 0)
252 let s = String.create len in
253 let n = Unix.read fd s 0 len in
254 if n != len then failwith "incomplete read(data)";
258 let yratio y =
259 if y = state.maxy
260 then 1.0
261 else float y /. float state.maxy
264 let makecmd s l =
265 let b = Buffer.create 10 in
266 Buffer.add_string b s;
267 let rec combine = function
268 | [] -> b
269 | x :: xs ->
270 Buffer.add_char b ' ';
271 let s =
272 match x with
273 | `b b -> if b then "1" else "0"
274 | `s s -> s
275 | `i i -> string_of_int i
276 | `f f -> string_of_float f
277 | `I f -> string_of_int (truncate f)
279 Buffer.add_string b s;
280 combine xs;
282 combine l;
285 let wcmd s l =
286 let cmd = Buffer.contents (makecmd s l) in
287 writecmd state.csock cmd;
290 let calcips h =
291 if conf.presentation
292 then
293 let d = state.h - h in
294 max 0 ((d + 1) / 2)
295 else
296 conf.interpagespace
299 let calcheight () =
300 let rec f pn ph pi fh l =
301 match l with
302 | (n, _, h) :: rest ->
303 let ips = calcips h in
304 let fh =
305 if n = 0 && conf.presentation
306 then
307 fh+ips
308 else
311 let fh = fh + ((n - pn) * (ph + pi)) in
312 f n h ips fh rest
314 | [] ->
315 let inc =
316 if conf.presentation
317 then 0
318 else -pi
320 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
321 max 0 fh
323 let fh = f 0 0 0 0 state.pages in
327 let getpageyh pageno =
328 let rec f pn ph pi y l =
329 match l with
330 | (n, _, h) :: rest ->
331 let ips = calcips h in
332 if n >= pageno
333 then
334 y + (pageno - pn) * (ph + pi), h
335 else
336 let y = y + (n - pn) * (ph + pi) in
337 f n h ips y rest
339 | [] ->
340 y + (pageno - pn) * (ph + pi), ph
342 f 0 0 0 0 state.pages
345 let getpagey pageno = fst (getpageyh pageno);;
347 let layout y sh =
348 let rec f ~pageno ~pdimno ~prev ~py ~vh ~pdims ~cacheleft ~accu =
349 let ((w, h, ips) as curr), rest, pdimno, p0a =
350 match pdims with
351 | (pageno', w, h) :: rest when pageno' = pageno ->
352 let ips = calcips h in
353 (w, h, ips), rest, pdimno + 1,
354 if conf.presentation then ips else 0
355 | _ ->
356 prev, pdims, pdimno, 0
358 if pageno = state.pagecount || cacheleft = 0 || vh >= sh
359 then
360 accu
361 else
362 let py = py + p0a in
363 let vy = y + vh in
364 if py + h <= vy
365 then
366 let py = py + h + ips in
367 let vh = max 0 (py - y) in
368 f ~pageno:(pageno+1)
369 ~pdimno
370 ~prev:curr
373 ~pdims:rest
374 ~cacheleft
375 ~accu
376 else
377 let top = vy - py in
378 let left = h - top in
379 let left = min (sh - vh) left in
380 let py = py + h + ips in
381 let p0y = if top < 0 then -top else 0 in
382 let e =
383 { pageno = pageno
384 ; pagedimno = pdimno
385 ; pagew = w
386 ; pageh = h
387 ; pagedispy = vh+p0y
388 ; pagey = top+p0y
389 ; pagevh = left-p0y
392 let accu = e :: accu in
393 f ~pageno:(pageno+1)
394 ~pdimno
395 ~prev:curr
397 ~vh:(vh+left+ips)
398 ~pdims:rest
399 ~cacheleft:(cacheleft-1)
400 ~accu
402 if state.invalidated = 0
403 then (
404 let accu =
406 ~pageno:0
407 ~pdimno:~-1
408 ~prev:(0,0,0)
409 ~py:0
410 ~vh:0
411 ~pdims:state.pages
412 ~cacheleft:(cblen state.pagecache)
413 ~accu:[]
415 List.rev accu
417 else
421 let clamp incr =
422 let y = state.y + incr in
423 let y = max 0 y in
424 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
428 let getopaque pageno =
429 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w, state.rotate))
430 with Not_found -> None
433 let cache pageno opaque =
434 Hashtbl.replace state.pagemap (pageno + 1, state.w, state.rotate) opaque
437 let validopaque opaque = String.length opaque > 0;;
439 let render l =
440 match getopaque l.pageno with
441 | None when not state.rendering ->
442 state.rendering <- true;
443 cache l.pageno "";
444 wcmd "render" [`i (l.pageno + 1)
445 ;`i l.pagedimno
446 ;`i l.pagew
447 ;`i l.pageh];
449 | _ -> ()
452 let loadlayout layout =
453 let rec f all = function
454 | l :: ls ->
455 begin match getopaque l.pageno with
456 | None -> render l; f false ls
457 | Some opaque -> f (all && validopaque opaque) ls
459 | [] -> all
461 f (layout <> []) layout;
464 let preload () =
465 if conf.preload
466 then
467 let evictedvisible =
468 let evictedopaque = cbpeekw state.pagecache in
469 List.exists (fun l ->
470 match getopaque l.pageno with
471 | Some opaque when validopaque opaque ->
472 evictedopaque = opaque
473 | otherwise -> false
474 ) state.layout
476 if not evictedvisible
477 then
478 let y = if state.y < state.h then 0 else state.y - state.h in
479 let pages = layout y (state.h*3) in
480 List.iter render pages;
483 let gotoy y =
484 let y = max 0 y in
485 let y = min state.maxy y in
486 let pages = layout y state.h in
487 let ready = loadlayout pages in
488 state.ty <- yratio y;
489 if conf.showall
490 then (
491 if ready
492 then (
493 state.layout <- pages;
494 state.y <- y;
495 Glut.postRedisplay ();
498 else (
499 state.layout <- pages;
500 state.y <- y;
501 Glut.postRedisplay ();
503 preload ();
506 let addnav () =
507 cbput state.hists.nav (yratio state.y);
508 cbrfollowlen state.hists.nav;
511 let getnav () =
512 let y = cbget state.hists.nav ~-1 in
513 truncate (y *. float state.maxy)
516 let gotopage n top =
517 let y, h = getpageyh n in
518 addnav ();
519 gotoy (y + (truncate (top *. float h)));
522 let gotopage1 n top =
523 let y = getpagey n in
524 addnav ();
525 gotoy (y + top);
528 let invalidate () =
529 state.layout <- [];
530 state.pages <- [];
531 state.rects <- [];
532 state.rects1 <- [];
533 state.invalidated <- state.invalidated + 1;
536 let scalecolor c =
537 let c = c *. state.colorscale in
538 (c, c, c);
541 let represent () =
542 let y =
543 match state.layout with
544 | [] ->
545 let rely = yratio state.y in
546 state.maxy <- calcheight ();
547 truncate (float state.maxy *. rely)
549 | l :: _ ->
550 state.maxy <- calcheight ();
551 getpagey l.pageno
553 gotoy y
556 let reshape ~w ~h =
557 let margin = truncate (0.5 *. (float w -. float w *. conf.zoom)) in
558 state.winw <- w;
559 let w = w - margin * 2 - conf.scrollw in
560 state.w <- w;
561 state.h <- h;
562 GlMat.mode `modelview;
563 GlMat.load_identity ();
564 GlMat.mode `projection;
565 GlMat.load_identity ();
566 GlMat.rotate ~x:1.0 ~angle:180.0 ();
567 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
568 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
569 GlClear.color (scalecolor 1.0);
570 GlClear.clear [`color];
572 invalidate ();
573 wcmd "geometry" [`i state.w; `i h];
576 let showtext c s =
577 GlDraw.viewport 0 0 state.winw state.h;
578 GlDraw.color (0.0, 0.0, 0.0);
579 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
580 GlDraw.rect
581 (0.0, float (state.h - 18))
582 (float state.w -. sw, float state.h)
584 let font = Glut.BITMAP_8_BY_13 in
585 GlDraw.color (1.0, 1.0, 1.0);
586 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
587 Glut.bitmapCharacter ~font ~c:(Char.code c);
588 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
591 let enttext () =
592 let len = String.length state.text in
593 match state.textentry with
594 | None ->
595 if len > 0 then showtext ' ' state.text
597 | Some (c, text, _, _, _) ->
598 let s =
599 if len > 0
600 then
601 text ^ " [" ^ state.text ^ "]"
602 else
603 text
605 showtext c s;
608 let showtext c s =
609 if true
610 then (
611 state.text <- Printf.sprintf "%c%s" c s;
612 Glut.postRedisplay ();
614 else (
615 showtext c s;
616 Glut.swapBuffers ();
620 let act cmd =
621 match cmd.[0] with
622 | 'c' ->
623 state.pages <- [];
625 | 'D' ->
626 state.rects <- state.rects1;
627 Glut.postRedisplay ()
629 | 'C' ->
630 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
631 state.pagecount <- n;
632 state.invalidated <- state.invalidated - 1;
633 if state.invalidated = 0
634 then represent ()
636 | 't' ->
637 let s = Scanf.sscanf cmd "t %n"
638 (fun n -> String.sub cmd n (String.length cmd - n))
640 Glut.setWindowTitle s
642 | 'T' ->
643 let s = Scanf.sscanf cmd "T %n"
644 (fun n -> String.sub cmd n (String.length cmd - n))
646 if state.textentry = None
647 then (
648 state.text <- s;
649 showtext ' ' s;
651 else (
652 state.text <- s;
653 Glut.postRedisplay ();
656 | 'V' ->
657 if conf.verbose
658 then
659 let s = Scanf.sscanf cmd "V %n"
660 (fun n -> String.sub cmd n (String.length cmd - n))
662 state.text <- s;
663 showtext ' ' s;
665 | 'F' ->
666 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
667 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
668 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
669 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
671 let y = (getpagey pageno) + truncate y0 in
672 addnav ();
673 gotoy y;
674 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
676 | 'R' ->
677 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
678 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
679 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
680 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
682 state.rects1 <-
683 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
685 | 'r' ->
686 let n, w, h, r, p =
687 Scanf.sscanf cmd "r %d %d %d %d %s"
688 (fun n w h r p -> (n, w, h, r, p))
690 Hashtbl.replace state.pagemap (n, w, r) p;
691 let opaque = cbpeekw state.pagecache in
692 if validopaque opaque
693 then (
694 let k =
695 Hashtbl.fold
696 (fun k v a -> if v = opaque then k else a)
697 state.pagemap (-1, -1, -1)
699 wcmd "free" [`s opaque];
700 Hashtbl.remove state.pagemap k
702 cbput state.pagecache p;
703 state.rendering <- false;
704 if conf.showall
705 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
706 else (
707 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
708 if visible
709 then gotoy state.y
710 else (ignore (loadlayout state.layout); preload ())
713 | 'l' ->
714 let (n, w, h) as pagelayout =
715 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
717 state.pages <- pagelayout :: state.pages
719 | 'o' ->
720 let (l, n, t, h, pos) =
721 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
723 let s = String.sub cmd pos (String.length cmd - pos) in
724 let s =
725 let l = String.length s in
726 let b = Buffer.create (String.length s) in
727 let rec loop pc2 i =
728 if i = l
729 then ()
730 else
731 let pc2 =
732 match s.[i] with
733 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
734 | '\xc2' -> true
735 | c ->
736 let c = if Char.code c land 0x80 = 0 then c else '?' in
737 Buffer.add_char b c;
738 false
740 loop pc2 (i+1)
742 loop false 0;
743 Buffer.contents b
745 let outline = (s, l, n, float t /. float h) in
746 let outlines =
747 match state.outlines with
748 | Olist outlines -> Olist (outline :: outlines)
749 | Oarray _ -> Olist [outline]
750 | Onarrow _ -> Olist [outline]
752 state.outlines <- outlines
754 | _ ->
755 log "unknown cmd `%S'" cmd
758 let now = Unix.gettimeofday;;
760 let idle () =
761 let rec loop delay =
762 let r, _, _ = Unix.select [state.csock] [] [] delay in
763 begin match r with
764 | [] ->
765 if conf.autoscroll
766 then begin
767 let y = state.y + conf.scrollincr in
768 let y = if y >= state.maxy then 0 else y in
769 gotoy y;
770 state.text <- "";
771 end;
773 | _ ->
774 let cmd = readcmd state.csock in
775 act cmd;
776 loop 0.0
777 end;
778 in loop 0.001
781 let onhist cb = function
782 | HCprev -> cbget cb ~-1
783 | HCnext -> cbget cb 1
784 | HCfirst -> cbget cb ~-(cb.rc)
785 | HClast -> cbget cb (cb.len - 1 - cb.rc)
788 let search pattern forward =
789 if String.length pattern > 0
790 then
791 let pn, py =
792 match state.layout with
793 | [] -> 0, 0
794 | l :: _ ->
795 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
797 let cmd =
798 let b = makecmd "search"
799 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
801 Buffer.add_char b ',';
802 Buffer.add_string b pattern;
803 Buffer.add_char b '\000';
804 Buffer.contents b;
806 writecmd state.csock cmd;
809 let intentry text key =
810 let c = Char.unsafe_chr key in
811 match c with
812 | '0' .. '9' ->
813 let s = "x" in s.[0] <- c;
814 let text = text ^ s in
815 TEcont text
817 | _ ->
818 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
819 TEcont text
822 let addchar s c =
823 let b = Buffer.create (String.length s + 1) in
824 Buffer.add_string b s;
825 Buffer.add_char b c;
826 Buffer.contents b;
829 let textentry text key =
830 let c = Char.unsafe_chr key in
831 match c with
832 | _ when key >= 32 && key < 127 ->
833 let text = addchar text c in
834 TEcont text
836 | _ ->
837 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
838 TEcont text
841 let rotate angle =
842 state.rotate <- angle;
843 invalidate ();
844 wcmd "rotate" [`i angle];
847 let optentry text key =
848 let btos b = if b then "on" else "off" in
849 let c = Char.unsafe_chr key in
850 match c with
851 | 's' ->
852 let ondone s =
853 try conf.scrollincr <- int_of_string s with exc ->
854 state.text <- Printf.sprintf "bad integer `%s': %s"
855 s (Printexc.to_string exc)
857 TEswitch ('#', "", None, intentry, ondone)
859 | 'R' ->
860 let ondone s =
861 match try
862 Some (int_of_string s)
863 with exc ->
864 state.text <- Printf.sprintf "bad integer `%s': %s"
865 s (Printexc.to_string exc);
866 None
867 with
868 | Some angle -> rotate angle
869 | None -> ()
871 TEswitch ('^', "", None, intentry, ondone)
873 | 'i' ->
874 conf.icase <- not conf.icase;
875 TEdone ("case insensitive search " ^ (btos conf.icase))
877 | 'p' ->
878 conf.preload <- not conf.preload;
879 gotoy state.y;
880 TEdone ("preload " ^ (btos conf.preload))
882 | 'v' ->
883 conf.verbose <- not conf.verbose;
884 TEdone ("verbose " ^ (btos conf.verbose))
886 | 'h' ->
887 conf.maxhfit <- not conf.maxhfit;
888 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
889 TEdone ("maxhfit " ^ (btos conf.maxhfit))
891 | 'c' ->
892 conf.crophack <- not conf.crophack;
893 TEdone ("crophack " ^ btos conf.crophack)
895 | 'a' ->
896 conf.showall <- not conf.showall;
897 TEdone ("showall " ^ btos conf.showall)
899 | 'f' ->
900 conf.underinfo <- not conf.underinfo;
901 TEdone ("underinfo " ^ btos conf.underinfo)
903 | 'S' ->
904 let ondone s =
906 conf.interpagespace <- int_of_string s;
907 let rely = yratio state.y in
908 state.maxy <- calcheight ();
909 gotoy (truncate (float state.maxy *. rely));
910 with exc ->
911 state.text <- Printf.sprintf "bad integer `%s': %s"
912 s (Printexc.to_string exc)
914 TEswitch ('%', "", None, intentry, ondone)
916 | _ ->
917 state.text <- Printf.sprintf "bad option %d `%c'" key c;
918 TEstop
921 let maxoutlinerows () = (state.h - 31) / 16;;
923 let enterselector allowdel outlines errmsg =
924 if Array.length outlines = 0
925 then (
926 showtext ' ' errmsg;
928 else (
929 Glut.setCursor Glut.CURSOR_INHERIT;
930 let pageno =
931 match state.layout with
932 | [] -> -1
933 | {pageno=pageno} :: rest -> pageno
935 let active =
936 let rec loop n =
937 if n = Array.length outlines
938 then 0
939 else
940 let (_, _, outlinepageno, _) = outlines.(n) in
941 if outlinepageno >= pageno then n else loop (n+1)
943 loop 0
945 state.outline <-
946 Some (allowdel, active,
947 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
948 Glut.postRedisplay ();
952 let enteroutlinemode () =
953 let outlines =
954 match state.outlines with
955 | Oarray a -> a
956 | Olist l ->
957 let a = Array.of_list (List.rev l) in
958 state.outlines <- Oarray a;
960 | Onarrow (a, b) -> a
962 enterselector false outlines "Document has no outline";
965 let enterbookmarkmode () =
966 let bookmarks = Array.of_list state.bookmarks in
967 enterselector true bookmarks "Document has no bookmarks (yet)";
971 let quickbookmark ?title () =
972 match state.layout with
973 | [] -> ()
974 | l :: _ ->
975 let title =
976 match title with
977 | None ->
978 let sec = Unix.gettimeofday () in
979 let tm = Unix.localtime sec in
980 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
981 l.pageno
982 tm.Unix.tm_mday
983 tm.Unix.tm_mon
984 (tm.Unix.tm_year + 1900)
985 tm.Unix.tm_hour
986 tm.Unix.tm_min
987 | Some title -> title
989 state.bookmarks <-
990 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
993 let doreshape w h =
994 state.fullscreen <- None;
995 Glut.reshapeWindow w h;
998 let opendoc path password =
999 invalidate ();
1000 state.path <- path;
1001 state.password <- password;
1002 Hashtbl.clear state.pagemap;
1004 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1005 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1006 wcmd "geometry" [`i state.w; `i state.h];
1009 let viewkeyboard ~key ~x ~y =
1010 let enttext te =
1011 state.textentry <- te;
1012 state.text <- "";
1013 enttext ();
1014 Glut.postRedisplay ()
1016 match state.textentry with
1017 | None ->
1018 let c = Char.chr key in
1019 begin match c with
1020 | '\027' | 'q' ->
1021 exit 0
1023 | '\008' ->
1024 let y = getnav () in
1025 gotoy y
1027 | 'o' ->
1028 enteroutlinemode ()
1030 | 'u' ->
1031 state.rects <- [];
1032 state.text <- "";
1033 Glut.postRedisplay ()
1035 | '/' | '?' ->
1036 let ondone isforw s =
1037 cbput state.hists.pat s;
1038 cbrfollowlen state.hists.pat;
1039 state.searchpattern <- s;
1040 search s isforw
1042 enttext (Some (c, "", Some (onhist state.hists.pat),
1043 textentry, ondone (c ='/')))
1045 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1046 conf.zoom <- min 10.0 (conf.zoom +. 0.1);
1047 reshape state.winw state.h
1049 | '+' ->
1050 let ondone s =
1051 let n =
1052 try int_of_string s with exc ->
1053 state.text <- Printf.sprintf "bad integer `%s': %s"
1054 s (Printexc.to_string exc);
1055 max_int
1057 if n != max_int
1058 then (
1059 conf.pagebias <- n;
1060 state.text <- "page bias is now " ^ string_of_int n;
1063 enttext (Some ('+', "", None, intentry, ondone))
1065 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1066 conf.zoom <- max 0.1 (conf.zoom -. 0.1);
1067 if conf.zoom <= 1.0 then state.x <- 0;
1068 reshape state.winw state.h;
1070 | '-' ->
1071 let ondone msg =
1072 state.text <- msg;
1074 enttext (Some ('-', "", None, optentry, ondone))
1076 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1077 state.x <- 0;
1078 conf.zoom <- 1.0;
1079 reshape state.winw state.h
1081 | '0' .. '9' ->
1082 let ondone s =
1083 let n =
1084 try int_of_string s with exc ->
1085 state.text <- Printf.sprintf "bad integer `%s': %s"
1086 s (Printexc.to_string exc);
1089 if n >= 0
1090 then (
1091 addnav ();
1092 cbput state.hists.pag (string_of_int n);
1093 cbrfollowlen state.hists.pag;
1094 gotoy (getpagey (n + conf.pagebias - 1))
1097 let pageentry text key =
1098 match Char.unsafe_chr key with
1099 | 'g' -> TEdone text
1100 | _ -> intentry text key
1102 let text = "x" in text.[0] <- c;
1103 enttext (Some (':', text, Some (onhist state.hists.pag),
1104 pageentry, ondone))
1106 | 'b' ->
1107 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
1108 reshape state.winw state.h;
1110 | 'l' ->
1111 conf.hlinks <- not conf.hlinks;
1112 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1113 Glut.postRedisplay ()
1115 | 'a' ->
1116 conf.autoscroll <- not conf.autoscroll
1118 | 'P' ->
1119 conf.presentation <- not conf.presentation;
1120 represent ()
1122 | 'f' ->
1123 begin match state.fullscreen with
1124 | None ->
1125 state.fullscreen <- Some (state.w, state.h);
1126 Glut.fullScreen ()
1127 | Some (w, h) ->
1128 state.fullscreen <- None;
1129 doreshape w h
1132 | 'g' ->
1133 gotoy 0
1135 | 'n' ->
1136 search state.searchpattern true
1138 | 'p' | 'N' ->
1139 search state.searchpattern false
1141 | 't' ->
1142 begin match state.layout with
1143 | [] -> ()
1144 | l :: _ ->
1145 gotoy (getpagey l.pageno)
1148 | ' ' ->
1149 begin match List.rev state.layout with
1150 | [] -> ()
1151 | l :: _ ->
1152 let pageno = min (l.pageno+1) (state.pagecount-1) in
1153 gotoy (getpagey pageno)
1156 | '\127' ->
1157 begin match state.layout with
1158 | [] -> ()
1159 | l :: _ ->
1160 let pageno = max 0 (l.pageno-1) in
1161 gotoy (getpagey pageno)
1164 | '=' ->
1165 let f (fn, ln) l =
1166 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1168 let fn, ln = List.fold_left f (-1, -1) state.layout in
1169 let s =
1170 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1171 let percent =
1172 if maxy <= 0
1173 then 100.
1174 else (100. *. (float state.y /. float maxy)) in
1175 if fn = ln
1176 then
1177 Printf.sprintf "Page %d of %d %.2f%%"
1178 (fn+1) state.pagecount percent
1179 else
1180 Printf.sprintf
1181 "Pages %d-%d of %d %.2f%%"
1182 (fn+1) (ln+1) state.pagecount percent
1184 showtext ' ' s;
1186 | 'w' ->
1187 begin match state.layout with
1188 | [] -> ()
1189 | l :: _ ->
1190 doreshape l.pagew l.pageh;
1191 Glut.postRedisplay ();
1194 | '\'' ->
1195 enterbookmarkmode ()
1197 | 'm' ->
1198 let ondone s =
1199 match state.layout with
1200 | l :: _ ->
1201 state.bookmarks <-
1202 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1203 :: state.bookmarks
1204 | _ -> ()
1206 enttext (Some ('~', "", None, textentry, ondone))
1208 | '~' ->
1209 quickbookmark ();
1210 showtext ' ' "Quick bookmark added";
1212 | 'z' ->
1213 begin match state.layout with
1214 | l :: _ ->
1215 let a = getpagewh l.pagedimno in
1216 let w, h =
1217 if conf.crophack
1218 then
1219 (truncate (1.8 *. (a.(1) -. a.(0))),
1220 truncate (1.2 *. (a.(3) -. a.(0))))
1221 else
1222 (truncate (a.(1) -. a.(0)),
1223 truncate (a.(3) -. a.(0)))
1225 doreshape w h;
1226 Glut.postRedisplay ();
1228 | [] -> ()
1231 | '<' | '>' ->
1232 rotate (state.rotate + (if c = '>' then 30 else -30));
1234 | '[' | ']' ->
1235 state.colorscale <-
1236 max 0.0
1237 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1238 Glut.postRedisplay ()
1240 | 'k' -> gotoy (clamp (-conf.scrollincr))
1241 | 'j' -> gotoy (clamp conf.scrollincr)
1243 | 'r' -> opendoc state.path state.password
1245 | _ ->
1246 vlog "huh? %d %c" key (Char.chr key);
1249 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1250 let len = String.length text in
1251 if len = 0
1252 then (
1253 state.textentry <- None;
1254 Glut.postRedisplay ();
1256 else (
1257 let s = String.sub text 0 (len - 1) in
1258 enttext (Some (c, s, onhist, onkey, ondone))
1261 | Some (c, text, onhist, onkey, ondone) ->
1262 begin match Char.unsafe_chr key with
1263 | '\r' | '\n' ->
1264 ondone text;
1265 state.textentry <- None;
1266 Glut.postRedisplay ()
1268 | '\027' ->
1269 state.textentry <- None;
1270 Glut.postRedisplay ()
1272 | _ ->
1273 begin match onkey text key with
1274 | TEdone text ->
1275 state.textentry <- None;
1276 ondone text;
1277 Glut.postRedisplay ()
1279 | TEcont text ->
1280 enttext (Some (c, text, onhist, onkey, ondone));
1282 | TEstop ->
1283 state.textentry <- None;
1284 Glut.postRedisplay ()
1286 | TEswitch te ->
1287 state.textentry <- Some te;
1288 Glut.postRedisplay ()
1289 end;
1290 end;
1293 let narrow outlines pattern =
1294 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1295 match reopt with
1296 | None -> None
1297 | Some re ->
1298 let rec fold accu n =
1299 if n = -1
1300 then accu
1301 else
1302 let (s, _, _, _) as o = outlines.(n) in
1303 let accu =
1304 if (try ignore (Str.search_forward re s 0); true
1305 with Not_found -> false)
1306 then (o :: accu)
1307 else accu
1309 fold accu (n-1)
1311 let matched = fold [] (Array.length outlines - 1) in
1312 if matched = [] then None else Some (Array.of_list matched)
1315 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1316 let search active pattern incr =
1317 let dosearch re =
1318 let rec loop n =
1319 if n = Array.length outlines || n = -1
1320 then None
1321 else
1322 let (s, _, _, _) = outlines.(n) in
1324 (try ignore (Str.search_forward re s 0); true
1325 with Not_found -> false)
1326 then Some n
1327 else loop (n + incr)
1329 loop active
1332 let re = Str.regexp_case_fold pattern in
1333 dosearch re
1334 with Failure s ->
1335 state.text <- s;
1336 None
1338 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1339 match key with
1340 | 27 ->
1341 if String.length qsearch = 0
1342 then (
1343 state.text <- "";
1344 state.outline <- None;
1345 Glut.postRedisplay ();
1347 else (
1348 state.text <- "";
1349 state.outline <- Some (allowdel, active, first, outlines, "");
1350 Glut.postRedisplay ();
1353 | 18 | 19 ->
1354 let incr = if key = 18 then -1 else 1 in
1355 let active, first =
1356 match search (active + incr) qsearch incr with
1357 | None ->
1358 state.text <- qsearch ^ " [not found]";
1359 active, first
1360 | Some active ->
1361 state.text <- qsearch;
1362 active, firstof active
1364 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1365 Glut.postRedisplay ();
1367 | 8 ->
1368 let len = String.length qsearch in
1369 if len = 0
1370 then ()
1371 else (
1372 if len = 1
1373 then (
1374 state.text <- "";
1375 state.outline <- Some (allowdel, active, first, outlines, "");
1377 else
1378 let qsearch = String.sub qsearch 0 (len - 1) in
1379 let active, first =
1380 match search active qsearch ~-1 with
1381 | None ->
1382 state.text <- qsearch ^ " [not found]";
1383 active, first
1384 | Some active ->
1385 state.text <- qsearch;
1386 active, firstof active
1388 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1390 Glut.postRedisplay ()
1392 | 13 ->
1393 if active < Array.length outlines
1394 then (
1395 let (_, _, n, t) = outlines.(active) in
1396 gotopage n t;
1398 state.text <- "";
1399 if allowdel then state.bookmarks <- Array.to_list outlines;
1400 state.outline <- None;
1401 Glut.postRedisplay ();
1403 | _ when key >= 32 && key < 127 ->
1404 let pattern = addchar qsearch (Char.chr key) in
1405 let active, first =
1406 match search active pattern 1 with
1407 | None ->
1408 state.text <- pattern ^ " [not found]";
1409 active, first
1410 | Some active ->
1411 state.text <- pattern;
1412 active, firstof active
1414 state.outline <- Some (allowdel, active, first, outlines, pattern);
1415 Glut.postRedisplay ()
1417 | 14 when not allowdel ->
1418 let optoutlines = narrow outlines qsearch in
1419 begin match optoutlines with
1420 | None -> state.text <- "can't narrow"
1421 | Some outlines ->
1422 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1423 match state.outlines with
1424 | Olist l -> ()
1425 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1426 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1427 end;
1428 Glut.postRedisplay ()
1430 | 21 when not allowdel ->
1431 let outline =
1432 match state.outlines with
1433 | Oarray a -> a
1434 | Olist l ->
1435 let a = Array.of_list (List.rev l) in
1436 state.outlines <- Oarray a;
1438 | Onarrow (a, b) ->
1439 state.outlines <- Oarray b;
1442 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1443 Glut.postRedisplay ()
1445 | 12 ->
1446 state.outline <-
1447 Some (allowdel, active, firstof active, outlines, qsearch);
1448 Glut.postRedisplay ()
1450 | 127 when allowdel ->
1451 let len = Array.length outlines - 1 in
1452 if len = 0
1453 then (
1454 state.outline <- None;
1455 state.bookmarks <- [];
1457 else (
1458 let bookmarks = Array.init len
1459 (fun i ->
1460 let i = if i >= active then i + 1 else i in
1461 outlines.(i)
1464 state.outline <-
1465 Some (allowdel,
1466 min active (len-1),
1467 min first (len-1),
1468 bookmarks, qsearch)
1471 Glut.postRedisplay ()
1473 | _ -> log "unknown key %d" key
1476 let keyboard ~key ~x ~y =
1477 if key = 7
1478 then
1479 wcmd "interrupt" []
1480 else
1481 match state.outline with
1482 | None -> viewkeyboard ~key ~x ~y
1483 | Some outline -> outlinekeyboard ~key ~x ~y outline
1486 let special ~key ~x ~y =
1487 match state.outline with
1488 | None ->
1489 begin match state.textentry with
1490 | None ->
1491 let y =
1492 match key with
1493 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1494 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1495 | Glut.KEY_DOWN -> clamp conf.scrollincr
1496 | Glut.KEY_PAGE_UP ->
1497 if Glut.getModifiers () land Glut.active_ctrl != 0
1498 then
1499 match state.layout with
1500 | [] -> state.y
1501 | l :: _ -> state.y - l.pagey
1502 else
1503 clamp (-state.h)
1504 | Glut.KEY_PAGE_DOWN ->
1505 if Glut.getModifiers () land Glut.active_ctrl != 0
1506 then
1507 match List.rev state.layout with
1508 | [] -> state.y
1509 | l :: _ -> getpagey l.pageno
1510 else
1511 clamp state.h
1512 | Glut.KEY_HOME -> addnav (); 0
1513 | Glut.KEY_END ->
1514 addnav ();
1515 state.maxy - (if conf.maxhfit then state.h else 0)
1517 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1518 state.x <- state.x - 10;
1519 state.y
1520 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1521 state.x <- state.x + 10;
1522 state.y
1524 | _ -> state.y
1526 if not conf.verbose then state.text <- "";
1527 gotoy y
1529 | Some (c, s, Some onhist, onkey, ondone) ->
1530 let s =
1531 match key with
1532 | Glut.KEY_UP -> onhist HCprev
1533 | Glut.KEY_DOWN -> onhist HCnext
1534 | Glut.KEY_HOME -> onhist HCfirst
1535 | Glut.KEY_END -> onhist HClast
1536 | _ -> state.text
1538 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1539 Glut.postRedisplay ()
1541 | _ -> ()
1544 | Some (allowdel, active, first, outlines, qsearch) ->
1545 let maxrows = maxoutlinerows () in
1546 let navigate incr =
1547 let active = active + incr in
1548 let active = max 0 (min active (Array.length outlines - 1)) in
1549 let first =
1550 if active > first
1551 then
1552 let rows = active - first in
1553 if rows > maxrows then active - maxrows else first
1554 else active
1556 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1557 Glut.postRedisplay ()
1559 match key with
1560 | Glut.KEY_UP -> navigate ~-1
1561 | Glut.KEY_DOWN -> navigate 1
1562 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1563 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1565 | Glut.KEY_HOME ->
1566 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1567 Glut.postRedisplay ()
1569 | Glut.KEY_END ->
1570 let active = Array.length outlines - 1 in
1571 let first = max 0 (active - maxrows) in
1572 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1573 Glut.postRedisplay ()
1575 | _ -> ()
1578 let drawplaceholder l =
1579 GlDraw.color (scalecolor 1.0);
1580 GlDraw.rect
1581 (0.0, float l.pagedispy)
1582 (float l.pagew, float (l.pagedispy + l.pagevh))
1584 let x = 0.0
1585 and y = float (l.pagedispy + 13) in
1586 let font = Glut.BITMAP_8_BY_13 in
1587 GlDraw.color (0.0, 0.0, 0.0);
1588 GlPix.raster_pos ~x ~y ();
1589 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1590 ("Loading " ^ string_of_int l.pageno);
1593 let now () = Unix.gettimeofday ();;
1595 let drawpage i l =
1596 begin match getopaque l.pageno with
1597 | Some opaque when validopaque opaque ->
1598 if state.textentry = None
1599 then GlDraw.color (scalecolor 1.0)
1600 else GlDraw.color (scalecolor 0.4);
1601 let a = now () in
1602 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1603 opaque;
1604 let b = now () in
1605 let d = b-.a in
1606 vlog "draw %d %f sec" l.pageno d;
1608 | _ ->
1609 drawplaceholder l;
1610 end;
1611 l.pagedispy + l.pagevh;
1614 let scrollindicator () =
1615 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1616 GlDraw.color (0.64 , 0.64, 0.64);
1617 GlDraw.rect
1618 (0., 0.)
1619 (float conf.scrollw, float state.h)
1621 GlDraw.color (0.0, 0.0, 0.0);
1622 let sh = (float (maxy + state.h) /. float state.h) in
1623 let sh = float state.h /. sh in
1624 let sh = max sh (float conf.scrollh) in
1626 let percent =
1627 if state.y = state.maxy
1628 then 1.0
1629 else float state.y /. float maxy
1631 let position = (float state.h -. sh) *. percent in
1633 let position =
1634 if position +. sh > float state.h
1635 then
1636 float state.h -. sh
1637 else
1638 position
1640 GlDraw.rect
1641 (0.0, position)
1642 (float conf.scrollw, position +. sh)
1646 let showsel margin =
1647 match state.mstate with
1648 | Mnone | Mpan _ ->
1651 | Msel ((x0, y0), (x1, y1)) ->
1652 let rec loop = function
1653 | l :: ls ->
1654 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1655 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1656 then
1657 match getopaque l.pageno with
1658 | Some opaque when validopaque opaque ->
1659 let oy = -l.pagey + l.pagedispy in
1660 seltext opaque
1661 (x0 - margin - state.x, y0,
1662 x1 - margin - state.x, y1) oy;
1664 | _ -> ()
1665 else loop ls
1666 | [] -> ()
1668 loop state.layout
1671 let showrects () =
1672 let panx = float state.x in
1673 Gl.enable `blend;
1674 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1675 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1676 List.iter
1677 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1678 List.iter (fun l ->
1679 if l.pageno = pageno
1680 then (
1681 let d = float (l.pagedispy - l.pagey) in
1682 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1683 GlDraw.begins `quads;
1685 GlDraw.vertex2 (x0+.panx, y0+.d);
1686 GlDraw.vertex2 (x1+.panx, y1+.d);
1687 GlDraw.vertex2 (x2+.panx, y2+.d);
1688 GlDraw.vertex2 (x3+.panx, y3+.d);
1690 GlDraw.ends ();
1692 ) state.layout
1693 ) state.rects
1695 Gl.disable `blend;
1698 let showoutline = function
1699 | None -> ()
1700 | Some (allowdel, active, first, outlines, qsearch) ->
1701 Gl.enable `blend;
1702 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1703 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1704 GlDraw.rect (0., 0.) (float state.w, float state.h);
1705 Gl.disable `blend;
1707 GlDraw.color (1., 1., 1.);
1708 let font = Glut.BITMAP_9_BY_15 in
1709 let draw_string x y s =
1710 GlPix.raster_pos ~x ~y ();
1711 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1713 let rec loop row =
1714 if row = Array.length outlines || (row - first) * 16 > state.h
1715 then ()
1716 else (
1717 let (s, l, _, _) = outlines.(row) in
1718 let y = (row - first) * 16 in
1719 let x = 5 + 15*l in
1720 if row = active
1721 then (
1722 Gl.enable `blend;
1723 GlDraw.polygon_mode `both `line;
1724 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1725 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1726 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
1727 GlDraw.rect (0., float (y + 1))
1728 ((float state.w -. sw), float (y + 18));
1729 GlDraw.polygon_mode `both `fill;
1730 Gl.disable `blend;
1731 GlDraw.color (1., 1., 1.);
1733 draw_string (float x) (float (y + 16)) s;
1734 loop (row+1)
1737 loop first
1740 let display () =
1741 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1742 GlDraw.viewport margin 0 state.w state.h;
1743 GlClear.color (scalecolor 0.5);
1744 GlClear.clear [`color];
1745 if state.x != 0
1746 then (
1747 let x = float state.x in
1748 GlMat.translate ~x ();
1750 if conf.zoom > 1.0
1751 then (
1752 Gl.enable `scissor_test;
1753 GlMisc.scissor 0 0 (state.winw - conf.scrollw) state.h;
1755 let _lasty = List.fold_left drawpage 0 (state.layout) in
1756 if conf.zoom > 1.0
1757 then
1758 Gl.disable `scissor_test
1760 if state.x != 0
1761 then (
1762 let x = -.float state.x in
1763 GlMat.translate ~x ();
1765 showrects ();
1766 GlDraw.viewport (state.winw - conf.scrollw) 0 state.winw state.h;
1767 scrollindicator ();
1768 showsel margin;
1769 GlDraw.viewport 0 0 state.winw state.h;
1770 showoutline state.outline;
1771 enttext ();
1772 Glut.swapBuffers ();
1775 let getunder x y =
1776 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1777 let x = x - margin - state.x in
1778 let rec f = function
1779 | l :: rest ->
1780 begin match getopaque l.pageno with
1781 | Some opaque when validopaque opaque ->
1782 let y = y - l.pagedispy in
1783 if y > 0
1784 then
1785 let y = l.pagey + y in
1786 match whatsunder opaque x y with
1787 | Unone -> f rest
1788 | under -> under
1789 else
1790 f rest
1791 | _ ->
1792 f rest
1794 | [] -> Unone
1796 f state.layout
1799 let mouse ~button ~bstate ~x ~y =
1800 match button with
1801 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1802 let incr =
1803 if n = 3
1804 then
1805 -conf.scrollincr
1806 else
1807 conf.scrollincr
1809 let incr = incr * 2 in
1810 let y = clamp incr in
1811 gotoy y
1813 | Glut.LEFT_BUTTON when state.outline = None
1814 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1815 if bstate = Glut.DOWN
1816 then
1817 state.mstate <- Mpan (x, y)
1818 else
1819 state.mstate <- Mnone
1821 | Glut.LEFT_BUTTON when state.outline = None ->
1822 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1823 begin match dest with
1824 | Ulinkgoto (pageno, top) ->
1825 if pageno >= 0
1826 then
1827 gotopage1 pageno top
1829 | Ulinkuri s ->
1830 print_endline s
1832 | Unone when bstate = Glut.DOWN ->
1833 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1834 state.mstate <- Mpan (x, y);
1836 | Unone | Utext _ ->
1837 if bstate = Glut.DOWN
1838 then (
1839 if state.rotate mod 360 = 0
1840 then (
1841 state.mstate <- Msel ((x, y), (x, y));
1842 Glut.postRedisplay ()
1845 else (
1846 match state.mstate with
1847 | Mnone -> ()
1849 | Mpan _ ->
1850 Glut.setCursor Glut.CURSOR_INHERIT;
1851 state.mstate <- Mnone
1853 | Msel ((x0, y0), (x1, y1)) ->
1854 let f l =
1855 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1856 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1857 then
1858 match getopaque l.pageno with
1859 | Some opaque when validopaque opaque ->
1860 copysel opaque
1861 | _ -> ()
1863 List.iter f state.layout;
1864 copysel ""; (* ugly *)
1865 Glut.setCursor Glut.CURSOR_INHERIT;
1866 state.mstate <- Mnone;
1870 | _ ->
1873 let mouse ~button ~state ~x ~y = mouse button state x y;;
1875 let motion ~x ~y =
1876 if state.outline = None
1877 then
1878 match state.mstate with
1879 | Mnone -> ()
1881 | Mpan (x0, y0) ->
1882 let dx = x - x0
1883 and dy = y0 - y in
1884 state.mstate <- Mpan (x, y);
1885 if conf.zoom > 1.0 then state.x <- state.x + dx;
1886 let y = clamp dy in
1887 gotoy y
1889 | Msel (a, _) ->
1890 state.mstate <- Msel (a, (x, y));
1891 Glut.postRedisplay ()
1894 let pmotion ~x ~y =
1895 if state.outline = None
1896 then
1897 match state.mstate with
1898 | Mnone ->
1899 begin match getunder x y with
1900 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1901 | Ulinkuri uri ->
1902 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1903 Glut.setCursor Glut.CURSOR_INFO
1904 | Ulinkgoto (page, y) ->
1905 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1906 Glut.setCursor Glut.CURSOR_INFO
1907 | Utext s ->
1908 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1909 Glut.setCursor Glut.CURSOR_TEXT
1912 | Mpan _ | Msel _ ->
1916 let () =
1917 let statepath =
1918 let home =
1919 if Sys.os_type = "Win32"
1920 then
1921 try Sys.getenv "HOMEPATH" with Not_found -> ""
1922 else
1923 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1925 Filename.concat home "llpp"
1927 let pstate =
1929 let ic = open_in_bin statepath in
1930 let hash = input_value ic in
1931 close_in ic;
1932 hash
1933 with exn ->
1934 if false
1935 then
1936 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1938 Hashtbl.create 1
1940 let savestate () =
1942 let w, h =
1943 match state.fullscreen with
1944 | None -> state.winw, state.h
1945 | Some wh -> wh
1947 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1948 let oc = open_out_bin statepath in
1949 output_value oc pstate
1950 with exn ->
1951 if false
1952 then
1953 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1956 let setstate () =
1958 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1959 state.w <- statew;
1960 state.h <- stateh;
1961 state.bookmarks <- statebookmarks;
1962 with Not_found -> ()
1963 | exn ->
1964 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1967 Arg.parse
1968 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
1969 (fun s -> state.path <- s)
1970 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
1972 let name =
1973 if String.length state.path = 0
1974 then (prerr_endline "filename missing"; exit 1)
1975 else state.path
1978 setstate ();
1979 let _ = Glut.init Sys.argv in
1980 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1981 let () = Glut.initWindowSize state.w state.h in
1982 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1984 let csock, ssock =
1985 if Sys.os_type = "Unix"
1986 then
1987 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1988 else
1989 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1990 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1991 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1992 Unix.bind sock addr;
1993 Unix.listen sock 1;
1994 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1995 Unix.connect csock addr;
1996 let ssock, _ = Unix.accept sock in
1997 Unix.close sock;
1998 let opts sock =
1999 Unix.setsockopt sock Unix.TCP_NODELAY true;
2000 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2002 opts ssock;
2003 opts csock;
2004 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2005 ssock, csock
2008 let () = Glut.displayFunc display in
2009 let () = Glut.reshapeFunc reshape in
2010 let () = Glut.keyboardFunc keyboard in
2011 let () = Glut.specialFunc special in
2012 let () = Glut.idleFunc (Some idle) in
2013 let () = Glut.mouseFunc mouse in
2014 let () = Glut.motionFunc motion in
2015 let () = Glut.passiveMotionFunc pmotion in
2017 init ssock;
2018 state.csock <- csock;
2019 state.ssock <- ssock;
2020 state.text <- "Opening " ^ name;
2021 writecmd state.csock ("open " ^ state.path ^ "\000" ^ state.password ^ "\000");
2023 at_exit savestate;
2025 let rec handlelablglutbug () =
2027 Glut.mainLoop ();
2028 with Glut.BadEnum "key in special_of_int" ->
2029 showtext '!' " LablGlut bug: special key not recognized";
2030 handlelablglutbug ()
2032 handlelablglutbug ();