Add notification message when switching presentation mode
[llpp.git] / main.ml
blob2e530626eaa4b392b38673702c64d1753108d447
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 external init : Unix.file_descr -> unit = "ml_init";;
12 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext : string -> (int * int * int * int) -> int -> unit =
14 "ml_seltext";;
15 external copysel : string -> unit = "ml_copysel";;
16 external getpagewh : int -> float array = "ml_getpagewh";;
17 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
19 type mpos = int * int
20 type mstate = Msel of (mpos * mpos) | Mpan of mpos | Mnone;;
22 type 'a circbuf =
23 { store : 'a array
24 ; mutable rc : int
25 ; mutable wc : int
26 ; mutable len : int
30 type textentry = (char * string * onhist option * onkey * ondone)
31 and onkey = string -> int -> te
32 and ondone = string -> unit
33 and onhist = histcmd -> string
34 and histcmd = HCnext | HCprev | HCfirst | HClast
35 and te =
36 | TEstop
37 | TEdone of string
38 | TEcont of string
39 | TEswitch of textentry
42 let cbnew n v =
43 { store = Array.create n v
44 ; rc = 0
45 ; wc = 0
46 ; len = 0
50 let cblen b = Array.length b.store;;
52 let cbput b v =
53 let len = cblen b in
54 b.store.(b.wc) <- v;
55 b.wc <- (b.wc + 1) mod len;
56 b.len <- min (b.len + 1) len;
59 let cbpeekw b = b.store.(b.wc);;
61 let cbget b dir =
62 if b.len = 0
63 then b.store.(0)
64 else
65 let rc = b.rc + dir in
66 let rc = if rc = -1 then b.len - 1 else rc in
67 let rc = if rc = b.len then 0 else rc in
68 b.rc <- rc;
69 b.store.(rc);
72 let cbrfollowlen b =
73 b.rc <- b.len;
76 let cbclear b v =
77 b.len <- 0;
78 Array.fill b.store 0 (Array.length b.store) v;
81 type layout =
82 { pageno : int
83 ; pagedimno : int
84 ; pagew : int
85 ; pageh : int
86 ; pagedispy : int
87 ; pagey : int
88 ; pagevh : int
92 type conf =
93 { mutable scrollw : int
94 ; mutable scrollh : int
95 ; mutable icase : bool
96 ; mutable preload : bool
97 ; mutable pagebias : int
98 ; mutable verbose : bool
99 ; mutable scrollincr : int
100 ; mutable maxhfit : bool
101 ; mutable crophack : bool
102 ; mutable autoscroll : bool
103 ; mutable showall : bool
104 ; mutable hlinks : bool
105 ; mutable underinfo : bool
106 ; mutable interpagespace : int
107 ; mutable zoom : float
108 ; mutable presentation : bool
112 type outline = string * int * int * float;;
113 type outlines =
114 | Oarray of outline array
115 | Olist of outline list
116 | Onarrow of outline array * outline array
119 type rect = (float * float * float * float * float * float * float * float);;
121 type state =
122 { mutable csock : Unix.file_descr
123 ; mutable ssock : Unix.file_descr
124 ; mutable w : int
125 ; mutable h : int
126 ; mutable winw : int
127 ; mutable rotate : int
128 ; mutable x : int
129 ; mutable y : int
130 ; mutable ty : float
131 ; mutable maxy : int
132 ; mutable layout : layout list
133 ; pagemap : ((int * int * int), string) Hashtbl.t
134 ; mutable pages : (int * int * int) list
135 ; mutable pagecount : int
136 ; pagecache : string circbuf
137 ; mutable rendering : bool
138 ; mutable mstate : mstate
139 ; mutable searchpattern : string
140 ; mutable rects : (int * int * rect) list
141 ; mutable rects1 : (int * int * rect) list
142 ; mutable text : string
143 ; mutable fullscreen : (int * int) option
144 ; mutable textentry : textentry option
145 ; mutable outlines : outlines
146 ; mutable outline : (bool * int * int * outline array * string) option
147 ; mutable bookmarks : outline list
148 ; mutable path : string
149 ; mutable password : string
150 ; mutable invalidated : int
151 ; mutable colorscale : float
152 ; hists : hists
154 and hists =
155 { pat : string circbuf
156 ; pag : string circbuf
157 ; nav : float circbuf
161 let conf =
162 { scrollw = 5
163 ; scrollh = 12
164 ; icase = true
165 ; preload = true
166 ; pagebias = 0
167 ; verbose = false
168 ; scrollincr = 24
169 ; maxhfit = true
170 ; crophack = false
171 ; autoscroll = false
172 ; showall = false
173 ; hlinks = false
174 ; underinfo = false
175 ; interpagespace = 2
176 ; zoom = 1.0
177 ; presentation = false
181 let state =
182 { csock = Unix.stdin
183 ; ssock = Unix.stdin
184 ; w = 900
185 ; h = 900
186 ; winw = 900
187 ; rotate = 0
188 ; y = 0
189 ; x = 0
190 ; ty = 0.0
191 ; layout = []
192 ; maxy = max_int
193 ; pagemap = Hashtbl.create 10
194 ; pagecache = cbnew 10 ""
195 ; pages = []
196 ; pagecount = 0
197 ; rendering = false
198 ; mstate = Mnone
199 ; rects = []
200 ; rects1 = []
201 ; text = ""
202 ; fullscreen = None
203 ; textentry = None
204 ; searchpattern = ""
205 ; outlines = Olist []
206 ; outline = None
207 ; bookmarks = []
208 ; path = ""
209 ; password = ""
210 ; invalidated = 0
211 ; hists =
212 { nav = cbnew 100 0.0
213 ; pat = cbnew 20 ""
214 ; pag = cbnew 10 ""
216 ; colorscale = 1.0
220 let vlog fmt =
221 if conf.verbose
222 then
223 Printf.kprintf prerr_endline fmt
224 else
225 Printf.kprintf ignore fmt
228 let writecmd fd s =
229 let len = String.length s in
230 let n = 4 + len in
231 let b = Buffer.create n in
232 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
233 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
234 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
235 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
236 Buffer.add_string b s;
237 let s' = Buffer.contents b in
238 let n' = Unix.write fd s' 0 n in
239 if n' != n then failwith "write failed";
242 let readcmd fd =
243 let s = "xxxx" in
244 let n = Unix.read fd s 0 4 in
245 if n != 4 then failwith "incomplete read(len)";
246 let len = 0
247 lor (Char.code s.[0] lsl 24)
248 lor (Char.code s.[1] lsl 16)
249 lor (Char.code s.[2] lsl 8)
250 lor (Char.code s.[3] lsl 0)
252 let s = String.create len in
253 let n = Unix.read fd s 0 len in
254 if n != len then failwith "incomplete read(data)";
258 let yratio y =
259 if y = state.maxy
260 then 1.0
261 else float y /. float state.maxy
264 let makecmd s l =
265 let b = Buffer.create 10 in
266 Buffer.add_string b s;
267 let rec combine = function
268 | [] -> b
269 | x :: xs ->
270 Buffer.add_char b ' ';
271 let s =
272 match x with
273 | `b b -> if b then "1" else "0"
274 | `s s -> s
275 | `i i -> string_of_int i
276 | `f f -> string_of_float f
277 | `I f -> string_of_int (truncate f)
279 Buffer.add_string b s;
280 combine xs;
282 combine l;
285 let wcmd s l =
286 let cmd = Buffer.contents (makecmd s l) in
287 writecmd state.csock cmd;
290 let calcips h =
291 if conf.presentation
292 then
293 let d = state.h - h in
294 max 0 ((d + 1) / 2)
295 else
296 conf.interpagespace
299 let calcheight () =
300 let rec f pn ph pi fh l =
301 match l with
302 | (n, _, h) :: rest ->
303 let ips = calcips h in
304 let fh =
305 if n = 0 && conf.presentation
306 then
307 fh+ips
308 else
311 let fh = fh + ((n - pn) * (ph + pi)) in
312 f n h ips fh rest
314 | [] ->
315 let inc =
316 if conf.presentation
317 then 0
318 else -pi
320 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
321 max 0 fh
323 let fh = f 0 0 0 0 state.pages in
327 let getpageyh pageno =
328 let rec f pn ph pi y l =
329 match l with
330 | (n, _, h) :: rest ->
331 let ips = calcips h in
332 if n >= pageno
333 then
334 y + (pageno - pn) * (ph + pi), h
335 else
336 let y = y + (n - pn) * (ph + pi) in
337 f n h ips y rest
339 | [] ->
340 y + (pageno - pn) * (ph + pi), ph
342 f 0 0 0 0 state.pages
345 let getpagey pageno = fst (getpageyh pageno);;
347 let layout y sh =
348 let rec f ~pageno ~pdimno ~prev ~py ~vh ~pdims ~cacheleft ~accu =
349 let ((w, h, ips) as curr), rest, pdimno, p0a =
350 match pdims with
351 | (pageno', w, h) :: rest when pageno' = pageno ->
352 let ips = calcips h in
353 (w, h, ips), rest, pdimno + 1,
354 if conf.presentation && pageno = 0 then ips else 0
355 | _ ->
356 prev, pdims, pdimno, 0
358 if pageno = state.pagecount || cacheleft = 0 || vh >= sh
359 then
360 accu
361 else
362 let py = py + p0a in
363 let vy = y + vh in
364 if py + h <= vy
365 then
366 let py = py + h + ips in
367 let vh = max 0 (py - y) in
368 f ~pageno:(pageno+1)
369 ~pdimno
370 ~prev:curr
373 ~pdims:rest
374 ~cacheleft
375 ~accu
376 else
377 let top = vy - py in
378 let left = h - top in
379 let left = min (sh - vh) left in
380 let py = py + h + ips in
381 let p0y = if top < 0 then -top else 0 in
382 let e =
383 { pageno = pageno
384 ; pagedimno = pdimno
385 ; pagew = w
386 ; pageh = h
387 ; pagedispy = vh+p0y
388 ; pagey = top+p0y
389 ; pagevh = left-p0y
392 let accu = e :: accu in
393 f ~pageno:(pageno+1)
394 ~pdimno
395 ~prev:curr
397 ~vh:(vh+left+ips)
398 ~pdims:rest
399 ~cacheleft:(cacheleft-1)
400 ~accu
402 if state.invalidated = 0
403 then (
404 let accu =
406 ~pageno:0
407 ~pdimno:~-1
408 ~prev:(0,0,0)
409 ~py:0
410 ~vh:0
411 ~pdims:state.pages
412 ~cacheleft:(cblen state.pagecache)
413 ~accu:[]
415 List.rev accu
417 else
421 let clamp incr =
422 let y = state.y + incr in
423 let y = max 0 y in
424 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
428 let getopaque pageno =
429 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w, state.rotate))
430 with Not_found -> None
433 let cache pageno opaque =
434 Hashtbl.replace state.pagemap (pageno + 1, state.w, state.rotate) opaque
437 let validopaque opaque = String.length opaque > 0;;
439 let render l =
440 match getopaque l.pageno with
441 | None when not state.rendering ->
442 state.rendering <- true;
443 cache l.pageno "";
444 wcmd "render" [`i (l.pageno + 1)
445 ;`i l.pagedimno
446 ;`i l.pagew
447 ;`i l.pageh];
449 | _ -> ()
452 let loadlayout layout =
453 let rec f all = function
454 | l :: ls ->
455 begin match getopaque l.pageno with
456 | None -> render l; f false ls
457 | Some opaque -> f (all && validopaque opaque) ls
459 | [] -> all
461 f (layout <> []) layout;
464 let preload () =
465 if conf.preload
466 then
467 let evictedvisible =
468 let evictedopaque = cbpeekw state.pagecache in
469 List.exists (fun l ->
470 match getopaque l.pageno with
471 | Some opaque when validopaque opaque ->
472 evictedopaque = opaque
473 | otherwise -> false
474 ) state.layout
476 if not evictedvisible
477 then
478 let rely = yratio state.y in
479 let presentation = conf.presentation in
480 let interpagespace = conf.interpagespace in
481 let maxy = state.maxy in
482 conf.presentation <- false;
483 conf.interpagespace <- 0;
484 state.maxy <- calcheight ();
485 let y = truncate (float state.maxy *. rely) in
486 let y = if y < state.h then 0 else y - state.h in
487 let pages = layout y (state.h*3) in
488 List.iter render pages;
489 conf.presentation <- presentation;
490 conf.interpagespace <- interpagespace;
491 state.maxy <- maxy;
494 let gotoy y =
495 let y = max 0 y in
496 let y = min state.maxy y in
497 let pages = layout y state.h in
498 let ready = loadlayout pages in
499 state.ty <- yratio y;
500 if conf.showall
501 then (
502 if ready
503 then (
504 state.layout <- pages;
505 state.y <- y;
506 Glut.postRedisplay ();
509 else (
510 state.layout <- pages;
511 state.y <- y;
512 Glut.postRedisplay ();
514 preload ();
517 let addnav () =
518 cbput state.hists.nav (yratio state.y);
519 cbrfollowlen state.hists.nav;
522 let getnav () =
523 let y = cbget state.hists.nav ~-1 in
524 truncate (y *. float state.maxy)
527 let gotopage n top =
528 let y, h = getpageyh n in
529 addnav ();
530 gotoy (y + (truncate (top *. float h)));
533 let gotopage1 n top =
534 let y = getpagey n in
535 addnav ();
536 gotoy (y + top);
539 let invalidate () =
540 state.layout <- [];
541 state.pages <- [];
542 state.rects <- [];
543 state.rects1 <- [];
544 state.invalidated <- state.invalidated + 1;
547 let scalecolor c =
548 let c = c *. state.colorscale in
549 (c, c, c);
552 let represent () =
553 let y =
554 match state.layout with
555 | [] ->
556 let rely = yratio state.y in
557 state.maxy <- calcheight ();
558 truncate (float state.maxy *. rely)
560 | l :: _ ->
561 state.maxy <- calcheight ();
562 getpagey l.pageno
564 gotoy y
567 let reshape ~w ~h =
568 let margin = truncate (0.5 *. (float w -. float w *. conf.zoom)) in
569 state.winw <- w;
570 let w = w - margin * 2 - conf.scrollw in
571 state.w <- w;
572 state.h <- h;
573 GlMat.mode `modelview;
574 GlMat.load_identity ();
575 GlMat.mode `projection;
576 GlMat.load_identity ();
577 GlMat.rotate ~x:1.0 ~angle:180.0 ();
578 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
579 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
580 GlClear.color (scalecolor 1.0);
581 GlClear.clear [`color];
583 invalidate ();
584 wcmd "geometry" [`i state.w; `i h];
587 let showtext c s =
588 GlDraw.viewport 0 0 state.winw state.h;
589 GlDraw.color (0.0, 0.0, 0.0);
590 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
591 GlDraw.rect
592 (0.0, float (state.h - 18))
593 (float state.w -. sw, float state.h)
595 let font = Glut.BITMAP_8_BY_13 in
596 GlDraw.color (1.0, 1.0, 1.0);
597 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
598 Glut.bitmapCharacter ~font ~c:(Char.code c);
599 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
602 let enttext () =
603 let len = String.length state.text in
604 match state.textentry with
605 | None ->
606 if len > 0 then showtext ' ' state.text
608 | Some (c, text, _, _, _) ->
609 let s =
610 if len > 0
611 then
612 text ^ " [" ^ state.text ^ "]"
613 else
614 text
616 showtext c s;
619 let showtext c s =
620 if true
621 then (
622 state.text <- Printf.sprintf "%c%s" c s;
623 Glut.postRedisplay ();
625 else (
626 showtext c s;
627 Glut.swapBuffers ();
631 let act cmd =
632 match cmd.[0] with
633 | 'c' ->
634 state.pages <- [];
636 | 'D' ->
637 state.rects <- state.rects1;
638 Glut.postRedisplay ()
640 | 'C' ->
641 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
642 state.pagecount <- n;
643 state.invalidated <- state.invalidated - 1;
644 if state.invalidated = 0
645 then represent ()
647 | 't' ->
648 let s = Scanf.sscanf cmd "t %n"
649 (fun n -> String.sub cmd n (String.length cmd - n))
651 Glut.setWindowTitle s
653 | 'T' ->
654 let s = Scanf.sscanf cmd "T %n"
655 (fun n -> String.sub cmd n (String.length cmd - n))
657 if state.textentry = None
658 then (
659 state.text <- s;
660 showtext ' ' s;
662 else (
663 state.text <- s;
664 Glut.postRedisplay ();
667 | 'V' ->
668 if conf.verbose
669 then
670 let s = Scanf.sscanf cmd "V %n"
671 (fun n -> String.sub cmd n (String.length cmd - n))
673 state.text <- s;
674 showtext ' ' s;
676 | 'F' ->
677 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
678 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
679 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
680 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
682 let y = (getpagey pageno) + truncate y0 in
683 addnav ();
684 gotoy y;
685 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
687 | 'R' ->
688 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
689 Scanf.sscanf cmd "R %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 state.rects1 <-
694 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
696 | 'r' ->
697 let n, w, h, r, p =
698 Scanf.sscanf cmd "r %d %d %d %d %s"
699 (fun n w h r p -> (n, w, h, r, p))
701 Hashtbl.replace state.pagemap (n, w, r) p;
702 let opaque = cbpeekw state.pagecache in
703 if validopaque opaque
704 then (
705 let k =
706 Hashtbl.fold
707 (fun k v a -> if v = opaque then k else a)
708 state.pagemap (-1, -1, -1)
710 wcmd "free" [`s opaque];
711 Hashtbl.remove state.pagemap k
713 cbput state.pagecache p;
714 state.rendering <- false;
715 if conf.showall
716 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
717 else (
718 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
719 if visible
720 then gotoy state.y
721 else (ignore (loadlayout state.layout); preload ())
724 | 'l' ->
725 let (n, w, h) as pagelayout =
726 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
728 state.pages <- pagelayout :: state.pages
730 | 'o' ->
731 let (l, n, t, h, pos) =
732 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
734 let s = String.sub cmd pos (String.length cmd - pos) in
735 let s =
736 let l = String.length s in
737 let b = Buffer.create (String.length s) in
738 let rec loop pc2 i =
739 if i = l
740 then ()
741 else
742 let pc2 =
743 match s.[i] with
744 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
745 | '\xc2' -> true
746 | c ->
747 let c = if Char.code c land 0x80 = 0 then c else '?' in
748 Buffer.add_char b c;
749 false
751 loop pc2 (i+1)
753 loop false 0;
754 Buffer.contents b
756 let outline = (s, l, n, float t /. float h) in
757 let outlines =
758 match state.outlines with
759 | Olist outlines -> Olist (outline :: outlines)
760 | Oarray _ -> Olist [outline]
761 | Onarrow _ -> Olist [outline]
763 state.outlines <- outlines
765 | _ ->
766 log "unknown cmd `%S'" cmd
769 let now = Unix.gettimeofday;;
771 let idle () =
772 let rec loop delay =
773 let r, _, _ = Unix.select [state.csock] [] [] delay in
774 begin match r with
775 | [] ->
776 if conf.autoscroll
777 then begin
778 let y = state.y + conf.scrollincr in
779 let y = if y >= state.maxy then 0 else y in
780 gotoy y;
781 state.text <- "";
782 end;
784 | _ ->
785 let cmd = readcmd state.csock in
786 act cmd;
787 loop 0.0
788 end;
789 in loop 0.001
792 let onhist cb = function
793 | HCprev -> cbget cb ~-1
794 | HCnext -> cbget cb 1
795 | HCfirst -> cbget cb ~-(cb.rc)
796 | HClast -> cbget cb (cb.len - 1 - cb.rc)
799 let search pattern forward =
800 if String.length pattern > 0
801 then
802 let pn, py =
803 match state.layout with
804 | [] -> 0, 0
805 | l :: _ ->
806 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
808 let cmd =
809 let b = makecmd "search"
810 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
812 Buffer.add_char b ',';
813 Buffer.add_string b pattern;
814 Buffer.add_char b '\000';
815 Buffer.contents b;
817 writecmd state.csock cmd;
820 let intentry text key =
821 let c = Char.unsafe_chr key in
822 match c with
823 | '0' .. '9' ->
824 let s = "x" in s.[0] <- c;
825 let text = text ^ s in
826 TEcont text
828 | _ ->
829 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
830 TEcont text
833 let addchar s c =
834 let b = Buffer.create (String.length s + 1) in
835 Buffer.add_string b s;
836 Buffer.add_char b c;
837 Buffer.contents b;
840 let textentry text key =
841 let c = Char.unsafe_chr key in
842 match c with
843 | _ when key >= 32 && key < 127 ->
844 let text = addchar text c in
845 TEcont text
847 | _ ->
848 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
849 TEcont text
852 let rotate angle =
853 state.rotate <- angle;
854 invalidate ();
855 wcmd "rotate" [`i angle];
858 let optentry text key =
859 let btos b = if b then "on" else "off" in
860 let c = Char.unsafe_chr key in
861 match c with
862 | 's' ->
863 let ondone s =
864 try conf.scrollincr <- int_of_string s with exc ->
865 state.text <- Printf.sprintf "bad integer `%s': %s"
866 s (Printexc.to_string exc)
868 TEswitch ('#', "", None, intentry, ondone)
870 | 'R' ->
871 let ondone s =
872 match try
873 Some (int_of_string s)
874 with exc ->
875 state.text <- Printf.sprintf "bad integer `%s': %s"
876 s (Printexc.to_string exc);
877 None
878 with
879 | Some angle -> rotate angle
880 | None -> ()
882 TEswitch ('^', "", None, intentry, ondone)
884 | 'i' ->
885 conf.icase <- not conf.icase;
886 TEdone ("case insensitive search " ^ (btos conf.icase))
888 | 'p' ->
889 conf.preload <- not conf.preload;
890 gotoy state.y;
891 TEdone ("preload " ^ (btos conf.preload))
893 | 'v' ->
894 conf.verbose <- not conf.verbose;
895 TEdone ("verbose " ^ (btos conf.verbose))
897 | 'h' ->
898 conf.maxhfit <- not conf.maxhfit;
899 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
900 TEdone ("maxhfit " ^ (btos conf.maxhfit))
902 | 'c' ->
903 conf.crophack <- not conf.crophack;
904 TEdone ("crophack " ^ btos conf.crophack)
906 | 'a' ->
907 conf.showall <- not conf.showall;
908 TEdone ("showall " ^ btos conf.showall)
910 | 'f' ->
911 conf.underinfo <- not conf.underinfo;
912 TEdone ("underinfo " ^ btos conf.underinfo)
914 | 'S' ->
915 let ondone s =
917 conf.interpagespace <- int_of_string s;
918 let rely = yratio state.y in
919 state.maxy <- calcheight ();
920 gotoy (truncate (float state.maxy *. rely));
921 with exc ->
922 state.text <- Printf.sprintf "bad integer `%s': %s"
923 s (Printexc.to_string exc)
925 TEswitch ('%', "", None, intentry, ondone)
927 | _ ->
928 state.text <- Printf.sprintf "bad option %d `%c'" key c;
929 TEstop
932 let maxoutlinerows () = (state.h - 31) / 16;;
934 let enterselector allowdel outlines errmsg =
935 if Array.length outlines = 0
936 then (
937 showtext ' ' errmsg;
939 else (
940 Glut.setCursor Glut.CURSOR_INHERIT;
941 let pageno =
942 match state.layout with
943 | [] -> -1
944 | {pageno=pageno} :: rest -> pageno
946 let active =
947 let rec loop n =
948 if n = Array.length outlines
949 then 0
950 else
951 let (_, _, outlinepageno, _) = outlines.(n) in
952 if outlinepageno >= pageno then n else loop (n+1)
954 loop 0
956 state.outline <-
957 Some (allowdel, active,
958 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
959 Glut.postRedisplay ();
963 let enteroutlinemode () =
964 let outlines =
965 match state.outlines with
966 | Oarray a -> a
967 | Olist l ->
968 let a = Array.of_list (List.rev l) in
969 state.outlines <- Oarray a;
971 | Onarrow (a, b) -> a
973 enterselector false outlines "Document has no outline";
976 let enterbookmarkmode () =
977 let bookmarks = Array.of_list state.bookmarks in
978 enterselector true bookmarks "Document has no bookmarks (yet)";
982 let quickbookmark ?title () =
983 match state.layout with
984 | [] -> ()
985 | l :: _ ->
986 let title =
987 match title with
988 | None ->
989 let sec = Unix.gettimeofday () in
990 let tm = Unix.localtime sec in
991 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
992 l.pageno
993 tm.Unix.tm_mday
994 tm.Unix.tm_mon
995 (tm.Unix.tm_year + 1900)
996 tm.Unix.tm_hour
997 tm.Unix.tm_min
998 | Some title -> title
1000 state.bookmarks <-
1001 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1004 let doreshape w h =
1005 state.fullscreen <- None;
1006 Glut.reshapeWindow w h;
1009 let opendoc path password =
1010 invalidate ();
1011 state.path <- path;
1012 state.password <- password;
1013 Hashtbl.clear state.pagemap;
1015 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1016 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1017 wcmd "geometry" [`i state.w; `i state.h];
1020 let viewkeyboard ~key ~x ~y =
1021 let enttext te =
1022 state.textentry <- te;
1023 state.text <- "";
1024 enttext ();
1025 Glut.postRedisplay ()
1027 match state.textentry with
1028 | None ->
1029 let c = Char.chr key in
1030 begin match c with
1031 | '\027' | 'q' ->
1032 exit 0
1034 | '\008' ->
1035 let y = getnav () in
1036 gotoy y
1038 | 'o' ->
1039 enteroutlinemode ()
1041 | 'u' ->
1042 state.rects <- [];
1043 state.text <- "";
1044 Glut.postRedisplay ()
1046 | '/' | '?' ->
1047 let ondone isforw s =
1048 cbput state.hists.pat s;
1049 cbrfollowlen state.hists.pat;
1050 state.searchpattern <- s;
1051 search s isforw
1053 enttext (Some (c, "", Some (onhist state.hists.pat),
1054 textentry, ondone (c ='/')))
1056 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1057 conf.zoom <- min 10.0 (conf.zoom +. 0.1);
1058 reshape state.winw state.h
1060 | '+' ->
1061 let ondone s =
1062 let n =
1063 try int_of_string s with exc ->
1064 state.text <- Printf.sprintf "bad integer `%s': %s"
1065 s (Printexc.to_string exc);
1066 max_int
1068 if n != max_int
1069 then (
1070 conf.pagebias <- n;
1071 state.text <- "page bias is now " ^ string_of_int n;
1074 enttext (Some ('+', "", None, intentry, ondone))
1076 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1077 conf.zoom <- max 0.1 (conf.zoom -. 0.1);
1078 if conf.zoom <= 1.0 then state.x <- 0;
1079 reshape state.winw state.h;
1081 | '-' ->
1082 let ondone msg =
1083 state.text <- msg;
1085 enttext (Some ('-', "", None, optentry, ondone))
1087 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1088 state.x <- 0;
1089 conf.zoom <- 1.0;
1090 reshape state.winw state.h
1092 | '0' .. '9' ->
1093 let ondone s =
1094 let n =
1095 try int_of_string s with exc ->
1096 state.text <- Printf.sprintf "bad integer `%s': %s"
1097 s (Printexc.to_string exc);
1100 if n >= 0
1101 then (
1102 addnav ();
1103 cbput state.hists.pag (string_of_int n);
1104 cbrfollowlen state.hists.pag;
1105 gotoy (getpagey (n + conf.pagebias - 1))
1108 let pageentry text key =
1109 match Char.unsafe_chr key with
1110 | 'g' -> TEdone text
1111 | _ -> intentry text key
1113 let text = "x" in text.[0] <- c;
1114 enttext (Some (':', text, Some (onhist state.hists.pag),
1115 pageentry, ondone))
1117 | 'b' ->
1118 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
1119 reshape state.winw state.h;
1121 | 'l' ->
1122 conf.hlinks <- not conf.hlinks;
1123 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1124 Glut.postRedisplay ()
1126 | 'a' ->
1127 conf.autoscroll <- not conf.autoscroll
1129 | 'P' ->
1130 conf.presentation <- not conf.presentation;
1131 showtext ' ' ("Presnetation mode " ^
1132 if conf.presentation then "on" else "off");
1133 represent ()
1135 | 'f' ->
1136 begin match state.fullscreen with
1137 | None ->
1138 state.fullscreen <- Some (state.w, state.h);
1139 Glut.fullScreen ()
1140 | Some (w, h) ->
1141 state.fullscreen <- None;
1142 doreshape w h
1145 | 'g' ->
1146 gotoy 0
1148 | 'n' ->
1149 search state.searchpattern true
1151 | 'p' | 'N' ->
1152 search state.searchpattern false
1154 | 't' ->
1155 begin match state.layout with
1156 | [] -> ()
1157 | l :: _ ->
1158 gotoy (getpagey l.pageno)
1161 | ' ' ->
1162 begin match List.rev state.layout with
1163 | [] -> ()
1164 | l :: _ ->
1165 let pageno = min (l.pageno+1) (state.pagecount-1) in
1166 gotoy (getpagey pageno)
1169 | '\127' ->
1170 begin match state.layout with
1171 | [] -> ()
1172 | l :: _ ->
1173 let pageno = max 0 (l.pageno-1) in
1174 gotoy (getpagey pageno)
1177 | '=' ->
1178 let f (fn, ln) l =
1179 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1181 let fn, ln = List.fold_left f (-1, -1) state.layout in
1182 let s =
1183 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1184 let percent =
1185 if maxy <= 0
1186 then 100.
1187 else (100. *. (float state.y /. float maxy)) in
1188 if fn = ln
1189 then
1190 Printf.sprintf "Page %d of %d %.2f%%"
1191 (fn+1) state.pagecount percent
1192 else
1193 Printf.sprintf
1194 "Pages %d-%d of %d %.2f%%"
1195 (fn+1) (ln+1) state.pagecount percent
1197 showtext ' ' s;
1199 | 'w' ->
1200 begin match state.layout with
1201 | [] -> ()
1202 | l :: _ ->
1203 doreshape l.pagew l.pageh;
1204 Glut.postRedisplay ();
1207 | '\'' ->
1208 enterbookmarkmode ()
1210 | 'm' ->
1211 let ondone s =
1212 match state.layout with
1213 | l :: _ ->
1214 state.bookmarks <-
1215 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1216 :: state.bookmarks
1217 | _ -> ()
1219 enttext (Some ('~', "", None, textentry, ondone))
1221 | '~' ->
1222 quickbookmark ();
1223 showtext ' ' "Quick bookmark added";
1225 | 'z' ->
1226 begin match state.layout with
1227 | l :: _ ->
1228 let a = getpagewh l.pagedimno in
1229 let w, h =
1230 if conf.crophack
1231 then
1232 (truncate (1.8 *. (a.(1) -. a.(0))),
1233 truncate (1.2 *. (a.(3) -. a.(0))))
1234 else
1235 (truncate (a.(1) -. a.(0)),
1236 truncate (a.(3) -. a.(0)))
1238 doreshape w h;
1239 Glut.postRedisplay ();
1241 | [] -> ()
1244 | '<' | '>' ->
1245 rotate (state.rotate + (if c = '>' then 30 else -30));
1247 | '[' | ']' ->
1248 state.colorscale <-
1249 max 0.0
1250 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1251 Glut.postRedisplay ()
1253 | 'k' -> gotoy (clamp (-conf.scrollincr))
1254 | 'j' -> gotoy (clamp conf.scrollincr)
1256 | 'r' -> opendoc state.path state.password
1258 | _ ->
1259 vlog "huh? %d %c" key (Char.chr key);
1262 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1263 let len = String.length text in
1264 if len = 0
1265 then (
1266 state.textentry <- None;
1267 Glut.postRedisplay ();
1269 else (
1270 let s = String.sub text 0 (len - 1) in
1271 enttext (Some (c, s, onhist, onkey, ondone))
1274 | Some (c, text, onhist, onkey, ondone) ->
1275 begin match Char.unsafe_chr key with
1276 | '\r' | '\n' ->
1277 ondone text;
1278 state.textentry <- None;
1279 Glut.postRedisplay ()
1281 | '\027' ->
1282 state.textentry <- None;
1283 Glut.postRedisplay ()
1285 | _ ->
1286 begin match onkey text key with
1287 | TEdone text ->
1288 state.textentry <- None;
1289 ondone text;
1290 Glut.postRedisplay ()
1292 | TEcont text ->
1293 enttext (Some (c, text, onhist, onkey, ondone));
1295 | TEstop ->
1296 state.textentry <- None;
1297 Glut.postRedisplay ()
1299 | TEswitch te ->
1300 state.textentry <- Some te;
1301 Glut.postRedisplay ()
1302 end;
1303 end;
1306 let narrow outlines pattern =
1307 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1308 match reopt with
1309 | None -> None
1310 | Some re ->
1311 let rec fold accu n =
1312 if n = -1
1313 then accu
1314 else
1315 let (s, _, _, _) as o = outlines.(n) in
1316 let accu =
1317 if (try ignore (Str.search_forward re s 0); true
1318 with Not_found -> false)
1319 then (o :: accu)
1320 else accu
1322 fold accu (n-1)
1324 let matched = fold [] (Array.length outlines - 1) in
1325 if matched = [] then None else Some (Array.of_list matched)
1328 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1329 let search active pattern incr =
1330 let dosearch re =
1331 let rec loop n =
1332 if n = Array.length outlines || n = -1
1333 then None
1334 else
1335 let (s, _, _, _) = outlines.(n) in
1337 (try ignore (Str.search_forward re s 0); true
1338 with Not_found -> false)
1339 then Some n
1340 else loop (n + incr)
1342 loop active
1345 let re = Str.regexp_case_fold pattern in
1346 dosearch re
1347 with Failure s ->
1348 state.text <- s;
1349 None
1351 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1352 match key with
1353 | 27 ->
1354 if String.length qsearch = 0
1355 then (
1356 state.text <- "";
1357 state.outline <- None;
1358 Glut.postRedisplay ();
1360 else (
1361 state.text <- "";
1362 state.outline <- Some (allowdel, active, first, outlines, "");
1363 Glut.postRedisplay ();
1366 | 18 | 19 ->
1367 let incr = if key = 18 then -1 else 1 in
1368 let active, first =
1369 match search (active + incr) qsearch incr with
1370 | None ->
1371 state.text <- qsearch ^ " [not found]";
1372 active, first
1373 | Some active ->
1374 state.text <- qsearch;
1375 active, firstof active
1377 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1378 Glut.postRedisplay ();
1380 | 8 ->
1381 let len = String.length qsearch in
1382 if len = 0
1383 then ()
1384 else (
1385 if len = 1
1386 then (
1387 state.text <- "";
1388 state.outline <- Some (allowdel, active, first, outlines, "");
1390 else
1391 let qsearch = String.sub qsearch 0 (len - 1) in
1392 let active, first =
1393 match search active qsearch ~-1 with
1394 | None ->
1395 state.text <- qsearch ^ " [not found]";
1396 active, first
1397 | Some active ->
1398 state.text <- qsearch;
1399 active, firstof active
1401 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1403 Glut.postRedisplay ()
1405 | 13 ->
1406 if active < Array.length outlines
1407 then (
1408 let (_, _, n, t) = outlines.(active) in
1409 gotopage n t;
1411 state.text <- "";
1412 if allowdel then state.bookmarks <- Array.to_list outlines;
1413 state.outline <- None;
1414 Glut.postRedisplay ();
1416 | _ when key >= 32 && key < 127 ->
1417 let pattern = addchar qsearch (Char.chr key) in
1418 let active, first =
1419 match search active pattern 1 with
1420 | None ->
1421 state.text <- pattern ^ " [not found]";
1422 active, first
1423 | Some active ->
1424 state.text <- pattern;
1425 active, firstof active
1427 state.outline <- Some (allowdel, active, first, outlines, pattern);
1428 Glut.postRedisplay ()
1430 | 14 when not allowdel ->
1431 let optoutlines = narrow outlines qsearch in
1432 begin match optoutlines with
1433 | None -> state.text <- "can't narrow"
1434 | Some outlines ->
1435 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1436 match state.outlines with
1437 | Olist l -> ()
1438 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1439 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1440 end;
1441 Glut.postRedisplay ()
1443 | 21 when not allowdel ->
1444 let outline =
1445 match state.outlines with
1446 | Oarray a -> a
1447 | Olist l ->
1448 let a = Array.of_list (List.rev l) in
1449 state.outlines <- Oarray a;
1451 | Onarrow (a, b) ->
1452 state.outlines <- Oarray b;
1455 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1456 Glut.postRedisplay ()
1458 | 12 ->
1459 state.outline <-
1460 Some (allowdel, active, firstof active, outlines, qsearch);
1461 Glut.postRedisplay ()
1463 | 127 when allowdel ->
1464 let len = Array.length outlines - 1 in
1465 if len = 0
1466 then (
1467 state.outline <- None;
1468 state.bookmarks <- [];
1470 else (
1471 let bookmarks = Array.init len
1472 (fun i ->
1473 let i = if i >= active then i + 1 else i in
1474 outlines.(i)
1477 state.outline <-
1478 Some (allowdel,
1479 min active (len-1),
1480 min first (len-1),
1481 bookmarks, qsearch)
1484 Glut.postRedisplay ()
1486 | _ -> log "unknown key %d" key
1489 let keyboard ~key ~x ~y =
1490 if key = 7
1491 then
1492 wcmd "interrupt" []
1493 else
1494 match state.outline with
1495 | None -> viewkeyboard ~key ~x ~y
1496 | Some outline -> outlinekeyboard ~key ~x ~y outline
1499 let special ~key ~x ~y =
1500 match state.outline with
1501 | None ->
1502 begin match state.textentry with
1503 | None ->
1504 let y =
1505 match key with
1506 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1507 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1508 | Glut.KEY_DOWN -> clamp conf.scrollincr
1509 | Glut.KEY_PAGE_UP ->
1510 if Glut.getModifiers () land Glut.active_ctrl != 0
1511 then
1512 match state.layout with
1513 | [] -> state.y
1514 | l :: _ -> state.y - l.pagey
1515 else
1516 clamp (-state.h)
1517 | Glut.KEY_PAGE_DOWN ->
1518 if Glut.getModifiers () land Glut.active_ctrl != 0
1519 then
1520 match List.rev state.layout with
1521 | [] -> state.y
1522 | l :: _ -> getpagey l.pageno
1523 else
1524 clamp state.h
1525 | Glut.KEY_HOME -> addnav (); 0
1526 | Glut.KEY_END ->
1527 addnav ();
1528 state.maxy - (if conf.maxhfit then state.h else 0)
1530 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1531 state.x <- state.x - 10;
1532 state.y
1533 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1534 state.x <- state.x + 10;
1535 state.y
1537 | _ -> state.y
1539 if not conf.verbose then state.text <- "";
1540 gotoy y
1542 | Some (c, s, Some onhist, onkey, ondone) ->
1543 let s =
1544 match key with
1545 | Glut.KEY_UP -> onhist HCprev
1546 | Glut.KEY_DOWN -> onhist HCnext
1547 | Glut.KEY_HOME -> onhist HCfirst
1548 | Glut.KEY_END -> onhist HClast
1549 | _ -> state.text
1551 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1552 Glut.postRedisplay ()
1554 | _ -> ()
1557 | Some (allowdel, active, first, outlines, qsearch) ->
1558 let maxrows = maxoutlinerows () in
1559 let navigate incr =
1560 let active = active + incr in
1561 let active = max 0 (min active (Array.length outlines - 1)) in
1562 let first =
1563 if active > first
1564 then
1565 let rows = active - first in
1566 if rows > maxrows then active - maxrows else first
1567 else active
1569 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1570 Glut.postRedisplay ()
1572 match key with
1573 | Glut.KEY_UP -> navigate ~-1
1574 | Glut.KEY_DOWN -> navigate 1
1575 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1576 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1578 | Glut.KEY_HOME ->
1579 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1580 Glut.postRedisplay ()
1582 | Glut.KEY_END ->
1583 let active = Array.length outlines - 1 in
1584 let first = max 0 (active - maxrows) in
1585 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1586 Glut.postRedisplay ()
1588 | _ -> ()
1591 let drawplaceholder l =
1592 GlDraw.color (scalecolor 1.0);
1593 GlDraw.rect
1594 (0.0, float l.pagedispy)
1595 (float l.pagew, float (l.pagedispy + l.pagevh))
1597 let x = 0.0
1598 and y = float (l.pagedispy + 13) in
1599 let font = Glut.BITMAP_8_BY_13 in
1600 GlDraw.color (0.0, 0.0, 0.0);
1601 GlPix.raster_pos ~x ~y ();
1602 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1603 ("Loading " ^ string_of_int l.pageno);
1606 let now () = Unix.gettimeofday ();;
1608 let drawpage i l =
1609 begin match getopaque l.pageno with
1610 | Some opaque when validopaque opaque ->
1611 if state.textentry = None
1612 then GlDraw.color (scalecolor 1.0)
1613 else GlDraw.color (scalecolor 0.4);
1614 let a = now () in
1615 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1616 opaque;
1617 let b = now () in
1618 let d = b-.a in
1619 vlog "draw %d %f sec" l.pageno d;
1621 | _ ->
1622 drawplaceholder l;
1623 end;
1624 l.pagedispy + l.pagevh;
1627 let scrollindicator () =
1628 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1629 GlDraw.color (0.64 , 0.64, 0.64);
1630 GlDraw.rect
1631 (0., 0.)
1632 (float conf.scrollw, float state.h)
1634 GlDraw.color (0.0, 0.0, 0.0);
1635 let sh = (float (maxy + state.h) /. float state.h) in
1636 let sh = float state.h /. sh in
1637 let sh = max sh (float conf.scrollh) in
1639 let percent =
1640 if state.y = state.maxy
1641 then 1.0
1642 else float state.y /. float maxy
1644 let position = (float state.h -. sh) *. percent in
1646 let position =
1647 if position +. sh > float state.h
1648 then
1649 float state.h -. sh
1650 else
1651 position
1653 GlDraw.rect
1654 (0.0, position)
1655 (float conf.scrollw, position +. sh)
1659 let showsel margin =
1660 match state.mstate with
1661 | Mnone | Mpan _ ->
1664 | Msel ((x0, y0), (x1, y1)) ->
1665 let rec loop = function
1666 | l :: ls ->
1667 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1668 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1669 then
1670 match getopaque l.pageno with
1671 | Some opaque when validopaque opaque ->
1672 let oy = -l.pagey + l.pagedispy in
1673 seltext opaque
1674 (x0 - margin - state.x, y0,
1675 x1 - margin - state.x, y1) oy;
1677 | _ -> ()
1678 else loop ls
1679 | [] -> ()
1681 loop state.layout
1684 let showrects () =
1685 let panx = float state.x in
1686 Gl.enable `blend;
1687 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1688 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1689 List.iter
1690 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1691 List.iter (fun l ->
1692 if l.pageno = pageno
1693 then (
1694 let d = float (l.pagedispy - l.pagey) in
1695 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1696 GlDraw.begins `quads;
1698 GlDraw.vertex2 (x0+.panx, y0+.d);
1699 GlDraw.vertex2 (x1+.panx, y1+.d);
1700 GlDraw.vertex2 (x2+.panx, y2+.d);
1701 GlDraw.vertex2 (x3+.panx, y3+.d);
1703 GlDraw.ends ();
1705 ) state.layout
1706 ) state.rects
1708 Gl.disable `blend;
1711 let showoutline = function
1712 | None -> ()
1713 | Some (allowdel, active, first, outlines, qsearch) ->
1714 Gl.enable `blend;
1715 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1716 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1717 GlDraw.rect (0., 0.) (float state.w, float state.h);
1718 Gl.disable `blend;
1720 GlDraw.color (1., 1., 1.);
1721 let font = Glut.BITMAP_9_BY_15 in
1722 let draw_string x y s =
1723 GlPix.raster_pos ~x ~y ();
1724 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1726 let rec loop row =
1727 if row = Array.length outlines || (row - first) * 16 > state.h
1728 then ()
1729 else (
1730 let (s, l, _, _) = outlines.(row) in
1731 let y = (row - first) * 16 in
1732 let x = 5 + 15*l in
1733 if row = active
1734 then (
1735 Gl.enable `blend;
1736 GlDraw.polygon_mode `both `line;
1737 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1738 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1739 let sw = float (conf.scrollw - 1) *. float state.w /. float state.winw in
1740 GlDraw.rect (0., float (y + 1))
1741 ((float state.w -. sw), float (y + 18));
1742 GlDraw.polygon_mode `both `fill;
1743 Gl.disable `blend;
1744 GlDraw.color (1., 1., 1.);
1746 draw_string (float x) (float (y + 16)) s;
1747 loop (row+1)
1750 loop first
1753 let display () =
1754 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1755 GlDraw.viewport margin 0 state.w state.h;
1756 GlClear.color (scalecolor 0.5);
1757 GlClear.clear [`color];
1758 if state.x != 0
1759 then (
1760 let x = float state.x in
1761 GlMat.translate ~x ();
1763 if conf.zoom > 1.0
1764 then (
1765 Gl.enable `scissor_test;
1766 GlMisc.scissor 0 0 (state.winw - conf.scrollw) state.h;
1768 let _lasty = List.fold_left drawpage 0 (state.layout) in
1769 if conf.zoom > 1.0
1770 then
1771 Gl.disable `scissor_test
1773 if state.x != 0
1774 then (
1775 let x = -.float state.x in
1776 GlMat.translate ~x ();
1778 showrects ();
1779 GlDraw.viewport (state.winw - conf.scrollw) 0 state.winw state.h;
1780 scrollindicator ();
1781 showsel margin;
1782 GlDraw.viewport 0 0 state.winw state.h;
1783 showoutline state.outline;
1784 enttext ();
1785 Glut.swapBuffers ();
1788 let getunder x y =
1789 let margin = (state.winw - (state.w + conf.scrollw)) / 2 in
1790 let x = x - margin - state.x in
1791 let rec f = function
1792 | l :: rest ->
1793 begin match getopaque l.pageno with
1794 | Some opaque when validopaque opaque ->
1795 let y = y - l.pagedispy in
1796 if y > 0
1797 then
1798 let y = l.pagey + y in
1799 match whatsunder opaque x y with
1800 | Unone -> f rest
1801 | under -> under
1802 else
1803 f rest
1804 | _ ->
1805 f rest
1807 | [] -> Unone
1809 f state.layout
1812 let mouse ~button ~bstate ~x ~y =
1813 match button with
1814 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1815 let incr =
1816 if n = 3
1817 then
1818 -conf.scrollincr
1819 else
1820 conf.scrollincr
1822 let incr = incr * 2 in
1823 let y = clamp incr in
1824 gotoy y
1826 | Glut.LEFT_BUTTON when state.outline = None
1827 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1828 if bstate = Glut.DOWN
1829 then
1830 state.mstate <- Mpan (x, y)
1831 else
1832 state.mstate <- Mnone
1834 | Glut.LEFT_BUTTON when state.outline = None ->
1835 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1836 begin match dest with
1837 | Ulinkgoto (pageno, top) ->
1838 if pageno >= 0
1839 then
1840 gotopage1 pageno top
1842 | Ulinkuri s ->
1843 print_endline s
1845 | Unone when bstate = Glut.DOWN ->
1846 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1847 state.mstate <- Mpan (x, y);
1849 | Unone | Utext _ ->
1850 if bstate = Glut.DOWN
1851 then (
1852 if state.rotate mod 360 = 0
1853 then (
1854 state.mstate <- Msel ((x, y), (x, y));
1855 Glut.postRedisplay ()
1858 else (
1859 match state.mstate with
1860 | Mnone -> ()
1862 | Mpan _ ->
1863 Glut.setCursor Glut.CURSOR_INHERIT;
1864 state.mstate <- Mnone
1866 | Msel ((x0, y0), (x1, y1)) ->
1867 let f l =
1868 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1869 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1870 then
1871 match getopaque l.pageno with
1872 | Some opaque when validopaque opaque ->
1873 copysel opaque
1874 | _ -> ()
1876 List.iter f state.layout;
1877 copysel ""; (* ugly *)
1878 Glut.setCursor Glut.CURSOR_INHERIT;
1879 state.mstate <- Mnone;
1883 | _ ->
1886 let mouse ~button ~state ~x ~y = mouse button state x y;;
1888 let motion ~x ~y =
1889 if state.outline = None
1890 then
1891 match state.mstate with
1892 | Mnone -> ()
1894 | Mpan (x0, y0) ->
1895 let dx = x - x0
1896 and dy = y0 - y in
1897 state.mstate <- Mpan (x, y);
1898 if conf.zoom > 1.0 then state.x <- state.x + dx;
1899 let y = clamp dy in
1900 gotoy y
1902 | Msel (a, _) ->
1903 state.mstate <- Msel (a, (x, y));
1904 Glut.postRedisplay ()
1907 let pmotion ~x ~y =
1908 if state.outline = None
1909 then
1910 match state.mstate with
1911 | Mnone ->
1912 begin match getunder x y with
1913 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1914 | Ulinkuri uri ->
1915 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1916 Glut.setCursor Glut.CURSOR_INFO
1917 | Ulinkgoto (page, y) ->
1918 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1919 Glut.setCursor Glut.CURSOR_INFO
1920 | Utext s ->
1921 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1922 Glut.setCursor Glut.CURSOR_TEXT
1925 | Mpan _ | Msel _ ->
1929 let () =
1930 let statepath =
1931 let home =
1932 if Sys.os_type = "Win32"
1933 then
1934 try Sys.getenv "HOMEPATH" with Not_found -> ""
1935 else
1936 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1938 Filename.concat home "llpp"
1940 let pstate =
1942 let ic = open_in_bin statepath in
1943 let hash = input_value ic in
1944 close_in ic;
1945 hash
1946 with exn ->
1947 if false
1948 then
1949 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1951 Hashtbl.create 1
1953 let savestate () =
1955 let w, h =
1956 match state.fullscreen with
1957 | None -> state.winw, state.h
1958 | Some wh -> wh
1960 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1961 let oc = open_out_bin statepath in
1962 output_value oc pstate
1963 with exn ->
1964 if false
1965 then
1966 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1969 let setstate () =
1971 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1972 state.w <- statew;
1973 state.h <- stateh;
1974 state.bookmarks <- statebookmarks;
1975 with Not_found -> ()
1976 | exn ->
1977 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1980 Arg.parse
1981 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
1982 (fun s -> state.path <- s)
1983 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
1985 let name =
1986 if String.length state.path = 0
1987 then (prerr_endline "filename missing"; exit 1)
1988 else state.path
1991 setstate ();
1992 let _ = Glut.init Sys.argv in
1993 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1994 let () = Glut.initWindowSize state.w state.h in
1995 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1997 let csock, ssock =
1998 if Sys.os_type = "Unix"
1999 then
2000 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2001 else
2002 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2003 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2004 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2005 Unix.bind sock addr;
2006 Unix.listen sock 1;
2007 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2008 Unix.connect csock addr;
2009 let ssock, _ = Unix.accept sock in
2010 Unix.close sock;
2011 let opts sock =
2012 Unix.setsockopt sock Unix.TCP_NODELAY true;
2013 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2015 opts ssock;
2016 opts csock;
2017 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2018 ssock, csock
2021 let () = Glut.displayFunc display in
2022 let () = Glut.reshapeFunc reshape in
2023 let () = Glut.keyboardFunc keyboard in
2024 let () = Glut.specialFunc special in
2025 let () = Glut.idleFunc (Some idle) in
2026 let () = Glut.mouseFunc mouse in
2027 let () = Glut.motionFunc motion in
2028 let () = Glut.passiveMotionFunc pmotion in
2030 init ssock;
2031 state.csock <- csock;
2032 state.ssock <- ssock;
2033 state.text <- "Opening " ^ name;
2034 writecmd state.csock ("open " ^ state.path ^ "\000" ^ state.password ^ "\000");
2036 at_exit savestate;
2038 let rec handlelablglutbug () =
2040 Glut.mainLoop ();
2041 with Glut.BadEnum "key in special_of_int" ->
2042 showtext '!' " LablGlut bug: special key not recognized";
2043 handlelablglutbug ()
2045 handlelablglutbug ();