Crop
[llpp.git] / main.ml
blobdd7f7856af1312959ab6dd739c3fd50781c8e69a
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";;
13 external getpagewh : int -> float array = "ml_getpagewh";;
15 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
119 ; mutable bookmarks : outline list
120 ; mutable path : string
124 let conf =
125 { scrollw = 5
126 ; scrollh = 12
127 ; icase = true
128 ; rectsel = true
129 ; preload = false
130 ; titletext = false
131 ; pagebias = 0
132 ; redispimm = false
133 ; verbose = false
134 ; scrollincr = 18
135 ; maxhfit = true
139 let state =
140 { csock = Unix.stdin
141 ; ssock = Unix.stdin
142 ; w = 900
143 ; h = 900
144 ; y = 0
145 ; prevy = 0
146 ; layout = []
147 ; maxy = max_int
148 ; pagemap = Hashtbl.create 10
149 ; pagecache = cbnew 10 ""
150 ; pages = []
151 ; pagecount = 0
152 ; inflight = 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
162 ; bookmarks = []
163 ; path = ""
167 let vlog fmt =
168 if conf.verbose
169 then
170 Printf.kprintf prerr_endline fmt
171 else
172 Printf.kprintf ignore fmt
175 let writecmd fd s =
176 let len = String.length s in
177 let n = 4 + len in
178 let b = Buffer.create n in
179 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
180 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
181 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
182 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
183 Buffer.add_string b s;
184 let s' = Buffer.contents b in
185 let n' = Unix.write fd s' 0 n in
186 if n' != n then failwith "write failed";
189 let readcmd fd =
190 let s = "xxxx" in
191 let n = Unix.read fd s 0 4 in
192 if n != 4 then failwith "incomplete read(len)";
193 let len = 0
194 lor (Char.code s.[0] lsl 24)
195 lor (Char.code s.[1] lsl 16)
196 lor (Char.code s.[2] lsl 8)
197 lor (Char.code s.[3] lsl 0)
199 let s = String.create len in
200 let n = Unix.read fd s 0 len in
201 if n != len then failwith "incomplete read(data)";
205 let yratio y =
206 if y = state.maxy then 1.0
207 else float y /. float state.maxy
210 let makecmd s l =
211 let b = Buffer.create 10 in
212 Buffer.add_string b s;
213 let rec combine = function
214 | [] -> b
215 | x :: xs ->
216 Buffer.add_char b ' ';
217 let s =
218 match x with
219 | `b b -> if b then "1" else "0"
220 | `s s -> s
221 | `i i -> string_of_int i
222 | `f f -> string_of_float f
223 | `I f -> string_of_int (truncate f)
225 Buffer.add_string b s;
226 combine xs;
228 combine l;
231 let wcmd s l =
232 let cmd = Buffer.contents (makecmd s l) in
233 writecmd state.csock cmd;
236 let calcheight () =
237 let rec f pn ph fh l =
238 match l with
239 | (n, _, h) :: rest ->
240 let fh = fh + (n - pn) * ph in
241 f n h fh rest
243 | [] ->
244 let fh = fh + (ph * (state.pagecount - pn)) in
245 max 0 fh
247 let fh = f 0 0 0 state.pages in
251 let getpagey pageno =
252 let rec f pn ph y l =
253 match l with
254 | (n, _, h) :: rest ->
255 if n >= pageno
256 then
257 y + (pageno - pn) * ph
258 else
259 let y = y + (n - pn) * ph in
260 f n h y rest
262 | [] ->
263 y + (pageno - pn) * ph
265 f 0 0 0 state.pages;
268 let layout y sh =
269 let rec f pageno pdimno prev vy py dy l cacheleft accu =
270 if pageno = state.pagecount || cacheleft = 0
271 then accu
272 else
273 let ((_, w, h) as curr), rest, pdimno =
274 match l with
275 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
276 curr, rest, pdimno + 1
277 | _ ->
278 prev, l, pdimno
280 let pageno' = pageno + 1 in
281 if py + h > vy
282 then
283 let py' = vy - py in
284 let vh = h - py' in
285 if dy + vh > sh
286 then
287 let vh = sh - dy in
288 if vh <= 0
289 then
290 accu
291 else
292 let e =
293 { pageno = pageno
294 ; pagedimno = pdimno
295 ; pagew = w
296 ; pageh = h
297 ; pagedispy = dy
298 ; pagey = py'
299 ; pagevh = vh
302 e :: accu
303 else
304 let e =
305 { pageno = pageno
306 ; pagedimno = pdimno
307 ; pagew = w
308 ; pageh = h
309 ; pagedispy = dy
310 ; pagey = py'
311 ; pagevh = vh
314 let accu = e :: accu in
315 f pageno' pdimno curr
316 (vy + vh) (py + h) (dy + vh + 2) rest
317 (pred cacheleft) accu
318 else
319 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
321 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
322 state.maxy <- calcheight ();
323 List.rev accu
326 let clamp incr =
327 let y = state.y + incr in
328 let y = max 0 y in
329 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
333 let gotoy y =
334 let y = max 0 y in
335 let y = min state.maxy y in
336 let pages = layout y state.h in
337 state.y <- y;
338 state.layout <- pages;
339 if conf.redispimm
340 then
341 Glut.postRedisplay ()
345 let addnav () =
346 cbput state.navhist (yratio state.y);
347 cbrfollowlen state.navhist;
350 let getnav () =
351 let y = cbget state.navhist in
352 truncate (y *. float state.maxy)
355 let gotopage n top =
356 let y = getpagey n in
357 addnav ();
358 state.y <- y + top;
359 gotoy state.y;
362 let reshape ~w ~h =
363 let ratio = float w /. float state.w in
364 let fixbookmark (s, l, pageno, pagey) =
365 let pagey = truncate (float pagey *. ratio) in
366 (s, l, pageno, pagey)
368 state.bookmarks <- List.map fixbookmark state.bookmarks;
369 state.w <- w;
370 state.h <- h;
371 GlDraw.viewport 0 0 w h;
372 GlMat.mode `modelview;
373 GlMat.load_identity ();
374 GlMat.mode `projection;
375 GlMat.load_identity ();
376 GlMat.rotate ~x:1.0 ~angle:180.0 ();
377 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
378 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
379 GlClear.color (1., 1., 1.);
380 GlClear.clear [`color];
381 state.layout <- [];
382 state.pages <- [];
383 state.rects <- [];
384 state.text <- "";
385 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
388 let showtext c s =
389 if not conf.titletext
390 then (
391 GlDraw.color (0.0, 0.0, 0.0);
392 GlDraw.rect
393 (0.0, float (state.h - 18))
394 (float (state.w - conf.scrollw - 1), float state.h)
396 let font = Glut.BITMAP_8_BY_13 in
397 GlDraw.color (1.0, 1.0, 1.0);
398 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
399 Glut.bitmapCharacter ~font ~c:(Char.code c);
400 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
402 else
403 let len = String.length s in
404 let dst = String.create (len + 1) in
405 dst.[0] <- c;
406 StringLabels.blit
407 ~src:s
408 ~dst
409 ~src_pos:0
410 ~dst_pos:1
411 ~len
413 Glut.setWindowTitle ~title:dst
416 let enttext () =
417 let len = String.length state.text in
418 match state.textentry with
419 | None ->
420 if len > 0 then showtext ' ' state.text
422 | Some (c, text, _, _) ->
423 let s =
424 if len > 0
425 then
426 text ^ " [" ^ state.text ^ "]"
427 else
428 text
430 showtext c s;
433 let act cmd =
434 match cmd.[0] with
435 | 'c' ->
436 state.pages <- []
438 | 'd' ->
439 Glut.postRedisplay ()
441 | 'C' ->
442 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
443 state.pagecount <- n;
444 let rely = yratio state.y in
445 let maxy = calcheight () in
446 state.y <- truncate (float maxy *. rely);
447 let pages = layout state.y state.h in
448 state.layout <- pages;
449 Glut.postRedisplay ();
451 | 'T' ->
452 let s = Scanf.sscanf cmd "T %n"
453 (fun n -> String.sub cmd n (String.length cmd - n))
455 state.text <- s;
456 showtext ' ' s;
457 Glut.swapBuffers ();
458 (* Glut.postRedisplay () *)
460 | 'F' ->
461 let pageno, c, x0, y0, x1, y1 =
462 Scanf.sscanf cmd "F %d %d %f %f %f %f"
463 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
465 let y = (getpagey pageno) + truncate y0 in
466 addnav ();
467 gotoy y;
468 state.rects <- [pageno, c, (x0, y0), (x1, y1)]
470 | 'R' ->
471 let pageno, c, x0, y0, x1, y1 =
472 Scanf.sscanf cmd "R %d %d %f %f %f %f"
473 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
475 state.rects <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects
477 | 'r' ->
478 let n, w, h, p =
479 Scanf.sscanf cmd "r %d %d %d %s"
480 (fun n w h p -> (n, w, h, p))
482 Hashtbl.replace state.pagemap (n, w) p;
483 let evicted = cbpeekw state.pagecache in
484 if String.length evicted > 0
485 then begin
486 wcmd "free" [`s evicted];
487 let l = Hashtbl.fold (fun k p a ->
488 if evicted = p then k :: a else a) state.pagemap []
490 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
491 end;
492 cbput state.pagecache p;
493 state.inflight <- pred state.inflight;
494 Glut.postRedisplay ()
496 | 'l' ->
497 let (n, w, h) as pagelayout =
498 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
500 state.pages <- pagelayout :: state.pages
502 | 'o' ->
503 let (l, n, t, pos) =
504 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
506 let s = String.sub cmd pos (String.length cmd - pos) in
507 let outline = (s, l, n, t) in
508 let outlines =
509 match state.outlines with
510 | Olist outlines -> Olist (outline :: outlines)
511 | Oarray _ -> Olist [outline]
513 state.outlines <- outlines
515 | _ ->
516 log "unknown cmd `%S'" cmd
519 let getopaque pageno =
520 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
521 with Not_found -> None
524 let cache pageno opaque =
525 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
528 let validopaque opaque = String.length opaque > 0;;
530 let preload l =
531 match getopaque l.pageno with
532 | Some opaque when validopaque opaque ->
533 preload opaque
535 | None when state.inflight < 2+0*(cblen state.pagecache) ->
536 state.inflight <- succ state.inflight;
537 cache l.pageno "";
538 wcmd "render" [`i (l.pageno + 1)
539 ;`i l.pagedimno
540 ;`i l.pagew
541 ;`i l.pageh];
543 | _ -> ()
546 let idle () =
547 if not conf.redispimm && state.y != state.prevy
548 then (
549 state.prevy <- state.y;
550 Glut.postRedisplay ();
552 else
553 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
555 begin match r with
556 | [] ->
557 if conf.preload then begin
558 let h = state.h in
559 let y = if state.y < state.h then 0 else state.y - state.h in
560 let pages = layout y (h*3) in
561 List.iter preload pages;
562 end;
564 | _ ->
565 let cmd = readcmd state.csock in
566 act cmd;
567 end;
570 let search pattern forward =
571 if String.length pattern > 0
572 then
573 let pn, py =
574 match state.layout with
575 | [] -> 0, 0
576 | l :: _ ->
577 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
579 let cmd =
580 let b = makecmd "search"
581 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
583 Buffer.add_char b ',';
584 Buffer.add_string b pattern;
585 Buffer.add_char b '\000';
586 Buffer.contents b;
588 writecmd state.csock cmd;
591 let intentry text key =
592 let c = Char.unsafe_chr key in
593 match c with
594 | '0' .. '9' ->
595 let s = "x" in s.[0] <- c;
596 let text = text ^ s in
597 TEcont text
599 | _ ->
600 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
601 TEcont text
604 let addchar s c =
605 let b = Buffer.create (String.length s + 1) in
606 Buffer.add_string b s;
607 Buffer.add_char b c;
608 Buffer.contents b;
611 let textentry text key =
612 let c = Char.unsafe_chr key in
613 match c with
614 | _ when key >= 32 && key <= 127 ->
615 let text = addchar text c in
616 TEcont text
618 | _ ->
619 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
620 TEcont text
623 let optentry text key =
624 let btos b = if b then "on" else "off" in
625 let c = Char.unsafe_chr key in
626 match c with
627 | 'r' ->
628 conf.rectsel <- not conf.rectsel;
629 TEdone ("rectsel " ^ (btos conf.rectsel))
631 | 'i' ->
632 conf.icase <- not conf.icase;
633 TEdone ("case insensitive search " ^ (btos conf.icase))
635 | 'p' ->
636 conf.preload <- not conf.preload;
637 TEdone ("preload " ^ (btos conf.preload))
639 | 't' ->
640 conf.titletext <- not conf.titletext;
641 TEdone ("titletext " ^ (btos conf.titletext))
643 | 'd' ->
644 conf.redispimm <- not conf.redispimm;
645 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
647 | 'v' ->
648 conf.verbose <- not conf.verbose;
649 TEdone ("verbose " ^ (btos conf.verbose))
651 | 'h' ->
652 conf.maxhfit <- not conf.maxhfit;
653 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
654 TEdone ("maxhfit " ^ (btos conf.maxhfit))
656 | _ ->
657 state.text <- Printf.sprintf "bad option %d `%c'" key c;
658 TEstop
661 let maxoutlinerows () = (state.h - 31) / 16;;
663 let enterselector outlines errmsg =
664 if Array.length outlines = 0
665 then (
666 showtext ' ' errmsg;
667 Glut.swapBuffers ()
669 else
670 let pageno =
671 match state.layout with
672 | [] -> -1
673 | {pageno=pageno} :: rest -> pageno
675 let active =
676 let rec loop n =
677 if n = Array.length outlines
678 then 0
679 else
680 let (_, _, outlinepageno, _) = outlines.(n) in
681 if outlinepageno >= pageno then n else loop (n+1)
683 loop 0
685 state.outline <-
686 Some (active, max 0 (active - maxoutlinerows ()), outlines, "");
687 Glut.postRedisplay ();
690 let enteroutlinemode () =
691 let outlines =
692 match state.outlines with
693 | Oarray a -> a
694 | Olist l ->
695 let a = Array.of_list (List.rev l) in
696 state.outlines <- Oarray a;
699 enterselector outlines "Documents has no outline";
702 let enterbookmarkmode () =
703 let bookmarks = Array.of_list state.bookmarks in
704 enterselector bookmarks "Documents has no bookmarks (yet)";
707 let viewkeyboard ~key ~x ~y =
708 let enttext te =
709 state.textentry <- te;
710 state.text <- "";
711 enttext ();
712 Glut.swapBuffers ()
714 match state.textentry with
715 | None ->
716 let c = Char.chr key in
717 begin match c with
718 | '\027' | 'q' ->
719 exit 0
721 | '\008' ->
722 let y = getnav () in
723 gotoy y
725 | 'o' ->
726 enteroutlinemode ()
728 | 'u' ->
729 state.rects <- [];
730 state.text <- "";
731 Glut.postRedisplay ()
733 | '/' | '?' ->
734 let ondone isforw s =
735 state.searchpattern <- s;
736 search s isforw
738 enttext (Some (c, "", textentry, ondone (c ='/')))
740 | '+' ->
741 let ondone s =
742 let n =
743 try int_of_string s with exc ->
744 state.text <- Printf.sprintf "bad integer `%s': %s"
745 s (Printexc.to_string exc);
746 max_int
748 if n != max_int
749 then (
750 conf.pagebias <- n;
751 state.text <- "page bias is now " ^ string_of_int n;
754 enttext (Some ('+', "", intentry, ondone))
756 | '-' ->
757 let ondone msg =
758 state.text <- msg;
760 enttext (Some ('-', "", optentry, ondone))
762 | '0' .. '9' ->
763 let ondone s =
764 let n =
765 try int_of_string s with exc ->
766 state.text <- Printf.sprintf "bad integer `%s': %s"
767 s (Printexc.to_string exc);
770 if n >= 0
771 then (
772 addnav ();
773 state.y <- y;
774 gotoy (getpagey (n + conf.pagebias - 1))
777 let pageentry text key =
778 match Char.unsafe_chr key with
779 | 'g' -> TEdone text
780 | _ -> intentry text key
782 let text = "x" in text.[0] <- c;
783 enttext (Some (':', text, pageentry, ondone))
785 | 'b' ->
786 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
787 reshape state.w state.h;
789 | 'f' ->
790 begin match state.fullscreen with
791 | None ->
792 state.fullscreen <- Some (state.w, state.h);
793 Glut.fullScreen ()
794 | Some (w, h) ->
795 state.fullscreen <- None;
796 Glut.reshapeWindow ~w ~h
799 | 'n' ->
800 search state.searchpattern true
802 | 'p' ->
803 search state.searchpattern false
805 | 't' ->
806 begin match state.layout with
807 | [] -> ()
808 | l :: _ ->
809 gotoy (state.y - l.pagey);
812 | ' ' | 'N' ->
813 begin match List.rev state.layout with
814 | [] -> ()
815 | l :: _ ->
816 gotoy (clamp (l.pageh - l.pagey))
819 | '\127' | 'P' ->
820 begin match state.layout with
821 | [] -> ()
822 | l :: _ ->
823 gotoy (clamp (-l.pageh));
826 | '=' ->
827 let f (fn, ln) l =
828 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
830 let fn, ln = List.fold_left f (-1, -1) state.layout in
831 let s =
832 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
833 let percent = (100. *. (float state.y /. float maxy)) in
834 if fn = ln
835 then
836 Printf.sprintf "Page %d of %d %.2f%%"
837 (fn+1) state.pagecount percent
838 else
839 Printf.sprintf
840 "Pages %d-%d of %d %.2f%%"
841 (fn+1) (ln+1) state.pagecount percent
843 showtext ' ' s;
844 Glut.swapBuffers ()
846 | 'w' ->
847 begin match state.layout with
848 | [] -> ()
849 | l :: _ ->
850 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
851 Glut.postRedisplay ();
854 | '\'' ->
855 enterbookmarkmode ()
857 | 'm' ->
858 let ondone s =
859 match state.layout with
860 | l :: _ ->
861 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
862 | _ -> ()
864 enttext (Some ('~', "", textentry, ondone))
867 | 'z' ->
868 begin match state.layout with
869 | l :: _ ->
870 let a = getpagewh l.pagedimno in
871 let w = truncate (a.(1) -. a.(0))
872 and h = truncate (a.(3) -. a.(0)) in
873 Glut.reshapeWindow (w + conf.scrollw) h;
874 Glut.postRedisplay ();
876 | [] -> ()
879 | _ ->
880 vlog "huh? %d %c" key (Char.chr key);
883 | Some (c, text, onkey, ondone) when key = 8 ->
884 let len = String.length text in
885 if len = 0 || len = 1
886 then (
887 state.textentry <- None;
888 Glut.postRedisplay ();
890 else (
891 let s = String.sub text 0 (len - 1) in
892 enttext (Some (c, s, onkey, ondone))
895 | Some (c, text, onkey, ondone) ->
896 begin match Char.unsafe_chr key with
897 | '\r' | '\n' ->
898 ondone text;
899 state.textentry <- None;
900 Glut.postRedisplay ()
902 | '\027' ->
903 state.textentry <- None;
904 Glut.postRedisplay ()
906 | _ ->
907 begin match onkey text key with
908 | TEdone text ->
909 state.textentry <- None;
910 ondone text;
911 Glut.postRedisplay ()
913 | TEcont text ->
914 enttext (Some (c, text, onkey, ondone));
916 | TEstop ->
917 state.textentry <- None;
918 Glut.postRedisplay ()
919 end;
920 end;
923 let outlinekeyboard ~key ~x ~y (active, first, outlines, qsearch) =
924 let search active pattern incr =
925 let re = Str.regexp_case_fold pattern in
926 let rec loop n =
927 if n = Array.length outlines || n = -1 then None else
928 let (s, _, _, _) = outlines.(n) in
930 (try ignore (Str.search_forward re s 0); true
931 with Not_found -> false)
932 then (
933 let maxrows = maxoutlinerows () in
934 if first > n
935 then Some (n, max 0 (n - maxrows))
936 else Some (n, max first (n - maxrows))
938 else loop (n + incr)
940 loop active
942 match key with
943 | 27 ->
944 if String.length qsearch = 0
945 then (
946 state.text <- "";
947 state.outline <- None;
948 Glut.postRedisplay ();
950 else (
951 state.text <- "";
952 state.outline <- Some (active, first, outlines, "");
953 Glut.postRedisplay ();
956 | 18 | 19 ->
957 let incr = if key = 18 then -1 else 1 in
958 let active, first =
959 match search (active + incr) qsearch incr with
960 | None -> active, first
961 | Some af -> af
963 state.outline <- Some (active, first, outlines, qsearch);
964 Glut.postRedisplay ();
966 | 8 ->
967 let len = String.length qsearch in
968 if len = 0
969 then ()
970 else (
971 if len = 1
972 then (
973 state.text <- "";
974 state.outline <- Some (active, first, outlines, "");
976 else
977 let qsearch = String.sub qsearch 0 (len - 1) in
978 state.text <- qsearch;
979 state.outline <- Some (active, first, outlines, qsearch);
981 Glut.postRedisplay ()
983 | 13 ->
984 let (_, _, n, t) = outlines.(active) in
985 gotopage n t;
986 state.text <- "";
987 state.outline <- None;
988 Glut.postRedisplay ();
990 | _ when key >= 32 && key <= 127 ->
991 let pattern = addchar qsearch (Char.chr key) in
992 let pattern, active, first =
993 match search active pattern 1 with
994 | None -> qsearch, active, first
995 | Some (active, first) -> (pattern, active, first)
997 state.text <- pattern;
998 state.outline <- Some (active, first, outlines, pattern);
999 Glut.postRedisplay ()
1001 | _ -> log "unknown key %d" key
1004 let keyboard ~key ~x ~y =
1005 match state.outline with
1006 | None -> viewkeyboard ~key ~x ~y
1007 | Some outline -> outlinekeyboard ~key ~x ~y outline
1010 let special ~key ~x ~y =
1011 match state.outline with
1012 | None ->
1013 let y =
1014 match key with
1015 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1016 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1017 | Glut.KEY_DOWN -> clamp conf.scrollincr
1018 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1019 | Glut.KEY_PAGE_DOWN -> clamp state.h
1020 | Glut.KEY_HOME -> addnav (); 0
1021 | Glut.KEY_END ->
1022 addnav ();
1023 state.maxy - (if conf.maxhfit then state.h else 0)
1024 | _ -> state.y
1026 state.text <- "";
1027 gotoy y
1029 | Some (active, first, outlines, qsearch) ->
1030 let maxrows = maxoutlinerows () in
1031 let navigate incr =
1032 let active = active + incr in
1033 let active = max 0 (min active (Array.length outlines - 1)) in
1034 let first =
1035 if active > first
1036 then
1037 let rows = active - first in
1038 if rows > maxrows then first + incr else first
1039 else active
1041 state.outline <- Some (active, first, outlines, qsearch);
1042 Glut.postRedisplay ()
1044 match key with
1045 | Glut.KEY_UP -> navigate ~-1
1046 | Glut.KEY_DOWN -> navigate 1
1047 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1048 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1050 | Glut.KEY_HOME ->
1051 state.outline <- Some (0, 0, outlines, qsearch);
1052 Glut.postRedisplay ()
1054 | Glut.KEY_END ->
1055 let active = Array.length outlines - 1 in
1056 let first = max 0 (active - maxrows) in
1057 state.outline <- Some (active, first, outlines, qsearch);
1058 Glut.postRedisplay ()
1060 | _ -> ()
1063 let drawplaceholder l =
1064 if true
1065 then (
1066 GlDraw.color (0.2, 0.2, 0.2);
1067 GlDraw.color (1.0, 1.0, 1.0);
1068 GlDraw.rect
1069 (0.0, float l.pagedispy)
1070 (float l.pagew, float (l.pagedispy + l.pagevh))
1072 let x = 0.0
1073 and y = float (l.pagedispy + l.pagevh - 5) in
1074 let font = Glut.BITMAP_8_BY_13 in
1075 GlDraw.color (0.0, 0.0, 0.0);
1076 GlPix.raster_pos ~x ~y ();
1077 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1078 ("Loading " ^ string_of_int l.pageno);
1080 else (
1081 GlDraw.begins `quads;
1082 GlDraw.vertex2 (0.0, float l.pagedispy);
1083 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1084 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1085 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1086 GlDraw.ends ();
1090 let now () = Unix.gettimeofday ();;
1092 let drawpage i l =
1093 begin match getopaque l.pageno with
1094 | Some opaque when validopaque opaque ->
1095 GlDraw.color (1.0, 1.0, 1.0);
1096 let a = now () in
1097 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1098 let b = now () in
1099 let d = b-.a in
1100 vlog "draw %f sec" d;
1102 | Some _ ->
1103 drawplaceholder l
1105 | None ->
1106 if state.inflight < cblen state.pagecache
1107 then (
1108 List.iter preload state.layout;
1110 else (
1111 drawplaceholder l;
1112 vlog "inflight %d" state.inflight;
1114 end;
1115 GlDraw.color (0.5, 0.5, 0.5);
1116 GlDraw.rect
1117 (0., float i)
1118 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1120 l.pagedispy + l.pagevh;
1123 let scrollindicator () =
1124 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1125 GlDraw.color (0.64 , 0.64, 0.64);
1126 GlDraw.rect
1127 (float (state.w - conf.scrollw), 0.)
1128 (float state.w, float state.h)
1130 GlDraw.color (0.0, 0.0, 0.0);
1131 let sh = (float (maxy + state.h) /. float state.h) in
1132 let sh = float state.h /. sh in
1133 let sh = max sh (float conf.scrollh) in
1135 let percent =
1136 if state.y = state.maxy
1137 then 1.0
1138 else float state.y /. float maxy
1140 let position = (float state.h -. sh) *. percent in
1142 let position =
1143 if position +. sh > float state.h
1144 then
1145 float state.h -. sh
1146 else
1147 position
1149 GlDraw.rect
1150 (float (state.w - conf.scrollw), position)
1151 (float state.w, position +. sh)
1155 let showsel () =
1156 match state.mstate with
1157 | Mnone ->
1160 | Msel ((x0, y0), (x1, y1)) ->
1161 let y0' = min y0 y1
1162 and y1 = max y0 y1 in
1163 let y0 = y0' in
1164 let f l =
1165 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1166 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1167 then
1168 match getopaque l.pageno with
1169 | Some opaque when validopaque opaque ->
1170 let oy = -l.pagey + l.pagedispy in
1171 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1172 | _ -> ()
1174 List.iter f state.layout
1177 let showrects () =
1178 Gl.enable `blend;
1179 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1180 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1181 List.iter
1182 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1183 List.iter (fun l ->
1184 if l.pageno = pageno
1185 then (
1186 let d = float (l.pagedispy - l.pagey) in
1187 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1188 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1190 ) state.layout
1191 ) state.rects
1193 Gl.disable `blend;
1196 let showoutline = function
1197 | None -> ()
1198 | Some (active, first, outlines, qsearch) ->
1199 Gl.enable `blend;
1200 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1201 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1202 GlDraw.rect (0., 0.) (float state.w, float state.h);
1203 Gl.disable `blend;
1205 GlDraw.color (1., 1., 1.);
1206 let font = Glut.BITMAP_9_BY_15 in
1207 let draw_string x y s =
1208 GlPix.raster_pos ~x ~y ();
1209 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1211 let rec loop row =
1212 if row = Array.length outlines || (row - first) * 16 > state.h
1213 then ()
1214 else (
1215 let (s, l, _, _) = outlines.(row) in
1216 let y = (row - first) * 16 in
1217 let x = 5 + 5*l in
1218 if row = active
1219 then (
1220 Gl.enable `blend;
1221 GlDraw.polygon_mode `both `line;
1222 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1223 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1224 GlDraw.rect (0., float (y + 1))
1225 (float (state.w - conf.scrollw - 1), float (y + 18));
1226 GlDraw.polygon_mode `both `fill;
1227 Gl.disable `blend;
1228 GlDraw.color (1., 1., 1.);
1230 draw_string (float x) (float (y + 16)) s;
1231 loop (row+1)
1234 loop first
1237 let display () =
1238 let lasty = List.fold_left drawpage 0 (state.layout) in
1239 GlDraw.color (0.5, 0.5, 0.5);
1240 GlDraw.rect
1241 (0., float lasty)
1242 (float (state.w - conf.scrollw), float state.h)
1244 showrects ();
1245 scrollindicator ();
1246 showsel ();
1247 showoutline state.outline;
1248 enttext ();
1249 Glut.swapBuffers ();
1252 let getlink x y =
1253 let rec f = function
1254 | l :: rest ->
1255 begin match getopaque l.pageno with
1256 | Some opaque when validopaque opaque ->
1257 let y = y - l.pagedispy in
1258 if y > 0
1259 then
1260 let y = l.pagey + y in
1261 match getlink opaque x y with
1262 | None -> f rest
1263 | some -> some
1264 else
1265 f rest
1266 | _ ->
1267 f rest
1269 | [] -> None
1271 f state.layout
1274 let checklink x y =
1275 let rec f = function
1276 | l :: rest ->
1277 begin match getopaque l.pageno with
1278 | Some opaque when validopaque opaque ->
1279 let y = y - l.pagedispy in
1280 if y > 0
1281 then
1282 let y = l.pagey + y in
1283 if checklink opaque x y then true else f rest
1284 else
1285 f rest
1286 | _ ->
1287 f rest
1289 | [] -> false
1291 f state.layout
1294 let mouse ~button ~bstate ~x ~y =
1295 match button with
1296 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1297 let incr =
1298 if n = 3
1299 then
1300 -conf.scrollincr
1301 else
1302 conf.scrollincr
1304 let incr = incr * 2 in
1305 let y = clamp incr in
1306 gotoy y
1308 | Glut.LEFT_BUTTON when state.outline = None ->
1309 let dest = if bstate = Glut.DOWN then getlink x y else None in
1310 begin match dest with
1311 | Some (pageno, top) ->
1312 gotopage pageno top
1314 | None ->
1315 if bstate = Glut.DOWN
1316 then (
1317 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1318 state.mstate <- Msel ((x, y), (x, y));
1319 Glut.postRedisplay ()
1321 else (
1322 Glut.setCursor Glut.CURSOR_RIGHT_ARROW;
1323 state.mstate <- Mnone;
1327 | _ ->
1330 let mouse ~button ~state ~x ~y = mouse button state x y;;
1332 let motion ~x ~y =
1333 if state.outline = None
1334 then
1335 match state.mstate with
1336 | Mnone -> ()
1337 | Msel (a, _) ->
1338 state.mstate <- Msel (a, (x, y));
1339 Glut.postRedisplay ()
1342 let pmotion ~x ~y =
1343 if state.outline = None
1344 then
1345 match state.mstate with
1346 | Mnone when (checklink x y) ->
1347 Glut.setCursor Glut.CURSOR_INFO
1349 | Mnone ->
1350 Glut.setCursor Glut.CURSOR_RIGHT_ARROW
1352 | Msel (a, _) ->
1356 let () =
1357 let statepath = (Sys.getenv "HOME") ^ "/.config/llpp" in
1358 let pstate =
1360 let ic = open_in_bin statepath in
1361 let hash = input_value ic in
1362 close_in ic;
1363 hash
1364 with exn ->
1365 if false
1366 then
1367 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1369 Hashtbl.create 1
1371 let savestate () =
1373 Hashtbl.replace pstate state.path (state.bookmarks, state.w, state.h);
1374 let oc = open_out_bin statepath in
1375 output_value oc pstate
1376 with exn ->
1377 if false
1378 then
1379 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1382 let setstate () =
1384 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1385 state.w <- statew;
1386 state.h <- stateh;
1387 state.bookmarks <- statebookmarks;
1388 with exn ->
1389 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1392 Arg.parse [] (fun s -> state.path <- s) "options:";
1393 let name =
1394 if String.length state.path = 0
1395 then (prerr_endline "filename missing"; exit 1)
1396 else state.path
1399 setstate ();
1400 let _ = Glut.init Sys.argv in
1401 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1402 let () = Glut.initWindowSize state.w state.h in
1403 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1405 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1407 init ssock;
1408 state.csock <- csock;
1409 state.ssock <- ssock;
1410 writecmd csock ("open " ^ name ^ "\000");
1412 let () = Glut.displayFunc display in
1413 let () = Glut.reshapeFunc reshape in
1414 let () = Glut.keyboardFunc keyboard in
1415 let () = Glut.specialFunc special in
1416 let () = Glut.idleFunc (Some idle) in
1417 let () = Glut.mouseFunc mouse in
1418 let () = Glut.motionFunc motion in
1419 let () = Glut.passiveMotionFunc pmotion in
1421 at_exit savestate;
1423 let rec handlelablglutbug () =
1425 Glut.mainLoop ();
1426 with Glut.BadEnum "key in special_of_int" ->
1427 showtext '!' " LablGlut bug: special key not recognized";
1428 Glut.swapBuffers ();
1429 handlelablglutbug ()
1431 handlelablglutbug ();