Update to reflect changes in MuPDF
[llpp.git] / main.ml
blob0579e2af99ca77e81acba72ce0e8e7aed281771b
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
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 :
117 (char * string * (string -> int -> te) * (string -> unit)) option
118 ; mutable outlines : outlines
119 ; mutable outline : (bool * int * int * outline array * string) option
120 ; mutable bookmarks : outline list
121 ; mutable path : string
125 let conf =
126 { scrollw = 5
127 ; scrollh = 12
128 ; icase = true
129 ; rectsel = true
130 ; preload = false
131 ; titletext = false
132 ; pagebias = 0
133 ; redispimm = false
134 ; verbose = false
135 ; scrollincr = 18
136 ; maxhfit = true
137 ; markonquit = false
141 let state =
142 { csock = Unix.stdin
143 ; ssock = Unix.stdin
144 ; w = 900
145 ; h = 900
146 ; y = 0
147 ; prevy = 0
148 ; layout = []
149 ; maxy = max_int
150 ; pagemap = Hashtbl.create 10
151 ; pagecache = cbnew 10 ""
152 ; pages = []
153 ; pagecount = 0
154 ; inflight = 0
155 ; mstate = Mnone
156 ; navhist = cbnew 100 0.0
157 ; rects = []
158 ; rects1 = []
159 ; text = ""
160 ; fullscreen = None
161 ; textentry = None
162 ; searchpattern = ""
163 ; outlines = Olist []
164 ; outline = None
165 ; bookmarks = []
166 ; path = ""
170 let vlog fmt =
171 if conf.verbose
172 then
173 Printf.kprintf prerr_endline fmt
174 else
175 Printf.kprintf ignore fmt
178 let writecmd fd s =
179 let len = String.length s in
180 let n = 4 + len in
181 let b = Buffer.create n in
182 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
183 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
184 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
185 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
186 Buffer.add_string b s;
187 let s' = Buffer.contents b in
188 let n' = Unix.write fd s' 0 n in
189 if n' != n then failwith "write failed";
192 let readcmd fd =
193 let s = "xxxx" in
194 let n = Unix.read fd s 0 4 in
195 if n != 4 then failwith "incomplete read(len)";
196 let len = 0
197 lor (Char.code s.[0] lsl 24)
198 lor (Char.code s.[1] lsl 16)
199 lor (Char.code s.[2] lsl 8)
200 lor (Char.code s.[3] lsl 0)
202 let s = String.create len in
203 let n = Unix.read fd s 0 len in
204 if n != len then failwith "incomplete read(data)";
208 let yratio y =
209 if y = state.maxy then 1.0
210 else float y /. float state.maxy
213 let makecmd s l =
214 let b = Buffer.create 10 in
215 Buffer.add_string b s;
216 let rec combine = function
217 | [] -> b
218 | x :: xs ->
219 Buffer.add_char b ' ';
220 let s =
221 match x with
222 | `b b -> if b then "1" else "0"
223 | `s s -> s
224 | `i i -> string_of_int i
225 | `f f -> string_of_float f
226 | `I f -> string_of_int (truncate f)
228 Buffer.add_string b s;
229 combine xs;
231 combine l;
234 let wcmd s l =
235 let cmd = Buffer.contents (makecmd s l) in
236 writecmd state.csock cmd;
239 let calcheight () =
240 let rec f pn ph fh l =
241 match l with
242 | (n, _, h) :: rest ->
243 let fh = fh + (n - pn) * ph in
244 f n h fh rest
246 | [] ->
247 let fh = fh + (ph * (state.pagecount - pn)) in
248 max 0 fh
250 let fh = f 0 0 0 state.pages in
254 let getpagey pageno =
255 let rec f pn ph y l =
256 match l with
257 | (n, _, h) :: rest ->
258 if n >= pageno
259 then
260 y + (pageno - pn) * ph
261 else
262 let y = y + (n - pn) * ph in
263 f n h y rest
265 | [] ->
266 y + (pageno - pn) * ph
268 f 0 0 0 state.pages;
271 let layout y sh =
272 let rec f pageno pdimno prev vy py dy l cacheleft accu =
273 if pageno = state.pagecount || cacheleft = 0
274 then accu
275 else
276 let ((_, w, h) as curr), rest, pdimno =
277 match l with
278 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
279 curr, rest, pdimno + 1
280 | _ ->
281 prev, l, pdimno
283 let pageno' = pageno + 1 in
284 if py + h > vy
285 then
286 let py' = vy - py in
287 let vh = h - py' in
288 if dy + vh > sh
289 then
290 let vh = sh - dy in
291 if vh <= 0
292 then
293 accu
294 else
295 let e =
296 { pageno = pageno
297 ; pagedimno = pdimno
298 ; pagew = w
299 ; pageh = h
300 ; pagedispy = dy
301 ; pagey = py'
302 ; pagevh = vh
305 e :: accu
306 else
307 let e =
308 { pageno = pageno
309 ; pagedimno = pdimno
310 ; pagew = w
311 ; pageh = h
312 ; pagedispy = dy
313 ; pagey = py'
314 ; pagevh = vh
317 let accu = e :: accu in
318 f pageno' pdimno curr
319 (vy + vh) (py + h) (dy + vh + 2) rest
320 (pred cacheleft) accu
321 else
322 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
324 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
325 state.maxy <- calcheight ();
326 List.rev accu
329 let clamp incr =
330 let y = state.y + incr in
331 let y = max 0 y in
332 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
336 let gotoy y =
337 let y = max 0 y in
338 let y = min state.maxy y in
339 let pages = layout y state.h in
340 state.y <- y;
341 state.layout <- pages;
342 if conf.redispimm
343 then
344 Glut.postRedisplay ()
348 let addnav () =
349 cbput state.navhist (yratio state.y);
350 cbrfollowlen state.navhist;
353 let getnav () =
354 let y = cbget state.navhist in
355 truncate (y *. float state.maxy)
358 let gotopage n top =
359 let y = getpagey n in
360 addnav ();
361 state.y <- y + top;
362 gotoy state.y;
365 let reshape ~w ~h =
366 let ratio = float w /. float state.w in
367 let fixbookmark (s, l, pageno, pagey) =
368 let pagey = truncate (float pagey *. ratio) in
369 (s, l, pageno, pagey)
371 state.bookmarks <- List.map fixbookmark state.bookmarks;
372 state.w <- w;
373 state.h <- h;
374 GlDraw.viewport 0 0 w h;
375 GlMat.mode `modelview;
376 GlMat.load_identity ();
377 GlMat.mode `projection;
378 GlMat.load_identity ();
379 GlMat.rotate ~x:1.0 ~angle:180.0 ();
380 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
381 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
382 GlClear.color (1., 1., 1.);
383 GlClear.clear [`color];
384 state.layout <- [];
385 state.pages <- [];
386 state.rects <- [];
387 state.text <- "";
388 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
391 let showtext c s =
392 if not conf.titletext
393 then (
394 GlDraw.color (0.0, 0.0, 0.0);
395 GlDraw.rect
396 (0.0, float (state.h - 18))
397 (float (state.w - conf.scrollw - 1), float state.h)
399 let font = Glut.BITMAP_8_BY_13 in
400 GlDraw.color (1.0, 1.0, 1.0);
401 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
402 Glut.bitmapCharacter ~font ~c:(Char.code c);
403 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
405 else
406 let len = String.length s in
407 let dst = String.create (len + 1) in
408 dst.[0] <- c;
409 StringLabels.blit
410 ~src:s
411 ~dst
412 ~src_pos:0
413 ~dst_pos:1
414 ~len
416 Glut.setWindowTitle ~title:dst
419 let enttext () =
420 let len = String.length state.text in
421 match state.textentry with
422 | None ->
423 if len > 0 then showtext ' ' state.text
425 | Some (c, text, _, _) ->
426 let s =
427 if len > 0
428 then
429 text ^ " [" ^ state.text ^ "]"
430 else
431 text
433 showtext c s;
436 let act cmd =
437 match cmd.[0] with
438 | 'c' ->
439 state.pages <- [];
440 state.outlines <- Olist []
442 | 'D' ->
443 state.rects <- state.rects1;
444 Glut.postRedisplay ()
446 | 'd' ->
447 state.rects <- state.rects1;
448 Glut.postRedisplay ()
450 | 'C' ->
451 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
452 state.pagecount <- n;
453 let rely = yratio state.y in
454 let maxy = calcheight () in
455 state.y <- truncate (float maxy *. rely);
456 let pages = layout state.y state.h in
457 state.layout <- pages;
458 Glut.postRedisplay ();
460 | 't' ->
461 let s = Scanf.sscanf cmd "t %n"
462 (fun n -> String.sub cmd n (String.length cmd - n))
464 Glut.setWindowTitle s
466 | 'T' ->
467 let s = Scanf.sscanf cmd "T %n"
468 (fun n -> String.sub cmd n (String.length cmd - n))
470 state.text <- s;
471 showtext ' ' s;
472 Glut.swapBuffers ();
473 (* Glut.postRedisplay () *)
475 | 'F' ->
476 let pageno, c, x0, y0, x1, y1 =
477 Scanf.sscanf cmd "F %d %d %f %f %f %f"
478 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
480 let y = (getpagey pageno) + truncate y0 in
481 addnav ();
482 gotoy y;
483 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
485 | 'R' ->
486 let pageno, c, x0, y0, x1, y1 =
487 Scanf.sscanf cmd "R %d %d %f %f %f %f"
488 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
490 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
492 | 'r' ->
493 let n, w, h, p =
494 Scanf.sscanf cmd "r %d %d %d %s"
495 (fun n w h p -> (n, w, h, p))
497 Hashtbl.replace state.pagemap (n, w) p;
498 let evicted = cbpeekw state.pagecache in
499 if String.length evicted > 0
500 then begin
501 wcmd "free" [`s evicted];
502 let l = Hashtbl.fold (fun k p a ->
503 if evicted = p then k :: a else a) state.pagemap []
505 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
506 end;
507 cbput state.pagecache p;
508 state.inflight <- pred state.inflight;
509 Glut.postRedisplay ()
511 | 'l' ->
512 let (n, w, h) as pagelayout =
513 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
515 state.pages <- pagelayout :: state.pages
517 | 'o' ->
518 let (l, n, t, pos) =
519 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
521 let s = String.sub cmd pos (String.length cmd - pos) in
522 let outline = (s, l, n, t) in
523 let outlines =
524 match state.outlines with
525 | Olist outlines -> Olist (outline :: outlines)
526 | Oarray _ -> Olist [outline]
528 state.outlines <- outlines
530 | _ ->
531 log "unknown cmd `%S'" cmd
534 let getopaque pageno =
535 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
536 with Not_found -> None
539 let cache pageno opaque =
540 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
543 let validopaque opaque = String.length opaque > 0;;
545 let preload l =
546 match getopaque l.pageno with
547 | None when state.inflight < 2+0*(cblen state.pagecache) ->
548 state.inflight <- succ state.inflight;
549 cache l.pageno "";
550 wcmd "render" [`i (l.pageno + 1)
551 ;`i l.pagedimno
552 ;`i l.pagew
553 ;`i l.pageh];
555 | _ -> ()
558 let idle () =
559 if not conf.redispimm && state.y != state.prevy
560 then (
561 state.prevy <- state.y;
562 Glut.postRedisplay ();
564 else
565 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
567 begin match r with
568 | [] ->
569 if conf.preload then begin
570 let h = state.h in
571 let y = if state.y < state.h then 0 else state.y - state.h in
572 let pages = layout y (h*3) in
573 List.iter preload pages;
574 end;
576 | _ ->
577 let cmd = readcmd state.csock in
578 act cmd;
579 end;
582 let search pattern forward =
583 if String.length pattern > 0
584 then
585 let pn, py =
586 match state.layout with
587 | [] -> 0, 0
588 | l :: _ ->
589 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
591 let cmd =
592 let b = makecmd "search"
593 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
595 Buffer.add_char b ',';
596 Buffer.add_string b pattern;
597 Buffer.add_char b '\000';
598 Buffer.contents b;
600 writecmd state.csock cmd;
603 let intentry text key =
604 let c = Char.unsafe_chr key in
605 match c with
606 | '0' .. '9' ->
607 let s = "x" in s.[0] <- c;
608 let text = text ^ s in
609 TEcont text
611 | _ ->
612 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
613 TEcont text
616 let addchar s c =
617 let b = Buffer.create (String.length s + 1) in
618 Buffer.add_string b s;
619 Buffer.add_char b c;
620 Buffer.contents b;
623 let textentry text key =
624 let c = Char.unsafe_chr key in
625 match c with
626 | _ when key >= 32 && key < 127 ->
627 let text = addchar text c in
628 TEcont text
630 | _ ->
631 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
632 TEcont text
635 let optentry text key =
636 let btos b = if b then "on" else "off" in
637 let c = Char.unsafe_chr key in
638 match c with
639 | 'r' ->
640 conf.rectsel <- not conf.rectsel;
641 TEdone ("rectsel " ^ (btos conf.rectsel))
643 | 'i' ->
644 conf.icase <- not conf.icase;
645 TEdone ("case insensitive search " ^ (btos conf.icase))
647 | 'p' ->
648 conf.preload <- not conf.preload;
649 TEdone ("preload " ^ (btos conf.preload))
651 | 't' ->
652 conf.titletext <- not conf.titletext;
653 TEdone ("titletext " ^ (btos conf.titletext))
655 | 'd' ->
656 conf.redispimm <- not conf.redispimm;
657 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
659 | 'v' ->
660 conf.verbose <- not conf.verbose;
661 TEdone ("verbose " ^ (btos conf.verbose))
663 | 'h' ->
664 conf.maxhfit <- not conf.maxhfit;
665 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
666 TEdone ("maxhfit " ^ (btos conf.maxhfit))
668 | 'q' ->
669 conf.markonquit <- not conf.markonquit;
670 TEdone ("bookmark on quit " ^ btos conf.markonquit)
672 | _ ->
673 state.text <- Printf.sprintf "bad option %d `%c'" key c;
674 TEstop
677 let maxoutlinerows () = (state.h - 31) / 16;;
679 let enterselector allowdel outlines errmsg =
680 if Array.length outlines = 0
681 then (
682 showtext ' ' errmsg;
683 Glut.swapBuffers ()
685 else
686 let pageno =
687 match state.layout with
688 | [] -> -1
689 | {pageno=pageno} :: rest -> pageno
691 let active =
692 let rec loop n =
693 if n = Array.length outlines
694 then 0
695 else
696 let (_, _, outlinepageno, _) = outlines.(n) in
697 if outlinepageno >= pageno then n else loop (n+1)
699 loop 0
701 state.outline <-
702 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
703 Glut.postRedisplay ();
706 let enteroutlinemode () =
707 let outlines =
708 match state.outlines with
709 | Oarray a -> a
710 | Olist l ->
711 let a = Array.of_list (List.rev l) in
712 state.outlines <- Oarray a;
715 enterselector false outlines "Documents has no outline";
718 let enterbookmarkmode () =
719 let bookmarks = Array.of_list state.bookmarks in
720 enterselector true bookmarks "Documents has no bookmarks (yet)";
724 let quickbookmark ?title () =
725 match state.layout with
726 | [] -> ()
727 | l :: _ ->
728 let title =
729 match title with
730 | None ->
731 let sec = Unix.gettimeofday () in
732 let tm = Unix.localtime sec in
733 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
734 l.pageno
735 tm.Unix.tm_mday
736 tm.Unix.tm_mon
737 (tm.Unix.tm_year + 1900)
738 tm.Unix.tm_hour
739 tm.Unix.tm_min
740 | Some title -> title
742 state.bookmarks <-
743 (title, 0, l.pageno, l.pagey) :: state.bookmarks
746 let viewkeyboard ~key ~x ~y =
747 let enttext te =
748 state.textentry <- te;
749 state.text <- "";
750 enttext ();
751 Glut.swapBuffers ()
753 match state.textentry with
754 | None ->
755 let c = Char.chr key in
756 begin match c with
757 | '\027' | 'q' ->
758 exit 0
760 | '\008' ->
761 let y = getnav () in
762 gotoy y
764 | 'o' ->
765 enteroutlinemode ()
767 | 'u' ->
768 state.rects <- [];
769 state.text <- "";
770 Glut.postRedisplay ()
772 | '/' | '?' ->
773 let ondone isforw s =
774 state.searchpattern <- s;
775 search s isforw
777 enttext (Some (c, "", textentry, ondone (c ='/')))
779 | '+' ->
780 let ondone s =
781 let n =
782 try int_of_string s with exc ->
783 state.text <- Printf.sprintf "bad integer `%s': %s"
784 s (Printexc.to_string exc);
785 max_int
787 if n != max_int
788 then (
789 conf.pagebias <- n;
790 state.text <- "page bias is now " ^ string_of_int n;
793 enttext (Some ('+', "", intentry, ondone))
795 | '-' ->
796 let ondone msg =
797 state.text <- msg;
799 enttext (Some ('-', "", optentry, ondone))
801 | '0' .. '9' ->
802 let ondone s =
803 let n =
804 try int_of_string s with exc ->
805 state.text <- Printf.sprintf "bad integer `%s': %s"
806 s (Printexc.to_string exc);
809 if n >= 0
810 then (
811 addnav ();
812 state.y <- y;
813 gotoy (getpagey (n + conf.pagebias - 1))
816 let pageentry text key =
817 match Char.unsafe_chr key with
818 | 'g' -> TEdone text
819 | _ -> intentry text key
821 let text = "x" in text.[0] <- c;
822 enttext (Some (':', text, pageentry, ondone))
824 | 'b' ->
825 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
826 reshape state.w state.h;
828 | 'f' ->
829 begin match state.fullscreen with
830 | None ->
831 state.fullscreen <- Some (state.w, state.h);
832 Glut.fullScreen ()
833 | Some (w, h) ->
834 state.fullscreen <- None;
835 Glut.reshapeWindow ~w ~h
838 | 'n' ->
839 search state.searchpattern true
841 | 'p' ->
842 search state.searchpattern false
844 | 't' ->
845 begin match state.layout with
846 | [] -> ()
847 | l :: _ ->
848 gotoy (state.y - l.pagey);
851 | ' ' | 'N' ->
852 begin match List.rev state.layout with
853 | [] -> ()
854 | l :: _ ->
855 gotoy (clamp (l.pageh - l.pagey))
858 | '\127' | 'P' ->
859 begin match state.layout with
860 | [] -> ()
861 | l :: _ ->
862 gotoy (clamp (-l.pageh));
865 | '=' ->
866 let f (fn, ln) l =
867 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
869 let fn, ln = List.fold_left f (-1, -1) state.layout in
870 let s =
871 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
872 let percent = (100. *. (float state.y /. float maxy)) in
873 if fn = ln
874 then
875 Printf.sprintf "Page %d of %d %.2f%%"
876 (fn+1) state.pagecount percent
877 else
878 Printf.sprintf
879 "Pages %d-%d of %d %.2f%%"
880 (fn+1) (ln+1) state.pagecount percent
882 showtext ' ' s;
883 Glut.swapBuffers ()
885 | 'w' ->
886 begin match state.layout with
887 | [] -> ()
888 | l :: _ ->
889 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
890 Glut.postRedisplay ();
893 | '\'' ->
894 enterbookmarkmode ()
896 | 'm' ->
897 let ondone s =
898 match state.layout with
899 | l :: _ ->
900 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
901 | _ -> ()
903 enttext (Some ('~', "", textentry, ondone))
905 | '~' ->
906 quickbookmark ();
907 showtext ' ' "Quick bookmark added";
908 Glut.swapBuffers ()
910 | 'z' ->
911 begin match state.layout with
912 | l :: _ ->
913 let a = getpagewh l.pagedimno in
914 let w = truncate (a.(1) -. a.(0))
915 and h = truncate (a.(3) -. a.(0)) in
916 Glut.reshapeWindow (w + conf.scrollw) h;
917 Glut.postRedisplay ();
919 | [] -> ()
922 | _ ->
923 vlog "huh? %d %c" key (Char.chr key);
926 | Some (c, text, onkey, ondone) when key = 8 ->
927 let len = String.length text in
928 if len = 0 || len = 1
929 then (
930 state.textentry <- None;
931 Glut.postRedisplay ();
933 else (
934 let s = String.sub text 0 (len - 1) in
935 enttext (Some (c, s, onkey, ondone))
938 | Some (c, text, onkey, ondone) ->
939 begin match Char.unsafe_chr key with
940 | '\r' | '\n' ->
941 ondone text;
942 state.textentry <- None;
943 Glut.postRedisplay ()
945 | '\027' ->
946 state.textentry <- None;
947 Glut.postRedisplay ()
949 | _ ->
950 begin match onkey text key with
951 | TEdone text ->
952 state.textentry <- None;
953 ondone text;
954 Glut.postRedisplay ()
956 | TEcont text ->
957 enttext (Some (c, text, onkey, ondone));
959 | TEstop ->
960 state.textentry <- None;
961 Glut.postRedisplay ()
962 end;
963 end;
966 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
967 let search active pattern incr =
968 let re = Str.regexp_case_fold pattern in
969 let rec loop n =
970 if n = Array.length outlines || n = -1 then None else
971 let (s, _, _, _) = outlines.(n) in
973 (try ignore (Str.search_forward re s 0); true
974 with Not_found -> false)
975 then (
976 let maxrows = maxoutlinerows () in
977 if first > n
978 then Some (n, max 0 (n - maxrows))
979 else Some (n, max first (n - maxrows))
981 else loop (n + incr)
983 loop active
985 match key with
986 | 27 ->
987 if String.length qsearch = 0
988 then (
989 state.text <- "";
990 state.outline <- None;
991 Glut.postRedisplay ();
993 else (
994 state.text <- "";
995 state.outline <- Some (allowdel, active, first, outlines, "");
996 Glut.postRedisplay ();
999 | 18 | 19 ->
1000 let incr = if key = 18 then -1 else 1 in
1001 let active, first =
1002 match search (active + incr) qsearch incr with
1003 | None -> active, first
1004 | Some af -> af
1006 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1007 Glut.postRedisplay ();
1009 | 8 ->
1010 let len = String.length qsearch in
1011 if len = 0
1012 then ()
1013 else (
1014 if len = 1
1015 then (
1016 state.text <- "";
1017 state.outline <- Some (allowdel, active, first, outlines, "");
1019 else
1020 let qsearch = String.sub qsearch 0 (len - 1) in
1021 state.text <- qsearch;
1022 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1024 Glut.postRedisplay ()
1026 | 13 ->
1027 if active < Array.length outlines
1028 then (
1029 let (_, _, n, t) = outlines.(active) in
1030 gotopage n t;
1032 state.text <- "";
1033 if allowdel then state.bookmarks <- Array.to_list outlines;
1034 state.outline <- None;
1035 Glut.postRedisplay ();
1037 | _ when key >= 32 && key < 127 ->
1038 let pattern = addchar qsearch (Char.chr key) in
1039 let pattern, active, first =
1040 match search active pattern 1 with
1041 | None -> qsearch, active, first
1042 | Some (active, first) -> (pattern, active, first)
1044 state.text <- pattern;
1045 state.outline <- Some (allowdel, active, first, outlines, pattern);
1046 Glut.postRedisplay ()
1048 | 127 when allowdel ->
1049 let len = Array.length outlines - 1 in
1050 if len = 0
1051 then (
1052 state.outline <- None;
1053 state.bookmarks <- [];
1055 else (
1056 let bookmarks = Array.init len
1057 (fun i ->
1058 let i = if i >= active then i + 1 else i in
1059 outlines.(i)
1062 state.outline <-
1063 Some (allowdel,
1064 min active (len-1),
1065 min first (len-1),
1066 bookmarks, qsearch)
1069 Glut.postRedisplay ()
1071 | _ -> log "unknown key %d" key
1074 let keyboard ~key ~x ~y =
1075 match state.outline with
1076 | None -> viewkeyboard ~key ~x ~y
1077 | Some outline -> outlinekeyboard ~key ~x ~y outline
1080 let special ~key ~x ~y =
1081 match state.outline with
1082 | None ->
1083 let y =
1084 match key with
1085 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1086 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1087 | Glut.KEY_DOWN -> clamp conf.scrollincr
1088 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1089 | Glut.KEY_PAGE_DOWN -> clamp state.h
1090 | Glut.KEY_HOME -> addnav (); 0
1091 | Glut.KEY_END ->
1092 addnav ();
1093 state.maxy - (if conf.maxhfit then state.h else 0)
1094 | _ -> state.y
1096 state.text <- "";
1097 gotoy y
1099 | Some (allowdel, active, first, outlines, qsearch) ->
1100 let maxrows = maxoutlinerows () in
1101 let navigate incr =
1102 let active = active + incr in
1103 let active = max 0 (min active (Array.length outlines - 1)) in
1104 let first =
1105 if active > first
1106 then
1107 let rows = active - first in
1108 if rows > maxrows then first + incr else first
1109 else active
1111 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1112 Glut.postRedisplay ()
1114 match key with
1115 | Glut.KEY_UP -> navigate ~-1
1116 | Glut.KEY_DOWN -> navigate 1
1117 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1118 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1120 | Glut.KEY_HOME ->
1121 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1122 Glut.postRedisplay ()
1124 | Glut.KEY_END ->
1125 let active = Array.length outlines - 1 in
1126 let first = max 0 (active - maxrows) in
1127 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1128 Glut.postRedisplay ()
1130 | _ -> ()
1133 let drawplaceholder l =
1134 if true
1135 then (
1136 GlDraw.color (0.2, 0.2, 0.2);
1137 GlDraw.color (1.0, 1.0, 1.0);
1138 GlDraw.rect
1139 (0.0, float l.pagedispy)
1140 (float l.pagew, float (l.pagedispy + l.pagevh))
1142 let x = 0.0
1143 and y = float (l.pagedispy + 13) in
1144 let font = Glut.BITMAP_8_BY_13 in
1145 GlDraw.color (0.0, 0.0, 0.0);
1146 GlPix.raster_pos ~x ~y ();
1147 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1148 ("Loading " ^ string_of_int l.pageno);
1150 else (
1151 GlDraw.begins `quads;
1152 GlDraw.vertex2 (0.0, float l.pagedispy);
1153 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1154 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1155 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1156 GlDraw.ends ();
1160 let now () = Unix.gettimeofday ();;
1162 let drawpage i l =
1163 begin match getopaque l.pageno with
1164 | Some opaque when validopaque opaque ->
1165 GlDraw.color (1.0, 1.0, 1.0);
1166 let a = now () in
1167 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1168 let b = now () in
1169 let d = b-.a in
1170 vlog "draw %f sec" d;
1172 | Some _ ->
1173 drawplaceholder l
1175 | None ->
1176 drawplaceholder l;
1177 if state.inflight < cblen state.pagecache
1178 then (
1179 List.iter preload state.layout;
1181 else (
1182 vlog "inflight %d" state.inflight;
1184 end;
1185 GlDraw.color (0.5, 0.5, 0.5);
1186 GlDraw.rect
1187 (0., float i)
1188 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1190 l.pagedispy + l.pagevh;
1193 let scrollindicator () =
1194 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1195 GlDraw.color (0.64 , 0.64, 0.64);
1196 GlDraw.rect
1197 (float (state.w - conf.scrollw), 0.)
1198 (float state.w, float state.h)
1200 GlDraw.color (0.0, 0.0, 0.0);
1201 let sh = (float (maxy + state.h) /. float state.h) in
1202 let sh = float state.h /. sh in
1203 let sh = max sh (float conf.scrollh) in
1205 let percent =
1206 if state.y = state.maxy
1207 then 1.0
1208 else float state.y /. float maxy
1210 let position = (float state.h -. sh) *. percent in
1212 let position =
1213 if position +. sh > float state.h
1214 then
1215 float state.h -. sh
1216 else
1217 position
1219 GlDraw.rect
1220 (float (state.w - conf.scrollw), position)
1221 (float state.w, position +. sh)
1225 let showsel () =
1226 match state.mstate with
1227 | Mnone ->
1230 | Msel ((x0, y0), (x1, y1)) ->
1231 let y0' = min y0 y1
1232 and y1 = max y0 y1 in
1233 let y0 = y0' in
1234 let f l =
1235 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1236 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1237 then
1238 match getopaque l.pageno with
1239 | Some opaque when validopaque opaque ->
1240 let oy = -l.pagey + l.pagedispy in
1241 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1242 | _ -> ()
1244 List.iter f state.layout
1247 let showrects () =
1248 Gl.enable `blend;
1249 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1250 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1251 List.iter
1252 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1253 List.iter (fun l ->
1254 if l.pageno = pageno
1255 then (
1256 let d = float (l.pagedispy - l.pagey) in
1257 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1258 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1260 ) state.layout
1261 ) state.rects
1263 Gl.disable `blend;
1266 let showoutline = function
1267 | None -> ()
1268 | Some (allowdel, active, first, outlines, qsearch) ->
1269 Gl.enable `blend;
1270 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1271 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1272 GlDraw.rect (0., 0.) (float state.w, float state.h);
1273 Gl.disable `blend;
1275 GlDraw.color (1., 1., 1.);
1276 let font = Glut.BITMAP_9_BY_15 in
1277 let draw_string x y s =
1278 GlPix.raster_pos ~x ~y ();
1279 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1281 let rec loop row =
1282 if row = Array.length outlines || (row - first) * 16 > state.h
1283 then ()
1284 else (
1285 let (s, l, _, _) = outlines.(row) in
1286 let y = (row - first) * 16 in
1287 let x = 5 + 5*l in
1288 if row = active
1289 then (
1290 Gl.enable `blend;
1291 GlDraw.polygon_mode `both `line;
1292 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1293 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1294 GlDraw.rect (0., float (y + 1))
1295 (float (state.w - conf.scrollw - 1), float (y + 18));
1296 GlDraw.polygon_mode `both `fill;
1297 Gl.disable `blend;
1298 GlDraw.color (1., 1., 1.);
1300 draw_string (float x) (float (y + 16)) s;
1301 loop (row+1)
1304 loop first
1307 let display () =
1308 let lasty = List.fold_left drawpage 0 (state.layout) in
1309 GlDraw.color (0.5, 0.5, 0.5);
1310 GlDraw.rect
1311 (0., float lasty)
1312 (float (state.w - conf.scrollw), float state.h)
1314 showrects ();
1315 scrollindicator ();
1316 showsel ();
1317 showoutline state.outline;
1318 enttext ();
1319 Glut.swapBuffers ();
1322 let getlink x y =
1323 let rec f = function
1324 | l :: rest ->
1325 begin match getopaque l.pageno with
1326 | Some opaque when validopaque opaque ->
1327 let y = y - l.pagedispy in
1328 if y > 0
1329 then
1330 let y = l.pagey + y in
1331 match getlink opaque x y with
1332 | None -> f rest
1333 | some -> some
1334 else
1335 f rest
1336 | _ ->
1337 f rest
1339 | [] -> None
1341 f state.layout
1344 let checklink x y =
1345 let rec f = function
1346 | l :: rest ->
1347 begin match getopaque l.pageno with
1348 | Some opaque when validopaque opaque ->
1349 let y = y - l.pagedispy in
1350 if y > 0
1351 then
1352 let y = l.pagey + y in
1353 if checklink opaque x y then true else f rest
1354 else
1355 f rest
1356 | _ ->
1357 f rest
1359 | [] -> false
1361 f state.layout
1364 let mouse ~button ~bstate ~x ~y =
1365 match button with
1366 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1367 let incr =
1368 if n = 3
1369 then
1370 -conf.scrollincr
1371 else
1372 conf.scrollincr
1374 let incr = incr * 2 in
1375 let y = clamp incr in
1376 gotoy y
1378 | Glut.LEFT_BUTTON when state.outline = None ->
1379 let dest = if bstate = Glut.DOWN then getlink x y else None in
1380 begin match dest with
1381 | Some (pageno, top) ->
1382 gotopage pageno top
1384 | None ->
1385 if bstate = Glut.DOWN
1386 then (
1387 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1388 state.mstate <- Msel ((x, y), (x, y));
1389 Glut.postRedisplay ()
1391 else (
1392 Glut.setCursor Glut.CURSOR_INHERIT;
1393 state.mstate <- Mnone;
1397 | _ ->
1400 let mouse ~button ~state ~x ~y = mouse button state x y;;
1402 let motion ~x ~y =
1403 if state.outline = None
1404 then
1405 match state.mstate with
1406 | Mnone -> ()
1407 | Msel (a, _) ->
1408 state.mstate <- Msel (a, (x, y));
1409 Glut.postRedisplay ()
1412 let pmotion ~x ~y =
1413 if state.outline = None
1414 then
1415 match state.mstate with
1416 | Mnone when (checklink x y) ->
1417 Glut.setCursor Glut.CURSOR_INFO
1419 | Mnone ->
1420 Glut.setCursor Glut.CURSOR_INHERIT
1422 | Msel (a, _) ->
1426 let () =
1427 let statepath = (Sys.getenv "HOME") ^ "/.config/llpp" in
1428 let pstate =
1430 let ic = open_in_bin statepath in
1431 let hash = input_value ic in
1432 close_in ic;
1433 hash
1434 with exn ->
1435 if false
1436 then
1437 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1439 Hashtbl.create 1
1441 let savestate () =
1443 let w, h =
1444 match state.fullscreen with
1445 | None -> state.w, state.h
1446 | Some wh -> wh
1448 if conf.markonquit then quickbookmark ();
1449 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1450 let oc = open_out_bin statepath in
1451 output_value oc pstate
1452 with exn ->
1453 if false
1454 then
1455 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1458 let setstate () =
1460 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1461 state.w <- statew;
1462 state.h <- stateh;
1463 state.bookmarks <- statebookmarks;
1464 with Not_found -> ()
1465 | exn ->
1466 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1469 Arg.parse [] (fun s -> state.path <- s) "options:";
1470 let name =
1471 if String.length state.path = 0
1472 then (prerr_endline "filename missing"; exit 1)
1473 else state.path
1476 setstate ();
1477 let _ = Glut.init Sys.argv in
1478 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1479 let () = Glut.initWindowSize state.w state.h in
1480 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1482 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1484 init ssock;
1485 state.csock <- csock;
1486 state.ssock <- ssock;
1487 writecmd csock ("open " ^ name ^ "\000");
1489 let () = Glut.displayFunc display in
1490 let () = Glut.reshapeFunc reshape in
1491 let () = Glut.keyboardFunc keyboard in
1492 let () = Glut.specialFunc special in
1493 let () = Glut.idleFunc (Some idle) in
1494 let () = Glut.mouseFunc mouse in
1495 let () = Glut.motionFunc motion in
1496 let () = Glut.passiveMotionFunc pmotion in
1498 at_exit savestate;
1500 let rec handlelablglutbug () =
1502 Glut.mainLoop ();
1503 with Glut.BadEnum "key in special_of_int" ->
1504 showtext '!' " LablGlut bug: special key not recognized";
1505 Glut.swapBuffers ();
1506 handlelablglutbug ()
1508 handlelablglutbug ();