Change the way outlines/bookmarks are stored
[llpp.git] / main.ml
blob986d9ae39b3a57ed0e015146b09f2dc62653d7d8
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 -> 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 highlightlinks : string -> int -> unit = "ml_highlightlinks";;
17 external getpagewh : int -> float array = "ml_getpagewh";;
18 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
20 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
22 type 'a circbuf =
23 { store : 'a array
24 ; mutable rc : int
25 ; mutable wc : int
26 ; mutable len : int
30 type textentry = (char * string * onhist option * onkey * ondone)
31 and onkey = string -> int -> te
32 and ondone = string -> unit
33 and onhist = histcmd -> string
34 and histcmd = HCnext | HCprev | HCfirst | HClast
35 and te =
36 | TEstop
37 | TEdone of string
38 | TEcont of string
39 | TEswitch of textentry
42 let cbnew n v =
43 { store = Array.create n v
44 ; rc = 0
45 ; wc = 0
46 ; len = 0
50 let cblen b = Array.length b.store;;
52 let cbput b v =
53 let len = cblen b in
54 b.store.(b.wc) <- v;
55 b.wc <- (b.wc + 1) mod len;
56 b.len <- min (b.len + 1) len;
59 let cbpeekw b = b.store.(b.wc);;
61 let cbget b dir =
62 if b.len = 0 then b.store.(0) else
63 let rc = b.rc + dir in
64 let rc = if rc = -1 then b.len - 1 else rc in
65 let rc = if rc = b.len then 0 else rc in
66 b.rc <- rc;
67 b.store.(rc);
70 let cbrfollowlen b =
71 b.rc <- b.len;
74 type layout =
75 { pageno : int
76 ; pagedimno : int
77 ; pagew : int
78 ; pageh : int
79 ; pagedispy : int
80 ; pagey : int
81 ; pagevh : int
85 type conf =
86 { mutable scrollw : int
87 ; mutable scrollh : int
88 ; mutable icase : bool
89 ; mutable preload : bool
90 ; mutable pagebias : int
91 ; mutable verbose : bool
92 ; mutable scrollincr : int
93 ; mutable maxhfit : bool
94 ; mutable crophack : bool
95 ; mutable autoscroll : bool
96 ; mutable showall : bool
97 ; mutable hlinks : bool
98 ; mutable underinfo : bool
102 type outline = string * int * int * float;;
103 type outlines =
104 | Oarray of outline array
105 | Olist of outline list
106 | Onarrow of outline array * outline array
109 type rect = (float * float * float * float * float * float * float * float);;
111 type state =
112 { mutable csock : Unix.file_descr
113 ; mutable ssock : Unix.file_descr
114 ; mutable w : int
115 ; mutable h : int
116 ; mutable rotate : int
117 ; mutable y : int
118 ; mutable ty : float
119 ; mutable maxy : int
120 ; mutable layout : layout list
121 ; pagemap : ((int * int * int), string) Hashtbl.t
122 ; mutable pages : (int * int * int) list
123 ; mutable pagecount : int
124 ; pagecache : string circbuf
125 ; mutable rendering : bool
126 ; mutable mstate : mstate
127 ; mutable searchpattern : string
128 ; mutable rects : (int * int * rect) list
129 ; mutable rects1 : (int * int * rect) list
130 ; mutable text : string
131 ; mutable fullscreen : (int * int) option
132 ; mutable textentry : textentry option
133 ; mutable outlines : outlines
134 ; mutable outline : (bool * int * int * outline array * string) option
135 ; mutable bookmarks : outline list
136 ; mutable path : string
137 ; mutable invalidated : int
138 ; mutable colorscale : float
139 ; hists : hists
141 and hists =
142 { pat : string circbuf
143 ; pag : string circbuf
144 ; nav : float circbuf
148 let conf =
149 { scrollw = 5
150 ; scrollh = 12
151 ; icase = true
152 ; preload = true
153 ; pagebias = 0
154 ; verbose = false
155 ; scrollincr = 24
156 ; maxhfit = true
157 ; crophack = false
158 ; autoscroll = false
159 ; showall = false
160 ; hlinks = false
161 ; underinfo = false
165 let state =
166 { csock = Unix.stdin
167 ; ssock = Unix.stdin
168 ; w = 900
169 ; h = 900
170 ; rotate = 0
171 ; y = 0
172 ; ty = 0.0
173 ; layout = []
174 ; maxy = max_int
175 ; pagemap = Hashtbl.create 10
176 ; pagecache = cbnew 10 ""
177 ; pages = []
178 ; pagecount = 0
179 ; rendering = false
180 ; mstate = Mnone
181 ; rects = []
182 ; rects1 = []
183 ; text = ""
184 ; fullscreen = None
185 ; textentry = None
186 ; searchpattern = ""
187 ; outlines = Olist []
188 ; outline = None
189 ; bookmarks = []
190 ; path = ""
191 ; invalidated = 0
192 ; hists =
193 { nav = cbnew 100 0.0
194 ; pat = cbnew 20 ""
195 ; pag = cbnew 10 ""
197 ; colorscale = 1.0
201 let vlog fmt =
202 if conf.verbose
203 then
204 Printf.kprintf prerr_endline fmt
205 else
206 Printf.kprintf ignore fmt
209 let writecmd fd s =
210 let len = String.length s in
211 let n = 4 + len in
212 let b = Buffer.create n in
213 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
214 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
215 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
216 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
217 Buffer.add_string b s;
218 let s' = Buffer.contents b in
219 let n' = Unix.write fd s' 0 n in
220 if n' != n then failwith "write failed";
223 let readcmd fd =
224 let s = "xxxx" in
225 let n = Unix.read fd s 0 4 in
226 if n != 4 then failwith "incomplete read(len)";
227 let len = 0
228 lor (Char.code s.[0] lsl 24)
229 lor (Char.code s.[1] lsl 16)
230 lor (Char.code s.[2] lsl 8)
231 lor (Char.code s.[3] lsl 0)
233 let s = String.create len in
234 let n = Unix.read fd s 0 len in
235 if n != len then failwith "incomplete read(data)";
239 let yratio y =
240 if y = state.maxy then 1.0
241 else float y /. float state.maxy
244 let makecmd s l =
245 let b = Buffer.create 10 in
246 Buffer.add_string b s;
247 let rec combine = function
248 | [] -> b
249 | x :: xs ->
250 Buffer.add_char b ' ';
251 let s =
252 match x with
253 | `b b -> if b then "1" else "0"
254 | `s s -> s
255 | `i i -> string_of_int i
256 | `f f -> string_of_float f
257 | `I f -> string_of_int (truncate f)
259 Buffer.add_string b s;
260 combine xs;
262 combine l;
265 let wcmd s l =
266 let cmd = Buffer.contents (makecmd s l) in
267 writecmd state.csock cmd;
270 let calcheight () =
271 let rec f pn ph fh l =
272 match l with
273 | (n, _, h) :: rest ->
274 let fh = fh + (n - pn) * ph in
275 f n h fh rest
277 | [] ->
278 let fh = fh + (ph * (state.pagecount - pn)) in
279 max 0 fh
281 let fh = f 0 0 0 state.pages in
285 let getpageyh pageno =
286 let rec f pn ph y l =
287 match l with
288 | (n, _, h) :: rest ->
289 if n >= pageno
290 then
291 y + (pageno - pn) * ph, h
292 else
293 let y = y + (n - pn) * ph in
294 f n h y rest
296 | [] ->
297 y + (pageno - pn) * ph, ph
299 f 0 0 0 state.pages;
302 let getpagey pageno = fst (getpageyh pageno);;
304 let layout y sh =
305 let rec f pageno pdimno prev vy py dy l cacheleft accu =
306 if pageno = state.pagecount || cacheleft = 0
307 then accu
308 else
309 let ((_, w, h) as curr), rest, pdimno =
310 match l with
311 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
312 curr, rest, pdimno + 1
313 | _ ->
314 prev, l, pdimno
316 let pageno' = pageno + 1 in
317 if py + h > vy
318 then
319 let py' = vy - py in
320 let vh = h - py' in
321 if dy + vh > sh
322 then
323 let vh = sh - dy in
324 if vh <= 0
325 then
326 accu
327 else
328 let e =
329 { pageno = pageno
330 ; pagedimno = pdimno
331 ; pagew = w
332 ; pageh = h
333 ; pagedispy = dy
334 ; pagey = py'
335 ; pagevh = vh
338 e :: accu
339 else
340 let e =
341 { pageno = pageno
342 ; pagedimno = pdimno
343 ; pagew = w
344 ; pageh = h
345 ; pagedispy = dy
346 ; pagey = py'
347 ; pagevh = vh
350 let accu = e :: accu in
351 f pageno' pdimno curr
352 (vy + vh) (py + h) (dy + vh + 2) rest
353 (pred cacheleft) accu
354 else
355 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
357 if state.invalidated = 0
358 then
359 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
360 state.maxy <- calcheight ();
361 List.rev accu
362 else
366 let clamp incr =
367 let y = state.y + incr in
368 let y = max 0 y in
369 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
373 let getopaque pageno =
374 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
375 state.rotate))
376 with Not_found -> None
379 let cache pageno opaque =
380 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
381 state.rotate) opaque
384 let validopaque opaque = String.length opaque > 0;;
386 let render l =
387 match getopaque l.pageno with
388 | None when not state.rendering ->
389 state.rendering <- true;
390 cache l.pageno "";
391 wcmd "render" [`i (l.pageno + 1)
392 ;`i l.pagedimno
393 ;`i l.pagew
394 ;`i l.pageh];
396 | _ -> ()
399 let loadlayout layout =
400 let rec f all = function
401 | l :: ls ->
402 begin match getopaque l.pageno with
403 | None -> render l; f false ls
404 | Some opaque -> f (all && validopaque opaque) ls
406 | [] -> all
408 f (layout <> []) layout;
411 let preload () =
412 if conf.preload && List.length state.layout < cblen state.pagecache then begin
413 let y = if state.y < state.h then 0 else state.y - state.h in
414 let pages = layout y (state.h*3) in
415 List.iter render pages;
416 end;
419 let gotoy y =
420 let y = max 0 y in
421 let y = min state.maxy y in
422 let pages = layout y state.h in
423 let ready = loadlayout pages in
424 state.ty <- yratio y;
425 if conf.showall then (
426 if ready then (
427 state.layout <- pages;
428 state.y <- y;
429 Glut.postRedisplay ();
432 else (
433 state.layout <- pages;
434 state.y <- y;
435 Glut.postRedisplay ();
437 preload ();
440 let addnav () =
441 cbput state.hists.nav (yratio state.y);
442 cbrfollowlen state.hists.nav;
445 let getnav () =
446 let y = cbget state.hists.nav ~-1 in
447 truncate (y *. float state.maxy)
450 let gotopage n top =
451 let y, h = getpageyh n in
452 addnav ();
453 gotoy (y + (truncate (top *. float h)));
456 let gotopage1 n top =
457 let y = getpagey n in
458 addnav ();
459 gotoy (y + top);
462 let invalidate () =
463 state.layout <- [];
464 state.pages <- [];
465 state.rects <- [];
466 state.rects1 <- [];
467 state.invalidated <- state.invalidated + 1;
470 let scalecolor c =
471 let c = c *. state.colorscale in
472 (c, c, c);
475 let reshape ~w ~h =
476 let ratio = float w /. float state.w in
477 state.w <- w;
478 state.h <- h;
479 GlDraw.viewport 0 0 w h;
480 GlMat.mode `modelview;
481 GlMat.load_identity ();
482 GlMat.mode `projection;
483 GlMat.load_identity ();
484 GlMat.rotate ~x:1.0 ~angle:180.0 ();
485 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
486 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
487 GlClear.color (scalecolor 1.0);
488 GlClear.clear [`color];
490 invalidate ();
491 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
494 let showtext c s =
495 GlDraw.color (0.0, 0.0, 0.0);
496 GlDraw.rect
497 (0.0, float (state.h - 18))
498 (float (state.w - conf.scrollw - 1), float state.h)
500 let font = Glut.BITMAP_8_BY_13 in
501 GlDraw.color (1.0, 1.0, 1.0);
502 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
503 Glut.bitmapCharacter ~font ~c:(Char.code c);
504 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
507 let enttext () =
508 let len = String.length state.text in
509 match state.textentry with
510 | None ->
511 if len > 0 then showtext ' ' state.text
513 | Some (c, text, _, _, _) ->
514 let s =
515 if len > 0
516 then
517 text ^ " [" ^ state.text ^ "]"
518 else
519 text
521 showtext c s;
524 let showtext c s =
525 if true
526 then (
527 state.text <- Printf.sprintf "%c%s" c s;
528 Glut.postRedisplay ();
530 else (
531 showtext c s;
532 Glut.swapBuffers ();
537 let act cmd =
538 match cmd.[0] with
539 | 'c' ->
540 state.pages <- [];
542 | 'D' ->
543 state.rects <- state.rects1;
544 Glut.postRedisplay ()
546 | 'C' ->
547 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
548 state.pagecount <- n;
549 state.invalidated <- state.invalidated - 1;
550 if state.invalidated = 0
551 then (
552 let rely = yratio state.y in
553 state.maxy <- calcheight ();
554 gotoy (truncate (float state.maxy *. rely));
557 | 't' ->
558 let s = Scanf.sscanf cmd "t %n"
559 (fun n -> String.sub cmd n (String.length cmd - n))
561 Glut.setWindowTitle s
563 | 'T' ->
564 let s = Scanf.sscanf cmd "T %n"
565 (fun n -> String.sub cmd n (String.length cmd - n))
567 if state.textentry = None
568 then (
569 state.text <- s;
570 showtext ' ' s;
572 else (
573 state.text <- s;
574 Glut.postRedisplay ();
577 | 'V' ->
578 if conf.verbose
579 then
580 let s = Scanf.sscanf cmd "V %n"
581 (fun n -> String.sub cmd n (String.length cmd - n))
583 state.text <- s;
584 showtext ' ' s;
586 | 'F' ->
587 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
588 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
589 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
590 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
592 let y = (getpagey pageno) + truncate y0 in
593 addnav ();
594 gotoy y;
595 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
597 | 'R' ->
598 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
599 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
600 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
601 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
603 state.rects1 <-
604 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
606 | 'r' ->
607 let n, w, h, r, p =
608 Scanf.sscanf cmd "r %d %d %d %d %s"
609 (fun n w h r p -> (n, w, h, r, p))
611 Hashtbl.replace state.pagemap (n, w, r) p;
612 let opaque = cbpeekw state.pagecache in
613 if validopaque opaque
614 then (
615 let k =
616 Hashtbl.fold
617 (fun k v a -> if v = opaque then k else a)
618 state.pagemap (-1, -1, -1)
620 wcmd "free" [`s opaque];
621 Hashtbl.remove state.pagemap k
623 cbput state.pagecache p;
624 state.rendering <- false;
625 if conf.showall
626 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
627 else (
628 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
629 if visible then gotoy state.y
630 else (ignore (loadlayout state.layout); preload ())
633 | 'l' ->
634 let (n, w, h) as pagelayout =
635 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
637 state.pages <- pagelayout :: state.pages
639 | 'o' ->
640 let (l, n, t, h, pos) =
641 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
643 let s = String.sub cmd pos (String.length cmd - pos) in
644 let outline = (s, l, n, float t /. float h) in
645 let outlines =
646 match state.outlines with
647 | Olist outlines -> Olist (outline :: outlines)
648 | Oarray _ -> Olist [outline]
649 | Onarrow _ -> Olist [outline]
651 state.outlines <- outlines
653 | _ ->
654 log "unknown cmd `%S'" cmd
657 let now = Unix.gettimeofday;;
659 let idle () =
660 let rec loop delay =
661 let r, _, _ = Unix.select [state.csock] [] [] delay in
662 begin match r with
663 | [] ->
664 if conf.autoscroll then begin
665 let y = state.y + conf.scrollincr in
666 let y = if y >= state.maxy then 0 else y in
667 gotoy y;
668 state.text <- "";
669 end;
671 | _ ->
672 let cmd = readcmd state.csock in
673 act cmd;
674 loop 0.0
675 end;
676 in loop 0.001
679 let onhist cb = function
680 | HCprev -> cbget cb ~-1
681 | HCnext -> cbget cb 1
682 | HCfirst -> cbget cb ~-(cb.rc)
683 | HClast -> cbget cb (cb.len - 1 - cb.rc)
686 let search pattern forward =
687 if String.length pattern > 0
688 then
689 let pn, py =
690 match state.layout with
691 | [] -> 0, 0
692 | l :: _ ->
693 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
695 let cmd =
696 let b = makecmd "search"
697 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
699 Buffer.add_char b ',';
700 Buffer.add_string b pattern;
701 Buffer.add_char b '\000';
702 Buffer.contents b;
704 writecmd state.csock cmd;
707 let intentry text key =
708 let c = Char.unsafe_chr key in
709 match c with
710 | '0' .. '9' ->
711 let s = "x" in s.[0] <- c;
712 let text = text ^ s in
713 TEcont text
715 | _ ->
716 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
717 TEcont text
720 let addchar s c =
721 let b = Buffer.create (String.length s + 1) in
722 Buffer.add_string b s;
723 Buffer.add_char b c;
724 Buffer.contents b;
727 let textentry text key =
728 let c = Char.unsafe_chr key in
729 match c with
730 | _ when key >= 32 && key < 127 ->
731 let text = addchar text c in
732 TEcont text
734 | _ ->
735 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
736 TEcont text
739 let rotate angle =
740 state.rotate <- angle;
741 invalidate ();
742 wcmd "rotate" [`i angle];
745 let optentry text key =
746 let btos b = if b then "on" else "off" in
747 let c = Char.unsafe_chr key in
748 match c with
749 | 's' ->
750 let ondone s =
751 try conf.scrollincr <- int_of_string s with exc ->
752 state.text <- Printf.sprintf "bad integer `%s': %s"
753 s (Printexc.to_string exc)
755 TEswitch ('#', "", None, intentry, ondone)
757 | 'R' ->
758 let ondone s =
759 match try
760 Some (int_of_string s)
761 with exc ->
762 state.text <- Printf.sprintf "bad integer `%s': %s"
763 s (Printexc.to_string exc);
764 None
765 with
766 | Some angle -> rotate angle
767 | None -> ()
769 TEswitch ('^', "", None, intentry, ondone)
771 | 'i' ->
772 conf.icase <- not conf.icase;
773 TEdone ("case insensitive search " ^ (btos conf.icase))
775 | 'p' ->
776 conf.preload <- not conf.preload;
777 gotoy state.y;
778 TEdone ("preload " ^ (btos conf.preload))
780 | 'v' ->
781 conf.verbose <- not conf.verbose;
782 TEdone ("verbose " ^ (btos conf.verbose))
784 | 'h' ->
785 conf.maxhfit <- not conf.maxhfit;
786 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
787 TEdone ("maxhfit " ^ (btos conf.maxhfit))
789 | 'c' ->
790 conf.crophack <- not conf.crophack;
791 TEdone ("crophack " ^ btos conf.crophack)
793 | 'a' ->
794 conf.showall <- not conf.showall;
795 TEdone ("showall " ^ btos conf.showall)
797 | 'f' ->
798 conf.underinfo <- not conf.underinfo;
799 TEdone ("underinfo " ^ btos conf.underinfo)
801 | _ ->
802 state.text <- Printf.sprintf "bad option %d `%c'" key c;
803 TEstop
806 let maxoutlinerows () = (state.h - 31) / 16;;
808 let enterselector allowdel outlines errmsg =
809 if Array.length outlines = 0
810 then (
811 showtext ' ' errmsg;
813 else (
814 Glut.setCursor Glut.CURSOR_INHERIT;
815 let pageno =
816 match state.layout with
817 | [] -> -1
818 | {pageno=pageno} :: rest -> pageno
820 let active =
821 let rec loop n =
822 if n = Array.length outlines
823 then 0
824 else
825 let (_, _, outlinepageno, _) = outlines.(n) in
826 if outlinepageno >= pageno then n else loop (n+1)
828 loop 0
830 state.outline <-
831 Some (allowdel, active,
832 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
833 Glut.postRedisplay ();
837 let enteroutlinemode () =
838 let outlines =
839 match state.outlines with
840 | Oarray a -> a
841 | Olist l ->
842 let a = Array.of_list (List.rev l) in
843 state.outlines <- Oarray a;
845 | Onarrow (a, b) -> a
847 enterselector false outlines "Document has no outline";
850 let enterbookmarkmode () =
851 let bookmarks = Array.of_list state.bookmarks in
852 enterselector true bookmarks "Document has no bookmarks (yet)";
856 let quickbookmark ?title () =
857 match state.layout with
858 | [] -> ()
859 | l :: _ ->
860 let title =
861 match title with
862 | None ->
863 let sec = Unix.gettimeofday () in
864 let tm = Unix.localtime sec in
865 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
866 l.pageno
867 tm.Unix.tm_mday
868 tm.Unix.tm_mon
869 (tm.Unix.tm_year + 1900)
870 tm.Unix.tm_hour
871 tm.Unix.tm_min
872 | Some title -> title
874 state.bookmarks <-
875 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
878 let doreshape w h =
879 state.fullscreen <- None;
880 Glut.reshapeWindow w h;
883 let viewkeyboard ~key ~x ~y =
884 let enttext te =
885 state.textentry <- te;
886 state.text <- "";
887 enttext ();
888 Glut.postRedisplay ()
890 match state.textentry with
891 | None ->
892 let c = Char.chr key in
893 begin match c with
894 | '\027' | 'q' ->
895 exit 0
897 | '\008' ->
898 let y = getnav () in
899 gotoy y
901 | 'o' ->
902 enteroutlinemode ()
904 | 'u' ->
905 state.rects <- [];
906 state.text <- "";
907 Glut.postRedisplay ()
909 | '/' | '?' ->
910 let ondone isforw s =
911 cbput state.hists.pat s;
912 cbrfollowlen state.hists.pat;
913 state.searchpattern <- s;
914 search s isforw
916 enttext (Some (c, "", Some (onhist state.hists.pat),
917 textentry, ondone (c ='/')))
919 | '+' ->
920 let ondone s =
921 let n =
922 try int_of_string s with exc ->
923 state.text <- Printf.sprintf "bad integer `%s': %s"
924 s (Printexc.to_string exc);
925 max_int
927 if n != max_int
928 then (
929 conf.pagebias <- n;
930 state.text <- "page bias is now " ^ string_of_int n;
933 enttext (Some ('+', "", None, intentry, ondone))
935 | '-' ->
936 let ondone msg =
937 state.text <- msg;
939 enttext (Some ('-', "", None, optentry, ondone))
941 | '0' .. '9' ->
942 let ondone s =
943 let n =
944 try int_of_string s with exc ->
945 state.text <- Printf.sprintf "bad integer `%s': %s"
946 s (Printexc.to_string exc);
949 if n >= 0
950 then (
951 addnav ();
952 cbput state.hists.pag (string_of_int n);
953 cbrfollowlen state.hists.pag;
954 gotoy (getpagey (n + conf.pagebias - 1))
957 let pageentry text key =
958 match Char.unsafe_chr key with
959 | 'g' -> TEdone text
960 | _ -> intentry text key
962 let text = "x" in text.[0] <- c;
963 enttext (Some (':', text, Some (onhist state.hists.pag),
964 pageentry, ondone))
966 | 'b' ->
967 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
968 reshape state.w state.h;
970 | 'l' ->
971 conf.hlinks <- not conf.hlinks;
972 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
973 Glut.postRedisplay ()
975 | 'a' ->
976 conf.autoscroll <- not conf.autoscroll
978 | 'f' ->
979 begin match state.fullscreen with
980 | None ->
981 state.fullscreen <- Some (state.w, state.h);
982 Glut.fullScreen ()
983 | Some (w, h) ->
984 state.fullscreen <- None;
985 doreshape w h
988 | 'g' ->
989 gotoy 0
991 | 'n' ->
992 search state.searchpattern true
994 | 'p' | 'N' ->
995 search state.searchpattern false
997 | 't' ->
998 begin match state.layout with
999 | [] -> ()
1000 | l :: _ ->
1001 gotoy (state.y - l.pagey);
1004 | ' ' ->
1005 begin match List.rev state.layout with
1006 | [] -> ()
1007 | l :: _ ->
1008 gotoy (clamp (l.pageh - l.pagey))
1011 | '\127' ->
1012 begin match state.layout with
1013 | [] -> ()
1014 | l :: _ ->
1015 gotoy (clamp (-l.pageh));
1018 | '=' ->
1019 let f (fn, ln) l =
1020 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1022 let fn, ln = List.fold_left f (-1, -1) state.layout in
1023 let s =
1024 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1025 let percent =
1026 if maxy <= 0
1027 then 100.
1028 else (100. *. (float state.y /. float maxy)) in
1029 if fn = ln
1030 then
1031 Printf.sprintf "Page %d of %d %.2f%%"
1032 (fn+1) state.pagecount percent
1033 else
1034 Printf.sprintf
1035 "Pages %d-%d of %d %.2f%%"
1036 (fn+1) (ln+1) state.pagecount percent
1038 showtext ' ' s;
1040 | 'w' ->
1041 begin match state.layout with
1042 | [] -> ()
1043 | l :: _ ->
1044 doreshape (l.pagew + conf.scrollw) l.pageh;
1045 Glut.postRedisplay ();
1048 | '\'' ->
1049 enterbookmarkmode ()
1051 | 'm' ->
1052 let ondone s =
1053 match state.layout with
1054 | l :: _ ->
1055 state.bookmarks <-
1056 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1057 :: state.bookmarks
1058 | _ -> ()
1060 enttext (Some ('~', "", None, textentry, ondone))
1062 | '~' ->
1063 quickbookmark ();
1064 showtext ' ' "Quick bookmark added";
1066 | 'z' ->
1067 begin match state.layout with
1068 | l :: _ ->
1069 let a = getpagewh l.pagedimno in
1070 let w, h =
1071 if conf.crophack
1072 then
1073 (truncate (1.8 *. (a.(1) -. a.(0))),
1074 truncate (1.2 *. (a.(3) -. a.(0))))
1075 else
1076 (truncate (a.(1) -. a.(0)),
1077 truncate (a.(3) -. a.(0)))
1079 doreshape (w + conf.scrollw) h;
1080 Glut.postRedisplay ();
1082 | [] -> ()
1085 | '<' | '>' ->
1086 rotate (state.rotate + (if c = '>' then 30 else -30));
1088 | '[' | ']' ->
1089 state.colorscale <-
1090 max 0.0
1091 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1092 Glut.postRedisplay ()
1094 | 'k' -> gotoy (clamp (-conf.scrollincr))
1095 | 'j' -> gotoy (clamp conf.scrollincr)
1097 | _ ->
1098 vlog "huh? %d %c" key (Char.chr key);
1101 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1102 let len = String.length text in
1103 if len = 0
1104 then (
1105 state.textentry <- None;
1106 Glut.postRedisplay ();
1108 else (
1109 let s = String.sub text 0 (len - 1) in
1110 enttext (Some (c, s, onhist, onkey, ondone))
1113 | Some (c, text, onhist, onkey, ondone) ->
1114 begin match Char.unsafe_chr key with
1115 | '\r' | '\n' ->
1116 ondone text;
1117 state.textentry <- None;
1118 Glut.postRedisplay ()
1120 | '\027' ->
1121 state.textentry <- None;
1122 Glut.postRedisplay ()
1124 | _ ->
1125 begin match onkey text key with
1126 | TEdone text ->
1127 state.textentry <- None;
1128 ondone text;
1129 Glut.postRedisplay ()
1131 | TEcont text ->
1132 enttext (Some (c, text, onhist, onkey, ondone));
1134 | TEstop ->
1135 state.textentry <- None;
1136 Glut.postRedisplay ()
1138 | TEswitch te ->
1139 state.textentry <- Some te;
1140 Glut.postRedisplay ()
1141 end;
1142 end;
1145 let narrow outlines pattern =
1146 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1147 match reopt with
1148 | None -> None
1149 | Some re ->
1150 let rec fold accu n =
1151 if n = -1 then accu else
1152 let (s, _, _, _) as o = outlines.(n) in
1153 let accu =
1154 if (try ignore (Str.search_forward re s 0); true
1155 with Not_found -> false)
1156 then (o :: accu)
1157 else accu
1159 fold accu (n-1)
1161 let matched = fold [] (Array.length outlines - 1) in
1162 if matched = [] then None else Some (Array.of_list matched)
1165 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1166 let search active pattern incr =
1167 let dosearch re =
1168 let rec loop n =
1169 if n = Array.length outlines || n = -1 then None else
1170 let (s, _, _, _) = outlines.(n) in
1172 (try ignore (Str.search_forward re s 0); true
1173 with Not_found -> false)
1174 then Some n
1175 else loop (n + incr)
1177 loop active
1180 let re = Str.regexp_case_fold pattern in
1181 dosearch re
1182 with Failure s ->
1183 state.text <- s;
1184 None
1186 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1187 match key with
1188 | 27 ->
1189 if String.length qsearch = 0
1190 then (
1191 state.text <- "";
1192 state.outline <- None;
1193 Glut.postRedisplay ();
1195 else (
1196 state.text <- "";
1197 state.outline <- Some (allowdel, active, first, outlines, "");
1198 Glut.postRedisplay ();
1201 | 18 | 19 ->
1202 let incr = if key = 18 then -1 else 1 in
1203 let active, first =
1204 match search (active + incr) qsearch incr with
1205 | None ->
1206 state.text <- qsearch ^ " [not found]";
1207 active, first
1208 | Some active ->
1209 state.text <- qsearch;
1210 active, firstof active
1212 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1213 Glut.postRedisplay ();
1215 | 8 ->
1216 let len = String.length qsearch in
1217 if len = 0
1218 then ()
1219 else (
1220 if len = 1
1221 then (
1222 state.text <- "";
1223 state.outline <- Some (allowdel, active, first, outlines, "");
1225 else
1226 let qsearch = String.sub qsearch 0 (len - 1) in
1227 let active, first =
1228 match search active qsearch ~-1 with
1229 | None ->
1230 state.text <- qsearch ^ " [not found]";
1231 active, first
1232 | Some active ->
1233 state.text <- qsearch;
1234 active, firstof active
1236 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1238 Glut.postRedisplay ()
1240 | 13 ->
1241 if active < Array.length outlines
1242 then (
1243 let (_, _, n, t) = outlines.(active) in
1244 gotopage n t;
1246 state.text <- "";
1247 if allowdel then state.bookmarks <- Array.to_list outlines;
1248 state.outline <- None;
1249 Glut.postRedisplay ();
1251 | _ when key >= 32 && key < 127 ->
1252 let pattern = addchar qsearch (Char.chr key) in
1253 let active, first =
1254 match search active pattern 1 with
1255 | None ->
1256 state.text <- pattern ^ " [not found]";
1257 active, first
1258 | Some active ->
1259 state.text <- pattern;
1260 active, firstof active
1262 state.outline <- Some (allowdel, active, first, outlines, pattern);
1263 Glut.postRedisplay ()
1265 | 14 when not allowdel ->
1266 let optoutlines = narrow outlines qsearch in
1267 begin match optoutlines with
1268 | None -> state.text <- "can't narrow"
1269 | Some outlines ->
1270 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1271 match state.outlines with
1272 | Olist l -> ()
1273 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1274 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1275 end;
1276 Glut.postRedisplay ()
1278 | 21 when not allowdel ->
1279 let outline =
1280 match state.outlines with
1281 | Oarray a -> a
1282 | Olist l ->
1283 let a = Array.of_list (List.rev l) in
1284 state.outlines <- Oarray a;
1286 | Onarrow (a, b) ->
1287 state.outlines <- Oarray b;
1290 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1291 Glut.postRedisplay ()
1293 | 12 ->
1294 state.outline <-
1295 Some (allowdel, active, firstof active, outlines, qsearch);
1296 Glut.postRedisplay ()
1298 | 127 when allowdel ->
1299 let len = Array.length outlines - 1 in
1300 if len = 0
1301 then (
1302 state.outline <- None;
1303 state.bookmarks <- [];
1305 else (
1306 let bookmarks = Array.init len
1307 (fun i ->
1308 let i = if i >= active then i + 1 else i in
1309 outlines.(i)
1312 state.outline <-
1313 Some (allowdel,
1314 min active (len-1),
1315 min first (len-1),
1316 bookmarks, qsearch)
1319 Glut.postRedisplay ()
1321 | _ -> log "unknown key %d" key
1324 let keyboard ~key ~x ~y =
1325 if key = 7
1326 then
1327 wcmd "interrupt" []
1328 else
1329 match state.outline with
1330 | None -> viewkeyboard ~key ~x ~y
1331 | Some outline -> outlinekeyboard ~key ~x ~y outline
1334 let special ~key ~x ~y =
1335 match state.outline with
1336 | None ->
1337 begin match state.textentry with
1338 | None ->
1339 let y =
1340 match key with
1341 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1342 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1343 | Glut.KEY_DOWN -> clamp conf.scrollincr
1344 | Glut.KEY_PAGE_UP ->
1345 if Glut.getModifiers () land Glut.active_ctrl != 0
1346 then
1347 match state.layout with
1348 | [] -> state.y
1349 | l :: _ -> state.y - l.pagey
1350 else
1351 clamp (-state.h)
1352 | Glut.KEY_PAGE_DOWN ->
1353 if Glut.getModifiers () land Glut.active_ctrl != 0
1354 then
1355 match List.rev state.layout with
1356 | [] -> state.y
1357 | l :: _ -> getpagey l.pageno
1358 else
1359 clamp state.h
1360 | Glut.KEY_HOME -> addnav (); 0
1361 | Glut.KEY_END ->
1362 addnav ();
1363 state.maxy - (if conf.maxhfit then state.h else 0)
1364 | _ -> state.y
1366 state.text <- "";
1367 gotoy y
1369 | Some (c, s, Some onhist, onkey, ondone) ->
1370 let s =
1371 match key with
1372 | Glut.KEY_UP -> onhist HCprev
1373 | Glut.KEY_DOWN -> onhist HCnext
1374 | Glut.KEY_HOME -> onhist HCfirst
1375 | Glut.KEY_END -> onhist HClast
1376 | _ -> state.text
1378 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1379 Glut.postRedisplay ()
1381 | _ -> ()
1384 | Some (allowdel, active, first, outlines, qsearch) ->
1385 let maxrows = maxoutlinerows () in
1386 let navigate incr =
1387 let active = active + incr in
1388 let active = max 0 (min active (Array.length outlines - 1)) in
1389 let first =
1390 if active > first
1391 then
1392 let rows = active - first in
1393 if rows > maxrows then active - maxrows else first
1394 else active
1396 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1397 Glut.postRedisplay ()
1399 match key with
1400 | Glut.KEY_UP -> navigate ~-1
1401 | Glut.KEY_DOWN -> navigate 1
1402 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1403 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1405 | Glut.KEY_HOME ->
1406 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1407 Glut.postRedisplay ()
1409 | Glut.KEY_END ->
1410 let active = Array.length outlines - 1 in
1411 let first = max 0 (active - maxrows) in
1412 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1413 Glut.postRedisplay ()
1415 | _ -> ()
1418 let drawplaceholder l =
1419 GlDraw.color (scalecolor 1.0);
1420 GlDraw.rect
1421 (0.0, float l.pagedispy)
1422 (float l.pagew, float (l.pagedispy + l.pagevh))
1424 let x = 0.0
1425 and y = float (l.pagedispy + 13) in
1426 let font = Glut.BITMAP_8_BY_13 in
1427 GlDraw.color (0.0, 0.0, 0.0);
1428 GlPix.raster_pos ~x ~y ();
1429 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1430 ("Loading " ^ string_of_int l.pageno);
1433 let now () = Unix.gettimeofday ();;
1435 let drawpage i l =
1436 begin match getopaque l.pageno with
1437 | Some opaque when validopaque opaque ->
1438 if state.textentry = None
1439 then GlDraw.color (scalecolor 1.0)
1440 else GlDraw.color (scalecolor 0.4);
1441 let a = now () in
1442 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1443 let b = now () in
1444 let d = b-.a in
1445 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1446 vlog "draw %f sec" d;
1448 | _ ->
1449 drawplaceholder l;
1450 end;
1451 GlDraw.color (0.5, 0.5, 0.5);
1452 GlDraw.rect
1453 (0., float i)
1454 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1456 l.pagedispy + l.pagevh;
1459 let scrollindicator () =
1460 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1461 GlDraw.color (0.64 , 0.64, 0.64);
1462 GlDraw.rect
1463 (float (state.w - conf.scrollw), 0.)
1464 (float state.w, float state.h)
1466 GlDraw.color (0.0, 0.0, 0.0);
1467 let sh = (float (maxy + state.h) /. float state.h) in
1468 let sh = float state.h /. sh in
1469 let sh = max sh (float conf.scrollh) in
1471 let percent =
1472 if state.y = state.maxy
1473 then 1.0
1474 else float state.y /. float maxy
1476 let position = (float state.h -. sh) *. percent in
1478 let position =
1479 if position +. sh > float state.h
1480 then
1481 float state.h -. sh
1482 else
1483 position
1485 GlDraw.rect
1486 (float (state.w - conf.scrollw), position)
1487 (float state.w, position +. sh)
1491 let showsel () =
1492 match state.mstate with
1493 | Mnone ->
1496 | Msel ((x0, y0), (x1, y1)) ->
1497 let rec loop = function
1498 | l :: ls ->
1499 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1500 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1501 then
1502 match getopaque l.pageno with
1503 | Some opaque when validopaque opaque ->
1504 let oy = -l.pagey + l.pagedispy in
1505 seltext opaque (x0, y0, x1, y1) oy;
1507 | _ -> ()
1508 else loop ls
1509 | [] -> ()
1511 loop state.layout
1514 let showrects () =
1515 Gl.enable `blend;
1516 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1517 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1518 List.iter
1519 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1520 List.iter (fun l ->
1521 if l.pageno = pageno
1522 then (
1523 let d = float (l.pagedispy - l.pagey) in
1524 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1525 GlDraw.begins `quads;
1527 GlDraw.vertex2 (x0, y0+.d);
1528 GlDraw.vertex2 (x1, y1+.d);
1529 GlDraw.vertex2 (x2, y2+.d);
1530 GlDraw.vertex2 (x3, y3+.d);
1532 GlDraw.ends ();
1534 ) state.layout
1535 ) state.rects
1537 Gl.disable `blend;
1540 let showoutline = function
1541 | None -> ()
1542 | Some (allowdel, active, first, outlines, qsearch) ->
1543 Gl.enable `blend;
1544 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1545 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1546 GlDraw.rect (0., 0.) (float state.w, float state.h);
1547 Gl.disable `blend;
1549 GlDraw.color (1., 1., 1.);
1550 let font = Glut.BITMAP_9_BY_15 in
1551 let draw_string x y s =
1552 GlPix.raster_pos ~x ~y ();
1553 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1555 let rec loop row =
1556 if row = Array.length outlines || (row - first) * 16 > state.h
1557 then ()
1558 else (
1559 let (s, l, _, _) = outlines.(row) in
1560 let y = (row - first) * 16 in
1561 let x = 5 + 15*l in
1562 if row = active
1563 then (
1564 Gl.enable `blend;
1565 GlDraw.polygon_mode `both `line;
1566 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1567 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1568 GlDraw.rect (0., float (y + 1))
1569 (float (state.w - conf.scrollw - 1), float (y + 18));
1570 GlDraw.polygon_mode `both `fill;
1571 Gl.disable `blend;
1572 GlDraw.color (1., 1., 1.);
1574 draw_string (float x) (float (y + 16)) s;
1575 loop (row+1)
1578 loop first
1581 let display () =
1582 let lasty = List.fold_left drawpage 0 (state.layout) in
1583 GlDraw.color (scalecolor 0.5);
1584 GlDraw.rect
1585 (0., float lasty)
1586 (float (state.w - conf.scrollw), float state.h)
1588 showrects ();
1589 scrollindicator ();
1590 showsel ();
1591 showoutline state.outline;
1592 enttext ();
1593 Glut.swapBuffers ();
1596 let getunder x y =
1597 let rec f = function
1598 | l :: rest ->
1599 begin match getopaque l.pageno with
1600 | Some opaque when validopaque opaque ->
1601 let y = y - l.pagedispy in
1602 if y > 0
1603 then
1604 let y = l.pagey + y in
1605 match whatsunder opaque x y with
1606 | Unone -> f rest
1607 | under -> under
1608 else
1609 f rest
1610 | _ ->
1611 f rest
1613 | [] -> Unone
1615 f state.layout
1618 let mouse ~button ~bstate ~x ~y =
1619 match button with
1620 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1621 let incr =
1622 if n = 3
1623 then
1624 -conf.scrollincr
1625 else
1626 conf.scrollincr
1628 let incr = incr * 2 in
1629 let y = clamp incr in
1630 gotoy y
1632 | Glut.LEFT_BUTTON when state.outline = None ->
1633 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1634 begin match dest with
1635 | Ulinkgoto (pageno, top) ->
1636 if pageno >= 0
1637 then
1638 gotopage1 pageno top
1640 | Ulinkuri s ->
1641 print_endline s
1643 | Unone when bstate = Glut.DOWN ->
1644 Glut.setCursor Glut.CURSOR_INHERIT;
1645 state.mstate <- Mnone
1647 | Unone | Utext _ ->
1648 if bstate = Glut.DOWN
1649 then (
1650 if state.rotate mod 360 = 0 then (
1651 state.mstate <- Msel ((x, y), (x, y));
1652 Glut.postRedisplay ()
1655 else (
1656 match state.mstate with
1657 | Mnone -> ()
1658 | Msel ((x0, y0), (x1, y1)) ->
1659 let f l =
1660 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1661 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1662 then
1663 match getopaque l.pageno with
1664 | Some opaque when validopaque opaque ->
1665 copysel opaque
1666 | _ -> ()
1668 List.iter f state.layout;
1669 copysel ""; (* ugly *)
1670 Glut.setCursor Glut.CURSOR_INHERIT;
1671 state.mstate <- Mnone;
1675 | _ ->
1678 let mouse ~button ~state ~x ~y = mouse button state x y;;
1680 let motion ~x ~y =
1681 if state.outline = None
1682 then
1683 match state.mstate with
1684 | Mnone -> ()
1685 | Msel (a, _) ->
1686 state.mstate <- Msel (a, (x, y));
1687 Glut.postRedisplay ()
1690 let pmotion ~x ~y =
1691 if state.outline = None
1692 then
1693 match state.mstate with
1694 | Mnone ->
1695 begin match getunder x y with
1696 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1697 | Ulinkuri uri ->
1698 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1699 Glut.setCursor Glut.CURSOR_INFO
1700 | Ulinkgoto (page, y) ->
1701 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1702 Glut.setCursor Glut.CURSOR_INFO
1703 | Utext s ->
1704 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1705 Glut.setCursor Glut.CURSOR_TEXT
1708 | Msel (a, _) ->
1712 let () =
1713 let statepath =
1714 let home =
1715 if Sys.os_type = "Win32"
1716 then
1717 try Sys.getenv "HOMEPATH" with Not_found -> ""
1718 else
1719 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1721 Filename.concat home "llpp"
1723 let pstate =
1725 let ic = open_in_bin statepath in
1726 let hash = input_value ic in
1727 close_in ic;
1728 hash
1729 with exn ->
1730 if false
1731 then
1732 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1734 Hashtbl.create 1
1736 let savestate () =
1738 let w, h =
1739 match state.fullscreen with
1740 | None -> state.w, state.h
1741 | Some wh -> wh
1743 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1744 let oc = open_out_bin statepath in
1745 output_value oc pstate
1746 with exn ->
1747 if false
1748 then
1749 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1752 let setstate () =
1754 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1755 state.w <- statew;
1756 state.h <- stateh;
1757 state.bookmarks <- statebookmarks;
1758 with Not_found -> ()
1759 | exn ->
1760 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1763 Arg.parse [] (fun s -> state.path <- s) "options:";
1764 let name =
1765 if String.length state.path = 0
1766 then (prerr_endline "filename missing"; exit 1)
1767 else state.path
1770 setstate ();
1771 let _ = Glut.init Sys.argv in
1772 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1773 let () = Glut.initWindowSize state.w state.h in
1774 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1776 let csock, ssock =
1777 if Sys.os_type = "Unix"
1778 then
1779 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1780 else
1781 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1782 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1783 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1784 Unix.bind sock addr;
1785 Unix.listen sock 1;
1786 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1787 Unix.connect csock addr;
1788 let ssock, _ = Unix.accept sock in
1789 Unix.close sock;
1790 let opts sock =
1791 Unix.setsockopt sock Unix.TCP_NODELAY true;
1792 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1794 opts ssock;
1795 opts csock;
1796 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1797 ssock, csock
1800 let () = Glut.displayFunc display in
1801 let () = Glut.reshapeFunc reshape in
1802 let () = Glut.keyboardFunc keyboard in
1803 let () = Glut.specialFunc special in
1804 let () = Glut.idleFunc (Some idle) in
1805 let () = Glut.mouseFunc mouse in
1806 let () = Glut.motionFunc motion in
1807 let () = Glut.passiveMotionFunc pmotion in
1809 init ssock;
1810 state.csock <- csock;
1811 state.ssock <- ssock;
1812 state.text <- "Opening " ^ name;
1813 writecmd csock ("open " ^ name ^ "\000");
1815 at_exit savestate;
1817 let rec handlelablglutbug () =
1819 Glut.mainLoop ();
1820 with Glut.BadEnum "key in special_of_int" ->
1821 showtext '!' " LablGlut bug: special key not recognized";
1822 handlelablglutbug ()
1824 handlelablglutbug ();