Cosmetics
[llpp.git] / main.ml
blob7f0392706d71d5078dfe7d2ebdf5429f5ada4a11
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 safe : int
111 ; mutable mstate : mstate
112 ; mutable searchpattern : string
113 ; mutable rects : (int * int * Gl.point2 * Gl.point2) list
114 ; mutable text : string
115 ; mutable fullscreen : (int * int) option
116 ; mutable textentry :
117 (char * string * (string -> int -> te) * (string -> unit)) option
118 ; mutable outlines : outlines
119 ; mutable outline : (int * int * outline array * string) option
123 let conf =
124 { scrollw = 5
125 ; scrollh = 12
126 ; icase = true
127 ; rectsel = true
128 ; preload = false
129 ; titletext = false
130 ; pagebias = 0
131 ; redispimm = false
132 ; verbose = false
133 ; scrollincr = 18
134 ; maxhfit = true
138 let state =
139 { csock = Unix.stdin
140 ; ssock = Unix.stdin
141 ; w = 0
142 ; h = 0
143 ; y = 0
144 ; prevy = 0
145 ; layout = []
146 ; maxy = max_int
147 ; pagemap = Hashtbl.create 10
148 ; pagecache = cbnew 10 ""
149 ; pages = []
150 ; pagecount = 0
151 ; inflight = 0
152 ; safe = 0
153 ; mstate = Mnone
154 ; navhist = cbnew 100 0.0
155 ; rects = []
156 ; text = ""
157 ; fullscreen = None
158 ; textentry = None
159 ; searchpattern = ""
160 ; outlines = Olist []
161 ; outline = None
165 let vlog fmt =
166 if conf.verbose
167 then
168 Printf.kprintf prerr_endline fmt
169 else
170 Printf.kprintf ignore fmt
173 let writecmd fd s =
174 let len = String.length s in
175 let n = 4 + len in
176 let b = Buffer.create n in
177 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
178 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
179 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
180 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
181 Buffer.add_string b s;
182 let s' = Buffer.contents b in
183 let n' = Unix.write fd s' 0 n in
184 if n' != n then failwith "write failed";
187 let readcmd fd =
188 let s = "xxxx" in
189 let n = Unix.read fd s 0 4 in
190 if n != 4 then failwith "incomplete read(len)";
191 let len = 0
192 lor (Char.code s.[0] lsl 24)
193 lor (Char.code s.[1] lsl 16)
194 lor (Char.code s.[2] lsl 8)
195 lor (Char.code s.[3] lsl 0)
197 let s = String.create len in
198 let n = Unix.read fd s 0 len in
199 if n != len then failwith "incomplete read(data)";
203 let yratio y =
204 if y = state.maxy then 1.0
205 else float y /. float state.maxy
208 let wcmd s l =
209 let b = Buffer.create 10 in
210 Buffer.add_string b s;
211 let rec combine = function
212 | [] -> Buffer.contents b
213 | x :: xs ->
214 Buffer.add_char b ' ';
215 let s =
216 match x with
217 | `b b -> if b then "1" else "0"
218 | `s s -> s
219 | `i i -> string_of_int i
220 | `f f -> string_of_float f
221 | `I f -> string_of_int (truncate f)
223 Buffer.add_string b s;
224 combine xs;
226 let s = combine l in
227 writecmd state.csock s;
230 let calcheight () =
231 let rec f pn ph fh l =
232 match l with
233 | (n, _, h) :: rest ->
234 let fh = fh + (n - pn) * ph in
235 f n h fh rest
237 | [] ->
238 let fh = fh + (ph * (state.pagecount - pn)) in
239 max 0 (fh - (if conf.maxhfit then state.h else 0))
241 let fh = f 0 0 0 state.pages in
245 let getpagey pageno =
246 let rec f pn ph y l =
247 match l with
248 | (n, _, h) :: rest ->
249 if n >= pageno
250 then
251 y + (pageno - pn) * ph
252 else
253 let y = y + (n - pn) * ph in
254 f n h y rest
256 | [] ->
257 y + (pageno - pn) * ph
259 f 0 0 0 state.pages;
262 let layout y sh =
263 let rec f pageno pdimno prev vy py dy l cacheleft accu =
264 if pageno = state.pagecount || cacheleft = 0
265 then accu
266 else
267 let ((_, w, h) as curr), rest, pdimno =
268 match l with
269 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
270 curr, rest, pdimno + 1
271 | _ ->
272 prev, l, pdimno
274 let pageno' = pageno + 1 in
275 if py + h > vy
276 then
277 let py' = vy - py in
278 let vh = h - py' in
279 if dy + vh > sh
280 then
281 let vh = sh - dy in
282 if vh <= 0
283 then
284 accu
285 else
286 let e =
287 { pageno = pageno
288 ; pagedimno = pdimno
289 ; pagew = w
290 ; pageh = h
291 ; pagedispy = dy
292 ; pagey = py'
293 ; pagevh = vh
296 e :: accu
297 else
298 let e =
299 { pageno = pageno
300 ; pagedimno = pdimno
301 ; pagew = w
302 ; pageh = h
303 ; pagedispy = dy
304 ; pagey = py'
305 ; pagevh = vh
308 let accu = e :: accu in
309 f pageno' pdimno curr
310 (vy + vh) (py + h) (dy + vh + 2) rest
311 (pred cacheleft) accu
312 else
313 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
315 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
316 state.maxy <- calcheight ();
317 List.rev accu
320 let clamp incr =
321 let y = state.y + incr in
322 let y = max 0 y in
323 let y = min y state.maxy in
327 let gotoy y =
328 let y = max 0 y in
329 let y = min state.maxy y in
330 let pages = layout y state.h in
331 state.y <- y;
332 state.layout <- pages;
333 if conf.redispimm
334 then
335 Glut.postRedisplay ()
339 let addnav () =
340 cbput state.navhist (yratio state.y);
341 cbrfollowlen state.navhist;
344 let getnav () =
345 let y = cbget state.navhist in
346 truncate (y *. float state.maxy)
349 let gotopage n top =
350 let y = getpagey n in
351 addnav ();
352 state.y <- y + top;
353 gotoy state.y;
356 let reshape ~w ~h =
357 state.w <- w;
358 state.h <- h;
359 GlDraw.viewport 0 0 w h;
360 GlMat.mode `modelview;
361 GlMat.load_identity ();
362 GlMat.mode `projection;
363 GlMat.load_identity ();
364 GlMat.rotate ~x:1.0 ~angle:180.0 ();
365 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
366 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
367 GlClear.color (1., 1., 1.);
368 GlClear.clear [`color];
369 state.layout <- [];
370 state.pages <- [];
371 state.rects <- [];
372 state.text <- "";
373 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
376 let showtext c s =
377 if not conf.titletext
378 then (
379 GlDraw.color (0.0, 0.0, 0.0);
380 GlDraw.rect
381 (0.0, float (state.h - 18))
382 (float (state.w - conf.scrollw - 1), float state.h)
384 let font = Glut.BITMAP_8_BY_13 in
385 GlDraw.color (1.0, 1.0, 1.0);
386 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
387 Glut.bitmapCharacter ~font ~c:(Char.code c);
388 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
390 else
391 let len = String.length s in
392 let dst = String.create (len + 1) in
393 dst.[0] <- c;
394 StringLabels.blit
395 ~src:s
396 ~dst
397 ~src_pos:0
398 ~dst_pos:1
399 ~len
401 Glut.setWindowTitle ~title:dst
404 let enttext () =
405 let len = String.length state.text in
406 match state.textentry with
407 | None ->
408 if len > 0 then showtext ' ' state.text
410 | Some (c, text, _, _) ->
411 let s =
412 if len > 0
413 then
414 text ^ " [" ^ state.text ^ "]"
415 else
416 text
418 showtext c s;
421 let act cmd =
422 match cmd.[0] with
423 | 'c' ->
424 state.pages <- []
426 | 'd' ->
427 Glut.postRedisplay ()
429 | 'C' ->
430 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
431 state.pagecount <- n;
432 let rely = yratio state.y in
433 let maxy = calcheight () in
434 state.y <- truncate (float maxy *. rely);
435 let pages = layout state.y state.h in
436 state.layout <- pages;
437 Glut.postRedisplay ();
439 | 'f' ->
440 state.safe <- state.safe - 1;
441 if state.safe = 0
442 then
443 Glut.postRedisplay ()
446 | 'T' ->
447 let s = Scanf.sscanf cmd "T %S" (fun s -> s) in
448 state.text <- s;
449 showtext ' ' s;
450 Glut.swapBuffers ();
451 (* Glut.postRedisplay () *)
453 | 'F' ->
454 let pageno, c, x0, y0, x1, y1 =
455 Scanf.sscanf cmd "F %d %d %f %f %f %f"
456 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
458 let y = (getpagey pageno) + truncate y0 in
459 addnav ();
460 gotoy y;
461 state.rects <- [pageno, c, (x0, y0), (x1, y1)]
463 | 'R' ->
464 let pageno, c, x0, y0, x1, y1 =
465 Scanf.sscanf cmd "R %d %d %f %f %f %f"
466 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
468 state.rects <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects
470 | 'r' ->
471 let n, w, h, p =
472 Scanf.sscanf cmd "r %d %d %d %s"
473 (fun n w h p -> (n, w, h, p))
475 Hashtbl.replace state.pagemap (n, w) p;
476 let evicted = cbpeekw state.pagecache in
477 if String.length evicted > 0
478 then begin
479 state.safe <- state.safe + 1;
480 wcmd "free" [`s evicted];
481 let l = Hashtbl.fold (fun k p a ->
482 if evicted = p then k :: a else a) state.pagemap []
484 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
485 end;
486 cbput state.pagecache p;
487 state.inflight <- pred state.inflight;
488 Glut.postRedisplay ()
490 | 'l' ->
491 let (n, w, h) as pagelayout =
492 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
494 state.pages <- pagelayout :: state.pages
496 | 'o' ->
497 let (l, n, t, pos) =
498 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
500 let s = String.sub cmd pos (String.length cmd - pos) in
501 let outline = (s, l, n, t) in
502 let outlines =
503 match state.outlines with
504 | Olist outlines -> Olist (outline :: outlines)
505 | Oarray _ -> Olist [outline]
507 state.outlines <- outlines
509 | _ ->
510 log "unknown cmd `%S'" cmd
513 let getopaque pageno =
514 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
515 with Not_found -> None
518 let cache pageno opaque =
519 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
522 let validopaque opaque = String.length opaque > 0;;
524 let preload l =
525 match getopaque l.pageno with
526 | Some opaque when validopaque opaque ->
527 preload opaque
529 | None when state.inflight < 2+0*(cblen state.pagecache) ->
530 state.inflight <- succ state.inflight;
531 cache l.pageno "";
532 wcmd "render" [`i (l.pageno + 1)
533 ;`i l.pagedimno
534 ;`i l.pagew
535 ;`i l.pageh];
537 | _ -> ()
540 let idle () =
541 if not conf.redispimm && state.y != state.prevy
542 then (
543 state.prevy <- state.y;
544 Glut.postRedisplay ();
546 else
547 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
549 begin match r with
550 | [] ->
551 if conf.preload then begin
552 let h = state.h in
553 let y = if state.y < state.h then 0 else state.y - state.h in
554 let pages = layout y (h*3) in
555 List.iter preload pages;
556 end;
558 | _ ->
559 let cmd = readcmd state.csock in
560 act cmd;
561 end;
564 let search pattern forward =
565 if String.length pattern > 0
566 then
567 let pn, py =
568 match state.layout with
569 | [] -> 0, 0
570 | l :: _ ->
571 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
573 wcmd "search" [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)
574 ; `s (pattern ^ "\000")]
577 let intentry text key =
578 let c = Char.unsafe_chr key in
579 match c with
580 | '0' .. '9' ->
581 let s = "x" in s.[0] <- c;
582 let text = text ^ s in
583 TEcont text
585 | _ ->
586 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
587 TEcont text
590 let addchar s c =
591 let b = Buffer.create (String.length s + 1) in
592 Buffer.add_string b s;
593 Buffer.add_char b c;
594 Buffer.contents b;
597 let textentry text key =
598 let c = Char.unsafe_chr key in
599 match c with
600 | _ when key >= 32 && key <= 127 ->
601 let text = addchar text c in
602 TEcont text
604 | _ ->
605 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
606 TEcont text
609 let optentry text key =
610 let btos b = if b then "on" else "off" in
611 let c = Char.unsafe_chr key in
612 match c with
613 | 'r' ->
614 conf.rectsel <- not conf.rectsel;
615 TEdone ("rectsel " ^ (btos conf.rectsel))
617 | 'i' ->
618 conf.icase <- not conf.icase;
619 TEdone ("case insensitive search " ^ (btos conf.icase))
621 | 'p' ->
622 conf.preload <- not conf.preload;
623 TEdone ("preload " ^ (btos conf.preload))
625 | 't' ->
626 conf.titletext <- not conf.titletext;
627 TEdone ("titletext " ^ (btos conf.titletext))
629 | 'd' ->
630 conf.redispimm <- not conf.redispimm;
631 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
633 | 'v' ->
634 conf.verbose <- not conf.verbose;
635 TEdone ("verbose " ^ (btos conf.verbose))
637 | 'h' ->
638 conf.maxhfit <- not conf.maxhfit;
639 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
640 TEdone ("maxhfit " ^ (btos conf.maxhfit))
642 | _ ->
643 state.text <- Printf.sprintf "bad option %d `%c'" key c;
644 TEstop
647 let maxoutlinerows () = (state.h - 31) / 16;;
649 let enteroutlinemode () =
650 let pageno =
651 match state.layout with
652 | [] -> -1
653 | {pageno=pageno} :: rest -> pageno
655 let outlines =
656 match state.outlines with
657 | Oarray a -> a
658 | Olist l ->
659 let a = Array.of_list (List.rev l) in
660 state.outlines <- Oarray a;
663 let active =
664 let rec loop n =
665 if n = Array.length outlines
666 then 0
667 else
668 let (_, _, outlinepageno, _) = outlines.(n) in
669 if outlinepageno >= pageno then n else loop (n+1)
671 loop 0
673 state.outline <-
674 Some (active, max 0 (active - maxoutlinerows ()), outlines, "");
675 Glut.postRedisplay ();
678 let viewkeyboard ~key ~x ~y =
679 let enttext te =
680 state.textentry <- te;
681 state.text <- "";
682 enttext ();
683 Glut.swapBuffers ()
685 match state.textentry with
686 | None ->
687 let c = Char.chr key in
688 begin match c with
689 | '\027' | 'q' ->
690 exit 0
692 | '\008' ->
693 let y = getnav () in
694 gotoy y
696 | 'o' ->
697 enteroutlinemode ()
699 | 'u' ->
700 state.rects <- [];
701 state.text <- "";
702 Glut.postRedisplay ()
704 | '/' | '?' ->
705 let ondone isforw s =
706 state.searchpattern <- s;
707 search s isforw
709 enttext (Some (c, "", textentry, ondone (c ='/')))
711 | '+' ->
712 let ondone s =
713 let n =
714 try int_of_string s with exc ->
715 state.text <- Printf.sprintf "bad integer `%s': %s"
716 s (Printexc.to_string exc);
717 max_int
719 if n != max_int
720 then (
721 conf.pagebias <- n;
722 state.text <- "page bias is now " ^ string_of_int n;
725 enttext (Some ('+', "", intentry, ondone))
727 | '-' ->
728 let ondone msg =
729 state.text <- msg;
731 enttext (Some ('-', "", optentry, ondone))
733 | '0' .. '9' ->
734 let ondone s =
735 let n =
736 try int_of_string s with exc ->
737 state.text <- Printf.sprintf "bad integer `%s': %s"
738 s (Printexc.to_string exc);
741 if n >= 0
742 then (
743 addnav ();
744 state.y <- y;
745 gotoy (getpagey (n + conf.pagebias - 1))
748 let pageentry text key =
749 match Char.unsafe_chr key with
750 | 'g' -> TEdone text
751 | _ -> intentry text key
753 let text = "x" in text.[0] <- c;
754 enttext (Some (':', text, pageentry, ondone))
756 | 'b' ->
757 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
758 reshape state.w state.h;
760 | 'f' ->
761 begin match state.fullscreen with
762 | None ->
763 state.fullscreen <- Some (state.w, state.h);
764 Glut.fullScreen ()
765 | Some (w, h) ->
766 state.fullscreen <- None;
767 Glut.reshapeWindow ~w ~h
770 | 'n' ->
771 search state.searchpattern true
773 | 'p' ->
774 search state.searchpattern false
776 | 't' ->
777 begin match state.layout with
778 | [] -> ()
779 | l :: _ ->
780 gotoy (state.y - l.pagey);
783 | ' ' | 'N' ->
784 begin match List.rev state.layout with
785 | [] -> ()
786 | l :: _ ->
787 gotoy (state.y + l.pageh - l.pagey)
790 | '\127' | 'P' ->
791 begin match state.layout with
792 | [] -> ()
793 | l :: _ ->
794 gotoy (state.y-l.pageh);
797 | '=' ->
798 let f (fn, ln) l =
799 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
801 let fn, ln = List.fold_left f (-1, -1) state.layout in
802 let s =
803 let percent = (100. *. yratio state.y) in
804 if fn = ln
805 then
806 Printf.sprintf "Page %d of %d %.2f%%"
807 (fn+1) state.pagecount percent
808 else
809 Printf.sprintf
810 "Pages %d-%d of %d %.2f%%"
811 (fn+1) (ln+1) state.pagecount percent
813 showtext ' ' s;
814 Glut.swapBuffers ()
816 | 'w' ->
817 begin match state.layout with
818 | [] -> ()
819 | l :: _ ->
820 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
821 Glut.postRedisplay ();
824 | _ ->
825 vlog "huh? %d %c" key (Char.chr key);
828 | Some (c, text, onkey, ondone) when key = 8 ->
829 let len = String.length text in
830 if len = 0 || len = 1
831 then (
832 state.textentry <- None;
833 Glut.postRedisplay ();
835 else (
836 let s = String.sub text 0 (len - 1) in
837 enttext (Some (c, s, onkey, ondone))
840 | Some (c, text, onkey, ondone) ->
841 begin match Char.unsafe_chr key with
842 | '\r' | '\n' ->
843 ondone text;
844 state.textentry <- None;
845 Glut.postRedisplay ()
847 | '\027' ->
848 state.textentry <- None;
849 Glut.postRedisplay ()
851 | _ ->
852 begin match onkey text key with
853 | TEdone text ->
854 state.textentry <- None;
855 ondone text;
856 Glut.postRedisplay ()
858 | TEcont text ->
859 enttext (Some (c, text, onkey, ondone));
861 | TEstop ->
862 state.textentry <- None;
863 Glut.postRedisplay ()
864 end;
865 end;
868 let outlinekeyboard ~key ~x ~y (active, first, outlines, qsearch) =
869 let search active pattern incr =
870 let re = Str.regexp_case_fold pattern in
871 let rec loop n =
872 if n = Array.length outlines || n = -1 then None else
873 let (s, _, _, _) = outlines.(n) in
875 (try ignore (Str.search_forward re s 0); true
876 with Not_found -> false)
877 then (
878 let maxrows = maxoutlinerows () in
879 if first > n
880 then Some (n, max 0 (n - maxrows))
881 else Some (n, max first (n - maxrows))
883 else loop (n + incr)
885 loop active
887 match key with
888 | 27 ->
889 if String.length qsearch = 0
890 then (
891 state.text <- "";
892 state.outline <- None;
893 Glut.postRedisplay ();
895 else (
896 state.text <- "";
897 state.outline <- Some (active, first, outlines, "");
898 Glut.postRedisplay ();
901 | 18 | 19 ->
902 let incr = if key = 18 then -1 else 1 in
903 let active, first =
904 match search (active + incr) qsearch incr with
905 | None -> active, first
906 | Some af -> af
908 state.outline <- Some (active, first, outlines, qsearch);
909 Glut.postRedisplay ();
911 | 8 ->
912 let len = String.length qsearch in
913 if len = 0
914 then ()
915 else (
916 if len = 1
917 then (
918 state.text <- "";
919 state.outline <- Some (active, first, outlines, "");
921 else
922 let qsearch = String.sub qsearch 0 (len - 1) in
923 state.text <- qsearch;
924 state.outline <- Some (active, first, outlines, qsearch);
926 Glut.postRedisplay ()
928 | 13 ->
929 let (_, _, n, t) = outlines.(active) in
930 gotopage n t;
931 state.text <- "";
932 state.outline <- None;
933 Glut.postRedisplay ();
935 | _ when key >= 32 && key <= 127 ->
936 let pattern = addchar qsearch (Char.chr key) in
937 let active, first =
938 match search active pattern 1 with
939 | None -> active, first
940 | Some af -> af
942 state.text <- pattern;
943 state.outline <- Some (active, first, outlines, pattern);
944 Glut.postRedisplay ()
946 | _ -> log "unknown key %d" key
949 let keyboard ~key ~x ~y =
950 match state.outline with
951 | None -> viewkeyboard ~key ~x ~y
952 | Some outline -> outlinekeyboard ~key ~x ~y outline
955 let special ~key ~x ~y =
956 match state.outline with
957 | None ->
958 let y =
959 match key with
960 | Glut.KEY_F3 -> search state.searchpattern true; state.y
961 | Glut.KEY_UP -> clamp (-conf.scrollincr)
962 | Glut.KEY_DOWN -> clamp conf.scrollincr
963 | Glut.KEY_PAGE_UP -> clamp (-state.h)
964 | Glut.KEY_PAGE_DOWN -> clamp state.h
965 | Glut.KEY_HOME -> addnav (); 0
966 | Glut.KEY_END ->
967 addnav ();
968 state.maxy - (if conf.maxhfit then 0 else state.h)
969 | _ -> state.y
971 state.text <- "";
972 gotoy y
974 | Some (active, first, outlines, qsearch) ->
975 let maxrows = maxoutlinerows () in
976 let navigate incr =
977 let active = active + incr in
978 let active = max 0 (min active (Array.length outlines - 1)) in
979 let first =
980 if active > first
981 then
982 let rows = active - first in
983 if rows > maxrows then first + incr else first
984 else active
986 state.outline <- Some (active, first, outlines, qsearch);
987 Glut.postRedisplay ()
989 match key with
990 | Glut.KEY_UP -> navigate ~-1
991 | Glut.KEY_DOWN -> navigate 1
992 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
993 | Glut.KEY_PAGE_DOWN -> navigate maxrows
995 | Glut.KEY_HOME ->
996 state.outline <- Some (0, 0, outlines, qsearch);
997 Glut.postRedisplay ()
999 | Glut.KEY_END ->
1000 let active = Array.length outlines - 1 in
1001 let first = max 0 (active - maxrows) in
1002 state.outline <- Some (active, first, outlines, qsearch);
1003 Glut.postRedisplay ()
1005 | _ -> ()
1008 let drawplaceholder l =
1009 if true
1010 then (
1011 GlDraw.color (0.2, 0.2, 0.2);
1012 GlDraw.color (1.0, 1.0, 1.0);
1013 GlDraw.rect
1014 (0.0, float l.pagedispy)
1015 (float l.pagew, float (l.pagedispy + l.pagevh))
1017 let x = 0.0
1018 and y = float (l.pagedispy + l.pagevh - 5) in
1019 let font = Glut.BITMAP_8_BY_13 in
1020 GlDraw.color (0.0, 0.0, 0.0);
1021 GlPix.raster_pos ~x ~y ();
1022 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1023 ("Loading " ^ string_of_int l.pageno);
1025 else (
1026 GlDraw.begins `quads;
1027 GlDraw.vertex2 (0.0, float l.pagedispy);
1028 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1029 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1030 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1031 GlDraw.ends ();
1035 let now () = Unix.gettimeofday ();;
1037 let drawpage i l =
1038 begin match getopaque l.pageno with
1039 | Some opaque when validopaque opaque ->
1040 GlDraw.color (1.0, 1.0, 1.0);
1041 let a = now () in
1042 if state.safe = 0
1043 then
1044 draw l.pagedispy l.pagew l.pagevh l.pagey opaque
1046 let b = now () in
1047 let d = b-.a in
1048 vlog "draw %f sec safe %d" d state.safe;
1050 | Some _ ->
1051 drawplaceholder l
1053 | None ->
1054 if state.inflight < cblen state.pagecache
1055 then (
1056 List.iter preload state.layout;
1058 else (
1059 drawplaceholder l;
1060 vlog "inflight %d" state.inflight;
1062 end;
1063 GlDraw.color (0.5, 0.5, 0.5);
1064 GlDraw.rect
1065 (0., float i)
1066 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1068 l.pagedispy + l.pagevh;
1071 let scrollindicator () =
1072 GlDraw.color (0.64 , 0.64, 0.64);
1073 GlDraw.rect
1074 (float (state.w - conf.scrollw), 0.)
1075 (float state.w, float state.h)
1077 GlDraw.color (0.0, 0.0, 0.0);
1078 let sh = (float (state.maxy + state.h) /. float state.h) in
1079 let sh = float state.h /. sh in
1080 let sh = max sh (float conf.scrollh) in
1082 let percent = yratio state.y in
1083 let position = (float state.h -. sh) *. percent in
1085 let position =
1086 if position +. sh > float state.h
1087 then
1088 float state.h -. sh
1089 else
1090 position
1092 GlDraw.rect
1093 (float (state.w - conf.scrollw), position)
1094 (float state.w, position +. sh)
1098 let showsel () =
1099 match state.mstate with
1100 | Mnone ->
1103 | Msel ((x0, y0), (x1, y1)) ->
1104 let y0' = min y0 y1
1105 and y1 = max y0 y1 in
1106 let y0 = y0' in
1107 let f l =
1108 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1109 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1110 then
1111 match getopaque l.pageno with
1112 | Some opaque when validopaque opaque ->
1113 let oy = -l.pagey + l.pagedispy in
1114 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1115 | _ -> ()
1117 List.iter f state.layout
1120 let showrects () =
1121 Gl.enable `blend;
1122 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1123 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1124 List.iter
1125 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1126 List.iter (fun l ->
1127 if l.pageno = pageno
1128 then (
1129 let d = float (l.pagedispy - l.pagey) in
1130 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1131 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1133 ) state.layout
1134 ) state.rects
1136 Gl.disable `blend;
1139 let showoutline = function
1140 | None -> ()
1141 | Some (active, first, outlines, qsearch) ->
1142 Gl.enable `blend;
1143 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1144 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1145 GlDraw.rect (0., 0.) (float state.w, float state.h);
1146 Gl.disable `blend;
1148 GlDraw.color (1., 1., 1.);
1149 let font = Glut.BITMAP_9_BY_15 in
1150 let draw_string x y s =
1151 GlPix.raster_pos ~x ~y ();
1152 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1154 let rec loop row =
1155 if row = Array.length outlines || (row - first) * 16 > state.h
1156 then ()
1157 else (
1158 let (s, l, _, _) = outlines.(row) in
1159 let y = (row - first) * 16 in
1160 let x = 5 + 5*l in
1161 if row = active
1162 then (
1163 Gl.enable `blend;
1164 GlDraw.polygon_mode `both `line;
1165 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1166 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1167 GlDraw.rect (0., float (y + 1))
1168 (float (state.w - conf.scrollw - 1), float (y + 18));
1169 GlDraw.polygon_mode `both `fill;
1170 Gl.disable `blend;
1171 GlDraw.color (1., 1., 1.);
1173 draw_string (float x) (float (y + 16)) s;
1174 loop (row+1)
1177 loop first
1180 let display () =
1181 let lasty = List.fold_left drawpage 0 (state.layout) in
1182 GlDraw.color (0.5, 0.5, 0.5);
1183 GlDraw.rect
1184 (0., float lasty)
1185 (float (state.w - conf.scrollw), float state.h)
1187 showrects ();
1188 scrollindicator ();
1189 showsel ();
1190 showoutline state.outline;
1191 enttext ();
1192 Glut.swapBuffers ();
1195 let getlink x y =
1196 let rec f = function
1197 | l :: rest ->
1198 begin match getopaque l.pageno with
1199 | Some opaque when validopaque opaque ->
1200 let y = y - l.pagedispy in
1201 if y > 0
1202 then
1203 let y = l.pagey + y in
1204 match getlink opaque x y with
1205 | None -> f rest
1206 | some -> some
1207 else
1208 f rest
1209 | _ ->
1210 f rest
1212 | [] -> None
1214 f state.layout
1217 let checklink x y =
1218 let rec f = function
1219 | l :: rest ->
1220 begin match getopaque l.pageno with
1221 | Some opaque when validopaque opaque ->
1222 let y = y - l.pagedispy in
1223 if y > 0
1224 then
1225 let y = l.pagey + y in
1226 if checklink opaque x y then true else f rest
1227 else
1228 f rest
1229 | _ ->
1230 f rest
1232 | [] -> false
1234 f state.layout
1237 let mouse ~button ~bstate ~x ~y =
1238 match button with
1239 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1240 let incr =
1241 if n = 3
1242 then
1243 -conf.scrollincr
1244 else
1245 conf.scrollincr
1247 let incr = incr * 2 in
1248 let y = clamp incr in
1249 gotoy y
1251 | Glut.LEFT_BUTTON when state.outline = None ->
1252 let dest = if bstate = Glut.DOWN then getlink x y else None in
1253 begin match dest with
1254 | Some (pageno, top) ->
1255 gotopage pageno top
1257 | None ->
1258 if bstate = Glut.DOWN
1259 then (
1260 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1261 state.mstate <- Msel ((x, y), (x, y));
1262 Glut.postRedisplay ()
1264 else (
1265 Glut.setCursor Glut.CURSOR_RIGHT_ARROW;
1266 state.mstate <- Mnone;
1270 | _ ->
1273 let mouse ~button ~state ~x ~y = mouse button state x y;;
1275 let motion ~x ~y =
1276 if state.outline = None
1277 then
1278 match state.mstate with
1279 | Mnone -> ()
1280 | Msel (a, _) ->
1281 state.mstate <- Msel (a, (x, y));
1282 Glut.postRedisplay ()
1285 let pmotion ~x ~y =
1286 if state.outline = None
1287 then
1288 match state.mstate with
1289 | Mnone when (checklink x y) ->
1290 Glut.setCursor Glut.CURSOR_INFO
1292 | Mnone ->
1293 Glut.setCursor Glut.CURSOR_RIGHT_ARROW
1295 | Msel (a, _) ->
1299 let () =
1300 let name = ref "" in
1301 Arg.parse [] (fun s -> name := s) "options:";
1302 let name =
1303 if String.length !name = 0
1304 then (prerr_endline "filename missing"; exit 1)
1305 else !name
1308 let w = 900 in
1309 let h = 900 in
1310 let _ = Glut.init Sys.argv in
1311 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1312 let () = Glut.initWindowSize w h in
1313 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1315 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1317 init ssock;
1318 state.w <- w;
1319 state.h <- h;
1320 state.csock <- csock;
1321 state.ssock <- ssock;
1322 writecmd csock ("open " ^ name ^ "\000");
1324 let () = Glut.displayFunc display in
1325 let () = Glut.reshapeFunc reshape in
1326 let () = Glut.keyboardFunc keyboard in
1327 let () = Glut.specialFunc special in
1328 let () = Glut.idleFunc (Some idle) in
1329 let () = Glut.mouseFunc mouse in
1330 let () = Glut.motionFunc motion in
1331 let () = Glut.passiveMotionFunc pmotion in
1332 let rec handlelablglutbug () =
1334 Glut.mainLoop ();
1335 with Glut.BadEnum "key in special_of_int" ->
1336 showtext '!' " LablGlut bug: special key not recognized";
1337 Glut.swapBuffers ();
1338 handlelablglutbug ()
1340 handlelablglutbug ()