Better initial window mapping
[llpp.git] / main.ml
blob29a164ae4305b7ff18ad920b5499e5c9d6c38c5a
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;;
16 type te =
17 | TEstop
18 | TEdone of string
19 | TEcont of string
22 type 'a circbuf =
23 { store : 'a array
24 ; mutable rc : int
25 ; mutable wc : int
26 ; mutable len : int
30 let cbnew n v =
31 { store = Array.create n v
32 ; rc = 0
33 ; wc = 0
34 ; len = 0
38 let cblen b = Array.length b.store;;
40 let cbput b v =
41 let len = cblen b in
42 b.store.(b.wc) <- v;
43 b.wc <- (b.wc + 1) mod len;
44 b.len <- (b.len + 1) mod len;
47 let cbpeekw b = b.store.(b.wc);;
49 let cbget b =
50 let v = b.store.(b.rc) in
51 if b.len = 0
52 then
54 else (
55 let rc = if b.rc = 0 then b.len - 1 else b.rc - 1 in
56 b.rc <- rc;
61 let cbrfollowlen b =
62 b.rc <- b.len - 1;
65 type layout =
66 { pageno : int
67 ; pagedimno : int
68 ; pagew : int
69 ; pageh : int
70 ; pagedispy : int
71 ; pagey : int
72 ; pagevh : int
76 type conf =
77 { mutable scrollw : int
78 ; mutable scrollh : int
79 ; mutable rectsel : bool
80 ; mutable icase : bool
81 ; mutable preload : bool
82 ; mutable titletext : bool
83 ; mutable pagebias : int
84 ; mutable redispimm : bool
85 ; mutable verbose : bool
86 ; mutable scrollincr : int
87 ; mutable maxhfit : bool
91 type outline = string * int * int * int;;
92 type outlines = Oarray of outline array | Olist of outline list;;
94 type state =
95 { mutable csock : Unix.file_descr
96 ; mutable ssock : Unix.file_descr
97 ; mutable w : int
98 ; mutable h : int
99 ; mutable y : int
100 ; mutable prevy : int
101 ; mutable maxy : int
102 ; mutable layout : layout list
103 ; pagemap : ((int * int), string) Hashtbl.t
104 ; mutable pages : (int * int * int) list
105 ; mutable pagecount : int
106 ; pagecache : string circbuf
107 ; navhist : float circbuf
108 ; mutable inflight : int
109 ; mutable mstate : mstate
110 ; mutable searchpattern : string
111 ; mutable rects : (int * int * Gl.point2 * Gl.point2) list
112 ; mutable text : string
113 ; mutable fullscreen : (int * int) option
114 ; mutable textentry :
115 (char * string * (string -> int -> te) * (string -> unit)) option
116 ; mutable outlines : outlines
117 ; mutable outline : (int * int * outline array * string) option
118 ; mutable bookmarks : outline list
119 ; mutable path : string
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 = 900
142 ; h = 900
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 ; mstate = Mnone
153 ; navhist = cbnew 100 0.0
154 ; rects = []
155 ; text = ""
156 ; fullscreen = None
157 ; textentry = None
158 ; searchpattern = ""
159 ; outlines = Olist []
160 ; outline = None
161 ; bookmarks = []
162 ; path = ""
166 let vlog fmt =
167 if conf.verbose
168 then
169 Printf.kprintf prerr_endline fmt
170 else
171 Printf.kprintf ignore fmt
174 let writecmd fd s =
175 let len = String.length s in
176 let n = 4 + len in
177 let b = Buffer.create n in
178 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
179 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
180 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
181 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
182 Buffer.add_string b s;
183 let s' = Buffer.contents b in
184 let n' = Unix.write fd s' 0 n in
185 if n' != n then failwith "write failed";
188 let readcmd fd =
189 let s = "xxxx" in
190 let n = Unix.read fd s 0 4 in
191 if n != 4 then failwith "incomplete read(len)";
192 let len = 0
193 lor (Char.code s.[0] lsl 24)
194 lor (Char.code s.[1] lsl 16)
195 lor (Char.code s.[2] lsl 8)
196 lor (Char.code s.[3] lsl 0)
198 let s = String.create len in
199 let n = Unix.read fd s 0 len in
200 if n != len then failwith "incomplete read(data)";
204 let yratio y =
205 if y = state.maxy then 1.0
206 else float y /. float state.maxy
209 let makecmd s l =
210 let b = Buffer.create 10 in
211 Buffer.add_string b s;
212 let rec combine = function
213 | [] -> b
214 | x :: xs ->
215 Buffer.add_char b ' ';
216 let s =
217 match x with
218 | `b b -> if b then "1" else "0"
219 | `s s -> s
220 | `i i -> string_of_int i
221 | `f f -> string_of_float f
222 | `I f -> string_of_int (truncate f)
224 Buffer.add_string b s;
225 combine xs;
227 combine l;
230 let wcmd s l =
231 let cmd = Buffer.contents (makecmd s l) in
232 writecmd state.csock cmd;
235 let calcheight () =
236 let rec f pn ph fh l =
237 match l with
238 | (n, _, h) :: rest ->
239 let fh = fh + (n - pn) * ph in
240 f n h fh rest
242 | [] ->
243 let fh = fh + (ph * (state.pagecount - pn)) in
244 max 0 fh
246 let fh = f 0 0 0 state.pages in
250 let getpagey pageno =
251 let rec f pn ph y l =
252 match l with
253 | (n, _, h) :: rest ->
254 if n >= pageno
255 then
256 y + (pageno - pn) * ph
257 else
258 let y = y + (n - pn) * ph in
259 f n h y rest
261 | [] ->
262 y + (pageno - pn) * ph
264 f 0 0 0 state.pages;
267 let layout y sh =
268 let rec f pageno pdimno prev vy py dy l cacheleft accu =
269 if pageno = state.pagecount || cacheleft = 0
270 then accu
271 else
272 let ((_, w, h) as curr), rest, pdimno =
273 match l with
274 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
275 curr, rest, pdimno + 1
276 | _ ->
277 prev, l, pdimno
279 let pageno' = pageno + 1 in
280 if py + h > vy
281 then
282 let py' = vy - py in
283 let vh = h - py' in
284 if dy + vh > sh
285 then
286 let vh = sh - dy in
287 if vh <= 0
288 then
289 accu
290 else
291 let e =
292 { pageno = pageno
293 ; pagedimno = pdimno
294 ; pagew = w
295 ; pageh = h
296 ; pagedispy = dy
297 ; pagey = py'
298 ; pagevh = vh
301 e :: accu
302 else
303 let e =
304 { pageno = pageno
305 ; pagedimno = pdimno
306 ; pagew = w
307 ; pageh = h
308 ; pagedispy = dy
309 ; pagey = py'
310 ; pagevh = vh
313 let accu = e :: accu in
314 f pageno' pdimno curr
315 (vy + vh) (py + h) (dy + vh + 2) rest
316 (pred cacheleft) accu
317 else
318 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
320 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
321 state.maxy <- calcheight ();
322 List.rev accu
325 let clamp incr =
326 let y = state.y + incr in
327 let y = max 0 y in
328 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
332 let gotoy y =
333 let y = max 0 y in
334 let y = min state.maxy y in
335 let pages = layout y state.h in
336 state.y <- y;
337 state.layout <- pages;
338 if conf.redispimm
339 then
340 Glut.postRedisplay ()
344 let addnav () =
345 cbput state.navhist (yratio state.y);
346 cbrfollowlen state.navhist;
349 let getnav () =
350 let y = cbget state.navhist in
351 truncate (y *. float state.maxy)
354 let gotopage n top =
355 let y = getpagey n in
356 addnav ();
357 state.y <- y + top;
358 gotoy state.y;
361 let reshape ~w ~h =
362 let ratio = float w /. float state.w in
363 let fixbookmark (s, l, pageno, pagey) =
364 let pagey = truncate (float pagey *. ratio) in
365 (s, l, pageno, pagey)
367 state.bookmarks <- List.map fixbookmark state.bookmarks;
368 state.w <- w;
369 state.h <- h;
370 GlDraw.viewport 0 0 w h;
371 GlMat.mode `modelview;
372 GlMat.load_identity ();
373 GlMat.mode `projection;
374 GlMat.load_identity ();
375 GlMat.rotate ~x:1.0 ~angle:180.0 ();
376 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
377 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
378 GlClear.color (1., 1., 1.);
379 GlClear.clear [`color];
380 state.layout <- [];
381 state.pages <- [];
382 state.rects <- [];
383 state.text <- "";
384 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
387 let showtext c s =
388 if not conf.titletext
389 then (
390 GlDraw.color (0.0, 0.0, 0.0);
391 GlDraw.rect
392 (0.0, float (state.h - 18))
393 (float (state.w - conf.scrollw - 1), float state.h)
395 let font = Glut.BITMAP_8_BY_13 in
396 GlDraw.color (1.0, 1.0, 1.0);
397 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
398 Glut.bitmapCharacter ~font ~c:(Char.code c);
399 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
401 else
402 let len = String.length s in
403 let dst = String.create (len + 1) in
404 dst.[0] <- c;
405 StringLabels.blit
406 ~src:s
407 ~dst
408 ~src_pos:0
409 ~dst_pos:1
410 ~len
412 Glut.setWindowTitle ~title:dst
415 let enttext () =
416 let len = String.length state.text in
417 match state.textentry with
418 | None ->
419 if len > 0 then showtext ' ' state.text
421 | Some (c, text, _, _) ->
422 let s =
423 if len > 0
424 then
425 text ^ " [" ^ state.text ^ "]"
426 else
427 text
429 showtext c s;
432 let act cmd =
433 match cmd.[0] with
434 | 'c' ->
435 state.pages <- []
437 | 'd' ->
438 Glut.postRedisplay ()
440 | 'C' ->
441 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
442 state.pagecount <- n;
443 let rely = yratio state.y in
444 let maxy = calcheight () in
445 state.y <- truncate (float maxy *. rely);
446 let pages = layout state.y state.h in
447 state.layout <- pages;
448 Glut.postRedisplay ();
450 | 'T' ->
451 let s = Scanf.sscanf cmd "T %n"
452 (fun n -> String.sub cmd n (String.length cmd - n))
454 state.text <- s;
455 showtext ' ' s;
456 Glut.swapBuffers ();
457 (* Glut.postRedisplay () *)
459 | 'F' ->
460 let pageno, c, x0, y0, x1, y1 =
461 Scanf.sscanf cmd "F %d %d %f %f %f %f"
462 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
464 let y = (getpagey pageno) + truncate y0 in
465 addnav ();
466 gotoy y;
467 state.rects <- [pageno, c, (x0, y0), (x1, y1)]
469 | 'R' ->
470 let pageno, c, x0, y0, x1, y1 =
471 Scanf.sscanf cmd "R %d %d %f %f %f %f"
472 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
474 state.rects <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects
476 | 'r' ->
477 let n, w, h, p =
478 Scanf.sscanf cmd "r %d %d %d %s"
479 (fun n w h p -> (n, w, h, p))
481 Hashtbl.replace state.pagemap (n, w) p;
482 let evicted = cbpeekw state.pagecache in
483 if String.length evicted > 0
484 then begin
485 wcmd "free" [`s evicted];
486 let l = Hashtbl.fold (fun k p a ->
487 if evicted = p then k :: a else a) state.pagemap []
489 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
490 end;
491 cbput state.pagecache p;
492 state.inflight <- pred state.inflight;
493 Glut.postRedisplay ()
495 | 'l' ->
496 let (n, w, h) as pagelayout =
497 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
499 state.pages <- pagelayout :: state.pages
501 | 'o' ->
502 let (l, n, t, pos) =
503 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
505 let s = String.sub cmd pos (String.length cmd - pos) in
506 let outline = (s, l, n, t) in
507 let outlines =
508 match state.outlines with
509 | Olist outlines -> Olist (outline :: outlines)
510 | Oarray _ -> Olist [outline]
512 state.outlines <- outlines
514 | _ ->
515 log "unknown cmd `%S'" cmd
518 let getopaque pageno =
519 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
520 with Not_found -> None
523 let cache pageno opaque =
524 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
527 let validopaque opaque = String.length opaque > 0;;
529 let preload l =
530 match getopaque l.pageno with
531 | Some opaque when validopaque opaque ->
532 preload opaque
534 | None when state.inflight < 2+0*(cblen state.pagecache) ->
535 state.inflight <- succ state.inflight;
536 cache l.pageno "";
537 wcmd "render" [`i (l.pageno + 1)
538 ;`i l.pagedimno
539 ;`i l.pagew
540 ;`i l.pageh];
542 | _ -> ()
545 let idle () =
546 if not conf.redispimm && state.y != state.prevy
547 then (
548 state.prevy <- state.y;
549 Glut.postRedisplay ();
551 else
552 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
554 begin match r with
555 | [] ->
556 if conf.preload then begin
557 let h = state.h in
558 let y = if state.y < state.h then 0 else state.y - state.h in
559 let pages = layout y (h*3) in
560 List.iter preload pages;
561 end;
563 | _ ->
564 let cmd = readcmd state.csock in
565 act cmd;
566 end;
569 let search pattern forward =
570 if String.length pattern > 0
571 then
572 let pn, py =
573 match state.layout with
574 | [] -> 0, 0
575 | l :: _ ->
576 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
578 let cmd =
579 let b = makecmd "search"
580 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
582 Buffer.add_char b ',';
583 Buffer.add_string b pattern;
584 Buffer.add_char b '\000';
585 Buffer.contents b;
587 writecmd state.csock cmd;
590 let intentry text key =
591 let c = Char.unsafe_chr key in
592 match c with
593 | '0' .. '9' ->
594 let s = "x" in s.[0] <- c;
595 let text = text ^ s in
596 TEcont text
598 | _ ->
599 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
600 TEcont text
603 let addchar s c =
604 let b = Buffer.create (String.length s + 1) in
605 Buffer.add_string b s;
606 Buffer.add_char b c;
607 Buffer.contents b;
610 let textentry text key =
611 let c = Char.unsafe_chr key in
612 match c with
613 | _ when key >= 32 && key <= 127 ->
614 let text = addchar text c in
615 TEcont text
617 | _ ->
618 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
619 TEcont text
622 let optentry text key =
623 let btos b = if b then "on" else "off" in
624 let c = Char.unsafe_chr key in
625 match c with
626 | 'r' ->
627 conf.rectsel <- not conf.rectsel;
628 TEdone ("rectsel " ^ (btos conf.rectsel))
630 | 'i' ->
631 conf.icase <- not conf.icase;
632 TEdone ("case insensitive search " ^ (btos conf.icase))
634 | 'p' ->
635 conf.preload <- not conf.preload;
636 TEdone ("preload " ^ (btos conf.preload))
638 | 't' ->
639 conf.titletext <- not conf.titletext;
640 TEdone ("titletext " ^ (btos conf.titletext))
642 | 'd' ->
643 conf.redispimm <- not conf.redispimm;
644 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
646 | 'v' ->
647 conf.verbose <- not conf.verbose;
648 TEdone ("verbose " ^ (btos conf.verbose))
650 | 'h' ->
651 conf.maxhfit <- not conf.maxhfit;
652 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
653 TEdone ("maxhfit " ^ (btos conf.maxhfit))
655 | _ ->
656 state.text <- Printf.sprintf "bad option %d `%c'" key c;
657 TEstop
660 let maxoutlinerows () = (state.h - 31) / 16;;
662 let enterselector outlines errmsg =
663 if Array.length outlines = 0
664 then (
665 showtext ' ' errmsg;
666 Glut.swapBuffers ()
668 else
669 let pageno =
670 match state.layout with
671 | [] -> -1
672 | {pageno=pageno} :: rest -> pageno
674 let active =
675 let rec loop n =
676 if n = Array.length outlines
677 then 0
678 else
679 let (_, _, outlinepageno, _) = outlines.(n) in
680 if outlinepageno >= pageno then n else loop (n+1)
682 loop 0
684 state.outline <-
685 Some (active, max 0 (active - maxoutlinerows ()), outlines, "");
686 Glut.postRedisplay ();
689 let enteroutlinemode () =
690 let outlines =
691 match state.outlines with
692 | Oarray a -> a
693 | Olist l ->
694 let a = Array.of_list (List.rev l) in
695 state.outlines <- Oarray a;
698 enterselector outlines "Documents has no outline";
701 let enterbookmarkmode () =
702 let bookmarks = Array.of_list state.bookmarks in
703 enterselector bookmarks "Documents has no bookmarks (yet)";
706 let viewkeyboard ~key ~x ~y =
707 let enttext te =
708 state.textentry <- te;
709 state.text <- "";
710 enttext ();
711 Glut.swapBuffers ()
713 match state.textentry with
714 | None ->
715 let c = Char.chr key in
716 begin match c with
717 | '\027' | 'q' ->
718 exit 0
720 | '\008' ->
721 let y = getnav () in
722 gotoy y
724 | 'o' ->
725 enteroutlinemode ()
727 | 'u' ->
728 state.rects <- [];
729 state.text <- "";
730 Glut.postRedisplay ()
732 | '/' | '?' ->
733 let ondone isforw s =
734 state.searchpattern <- s;
735 search s isforw
737 enttext (Some (c, "", textentry, ondone (c ='/')))
739 | '+' ->
740 let ondone s =
741 let n =
742 try int_of_string s with exc ->
743 state.text <- Printf.sprintf "bad integer `%s': %s"
744 s (Printexc.to_string exc);
745 max_int
747 if n != max_int
748 then (
749 conf.pagebias <- n;
750 state.text <- "page bias is now " ^ string_of_int n;
753 enttext (Some ('+', "", intentry, ondone))
755 | '-' ->
756 let ondone msg =
757 state.text <- msg;
759 enttext (Some ('-', "", optentry, ondone))
761 | '0' .. '9' ->
762 let ondone s =
763 let n =
764 try int_of_string s with exc ->
765 state.text <- Printf.sprintf "bad integer `%s': %s"
766 s (Printexc.to_string exc);
769 if n >= 0
770 then (
771 addnav ();
772 state.y <- y;
773 gotoy (getpagey (n + conf.pagebias - 1))
776 let pageentry text key =
777 match Char.unsafe_chr key with
778 | 'g' -> TEdone text
779 | _ -> intentry text key
781 let text = "x" in text.[0] <- c;
782 enttext (Some (':', text, pageentry, ondone))
784 | 'b' ->
785 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
786 reshape state.w state.h;
788 | 'f' ->
789 begin match state.fullscreen with
790 | None ->
791 state.fullscreen <- Some (state.w, state.h);
792 Glut.fullScreen ()
793 | Some (w, h) ->
794 state.fullscreen <- None;
795 Glut.reshapeWindow ~w ~h
798 | 'n' ->
799 search state.searchpattern true
801 | 'p' ->
802 search state.searchpattern false
804 | 't' ->
805 begin match state.layout with
806 | [] -> ()
807 | l :: _ ->
808 gotoy (state.y - l.pagey);
811 | ' ' | 'N' ->
812 begin match List.rev state.layout with
813 | [] -> ()
814 | l :: _ ->
815 gotoy (clamp (l.pageh - l.pagey))
818 | '\127' | 'P' ->
819 begin match state.layout with
820 | [] -> ()
821 | l :: _ ->
822 gotoy (clamp (-l.pageh));
825 | '=' ->
826 let f (fn, ln) l =
827 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
829 let fn, ln = List.fold_left f (-1, -1) state.layout in
830 let s =
831 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
832 let percent = (100. *. (float state.y /. float maxy)) in
833 if fn = ln
834 then
835 Printf.sprintf "Page %d of %d %.2f%%"
836 (fn+1) state.pagecount percent
837 else
838 Printf.sprintf
839 "Pages %d-%d of %d %.2f%%"
840 (fn+1) (ln+1) state.pagecount percent
842 showtext ' ' s;
843 Glut.swapBuffers ()
845 | 'w' ->
846 begin match state.layout with
847 | [] -> ()
848 | l :: _ ->
849 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
850 Glut.postRedisplay ();
853 | '\'' ->
854 enterbookmarkmode ()
856 | 'm' ->
857 let ondone s =
858 match state.layout with
859 | l :: _ ->
860 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
861 | _ -> ()
863 enttext (Some ('~', "", textentry, ondone))
865 | _ ->
866 vlog "huh? %d %c" key (Char.chr key);
869 | Some (c, text, onkey, ondone) when key = 8 ->
870 let len = String.length text in
871 if len = 0 || len = 1
872 then (
873 state.textentry <- None;
874 Glut.postRedisplay ();
876 else (
877 let s = String.sub text 0 (len - 1) in
878 enttext (Some (c, s, onkey, ondone))
881 | Some (c, text, onkey, ondone) ->
882 begin match Char.unsafe_chr key with
883 | '\r' | '\n' ->
884 ondone text;
885 state.textentry <- None;
886 Glut.postRedisplay ()
888 | '\027' ->
889 state.textentry <- None;
890 Glut.postRedisplay ()
892 | _ ->
893 begin match onkey text key with
894 | TEdone text ->
895 state.textentry <- None;
896 ondone text;
897 Glut.postRedisplay ()
899 | TEcont text ->
900 enttext (Some (c, text, onkey, ondone));
902 | TEstop ->
903 state.textentry <- None;
904 Glut.postRedisplay ()
905 end;
906 end;
909 let outlinekeyboard ~key ~x ~y (active, first, outlines, qsearch) =
910 let search active pattern incr =
911 let re = Str.regexp_case_fold pattern in
912 let rec loop n =
913 if n = Array.length outlines || n = -1 then None else
914 let (s, _, _, _) = outlines.(n) in
916 (try ignore (Str.search_forward re s 0); true
917 with Not_found -> false)
918 then (
919 let maxrows = maxoutlinerows () in
920 if first > n
921 then Some (n, max 0 (n - maxrows))
922 else Some (n, max first (n - maxrows))
924 else loop (n + incr)
926 loop active
928 match key with
929 | 27 ->
930 if String.length qsearch = 0
931 then (
932 state.text <- "";
933 state.outline <- None;
934 Glut.postRedisplay ();
936 else (
937 state.text <- "";
938 state.outline <- Some (active, first, outlines, "");
939 Glut.postRedisplay ();
942 | 18 | 19 ->
943 let incr = if key = 18 then -1 else 1 in
944 let active, first =
945 match search (active + incr) qsearch incr with
946 | None -> active, first
947 | Some af -> af
949 state.outline <- Some (active, first, outlines, qsearch);
950 Glut.postRedisplay ();
952 | 8 ->
953 let len = String.length qsearch in
954 if len = 0
955 then ()
956 else (
957 if len = 1
958 then (
959 state.text <- "";
960 state.outline <- Some (active, first, outlines, "");
962 else
963 let qsearch = String.sub qsearch 0 (len - 1) in
964 state.text <- qsearch;
965 state.outline <- Some (active, first, outlines, qsearch);
967 Glut.postRedisplay ()
969 | 13 ->
970 let (_, _, n, t) = outlines.(active) in
971 gotopage n t;
972 state.text <- "";
973 state.outline <- None;
974 Glut.postRedisplay ();
976 | _ when key >= 32 && key <= 127 ->
977 let pattern = addchar qsearch (Char.chr key) in
978 let pattern, active, first =
979 match search active pattern 1 with
980 | None -> qsearch, active, first
981 | Some (active, first) -> (pattern, active, first)
983 state.text <- pattern;
984 state.outline <- Some (active, first, outlines, pattern);
985 Glut.postRedisplay ()
987 | _ -> log "unknown key %d" key
990 let keyboard ~key ~x ~y =
991 match state.outline with
992 | None -> viewkeyboard ~key ~x ~y
993 | Some outline -> outlinekeyboard ~key ~x ~y outline
996 let special ~key ~x ~y =
997 match state.outline with
998 | None ->
999 let y =
1000 match key with
1001 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1002 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1003 | Glut.KEY_DOWN -> clamp conf.scrollincr
1004 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1005 | Glut.KEY_PAGE_DOWN -> clamp state.h
1006 | Glut.KEY_HOME -> addnav (); 0
1007 | Glut.KEY_END ->
1008 addnav ();
1009 state.maxy - (if conf.maxhfit then state.h else 0)
1010 | _ -> state.y
1012 state.text <- "";
1013 gotoy y
1015 | Some (active, first, outlines, qsearch) ->
1016 let maxrows = maxoutlinerows () in
1017 let navigate incr =
1018 let active = active + incr in
1019 let active = max 0 (min active (Array.length outlines - 1)) in
1020 let first =
1021 if active > first
1022 then
1023 let rows = active - first in
1024 if rows > maxrows then first + incr else first
1025 else active
1027 state.outline <- Some (active, first, outlines, qsearch);
1028 Glut.postRedisplay ()
1030 match key with
1031 | Glut.KEY_UP -> navigate ~-1
1032 | Glut.KEY_DOWN -> navigate 1
1033 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1034 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1036 | Glut.KEY_HOME ->
1037 state.outline <- Some (0, 0, outlines, qsearch);
1038 Glut.postRedisplay ()
1040 | Glut.KEY_END ->
1041 let active = Array.length outlines - 1 in
1042 let first = max 0 (active - maxrows) in
1043 state.outline <- Some (active, first, outlines, qsearch);
1044 Glut.postRedisplay ()
1046 | _ -> ()
1049 let drawplaceholder l =
1050 if true
1051 then (
1052 GlDraw.color (0.2, 0.2, 0.2);
1053 GlDraw.color (1.0, 1.0, 1.0);
1054 GlDraw.rect
1055 (0.0, float l.pagedispy)
1056 (float l.pagew, float (l.pagedispy + l.pagevh))
1058 let x = 0.0
1059 and y = float (l.pagedispy + l.pagevh - 5) in
1060 let font = Glut.BITMAP_8_BY_13 in
1061 GlDraw.color (0.0, 0.0, 0.0);
1062 GlPix.raster_pos ~x ~y ();
1063 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1064 ("Loading " ^ string_of_int l.pageno);
1066 else (
1067 GlDraw.begins `quads;
1068 GlDraw.vertex2 (0.0, float l.pagedispy);
1069 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1070 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1071 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1072 GlDraw.ends ();
1076 let now () = Unix.gettimeofday ();;
1078 let drawpage i l =
1079 begin match getopaque l.pageno with
1080 | Some opaque when validopaque opaque ->
1081 GlDraw.color (1.0, 1.0, 1.0);
1082 let a = now () in
1083 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1084 let b = now () in
1085 let d = b-.a in
1086 vlog "draw %f sec" d;
1088 | Some _ ->
1089 drawplaceholder l
1091 | None ->
1092 if state.inflight < cblen state.pagecache
1093 then (
1094 List.iter preload state.layout;
1096 else (
1097 drawplaceholder l;
1098 vlog "inflight %d" state.inflight;
1100 end;
1101 GlDraw.color (0.5, 0.5, 0.5);
1102 GlDraw.rect
1103 (0., float i)
1104 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1106 l.pagedispy + l.pagevh;
1109 let scrollindicator () =
1110 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1111 GlDraw.color (0.64 , 0.64, 0.64);
1112 GlDraw.rect
1113 (float (state.w - conf.scrollw), 0.)
1114 (float state.w, float state.h)
1116 GlDraw.color (0.0, 0.0, 0.0);
1117 let sh = (float (maxy + state.h) /. float state.h) in
1118 let sh = float state.h /. sh in
1119 let sh = max sh (float conf.scrollh) in
1121 let percent =
1122 if state.y = state.maxy
1123 then 1.0
1124 else float state.y /. float maxy
1126 let position = (float state.h -. sh) *. percent in
1128 let position =
1129 if position +. sh > float state.h
1130 then
1131 float state.h -. sh
1132 else
1133 position
1135 GlDraw.rect
1136 (float (state.w - conf.scrollw), position)
1137 (float state.w, position +. sh)
1141 let showsel () =
1142 match state.mstate with
1143 | Mnone ->
1146 | Msel ((x0, y0), (x1, y1)) ->
1147 let y0' = min y0 y1
1148 and y1 = max y0 y1 in
1149 let y0 = y0' in
1150 let f l =
1151 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1152 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1153 then
1154 match getopaque l.pageno with
1155 | Some opaque when validopaque opaque ->
1156 let oy = -l.pagey + l.pagedispy in
1157 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1158 | _ -> ()
1160 List.iter f state.layout
1163 let showrects () =
1164 Gl.enable `blend;
1165 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1166 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1167 List.iter
1168 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1169 List.iter (fun l ->
1170 if l.pageno = pageno
1171 then (
1172 let d = float (l.pagedispy - l.pagey) in
1173 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1174 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1176 ) state.layout
1177 ) state.rects
1179 Gl.disable `blend;
1182 let showoutline = function
1183 | None -> ()
1184 | Some (active, first, outlines, qsearch) ->
1185 Gl.enable `blend;
1186 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1187 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1188 GlDraw.rect (0., 0.) (float state.w, float state.h);
1189 Gl.disable `blend;
1191 GlDraw.color (1., 1., 1.);
1192 let font = Glut.BITMAP_9_BY_15 in
1193 let draw_string x y s =
1194 GlPix.raster_pos ~x ~y ();
1195 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1197 let rec loop row =
1198 if row = Array.length outlines || (row - first) * 16 > state.h
1199 then ()
1200 else (
1201 let (s, l, _, _) = outlines.(row) in
1202 let y = (row - first) * 16 in
1203 let x = 5 + 5*l in
1204 if row = active
1205 then (
1206 Gl.enable `blend;
1207 GlDraw.polygon_mode `both `line;
1208 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1209 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1210 GlDraw.rect (0., float (y + 1))
1211 (float (state.w - conf.scrollw - 1), float (y + 18));
1212 GlDraw.polygon_mode `both `fill;
1213 Gl.disable `blend;
1214 GlDraw.color (1., 1., 1.);
1216 draw_string (float x) (float (y + 16)) s;
1217 loop (row+1)
1220 loop first
1223 let display () =
1224 let lasty = List.fold_left drawpage 0 (state.layout) in
1225 GlDraw.color (0.5, 0.5, 0.5);
1226 GlDraw.rect
1227 (0., float lasty)
1228 (float (state.w - conf.scrollw), float state.h)
1230 showrects ();
1231 scrollindicator ();
1232 showsel ();
1233 showoutline state.outline;
1234 enttext ();
1235 Glut.swapBuffers ();
1238 let getlink x y =
1239 let rec f = function
1240 | l :: rest ->
1241 begin match getopaque l.pageno with
1242 | Some opaque when validopaque opaque ->
1243 let y = y - l.pagedispy in
1244 if y > 0
1245 then
1246 let y = l.pagey + y in
1247 match getlink opaque x y with
1248 | None -> f rest
1249 | some -> some
1250 else
1251 f rest
1252 | _ ->
1253 f rest
1255 | [] -> None
1257 f state.layout
1260 let checklink x y =
1261 let rec f = function
1262 | l :: rest ->
1263 begin match getopaque l.pageno with
1264 | Some opaque when validopaque opaque ->
1265 let y = y - l.pagedispy in
1266 if y > 0
1267 then
1268 let y = l.pagey + y in
1269 if checklink opaque x y then true else f rest
1270 else
1271 f rest
1272 | _ ->
1273 f rest
1275 | [] -> false
1277 f state.layout
1280 let mouse ~button ~bstate ~x ~y =
1281 match button with
1282 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1283 let incr =
1284 if n = 3
1285 then
1286 -conf.scrollincr
1287 else
1288 conf.scrollincr
1290 let incr = incr * 2 in
1291 let y = clamp incr in
1292 gotoy y
1294 | Glut.LEFT_BUTTON when state.outline = None ->
1295 let dest = if bstate = Glut.DOWN then getlink x y else None in
1296 begin match dest with
1297 | Some (pageno, top) ->
1298 gotopage pageno top
1300 | None ->
1301 if bstate = Glut.DOWN
1302 then (
1303 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1304 state.mstate <- Msel ((x, y), (x, y));
1305 Glut.postRedisplay ()
1307 else (
1308 Glut.setCursor Glut.CURSOR_RIGHT_ARROW;
1309 state.mstate <- Mnone;
1313 | _ ->
1316 let mouse ~button ~state ~x ~y = mouse button state x y;;
1318 let motion ~x ~y =
1319 if state.outline = None
1320 then
1321 match state.mstate with
1322 | Mnone -> ()
1323 | Msel (a, _) ->
1324 state.mstate <- Msel (a, (x, y));
1325 Glut.postRedisplay ()
1328 let pmotion ~x ~y =
1329 if state.outline = None
1330 then
1331 match state.mstate with
1332 | Mnone when (checklink x y) ->
1333 Glut.setCursor Glut.CURSOR_INFO
1335 | Mnone ->
1336 Glut.setCursor Glut.CURSOR_RIGHT_ARROW
1338 | Msel (a, _) ->
1342 let () =
1343 let statepath = (Sys.getenv "HOME") ^ "/.config/llpp" in
1344 let pstate =
1346 let ic = open_in_bin statepath in
1347 let hash = input_value ic in
1348 close_in ic;
1349 hash
1350 with exn ->
1351 if false
1352 then
1353 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1355 Hashtbl.create 1
1357 let savestate () =
1359 Hashtbl.replace pstate state.path (state.bookmarks, state.w, state.h);
1360 let oc = open_out_bin statepath in
1361 output_value oc pstate
1362 with exn ->
1363 if false
1364 then
1365 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1368 let setstate () =
1370 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1371 state.w <- statew;
1372 state.h <- stateh;
1373 state.bookmarks <- statebookmarks;
1374 with exn ->
1375 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1378 Arg.parse [] (fun s -> state.path <- s) "options:";
1379 let name =
1380 if String.length state.path = 0
1381 then (prerr_endline "filename missing"; exit 1)
1382 else state.path
1385 setstate ();
1386 let _ = Glut.init Sys.argv in
1387 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1388 let () = Glut.initWindowSize state.w state.h in
1389 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1391 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1393 init ssock;
1394 state.csock <- csock;
1395 state.ssock <- ssock;
1396 writecmd csock ("open " ^ name ^ "\000");
1398 let () = Glut.displayFunc display in
1399 let () = Glut.reshapeFunc reshape in
1400 let () = Glut.keyboardFunc keyboard in
1401 let () = Glut.specialFunc special in
1402 let () = Glut.idleFunc (Some idle) in
1403 let () = Glut.mouseFunc mouse in
1404 let () = Glut.motionFunc motion in
1405 let () = Glut.passiveMotionFunc pmotion in
1407 at_exit savestate;
1409 let rec handlelablglutbug () =
1411 Glut.mainLoop ();
1412 with Glut.BadEnum "key in special_of_int" ->
1413 showtext '!' " LablGlut bug: special key not recognized";
1414 Glut.swapBuffers ();
1415 handlelablglutbug ()
1417 handlelablglutbug ();