Bump the select timeout
[llpp.git] / main.ml
blob35ce6aead6ac1a084e62034bca97f14e5a577c76
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 gettext : string -> (int * int * int * int) -> int -> bool -> unit =
9 "ml_gettext";;
10 external checklink : string -> int -> int -> bool = "ml_checklink";;
11 external getlink : string -> int -> int -> (int * int) option = "ml_getlink";;
12 external getpagewh : int -> float array = "ml_getpagewh";;
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
88 ; mutable markonquit : bool
89 ; mutable crophack : bool
93 type outline = string * int * int * int;;
94 type outlines = Oarray of outline array | Olist of outline list;;
96 type state =
97 { mutable csock : Unix.file_descr
98 ; mutable ssock : Unix.file_descr
99 ; mutable w : int
100 ; mutable h : int
101 ; mutable y : int
102 ; mutable prevy : int
103 ; mutable maxy : int
104 ; mutable layout : layout list
105 ; pagemap : ((int * int), string) Hashtbl.t
106 ; mutable pages : (int * int * int) list
107 ; mutable pagecount : int
108 ; pagecache : string circbuf
109 ; navhist : float circbuf
110 ; mutable inflight : int
111 ; mutable mstate : mstate
112 ; mutable searchpattern : string
113 ; mutable rects : (int * int * Gl.point2 * Gl.point2) list
114 ; mutable rects1 : (int * int * Gl.point2 * Gl.point2) list
115 ; mutable text : string
116 ; mutable fullscreen : (int * int) option
117 ; mutable textentry :
118 (char * string * (string -> int -> te) * (string -> unit)) option
119 ; mutable outlines : outlines
120 ; mutable outline : (bool * int * int * outline array * string) option
121 ; mutable bookmarks : outline list
122 ; mutable path : string
126 let conf =
127 { scrollw = 5
128 ; scrollh = 12
129 ; icase = true
130 ; rectsel = true
131 ; preload = false
132 ; titletext = false
133 ; pagebias = 0
134 ; redispimm = false
135 ; verbose = false
136 ; scrollincr = 24
137 ; maxhfit = true
138 ; markonquit = false
139 ; crophack = false
143 let state =
144 { csock = Unix.stdin
145 ; ssock = Unix.stdin
146 ; w = 900
147 ; h = 900
148 ; y = 0
149 ; prevy = 0
150 ; layout = []
151 ; maxy = max_int
152 ; pagemap = Hashtbl.create 10
153 ; pagecache = cbnew 10 ""
154 ; pages = []
155 ; pagecount = 0
156 ; inflight = 0
157 ; mstate = Mnone
158 ; navhist = cbnew 100 0.0
159 ; rects = []
160 ; rects1 = []
161 ; text = ""
162 ; fullscreen = None
163 ; textentry = None
164 ; searchpattern = ""
165 ; outlines = Olist []
166 ; outline = None
167 ; bookmarks = []
168 ; path = ""
172 let vlog fmt =
173 if conf.verbose
174 then
175 Printf.kprintf prerr_endline fmt
176 else
177 Printf.kprintf ignore fmt
180 let writecmd fd s =
181 let len = String.length s in
182 let n = 4 + len in
183 let b = Buffer.create n in
184 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
185 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
186 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
187 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
188 Buffer.add_string b s;
189 let s' = Buffer.contents b in
190 let n' = Unix.write fd s' 0 n in
191 if n' != n then failwith "write failed";
194 let readcmd fd =
195 let s = "xxxx" in
196 let n = Unix.read fd s 0 4 in
197 if n != 4 then failwith "incomplete read(len)";
198 let len = 0
199 lor (Char.code s.[0] lsl 24)
200 lor (Char.code s.[1] lsl 16)
201 lor (Char.code s.[2] lsl 8)
202 lor (Char.code s.[3] lsl 0)
204 let s = String.create len in
205 let n = Unix.read fd s 0 len in
206 if n != len then failwith "incomplete read(data)";
210 let yratio y =
211 if y = state.maxy then 1.0
212 else float y /. float state.maxy
215 let makecmd s l =
216 let b = Buffer.create 10 in
217 Buffer.add_string b s;
218 let rec combine = function
219 | [] -> b
220 | x :: xs ->
221 Buffer.add_char b ' ';
222 let s =
223 match x with
224 | `b b -> if b then "1" else "0"
225 | `s s -> s
226 | `i i -> string_of_int i
227 | `f f -> string_of_float f
228 | `I f -> string_of_int (truncate f)
230 Buffer.add_string b s;
231 combine xs;
233 combine l;
236 let wcmd s l =
237 let cmd = Buffer.contents (makecmd s l) in
238 writecmd state.csock cmd;
241 let calcheight () =
242 let rec f pn ph fh l =
243 match l with
244 | (n, _, h) :: rest ->
245 let fh = fh + (n - pn) * ph in
246 f n h fh rest
248 | [] ->
249 let fh = fh + (ph * (state.pagecount - pn)) in
250 max 0 fh
252 let fh = f 0 0 0 state.pages in
256 let getpagey pageno =
257 let rec f pn ph y l =
258 match l with
259 | (n, _, h) :: rest ->
260 if n >= pageno
261 then
262 y + (pageno - pn) * ph
263 else
264 let y = y + (n - pn) * ph in
265 f n h y rest
267 | [] ->
268 y + (pageno - pn) * ph
270 f 0 0 0 state.pages;
273 let layout y sh =
274 let rec f pageno pdimno prev vy py dy l cacheleft accu =
275 if pageno = state.pagecount || cacheleft = 0
276 then accu
277 else
278 let ((_, w, h) as curr), rest, pdimno =
279 match l with
280 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
281 curr, rest, pdimno + 1
282 | _ ->
283 prev, l, pdimno
285 let pageno' = pageno + 1 in
286 if py + h > vy
287 then
288 let py' = vy - py in
289 let vh = h - py' in
290 if dy + vh > sh
291 then
292 let vh = sh - dy in
293 if vh <= 0
294 then
295 accu
296 else
297 let e =
298 { pageno = pageno
299 ; pagedimno = pdimno
300 ; pagew = w
301 ; pageh = h
302 ; pagedispy = dy
303 ; pagey = py'
304 ; pagevh = vh
307 e :: accu
308 else
309 let e =
310 { pageno = pageno
311 ; pagedimno = pdimno
312 ; pagew = w
313 ; pageh = h
314 ; pagedispy = dy
315 ; pagey = py'
316 ; pagevh = vh
319 let accu = e :: accu in
320 f pageno' pdimno curr
321 (vy + vh) (py + h) (dy + vh + 2) rest
322 (pred cacheleft) accu
323 else
324 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
326 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
327 state.maxy <- calcheight ();
328 List.rev accu
331 let clamp incr =
332 let y = state.y + incr in
333 let y = max 0 y in
334 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
338 let gotoy y =
339 let y = max 0 y in
340 let y = min state.maxy y in
341 let pages = layout y state.h in
342 state.y <- y;
343 state.layout <- pages;
344 if conf.redispimm
345 then
346 Glut.postRedisplay ()
350 let addnav () =
351 cbput state.navhist (yratio state.y);
352 cbrfollowlen state.navhist;
355 let getnav () =
356 let y = cbget state.navhist in
357 truncate (y *. float state.maxy)
360 let gotopage n top =
361 let y = getpagey n in
362 addnav ();
363 state.y <- y + top;
364 gotoy state.y;
367 let reshape ~w ~h =
368 let ratio = float w /. float state.w in
369 let fixbookmark (s, l, pageno, pagey) =
370 let pagey = truncate (float pagey *. ratio) in
371 (s, l, pageno, pagey)
373 state.bookmarks <- List.map fixbookmark state.bookmarks;
374 state.w <- w;
375 state.h <- h;
376 GlDraw.viewport 0 0 w h;
377 GlMat.mode `modelview;
378 GlMat.load_identity ();
379 GlMat.mode `projection;
380 GlMat.load_identity ();
381 GlMat.rotate ~x:1.0 ~angle:180.0 ();
382 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
383 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
384 GlClear.color (1., 1., 1.);
385 GlClear.clear [`color];
386 state.layout <- [];
387 state.pages <- [];
388 state.rects <- [];
389 state.text <- "";
390 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
393 let showtext c s =
394 if not conf.titletext
395 then (
396 GlDraw.color (0.0, 0.0, 0.0);
397 GlDraw.rect
398 (0.0, float (state.h - 18))
399 (float (state.w - conf.scrollw - 1), float state.h)
401 let font = Glut.BITMAP_8_BY_13 in
402 GlDraw.color (1.0, 1.0, 1.0);
403 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
404 Glut.bitmapCharacter ~font ~c:(Char.code c);
405 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
407 else
408 let len = String.length s in
409 let dst = String.create (len + 1) in
410 dst.[0] <- c;
411 StringLabels.blit
412 ~src:s
413 ~dst
414 ~src_pos:0
415 ~dst_pos:1
416 ~len
418 Glut.setWindowTitle ~title:dst
421 let enttext () =
422 let len = String.length state.text in
423 match state.textentry with
424 | None ->
425 if len > 0 then showtext ' ' state.text
427 | Some (c, text, _, _) ->
428 let s =
429 if len > 0
430 then
431 text ^ " [" ^ state.text ^ "]"
432 else
433 text
435 showtext c s;
438 let act cmd =
439 match cmd.[0] with
440 | 'c' ->
441 state.pages <- [];
442 state.outlines <- Olist []
444 | 'D' ->
445 state.rects <- state.rects1;
446 Glut.postRedisplay ()
448 | 'd' ->
449 state.rects <- state.rects1;
450 Glut.postRedisplay ()
452 | 'C' ->
453 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
454 state.pagecount <- n;
455 let rely = yratio state.y in
456 let maxy = calcheight () in
457 state.y <- truncate (float maxy *. rely);
458 let pages = layout state.y state.h in
459 state.layout <- pages;
460 Glut.postRedisplay ();
462 | 't' ->
463 let s = Scanf.sscanf cmd "t %n"
464 (fun n -> String.sub cmd n (String.length cmd - n))
466 Glut.setWindowTitle s
468 | 'T' ->
469 let s = Scanf.sscanf cmd "T %n"
470 (fun n -> String.sub cmd n (String.length cmd - n))
472 state.text <- s;
473 showtext ' ' s;
474 Glut.swapBuffers ();
476 | 'V' ->
477 if conf.verbose
478 then
479 let s = Scanf.sscanf cmd "V %n"
480 (fun n -> String.sub cmd n (String.length cmd - n))
482 state.text <- s;
483 showtext ' ' s;
484 Glut.swapBuffers ();
486 | 'F' ->
487 let pageno, c, x0, y0, x1, y1 =
488 Scanf.sscanf cmd "F %d %d %f %f %f %f"
489 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
491 let y = (getpagey pageno) + truncate y0 in
492 addnav ();
493 gotoy y;
494 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
496 | 'R' ->
497 let pageno, c, x0, y0, x1, y1 =
498 Scanf.sscanf cmd "R %d %d %f %f %f %f"
499 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
501 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
503 | 'r' ->
504 let n, w, h, p =
505 Scanf.sscanf cmd "r %d %d %d %s"
506 (fun n w h p -> (n, w, h, p))
508 Hashtbl.replace state.pagemap (n, w) p;
509 let evicted = cbpeekw state.pagecache in
510 if String.length evicted > 0
511 then begin
512 wcmd "free" [`s evicted];
513 let l = Hashtbl.fold (fun k p a ->
514 if evicted = p then k :: a else a) state.pagemap []
516 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
517 end;
518 cbput state.pagecache p;
519 state.inflight <- pred state.inflight;
520 Glut.postRedisplay ()
522 | 'l' ->
523 let (n, w, h) as pagelayout =
524 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
526 state.pages <- pagelayout :: state.pages
528 | 'o' ->
529 let (l, n, t, pos) =
530 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
532 let s = String.sub cmd pos (String.length cmd - pos) in
533 let outline = (s, l, n, t) in
534 let outlines =
535 match state.outlines with
536 | Olist outlines -> Olist (outline :: outlines)
537 | Oarray _ -> Olist [outline]
539 state.outlines <- outlines
541 | _ ->
542 log "unknown cmd `%S'" cmd
545 let getopaque pageno =
546 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
547 with Not_found -> None
550 let cache pageno opaque =
551 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
554 let validopaque opaque = String.length opaque > 0;;
556 let preload l =
557 match getopaque l.pageno with
558 | None when state.inflight < 2+0*(cblen state.pagecache) ->
559 state.inflight <- succ state.inflight;
560 cache l.pageno "";
561 wcmd "render" [`i (l.pageno + 1)
562 ;`i l.pagedimno
563 ;`i l.pagew
564 ;`i l.pageh];
566 | _ -> ()
569 let idle () =
570 if not conf.redispimm && state.y != state.prevy
571 then (
572 state.prevy <- state.y;
573 Glut.postRedisplay ();
575 else
576 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
578 begin match r with
579 | [] ->
580 if conf.preload then begin
581 let h = state.h in
582 let y = if state.y < state.h then 0 else state.y - state.h in
583 let pages = layout y (h*3) in
584 List.iter preload pages;
585 end;
587 | _ ->
588 let cmd = readcmd state.csock in
589 act cmd;
590 end;
593 let search pattern forward =
594 if String.length pattern > 0
595 then
596 let pn, py =
597 match state.layout with
598 | [] -> 0, 0
599 | l :: _ ->
600 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
602 let cmd =
603 let b = makecmd "search"
604 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
606 Buffer.add_char b ',';
607 Buffer.add_string b pattern;
608 Buffer.add_char b '\000';
609 Buffer.contents b;
611 writecmd state.csock cmd;
614 let intentry text key =
615 let c = Char.unsafe_chr key in
616 match c with
617 | '0' .. '9' ->
618 let s = "x" in s.[0] <- c;
619 let text = text ^ s in
620 TEcont text
622 | _ ->
623 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
624 TEcont text
627 let addchar s c =
628 let b = Buffer.create (String.length s + 1) in
629 Buffer.add_string b s;
630 Buffer.add_char b c;
631 Buffer.contents b;
634 let textentry text key =
635 let c = Char.unsafe_chr key in
636 match c with
637 | _ when key >= 32 && key < 127 ->
638 let text = addchar text c in
639 TEcont text
641 | _ ->
642 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
643 TEcont text
646 let optentry text key =
647 let btos b = if b then "on" else "off" in
648 let c = Char.unsafe_chr key in
649 match c with
650 | 'r' ->
651 conf.rectsel <- not conf.rectsel;
652 TEdone ("rectsel " ^ (btos conf.rectsel))
654 | 'i' ->
655 conf.icase <- not conf.icase;
656 TEdone ("case insensitive search " ^ (btos conf.icase))
658 | 'p' ->
659 conf.preload <- not conf.preload;
660 TEdone ("preload " ^ (btos conf.preload))
662 | 't' ->
663 conf.titletext <- not conf.titletext;
664 TEdone ("titletext " ^ (btos conf.titletext))
666 | 'd' ->
667 conf.redispimm <- not conf.redispimm;
668 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
670 | 'v' ->
671 conf.verbose <- not conf.verbose;
672 TEdone ("verbose " ^ (btos conf.verbose))
674 | 'h' ->
675 conf.maxhfit <- not conf.maxhfit;
676 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
677 TEdone ("maxhfit " ^ (btos conf.maxhfit))
679 | 'q' ->
680 conf.markonquit <- not conf.markonquit;
681 TEdone ("bookmark on quit " ^ btos conf.markonquit)
683 | 'c' ->
684 conf.crophack <- not conf.crophack;
685 TEdone ("crophack " ^ btos conf.crophack)
687 | _ ->
688 state.text <- Printf.sprintf "bad option %d `%c'" key c;
689 TEstop
692 let maxoutlinerows () = (state.h - 31) / 16;;
694 let enterselector allowdel outlines errmsg =
695 if Array.length outlines = 0
696 then (
697 showtext ' ' errmsg;
698 Glut.swapBuffers ()
700 else
701 let pageno =
702 match state.layout with
703 | [] -> -1
704 | {pageno=pageno} :: rest -> pageno
706 let active =
707 let rec loop n =
708 if n = Array.length outlines
709 then 0
710 else
711 let (_, _, outlinepageno, _) = outlines.(n) in
712 if outlinepageno >= pageno then n else loop (n+1)
714 loop 0
716 state.outline <-
717 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
718 Glut.postRedisplay ();
721 let enteroutlinemode () =
722 let outlines =
723 match state.outlines with
724 | Oarray a -> a
725 | Olist l ->
726 let a = Array.of_list (List.rev l) in
727 state.outlines <- Oarray a;
730 enterselector false outlines "Documents has no outline";
733 let enterbookmarkmode () =
734 let bookmarks = Array.of_list state.bookmarks in
735 enterselector true bookmarks "Documents has no bookmarks (yet)";
739 let quickbookmark ?title () =
740 match state.layout with
741 | [] -> ()
742 | l :: _ ->
743 let title =
744 match title with
745 | None ->
746 let sec = Unix.gettimeofday () in
747 let tm = Unix.localtime sec in
748 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
749 l.pageno
750 tm.Unix.tm_mday
751 tm.Unix.tm_mon
752 (tm.Unix.tm_year + 1900)
753 tm.Unix.tm_hour
754 tm.Unix.tm_min
755 | Some title -> title
757 state.bookmarks <-
758 (title, 0, l.pageno, l.pagey) :: state.bookmarks
761 let viewkeyboard ~key ~x ~y =
762 let enttext te =
763 state.textentry <- te;
764 state.text <- "";
765 enttext ();
766 Glut.swapBuffers ()
768 match state.textentry with
769 | None ->
770 let c = Char.chr key in
771 begin match c with
772 | '\027' | 'q' ->
773 exit 0
775 | '\008' ->
776 let y = getnav () in
777 gotoy y
779 | 'o' ->
780 enteroutlinemode ()
782 | 'u' ->
783 state.rects <- [];
784 state.text <- "";
785 Glut.postRedisplay ()
787 | '/' | '?' ->
788 let ondone isforw s =
789 state.searchpattern <- s;
790 search s isforw
792 enttext (Some (c, "", textentry, ondone (c ='/')))
794 | '+' ->
795 let ondone s =
796 let n =
797 try int_of_string s with exc ->
798 state.text <- Printf.sprintf "bad integer `%s': %s"
799 s (Printexc.to_string exc);
800 max_int
802 if n != max_int
803 then (
804 conf.pagebias <- n;
805 state.text <- "page bias is now " ^ string_of_int n;
808 enttext (Some ('+', "", intentry, ondone))
810 | '-' ->
811 let ondone msg =
812 state.text <- msg;
814 enttext (Some ('-', "", optentry, ondone))
816 | '0' .. '9' ->
817 let ondone s =
818 let n =
819 try int_of_string s with exc ->
820 state.text <- Printf.sprintf "bad integer `%s': %s"
821 s (Printexc.to_string exc);
824 if n >= 0
825 then (
826 addnav ();
827 state.y <- y;
828 gotoy (getpagey (n + conf.pagebias - 1))
831 let pageentry text key =
832 match Char.unsafe_chr key with
833 | 'g' -> TEdone text
834 | _ -> intentry text key
836 let text = "x" in text.[0] <- c;
837 enttext (Some (':', text, pageentry, ondone))
839 | 'b' ->
840 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
841 reshape state.w state.h;
843 | 'f' ->
844 begin match state.fullscreen with
845 | None ->
846 state.fullscreen <- Some (state.w, state.h);
847 Glut.fullScreen ()
848 | Some (w, h) ->
849 state.fullscreen <- None;
850 Glut.reshapeWindow ~w ~h
853 | 'n' ->
854 search state.searchpattern true
856 | 'p' ->
857 search state.searchpattern false
859 | 't' ->
860 begin match state.layout with
861 | [] -> ()
862 | l :: _ ->
863 gotoy (state.y - l.pagey);
866 | ' ' | 'N' ->
867 begin match List.rev state.layout with
868 | [] -> ()
869 | l :: _ ->
870 gotoy (clamp (l.pageh - l.pagey))
873 | '\127' | 'P' ->
874 begin match state.layout with
875 | [] -> ()
876 | l :: _ ->
877 gotoy (clamp (-l.pageh));
880 | '=' ->
881 let f (fn, ln) l =
882 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
884 let fn, ln = List.fold_left f (-1, -1) state.layout in
885 let s =
886 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
887 let percent = (100. *. (float state.y /. float maxy)) in
888 if fn = ln
889 then
890 Printf.sprintf "Page %d of %d %.2f%%"
891 (fn+1) state.pagecount percent
892 else
893 Printf.sprintf
894 "Pages %d-%d of %d %.2f%%"
895 (fn+1) (ln+1) state.pagecount percent
897 showtext ' ' s;
898 Glut.swapBuffers ()
900 | 'w' ->
901 begin match state.layout with
902 | [] -> ()
903 | l :: _ ->
904 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
905 Glut.postRedisplay ();
908 | '\'' ->
909 enterbookmarkmode ()
911 | 'm' ->
912 let ondone s =
913 match state.layout with
914 | l :: _ ->
915 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
916 | _ -> ()
918 enttext (Some ('~', "", textentry, ondone))
920 | '~' ->
921 quickbookmark ();
922 showtext ' ' "Quick bookmark added";
923 Glut.swapBuffers ()
925 | 'z' ->
926 begin match state.layout with
927 | l :: _ ->
928 let a = getpagewh l.pagedimno in
929 let w, h =
930 if conf.crophack
931 then
932 (truncate (1.8 *. (a.(1) -. a.(0))),
933 truncate (1.4 *. (a.(3) -. a.(0))))
934 else
935 (truncate (a.(1) -. a.(0)),
936 truncate (a.(3) -. a.(0)))
938 Glut.reshapeWindow (w + conf.scrollw) h;
939 Glut.postRedisplay ();
941 | [] -> ()
944 | _ ->
945 vlog "huh? %d %c" key (Char.chr key);
948 | Some (c, text, onkey, ondone) when key = 8 ->
949 let len = String.length text in
950 if len = 0 || len = 1
951 then (
952 state.textentry <- None;
953 Glut.postRedisplay ();
955 else (
956 let s = String.sub text 0 (len - 1) in
957 enttext (Some (c, s, onkey, ondone))
960 | Some (c, text, onkey, ondone) ->
961 begin match Char.unsafe_chr key with
962 | '\r' | '\n' ->
963 ondone text;
964 state.textentry <- None;
965 Glut.postRedisplay ()
967 | '\027' ->
968 state.textentry <- None;
969 Glut.postRedisplay ()
971 | _ ->
972 begin match onkey text key with
973 | TEdone text ->
974 state.textentry <- None;
975 ondone text;
976 Glut.postRedisplay ()
978 | TEcont text ->
979 enttext (Some (c, text, onkey, ondone));
981 | TEstop ->
982 state.textentry <- None;
983 Glut.postRedisplay ()
984 end;
985 end;
988 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
989 let search active pattern incr =
990 let dosearch re =
991 let rec loop n =
992 if n = Array.length outlines || n = -1 then None else
993 let (s, _, _, _) = outlines.(n) in
995 (try ignore (Str.search_forward re s 0); true
996 with Not_found -> false)
997 then (
998 let maxrows = (min (Array.length outlines) (maxoutlinerows ())) / 2 in
999 if first > n
1000 then Some (n, max 0 (n - maxrows))
1001 else Some (n, max first (n - maxrows))
1003 else loop (n + incr)
1005 loop active
1008 let re = Str.regexp_case_fold pattern in
1009 dosearch re
1010 with Failure s ->
1011 state.text <- s;
1012 None
1014 match key with
1015 | 27 ->
1016 if String.length qsearch = 0
1017 then (
1018 state.text <- "";
1019 state.outline <- None;
1020 Glut.postRedisplay ();
1022 else (
1023 state.text <- "";
1024 state.outline <- Some (allowdel, active, first, outlines, "");
1025 Glut.postRedisplay ();
1028 | 18 | 19 ->
1029 let incr = if key = 18 then -1 else 1 in
1030 let active, first =
1031 match search (active + incr) qsearch incr with
1032 | None ->
1033 state.text <- qsearch ^ " [not found]";
1034 active, first
1035 | Some af ->
1036 state.text <- qsearch;
1039 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1040 Glut.postRedisplay ();
1042 | 8 ->
1043 let len = String.length qsearch in
1044 if len = 0
1045 then ()
1046 else (
1047 if len = 1
1048 then (
1049 state.text <- "";
1050 state.outline <- Some (allowdel, active, first, outlines, "");
1052 else
1053 let qsearch = String.sub qsearch 0 (len - 1) in
1054 state.text <- qsearch;
1055 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1057 Glut.postRedisplay ()
1059 | 13 ->
1060 if active < Array.length outlines
1061 then (
1062 let (_, _, n, t) = outlines.(active) in
1063 gotopage n t;
1065 state.text <- "";
1066 if allowdel then state.bookmarks <- Array.to_list outlines;
1067 state.outline <- None;
1068 Glut.postRedisplay ();
1070 | _ when key >= 32 && key < 127 ->
1071 let pattern = addchar qsearch (Char.chr key) in
1072 let active, first =
1073 match search active pattern 1 with
1074 | None ->
1075 state.text <- pattern ^ " [not found]";
1076 active, first
1077 | Some (active, first) ->
1078 state.text <- pattern;
1079 active, first
1081 state.outline <- Some (allowdel, active, first, outlines, pattern);
1082 Glut.postRedisplay ()
1084 | 127 when allowdel ->
1085 let len = Array.length outlines - 1 in
1086 if len = 0
1087 then (
1088 state.outline <- None;
1089 state.bookmarks <- [];
1091 else (
1092 let bookmarks = Array.init len
1093 (fun i ->
1094 let i = if i >= active then i + 1 else i in
1095 outlines.(i)
1098 state.outline <-
1099 Some (allowdel,
1100 min active (len-1),
1101 min first (len-1),
1102 bookmarks, qsearch)
1105 Glut.postRedisplay ()
1107 | _ -> log "unknown key %d" key
1110 let keyboard ~key ~x ~y =
1111 match state.outline with
1112 | None -> viewkeyboard ~key ~x ~y
1113 | Some outline -> outlinekeyboard ~key ~x ~y outline
1116 let special ~key ~x ~y =
1117 match state.outline with
1118 | None ->
1119 let y =
1120 match key with
1121 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1122 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1123 | Glut.KEY_DOWN -> clamp conf.scrollincr
1124 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1125 | Glut.KEY_PAGE_DOWN -> clamp state.h
1126 | Glut.KEY_HOME -> addnav (); 0
1127 | Glut.KEY_END ->
1128 addnav ();
1129 state.maxy - (if conf.maxhfit then state.h else 0)
1130 | _ -> state.y
1132 state.text <- "";
1133 gotoy y
1135 | Some (allowdel, active, first, outlines, qsearch) ->
1136 let maxrows = maxoutlinerows () in
1137 let navigate incr =
1138 let active = active + incr in
1139 let active = max 0 (min active (Array.length outlines - 1)) in
1140 let first =
1141 if active > first
1142 then
1143 let rows = active - first in
1144 if rows > maxrows then first + incr else first
1145 else active
1147 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1148 Glut.postRedisplay ()
1150 match key with
1151 | Glut.KEY_UP -> navigate ~-1
1152 | Glut.KEY_DOWN -> navigate 1
1153 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1154 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1156 | Glut.KEY_HOME ->
1157 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1158 Glut.postRedisplay ()
1160 | Glut.KEY_END ->
1161 let active = Array.length outlines - 1 in
1162 let first = max 0 (active - maxrows) in
1163 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1164 Glut.postRedisplay ()
1166 | _ -> ()
1169 let drawplaceholder l =
1170 if true
1171 then (
1172 GlDraw.color (0.2, 0.2, 0.2);
1173 GlDraw.color (1.0, 1.0, 1.0);
1174 GlDraw.rect
1175 (0.0, float l.pagedispy)
1176 (float l.pagew, float (l.pagedispy + l.pagevh))
1178 let x = 0.0
1179 and y = float (l.pagedispy + 13) in
1180 let font = Glut.BITMAP_8_BY_13 in
1181 GlDraw.color (0.0, 0.0, 0.0);
1182 GlPix.raster_pos ~x ~y ();
1183 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1184 ("Loading " ^ string_of_int l.pageno);
1186 else (
1187 GlDraw.begins `quads;
1188 GlDraw.vertex2 (0.0, float l.pagedispy);
1189 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1190 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1191 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1192 GlDraw.ends ();
1196 let now () = Unix.gettimeofday ();;
1198 let drawpage i l =
1199 begin match getopaque l.pageno with
1200 | Some opaque when validopaque opaque ->
1201 GlDraw.color (1.0, 1.0, 1.0);
1202 let a = now () in
1203 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1204 let b = now () in
1205 let d = b-.a in
1206 vlog "draw %f sec" d;
1208 | Some _ ->
1209 drawplaceholder l
1211 | None ->
1212 drawplaceholder l;
1213 if state.inflight < cblen state.pagecache
1214 then (
1215 List.iter preload state.layout;
1217 else (
1218 vlog "inflight %d" state.inflight;
1220 end;
1221 GlDraw.color (0.5, 0.5, 0.5);
1222 GlDraw.rect
1223 (0., float i)
1224 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1226 l.pagedispy + l.pagevh;
1229 let scrollindicator () =
1230 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1231 GlDraw.color (0.64 , 0.64, 0.64);
1232 GlDraw.rect
1233 (float (state.w - conf.scrollw), 0.)
1234 (float state.w, float state.h)
1236 GlDraw.color (0.0, 0.0, 0.0);
1237 let sh = (float (maxy + state.h) /. float state.h) in
1238 let sh = float state.h /. sh in
1239 let sh = max sh (float conf.scrollh) in
1241 let percent =
1242 if state.y = state.maxy
1243 then 1.0
1244 else float state.y /. float maxy
1246 let position = (float state.h -. sh) *. percent in
1248 let position =
1249 if position +. sh > float state.h
1250 then
1251 float state.h -. sh
1252 else
1253 position
1255 GlDraw.rect
1256 (float (state.w - conf.scrollw), position)
1257 (float state.w, position +. sh)
1261 let showsel () =
1262 match state.mstate with
1263 | Mnone ->
1266 | Msel ((x0, y0), (x1, y1)) ->
1267 let y0' = min y0 y1
1268 and y1 = max y0 y1 in
1269 let y0 = y0' in
1270 let f l =
1271 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1272 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1273 then
1274 match getopaque l.pageno with
1275 | Some opaque when validopaque opaque ->
1276 let oy = -l.pagey + l.pagedispy in
1277 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1278 | _ -> ()
1280 List.iter f state.layout
1283 let showrects () =
1284 Gl.enable `blend;
1285 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1286 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1287 List.iter
1288 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1289 List.iter (fun l ->
1290 if l.pageno = pageno
1291 then (
1292 let d = float (l.pagedispy - l.pagey) in
1293 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1294 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1296 ) state.layout
1297 ) state.rects
1299 Gl.disable `blend;
1302 let showoutline = function
1303 | None -> ()
1304 | Some (allowdel, active, first, outlines, qsearch) ->
1305 Gl.enable `blend;
1306 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1307 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1308 GlDraw.rect (0., 0.) (float state.w, float state.h);
1309 Gl.disable `blend;
1311 GlDraw.color (1., 1., 1.);
1312 let font = Glut.BITMAP_9_BY_15 in
1313 let draw_string x y s =
1314 GlPix.raster_pos ~x ~y ();
1315 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1317 let rec loop row =
1318 if row = Array.length outlines || (row - first) * 16 > state.h
1319 then ()
1320 else (
1321 let (s, l, _, _) = outlines.(row) in
1322 let y = (row - first) * 16 in
1323 let x = 5 + 5*l in
1324 if row = active
1325 then (
1326 Gl.enable `blend;
1327 GlDraw.polygon_mode `both `line;
1328 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1329 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1330 GlDraw.rect (0., float (y + 1))
1331 (float (state.w - conf.scrollw - 1), float (y + 18));
1332 GlDraw.polygon_mode `both `fill;
1333 Gl.disable `blend;
1334 GlDraw.color (1., 1., 1.);
1336 draw_string (float x) (float (y + 16)) s;
1337 loop (row+1)
1340 loop first
1343 let display () =
1344 let lasty = List.fold_left drawpage 0 (state.layout) in
1345 GlDraw.color (0.5, 0.5, 0.5);
1346 GlDraw.rect
1347 (0., float lasty)
1348 (float (state.w - conf.scrollw), float state.h)
1350 showrects ();
1351 scrollindicator ();
1352 showsel ();
1353 showoutline state.outline;
1354 enttext ();
1355 Glut.swapBuffers ();
1358 let getlink x y =
1359 let rec f = function
1360 | l :: rest ->
1361 begin match getopaque l.pageno with
1362 | Some opaque when validopaque opaque ->
1363 let y = y - l.pagedispy in
1364 if y > 0
1365 then
1366 let y = l.pagey + y in
1367 match getlink opaque x y with
1368 | None -> f rest
1369 | some -> some
1370 else
1371 f rest
1372 | _ ->
1373 f rest
1375 | [] -> None
1377 f state.layout
1380 let checklink x y =
1381 let rec f = function
1382 | l :: rest ->
1383 begin match getopaque l.pageno with
1384 | Some opaque when validopaque opaque ->
1385 let y = y - l.pagedispy in
1386 if y > 0
1387 then
1388 let y = l.pagey + y in
1389 if checklink opaque x y then true else f rest
1390 else
1391 f rest
1392 | _ ->
1393 f rest
1395 | [] -> false
1397 f state.layout
1400 let mouse ~button ~bstate ~x ~y =
1401 match button with
1402 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1403 let incr =
1404 if n = 3
1405 then
1406 -conf.scrollincr
1407 else
1408 conf.scrollincr
1410 let incr = incr * 2 in
1411 let y = clamp incr in
1412 gotoy y
1414 | Glut.LEFT_BUTTON when state.outline = None ->
1415 let dest = if bstate = Glut.DOWN then getlink x y else None in
1416 begin match dest with
1417 | Some (pageno, top) ->
1418 gotopage pageno top
1420 | None ->
1421 if bstate = Glut.DOWN
1422 then (
1423 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1424 state.mstate <- Msel ((x, y), (x, y));
1425 Glut.postRedisplay ()
1427 else (
1428 Glut.setCursor Glut.CURSOR_INHERIT;
1429 state.mstate <- Mnone;
1433 | _ ->
1436 let mouse ~button ~state ~x ~y = mouse button state x y;;
1438 let motion ~x ~y =
1439 if state.outline = None
1440 then
1441 match state.mstate with
1442 | Mnone -> ()
1443 | Msel (a, _) ->
1444 state.mstate <- Msel (a, (x, y));
1445 Glut.postRedisplay ()
1448 let pmotion ~x ~y =
1449 if state.outline = None
1450 then
1451 match state.mstate with
1452 | Mnone when (checklink x y) ->
1453 Glut.setCursor Glut.CURSOR_INFO
1455 | Mnone ->
1456 Glut.setCursor Glut.CURSOR_INHERIT
1458 | Msel (a, _) ->
1462 let () =
1463 let statepath =
1464 let home =
1465 if Sys.os_type = "Win32"
1466 then
1467 try Sys.getenv "HOMEPATH" with Not_found -> ""
1468 else
1469 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1471 Filename.concat home "llpp"
1473 let pstate =
1475 let ic = open_in_bin statepath in
1476 let hash = input_value ic in
1477 close_in ic;
1478 hash
1479 with exn ->
1480 if false
1481 then
1482 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1484 Hashtbl.create 1
1486 let savestate () =
1488 let w, h =
1489 match state.fullscreen with
1490 | None -> state.w, state.h
1491 | Some wh -> wh
1493 if conf.markonquit then quickbookmark ();
1494 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1495 let oc = open_out_bin statepath in
1496 output_value oc pstate
1497 with exn ->
1498 if false
1499 then
1500 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1503 let setstate () =
1505 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1506 state.w <- statew;
1507 state.h <- stateh;
1508 state.bookmarks <- statebookmarks;
1509 with Not_found -> ()
1510 | exn ->
1511 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1514 Arg.parse [] (fun s -> state.path <- s) "options:";
1515 let name =
1516 if String.length state.path = 0
1517 then (prerr_endline "filename missing"; exit 1)
1518 else state.path
1521 setstate ();
1522 let _ = Glut.init Sys.argv in
1523 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1524 let () = Glut.initWindowSize state.w state.h in
1525 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1527 let csock, ssock =
1528 if Sys.os_type = "Unix"
1529 then
1530 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1531 else
1532 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1533 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1534 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1535 Unix.bind sock addr;
1536 Unix.listen sock 1;
1537 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1538 Unix.connect csock addr;
1539 let ssock, _ = Unix.accept sock in
1540 Unix.close sock;
1541 let opts sock =
1542 Unix.setsockopt sock Unix.TCP_NODELAY true;
1543 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1545 opts ssock;
1546 opts csock;
1547 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1548 ssock, csock
1551 let () = Glut.displayFunc display in
1552 let () = Glut.reshapeFunc reshape in
1553 let () = Glut.keyboardFunc keyboard in
1554 let () = Glut.specialFunc special in
1555 let () = Glut.idleFunc (Some idle) in
1556 let () = Glut.mouseFunc mouse in
1557 let () = Glut.motionFunc motion in
1558 let () = Glut.passiveMotionFunc pmotion in
1560 init ssock;
1561 state.csock <- csock;
1562 state.ssock <- ssock;
1563 writecmd csock ("open " ^ name ^ "\000");
1565 at_exit savestate;
1567 let rec handlelablglutbug () =
1569 Glut.mainLoop ();
1570 with Glut.BadEnum "key in special_of_int" ->
1571 showtext '!' " LablGlut bug: special key not recognized";
1572 Glut.swapBuffers ();
1573 handlelablglutbug ()
1575 handlelablglutbug ();