Dim page view a little when waiting for users input
[llpp.git] / main.ml
blobb62c52463c7d70edecd544f2a6bd01bd21e2f5c6
1 let log fmt = Printf.kprintf prerr_endline fmt;;
2 let dolog fmt = Printf.kprintf prerr_endline fmt;;
4 external init : Unix.file_descr -> unit = "ml_init";;
5 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
6 external gettext : string -> (int * int * int * int) -> int -> bool -> unit =
7 "ml_gettext";;
8 external checklink : string -> int -> int -> bool = "ml_checklink";;
9 external getlink : string -> int -> int -> (int * int) option = "ml_getlink";;
10 external getpagewh : int -> float array = "ml_getpagewh";;
12 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
14 type textentry = char * string * (string -> int -> te) * (string -> unit)
15 and te =
16 | TEstop
17 | TEdone of string
18 | TEcont of string
19 | TEswitch of textentry
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 pagebias : int
83 ; mutable redispimm : bool
84 ; mutable verbose : bool
85 ; mutable scrollincr : int
86 ; mutable maxhfit : bool
87 ; mutable crophack : 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 rects1 : (int * int * Gl.point2 * Gl.point2) list
113 ; mutable text : string
114 ; mutable fullscreen : (int * int) option
115 ; mutable textentry : textentry option
116 ; mutable outlines : outlines
117 ; mutable outline : (bool * 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 ; pagebias = 0
130 ; redispimm = false
131 ; verbose = false
132 ; scrollincr = 24
133 ; maxhfit = true
134 ; crophack = false
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 ; rects1 = []
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 GlDraw.color (0.0, 0.0, 0.0);
390 GlDraw.rect
391 (0.0, float (state.h - 18))
392 (float (state.w - conf.scrollw - 1), float state.h)
394 let font = Glut.BITMAP_8_BY_13 in
395 GlDraw.color (1.0, 1.0, 1.0);
396 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
397 Glut.bitmapCharacter ~font ~c:(Char.code c);
398 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
401 let enttext () =
402 let len = String.length state.text in
403 match state.textentry with
404 | None ->
405 if len > 0 then showtext ' ' state.text
407 | Some (c, text, _, _) ->
408 let s =
409 if len > 0
410 then
411 text ^ " [" ^ state.text ^ "]"
412 else
413 text
415 showtext c s;
418 let act cmd =
419 match cmd.[0] with
420 | 'c' ->
421 state.pages <- [];
422 state.outlines <- Olist []
424 | 'D' ->
425 state.rects <- state.rects1;
426 Glut.postRedisplay ()
428 | 'd' ->
429 state.rects <- state.rects1;
430 Glut.postRedisplay ()
432 | 'C' ->
433 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
434 state.pagecount <- n;
435 let rely = yratio state.y in
436 let maxy = calcheight () in
437 state.y <- truncate (float maxy *. rely);
438 let pages = layout state.y state.h in
439 state.layout <- pages;
440 Glut.postRedisplay ();
442 | 't' ->
443 let s = Scanf.sscanf cmd "t %n"
444 (fun n -> String.sub cmd n (String.length cmd - n))
446 Glut.setWindowTitle s
448 | 'T' ->
449 let s = Scanf.sscanf cmd "T %n"
450 (fun n -> String.sub cmd n (String.length cmd - n))
452 state.text <- s;
453 showtext ' ' s;
454 Glut.swapBuffers ();
456 | 'V' ->
457 if conf.verbose
458 then
459 let s = Scanf.sscanf cmd "V %n"
460 (fun n -> String.sub cmd n (String.length cmd - n))
462 state.text <- s;
463 showtext ' ' s;
464 Glut.swapBuffers ();
466 | 'F' ->
467 let pageno, c, x0, y0, x1, y1 =
468 Scanf.sscanf cmd "F %d %d %f %f %f %f"
469 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
471 let y = (getpagey pageno) + truncate y0 in
472 addnav ();
473 gotoy y;
474 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
476 | 'R' ->
477 let pageno, c, x0, y0, x1, y1 =
478 Scanf.sscanf cmd "R %d %d %f %f %f %f"
479 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
481 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
483 | 'r' ->
484 let n, w, h, p =
485 Scanf.sscanf cmd "r %d %d %d %s"
486 (fun n w h p -> (n, w, h, p))
488 Hashtbl.replace state.pagemap (n, w) p;
489 let evicted = cbpeekw state.pagecache in
490 if String.length evicted > 0
491 then begin
492 wcmd "free" [`s evicted];
493 let l = Hashtbl.fold (fun k p a ->
494 if evicted = p then k :: a else a) state.pagemap []
496 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
497 end;
498 cbput state.pagecache p;
499 state.inflight <- pred state.inflight;
500 Glut.postRedisplay ()
502 | 'l' ->
503 let (n, w, h) as pagelayout =
504 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
506 state.pages <- pagelayout :: state.pages
508 | 'o' ->
509 let (l, n, t, pos) =
510 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
512 let s = String.sub cmd pos (String.length cmd - pos) in
513 let outline = (s, l, n, t) in
514 let outlines =
515 match state.outlines with
516 | Olist outlines -> Olist (outline :: outlines)
517 | Oarray _ -> Olist [outline]
519 state.outlines <- outlines
521 | _ ->
522 log "unknown cmd `%S'" cmd
525 let getopaque pageno =
526 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
527 with Not_found -> None
530 let cache pageno opaque =
531 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
534 let validopaque opaque = String.length opaque > 0;;
536 let preload l =
537 match getopaque l.pageno with
538 | None when state.inflight < 2+0*(cblen state.pagecache) ->
539 state.inflight <- succ state.inflight;
540 cache l.pageno "";
541 wcmd "render" [`i (l.pageno + 1)
542 ;`i l.pagedimno
543 ;`i l.pagew
544 ;`i l.pageh];
546 | _ -> ()
549 let idle () =
550 if not conf.redispimm && state.y != state.prevy
551 then (
552 state.prevy <- state.y;
553 Glut.postRedisplay ();
555 else
556 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
558 begin match r with
559 | [] ->
560 if conf.preload then begin
561 let h = state.h in
562 let y = if state.y < state.h then 0 else state.y - state.h in
563 let pages = layout y (h*3) in
564 List.iter preload pages;
565 end;
567 | _ ->
568 let cmd = readcmd state.csock in
569 act cmd;
570 end;
573 let search pattern forward =
574 if String.length pattern > 0
575 then
576 let pn, py =
577 match state.layout with
578 | [] -> 0, 0
579 | l :: _ ->
580 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
582 let cmd =
583 let b = makecmd "search"
584 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
586 Buffer.add_char b ',';
587 Buffer.add_string b pattern;
588 Buffer.add_char b '\000';
589 Buffer.contents b;
591 writecmd state.csock cmd;
594 let intentry text key =
595 let c = Char.unsafe_chr key in
596 match c with
597 | '0' .. '9' ->
598 let s = "x" in s.[0] <- c;
599 let text = text ^ s in
600 TEcont text
602 | _ ->
603 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
604 TEcont text
607 let addchar s c =
608 let b = Buffer.create (String.length s + 1) in
609 Buffer.add_string b s;
610 Buffer.add_char b c;
611 Buffer.contents b;
614 let textentry text key =
615 let c = Char.unsafe_chr key in
616 match c with
617 | _ when key >= 32 && key < 127 ->
618 let text = addchar text c in
619 TEcont text
621 | _ ->
622 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
623 TEcont text
626 let optentry text key =
627 let btos b = if b then "on" else "off" in
628 let c = Char.unsafe_chr key in
629 match c with
630 | 'r' ->
631 conf.rectsel <- not conf.rectsel;
632 TEdone ("rectsel " ^ (btos conf.rectsel))
634 | 's' ->
635 let ondone s =
636 try conf.scrollincr <- int_of_string s with exc ->
637 state.text <- Printf.sprintf "bad integer `%s': %s"
638 s (Printexc.to_string exc)
640 TEswitch ('#', "", intentry, ondone)
642 | 'i' ->
643 conf.icase <- not conf.icase;
644 TEdone ("case insensitive search " ^ (btos conf.icase))
646 | 'p' ->
647 conf.preload <- not conf.preload;
648 TEdone ("preload " ^ (btos conf.preload))
650 | 'd' ->
651 conf.redispimm <- not conf.redispimm;
652 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
654 | 'v' ->
655 conf.verbose <- not conf.verbose;
656 TEdone ("verbose " ^ (btos conf.verbose))
658 | 'h' ->
659 conf.maxhfit <- not conf.maxhfit;
660 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
661 TEdone ("maxhfit " ^ (btos conf.maxhfit))
663 | 'c' ->
664 conf.crophack <- not conf.crophack;
665 TEdone ("crophack " ^ btos conf.crophack)
667 | _ ->
668 state.text <- Printf.sprintf "bad option %d `%c'" key c;
669 TEstop
672 let maxoutlinerows () = (state.h - 31) / 16;;
674 let enterselector allowdel outlines errmsg =
675 if Array.length outlines = 0
676 then (
677 showtext ' ' errmsg;
678 Glut.swapBuffers ()
680 else
681 let pageno =
682 match state.layout with
683 | [] -> -1
684 | {pageno=pageno} :: rest -> pageno
686 let active =
687 let rec loop n =
688 if n = Array.length outlines
689 then 0
690 else
691 let (_, _, outlinepageno, _) = outlines.(n) in
692 if outlinepageno >= pageno then n else loop (n+1)
694 loop 0
696 state.outline <-
697 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
698 Glut.postRedisplay ();
701 let enteroutlinemode () =
702 let outlines =
703 match state.outlines with
704 | Oarray a -> a
705 | Olist l ->
706 let a = Array.of_list (List.rev l) in
707 state.outlines <- Oarray a;
710 enterselector false outlines "Documents has no outline";
713 let enterbookmarkmode () =
714 let bookmarks = Array.of_list state.bookmarks in
715 enterselector true bookmarks "Documents has no bookmarks (yet)";
719 let quickbookmark ?title () =
720 match state.layout with
721 | [] -> ()
722 | l :: _ ->
723 let title =
724 match title with
725 | None ->
726 let sec = Unix.gettimeofday () in
727 let tm = Unix.localtime sec in
728 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
729 l.pageno
730 tm.Unix.tm_mday
731 tm.Unix.tm_mon
732 (tm.Unix.tm_year + 1900)
733 tm.Unix.tm_hour
734 tm.Unix.tm_min
735 | Some title -> title
737 state.bookmarks <-
738 (title, 0, l.pageno, l.pagey) :: state.bookmarks
741 let viewkeyboard ~key ~x ~y =
742 let enttext te =
743 state.textentry <- te;
744 state.text <- "";
745 enttext ();
746 Glut.postRedisplay ()
748 match state.textentry with
749 | None ->
750 let c = Char.chr key in
751 begin match c with
752 | '\027' | 'q' ->
753 exit 0
755 | '\008' ->
756 let y = getnav () in
757 gotoy y
759 | 'o' ->
760 enteroutlinemode ()
762 | 'u' ->
763 state.rects <- [];
764 state.text <- "";
765 Glut.postRedisplay ()
767 | '/' | '?' ->
768 let ondone isforw s =
769 state.searchpattern <- s;
770 search s isforw
772 enttext (Some (c, "", textentry, ondone (c ='/')))
774 | '+' ->
775 let ondone s =
776 let n =
777 try int_of_string s with exc ->
778 state.text <- Printf.sprintf "bad integer `%s': %s"
779 s (Printexc.to_string exc);
780 max_int
782 if n != max_int
783 then (
784 conf.pagebias <- n;
785 state.text <- "page bias is now " ^ string_of_int n;
788 enttext (Some ('+', "", intentry, ondone))
790 | '-' ->
791 let ondone msg =
792 state.text <- msg;
794 enttext (Some ('-', "", optentry, ondone))
796 | '0' .. '9' ->
797 let ondone s =
798 let n =
799 try int_of_string s with exc ->
800 state.text <- Printf.sprintf "bad integer `%s': %s"
801 s (Printexc.to_string exc);
804 if n >= 0
805 then (
806 addnav ();
807 state.y <- y;
808 gotoy (getpagey (n + conf.pagebias - 1))
811 let pageentry text key =
812 match Char.unsafe_chr key with
813 | 'g' -> TEdone text
814 | _ -> intentry text key
816 let text = "x" in text.[0] <- c;
817 enttext (Some (':', text, pageentry, ondone))
819 | 'b' ->
820 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
821 reshape state.w state.h;
823 | 'f' ->
824 begin match state.fullscreen with
825 | None ->
826 state.fullscreen <- Some (state.w, state.h);
827 Glut.fullScreen ()
828 | Some (w, h) ->
829 state.fullscreen <- None;
830 Glut.reshapeWindow ~w ~h
833 | 'n' ->
834 search state.searchpattern true
836 | 'p' ->
837 search state.searchpattern false
839 | 't' ->
840 begin match state.layout with
841 | [] -> ()
842 | l :: _ ->
843 gotoy (state.y - l.pagey);
846 | ' ' | 'N' ->
847 begin match List.rev state.layout with
848 | [] -> ()
849 | l :: _ ->
850 gotoy (clamp (l.pageh - l.pagey))
853 | '\127' | 'P' ->
854 begin match state.layout with
855 | [] -> ()
856 | l :: _ ->
857 gotoy (clamp (-l.pageh));
860 | '=' ->
861 let f (fn, ln) l =
862 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
864 let fn, ln = List.fold_left f (-1, -1) state.layout in
865 let s =
866 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
867 let percent =
868 if maxy <= 0
869 then 100.
870 else (100. *. (float state.y /. float maxy)) in
871 if fn = ln
872 then
873 Printf.sprintf "Page %d of %d %.2f%%"
874 (fn+1) state.pagecount percent
875 else
876 Printf.sprintf
877 "Pages %d-%d of %d %.2f%%"
878 (fn+1) (ln+1) state.pagecount percent
880 showtext ' ' s;
881 Glut.swapBuffers ()
883 | 'w' ->
884 begin match state.layout with
885 | [] -> ()
886 | l :: _ ->
887 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
888 Glut.postRedisplay ();
891 | '\'' ->
892 enterbookmarkmode ()
894 | 'm' ->
895 let ondone s =
896 match state.layout with
897 | l :: _ ->
898 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
899 | _ -> ()
901 enttext (Some ('~', "", textentry, ondone))
903 | '~' ->
904 quickbookmark ();
905 showtext ' ' "Quick bookmark added";
906 Glut.swapBuffers ()
908 | 'z' ->
909 begin match state.layout with
910 | l :: _ ->
911 let a = getpagewh l.pagedimno in
912 let w, h =
913 if conf.crophack
914 then
915 (truncate (1.8 *. (a.(1) -. a.(0))),
916 truncate (1.4 *. (a.(3) -. a.(0))))
917 else
918 (truncate (a.(1) -. a.(0)),
919 truncate (a.(3) -. a.(0)))
921 Glut.reshapeWindow (w + conf.scrollw) h;
922 Glut.postRedisplay ();
924 | [] -> ()
927 | _ ->
928 vlog "huh? %d %c" key (Char.chr key);
931 | Some (c, text, onkey, ondone) when key = 8 ->
932 let len = String.length text in
933 if len = 0 || len = 1
934 then (
935 state.textentry <- None;
936 Glut.postRedisplay ();
938 else (
939 let s = String.sub text 0 (len - 1) in
940 enttext (Some (c, s, onkey, ondone))
943 | Some (c, text, onkey, ondone) ->
944 begin match Char.unsafe_chr key with
945 | '\r' | '\n' ->
946 ondone text;
947 state.textentry <- None;
948 Glut.postRedisplay ()
950 | '\027' ->
951 state.textentry <- None;
952 Glut.postRedisplay ()
954 | _ ->
955 begin match onkey text key with
956 | TEdone text ->
957 state.textentry <- None;
958 ondone text;
959 Glut.postRedisplay ()
961 | TEcont text ->
962 enttext (Some (c, text, onkey, ondone));
964 | TEstop ->
965 state.textentry <- None;
966 Glut.postRedisplay ()
968 | TEswitch te ->
969 state.textentry <- Some te;
970 Glut.postRedisplay ()
971 end;
972 end;
975 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
976 let search active pattern incr =
977 let dosearch re =
978 let rec loop n =
979 if n = Array.length outlines || n = -1 then None else
980 let (s, _, _, _) = outlines.(n) in
982 (try ignore (Str.search_forward re s 0); true
983 with Not_found -> false)
984 then (
985 let maxrows = (min (Array.length outlines) (maxoutlinerows ())) / 2 in
986 if first > n
987 then Some (n, max 0 (n - maxrows))
988 else Some (n, max first (n - maxrows))
990 else loop (n + incr)
992 loop active
995 let re = Str.regexp_case_fold pattern in
996 dosearch re
997 with Failure s ->
998 state.text <- s;
999 None
1001 match key with
1002 | 27 ->
1003 if String.length qsearch = 0
1004 then (
1005 state.text <- "";
1006 state.outline <- None;
1007 Glut.postRedisplay ();
1009 else (
1010 state.text <- "";
1011 state.outline <- Some (allowdel, active, first, outlines, "");
1012 Glut.postRedisplay ();
1015 | 18 | 19 ->
1016 let incr = if key = 18 then -1 else 1 in
1017 let active, first =
1018 match search (active + incr) qsearch incr with
1019 | None ->
1020 state.text <- qsearch ^ " [not found]";
1021 active, first
1022 | Some af ->
1023 state.text <- qsearch;
1026 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1027 Glut.postRedisplay ();
1029 | 8 ->
1030 let len = String.length qsearch in
1031 if len = 0
1032 then ()
1033 else (
1034 if len = 1
1035 then (
1036 state.text <- "";
1037 state.outline <- Some (allowdel, active, first, outlines, "");
1039 else
1040 let qsearch = String.sub qsearch 0 (len - 1) in
1041 state.text <- qsearch;
1042 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1044 Glut.postRedisplay ()
1046 | 13 ->
1047 if active < Array.length outlines
1048 then (
1049 let (_, _, n, t) = outlines.(active) in
1050 gotopage n t;
1052 state.text <- "";
1053 if allowdel then state.bookmarks <- Array.to_list outlines;
1054 state.outline <- None;
1055 Glut.postRedisplay ();
1057 | _ when key >= 32 && key < 127 ->
1058 let pattern = addchar qsearch (Char.chr key) in
1059 let active, first =
1060 match search active pattern 1 with
1061 | None ->
1062 state.text <- pattern ^ " [not found]";
1063 active, first
1064 | Some (active, first) ->
1065 state.text <- pattern;
1066 active, first
1068 state.outline <- Some (allowdel, active, first, outlines, pattern);
1069 Glut.postRedisplay ()
1071 | 127 when allowdel ->
1072 let len = Array.length outlines - 1 in
1073 if len = 0
1074 then (
1075 state.outline <- None;
1076 state.bookmarks <- [];
1078 else (
1079 let bookmarks = Array.init len
1080 (fun i ->
1081 let i = if i >= active then i + 1 else i in
1082 outlines.(i)
1085 state.outline <-
1086 Some (allowdel,
1087 min active (len-1),
1088 min first (len-1),
1089 bookmarks, qsearch)
1092 Glut.postRedisplay ()
1094 | _ -> log "unknown key %d" key
1097 let keyboard ~key ~x ~y =
1098 match state.outline with
1099 | None -> viewkeyboard ~key ~x ~y
1100 | Some outline -> outlinekeyboard ~key ~x ~y outline
1103 let special ~key ~x ~y =
1104 match state.outline with
1105 | None ->
1106 let y =
1107 match key with
1108 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1109 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1110 | Glut.KEY_DOWN -> clamp conf.scrollincr
1111 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1112 | Glut.KEY_PAGE_DOWN -> clamp state.h
1113 | Glut.KEY_HOME -> addnav (); 0
1114 | Glut.KEY_END ->
1115 addnav ();
1116 state.maxy - (if conf.maxhfit then state.h else 0)
1117 | _ -> state.y
1119 state.text <- "";
1120 gotoy y
1122 | Some (allowdel, active, first, outlines, qsearch) ->
1123 let maxrows = maxoutlinerows () in
1124 let navigate incr =
1125 let active = active + incr in
1126 let active = max 0 (min active (Array.length outlines - 1)) in
1127 let first =
1128 if active > first
1129 then
1130 let rows = active - first in
1131 if rows > maxrows then first + incr else first
1132 else active
1134 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1135 Glut.postRedisplay ()
1137 match key with
1138 | Glut.KEY_UP -> navigate ~-1
1139 | Glut.KEY_DOWN -> navigate 1
1140 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1141 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1143 | Glut.KEY_HOME ->
1144 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1145 Glut.postRedisplay ()
1147 | Glut.KEY_END ->
1148 let active = Array.length outlines - 1 in
1149 let first = max 0 (active - maxrows) in
1150 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1151 Glut.postRedisplay ()
1153 | _ -> ()
1156 let drawplaceholder l =
1157 if true
1158 then (
1159 GlDraw.color (0.2, 0.2, 0.2);
1160 GlDraw.color (1.0, 1.0, 1.0);
1161 GlDraw.rect
1162 (0.0, float l.pagedispy)
1163 (float l.pagew, float (l.pagedispy + l.pagevh))
1165 let x = 0.0
1166 and y = float (l.pagedispy + 13) in
1167 let font = Glut.BITMAP_8_BY_13 in
1168 GlDraw.color (0.0, 0.0, 0.0);
1169 GlPix.raster_pos ~x ~y ();
1170 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1171 ("Loading " ^ string_of_int l.pageno);
1173 else (
1174 GlDraw.begins `quads;
1175 GlDraw.vertex2 (0.0, float l.pagedispy);
1176 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1177 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1178 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1179 GlDraw.ends ();
1183 let now () = Unix.gettimeofday ();;
1185 let drawpage i l =
1186 begin match getopaque l.pageno with
1187 | Some opaque when validopaque opaque ->
1188 if state.textentry = None
1189 then GlDraw.color (1.0, 1.0, 1.0)
1190 else GlDraw.color (0.4, 0.4, 0.4);
1191 let a = now () in
1192 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1193 let b = now () in
1194 let d = b-.a in
1195 vlog "draw %f sec" d;
1197 | Some _ ->
1198 drawplaceholder l
1200 | None ->
1201 drawplaceholder l;
1202 if state.inflight < cblen state.pagecache
1203 then (
1204 List.iter preload state.layout;
1206 else (
1207 vlog "inflight %d" state.inflight;
1209 end;
1210 GlDraw.color (0.5, 0.5, 0.5);
1211 GlDraw.rect
1212 (0., float i)
1213 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1215 l.pagedispy + l.pagevh;
1218 let scrollindicator () =
1219 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1220 GlDraw.color (0.64 , 0.64, 0.64);
1221 GlDraw.rect
1222 (float (state.w - conf.scrollw), 0.)
1223 (float state.w, float state.h)
1225 GlDraw.color (0.0, 0.0, 0.0);
1226 let sh = (float (maxy + state.h) /. float state.h) in
1227 let sh = float state.h /. sh in
1228 let sh = max sh (float conf.scrollh) in
1230 let percent =
1231 if state.y = state.maxy
1232 then 1.0
1233 else float state.y /. float maxy
1235 let position = (float state.h -. sh) *. percent in
1237 let position =
1238 if position +. sh > float state.h
1239 then
1240 float state.h -. sh
1241 else
1242 position
1244 GlDraw.rect
1245 (float (state.w - conf.scrollw), position)
1246 (float state.w, position +. sh)
1250 let showsel () =
1251 match state.mstate with
1252 | Mnone ->
1255 | Msel ((x0, y0), (x1, y1)) ->
1256 let y0' = min y0 y1
1257 and y1 = max y0 y1 in
1258 let y0 = y0' in
1259 let f l =
1260 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1261 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1262 then
1263 match getopaque l.pageno with
1264 | Some opaque when validopaque opaque ->
1265 let oy = -l.pagey + l.pagedispy in
1266 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1267 | _ -> ()
1269 List.iter f state.layout
1272 let showrects () =
1273 Gl.enable `blend;
1274 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1275 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1276 List.iter
1277 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1278 List.iter (fun l ->
1279 if l.pageno = pageno
1280 then (
1281 let d = float (l.pagedispy - l.pagey) in
1282 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1283 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1285 ) state.layout
1286 ) state.rects
1288 Gl.disable `blend;
1291 let showoutline = function
1292 | None -> ()
1293 | Some (allowdel, active, first, outlines, qsearch) ->
1294 Gl.enable `blend;
1295 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1296 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1297 GlDraw.rect (0., 0.) (float state.w, float state.h);
1298 Gl.disable `blend;
1300 GlDraw.color (1., 1., 1.);
1301 let font = Glut.BITMAP_9_BY_15 in
1302 let draw_string x y s =
1303 GlPix.raster_pos ~x ~y ();
1304 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1306 let rec loop row =
1307 if row = Array.length outlines || (row - first) * 16 > state.h
1308 then ()
1309 else (
1310 let (s, l, _, _) = outlines.(row) in
1311 let y = (row - first) * 16 in
1312 let x = 5 + 5*l in
1313 if row = active
1314 then (
1315 Gl.enable `blend;
1316 GlDraw.polygon_mode `both `line;
1317 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1318 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1319 GlDraw.rect (0., float (y + 1))
1320 (float (state.w - conf.scrollw - 1), float (y + 18));
1321 GlDraw.polygon_mode `both `fill;
1322 Gl.disable `blend;
1323 GlDraw.color (1., 1., 1.);
1325 draw_string (float x) (float (y + 16)) s;
1326 loop (row+1)
1329 loop first
1332 let display () =
1333 let lasty = List.fold_left drawpage 0 (state.layout) in
1334 GlDraw.color (0.5, 0.5, 0.5);
1335 GlDraw.rect
1336 (0., float lasty)
1337 (float (state.w - conf.scrollw), float state.h)
1339 showrects ();
1340 scrollindicator ();
1341 showsel ();
1342 showoutline state.outline;
1343 enttext ();
1344 Glut.swapBuffers ();
1347 let getlink x y =
1348 let rec f = function
1349 | l :: rest ->
1350 begin match getopaque l.pageno with
1351 | Some opaque when validopaque opaque ->
1352 let y = y - l.pagedispy in
1353 if y > 0
1354 then
1355 let y = l.pagey + y in
1356 match getlink opaque x y with
1357 | None -> f rest
1358 | some -> some
1359 else
1360 f rest
1361 | _ ->
1362 f rest
1364 | [] -> None
1366 f state.layout
1369 let checklink x y =
1370 let rec f = function
1371 | l :: rest ->
1372 begin match getopaque l.pageno with
1373 | Some opaque when validopaque opaque ->
1374 let y = y - l.pagedispy in
1375 if y > 0
1376 then
1377 let y = l.pagey + y in
1378 if checklink opaque x y then true else f rest
1379 else
1380 f rest
1381 | _ ->
1382 f rest
1384 | [] -> false
1386 f state.layout
1389 let mouse ~button ~bstate ~x ~y =
1390 match button with
1391 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1392 let incr =
1393 if n = 3
1394 then
1395 -conf.scrollincr
1396 else
1397 conf.scrollincr
1399 let incr = incr * 2 in
1400 let y = clamp incr in
1401 gotoy y
1403 | Glut.LEFT_BUTTON when state.outline = None ->
1404 let dest = if bstate = Glut.DOWN then getlink x y else None in
1405 begin match dest with
1406 | Some (pageno, top) ->
1407 gotopage pageno top
1409 | None ->
1410 if bstate = Glut.DOWN
1411 then (
1412 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1413 state.mstate <- Msel ((x, y), (x, y));
1414 Glut.postRedisplay ()
1416 else (
1417 Glut.setCursor Glut.CURSOR_INHERIT;
1418 state.mstate <- Mnone;
1422 | _ ->
1425 let mouse ~button ~state ~x ~y = mouse button state x y;;
1427 let motion ~x ~y =
1428 if state.outline = None
1429 then
1430 match state.mstate with
1431 | Mnone -> ()
1432 | Msel (a, _) ->
1433 state.mstate <- Msel (a, (x, y));
1434 Glut.postRedisplay ()
1437 let pmotion ~x ~y =
1438 if state.outline = None
1439 then
1440 match state.mstate with
1441 | Mnone when (checklink x y) ->
1442 Glut.setCursor Glut.CURSOR_INFO
1444 | Mnone ->
1445 Glut.setCursor Glut.CURSOR_INHERIT
1447 | Msel (a, _) ->
1451 let () =
1452 let statepath =
1453 let home =
1454 if Sys.os_type = "Win32"
1455 then
1456 try Sys.getenv "HOMEPATH" with Not_found -> ""
1457 else
1458 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1460 Filename.concat home "llpp"
1462 let pstate =
1464 let ic = open_in_bin statepath in
1465 let hash = input_value ic in
1466 close_in ic;
1467 hash
1468 with exn ->
1469 if false
1470 then
1471 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1473 Hashtbl.create 1
1475 let savestate () =
1477 let w, h =
1478 match state.fullscreen with
1479 | None -> state.w, state.h
1480 | Some wh -> wh
1482 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1483 let oc = open_out_bin statepath in
1484 output_value oc pstate
1485 with exn ->
1486 if false
1487 then
1488 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1491 let setstate () =
1493 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1494 state.w <- statew;
1495 state.h <- stateh;
1496 state.bookmarks <- statebookmarks;
1497 with Not_found -> ()
1498 | exn ->
1499 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1502 Arg.parse [] (fun s -> state.path <- s) "options:";
1503 let name =
1504 if String.length state.path = 0
1505 then (prerr_endline "filename missing"; exit 1)
1506 else state.path
1509 setstate ();
1510 let _ = Glut.init Sys.argv in
1511 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1512 let () = Glut.initWindowSize state.w state.h in
1513 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1515 let csock, ssock =
1516 if Sys.os_type = "Unix"
1517 then
1518 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1519 else
1520 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1521 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1522 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1523 Unix.bind sock addr;
1524 Unix.listen sock 1;
1525 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1526 Unix.connect csock addr;
1527 let ssock, _ = Unix.accept sock in
1528 Unix.close sock;
1529 let opts sock =
1530 Unix.setsockopt sock Unix.TCP_NODELAY true;
1531 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1533 opts ssock;
1534 opts csock;
1535 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1536 ssock, csock
1539 let () = Glut.displayFunc display in
1540 let () = Glut.reshapeFunc reshape in
1541 let () = Glut.keyboardFunc keyboard in
1542 let () = Glut.specialFunc special in
1543 let () = Glut.idleFunc (Some idle) in
1544 let () = Glut.mouseFunc mouse in
1545 let () = Glut.motionFunc motion in
1546 let () = Glut.passiveMotionFunc pmotion in
1548 init ssock;
1549 state.csock <- csock;
1550 state.ssock <- ssock;
1551 writecmd csock ("open " ^ name ^ "\000");
1553 at_exit savestate;
1555 let rec handlelablglutbug () =
1557 Glut.mainLoop ();
1558 with Glut.BadEnum "key in special_of_int" ->
1559 showtext '!' " LablGlut bug: special key not recognized";
1560 Glut.swapBuffers ();
1561 handlelablglutbug ()
1563 handlelablglutbug ();