Ctrl-g to interrupt searches
[llpp.git] / main.ml
blobc4a598d96b6451e78d771c3cad8396bdc005b07a
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
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
631 let search pattern forward =
632 if String.length pattern > 0
633 then
634 let pn, py =
635 match state.layout with
636 | [] -> 0, 0
637 | l :: _ ->
638 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
640 let cmd =
641 let b = makecmd "search"
642 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
644 Buffer.add_char b ',';
645 Buffer.add_string b pattern;
646 Buffer.add_char b '\000';
647 Buffer.contents b;
649 writecmd state.csock cmd;
652 let intentry text key =
653 let c = Char.unsafe_chr key in
654 match c with
655 | '0' .. '9' ->
656 let s = "x" in s.[0] <- c;
657 let text = text ^ s in
658 TEcont text
660 | _ ->
661 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
662 TEcont text
665 let addchar s c =
666 let b = Buffer.create (String.length s + 1) in
667 Buffer.add_string b s;
668 Buffer.add_char b c;
669 Buffer.contents b;
672 let textentry text key =
673 let c = Char.unsafe_chr key in
674 match c with
675 | _ when key >= 32 && key < 127 ->
676 let text = addchar text c in
677 TEcont text
679 | _ ->
680 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
681 TEcont text
684 let optentry text key =
685 let btos b = if b then "on" else "off" in
686 let c = Char.unsafe_chr key in
687 match c with
688 | 'r' ->
689 conf.rectsel <- not conf.rectsel;
690 TEdone ("rectsel " ^ (btos conf.rectsel))
692 | 's' ->
693 let ondone s =
694 try conf.scrollincr <- int_of_string s with exc ->
695 state.text <- Printf.sprintf "bad integer `%s': %s"
696 s (Printexc.to_string exc)
698 TEswitch ('#', "", None, intentry, ondone)
700 | 'R' ->
701 let ondone s =
703 state.rotate <- int_of_string s;
704 wcmd "rotate" [`i state.rotate]
705 with exc ->
706 state.text <- Printf.sprintf "bad integer `%s': %s"
707 s (Printexc.to_string exc)
709 TEswitch ('^', "", None, intentry, ondone)
711 | 'i' ->
712 conf.icase <- not conf.icase;
713 TEdone ("case insensitive search " ^ (btos conf.icase))
715 | 'p' ->
716 conf.preload <- not conf.preload;
717 TEdone ("preload " ^ (btos conf.preload))
719 | 'd' ->
720 conf.redispimm <- not conf.redispimm;
721 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
723 | 'v' ->
724 conf.verbose <- not conf.verbose;
725 TEdone ("verbose " ^ (btos conf.verbose))
727 | 'h' ->
728 conf.maxhfit <- not conf.maxhfit;
729 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
730 TEdone ("maxhfit " ^ (btos conf.maxhfit))
732 | 'c' ->
733 conf.crophack <- not conf.crophack;
734 TEdone ("crophack " ^ btos conf.crophack)
736 | 'a' ->
737 conf.showall <- not conf.showall;
738 TEdone ("showall " ^ btos conf.showall)
740 | _ ->
741 state.text <- Printf.sprintf "bad option %d `%c'" key c;
742 TEstop
745 let maxoutlinerows () = (state.h - 31) / 16;;
747 let enterselector allowdel outlines errmsg =
748 if Array.length outlines = 0
749 then (
750 showtext ' ' errmsg;
751 Glut.swapBuffers ()
753 else
754 let pageno =
755 match state.layout with
756 | [] -> -1
757 | {pageno=pageno} :: rest -> pageno
759 let active =
760 let rec loop n =
761 if n = Array.length outlines
762 then 0
763 else
764 let (_, _, outlinepageno, _) = outlines.(n) in
765 if outlinepageno >= pageno then n else loop (n+1)
767 loop 0
769 state.outline <-
770 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
771 Glut.postRedisplay ();
774 let enteroutlinemode () =
775 let outlines =
776 match state.outlines with
777 | Oarray a -> a
778 | Olist l ->
779 let a = Array.of_list (List.rev l) in
780 state.outlines <- Oarray a;
782 | Onarrow (a, b) -> a
784 enterselector false outlines "Documents has no outline";
787 let enterbookmarkmode () =
788 let bookmarks = Array.of_list state.bookmarks in
789 enterselector true bookmarks "Documents has no bookmarks (yet)";
793 let quickbookmark ?title () =
794 match state.layout with
795 | [] -> ()
796 | l :: _ ->
797 let title =
798 match title with
799 | None ->
800 let sec = Unix.gettimeofday () in
801 let tm = Unix.localtime sec in
802 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
803 l.pageno
804 tm.Unix.tm_mday
805 tm.Unix.tm_mon
806 (tm.Unix.tm_year + 1900)
807 tm.Unix.tm_hour
808 tm.Unix.tm_min
809 | Some title -> title
811 state.bookmarks <-
812 (title, 0, l.pageno, l.pagey) :: state.bookmarks
815 let viewkeyboard ~key ~x ~y =
816 let enttext te =
817 state.textentry <- te;
818 state.text <- "";
819 enttext ();
820 Glut.postRedisplay ()
822 match state.textentry with
823 | None ->
824 let c = Char.chr key in
825 begin match c with
826 | '\027' | 'q' ->
827 exit 0
829 | '\008' ->
830 let y = getnav () in
831 gotoy y
833 | 'o' ->
834 enteroutlinemode ()
836 | 'u' ->
837 state.rects <- [];
838 state.text <- "";
839 Glut.postRedisplay ()
841 | '/' | '?' ->
842 let ondone isforw s =
843 cbput state.hists.pat s;
844 cbrfollowlen state.hists.pat;
845 state.searchpattern <- s;
846 search s isforw
848 enttext (Some (c, "", Some (onhist state.hists.pat),
849 textentry, ondone (c ='/')))
851 | '+' ->
852 let ondone s =
853 let n =
854 try int_of_string s with exc ->
855 state.text <- Printf.sprintf "bad integer `%s': %s"
856 s (Printexc.to_string exc);
857 max_int
859 if n != max_int
860 then (
861 conf.pagebias <- n;
862 state.text <- "page bias is now " ^ string_of_int n;
865 enttext (Some ('+', "", None, intentry, ondone))
867 | '-' ->
868 let ondone msg =
869 state.text <- msg;
871 enttext (Some ('-', "", None, optentry, ondone))
873 | '0' .. '9' ->
874 let ondone s =
875 let n =
876 try int_of_string s with exc ->
877 state.text <- Printf.sprintf "bad integer `%s': %s"
878 s (Printexc.to_string exc);
881 if n >= 0
882 then (
883 addnav ();
884 cbput state.hists.pag (string_of_int n);
885 cbrfollowlen state.hists.pag;
886 gotoy (getpagey (n + conf.pagebias - 1))
889 let pageentry text key =
890 match Char.unsafe_chr key with
891 | 'g' -> TEdone text
892 | _ -> intentry text key
894 let text = "x" in text.[0] <- c;
895 enttext (Some (':', text, Some (onhist state.hists.pag),
896 pageentry, ondone))
898 | 'b' ->
899 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
900 reshape state.w state.h;
902 | 'a' ->
903 conf.autoscroll <- not conf.autoscroll
905 | 'f' ->
906 begin match state.fullscreen with
907 | None ->
908 state.fullscreen <- Some (state.w, state.h);
909 Glut.fullScreen ()
910 | Some (w, h) ->
911 state.fullscreen <- None;
912 Glut.reshapeWindow ~w ~h
915 | 'g' ->
916 gotoy 0
918 | 'n' ->
919 search state.searchpattern true
921 | 'p' | 'N' ->
922 search state.searchpattern false
924 | 't' ->
925 begin match state.layout with
926 | [] -> ()
927 | l :: _ ->
928 gotoy (state.y - l.pagey);
931 | ' ' ->
932 begin match List.rev state.layout with
933 | [] -> ()
934 | l :: _ ->
935 gotoy (clamp (l.pageh - l.pagey))
938 | '\127' ->
939 begin match state.layout with
940 | [] -> ()
941 | l :: _ ->
942 gotoy (clamp (-l.pageh));
945 | '=' ->
946 let f (fn, ln) l =
947 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
949 let fn, ln = List.fold_left f (-1, -1) state.layout in
950 let s =
951 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
952 let percent =
953 if maxy <= 0
954 then 100.
955 else (100. *. (float state.y /. float maxy)) in
956 if fn = ln
957 then
958 Printf.sprintf "Page %d of %d %.2f%%"
959 (fn+1) state.pagecount percent
960 else
961 Printf.sprintf
962 "Pages %d-%d of %d %.2f%%"
963 (fn+1) (ln+1) state.pagecount percent
965 showtext ' ' s;
966 Glut.swapBuffers ()
968 | 'w' ->
969 begin match state.layout with
970 | [] -> ()
971 | l :: _ ->
972 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
973 Glut.postRedisplay ();
976 | '\'' ->
977 enterbookmarkmode ()
979 | 'm' ->
980 let ondone s =
981 match state.layout with
982 | l :: _ ->
983 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
984 | _ -> ()
986 enttext (Some ('~', "", None, textentry, ondone))
988 | '~' ->
989 quickbookmark ();
990 showtext ' ' "Quick bookmark added";
991 Glut.swapBuffers ()
993 | 'z' ->
994 begin match state.layout with
995 | l :: _ ->
996 let a = getpagewh l.pagedimno in
997 let w, h =
998 if conf.crophack
999 then
1000 (truncate (1.8 *. (a.(1) -. a.(0))),
1001 truncate (1.4 *. (a.(3) -. a.(0))))
1002 else
1003 (truncate (a.(1) -. a.(0)),
1004 truncate (a.(3) -. a.(0)))
1006 Glut.reshapeWindow (w + conf.scrollw) h;
1007 Glut.postRedisplay ();
1009 | [] -> ()
1012 | '<' | '>' ->
1013 state.rotate <- state.rotate + (if c = '>' then 30 else -30);
1014 wcmd "rotate" [`i state.rotate]
1016 | _ ->
1017 vlog "huh? %d %c" key (Char.chr key);
1020 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1021 let len = String.length text in
1022 if len = 0
1023 then (
1024 state.textentry <- None;
1025 Glut.postRedisplay ();
1027 else (
1028 let s = String.sub text 0 (len - 1) in
1029 enttext (Some (c, s, onhist, onkey, ondone))
1032 | Some (c, text, onhist, onkey, ondone) ->
1033 begin match Char.unsafe_chr key with
1034 | '\r' | '\n' ->
1035 ondone text;
1036 state.textentry <- None;
1037 Glut.postRedisplay ()
1039 | '\027' ->
1040 state.textentry <- None;
1041 Glut.postRedisplay ()
1043 | _ ->
1044 begin match onkey text key with
1045 | TEdone text ->
1046 state.textentry <- None;
1047 ondone text;
1048 Glut.postRedisplay ()
1050 | TEcont text ->
1051 enttext (Some (c, text, onhist, onkey, ondone));
1053 | TEstop ->
1054 state.textentry <- None;
1055 Glut.postRedisplay ()
1057 | TEswitch te ->
1058 state.textentry <- Some te;
1059 Glut.postRedisplay ()
1060 end;
1061 end;
1064 let narrow outlines pattern =
1065 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1066 match reopt with
1067 | None -> None
1068 | Some re ->
1069 let rec fold accu n =
1070 if n = -1 then accu else
1071 let (s, _, _, _) as o = outlines.(n) in
1072 let accu =
1073 if (try ignore (Str.search_forward re s 0); true
1074 with Not_found -> false)
1075 then (o :: accu)
1076 else accu
1078 fold accu (n-1)
1080 let matched = fold [] (Array.length outlines - 1) in
1081 if matched = [] then None else Some (Array.of_list matched)
1084 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1085 let search active pattern incr =
1086 let dosearch re =
1087 let rec loop n =
1088 if n = Array.length outlines || n = -1 then None else
1089 let (s, _, _, _) = outlines.(n) in
1091 (try ignore (Str.search_forward re s 0); true
1092 with Not_found -> false)
1093 then (
1094 let maxrows = (min (Array.length outlines) (maxoutlinerows ())) / 2 in
1095 if first > n
1096 then Some (n, max 0 (n - maxrows))
1097 else Some (n, max first (n - maxrows))
1099 else loop (n + incr)
1101 loop active
1104 let re = Str.regexp_case_fold pattern in
1105 dosearch re
1106 with Failure s ->
1107 state.text <- s;
1108 None
1110 match key with
1111 | 27 ->
1112 if String.length qsearch = 0
1113 then (
1114 state.text <- "";
1115 state.outline <- None;
1116 Glut.postRedisplay ();
1118 else (
1119 state.text <- "";
1120 state.outline <- Some (allowdel, active, first, outlines, "");
1121 Glut.postRedisplay ();
1124 | 18 | 19 ->
1125 let incr = if key = 18 then -1 else 1 in
1126 let active, first =
1127 match search (active + incr) qsearch incr with
1128 | None ->
1129 state.text <- qsearch ^ " [not found]";
1130 active, first
1131 | Some af ->
1132 state.text <- qsearch;
1135 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1136 Glut.postRedisplay ();
1138 | 8 ->
1139 let len = String.length qsearch in
1140 if len = 0
1141 then ()
1142 else (
1143 if len = 1
1144 then (
1145 state.text <- "";
1146 state.outline <- Some (allowdel, active, first, outlines, "");
1148 else
1149 let qsearch = String.sub qsearch 0 (len - 1) in
1150 let active, first =
1151 match search active qsearch ~-1 with
1152 | None ->
1153 state.text <- qsearch ^ " [not found]";
1154 active, first
1155 | Some af ->
1156 state.text <- qsearch;
1159 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1161 Glut.postRedisplay ()
1163 | 13 ->
1164 if active < Array.length outlines
1165 then (
1166 let (_, _, n, t) = outlines.(active) in
1167 gotopage n t;
1169 state.text <- "";
1170 if allowdel then state.bookmarks <- Array.to_list outlines;
1171 state.outline <- None;
1172 Glut.postRedisplay ();
1174 | _ when key >= 32 && key < 127 ->
1175 let pattern = addchar qsearch (Char.chr key) in
1176 let active, first =
1177 match search active pattern 1 with
1178 | None ->
1179 state.text <- pattern ^ " [not found]";
1180 active, first
1181 | Some (active, first) ->
1182 state.text <- pattern;
1183 active, first
1185 state.outline <- Some (allowdel, active, first, outlines, pattern);
1186 Glut.postRedisplay ()
1188 | 14 when not allowdel ->
1189 let optoutlines = narrow outlines qsearch in
1190 begin match optoutlines with
1191 | None -> state.text <- "can't narrow"
1192 | Some outlines ->
1193 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1194 match state.outlines with
1195 | Olist l -> ()
1196 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1197 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1198 end;
1199 Glut.postRedisplay ()
1201 | 21 when not allowdel ->
1202 let outline =
1203 match state.outlines with
1204 | Oarray a -> a
1205 | Olist l ->
1206 let a = Array.of_list (List.rev l) in
1207 state.outlines <- Oarray a;
1209 | Onarrow (a, b) -> b
1211 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1212 Glut.postRedisplay ()
1214 | 127 when allowdel ->
1215 let len = Array.length outlines - 1 in
1216 if len = 0
1217 then (
1218 state.outline <- None;
1219 state.bookmarks <- [];
1221 else (
1222 let bookmarks = Array.init len
1223 (fun i ->
1224 let i = if i >= active then i + 1 else i in
1225 outlines.(i)
1228 state.outline <-
1229 Some (allowdel,
1230 min active (len-1),
1231 min first (len-1),
1232 bookmarks, qsearch)
1235 Glut.postRedisplay ()
1237 | _ -> log "unknown key %d" key
1240 let keyboard ~key ~x ~y =
1241 if key = 7
1242 then
1243 wcmd "interrupt" []
1244 else
1245 match state.outline with
1246 | None -> viewkeyboard ~key ~x ~y
1247 | Some outline -> outlinekeyboard ~key ~x ~y outline
1250 let special ~key ~x ~y =
1251 match state.outline with
1252 | None ->
1253 begin match state.textentry with
1254 | None ->
1255 let y =
1256 match key with
1257 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1258 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1259 | Glut.KEY_DOWN -> clamp conf.scrollincr
1260 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1261 | Glut.KEY_PAGE_DOWN -> clamp state.h
1262 | Glut.KEY_HOME -> addnav (); 0
1263 | Glut.KEY_END ->
1264 addnav ();
1265 state.maxy - (if conf.maxhfit then state.h else 0)
1266 | _ -> state.y
1268 state.text <- "";
1269 gotoy y
1271 | Some (c, s, Some onhist, onkey, ondone) ->
1272 let s =
1273 match key with
1274 | Glut.KEY_UP -> onhist HCprev
1275 | Glut.KEY_DOWN -> onhist HCnext
1276 | _ -> state.text
1278 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1279 Glut.postRedisplay ()
1281 | _ -> ()
1284 | Some (allowdel, active, first, outlines, qsearch) ->
1285 let maxrows = maxoutlinerows () in
1286 let navigate incr =
1287 let active = active + incr in
1288 let active = max 0 (min active (Array.length outlines - 1)) in
1289 let first =
1290 if active > first
1291 then
1292 let rows = active - first in
1293 if rows > maxrows then first + incr else first
1294 else active
1296 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1297 Glut.postRedisplay ()
1299 match key with
1300 | Glut.KEY_UP -> navigate ~-1
1301 | Glut.KEY_DOWN -> navigate 1
1302 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1303 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1305 | Glut.KEY_HOME ->
1306 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1307 Glut.postRedisplay ()
1309 | Glut.KEY_END ->
1310 let active = Array.length outlines - 1 in
1311 let first = max 0 (active - maxrows) in
1312 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1313 Glut.postRedisplay ()
1315 | _ -> ()
1318 let drawplaceholder l =
1319 GlDraw.color (1.0, 1.0, 1.0);
1320 GlDraw.rect
1321 (0.0, float l.pagedispy)
1322 (float l.pagew, float (l.pagedispy + l.pagevh))
1324 let x = 0.0
1325 and y = float (l.pagedispy + 13) in
1326 let font = Glut.BITMAP_8_BY_13 in
1327 GlDraw.color (0.0, 0.0, 0.0);
1328 GlPix.raster_pos ~x ~y ();
1329 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1330 ("Loading " ^ string_of_int l.pageno);
1333 let now () = Unix.gettimeofday ();;
1335 let drawpage i l =
1336 begin match getopaque l.pageno with
1337 | Some opaque when validopaque opaque ->
1338 if state.textentry = None
1339 then GlDraw.color (1.0, 1.0, 1.0)
1340 else GlDraw.color (0.4, 0.4, 0.4);
1341 let a = now () in
1342 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1343 let b = now () in
1344 let d = b-.a in
1345 vlog "draw %f sec" d;
1347 | Some _ ->
1348 drawplaceholder l
1350 | None ->
1351 drawplaceholder l;
1352 if state.inflight < cblen state.pagecache
1353 then (
1354 List.iter preload state.layout;
1356 else (
1357 vlog "inflight %d" state.inflight;
1359 end;
1360 GlDraw.color (0.5, 0.5, 0.5);
1361 GlDraw.rect
1362 (0., float i)
1363 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1365 l.pagedispy + l.pagevh;
1368 let scrollindicator () =
1369 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1370 GlDraw.color (0.64 , 0.64, 0.64);
1371 GlDraw.rect
1372 (float (state.w - conf.scrollw), 0.)
1373 (float state.w, float state.h)
1375 GlDraw.color (0.0, 0.0, 0.0);
1376 let sh = (float (maxy + state.h) /. float state.h) in
1377 let sh = float state.h /. sh in
1378 let sh = max sh (float conf.scrollh) in
1380 let percent =
1381 if state.y = state.maxy
1382 then 1.0
1383 else float state.y /. float maxy
1385 let position = (float state.h -. sh) *. percent in
1387 let position =
1388 if position +. sh > float state.h
1389 then
1390 float state.h -. sh
1391 else
1392 position
1394 GlDraw.rect
1395 (float (state.w - conf.scrollw), position)
1396 (float state.w, position +. sh)
1400 let showsel () =
1401 match state.mstate with
1402 | Mnone ->
1405 | Msel ((x0, y0), (x1, y1)) ->
1406 let y0' = min y0 y1
1407 and y1 = max y0 y1 in
1408 let y0 = y0' in
1409 let f l =
1410 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1411 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1412 then
1413 match getopaque l.pageno with
1414 | Some opaque when validopaque opaque ->
1415 let oy = -l.pagey + l.pagedispy in
1416 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1417 | _ -> ()
1419 List.iter f state.layout
1422 let showrects () =
1423 Gl.enable `blend;
1424 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1425 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1426 List.iter
1427 (fun (pageno, c, (x0, y0), (x1, y1)) ->
1428 List.iter (fun l ->
1429 if l.pageno = pageno
1430 then (
1431 let d = float (l.pagedispy - l.pagey) in
1432 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1433 GlDraw.rect (x0, y0 +. d) (x1, y1 +. d)
1435 ) state.layout
1436 ) state.rects
1438 Gl.disable `blend;
1441 let showoutline = function
1442 | None -> ()
1443 | Some (allowdel, active, first, outlines, qsearch) ->
1444 Gl.enable `blend;
1445 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1446 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1447 GlDraw.rect (0., 0.) (float state.w, float state.h);
1448 Gl.disable `blend;
1450 GlDraw.color (1., 1., 1.);
1451 let font = Glut.BITMAP_9_BY_15 in
1452 let draw_string x y s =
1453 GlPix.raster_pos ~x ~y ();
1454 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1456 let rec loop row =
1457 if row = Array.length outlines || (row - first) * 16 > state.h
1458 then ()
1459 else (
1460 let (s, l, _, _) = outlines.(row) in
1461 let y = (row - first) * 16 in
1462 let x = 5 + 5*l in
1463 if row = active
1464 then (
1465 Gl.enable `blend;
1466 GlDraw.polygon_mode `both `line;
1467 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1468 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1469 GlDraw.rect (0., float (y + 1))
1470 (float (state.w - conf.scrollw - 1), float (y + 18));
1471 GlDraw.polygon_mode `both `fill;
1472 Gl.disable `blend;
1473 GlDraw.color (1., 1., 1.);
1475 draw_string (float x) (float (y + 16)) s;
1476 loop (row+1)
1479 loop first
1482 let display () =
1483 let lasty = List.fold_left drawpage 0 (state.layout) in
1484 GlDraw.color (0.5, 0.5, 0.5);
1485 GlDraw.rect
1486 (0., float lasty)
1487 (float (state.w - conf.scrollw), float state.h)
1489 showrects ();
1490 scrollindicator ();
1491 showsel ();
1492 showoutline state.outline;
1493 enttext ();
1494 Glut.swapBuffers ();
1497 let getlink x y =
1498 let rec f = function
1499 | l :: rest ->
1500 begin match getopaque l.pageno with
1501 | Some opaque when validopaque opaque ->
1502 let y = y - l.pagedispy in
1503 if y > 0
1504 then
1505 let y = l.pagey + y in
1506 match getlink opaque x y with
1507 | None -> f rest
1508 | some -> some
1509 else
1510 f rest
1511 | _ ->
1512 f rest
1514 | [] -> None
1516 f state.layout
1519 let checklink x y =
1520 let rec f = function
1521 | l :: rest ->
1522 begin match getopaque l.pageno with
1523 | Some opaque when validopaque opaque ->
1524 let y = y - l.pagedispy in
1525 if y > 0
1526 then
1527 let y = l.pagey + y in
1528 if checklink opaque x y then true else f rest
1529 else
1530 f rest
1531 | _ ->
1532 f rest
1534 | [] -> false
1536 f state.layout
1539 let mouse ~button ~bstate ~x ~y =
1540 match button with
1541 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1542 let incr =
1543 if n = 3
1544 then
1545 -conf.scrollincr
1546 else
1547 conf.scrollincr
1549 let incr = incr * 2 in
1550 let y = clamp incr in
1551 gotoy y
1553 | Glut.LEFT_BUTTON when state.outline = None ->
1554 let dest = if bstate = Glut.DOWN then getlink x y else None in
1555 begin match dest with
1556 | Some (pageno, top) ->
1557 gotopage pageno top
1559 | None ->
1560 if bstate = Glut.DOWN
1561 then (
1562 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1563 state.mstate <- Msel ((x, y), (x, y));
1564 Glut.postRedisplay ()
1566 else (
1567 Glut.setCursor Glut.CURSOR_INHERIT;
1568 state.mstate <- Mnone;
1572 | _ ->
1575 let mouse ~button ~state ~x ~y = mouse button state x y;;
1577 let motion ~x ~y =
1578 if state.outline = None
1579 then
1580 match state.mstate with
1581 | Mnone -> ()
1582 | Msel (a, _) ->
1583 state.mstate <- Msel (a, (x, y));
1584 Glut.postRedisplay ()
1587 let pmotion ~x ~y =
1588 if state.outline = None
1589 then
1590 match state.mstate with
1591 | Mnone when (checklink x y) ->
1592 Glut.setCursor Glut.CURSOR_INFO
1594 | Mnone ->
1595 Glut.setCursor Glut.CURSOR_INHERIT
1597 | Msel (a, _) ->
1601 let () =
1602 let statepath =
1603 let home =
1604 if Sys.os_type = "Win32"
1605 then
1606 try Sys.getenv "HOMEPATH" with Not_found -> ""
1607 else
1608 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1610 Filename.concat home "llpp"
1612 let pstate =
1614 let ic = open_in_bin statepath in
1615 let hash = input_value ic in
1616 close_in ic;
1617 hash
1618 with exn ->
1619 if false
1620 then
1621 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1623 Hashtbl.create 1
1625 let savestate () =
1627 let w, h =
1628 match state.fullscreen with
1629 | None -> state.w, state.h
1630 | Some wh -> wh
1632 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1633 let oc = open_out_bin statepath in
1634 output_value oc pstate
1635 with exn ->
1636 if false
1637 then
1638 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1641 let setstate () =
1643 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1644 state.w <- statew;
1645 state.h <- stateh;
1646 state.bookmarks <- statebookmarks;
1647 with Not_found -> ()
1648 | exn ->
1649 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1652 Arg.parse [] (fun s -> state.path <- s) "options:";
1653 let name =
1654 if String.length state.path = 0
1655 then (prerr_endline "filename missing"; exit 1)
1656 else state.path
1659 setstate ();
1660 let _ = Glut.init Sys.argv in
1661 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1662 let () = Glut.initWindowSize state.w state.h in
1663 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1665 let csock, ssock =
1666 if Sys.os_type = "Unix"
1667 then
1668 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1669 else
1670 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1671 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1672 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1673 Unix.bind sock addr;
1674 Unix.listen sock 1;
1675 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1676 Unix.connect csock addr;
1677 let ssock, _ = Unix.accept sock in
1678 Unix.close sock;
1679 let opts sock =
1680 Unix.setsockopt sock Unix.TCP_NODELAY true;
1681 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1683 opts ssock;
1684 opts csock;
1685 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1686 ssock, csock
1689 let () = Glut.displayFunc display in
1690 let () = Glut.reshapeFunc reshape in
1691 let () = Glut.keyboardFunc keyboard in
1692 let () = Glut.specialFunc special in
1693 let () = Glut.idleFunc (Some idle) in
1694 let () = Glut.mouseFunc mouse in
1695 let () = Glut.motionFunc motion in
1696 let () = Glut.passiveMotionFunc pmotion in
1698 init ssock;
1699 state.csock <- csock;
1700 state.ssock <- ssock;
1701 writecmd csock ("open " ^ name ^ "\000");
1703 at_exit savestate;
1705 let rec handlelablglutbug () =
1707 Glut.mainLoop ();
1708 with Glut.BadEnum "key in special_of_int" ->
1709 showtext '!' " LablGlut bug: special key not recognized";
1710 Glut.swapBuffers ();
1711 handlelablglutbug ()
1713 handlelablglutbug ();