Make openxref static
[llpp.git] / main.ml
blob73806fb0086ed5182305afd252d731cb5c41e411
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 %S" (fun s -> s) in
439 state.text <- s;
440 showtext ' ' s;
441 Glut.swapBuffers ();
442 (* Glut.postRedisplay () *)
444 | 'F' ->
445 let pageno, c, x0, y0, x1, y1 =
446 Scanf.sscanf cmd "F %d %d %f %f %f %f"
447 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
449 let y = (getpagey pageno) + truncate y0 in
450 addnav ();
451 gotoy y;
452 state.rects <- [pageno, c, (x0, y0), (x1, y1)]
454 | 'R' ->
455 let pageno, c, x0, y0, x1, y1 =
456 Scanf.sscanf cmd "R %d %d %f %f %f %f"
457 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
459 state.rects <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects
461 | 'r' ->
462 let n, w, h, p =
463 Scanf.sscanf cmd "r %d %d %d %s"
464 (fun n w h p -> (n, w, h, p))
466 Hashtbl.replace state.pagemap (n, w) p;
467 let evicted = cbpeekw state.pagecache in
468 if String.length evicted > 0
469 then begin
470 wcmd "free" [`s evicted];
471 let l = Hashtbl.fold (fun k p a ->
472 if evicted = p then k :: a else a) state.pagemap []
474 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
475 end;
476 cbput state.pagecache p;
477 state.inflight <- pred state.inflight;
478 Glut.postRedisplay ()
480 | 'l' ->
481 let (n, w, h) as pagelayout =
482 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
484 state.pages <- pagelayout :: state.pages
486 | 'o' ->
487 let (l, n, t, pos) =
488 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
490 let s = String.sub cmd pos (String.length cmd - pos) in
491 let outline = (s, l, n, t) in
492 let outlines =
493 match state.outlines with
494 | Olist outlines -> Olist (outline :: outlines)
495 | Oarray _ -> Olist [outline]
497 state.outlines <- outlines
499 | _ ->
500 log "unknown cmd `%S'" cmd
503 let getopaque pageno =
504 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
505 with Not_found -> None
508 let cache pageno opaque =
509 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
512 let validopaque opaque = String.length opaque > 0;;
514 let preload l =
515 match getopaque l.pageno with
516 | Some opaque when validopaque opaque ->
517 preload opaque
519 | None when state.inflight < 2+0*(cblen state.pagecache) ->
520 state.inflight <- succ state.inflight;
521 cache l.pageno "";
522 wcmd "render" [`i (l.pageno + 1)
523 ;`i l.pagedimno
524 ;`i l.pagew
525 ;`i l.pageh];
527 | _ -> ()
530 let idle () =
531 if not conf.redispimm && state.y != state.prevy
532 then (
533 state.prevy <- state.y;
534 Glut.postRedisplay ();
536 else
537 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
539 begin match r with
540 | [] ->
541 if conf.preload then begin
542 let h = state.h in
543 let y = if state.y < state.h then 0 else state.y - state.h in
544 let pages = layout y (h*3) in
545 List.iter preload pages;
546 end;
548 | _ ->
549 let cmd = readcmd state.csock in
550 act cmd;
551 end;
554 let search pattern forward =
555 if String.length pattern > 0
556 then
557 let pn, py =
558 match state.layout with
559 | [] -> 0, 0
560 | l :: _ ->
561 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
563 wcmd "search" [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)
564 ; `s (pattern ^ "\000")]
567 let intentry text key =
568 let c = Char.unsafe_chr key in
569 match c with
570 | '0' .. '9' ->
571 let s = "x" in s.[0] <- c;
572 let text = text ^ s in
573 TEcont text
575 | _ ->
576 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
577 TEcont text
580 let addchar s c =
581 let b = Buffer.create (String.length s + 1) in
582 Buffer.add_string b s;
583 Buffer.add_char b c;
584 Buffer.contents b;
587 let textentry text key =
588 let c = Char.unsafe_chr key in
589 match c with
590 | _ when key >= 32 && key <= 127 ->
591 let text = addchar text c in
592 TEcont text
594 | _ ->
595 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
596 TEcont text
599 let optentry text key =
600 let btos b = if b then "on" else "off" in
601 let c = Char.unsafe_chr key in
602 match c with
603 | 'r' ->
604 conf.rectsel <- not conf.rectsel;
605 TEdone ("rectsel " ^ (btos conf.rectsel))
607 | 'i' ->
608 conf.icase <- not conf.icase;
609 TEdone ("case insensitive search " ^ (btos conf.icase))
611 | 'p' ->
612 conf.preload <- not conf.preload;
613 TEdone ("preload " ^ (btos conf.preload))
615 | 't' ->
616 conf.titletext <- not conf.titletext;
617 TEdone ("titletext " ^ (btos conf.titletext))
619 | 'd' ->
620 conf.redispimm <- not conf.redispimm;
621 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
623 | 'v' ->
624 conf.verbose <- not conf.verbose;
625 TEdone ("verbose " ^ (btos conf.verbose))
627 | 'h' ->
628 conf.maxhfit <- not conf.maxhfit;
629 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
630 TEdone ("maxhfit " ^ (btos conf.maxhfit))
632 | _ ->
633 state.text <- Printf.sprintf "bad option %d `%c'" key c;
634 TEstop
637 let maxoutlinerows () = (state.h - 31) / 16;;
639 let enteroutlinemode () =
640 let pageno =
641 match state.layout with
642 | [] -> -1
643 | {pageno=pageno} :: rest -> pageno
645 let outlines =
646 match state.outlines with
647 | Oarray a -> a
648 | Olist l ->
649 let a = Array.of_list (List.rev l) in
650 state.outlines <- Oarray a;
653 let active =
654 let rec loop n =
655 if n = Array.length outlines
656 then 0
657 else
658 let (_, _, outlinepageno, _) = outlines.(n) in
659 if outlinepageno >= pageno then n else loop (n+1)
661 loop 0
663 state.outline <-
664 Some (active, max 0 (active - maxoutlinerows ()), outlines, "");
665 Glut.postRedisplay ();
668 let viewkeyboard ~key ~x ~y =
669 let enttext te =
670 state.textentry <- te;
671 state.text <- "";
672 enttext ();
673 Glut.swapBuffers ()
675 match state.textentry with
676 | None ->
677 let c = Char.chr key in
678 begin match c with
679 | '\027' | 'q' ->
680 exit 0
682 | '\008' ->
683 let y = getnav () in
684 gotoy y
686 | 'o' ->
687 enteroutlinemode ()
689 | 'u' ->
690 state.rects <- [];
691 state.text <- "";
692 Glut.postRedisplay ()
694 | '/' | '?' ->
695 let ondone isforw s =
696 state.searchpattern <- s;
697 search s isforw
699 enttext (Some (c, "", textentry, ondone (c ='/')))
701 | '+' ->
702 let ondone s =
703 let n =
704 try int_of_string s with exc ->
705 state.text <- Printf.sprintf "bad integer `%s': %s"
706 s (Printexc.to_string exc);
707 max_int
709 if n != max_int
710 then (
711 conf.pagebias <- n;
712 state.text <- "page bias is now " ^ string_of_int n;
715 enttext (Some ('+', "", intentry, ondone))
717 | '-' ->
718 let ondone msg =
719 state.text <- msg;
721 enttext (Some ('-', "", optentry, ondone))
723 | '0' .. '9' ->
724 let ondone s =
725 let n =
726 try int_of_string s with exc ->
727 state.text <- Printf.sprintf "bad integer `%s': %s"
728 s (Printexc.to_string exc);
731 if n >= 0
732 then (
733 addnav ();
734 state.y <- y;
735 gotoy (getpagey (n + conf.pagebias - 1))
738 let pageentry text key =
739 match Char.unsafe_chr key with
740 | 'g' -> TEdone text
741 | _ -> intentry text key
743 let text = "x" in text.[0] <- c;
744 enttext (Some (':', text, pageentry, ondone))
746 | 'b' ->
747 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
748 reshape state.w state.h;
750 | 'f' ->
751 begin match state.fullscreen with
752 | None ->
753 state.fullscreen <- Some (state.w, state.h);
754 Glut.fullScreen ()
755 | Some (w, h) ->
756 state.fullscreen <- None;
757 Glut.reshapeWindow ~w ~h
760 | 'n' ->
761 search state.searchpattern true
763 | 'p' ->
764 search state.searchpattern false
766 | 't' ->
767 begin match state.layout with
768 | [] -> ()
769 | l :: _ ->
770 gotoy (state.y - l.pagey);
773 | ' ' | 'N' ->
774 begin match List.rev state.layout with
775 | [] -> ()
776 | l :: _ ->
777 gotoy (clamp (l.pageh - l.pagey))
780 | '\127' | 'P' ->
781 begin match state.layout with
782 | [] -> ()
783 | l :: _ ->
784 gotoy (clamp (-l.pageh));
787 | '=' ->
788 let f (fn, ln) l =
789 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
791 let fn, ln = List.fold_left f (-1, -1) state.layout in
792 let s =
793 let percent = (100. *. yratio state.y) in
794 if fn = ln
795 then
796 Printf.sprintf "Page %d of %d %.2f%%"
797 (fn+1) state.pagecount percent
798 else
799 Printf.sprintf
800 "Pages %d-%d of %d %.2f%%"
801 (fn+1) (ln+1) state.pagecount percent
803 showtext ' ' s;
804 Glut.swapBuffers ()
806 | 'w' ->
807 begin match state.layout with
808 | [] -> ()
809 | l :: _ ->
810 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
811 Glut.postRedisplay ();
814 | _ ->
815 vlog "huh? %d %c" key (Char.chr key);
818 | Some (c, text, onkey, ondone) when key = 8 ->
819 let len = String.length text in
820 if len = 0 || len = 1
821 then (
822 state.textentry <- None;
823 Glut.postRedisplay ();
825 else (
826 let s = String.sub text 0 (len - 1) in
827 enttext (Some (c, s, onkey, ondone))
830 | Some (c, text, onkey, ondone) ->
831 begin match Char.unsafe_chr key with
832 | '\r' | '\n' ->
833 ondone text;
834 state.textentry <- None;
835 Glut.postRedisplay ()
837 | '\027' ->
838 state.textentry <- None;
839 Glut.postRedisplay ()
841 | _ ->
842 begin match onkey text key with
843 | TEdone text ->
844 state.textentry <- None;
845 ondone text;
846 Glut.postRedisplay ()
848 | TEcont text ->
849 enttext (Some (c, text, onkey, ondone));
851 | TEstop ->
852 state.textentry <- None;
853 Glut.postRedisplay ()
854 end;
855 end;
858 let outlinekeyboard ~key ~x ~y (active, first, outlines, qsearch) =
859 let search active pattern incr =
860 let re = Str.regexp_case_fold pattern in
861 let rec loop n =
862 if n = Array.length outlines || n = -1 then None else
863 let (s, _, _, _) = outlines.(n) in
865 (try ignore (Str.search_forward re s 0); true
866 with Not_found -> false)
867 then (
868 let maxrows = maxoutlinerows () in
869 if first > n
870 then Some (n, max 0 (n - maxrows))
871 else Some (n, max first (n - maxrows))
873 else loop (n + incr)
875 loop active
877 match key with
878 | 27 ->
879 if String.length qsearch = 0
880 then (
881 state.text <- "";
882 state.outline <- None;
883 Glut.postRedisplay ();
885 else (
886 state.text <- "";
887 state.outline <- Some (active, first, outlines, "");
888 Glut.postRedisplay ();
891 | 18 | 19 ->
892 let incr = if key = 18 then -1 else 1 in
893 let active, first =
894 match search (active + incr) qsearch incr with
895 | None -> active, first
896 | Some af -> af
898 state.outline <- Some (active, first, outlines, qsearch);
899 Glut.postRedisplay ();
901 | 8 ->
902 let len = String.length qsearch in
903 if len = 0
904 then ()
905 else (
906 if len = 1
907 then (
908 state.text <- "";
909 state.outline <- Some (active, first, outlines, "");
911 else
912 let qsearch = String.sub qsearch 0 (len - 1) in
913 state.text <- qsearch;
914 state.outline <- Some (active, first, outlines, qsearch);
916 Glut.postRedisplay ()
918 | 13 ->
919 let (_, _, n, t) = outlines.(active) in
920 gotopage n t;
921 state.text <- "";
922 state.outline <- None;
923 Glut.postRedisplay ();
925 | _ when key >= 32 && key <= 127 ->
926 let pattern = addchar qsearch (Char.chr key) in
927 let pattern, active, first =
928 match search active pattern 1 with
929 | None -> qsearch, active, first
930 | Some (active, first) -> (pattern, active, first)
932 state.text <- pattern;
933 state.outline <- Some (active, first, outlines, pattern);
934 Glut.postRedisplay ()
936 | _ -> log "unknown key %d" key
939 let keyboard ~key ~x ~y =
940 match state.outline with
941 | None -> viewkeyboard ~key ~x ~y
942 | Some outline -> outlinekeyboard ~key ~x ~y outline
945 let special ~key ~x ~y =
946 match state.outline with
947 | None ->
948 let y =
949 match key with
950 | Glut.KEY_F3 -> search state.searchpattern true; state.y
951 | Glut.KEY_UP -> clamp (-conf.scrollincr)
952 | Glut.KEY_DOWN -> clamp conf.scrollincr
953 | Glut.KEY_PAGE_UP -> clamp (-state.h)
954 | Glut.KEY_PAGE_DOWN -> clamp state.h
955 | Glut.KEY_HOME -> addnav (); 0
956 | Glut.KEY_END ->
957 addnav ();
958 state.maxy - (if conf.maxhfit then state.h else 0)
959 | _ -> state.y
961 state.text <- "";
962 gotoy y
964 | Some (active, first, outlines, qsearch) ->
965 let maxrows = maxoutlinerows () in
966 let navigate incr =
967 let active = active + incr in
968 let active = max 0 (min active (Array.length outlines - 1)) in
969 let first =
970 if active > first
971 then
972 let rows = active - first in
973 if rows > maxrows then first + incr else first
974 else active
976 state.outline <- Some (active, first, outlines, qsearch);
977 Glut.postRedisplay ()
979 match key with
980 | Glut.KEY_UP -> navigate ~-1
981 | Glut.KEY_DOWN -> navigate 1
982 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
983 | Glut.KEY_PAGE_DOWN -> navigate maxrows
985 | Glut.KEY_HOME ->
986 state.outline <- Some (0, 0, outlines, qsearch);
987 Glut.postRedisplay ()
989 | Glut.KEY_END ->
990 let active = Array.length outlines - 1 in
991 let first = max 0 (active - maxrows) in
992 state.outline <- Some (active, first, outlines, qsearch);
993 Glut.postRedisplay ()
995 | _ -> ()
998 let drawplaceholder l =
999 if true
1000 then (
1001 GlDraw.color (0.2, 0.2, 0.2);
1002 GlDraw.color (1.0, 1.0, 1.0);
1003 GlDraw.rect
1004 (0.0, float l.pagedispy)
1005 (float l.pagew, float (l.pagedispy + l.pagevh))
1007 let x = 0.0
1008 and y = float (l.pagedispy + l.pagevh - 5) in
1009 let font = Glut.BITMAP_8_BY_13 in
1010 GlDraw.color (0.0, 0.0, 0.0);
1011 GlPix.raster_pos ~x ~y ();
1012 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1013 ("Loading " ^ string_of_int l.pageno);
1015 else (
1016 GlDraw.begins `quads;
1017 GlDraw.vertex2 (0.0, float l.pagedispy);
1018 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1019 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1020 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1021 GlDraw.ends ();
1025 let now () = Unix.gettimeofday ();;
1027 let drawpage i l =
1028 begin match getopaque l.pageno with
1029 | Some opaque when validopaque opaque ->
1030 GlDraw.color (1.0, 1.0, 1.0);
1031 let a = now () in
1032 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1033 let b = now () in
1034 let d = b-.a in
1035 vlog "draw %f sec" d;
1037 | Some _ ->
1038 drawplaceholder l
1040 | None ->
1041 if state.inflight < cblen state.pagecache
1042 then (
1043 List.iter preload state.layout;
1045 else (
1046 drawplaceholder l;
1047 vlog "inflight %d" state.inflight;
1049 end;
1050 GlDraw.color (0.5, 0.5, 0.5);
1051 GlDraw.rect
1052 (0., float i)
1053 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1055 l.pagedispy + l.pagevh;
1058 let scrollindicator () =
1059 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1060 GlDraw.color (0.64 , 0.64, 0.64);
1061 GlDraw.rect
1062 (float (state.w - conf.scrollw), 0.)
1063 (float state.w, float state.h)
1065 GlDraw.color (0.0, 0.0, 0.0);
1066 let sh = (float (maxy + state.h) /. float state.h) in
1067 let sh = float state.h /. sh in
1068 let sh = max sh (float conf.scrollh) in
1070 let percent =
1071 if state.y = state.maxy
1072 then 1.0
1073 else float state.y /. float maxy
1075 let position = (float state.h -. sh) *. percent in
1077 let position =
1078 if position +. sh > float state.h
1079 then
1080 float state.h -. sh
1081 else
1082 position
1084 GlDraw.rect
1085 (float (state.w - conf.scrollw), position)
1086 (float state.w, position +. sh)
1090 let showsel () =
1091 match state.mstate with
1092 | Mnone ->
1095 | Msel ((x0, y0), (x1, y1)) ->
1096 let y0' = min y0 y1
1097 and y1 = max y0 y1 in
1098 let y0 = y0' in
1099 let f l =
1100 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1101 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1102 then
1103 match getopaque l.pageno with
1104 | Some opaque when validopaque opaque ->
1105 let oy = -l.pagey + l.pagedispy in
1106 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1107 | _ -> ()
1109 List.iter f state.layout
1112 let showrects () =
1113 Gl.enable `blend;
1114 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1115 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1116 List.iter
1117 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1118 List.iter (fun l ->
1119 if l.pageno = pageno
1120 then (
1121 let d = float (l.pagedispy - l.pagey) in
1122 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1123 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1125 ) state.layout
1126 ) state.rects
1128 Gl.disable `blend;
1131 let showoutline = function
1132 | None -> ()
1133 | Some (active, first, outlines, qsearch) ->
1134 Gl.enable `blend;
1135 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1136 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1137 GlDraw.rect (0., 0.) (float state.w, float state.h);
1138 Gl.disable `blend;
1140 GlDraw.color (1., 1., 1.);
1141 let font = Glut.BITMAP_9_BY_15 in
1142 let draw_string x y s =
1143 GlPix.raster_pos ~x ~y ();
1144 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1146 let rec loop row =
1147 if row = Array.length outlines || (row - first) * 16 > state.h
1148 then ()
1149 else (
1150 let (s, l, _, _) = outlines.(row) in
1151 let y = (row - first) * 16 in
1152 let x = 5 + 5*l in
1153 if row = active
1154 then (
1155 Gl.enable `blend;
1156 GlDraw.polygon_mode `both `line;
1157 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1158 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1159 GlDraw.rect (0., float (y + 1))
1160 (float (state.w - conf.scrollw - 1), float (y + 18));
1161 GlDraw.polygon_mode `both `fill;
1162 Gl.disable `blend;
1163 GlDraw.color (1., 1., 1.);
1165 draw_string (float x) (float (y + 16)) s;
1166 loop (row+1)
1169 loop first
1172 let display () =
1173 let lasty = List.fold_left drawpage 0 (state.layout) in
1174 GlDraw.color (0.5, 0.5, 0.5);
1175 GlDraw.rect
1176 (0., float lasty)
1177 (float (state.w - conf.scrollw), float state.h)
1179 showrects ();
1180 scrollindicator ();
1181 showsel ();
1182 showoutline state.outline;
1183 enttext ();
1184 Glut.swapBuffers ();
1187 let getlink x y =
1188 let rec f = function
1189 | l :: rest ->
1190 begin match getopaque l.pageno with
1191 | Some opaque when validopaque opaque ->
1192 let y = y - l.pagedispy in
1193 if y > 0
1194 then
1195 let y = l.pagey + y in
1196 match getlink opaque x y with
1197 | None -> f rest
1198 | some -> some
1199 else
1200 f rest
1201 | _ ->
1202 f rest
1204 | [] -> None
1206 f state.layout
1209 let checklink x y =
1210 let rec f = function
1211 | l :: rest ->
1212 begin match getopaque l.pageno with
1213 | Some opaque when validopaque opaque ->
1214 let y = y - l.pagedispy in
1215 if y > 0
1216 then
1217 let y = l.pagey + y in
1218 if checklink opaque x y then true else f rest
1219 else
1220 f rest
1221 | _ ->
1222 f rest
1224 | [] -> false
1226 f state.layout
1229 let mouse ~button ~bstate ~x ~y =
1230 match button with
1231 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1232 let incr =
1233 if n = 3
1234 then
1235 -conf.scrollincr
1236 else
1237 conf.scrollincr
1239 let incr = incr * 2 in
1240 let y = clamp incr in
1241 gotoy y
1243 | Glut.LEFT_BUTTON when state.outline = None ->
1244 let dest = if bstate = Glut.DOWN then getlink x y else None in
1245 begin match dest with
1246 | Some (pageno, top) ->
1247 gotopage pageno top
1249 | None ->
1250 if bstate = Glut.DOWN
1251 then (
1252 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1253 state.mstate <- Msel ((x, y), (x, y));
1254 Glut.postRedisplay ()
1256 else (
1257 Glut.setCursor Glut.CURSOR_RIGHT_ARROW;
1258 state.mstate <- Mnone;
1262 | _ ->
1265 let mouse ~button ~state ~x ~y = mouse button state x y;;
1267 let motion ~x ~y =
1268 if state.outline = None
1269 then
1270 match state.mstate with
1271 | Mnone -> ()
1272 | Msel (a, _) ->
1273 state.mstate <- Msel (a, (x, y));
1274 Glut.postRedisplay ()
1277 let pmotion ~x ~y =
1278 if state.outline = None
1279 then
1280 match state.mstate with
1281 | Mnone when (checklink x y) ->
1282 Glut.setCursor Glut.CURSOR_INFO
1284 | Mnone ->
1285 Glut.setCursor Glut.CURSOR_RIGHT_ARROW
1287 | Msel (a, _) ->
1291 let () =
1292 let name = ref "" in
1293 Arg.parse [] (fun s -> name := s) "options:";
1294 let name =
1295 if String.length !name = 0
1296 then (prerr_endline "filename missing"; exit 1)
1297 else !name
1300 let w = 900 in
1301 let h = 900 in
1302 let _ = Glut.init Sys.argv in
1303 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1304 let () = Glut.initWindowSize w h in
1305 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1307 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1309 init ssock;
1310 state.w <- w;
1311 state.h <- h;
1312 state.csock <- csock;
1313 state.ssock <- ssock;
1314 writecmd csock ("open " ^ name ^ "\000");
1316 let () = Glut.displayFunc display in
1317 let () = Glut.reshapeFunc reshape in
1318 let () = Glut.keyboardFunc keyboard in
1319 let () = Glut.specialFunc special in
1320 let () = Glut.idleFunc (Some idle) in
1321 let () = Glut.mouseFunc mouse in
1322 let () = Glut.motionFunc motion in
1323 let () = Glut.passiveMotionFunc pmotion in
1324 let rec handlelablglutbug () =
1326 Glut.mainLoop ();
1327 with Glut.BadEnum "key in special_of_int" ->
1328 showtext '!' " LablGlut bug: special key not recognized";
1329 Glut.swapBuffers ();
1330 handlelablglutbug ()
1332 handlelablglutbug ()