Yet another maxhfit related fixup
[llpp.git] / main.ml
blobd544aa741ebad7e2ee6d9a432d2c7113145fe947
1 open Format;;
3 let log fmt = Printf.kprintf prerr_endline fmt;;
4 let dolog fmt = Printf.kprintf prerr_endline fmt;;
6 external init : Unix.file_descr -> unit = "ml_init";;
7 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
8 external preload : string -> unit = "ml_preload";;
9 external gettext : string -> (int * int * int * int) -> int -> bool -> unit =
10 "ml_gettext";;
11 external checklink : string -> int -> int -> bool = "ml_checklink";;
12 external getlink : string -> int -> int -> (int * int) option = "ml_getlink";;
14 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
17 type te =
18 | TEstop
19 | TEdone of string
20 | TEcont of string
23 type 'a circbuf =
24 { store : 'a array
25 ; mutable rc : int
26 ; mutable wc : int
27 ; mutable len : int
31 let cbnew n v =
32 { store = Array.create n v
33 ; rc = 0
34 ; wc = 0
35 ; len = 0
39 let cblen b = Array.length b.store;;
41 let cbput b v =
42 let len = cblen b in
43 b.store.(b.wc) <- v;
44 b.wc <- (b.wc + 1) mod len;
45 b.len <- (b.len + 1) mod len;
48 let cbpeekw b = b.store.(b.wc);;
50 let cbget b =
51 let v = b.store.(b.rc) in
52 if b.len = 0
53 then
55 else (
56 let rc = if b.rc = 0 then b.len - 1 else b.rc - 1 in
57 b.rc <- rc;
62 let cbrfollowlen b =
63 b.rc <- b.len - 1;
66 type layout =
67 { pageno : int
68 ; pagedimno : int
69 ; pagew : int
70 ; pageh : int
71 ; pagedispy : int
72 ; pagey : int
73 ; pagevh : int
77 type conf =
78 { mutable scrollw : int
79 ; mutable scrollh : int
80 ; mutable rectsel : bool
81 ; mutable icase : bool
82 ; mutable preload : bool
83 ; mutable titletext : bool
84 ; mutable pagebias : int
85 ; mutable redispimm : bool
86 ; mutable verbose : bool
87 ; mutable scrollincr : int
88 ; mutable maxhfit : bool
92 type outline = string * int * int * int;;
93 type outlines = Oarray of outline array | Olist of outline list;;
95 type state =
96 { mutable csock : Unix.file_descr
97 ; mutable ssock : Unix.file_descr
98 ; mutable w : int
99 ; mutable h : int
100 ; mutable y : int
101 ; mutable prevy : int
102 ; mutable maxy : int
103 ; mutable layout : layout list
104 ; pagemap : ((int * int), string) Hashtbl.t
105 ; mutable pages : (int * int * int) list
106 ; mutable pagecount : int
107 ; pagecache : string circbuf
108 ; navhist : float circbuf
109 ; mutable inflight : int
110 ; mutable mstate : mstate
111 ; mutable searchpattern : string
112 ; mutable rects : (int * int * Gl.point2 * Gl.point2) list
113 ; mutable text : string
114 ; mutable fullscreen : (int * int) option
115 ; mutable textentry :
116 (char * string * (string -> int -> te) * (string -> unit)) option
117 ; mutable outlines : outlines
118 ; mutable outline : (int * int * outline array * string) option
122 let conf =
123 { scrollw = 5
124 ; scrollh = 12
125 ; icase = true
126 ; rectsel = true
127 ; preload = false
128 ; titletext = false
129 ; pagebias = 0
130 ; redispimm = false
131 ; verbose = false
132 ; scrollincr = 18
133 ; maxhfit = true
137 let state =
138 { csock = Unix.stdin
139 ; ssock = Unix.stdin
140 ; w = 0
141 ; h = 0
142 ; y = 0
143 ; prevy = 0
144 ; layout = []
145 ; maxy = max_int
146 ; pagemap = Hashtbl.create 10
147 ; pagecache = cbnew 10 ""
148 ; pages = []
149 ; pagecount = 0
150 ; inflight = 0
151 ; mstate = Mnone
152 ; navhist = cbnew 100 0.0
153 ; rects = []
154 ; text = ""
155 ; fullscreen = None
156 ; textentry = None
157 ; searchpattern = ""
158 ; outlines = Olist []
159 ; outline = None
163 let vlog fmt =
164 if conf.verbose
165 then
166 Printf.kprintf prerr_endline fmt
167 else
168 Printf.kprintf ignore fmt
171 let writecmd fd s =
172 let len = String.length s in
173 let n = 4 + len in
174 let b = Buffer.create n in
175 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
176 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
177 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
178 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
179 Buffer.add_string b s;
180 let s' = Buffer.contents b in
181 let n' = Unix.write fd s' 0 n in
182 if n' != n then failwith "write failed";
185 let readcmd fd =
186 let s = "xxxx" in
187 let n = Unix.read fd s 0 4 in
188 if n != 4 then failwith "incomplete read(len)";
189 let len = 0
190 lor (Char.code s.[0] lsl 24)
191 lor (Char.code s.[1] lsl 16)
192 lor (Char.code s.[2] lsl 8)
193 lor (Char.code s.[3] lsl 0)
195 let s = String.create len in
196 let n = Unix.read fd s 0 len in
197 if n != len then failwith "incomplete read(data)";
201 let yratio y =
202 if y = state.maxy then 1.0
203 else float y /. float state.maxy
206 let wcmd s l =
207 let b = Buffer.create 10 in
208 Buffer.add_string b s;
209 let rec combine = function
210 | [] -> Buffer.contents b
211 | x :: xs ->
212 Buffer.add_char b ' ';
213 let s =
214 match x with
215 | `b b -> if b then "1" else "0"
216 | `s s -> s
217 | `i i -> string_of_int i
218 | `f f -> string_of_float f
219 | `I f -> string_of_int (truncate f)
221 Buffer.add_string b s;
222 combine xs;
224 let s = combine l in
225 writecmd state.csock s;
228 let calcheight () =
229 let rec f pn ph fh l =
230 match l with
231 | (n, _, h) :: rest ->
232 let fh = fh + (n - pn) * ph in
233 f n h fh rest
235 | [] ->
236 let fh = fh + (ph * (state.pagecount - pn)) in
237 max 0 fh
239 let fh = f 0 0 0 state.pages in
243 let getpagey pageno =
244 let rec f pn ph y l =
245 match l with
246 | (n, _, h) :: rest ->
247 if n >= pageno
248 then
249 y + (pageno - pn) * ph
250 else
251 let y = y + (n - pn) * ph in
252 f n h y rest
254 | [] ->
255 y + (pageno - pn) * ph
257 f 0 0 0 state.pages;
260 let layout y sh =
261 let rec f pageno pdimno prev vy py dy l cacheleft accu =
262 if pageno = state.pagecount || cacheleft = 0
263 then accu
264 else
265 let ((_, w, h) as curr), rest, pdimno =
266 match l with
267 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
268 curr, rest, pdimno + 1
269 | _ ->
270 prev, l, pdimno
272 let pageno' = pageno + 1 in
273 if py + h > vy
274 then
275 let py' = vy - py in
276 let vh = h - py' in
277 if dy + vh > sh
278 then
279 let vh = sh - dy in
280 if vh <= 0
281 then
282 accu
283 else
284 let e =
285 { pageno = pageno
286 ; pagedimno = pdimno
287 ; pagew = w
288 ; pageh = h
289 ; pagedispy = dy
290 ; pagey = py'
291 ; pagevh = vh
294 e :: accu
295 else
296 let e =
297 { pageno = pageno
298 ; pagedimno = pdimno
299 ; pagew = w
300 ; pageh = h
301 ; pagedispy = dy
302 ; pagey = py'
303 ; pagevh = vh
306 let accu = e :: accu in
307 f pageno' pdimno curr
308 (vy + vh) (py + h) (dy + vh + 2) rest
309 (pred cacheleft) accu
310 else
311 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
313 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
314 state.maxy <- calcheight ();
315 List.rev accu
318 let clamp incr =
319 let y = state.y + incr in
320 let y = max 0 y in
321 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
325 let gotoy y =
326 let y = max 0 y in
327 let y = min state.maxy y in
328 let pages = layout y state.h in
329 state.y <- y;
330 state.layout <- pages;
331 if conf.redispimm
332 then
333 Glut.postRedisplay ()
337 let addnav () =
338 cbput state.navhist (yratio state.y);
339 cbrfollowlen state.navhist;
342 let getnav () =
343 let y = cbget state.navhist in
344 truncate (y *. float state.maxy)
347 let gotopage n top =
348 let y = getpagey n in
349 addnav ();
350 state.y <- y + top;
351 gotoy state.y;
354 let reshape ~w ~h =
355 state.w <- w;
356 state.h <- h;
357 GlDraw.viewport 0 0 w h;
358 GlMat.mode `modelview;
359 GlMat.load_identity ();
360 GlMat.mode `projection;
361 GlMat.load_identity ();
362 GlMat.rotate ~x:1.0 ~angle:180.0 ();
363 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
364 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
365 GlClear.color (1., 1., 1.);
366 GlClear.clear [`color];
367 state.layout <- [];
368 state.pages <- [];
369 state.rects <- [];
370 state.text <- "";
371 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
374 let showtext c s =
375 if not conf.titletext
376 then (
377 GlDraw.color (0.0, 0.0, 0.0);
378 GlDraw.rect
379 (0.0, float (state.h - 18))
380 (float (state.w - conf.scrollw - 1), float state.h)
382 let font = Glut.BITMAP_8_BY_13 in
383 GlDraw.color (1.0, 1.0, 1.0);
384 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
385 Glut.bitmapCharacter ~font ~c:(Char.code c);
386 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
388 else
389 let len = String.length s in
390 let dst = String.create (len + 1) in
391 dst.[0] <- c;
392 StringLabels.blit
393 ~src:s
394 ~dst
395 ~src_pos:0
396 ~dst_pos:1
397 ~len
399 Glut.setWindowTitle ~title:dst
402 let enttext () =
403 let len = String.length state.text in
404 match state.textentry with
405 | None ->
406 if len > 0 then showtext ' ' state.text
408 | Some (c, text, _, _) ->
409 let s =
410 if len > 0
411 then
412 text ^ " [" ^ state.text ^ "]"
413 else
414 text
416 showtext c s;
419 let act cmd =
420 match cmd.[0] with
421 | 'c' ->
422 state.pages <- []
424 | 'd' ->
425 Glut.postRedisplay ()
427 | 'C' ->
428 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
429 state.pagecount <- n;
430 let rely = yratio state.y in
431 let maxy = calcheight () in
432 state.y <- truncate (float maxy *. rely);
433 let pages = layout state.y state.h in
434 state.layout <- pages;
435 Glut.postRedisplay ();
437 | 'T' ->
438 let s = Scanf.sscanf cmd "T %n"
439 (fun n -> String.sub cmd n (String.length cmd - n))
441 state.text <- s;
442 showtext ' ' s;
443 Glut.swapBuffers ();
444 (* Glut.postRedisplay () *)
446 | 'F' ->
447 let pageno, c, x0, y0, x1, y1 =
448 Scanf.sscanf cmd "F %d %d %f %f %f %f"
449 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
451 let y = (getpagey pageno) + truncate y0 in
452 addnav ();
453 gotoy y;
454 state.rects <- [pageno, c, (x0, y0), (x1, y1)]
456 | 'R' ->
457 let pageno, c, x0, y0, x1, y1 =
458 Scanf.sscanf cmd "R %d %d %f %f %f %f"
459 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
461 state.rects <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects
463 | 'r' ->
464 let n, w, h, p =
465 Scanf.sscanf cmd "r %d %d %d %s"
466 (fun n w h p -> (n, w, h, p))
468 Hashtbl.replace state.pagemap (n, w) p;
469 let evicted = cbpeekw state.pagecache in
470 if String.length evicted > 0
471 then begin
472 wcmd "free" [`s evicted];
473 let l = Hashtbl.fold (fun k p a ->
474 if evicted = p then k :: a else a) state.pagemap []
476 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
477 end;
478 cbput state.pagecache p;
479 state.inflight <- pred state.inflight;
480 Glut.postRedisplay ()
482 | 'l' ->
483 let (n, w, h) as pagelayout =
484 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
486 state.pages <- pagelayout :: state.pages
488 | 'o' ->
489 let (l, n, t, pos) =
490 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
492 let s = String.sub cmd pos (String.length cmd - pos) in
493 let outline = (s, l, n, t) in
494 let outlines =
495 match state.outlines with
496 | Olist outlines -> Olist (outline :: outlines)
497 | Oarray _ -> Olist [outline]
499 state.outlines <- outlines
501 | _ ->
502 log "unknown cmd `%S'" cmd
505 let getopaque pageno =
506 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
507 with Not_found -> None
510 let cache pageno opaque =
511 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
514 let validopaque opaque = String.length opaque > 0;;
516 let preload l =
517 match getopaque l.pageno with
518 | Some opaque when validopaque opaque ->
519 preload opaque
521 | None when state.inflight < 2+0*(cblen state.pagecache) ->
522 state.inflight <- succ state.inflight;
523 cache l.pageno "";
524 wcmd "render" [`i (l.pageno + 1)
525 ;`i l.pagedimno
526 ;`i l.pagew
527 ;`i l.pageh];
529 | _ -> ()
532 let idle () =
533 if not conf.redispimm && state.y != state.prevy
534 then (
535 state.prevy <- state.y;
536 Glut.postRedisplay ();
538 else
539 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
541 begin match r with
542 | [] ->
543 if conf.preload then begin
544 let h = state.h in
545 let y = if state.y < state.h then 0 else state.y - state.h in
546 let pages = layout y (h*3) in
547 List.iter preload pages;
548 end;
550 | _ ->
551 let cmd = readcmd state.csock in
552 act cmd;
553 end;
556 let search pattern forward =
557 if String.length pattern > 0
558 then
559 let pn, py =
560 match state.layout with
561 | [] -> 0, 0
562 | l :: _ ->
563 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
565 wcmd "search" [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)
566 ; `s (pattern ^ "\000")]
569 let intentry text key =
570 let c = Char.unsafe_chr key in
571 match c with
572 | '0' .. '9' ->
573 let s = "x" in s.[0] <- c;
574 let text = text ^ s in
575 TEcont text
577 | _ ->
578 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
579 TEcont text
582 let addchar s c =
583 let b = Buffer.create (String.length s + 1) in
584 Buffer.add_string b s;
585 Buffer.add_char b c;
586 Buffer.contents b;
589 let textentry text key =
590 let c = Char.unsafe_chr key in
591 match c with
592 | _ when key >= 32 && key <= 127 ->
593 let text = addchar text c in
594 TEcont text
596 | _ ->
597 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
598 TEcont text
601 let optentry text key =
602 let btos b = if b then "on" else "off" in
603 let c = Char.unsafe_chr key in
604 match c with
605 | 'r' ->
606 conf.rectsel <- not conf.rectsel;
607 TEdone ("rectsel " ^ (btos conf.rectsel))
609 | 'i' ->
610 conf.icase <- not conf.icase;
611 TEdone ("case insensitive search " ^ (btos conf.icase))
613 | 'p' ->
614 conf.preload <- not conf.preload;
615 TEdone ("preload " ^ (btos conf.preload))
617 | 't' ->
618 conf.titletext <- not conf.titletext;
619 TEdone ("titletext " ^ (btos conf.titletext))
621 | 'd' ->
622 conf.redispimm <- not conf.redispimm;
623 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
625 | 'v' ->
626 conf.verbose <- not conf.verbose;
627 TEdone ("verbose " ^ (btos conf.verbose))
629 | 'h' ->
630 conf.maxhfit <- not conf.maxhfit;
631 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
632 TEdone ("maxhfit " ^ (btos conf.maxhfit))
634 | _ ->
635 state.text <- Printf.sprintf "bad option %d `%c'" key c;
636 TEstop
639 let maxoutlinerows () = (state.h - 31) / 16;;
641 let enteroutlinemode () =
642 let pageno =
643 match state.layout with
644 | [] -> -1
645 | {pageno=pageno} :: rest -> pageno
647 let outlines =
648 match state.outlines with
649 | Oarray a -> a
650 | Olist l ->
651 let a = Array.of_list (List.rev l) in
652 state.outlines <- Oarray a;
655 if Array.length outlines = 0
656 then (
657 showtext ' ' "Document has no outline";
658 Glut.swapBuffers ()
660 else
661 let active =
662 let rec loop n =
663 if n = Array.length outlines
664 then 0
665 else
666 let (_, _, outlinepageno, _) = outlines.(n) in
667 if outlinepageno >= pageno then n else loop (n+1)
669 loop 0
671 state.outline <-
672 Some (active, max 0 (active - maxoutlinerows ()), outlines, "");
673 Glut.postRedisplay ();
676 let viewkeyboard ~key ~x ~y =
677 let enttext te =
678 state.textentry <- te;
679 state.text <- "";
680 enttext ();
681 Glut.swapBuffers ()
683 match state.textentry with
684 | None ->
685 let c = Char.chr key in
686 begin match c with
687 | '\027' | 'q' ->
688 exit 0
690 | '\008' ->
691 let y = getnav () in
692 gotoy y
694 | 'o' ->
695 enteroutlinemode ()
697 | 'u' ->
698 state.rects <- [];
699 state.text <- "";
700 Glut.postRedisplay ()
702 | '/' | '?' ->
703 let ondone isforw s =
704 state.searchpattern <- s;
705 search s isforw
707 enttext (Some (c, "", textentry, ondone (c ='/')))
709 | '+' ->
710 let ondone s =
711 let n =
712 try int_of_string s with exc ->
713 state.text <- Printf.sprintf "bad integer `%s': %s"
714 s (Printexc.to_string exc);
715 max_int
717 if n != max_int
718 then (
719 conf.pagebias <- n;
720 state.text <- "page bias is now " ^ string_of_int n;
723 enttext (Some ('+', "", intentry, ondone))
725 | '-' ->
726 let ondone msg =
727 state.text <- msg;
729 enttext (Some ('-', "", optentry, ondone))
731 | '0' .. '9' ->
732 let ondone s =
733 let n =
734 try int_of_string s with exc ->
735 state.text <- Printf.sprintf "bad integer `%s': %s"
736 s (Printexc.to_string exc);
739 if n >= 0
740 then (
741 addnav ();
742 state.y <- y;
743 gotoy (getpagey (n + conf.pagebias - 1))
746 let pageentry text key =
747 match Char.unsafe_chr key with
748 | 'g' -> TEdone text
749 | _ -> intentry text key
751 let text = "x" in text.[0] <- c;
752 enttext (Some (':', text, pageentry, ondone))
754 | 'b' ->
755 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
756 reshape state.w state.h;
758 | 'f' ->
759 begin match state.fullscreen with
760 | None ->
761 state.fullscreen <- Some (state.w, state.h);
762 Glut.fullScreen ()
763 | Some (w, h) ->
764 state.fullscreen <- None;
765 Glut.reshapeWindow ~w ~h
768 | 'n' ->
769 search state.searchpattern true
771 | 'p' ->
772 search state.searchpattern false
774 | 't' ->
775 begin match state.layout with
776 | [] -> ()
777 | l :: _ ->
778 gotoy (state.y - l.pagey);
781 | ' ' | 'N' ->
782 begin match List.rev state.layout with
783 | [] -> ()
784 | l :: _ ->
785 gotoy (clamp (l.pageh - l.pagey))
788 | '\127' | 'P' ->
789 begin match state.layout with
790 | [] -> ()
791 | l :: _ ->
792 gotoy (clamp (-l.pageh));
795 | '=' ->
796 let f (fn, ln) l =
797 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
799 let fn, ln = List.fold_left f (-1, -1) state.layout in
800 let s =
801 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
802 let percent = (100. *. (float state.y /. float maxy)) in
803 if fn = ln
804 then
805 Printf.sprintf "Page %d of %d %.2f%%"
806 (fn+1) state.pagecount percent
807 else
808 Printf.sprintf
809 "Pages %d-%d of %d %.2f%%"
810 (fn+1) (ln+1) state.pagecount percent
812 showtext ' ' s;
813 Glut.swapBuffers ()
815 | 'w' ->
816 begin match state.layout with
817 | [] -> ()
818 | l :: _ ->
819 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
820 Glut.postRedisplay ();
823 | _ ->
824 vlog "huh? %d %c" key (Char.chr key);
827 | Some (c, text, onkey, ondone) when key = 8 ->
828 let len = String.length text in
829 if len = 0 || len = 1
830 then (
831 state.textentry <- None;
832 Glut.postRedisplay ();
834 else (
835 let s = String.sub text 0 (len - 1) in
836 enttext (Some (c, s, onkey, ondone))
839 | Some (c, text, onkey, ondone) ->
840 begin match Char.unsafe_chr key with
841 | '\r' | '\n' ->
842 ondone text;
843 state.textentry <- None;
844 Glut.postRedisplay ()
846 | '\027' ->
847 state.textentry <- None;
848 Glut.postRedisplay ()
850 | _ ->
851 begin match onkey text key with
852 | TEdone text ->
853 state.textentry <- None;
854 ondone text;
855 Glut.postRedisplay ()
857 | TEcont text ->
858 enttext (Some (c, text, onkey, ondone));
860 | TEstop ->
861 state.textentry <- None;
862 Glut.postRedisplay ()
863 end;
864 end;
867 let outlinekeyboard ~key ~x ~y (active, first, outlines, qsearch) =
868 let search active pattern incr =
869 let re = Str.regexp_case_fold pattern in
870 let rec loop n =
871 if n = Array.length outlines || n = -1 then None else
872 let (s, _, _, _) = outlines.(n) in
874 (try ignore (Str.search_forward re s 0); true
875 with Not_found -> false)
876 then (
877 let maxrows = maxoutlinerows () in
878 if first > n
879 then Some (n, max 0 (n - maxrows))
880 else Some (n, max first (n - maxrows))
882 else loop (n + incr)
884 loop active
886 match key with
887 | 27 ->
888 if String.length qsearch = 0
889 then (
890 state.text <- "";
891 state.outline <- None;
892 Glut.postRedisplay ();
894 else (
895 state.text <- "";
896 state.outline <- Some (active, first, outlines, "");
897 Glut.postRedisplay ();
900 | 18 | 19 ->
901 let incr = if key = 18 then -1 else 1 in
902 let active, first =
903 match search (active + incr) qsearch incr with
904 | None -> active, first
905 | Some af -> af
907 state.outline <- Some (active, first, outlines, qsearch);
908 Glut.postRedisplay ();
910 | 8 ->
911 let len = String.length qsearch in
912 if len = 0
913 then ()
914 else (
915 if len = 1
916 then (
917 state.text <- "";
918 state.outline <- Some (active, first, outlines, "");
920 else
921 let qsearch = String.sub qsearch 0 (len - 1) in
922 state.text <- qsearch;
923 state.outline <- Some (active, first, outlines, qsearch);
925 Glut.postRedisplay ()
927 | 13 ->
928 let (_, _, n, t) = outlines.(active) in
929 gotopage n t;
930 state.text <- "";
931 state.outline <- None;
932 Glut.postRedisplay ();
934 | _ when key >= 32 && key <= 127 ->
935 let pattern = addchar qsearch (Char.chr key) in
936 let pattern, active, first =
937 match search active pattern 1 with
938 | None -> qsearch, active, first
939 | Some (active, first) -> (pattern, active, first)
941 state.text <- pattern;
942 state.outline <- Some (active, first, outlines, pattern);
943 Glut.postRedisplay ()
945 | _ -> log "unknown key %d" key
948 let keyboard ~key ~x ~y =
949 match state.outline with
950 | None -> viewkeyboard ~key ~x ~y
951 | Some outline -> outlinekeyboard ~key ~x ~y outline
954 let special ~key ~x ~y =
955 match state.outline with
956 | None ->
957 let y =
958 match key with
959 | Glut.KEY_F3 -> search state.searchpattern true; state.y
960 | Glut.KEY_UP -> clamp (-conf.scrollincr)
961 | Glut.KEY_DOWN -> clamp conf.scrollincr
962 | Glut.KEY_PAGE_UP -> clamp (-state.h)
963 | Glut.KEY_PAGE_DOWN -> clamp state.h
964 | Glut.KEY_HOME -> addnav (); 0
965 | Glut.KEY_END ->
966 addnav ();
967 state.maxy - (if conf.maxhfit then state.h else 0)
968 | _ -> state.y
970 state.text <- "";
971 gotoy y
973 | Some (active, first, outlines, qsearch) ->
974 let maxrows = maxoutlinerows () in
975 let navigate incr =
976 let active = active + incr in
977 let active = max 0 (min active (Array.length outlines - 1)) in
978 let first =
979 if active > first
980 then
981 let rows = active - first in
982 if rows > maxrows then first + incr else first
983 else active
985 state.outline <- Some (active, first, outlines, qsearch);
986 Glut.postRedisplay ()
988 match key with
989 | Glut.KEY_UP -> navigate ~-1
990 | Glut.KEY_DOWN -> navigate 1
991 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
992 | Glut.KEY_PAGE_DOWN -> navigate maxrows
994 | Glut.KEY_HOME ->
995 state.outline <- Some (0, 0, outlines, qsearch);
996 Glut.postRedisplay ()
998 | Glut.KEY_END ->
999 let active = Array.length outlines - 1 in
1000 let first = max 0 (active - maxrows) in
1001 state.outline <- Some (active, first, outlines, qsearch);
1002 Glut.postRedisplay ()
1004 | _ -> ()
1007 let drawplaceholder l =
1008 if true
1009 then (
1010 GlDraw.color (0.2, 0.2, 0.2);
1011 GlDraw.color (1.0, 1.0, 1.0);
1012 GlDraw.rect
1013 (0.0, float l.pagedispy)
1014 (float l.pagew, float (l.pagedispy + l.pagevh))
1016 let x = 0.0
1017 and y = float (l.pagedispy + l.pagevh - 5) in
1018 let font = Glut.BITMAP_8_BY_13 in
1019 GlDraw.color (0.0, 0.0, 0.0);
1020 GlPix.raster_pos ~x ~y ();
1021 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1022 ("Loading " ^ string_of_int l.pageno);
1024 else (
1025 GlDraw.begins `quads;
1026 GlDraw.vertex2 (0.0, float l.pagedispy);
1027 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1028 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1029 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1030 GlDraw.ends ();
1034 let now () = Unix.gettimeofday ();;
1036 let drawpage i l =
1037 begin match getopaque l.pageno with
1038 | Some opaque when validopaque opaque ->
1039 GlDraw.color (1.0, 1.0, 1.0);
1040 let a = now () in
1041 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1042 let b = now () in
1043 let d = b-.a in
1044 vlog "draw %f sec" d;
1046 | Some _ ->
1047 drawplaceholder l
1049 | None ->
1050 if state.inflight < cblen state.pagecache
1051 then (
1052 List.iter preload state.layout;
1054 else (
1055 drawplaceholder l;
1056 vlog "inflight %d" state.inflight;
1058 end;
1059 GlDraw.color (0.5, 0.5, 0.5);
1060 GlDraw.rect
1061 (0., float i)
1062 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1064 l.pagedispy + l.pagevh;
1067 let scrollindicator () =
1068 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1069 GlDraw.color (0.64 , 0.64, 0.64);
1070 GlDraw.rect
1071 (float (state.w - conf.scrollw), 0.)
1072 (float state.w, float state.h)
1074 GlDraw.color (0.0, 0.0, 0.0);
1075 let sh = (float (maxy + state.h) /. float state.h) in
1076 let sh = float state.h /. sh in
1077 let sh = max sh (float conf.scrollh) in
1079 let percent =
1080 if state.y = state.maxy
1081 then 1.0
1082 else float state.y /. float maxy
1084 let position = (float state.h -. sh) *. percent in
1086 let position =
1087 if position +. sh > float state.h
1088 then
1089 float state.h -. sh
1090 else
1091 position
1093 GlDraw.rect
1094 (float (state.w - conf.scrollw), position)
1095 (float state.w, position +. sh)
1099 let showsel () =
1100 match state.mstate with
1101 | Mnone ->
1104 | Msel ((x0, y0), (x1, y1)) ->
1105 let y0' = min y0 y1
1106 and y1 = max y0 y1 in
1107 let y0 = y0' in
1108 let f l =
1109 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1110 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1111 then
1112 match getopaque l.pageno with
1113 | Some opaque when validopaque opaque ->
1114 let oy = -l.pagey + l.pagedispy in
1115 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1116 | _ -> ()
1118 List.iter f state.layout
1121 let showrects () =
1122 Gl.enable `blend;
1123 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1124 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1125 List.iter
1126 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1127 List.iter (fun l ->
1128 if l.pageno = pageno
1129 then (
1130 let d = float (l.pagedispy - l.pagey) in
1131 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1132 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1134 ) state.layout
1135 ) state.rects
1137 Gl.disable `blend;
1140 let showoutline = function
1141 | None -> ()
1142 | Some (active, first, outlines, qsearch) ->
1143 Gl.enable `blend;
1144 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1145 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1146 GlDraw.rect (0., 0.) (float state.w, float state.h);
1147 Gl.disable `blend;
1149 GlDraw.color (1., 1., 1.);
1150 let font = Glut.BITMAP_9_BY_15 in
1151 let draw_string x y s =
1152 GlPix.raster_pos ~x ~y ();
1153 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1155 let rec loop row =
1156 if row = Array.length outlines || (row - first) * 16 > state.h
1157 then ()
1158 else (
1159 let (s, l, _, _) = outlines.(row) in
1160 let y = (row - first) * 16 in
1161 let x = 5 + 5*l in
1162 if row = active
1163 then (
1164 Gl.enable `blend;
1165 GlDraw.polygon_mode `both `line;
1166 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1167 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1168 GlDraw.rect (0., float (y + 1))
1169 (float (state.w - conf.scrollw - 1), float (y + 18));
1170 GlDraw.polygon_mode `both `fill;
1171 Gl.disable `blend;
1172 GlDraw.color (1., 1., 1.);
1174 draw_string (float x) (float (y + 16)) s;
1175 loop (row+1)
1178 loop first
1181 let display () =
1182 let lasty = List.fold_left drawpage 0 (state.layout) in
1183 GlDraw.color (0.5, 0.5, 0.5);
1184 GlDraw.rect
1185 (0., float lasty)
1186 (float (state.w - conf.scrollw), float state.h)
1188 showrects ();
1189 scrollindicator ();
1190 showsel ();
1191 showoutline state.outline;
1192 enttext ();
1193 Glut.swapBuffers ();
1196 let getlink x y =
1197 let rec f = function
1198 | l :: rest ->
1199 begin match getopaque l.pageno with
1200 | Some opaque when validopaque opaque ->
1201 let y = y - l.pagedispy in
1202 if y > 0
1203 then
1204 let y = l.pagey + y in
1205 match getlink opaque x y with
1206 | None -> f rest
1207 | some -> some
1208 else
1209 f rest
1210 | _ ->
1211 f rest
1213 | [] -> None
1215 f state.layout
1218 let checklink x y =
1219 let rec f = function
1220 | l :: rest ->
1221 begin match getopaque l.pageno with
1222 | Some opaque when validopaque opaque ->
1223 let y = y - l.pagedispy in
1224 if y > 0
1225 then
1226 let y = l.pagey + y in
1227 if checklink opaque x y then true else f rest
1228 else
1229 f rest
1230 | _ ->
1231 f rest
1233 | [] -> false
1235 f state.layout
1238 let mouse ~button ~bstate ~x ~y =
1239 match button with
1240 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1241 let incr =
1242 if n = 3
1243 then
1244 -conf.scrollincr
1245 else
1246 conf.scrollincr
1248 let incr = incr * 2 in
1249 let y = clamp incr in
1250 gotoy y
1252 | Glut.LEFT_BUTTON when state.outline = None ->
1253 let dest = if bstate = Glut.DOWN then getlink x y else None in
1254 begin match dest with
1255 | Some (pageno, top) ->
1256 gotopage pageno top
1258 | None ->
1259 if bstate = Glut.DOWN
1260 then (
1261 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1262 state.mstate <- Msel ((x, y), (x, y));
1263 Glut.postRedisplay ()
1265 else (
1266 Glut.setCursor Glut.CURSOR_RIGHT_ARROW;
1267 state.mstate <- Mnone;
1271 | _ ->
1274 let mouse ~button ~state ~x ~y = mouse button state x y;;
1276 let motion ~x ~y =
1277 if state.outline = None
1278 then
1279 match state.mstate with
1280 | Mnone -> ()
1281 | Msel (a, _) ->
1282 state.mstate <- Msel (a, (x, y));
1283 Glut.postRedisplay ()
1286 let pmotion ~x ~y =
1287 if state.outline = None
1288 then
1289 match state.mstate with
1290 | Mnone when (checklink x y) ->
1291 Glut.setCursor Glut.CURSOR_INFO
1293 | Mnone ->
1294 Glut.setCursor Glut.CURSOR_RIGHT_ARROW
1296 | Msel (a, _) ->
1300 let () =
1301 let name = ref "" in
1302 Arg.parse [] (fun s -> name := s) "options:";
1303 let name =
1304 if String.length !name = 0
1305 then (prerr_endline "filename missing"; exit 1)
1306 else !name
1309 let w = 900 in
1310 let h = 900 in
1311 let _ = Glut.init Sys.argv in
1312 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1313 let () = Glut.initWindowSize w h in
1314 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1316 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1318 init ssock;
1319 state.w <- w;
1320 state.h <- h;
1321 state.csock <- csock;
1322 state.ssock <- ssock;
1323 writecmd csock ("open " ^ name ^ "\000");
1325 let () = Glut.displayFunc display in
1326 let () = Glut.reshapeFunc reshape in
1327 let () = Glut.keyboardFunc keyboard in
1328 let () = Glut.specialFunc special in
1329 let () = Glut.idleFunc (Some idle) in
1330 let () = Glut.mouseFunc mouse in
1331 let () = Glut.motionFunc motion in
1332 let () = Glut.passiveMotionFunc pmotion in
1333 let rec handlelablglutbug () =
1335 Glut.mainLoop ();
1336 with Glut.BadEnum "key in special_of_int" ->
1337 showtext '!' " LablGlut bug: special key not recognized";
1338 Glut.swapBuffers ();
1339 handlelablglutbug ()
1341 handlelablglutbug ()