Change default cursor to inherited one
[llpp.git] / main.ml
bloba7f98eeb8eedd39b6e0f0cc4431298ef62ad1835
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 Glut.postRedisplay ();
459 | 'T' ->
460 let s = Scanf.sscanf cmd "T %n"
461 (fun n -> String.sub cmd n (String.length cmd - n))
463 state.text <- s;
464 showtext ' ' s;
465 Glut.swapBuffers ();
466 (* Glut.postRedisplay () *)
468 | 'F' ->
469 let pageno, c, x0, y0, x1, y1 =
470 Scanf.sscanf cmd "F %d %d %f %f %f %f"
471 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
473 let y = (getpagey pageno) + truncate y0 in
474 addnav ();
475 gotoy y;
476 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
478 | 'R' ->
479 let pageno, c, x0, y0, x1, y1 =
480 Scanf.sscanf cmd "R %d %d %f %f %f %f"
481 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
483 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
485 | 'r' ->
486 let n, w, h, p =
487 Scanf.sscanf cmd "r %d %d %d %s"
488 (fun n w h p -> (n, w, h, p))
490 Hashtbl.replace state.pagemap (n, w) p;
491 let evicted = cbpeekw state.pagecache in
492 if String.length evicted > 0
493 then begin
494 wcmd "free" [`s evicted];
495 let l = Hashtbl.fold (fun k p a ->
496 if evicted = p then k :: a else a) state.pagemap []
498 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
499 end;
500 cbput state.pagecache p;
501 state.inflight <- pred state.inflight;
502 Glut.postRedisplay ()
504 | 'l' ->
505 let (n, w, h) as pagelayout =
506 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
508 state.pages <- pagelayout :: state.pages
510 | 'o' ->
511 let (l, n, t, pos) =
512 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
514 let s = String.sub cmd pos (String.length cmd - pos) in
515 let outline = (s, l, n, t) in
516 let outlines =
517 match state.outlines with
518 | Olist outlines -> Olist (outline :: outlines)
519 | Oarray _ -> Olist [outline]
521 state.outlines <- outlines
523 | _ ->
524 log "unknown cmd `%S'" cmd
527 let getopaque pageno =
528 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw))
529 with Not_found -> None
532 let cache pageno opaque =
533 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw) opaque
536 let validopaque opaque = String.length opaque > 0;;
538 let preload l =
539 match getopaque l.pageno with
540 | None when state.inflight < 2+0*(cblen state.pagecache) ->
541 state.inflight <- succ state.inflight;
542 cache l.pageno "";
543 wcmd "render" [`i (l.pageno + 1)
544 ;`i l.pagedimno
545 ;`i l.pagew
546 ;`i l.pageh];
548 | _ -> ()
551 let idle () =
552 if not conf.redispimm && state.y != state.prevy
553 then (
554 state.prevy <- state.y;
555 Glut.postRedisplay ();
557 else
558 let r, _, _ = Unix.select [state.csock] [] [] 0.02 in
560 begin match r with
561 | [] ->
562 if conf.preload then begin
563 let h = state.h in
564 let y = if state.y < state.h then 0 else state.y - state.h in
565 let pages = layout y (h*3) in
566 List.iter preload pages;
567 end;
569 | _ ->
570 let cmd = readcmd state.csock in
571 act cmd;
572 end;
575 let search pattern forward =
576 if String.length pattern > 0
577 then
578 let pn, py =
579 match state.layout with
580 | [] -> 0, 0
581 | l :: _ ->
582 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
584 let cmd =
585 let b = makecmd "search"
586 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
588 Buffer.add_char b ',';
589 Buffer.add_string b pattern;
590 Buffer.add_char b '\000';
591 Buffer.contents b;
593 writecmd state.csock cmd;
596 let intentry text key =
597 let c = Char.unsafe_chr key in
598 match c with
599 | '0' .. '9' ->
600 let s = "x" in s.[0] <- c;
601 let text = text ^ s in
602 TEcont text
604 | _ ->
605 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
606 TEcont text
609 let addchar s c =
610 let b = Buffer.create (String.length s + 1) in
611 Buffer.add_string b s;
612 Buffer.add_char b c;
613 Buffer.contents b;
616 let textentry text key =
617 let c = Char.unsafe_chr key in
618 match c with
619 | _ when key >= 32 && key < 127 ->
620 let text = addchar text c in
621 TEcont text
623 | _ ->
624 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
625 TEcont text
628 let optentry text key =
629 let btos b = if b then "on" else "off" in
630 let c = Char.unsafe_chr key in
631 match c with
632 | 'r' ->
633 conf.rectsel <- not conf.rectsel;
634 TEdone ("rectsel " ^ (btos conf.rectsel))
636 | 'i' ->
637 conf.icase <- not conf.icase;
638 TEdone ("case insensitive search " ^ (btos conf.icase))
640 | 'p' ->
641 conf.preload <- not conf.preload;
642 TEdone ("preload " ^ (btos conf.preload))
644 | 't' ->
645 conf.titletext <- not conf.titletext;
646 TEdone ("titletext " ^ (btos conf.titletext))
648 | 'd' ->
649 conf.redispimm <- not conf.redispimm;
650 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
652 | 'v' ->
653 conf.verbose <- not conf.verbose;
654 TEdone ("verbose " ^ (btos conf.verbose))
656 | 'h' ->
657 conf.maxhfit <- not conf.maxhfit;
658 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
659 TEdone ("maxhfit " ^ (btos conf.maxhfit))
661 | 'q' ->
662 conf.markonquit <- not conf.markonquit;
663 TEdone ("bookmark on quit " ^ btos conf.markonquit)
665 | _ ->
666 state.text <- Printf.sprintf "bad option %d `%c'" key c;
667 TEstop
670 let maxoutlinerows () = (state.h - 31) / 16;;
672 let enterselector allowdel outlines errmsg =
673 if Array.length outlines = 0
674 then (
675 showtext ' ' errmsg;
676 Glut.swapBuffers ()
678 else
679 let pageno =
680 match state.layout with
681 | [] -> -1
682 | {pageno=pageno} :: rest -> pageno
684 let active =
685 let rec loop n =
686 if n = Array.length outlines
687 then 0
688 else
689 let (_, _, outlinepageno, _) = outlines.(n) in
690 if outlinepageno >= pageno then n else loop (n+1)
692 loop 0
694 state.outline <-
695 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
696 Glut.postRedisplay ();
699 let enteroutlinemode () =
700 let outlines =
701 match state.outlines with
702 | Oarray a -> a
703 | Olist l ->
704 let a = Array.of_list (List.rev l) in
705 state.outlines <- Oarray a;
708 enterselector false outlines "Documents has no outline";
711 let enterbookmarkmode () =
712 let bookmarks = Array.of_list state.bookmarks in
713 enterselector true bookmarks "Documents has no bookmarks (yet)";
717 let quickbookmark ?title () =
718 match state.layout with
719 | [] -> ()
720 | l :: _ ->
721 let title =
722 match title with
723 | None ->
724 let sec = Unix.gettimeofday () in
725 let tm = Unix.localtime sec in
726 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
727 l.pageno
728 tm.Unix.tm_mday
729 tm.Unix.tm_mon
730 (tm.Unix.tm_year + 1900)
731 tm.Unix.tm_hour
732 tm.Unix.tm_min
733 | Some title -> title
735 state.bookmarks <-
736 (title, 0, l.pageno, l.pagey) :: state.bookmarks
739 let viewkeyboard ~key ~x ~y =
740 let enttext te =
741 state.textentry <- te;
742 state.text <- "";
743 enttext ();
744 Glut.swapBuffers ()
746 match state.textentry with
747 | None ->
748 let c = Char.chr key in
749 begin match c with
750 | '\027' | 'q' ->
751 exit 0
753 | '\008' ->
754 let y = getnav () in
755 gotoy y
757 | 'o' ->
758 enteroutlinemode ()
760 | 'u' ->
761 state.rects <- [];
762 state.text <- "";
763 Glut.postRedisplay ()
765 | '/' | '?' ->
766 let ondone isforw s =
767 state.searchpattern <- s;
768 search s isforw
770 enttext (Some (c, "", textentry, ondone (c ='/')))
772 | '+' ->
773 let ondone s =
774 let n =
775 try int_of_string s with exc ->
776 state.text <- Printf.sprintf "bad integer `%s': %s"
777 s (Printexc.to_string exc);
778 max_int
780 if n != max_int
781 then (
782 conf.pagebias <- n;
783 state.text <- "page bias is now " ^ string_of_int n;
786 enttext (Some ('+', "", intentry, ondone))
788 | '-' ->
789 let ondone msg =
790 state.text <- msg;
792 enttext (Some ('-', "", optentry, ondone))
794 | '0' .. '9' ->
795 let ondone s =
796 let n =
797 try int_of_string s with exc ->
798 state.text <- Printf.sprintf "bad integer `%s': %s"
799 s (Printexc.to_string exc);
802 if n >= 0
803 then (
804 addnav ();
805 state.y <- y;
806 gotoy (getpagey (n + conf.pagebias - 1))
809 let pageentry text key =
810 match Char.unsafe_chr key with
811 | 'g' -> TEdone text
812 | _ -> intentry text key
814 let text = "x" in text.[0] <- c;
815 enttext (Some (':', text, pageentry, ondone))
817 | 'b' ->
818 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
819 reshape state.w state.h;
821 | 'f' ->
822 begin match state.fullscreen with
823 | None ->
824 state.fullscreen <- Some (state.w, state.h);
825 Glut.fullScreen ()
826 | Some (w, h) ->
827 state.fullscreen <- None;
828 Glut.reshapeWindow ~w ~h
831 | 'n' ->
832 search state.searchpattern true
834 | 'p' ->
835 search state.searchpattern false
837 | 't' ->
838 begin match state.layout with
839 | [] -> ()
840 | l :: _ ->
841 gotoy (state.y - l.pagey);
844 | ' ' | 'N' ->
845 begin match List.rev state.layout with
846 | [] -> ()
847 | l :: _ ->
848 gotoy (clamp (l.pageh - l.pagey))
851 | '\127' | 'P' ->
852 begin match state.layout with
853 | [] -> ()
854 | l :: _ ->
855 gotoy (clamp (-l.pageh));
858 | '=' ->
859 let f (fn, ln) l =
860 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
862 let fn, ln = List.fold_left f (-1, -1) state.layout in
863 let s =
864 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
865 let percent = (100. *. (float state.y /. float maxy)) in
866 if fn = ln
867 then
868 Printf.sprintf "Page %d of %d %.2f%%"
869 (fn+1) state.pagecount percent
870 else
871 Printf.sprintf
872 "Pages %d-%d of %d %.2f%%"
873 (fn+1) (ln+1) state.pagecount percent
875 showtext ' ' s;
876 Glut.swapBuffers ()
878 | 'w' ->
879 begin match state.layout with
880 | [] -> ()
881 | l :: _ ->
882 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
883 Glut.postRedisplay ();
886 | '\'' ->
887 enterbookmarkmode ()
889 | 'm' ->
890 let ondone s =
891 match state.layout with
892 | l :: _ ->
893 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
894 | _ -> ()
896 enttext (Some ('~', "", textentry, ondone))
898 | '~' ->
899 quickbookmark ();
900 showtext ' ' "Quick bookmark added";
901 Glut.swapBuffers ()
903 | 'z' ->
904 begin match state.layout with
905 | l :: _ ->
906 let a = getpagewh l.pagedimno in
907 let w = truncate (a.(1) -. a.(0))
908 and h = truncate (a.(3) -. a.(0)) in
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 re = Str.regexp_case_fold pattern in
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 = maxoutlinerows () 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
978 match key with
979 | 27 ->
980 if String.length qsearch = 0
981 then (
982 state.text <- "";
983 state.outline <- None;
984 Glut.postRedisplay ();
986 else (
987 state.text <- "";
988 state.outline <- Some (allowdel, active, first, outlines, "");
989 Glut.postRedisplay ();
992 | 18 | 19 ->
993 let incr = if key = 18 then -1 else 1 in
994 let active, first =
995 match search (active + incr) qsearch incr with
996 | None -> active, first
997 | Some af -> af
999 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1000 Glut.postRedisplay ();
1002 | 8 ->
1003 let len = String.length qsearch in
1004 if len = 0
1005 then ()
1006 else (
1007 if len = 1
1008 then (
1009 state.text <- "";
1010 state.outline <- Some (allowdel, active, first, outlines, "");
1012 else
1013 let qsearch = String.sub qsearch 0 (len - 1) in
1014 state.text <- qsearch;
1015 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1017 Glut.postRedisplay ()
1019 | 13 ->
1020 if active < Array.length outlines
1021 then (
1022 let (_, _, n, t) = outlines.(active) in
1023 gotopage n t;
1025 state.text <- "";
1026 if allowdel then state.bookmarks <- Array.to_list outlines;
1027 state.outline <- None;
1028 Glut.postRedisplay ();
1030 | _ when key >= 32 && key < 127 ->
1031 let pattern = addchar qsearch (Char.chr key) in
1032 let pattern, active, first =
1033 match search active pattern 1 with
1034 | None -> qsearch, active, first
1035 | Some (active, first) -> (pattern, active, first)
1037 state.text <- pattern;
1038 state.outline <- Some (allowdel, active, first, outlines, pattern);
1039 Glut.postRedisplay ()
1041 | 127 when allowdel ->
1042 let len = Array.length outlines - 1 in
1043 if len = 0
1044 then (
1045 state.outline <- None;
1046 state.bookmarks <- [];
1048 else (
1049 let bookmarks = Array.init len
1050 (fun i ->
1051 let i = if i >= active then i + 1 else i in
1052 outlines.(i)
1055 state.outline <-
1056 Some (allowdel,
1057 min active (len-1),
1058 min first (len-1),
1059 bookmarks, qsearch)
1062 Glut.postRedisplay ()
1064 | _ -> log "unknown key %d" key
1067 let keyboard ~key ~x ~y =
1068 match state.outline with
1069 | None -> viewkeyboard ~key ~x ~y
1070 | Some outline -> outlinekeyboard ~key ~x ~y outline
1073 let special ~key ~x ~y =
1074 match state.outline with
1075 | None ->
1076 let y =
1077 match key with
1078 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1079 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1080 | Glut.KEY_DOWN -> clamp conf.scrollincr
1081 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1082 | Glut.KEY_PAGE_DOWN -> clamp state.h
1083 | Glut.KEY_HOME -> addnav (); 0
1084 | Glut.KEY_END ->
1085 addnav ();
1086 state.maxy - (if conf.maxhfit then state.h else 0)
1087 | _ -> state.y
1089 state.text <- "";
1090 gotoy y
1092 | Some (allowdel, active, first, outlines, qsearch) ->
1093 let maxrows = maxoutlinerows () in
1094 let navigate incr =
1095 let active = active + incr in
1096 let active = max 0 (min active (Array.length outlines - 1)) in
1097 let first =
1098 if active > first
1099 then
1100 let rows = active - first in
1101 if rows > maxrows then first + incr else first
1102 else active
1104 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1105 Glut.postRedisplay ()
1107 match key with
1108 | Glut.KEY_UP -> navigate ~-1
1109 | Glut.KEY_DOWN -> navigate 1
1110 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1111 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1113 | Glut.KEY_HOME ->
1114 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1115 Glut.postRedisplay ()
1117 | Glut.KEY_END ->
1118 let active = Array.length outlines - 1 in
1119 let first = max 0 (active - maxrows) in
1120 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1121 Glut.postRedisplay ()
1123 | _ -> ()
1126 let drawplaceholder l =
1127 if true
1128 then (
1129 GlDraw.color (0.2, 0.2, 0.2);
1130 GlDraw.color (1.0, 1.0, 1.0);
1131 GlDraw.rect
1132 (0.0, float l.pagedispy)
1133 (float l.pagew, float (l.pagedispy + l.pagevh))
1135 let x = 0.0
1136 and y = float (l.pagedispy + 13) in
1137 let font = Glut.BITMAP_8_BY_13 in
1138 GlDraw.color (0.0, 0.0, 0.0);
1139 GlPix.raster_pos ~x ~y ();
1140 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1141 ("Loading " ^ string_of_int l.pageno);
1143 else (
1144 GlDraw.begins `quads;
1145 GlDraw.vertex2 (0.0, float l.pagedispy);
1146 GlDraw.vertex2 (float l.pagew, float l.pagedispy);
1147 GlDraw.vertex2 (float l.pagew, float (l.pagedispy + l.pagevh));
1148 GlDraw.vertex2 (0.0, float (l.pagedispy + l.pagevh));
1149 GlDraw.ends ();
1153 let now () = Unix.gettimeofday ();;
1155 let drawpage i l =
1156 begin match getopaque l.pageno with
1157 | Some opaque when validopaque opaque ->
1158 GlDraw.color (1.0, 1.0, 1.0);
1159 let a = now () in
1160 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1161 let b = now () in
1162 let d = b-.a in
1163 vlog "draw %f sec" d;
1165 | Some _ ->
1166 drawplaceholder l
1168 | None ->
1169 drawplaceholder l;
1170 if state.inflight < cblen state.pagecache
1171 then (
1172 List.iter preload state.layout;
1174 else (
1175 vlog "inflight %d" state.inflight;
1177 end;
1178 GlDraw.color (0.5, 0.5, 0.5);
1179 GlDraw.rect
1180 (0., float i)
1181 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1183 l.pagedispy + l.pagevh;
1186 let scrollindicator () =
1187 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1188 GlDraw.color (0.64 , 0.64, 0.64);
1189 GlDraw.rect
1190 (float (state.w - conf.scrollw), 0.)
1191 (float state.w, float state.h)
1193 GlDraw.color (0.0, 0.0, 0.0);
1194 let sh = (float (maxy + state.h) /. float state.h) in
1195 let sh = float state.h /. sh in
1196 let sh = max sh (float conf.scrollh) in
1198 let percent =
1199 if state.y = state.maxy
1200 then 1.0
1201 else float state.y /. float maxy
1203 let position = (float state.h -. sh) *. percent in
1205 let position =
1206 if position +. sh > float state.h
1207 then
1208 float state.h -. sh
1209 else
1210 position
1212 GlDraw.rect
1213 (float (state.w - conf.scrollw), position)
1214 (float state.w, position +. sh)
1218 let showsel () =
1219 match state.mstate with
1220 | Mnone ->
1223 | Msel ((x0, y0), (x1, y1)) ->
1224 let y0' = min y0 y1
1225 and y1 = max y0 y1 in
1226 let y0 = y0' in
1227 let f l =
1228 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1229 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1230 then
1231 match getopaque l.pageno with
1232 | Some opaque when validopaque opaque ->
1233 let oy = -l.pagey + l.pagedispy in
1234 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1235 | _ -> ()
1237 List.iter f state.layout
1240 let showrects () =
1241 Gl.enable `blend;
1242 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1243 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1244 List.iter
1245 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1246 List.iter (fun l ->
1247 if l.pageno = pageno
1248 then (
1249 let d = float (l.pagedispy - l.pagey) in
1250 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1251 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1253 ) state.layout
1254 ) state.rects
1256 Gl.disable `blend;
1259 let showoutline = function
1260 | None -> ()
1261 | Some (allowdel, active, first, outlines, qsearch) ->
1262 Gl.enable `blend;
1263 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1264 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1265 GlDraw.rect (0., 0.) (float state.w, float state.h);
1266 Gl.disable `blend;
1268 GlDraw.color (1., 1., 1.);
1269 let font = Glut.BITMAP_9_BY_15 in
1270 let draw_string x y s =
1271 GlPix.raster_pos ~x ~y ();
1272 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1274 let rec loop row =
1275 if row = Array.length outlines || (row - first) * 16 > state.h
1276 then ()
1277 else (
1278 let (s, l, _, _) = outlines.(row) in
1279 let y = (row - first) * 16 in
1280 let x = 5 + 5*l in
1281 if row = active
1282 then (
1283 Gl.enable `blend;
1284 GlDraw.polygon_mode `both `line;
1285 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1286 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1287 GlDraw.rect (0., float (y + 1))
1288 (float (state.w - conf.scrollw - 1), float (y + 18));
1289 GlDraw.polygon_mode `both `fill;
1290 Gl.disable `blend;
1291 GlDraw.color (1., 1., 1.);
1293 draw_string (float x) (float (y + 16)) s;
1294 loop (row+1)
1297 loop first
1300 let display () =
1301 let lasty = List.fold_left drawpage 0 (state.layout) in
1302 GlDraw.color (0.5, 0.5, 0.5);
1303 GlDraw.rect
1304 (0., float lasty)
1305 (float (state.w - conf.scrollw), float state.h)
1307 showrects ();
1308 scrollindicator ();
1309 showsel ();
1310 showoutline state.outline;
1311 enttext ();
1312 Glut.swapBuffers ();
1315 let getlink x y =
1316 let rec f = function
1317 | l :: rest ->
1318 begin match getopaque l.pageno with
1319 | Some opaque when validopaque opaque ->
1320 let y = y - l.pagedispy in
1321 if y > 0
1322 then
1323 let y = l.pagey + y in
1324 match getlink opaque x y with
1325 | None -> f rest
1326 | some -> some
1327 else
1328 f rest
1329 | _ ->
1330 f rest
1332 | [] -> None
1334 f state.layout
1337 let checklink x y =
1338 let rec f = function
1339 | l :: rest ->
1340 begin match getopaque l.pageno with
1341 | Some opaque when validopaque opaque ->
1342 let y = y - l.pagedispy in
1343 if y > 0
1344 then
1345 let y = l.pagey + y in
1346 if checklink opaque x y then true else f rest
1347 else
1348 f rest
1349 | _ ->
1350 f rest
1352 | [] -> false
1354 f state.layout
1357 let mouse ~button ~bstate ~x ~y =
1358 match button with
1359 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1360 let incr =
1361 if n = 3
1362 then
1363 -conf.scrollincr
1364 else
1365 conf.scrollincr
1367 let incr = incr * 2 in
1368 let y = clamp incr in
1369 gotoy y
1371 | Glut.LEFT_BUTTON when state.outline = None ->
1372 let dest = if bstate = Glut.DOWN then getlink x y else None in
1373 begin match dest with
1374 | Some (pageno, top) ->
1375 gotopage pageno top
1377 | None ->
1378 if bstate = Glut.DOWN
1379 then (
1380 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1381 state.mstate <- Msel ((x, y), (x, y));
1382 Glut.postRedisplay ()
1384 else (
1385 Glut.setCursor Glut.CURSOR_INHERIT;
1386 state.mstate <- Mnone;
1390 | _ ->
1393 let mouse ~button ~state ~x ~y = mouse button state x y;;
1395 let motion ~x ~y =
1396 if state.outline = None
1397 then
1398 match state.mstate with
1399 | Mnone -> ()
1400 | Msel (a, _) ->
1401 state.mstate <- Msel (a, (x, y));
1402 Glut.postRedisplay ()
1405 let pmotion ~x ~y =
1406 if state.outline = None
1407 then
1408 match state.mstate with
1409 | Mnone when (checklink x y) ->
1410 Glut.setCursor Glut.CURSOR_INFO
1412 | Mnone ->
1413 Glut.setCursor Glut.CURSOR_INHERIT
1415 | Msel (a, _) ->
1419 let () =
1420 let statepath = (Sys.getenv "HOME") ^ "/.config/llpp" in
1421 let pstate =
1423 let ic = open_in_bin statepath in
1424 let hash = input_value ic in
1425 close_in ic;
1426 hash
1427 with exn ->
1428 if false
1429 then
1430 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1432 Hashtbl.create 1
1434 let savestate () =
1436 let w, h =
1437 match state.fullscreen with
1438 | None -> state.w, state.h
1439 | Some wh -> wh
1441 if conf.markonquit then quickbookmark ();
1442 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1443 let oc = open_out_bin statepath in
1444 output_value oc pstate
1445 with exn ->
1446 if false
1447 then
1448 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1451 let setstate () =
1453 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1454 state.w <- statew;
1455 state.h <- stateh;
1456 state.bookmarks <- statebookmarks;
1457 with Not_found -> ()
1458 | exn ->
1459 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1462 Arg.parse [] (fun s -> state.path <- s) "options:";
1463 let name =
1464 if String.length state.path = 0
1465 then (prerr_endline "filename missing"; exit 1)
1466 else state.path
1469 setstate ();
1470 let _ = Glut.init Sys.argv in
1471 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1472 let () = Glut.initWindowSize state.w state.h in
1473 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1475 let csock, ssock = Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 in
1477 init ssock;
1478 state.csock <- csock;
1479 state.ssock <- ssock;
1480 writecmd csock ("open " ^ name ^ "\000");
1482 let () = Glut.displayFunc display in
1483 let () = Glut.reshapeFunc reshape in
1484 let () = Glut.keyboardFunc keyboard in
1485 let () = Glut.specialFunc special in
1486 let () = Glut.idleFunc (Some idle) in
1487 let () = Glut.mouseFunc mouse in
1488 let () = Glut.motionFunc motion in
1489 let () = Glut.passiveMotionFunc pmotion in
1491 at_exit savestate;
1493 let rec handlelablglutbug () =
1495 Glut.mainLoop ();
1496 with Glut.BadEnum "key in special_of_int" ->
1497 showtext '!' " LablGlut bug: special key not recognized";
1498 Glut.swapBuffers ();
1499 handlelablglutbug ()
1501 handlelablglutbug ();