Make it less chatty when hovering over
[llpp.git] / main.ml
blobdf5363ac5ea7e9e11e37f74cef51201b3a8c9d30
1 type link = | LNone | LUri of string | LGoto of (int * int);;
3 let log fmt = Printf.kprintf prerr_endline fmt;;
4 let dolog fmt = Printf.kprintf prerr_endline fmt;;
6 external init : Unix.file_descr -> unit = "ml_init";;
7 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
8 external gettext : string -> (int * int * int * int) -> int -> bool -> unit =
9 "ml_gettext";;
10 external getlink : string -> int -> int -> link = "ml_getlink";;
11 external highlightlinks : string -> int -> unit = "ml_highlightlinks";;
12 external getpagewh : int -> float array = "ml_getpagewh";;
14 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
16 type 'a circbuf =
17 { store : 'a array
18 ; mutable rc : int
19 ; mutable wc : int
20 ; mutable len : int
24 type textentry = (char * string * onhist option * onkey * ondone)
25 and onkey = string -> int -> te
26 and ondone = string -> unit
27 and onhist = histcmd -> string
28 and histcmd = HCnext | HCprev | HCfirst | HClast
29 and te =
30 | TEstop
31 | TEdone of string
32 | TEcont of string
33 | TEswitch of textentry
36 let cbnew n v =
37 { store = Array.create n v
38 ; rc = 0
39 ; wc = 0
40 ; len = 0
44 let cblen b = Array.length b.store;;
46 let cbput b v =
47 let len = cblen b in
48 b.store.(b.wc) <- v;
49 b.wc <- (b.wc + 1) mod len;
50 b.len <- min (b.len + 1) len;
53 let cbpeekw b = b.store.(b.wc);;
55 let cbget b dir =
56 if b.len = 0 then b.store.(0) else
57 let rc = b.rc + dir in
58 let rc = if rc = -1 then b.len - 1 else rc in
59 let rc = if rc = b.len then 0 else rc in
60 b.rc <- rc;
61 b.store.(rc);
64 let cbrfollowlen b =
65 b.rc <- b.len;
68 type layout =
69 { pageno : int
70 ; pagedimno : int
71 ; pagew : int
72 ; pageh : int
73 ; pagedispy : int
74 ; pagey : int
75 ; pagevh : int
79 type conf =
80 { mutable scrollw : int
81 ; mutable scrollh : int
82 ; mutable rectsel : bool
83 ; mutable icase : bool
84 ; mutable preload : bool
85 ; mutable pagebias : int
86 ; mutable verbose : bool
87 ; mutable scrollincr : int
88 ; mutable maxhfit : bool
89 ; mutable crophack : bool
90 ; mutable autoscroll : bool
91 ; mutable showall : bool
92 ; mutable hlinks : bool
96 type outline = string * int * int * int;;
97 type outlines =
98 | Oarray of outline array
99 | Olist of outline list
100 | Onarrow of outline array * outline array
103 type rect = (float * float * float * float * float * float * float * float);;
105 type state =
106 { mutable csock : Unix.file_descr
107 ; mutable ssock : Unix.file_descr
108 ; mutable w : int
109 ; mutable h : int
110 ; mutable rotate : int
111 ; mutable y : int
112 ; mutable ty : int
113 ; mutable prevy : int
114 ; mutable maxy : int
115 ; mutable layout : layout list
116 ; pagemap : ((int * int * int), string) Hashtbl.t
117 ; mutable pages : (int * int * int) list
118 ; mutable pagecount : int
119 ; pagecache : string circbuf
120 ; mutable inflight : int
121 ; mutable mstate : mstate
122 ; mutable searchpattern : string
123 ; mutable rects : (int * int * rect) list
124 ; mutable rects1 : (int * int * rect) list
125 ; mutable text : string
126 ; mutable fullscreen : (int * int) option
127 ; mutable textentry : textentry option
128 ; mutable outlines : outlines
129 ; mutable outline : (bool * int * int * outline array * string) option
130 ; mutable bookmarks : outline list
131 ; mutable path : string
132 ; mutable invalidated : int
133 ; hists : hists
135 and hists =
136 { pat : string circbuf
137 ; pag : string circbuf
138 ; nav : float circbuf
142 let conf =
143 { scrollw = 5
144 ; scrollh = 12
145 ; icase = true
146 ; rectsel = true
147 ; preload = false
148 ; pagebias = 0
149 ; verbose = false
150 ; scrollincr = 24
151 ; maxhfit = true
152 ; crophack = false
153 ; autoscroll = false
154 ; showall = false
155 ; hlinks = false
159 let state =
160 { csock = Unix.stdin
161 ; ssock = Unix.stdin
162 ; w = 900
163 ; h = 900
164 ; rotate = 0
165 ; y = 0
166 ; ty = 0
167 ; prevy = 0
168 ; layout = []
169 ; maxy = max_int
170 ; pagemap = Hashtbl.create 10
171 ; pagecache = cbnew 10 ""
172 ; pages = []
173 ; pagecount = 0
174 ; inflight = 0
175 ; mstate = Mnone
176 ; rects = []
177 ; rects1 = []
178 ; text = ""
179 ; fullscreen = None
180 ; textentry = None
181 ; searchpattern = ""
182 ; outlines = Olist []
183 ; outline = None
184 ; bookmarks = []
185 ; path = ""
186 ; invalidated = 0
187 ; hists =
188 { nav = cbnew 100 0.0
189 ; pat = cbnew 20 ""
190 ; pag = cbnew 10 ""
195 let vlog fmt =
196 if conf.verbose
197 then
198 Printf.kprintf prerr_endline fmt
199 else
200 Printf.kprintf ignore fmt
203 let writecmd fd s =
204 let len = String.length s in
205 let n = 4 + len in
206 let b = Buffer.create n in
207 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
208 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
209 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
210 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
211 Buffer.add_string b s;
212 let s' = Buffer.contents b in
213 let n' = Unix.write fd s' 0 n in
214 if n' != n then failwith "write failed";
217 let readcmd fd =
218 let s = "xxxx" in
219 let n = Unix.read fd s 0 4 in
220 if n != 4 then failwith "incomplete read(len)";
221 let len = 0
222 lor (Char.code s.[0] lsl 24)
223 lor (Char.code s.[1] lsl 16)
224 lor (Char.code s.[2] lsl 8)
225 lor (Char.code s.[3] lsl 0)
227 let s = String.create len in
228 let n = Unix.read fd s 0 len in
229 if n != len then failwith "incomplete read(data)";
233 let yratio y =
234 if y = state.maxy then 1.0
235 else float y /. float state.maxy
238 let makecmd s l =
239 let b = Buffer.create 10 in
240 Buffer.add_string b s;
241 let rec combine = function
242 | [] -> b
243 | x :: xs ->
244 Buffer.add_char b ' ';
245 let s =
246 match x with
247 | `b b -> if b then "1" else "0"
248 | `s s -> s
249 | `i i -> string_of_int i
250 | `f f -> string_of_float f
251 | `I f -> string_of_int (truncate f)
253 Buffer.add_string b s;
254 combine xs;
256 combine l;
259 let wcmd s l =
260 let cmd = Buffer.contents (makecmd s l) in
261 writecmd state.csock cmd;
264 let calcheight () =
265 let rec f pn ph fh l =
266 match l with
267 | (n, _, h) :: rest ->
268 let fh = fh + (n - pn) * ph in
269 f n h fh rest
271 | [] ->
272 let fh = fh + (ph * (state.pagecount - pn)) in
273 max 0 fh
275 let fh = f 0 0 0 state.pages in
279 let getpagey pageno =
280 let rec f pn ph y l =
281 match l with
282 | (n, _, h) :: rest ->
283 if n >= pageno
284 then
285 y + (pageno - pn) * ph
286 else
287 let y = y + (n - pn) * ph in
288 f n h y rest
290 | [] ->
291 y + (pageno - pn) * ph
293 f 0 0 0 state.pages;
296 let layout y sh =
297 let rec f pageno pdimno prev vy py dy l cacheleft accu =
298 if pageno = state.pagecount || cacheleft = 0
299 then accu
300 else
301 let ((_, w, h) as curr), rest, pdimno =
302 match l with
303 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
304 curr, rest, pdimno + 1
305 | _ ->
306 prev, l, pdimno
308 let pageno' = pageno + 1 in
309 if py + h > vy
310 then
311 let py' = vy - py in
312 let vh = h - py' in
313 if dy + vh > sh
314 then
315 let vh = sh - dy in
316 if vh <= 0
317 then
318 accu
319 else
320 let e =
321 { pageno = pageno
322 ; pagedimno = pdimno
323 ; pagew = w
324 ; pageh = h
325 ; pagedispy = dy
326 ; pagey = py'
327 ; pagevh = vh
330 e :: accu
331 else
332 let e =
333 { pageno = pageno
334 ; pagedimno = pdimno
335 ; pagew = w
336 ; pageh = h
337 ; pagedispy = dy
338 ; pagey = py'
339 ; pagevh = vh
342 let accu = e :: accu in
343 f pageno' pdimno curr
344 (vy + vh) (py + h) (dy + vh + 2) rest
345 (pred cacheleft) accu
346 else
347 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
349 if state.invalidated = 0
350 then
351 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
352 state.maxy <- calcheight ();
353 List.rev accu
354 else
358 let clamp incr =
359 let y = state.y + incr in
360 let y = max 0 y in
361 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
365 let getopaque pageno =
366 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
367 state.rotate))
368 with Not_found -> None
371 let cache pageno opaque =
372 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
373 state.rotate) opaque
376 let validopaque opaque = String.length opaque > 0;;
378 let preload l =
379 match getopaque l.pageno with
380 | None when state.inflight < 2+0*(cblen state.pagecache) ->
381 state.inflight <- succ state.inflight;
382 cache l.pageno "";
383 wcmd "render" [`i (l.pageno + 1)
384 ;`i l.pagedimno
385 ;`i l.pagew
386 ;`i l.pageh];
388 | _ -> ()
391 let gotoy y =
392 let y = max 0 y in
393 let y = min state.maxy y in
394 let pages = layout y state.h in
395 let rec f all = function
396 | l :: ls ->
397 begin match getopaque l.pageno with
398 | None -> preload l; f false ls
399 | Some opaque -> f (all && validopaque opaque) ls
401 | [] -> all
403 if not conf.showall || f true pages
404 then (
405 state.y <- y;
406 state.layout <- pages;
408 state.ty <- y;
409 if conf.preload then begin
410 let h = state.h in
411 let y = if state.y < state.h then 0 else state.y - state.h in
412 let pages = layout y (h*3) in
413 List.iter preload pages;
414 end;
417 let addnav () =
418 cbput state.hists.nav (yratio state.y);
419 cbrfollowlen state.hists.nav;
422 let getnav () =
423 let y = cbget state.hists.nav ~-1 in
424 truncate (y *. float state.maxy)
427 let gotopage n top =
428 let y = getpagey n in
429 addnav ();
430 gotoy (y + top);
433 let invalidate () =
434 state.layout <- [];
435 state.pages <- [];
436 state.rects <- [];
437 state.rects1 <- [];
438 state.invalidated <- state.invalidated + 1;
441 let reshape ~w ~h =
442 let ratio = float w /. float state.w in
443 let fixbookmark (s, l, pageno, pagey) =
444 let pagey = truncate (float pagey *. ratio) in
445 (s, l, pageno, pagey)
447 state.bookmarks <- List.map fixbookmark state.bookmarks;
448 state.w <- w;
449 state.h <- h;
450 GlDraw.viewport 0 0 w h;
451 GlMat.mode `modelview;
452 GlMat.load_identity ();
453 GlMat.mode `projection;
454 GlMat.load_identity ();
455 GlMat.rotate ~x:1.0 ~angle:180.0 ();
456 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
457 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
458 GlClear.color (1., 1., 1.);
459 GlClear.clear [`color];
460 invalidate ();
461 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
464 let showtext c s =
465 GlDraw.color (0.0, 0.0, 0.0);
466 GlDraw.rect
467 (0.0, float (state.h - 18))
468 (float (state.w - conf.scrollw - 1), float state.h)
470 let font = Glut.BITMAP_8_BY_13 in
471 GlDraw.color (1.0, 1.0, 1.0);
472 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
473 Glut.bitmapCharacter ~font ~c:(Char.code c);
474 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
477 let enttext () =
478 let len = String.length state.text in
479 match state.textentry with
480 | None ->
481 if len > 0 then showtext ' ' state.text
483 | Some (c, text, _, _, _) ->
484 let s =
485 if len > 0
486 then
487 text ^ " [" ^ state.text ^ "]"
488 else
489 text
491 showtext c s;
494 let act cmd =
495 match cmd.[0] with
496 | 'c' ->
497 state.pages <- [];
498 state.outlines <- Olist []
500 | 'D' ->
501 state.rects <- state.rects1;
502 Glut.postRedisplay ()
504 | 'd' ->
505 state.rects <- state.rects1;
506 Glut.postRedisplay ()
508 | 'C' ->
509 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
510 state.pagecount <- n;
511 state.invalidated <- state.invalidated - 1;
512 if state.invalidated = 0
513 then (
514 let rely = yratio state.y in
515 let maxy = calcheight () in
516 state.y <- truncate (float maxy *. rely);
517 state.ty <- state.y;
518 let pages = layout state.y state.h in
519 state.layout <- pages;
520 Glut.postRedisplay ();
523 | 't' ->
524 let s = Scanf.sscanf cmd "t %n"
525 (fun n -> String.sub cmd n (String.length cmd - n))
527 Glut.setWindowTitle s
529 | 'T' ->
530 let s = Scanf.sscanf cmd "T %n"
531 (fun n -> String.sub cmd n (String.length cmd - n))
533 if state.textentry = None
534 then (
535 state.text <- s;
536 showtext ' ' s;
537 Glut.swapBuffers ();
539 else (
540 state.text <- s;
541 Glut.postRedisplay ();
544 | 'V' ->
545 if conf.verbose
546 then
547 let s = Scanf.sscanf cmd "V %n"
548 (fun n -> String.sub cmd n (String.length cmd - n))
550 state.text <- s;
551 showtext ' ' s;
552 Glut.swapBuffers ();
554 | 'F' ->
555 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
556 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
557 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
558 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
560 let y = (getpagey pageno) + truncate y0 in
561 addnav ();
562 gotoy y;
563 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
565 | 'R' ->
566 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
567 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
568 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
569 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
571 state.rects1 <-
572 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
574 | 'r' ->
575 let n, w, h, r, p =
576 Scanf.sscanf cmd "r %d %d %d %d %s"
577 (fun n w h r p -> (n, w, h, r, p))
579 Hashtbl.replace state.pagemap (n, w, r) p;
580 let evicted = cbpeekw state.pagecache in
581 if String.length evicted > 0
582 then begin
583 wcmd "free" [`s evicted];
584 let l = Hashtbl.fold (fun k p a ->
585 if evicted = p then k :: a else a) state.pagemap []
587 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
588 end;
589 cbput state.pagecache p;
590 state.inflight <- pred state.inflight;
591 if conf.showall then gotoy state.ty;
592 Glut.postRedisplay ()
594 | 'l' ->
595 let (n, w, h) as pagelayout =
596 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
598 state.pages <- pagelayout :: state.pages
600 | 'o' ->
601 let (l, n, t, pos) =
602 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
604 let s = String.sub cmd pos (String.length cmd - pos) in
605 let outline = (s, l, n, t) in
606 let outlines =
607 match state.outlines with
608 | Olist outlines -> Olist (outline :: outlines)
609 | Oarray _ -> Olist [outline]
610 | Onarrow _ -> Olist [outline]
612 state.outlines <- outlines
614 | _ ->
615 log "unknown cmd `%S'" cmd
618 let idle () =
619 if state.y != state.prevy
620 then (
621 state.prevy <- state.y;
622 Glut.postRedisplay ();
624 else
625 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
627 begin match r with
628 | [] ->
629 if conf.autoscroll then begin
630 let y = state.y + conf.scrollincr in
631 let y = if y >= state.maxy then 0 else y in
632 gotoy y;
633 state.text <- "";
634 state.prevy <- state.y;
635 Glut.postRedisplay ();
636 end;
638 | _ ->
639 let cmd = readcmd state.csock in
640 act cmd;
641 end;
644 let onhist cb = function
645 | HCprev -> cbget cb ~-1
646 | HCnext -> cbget cb 1
647 | HCfirst -> cbget cb ~-(cb.rc)
648 | HClast -> cbget cb (cb.len - 1 - cb.rc)
651 let search pattern forward =
652 if String.length pattern > 0
653 then
654 let pn, py =
655 match state.layout with
656 | [] -> 0, 0
657 | l :: _ ->
658 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
660 let cmd =
661 let b = makecmd "search"
662 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
664 Buffer.add_char b ',';
665 Buffer.add_string b pattern;
666 Buffer.add_char b '\000';
667 Buffer.contents b;
669 writecmd state.csock cmd;
672 let intentry text key =
673 let c = Char.unsafe_chr key in
674 match c with
675 | '0' .. '9' ->
676 let s = "x" in s.[0] <- c;
677 let text = text ^ s in
678 TEcont text
680 | _ ->
681 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
682 TEcont text
685 let addchar s c =
686 let b = Buffer.create (String.length s + 1) in
687 Buffer.add_string b s;
688 Buffer.add_char b c;
689 Buffer.contents b;
692 let textentry text key =
693 let c = Char.unsafe_chr key in
694 match c with
695 | _ when key >= 32 && key < 127 ->
696 let text = addchar text c in
697 TEcont text
699 | _ ->
700 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
701 TEcont text
704 let rotate angle =
705 state.rotate <- angle;
706 invalidate ();
707 wcmd "rotate" [`i angle];
710 let optentry text key =
711 let btos b = if b then "on" else "off" in
712 let c = Char.unsafe_chr key in
713 match c with
714 | 'r' ->
715 conf.rectsel <- not conf.rectsel;
716 TEdone ("rectsel " ^ (btos conf.rectsel))
718 | 's' ->
719 let ondone s =
720 try conf.scrollincr <- int_of_string s with exc ->
721 state.text <- Printf.sprintf "bad integer `%s': %s"
722 s (Printexc.to_string exc)
724 TEswitch ('#', "", None, intentry, ondone)
726 | 'R' ->
727 let ondone s =
728 match try
729 Some (int_of_string s)
730 with exc ->
731 state.text <- Printf.sprintf "bad integer `%s': %s"
732 s (Printexc.to_string exc);
733 None
734 with
735 | Some angle -> rotate angle
736 | None -> ()
738 TEswitch ('^', "", None, intentry, ondone)
740 | 'i' ->
741 conf.icase <- not conf.icase;
742 TEdone ("case insensitive search " ^ (btos conf.icase))
744 | 'p' ->
745 conf.preload <- not conf.preload;
746 gotoy state.y;
747 TEdone ("preload " ^ (btos conf.preload))
749 | 'v' ->
750 conf.verbose <- not conf.verbose;
751 TEdone ("verbose " ^ (btos conf.verbose))
753 | 'h' ->
754 conf.maxhfit <- not conf.maxhfit;
755 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
756 TEdone ("maxhfit " ^ (btos conf.maxhfit))
758 | 'c' ->
759 conf.crophack <- not conf.crophack;
760 TEdone ("crophack " ^ btos conf.crophack)
762 | 'a' ->
763 conf.showall <- not conf.showall;
764 TEdone ("showall " ^ btos conf.showall)
766 | _ ->
767 state.text <- Printf.sprintf "bad option %d `%c'" key c;
768 TEstop
771 let maxoutlinerows () = (state.h - 31) / 16;;
773 let enterselector allowdel outlines errmsg =
774 if Array.length outlines = 0
775 then (
776 showtext ' ' errmsg;
777 Glut.swapBuffers ()
779 else
780 let pageno =
781 match state.layout with
782 | [] -> -1
783 | {pageno=pageno} :: rest -> pageno
785 let active =
786 let rec loop n =
787 if n = Array.length outlines
788 then 0
789 else
790 let (_, _, outlinepageno, _) = outlines.(n) in
791 if outlinepageno >= pageno then n else loop (n+1)
793 loop 0
795 state.outline <-
796 Some (allowdel, active,
797 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
798 Glut.postRedisplay ();
801 let enteroutlinemode () =
802 let outlines =
803 match state.outlines with
804 | Oarray a -> a
805 | Olist l ->
806 let a = Array.of_list (List.rev l) in
807 state.outlines <- Oarray a;
809 | Onarrow (a, b) -> a
811 enterselector false outlines "Documents has no outline";
814 let enterbookmarkmode () =
815 let bookmarks = Array.of_list state.bookmarks in
816 enterselector true bookmarks "Documents has no bookmarks (yet)";
820 let quickbookmark ?title () =
821 match state.layout with
822 | [] -> ()
823 | l :: _ ->
824 let title =
825 match title with
826 | None ->
827 let sec = Unix.gettimeofday () in
828 let tm = Unix.localtime sec in
829 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
830 l.pageno
831 tm.Unix.tm_mday
832 tm.Unix.tm_mon
833 (tm.Unix.tm_year + 1900)
834 tm.Unix.tm_hour
835 tm.Unix.tm_min
836 | Some title -> title
838 state.bookmarks <-
839 (title, 0, l.pageno, l.pagey) :: state.bookmarks
842 let viewkeyboard ~key ~x ~y =
843 let enttext te =
844 state.textentry <- te;
845 state.text <- "";
846 enttext ();
847 Glut.postRedisplay ()
849 match state.textentry with
850 | None ->
851 let c = Char.chr key in
852 begin match c with
853 | '\027' | 'q' ->
854 exit 0
856 | '\008' ->
857 let y = getnav () in
858 gotoy y
860 | 'o' ->
861 enteroutlinemode ()
863 | 'u' ->
864 state.rects <- [];
865 state.text <- "";
866 Glut.postRedisplay ()
868 | '/' | '?' ->
869 let ondone isforw s =
870 cbput state.hists.pat s;
871 cbrfollowlen state.hists.pat;
872 state.searchpattern <- s;
873 search s isforw
875 enttext (Some (c, "", Some (onhist state.hists.pat),
876 textentry, ondone (c ='/')))
878 | '+' ->
879 let ondone s =
880 let n =
881 try int_of_string s with exc ->
882 state.text <- Printf.sprintf "bad integer `%s': %s"
883 s (Printexc.to_string exc);
884 max_int
886 if n != max_int
887 then (
888 conf.pagebias <- n;
889 state.text <- "page bias is now " ^ string_of_int n;
892 enttext (Some ('+', "", None, intentry, ondone))
894 | '-' ->
895 let ondone msg =
896 state.text <- msg;
898 enttext (Some ('-', "", None, optentry, ondone))
900 | '0' .. '9' ->
901 let ondone s =
902 let n =
903 try int_of_string s with exc ->
904 state.text <- Printf.sprintf "bad integer `%s': %s"
905 s (Printexc.to_string exc);
908 if n >= 0
909 then (
910 addnav ();
911 cbput state.hists.pag (string_of_int n);
912 cbrfollowlen state.hists.pag;
913 gotoy (getpagey (n + conf.pagebias - 1))
916 let pageentry text key =
917 match Char.unsafe_chr key with
918 | 'g' -> TEdone text
919 | _ -> intentry text key
921 let text = "x" in text.[0] <- c;
922 enttext (Some (':', text, Some (onhist state.hists.pag),
923 pageentry, ondone))
925 | 'b' ->
926 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
927 reshape state.w state.h;
929 | 'l' ->
930 conf.hlinks <- not conf.hlinks;
931 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
932 Glut.postRedisplay ()
934 | 'a' ->
935 conf.autoscroll <- not conf.autoscroll
937 | 'f' ->
938 begin match state.fullscreen with
939 | None ->
940 state.fullscreen <- Some (state.w, state.h);
941 Glut.fullScreen ()
942 | Some (w, h) ->
943 state.fullscreen <- None;
944 Glut.reshapeWindow ~w ~h
947 | 'g' ->
948 gotoy 0
950 | 'n' ->
951 search state.searchpattern true
953 | 'p' | 'N' ->
954 search state.searchpattern false
956 | 't' ->
957 begin match state.layout with
958 | [] -> ()
959 | l :: _ ->
960 gotoy (state.y - l.pagey);
963 | ' ' ->
964 begin match List.rev state.layout with
965 | [] -> ()
966 | l :: _ ->
967 gotoy (clamp (l.pageh - l.pagey))
970 | '\127' ->
971 begin match state.layout with
972 | [] -> ()
973 | l :: _ ->
974 gotoy (clamp (-l.pageh));
977 | '=' ->
978 let f (fn, ln) l =
979 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
981 let fn, ln = List.fold_left f (-1, -1) state.layout in
982 let s =
983 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
984 let percent =
985 if maxy <= 0
986 then 100.
987 else (100. *. (float state.y /. float maxy)) in
988 if fn = ln
989 then
990 Printf.sprintf "Page %d of %d %.2f%%"
991 (fn+1) state.pagecount percent
992 else
993 Printf.sprintf
994 "Pages %d-%d of %d %.2f%%"
995 (fn+1) (ln+1) state.pagecount percent
997 showtext ' ' s;
998 Glut.swapBuffers ()
1000 | 'w' ->
1001 begin match state.layout with
1002 | [] -> ()
1003 | l :: _ ->
1004 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1005 Glut.postRedisplay ();
1008 | '\'' ->
1009 enterbookmarkmode ()
1011 | 'm' ->
1012 let ondone s =
1013 match state.layout with
1014 | l :: _ ->
1015 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1016 | _ -> ()
1018 enttext (Some ('~', "", None, textentry, ondone))
1020 | '~' ->
1021 quickbookmark ();
1022 showtext ' ' "Quick bookmark added";
1023 Glut.swapBuffers ()
1025 | 'z' ->
1026 begin match state.layout with
1027 | l :: _ ->
1028 let a = getpagewh l.pagedimno in
1029 let w, h =
1030 if conf.crophack
1031 then
1032 (truncate (1.8 *. (a.(1) -. a.(0))),
1033 truncate (1.2 *. (a.(3) -. a.(0))))
1034 else
1035 (truncate (a.(1) -. a.(0)),
1036 truncate (a.(3) -. a.(0)))
1038 Glut.reshapeWindow (w + conf.scrollw) h;
1039 Glut.postRedisplay ();
1041 | [] -> ()
1044 | '<' | '>' ->
1045 rotate (state.rotate + (if c = '>' then 30 else -30));
1047 | _ ->
1048 vlog "huh? %d %c" key (Char.chr key);
1051 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1052 let len = String.length text in
1053 if len = 0
1054 then (
1055 state.textentry <- None;
1056 Glut.postRedisplay ();
1058 else (
1059 let s = String.sub text 0 (len - 1) in
1060 enttext (Some (c, s, onhist, onkey, ondone))
1063 | Some (c, text, onhist, onkey, ondone) ->
1064 begin match Char.unsafe_chr key with
1065 | '\r' | '\n' ->
1066 ondone text;
1067 state.textentry <- None;
1068 Glut.postRedisplay ()
1070 | '\027' ->
1071 state.textentry <- None;
1072 Glut.postRedisplay ()
1074 | _ ->
1075 begin match onkey text key with
1076 | TEdone text ->
1077 state.textentry <- None;
1078 ondone text;
1079 Glut.postRedisplay ()
1081 | TEcont text ->
1082 enttext (Some (c, text, onhist, onkey, ondone));
1084 | TEstop ->
1085 state.textentry <- None;
1086 Glut.postRedisplay ()
1088 | TEswitch te ->
1089 state.textentry <- Some te;
1090 Glut.postRedisplay ()
1091 end;
1092 end;
1095 let narrow outlines pattern =
1096 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1097 match reopt with
1098 | None -> None
1099 | Some re ->
1100 let rec fold accu n =
1101 if n = -1 then accu else
1102 let (s, _, _, _) as o = outlines.(n) in
1103 let accu =
1104 if (try ignore (Str.search_forward re s 0); true
1105 with Not_found -> false)
1106 then (o :: accu)
1107 else accu
1109 fold accu (n-1)
1111 let matched = fold [] (Array.length outlines - 1) in
1112 if matched = [] then None else Some (Array.of_list matched)
1115 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1116 let search active pattern incr =
1117 let dosearch re =
1118 let rec loop n =
1119 if n = Array.length outlines || n = -1 then None else
1120 let (s, _, _, _) = outlines.(n) in
1122 (try ignore (Str.search_forward re s 0); true
1123 with Not_found -> false)
1124 then Some n
1125 else loop (n + incr)
1127 loop active
1130 let re = Str.regexp_case_fold pattern in
1131 dosearch re
1132 with Failure s ->
1133 state.text <- s;
1134 None
1136 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1137 match key with
1138 | 27 ->
1139 if String.length qsearch = 0
1140 then (
1141 state.text <- "";
1142 state.outline <- None;
1143 Glut.postRedisplay ();
1145 else (
1146 state.text <- "";
1147 state.outline <- Some (allowdel, active, first, outlines, "");
1148 Glut.postRedisplay ();
1151 | 18 | 19 ->
1152 let incr = if key = 18 then -1 else 1 in
1153 let active, first =
1154 match search (active + incr) qsearch incr with
1155 | None ->
1156 state.text <- qsearch ^ " [not found]";
1157 active, first
1158 | Some active ->
1159 state.text <- qsearch;
1160 active, firstof active
1162 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1163 Glut.postRedisplay ();
1165 | 8 ->
1166 let len = String.length qsearch in
1167 if len = 0
1168 then ()
1169 else (
1170 if len = 1
1171 then (
1172 state.text <- "";
1173 state.outline <- Some (allowdel, active, first, outlines, "");
1175 else
1176 let qsearch = String.sub qsearch 0 (len - 1) in
1177 let active, first =
1178 match search active qsearch ~-1 with
1179 | None ->
1180 state.text <- qsearch ^ " [not found]";
1181 active, first
1182 | Some active ->
1183 state.text <- qsearch;
1184 active, firstof active
1186 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1188 Glut.postRedisplay ()
1190 | 13 ->
1191 if active < Array.length outlines
1192 then (
1193 let (_, _, n, t) = outlines.(active) in
1194 gotopage n t;
1196 state.text <- "";
1197 if allowdel then state.bookmarks <- Array.to_list outlines;
1198 state.outline <- None;
1199 Glut.postRedisplay ();
1201 | _ when key >= 32 && key < 127 ->
1202 let pattern = addchar qsearch (Char.chr key) in
1203 let active, first =
1204 match search active pattern 1 with
1205 | None ->
1206 state.text <- pattern ^ " [not found]";
1207 active, first
1208 | Some active ->
1209 state.text <- pattern;
1210 active, firstof active
1212 state.outline <- Some (allowdel, active, first, outlines, pattern);
1213 Glut.postRedisplay ()
1215 | 14 when not allowdel ->
1216 let optoutlines = narrow outlines qsearch in
1217 begin match optoutlines with
1218 | None -> state.text <- "can't narrow"
1219 | Some outlines ->
1220 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1221 match state.outlines with
1222 | Olist l -> ()
1223 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1224 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1225 end;
1226 Glut.postRedisplay ()
1228 | 21 when not allowdel ->
1229 let outline =
1230 match state.outlines with
1231 | Oarray a -> a
1232 | Olist l ->
1233 let a = Array.of_list (List.rev l) in
1234 state.outlines <- Oarray a;
1236 | Onarrow (a, b) ->
1237 state.outlines <- Oarray b;
1240 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1241 Glut.postRedisplay ()
1243 | 12 ->
1244 state.outline <-
1245 Some (allowdel, active, firstof active, outlines, qsearch);
1246 Glut.postRedisplay ()
1248 | 127 when allowdel ->
1249 let len = Array.length outlines - 1 in
1250 if len = 0
1251 then (
1252 state.outline <- None;
1253 state.bookmarks <- [];
1255 else (
1256 let bookmarks = Array.init len
1257 (fun i ->
1258 let i = if i >= active then i + 1 else i in
1259 outlines.(i)
1262 state.outline <-
1263 Some (allowdel,
1264 min active (len-1),
1265 min first (len-1),
1266 bookmarks, qsearch)
1269 Glut.postRedisplay ()
1271 | _ -> log "unknown key %d" key
1274 let keyboard ~key ~x ~y =
1275 if key = 7
1276 then
1277 wcmd "interrupt" []
1278 else
1279 match state.outline with
1280 | None -> viewkeyboard ~key ~x ~y
1281 | Some outline -> outlinekeyboard ~key ~x ~y outline
1284 let special ~key ~x ~y =
1285 match state.outline with
1286 | None ->
1287 begin match state.textentry with
1288 | None ->
1289 let y =
1290 match key with
1291 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1292 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1293 | Glut.KEY_DOWN -> clamp conf.scrollincr
1294 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1295 | Glut.KEY_PAGE_DOWN -> clamp state.h
1296 | Glut.KEY_HOME -> addnav (); 0
1297 | Glut.KEY_END ->
1298 addnav ();
1299 state.maxy - (if conf.maxhfit then state.h else 0)
1300 | _ -> state.y
1302 state.text <- "";
1303 gotoy y
1305 | Some (c, s, Some onhist, onkey, ondone) ->
1306 let s =
1307 match key with
1308 | Glut.KEY_UP -> onhist HCprev
1309 | Glut.KEY_DOWN -> onhist HCnext
1310 | Glut.KEY_HOME -> onhist HCfirst
1311 | Glut.KEY_END -> onhist HClast
1312 | _ -> state.text
1314 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1315 Glut.postRedisplay ()
1317 | _ -> ()
1320 | Some (allowdel, active, first, outlines, qsearch) ->
1321 let maxrows = maxoutlinerows () in
1322 let navigate incr =
1323 let active = active + incr in
1324 let active = max 0 (min active (Array.length outlines - 1)) in
1325 let first =
1326 if active > first
1327 then
1328 let rows = active - first in
1329 if rows > maxrows then active - maxrows else first
1330 else active
1332 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1333 Glut.postRedisplay ()
1335 match key with
1336 | Glut.KEY_UP -> navigate ~-1
1337 | Glut.KEY_DOWN -> navigate 1
1338 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1339 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1341 | Glut.KEY_HOME ->
1342 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1343 Glut.postRedisplay ()
1345 | Glut.KEY_END ->
1346 let active = Array.length outlines - 1 in
1347 let first = max 0 (active - maxrows) in
1348 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1349 Glut.postRedisplay ()
1351 | _ -> ()
1354 let drawplaceholder l =
1355 GlDraw.color (1.0, 1.0, 1.0);
1356 GlDraw.rect
1357 (0.0, float l.pagedispy)
1358 (float l.pagew, float (l.pagedispy + l.pagevh))
1360 let x = 0.0
1361 and y = float (l.pagedispy + 13) in
1362 let font = Glut.BITMAP_8_BY_13 in
1363 GlDraw.color (0.0, 0.0, 0.0);
1364 GlPix.raster_pos ~x ~y ();
1365 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1366 ("Loading " ^ string_of_int l.pageno);
1369 let now () = Unix.gettimeofday ();;
1371 let drawpage i l =
1372 begin match getopaque l.pageno with
1373 | Some opaque when validopaque opaque ->
1374 if state.textentry = None
1375 then GlDraw.color (1.0, 1.0, 1.0)
1376 else GlDraw.color (0.4, 0.4, 0.4);
1377 let a = now () in
1378 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1379 let b = now () in
1380 let d = b-.a in
1381 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1382 vlog "draw %f sec" d;
1384 | Some _ ->
1385 drawplaceholder l
1387 | None ->
1388 drawplaceholder l;
1389 if state.inflight < cblen state.pagecache
1390 then (
1391 List.iter preload state.layout;
1393 else (
1394 vlog "inflight %d" state.inflight;
1396 end;
1397 GlDraw.color (0.5, 0.5, 0.5);
1398 GlDraw.rect
1399 (0., float i)
1400 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1402 l.pagedispy + l.pagevh;
1405 let scrollindicator () =
1406 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1407 GlDraw.color (0.64 , 0.64, 0.64);
1408 GlDraw.rect
1409 (float (state.w - conf.scrollw), 0.)
1410 (float state.w, float state.h)
1412 GlDraw.color (0.0, 0.0, 0.0);
1413 let sh = (float (maxy + state.h) /. float state.h) in
1414 let sh = float state.h /. sh in
1415 let sh = max sh (float conf.scrollh) in
1417 let percent =
1418 if state.y = state.maxy
1419 then 1.0
1420 else float state.y /. float maxy
1422 let position = (float state.h -. sh) *. percent in
1424 let position =
1425 if position +. sh > float state.h
1426 then
1427 float state.h -. sh
1428 else
1429 position
1431 GlDraw.rect
1432 (float (state.w - conf.scrollw), position)
1433 (float state.w, position +. sh)
1437 let showsel () =
1438 match state.mstate with
1439 | Mnone ->
1442 | Msel ((x0, y0), (x1, y1)) ->
1443 let y0' = min y0 y1
1444 and y1 = max y0 y1 in
1445 let y0 = y0' in
1446 let f l =
1447 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1448 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1449 then
1450 match getopaque l.pageno with
1451 | Some opaque when validopaque opaque ->
1452 let oy = -l.pagey + l.pagedispy in
1453 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1454 | _ -> ()
1456 List.iter f state.layout
1459 let showrects () =
1460 Gl.enable `blend;
1461 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1462 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1463 List.iter
1464 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1465 List.iter (fun l ->
1466 if l.pageno = pageno
1467 then (
1468 let d = float (l.pagedispy - l.pagey) in
1469 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1470 GlDraw.begins `quads;
1472 GlDraw.vertex2 (x0, y0+.d);
1473 GlDraw.vertex2 (x1, y1+.d);
1474 GlDraw.vertex2 (x2, y2+.d);
1475 GlDraw.vertex2 (x3, y3+.d);
1477 GlDraw.ends ();
1478 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1480 ) state.layout
1481 ) state.rects
1483 Gl.disable `blend;
1486 let showoutline = function
1487 | None -> ()
1488 | Some (allowdel, active, first, outlines, qsearch) ->
1489 Gl.enable `blend;
1490 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1491 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1492 GlDraw.rect (0., 0.) (float state.w, float state.h);
1493 Gl.disable `blend;
1495 GlDraw.color (1., 1., 1.);
1496 let font = Glut.BITMAP_9_BY_15 in
1497 let draw_string x y s =
1498 GlPix.raster_pos ~x ~y ();
1499 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1501 let rec loop row =
1502 if row = Array.length outlines || (row - first) * 16 > state.h
1503 then ()
1504 else (
1505 let (s, l, _, _) = outlines.(row) in
1506 let y = (row - first) * 16 in
1507 let x = 5 + 15*l in
1508 if row = active
1509 then (
1510 Gl.enable `blend;
1511 GlDraw.polygon_mode `both `line;
1512 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1513 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1514 GlDraw.rect (0., float (y + 1))
1515 (float (state.w - conf.scrollw - 1), float (y + 18));
1516 GlDraw.polygon_mode `both `fill;
1517 Gl.disable `blend;
1518 GlDraw.color (1., 1., 1.);
1520 draw_string (float x) (float (y + 16)) s;
1521 loop (row+1)
1524 loop first
1527 let display () =
1528 let lasty = List.fold_left drawpage 0 (state.layout) in
1529 GlDraw.color (0.5, 0.5, 0.5);
1530 GlDraw.rect
1531 (0., float lasty)
1532 (float (state.w - conf.scrollw), float state.h)
1534 showrects ();
1535 scrollindicator ();
1536 showsel ();
1537 showoutline state.outline;
1538 enttext ();
1539 Glut.swapBuffers ();
1542 let getlink x y =
1543 let rec f = function
1544 | l :: rest ->
1545 begin match getopaque l.pageno with
1546 | Some opaque when validopaque opaque ->
1547 let y = y - l.pagedispy in
1548 if y > 0
1549 then
1550 let y = l.pagey + y in
1551 match getlink opaque x y with
1552 | LNone -> f rest
1553 | link -> link
1554 else
1555 f rest
1556 | _ ->
1557 f rest
1559 | [] -> LNone
1561 f state.layout
1564 let mouse ~button ~bstate ~x ~y =
1565 match button with
1566 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1567 let incr =
1568 if n = 3
1569 then
1570 -conf.scrollincr
1571 else
1572 conf.scrollincr
1574 let incr = incr * 2 in
1575 let y = clamp incr in
1576 gotoy y
1578 | Glut.LEFT_BUTTON when state.outline = None ->
1579 let dest = if bstate = Glut.DOWN then getlink x y else LNone in
1580 begin match dest with
1581 | LGoto (pageno, top) ->
1582 if pageno >= 0
1583 then
1584 gotopage pageno top
1586 | LUri s ->
1587 print_endline s
1589 | LNone ->
1590 if bstate = Glut.DOWN
1591 then (
1592 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1593 state.mstate <- Msel ((x, y), (x, y));
1594 Glut.postRedisplay ()
1596 else (
1597 Glut.setCursor Glut.CURSOR_INHERIT;
1598 state.mstate <- Mnone;
1602 | _ ->
1605 let mouse ~button ~state ~x ~y = mouse button state x y;;
1607 let motion ~x ~y =
1608 if state.outline = None
1609 then
1610 match state.mstate with
1611 | Mnone -> ()
1612 | Msel (a, _) ->
1613 state.mstate <- Msel (a, (x, y));
1614 Glut.postRedisplay ()
1617 let pmotion ~x ~y =
1618 if state.outline = None
1619 then
1620 match state.mstate with
1621 | Mnone ->
1622 if getlink x y != LNone
1623 then Glut.setCursor Glut.CURSOR_INFO
1624 else Glut.setCursor Glut.CURSOR_INHERIT
1626 | Msel (a, _) ->
1630 let () =
1631 let statepath =
1632 let home =
1633 if Sys.os_type = "Win32"
1634 then
1635 try Sys.getenv "HOMEPATH" with Not_found -> ""
1636 else
1637 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1639 Filename.concat home "llpp"
1641 let pstate =
1643 let ic = open_in_bin statepath in
1644 let hash = input_value ic in
1645 close_in ic;
1646 hash
1647 with exn ->
1648 if false
1649 then
1650 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1652 Hashtbl.create 1
1654 let savestate () =
1656 let w, h =
1657 match state.fullscreen with
1658 | None -> state.w, state.h
1659 | Some wh -> wh
1661 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1662 let oc = open_out_bin statepath in
1663 output_value oc pstate
1664 with exn ->
1665 if false
1666 then
1667 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1670 let setstate () =
1672 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1673 state.w <- statew;
1674 state.h <- stateh;
1675 state.bookmarks <- statebookmarks;
1676 with Not_found -> ()
1677 | exn ->
1678 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1681 Arg.parse [] (fun s -> state.path <- s) "options:";
1682 let name =
1683 if String.length state.path = 0
1684 then (prerr_endline "filename missing"; exit 1)
1685 else state.path
1688 setstate ();
1689 let _ = Glut.init Sys.argv in
1690 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1691 let () = Glut.initWindowSize state.w state.h in
1692 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1694 let csock, ssock =
1695 if Sys.os_type = "Unix"
1696 then
1697 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1698 else
1699 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1700 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1701 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1702 Unix.bind sock addr;
1703 Unix.listen sock 1;
1704 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1705 Unix.connect csock addr;
1706 let ssock, _ = Unix.accept sock in
1707 Unix.close sock;
1708 let opts sock =
1709 Unix.setsockopt sock Unix.TCP_NODELAY true;
1710 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1712 opts ssock;
1713 opts csock;
1714 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1715 ssock, csock
1718 let () = Glut.displayFunc display in
1719 let () = Glut.reshapeFunc reshape in
1720 let () = Glut.keyboardFunc keyboard in
1721 let () = Glut.specialFunc special in
1722 let () = Glut.idleFunc (Some idle) in
1723 let () = Glut.mouseFunc mouse in
1724 let () = Glut.motionFunc motion in
1725 let () = Glut.passiveMotionFunc pmotion in
1727 init ssock;
1728 state.csock <- csock;
1729 state.ssock <- ssock;
1730 writecmd csock ("open " ^ name ^ "\000");
1732 at_exit savestate;
1734 let rec handlelablglutbug () =
1736 Glut.mainLoop ();
1737 with Glut.BadEnum "key in special_of_int" ->
1738 showtext '!' " LablGlut bug: special key not recognized";
1739 Glut.swapBuffers ();
1740 handlelablglutbug ()
1742 handlelablglutbug ();