Move autoscroll from '-' 'a' to just 'a'
[llpp.git] / main.ml
blob81e0c849c5479ec39dd0004037fde5697e67a5e7
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
88 ; mutable autoscroll : bool
92 type outline = string * int * int * int;;
93 type outlines = Oarray of outline array | Olist of outline list;;
95 type state =
96 { mutable csock : Unix.file_descr
97 ; mutable ssock : Unix.file_descr
98 ; mutable w : int
99 ; mutable h : int
100 ; mutable y : int
101 ; mutable prevy : int
102 ; mutable maxy : int
103 ; mutable layout : layout list
104 ; pagemap : ((int * int), string) Hashtbl.t
105 ; mutable pages : (int * int * int) list
106 ; mutable pagecount : int
107 ; pagecache : string circbuf
108 ; navhist : float circbuf
109 ; mutable inflight : int
110 ; mutable mstate : mstate
111 ; mutable searchpattern : string
112 ; mutable rects : (int * int * Gl.point2 * Gl.point2) list
113 ; mutable rects1 : (int * int * Gl.point2 * Gl.point2) list
114 ; mutable text : string
115 ; mutable fullscreen : (int * int) option
116 ; mutable textentry : textentry option
117 ; mutable outlines : outlines
118 ; mutable outline : (bool * int * int * outline array * string) option
119 ; mutable bookmarks : outline list
120 ; mutable path : string
124 let conf =
125 { scrollw = 5
126 ; scrollh = 12
127 ; icase = true
128 ; rectsel = true
129 ; preload = false
130 ; pagebias = 0
131 ; redispimm = false
132 ; verbose = false
133 ; scrollincr = 24
134 ; maxhfit = true
135 ; crophack = false
136 ; autoscroll = false
140 let state =
141 { csock = Unix.stdin
142 ; ssock = Unix.stdin
143 ; w = 900
144 ; h = 900
145 ; y = 0
146 ; prevy = 0
147 ; layout = []
148 ; maxy = max_int
149 ; pagemap = Hashtbl.create 10
150 ; pagecache = cbnew 10 ""
151 ; pages = []
152 ; pagecount = 0
153 ; inflight = 0
154 ; mstate = Mnone
155 ; navhist = cbnew 100 0.0
156 ; rects = []
157 ; rects1 = []
158 ; text = ""
159 ; fullscreen = None
160 ; textentry = None
161 ; searchpattern = ""
162 ; outlines = Olist []
163 ; outline = None
164 ; bookmarks = []
165 ; path = ""
169 let vlog fmt =
170 if conf.verbose
171 then
172 Printf.kprintf prerr_endline fmt
173 else
174 Printf.kprintf ignore fmt
177 let writecmd fd s =
178 let len = String.length s in
179 let n = 4 + len in
180 let b = Buffer.create n in
181 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
182 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
183 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
184 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
185 Buffer.add_string b s;
186 let s' = Buffer.contents b in
187 let n' = Unix.write fd s' 0 n in
188 if n' != n then failwith "write failed";
191 let readcmd fd =
192 let s = "xxxx" in
193 let n = Unix.read fd s 0 4 in
194 if n != 4 then failwith "incomplete read(len)";
195 let len = 0
196 lor (Char.code s.[0] lsl 24)
197 lor (Char.code s.[1] lsl 16)
198 lor (Char.code s.[2] lsl 8)
199 lor (Char.code s.[3] lsl 0)
201 let s = String.create len in
202 let n = Unix.read fd s 0 len in
203 if n != len then failwith "incomplete read(data)";
207 let yratio y =
208 if y = state.maxy then 1.0
209 else float y /. float state.maxy
212 let makecmd s l =
213 let b = Buffer.create 10 in
214 Buffer.add_string b s;
215 let rec combine = function
216 | [] -> b
217 | x :: xs ->
218 Buffer.add_char b ' ';
219 let s =
220 match x with
221 | `b b -> if b then "1" else "0"
222 | `s s -> s
223 | `i i -> string_of_int i
224 | `f f -> string_of_float f
225 | `I f -> string_of_int (truncate f)
227 Buffer.add_string b s;
228 combine xs;
230 combine l;
233 let wcmd s l =
234 let cmd = Buffer.contents (makecmd s l) in
235 writecmd state.csock cmd;
238 let calcheight () =
239 let rec f pn ph fh l =
240 match l with
241 | (n, _, h) :: rest ->
242 let fh = fh + (n - pn) * ph in
243 f n h fh rest
245 | [] ->
246 let fh = fh + (ph * (state.pagecount - pn)) in
247 max 0 fh
249 let fh = f 0 0 0 state.pages in
253 let getpagey pageno =
254 let rec f pn ph y l =
255 match l with
256 | (n, _, h) :: rest ->
257 if n >= pageno
258 then
259 y + (pageno - pn) * ph
260 else
261 let y = y + (n - pn) * ph in
262 f n h y rest
264 | [] ->
265 y + (pageno - pn) * ph
267 f 0 0 0 state.pages;
270 let layout y sh =
271 let rec f pageno pdimno prev vy py dy l cacheleft accu =
272 if pageno = state.pagecount || cacheleft = 0
273 then accu
274 else
275 let ((_, w, h) as curr), rest, pdimno =
276 match l with
277 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
278 curr, rest, pdimno + 1
279 | _ ->
280 prev, l, pdimno
282 let pageno' = pageno + 1 in
283 if py + h > vy
284 then
285 let py' = vy - py in
286 let vh = h - py' in
287 if dy + vh > sh
288 then
289 let vh = sh - dy in
290 if vh <= 0
291 then
292 accu
293 else
294 let e =
295 { pageno = pageno
296 ; pagedimno = pdimno
297 ; pagew = w
298 ; pageh = h
299 ; pagedispy = dy
300 ; pagey = py'
301 ; pagevh = vh
304 e :: accu
305 else
306 let e =
307 { pageno = pageno
308 ; pagedimno = pdimno
309 ; pagew = w
310 ; pageh = h
311 ; pagedispy = dy
312 ; pagey = py'
313 ; pagevh = vh
316 let accu = e :: accu in
317 f pageno' pdimno curr
318 (vy + vh) (py + h) (dy + vh + 2) rest
319 (pred cacheleft) accu
320 else
321 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
323 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
324 state.maxy <- calcheight ();
325 List.rev accu
328 let clamp incr =
329 let y = state.y + incr in
330 let y = max 0 y in
331 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
335 let gotoy y =
336 let y = max 0 y in
337 let y = min state.maxy y in
338 let pages = layout y state.h in
339 state.y <- y;
340 state.layout <- pages;
341 if conf.redispimm
342 then
343 Glut.postRedisplay ()
347 let addnav () =
348 cbput state.navhist (yratio state.y);
349 cbrfollowlen state.navhist;
352 let getnav () =
353 let y = cbget state.navhist in
354 truncate (y *. float state.maxy)
357 let gotopage n top =
358 let y = getpagey n in
359 addnav ();
360 state.y <- y + top;
361 gotoy state.y;
364 let reshape ~w ~h =
365 let ratio = float w /. float state.w in
366 let fixbookmark (s, l, pageno, pagey) =
367 let pagey = truncate (float pagey *. ratio) in
368 (s, l, pageno, pagey)
370 state.bookmarks <- List.map fixbookmark state.bookmarks;
371 state.w <- w;
372 state.h <- h;
373 GlDraw.viewport 0 0 w h;
374 GlMat.mode `modelview;
375 GlMat.load_identity ();
376 GlMat.mode `projection;
377 GlMat.load_identity ();
378 GlMat.rotate ~x:1.0 ~angle:180.0 ();
379 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
380 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
381 GlClear.color (1., 1., 1.);
382 GlClear.clear [`color];
383 state.layout <- [];
384 state.pages <- [];
385 state.rects <- [];
386 state.text <- "";
387 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
390 let showtext c s =
391 GlDraw.color (0.0, 0.0, 0.0);
392 GlDraw.rect
393 (0.0, float (state.h - 18))
394 (float (state.w - conf.scrollw - 1), float state.h)
396 let font = Glut.BITMAP_8_BY_13 in
397 GlDraw.color (1.0, 1.0, 1.0);
398 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
399 Glut.bitmapCharacter ~font ~c:(Char.code c);
400 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
403 let enttext () =
404 let len = String.length state.text in
405 match state.textentry with
406 | None ->
407 if len > 0 then showtext ' ' state.text
409 | Some (c, text, _, _) ->
410 let s =
411 if len > 0
412 then
413 text ^ " [" ^ state.text ^ "]"
414 else
415 text
417 showtext c s;
420 let act cmd =
421 match cmd.[0] with
422 | 'c' ->
423 state.pages <- [];
424 state.outlines <- Olist []
426 | 'D' ->
427 state.rects <- state.rects1;
428 Glut.postRedisplay ()
430 | 'd' ->
431 state.rects <- state.rects1;
432 Glut.postRedisplay ()
434 | 'C' ->
435 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
436 state.pagecount <- n;
437 let rely = yratio state.y in
438 let maxy = calcheight () in
439 state.y <- truncate (float maxy *. rely);
440 let pages = layout state.y state.h in
441 state.layout <- pages;
442 Glut.postRedisplay ();
444 | 't' ->
445 let s = Scanf.sscanf cmd "t %n"
446 (fun n -> String.sub cmd n (String.length cmd - n))
448 Glut.setWindowTitle s
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 ();
458 | 'V' ->
459 if conf.verbose
460 then
461 let s = Scanf.sscanf cmd "V %n"
462 (fun n -> String.sub cmd n (String.length cmd - n))
464 state.text <- s;
465 showtext ' ' s;
466 Glut.swapBuffers ();
468 | 'F' ->
469 let pageno, c, x0, y0, x1, y1 =
470 Scanf.sscanf cmd "F %d %d %f %f %f %f"
471 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
473 let y = (getpagey pageno) + truncate y0 in
474 addnav ();
475 gotoy y;
476 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
478 | 'R' ->
479 let pageno, c, x0, y0, x1, y1 =
480 Scanf.sscanf cmd "R %d %d %f %f %f %f"
481 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
483 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
485 | 'r' ->
486 let n, w, h, p =
487 Scanf.sscanf cmd "r %d %d %d %s"
488 (fun n w h p -> (n, w, h, p))
490 Hashtbl.replace state.pagemap (n, w) p;
491 let evicted = cbpeekw state.pagecache in
492 if String.length evicted > 0
493 then begin
494 wcmd "free" [`s evicted];
495 let l = Hashtbl.fold (fun k p a ->
496 if evicted = p then k :: a else a) state.pagemap []
498 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
499 end;
500 cbput state.pagecache p;
501 state.inflight <- pred state.inflight;
502 Glut.postRedisplay ()
504 | 'l' ->
505 let (n, w, h) as pagelayout =
506 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
508 state.pages <- pagelayout :: state.pages
510 | 'o' ->
511 let (l, n, t, pos) =
512 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
514 let s = String.sub cmd pos (String.length cmd - pos) in
515 let outline = (s, l, n, t) in
516 let outlines =
517 match state.outlines with
518 | Olist outlines -> Olist (outline :: outlines)
519 | Oarray _ -> Olist [outline]
521 state.outlines <- outlines
523 | _ ->
524 log "unknown cmd `%S'" cmd
527 let getopaque pageno =
528 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
529 with Not_found -> None
532 let cache pageno opaque =
533 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
536 let validopaque opaque = String.length opaque > 0;;
538 let preload l =
539 match getopaque l.pageno with
540 | None when state.inflight < 2+0*(cblen state.pagecache) ->
541 state.inflight <- succ state.inflight;
542 cache l.pageno "";
543 wcmd "render" [`i (l.pageno + 1)
544 ;`i l.pagedimno
545 ;`i l.pagew
546 ;`i l.pageh];
548 | _ -> ()
551 let idle () =
552 if not conf.redispimm && state.y != state.prevy
553 then (
554 state.prevy <- state.y;
555 Glut.postRedisplay ();
557 else
558 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
560 begin match r with
561 | [] ->
562 if conf.preload then begin
563 let h = state.h in
564 let y = if state.y < state.h then 0 else state.y - state.h in
565 let pages = layout y (h*3) in
566 List.iter preload pages;
567 end;
568 if conf.autoscroll then begin
569 let y = state.y + conf.scrollincr in
570 let y = if y = state.maxy then 0 else y in
571 gotoy y;
572 state.text <- "";
573 state.prevy <- state.y;
574 Glut.postRedisplay ();
575 end;
577 | _ ->
578 let cmd = readcmd state.csock in
579 act cmd;
580 end;
583 let search pattern forward =
584 if String.length pattern > 0
585 then
586 let pn, py =
587 match state.layout with
588 | [] -> 0, 0
589 | l :: _ ->
590 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
592 let cmd =
593 let b = makecmd "search"
594 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
596 Buffer.add_char b ',';
597 Buffer.add_string b pattern;
598 Buffer.add_char b '\000';
599 Buffer.contents b;
601 writecmd state.csock cmd;
604 let intentry text key =
605 let c = Char.unsafe_chr key in
606 match c with
607 | '0' .. '9' ->
608 let s = "x" in s.[0] <- c;
609 let text = text ^ s in
610 TEcont text
612 | _ ->
613 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
614 TEcont text
617 let addchar s c =
618 let b = Buffer.create (String.length s + 1) in
619 Buffer.add_string b s;
620 Buffer.add_char b c;
621 Buffer.contents b;
624 let textentry text key =
625 let c = Char.unsafe_chr key in
626 match c with
627 | _ when key >= 32 && key < 127 ->
628 let text = addchar text c in
629 TEcont text
631 | _ ->
632 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
633 TEcont text
636 let optentry text key =
637 let btos b = if b then "on" else "off" in
638 let c = Char.unsafe_chr key in
639 match c with
640 | 'r' ->
641 conf.rectsel <- not conf.rectsel;
642 TEdone ("rectsel " ^ (btos conf.rectsel))
644 | 's' ->
645 let ondone s =
646 try conf.scrollincr <- int_of_string s with exc ->
647 state.text <- Printf.sprintf "bad integer `%s': %s"
648 s (Printexc.to_string exc)
650 TEswitch ('#', "", intentry, ondone)
652 | 'i' ->
653 conf.icase <- not conf.icase;
654 TEdone ("case insensitive search " ^ (btos conf.icase))
656 | 'p' ->
657 conf.preload <- not conf.preload;
658 TEdone ("preload " ^ (btos conf.preload))
660 | 'd' ->
661 conf.redispimm <- not conf.redispimm;
662 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
664 | 'v' ->
665 conf.verbose <- not conf.verbose;
666 TEdone ("verbose " ^ (btos conf.verbose))
668 | 'h' ->
669 conf.maxhfit <- not conf.maxhfit;
670 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
671 TEdone ("maxhfit " ^ (btos conf.maxhfit))
673 | 'c' ->
674 conf.crophack <- not conf.crophack;
675 TEdone ("crophack " ^ btos conf.crophack)
677 | _ ->
678 state.text <- Printf.sprintf "bad option %d `%c'" key c;
679 TEstop
682 let maxoutlinerows () = (state.h - 31) / 16;;
684 let enterselector allowdel outlines errmsg =
685 if Array.length outlines = 0
686 then (
687 showtext ' ' errmsg;
688 Glut.swapBuffers ()
690 else
691 let pageno =
692 match state.layout with
693 | [] -> -1
694 | {pageno=pageno} :: rest -> pageno
696 let active =
697 let rec loop n =
698 if n = Array.length outlines
699 then 0
700 else
701 let (_, _, outlinepageno, _) = outlines.(n) in
702 if outlinepageno >= pageno then n else loop (n+1)
704 loop 0
706 state.outline <-
707 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
708 Glut.postRedisplay ();
711 let enteroutlinemode () =
712 let outlines =
713 match state.outlines with
714 | Oarray a -> a
715 | Olist l ->
716 let a = Array.of_list (List.rev l) in
717 state.outlines <- Oarray a;
720 enterselector false outlines "Documents has no outline";
723 let enterbookmarkmode () =
724 let bookmarks = Array.of_list state.bookmarks in
725 enterselector true bookmarks "Documents has no bookmarks (yet)";
729 let quickbookmark ?title () =
730 match state.layout with
731 | [] -> ()
732 | l :: _ ->
733 let title =
734 match title with
735 | None ->
736 let sec = Unix.gettimeofday () in
737 let tm = Unix.localtime sec in
738 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
739 l.pageno
740 tm.Unix.tm_mday
741 tm.Unix.tm_mon
742 (tm.Unix.tm_year + 1900)
743 tm.Unix.tm_hour
744 tm.Unix.tm_min
745 | Some title -> title
747 state.bookmarks <-
748 (title, 0, l.pageno, l.pagey) :: state.bookmarks
751 let viewkeyboard ~key ~x ~y =
752 let enttext te =
753 state.textentry <- te;
754 state.text <- "";
755 enttext ();
756 Glut.postRedisplay ()
758 match state.textentry with
759 | None ->
760 let c = Char.chr key in
761 begin match c with
762 | '\027' | 'q' ->
763 exit 0
765 | '\008' ->
766 let y = getnav () in
767 gotoy y
769 | 'o' ->
770 enteroutlinemode ()
772 | 'u' ->
773 state.rects <- [];
774 state.text <- "";
775 Glut.postRedisplay ()
777 | '/' | '?' ->
778 let ondone isforw s =
779 state.searchpattern <- s;
780 search s isforw
782 enttext (Some (c, "", textentry, ondone (c ='/')))
784 | '+' ->
785 let ondone s =
786 let n =
787 try int_of_string s with exc ->
788 state.text <- Printf.sprintf "bad integer `%s': %s"
789 s (Printexc.to_string exc);
790 max_int
792 if n != max_int
793 then (
794 conf.pagebias <- n;
795 state.text <- "page bias is now " ^ string_of_int n;
798 enttext (Some ('+', "", intentry, ondone))
800 | '-' ->
801 let ondone msg =
802 state.text <- msg;
804 enttext (Some ('-', "", optentry, ondone))
806 | '0' .. '9' ->
807 let ondone s =
808 let n =
809 try int_of_string s with exc ->
810 state.text <- Printf.sprintf "bad integer `%s': %s"
811 s (Printexc.to_string exc);
814 if n >= 0
815 then (
816 addnav ();
817 state.y <- y;
818 gotoy (getpagey (n + conf.pagebias - 1))
821 let pageentry text key =
822 match Char.unsafe_chr key with
823 | 'g' -> TEdone text
824 | _ -> intentry text key
826 let text = "x" in text.[0] <- c;
827 enttext (Some (':', text, pageentry, ondone))
829 | 'b' ->
830 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
831 reshape state.w state.h;
833 | 'a' ->
834 conf.autoscroll <- not conf.autoscroll
836 | 'f' ->
837 begin match state.fullscreen with
838 | None ->
839 state.fullscreen <- Some (state.w, state.h);
840 Glut.fullScreen ()
841 | Some (w, h) ->
842 state.fullscreen <- None;
843 Glut.reshapeWindow ~w ~h
846 | 'n' ->
847 search state.searchpattern true
849 | 'p' ->
850 search state.searchpattern false
852 | 't' ->
853 begin match state.layout with
854 | [] -> ()
855 | l :: _ ->
856 gotoy (state.y - l.pagey);
859 | ' ' | 'N' ->
860 begin match List.rev state.layout with
861 | [] -> ()
862 | l :: _ ->
863 gotoy (clamp (l.pageh - l.pagey))
866 | '\127' | 'P' ->
867 begin match state.layout with
868 | [] -> ()
869 | l :: _ ->
870 gotoy (clamp (-l.pageh));
873 | '=' ->
874 let f (fn, ln) l =
875 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
877 let fn, ln = List.fold_left f (-1, -1) state.layout in
878 let s =
879 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
880 let percent =
881 if maxy <= 0
882 then 100.
883 else (100. *. (float state.y /. float maxy)) in
884 if fn = ln
885 then
886 Printf.sprintf "Page %d of %d %.2f%%"
887 (fn+1) state.pagecount percent
888 else
889 Printf.sprintf
890 "Pages %d-%d of %d %.2f%%"
891 (fn+1) (ln+1) state.pagecount percent
893 showtext ' ' s;
894 Glut.swapBuffers ()
896 | 'w' ->
897 begin match state.layout with
898 | [] -> ()
899 | l :: _ ->
900 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
901 Glut.postRedisplay ();
904 | '\'' ->
905 enterbookmarkmode ()
907 | 'm' ->
908 let ondone s =
909 match state.layout with
910 | l :: _ ->
911 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
912 | _ -> ()
914 enttext (Some ('~', "", textentry, ondone))
916 | '~' ->
917 quickbookmark ();
918 showtext ' ' "Quick bookmark added";
919 Glut.swapBuffers ()
921 | 'z' ->
922 begin match state.layout with
923 | l :: _ ->
924 let a = getpagewh l.pagedimno in
925 let w, h =
926 if conf.crophack
927 then
928 (truncate (1.8 *. (a.(1) -. a.(0))),
929 truncate (1.4 *. (a.(3) -. a.(0))))
930 else
931 (truncate (a.(1) -. a.(0)),
932 truncate (a.(3) -. a.(0)))
934 Glut.reshapeWindow (w + conf.scrollw) h;
935 Glut.postRedisplay ();
937 | [] -> ()
940 | _ ->
941 vlog "huh? %d %c" key (Char.chr key);
944 | Some (c, text, onkey, ondone) when key = 8 ->
945 let len = String.length text in
946 if len = 0 || len = 1
947 then (
948 state.textentry <- None;
949 Glut.postRedisplay ();
951 else (
952 let s = String.sub text 0 (len - 1) in
953 enttext (Some (c, s, onkey, ondone))
956 | Some (c, text, onkey, ondone) ->
957 begin match Char.unsafe_chr key with
958 | '\r' | '\n' ->
959 ondone text;
960 state.textentry <- None;
961 Glut.postRedisplay ()
963 | '\027' ->
964 state.textentry <- None;
965 Glut.postRedisplay ()
967 | _ ->
968 begin match onkey text key with
969 | TEdone text ->
970 state.textentry <- None;
971 ondone text;
972 Glut.postRedisplay ()
974 | TEcont text ->
975 enttext (Some (c, text, onkey, ondone));
977 | TEstop ->
978 state.textentry <- None;
979 Glut.postRedisplay ()
981 | TEswitch te ->
982 state.textentry <- Some te;
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 if state.textentry = None
1202 then GlDraw.color (1.0, 1.0, 1.0)
1203 else GlDraw.color (0.4, 0.4, 0.4);
1204 let a = now () in
1205 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1206 let b = now () in
1207 let d = b-.a in
1208 vlog "draw %f sec" d;
1210 | Some _ ->
1211 drawplaceholder l
1213 | None ->
1214 drawplaceholder l;
1215 if state.inflight < cblen state.pagecache
1216 then (
1217 List.iter preload state.layout;
1219 else (
1220 vlog "inflight %d" state.inflight;
1222 end;
1223 GlDraw.color (0.5, 0.5, 0.5);
1224 GlDraw.rect
1225 (0., float i)
1226 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1228 l.pagedispy + l.pagevh;
1231 let scrollindicator () =
1232 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1233 GlDraw.color (0.64 , 0.64, 0.64);
1234 GlDraw.rect
1235 (float (state.w - conf.scrollw), 0.)
1236 (float state.w, float state.h)
1238 GlDraw.color (0.0, 0.0, 0.0);
1239 let sh = (float (maxy + state.h) /. float state.h) in
1240 let sh = float state.h /. sh in
1241 let sh = max sh (float conf.scrollh) in
1243 let percent =
1244 if state.y = state.maxy
1245 then 1.0
1246 else float state.y /. float maxy
1248 let position = (float state.h -. sh) *. percent in
1250 let position =
1251 if position +. sh > float state.h
1252 then
1253 float state.h -. sh
1254 else
1255 position
1257 GlDraw.rect
1258 (float (state.w - conf.scrollw), position)
1259 (float state.w, position +. sh)
1263 let showsel () =
1264 match state.mstate with
1265 | Mnone ->
1268 | Msel ((x0, y0), (x1, y1)) ->
1269 let y0' = min y0 y1
1270 and y1 = max y0 y1 in
1271 let y0 = y0' in
1272 let f l =
1273 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1274 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1275 then
1276 match getopaque l.pageno with
1277 | Some opaque when validopaque opaque ->
1278 let oy = -l.pagey + l.pagedispy in
1279 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1280 | _ -> ()
1282 List.iter f state.layout
1285 let showrects () =
1286 Gl.enable `blend;
1287 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1288 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1289 List.iter
1290 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1291 List.iter (fun l ->
1292 if l.pageno = pageno
1293 then (
1294 let d = float (l.pagedispy - l.pagey) in
1295 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1296 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1298 ) state.layout
1299 ) state.rects
1301 Gl.disable `blend;
1304 let showoutline = function
1305 | None -> ()
1306 | Some (allowdel, active, first, outlines, qsearch) ->
1307 Gl.enable `blend;
1308 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1309 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1310 GlDraw.rect (0., 0.) (float state.w, float state.h);
1311 Gl.disable `blend;
1313 GlDraw.color (1., 1., 1.);
1314 let font = Glut.BITMAP_9_BY_15 in
1315 let draw_string x y s =
1316 GlPix.raster_pos ~x ~y ();
1317 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1319 let rec loop row =
1320 if row = Array.length outlines || (row - first) * 16 > state.h
1321 then ()
1322 else (
1323 let (s, l, _, _) = outlines.(row) in
1324 let y = (row - first) * 16 in
1325 let x = 5 + 5*l in
1326 if row = active
1327 then (
1328 Gl.enable `blend;
1329 GlDraw.polygon_mode `both `line;
1330 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1331 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1332 GlDraw.rect (0., float (y + 1))
1333 (float (state.w - conf.scrollw - 1), float (y + 18));
1334 GlDraw.polygon_mode `both `fill;
1335 Gl.disable `blend;
1336 GlDraw.color (1., 1., 1.);
1338 draw_string (float x) (float (y + 16)) s;
1339 loop (row+1)
1342 loop first
1345 let display () =
1346 let lasty = List.fold_left drawpage 0 (state.layout) in
1347 GlDraw.color (0.5, 0.5, 0.5);
1348 GlDraw.rect
1349 (0., float lasty)
1350 (float (state.w - conf.scrollw), float state.h)
1352 showrects ();
1353 scrollindicator ();
1354 showsel ();
1355 showoutline state.outline;
1356 enttext ();
1357 Glut.swapBuffers ();
1360 let getlink x y =
1361 let rec f = function
1362 | l :: rest ->
1363 begin match getopaque l.pageno with
1364 | Some opaque when validopaque opaque ->
1365 let y = y - l.pagedispy in
1366 if y > 0
1367 then
1368 let y = l.pagey + y in
1369 match getlink opaque x y with
1370 | None -> f rest
1371 | some -> some
1372 else
1373 f rest
1374 | _ ->
1375 f rest
1377 | [] -> None
1379 f state.layout
1382 let checklink x y =
1383 let rec f = function
1384 | l :: rest ->
1385 begin match getopaque l.pageno with
1386 | Some opaque when validopaque opaque ->
1387 let y = y - l.pagedispy in
1388 if y > 0
1389 then
1390 let y = l.pagey + y in
1391 if checklink opaque x y then true else f rest
1392 else
1393 f rest
1394 | _ ->
1395 f rest
1397 | [] -> false
1399 f state.layout
1402 let mouse ~button ~bstate ~x ~y =
1403 match button with
1404 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1405 let incr =
1406 if n = 3
1407 then
1408 -conf.scrollincr
1409 else
1410 conf.scrollincr
1412 let incr = incr * 2 in
1413 let y = clamp incr in
1414 gotoy y
1416 | Glut.LEFT_BUTTON when state.outline = None ->
1417 let dest = if bstate = Glut.DOWN then getlink x y else None in
1418 begin match dest with
1419 | Some (pageno, top) ->
1420 gotopage pageno top
1422 | None ->
1423 if bstate = Glut.DOWN
1424 then (
1425 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1426 state.mstate <- Msel ((x, y), (x, y));
1427 Glut.postRedisplay ()
1429 else (
1430 Glut.setCursor Glut.CURSOR_INHERIT;
1431 state.mstate <- Mnone;
1435 | _ ->
1438 let mouse ~button ~state ~x ~y = mouse button state x y;;
1440 let motion ~x ~y =
1441 if state.outline = None
1442 then
1443 match state.mstate with
1444 | Mnone -> ()
1445 | Msel (a, _) ->
1446 state.mstate <- Msel (a, (x, y));
1447 Glut.postRedisplay ()
1450 let pmotion ~x ~y =
1451 if state.outline = None
1452 then
1453 match state.mstate with
1454 | Mnone when (checklink x y) ->
1455 Glut.setCursor Glut.CURSOR_INFO
1457 | Mnone ->
1458 Glut.setCursor Glut.CURSOR_INHERIT
1460 | Msel (a, _) ->
1464 let () =
1465 let statepath =
1466 let home =
1467 if Sys.os_type = "Win32"
1468 then
1469 try Sys.getenv "HOMEPATH" with Not_found -> ""
1470 else
1471 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1473 Filename.concat home "llpp"
1475 let pstate =
1477 let ic = open_in_bin statepath in
1478 let hash = input_value ic in
1479 close_in ic;
1480 hash
1481 with exn ->
1482 if false
1483 then
1484 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1486 Hashtbl.create 1
1488 let savestate () =
1490 let w, h =
1491 match state.fullscreen with
1492 | None -> state.w, state.h
1493 | Some wh -> wh
1495 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1496 let oc = open_out_bin statepath in
1497 output_value oc pstate
1498 with exn ->
1499 if false
1500 then
1501 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1504 let setstate () =
1506 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1507 state.w <- statew;
1508 state.h <- stateh;
1509 state.bookmarks <- statebookmarks;
1510 with Not_found -> ()
1511 | exn ->
1512 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1515 Arg.parse [] (fun s -> state.path <- s) "options:";
1516 let name =
1517 if String.length state.path = 0
1518 then (prerr_endline "filename missing"; exit 1)
1519 else state.path
1522 setstate ();
1523 let _ = Glut.init Sys.argv in
1524 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1525 let () = Glut.initWindowSize state.w state.h in
1526 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1528 let csock, ssock =
1529 if Sys.os_type = "Unix"
1530 then
1531 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1532 else
1533 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1534 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1535 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1536 Unix.bind sock addr;
1537 Unix.listen sock 1;
1538 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1539 Unix.connect csock addr;
1540 let ssock, _ = Unix.accept sock in
1541 Unix.close sock;
1542 let opts sock =
1543 Unix.setsockopt sock Unix.TCP_NODELAY true;
1544 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1546 opts ssock;
1547 opts csock;
1548 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1549 ssock, csock
1552 let () = Glut.displayFunc display in
1553 let () = Glut.reshapeFunc reshape in
1554 let () = Glut.keyboardFunc keyboard in
1555 let () = Glut.specialFunc special in
1556 let () = Glut.idleFunc (Some idle) in
1557 let () = Glut.mouseFunc mouse in
1558 let () = Glut.motionFunc motion in
1559 let () = Glut.passiveMotionFunc pmotion in
1561 init ssock;
1562 state.csock <- csock;
1563 state.ssock <- ssock;
1564 writecmd csock ("open " ^ name ^ "\000");
1566 at_exit savestate;
1568 let rec handlelablglutbug () =
1570 Glut.mainLoop ();
1571 with Glut.BadEnum "key in special_of_int" ->
1572 showtext '!' " LablGlut bug: special key not recognized";
1573 Glut.swapBuffers ();
1574 handlelablglutbug ()
1576 handlelablglutbug ();