Simplify outline centering and add a key to center on demand
[llpp.git] / main.ml
blob38c29caeb98fe89f784a336622de43e74bc9713f
1 let log fmt = Printf.kprintf prerr_endline fmt;;
2 let dolog fmt = Printf.kprintf prerr_endline fmt;;
4 external init : Unix.file_descr -> unit = "ml_init";;
5 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
6 external gettext : string -> (int * int * int * int) -> int -> bool -> unit =
7 "ml_gettext";;
8 external checklink : string -> int -> int -> bool = "ml_checklink";;
9 external getlink : string -> int -> int -> (int * int) option = "ml_getlink";;
10 external getpagewh : int -> float array = "ml_getpagewh";;
12 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
14 type 'a circbuf =
15 { store : 'a array
16 ; mutable rc : int
17 ; mutable wc : int
18 ; mutable len : int
22 type textentry = (char * string * onhist option * onkey * ondone)
23 and onkey = string -> int -> te
24 and ondone = string -> unit
25 and onhist = histcmd -> string
26 and histcmd = HCnext | HCprev | HCfirst | HClast
27 and te =
28 | TEstop
29 | TEdone of string
30 | TEcont of string
31 | TEswitch of textentry
34 let cbnew n v =
35 { store = Array.create n v
36 ; rc = 0
37 ; wc = 0
38 ; len = 0
42 let cblen b = Array.length b.store;;
44 let cbput b v =
45 let len = cblen b in
46 b.store.(b.wc) <- v;
47 b.wc <- (b.wc + 1) mod len;
48 b.len <- min (b.len + 1) len;
51 let cbpeekw b = b.store.(b.wc);;
53 let cbget b dir =
54 if b.len = 0 then b.store.(0) else
55 let rc = b.rc + dir in
56 let rc = if rc = -1 then b.len - 1 else rc in
57 let rc = if rc = b.len then 0 else rc in
58 b.rc <- rc;
59 b.store.(rc);
62 let cbrfollowlen b =
63 b.rc <- b.len;
66 type layout =
67 { pageno : int
68 ; pagedimno : int
69 ; pagew : int
70 ; pageh : int
71 ; pagedispy : int
72 ; pagey : int
73 ; pagevh : int
77 type conf =
78 { mutable scrollw : int
79 ; mutable scrollh : int
80 ; mutable rectsel : bool
81 ; mutable icase : bool
82 ; mutable preload : bool
83 ; mutable pagebias : int
84 ; mutable redispimm : bool
85 ; mutable verbose : bool
86 ; mutable scrollincr : int
87 ; mutable maxhfit : bool
88 ; mutable crophack : bool
89 ; mutable autoscroll : bool
90 ; mutable showall : bool
94 type outline = string * int * int * int;;
95 type outlines =
96 | Oarray of outline array
97 | Olist of outline list
98 | Onarrow of outline array * outline array
101 type state =
102 { mutable csock : Unix.file_descr
103 ; mutable ssock : Unix.file_descr
104 ; mutable w : int
105 ; mutable h : int
106 ; mutable rotate : int
107 ; mutable y : int
108 ; mutable ty : int
109 ; mutable prevy : int
110 ; mutable maxy : int
111 ; mutable layout : layout list
112 ; pagemap : ((int * int * int), string) Hashtbl.t
113 ; mutable pages : (int * int * int) list
114 ; mutable pagecount : int
115 ; pagecache : string circbuf
116 ; mutable inflight : int
117 ; mutable mstate : mstate
118 ; mutable searchpattern : string
119 ; mutable rects : (int * int * Gl.point2 * Gl.point2) list
120 ; mutable rects1 : (int * int * Gl.point2 * Gl.point2) list
121 ; mutable text : string
122 ; mutable fullscreen : (int * int) option
123 ; mutable textentry : textentry option
124 ; mutable outlines : outlines
125 ; mutable outline : (bool * int * int * outline array * string) option
126 ; mutable bookmarks : outline list
127 ; mutable path : string
128 ; hists : hists
130 and hists =
131 { pat : string circbuf
132 ; pag : string circbuf
133 ; nav : float circbuf
137 let conf =
138 { scrollw = 5
139 ; scrollh = 12
140 ; icase = true
141 ; rectsel = true
142 ; preload = false
143 ; pagebias = 0
144 ; redispimm = false
145 ; verbose = false
146 ; scrollincr = 24
147 ; maxhfit = true
148 ; crophack = false
149 ; autoscroll = false
150 ; showall = false
154 let state =
155 { csock = Unix.stdin
156 ; ssock = Unix.stdin
157 ; w = 900
158 ; h = 900
159 ; rotate = 0
160 ; y = 0
161 ; ty = 0
162 ; prevy = 0
163 ; layout = []
164 ; maxy = max_int
165 ; pagemap = Hashtbl.create 10
166 ; pagecache = cbnew 10 ""
167 ; pages = []
168 ; pagecount = 0
169 ; inflight = 0
170 ; mstate = Mnone
171 ; rects = []
172 ; rects1 = []
173 ; text = ""
174 ; fullscreen = None
175 ; textentry = None
176 ; searchpattern = ""
177 ; outlines = Olist []
178 ; outline = None
179 ; bookmarks = []
180 ; path = ""
181 ; hists =
182 { nav = cbnew 100 0.0
183 ; pat = cbnew 20 ""
184 ; pag = cbnew 10 ""
189 let vlog fmt =
190 if conf.verbose
191 then
192 Printf.kprintf prerr_endline fmt
193 else
194 Printf.kprintf ignore fmt
197 let writecmd fd s =
198 let len = String.length s in
199 let n = 4 + len in
200 let b = Buffer.create n in
201 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
202 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
203 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
204 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
205 Buffer.add_string b s;
206 let s' = Buffer.contents b in
207 let n' = Unix.write fd s' 0 n in
208 if n' != n then failwith "write failed";
211 let readcmd fd =
212 let s = "xxxx" in
213 let n = Unix.read fd s 0 4 in
214 if n != 4 then failwith "incomplete read(len)";
215 let len = 0
216 lor (Char.code s.[0] lsl 24)
217 lor (Char.code s.[1] lsl 16)
218 lor (Char.code s.[2] lsl 8)
219 lor (Char.code s.[3] lsl 0)
221 let s = String.create len in
222 let n = Unix.read fd s 0 len in
223 if n != len then failwith "incomplete read(data)";
227 let yratio y =
228 if y = state.maxy then 1.0
229 else float y /. float state.maxy
232 let makecmd s l =
233 let b = Buffer.create 10 in
234 Buffer.add_string b s;
235 let rec combine = function
236 | [] -> b
237 | x :: xs ->
238 Buffer.add_char b ' ';
239 let s =
240 match x with
241 | `b b -> if b then "1" else "0"
242 | `s s -> s
243 | `i i -> string_of_int i
244 | `f f -> string_of_float f
245 | `I f -> string_of_int (truncate f)
247 Buffer.add_string b s;
248 combine xs;
250 combine l;
253 let wcmd s l =
254 let cmd = Buffer.contents (makecmd s l) in
255 writecmd state.csock cmd;
258 let calcheight () =
259 let rec f pn ph fh l =
260 match l with
261 | (n, _, h) :: rest ->
262 let fh = fh + (n - pn) * ph in
263 f n h fh rest
265 | [] ->
266 let fh = fh + (ph * (state.pagecount - pn)) in
267 max 0 fh
269 let fh = f 0 0 0 state.pages in
273 let getpagey pageno =
274 let rec f pn ph y l =
275 match l with
276 | (n, _, h) :: rest ->
277 if n >= pageno
278 then
279 y + (pageno - pn) * ph
280 else
281 let y = y + (n - pn) * ph in
282 f n h y rest
284 | [] ->
285 y + (pageno - pn) * ph
287 f 0 0 0 state.pages;
290 let layout y sh =
291 let rec f pageno pdimno prev vy py dy l cacheleft accu =
292 if pageno = state.pagecount || cacheleft = 0
293 then accu
294 else
295 let ((_, w, h) as curr), rest, pdimno =
296 match l with
297 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
298 curr, rest, pdimno + 1
299 | _ ->
300 prev, l, pdimno
302 let pageno' = pageno + 1 in
303 if py + h > vy
304 then
305 let py' = vy - py in
306 let vh = h - py' in
307 if dy + vh > sh
308 then
309 let vh = sh - dy in
310 if vh <= 0
311 then
312 accu
313 else
314 let e =
315 { pageno = pageno
316 ; pagedimno = pdimno
317 ; pagew = w
318 ; pageh = h
319 ; pagedispy = dy
320 ; pagey = py'
321 ; pagevh = vh
324 e :: accu
325 else
326 let e =
327 { pageno = pageno
328 ; pagedimno = pdimno
329 ; pagew = w
330 ; pageh = h
331 ; pagedispy = dy
332 ; pagey = py'
333 ; pagevh = vh
336 let accu = e :: accu in
337 f pageno' pdimno curr
338 (vy + vh) (py + h) (dy + vh + 2) rest
339 (pred cacheleft) accu
340 else
341 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
343 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
344 state.maxy <- calcheight ();
345 List.rev accu
348 let clamp incr =
349 let y = state.y + incr in
350 let y = max 0 y in
351 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
355 let getopaque pageno =
356 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
357 state.rotate))
358 with Not_found -> None
361 let cache pageno opaque =
362 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
363 state.rotate) opaque
366 let validopaque opaque = String.length opaque > 0;;
368 let preload l =
369 match getopaque l.pageno with
370 | None when state.inflight < 2+0*(cblen state.pagecache) ->
371 state.inflight <- succ state.inflight;
372 cache l.pageno "";
373 wcmd "render" [`i (l.pageno + 1)
374 ;`i l.pagedimno
375 ;`i l.pagew
376 ;`i l.pageh];
378 | _ -> ()
381 let gotoy y =
382 let y = max 0 y in
383 let y = min state.maxy y in
384 let pages = layout y state.h in
385 let rec f all = function
386 | l :: ls ->
387 begin match getopaque l.pageno with
388 | None -> preload l; f false ls
389 | Some opaque -> f (all && validopaque opaque) ls
391 | [] -> all
393 if not conf.showall || f true pages
394 then (
395 state.y <- y;
396 state.layout <- pages;
398 state.ty <- y;
399 if conf.redispimm
400 then
401 Glut.postRedisplay ()
405 let addnav () =
406 cbput state.hists.nav (yratio state.y);
407 cbrfollowlen state.hists.nav;
410 let getnav () =
411 let y = cbget state.hists.nav ~-1 in
412 truncate (y *. float state.maxy)
415 let gotopage n top =
416 let y = getpagey n in
417 addnav ();
418 gotoy (y + top);
421 let reshape ~w ~h =
422 let ratio = float w /. float state.w in
423 let fixbookmark (s, l, pageno, pagey) =
424 let pagey = truncate (float pagey *. ratio) in
425 (s, l, pageno, pagey)
427 state.bookmarks <- List.map fixbookmark state.bookmarks;
428 state.w <- w;
429 state.h <- h;
430 GlDraw.viewport 0 0 w h;
431 GlMat.mode `modelview;
432 GlMat.load_identity ();
433 GlMat.mode `projection;
434 GlMat.load_identity ();
435 GlMat.rotate ~x:1.0 ~angle:180.0 ();
436 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
437 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
438 GlClear.color (1., 1., 1.);
439 GlClear.clear [`color];
440 state.layout <- [];
441 state.pages <- [];
442 state.rects <- [];
443 state.text <- "";
444 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
447 let showtext c s =
448 GlDraw.color (0.0, 0.0, 0.0);
449 GlDraw.rect
450 (0.0, float (state.h - 18))
451 (float (state.w - conf.scrollw - 1), float state.h)
453 let font = Glut.BITMAP_8_BY_13 in
454 GlDraw.color (1.0, 1.0, 1.0);
455 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
456 Glut.bitmapCharacter ~font ~c:(Char.code c);
457 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
460 let enttext () =
461 let len = String.length state.text in
462 match state.textentry with
463 | None ->
464 if len > 0 then showtext ' ' state.text
466 | Some (c, text, _, _, _) ->
467 let s =
468 if len > 0
469 then
470 text ^ " [" ^ state.text ^ "]"
471 else
472 text
474 showtext c s;
477 let act cmd =
478 match cmd.[0] with
479 | 'c' ->
480 state.pages <- [];
481 state.outlines <- Olist []
483 | 'D' ->
484 state.rects <- state.rects1;
485 Glut.postRedisplay ()
487 | 'd' ->
488 state.rects <- state.rects1;
489 Glut.postRedisplay ()
491 | 'C' ->
492 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
493 state.pagecount <- n;
494 let rely = yratio state.y in
495 let maxy = calcheight () in
496 state.y <- truncate (float maxy *. rely);
497 state.ty <- state.y;
498 let pages = layout state.y state.h in
499 state.layout <- pages;
500 Glut.postRedisplay ();
502 | 't' ->
503 let s = Scanf.sscanf cmd "t %n"
504 (fun n -> String.sub cmd n (String.length cmd - n))
506 Glut.setWindowTitle s
508 | 'T' ->
509 let s = Scanf.sscanf cmd "T %n"
510 (fun n -> String.sub cmd n (String.length cmd - n))
512 if state.textentry = None
513 then (
514 state.text <- s;
515 showtext ' ' s;
516 Glut.swapBuffers ();
518 else (
519 state.text <- s;
520 Glut.postRedisplay ();
523 | 'V' ->
524 if conf.verbose
525 then
526 let s = Scanf.sscanf cmd "V %n"
527 (fun n -> String.sub cmd n (String.length cmd - n))
529 state.text <- s;
530 showtext ' ' s;
531 Glut.swapBuffers ();
533 | 'F' ->
534 let pageno, c, x0, y0, x1, y1 =
535 Scanf.sscanf cmd "F %d %d %f %f %f %f"
536 (fun p c x0 y0 x1 y1 -> (p, c, x0, y0, x1, y1))
538 let y = (getpagey pageno) + truncate y0 in
539 addnav ();
540 gotoy y;
541 state.rects1 <- [pageno, c, (x0, y0), (x1, y1)]
543 | 'R' ->
544 let pageno, c, x0, y0, x1, y1 =
545 Scanf.sscanf cmd "R %d %d %f %f %f %f"
546 (fun pageno c x0 y0 x1 y1 -> (pageno, c, x0, y0, x1, y1))
548 state.rects1 <- (pageno, c, (x0, y0), (x1, y1)) :: state.rects1
550 | 'r' ->
551 let n, w, h, r, p =
552 Scanf.sscanf cmd "r %d %d %d %d %s"
553 (fun n w h r p -> (n, w, h, r, p))
555 Hashtbl.replace state.pagemap (n, w, r) p;
556 let evicted = cbpeekw state.pagecache in
557 if String.length evicted > 0
558 then begin
559 wcmd "free" [`s evicted];
560 let l = Hashtbl.fold (fun k p a ->
561 if evicted = p then k :: a else a) state.pagemap []
563 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
564 end;
565 cbput state.pagecache p;
566 state.inflight <- pred state.inflight;
567 if conf.showall then gotoy state.ty;
568 Glut.postRedisplay ()
570 | 'l' ->
571 let (n, w, h) as pagelayout =
572 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
574 state.pages <- pagelayout :: state.pages
576 | 'o' ->
577 let (l, n, t, pos) =
578 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
580 let s = String.sub cmd pos (String.length cmd - pos) in
581 let outline = (s, l, n, t) in
582 let outlines =
583 match state.outlines with
584 | Olist outlines -> Olist (outline :: outlines)
585 | Oarray _ -> Olist [outline]
586 | Onarrow _ -> Olist [outline]
588 state.outlines <- outlines
590 | _ ->
591 log "unknown cmd `%S'" cmd
594 let idle () =
595 if not conf.redispimm && state.y != state.prevy
596 then (
597 state.prevy <- state.y;
598 Glut.postRedisplay ();
600 else
601 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
603 begin match r with
604 | [] ->
605 if conf.preload then begin
606 let h = state.h in
607 let y = if state.y < state.h then 0 else state.y - state.h in
608 let pages = layout y (h*3) in
609 List.iter preload pages;
610 end;
611 if conf.autoscroll then begin
612 let y = state.y + conf.scrollincr in
613 let y = if y >= state.maxy then 0 else y in
614 gotoy y;
615 state.text <- "";
616 state.prevy <- state.y;
617 Glut.postRedisplay ();
618 end;
620 | _ ->
621 let cmd = readcmd state.csock in
622 act cmd;
623 end;
626 let onhist cb = function
627 | HCprev -> cbget cb ~-1
628 | HCnext -> cbget cb 1
629 | HCfirst -> cbget cb ~-(cb.rc)
630 | HClast -> cbget cb (cb.len - 1 - cb.rc)
633 let search pattern forward =
634 if String.length pattern > 0
635 then
636 let pn, py =
637 match state.layout with
638 | [] -> 0, 0
639 | l :: _ ->
640 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
642 let cmd =
643 let b = makecmd "search"
644 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
646 Buffer.add_char b ',';
647 Buffer.add_string b pattern;
648 Buffer.add_char b '\000';
649 Buffer.contents b;
651 writecmd state.csock cmd;
654 let intentry text key =
655 let c = Char.unsafe_chr key in
656 match c with
657 | '0' .. '9' ->
658 let s = "x" in s.[0] <- c;
659 let text = text ^ s in
660 TEcont text
662 | _ ->
663 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
664 TEcont text
667 let addchar s c =
668 let b = Buffer.create (String.length s + 1) in
669 Buffer.add_string b s;
670 Buffer.add_char b c;
671 Buffer.contents b;
674 let textentry text key =
675 let c = Char.unsafe_chr key in
676 match c with
677 | _ when key >= 32 && key < 127 ->
678 let text = addchar text c in
679 TEcont text
681 | _ ->
682 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
683 TEcont text
686 let optentry text key =
687 let btos b = if b then "on" else "off" in
688 let c = Char.unsafe_chr key in
689 match c with
690 | 'r' ->
691 conf.rectsel <- not conf.rectsel;
692 TEdone ("rectsel " ^ (btos conf.rectsel))
694 | 's' ->
695 let ondone s =
696 try conf.scrollincr <- int_of_string s with exc ->
697 state.text <- Printf.sprintf "bad integer `%s': %s"
698 s (Printexc.to_string exc)
700 TEswitch ('#', "", None, intentry, ondone)
702 | 'R' ->
703 let ondone s =
705 state.rotate <- int_of_string s;
706 wcmd "rotate" [`i state.rotate]
707 with exc ->
708 state.text <- Printf.sprintf "bad integer `%s': %s"
709 s (Printexc.to_string exc)
711 TEswitch ('^', "", None, intentry, ondone)
713 | 'i' ->
714 conf.icase <- not conf.icase;
715 TEdone ("case insensitive search " ^ (btos conf.icase))
717 | 'p' ->
718 conf.preload <- not conf.preload;
719 TEdone ("preload " ^ (btos conf.preload))
721 | 'd' ->
722 conf.redispimm <- not conf.redispimm;
723 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
725 | 'v' ->
726 conf.verbose <- not conf.verbose;
727 TEdone ("verbose " ^ (btos conf.verbose))
729 | 'h' ->
730 conf.maxhfit <- not conf.maxhfit;
731 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
732 TEdone ("maxhfit " ^ (btos conf.maxhfit))
734 | 'c' ->
735 conf.crophack <- not conf.crophack;
736 TEdone ("crophack " ^ btos conf.crophack)
738 | 'a' ->
739 conf.showall <- not conf.showall;
740 TEdone ("showall " ^ btos conf.showall)
742 | _ ->
743 state.text <- Printf.sprintf "bad option %d `%c'" key c;
744 TEstop
747 let maxoutlinerows () = (state.h - 31) / 16;;
749 let enterselector allowdel outlines errmsg =
750 if Array.length outlines = 0
751 then (
752 showtext ' ' errmsg;
753 Glut.swapBuffers ()
755 else
756 let pageno =
757 match state.layout with
758 | [] -> -1
759 | {pageno=pageno} :: rest -> pageno
761 let active =
762 let rec loop n =
763 if n = Array.length outlines
764 then 0
765 else
766 let (_, _, outlinepageno, _) = outlines.(n) in
767 if outlinepageno >= pageno then n else loop (n+1)
769 loop 0
771 state.outline <-
772 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
773 Glut.postRedisplay ();
776 let enteroutlinemode () =
777 let outlines =
778 match state.outlines with
779 | Oarray a -> a
780 | Olist l ->
781 let a = Array.of_list (List.rev l) in
782 state.outlines <- Oarray a;
784 | Onarrow (a, b) -> a
786 enterselector false outlines "Documents has no outline";
789 let enterbookmarkmode () =
790 let bookmarks = Array.of_list state.bookmarks in
791 enterselector true bookmarks "Documents has no bookmarks (yet)";
795 let quickbookmark ?title () =
796 match state.layout with
797 | [] -> ()
798 | l :: _ ->
799 let title =
800 match title with
801 | None ->
802 let sec = Unix.gettimeofday () in
803 let tm = Unix.localtime sec in
804 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
805 l.pageno
806 tm.Unix.tm_mday
807 tm.Unix.tm_mon
808 (tm.Unix.tm_year + 1900)
809 tm.Unix.tm_hour
810 tm.Unix.tm_min
811 | Some title -> title
813 state.bookmarks <-
814 (title, 0, l.pageno, l.pagey) :: state.bookmarks
817 let viewkeyboard ~key ~x ~y =
818 let enttext te =
819 state.textentry <- te;
820 state.text <- "";
821 enttext ();
822 Glut.postRedisplay ()
824 match state.textentry with
825 | None ->
826 let c = Char.chr key in
827 begin match c with
828 | '\027' | 'q' ->
829 exit 0
831 | '\008' ->
832 let y = getnav () in
833 gotoy y
835 | 'o' ->
836 enteroutlinemode ()
838 | 'u' ->
839 state.rects <- [];
840 state.text <- "";
841 Glut.postRedisplay ()
843 | '/' | '?' ->
844 let ondone isforw s =
845 cbput state.hists.pat s;
846 cbrfollowlen state.hists.pat;
847 state.searchpattern <- s;
848 search s isforw
850 enttext (Some (c, "", Some (onhist state.hists.pat),
851 textentry, ondone (c ='/')))
853 | '+' ->
854 let ondone s =
855 let n =
856 try int_of_string s with exc ->
857 state.text <- Printf.sprintf "bad integer `%s': %s"
858 s (Printexc.to_string exc);
859 max_int
861 if n != max_int
862 then (
863 conf.pagebias <- n;
864 state.text <- "page bias is now " ^ string_of_int n;
867 enttext (Some ('+', "", None, intentry, ondone))
869 | '-' ->
870 let ondone msg =
871 state.text <- msg;
873 enttext (Some ('-', "", None, optentry, ondone))
875 | '0' .. '9' ->
876 let ondone s =
877 let n =
878 try int_of_string s with exc ->
879 state.text <- Printf.sprintf "bad integer `%s': %s"
880 s (Printexc.to_string exc);
883 if n >= 0
884 then (
885 addnav ();
886 cbput state.hists.pag (string_of_int n);
887 cbrfollowlen state.hists.pag;
888 gotoy (getpagey (n + conf.pagebias - 1))
891 let pageentry text key =
892 match Char.unsafe_chr key with
893 | 'g' -> TEdone text
894 | _ -> intentry text key
896 let text = "x" in text.[0] <- c;
897 enttext (Some (':', text, Some (onhist state.hists.pag),
898 pageentry, ondone))
900 | 'b' ->
901 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
902 reshape state.w state.h;
904 | 'a' ->
905 conf.autoscroll <- not conf.autoscroll
907 | 'f' ->
908 begin match state.fullscreen with
909 | None ->
910 state.fullscreen <- Some (state.w, state.h);
911 Glut.fullScreen ()
912 | Some (w, h) ->
913 state.fullscreen <- None;
914 Glut.reshapeWindow ~w ~h
917 | 'g' ->
918 gotoy 0
920 | 'n' ->
921 search state.searchpattern true
923 | 'p' | 'N' ->
924 search state.searchpattern false
926 | 't' ->
927 begin match state.layout with
928 | [] -> ()
929 | l :: _ ->
930 gotoy (state.y - l.pagey);
933 | ' ' ->
934 begin match List.rev state.layout with
935 | [] -> ()
936 | l :: _ ->
937 gotoy (clamp (l.pageh - l.pagey))
940 | '\127' ->
941 begin match state.layout with
942 | [] -> ()
943 | l :: _ ->
944 gotoy (clamp (-l.pageh));
947 | '=' ->
948 let f (fn, ln) l =
949 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
951 let fn, ln = List.fold_left f (-1, -1) state.layout in
952 let s =
953 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
954 let percent =
955 if maxy <= 0
956 then 100.
957 else (100. *. (float state.y /. float maxy)) in
958 if fn = ln
959 then
960 Printf.sprintf "Page %d of %d %.2f%%"
961 (fn+1) state.pagecount percent
962 else
963 Printf.sprintf
964 "Pages %d-%d of %d %.2f%%"
965 (fn+1) (ln+1) state.pagecount percent
967 showtext ' ' s;
968 Glut.swapBuffers ()
970 | 'w' ->
971 begin match state.layout with
972 | [] -> ()
973 | l :: _ ->
974 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
975 Glut.postRedisplay ();
978 | '\'' ->
979 enterbookmarkmode ()
981 | 'm' ->
982 let ondone s =
983 match state.layout with
984 | l :: _ ->
985 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
986 | _ -> ()
988 enttext (Some ('~', "", None, textentry, ondone))
990 | '~' ->
991 quickbookmark ();
992 showtext ' ' "Quick bookmark added";
993 Glut.swapBuffers ()
995 | 'z' ->
996 begin match state.layout with
997 | l :: _ ->
998 let a = getpagewh l.pagedimno in
999 let w, h =
1000 if conf.crophack
1001 then
1002 (truncate (1.8 *. (a.(1) -. a.(0))),
1003 truncate (1.4 *. (a.(3) -. a.(0))))
1004 else
1005 (truncate (a.(1) -. a.(0)),
1006 truncate (a.(3) -. a.(0)))
1008 Glut.reshapeWindow (w + conf.scrollw) h;
1009 Glut.postRedisplay ();
1011 | [] -> ()
1014 | '<' | '>' ->
1015 state.rotate <- state.rotate + (if c = '>' then 30 else -30);
1016 wcmd "rotate" [`i state.rotate]
1018 | _ ->
1019 vlog "huh? %d %c" key (Char.chr key);
1022 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1023 let len = String.length text in
1024 if len = 0
1025 then (
1026 state.textentry <- None;
1027 Glut.postRedisplay ();
1029 else (
1030 let s = String.sub text 0 (len - 1) in
1031 enttext (Some (c, s, onhist, onkey, ondone))
1034 | Some (c, text, onhist, onkey, ondone) ->
1035 begin match Char.unsafe_chr key with
1036 | '\r' | '\n' ->
1037 ondone text;
1038 state.textentry <- None;
1039 Glut.postRedisplay ()
1041 | '\027' ->
1042 state.textentry <- None;
1043 Glut.postRedisplay ()
1045 | _ ->
1046 begin match onkey text key with
1047 | TEdone text ->
1048 state.textentry <- None;
1049 ondone text;
1050 Glut.postRedisplay ()
1052 | TEcont text ->
1053 enttext (Some (c, text, onhist, onkey, ondone));
1055 | TEstop ->
1056 state.textentry <- None;
1057 Glut.postRedisplay ()
1059 | TEswitch te ->
1060 state.textentry <- Some te;
1061 Glut.postRedisplay ()
1062 end;
1063 end;
1066 let narrow outlines pattern =
1067 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1068 match reopt with
1069 | None -> None
1070 | Some re ->
1071 let rec fold accu n =
1072 if n = -1 then accu else
1073 let (s, _, _, _) as o = outlines.(n) in
1074 let accu =
1075 if (try ignore (Str.search_forward re s 0); true
1076 with Not_found -> false)
1077 then (o :: accu)
1078 else accu
1080 fold accu (n-1)
1082 let matched = fold [] (Array.length outlines - 1) in
1083 if matched = [] then None else Some (Array.of_list matched)
1086 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1087 let search active pattern incr =
1088 let dosearch re =
1089 let rec loop n =
1090 if n = Array.length outlines || n = -1 then None else
1091 let (s, _, _, _) = outlines.(n) in
1093 (try ignore (Str.search_forward re s 0); true
1094 with Not_found -> false)
1095 then Some n
1096 else loop (n + incr)
1098 loop active
1101 let re = Str.regexp_case_fold pattern in
1102 dosearch re
1103 with Failure s ->
1104 state.text <- s;
1105 None
1107 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1108 match key with
1109 | 27 ->
1110 if String.length qsearch = 0
1111 then (
1112 state.text <- "";
1113 state.outline <- None;
1114 Glut.postRedisplay ();
1116 else (
1117 state.text <- "";
1118 state.outline <- Some (allowdel, active, first, outlines, "");
1119 Glut.postRedisplay ();
1122 | 18 | 19 ->
1123 let incr = if key = 18 then -1 else 1 in
1124 let active, first =
1125 match search (active + incr) qsearch incr with
1126 | None ->
1127 state.text <- qsearch ^ " [not found]";
1128 active, first
1129 | Some active ->
1130 state.text <- qsearch;
1131 active, firstof active
1133 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1134 Glut.postRedisplay ();
1136 | 8 ->
1137 let len = String.length qsearch in
1138 if len = 0
1139 then ()
1140 else (
1141 if len = 1
1142 then (
1143 state.text <- "";
1144 state.outline <- Some (allowdel, active, first, outlines, "");
1146 else
1147 let qsearch = String.sub qsearch 0 (len - 1) in
1148 let active, first =
1149 match search active qsearch ~-1 with
1150 | None ->
1151 state.text <- qsearch ^ " [not found]";
1152 active, first
1153 | Some active ->
1154 state.text <- qsearch;
1155 active, firstof active
1157 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1159 Glut.postRedisplay ()
1161 | 13 ->
1162 if active < Array.length outlines
1163 then (
1164 let (_, _, n, t) = outlines.(active) in
1165 gotopage n t;
1167 state.text <- "";
1168 if allowdel then state.bookmarks <- Array.to_list outlines;
1169 state.outline <- None;
1170 Glut.postRedisplay ();
1172 | _ when key >= 32 && key < 127 ->
1173 let pattern = addchar qsearch (Char.chr key) in
1174 let active, first =
1175 match search active pattern 1 with
1176 | None ->
1177 state.text <- pattern ^ " [not found]";
1178 active, first
1179 | Some active ->
1180 state.text <- pattern;
1181 active, firstof active
1183 state.outline <- Some (allowdel, active, first, outlines, pattern);
1184 Glut.postRedisplay ()
1186 | 14 when not allowdel ->
1187 let optoutlines = narrow outlines qsearch in
1188 begin match optoutlines with
1189 | None -> state.text <- "can't narrow"
1190 | Some outlines ->
1191 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1192 match state.outlines with
1193 | Olist l -> ()
1194 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1195 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1196 end;
1197 Glut.postRedisplay ()
1199 | 21 when not allowdel ->
1200 let outline =
1201 match state.outlines with
1202 | Oarray a -> a
1203 | Olist l ->
1204 let a = Array.of_list (List.rev l) in
1205 state.outlines <- Oarray a;
1207 | Onarrow (a, b) ->
1208 state.outlines <- Oarray b;
1211 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1212 Glut.postRedisplay ()
1214 | 12 ->
1215 state.outline <-
1216 Some (allowdel, active, firstof active, outlines, qsearch);
1217 Glut.postRedisplay ()
1219 | 127 when allowdel ->
1220 let len = Array.length outlines - 1 in
1221 if len = 0
1222 then (
1223 state.outline <- None;
1224 state.bookmarks <- [];
1226 else (
1227 let bookmarks = Array.init len
1228 (fun i ->
1229 let i = if i >= active then i + 1 else i in
1230 outlines.(i)
1233 state.outline <-
1234 Some (allowdel,
1235 min active (len-1),
1236 min first (len-1),
1237 bookmarks, qsearch)
1240 Glut.postRedisplay ()
1242 | _ -> log "unknown key %d" key
1245 let keyboard ~key ~x ~y =
1246 if key = 7
1247 then
1248 wcmd "interrupt" []
1249 else
1250 match state.outline with
1251 | None -> viewkeyboard ~key ~x ~y
1252 | Some outline -> outlinekeyboard ~key ~x ~y outline
1255 let special ~key ~x ~y =
1256 match state.outline with
1257 | None ->
1258 begin match state.textentry with
1259 | None ->
1260 let y =
1261 match key with
1262 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1263 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1264 | Glut.KEY_DOWN -> clamp conf.scrollincr
1265 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1266 | Glut.KEY_PAGE_DOWN -> clamp state.h
1267 | Glut.KEY_HOME -> addnav (); 0
1268 | Glut.KEY_END ->
1269 addnav ();
1270 state.maxy - (if conf.maxhfit then state.h else 0)
1271 | _ -> state.y
1273 state.text <- "";
1274 gotoy y
1276 | Some (c, s, Some onhist, onkey, ondone) ->
1277 let s =
1278 match key with
1279 | Glut.KEY_UP -> onhist HCprev
1280 | Glut.KEY_DOWN -> onhist HCnext
1281 | Glut.KEY_HOME -> onhist HCfirst
1282 | Glut.KEY_END -> onhist HClast
1283 | _ -> state.text
1285 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1286 Glut.postRedisplay ()
1288 | _ -> ()
1291 | Some (allowdel, active, first, outlines, qsearch) ->
1292 let maxrows = maxoutlinerows () in
1293 let navigate incr =
1294 let active = active + incr in
1295 let active = max 0 (min active (Array.length outlines - 1)) in
1296 let first =
1297 if active > first
1298 then
1299 let rows = active - first in
1300 if rows > maxrows then first + incr else first
1301 else active
1303 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1304 Glut.postRedisplay ()
1306 match key with
1307 | Glut.KEY_UP -> navigate ~-1
1308 | Glut.KEY_DOWN -> navigate 1
1309 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1310 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1312 | Glut.KEY_HOME ->
1313 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1314 Glut.postRedisplay ()
1316 | Glut.KEY_END ->
1317 let active = Array.length outlines - 1 in
1318 let first = max 0 (active - maxrows) in
1319 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1320 Glut.postRedisplay ()
1322 | _ -> ()
1325 let drawplaceholder l =
1326 GlDraw.color (1.0, 1.0, 1.0);
1327 GlDraw.rect
1328 (0.0, float l.pagedispy)
1329 (float l.pagew, float (l.pagedispy + l.pagevh))
1331 let x = 0.0
1332 and y = float (l.pagedispy + 13) in
1333 let font = Glut.BITMAP_8_BY_13 in
1334 GlDraw.color (0.0, 0.0, 0.0);
1335 GlPix.raster_pos ~x ~y ();
1336 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1337 ("Loading " ^ string_of_int l.pageno);
1340 let now () = Unix.gettimeofday ();;
1342 let drawpage i l =
1343 begin match getopaque l.pageno with
1344 | Some opaque when validopaque opaque ->
1345 if state.textentry = None
1346 then GlDraw.color (1.0, 1.0, 1.0)
1347 else GlDraw.color (0.4, 0.4, 0.4);
1348 let a = now () in
1349 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1350 let b = now () in
1351 let d = b-.a in
1352 vlog "draw %f sec" d;
1354 | Some _ ->
1355 drawplaceholder l
1357 | None ->
1358 drawplaceholder l;
1359 if state.inflight < cblen state.pagecache
1360 then (
1361 List.iter preload state.layout;
1363 else (
1364 vlog "inflight %d" state.inflight;
1366 end;
1367 GlDraw.color (0.5, 0.5, 0.5);
1368 GlDraw.rect
1369 (0., float i)
1370 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1372 l.pagedispy + l.pagevh;
1375 let scrollindicator () =
1376 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1377 GlDraw.color (0.64 , 0.64, 0.64);
1378 GlDraw.rect
1379 (float (state.w - conf.scrollw), 0.)
1380 (float state.w, float state.h)
1382 GlDraw.color (0.0, 0.0, 0.0);
1383 let sh = (float (maxy + state.h) /. float state.h) in
1384 let sh = float state.h /. sh in
1385 let sh = max sh (float conf.scrollh) in
1387 let percent =
1388 if state.y = state.maxy
1389 then 1.0
1390 else float state.y /. float maxy
1392 let position = (float state.h -. sh) *. percent in
1394 let position =
1395 if position +. sh > float state.h
1396 then
1397 float state.h -. sh
1398 else
1399 position
1401 GlDraw.rect
1402 (float (state.w - conf.scrollw), position)
1403 (float state.w, position +. sh)
1407 let showsel () =
1408 match state.mstate with
1409 | Mnone ->
1412 | Msel ((x0, y0), (x1, y1)) ->
1413 let y0' = min y0 y1
1414 and y1 = max y0 y1 in
1415 let y0 = y0' in
1416 let f l =
1417 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1418 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1419 then
1420 match getopaque l.pageno with
1421 | Some opaque when validopaque opaque ->
1422 let oy = -l.pagey + l.pagedispy in
1423 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1424 | _ -> ()
1426 List.iter f state.layout
1429 let showrects () =
1430 Gl.enable `blend;
1431 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1432 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1433 List.iter
1434 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1435 List.iter (fun l ->
1436 if l.pageno = pageno
1437 then (
1438 let d = float (l.pagedispy - l.pagey) in
1439 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1440 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1442 ) state.layout
1443 ) state.rects
1445 Gl.disable `blend;
1448 let showoutline = function
1449 | None -> ()
1450 | Some (allowdel, active, first, outlines, qsearch) ->
1451 Gl.enable `blend;
1452 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1453 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1454 GlDraw.rect (0., 0.) (float state.w, float state.h);
1455 Gl.disable `blend;
1457 GlDraw.color (1., 1., 1.);
1458 let font = Glut.BITMAP_9_BY_15 in
1459 let draw_string x y s =
1460 GlPix.raster_pos ~x ~y ();
1461 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1463 let rec loop row =
1464 if row = Array.length outlines || (row - first) * 16 > state.h
1465 then ()
1466 else (
1467 let (s, l, _, _) = outlines.(row) in
1468 let y = (row - first) * 16 in
1469 let x = 5 + 5*l in
1470 if row = active
1471 then (
1472 Gl.enable `blend;
1473 GlDraw.polygon_mode `both `line;
1474 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1475 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1476 GlDraw.rect (0., float (y + 1))
1477 (float (state.w - conf.scrollw - 1), float (y + 18));
1478 GlDraw.polygon_mode `both `fill;
1479 Gl.disable `blend;
1480 GlDraw.color (1., 1., 1.);
1482 draw_string (float x) (float (y + 16)) s;
1483 loop (row+1)
1486 loop first
1489 let display () =
1490 let lasty = List.fold_left drawpage 0 (state.layout) in
1491 GlDraw.color (0.5, 0.5, 0.5);
1492 GlDraw.rect
1493 (0., float lasty)
1494 (float (state.w - conf.scrollw), float state.h)
1496 showrects ();
1497 scrollindicator ();
1498 showsel ();
1499 showoutline state.outline;
1500 enttext ();
1501 Glut.swapBuffers ();
1504 let getlink x y =
1505 let rec f = function
1506 | l :: rest ->
1507 begin match getopaque l.pageno with
1508 | Some opaque when validopaque opaque ->
1509 let y = y - l.pagedispy in
1510 if y > 0
1511 then
1512 let y = l.pagey + y in
1513 match getlink opaque x y with
1514 | None -> f rest
1515 | some -> some
1516 else
1517 f rest
1518 | _ ->
1519 f rest
1521 | [] -> None
1523 f state.layout
1526 let checklink x y =
1527 let rec f = function
1528 | l :: rest ->
1529 begin match getopaque l.pageno with
1530 | Some opaque when validopaque opaque ->
1531 let y = y - l.pagedispy in
1532 if y > 0
1533 then
1534 let y = l.pagey + y in
1535 if checklink opaque x y then true else f rest
1536 else
1537 f rest
1538 | _ ->
1539 f rest
1541 | [] -> false
1543 f state.layout
1546 let mouse ~button ~bstate ~x ~y =
1547 match button with
1548 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1549 let incr =
1550 if n = 3
1551 then
1552 -conf.scrollincr
1553 else
1554 conf.scrollincr
1556 let incr = incr * 2 in
1557 let y = clamp incr in
1558 gotoy y
1560 | Glut.LEFT_BUTTON when state.outline = None ->
1561 let dest = if bstate = Glut.DOWN then getlink x y else None in
1562 begin match dest with
1563 | Some (pageno, top) ->
1564 gotopage pageno top
1566 | None ->
1567 if bstate = Glut.DOWN
1568 then (
1569 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1570 state.mstate <- Msel ((x, y), (x, y));
1571 Glut.postRedisplay ()
1573 else (
1574 Glut.setCursor Glut.CURSOR_INHERIT;
1575 state.mstate <- Mnone;
1579 | _ ->
1582 let mouse ~button ~state ~x ~y = mouse button state x y;;
1584 let motion ~x ~y =
1585 if state.outline = None
1586 then
1587 match state.mstate with
1588 | Mnone -> ()
1589 | Msel (a, _) ->
1590 state.mstate <- Msel (a, (x, y));
1591 Glut.postRedisplay ()
1594 let pmotion ~x ~y =
1595 if state.outline = None
1596 then
1597 match state.mstate with
1598 | Mnone when (checklink x y) ->
1599 Glut.setCursor Glut.CURSOR_INFO
1601 | Mnone ->
1602 Glut.setCursor Glut.CURSOR_INHERIT
1604 | Msel (a, _) ->
1608 let () =
1609 let statepath =
1610 let home =
1611 if Sys.os_type = "Win32"
1612 then
1613 try Sys.getenv "HOMEPATH" with Not_found -> ""
1614 else
1615 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1617 Filename.concat home "llpp"
1619 let pstate =
1621 let ic = open_in_bin statepath in
1622 let hash = input_value ic in
1623 close_in ic;
1624 hash
1625 with exn ->
1626 if false
1627 then
1628 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1630 Hashtbl.create 1
1632 let savestate () =
1634 let w, h =
1635 match state.fullscreen with
1636 | None -> state.w, state.h
1637 | Some wh -> wh
1639 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1640 let oc = open_out_bin statepath in
1641 output_value oc pstate
1642 with exn ->
1643 if false
1644 then
1645 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1648 let setstate () =
1650 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1651 state.w <- statew;
1652 state.h <- stateh;
1653 state.bookmarks <- statebookmarks;
1654 with Not_found -> ()
1655 | exn ->
1656 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1659 Arg.parse [] (fun s -> state.path <- s) "options:";
1660 let name =
1661 if String.length state.path = 0
1662 then (prerr_endline "filename missing"; exit 1)
1663 else state.path
1666 setstate ();
1667 let _ = Glut.init Sys.argv in
1668 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1669 let () = Glut.initWindowSize state.w state.h in
1670 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1672 let csock, ssock =
1673 if Sys.os_type = "Unix"
1674 then
1675 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1676 else
1677 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1678 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1679 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1680 Unix.bind sock addr;
1681 Unix.listen sock 1;
1682 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1683 Unix.connect csock addr;
1684 let ssock, _ = Unix.accept sock in
1685 Unix.close sock;
1686 let opts sock =
1687 Unix.setsockopt sock Unix.TCP_NODELAY true;
1688 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1690 opts ssock;
1691 opts csock;
1692 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1693 ssock, csock
1696 let () = Glut.displayFunc display in
1697 let () = Glut.reshapeFunc reshape in
1698 let () = Glut.keyboardFunc keyboard in
1699 let () = Glut.specialFunc special in
1700 let () = Glut.idleFunc (Some idle) in
1701 let () = Glut.mouseFunc mouse in
1702 let () = Glut.motionFunc motion in
1703 let () = Glut.passiveMotionFunc pmotion in
1705 init ssock;
1706 state.csock <- csock;
1707 state.ssock <- ssock;
1708 writecmd csock ("open " ^ name ^ "\000");
1710 at_exit savestate;
1712 let rec handlelablglutbug () =
1714 Glut.mainLoop ();
1715 with Glut.BadEnum "key in special_of_int" ->
1716 showtext '!' " LablGlut bug: special key not recognized";
1717 Glut.swapBuffers ();
1718 handlelablglutbug ()
1720 handlelablglutbug ();