Stop panning once whole page can be visible
[llpp.git] / main.ml
blob419406de5a514bdc4276e81713b535e118fec3b9
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 margin : int
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 ; margin = 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 calcheight () =
291 let rec f pn ph fh l =
292 match l with
293 | (n, _, h) :: rest ->
294 let fh = fh + (n - pn) * (ph + conf.interpagespace) in
295 f n h fh rest
297 | [] ->
298 let fh = fh + ((ph + conf.interpagespace) * (state.pagecount - pn)) in
299 max 0 fh
301 let fh = f 0 0 0 state.pages in
302 fh + (if conf.presentation then conf.interpagespace else -conf.interpagespace);
305 let getpageyh pageno =
306 let inc = if conf.presentation then conf.interpagespace else 0 in
307 let rec f pn ph y l =
308 match l with
309 | (n, _, h) :: rest ->
310 if n >= pageno
311 then
312 y + (pageno - pn) * (ph + conf.interpagespace), h
313 else
314 let y = y + (n - pn) * (ph + conf.interpagespace) in
315 f n h y rest
317 | [] ->
318 y + (pageno - pn) * (ph + conf.interpagespace) + inc, ph
320 f 0 0 0 state.pages;
323 let getpagey pageno = fst (getpageyh pageno);;
325 let layout y sh =
326 let ips = conf.interpagespace in
327 let rec f ~pageno ~pdimno ~prev ~vy ~py ~dy ~pdims ~cacheleft ~accu =
328 if pageno = state.pagecount || cacheleft = 0
329 then accu
330 else
331 let ((_, w, h) as curr), rest, pdimno =
332 match pdims with
333 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
334 curr, rest, pdimno + 1
335 | _ ->
336 prev, pdims, pdimno
338 let pageno' = pageno + 1 in
339 if py + h > vy
340 then
341 let py' = vy - py in
342 let vh = h - py' in
343 if dy + vh > sh
344 then
345 let vh = sh - dy in
346 if vh <= 0
347 then
348 accu
349 else
350 let e =
351 { pageno = pageno
352 ; pagedimno = pdimno
353 ; pagew = w
354 ; pageh = h
355 ; pagedispy = dy
356 ; pagey = py'
357 ; pagevh = vh
360 e :: accu
361 else
362 let e =
363 { pageno = pageno
364 ; pagedimno = pdimno
365 ; pagew = w
366 ; pageh = h
367 ; pagedispy = dy
368 ; pagey = py'
369 ; pagevh = vh
372 let accu = e :: accu in
373 f ~pageno:pageno'
374 ~pdimno
375 ~prev:curr
376 ~vy:(vy + vh)
377 ~py:(py + h)
378 ~dy:(dy + vh + ips)
379 ~pdims:rest
380 ~cacheleft:(pred cacheleft)
381 ~accu
382 else (
383 let py' = vy - py in
384 let vh = h - py' in
385 let t = ips + vh in
386 let dy, py = if t < 0 then 0, py + h + ips else t, py + h - vh in
387 f ~pageno:pageno'
388 ~pdimno
389 ~prev:curr
393 ~pdims:rest
394 ~cacheleft
395 ~accu
398 if state.invalidated = 0
399 then
400 let vy, py, dy =
401 if conf.presentation
402 then (
403 if y < ips
404 then
405 y, y, ips - y
406 else
407 y - ips, 0, 0
409 else
410 y, 0, 0
412 let accu =
414 ~pageno:0
415 ~pdimno:~-1
416 ~prev:(0,0,0)
420 ~pdims:state.pages
421 ~cacheleft:(cblen state.pagecache)
422 ~accu:[]
424 state.maxy <- calcheight ();
425 List.rev accu
426 else
430 let clamp incr =
431 let y = state.y + incr in
432 let y = max 0 y in
433 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
437 let getopaque pageno =
438 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w, state.rotate))
439 with Not_found -> None
442 let cache pageno opaque =
443 Hashtbl.replace state.pagemap (pageno + 1, state.w, state.rotate) opaque
446 let validopaque opaque = String.length opaque > 0;;
448 let render l =
449 match getopaque l.pageno with
450 | None when not state.rendering ->
451 state.rendering <- true;
452 cache l.pageno "";
453 wcmd "render" [`i (l.pageno + 1)
454 ;`i l.pagedimno
455 ;`i l.pagew
456 ;`i l.pageh];
458 | _ -> ()
461 let loadlayout layout =
462 let rec f all = function
463 | l :: ls ->
464 begin match getopaque l.pageno with
465 | None -> render l; f false ls
466 | Some opaque -> f (all && validopaque opaque) ls
468 | [] -> all
470 f (layout <> []) layout;
473 let preload () =
474 if conf.preload
475 then
476 let evictedvisible =
477 let evictedopaque = cbpeekw state.pagecache in
478 List.exists (fun l ->
479 match getopaque l.pageno with
480 | Some opaque when validopaque opaque ->
481 evictedopaque = opaque
482 | otherwise -> false
483 ) state.layout
485 if not evictedvisible
486 then
487 let y = if state.y < state.h then 0 else state.y - state.h in
488 let pages = layout y (state.h*3) in
489 List.iter render pages;
492 let gotoy y =
493 let y = max 0 y in
494 let y = min state.maxy y in
495 let pages = layout y state.h in
496 let ready = loadlayout pages in
497 state.ty <- yratio y;
498 if conf.showall
499 then (
500 if ready
501 then (
502 state.layout <- pages;
503 state.y <- y;
504 Glut.postRedisplay ();
507 else (
508 state.layout <- pages;
509 state.y <- y;
510 Glut.postRedisplay ();
512 preload ();
515 let addnav () =
516 cbput state.hists.nav (yratio state.y);
517 cbrfollowlen state.hists.nav;
520 let getnav () =
521 let y = cbget state.hists.nav ~-1 in
522 truncate (y *. float state.maxy)
525 let gotopage n top =
526 let y, h = getpageyh n in
527 addnav ();
528 gotoy (y + (truncate (top *. float h)));
531 let gotopage1 n top =
532 let y = getpagey n in
533 addnav ();
534 gotoy (y + top);
537 let invalidate () =
538 state.layout <- [];
539 state.pages <- [];
540 state.rects <- [];
541 state.rects1 <- [];
542 state.invalidated <- state.invalidated + 1;
545 let scalecolor c =
546 let c = c *. state.colorscale in
547 (c, c, c);
550 let represent () =
551 let rely =
552 if conf.presentation
553 then
554 match state.pages with
555 | [] -> yratio state.y
556 | (_, _, h) :: _ ->
557 let ips =
558 let d = state.h - h in
559 max 0 ((d + 1) / 2)
561 let rely = yratio state.y in
562 conf.interpagespace <- ips;
563 rely
564 else
565 let rely = yratio state.y in
566 conf.interpagespace <- 2;
567 rely
569 state.maxy <- calcheight ();
570 gotoy (truncate (float state.maxy *. rely));
573 let reshape ~w ~h =
574 let margin =
575 let m = float conf.margin in
576 let m = m *. (float w /. 20.) in
577 let m = truncate m in
578 if m*2 > (w - conf.scrollw) then 0 else m
580 state.winw <- w;
581 let w = w - margin * 2 - conf.scrollw in
582 state.w <- w;
583 state.h <- h;
584 GlMat.mode `modelview;
585 GlMat.load_identity ();
586 GlMat.mode `projection;
587 GlMat.load_identity ();
588 GlMat.rotate ~x:1.0 ~angle:180.0 ();
589 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
590 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
591 GlClear.color (scalecolor 1.0);
592 GlClear.clear [`color];
594 invalidate ();
595 wcmd "geometry" [`i state.w; `i h];
598 let showtext c s =
599 GlDraw.viewport 0 0 state.winw state.h;
600 GlDraw.color (0.0, 0.0, 0.0);
601 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
602 GlDraw.rect
603 (0.0, float (state.h - 18))
604 (float state.w -. sw, float state.h)
606 let font = Glut.BITMAP_8_BY_13 in
607 GlDraw.color (1.0, 1.0, 1.0);
608 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
609 Glut.bitmapCharacter ~font ~c:(Char.code c);
610 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
613 let enttext () =
614 let len = String.length state.text in
615 match state.textentry with
616 | None ->
617 if len > 0 then showtext ' ' state.text
619 | Some (c, text, _, _, _) ->
620 let s =
621 if len > 0
622 then
623 text ^ " [" ^ state.text ^ "]"
624 else
625 text
627 showtext c s;
630 let showtext c s =
631 if true
632 then (
633 state.text <- Printf.sprintf "%c%s" c s;
634 Glut.postRedisplay ();
636 else (
637 showtext c s;
638 Glut.swapBuffers ();
642 let act cmd =
643 match cmd.[0] with
644 | 'c' ->
645 state.pages <- [];
647 | 'D' ->
648 state.rects <- state.rects1;
649 Glut.postRedisplay ()
651 | 'C' ->
652 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
653 state.pagecount <- n;
654 state.invalidated <- state.invalidated - 1;
655 if state.invalidated = 0
656 then represent ()
658 | 't' ->
659 let s = Scanf.sscanf cmd "t %n"
660 (fun n -> String.sub cmd n (String.length cmd - n))
662 Glut.setWindowTitle s
664 | 'T' ->
665 let s = Scanf.sscanf cmd "T %n"
666 (fun n -> String.sub cmd n (String.length cmd - n))
668 if state.textentry = None
669 then (
670 state.text <- s;
671 showtext ' ' s;
673 else (
674 state.text <- s;
675 Glut.postRedisplay ();
678 | 'V' ->
679 if conf.verbose
680 then
681 let s = Scanf.sscanf cmd "V %n"
682 (fun n -> String.sub cmd n (String.length cmd - n))
684 state.text <- s;
685 showtext ' ' s;
687 | 'F' ->
688 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
689 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
690 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
691 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
693 let y = (getpagey pageno) + truncate y0 in
694 addnav ();
695 gotoy y;
696 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
698 | 'R' ->
699 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
700 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
701 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
702 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
704 state.rects1 <-
705 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
707 | 'r' ->
708 let n, w, h, r, p =
709 Scanf.sscanf cmd "r %d %d %d %d %s"
710 (fun n w h r p -> (n, w, h, r, p))
712 Hashtbl.replace state.pagemap (n, w, r) p;
713 let opaque = cbpeekw state.pagecache in
714 if validopaque opaque
715 then (
716 let k =
717 Hashtbl.fold
718 (fun k v a -> if v = opaque then k else a)
719 state.pagemap (-1, -1, -1)
721 wcmd "free" [`s opaque];
722 Hashtbl.remove state.pagemap k
724 cbput state.pagecache p;
725 state.rendering <- false;
726 if conf.showall
727 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
728 else (
729 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
730 if visible
731 then gotoy state.y
732 else (ignore (loadlayout state.layout); preload ())
735 | 'l' ->
736 let (n, w, h) as pagelayout =
737 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
739 state.pages <- pagelayout :: state.pages
741 | 'o' ->
742 let (l, n, t, h, pos) =
743 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
745 let s = String.sub cmd pos (String.length cmd - pos) in
746 let s =
747 let l = String.length s in
748 let b = Buffer.create (String.length s) in
749 let rec loop pc2 i =
750 if i = l
751 then ()
752 else
753 let pc2 =
754 match s.[i] with
755 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
756 | '\xc2' -> true
757 | c ->
758 let c = if Char.code c land 0x80 = 0 then c else '?' in
759 Buffer.add_char b c;
760 false
762 loop pc2 (i+1)
764 loop false 0;
765 Buffer.contents b
767 let outline = (s, l, n, float t /. float h) in
768 let outlines =
769 match state.outlines with
770 | Olist outlines -> Olist (outline :: outlines)
771 | Oarray _ -> Olist [outline]
772 | Onarrow _ -> Olist [outline]
774 state.outlines <- outlines
776 | _ ->
777 log "unknown cmd `%S'" cmd
780 let now = Unix.gettimeofday;;
782 let idle () =
783 let rec loop delay =
784 let r, _, _ = Unix.select [state.csock] [] [] delay in
785 begin match r with
786 | [] ->
787 if conf.autoscroll
788 then begin
789 let y = state.y + conf.scrollincr in
790 let y = if y >= state.maxy then 0 else y in
791 gotoy y;
792 state.text <- "";
793 end;
795 | _ ->
796 let cmd = readcmd state.csock in
797 act cmd;
798 loop 0.0
799 end;
800 in loop 0.001
803 let onhist cb = function
804 | HCprev -> cbget cb ~-1
805 | HCnext -> cbget cb 1
806 | HCfirst -> cbget cb ~-(cb.rc)
807 | HClast -> cbget cb (cb.len - 1 - cb.rc)
810 let search pattern forward =
811 if String.length pattern > 0
812 then
813 let pn, py =
814 match state.layout with
815 | [] -> 0, 0
816 | l :: _ ->
817 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
819 let cmd =
820 let b = makecmd "search"
821 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
823 Buffer.add_char b ',';
824 Buffer.add_string b pattern;
825 Buffer.add_char b '\000';
826 Buffer.contents b;
828 writecmd state.csock cmd;
831 let intentry text key =
832 let c = Char.unsafe_chr key in
833 match c with
834 | '0' .. '9' ->
835 let s = "x" in s.[0] <- c;
836 let text = text ^ s in
837 TEcont text
839 | _ ->
840 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
841 TEcont text
844 let addchar s c =
845 let b = Buffer.create (String.length s + 1) in
846 Buffer.add_string b s;
847 Buffer.add_char b c;
848 Buffer.contents b;
851 let textentry text key =
852 let c = Char.unsafe_chr key in
853 match c with
854 | _ when key >= 32 && key < 127 ->
855 let text = addchar text c in
856 TEcont text
858 | _ ->
859 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
860 TEcont text
863 let rotate angle =
864 state.rotate <- angle;
865 invalidate ();
866 wcmd "rotate" [`i angle];
869 let optentry text key =
870 let btos b = if b then "on" else "off" in
871 let c = Char.unsafe_chr key in
872 match c with
873 | 's' ->
874 let ondone s =
875 try conf.scrollincr <- int_of_string s with exc ->
876 state.text <- Printf.sprintf "bad integer `%s': %s"
877 s (Printexc.to_string exc)
879 TEswitch ('#', "", None, intentry, ondone)
881 | 'R' ->
882 let ondone s =
883 match try
884 Some (int_of_string s)
885 with exc ->
886 state.text <- Printf.sprintf "bad integer `%s': %s"
887 s (Printexc.to_string exc);
888 None
889 with
890 | Some angle -> rotate angle
891 | None -> ()
893 TEswitch ('^', "", None, intentry, ondone)
895 | 'i' ->
896 conf.icase <- not conf.icase;
897 TEdone ("case insensitive search " ^ (btos conf.icase))
899 | 'p' ->
900 conf.preload <- not conf.preload;
901 gotoy state.y;
902 TEdone ("preload " ^ (btos conf.preload))
904 | 'v' ->
905 conf.verbose <- not conf.verbose;
906 TEdone ("verbose " ^ (btos conf.verbose))
908 | 'h' ->
909 conf.maxhfit <- not conf.maxhfit;
910 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
911 TEdone ("maxhfit " ^ (btos conf.maxhfit))
913 | 'c' ->
914 conf.crophack <- not conf.crophack;
915 TEdone ("crophack " ^ btos conf.crophack)
917 | 'a' ->
918 conf.showall <- not conf.showall;
919 TEdone ("showall " ^ btos conf.showall)
921 | 'f' ->
922 conf.underinfo <- not conf.underinfo;
923 TEdone ("underinfo " ^ btos conf.underinfo)
925 | 'S' ->
926 let ondone s =
928 conf.interpagespace <- int_of_string s;
929 let rely = yratio state.y in
930 state.maxy <- calcheight ();
931 gotoy (truncate (float state.maxy *. rely));
932 with exc ->
933 state.text <- Printf.sprintf "bad integer `%s': %s"
934 s (Printexc.to_string exc)
936 TEswitch ('%', "", None, intentry, ondone)
938 | _ ->
939 state.text <- Printf.sprintf "bad option %d `%c'" key c;
940 TEstop
943 let maxoutlinerows () = (state.h - 31) / 16;;
945 let enterselector allowdel outlines errmsg =
946 if Array.length outlines = 0
947 then (
948 showtext ' ' errmsg;
950 else (
951 Glut.setCursor Glut.CURSOR_INHERIT;
952 let pageno =
953 match state.layout with
954 | [] -> -1
955 | {pageno=pageno} :: rest -> pageno
957 let active =
958 let rec loop n =
959 if n = Array.length outlines
960 then 0
961 else
962 let (_, _, outlinepageno, _) = outlines.(n) in
963 if outlinepageno >= pageno then n else loop (n+1)
965 loop 0
967 state.outline <-
968 Some (allowdel, active,
969 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
970 Glut.postRedisplay ();
974 let enteroutlinemode () =
975 let outlines =
976 match state.outlines with
977 | Oarray a -> a
978 | Olist l ->
979 let a = Array.of_list (List.rev l) in
980 state.outlines <- Oarray a;
982 | Onarrow (a, b) -> a
984 enterselector false outlines "Document has no outline";
987 let enterbookmarkmode () =
988 let bookmarks = Array.of_list state.bookmarks in
989 enterselector true bookmarks "Document has no bookmarks (yet)";
993 let quickbookmark ?title () =
994 match state.layout with
995 | [] -> ()
996 | l :: _ ->
997 let title =
998 match title with
999 | None ->
1000 let sec = Unix.gettimeofday () in
1001 let tm = Unix.localtime sec in
1002 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
1003 l.pageno
1004 tm.Unix.tm_mday
1005 tm.Unix.tm_mon
1006 (tm.Unix.tm_year + 1900)
1007 tm.Unix.tm_hour
1008 tm.Unix.tm_min
1009 | Some title -> title
1011 state.bookmarks <-
1012 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1015 let doreshape w h =
1016 state.fullscreen <- None;
1017 Glut.reshapeWindow w h;
1020 let opendoc path password =
1021 invalidate ();
1022 state.path <- path;
1023 state.password <- password;
1024 Hashtbl.clear state.pagemap;
1026 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1027 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1028 wcmd "geometry" [`i state.w; `i state.h];
1031 let viewkeyboard ~key ~x ~y =
1032 let enttext te =
1033 state.textentry <- te;
1034 state.text <- "";
1035 enttext ();
1036 Glut.postRedisplay ()
1038 match state.textentry with
1039 | None ->
1040 let c = Char.chr key in
1041 begin match c with
1042 | '\027' | 'q' ->
1043 exit 0
1045 | '\008' ->
1046 let y = getnav () in
1047 gotoy y
1049 | 'o' ->
1050 enteroutlinemode ()
1052 | 'u' ->
1053 state.rects <- [];
1054 state.text <- "";
1055 Glut.postRedisplay ()
1057 | '/' | '?' ->
1058 let ondone isforw s =
1059 cbput state.hists.pat s;
1060 cbrfollowlen state.hists.pat;
1061 state.searchpattern <- s;
1062 search s isforw
1064 enttext (Some (c, "", Some (onhist state.hists.pat),
1065 textentry, ondone (c ='/')))
1067 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1068 let margin = max ~-16 (conf.margin - 1) in
1069 conf.margin <- margin;
1070 reshape state.winw state.h;
1072 | '+' ->
1073 let ondone s =
1074 let n =
1075 try int_of_string s with exc ->
1076 state.text <- Printf.sprintf "bad integer `%s': %s"
1077 s (Printexc.to_string exc);
1078 max_int
1080 if n != max_int
1081 then (
1082 conf.pagebias <- n;
1083 state.text <- "page bias is now " ^ string_of_int n;
1086 enttext (Some ('+', "", None, intentry, ondone))
1088 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1089 let margin = min 8 (conf.margin + 1) in
1090 conf.margin <- margin;
1091 if margin > 0 then state.x <- 0;
1092 reshape state.winw state.h;
1094 | '-' ->
1095 let ondone msg =
1096 state.text <- msg;
1098 enttext (Some ('-', "", None, optentry, ondone))
1100 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1101 state.x <- 0;
1102 conf.margin <- 0;
1103 reshape state.winw state.h
1105 | '0' .. '9' ->
1106 let ondone s =
1107 let n =
1108 try int_of_string s with exc ->
1109 state.text <- Printf.sprintf "bad integer `%s': %s"
1110 s (Printexc.to_string exc);
1113 if n >= 0
1114 then (
1115 addnav ();
1116 cbput state.hists.pag (string_of_int n);
1117 cbrfollowlen state.hists.pag;
1118 gotoy (getpagey (n + conf.pagebias - 1))
1121 let pageentry text key =
1122 match Char.unsafe_chr key with
1123 | 'g' -> TEdone text
1124 | _ -> intentry text key
1126 let text = "x" in text.[0] <- c;
1127 enttext (Some (':', text, Some (onhist state.hists.pag),
1128 pageentry, ondone))
1130 | 'b' ->
1131 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
1132 reshape state.winw state.h;
1134 | 'l' ->
1135 conf.hlinks <- not conf.hlinks;
1136 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1137 Glut.postRedisplay ()
1139 | 'a' ->
1140 conf.autoscroll <- not conf.autoscroll
1142 | 'P' ->
1143 conf.presentation <- not conf.presentation;
1144 represent ()
1146 | 'f' ->
1147 begin match state.fullscreen with
1148 | None ->
1149 state.fullscreen <- Some (state.w, state.h);
1150 Glut.fullScreen ()
1151 | Some (w, h) ->
1152 state.fullscreen <- None;
1153 doreshape w h
1156 | 'g' ->
1157 gotoy 0
1159 | 'n' ->
1160 search state.searchpattern true
1162 | 'p' | 'N' ->
1163 search state.searchpattern false
1165 | 't' ->
1166 begin match state.layout with
1167 | [] -> ()
1168 | l :: _ ->
1169 gotoy (getpagey l.pageno - conf.interpagespace)
1172 | ' ' ->
1173 begin match List.rev state.layout with
1174 | [] -> ()
1175 | l :: _ ->
1176 gotoy (clamp (l.pageh - l.pagey + conf.interpagespace))
1179 | '\127' ->
1180 begin match state.layout with
1181 | [] -> ()
1182 | l :: _ ->
1183 gotoy (clamp (-l.pageh - conf.interpagespace));
1186 | '=' ->
1187 let f (fn, ln) l =
1188 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1190 let fn, ln = List.fold_left f (-1, -1) state.layout in
1191 let s =
1192 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1193 let percent =
1194 if maxy <= 0
1195 then 100.
1196 else (100. *. (float state.y /. float maxy)) in
1197 if fn = ln
1198 then
1199 Printf.sprintf "Page %d of %d %.2f%%"
1200 (fn+1) state.pagecount percent
1201 else
1202 Printf.sprintf
1203 "Pages %d-%d of %d %.2f%%"
1204 (fn+1) (ln+1) state.pagecount percent
1206 showtext ' ' s;
1208 | 'w' ->
1209 begin match state.layout with
1210 | [] -> ()
1211 | l :: _ ->
1212 doreshape l.pagew l.pageh;
1213 Glut.postRedisplay ();
1216 | '\'' ->
1217 enterbookmarkmode ()
1219 | 'm' ->
1220 let ondone s =
1221 match state.layout with
1222 | l :: _ ->
1223 state.bookmarks <-
1224 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1225 :: state.bookmarks
1226 | _ -> ()
1228 enttext (Some ('~', "", None, textentry, ondone))
1230 | '~' ->
1231 quickbookmark ();
1232 showtext ' ' "Quick bookmark added";
1234 | 'z' ->
1235 begin match state.layout with
1236 | l :: _ ->
1237 let a = getpagewh l.pagedimno in
1238 let w, h =
1239 if conf.crophack
1240 then
1241 (truncate (1.8 *. (a.(1) -. a.(0))),
1242 truncate (1.2 *. (a.(3) -. a.(0))))
1243 else
1244 (truncate (a.(1) -. a.(0)),
1245 truncate (a.(3) -. a.(0)))
1247 doreshape w h;
1248 Glut.postRedisplay ();
1250 | [] -> ()
1253 | '<' | '>' ->
1254 rotate (state.rotate + (if c = '>' then 30 else -30));
1256 | '[' | ']' ->
1257 state.colorscale <-
1258 max 0.0
1259 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1260 Glut.postRedisplay ()
1262 | 'k' -> gotoy (clamp (-conf.scrollincr))
1263 | 'j' -> gotoy (clamp conf.scrollincr)
1265 | 'r' -> opendoc state.path state.password
1267 | _ ->
1268 vlog "huh? %d %c" key (Char.chr key);
1271 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1272 let len = String.length text in
1273 if len = 0
1274 then (
1275 state.textentry <- None;
1276 Glut.postRedisplay ();
1278 else (
1279 let s = String.sub text 0 (len - 1) in
1280 enttext (Some (c, s, onhist, onkey, ondone))
1283 | Some (c, text, onhist, onkey, ondone) ->
1284 begin match Char.unsafe_chr key with
1285 | '\r' | '\n' ->
1286 ondone text;
1287 state.textentry <- None;
1288 Glut.postRedisplay ()
1290 | '\027' ->
1291 state.textentry <- None;
1292 Glut.postRedisplay ()
1294 | _ ->
1295 begin match onkey text key with
1296 | TEdone text ->
1297 state.textentry <- None;
1298 ondone text;
1299 Glut.postRedisplay ()
1301 | TEcont text ->
1302 enttext (Some (c, text, onhist, onkey, ondone));
1304 | TEstop ->
1305 state.textentry <- None;
1306 Glut.postRedisplay ()
1308 | TEswitch te ->
1309 state.textentry <- Some te;
1310 Glut.postRedisplay ()
1311 end;
1312 end;
1315 let narrow outlines pattern =
1316 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1317 match reopt with
1318 | None -> None
1319 | Some re ->
1320 let rec fold accu n =
1321 if n = -1
1322 then accu
1323 else
1324 let (s, _, _, _) as o = outlines.(n) in
1325 let accu =
1326 if (try ignore (Str.search_forward re s 0); true
1327 with Not_found -> false)
1328 then (o :: accu)
1329 else accu
1331 fold accu (n-1)
1333 let matched = fold [] (Array.length outlines - 1) in
1334 if matched = [] then None else Some (Array.of_list matched)
1337 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1338 let search active pattern incr =
1339 let dosearch re =
1340 let rec loop n =
1341 if n = Array.length outlines || n = -1
1342 then None
1343 else
1344 let (s, _, _, _) = outlines.(n) in
1346 (try ignore (Str.search_forward re s 0); true
1347 with Not_found -> false)
1348 then Some n
1349 else loop (n + incr)
1351 loop active
1354 let re = Str.regexp_case_fold pattern in
1355 dosearch re
1356 with Failure s ->
1357 state.text <- s;
1358 None
1360 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1361 match key with
1362 | 27 ->
1363 if String.length qsearch = 0
1364 then (
1365 state.text <- "";
1366 state.outline <- None;
1367 Glut.postRedisplay ();
1369 else (
1370 state.text <- "";
1371 state.outline <- Some (allowdel, active, first, outlines, "");
1372 Glut.postRedisplay ();
1375 | 18 | 19 ->
1376 let incr = if key = 18 then -1 else 1 in
1377 let active, first =
1378 match search (active + incr) qsearch incr with
1379 | None ->
1380 state.text <- qsearch ^ " [not found]";
1381 active, first
1382 | Some active ->
1383 state.text <- qsearch;
1384 active, firstof active
1386 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1387 Glut.postRedisplay ();
1389 | 8 ->
1390 let len = String.length qsearch in
1391 if len = 0
1392 then ()
1393 else (
1394 if len = 1
1395 then (
1396 state.text <- "";
1397 state.outline <- Some (allowdel, active, first, outlines, "");
1399 else
1400 let qsearch = String.sub qsearch 0 (len - 1) in
1401 let active, first =
1402 match search active qsearch ~-1 with
1403 | None ->
1404 state.text <- qsearch ^ " [not found]";
1405 active, first
1406 | Some active ->
1407 state.text <- qsearch;
1408 active, firstof active
1410 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1412 Glut.postRedisplay ()
1414 | 13 ->
1415 if active < Array.length outlines
1416 then (
1417 let (_, _, n, t) = outlines.(active) in
1418 gotopage n t;
1420 state.text <- "";
1421 if allowdel then state.bookmarks <- Array.to_list outlines;
1422 state.outline <- None;
1423 Glut.postRedisplay ();
1425 | _ when key >= 32 && key < 127 ->
1426 let pattern = addchar qsearch (Char.chr key) in
1427 let active, first =
1428 match search active pattern 1 with
1429 | None ->
1430 state.text <- pattern ^ " [not found]";
1431 active, first
1432 | Some active ->
1433 state.text <- pattern;
1434 active, firstof active
1436 state.outline <- Some (allowdel, active, first, outlines, pattern);
1437 Glut.postRedisplay ()
1439 | 14 when not allowdel ->
1440 let optoutlines = narrow outlines qsearch in
1441 begin match optoutlines with
1442 | None -> state.text <- "can't narrow"
1443 | Some outlines ->
1444 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1445 match state.outlines with
1446 | Olist l -> ()
1447 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1448 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1449 end;
1450 Glut.postRedisplay ()
1452 | 21 when not allowdel ->
1453 let outline =
1454 match state.outlines with
1455 | Oarray a -> a
1456 | Olist l ->
1457 let a = Array.of_list (List.rev l) in
1458 state.outlines <- Oarray a;
1460 | Onarrow (a, b) ->
1461 state.outlines <- Oarray b;
1464 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1465 Glut.postRedisplay ()
1467 | 12 ->
1468 state.outline <-
1469 Some (allowdel, active, firstof active, outlines, qsearch);
1470 Glut.postRedisplay ()
1472 | 127 when allowdel ->
1473 let len = Array.length outlines - 1 in
1474 if len = 0
1475 then (
1476 state.outline <- None;
1477 state.bookmarks <- [];
1479 else (
1480 let bookmarks = Array.init len
1481 (fun i ->
1482 let i = if i >= active then i + 1 else i in
1483 outlines.(i)
1486 state.outline <-
1487 Some (allowdel,
1488 min active (len-1),
1489 min first (len-1),
1490 bookmarks, qsearch)
1493 Glut.postRedisplay ()
1495 | _ -> log "unknown key %d" key
1498 let keyboard ~key ~x ~y =
1499 if key = 7
1500 then
1501 wcmd "interrupt" []
1502 else
1503 match state.outline with
1504 | None -> viewkeyboard ~key ~x ~y
1505 | Some outline -> outlinekeyboard ~key ~x ~y outline
1508 let special ~key ~x ~y =
1509 match state.outline with
1510 | None ->
1511 begin match state.textentry with
1512 | None ->
1513 let y =
1514 match key with
1515 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1516 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1517 | Glut.KEY_DOWN -> clamp conf.scrollincr
1518 | Glut.KEY_PAGE_UP ->
1519 if Glut.getModifiers () land Glut.active_ctrl != 0
1520 then
1521 match state.layout with
1522 | [] -> state.y
1523 | l :: _ -> state.y - l.pagey
1524 else
1525 clamp (-state.h)
1526 | Glut.KEY_PAGE_DOWN ->
1527 if Glut.getModifiers () land Glut.active_ctrl != 0
1528 then
1529 match List.rev state.layout with
1530 | [] -> state.y
1531 | l :: _ -> getpagey l.pageno
1532 else
1533 clamp state.h
1534 | Glut.KEY_HOME -> addnav (); 0
1535 | Glut.KEY_END ->
1536 addnav ();
1537 state.maxy - (if conf.maxhfit then state.h else 0)
1539 | Glut.KEY_RIGHT when conf.margin < 0 ->
1540 state.x <- state.x - 10;
1541 state.y
1542 | Glut.KEY_LEFT when conf.margin < 0 ->
1543 state.x <- state.x + 10;
1544 state.y
1546 | _ -> state.y
1548 if not conf.verbose then state.text <- "";
1549 gotoy y
1551 | Some (c, s, Some onhist, onkey, ondone) ->
1552 let s =
1553 match key with
1554 | Glut.KEY_UP -> onhist HCprev
1555 | Glut.KEY_DOWN -> onhist HCnext
1556 | Glut.KEY_HOME -> onhist HCfirst
1557 | Glut.KEY_END -> onhist HClast
1558 | _ -> state.text
1560 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1561 Glut.postRedisplay ()
1563 | _ -> ()
1566 | Some (allowdel, active, first, outlines, qsearch) ->
1567 let maxrows = maxoutlinerows () in
1568 let navigate incr =
1569 let active = active + incr in
1570 let active = max 0 (min active (Array.length outlines - 1)) in
1571 let first =
1572 if active > first
1573 then
1574 let rows = active - first in
1575 if rows > maxrows then active - maxrows else first
1576 else active
1578 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1579 Glut.postRedisplay ()
1581 match key with
1582 | Glut.KEY_UP -> navigate ~-1
1583 | Glut.KEY_DOWN -> navigate 1
1584 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1585 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1587 | Glut.KEY_HOME ->
1588 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1589 Glut.postRedisplay ()
1591 | Glut.KEY_END ->
1592 let active = Array.length outlines - 1 in
1593 let first = max 0 (active - maxrows) in
1594 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1595 Glut.postRedisplay ()
1597 | _ -> ()
1600 let drawplaceholder l =
1601 GlDraw.color (scalecolor 1.0);
1602 GlDraw.rect
1603 (0.0, float l.pagedispy)
1604 (float l.pagew, float (l.pagedispy + l.pagevh))
1606 let x = 0.0
1607 and y = float (l.pagedispy + 13) in
1608 let font = Glut.BITMAP_8_BY_13 in
1609 GlDraw.color (0.0, 0.0, 0.0);
1610 GlPix.raster_pos ~x ~y ();
1611 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1612 ("Loading " ^ string_of_int l.pageno);
1615 let now () = Unix.gettimeofday ();;
1617 let drawpage i l =
1618 begin match getopaque l.pageno with
1619 | Some opaque when validopaque opaque ->
1620 if state.textentry = None
1621 then GlDraw.color (scalecolor 1.0)
1622 else GlDraw.color (scalecolor 0.4);
1623 let a = now () in
1624 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1625 opaque;
1626 let b = now () in
1627 let d = b-.a in
1628 vlog "draw %f sec" d;
1630 | _ ->
1631 drawplaceholder l;
1632 end;
1633 l.pagedispy + l.pagevh;
1636 let scrollindicator () =
1637 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1638 GlDraw.color (0.64 , 0.64, 0.64);
1639 GlDraw.rect
1640 (0., 0.)
1641 (float conf.scrollw, float state.h)
1643 GlDraw.color (0.0, 0.0, 0.0);
1644 let sh = (float (maxy + state.h) /. float state.h) in
1645 let sh = float state.h /. sh in
1646 let sh = max sh (float conf.scrollh) in
1648 let percent =
1649 if state.y = state.maxy
1650 then 1.0
1651 else float state.y /. float maxy
1653 let position = (float state.h -. sh) *. percent in
1655 let position =
1656 if position +. sh > float state.h
1657 then
1658 float state.h -. sh
1659 else
1660 position
1662 GlDraw.rect
1663 (0.0, position)
1664 (float conf.scrollw, position +. sh)
1668 let showsel margin =
1669 match state.mstate with
1670 | Mnone | Mpan _ ->
1673 | Msel ((x0, y0), (x1, y1)) ->
1674 let rec loop = function
1675 | l :: ls ->
1676 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1677 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1678 then
1679 match getopaque l.pageno with
1680 | Some opaque when validopaque opaque ->
1681 let oy = -l.pagey + l.pagedispy in
1682 seltext opaque
1683 (x0 - margin - state.x, y0,
1684 x1 - margin - state.x, y1) oy;
1686 | _ -> ()
1687 else loop ls
1688 | [] -> ()
1690 loop state.layout
1693 let showrects () =
1694 Gl.enable `blend;
1695 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1696 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1697 List.iter
1698 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1699 List.iter (fun l ->
1700 if l.pageno = pageno
1701 then (
1702 let d = float (l.pagedispy - l.pagey) in
1703 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1704 GlDraw.begins `quads;
1706 GlDraw.vertex2 (x0, y0+.d);
1707 GlDraw.vertex2 (x1, y1+.d);
1708 GlDraw.vertex2 (x2, y2+.d);
1709 GlDraw.vertex2 (x3, y3+.d);
1711 GlDraw.ends ();
1713 ) state.layout
1714 ) state.rects
1716 Gl.disable `blend;
1719 let showoutline = function
1720 | None -> ()
1721 | Some (allowdel, active, first, outlines, qsearch) ->
1722 Gl.enable `blend;
1723 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1724 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1725 GlDraw.rect (0., 0.) (float state.w, float state.h);
1726 Gl.disable `blend;
1728 GlDraw.color (1., 1., 1.);
1729 let font = Glut.BITMAP_9_BY_15 in
1730 let draw_string x y s =
1731 GlPix.raster_pos ~x ~y ();
1732 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1734 let rec loop row =
1735 if row = Array.length outlines || (row - first) * 16 > state.h
1736 then ()
1737 else (
1738 let (s, l, _, _) = outlines.(row) in
1739 let y = (row - first) * 16 in
1740 let x = 5 + 15*l in
1741 if row = active
1742 then (
1743 Gl.enable `blend;
1744 GlDraw.polygon_mode `both `line;
1745 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1746 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1747 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
1748 GlDraw.rect (0., float (y + 1))
1749 ((float state.w -. sw), float (y + 18));
1750 GlDraw.polygon_mode `both `fill;
1751 Gl.disable `blend;
1752 GlDraw.color (1., 1., 1.);
1754 draw_string (float x) (float (y + 16)) s;
1755 loop (row+1)
1758 loop first
1761 let display () =
1762 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1763 GlDraw.viewport margin 0 state.w state.h;
1764 GlClear.color (scalecolor 0.5);
1765 GlClear.clear [`color];
1766 if state.x != 0
1767 then (
1768 let x = float state.x in
1769 GlMat.translate ~x ();
1771 if conf.margin < 0
1772 then (
1773 Gl.enable `scissor_test;
1774 GlMisc.scissor 0 0 (state.winw - conf.scrollw) state.h;
1776 let _lasty = List.fold_left drawpage 0 (state.layout) in
1777 if conf.margin < 0
1778 then
1779 Gl.disable `scissor_test
1781 if state.x != 0
1782 then (
1783 let x = -.float state.x in
1784 GlMat.translate ~x ();
1786 showrects ();
1787 GlDraw.viewport (state.winw - conf.scrollw) 0 state.winw state.h;
1788 scrollindicator ();
1789 showsel margin;
1790 GlDraw.viewport 0 0 state.winw state.h;
1791 showoutline state.outline;
1792 enttext ();
1793 Glut.swapBuffers ();
1796 let getunder x y =
1797 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1798 let x = x - margin - state.x in
1799 let rec f = function
1800 | l :: rest ->
1801 begin match getopaque l.pageno with
1802 | Some opaque when validopaque opaque ->
1803 let y = y - l.pagedispy in
1804 if y > 0
1805 then
1806 let y = l.pagey + y in
1807 match whatsunder opaque x y with
1808 | Unone -> f rest
1809 | under -> under
1810 else
1811 f rest
1812 | _ ->
1813 f rest
1815 | [] -> Unone
1817 f state.layout
1820 let mouse ~button ~bstate ~x ~y =
1821 match button with
1822 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1823 let incr =
1824 if n = 3
1825 then
1826 -conf.scrollincr
1827 else
1828 conf.scrollincr
1830 let incr = incr * 2 in
1831 let y = clamp incr in
1832 gotoy y
1834 | Glut.LEFT_BUTTON when state.outline = None
1835 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1836 if bstate = Glut.DOWN
1837 then
1838 state.mstate <- Mpan (x, y)
1839 else
1840 state.mstate <- Mnone
1842 | Glut.LEFT_BUTTON when state.outline = None ->
1843 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1844 begin match dest with
1845 | Ulinkgoto (pageno, top) ->
1846 if pageno >= 0
1847 then
1848 gotopage1 pageno top
1850 | Ulinkuri s ->
1851 print_endline s
1853 | Unone when bstate = Glut.DOWN ->
1854 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1855 state.mstate <- Mpan (x, y);
1857 | Unone | Utext _ ->
1858 if bstate = Glut.DOWN
1859 then (
1860 if state.rotate mod 360 = 0
1861 then (
1862 state.mstate <- Msel ((x, y), (x, y));
1863 Glut.postRedisplay ()
1866 else (
1867 match state.mstate with
1868 | Mnone -> ()
1870 | Mpan _ ->
1871 Glut.setCursor Glut.CURSOR_INHERIT;
1872 state.mstate <- Mnone
1874 | Msel ((x0, y0), (x1, y1)) ->
1875 let f l =
1876 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1877 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1878 then
1879 match getopaque l.pageno with
1880 | Some opaque when validopaque opaque ->
1881 copysel opaque
1882 | _ -> ()
1884 List.iter f state.layout;
1885 copysel ""; (* ugly *)
1886 Glut.setCursor Glut.CURSOR_INHERIT;
1887 state.mstate <- Mnone;
1891 | _ ->
1894 let mouse ~button ~state ~x ~y = mouse button state x y;;
1896 let motion ~x ~y =
1897 if state.outline = None
1898 then
1899 match state.mstate with
1900 | Mnone -> ()
1902 | Mpan (x0, y0) ->
1903 let dx = x - x0
1904 and dy = y0 - y in
1905 state.mstate <- Mpan (x, y);
1906 if conf.margin < 0 then state.x <- state.x + dx;
1907 let y = clamp dy in
1908 gotoy y
1910 | Msel (a, _) ->
1911 state.mstate <- Msel (a, (x, y));
1912 Glut.postRedisplay ()
1915 let pmotion ~x ~y =
1916 if state.outline = None
1917 then
1918 match state.mstate with
1919 | Mnone ->
1920 begin match getunder x y with
1921 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1922 | Ulinkuri uri ->
1923 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1924 Glut.setCursor Glut.CURSOR_INFO
1925 | Ulinkgoto (page, y) ->
1926 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1927 Glut.setCursor Glut.CURSOR_INFO
1928 | Utext s ->
1929 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1930 Glut.setCursor Glut.CURSOR_TEXT
1933 | Mpan _ | Msel _ ->
1937 let () =
1938 let statepath =
1939 let home =
1940 if Sys.os_type = "Win32"
1941 then
1942 try Sys.getenv "HOMEPATH" with Not_found -> ""
1943 else
1944 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1946 Filename.concat home "llpp"
1948 let pstate =
1950 let ic = open_in_bin statepath in
1951 let hash = input_value ic in
1952 close_in ic;
1953 hash
1954 with exn ->
1955 if false
1956 then
1957 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1959 Hashtbl.create 1
1961 let savestate () =
1963 let w, h =
1964 match state.fullscreen with
1965 | None -> state.winw, state.h
1966 | Some wh -> wh
1968 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1969 let oc = open_out_bin statepath in
1970 output_value oc pstate
1971 with exn ->
1972 if false
1973 then
1974 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1977 let setstate () =
1979 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1980 state.w <- statew;
1981 state.h <- stateh;
1982 state.bookmarks <- statebookmarks;
1983 with Not_found -> ()
1984 | exn ->
1985 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1988 Arg.parse
1989 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
1990 (fun s -> state.path <- s)
1991 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
1993 let name =
1994 if String.length state.path = 0
1995 then (prerr_endline "filename missing"; exit 1)
1996 else state.path
1999 setstate ();
2000 let _ = Glut.init Sys.argv in
2001 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2002 let () = Glut.initWindowSize state.w state.h in
2003 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
2005 let csock, ssock =
2006 if Sys.os_type = "Unix"
2007 then
2008 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2009 else
2010 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2011 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2012 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2013 Unix.bind sock addr;
2014 Unix.listen sock 1;
2015 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2016 Unix.connect csock addr;
2017 let ssock, _ = Unix.accept sock in
2018 Unix.close sock;
2019 let opts sock =
2020 Unix.setsockopt sock Unix.TCP_NODELAY true;
2021 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2023 opts ssock;
2024 opts csock;
2025 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2026 ssock, csock
2029 let () = Glut.displayFunc display in
2030 let () = Glut.reshapeFunc reshape in
2031 let () = Glut.keyboardFunc keyboard in
2032 let () = Glut.specialFunc special in
2033 let () = Glut.idleFunc (Some idle) in
2034 let () = Glut.mouseFunc mouse in
2035 let () = Glut.motionFunc motion in
2036 let () = Glut.passiveMotionFunc pmotion in
2038 init ssock;
2039 state.csock <- csock;
2040 state.ssock <- ssock;
2041 state.text <- "Opening " ^ name;
2042 writecmd state.csock ("open " ^ state.path ^ "\000" ^ state.password ^ "\000");
2044 at_exit savestate;
2046 let rec handlelablglutbug () =
2048 Glut.mainLoop ();
2049 with Glut.BadEnum "key in special_of_int" ->
2050 showtext '!' " LablGlut bug: special key not recognized";
2051 handlelablglutbug ()
2053 handlelablglutbug ();