Make outlines work with most current version of MuPDF
[llpp.git] / main.ml
blob19cb7527da13e09eb91ae9fc8a28c357ddedb5a6
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 <- []
441 | 'D' ->
442 state.rects <- state.rects1;
443 Glut.postRedisplay ()
445 | 'd' ->
446 state.rects <- state.rects1;
447 Glut.postRedisplay ()
449 | 'C' ->
450 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
451 state.pagecount <- n;
452 let rely = yratio state.y in
453 let maxy = calcheight () in
454 state.y <- truncate (float maxy *. rely);
455 let pages = layout state.y state.h in
456 state.layout <- pages;
457 state.outlines <- Olist [];
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 state.text <- s;
465 showtext ' ' s;
466 Glut.swapBuffers ();
467 (* Glut.postRedisplay () *)
469 | 'F' ->
470 let pageno, c, x0, y0, x1, y1 =
471 Scanf.sscanf cmd "F %d %d %f %f %f %f"
472 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
474 let y = (getpagey pageno) + truncate y0 in
475 addnav ();
476 gotoy y;
477 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
479 | 'R' ->
480 let pageno, c, x0, y0, x1, y1 =
481 Scanf.sscanf cmd "R %d %d %f %f %f %f"
482 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
484 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
486 | 'r' ->
487 let n, w, h, p =
488 Scanf.sscanf cmd "r %d %d %d %s"
489 (fun n w h p -> (n, w, h, p))
491 Hashtbl.replace state.pagemap (n, w) p;
492 let evicted = cbpeekw state.pagecache in
493 if String.length evicted > 0
494 then begin
495 wcmd "free" [`s evicted];
496 let l = Hashtbl.fold (fun k p a ->
497 if evicted = p then k :: a else a) state.pagemap []
499 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
500 end;
501 cbput state.pagecache p;
502 state.inflight <- pred state.inflight;
503 Glut.postRedisplay ()
505 | 'l' ->
506 let (n, w, h) as pagelayout =
507 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
509 state.pages <- pagelayout :: state.pages
511 | 'o' ->
512 let (l, n, t, pos) =
513 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
515 let s = String.sub cmd pos (String.length cmd - pos) in
516 let outline = (s, l, n, t) in
517 let outlines =
518 match state.outlines with
519 | Olist outlines -> Olist (outline :: outlines)
520 | Oarray _ -> Olist [outline]
522 state.outlines <- outlines
524 | _ ->
525 log "unknown cmd `%S'" cmd
528 let getopaque pageno =
529 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
530 with Not_found -> None
533 let cache pageno opaque =
534 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
537 let validopaque opaque = String.length opaque > 0;;
539 let preload l =
540 match getopaque l.pageno with
541 | None when state.inflight < 2+0*(cblen state.pagecache) ->
542 state.inflight <- succ state.inflight;
543 cache l.pageno "";
544 wcmd "render" [`i (l.pageno + 1)
545 ;`i l.pagedimno
546 ;`i l.pagew
547 ;`i l.pageh];
549 | _ -> ()
552 let idle () =
553 if not conf.redispimm && state.y != state.prevy
554 then (
555 state.prevy <- state.y;
556 Glut.postRedisplay ();
558 else
559 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
561 begin match r with
562 | [] ->
563 if conf.preload then begin
564 let h = state.h in
565 let y = if state.y < state.h then 0 else state.y - state.h in
566 let pages = layout y (h*3) in
567 List.iter preload pages;
568 end;
570 | _ ->
571 let cmd = readcmd state.csock in
572 act cmd;
573 end;
576 let search pattern forward =
577 if String.length pattern > 0
578 then
579 let pn, py =
580 match state.layout with
581 | [] -> 0, 0
582 | l :: _ ->
583 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
585 let cmd =
586 let b = makecmd "search"
587 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
589 Buffer.add_char b ',';
590 Buffer.add_string b pattern;
591 Buffer.add_char b '\000';
592 Buffer.contents b;
594 writecmd state.csock cmd;
597 let intentry text key =
598 let c = Char.unsafe_chr key in
599 match c with
600 | '0' .. '9' ->
601 let s = "x" in s.[0] <- c;
602 let text = text ^ s in
603 TEcont text
605 | _ ->
606 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
607 TEcont text
610 let addchar s c =
611 let b = Buffer.create (String.length s + 1) in
612 Buffer.add_string b s;
613 Buffer.add_char b c;
614 Buffer.contents b;
617 let textentry text key =
618 let c = Char.unsafe_chr key in
619 match c with
620 | _ when key >= 32 && key < 127 ->
621 let text = addchar text c in
622 TEcont text
624 | _ ->
625 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
626 TEcont text
629 let optentry text key =
630 let btos b = if b then "on" else "off" in
631 let c = Char.unsafe_chr key in
632 match c with
633 | 'r' ->
634 conf.rectsel <- not conf.rectsel;
635 TEdone ("rectsel " ^ (btos conf.rectsel))
637 | 'i' ->
638 conf.icase <- not conf.icase;
639 TEdone ("case insensitive search " ^ (btos conf.icase))
641 | 'p' ->
642 conf.preload <- not conf.preload;
643 TEdone ("preload " ^ (btos conf.preload))
645 | 't' ->
646 conf.titletext <- not conf.titletext;
647 TEdone ("titletext " ^ (btos conf.titletext))
649 | 'd' ->
650 conf.redispimm <- not conf.redispimm;
651 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
653 | 'v' ->
654 conf.verbose <- not conf.verbose;
655 TEdone ("verbose " ^ (btos conf.verbose))
657 | 'h' ->
658 conf.maxhfit <- not conf.maxhfit;
659 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
660 TEdone ("maxhfit " ^ (btos conf.maxhfit))
662 | 'q' ->
663 conf.markonquit <- not conf.markonquit;
664 TEdone ("bookmark on quit " ^ btos conf.markonquit)
666 | _ ->
667 state.text <- Printf.sprintf "bad option %d `%c'" key c;
668 TEstop
671 let maxoutlinerows () = (state.h - 31) / 16;;
673 let enterselector allowdel outlines errmsg =
674 if Array.length outlines = 0
675 then (
676 showtext ' ' errmsg;
677 Glut.swapBuffers ()
679 else
680 let pageno =
681 match state.layout with
682 | [] -> -1
683 | {pageno=pageno} :: rest -> pageno
685 let active =
686 let rec loop n =
687 if n = Array.length outlines
688 then 0
689 else
690 let (_, _, outlinepageno, _) = outlines.(n) in
691 if outlinepageno >= pageno then n else loop (n+1)
693 loop 0
695 state.outline <-
696 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
697 Glut.postRedisplay ();
700 let enteroutlinemode () =
701 let outlines =
702 match state.outlines with
703 | Oarray a -> a
704 | Olist l ->
705 let a = Array.of_list (List.rev l) in
706 state.outlines <- Oarray a;
709 enterselector false outlines "Documents has no outline";
712 let enterbookmarkmode () =
713 let bookmarks = Array.of_list state.bookmarks in
714 enterselector true bookmarks "Documents has no bookmarks (yet)";
718 let quickbookmark ?title () =
719 match state.layout with
720 | [] -> ()
721 | l :: _ ->
722 let title =
723 match title with
724 | None ->
725 let sec = Unix.gettimeofday () in
726 let tm = Unix.localtime sec in
727 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
728 l.pageno
729 tm.Unix.tm_mday
730 tm.Unix.tm_mon
731 (tm.Unix.tm_year + 1900)
732 tm.Unix.tm_hour
733 tm.Unix.tm_min
734 | Some title -> title
736 state.bookmarks <-
737 (title, 0, l.pageno, l.pagey) :: state.bookmarks
740 let viewkeyboard ~key ~x ~y =
741 let enttext te =
742 state.textentry <- te;
743 state.text <- "";
744 enttext ();
745 Glut.swapBuffers ()
747 match state.textentry with
748 | None ->
749 let c = Char.chr key in
750 begin match c with
751 | '\027' | 'q' ->
752 exit 0
754 | '\008' ->
755 let y = getnav () in
756 gotoy y
758 | 'o' ->
759 enteroutlinemode ()
761 | 'u' ->
762 state.rects <- [];
763 state.text <- "";
764 Glut.postRedisplay ()
766 | '/' | '?' ->
767 let ondone isforw s =
768 state.searchpattern <- s;
769 search s isforw
771 enttext (Some (c, "", textentry, ondone (c ='/')))
773 | '+' ->
774 let ondone s =
775 let n =
776 try int_of_string s with exc ->
777 state.text <- Printf.sprintf "bad integer `%s': %s"
778 s (Printexc.to_string exc);
779 max_int
781 if n != max_int
782 then (
783 conf.pagebias <- n;
784 state.text <- "page bias is now " ^ string_of_int n;
787 enttext (Some ('+', "", intentry, ondone))
789 | '-' ->
790 let ondone msg =
791 state.text <- msg;
793 enttext (Some ('-', "", optentry, ondone))
795 | '0' .. '9' ->
796 let ondone s =
797 let n =
798 try int_of_string s with exc ->
799 state.text <- Printf.sprintf "bad integer `%s': %s"
800 s (Printexc.to_string exc);
803 if n >= 0
804 then (
805 addnav ();
806 state.y <- y;
807 gotoy (getpagey (n + conf.pagebias - 1))
810 let pageentry text key =
811 match Char.unsafe_chr key with
812 | 'g' -> TEdone text
813 | _ -> intentry text key
815 let text = "x" in text.[0] <- c;
816 enttext (Some (':', text, pageentry, ondone))
818 | 'b' ->
819 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
820 reshape state.w state.h;
822 | 'f' ->
823 begin match state.fullscreen with
824 | None ->
825 state.fullscreen <- Some (state.w, state.h);
826 Glut.fullScreen ()
827 | Some (w, h) ->
828 state.fullscreen <- None;
829 Glut.reshapeWindow ~w ~h
832 | 'n' ->
833 search state.searchpattern true
835 | 'p' ->
836 search state.searchpattern false
838 | 't' ->
839 begin match state.layout with
840 | [] -> ()
841 | l :: _ ->
842 gotoy (state.y - l.pagey);
845 | ' ' | 'N' ->
846 begin match List.rev state.layout with
847 | [] -> ()
848 | l :: _ ->
849 gotoy (clamp (l.pageh - l.pagey))
852 | '\127' | 'P' ->
853 begin match state.layout with
854 | [] -> ()
855 | l :: _ ->
856 gotoy (clamp (-l.pageh));
859 | '=' ->
860 let f (fn, ln) l =
861 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
863 let fn, ln = List.fold_left f (-1, -1) state.layout in
864 let s =
865 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
866 let percent = (100. *. (float state.y /. float maxy)) in
867 if fn = ln
868 then
869 Printf.sprintf "Page %d of %d %.2f%%"
870 (fn+1) state.pagecount percent
871 else
872 Printf.sprintf
873 "Pages %d-%d of %d %.2f%%"
874 (fn+1) (ln+1) state.pagecount percent
876 showtext ' ' s;
877 Glut.swapBuffers ()
879 | 'w' ->
880 begin match state.layout with
881 | [] -> ()
882 | l :: _ ->
883 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
884 Glut.postRedisplay ();
887 | '\'' ->
888 enterbookmarkmode ()
890 | 'm' ->
891 let ondone s =
892 match state.layout with
893 | l :: _ ->
894 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
895 | _ -> ()
897 enttext (Some ('~', "", textentry, ondone))
899 | '~' ->
900 quickbookmark ();
901 showtext ' ' "Quick bookmark added";
902 Glut.swapBuffers ()
904 | 'z' ->
905 begin match state.layout with
906 | l :: _ ->
907 let a = getpagewh l.pagedimno in
908 let w = truncate (a.(1) -. a.(0))
909 and h = truncate (a.(3) -. a.(0)) in
910 Glut.reshapeWindow (w + conf.scrollw) h;
911 Glut.postRedisplay ();
913 | [] -> ()
916 | _ ->
917 vlog "huh? %d %c" key (Char.chr key);
920 | Some (c, text, onkey, ondone) when key = 8 ->
921 let len = String.length text in
922 if len = 0 || len = 1
923 then (
924 state.textentry <- None;
925 Glut.postRedisplay ();
927 else (
928 let s = String.sub text 0 (len - 1) in
929 enttext (Some (c, s, onkey, ondone))
932 | Some (c, text, onkey, ondone) ->
933 begin match Char.unsafe_chr key with
934 | '\r' | '\n' ->
935 ondone text;
936 state.textentry <- None;
937 Glut.postRedisplay ()
939 | '\027' ->
940 state.textentry <- None;
941 Glut.postRedisplay ()
943 | _ ->
944 begin match onkey text key with
945 | TEdone text ->
946 state.textentry <- None;
947 ondone text;
948 Glut.postRedisplay ()
950 | TEcont text ->
951 enttext (Some (c, text, onkey, ondone));
953 | TEstop ->
954 state.textentry <- None;
955 Glut.postRedisplay ()
956 end;
957 end;
960 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
961 let search active pattern incr =
962 let re = Str.regexp_case_fold pattern in
963 let rec loop n =
964 if n = Array.length outlines || n = -1 then None else
965 let (s, _, _, _) = outlines.(n) in
967 (try ignore (Str.search_forward re s 0); true
968 with Not_found -> false)
969 then (
970 let maxrows = maxoutlinerows () in
971 if first > n
972 then Some (n, max 0 (n - maxrows))
973 else Some (n, max first (n - maxrows))
975 else loop (n + incr)
977 loop active
979 match key with
980 | 27 ->
981 if String.length qsearch = 0
982 then (
983 state.text <- "";
984 state.outline <- None;
985 Glut.postRedisplay ();
987 else (
988 state.text <- "";
989 state.outline <- Some (allowdel, active, first, outlines, "");
990 Glut.postRedisplay ();
993 | 18 | 19 ->
994 let incr = if key = 18 then -1 else 1 in
995 let active, first =
996 match search (active + incr) qsearch incr with
997 | None -> active, first
998 | Some af -> af
1000 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1001 Glut.postRedisplay ();
1003 | 8 ->
1004 let len = String.length qsearch in
1005 if len = 0
1006 then ()
1007 else (
1008 if len = 1
1009 then (
1010 state.text <- "";
1011 state.outline <- Some (allowdel, active, first, outlines, "");
1013 else
1014 let qsearch = String.sub qsearch 0 (len - 1) in
1015 state.text <- qsearch;
1016 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1018 Glut.postRedisplay ()
1020 | 13 ->
1021 if active < Array.length outlines
1022 then (
1023 let (_, _, n, t) = outlines.(active) in
1024 gotopage n t;
1026 state.text <- "";
1027 if allowdel then state.bookmarks <- Array.to_list outlines;
1028 state.outline <- None;
1029 Glut.postRedisplay ();
1031 | _ when key >= 32 && key < 127 ->
1032 let pattern = addchar qsearch (Char.chr key) in
1033 let pattern, active, first =
1034 match search active pattern 1 with
1035 | None -> qsearch, active, first
1036 | Some (active, first) -> (pattern, active, first)
1038 state.text <- pattern;
1039 state.outline <- Some (allowdel, active, first, outlines, pattern);
1040 Glut.postRedisplay ()
1042 | 127 when allowdel ->
1043 let len = Array.length outlines - 1 in
1044 if len = 0
1045 then (
1046 state.outline <- None;
1047 state.bookmarks <- [];
1049 else (
1050 let bookmarks = Array.init len
1051 (fun i ->
1052 let i = if i >= active then i + 1 else i in
1053 outlines.(i)
1056 state.outline <-
1057 Some (allowdel,
1058 min active (len-1),
1059 min first (len-1),
1060 bookmarks, qsearch)
1063 Glut.postRedisplay ()
1065 | _ -> log "unknown key %d" key
1068 let keyboard ~key ~x ~y =
1069 match state.outline with
1070 | None -> viewkeyboard ~key ~x ~y
1071 | Some outline -> outlinekeyboard ~key ~x ~y outline
1074 let special ~key ~x ~y =
1075 match state.outline with
1076 | None ->
1077 let y =
1078 match key with
1079 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1080 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1081 | Glut.KEY_DOWN -> clamp conf.scrollincr
1082 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1083 | Glut.KEY_PAGE_DOWN -> clamp state.h
1084 | Glut.KEY_HOME -> addnav (); 0
1085 | Glut.KEY_END ->
1086 addnav ();
1087 state.maxy - (if conf.maxhfit then state.h else 0)
1088 | _ -> state.y
1090 state.text <- "";
1091 gotoy y
1093 | Some (allowdel, active, first, outlines, qsearch) ->
1094 let maxrows = maxoutlinerows () in
1095 let navigate incr =
1096 let active = active + incr in
1097 let active = max 0 (min active (Array.length outlines - 1)) in
1098 let first =
1099 if active > first
1100 then
1101 let rows = active - first in
1102 if rows > maxrows then first + incr else first
1103 else active
1105 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1106 Glut.postRedisplay ()
1108 match key with
1109 | Glut.KEY_UP -> navigate ~-1
1110 | Glut.KEY_DOWN -> navigate 1
1111 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1112 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1114 | Glut.KEY_HOME ->
1115 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1116 Glut.postRedisplay ()
1118 | Glut.KEY_END ->
1119 let active = Array.length outlines - 1 in
1120 let first = max 0 (active - maxrows) in
1121 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1122 Glut.postRedisplay ()
1124 | _ -> ()
1127 let drawplaceholder l =
1128 if true
1129 then (
1130 GlDraw.color (0.2, 0.2, 0.2);
1131 GlDraw.color (1.0, 1.0, 1.0);
1132 GlDraw.rect
1133 (0.0, float l.pagedispy)
1134 (float l.pagew, float (l.pagedispy + l.pagevh))
1136 let x = 0.0
1137 and y = float (l.pagedispy + 13) in
1138 let font = Glut.BITMAP_8_BY_13 in
1139 GlDraw.color (0.0, 0.0, 0.0);
1140 GlPix.raster_pos ~x ~y ();
1141 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1142 ("Loading " ^ string_of_int l.pageno);
1144 else (
1145 GlDraw.begins `quads;
1146 GlDraw.vertex2 (0.0, float l.pagedispy);
1147 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1148 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1149 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1150 GlDraw.ends ();
1154 let now () = Unix.gettimeofday ();;
1156 let drawpage i l =
1157 begin match getopaque l.pageno with
1158 | Some opaque when validopaque opaque ->
1159 GlDraw.color (1.0, 1.0, 1.0);
1160 let a = now () in
1161 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1162 let b = now () in
1163 let d = b-.a in
1164 vlog "draw %f sec" d;
1166 | Some _ ->
1167 drawplaceholder l
1169 | None ->
1170 drawplaceholder l;
1171 if state.inflight < cblen state.pagecache
1172 then (
1173 List.iter preload state.layout;
1175 else (
1176 vlog "inflight %d" state.inflight;
1178 end;
1179 GlDraw.color (0.5, 0.5, 0.5);
1180 GlDraw.rect
1181 (0., float i)
1182 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1184 l.pagedispy + l.pagevh;
1187 let scrollindicator () =
1188 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1189 GlDraw.color (0.64 , 0.64, 0.64);
1190 GlDraw.rect
1191 (float (state.w - conf.scrollw), 0.)
1192 (float state.w, float state.h)
1194 GlDraw.color (0.0, 0.0, 0.0);
1195 let sh = (float (maxy + state.h) /. float state.h) in
1196 let sh = float state.h /. sh in
1197 let sh = max sh (float conf.scrollh) in
1199 let percent =
1200 if state.y = state.maxy
1201 then 1.0
1202 else float state.y /. float maxy
1204 let position = (float state.h -. sh) *. percent in
1206 let position =
1207 if position +. sh > float state.h
1208 then
1209 float state.h -. sh
1210 else
1211 position
1213 GlDraw.rect
1214 (float (state.w - conf.scrollw), position)
1215 (float state.w, position +. sh)
1219 let showsel () =
1220 match state.mstate with
1221 | Mnone ->
1224 | Msel ((x0, y0), (x1, y1)) ->
1225 let y0' = min y0 y1
1226 and y1 = max y0 y1 in
1227 let y0 = y0' in
1228 let f l =
1229 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1230 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1231 then
1232 match getopaque l.pageno with
1233 | Some opaque when validopaque opaque ->
1234 let oy = -l.pagey + l.pagedispy in
1235 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1236 | _ -> ()
1238 List.iter f state.layout
1241 let showrects () =
1242 Gl.enable `blend;
1243 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1244 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1245 List.iter
1246 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1247 List.iter (fun l ->
1248 if l.pageno = pageno
1249 then (
1250 let d = float (l.pagedispy - l.pagey) in
1251 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1252 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1254 ) state.layout
1255 ) state.rects
1257 Gl.disable `blend;
1260 let showoutline = function
1261 | None -> ()
1262 | Some (allowdel, active, first, outlines, qsearch) ->
1263 Gl.enable `blend;
1264 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1265 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1266 GlDraw.rect (0., 0.) (float state.w, float state.h);
1267 Gl.disable `blend;
1269 GlDraw.color (1., 1., 1.);
1270 let font = Glut.BITMAP_9_BY_15 in
1271 let draw_string x y s =
1272 GlPix.raster_pos ~x ~y ();
1273 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1275 let rec loop row =
1276 if row = Array.length outlines || (row - first) * 16 > state.h
1277 then ()
1278 else (
1279 let (s, l, _, _) = outlines.(row) in
1280 let y = (row - first) * 16 in
1281 let x = 5 + 5*l in
1282 if row = active
1283 then (
1284 Gl.enable `blend;
1285 GlDraw.polygon_mode `both `line;
1286 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1287 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1288 GlDraw.rect (0., float (y + 1))
1289 (float (state.w - conf.scrollw - 1), float (y + 18));
1290 GlDraw.polygon_mode `both `fill;
1291 Gl.disable `blend;
1292 GlDraw.color (1., 1., 1.);
1294 draw_string (float x) (float (y + 16)) s;
1295 loop (row+1)
1298 loop first
1301 let display () =
1302 let lasty = List.fold_left drawpage 0 (state.layout) in
1303 GlDraw.color (0.5, 0.5, 0.5);
1304 GlDraw.rect
1305 (0., float lasty)
1306 (float (state.w - conf.scrollw), float state.h)
1308 showrects ();
1309 scrollindicator ();
1310 showsel ();
1311 showoutline state.outline;
1312 enttext ();
1313 Glut.swapBuffers ();
1316 let getlink x y =
1317 let rec f = function
1318 | l :: rest ->
1319 begin match getopaque l.pageno with
1320 | Some opaque when validopaque opaque ->
1321 let y = y - l.pagedispy in
1322 if y > 0
1323 then
1324 let y = l.pagey + y in
1325 match getlink opaque x y with
1326 | None -> f rest
1327 | some -> some
1328 else
1329 f rest
1330 | _ ->
1331 f rest
1333 | [] -> None
1335 f state.layout
1338 let checklink x y =
1339 let rec f = function
1340 | l :: rest ->
1341 begin match getopaque l.pageno with
1342 | Some opaque when validopaque opaque ->
1343 let y = y - l.pagedispy in
1344 if y > 0
1345 then
1346 let y = l.pagey + y in
1347 if checklink opaque x y then true else f rest
1348 else
1349 f rest
1350 | _ ->
1351 f rest
1353 | [] -> false
1355 f state.layout
1358 let mouse ~button ~bstate ~x ~y =
1359 match button with
1360 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1361 let incr =
1362 if n = 3
1363 then
1364 -conf.scrollincr
1365 else
1366 conf.scrollincr
1368 let incr = incr * 2 in
1369 let y = clamp incr in
1370 gotoy y
1372 | Glut.LEFT_BUTTON when state.outline = None ->
1373 let dest = if bstate = Glut.DOWN then getlink x y else None in
1374 begin match dest with
1375 | Some (pageno, top) ->
1376 gotopage pageno top
1378 | None ->
1379 if bstate = Glut.DOWN
1380 then (
1381 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1382 state.mstate <- Msel ((x, y), (x, y));
1383 Glut.postRedisplay ()
1385 else (
1386 Glut.setCursor Glut.CURSOR_INHERIT;
1387 state.mstate <- Mnone;
1391 | _ ->
1394 let mouse ~button ~state ~x ~y = mouse button state x y;;
1396 let motion ~x ~y =
1397 if state.outline = None
1398 then
1399 match state.mstate with
1400 | Mnone -> ()
1401 | Msel (a, _) ->
1402 state.mstate <- Msel (a, (x, y));
1403 Glut.postRedisplay ()
1406 let pmotion ~x ~y =
1407 if state.outline = None
1408 then
1409 match state.mstate with
1410 | Mnone when (checklink x y) ->
1411 Glut.setCursor Glut.CURSOR_INFO
1413 | Mnone ->
1414 Glut.setCursor Glut.CURSOR_INHERIT
1416 | Msel (a, _) ->
1420 let () =
1421 let statepath = (Sys.getenv "HOME") ^ "/.config/llpp" in
1422 let pstate =
1424 let ic = open_in_bin statepath in
1425 let hash = input_value ic in
1426 close_in ic;
1427 hash
1428 with exn ->
1429 if false
1430 then
1431 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1433 Hashtbl.create 1
1435 let savestate () =
1437 let w, h =
1438 match state.fullscreen with
1439 | None -> state.w, state.h
1440 | Some wh -> wh
1442 if conf.markonquit then quickbookmark ();
1443 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1444 let oc = open_out_bin statepath in
1445 output_value oc pstate
1446 with exn ->
1447 if false
1448 then
1449 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1452 let setstate () =
1454 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1455 state.w <- statew;
1456 state.h <- stateh;
1457 state.bookmarks <- statebookmarks;
1458 with Not_found -> ()
1459 | exn ->
1460 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1463 Arg.parse [] (fun s -> state.path <- s) "options:";
1464 let name =
1465 if String.length state.path = 0
1466 then (prerr_endline "filename missing"; exit 1)
1467 else state.path
1470 setstate ();
1471 let _ = Glut.init Sys.argv in
1472 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1473 let () = Glut.initWindowSize state.w state.h in
1474 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1476 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1478 init ssock;
1479 state.csock <- csock;
1480 state.ssock <- ssock;
1481 writecmd csock ("open " ^ name ^ "\000");
1483 let () = Glut.displayFunc display in
1484 let () = Glut.reshapeFunc reshape in
1485 let () = Glut.keyboardFunc keyboard in
1486 let () = Glut.specialFunc special in
1487 let () = Glut.idleFunc (Some idle) in
1488 let () = Glut.mouseFunc mouse in
1489 let () = Glut.motionFunc motion in
1490 let () = Glut.passiveMotionFunc pmotion in
1492 at_exit savestate;
1494 let rec handlelablglutbug () =
1496 Glut.mainLoop ();
1497 with Glut.BadEnum "key in special_of_int" ->
1498 showtext '!' " LablGlut bug: special key not recognized";
1499 Glut.swapBuffers ();
1500 handlelablglutbug ()
1502 handlelablglutbug ();