Fix row selector widht in outline mode
[llpp.git] / main.ml
blobadbecd56d9e2ec838bddad7bfa779e6e234a8a00
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 mstate = Msel of ((int * int) * (int * int)) | Mnone;;
21 type 'a circbuf =
22 { store : 'a array
23 ; mutable rc : int
24 ; mutable wc : int
25 ; mutable len : int
29 type textentry = (char * string * onhist option * onkey * ondone)
30 and onkey = string -> int -> te
31 and ondone = string -> unit
32 and onhist = histcmd -> string
33 and histcmd = HCnext | HCprev | HCfirst | HClast
34 and te =
35 | TEstop
36 | TEdone of string
37 | TEcont of string
38 | TEswitch of textentry
41 let cbnew n v =
42 { store = Array.create n v
43 ; rc = 0
44 ; wc = 0
45 ; len = 0
49 let cblen b = Array.length b.store;;
51 let cbput b v =
52 let len = cblen b in
53 b.store.(b.wc) <- v;
54 b.wc <- (b.wc + 1) mod len;
55 b.len <- min (b.len + 1) len;
58 let cbpeekw b = b.store.(b.wc);;
60 let cbget b dir =
61 if b.len = 0
62 then b.store.(0)
63 else
64 let rc = b.rc + dir in
65 let rc = if rc = -1 then b.len - 1 else rc in
66 let rc = if rc = b.len then 0 else rc in
67 b.rc <- rc;
68 b.store.(rc);
71 let cbrfollowlen b =
72 b.rc <- b.len;
75 let cbclear b v =
76 b.len <- 0;
77 Array.fill b.store 0 (Array.length b.store) v;
80 type layout =
81 { pageno : int
82 ; pagedimno : int
83 ; pagew : int
84 ; pageh : int
85 ; pagedispy : int
86 ; pagey : int
87 ; pagevh : int
91 type conf =
92 { mutable scrollw : int
93 ; mutable scrollh : int
94 ; mutable icase : bool
95 ; mutable preload : bool
96 ; mutable pagebias : int
97 ; mutable verbose : bool
98 ; mutable scrollincr : int
99 ; mutable maxhfit : bool
100 ; mutable crophack : bool
101 ; mutable autoscroll : bool
102 ; mutable showall : bool
103 ; mutable hlinks : bool
104 ; mutable underinfo : bool
105 ; mutable interpagespace : int
106 ; mutable margin : int
107 ; mutable presentation : bool
111 type outline = string * int * int * float;;
112 type outlines =
113 | Oarray of outline array
114 | Olist of outline list
115 | Onarrow of outline array * outline array
118 type rect = (float * float * float * float * float * float * float * float);;
120 type state =
121 { mutable csock : Unix.file_descr
122 ; mutable ssock : Unix.file_descr
123 ; mutable w : int
124 ; mutable h : int
125 ; mutable winw : int
126 ; mutable rotate : int
127 ; mutable x : int
128 ; mutable y : int
129 ; mutable ty : float
130 ; mutable maxy : int
131 ; mutable layout : layout list
132 ; pagemap : ((int * int * int), string) Hashtbl.t
133 ; mutable pages : (int * int * int) list
134 ; mutable pagecount : int
135 ; pagecache : string circbuf
136 ; mutable rendering : bool
137 ; mutable mstate : mstate
138 ; mutable searchpattern : string
139 ; mutable rects : (int * int * rect) list
140 ; mutable rects1 : (int * int * rect) list
141 ; mutable text : string
142 ; mutable fullscreen : (int * int) option
143 ; mutable textentry : textentry option
144 ; mutable outlines : outlines
145 ; mutable outline : (bool * int * int * outline array * string) option
146 ; mutable bookmarks : outline list
147 ; mutable path : string
148 ; mutable password : string
149 ; mutable invalidated : int
150 ; mutable colorscale : float
151 ; hists : hists
153 and hists =
154 { pat : string circbuf
155 ; pag : string circbuf
156 ; nav : float circbuf
160 let conf =
161 { scrollw = 5
162 ; scrollh = 12
163 ; icase = true
164 ; preload = true
165 ; pagebias = 0
166 ; verbose = false
167 ; scrollincr = 24
168 ; maxhfit = true
169 ; crophack = false
170 ; autoscroll = false
171 ; showall = false
172 ; hlinks = false
173 ; underinfo = false
174 ; interpagespace = 2
175 ; margin = 0
176 ; presentation = false
180 let state =
181 { csock = Unix.stdin
182 ; ssock = Unix.stdin
183 ; w = 900
184 ; h = 900
185 ; winw = 900
186 ; rotate = 0
187 ; y = 0
188 ; x = 0
189 ; ty = 0.0
190 ; layout = []
191 ; maxy = max_int
192 ; pagemap = Hashtbl.create 10
193 ; pagecache = cbnew 10 ""
194 ; pages = []
195 ; pagecount = 0
196 ; rendering = false
197 ; mstate = Mnone
198 ; rects = []
199 ; rects1 = []
200 ; text = ""
201 ; fullscreen = None
202 ; textentry = None
203 ; searchpattern = ""
204 ; outlines = Olist []
205 ; outline = None
206 ; bookmarks = []
207 ; path = ""
208 ; password = ""
209 ; invalidated = 0
210 ; hists =
211 { nav = cbnew 100 0.0
212 ; pat = cbnew 20 ""
213 ; pag = cbnew 10 ""
215 ; colorscale = 1.0
219 let vlog fmt =
220 if conf.verbose
221 then
222 Printf.kprintf prerr_endline fmt
223 else
224 Printf.kprintf ignore fmt
227 let writecmd fd s =
228 let len = String.length s in
229 let n = 4 + len in
230 let b = Buffer.create n in
231 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
232 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
233 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
234 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
235 Buffer.add_string b s;
236 let s' = Buffer.contents b in
237 let n' = Unix.write fd s' 0 n in
238 if n' != n then failwith "write failed";
241 let readcmd fd =
242 let s = "xxxx" in
243 let n = Unix.read fd s 0 4 in
244 if n != 4 then failwith "incomplete read(len)";
245 let len = 0
246 lor (Char.code s.[0] lsl 24)
247 lor (Char.code s.[1] lsl 16)
248 lor (Char.code s.[2] lsl 8)
249 lor (Char.code s.[3] lsl 0)
251 let s = String.create len in
252 let n = Unix.read fd s 0 len in
253 if n != len then failwith "incomplete read(data)";
257 let yratio y =
258 if y = state.maxy
259 then 1.0
260 else float y /. float state.maxy
263 let makecmd s l =
264 let b = Buffer.create 10 in
265 Buffer.add_string b s;
266 let rec combine = function
267 | [] -> b
268 | x :: xs ->
269 Buffer.add_char b ' ';
270 let s =
271 match x with
272 | `b b -> if b then "1" else "0"
273 | `s s -> s
274 | `i i -> string_of_int i
275 | `f f -> string_of_float f
276 | `I f -> string_of_int (truncate f)
278 Buffer.add_string b s;
279 combine xs;
281 combine l;
284 let wcmd s l =
285 let cmd = Buffer.contents (makecmd s l) in
286 writecmd state.csock cmd;
289 let calcheight () =
290 let rec f pn ph fh l =
291 match l with
292 | (n, _, h) :: rest ->
293 let fh = fh + (n - pn) * (ph + conf.interpagespace) in
294 f n h fh rest
296 | [] ->
297 let fh = fh + ((ph + conf.interpagespace) * (state.pagecount - pn)) in
298 max 0 fh
300 let fh = f 0 0 0 state.pages in
301 fh + (if conf.presentation then conf.interpagespace else 0);
304 let getpageyh pageno =
305 let inc = if conf.presentation then conf.interpagespace else 0 in
306 let rec f pn ph y l =
307 match l with
308 | (n, _, h) :: rest ->
309 if n >= pageno
310 then
311 y + (pageno - pn) * (ph + conf.interpagespace), h
312 else
313 let y = y + (n - pn) * (ph + conf.interpagespace) in
314 f n h y rest
316 | [] ->
317 y + (pageno - pn) * (ph + conf.interpagespace) + inc, ph
319 f 0 0 0 state.pages;
322 let getpagey pageno = fst (getpageyh pageno);;
324 let layout y sh =
325 let ips = conf.interpagespace in
326 let rec f ~pageno ~pdimno ~prev ~vy ~py ~dy ~pdims ~cacheleft ~accu =
327 if pageno = state.pagecount || cacheleft = 0
328 then accu
329 else
330 let ((_, w, h) as curr), rest, pdimno =
331 match pdims with
332 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
333 curr, rest, pdimno + 1
334 | _ ->
335 prev, pdims, pdimno
337 let pageno' = pageno + 1 in
338 if py + h > vy
339 then
340 let py' = vy - py in
341 let vh = h - py' in
342 if dy + vh > sh
343 then
344 let vh = sh - dy in
345 if vh <= 0
346 then
347 accu
348 else
349 let e =
350 { pageno = pageno
351 ; pagedimno = pdimno
352 ; pagew = w
353 ; pageh = h
354 ; pagedispy = dy
355 ; pagey = py'
356 ; pagevh = vh
359 e :: accu
360 else
361 let e =
362 { pageno = pageno
363 ; pagedimno = pdimno
364 ; pagew = w
365 ; pageh = h
366 ; pagedispy = dy
367 ; pagey = py'
368 ; pagevh = vh
371 let accu = e :: accu in
372 f ~pageno:pageno'
373 ~pdimno
374 ~prev:curr
375 ~vy:(vy + vh)
376 ~py:(py + h)
377 ~dy:(dy + vh + ips)
378 ~pdims:rest
379 ~cacheleft:(pred cacheleft)
380 ~accu
381 else (
382 let py' = vy - py in
383 let vh = h - py' in
384 let t = ips + vh in
385 let dy, py = if t < 0 then 0, py + h + ips else t, py + h - vh in
386 f ~pageno:pageno'
387 ~pdimno
388 ~prev:curr
392 ~pdims:rest
393 ~cacheleft
394 ~accu
397 if state.invalidated = 0
398 then
399 let vy, py, dy =
400 if conf.presentation
401 then (
402 if y < ips
403 then
404 y, y, ips - y
405 else
406 y - ips, 0, 0
408 else
409 y, 0, 0
411 let accu =
413 ~pageno:0
414 ~pdimno:~-1
415 ~prev:(0,0,0)
419 ~pdims:state.pages
420 ~cacheleft:(cblen state.pagecache)
421 ~accu:[]
423 state.maxy <- calcheight ();
424 List.rev accu
425 else
429 let clamp incr =
430 let y = state.y + incr in
431 let y = max 0 y in
432 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
436 let getopaque pageno =
437 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w, state.rotate))
438 with Not_found -> None
441 let cache pageno opaque =
442 Hashtbl.replace state.pagemap (pageno + 1, state.w, state.rotate) opaque
445 let validopaque opaque = String.length opaque > 0;;
447 let render l =
448 match getopaque l.pageno with
449 | None when not state.rendering ->
450 state.rendering <- true;
451 cache l.pageno "";
452 wcmd "render" [`i (l.pageno + 1)
453 ;`i l.pagedimno
454 ;`i l.pagew
455 ;`i l.pageh];
457 | _ -> ()
460 let loadlayout layout =
461 let rec f all = function
462 | l :: ls ->
463 begin match getopaque l.pageno with
464 | None -> render l; f false ls
465 | Some opaque -> f (all && validopaque opaque) ls
467 | [] -> all
469 f (layout <> []) layout;
472 let preload () =
473 if conf.preload
474 then
475 let evictedvisible =
476 let evictedopaque = cbpeekw state.pagecache in
477 List.exists (fun l ->
478 match getopaque l.pageno with
479 | Some opaque when validopaque opaque ->
480 evictedopaque = opaque
481 | otherwise -> false
482 ) state.layout
484 if not evictedvisible
485 then
486 let y = if state.y < state.h then 0 else state.y - state.h in
487 let pages = layout y (state.h*3) in
488 List.iter render pages;
491 let gotoy y =
492 let y = max 0 y in
493 let y = min state.maxy y in
494 let pages = layout y state.h in
495 let ready = loadlayout pages in
496 state.ty <- yratio y;
497 if conf.showall
498 then (
499 if ready
500 then (
501 state.layout <- pages;
502 state.y <- y;
503 Glut.postRedisplay ();
506 else (
507 state.layout <- pages;
508 state.y <- y;
509 Glut.postRedisplay ();
511 preload ();
514 let addnav () =
515 cbput state.hists.nav (yratio state.y);
516 cbrfollowlen state.hists.nav;
519 let getnav () =
520 let y = cbget state.hists.nav ~-1 in
521 truncate (y *. float state.maxy)
524 let gotopage n top =
525 let y, h = getpageyh n in
526 addnav ();
527 gotoy (y + (truncate (top *. float h)));
530 let gotopage1 n top =
531 let y = getpagey n in
532 addnav ();
533 gotoy (y + top);
536 let invalidate () =
537 state.layout <- [];
538 state.pages <- [];
539 state.rects <- [];
540 state.rects1 <- [];
541 state.invalidated <- state.invalidated + 1;
544 let scalecolor c =
545 let c = c *. state.colorscale in
546 (c, c, c);
549 let represent () =
550 let rely =
551 if conf.presentation
552 then
553 match state.pages with
554 | [] -> yratio state.y
555 | (_, _, h) :: _ ->
556 let ips =
557 let d = state.h - h in
558 max 0 ((d + 1) / 2)
560 let rely = yratio state.y in
561 conf.interpagespace <- ips;
562 rely
563 else
564 let rely = yratio state.y in
565 conf.interpagespace <- 2;
566 rely
568 state.maxy <- calcheight ();
569 gotoy (truncate (float state.maxy *. rely));
572 let reshape ~w ~h =
573 let margin =
574 let m = float conf.margin in
575 let m = m *. (float w /. 20.) in
576 let m = truncate m in
577 if m*2 > (w - conf.scrollw) then 0 else m
579 state.winw <- w;
580 let w = w - margin * 2 - conf.scrollw in
581 state.w <- w;
582 state.h <- h;
583 GlMat.mode `modelview;
584 GlMat.load_identity ();
585 GlMat.mode `projection;
586 GlMat.load_identity ();
587 GlMat.rotate ~x:1.0 ~angle:180.0 ();
588 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
589 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
590 GlClear.color (scalecolor 1.0);
591 GlClear.clear [`color];
593 invalidate ();
594 wcmd "geometry" [`i state.w; `i h];
597 let showtext c s =
598 GlDraw.viewport 0 0 state.winw state.h;
599 GlDraw.color (0.0, 0.0, 0.0);
600 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
601 GlDraw.rect
602 (0.0, float (state.h - 18))
603 (float state.w -. sw, float state.h)
605 let font = Glut.BITMAP_8_BY_13 in
606 GlDraw.color (1.0, 1.0, 1.0);
607 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
608 Glut.bitmapCharacter ~font ~c:(Char.code c);
609 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
612 let enttext () =
613 let len = String.length state.text in
614 match state.textentry with
615 | None ->
616 if len > 0 then showtext ' ' state.text
618 | Some (c, text, _, _, _) ->
619 let s =
620 if len > 0
621 then
622 text ^ " [" ^ state.text ^ "]"
623 else
624 text
626 showtext c s;
629 let showtext c s =
630 if true
631 then (
632 state.text <- Printf.sprintf "%c%s" c s;
633 Glut.postRedisplay ();
635 else (
636 showtext c s;
637 Glut.swapBuffers ();
641 let act cmd =
642 match cmd.[0] with
643 | 'c' ->
644 state.pages <- [];
646 | 'D' ->
647 state.rects <- state.rects1;
648 Glut.postRedisplay ()
650 | 'C' ->
651 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
652 state.pagecount <- n;
653 state.invalidated <- state.invalidated - 1;
654 if state.invalidated = 0
655 then represent ()
657 | 't' ->
658 let s = Scanf.sscanf cmd "t %n"
659 (fun n -> String.sub cmd n (String.length cmd - n))
661 Glut.setWindowTitle s
663 | 'T' ->
664 let s = Scanf.sscanf cmd "T %n"
665 (fun n -> String.sub cmd n (String.length cmd - n))
667 if state.textentry = None
668 then (
669 state.text <- s;
670 showtext ' ' s;
672 else (
673 state.text <- s;
674 Glut.postRedisplay ();
677 | 'V' ->
678 if conf.verbose
679 then
680 let s = Scanf.sscanf cmd "V %n"
681 (fun n -> String.sub cmd n (String.length cmd - n))
683 state.text <- s;
684 showtext ' ' s;
686 | 'F' ->
687 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
688 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
689 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
690 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
692 let y = (getpagey pageno) + truncate y0 in
693 addnav ();
694 gotoy y;
695 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
697 | 'R' ->
698 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
699 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
700 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
701 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
703 state.rects1 <-
704 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
706 | 'r' ->
707 let n, w, h, r, p =
708 Scanf.sscanf cmd "r %d %d %d %d %s"
709 (fun n w h r p -> (n, w, h, r, p))
711 Hashtbl.replace state.pagemap (n, w, r) p;
712 let opaque = cbpeekw state.pagecache in
713 if validopaque opaque
714 then (
715 let k =
716 Hashtbl.fold
717 (fun k v a -> if v = opaque then k else a)
718 state.pagemap (-1, -1, -1)
720 wcmd "free" [`s opaque];
721 Hashtbl.remove state.pagemap k
723 cbput state.pagecache p;
724 state.rendering <- false;
725 if conf.showall
726 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
727 else (
728 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
729 if visible
730 then gotoy state.y
731 else (ignore (loadlayout state.layout); preload ())
734 | 'l' ->
735 let (n, w, h) as pagelayout =
736 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
738 state.pages <- pagelayout :: state.pages
740 | 'o' ->
741 let (l, n, t, h, pos) =
742 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
744 let s = String.sub cmd pos (String.length cmd - pos) in
745 let s =
746 let l = String.length s in
747 let b = Buffer.create (String.length s) in
748 let rec loop pc2 i =
749 if i = l
750 then ()
751 else
752 let pc2 =
753 match s.[i] with
754 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
755 | '\xc2' -> true
756 | c ->
757 let c = if Char.code c land 0x80 = 0 then c else '?' in
758 Buffer.add_char b c;
759 false
761 loop pc2 (i+1)
763 loop false 0;
764 Buffer.contents b
766 let outline = (s, l, n, float t /. float h) in
767 let outlines =
768 match state.outlines with
769 | Olist outlines -> Olist (outline :: outlines)
770 | Oarray _ -> Olist [outline]
771 | Onarrow _ -> Olist [outline]
773 state.outlines <- outlines
775 | _ ->
776 log "unknown cmd `%S'" cmd
779 let now = Unix.gettimeofday;;
781 let idle () =
782 let rec loop delay =
783 let r, _, _ = Unix.select [state.csock] [] [] delay in
784 begin match r with
785 | [] ->
786 if conf.autoscroll
787 then begin
788 let y = state.y + conf.scrollincr in
789 let y = if y >= state.maxy then 0 else y in
790 gotoy y;
791 state.text <- "";
792 end;
794 | _ ->
795 let cmd = readcmd state.csock in
796 act cmd;
797 loop 0.0
798 end;
799 in loop 0.001
802 let onhist cb = function
803 | HCprev -> cbget cb ~-1
804 | HCnext -> cbget cb 1
805 | HCfirst -> cbget cb ~-(cb.rc)
806 | HClast -> cbget cb (cb.len - 1 - cb.rc)
809 let search pattern forward =
810 if String.length pattern > 0
811 then
812 let pn, py =
813 match state.layout with
814 | [] -> 0, 0
815 | l :: _ ->
816 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
818 let cmd =
819 let b = makecmd "search"
820 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
822 Buffer.add_char b ',';
823 Buffer.add_string b pattern;
824 Buffer.add_char b '\000';
825 Buffer.contents b;
827 writecmd state.csock cmd;
830 let intentry text key =
831 let c = Char.unsafe_chr key in
832 match c with
833 | '0' .. '9' ->
834 let s = "x" in s.[0] <- c;
835 let text = text ^ s in
836 TEcont text
838 | _ ->
839 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
840 TEcont text
843 let addchar s c =
844 let b = Buffer.create (String.length s + 1) in
845 Buffer.add_string b s;
846 Buffer.add_char b c;
847 Buffer.contents b;
850 let textentry text key =
851 let c = Char.unsafe_chr key in
852 match c with
853 | _ when key >= 32 && key < 127 ->
854 let text = addchar text c in
855 TEcont text
857 | _ ->
858 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
859 TEcont text
862 let rotate angle =
863 state.rotate <- angle;
864 invalidate ();
865 wcmd "rotate" [`i angle];
868 let optentry text key =
869 let btos b = if b then "on" else "off" in
870 let c = Char.unsafe_chr key in
871 match c with
872 | 's' ->
873 let ondone s =
874 try conf.scrollincr <- int_of_string s with exc ->
875 state.text <- Printf.sprintf "bad integer `%s': %s"
876 s (Printexc.to_string exc)
878 TEswitch ('#', "", None, intentry, ondone)
880 | 'R' ->
881 let ondone s =
882 match try
883 Some (int_of_string s)
884 with exc ->
885 state.text <- Printf.sprintf "bad integer `%s': %s"
886 s (Printexc.to_string exc);
887 None
888 with
889 | Some angle -> rotate angle
890 | None -> ()
892 TEswitch ('^', "", None, intentry, ondone)
894 | 'i' ->
895 conf.icase <- not conf.icase;
896 TEdone ("case insensitive search " ^ (btos conf.icase))
898 | 'p' ->
899 conf.preload <- not conf.preload;
900 gotoy state.y;
901 TEdone ("preload " ^ (btos conf.preload))
903 | 'v' ->
904 conf.verbose <- not conf.verbose;
905 TEdone ("verbose " ^ (btos conf.verbose))
907 | 'h' ->
908 conf.maxhfit <- not conf.maxhfit;
909 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
910 TEdone ("maxhfit " ^ (btos conf.maxhfit))
912 | 'c' ->
913 conf.crophack <- not conf.crophack;
914 TEdone ("crophack " ^ btos conf.crophack)
916 | 'a' ->
917 conf.showall <- not conf.showall;
918 TEdone ("showall " ^ btos conf.showall)
920 | 'f' ->
921 conf.underinfo <- not conf.underinfo;
922 TEdone ("underinfo " ^ btos conf.underinfo)
924 | 'S' ->
925 let ondone s =
927 conf.interpagespace <- int_of_string s;
928 let rely = yratio state.y in
929 state.maxy <- calcheight ();
930 gotoy (truncate (float state.maxy *. rely));
931 with exc ->
932 state.text <- Printf.sprintf "bad integer `%s': %s"
933 s (Printexc.to_string exc)
935 TEswitch ('%', "", None, intentry, ondone)
937 | _ ->
938 state.text <- Printf.sprintf "bad option %d `%c'" key c;
939 TEstop
942 let maxoutlinerows () = (state.h - 31) / 16;;
944 let enterselector allowdel outlines errmsg =
945 if Array.length outlines = 0
946 then (
947 showtext ' ' errmsg;
949 else (
950 Glut.setCursor Glut.CURSOR_INHERIT;
951 let pageno =
952 match state.layout with
953 | [] -> -1
954 | {pageno=pageno} :: rest -> pageno
956 let active =
957 let rec loop n =
958 if n = Array.length outlines
959 then 0
960 else
961 let (_, _, outlinepageno, _) = outlines.(n) in
962 if outlinepageno >= pageno then n else loop (n+1)
964 loop 0
966 state.outline <-
967 Some (allowdel, active,
968 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
969 Glut.postRedisplay ();
973 let enteroutlinemode () =
974 let outlines =
975 match state.outlines with
976 | Oarray a -> a
977 | Olist l ->
978 let a = Array.of_list (List.rev l) in
979 state.outlines <- Oarray a;
981 | Onarrow (a, b) -> a
983 enterselector false outlines "Document has no outline";
986 let enterbookmarkmode () =
987 let bookmarks = Array.of_list state.bookmarks in
988 enterselector true bookmarks "Document has no bookmarks (yet)";
992 let quickbookmark ?title () =
993 match state.layout with
994 | [] -> ()
995 | l :: _ ->
996 let title =
997 match title with
998 | None ->
999 let sec = Unix.gettimeofday () in
1000 let tm = Unix.localtime sec in
1001 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
1002 l.pageno
1003 tm.Unix.tm_mday
1004 tm.Unix.tm_mon
1005 (tm.Unix.tm_year + 1900)
1006 tm.Unix.tm_hour
1007 tm.Unix.tm_min
1008 | Some title -> title
1010 state.bookmarks <-
1011 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1014 let doreshape w h =
1015 state.fullscreen <- None;
1016 Glut.reshapeWindow w h;
1019 let opendoc path password =
1020 invalidate ();
1021 state.path <- path;
1022 state.password <- password;
1023 Hashtbl.clear state.pagemap;
1025 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1026 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1027 wcmd "geometry" [`i state.w; `i state.h];
1030 let viewkeyboard ~key ~x ~y =
1031 let enttext te =
1032 state.textentry <- te;
1033 state.text <- "";
1034 enttext ();
1035 Glut.postRedisplay ()
1037 match state.textentry with
1038 | None ->
1039 let c = Char.chr key in
1040 begin match c with
1041 | '\027' | 'q' ->
1042 exit 0
1044 | '\008' ->
1045 let y = getnav () in
1046 gotoy y
1048 | 'o' ->
1049 enteroutlinemode ()
1051 | 'u' ->
1052 state.rects <- [];
1053 state.text <- "";
1054 Glut.postRedisplay ()
1056 | '/' | '?' ->
1057 let ondone isforw s =
1058 cbput state.hists.pat s;
1059 cbrfollowlen state.hists.pat;
1060 state.searchpattern <- s;
1061 search s isforw
1063 enttext (Some (c, "", Some (onhist state.hists.pat),
1064 textentry, ondone (c ='/')))
1066 | '+' ->
1067 if Glut.getModifiers () land Glut.active_ctrl != 0
1068 then (
1069 let margin = max ~-16 (conf.margin - 1) in
1070 conf.margin <- margin;
1071 reshape state.winw state.h;
1073 else
1074 let ondone s =
1075 let n =
1076 try int_of_string s with exc ->
1077 state.text <- Printf.sprintf "bad integer `%s': %s"
1078 s (Printexc.to_string exc);
1079 max_int
1081 if n != max_int
1082 then (
1083 conf.pagebias <- n;
1084 state.text <- "page bias is now " ^ string_of_int n;
1087 enttext (Some ('+', "", None, intentry, ondone))
1089 | '-' ->
1090 if Glut.getModifiers () land Glut.active_ctrl != 0
1091 then (
1092 let margin = min 8 (conf.margin + 1) in
1093 conf.margin <- margin;
1094 reshape state.winw state.h;
1096 else
1097 let ondone msg =
1098 state.text <- msg;
1100 enttext (Some ('-', "", None, optentry, ondone))
1102 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1103 state.x <- 0;
1104 conf.margin <- 0;
1105 reshape state.winw state.h
1107 | '0' .. '9' ->
1108 let ondone s =
1109 let n =
1110 try int_of_string s with exc ->
1111 state.text <- Printf.sprintf "bad integer `%s': %s"
1112 s (Printexc.to_string exc);
1115 if n >= 0
1116 then (
1117 addnav ();
1118 cbput state.hists.pag (string_of_int n);
1119 cbrfollowlen state.hists.pag;
1120 gotoy (getpagey (n + conf.pagebias - 1))
1123 let pageentry text key =
1124 match Char.unsafe_chr key with
1125 | 'g' -> TEdone text
1126 | _ -> intentry text key
1128 let text = "x" in text.[0] <- c;
1129 enttext (Some (':', text, Some (onhist state.hists.pag),
1130 pageentry, ondone))
1132 | 'b' ->
1133 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
1134 reshape state.winw state.h;
1136 | 'l' ->
1137 conf.hlinks <- not conf.hlinks;
1138 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1139 Glut.postRedisplay ()
1141 | 'a' ->
1142 conf.autoscroll <- not conf.autoscroll
1144 | 'P' ->
1145 conf.presentation <- not conf.presentation;
1146 represent ()
1148 | 'f' ->
1149 begin match state.fullscreen with
1150 | None ->
1151 state.fullscreen <- Some (state.w, state.h);
1152 Glut.fullScreen ()
1153 | Some (w, h) ->
1154 state.fullscreen <- None;
1155 doreshape w h
1158 | 'g' ->
1159 gotoy 0
1161 | 'n' ->
1162 search state.searchpattern true
1164 | 'p' | 'N' ->
1165 search state.searchpattern false
1167 | 't' ->
1168 begin match state.layout with
1169 | [] -> ()
1170 | l :: _ ->
1171 gotoy (getpagey l.pageno - conf.interpagespace)
1174 | ' ' ->
1175 begin match List.rev state.layout with
1176 | [] -> ()
1177 | l :: _ ->
1178 gotoy (clamp (l.pageh - l.pagey + conf.interpagespace))
1181 | '\127' ->
1182 begin match state.layout with
1183 | [] -> ()
1184 | l :: _ ->
1185 gotoy (clamp (-l.pageh - conf.interpagespace));
1188 | '=' ->
1189 let f (fn, ln) l =
1190 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1192 let fn, ln = List.fold_left f (-1, -1) state.layout in
1193 let s =
1194 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1195 let percent =
1196 if maxy <= 0
1197 then 100.
1198 else (100. *. (float state.y /. float maxy)) in
1199 if fn = ln
1200 then
1201 Printf.sprintf "Page %d of %d %.2f%%"
1202 (fn+1) state.pagecount percent
1203 else
1204 Printf.sprintf
1205 "Pages %d-%d of %d %.2f%%"
1206 (fn+1) (ln+1) state.pagecount percent
1208 showtext ' ' s;
1210 | 'w' ->
1211 begin match state.layout with
1212 | [] -> ()
1213 | l :: _ ->
1214 doreshape l.pagew l.pageh;
1215 Glut.postRedisplay ();
1218 | '\'' ->
1219 enterbookmarkmode ()
1221 | 'm' ->
1222 let ondone s =
1223 match state.layout with
1224 | l :: _ ->
1225 state.bookmarks <-
1226 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1227 :: state.bookmarks
1228 | _ -> ()
1230 enttext (Some ('~', "", None, textentry, ondone))
1232 | '~' ->
1233 quickbookmark ();
1234 showtext ' ' "Quick bookmark added";
1236 | 'z' ->
1237 begin match state.layout with
1238 | l :: _ ->
1239 let a = getpagewh l.pagedimno in
1240 let w, h =
1241 if conf.crophack
1242 then
1243 (truncate (1.8 *. (a.(1) -. a.(0))),
1244 truncate (1.2 *. (a.(3) -. a.(0))))
1245 else
1246 (truncate (a.(1) -. a.(0)),
1247 truncate (a.(3) -. a.(0)))
1249 doreshape w h;
1250 Glut.postRedisplay ();
1252 | [] -> ()
1255 | '<' | '>' ->
1256 rotate (state.rotate + (if c = '>' then 30 else -30));
1258 | '[' | ']' ->
1259 state.colorscale <-
1260 max 0.0
1261 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1262 Glut.postRedisplay ()
1264 | 'k' -> gotoy (clamp (-conf.scrollincr))
1265 | 'j' -> gotoy (clamp conf.scrollincr)
1267 | 'r' -> opendoc state.path state.password
1269 | _ ->
1270 vlog "huh? %d %c" key (Char.chr key);
1273 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1274 let len = String.length text in
1275 if len = 0
1276 then (
1277 state.textentry <- None;
1278 Glut.postRedisplay ();
1280 else (
1281 let s = String.sub text 0 (len - 1) in
1282 enttext (Some (c, s, onhist, onkey, ondone))
1285 | Some (c, text, onhist, onkey, ondone) ->
1286 begin match Char.unsafe_chr key with
1287 | '\r' | '\n' ->
1288 ondone text;
1289 state.textentry <- None;
1290 Glut.postRedisplay ()
1292 | '\027' ->
1293 state.textentry <- None;
1294 Glut.postRedisplay ()
1296 | _ ->
1297 begin match onkey text key with
1298 | TEdone text ->
1299 state.textentry <- None;
1300 ondone text;
1301 Glut.postRedisplay ()
1303 | TEcont text ->
1304 enttext (Some (c, text, onhist, onkey, ondone));
1306 | TEstop ->
1307 state.textentry <- None;
1308 Glut.postRedisplay ()
1310 | TEswitch te ->
1311 state.textentry <- Some te;
1312 Glut.postRedisplay ()
1313 end;
1314 end;
1317 let narrow outlines pattern =
1318 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1319 match reopt with
1320 | None -> None
1321 | Some re ->
1322 let rec fold accu n =
1323 if n = -1
1324 then accu
1325 else
1326 let (s, _, _, _) as o = outlines.(n) in
1327 let accu =
1328 if (try ignore (Str.search_forward re s 0); true
1329 with Not_found -> false)
1330 then (o :: accu)
1331 else accu
1333 fold accu (n-1)
1335 let matched = fold [] (Array.length outlines - 1) in
1336 if matched = [] then None else Some (Array.of_list matched)
1339 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1340 let search active pattern incr =
1341 let dosearch re =
1342 let rec loop n =
1343 if n = Array.length outlines || n = -1
1344 then None
1345 else
1346 let (s, _, _, _) = outlines.(n) in
1348 (try ignore (Str.search_forward re s 0); true
1349 with Not_found -> false)
1350 then Some n
1351 else loop (n + incr)
1353 loop active
1356 let re = Str.regexp_case_fold pattern in
1357 dosearch re
1358 with Failure s ->
1359 state.text <- s;
1360 None
1362 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1363 match key with
1364 | 27 ->
1365 if String.length qsearch = 0
1366 then (
1367 state.text <- "";
1368 state.outline <- None;
1369 Glut.postRedisplay ();
1371 else (
1372 state.text <- "";
1373 state.outline <- Some (allowdel, active, first, outlines, "");
1374 Glut.postRedisplay ();
1377 | 18 | 19 ->
1378 let incr = if key = 18 then -1 else 1 in
1379 let active, first =
1380 match search (active + incr) qsearch incr 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);
1389 Glut.postRedisplay ();
1391 | 8 ->
1392 let len = String.length qsearch in
1393 if len = 0
1394 then ()
1395 else (
1396 if len = 1
1397 then (
1398 state.text <- "";
1399 state.outline <- Some (allowdel, active, first, outlines, "");
1401 else
1402 let qsearch = String.sub qsearch 0 (len - 1) in
1403 let active, first =
1404 match search active qsearch ~-1 with
1405 | None ->
1406 state.text <- qsearch ^ " [not found]";
1407 active, first
1408 | Some active ->
1409 state.text <- qsearch;
1410 active, firstof active
1412 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1414 Glut.postRedisplay ()
1416 | 13 ->
1417 if active < Array.length outlines
1418 then (
1419 let (_, _, n, t) = outlines.(active) in
1420 gotopage n t;
1422 state.text <- "";
1423 if allowdel then state.bookmarks <- Array.to_list outlines;
1424 state.outline <- None;
1425 Glut.postRedisplay ();
1427 | _ when key >= 32 && key < 127 ->
1428 let pattern = addchar qsearch (Char.chr key) in
1429 let active, first =
1430 match search active pattern 1 with
1431 | None ->
1432 state.text <- pattern ^ " [not found]";
1433 active, first
1434 | Some active ->
1435 state.text <- pattern;
1436 active, firstof active
1438 state.outline <- Some (allowdel, active, first, outlines, pattern);
1439 Glut.postRedisplay ()
1441 | 14 when not allowdel ->
1442 let optoutlines = narrow outlines qsearch in
1443 begin match optoutlines with
1444 | None -> state.text <- "can't narrow"
1445 | Some outlines ->
1446 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1447 match state.outlines with
1448 | Olist l -> ()
1449 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1450 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1451 end;
1452 Glut.postRedisplay ()
1454 | 21 when not allowdel ->
1455 let outline =
1456 match state.outlines with
1457 | Oarray a -> a
1458 | Olist l ->
1459 let a = Array.of_list (List.rev l) in
1460 state.outlines <- Oarray a;
1462 | Onarrow (a, b) ->
1463 state.outlines <- Oarray b;
1466 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1467 Glut.postRedisplay ()
1469 | 12 ->
1470 state.outline <-
1471 Some (allowdel, active, firstof active, outlines, qsearch);
1472 Glut.postRedisplay ()
1474 | 127 when allowdel ->
1475 let len = Array.length outlines - 1 in
1476 if len = 0
1477 then (
1478 state.outline <- None;
1479 state.bookmarks <- [];
1481 else (
1482 let bookmarks = Array.init len
1483 (fun i ->
1484 let i = if i >= active then i + 1 else i in
1485 outlines.(i)
1488 state.outline <-
1489 Some (allowdel,
1490 min active (len-1),
1491 min first (len-1),
1492 bookmarks, qsearch)
1495 Glut.postRedisplay ()
1497 | _ -> log "unknown key %d" key
1500 let keyboard ~key ~x ~y =
1501 if key = 7
1502 then
1503 wcmd "interrupt" []
1504 else
1505 match state.outline with
1506 | None -> viewkeyboard ~key ~x ~y
1507 | Some outline -> outlinekeyboard ~key ~x ~y outline
1510 let special ~key ~x ~y =
1511 match state.outline with
1512 | None ->
1513 begin match state.textentry with
1514 | None ->
1515 let y =
1516 match key with
1517 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1518 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1519 | Glut.KEY_DOWN -> clamp conf.scrollincr
1520 | Glut.KEY_PAGE_UP ->
1521 if Glut.getModifiers () land Glut.active_ctrl != 0
1522 then
1523 match state.layout with
1524 | [] -> state.y
1525 | l :: _ -> state.y - l.pagey
1526 else
1527 clamp (-state.h)
1528 | Glut.KEY_PAGE_DOWN ->
1529 if Glut.getModifiers () land Glut.active_ctrl != 0
1530 then
1531 match List.rev state.layout with
1532 | [] -> state.y
1533 | l :: _ -> getpagey l.pageno
1534 else
1535 clamp state.h
1536 | Glut.KEY_HOME -> addnav (); 0
1537 | Glut.KEY_END ->
1538 addnav ();
1539 state.maxy - (if conf.maxhfit then state.h else 0)
1541 | Glut.KEY_RIGHT when conf.margin < 0 ->
1542 state.x <- state.x - 10;
1543 state.y
1544 | Glut.KEY_LEFT when conf.margin < 0 ->
1545 state.x <- state.x + 10;
1546 state.y
1548 | _ -> state.y
1550 if not conf.verbose then state.text <- "";
1551 gotoy y
1553 | Some (c, s, Some onhist, onkey, ondone) ->
1554 let s =
1555 match key with
1556 | Glut.KEY_UP -> onhist HCprev
1557 | Glut.KEY_DOWN -> onhist HCnext
1558 | Glut.KEY_HOME -> onhist HCfirst
1559 | Glut.KEY_END -> onhist HClast
1560 | _ -> state.text
1562 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1563 Glut.postRedisplay ()
1565 | _ -> ()
1568 | Some (allowdel, active, first, outlines, qsearch) ->
1569 let maxrows = maxoutlinerows () in
1570 let navigate incr =
1571 let active = active + incr in
1572 let active = max 0 (min active (Array.length outlines - 1)) in
1573 let first =
1574 if active > first
1575 then
1576 let rows = active - first in
1577 if rows > maxrows then active - maxrows else first
1578 else active
1580 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1581 Glut.postRedisplay ()
1583 match key with
1584 | Glut.KEY_UP -> navigate ~-1
1585 | Glut.KEY_DOWN -> navigate 1
1586 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1587 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1589 | Glut.KEY_HOME ->
1590 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1591 Glut.postRedisplay ()
1593 | Glut.KEY_END ->
1594 let active = Array.length outlines - 1 in
1595 let first = max 0 (active - maxrows) in
1596 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1597 Glut.postRedisplay ()
1599 | _ -> ()
1602 let drawplaceholder l =
1603 GlDraw.color (scalecolor 1.0);
1604 GlDraw.rect
1605 (0.0, float l.pagedispy)
1606 (float l.pagew, float (l.pagedispy + l.pagevh))
1608 let x = 0.0
1609 and y = float (l.pagedispy + 13) in
1610 let font = Glut.BITMAP_8_BY_13 in
1611 GlDraw.color (0.0, 0.0, 0.0);
1612 GlPix.raster_pos ~x ~y ();
1613 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1614 ("Loading " ^ string_of_int l.pageno);
1617 let now () = Unix.gettimeofday ();;
1619 let drawpage i l =
1620 begin match getopaque l.pageno with
1621 | Some opaque when validopaque opaque ->
1622 if state.textentry = None
1623 then GlDraw.color (scalecolor 1.0)
1624 else GlDraw.color (scalecolor 0.4);
1625 let a = now () in
1626 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1627 opaque;
1628 let b = now () in
1629 let d = b-.a in
1630 vlog "draw %f sec" d;
1632 | _ ->
1633 drawplaceholder l;
1634 end;
1635 l.pagedispy + l.pagevh;
1638 let scrollindicator () =
1639 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1640 GlDraw.color (0.64 , 0.64, 0.64);
1641 GlDraw.rect
1642 (0., 0.)
1643 (float conf.scrollw, float state.h)
1645 GlDraw.color (0.0, 0.0, 0.0);
1646 let sh = (float (maxy + state.h) /. float state.h) in
1647 let sh = float state.h /. sh in
1648 let sh = max sh (float conf.scrollh) in
1650 let percent =
1651 if state.y = state.maxy
1652 then 1.0
1653 else float state.y /. float maxy
1655 let position = (float state.h -. sh) *. percent in
1657 let position =
1658 if position +. sh > float state.h
1659 then
1660 float state.h -. sh
1661 else
1662 position
1664 GlDraw.rect
1665 (0.0, position)
1666 (float conf.scrollw, position +. sh)
1670 let showsel margin =
1671 match state.mstate with
1672 | Mnone ->
1675 | Msel ((x0, y0), (x1, y1)) ->
1676 let rec loop = function
1677 | l :: ls ->
1678 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1679 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1680 then
1681 match getopaque l.pageno with
1682 | Some opaque when validopaque opaque ->
1683 let oy = -l.pagey + l.pagedispy in
1684 seltext opaque
1685 (x0 - margin - state.x, y0,
1686 x1 - margin - state.x, y1) oy;
1688 | _ -> ()
1689 else loop ls
1690 | [] -> ()
1692 loop state.layout
1695 let showrects () =
1696 Gl.enable `blend;
1697 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1698 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1699 List.iter
1700 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1701 List.iter (fun l ->
1702 if l.pageno = pageno
1703 then (
1704 let d = float (l.pagedispy - l.pagey) in
1705 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1706 GlDraw.begins `quads;
1708 GlDraw.vertex2 (x0, y0+.d);
1709 GlDraw.vertex2 (x1, y1+.d);
1710 GlDraw.vertex2 (x2, y2+.d);
1711 GlDraw.vertex2 (x3, y3+.d);
1713 GlDraw.ends ();
1715 ) state.layout
1716 ) state.rects
1718 Gl.disable `blend;
1721 let showoutline = function
1722 | None -> ()
1723 | Some (allowdel, active, first, outlines, qsearch) ->
1724 Gl.enable `blend;
1725 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1726 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1727 GlDraw.rect (0., 0.) (float state.w, float state.h);
1728 Gl.disable `blend;
1730 GlDraw.color (1., 1., 1.);
1731 let font = Glut.BITMAP_9_BY_15 in
1732 let draw_string x y s =
1733 GlPix.raster_pos ~x ~y ();
1734 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1736 let rec loop row =
1737 if row = Array.length outlines || (row - first) * 16 > state.h
1738 then ()
1739 else (
1740 let (s, l, _, _) = outlines.(row) in
1741 let y = (row - first) * 16 in
1742 let x = 5 + 15*l in
1743 if row = active
1744 then (
1745 Gl.enable `blend;
1746 GlDraw.polygon_mode `both `line;
1747 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1748 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1749 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
1750 GlDraw.rect (0., float (y + 1))
1751 ((float state.w -. sw), float (y + 18));
1752 GlDraw.polygon_mode `both `fill;
1753 Gl.disable `blend;
1754 GlDraw.color (1., 1., 1.);
1756 draw_string (float x) (float (y + 16)) s;
1757 loop (row+1)
1760 loop first
1763 let display () =
1764 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1765 GlDraw.viewport margin 0 state.w state.h;
1766 GlClear.color (scalecolor 0.5);
1767 GlClear.clear [`color];
1768 if state.x != 0
1769 then (
1770 let x = float state.x in
1771 GlMat.translate ~x ();
1773 if conf.margin < 0
1774 then (
1775 Gl.enable `scissor_test;
1776 GlMisc.scissor 0 0 (state.winw - conf.scrollw) state.h;
1778 let _lasty = List.fold_left drawpage 0 (state.layout) in
1779 if conf.margin < 0
1780 then
1781 Gl.disable `scissor_test
1783 if state.x != 0
1784 then (
1785 let x = -.float state.x in
1786 GlMat.translate ~x ();
1788 showrects ();
1789 GlDraw.viewport (state.winw - conf.scrollw) 0 state.winw state.h;
1790 scrollindicator ();
1791 showsel margin;
1792 GlDraw.viewport 0 0 state.winw state.h;
1793 showoutline state.outline;
1794 enttext ();
1795 Glut.swapBuffers ();
1798 let getunder x y =
1799 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1800 let x = x - margin - state.x in
1801 let rec f = function
1802 | l :: rest ->
1803 begin match getopaque l.pageno with
1804 | Some opaque when validopaque opaque ->
1805 let y = y - l.pagedispy in
1806 if y > 0
1807 then
1808 let y = l.pagey + y in
1809 match whatsunder opaque x y with
1810 | Unone -> f rest
1811 | under -> under
1812 else
1813 f rest
1814 | _ ->
1815 f rest
1817 | [] -> Unone
1819 f state.layout
1822 let mouse ~button ~bstate ~x ~y =
1823 match button with
1824 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1825 let incr =
1826 if n = 3
1827 then
1828 -conf.scrollincr
1829 else
1830 conf.scrollincr
1832 let incr = incr * 2 in
1833 let y = clamp incr in
1834 gotoy y
1836 | Glut.LEFT_BUTTON when state.outline = None ->
1837 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1838 begin match dest with
1839 | Ulinkgoto (pageno, top) ->
1840 if pageno >= 0
1841 then
1842 gotopage1 pageno top
1844 | Ulinkuri s ->
1845 print_endline s
1847 | Unone when bstate = Glut.DOWN ->
1848 Glut.setCursor Glut.CURSOR_INHERIT;
1849 state.mstate <- Mnone
1851 | Unone | Utext _ ->
1852 if bstate = Glut.DOWN
1853 then (
1854 if state.rotate mod 360 = 0
1855 then (
1856 state.mstate <- Msel ((x, y), (x, y));
1857 Glut.postRedisplay ()
1860 else (
1861 match state.mstate with
1862 | Mnone -> ()
1863 | Msel ((x0, y0), (x1, y1)) ->
1864 let f l =
1865 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1866 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1867 then
1868 match getopaque l.pageno with
1869 | Some opaque when validopaque opaque ->
1870 copysel opaque
1871 | _ -> ()
1873 List.iter f state.layout;
1874 copysel ""; (* ugly *)
1875 Glut.setCursor Glut.CURSOR_INHERIT;
1876 state.mstate <- Mnone;
1880 | _ ->
1883 let mouse ~button ~state ~x ~y = mouse button state x y;;
1885 let motion ~x ~y =
1886 if state.outline = None
1887 then
1888 match state.mstate with
1889 | Mnone -> ()
1890 | Msel (a, _) ->
1891 state.mstate <- Msel (a, (x, y));
1892 Glut.postRedisplay ()
1895 let pmotion ~x ~y =
1896 if state.outline = None
1897 then
1898 match state.mstate with
1899 | Mnone ->
1900 begin match getunder x y with
1901 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1902 | Ulinkuri uri ->
1903 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1904 Glut.setCursor Glut.CURSOR_INFO
1905 | Ulinkgoto (page, y) ->
1906 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1907 Glut.setCursor Glut.CURSOR_INFO
1908 | Utext s ->
1909 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1910 Glut.setCursor Glut.CURSOR_TEXT
1913 | Msel (a, _) ->
1917 let () =
1918 let statepath =
1919 let home =
1920 if Sys.os_type = "Win32"
1921 then
1922 try Sys.getenv "HOMEPATH" with Not_found -> ""
1923 else
1924 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1926 Filename.concat home "llpp"
1928 let pstate =
1930 let ic = open_in_bin statepath in
1931 let hash = input_value ic in
1932 close_in ic;
1933 hash
1934 with exn ->
1935 if false
1936 then
1937 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1939 Hashtbl.create 1
1941 let savestate () =
1943 let w, h =
1944 match state.fullscreen with
1945 | None -> state.winw, state.h
1946 | Some wh -> wh
1948 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1949 let oc = open_out_bin statepath in
1950 output_value oc pstate
1951 with exn ->
1952 if false
1953 then
1954 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1957 let setstate () =
1959 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1960 state.w <- statew;
1961 state.h <- stateh;
1962 state.bookmarks <- statebookmarks;
1963 with Not_found -> ()
1964 | exn ->
1965 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1968 Arg.parse
1969 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
1970 (fun s -> state.path <- s)
1971 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
1973 let name =
1974 if String.length state.path = 0
1975 then (prerr_endline "filename missing"; exit 1)
1976 else state.path
1979 setstate ();
1980 let _ = Glut.init Sys.argv in
1981 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1982 let () = Glut.initWindowSize state.w state.h in
1983 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1985 let csock, ssock =
1986 if Sys.os_type = "Unix"
1987 then
1988 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1989 else
1990 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1991 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1992 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1993 Unix.bind sock addr;
1994 Unix.listen sock 1;
1995 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1996 Unix.connect csock addr;
1997 let ssock, _ = Unix.accept sock in
1998 Unix.close sock;
1999 let opts sock =
2000 Unix.setsockopt sock Unix.TCP_NODELAY true;
2001 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2003 opts ssock;
2004 opts csock;
2005 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2006 ssock, csock
2009 let () = Glut.displayFunc display in
2010 let () = Glut.reshapeFunc reshape in
2011 let () = Glut.keyboardFunc keyboard in
2012 let () = Glut.specialFunc special in
2013 let () = Glut.idleFunc (Some idle) in
2014 let () = Glut.mouseFunc mouse in
2015 let () = Glut.motionFunc motion in
2016 let () = Glut.passiveMotionFunc pmotion in
2018 init ssock;
2019 state.csock <- csock;
2020 state.ssock <- ssock;
2021 state.text <- "Opening " ^ name;
2022 writecmd state.csock ("open " ^ state.path ^ "\000" ^ state.password ^ "\000");
2024 at_exit savestate;
2026 let rec handlelablglutbug () =
2028 Glut.mainLoop ();
2029 with Glut.BadEnum "key in special_of_int" ->
2030 showtext '!' " LablGlut bug: special key not recognized";
2031 handlelablglutbug ()
2033 handlelablglutbug ();