Use pdf_open_xref instead of open coding things
[llpp.git] / main.ml
blobe62616b4822ccb43d1990478a697b5c23547f6cb
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 external init : Unix.file_descr -> unit = "ml_init";;
12 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
13 external seltext : string -> (int * int * int * int) -> int -> unit =
14 "ml_seltext";;
15 external copysel : string -> unit = "ml_copysel";;
16 external getpagewh : int -> float array = "ml_getpagewh";;
17 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
19 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
21 type 'a circbuf =
22 { store : 'a array
23 ; mutable rc : int
24 ; mutable wc : int
25 ; mutable len : int
29 type textentry = (char * string * onhist option * onkey * ondone)
30 and onkey = string -> int -> te
31 and ondone = string -> unit
32 and onhist = histcmd -> string
33 and histcmd = HCnext | HCprev | HCfirst | HClast
34 and te =
35 | TEstop
36 | TEdone of string
37 | TEcont of string
38 | TEswitch of textentry
41 let cbnew n v =
42 { store = Array.create n v
43 ; rc = 0
44 ; wc = 0
45 ; len = 0
49 let cblen b = Array.length b.store;;
51 let cbput b v =
52 let len = cblen b in
53 b.store.(b.wc) <- v;
54 b.wc <- (b.wc + 1) mod len;
55 b.len <- min (b.len + 1) len;
58 let cbpeekw b = b.store.(b.wc);;
60 let cbget b dir =
61 if b.len = 0 then b.store.(0) else
62 let rc = b.rc + dir in
63 let rc = if rc = -1 then b.len - 1 else rc in
64 let rc = if rc = b.len then 0 else rc in
65 b.rc <- rc;
66 b.store.(rc);
69 let cbrfollowlen b =
70 b.rc <- b.len;
73 type layout =
74 { pageno : int
75 ; pagedimno : int
76 ; pagew : int
77 ; pageh : int
78 ; pagedispy : int
79 ; pagey : int
80 ; pagevh : int
84 type conf =
85 { mutable scrollw : int
86 ; mutable scrollh : int
87 ; mutable icase : bool
88 ; mutable preload : bool
89 ; mutable pagebias : int
90 ; mutable verbose : bool
91 ; mutable scrollincr : int
92 ; mutable maxhfit : bool
93 ; mutable crophack : bool
94 ; mutable autoscroll : bool
95 ; mutable showall : bool
96 ; mutable hlinks : bool
97 ; mutable underinfo : bool
101 type outline = string * int * int * float;;
102 type outlines =
103 | Oarray of outline array
104 | Olist of outline list
105 | Onarrow of outline array * outline array
108 type rect = (float * float * float * float * float * float * float * float);;
110 type state =
111 { mutable csock : Unix.file_descr
112 ; mutable ssock : Unix.file_descr
113 ; mutable w : int
114 ; mutable h : int
115 ; mutable rotate : int
116 ; mutable y : int
117 ; mutable ty : float
118 ; mutable maxy : int
119 ; mutable layout : layout list
120 ; pagemap : ((int * int * int), string) Hashtbl.t
121 ; mutable pages : (int * int * int) list
122 ; mutable pagecount : int
123 ; pagecache : string circbuf
124 ; mutable rendering : bool
125 ; mutable mstate : mstate
126 ; mutable searchpattern : string
127 ; mutable rects : (int * int * rect) list
128 ; mutable rects1 : (int * int * rect) list
129 ; mutable text : string
130 ; mutable fullscreen : (int * int) option
131 ; mutable textentry : textentry option
132 ; mutable outlines : outlines
133 ; mutable outline : (bool * int * int * outline array * string) option
134 ; mutable bookmarks : outline list
135 ; mutable path : string
136 ; mutable invalidated : int
137 ; mutable colorscale : float
138 ; hists : hists
140 and hists =
141 { pat : string circbuf
142 ; pag : string circbuf
143 ; nav : float circbuf
147 let conf =
148 { scrollw = 5
149 ; scrollh = 12
150 ; icase = true
151 ; preload = true
152 ; pagebias = 0
153 ; verbose = false
154 ; scrollincr = 24
155 ; maxhfit = true
156 ; crophack = false
157 ; autoscroll = false
158 ; showall = false
159 ; hlinks = false
160 ; underinfo = false
164 let state =
165 { csock = Unix.stdin
166 ; ssock = Unix.stdin
167 ; w = 900
168 ; h = 900
169 ; rotate = 0
170 ; y = 0
171 ; ty = 0.0
172 ; layout = []
173 ; maxy = max_int
174 ; pagemap = Hashtbl.create 10
175 ; pagecache = cbnew 10 ""
176 ; pages = []
177 ; pagecount = 0
178 ; rendering = false
179 ; mstate = Mnone
180 ; rects = []
181 ; rects1 = []
182 ; text = ""
183 ; fullscreen = None
184 ; textentry = None
185 ; searchpattern = ""
186 ; outlines = Olist []
187 ; outline = None
188 ; bookmarks = []
189 ; path = ""
190 ; invalidated = 0
191 ; hists =
192 { nav = cbnew 100 0.0
193 ; pat = cbnew 20 ""
194 ; pag = cbnew 10 ""
196 ; colorscale = 1.0
200 let vlog fmt =
201 if conf.verbose
202 then
203 Printf.kprintf prerr_endline fmt
204 else
205 Printf.kprintf ignore fmt
208 let writecmd fd s =
209 let len = String.length s in
210 let n = 4 + len in
211 let b = Buffer.create n in
212 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
213 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
214 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
215 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
216 Buffer.add_string b s;
217 let s' = Buffer.contents b in
218 let n' = Unix.write fd s' 0 n in
219 if n' != n then failwith "write failed";
222 let readcmd fd =
223 let s = "xxxx" in
224 let n = Unix.read fd s 0 4 in
225 if n != 4 then failwith "incomplete read(len)";
226 let len = 0
227 lor (Char.code s.[0] lsl 24)
228 lor (Char.code s.[1] lsl 16)
229 lor (Char.code s.[2] lsl 8)
230 lor (Char.code s.[3] lsl 0)
232 let s = String.create len in
233 let n = Unix.read fd s 0 len in
234 if n != len then failwith "incomplete read(data)";
238 let yratio y =
239 if y = state.maxy then 1.0
240 else float y /. float state.maxy
243 let makecmd s l =
244 let b = Buffer.create 10 in
245 Buffer.add_string b s;
246 let rec combine = function
247 | [] -> b
248 | x :: xs ->
249 Buffer.add_char b ' ';
250 let s =
251 match x with
252 | `b b -> if b then "1" else "0"
253 | `s s -> s
254 | `i i -> string_of_int i
255 | `f f -> string_of_float f
256 | `I f -> string_of_int (truncate f)
258 Buffer.add_string b s;
259 combine xs;
261 combine l;
264 let wcmd s l =
265 let cmd = Buffer.contents (makecmd s l) in
266 writecmd state.csock cmd;
269 let calcheight () =
270 let rec f pn ph fh l =
271 match l with
272 | (n, _, h) :: rest ->
273 let fh = fh + (n - pn) * ph in
274 f n h fh rest
276 | [] ->
277 let fh = fh + (ph * (state.pagecount - pn)) in
278 max 0 fh
280 let fh = f 0 0 0 state.pages in
284 let getpageyh pageno =
285 let rec f pn ph y l =
286 match l with
287 | (n, _, h) :: rest ->
288 if n >= pageno
289 then
290 y + (pageno - pn) * ph, h
291 else
292 let y = y + (n - pn) * ph in
293 f n h y rest
295 | [] ->
296 y + (pageno - pn) * ph, ph
298 f 0 0 0 state.pages;
301 let getpagey pageno = fst (getpageyh pageno);;
303 let layout y sh =
304 let rec f pageno pdimno prev vy py dy l cacheleft accu =
305 if pageno = state.pagecount || cacheleft = 0
306 then accu
307 else
308 let ((_, w, h) as curr), rest, pdimno =
309 match l with
310 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
311 curr, rest, pdimno + 1
312 | _ ->
313 prev, l, pdimno
315 let pageno' = pageno + 1 in
316 if py + h > vy
317 then
318 let py' = vy - py in
319 let vh = h - py' in
320 if dy + vh > sh
321 then
322 let vh = sh - dy in
323 if vh <= 0
324 then
325 accu
326 else
327 let e =
328 { pageno = pageno
329 ; pagedimno = pdimno
330 ; pagew = w
331 ; pageh = h
332 ; pagedispy = dy
333 ; pagey = py'
334 ; pagevh = vh
337 e :: accu
338 else
339 let e =
340 { pageno = pageno
341 ; pagedimno = pdimno
342 ; pagew = w
343 ; pageh = h
344 ; pagedispy = dy
345 ; pagey = py'
346 ; pagevh = vh
349 let accu = e :: accu in
350 f pageno' pdimno curr
351 (vy + vh) (py + h) (dy + vh + 2) rest
352 (pred cacheleft) accu
353 else
354 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
356 if state.invalidated = 0
357 then
358 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
359 state.maxy <- calcheight ();
360 List.rev accu
361 else
365 let clamp incr =
366 let y = state.y + incr in
367 let y = max 0 y in
368 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
372 let getopaque pageno =
373 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
374 state.rotate))
375 with Not_found -> None
378 let cache pageno opaque =
379 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
380 state.rotate) opaque
383 let validopaque opaque = String.length opaque > 0;;
385 let render l =
386 match getopaque l.pageno with
387 | None when not state.rendering ->
388 state.rendering <- true;
389 cache l.pageno "";
390 wcmd "render" [`i (l.pageno + 1)
391 ;`i l.pagedimno
392 ;`i l.pagew
393 ;`i l.pageh];
395 | _ -> ()
398 let loadlayout layout =
399 let rec f all = function
400 | l :: ls ->
401 begin match getopaque l.pageno with
402 | None -> render l; f false ls
403 | Some opaque -> f (all && validopaque opaque) ls
405 | [] -> all
407 f (layout <> []) layout;
410 let preload () =
411 if conf.preload then
412 let evictedvisible =
413 let evictedopaque = cbpeekw state.pagecache in
414 List.exists (fun l ->
415 match getopaque l.pageno with
416 | Some opaque when validopaque opaque ->
417 evictedopaque = opaque
418 | otherwise -> false
419 ) state.layout
421 if not evictedvisible then
422 let y = if state.y < state.h then 0 else state.y - state.h in
423 let pages = layout y (state.h*3) in
424 List.iter render pages;
427 let gotoy y =
428 let y = max 0 y in
429 let y = min state.maxy y in
430 let pages = layout y state.h in
431 let ready = loadlayout pages in
432 state.ty <- yratio y;
433 if conf.showall then (
434 if ready then (
435 state.layout <- pages;
436 state.y <- y;
437 Glut.postRedisplay ();
440 else (
441 state.layout <- pages;
442 state.y <- y;
443 Glut.postRedisplay ();
445 preload ();
448 let addnav () =
449 cbput state.hists.nav (yratio state.y);
450 cbrfollowlen state.hists.nav;
453 let getnav () =
454 let y = cbget state.hists.nav ~-1 in
455 truncate (y *. float state.maxy)
458 let gotopage n top =
459 let y, h = getpageyh n in
460 addnav ();
461 gotoy (y + (truncate (top *. float h)));
464 let gotopage1 n top =
465 let y = getpagey n in
466 addnav ();
467 gotoy (y + top);
470 let invalidate () =
471 state.layout <- [];
472 state.pages <- [];
473 state.rects <- [];
474 state.rects1 <- [];
475 state.invalidated <- state.invalidated + 1;
478 let scalecolor c =
479 let c = c *. state.colorscale in
480 (c, c, c);
483 let reshape ~w ~h =
484 state.w <- w;
485 state.h <- h;
486 GlDraw.viewport 0 0 w h;
487 GlMat.mode `modelview;
488 GlMat.load_identity ();
489 GlMat.mode `projection;
490 GlMat.load_identity ();
491 GlMat.rotate ~x:1.0 ~angle:180.0 ();
492 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
493 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
494 GlClear.color (scalecolor 1.0);
495 GlClear.clear [`color];
497 invalidate ();
498 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
501 let showtext c s =
502 GlDraw.color (0.0, 0.0, 0.0);
503 GlDraw.rect
504 (0.0, float (state.h - 18))
505 (float (state.w - conf.scrollw - 1), float state.h)
507 let font = Glut.BITMAP_8_BY_13 in
508 GlDraw.color (1.0, 1.0, 1.0);
509 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
510 Glut.bitmapCharacter ~font ~c:(Char.code c);
511 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
514 let enttext () =
515 let len = String.length state.text in
516 match state.textentry with
517 | None ->
518 if len > 0 then showtext ' ' state.text
520 | Some (c, text, _, _, _) ->
521 let s =
522 if len > 0
523 then
524 text ^ " [" ^ state.text ^ "]"
525 else
526 text
528 showtext c s;
531 let showtext c s =
532 if true
533 then (
534 state.text <- Printf.sprintf "%c%s" c s;
535 Glut.postRedisplay ();
537 else (
538 showtext c s;
539 Glut.swapBuffers ();
543 let act cmd =
544 match cmd.[0] with
545 | 'c' ->
546 state.pages <- [];
548 | 'D' ->
549 state.rects <- state.rects1;
550 Glut.postRedisplay ()
552 | 'C' ->
553 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
554 state.pagecount <- n;
555 state.invalidated <- state.invalidated - 1;
556 if state.invalidated = 0
557 then (
558 let rely = yratio state.y in
559 state.maxy <- calcheight ();
560 gotoy (truncate (float state.maxy *. rely));
563 | 't' ->
564 let s = Scanf.sscanf cmd "t %n"
565 (fun n -> String.sub cmd n (String.length cmd - n))
567 Glut.setWindowTitle s
569 | 'T' ->
570 let s = Scanf.sscanf cmd "T %n"
571 (fun n -> String.sub cmd n (String.length cmd - n))
573 if state.textentry = None
574 then (
575 state.text <- s;
576 showtext ' ' s;
578 else (
579 state.text <- s;
580 Glut.postRedisplay ();
583 | 'V' ->
584 if conf.verbose
585 then
586 let s = Scanf.sscanf cmd "V %n"
587 (fun n -> String.sub cmd n (String.length cmd - n))
589 state.text <- s;
590 showtext ' ' s;
592 | 'F' ->
593 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
594 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
595 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
596 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
598 let y = (getpagey pageno) + truncate y0 in
599 addnav ();
600 gotoy y;
601 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
603 | 'R' ->
604 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
605 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
606 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
607 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
609 state.rects1 <-
610 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
612 | 'r' ->
613 let n, w, h, r, p =
614 Scanf.sscanf cmd "r %d %d %d %d %s"
615 (fun n w h r p -> (n, w, h, r, p))
617 Hashtbl.replace state.pagemap (n, w, r) p;
618 let opaque = cbpeekw state.pagecache in
619 if validopaque opaque
620 then (
621 let k =
622 Hashtbl.fold
623 (fun k v a -> if v = opaque then k else a)
624 state.pagemap (-1, -1, -1)
626 wcmd "free" [`s opaque];
627 Hashtbl.remove state.pagemap k
629 cbput state.pagecache p;
630 state.rendering <- false;
631 if conf.showall
632 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
633 else (
634 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
635 if visible then gotoy state.y
636 else (ignore (loadlayout state.layout); preload ())
639 | 'l' ->
640 let (n, w, h) as pagelayout =
641 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
643 state.pages <- pagelayout :: state.pages
645 | 'o' ->
646 let (l, n, t, h, pos) =
647 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
649 let s = String.sub cmd pos (String.length cmd - pos) in
650 let s =
651 let l = String.length s in
652 let b = Buffer.create (String.length s) in
653 let rec loop pc2 i =
654 if i = l then () else
655 let pc2 =
656 match s.[i] with
657 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
658 | '\xc2' -> true
659 | c ->
660 let c = if Char.code c land 0x80 = 0 then c else '?' in
661 Buffer.add_char b c;
662 false
664 loop pc2 (i+1)
666 loop false 0;
667 Buffer.contents b
669 let outline = (s, l, n, float t /. float h) in
670 let outlines =
671 match state.outlines with
672 | Olist outlines -> Olist (outline :: outlines)
673 | Oarray _ -> Olist [outline]
674 | Onarrow _ -> Olist [outline]
676 state.outlines <- outlines
678 | _ ->
679 log "unknown cmd `%S'" cmd
682 let now = Unix.gettimeofday;;
684 let idle () =
685 let rec loop delay =
686 let r, _, _ = Unix.select [state.csock] [] [] delay in
687 begin match r with
688 | [] ->
689 if conf.autoscroll then begin
690 let y = state.y + conf.scrollincr in
691 let y = if y >= state.maxy then 0 else y in
692 gotoy y;
693 state.text <- "";
694 end;
696 | _ ->
697 let cmd = readcmd state.csock in
698 act cmd;
699 loop 0.0
700 end;
701 in loop 0.001
704 let onhist cb = function
705 | HCprev -> cbget cb ~-1
706 | HCnext -> cbget cb 1
707 | HCfirst -> cbget cb ~-(cb.rc)
708 | HClast -> cbget cb (cb.len - 1 - cb.rc)
711 let search pattern forward =
712 if String.length pattern > 0
713 then
714 let pn, py =
715 match state.layout with
716 | [] -> 0, 0
717 | l :: _ ->
718 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
720 let cmd =
721 let b = makecmd "search"
722 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
724 Buffer.add_char b ',';
725 Buffer.add_string b pattern;
726 Buffer.add_char b '\000';
727 Buffer.contents b;
729 writecmd state.csock cmd;
732 let intentry text key =
733 let c = Char.unsafe_chr key in
734 match c with
735 | '0' .. '9' ->
736 let s = "x" in s.[0] <- c;
737 let text = text ^ s in
738 TEcont text
740 | _ ->
741 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
742 TEcont text
745 let addchar s c =
746 let b = Buffer.create (String.length s + 1) in
747 Buffer.add_string b s;
748 Buffer.add_char b c;
749 Buffer.contents b;
752 let textentry text key =
753 let c = Char.unsafe_chr key in
754 match c with
755 | _ when key >= 32 && key < 127 ->
756 let text = addchar text c in
757 TEcont text
759 | _ ->
760 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
761 TEcont text
764 let rotate angle =
765 state.rotate <- angle;
766 invalidate ();
767 wcmd "rotate" [`i angle];
770 let optentry text key =
771 let btos b = if b then "on" else "off" in
772 let c = Char.unsafe_chr key in
773 match c with
774 | 's' ->
775 let ondone s =
776 try conf.scrollincr <- int_of_string s with exc ->
777 state.text <- Printf.sprintf "bad integer `%s': %s"
778 s (Printexc.to_string exc)
780 TEswitch ('#', "", None, intentry, ondone)
782 | 'R' ->
783 let ondone s =
784 match try
785 Some (int_of_string s)
786 with exc ->
787 state.text <- Printf.sprintf "bad integer `%s': %s"
788 s (Printexc.to_string exc);
789 None
790 with
791 | Some angle -> rotate angle
792 | None -> ()
794 TEswitch ('^', "", None, intentry, ondone)
796 | 'i' ->
797 conf.icase <- not conf.icase;
798 TEdone ("case insensitive search " ^ (btos conf.icase))
800 | 'p' ->
801 conf.preload <- not conf.preload;
802 gotoy state.y;
803 TEdone ("preload " ^ (btos conf.preload))
805 | 'v' ->
806 conf.verbose <- not conf.verbose;
807 TEdone ("verbose " ^ (btos conf.verbose))
809 | 'h' ->
810 conf.maxhfit <- not conf.maxhfit;
811 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
812 TEdone ("maxhfit " ^ (btos conf.maxhfit))
814 | 'c' ->
815 conf.crophack <- not conf.crophack;
816 TEdone ("crophack " ^ btos conf.crophack)
818 | 'a' ->
819 conf.showall <- not conf.showall;
820 TEdone ("showall " ^ btos conf.showall)
822 | 'f' ->
823 conf.underinfo <- not conf.underinfo;
824 TEdone ("underinfo " ^ btos conf.underinfo)
826 | _ ->
827 state.text <- Printf.sprintf "bad option %d `%c'" key c;
828 TEstop
831 let maxoutlinerows () = (state.h - 31) / 16;;
833 let enterselector allowdel outlines errmsg =
834 if Array.length outlines = 0
835 then (
836 showtext ' ' errmsg;
838 else (
839 Glut.setCursor Glut.CURSOR_INHERIT;
840 let pageno =
841 match state.layout with
842 | [] -> -1
843 | {pageno=pageno} :: rest -> pageno
845 let active =
846 let rec loop n =
847 if n = Array.length outlines
848 then 0
849 else
850 let (_, _, outlinepageno, _) = outlines.(n) in
851 if outlinepageno >= pageno then n else loop (n+1)
853 loop 0
855 state.outline <-
856 Some (allowdel, active,
857 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
858 Glut.postRedisplay ();
862 let enteroutlinemode () =
863 let outlines =
864 match state.outlines with
865 | Oarray a -> a
866 | Olist l ->
867 let a = Array.of_list (List.rev l) in
868 state.outlines <- Oarray a;
870 | Onarrow (a, b) -> a
872 enterselector false outlines "Document has no outline";
875 let enterbookmarkmode () =
876 let bookmarks = Array.of_list state.bookmarks in
877 enterselector true bookmarks "Document has no bookmarks (yet)";
881 let quickbookmark ?title () =
882 match state.layout with
883 | [] -> ()
884 | l :: _ ->
885 let title =
886 match title with
887 | None ->
888 let sec = Unix.gettimeofday () in
889 let tm = Unix.localtime sec in
890 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
891 l.pageno
892 tm.Unix.tm_mday
893 tm.Unix.tm_mon
894 (tm.Unix.tm_year + 1900)
895 tm.Unix.tm_hour
896 tm.Unix.tm_min
897 | Some title -> title
899 state.bookmarks <-
900 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
903 let doreshape w h =
904 state.fullscreen <- None;
905 Glut.reshapeWindow w h;
908 let opendoc path =
909 invalidate ();
910 state.path <- path;
911 Hashtbl.clear state.pagemap;
913 writecmd state.csock ("open " ^ path ^ "\000");
914 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
915 wcmd "geometry" [`i (state.w - conf.scrollw); `i state.h];
918 let viewkeyboard ~key ~x ~y =
919 let enttext te =
920 state.textentry <- te;
921 state.text <- "";
922 enttext ();
923 Glut.postRedisplay ()
925 match state.textentry with
926 | None ->
927 let c = Char.chr key in
928 begin match c with
929 | '\027' | 'q' ->
930 exit 0
932 | '\008' ->
933 let y = getnav () in
934 gotoy y
936 | 'o' ->
937 enteroutlinemode ()
939 | 'u' ->
940 state.rects <- [];
941 state.text <- "";
942 Glut.postRedisplay ()
944 | '/' | '?' ->
945 let ondone isforw s =
946 cbput state.hists.pat s;
947 cbrfollowlen state.hists.pat;
948 state.searchpattern <- s;
949 search s isforw
951 enttext (Some (c, "", Some (onhist state.hists.pat),
952 textentry, ondone (c ='/')))
954 | '+' ->
955 let ondone s =
956 let n =
957 try int_of_string s with exc ->
958 state.text <- Printf.sprintf "bad integer `%s': %s"
959 s (Printexc.to_string exc);
960 max_int
962 if n != max_int
963 then (
964 conf.pagebias <- n;
965 state.text <- "page bias is now " ^ string_of_int n;
968 enttext (Some ('+', "", None, intentry, ondone))
970 | '-' ->
971 let ondone msg =
972 state.text <- msg;
974 enttext (Some ('-', "", None, optentry, ondone))
976 | '0' .. '9' ->
977 let ondone s =
978 let n =
979 try int_of_string s with exc ->
980 state.text <- Printf.sprintf "bad integer `%s': %s"
981 s (Printexc.to_string exc);
984 if n >= 0
985 then (
986 addnav ();
987 cbput state.hists.pag (string_of_int n);
988 cbrfollowlen state.hists.pag;
989 gotoy (getpagey (n + conf.pagebias - 1))
992 let pageentry text key =
993 match Char.unsafe_chr key with
994 | 'g' -> TEdone text
995 | _ -> intentry text key
997 let text = "x" in text.[0] <- c;
998 enttext (Some (':', text, Some (onhist state.hists.pag),
999 pageentry, ondone))
1001 | 'b' ->
1002 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
1003 reshape state.w state.h;
1005 | 'l' ->
1006 conf.hlinks <- not conf.hlinks;
1007 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1008 Glut.postRedisplay ()
1010 | 'a' ->
1011 conf.autoscroll <- not conf.autoscroll
1013 | 'f' ->
1014 begin match state.fullscreen with
1015 | None ->
1016 state.fullscreen <- Some (state.w, state.h);
1017 Glut.fullScreen ()
1018 | Some (w, h) ->
1019 state.fullscreen <- None;
1020 doreshape w h
1023 | 'g' ->
1024 gotoy 0
1026 | 'n' ->
1027 search state.searchpattern true
1029 | 'p' | 'N' ->
1030 search state.searchpattern false
1032 | 't' ->
1033 begin match state.layout with
1034 | [] -> ()
1035 | l :: _ ->
1036 gotoy (state.y - l.pagey);
1039 | ' ' ->
1040 begin match List.rev state.layout with
1041 | [] -> ()
1042 | l :: _ ->
1043 gotoy (clamp (l.pageh - l.pagey))
1046 | '\127' ->
1047 begin match state.layout with
1048 | [] -> ()
1049 | l :: _ ->
1050 gotoy (clamp (-l.pageh));
1053 | '=' ->
1054 let f (fn, ln) l =
1055 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1057 let fn, ln = List.fold_left f (-1, -1) state.layout in
1058 let s =
1059 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1060 let percent =
1061 if maxy <= 0
1062 then 100.
1063 else (100. *. (float state.y /. float maxy)) in
1064 if fn = ln
1065 then
1066 Printf.sprintf "Page %d of %d %.2f%%"
1067 (fn+1) state.pagecount percent
1068 else
1069 Printf.sprintf
1070 "Pages %d-%d of %d %.2f%%"
1071 (fn+1) (ln+1) state.pagecount percent
1073 showtext ' ' s;
1075 | 'w' ->
1076 begin match state.layout with
1077 | [] -> ()
1078 | l :: _ ->
1079 doreshape (l.pagew + conf.scrollw) l.pageh;
1080 Glut.postRedisplay ();
1083 | '\'' ->
1084 enterbookmarkmode ()
1086 | 'm' ->
1087 let ondone s =
1088 match state.layout with
1089 | l :: _ ->
1090 state.bookmarks <-
1091 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1092 :: state.bookmarks
1093 | _ -> ()
1095 enttext (Some ('~', "", None, textentry, ondone))
1097 | '~' ->
1098 quickbookmark ();
1099 showtext ' ' "Quick bookmark added";
1101 | 'z' ->
1102 begin match state.layout with
1103 | l :: _ ->
1104 let a = getpagewh l.pagedimno in
1105 let w, h =
1106 if conf.crophack
1107 then
1108 (truncate (1.8 *. (a.(1) -. a.(0))),
1109 truncate (1.2 *. (a.(3) -. a.(0))))
1110 else
1111 (truncate (a.(1) -. a.(0)),
1112 truncate (a.(3) -. a.(0)))
1114 doreshape (w + conf.scrollw) h;
1115 Glut.postRedisplay ();
1117 | [] -> ()
1120 | '<' | '>' ->
1121 rotate (state.rotate + (if c = '>' then 30 else -30));
1123 | '[' | ']' ->
1124 state.colorscale <-
1125 max 0.0
1126 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1127 Glut.postRedisplay ()
1129 | 'k' -> gotoy (clamp (-conf.scrollincr))
1130 | 'j' -> gotoy (clamp conf.scrollincr)
1132 | 'r' -> opendoc state.path
1134 | _ ->
1135 vlog "huh? %d %c" key (Char.chr key);
1138 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1139 let len = String.length text in
1140 if len = 0
1141 then (
1142 state.textentry <- None;
1143 Glut.postRedisplay ();
1145 else (
1146 let s = String.sub text 0 (len - 1) in
1147 enttext (Some (c, s, onhist, onkey, ondone))
1150 | Some (c, text, onhist, onkey, ondone) ->
1151 begin match Char.unsafe_chr key with
1152 | '\r' | '\n' ->
1153 ondone text;
1154 state.textentry <- None;
1155 Glut.postRedisplay ()
1157 | '\027' ->
1158 state.textentry <- None;
1159 Glut.postRedisplay ()
1161 | _ ->
1162 begin match onkey text key with
1163 | TEdone text ->
1164 state.textentry <- None;
1165 ondone text;
1166 Glut.postRedisplay ()
1168 | TEcont text ->
1169 enttext (Some (c, text, onhist, onkey, ondone));
1171 | TEstop ->
1172 state.textentry <- None;
1173 Glut.postRedisplay ()
1175 | TEswitch te ->
1176 state.textentry <- Some te;
1177 Glut.postRedisplay ()
1178 end;
1179 end;
1182 let narrow outlines pattern =
1183 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1184 match reopt with
1185 | None -> None
1186 | Some re ->
1187 let rec fold accu n =
1188 if n = -1 then accu else
1189 let (s, _, _, _) as o = outlines.(n) in
1190 let accu =
1191 if (try ignore (Str.search_forward re s 0); true
1192 with Not_found -> false)
1193 then (o :: accu)
1194 else accu
1196 fold accu (n-1)
1198 let matched = fold [] (Array.length outlines - 1) in
1199 if matched = [] then None else Some (Array.of_list matched)
1202 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1203 let search active pattern incr =
1204 let dosearch re =
1205 let rec loop n =
1206 if n = Array.length outlines || n = -1 then None else
1207 let (s, _, _, _) = outlines.(n) in
1209 (try ignore (Str.search_forward re s 0); true
1210 with Not_found -> false)
1211 then Some n
1212 else loop (n + incr)
1214 loop active
1217 let re = Str.regexp_case_fold pattern in
1218 dosearch re
1219 with Failure s ->
1220 state.text <- s;
1221 None
1223 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1224 match key with
1225 | 27 ->
1226 if String.length qsearch = 0
1227 then (
1228 state.text <- "";
1229 state.outline <- None;
1230 Glut.postRedisplay ();
1232 else (
1233 state.text <- "";
1234 state.outline <- Some (allowdel, active, first, outlines, "");
1235 Glut.postRedisplay ();
1238 | 18 | 19 ->
1239 let incr = if key = 18 then -1 else 1 in
1240 let active, first =
1241 match search (active + incr) qsearch incr with
1242 | None ->
1243 state.text <- qsearch ^ " [not found]";
1244 active, first
1245 | Some active ->
1246 state.text <- qsearch;
1247 active, firstof active
1249 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1250 Glut.postRedisplay ();
1252 | 8 ->
1253 let len = String.length qsearch in
1254 if len = 0
1255 then ()
1256 else (
1257 if len = 1
1258 then (
1259 state.text <- "";
1260 state.outline <- Some (allowdel, active, first, outlines, "");
1262 else
1263 let qsearch = String.sub qsearch 0 (len - 1) in
1264 let active, first =
1265 match search active qsearch ~-1 with
1266 | None ->
1267 state.text <- qsearch ^ " [not found]";
1268 active, first
1269 | Some active ->
1270 state.text <- qsearch;
1271 active, firstof active
1273 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1275 Glut.postRedisplay ()
1277 | 13 ->
1278 if active < Array.length outlines
1279 then (
1280 let (_, _, n, t) = outlines.(active) in
1281 gotopage n t;
1283 state.text <- "";
1284 if allowdel then state.bookmarks <- Array.to_list outlines;
1285 state.outline <- None;
1286 Glut.postRedisplay ();
1288 | _ when key >= 32 && key < 127 ->
1289 let pattern = addchar qsearch (Char.chr key) in
1290 let active, first =
1291 match search active pattern 1 with
1292 | None ->
1293 state.text <- pattern ^ " [not found]";
1294 active, first
1295 | Some active ->
1296 state.text <- pattern;
1297 active, firstof active
1299 state.outline <- Some (allowdel, active, first, outlines, pattern);
1300 Glut.postRedisplay ()
1302 | 14 when not allowdel ->
1303 let optoutlines = narrow outlines qsearch in
1304 begin match optoutlines with
1305 | None -> state.text <- "can't narrow"
1306 | Some outlines ->
1307 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1308 match state.outlines with
1309 | Olist l -> ()
1310 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1311 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1312 end;
1313 Glut.postRedisplay ()
1315 | 21 when not allowdel ->
1316 let outline =
1317 match state.outlines with
1318 | Oarray a -> a
1319 | Olist l ->
1320 let a = Array.of_list (List.rev l) in
1321 state.outlines <- Oarray a;
1323 | Onarrow (a, b) ->
1324 state.outlines <- Oarray b;
1327 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1328 Glut.postRedisplay ()
1330 | 12 ->
1331 state.outline <-
1332 Some (allowdel, active, firstof active, outlines, qsearch);
1333 Glut.postRedisplay ()
1335 | 127 when allowdel ->
1336 let len = Array.length outlines - 1 in
1337 if len = 0
1338 then (
1339 state.outline <- None;
1340 state.bookmarks <- [];
1342 else (
1343 let bookmarks = Array.init len
1344 (fun i ->
1345 let i = if i >= active then i + 1 else i in
1346 outlines.(i)
1349 state.outline <-
1350 Some (allowdel,
1351 min active (len-1),
1352 min first (len-1),
1353 bookmarks, qsearch)
1356 Glut.postRedisplay ()
1358 | _ -> log "unknown key %d" key
1361 let keyboard ~key ~x ~y =
1362 if key = 7
1363 then
1364 wcmd "interrupt" []
1365 else
1366 match state.outline with
1367 | None -> viewkeyboard ~key ~x ~y
1368 | Some outline -> outlinekeyboard ~key ~x ~y outline
1371 let special ~key ~x ~y =
1372 match state.outline with
1373 | None ->
1374 begin match state.textentry with
1375 | None ->
1376 let y =
1377 match key with
1378 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1379 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1380 | Glut.KEY_DOWN -> clamp conf.scrollincr
1381 | Glut.KEY_PAGE_UP ->
1382 if Glut.getModifiers () land Glut.active_ctrl != 0
1383 then
1384 match state.layout with
1385 | [] -> state.y
1386 | l :: _ -> state.y - l.pagey
1387 else
1388 clamp (-state.h)
1389 | Glut.KEY_PAGE_DOWN ->
1390 if Glut.getModifiers () land Glut.active_ctrl != 0
1391 then
1392 match List.rev state.layout with
1393 | [] -> state.y
1394 | l :: _ -> getpagey l.pageno
1395 else
1396 clamp state.h
1397 | Glut.KEY_HOME -> addnav (); 0
1398 | Glut.KEY_END ->
1399 addnav ();
1400 state.maxy - (if conf.maxhfit then state.h else 0)
1401 | _ -> state.y
1403 if not conf.verbose then state.text <- "";
1404 gotoy y
1406 | Some (c, s, Some onhist, onkey, ondone) ->
1407 let s =
1408 match key with
1409 | Glut.KEY_UP -> onhist HCprev
1410 | Glut.KEY_DOWN -> onhist HCnext
1411 | Glut.KEY_HOME -> onhist HCfirst
1412 | Glut.KEY_END -> onhist HClast
1413 | _ -> state.text
1415 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1416 Glut.postRedisplay ()
1418 | _ -> ()
1421 | Some (allowdel, active, first, outlines, qsearch) ->
1422 let maxrows = maxoutlinerows () in
1423 let navigate incr =
1424 let active = active + incr in
1425 let active = max 0 (min active (Array.length outlines - 1)) in
1426 let first =
1427 if active > first
1428 then
1429 let rows = active - first in
1430 if rows > maxrows then active - maxrows else first
1431 else active
1433 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1434 Glut.postRedisplay ()
1436 match key with
1437 | Glut.KEY_UP -> navigate ~-1
1438 | Glut.KEY_DOWN -> navigate 1
1439 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1440 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1442 | Glut.KEY_HOME ->
1443 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1444 Glut.postRedisplay ()
1446 | Glut.KEY_END ->
1447 let active = Array.length outlines - 1 in
1448 let first = max 0 (active - maxrows) in
1449 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1450 Glut.postRedisplay ()
1452 | _ -> ()
1455 let drawplaceholder l =
1456 GlDraw.color (scalecolor 1.0);
1457 GlDraw.rect
1458 (0.0, float l.pagedispy)
1459 (float l.pagew, float (l.pagedispy + l.pagevh))
1461 let x = 0.0
1462 and y = float (l.pagedispy + 13) in
1463 let font = Glut.BITMAP_8_BY_13 in
1464 GlDraw.color (0.0, 0.0, 0.0);
1465 GlPix.raster_pos ~x ~y ();
1466 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1467 ("Loading " ^ string_of_int l.pageno);
1470 let now () = Unix.gettimeofday ();;
1472 let drawpage i l =
1473 begin match getopaque l.pageno with
1474 | Some opaque when validopaque opaque ->
1475 if state.textentry = None
1476 then GlDraw.color (scalecolor 1.0)
1477 else GlDraw.color (scalecolor 0.4);
1478 let a = now () in
1479 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks) opaque;
1480 let b = now () in
1481 let d = b-.a in
1482 vlog "draw %f sec" d;
1484 | _ ->
1485 drawplaceholder l;
1486 end;
1487 GlDraw.color (0.5, 0.5, 0.5);
1488 GlDraw.rect
1489 (0., float i)
1490 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1492 l.pagedispy + l.pagevh;
1495 let scrollindicator () =
1496 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1497 GlDraw.color (0.64 , 0.64, 0.64);
1498 GlDraw.rect
1499 (float (state.w - conf.scrollw), 0.)
1500 (float state.w, float state.h)
1502 GlDraw.color (0.0, 0.0, 0.0);
1503 let sh = (float (maxy + state.h) /. float state.h) in
1504 let sh = float state.h /. sh in
1505 let sh = max sh (float conf.scrollh) in
1507 let percent =
1508 if state.y = state.maxy
1509 then 1.0
1510 else float state.y /. float maxy
1512 let position = (float state.h -. sh) *. percent in
1514 let position =
1515 if position +. sh > float state.h
1516 then
1517 float state.h -. sh
1518 else
1519 position
1521 GlDraw.rect
1522 (float (state.w - conf.scrollw), position)
1523 (float state.w, position +. sh)
1527 let showsel () =
1528 match state.mstate with
1529 | Mnone ->
1532 | Msel ((x0, y0), (x1, y1)) ->
1533 let rec loop = function
1534 | l :: ls ->
1535 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1536 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1537 then
1538 match getopaque l.pageno with
1539 | Some opaque when validopaque opaque ->
1540 let oy = -l.pagey + l.pagedispy in
1541 seltext opaque (x0, y0, x1, y1) oy;
1543 | _ -> ()
1544 else loop ls
1545 | [] -> ()
1547 loop state.layout
1550 let showrects () =
1551 Gl.enable `blend;
1552 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1553 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1554 List.iter
1555 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1556 List.iter (fun l ->
1557 if l.pageno = pageno
1558 then (
1559 let d = float (l.pagedispy - l.pagey) in
1560 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1561 GlDraw.begins `quads;
1563 GlDraw.vertex2 (x0, y0+.d);
1564 GlDraw.vertex2 (x1, y1+.d);
1565 GlDraw.vertex2 (x2, y2+.d);
1566 GlDraw.vertex2 (x3, y3+.d);
1568 GlDraw.ends ();
1570 ) state.layout
1571 ) state.rects
1573 Gl.disable `blend;
1576 let showoutline = function
1577 | None -> ()
1578 | Some (allowdel, active, first, outlines, qsearch) ->
1579 Gl.enable `blend;
1580 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1581 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1582 GlDraw.rect (0., 0.) (float state.w, float state.h);
1583 Gl.disable `blend;
1585 GlDraw.color (1., 1., 1.);
1586 let font = Glut.BITMAP_9_BY_15 in
1587 let draw_string x y s =
1588 GlPix.raster_pos ~x ~y ();
1589 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1591 let rec loop row =
1592 if row = Array.length outlines || (row - first) * 16 > state.h
1593 then ()
1594 else (
1595 let (s, l, _, _) = outlines.(row) in
1596 let y = (row - first) * 16 in
1597 let x = 5 + 15*l in
1598 if row = active
1599 then (
1600 Gl.enable `blend;
1601 GlDraw.polygon_mode `both `line;
1602 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1603 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1604 GlDraw.rect (0., float (y + 1))
1605 (float (state.w - conf.scrollw - 1), float (y + 18));
1606 GlDraw.polygon_mode `both `fill;
1607 Gl.disable `blend;
1608 GlDraw.color (1., 1., 1.);
1610 draw_string (float x) (float (y + 16)) s;
1611 loop (row+1)
1614 loop first
1617 let display () =
1618 let lasty = List.fold_left drawpage 0 (state.layout) in
1619 GlDraw.color (scalecolor 0.5);
1620 GlDraw.rect
1621 (0., float lasty)
1622 (float (state.w - conf.scrollw), float state.h)
1624 showrects ();
1625 scrollindicator ();
1626 showsel ();
1627 showoutline state.outline;
1628 enttext ();
1629 Glut.swapBuffers ();
1632 let getunder x y =
1633 let rec f = function
1634 | l :: rest ->
1635 begin match getopaque l.pageno with
1636 | Some opaque when validopaque opaque ->
1637 let y = y - l.pagedispy in
1638 if y > 0
1639 then
1640 let y = l.pagey + y in
1641 match whatsunder opaque x y with
1642 | Unone -> f rest
1643 | under -> under
1644 else
1645 f rest
1646 | _ ->
1647 f rest
1649 | [] -> Unone
1651 f state.layout
1654 let mouse ~button ~bstate ~x ~y =
1655 match button with
1656 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1657 let incr =
1658 if n = 3
1659 then
1660 -conf.scrollincr
1661 else
1662 conf.scrollincr
1664 let incr = incr * 2 in
1665 let y = clamp incr in
1666 gotoy y
1668 | Glut.LEFT_BUTTON when state.outline = None ->
1669 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1670 begin match dest with
1671 | Ulinkgoto (pageno, top) ->
1672 if pageno >= 0
1673 then
1674 gotopage1 pageno top
1676 | Ulinkuri s ->
1677 print_endline s
1679 | Unone when bstate = Glut.DOWN ->
1680 Glut.setCursor Glut.CURSOR_INHERIT;
1681 state.mstate <- Mnone
1683 | Unone | Utext _ ->
1684 if bstate = Glut.DOWN
1685 then (
1686 if state.rotate mod 360 = 0 then (
1687 state.mstate <- Msel ((x, y), (x, y));
1688 Glut.postRedisplay ()
1691 else (
1692 match state.mstate with
1693 | Mnone -> ()
1694 | Msel ((x0, y0), (x1, y1)) ->
1695 let f l =
1696 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1697 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1698 then
1699 match getopaque l.pageno with
1700 | Some opaque when validopaque opaque ->
1701 copysel opaque
1702 | _ -> ()
1704 List.iter f state.layout;
1705 copysel ""; (* ugly *)
1706 Glut.setCursor Glut.CURSOR_INHERIT;
1707 state.mstate <- Mnone;
1711 | _ ->
1714 let mouse ~button ~state ~x ~y = mouse button state x y;;
1716 let motion ~x ~y =
1717 if state.outline = None
1718 then
1719 match state.mstate with
1720 | Mnone -> ()
1721 | Msel (a, _) ->
1722 state.mstate <- Msel (a, (x, y));
1723 Glut.postRedisplay ()
1726 let pmotion ~x ~y =
1727 if state.outline = None
1728 then
1729 match state.mstate with
1730 | Mnone ->
1731 begin match getunder x y with
1732 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1733 | Ulinkuri uri ->
1734 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1735 Glut.setCursor Glut.CURSOR_INFO
1736 | Ulinkgoto (page, y) ->
1737 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1738 Glut.setCursor Glut.CURSOR_INFO
1739 | Utext s ->
1740 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1741 Glut.setCursor Glut.CURSOR_TEXT
1744 | Msel (a, _) ->
1748 let () =
1749 let statepath =
1750 let home =
1751 if Sys.os_type = "Win32"
1752 then
1753 try Sys.getenv "HOMEPATH" with Not_found -> ""
1754 else
1755 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1757 Filename.concat home "llpp"
1759 let pstate =
1761 let ic = open_in_bin statepath in
1762 let hash = input_value ic in
1763 close_in ic;
1764 hash
1765 with exn ->
1766 if false
1767 then
1768 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1770 Hashtbl.create 1
1772 let savestate () =
1774 let w, h =
1775 match state.fullscreen with
1776 | None -> state.w, state.h
1777 | Some wh -> wh
1779 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1780 let oc = open_out_bin statepath in
1781 output_value oc pstate
1782 with exn ->
1783 if false
1784 then
1785 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1788 let setstate () =
1790 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1791 state.w <- statew;
1792 state.h <- stateh;
1793 state.bookmarks <- statebookmarks;
1794 with Not_found -> ()
1795 | exn ->
1796 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1799 Arg.parse [] (fun s -> state.path <- s) "options:";
1800 let name =
1801 if String.length state.path = 0
1802 then (prerr_endline "filename missing"; exit 1)
1803 else state.path
1806 setstate ();
1807 let _ = Glut.init Sys.argv in
1808 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1809 let () = Glut.initWindowSize state.w state.h in
1810 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1812 let csock, ssock =
1813 if Sys.os_type = "Unix"
1814 then
1815 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1816 else
1817 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1818 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1819 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1820 Unix.bind sock addr;
1821 Unix.listen sock 1;
1822 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1823 Unix.connect csock addr;
1824 let ssock, _ = Unix.accept sock in
1825 Unix.close sock;
1826 let opts sock =
1827 Unix.setsockopt sock Unix.TCP_NODELAY true;
1828 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1830 opts ssock;
1831 opts csock;
1832 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1833 ssock, csock
1836 let () = Glut.displayFunc display in
1837 let () = Glut.reshapeFunc reshape in
1838 let () = Glut.keyboardFunc keyboard in
1839 let () = Glut.specialFunc special in
1840 let () = Glut.idleFunc (Some idle) in
1841 let () = Glut.mouseFunc mouse in
1842 let () = Glut.motionFunc motion in
1843 let () = Glut.passiveMotionFunc pmotion in
1845 init ssock;
1846 state.csock <- csock;
1847 state.ssock <- ssock;
1848 state.text <- "Opening " ^ name;
1849 writecmd csock ("open " ^ name ^ "\000");
1851 at_exit savestate;
1853 let rec handlelablglutbug () =
1855 Glut.mainLoop ();
1856 with Glut.BadEnum "key in special_of_int" ->
1857 showtext '!' " LablGlut bug: special key not recognized";
1858 handlelablglutbug ()
1860 handlelablglutbug ();