Altivec code to clear pixmaps
[llpp.git] / main.ml
blob5eaa1174d6c812ef6c567f5edd40f54596cee5d8
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 state.w <- w;
477 state.h <- h;
478 GlDraw.viewport 0 0 w h;
479 GlMat.mode `modelview;
480 GlMat.load_identity ();
481 GlMat.mode `projection;
482 GlMat.load_identity ();
483 GlMat.rotate ~x:1.0 ~angle:180.0 ();
484 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
485 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
486 GlClear.color (scalecolor 1.0);
487 GlClear.clear [`color];
489 invalidate ();
490 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
493 let showtext c s =
494 GlDraw.color (0.0, 0.0, 0.0);
495 GlDraw.rect
496 (0.0, float (state.h - 18))
497 (float (state.w - conf.scrollw - 1), float state.h)
499 let font = Glut.BITMAP_8_BY_13 in
500 GlDraw.color (1.0, 1.0, 1.0);
501 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
502 Glut.bitmapCharacter ~font ~c:(Char.code c);
503 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
506 let enttext () =
507 let len = String.length state.text in
508 match state.textentry with
509 | None ->
510 if len > 0 then showtext ' ' state.text
512 | Some (c, text, _, _, _) ->
513 let s =
514 if len > 0
515 then
516 text ^ " [" ^ state.text ^ "]"
517 else
518 text
520 showtext c s;
523 let showtext c s =
524 if true
525 then (
526 state.text <- Printf.sprintf "%c%s" c s;
527 Glut.postRedisplay ();
529 else (
530 showtext c s;
531 Glut.swapBuffers ();
536 let act cmd =
537 match cmd.[0] with
538 | 'c' ->
539 state.pages <- [];
541 | 'D' ->
542 state.rects <- state.rects1;
543 Glut.postRedisplay ()
545 | 'C' ->
546 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
547 state.pagecount <- n;
548 state.invalidated <- state.invalidated - 1;
549 if state.invalidated = 0
550 then (
551 let rely = yratio state.y in
552 state.maxy <- calcheight ();
553 gotoy (truncate (float state.maxy *. rely));
556 | 't' ->
557 let s = Scanf.sscanf cmd "t %n"
558 (fun n -> String.sub cmd n (String.length cmd - n))
560 Glut.setWindowTitle s
562 | 'T' ->
563 let s = Scanf.sscanf cmd "T %n"
564 (fun n -> String.sub cmd n (String.length cmd - n))
566 if state.textentry = None
567 then (
568 state.text <- s;
569 showtext ' ' s;
571 else (
572 state.text <- s;
573 Glut.postRedisplay ();
576 | 'V' ->
577 if conf.verbose
578 then
579 let s = Scanf.sscanf cmd "V %n"
580 (fun n -> String.sub cmd n (String.length cmd - n))
582 state.text <- s;
583 showtext ' ' s;
585 | 'F' ->
586 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
587 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
588 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
589 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
591 let y = (getpagey pageno) + truncate y0 in
592 addnav ();
593 gotoy y;
594 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
596 | 'R' ->
597 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
598 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
599 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
600 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
602 state.rects1 <-
603 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
605 | 'r' ->
606 let n, w, h, r, p =
607 Scanf.sscanf cmd "r %d %d %d %d %s"
608 (fun n w h r p -> (n, w, h, r, p))
610 Hashtbl.replace state.pagemap (n, w, r) p;
611 let opaque = cbpeekw state.pagecache in
612 if validopaque opaque
613 then (
614 let k =
615 Hashtbl.fold
616 (fun k v a -> if v = opaque then k else a)
617 state.pagemap (-1, -1, -1)
619 wcmd "free" [`s opaque];
620 Hashtbl.remove state.pagemap k
622 cbput state.pagecache p;
623 state.rendering <- false;
624 if conf.showall
625 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
626 else (
627 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
628 if visible then gotoy state.y
629 else (ignore (loadlayout state.layout); preload ())
632 | 'l' ->
633 let (n, w, h) as pagelayout =
634 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
636 state.pages <- pagelayout :: state.pages
638 | 'o' ->
639 let (l, n, t, h, pos) =
640 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
642 let s = String.sub cmd pos (String.length cmd - pos) in
643 let outline = (s, l, n, float t /. float h) in
644 let outlines =
645 match state.outlines with
646 | Olist outlines -> Olist (outline :: outlines)
647 | Oarray _ -> Olist [outline]
648 | Onarrow _ -> Olist [outline]
650 state.outlines <- outlines
652 | _ ->
653 log "unknown cmd `%S'" cmd
656 let now = Unix.gettimeofday;;
658 let idle () =
659 let rec loop delay =
660 let r, _, _ = Unix.select [state.csock] [] [] delay in
661 begin match r with
662 | [] ->
663 if conf.autoscroll then begin
664 let y = state.y + conf.scrollincr in
665 let y = if y >= state.maxy then 0 else y in
666 gotoy y;
667 state.text <- "";
668 end;
670 | _ ->
671 let cmd = readcmd state.csock in
672 act cmd;
673 loop 0.0
674 end;
675 in loop 0.001
678 let onhist cb = function
679 | HCprev -> cbget cb ~-1
680 | HCnext -> cbget cb 1
681 | HCfirst -> cbget cb ~-(cb.rc)
682 | HClast -> cbget cb (cb.len - 1 - cb.rc)
685 let search pattern forward =
686 if String.length pattern > 0
687 then
688 let pn, py =
689 match state.layout with
690 | [] -> 0, 0
691 | l :: _ ->
692 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
694 let cmd =
695 let b = makecmd "search"
696 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
698 Buffer.add_char b ',';
699 Buffer.add_string b pattern;
700 Buffer.add_char b '\000';
701 Buffer.contents b;
703 writecmd state.csock cmd;
706 let intentry text key =
707 let c = Char.unsafe_chr key in
708 match c with
709 | '0' .. '9' ->
710 let s = "x" in s.[0] <- c;
711 let text = text ^ s in
712 TEcont text
714 | _ ->
715 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
716 TEcont text
719 let addchar s c =
720 let b = Buffer.create (String.length s + 1) in
721 Buffer.add_string b s;
722 Buffer.add_char b c;
723 Buffer.contents b;
726 let textentry text key =
727 let c = Char.unsafe_chr key in
728 match c with
729 | _ when key >= 32 && key < 127 ->
730 let text = addchar text c in
731 TEcont text
733 | _ ->
734 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
735 TEcont text
738 let rotate angle =
739 state.rotate <- angle;
740 invalidate ();
741 wcmd "rotate" [`i angle];
744 let optentry text key =
745 let btos b = if b then "on" else "off" in
746 let c = Char.unsafe_chr key in
747 match c with
748 | 's' ->
749 let ondone s =
750 try conf.scrollincr <- int_of_string s with exc ->
751 state.text <- Printf.sprintf "bad integer `%s': %s"
752 s (Printexc.to_string exc)
754 TEswitch ('#', "", None, intentry, ondone)
756 | 'R' ->
757 let ondone s =
758 match try
759 Some (int_of_string s)
760 with exc ->
761 state.text <- Printf.sprintf "bad integer `%s': %s"
762 s (Printexc.to_string exc);
763 None
764 with
765 | Some angle -> rotate angle
766 | None -> ()
768 TEswitch ('^', "", None, intentry, ondone)
770 | 'i' ->
771 conf.icase <- not conf.icase;
772 TEdone ("case insensitive search " ^ (btos conf.icase))
774 | 'p' ->
775 conf.preload <- not conf.preload;
776 gotoy state.y;
777 TEdone ("preload " ^ (btos conf.preload))
779 | 'v' ->
780 conf.verbose <- not conf.verbose;
781 TEdone ("verbose " ^ (btos conf.verbose))
783 | 'h' ->
784 conf.maxhfit <- not conf.maxhfit;
785 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
786 TEdone ("maxhfit " ^ (btos conf.maxhfit))
788 | 'c' ->
789 conf.crophack <- not conf.crophack;
790 TEdone ("crophack " ^ btos conf.crophack)
792 | 'a' ->
793 conf.showall <- not conf.showall;
794 TEdone ("showall " ^ btos conf.showall)
796 | 'f' ->
797 conf.underinfo <- not conf.underinfo;
798 TEdone ("underinfo " ^ btos conf.underinfo)
800 | _ ->
801 state.text <- Printf.sprintf "bad option %d `%c'" key c;
802 TEstop
805 let maxoutlinerows () = (state.h - 31) / 16;;
807 let enterselector allowdel outlines errmsg =
808 if Array.length outlines = 0
809 then (
810 showtext ' ' errmsg;
812 else (
813 Glut.setCursor Glut.CURSOR_INHERIT;
814 let pageno =
815 match state.layout with
816 | [] -> -1
817 | {pageno=pageno} :: rest -> pageno
819 let active =
820 let rec loop n =
821 if n = Array.length outlines
822 then 0
823 else
824 let (_, _, outlinepageno, _) = outlines.(n) in
825 if outlinepageno >= pageno then n else loop (n+1)
827 loop 0
829 state.outline <-
830 Some (allowdel, active,
831 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
832 Glut.postRedisplay ();
836 let enteroutlinemode () =
837 let outlines =
838 match state.outlines with
839 | Oarray a -> a
840 | Olist l ->
841 let a = Array.of_list (List.rev l) in
842 state.outlines <- Oarray a;
844 | Onarrow (a, b) -> a
846 enterselector false outlines "Document has no outline";
849 let enterbookmarkmode () =
850 let bookmarks = Array.of_list state.bookmarks in
851 enterselector true bookmarks "Document has no bookmarks (yet)";
855 let quickbookmark ?title () =
856 match state.layout with
857 | [] -> ()
858 | l :: _ ->
859 let title =
860 match title with
861 | None ->
862 let sec = Unix.gettimeofday () in
863 let tm = Unix.localtime sec in
864 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
865 l.pageno
866 tm.Unix.tm_mday
867 tm.Unix.tm_mon
868 (tm.Unix.tm_year + 1900)
869 tm.Unix.tm_hour
870 tm.Unix.tm_min
871 | Some title -> title
873 state.bookmarks <-
874 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
877 let doreshape w h =
878 state.fullscreen <- None;
879 Glut.reshapeWindow w h;
882 let viewkeyboard ~key ~x ~y =
883 let enttext te =
884 state.textentry <- te;
885 state.text <- "";
886 enttext ();
887 Glut.postRedisplay ()
889 match state.textentry with
890 | None ->
891 let c = Char.chr key in
892 begin match c with
893 | '\027' | 'q' ->
894 exit 0
896 | '\008' ->
897 let y = getnav () in
898 gotoy y
900 | 'o' ->
901 enteroutlinemode ()
903 | 'u' ->
904 state.rects <- [];
905 state.text <- "";
906 Glut.postRedisplay ()
908 | '/' | '?' ->
909 let ondone isforw s =
910 cbput state.hists.pat s;
911 cbrfollowlen state.hists.pat;
912 state.searchpattern <- s;
913 search s isforw
915 enttext (Some (c, "", Some (onhist state.hists.pat),
916 textentry, ondone (c ='/')))
918 | '+' ->
919 let ondone s =
920 let n =
921 try int_of_string s with exc ->
922 state.text <- Printf.sprintf "bad integer `%s': %s"
923 s (Printexc.to_string exc);
924 max_int
926 if n != max_int
927 then (
928 conf.pagebias <- n;
929 state.text <- "page bias is now " ^ string_of_int n;
932 enttext (Some ('+', "", None, intentry, ondone))
934 | '-' ->
935 let ondone msg =
936 state.text <- msg;
938 enttext (Some ('-', "", None, optentry, ondone))
940 | '0' .. '9' ->
941 let ondone s =
942 let n =
943 try int_of_string s with exc ->
944 state.text <- Printf.sprintf "bad integer `%s': %s"
945 s (Printexc.to_string exc);
948 if n >= 0
949 then (
950 addnav ();
951 cbput state.hists.pag (string_of_int n);
952 cbrfollowlen state.hists.pag;
953 gotoy (getpagey (n + conf.pagebias - 1))
956 let pageentry text key =
957 match Char.unsafe_chr key with
958 | 'g' -> TEdone text
959 | _ -> intentry text key
961 let text = "x" in text.[0] <- c;
962 enttext (Some (':', text, Some (onhist state.hists.pag),
963 pageentry, ondone))
965 | 'b' ->
966 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
967 reshape state.w state.h;
969 | 'l' ->
970 conf.hlinks <- not conf.hlinks;
971 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
972 Glut.postRedisplay ()
974 | 'a' ->
975 conf.autoscroll <- not conf.autoscroll
977 | 'f' ->
978 begin match state.fullscreen with
979 | None ->
980 state.fullscreen <- Some (state.w, state.h);
981 Glut.fullScreen ()
982 | Some (w, h) ->
983 state.fullscreen <- None;
984 doreshape w h
987 | 'g' ->
988 gotoy 0
990 | 'n' ->
991 search state.searchpattern true
993 | 'p' | 'N' ->
994 search state.searchpattern false
996 | 't' ->
997 begin match state.layout with
998 | [] -> ()
999 | l :: _ ->
1000 gotoy (state.y - l.pagey);
1003 | ' ' ->
1004 begin match List.rev state.layout with
1005 | [] -> ()
1006 | l :: _ ->
1007 gotoy (clamp (l.pageh - l.pagey))
1010 | '\127' ->
1011 begin match state.layout with
1012 | [] -> ()
1013 | l :: _ ->
1014 gotoy (clamp (-l.pageh));
1017 | '=' ->
1018 let f (fn, ln) l =
1019 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1021 let fn, ln = List.fold_left f (-1, -1) state.layout in
1022 let s =
1023 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1024 let percent =
1025 if maxy <= 0
1026 then 100.
1027 else (100. *. (float state.y /. float maxy)) in
1028 if fn = ln
1029 then
1030 Printf.sprintf "Page %d of %d %.2f%%"
1031 (fn+1) state.pagecount percent
1032 else
1033 Printf.sprintf
1034 "Pages %d-%d of %d %.2f%%"
1035 (fn+1) (ln+1) state.pagecount percent
1037 showtext ' ' s;
1039 | 'w' ->
1040 begin match state.layout with
1041 | [] -> ()
1042 | l :: _ ->
1043 doreshape (l.pagew + conf.scrollw) l.pageh;
1044 Glut.postRedisplay ();
1047 | '\'' ->
1048 enterbookmarkmode ()
1050 | 'm' ->
1051 let ondone s =
1052 match state.layout with
1053 | l :: _ ->
1054 state.bookmarks <-
1055 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1056 :: state.bookmarks
1057 | _ -> ()
1059 enttext (Some ('~', "", None, textentry, ondone))
1061 | '~' ->
1062 quickbookmark ();
1063 showtext ' ' "Quick bookmark added";
1065 | 'z' ->
1066 begin match state.layout with
1067 | l :: _ ->
1068 let a = getpagewh l.pagedimno in
1069 let w, h =
1070 if conf.crophack
1071 then
1072 (truncate (1.8 *. (a.(1) -. a.(0))),
1073 truncate (1.2 *. (a.(3) -. a.(0))))
1074 else
1075 (truncate (a.(1) -. a.(0)),
1076 truncate (a.(3) -. a.(0)))
1078 doreshape (w + conf.scrollw) h;
1079 Glut.postRedisplay ();
1081 | [] -> ()
1084 | '<' | '>' ->
1085 rotate (state.rotate + (if c = '>' then 30 else -30));
1087 | '[' | ']' ->
1088 state.colorscale <-
1089 max 0.0
1090 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1091 Glut.postRedisplay ()
1093 | 'k' -> gotoy (clamp (-conf.scrollincr))
1094 | 'j' -> gotoy (clamp conf.scrollincr)
1096 | _ ->
1097 vlog "huh? %d %c" key (Char.chr key);
1100 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1101 let len = String.length text in
1102 if len = 0
1103 then (
1104 state.textentry <- None;
1105 Glut.postRedisplay ();
1107 else (
1108 let s = String.sub text 0 (len - 1) in
1109 enttext (Some (c, s, onhist, onkey, ondone))
1112 | Some (c, text, onhist, onkey, ondone) ->
1113 begin match Char.unsafe_chr key with
1114 | '\r' | '\n' ->
1115 ondone text;
1116 state.textentry <- None;
1117 Glut.postRedisplay ()
1119 | '\027' ->
1120 state.textentry <- None;
1121 Glut.postRedisplay ()
1123 | _ ->
1124 begin match onkey text key with
1125 | TEdone text ->
1126 state.textentry <- None;
1127 ondone text;
1128 Glut.postRedisplay ()
1130 | TEcont text ->
1131 enttext (Some (c, text, onhist, onkey, ondone));
1133 | TEstop ->
1134 state.textentry <- None;
1135 Glut.postRedisplay ()
1137 | TEswitch te ->
1138 state.textentry <- Some te;
1139 Glut.postRedisplay ()
1140 end;
1141 end;
1144 let narrow outlines pattern =
1145 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1146 match reopt with
1147 | None -> None
1148 | Some re ->
1149 let rec fold accu n =
1150 if n = -1 then accu else
1151 let (s, _, _, _) as o = outlines.(n) in
1152 let accu =
1153 if (try ignore (Str.search_forward re s 0); true
1154 with Not_found -> false)
1155 then (o :: accu)
1156 else accu
1158 fold accu (n-1)
1160 let matched = fold [] (Array.length outlines - 1) in
1161 if matched = [] then None else Some (Array.of_list matched)
1164 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1165 let search active pattern incr =
1166 let dosearch re =
1167 let rec loop n =
1168 if n = Array.length outlines || n = -1 then None else
1169 let (s, _, _, _) = outlines.(n) in
1171 (try ignore (Str.search_forward re s 0); true
1172 with Not_found -> false)
1173 then Some n
1174 else loop (n + incr)
1176 loop active
1179 let re = Str.regexp_case_fold pattern in
1180 dosearch re
1181 with Failure s ->
1182 state.text <- s;
1183 None
1185 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1186 match key with
1187 | 27 ->
1188 if String.length qsearch = 0
1189 then (
1190 state.text <- "";
1191 state.outline <- None;
1192 Glut.postRedisplay ();
1194 else (
1195 state.text <- "";
1196 state.outline <- Some (allowdel, active, first, outlines, "");
1197 Glut.postRedisplay ();
1200 | 18 | 19 ->
1201 let incr = if key = 18 then -1 else 1 in
1202 let active, first =
1203 match search (active + incr) qsearch incr with
1204 | None ->
1205 state.text <- qsearch ^ " [not found]";
1206 active, first
1207 | Some active ->
1208 state.text <- qsearch;
1209 active, firstof active
1211 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1212 Glut.postRedisplay ();
1214 | 8 ->
1215 let len = String.length qsearch in
1216 if len = 0
1217 then ()
1218 else (
1219 if len = 1
1220 then (
1221 state.text <- "";
1222 state.outline <- Some (allowdel, active, first, outlines, "");
1224 else
1225 let qsearch = String.sub qsearch 0 (len - 1) in
1226 let active, first =
1227 match search active qsearch ~-1 with
1228 | None ->
1229 state.text <- qsearch ^ " [not found]";
1230 active, first
1231 | Some active ->
1232 state.text <- qsearch;
1233 active, firstof active
1235 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1237 Glut.postRedisplay ()
1239 | 13 ->
1240 if active < Array.length outlines
1241 then (
1242 let (_, _, n, t) = outlines.(active) in
1243 gotopage n t;
1245 state.text <- "";
1246 if allowdel then state.bookmarks <- Array.to_list outlines;
1247 state.outline <- None;
1248 Glut.postRedisplay ();
1250 | _ when key >= 32 && key < 127 ->
1251 let pattern = addchar qsearch (Char.chr key) in
1252 let active, first =
1253 match search active pattern 1 with
1254 | None ->
1255 state.text <- pattern ^ " [not found]";
1256 active, first
1257 | Some active ->
1258 state.text <- pattern;
1259 active, firstof active
1261 state.outline <- Some (allowdel, active, first, outlines, pattern);
1262 Glut.postRedisplay ()
1264 | 14 when not allowdel ->
1265 let optoutlines = narrow outlines qsearch in
1266 begin match optoutlines with
1267 | None -> state.text <- "can't narrow"
1268 | Some outlines ->
1269 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1270 match state.outlines with
1271 | Olist l -> ()
1272 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1273 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1274 end;
1275 Glut.postRedisplay ()
1277 | 21 when not allowdel ->
1278 let outline =
1279 match state.outlines with
1280 | Oarray a -> a
1281 | Olist l ->
1282 let a = Array.of_list (List.rev l) in
1283 state.outlines <- Oarray a;
1285 | Onarrow (a, b) ->
1286 state.outlines <- Oarray b;
1289 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1290 Glut.postRedisplay ()
1292 | 12 ->
1293 state.outline <-
1294 Some (allowdel, active, firstof active, outlines, qsearch);
1295 Glut.postRedisplay ()
1297 | 127 when allowdel ->
1298 let len = Array.length outlines - 1 in
1299 if len = 0
1300 then (
1301 state.outline <- None;
1302 state.bookmarks <- [];
1304 else (
1305 let bookmarks = Array.init len
1306 (fun i ->
1307 let i = if i >= active then i + 1 else i in
1308 outlines.(i)
1311 state.outline <-
1312 Some (allowdel,
1313 min active (len-1),
1314 min first (len-1),
1315 bookmarks, qsearch)
1318 Glut.postRedisplay ()
1320 | _ -> log "unknown key %d" key
1323 let keyboard ~key ~x ~y =
1324 if key = 7
1325 then
1326 wcmd "interrupt" []
1327 else
1328 match state.outline with
1329 | None -> viewkeyboard ~key ~x ~y
1330 | Some outline -> outlinekeyboard ~key ~x ~y outline
1333 let special ~key ~x ~y =
1334 match state.outline with
1335 | None ->
1336 begin match state.textentry with
1337 | None ->
1338 let y =
1339 match key with
1340 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1341 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1342 | Glut.KEY_DOWN -> clamp conf.scrollincr
1343 | Glut.KEY_PAGE_UP ->
1344 if Glut.getModifiers () land Glut.active_ctrl != 0
1345 then
1346 match state.layout with
1347 | [] -> state.y
1348 | l :: _ -> state.y - l.pagey
1349 else
1350 clamp (-state.h)
1351 | Glut.KEY_PAGE_DOWN ->
1352 if Glut.getModifiers () land Glut.active_ctrl != 0
1353 then
1354 match List.rev state.layout with
1355 | [] -> state.y
1356 | l :: _ -> getpagey l.pageno
1357 else
1358 clamp state.h
1359 | Glut.KEY_HOME -> addnav (); 0
1360 | Glut.KEY_END ->
1361 addnav ();
1362 state.maxy - (if conf.maxhfit then state.h else 0)
1363 | _ -> state.y
1365 state.text <- "";
1366 gotoy y
1368 | Some (c, s, Some onhist, onkey, ondone) ->
1369 let s =
1370 match key with
1371 | Glut.KEY_UP -> onhist HCprev
1372 | Glut.KEY_DOWN -> onhist HCnext
1373 | Glut.KEY_HOME -> onhist HCfirst
1374 | Glut.KEY_END -> onhist HClast
1375 | _ -> state.text
1377 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1378 Glut.postRedisplay ()
1380 | _ -> ()
1383 | Some (allowdel, active, first, outlines, qsearch) ->
1384 let maxrows = maxoutlinerows () in
1385 let navigate incr =
1386 let active = active + incr in
1387 let active = max 0 (min active (Array.length outlines - 1)) in
1388 let first =
1389 if active > first
1390 then
1391 let rows = active - first in
1392 if rows > maxrows then active - maxrows else first
1393 else active
1395 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1396 Glut.postRedisplay ()
1398 match key with
1399 | Glut.KEY_UP -> navigate ~-1
1400 | Glut.KEY_DOWN -> navigate 1
1401 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1402 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1404 | Glut.KEY_HOME ->
1405 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1406 Glut.postRedisplay ()
1408 | Glut.KEY_END ->
1409 let active = Array.length outlines - 1 in
1410 let first = max 0 (active - maxrows) in
1411 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1412 Glut.postRedisplay ()
1414 | _ -> ()
1417 let drawplaceholder l =
1418 GlDraw.color (scalecolor 1.0);
1419 GlDraw.rect
1420 (0.0, float l.pagedispy)
1421 (float l.pagew, float (l.pagedispy + l.pagevh))
1423 let x = 0.0
1424 and y = float (l.pagedispy + 13) in
1425 let font = Glut.BITMAP_8_BY_13 in
1426 GlDraw.color (0.0, 0.0, 0.0);
1427 GlPix.raster_pos ~x ~y ();
1428 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1429 ("Loading " ^ string_of_int l.pageno);
1432 let now () = Unix.gettimeofday ();;
1434 let drawpage i l =
1435 begin match getopaque l.pageno with
1436 | Some opaque when validopaque opaque ->
1437 if state.textentry = None
1438 then GlDraw.color (scalecolor 1.0)
1439 else GlDraw.color (scalecolor 0.4);
1440 let a = now () in
1441 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1442 let b = now () in
1443 let d = b-.a in
1444 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1445 vlog "draw %f sec" d;
1447 | _ ->
1448 drawplaceholder l;
1449 end;
1450 GlDraw.color (0.5, 0.5, 0.5);
1451 GlDraw.rect
1452 (0., float i)
1453 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1455 l.pagedispy + l.pagevh;
1458 let scrollindicator () =
1459 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1460 GlDraw.color (0.64 , 0.64, 0.64);
1461 GlDraw.rect
1462 (float (state.w - conf.scrollw), 0.)
1463 (float state.w, float state.h)
1465 GlDraw.color (0.0, 0.0, 0.0);
1466 let sh = (float (maxy + state.h) /. float state.h) in
1467 let sh = float state.h /. sh in
1468 let sh = max sh (float conf.scrollh) in
1470 let percent =
1471 if state.y = state.maxy
1472 then 1.0
1473 else float state.y /. float maxy
1475 let position = (float state.h -. sh) *. percent in
1477 let position =
1478 if position +. sh > float state.h
1479 then
1480 float state.h -. sh
1481 else
1482 position
1484 GlDraw.rect
1485 (float (state.w - conf.scrollw), position)
1486 (float state.w, position +. sh)
1490 let showsel () =
1491 match state.mstate with
1492 | Mnone ->
1495 | Msel ((x0, y0), (x1, y1)) ->
1496 let rec loop = function
1497 | l :: ls ->
1498 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1499 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1500 then
1501 match getopaque l.pageno with
1502 | Some opaque when validopaque opaque ->
1503 let oy = -l.pagey + l.pagedispy in
1504 seltext opaque (x0, y0, x1, y1) oy;
1506 | _ -> ()
1507 else loop ls
1508 | [] -> ()
1510 loop state.layout
1513 let showrects () =
1514 Gl.enable `blend;
1515 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1516 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1517 List.iter
1518 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1519 List.iter (fun l ->
1520 if l.pageno = pageno
1521 then (
1522 let d = float (l.pagedispy - l.pagey) in
1523 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1524 GlDraw.begins `quads;
1526 GlDraw.vertex2 (x0, y0+.d);
1527 GlDraw.vertex2 (x1, y1+.d);
1528 GlDraw.vertex2 (x2, y2+.d);
1529 GlDraw.vertex2 (x3, y3+.d);
1531 GlDraw.ends ();
1533 ) state.layout
1534 ) state.rects
1536 Gl.disable `blend;
1539 let showoutline = function
1540 | None -> ()
1541 | Some (allowdel, active, first, outlines, qsearch) ->
1542 Gl.enable `blend;
1543 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1544 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1545 GlDraw.rect (0., 0.) (float state.w, float state.h);
1546 Gl.disable `blend;
1548 GlDraw.color (1., 1., 1.);
1549 let font = Glut.BITMAP_9_BY_15 in
1550 let draw_string x y s =
1551 GlPix.raster_pos ~x ~y ();
1552 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1554 let rec loop row =
1555 if row = Array.length outlines || (row - first) * 16 > state.h
1556 then ()
1557 else (
1558 let (s, l, _, _) = outlines.(row) in
1559 let y = (row - first) * 16 in
1560 let x = 5 + 15*l in
1561 if row = active
1562 then (
1563 Gl.enable `blend;
1564 GlDraw.polygon_mode `both `line;
1565 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1566 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1567 GlDraw.rect (0., float (y + 1))
1568 (float (state.w - conf.scrollw - 1), float (y + 18));
1569 GlDraw.polygon_mode `both `fill;
1570 Gl.disable `blend;
1571 GlDraw.color (1., 1., 1.);
1573 draw_string (float x) (float (y + 16)) s;
1574 loop (row+1)
1577 loop first
1580 let display () =
1581 let lasty = List.fold_left drawpage 0 (state.layout) in
1582 GlDraw.color (scalecolor 0.5);
1583 GlDraw.rect
1584 (0., float lasty)
1585 (float (state.w - conf.scrollw), float state.h)
1587 showrects ();
1588 scrollindicator ();
1589 showsel ();
1590 showoutline state.outline;
1591 enttext ();
1592 Glut.swapBuffers ();
1595 let getunder x y =
1596 let rec f = function
1597 | l :: rest ->
1598 begin match getopaque l.pageno with
1599 | Some opaque when validopaque opaque ->
1600 let y = y - l.pagedispy in
1601 if y > 0
1602 then
1603 let y = l.pagey + y in
1604 match whatsunder opaque x y with
1605 | Unone -> f rest
1606 | under -> under
1607 else
1608 f rest
1609 | _ ->
1610 f rest
1612 | [] -> Unone
1614 f state.layout
1617 let mouse ~button ~bstate ~x ~y =
1618 match button with
1619 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1620 let incr =
1621 if n = 3
1622 then
1623 -conf.scrollincr
1624 else
1625 conf.scrollincr
1627 let incr = incr * 2 in
1628 let y = clamp incr in
1629 gotoy y
1631 | Glut.LEFT_BUTTON when state.outline = None ->
1632 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1633 begin match dest with
1634 | Ulinkgoto (pageno, top) ->
1635 if pageno >= 0
1636 then
1637 gotopage1 pageno top
1639 | Ulinkuri s ->
1640 print_endline s
1642 | Unone when bstate = Glut.DOWN ->
1643 Glut.setCursor Glut.CURSOR_INHERIT;
1644 state.mstate <- Mnone
1646 | Unone | Utext _ ->
1647 if bstate = Glut.DOWN
1648 then (
1649 if state.rotate mod 360 = 0 then (
1650 state.mstate <- Msel ((x, y), (x, y));
1651 Glut.postRedisplay ()
1654 else (
1655 match state.mstate with
1656 | Mnone -> ()
1657 | Msel ((x0, y0), (x1, y1)) ->
1658 let f l =
1659 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1660 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1661 then
1662 match getopaque l.pageno with
1663 | Some opaque when validopaque opaque ->
1664 copysel opaque
1665 | _ -> ()
1667 List.iter f state.layout;
1668 copysel ""; (* ugly *)
1669 Glut.setCursor Glut.CURSOR_INHERIT;
1670 state.mstate <- Mnone;
1674 | _ ->
1677 let mouse ~button ~state ~x ~y = mouse button state x y;;
1679 let motion ~x ~y =
1680 if state.outline = None
1681 then
1682 match state.mstate with
1683 | Mnone -> ()
1684 | Msel (a, _) ->
1685 state.mstate <- Msel (a, (x, y));
1686 Glut.postRedisplay ()
1689 let pmotion ~x ~y =
1690 if state.outline = None
1691 then
1692 match state.mstate with
1693 | Mnone ->
1694 begin match getunder x y with
1695 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1696 | Ulinkuri uri ->
1697 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1698 Glut.setCursor Glut.CURSOR_INFO
1699 | Ulinkgoto (page, y) ->
1700 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1701 Glut.setCursor Glut.CURSOR_INFO
1702 | Utext s ->
1703 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1704 Glut.setCursor Glut.CURSOR_TEXT
1707 | Msel (a, _) ->
1711 let () =
1712 let statepath =
1713 let home =
1714 if Sys.os_type = "Win32"
1715 then
1716 try Sys.getenv "HOMEPATH" with Not_found -> ""
1717 else
1718 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1720 Filename.concat home "llpp"
1722 let pstate =
1724 let ic = open_in_bin statepath in
1725 let hash = input_value ic in
1726 close_in ic;
1727 hash
1728 with exn ->
1729 if false
1730 then
1731 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1733 Hashtbl.create 1
1735 let savestate () =
1737 let w, h =
1738 match state.fullscreen with
1739 | None -> state.w, state.h
1740 | Some wh -> wh
1742 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1743 let oc = open_out_bin statepath in
1744 output_value oc pstate
1745 with exn ->
1746 if false
1747 then
1748 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1751 let setstate () =
1753 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1754 state.w <- statew;
1755 state.h <- stateh;
1756 state.bookmarks <- statebookmarks;
1757 with Not_found -> ()
1758 | exn ->
1759 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1762 Arg.parse [] (fun s -> state.path <- s) "options:";
1763 let name =
1764 if String.length state.path = 0
1765 then (prerr_endline "filename missing"; exit 1)
1766 else state.path
1769 setstate ();
1770 let _ = Glut.init Sys.argv in
1771 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1772 let () = Glut.initWindowSize state.w state.h in
1773 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1775 let csock, ssock =
1776 if Sys.os_type = "Unix"
1777 then
1778 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1779 else
1780 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1781 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1782 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1783 Unix.bind sock addr;
1784 Unix.listen sock 1;
1785 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1786 Unix.connect csock addr;
1787 let ssock, _ = Unix.accept sock in
1788 Unix.close sock;
1789 let opts sock =
1790 Unix.setsockopt sock Unix.TCP_NODELAY true;
1791 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1793 opts ssock;
1794 opts csock;
1795 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1796 ssock, csock
1799 let () = Glut.displayFunc display in
1800 let () = Glut.reshapeFunc reshape in
1801 let () = Glut.keyboardFunc keyboard in
1802 let () = Glut.specialFunc special in
1803 let () = Glut.idleFunc (Some idle) in
1804 let () = Glut.mouseFunc mouse in
1805 let () = Glut.motionFunc motion in
1806 let () = Glut.passiveMotionFunc pmotion in
1808 init ssock;
1809 state.csock <- csock;
1810 state.ssock <- ssock;
1811 state.text <- "Opening " ^ name;
1812 writecmd csock ("open " ^ name ^ "\000");
1814 at_exit savestate;
1816 let rec handlelablglutbug () =
1818 Glut.mainLoop ();
1819 with Glut.BadEnum "key in special_of_int" ->
1820 showtext '!' " LablGlut bug: special key not recognized";
1821 handlelablglutbug ()
1823 handlelablglutbug ();