mupdf switched to RGBA - follow the lead
[llpp.git] / main.ml
blobb299621674af54ce4feae456b67cafe9bd2f3c20
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 te =
15 | TEstop
16 | TEdone of string
17 | TEcont of string
20 type 'a circbuf =
21 { store : 'a array
22 ; mutable rc : int
23 ; mutable wc : int
24 ; mutable len : int
28 let cbnew n v =
29 { store = Array.create n v
30 ; rc = 0
31 ; wc = 0
32 ; len = 0
36 let cblen b = Array.length b.store;;
38 let cbput b v =
39 let len = cblen b in
40 b.store.(b.wc) <- v;
41 b.wc <- (b.wc + 1) mod len;
42 b.len <- (b.len + 1) mod len;
45 let cbpeekw b = b.store.(b.wc);;
47 let cbget b =
48 let v = b.store.(b.rc) in
49 if b.len = 0
50 then
52 else (
53 let rc = if b.rc = 0 then b.len - 1 else b.rc - 1 in
54 b.rc <- rc;
59 let cbrfollowlen b =
60 b.rc <- b.len - 1;
63 type layout =
64 { pageno : int
65 ; pagedimno : int
66 ; pagew : int
67 ; pageh : int
68 ; pagedispy : int
69 ; pagey : int
70 ; pagevh : int
74 type conf =
75 { mutable scrollw : int
76 ; mutable scrollh : int
77 ; mutable rectsel : bool
78 ; mutable icase : bool
79 ; mutable preload : bool
80 ; mutable pagebias : int
81 ; mutable redispimm : bool
82 ; mutable verbose : bool
83 ; mutable scrollincr : int
84 ; mutable maxhfit : bool
85 ; mutable crophack : bool
89 type outline = string * int * int * int;;
90 type outlines = Oarray of outline array | Olist of outline list;;
92 type state =
93 { mutable csock : Unix.file_descr
94 ; mutable ssock : Unix.file_descr
95 ; mutable w : int
96 ; mutable h : int
97 ; mutable y : int
98 ; mutable prevy : int
99 ; mutable maxy : int
100 ; mutable layout : layout list
101 ; pagemap : ((int * int), string) Hashtbl.t
102 ; mutable pages : (int * int * int) list
103 ; mutable pagecount : int
104 ; pagecache : string circbuf
105 ; navhist : float circbuf
106 ; mutable inflight : int
107 ; mutable mstate : mstate
108 ; mutable searchpattern : string
109 ; mutable rects : (int * int * Gl.point2 * Gl.point2) list
110 ; mutable rects1 : (int * int * Gl.point2 * Gl.point2) list
111 ; mutable text : string
112 ; mutable fullscreen : (int * int) option
113 ; mutable textentry :
114 (char * string * (string -> int -> te) * (string -> unit)) option
115 ; mutable outlines : outlines
116 ; mutable outline : (bool * int * int * outline array * string) option
117 ; mutable bookmarks : outline list
118 ; mutable path : string
122 let conf =
123 { scrollw = 5
124 ; scrollh = 12
125 ; icase = true
126 ; rectsel = true
127 ; preload = false
128 ; pagebias = 0
129 ; redispimm = false
130 ; verbose = false
131 ; scrollincr = 24
132 ; maxhfit = true
133 ; crophack = false
137 let state =
138 { csock = Unix.stdin
139 ; ssock = Unix.stdin
140 ; w = 900
141 ; h = 900
142 ; y = 0
143 ; prevy = 0
144 ; layout = []
145 ; maxy = max_int
146 ; pagemap = Hashtbl.create 10
147 ; pagecache = cbnew 10 ""
148 ; pages = []
149 ; pagecount = 0
150 ; inflight = 0
151 ; mstate = Mnone
152 ; navhist = cbnew 100 0.0
153 ; rects = []
154 ; rects1 = []
155 ; text = ""
156 ; fullscreen = None
157 ; textentry = None
158 ; searchpattern = ""
159 ; outlines = Olist []
160 ; outline = None
161 ; bookmarks = []
162 ; path = ""
166 let vlog fmt =
167 if conf.verbose
168 then
169 Printf.kprintf prerr_endline fmt
170 else
171 Printf.kprintf ignore fmt
174 let writecmd fd s =
175 let len = String.length s in
176 let n = 4 + len in
177 let b = Buffer.create n in
178 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
179 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
180 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
181 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
182 Buffer.add_string b s;
183 let s' = Buffer.contents b in
184 let n' = Unix.write fd s' 0 n in
185 if n' != n then failwith "write failed";
188 let readcmd fd =
189 let s = "xxxx" in
190 let n = Unix.read fd s 0 4 in
191 if n != 4 then failwith "incomplete read(len)";
192 let len = 0
193 lor (Char.code s.[0] lsl 24)
194 lor (Char.code s.[1] lsl 16)
195 lor (Char.code s.[2] lsl 8)
196 lor (Char.code s.[3] lsl 0)
198 let s = String.create len in
199 let n = Unix.read fd s 0 len in
200 if n != len then failwith "incomplete read(data)";
204 let yratio y =
205 if y = state.maxy then 1.0
206 else float y /. float state.maxy
209 let makecmd s l =
210 let b = Buffer.create 10 in
211 Buffer.add_string b s;
212 let rec combine = function
213 | [] -> b
214 | x :: xs ->
215 Buffer.add_char b ' ';
216 let s =
217 match x with
218 | `b b -> if b then "1" else "0"
219 | `s s -> s
220 | `i i -> string_of_int i
221 | `f f -> string_of_float f
222 | `I f -> string_of_int (truncate f)
224 Buffer.add_string b s;
225 combine xs;
227 combine l;
230 let wcmd s l =
231 let cmd = Buffer.contents (makecmd s l) in
232 writecmd state.csock cmd;
235 let calcheight () =
236 let rec f pn ph fh l =
237 match l with
238 | (n, _, h) :: rest ->
239 let fh = fh + (n - pn) * ph in
240 f n h fh rest
242 | [] ->
243 let fh = fh + (ph * (state.pagecount - pn)) in
244 max 0 fh
246 let fh = f 0 0 0 state.pages in
250 let getpagey pageno =
251 let rec f pn ph y l =
252 match l with
253 | (n, _, h) :: rest ->
254 if n >= pageno
255 then
256 y + (pageno - pn) * ph
257 else
258 let y = y + (n - pn) * ph in
259 f n h y rest
261 | [] ->
262 y + (pageno - pn) * ph
264 f 0 0 0 state.pages;
267 let layout y sh =
268 let rec f pageno pdimno prev vy py dy l cacheleft accu =
269 if pageno = state.pagecount || cacheleft = 0
270 then accu
271 else
272 let ((_, w, h) as curr), rest, pdimno =
273 match l with
274 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
275 curr, rest, pdimno + 1
276 | _ ->
277 prev, l, pdimno
279 let pageno' = pageno + 1 in
280 if py + h > vy
281 then
282 let py' = vy - py in
283 let vh = h - py' in
284 if dy + vh > sh
285 then
286 let vh = sh - dy in
287 if vh <= 0
288 then
289 accu
290 else
291 let e =
292 { pageno = pageno
293 ; pagedimno = pdimno
294 ; pagew = w
295 ; pageh = h
296 ; pagedispy = dy
297 ; pagey = py'
298 ; pagevh = vh
301 e :: accu
302 else
303 let e =
304 { pageno = pageno
305 ; pagedimno = pdimno
306 ; pagew = w
307 ; pageh = h
308 ; pagedispy = dy
309 ; pagey = py'
310 ; pagevh = vh
313 let accu = e :: accu in
314 f pageno' pdimno curr
315 (vy + vh) (py + h) (dy + vh + 2) rest
316 (pred cacheleft) accu
317 else
318 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
320 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
321 state.maxy <- calcheight ();
322 List.rev accu
325 let clamp incr =
326 let y = state.y + incr in
327 let y = max 0 y in
328 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
332 let gotoy y =
333 let y = max 0 y in
334 let y = min state.maxy y in
335 let pages = layout y state.h in
336 state.y <- y;
337 state.layout <- pages;
338 if conf.redispimm
339 then
340 Glut.postRedisplay ()
344 let addnav () =
345 cbput state.navhist (yratio state.y);
346 cbrfollowlen state.navhist;
349 let getnav () =
350 let y = cbget state.navhist in
351 truncate (y *. float state.maxy)
354 let gotopage n top =
355 let y = getpagey n in
356 addnav ();
357 state.y <- y + top;
358 gotoy state.y;
361 let reshape ~w ~h =
362 let ratio = float w /. float state.w in
363 let fixbookmark (s, l, pageno, pagey) =
364 let pagey = truncate (float pagey *. ratio) in
365 (s, l, pageno, pagey)
367 state.bookmarks <- List.map fixbookmark state.bookmarks;
368 state.w <- w;
369 state.h <- h;
370 GlDraw.viewport 0 0 w h;
371 GlMat.mode `modelview;
372 GlMat.load_identity ();
373 GlMat.mode `projection;
374 GlMat.load_identity ();
375 GlMat.rotate ~x:1.0 ~angle:180.0 ();
376 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
377 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
378 GlClear.color (1., 1., 1.);
379 GlClear.clear [`color];
380 state.layout <- [];
381 state.pages <- [];
382 state.rects <- [];
383 state.text <- "";
384 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
387 let showtext c s =
388 GlDraw.color (0.0, 0.0, 0.0);
389 GlDraw.rect
390 (0.0, float (state.h - 18))
391 (float (state.w - conf.scrollw - 1), float state.h)
393 let font = Glut.BITMAP_8_BY_13 in
394 GlDraw.color (1.0, 1.0, 1.0);
395 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
396 Glut.bitmapCharacter ~font ~c:(Char.code c);
397 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
400 let enttext () =
401 let len = String.length state.text in
402 match state.textentry with
403 | None ->
404 if len > 0 then showtext ' ' state.text
406 | Some (c, text, _, _) ->
407 let s =
408 if len > 0
409 then
410 text ^ " [" ^ state.text ^ "]"
411 else
412 text
414 showtext c s;
417 let act cmd =
418 match cmd.[0] with
419 | 'c' ->
420 state.pages <- [];
421 state.outlines <- Olist []
423 | 'D' ->
424 state.rects <- state.rects1;
425 Glut.postRedisplay ()
427 | 'd' ->
428 state.rects <- state.rects1;
429 Glut.postRedisplay ()
431 | 'C' ->
432 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
433 state.pagecount <- n;
434 let rely = yratio state.y in
435 let maxy = calcheight () in
436 state.y <- truncate (float maxy *. rely);
437 let pages = layout state.y state.h in
438 state.layout <- pages;
439 Glut.postRedisplay ();
441 | 't' ->
442 let s = Scanf.sscanf cmd "t %n"
443 (fun n -> String.sub cmd n (String.length cmd - n))
445 Glut.setWindowTitle s
447 | 'T' ->
448 let s = Scanf.sscanf cmd "T %n"
449 (fun n -> String.sub cmd n (String.length cmd - n))
451 state.text <- s;
452 showtext ' ' s;
453 Glut.swapBuffers ();
455 | 'V' ->
456 if conf.verbose
457 then
458 let s = Scanf.sscanf cmd "V %n"
459 (fun n -> String.sub cmd n (String.length cmd - n))
461 state.text <- s;
462 showtext ' ' s;
463 Glut.swapBuffers ();
465 | 'F' ->
466 let pageno, c, x0, y0, x1, y1 =
467 Scanf.sscanf cmd "F %d %d %f %f %f %f"
468 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
470 let y = (getpagey pageno) + truncate y0 in
471 addnav ();
472 gotoy y;
473 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
475 | 'R' ->
476 let pageno, c, x0, y0, x1, y1 =
477 Scanf.sscanf cmd "R %d %d %f %f %f %f"
478 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
480 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
482 | 'r' ->
483 let n, w, h, p =
484 Scanf.sscanf cmd "r %d %d %d %s"
485 (fun n w h p -> (n, w, h, p))
487 Hashtbl.replace state.pagemap (n, w) p;
488 let evicted = cbpeekw state.pagecache in
489 if String.length evicted > 0
490 then begin
491 wcmd "free" [`s evicted];
492 let l = Hashtbl.fold (fun k p a ->
493 if evicted = p then k :: a else a) state.pagemap []
495 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
496 end;
497 cbput state.pagecache p;
498 state.inflight <- pred state.inflight;
499 Glut.postRedisplay ()
501 | 'l' ->
502 let (n, w, h) as pagelayout =
503 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
505 state.pages <- pagelayout :: state.pages
507 | 'o' ->
508 let (l, n, t, pos) =
509 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
511 let s = String.sub cmd pos (String.length cmd - pos) in
512 let outline = (s, l, n, t) in
513 let outlines =
514 match state.outlines with
515 | Olist outlines -> Olist (outline :: outlines)
516 | Oarray _ -> Olist [outline]
518 state.outlines <- outlines
520 | _ ->
521 log "unknown cmd `%S'" cmd
524 let getopaque pageno =
525 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
526 with Not_found -> None
529 let cache pageno opaque =
530 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
533 let validopaque opaque = String.length opaque > 0;;
535 let preload l =
536 match getopaque l.pageno with
537 | None when state.inflight < 2+0*(cblen state.pagecache) ->
538 state.inflight <- succ state.inflight;
539 cache l.pageno "";
540 wcmd "render" [`i (l.pageno + 1)
541 ;`i l.pagedimno
542 ;`i l.pagew
543 ;`i l.pageh];
545 | _ -> ()
548 let idle () =
549 if not conf.redispimm && state.y != state.prevy
550 then (
551 state.prevy <- state.y;
552 Glut.postRedisplay ();
554 else
555 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
557 begin match r with
558 | [] ->
559 if conf.preload then begin
560 let h = state.h in
561 let y = if state.y < state.h then 0 else state.y - state.h in
562 let pages = layout y (h*3) in
563 List.iter preload pages;
564 end;
566 | _ ->
567 let cmd = readcmd state.csock in
568 act cmd;
569 end;
572 let search pattern forward =
573 if String.length pattern > 0
574 then
575 let pn, py =
576 match state.layout with
577 | [] -> 0, 0
578 | l :: _ ->
579 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
581 let cmd =
582 let b = makecmd "search"
583 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
585 Buffer.add_char b ',';
586 Buffer.add_string b pattern;
587 Buffer.add_char b '\000';
588 Buffer.contents b;
590 writecmd state.csock cmd;
593 let intentry text key =
594 let c = Char.unsafe_chr key in
595 match c with
596 | '0' .. '9' ->
597 let s = "x" in s.[0] <- c;
598 let text = text ^ s in
599 TEcont text
601 | _ ->
602 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
603 TEcont text
606 let addchar s c =
607 let b = Buffer.create (String.length s + 1) in
608 Buffer.add_string b s;
609 Buffer.add_char b c;
610 Buffer.contents b;
613 let textentry text key =
614 let c = Char.unsafe_chr key in
615 match c with
616 | _ when key >= 32 && key < 127 ->
617 let text = addchar text c in
618 TEcont text
620 | _ ->
621 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
622 TEcont text
625 let optentry text key =
626 let btos b = if b then "on" else "off" in
627 let c = Char.unsafe_chr key in
628 match c with
629 | 'r' ->
630 conf.rectsel <- not conf.rectsel;
631 TEdone ("rectsel " ^ (btos conf.rectsel))
633 | 'i' ->
634 conf.icase <- not conf.icase;
635 TEdone ("case insensitive search " ^ (btos conf.icase))
637 | 'p' ->
638 conf.preload <- not conf.preload;
639 TEdone ("preload " ^ (btos conf.preload))
641 | 'd' ->
642 conf.redispimm <- not conf.redispimm;
643 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
645 | 'v' ->
646 conf.verbose <- not conf.verbose;
647 TEdone ("verbose " ^ (btos conf.verbose))
649 | 'h' ->
650 conf.maxhfit <- not conf.maxhfit;
651 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
652 TEdone ("maxhfit " ^ (btos conf.maxhfit))
654 | 'c' ->
655 conf.crophack <- not conf.crophack;
656 TEdone ("crophack " ^ btos conf.crophack)
658 | _ ->
659 state.text <- Printf.sprintf "bad option %d `%c'" key c;
660 TEstop
663 let maxoutlinerows () = (state.h - 31) / 16;;
665 let enterselector allowdel outlines errmsg =
666 if Array.length outlines = 0
667 then (
668 showtext ' ' errmsg;
669 Glut.swapBuffers ()
671 else
672 let pageno =
673 match state.layout with
674 | [] -> -1
675 | {pageno=pageno} :: rest -> pageno
677 let active =
678 let rec loop n =
679 if n = Array.length outlines
680 then 0
681 else
682 let (_, _, outlinepageno, _) = outlines.(n) in
683 if outlinepageno >= pageno then n else loop (n+1)
685 loop 0
687 state.outline <-
688 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
689 Glut.postRedisplay ();
692 let enteroutlinemode () =
693 let outlines =
694 match state.outlines with
695 | Oarray a -> a
696 | Olist l ->
697 let a = Array.of_list (List.rev l) in
698 state.outlines <- Oarray a;
701 enterselector false outlines "Documents has no outline";
704 let enterbookmarkmode () =
705 let bookmarks = Array.of_list state.bookmarks in
706 enterselector true bookmarks "Documents has no bookmarks (yet)";
710 let quickbookmark ?title () =
711 match state.layout with
712 | [] -> ()
713 | l :: _ ->
714 let title =
715 match title with
716 | None ->
717 let sec = Unix.gettimeofday () in
718 let tm = Unix.localtime sec in
719 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
720 l.pageno
721 tm.Unix.tm_mday
722 tm.Unix.tm_mon
723 (tm.Unix.tm_year + 1900)
724 tm.Unix.tm_hour
725 tm.Unix.tm_min
726 | Some title -> title
728 state.bookmarks <-
729 (title, 0, l.pageno, l.pagey) :: state.bookmarks
732 let viewkeyboard ~key ~x ~y =
733 let enttext te =
734 state.textentry <- te;
735 state.text <- "";
736 enttext ();
737 Glut.swapBuffers ()
739 match state.textentry with
740 | None ->
741 let c = Char.chr key in
742 begin match c with
743 | '\027' | 'q' ->
744 exit 0
746 | '\008' ->
747 let y = getnav () in
748 gotoy y
750 | 'o' ->
751 enteroutlinemode ()
753 | 'u' ->
754 state.rects <- [];
755 state.text <- "";
756 Glut.postRedisplay ()
758 | '/' | '?' ->
759 let ondone isforw s =
760 state.searchpattern <- s;
761 search s isforw
763 enttext (Some (c, "", textentry, ondone (c ='/')))
765 | '+' ->
766 let ondone s =
767 let n =
768 try int_of_string s with exc ->
769 state.text <- Printf.sprintf "bad integer `%s': %s"
770 s (Printexc.to_string exc);
771 max_int
773 if n != max_int
774 then (
775 conf.pagebias <- n;
776 state.text <- "page bias is now " ^ string_of_int n;
779 enttext (Some ('+', "", intentry, ondone))
781 | '-' ->
782 let ondone msg =
783 state.text <- msg;
785 enttext (Some ('-', "", optentry, ondone))
787 | '0' .. '9' ->
788 let ondone s =
789 let n =
790 try int_of_string s with exc ->
791 state.text <- Printf.sprintf "bad integer `%s': %s"
792 s (Printexc.to_string exc);
795 if n >= 0
796 then (
797 addnav ();
798 state.y <- y;
799 gotoy (getpagey (n + conf.pagebias - 1))
802 let pageentry text key =
803 match Char.unsafe_chr key with
804 | 'g' -> TEdone text
805 | _ -> intentry text key
807 let text = "x" in text.[0] <- c;
808 enttext (Some (':', text, pageentry, ondone))
810 | 'b' ->
811 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
812 reshape state.w state.h;
814 | 'f' ->
815 begin match state.fullscreen with
816 | None ->
817 state.fullscreen <- Some (state.w, state.h);
818 Glut.fullScreen ()
819 | Some (w, h) ->
820 state.fullscreen <- None;
821 Glut.reshapeWindow ~w ~h
824 | 'n' ->
825 search state.searchpattern true
827 | 'p' ->
828 search state.searchpattern false
830 | 't' ->
831 begin match state.layout with
832 | [] -> ()
833 | l :: _ ->
834 gotoy (state.y - l.pagey);
837 | ' ' | 'N' ->
838 begin match List.rev state.layout with
839 | [] -> ()
840 | l :: _ ->
841 gotoy (clamp (l.pageh - l.pagey))
844 | '\127' | 'P' ->
845 begin match state.layout with
846 | [] -> ()
847 | l :: _ ->
848 gotoy (clamp (-l.pageh));
851 | '=' ->
852 let f (fn, ln) l =
853 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
855 let fn, ln = List.fold_left f (-1, -1) state.layout in
856 let s =
857 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
858 let percent = (100. *. (float state.y /. float maxy)) in
859 if fn = ln
860 then
861 Printf.sprintf "Page %d of %d %.2f%%"
862 (fn+1) state.pagecount percent
863 else
864 Printf.sprintf
865 "Pages %d-%d of %d %.2f%%"
866 (fn+1) (ln+1) state.pagecount percent
868 showtext ' ' s;
869 Glut.swapBuffers ()
871 | 'w' ->
872 begin match state.layout with
873 | [] -> ()
874 | l :: _ ->
875 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
876 Glut.postRedisplay ();
879 | '\'' ->
880 enterbookmarkmode ()
882 | 'm' ->
883 let ondone s =
884 match state.layout with
885 | l :: _ ->
886 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
887 | _ -> ()
889 enttext (Some ('~', "", textentry, ondone))
891 | '~' ->
892 quickbookmark ();
893 showtext ' ' "Quick bookmark added";
894 Glut.swapBuffers ()
896 | 'z' ->
897 begin match state.layout with
898 | l :: _ ->
899 let a = getpagewh l.pagedimno in
900 let w, h =
901 if conf.crophack
902 then
903 (truncate (1.8 *. (a.(1) -. a.(0))),
904 truncate (1.4 *. (a.(3) -. a.(0))))
905 else
906 (truncate (a.(1) -. a.(0)),
907 truncate (a.(3) -. a.(0)))
909 Glut.reshapeWindow (w + conf.scrollw) h;
910 Glut.postRedisplay ();
912 | [] -> ()
915 | _ ->
916 vlog "huh? %d %c" key (Char.chr key);
919 | Some (c, text, onkey, ondone) when key = 8 ->
920 let len = String.length text in
921 if len = 0 || len = 1
922 then (
923 state.textentry <- None;
924 Glut.postRedisplay ();
926 else (
927 let s = String.sub text 0 (len - 1) in
928 enttext (Some (c, s, onkey, ondone))
931 | Some (c, text, onkey, ondone) ->
932 begin match Char.unsafe_chr key with
933 | '\r' | '\n' ->
934 ondone text;
935 state.textentry <- None;
936 Glut.postRedisplay ()
938 | '\027' ->
939 state.textentry <- None;
940 Glut.postRedisplay ()
942 | _ ->
943 begin match onkey text key with
944 | TEdone text ->
945 state.textentry <- None;
946 ondone text;
947 Glut.postRedisplay ()
949 | TEcont text ->
950 enttext (Some (c, text, onkey, ondone));
952 | TEstop ->
953 state.textentry <- None;
954 Glut.postRedisplay ()
955 end;
956 end;
959 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
960 let search active pattern incr =
961 let dosearch re =
962 let rec loop n =
963 if n = Array.length outlines || n = -1 then None else
964 let (s, _, _, _) = outlines.(n) in
966 (try ignore (Str.search_forward re s 0); true
967 with Not_found -> false)
968 then (
969 let maxrows = (min (Array.length outlines) (maxoutlinerows ())) / 2 in
970 if first > n
971 then Some (n, max 0 (n - maxrows))
972 else Some (n, max first (n - maxrows))
974 else loop (n + incr)
976 loop active
979 let re = Str.regexp_case_fold pattern in
980 dosearch re
981 with Failure s ->
982 state.text <- s;
983 None
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 ->
1004 state.text <- qsearch ^ " [not found]";
1005 active, first
1006 | Some af ->
1007 state.text <- qsearch;
1010 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1011 Glut.postRedisplay ();
1013 | 8 ->
1014 let len = String.length qsearch in
1015 if len = 0
1016 then ()
1017 else (
1018 if len = 1
1019 then (
1020 state.text <- "";
1021 state.outline <- Some (allowdel, active, first, outlines, "");
1023 else
1024 let qsearch = String.sub qsearch 0 (len - 1) in
1025 state.text <- qsearch;
1026 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1028 Glut.postRedisplay ()
1030 | 13 ->
1031 if active < Array.length outlines
1032 then (
1033 let (_, _, n, t) = outlines.(active) in
1034 gotopage n t;
1036 state.text <- "";
1037 if allowdel then state.bookmarks <- Array.to_list outlines;
1038 state.outline <- None;
1039 Glut.postRedisplay ();
1041 | _ when key >= 32 && key < 127 ->
1042 let pattern = addchar qsearch (Char.chr key) in
1043 let active, first =
1044 match search active pattern 1 with
1045 | None ->
1046 state.text <- pattern ^ " [not found]";
1047 active, first
1048 | Some (active, first) ->
1049 state.text <- pattern;
1050 active, first
1052 state.outline <- Some (allowdel, active, first, outlines, pattern);
1053 Glut.postRedisplay ()
1055 | 127 when allowdel ->
1056 let len = Array.length outlines - 1 in
1057 if len = 0
1058 then (
1059 state.outline <- None;
1060 state.bookmarks <- [];
1062 else (
1063 let bookmarks = Array.init len
1064 (fun i ->
1065 let i = if i >= active then i + 1 else i in
1066 outlines.(i)
1069 state.outline <-
1070 Some (allowdel,
1071 min active (len-1),
1072 min first (len-1),
1073 bookmarks, qsearch)
1076 Glut.postRedisplay ()
1078 | _ -> log "unknown key %d" key
1081 let keyboard ~key ~x ~y =
1082 match state.outline with
1083 | None -> viewkeyboard ~key ~x ~y
1084 | Some outline -> outlinekeyboard ~key ~x ~y outline
1087 let special ~key ~x ~y =
1088 match state.outline with
1089 | None ->
1090 let y =
1091 match key with
1092 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1093 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1094 | Glut.KEY_DOWN -> clamp conf.scrollincr
1095 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1096 | Glut.KEY_PAGE_DOWN -> clamp state.h
1097 | Glut.KEY_HOME -> addnav (); 0
1098 | Glut.KEY_END ->
1099 addnav ();
1100 state.maxy - (if conf.maxhfit then state.h else 0)
1101 | _ -> state.y
1103 state.text <- "";
1104 gotoy y
1106 | Some (allowdel, active, first, outlines, qsearch) ->
1107 let maxrows = maxoutlinerows () in
1108 let navigate incr =
1109 let active = active + incr in
1110 let active = max 0 (min active (Array.length outlines - 1)) in
1111 let first =
1112 if active > first
1113 then
1114 let rows = active - first in
1115 if rows > maxrows then first + incr else first
1116 else active
1118 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1119 Glut.postRedisplay ()
1121 match key with
1122 | Glut.KEY_UP -> navigate ~-1
1123 | Glut.KEY_DOWN -> navigate 1
1124 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1125 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1127 | Glut.KEY_HOME ->
1128 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1129 Glut.postRedisplay ()
1131 | Glut.KEY_END ->
1132 let active = Array.length outlines - 1 in
1133 let first = max 0 (active - maxrows) in
1134 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1135 Glut.postRedisplay ()
1137 | _ -> ()
1140 let drawplaceholder l =
1141 if true
1142 then (
1143 GlDraw.color (0.2, 0.2, 0.2);
1144 GlDraw.color (1.0, 1.0, 1.0);
1145 GlDraw.rect
1146 (0.0, float l.pagedispy)
1147 (float l.pagew, float (l.pagedispy + l.pagevh))
1149 let x = 0.0
1150 and y = float (l.pagedispy + 13) in
1151 let font = Glut.BITMAP_8_BY_13 in
1152 GlDraw.color (0.0, 0.0, 0.0);
1153 GlPix.raster_pos ~x ~y ();
1154 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1155 ("Loading " ^ string_of_int l.pageno);
1157 else (
1158 GlDraw.begins `quads;
1159 GlDraw.vertex2 (0.0, float l.pagedispy);
1160 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1161 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1162 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1163 GlDraw.ends ();
1167 let now () = Unix.gettimeofday ();;
1169 let drawpage i l =
1170 begin match getopaque l.pageno with
1171 | Some opaque when validopaque opaque ->
1172 GlDraw.color (1.0, 1.0, 1.0);
1173 let a = now () in
1174 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1175 let b = now () in
1176 let d = b-.a in
1177 vlog "draw %f sec" d;
1179 | Some _ ->
1180 drawplaceholder l
1182 | None ->
1183 drawplaceholder l;
1184 if state.inflight < cblen state.pagecache
1185 then (
1186 List.iter preload state.layout;
1188 else (
1189 vlog "inflight %d" state.inflight;
1191 end;
1192 GlDraw.color (0.5, 0.5, 0.5);
1193 GlDraw.rect
1194 (0., float i)
1195 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1197 l.pagedispy + l.pagevh;
1200 let scrollindicator () =
1201 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1202 GlDraw.color (0.64 , 0.64, 0.64);
1203 GlDraw.rect
1204 (float (state.w - conf.scrollw), 0.)
1205 (float state.w, float state.h)
1207 GlDraw.color (0.0, 0.0, 0.0);
1208 let sh = (float (maxy + state.h) /. float state.h) in
1209 let sh = float state.h /. sh in
1210 let sh = max sh (float conf.scrollh) in
1212 let percent =
1213 if state.y = state.maxy
1214 then 1.0
1215 else float state.y /. float maxy
1217 let position = (float state.h -. sh) *. percent in
1219 let position =
1220 if position +. sh > float state.h
1221 then
1222 float state.h -. sh
1223 else
1224 position
1226 GlDraw.rect
1227 (float (state.w - conf.scrollw), position)
1228 (float state.w, position +. sh)
1232 let showsel () =
1233 match state.mstate with
1234 | Mnone ->
1237 | Msel ((x0, y0), (x1, y1)) ->
1238 let y0' = min y0 y1
1239 and y1 = max y0 y1 in
1240 let y0 = y0' in
1241 let f l =
1242 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1243 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1244 then
1245 match getopaque l.pageno with
1246 | Some opaque when validopaque opaque ->
1247 let oy = -l.pagey + l.pagedispy in
1248 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1249 | _ -> ()
1251 List.iter f state.layout
1254 let showrects () =
1255 Gl.enable `blend;
1256 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1257 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1258 List.iter
1259 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1260 List.iter (fun l ->
1261 if l.pageno = pageno
1262 then (
1263 let d = float (l.pagedispy - l.pagey) in
1264 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1265 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1267 ) state.layout
1268 ) state.rects
1270 Gl.disable `blend;
1273 let showoutline = function
1274 | None -> ()
1275 | Some (allowdel, active, first, outlines, qsearch) ->
1276 Gl.enable `blend;
1277 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1278 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1279 GlDraw.rect (0., 0.) (float state.w, float state.h);
1280 Gl.disable `blend;
1282 GlDraw.color (1., 1., 1.);
1283 let font = Glut.BITMAP_9_BY_15 in
1284 let draw_string x y s =
1285 GlPix.raster_pos ~x ~y ();
1286 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1288 let rec loop row =
1289 if row = Array.length outlines || (row - first) * 16 > state.h
1290 then ()
1291 else (
1292 let (s, l, _, _) = outlines.(row) in
1293 let y = (row - first) * 16 in
1294 let x = 5 + 5*l in
1295 if row = active
1296 then (
1297 Gl.enable `blend;
1298 GlDraw.polygon_mode `both `line;
1299 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1300 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1301 GlDraw.rect (0., float (y + 1))
1302 (float (state.w - conf.scrollw - 1), float (y + 18));
1303 GlDraw.polygon_mode `both `fill;
1304 Gl.disable `blend;
1305 GlDraw.color (1., 1., 1.);
1307 draw_string (float x) (float (y + 16)) s;
1308 loop (row+1)
1311 loop first
1314 let display () =
1315 let lasty = List.fold_left drawpage 0 (state.layout) in
1316 GlDraw.color (0.5, 0.5, 0.5);
1317 GlDraw.rect
1318 (0., float lasty)
1319 (float (state.w - conf.scrollw), float state.h)
1321 showrects ();
1322 scrollindicator ();
1323 showsel ();
1324 showoutline state.outline;
1325 enttext ();
1326 Glut.swapBuffers ();
1329 let getlink x y =
1330 let rec f = function
1331 | l :: rest ->
1332 begin match getopaque l.pageno with
1333 | Some opaque when validopaque opaque ->
1334 let y = y - l.pagedispy in
1335 if y > 0
1336 then
1337 let y = l.pagey + y in
1338 match getlink opaque x y with
1339 | None -> f rest
1340 | some -> some
1341 else
1342 f rest
1343 | _ ->
1344 f rest
1346 | [] -> None
1348 f state.layout
1351 let checklink x y =
1352 let rec f = function
1353 | l :: rest ->
1354 begin match getopaque l.pageno with
1355 | Some opaque when validopaque opaque ->
1356 let y = y - l.pagedispy in
1357 if y > 0
1358 then
1359 let y = l.pagey + y in
1360 if checklink opaque x y then true else f rest
1361 else
1362 f rest
1363 | _ ->
1364 f rest
1366 | [] -> false
1368 f state.layout
1371 let mouse ~button ~bstate ~x ~y =
1372 match button with
1373 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1374 let incr =
1375 if n = 3
1376 then
1377 -conf.scrollincr
1378 else
1379 conf.scrollincr
1381 let incr = incr * 2 in
1382 let y = clamp incr in
1383 gotoy y
1385 | Glut.LEFT_BUTTON when state.outline = None ->
1386 let dest = if bstate = Glut.DOWN then getlink x y else None in
1387 begin match dest with
1388 | Some (pageno, top) ->
1389 gotopage pageno top
1391 | None ->
1392 if bstate = Glut.DOWN
1393 then (
1394 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1395 state.mstate <- Msel ((x, y), (x, y));
1396 Glut.postRedisplay ()
1398 else (
1399 Glut.setCursor Glut.CURSOR_INHERIT;
1400 state.mstate <- Mnone;
1404 | _ ->
1407 let mouse ~button ~state ~x ~y = mouse button state x y;;
1409 let motion ~x ~y =
1410 if state.outline = None
1411 then
1412 match state.mstate with
1413 | Mnone -> ()
1414 | Msel (a, _) ->
1415 state.mstate <- Msel (a, (x, y));
1416 Glut.postRedisplay ()
1419 let pmotion ~x ~y =
1420 if state.outline = None
1421 then
1422 match state.mstate with
1423 | Mnone when (checklink x y) ->
1424 Glut.setCursor Glut.CURSOR_INFO
1426 | Mnone ->
1427 Glut.setCursor Glut.CURSOR_INHERIT
1429 | Msel (a, _) ->
1433 let () =
1434 let statepath =
1435 let home =
1436 if Sys.os_type = "Win32"
1437 then
1438 try Sys.getenv "HOMEPATH" with Not_found -> ""
1439 else
1440 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1442 Filename.concat home "llpp"
1444 let pstate =
1446 let ic = open_in_bin statepath in
1447 let hash = input_value ic in
1448 close_in ic;
1449 hash
1450 with exn ->
1451 if false
1452 then
1453 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1455 Hashtbl.create 1
1457 let savestate () =
1459 let w, h =
1460 match state.fullscreen with
1461 | None -> state.w, state.h
1462 | Some wh -> wh
1464 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1465 let oc = open_out_bin statepath in
1466 output_value oc pstate
1467 with exn ->
1468 if false
1469 then
1470 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1473 let setstate () =
1475 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1476 state.w <- statew;
1477 state.h <- stateh;
1478 state.bookmarks <- statebookmarks;
1479 with Not_found -> ()
1480 | exn ->
1481 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1484 Arg.parse [] (fun s -> state.path <- s) "options:";
1485 let name =
1486 if String.length state.path = 0
1487 then (prerr_endline "filename missing"; exit 1)
1488 else state.path
1491 setstate ();
1492 let _ = Glut.init Sys.argv in
1493 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1494 let () = Glut.initWindowSize state.w state.h in
1495 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1497 let csock, ssock =
1498 if Sys.os_type = "Unix"
1499 then
1500 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1501 else
1502 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1503 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1504 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1505 Unix.bind sock addr;
1506 Unix.listen sock 1;
1507 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1508 Unix.connect csock addr;
1509 let ssock, _ = Unix.accept sock in
1510 Unix.close sock;
1511 let opts sock =
1512 Unix.setsockopt sock Unix.TCP_NODELAY true;
1513 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1515 opts ssock;
1516 opts csock;
1517 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1518 ssock, csock
1521 let () = Glut.displayFunc display in
1522 let () = Glut.reshapeFunc reshape in
1523 let () = Glut.keyboardFunc keyboard in
1524 let () = Glut.specialFunc special in
1525 let () = Glut.idleFunc (Some idle) in
1526 let () = Glut.mouseFunc mouse in
1527 let () = Glut.motionFunc motion in
1528 let () = Glut.passiveMotionFunc pmotion in
1530 init ssock;
1531 state.csock <- csock;
1532 state.ssock <- ssock;
1533 writecmd csock ("open " ^ name ^ "\000");
1535 at_exit savestate;
1537 let rec handlelablglutbug () =
1539 Glut.mainLoop ();
1540 with Glut.BadEnum "key in special_of_int" ->
1541 showtext '!' " LablGlut bug: special key not recognized";
1542 Glut.swapBuffers ();
1543 handlelablglutbug ()
1545 handlelablglutbug ();