Change the way (mostly when) pages are submitted for rendering
[llpp.git] / main.ml
blob703154a440565f31182b08661996e261ff55a460
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 seltext : string -> (int * int * int * int) -> int -> unit =
9 "ml_seltext";;
10 external copysel : string -> unit = "ml_copysel";;
11 external getlink : string -> int -> int -> link = "ml_getlink";;
12 external highlightlinks : string -> int -> unit = "ml_highlightlinks";;
13 external getpagewh : int -> float array = "ml_getpagewh";;
15 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
17 type 'a circbuf =
18 { store : 'a array
19 ; mutable rc : int
20 ; mutable wc : int
21 ; mutable len : int
25 type textentry = (char * string * onhist option * onkey * ondone)
26 and onkey = string -> int -> te
27 and ondone = string -> unit
28 and onhist = histcmd -> string
29 and histcmd = HCnext | HCprev | HCfirst | HClast
30 and te =
31 | TEstop
32 | TEdone of string
33 | TEcont of string
34 | TEswitch of textentry
37 let cbnew n v =
38 { store = Array.create n v
39 ; rc = 0
40 ; wc = 0
41 ; len = 0
45 let cblen b = Array.length b.store;;
47 let cbput b v =
48 let len = cblen b in
49 b.store.(b.wc) <- v;
50 b.wc <- (b.wc + 1) mod len;
51 b.len <- min (b.len + 1) len;
54 let cbpeekw b = b.store.(b.wc);;
56 let cbget b dir =
57 if b.len = 0 then b.store.(0) else
58 let rc = b.rc + dir in
59 let rc = if rc = -1 then b.len - 1 else rc in
60 let rc = if rc = b.len then 0 else rc in
61 b.rc <- rc;
62 b.store.(rc);
65 let cbrfollowlen b =
66 b.rc <- b.len;
69 type layout =
70 { pageno : int
71 ; pagedimno : int
72 ; pagew : int
73 ; pageh : int
74 ; pagedispy : int
75 ; pagey : int
76 ; pagevh : int
80 type conf =
81 { mutable scrollw : int
82 ; mutable scrollh : int
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 : float
113 ; mutable maxy : int
114 ; mutable layout : layout list
115 ; pagemap : ((int * int * int), string) Hashtbl.t
116 ; mutable pages : (int * int * int) list
117 ; mutable pagecount : int
118 ; pagecache : string circbuf
119 ; mutable rendering : bool
120 ; mutable mstate : mstate
121 ; mutable searchpattern : string
122 ; mutable rects : (int * int * rect) list
123 ; mutable rects1 : (int * int * rect) list
124 ; mutable text : string
125 ; mutable fullscreen : (int * int) option
126 ; mutable textentry : textentry option
127 ; mutable outlines : outlines
128 ; mutable outline : (bool * int * int * outline array * string) option
129 ; mutable bookmarks : outline list
130 ; mutable path : string
131 ; mutable invalidated : int
132 ; mutable colorscale : float
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 ; preload = false
147 ; pagebias = 0
148 ; verbose = false
149 ; scrollincr = 24
150 ; maxhfit = true
151 ; crophack = false
152 ; autoscroll = false
153 ; showall = false
154 ; hlinks = false
158 let state =
159 { csock = Unix.stdin
160 ; ssock = Unix.stdin
161 ; w = 900
162 ; h = 900
163 ; rotate = 0
164 ; y = 0
165 ; ty = 0.0
166 ; layout = []
167 ; maxy = max_int
168 ; pagemap = Hashtbl.create 10
169 ; pagecache = cbnew 10 ""
170 ; pages = []
171 ; pagecount = 0
172 ; rendering = false
173 ; mstate = Mnone
174 ; rects = []
175 ; rects1 = []
176 ; text = ""
177 ; fullscreen = None
178 ; textentry = None
179 ; searchpattern = ""
180 ; outlines = Olist []
181 ; outline = None
182 ; bookmarks = []
183 ; path = ""
184 ; invalidated = 0
185 ; hists =
186 { nav = cbnew 100 0.0
187 ; pat = cbnew 20 ""
188 ; pag = cbnew 10 ""
190 ; colorscale = 1.0
194 let vlog fmt =
195 if conf.verbose
196 then
197 Printf.kprintf prerr_endline fmt
198 else
199 Printf.kprintf ignore fmt
202 let writecmd fd s =
203 let len = String.length s in
204 let n = 4 + len in
205 let b = Buffer.create n in
206 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
207 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
208 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
209 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
210 Buffer.add_string b s;
211 let s' = Buffer.contents b in
212 let n' = Unix.write fd s' 0 n in
213 if n' != n then failwith "write failed";
216 let readcmd fd =
217 let s = "xxxx" in
218 let n = Unix.read fd s 0 4 in
219 if n != 4 then failwith "incomplete read(len)";
220 let len = 0
221 lor (Char.code s.[0] lsl 24)
222 lor (Char.code s.[1] lsl 16)
223 lor (Char.code s.[2] lsl 8)
224 lor (Char.code s.[3] lsl 0)
226 let s = String.create len in
227 let n = Unix.read fd s 0 len in
228 if n != len then failwith "incomplete read(data)";
232 let yratio y =
233 if y = state.maxy then 1.0
234 else float y /. float state.maxy
237 let makecmd s l =
238 let b = Buffer.create 10 in
239 Buffer.add_string b s;
240 let rec combine = function
241 | [] -> b
242 | x :: xs ->
243 Buffer.add_char b ' ';
244 let s =
245 match x with
246 | `b b -> if b then "1" else "0"
247 | `s s -> s
248 | `i i -> string_of_int i
249 | `f f -> string_of_float f
250 | `I f -> string_of_int (truncate f)
252 Buffer.add_string b s;
253 combine xs;
255 combine l;
258 let wcmd s l =
259 let cmd = Buffer.contents (makecmd s l) in
260 writecmd state.csock cmd;
263 let calcheight () =
264 let rec f pn ph fh l =
265 match l with
266 | (n, _, h) :: rest ->
267 let fh = fh + (n - pn) * ph in
268 f n h fh rest
270 | [] ->
271 let fh = fh + (ph * (state.pagecount - pn)) in
272 max 0 fh
274 let fh = f 0 0 0 state.pages in
278 let getpagey pageno =
279 let rec f pn ph y l =
280 match l with
281 | (n, _, h) :: rest ->
282 if n >= pageno
283 then
284 y + (pageno - pn) * ph
285 else
286 let y = y + (n - pn) * ph in
287 f n h y rest
289 | [] ->
290 y + (pageno - pn) * ph
292 f 0 0 0 state.pages;
295 let layout y sh =
296 let rec f pageno pdimno prev vy py dy l cacheleft accu =
297 if pageno = state.pagecount || cacheleft = 0
298 then accu
299 else
300 let ((_, w, h) as curr), rest, pdimno =
301 match l with
302 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
303 curr, rest, pdimno + 1
304 | _ ->
305 prev, l, pdimno
307 let pageno' = pageno + 1 in
308 if py + h > vy
309 then
310 let py' = vy - py in
311 let vh = h - py' in
312 if dy + vh > sh
313 then
314 let vh = sh - dy in
315 if vh <= 0
316 then
317 accu
318 else
319 let e =
320 { pageno = pageno
321 ; pagedimno = pdimno
322 ; pagew = w
323 ; pageh = h
324 ; pagedispy = dy
325 ; pagey = py'
326 ; pagevh = vh
329 e :: accu
330 else
331 let e =
332 { pageno = pageno
333 ; pagedimno = pdimno
334 ; pagew = w
335 ; pageh = h
336 ; pagedispy = dy
337 ; pagey = py'
338 ; pagevh = vh
341 let accu = e :: accu in
342 f pageno' pdimno curr
343 (vy + vh) (py + h) (dy + vh + 2) rest
344 (pred cacheleft) accu
345 else
346 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
348 if state.invalidated = 0
349 then
350 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
351 state.maxy <- calcheight ();
352 List.rev accu
353 else
357 let clamp incr =
358 let y = state.y + incr in
359 let y = max 0 y in
360 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
364 let getopaque pageno =
365 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
366 state.rotate))
367 with Not_found -> None
370 let cache pageno opaque =
371 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
372 state.rotate) opaque
375 let validopaque opaque = String.length opaque > 0;;
377 let render l =
378 match getopaque l.pageno with
379 | None when not state.rendering ->
380 state.rendering <- true;
381 cache l.pageno "";
382 wcmd "render" [`i (l.pageno + 1)
383 ;`i l.pagedimno
384 ;`i l.pagew
385 ;`i l.pageh];
387 | _ -> ()
390 let loadlayout layout =
391 let rec f all = function
392 | l :: ls ->
393 begin match getopaque l.pageno with
394 | None -> render l; f false ls
395 | Some opaque -> f (all && validopaque opaque) ls
397 | [] -> all
399 f (layout <> []) layout;
402 let gotoy y =
403 let y = max 0 y in
404 let y = min state.maxy y in
405 let pages = layout y state.h in
406 let ready = loadlayout pages in
407 state.ty <- yratio y;
408 if conf.showall then (
409 if ready then (
410 state.layout <- pages;
411 state.y <- y;
412 Glut.postRedisplay ();
415 else (
416 state.layout <- pages;
417 state.y <- y;
418 Glut.postRedisplay ();
420 if conf.preload then begin
421 let y = if state.y < state.h then 0 else state.y - state.h in
422 let pages = layout y (state.h*3) in
423 List.iter render pages;
424 end;
427 let addnav () =
428 cbput state.hists.nav (yratio state.y);
429 cbrfollowlen state.hists.nav;
432 let getnav () =
433 let y = cbget state.hists.nav ~-1 in
434 truncate (y *. float state.maxy)
437 let gotopage n top =
438 let y = getpagey n in
439 addnav ();
440 gotoy (y + top);
443 let invalidate () =
444 state.layout <- [];
445 state.pages <- [];
446 state.rects <- [];
447 state.rects1 <- [];
448 state.invalidated <- state.invalidated + 1;
451 let scalecolor c =
452 let c = c *. state.colorscale in
453 (c, c, c);
456 let reshape ~w ~h =
457 let ratio = float w /. float state.w in
458 let fixbookmark (s, l, pageno, pagey) =
459 let pagey = truncate (float pagey *. ratio) in
460 (s, l, pageno, pagey)
462 state.bookmarks <- List.map fixbookmark state.bookmarks;
463 state.w <- w;
464 state.h <- h;
465 GlDraw.viewport 0 0 w h;
466 GlMat.mode `modelview;
467 GlMat.load_identity ();
468 GlMat.mode `projection;
469 GlMat.load_identity ();
470 GlMat.rotate ~x:1.0 ~angle:180.0 ();
471 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
472 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
473 GlClear.color (scalecolor 1.0);
474 GlClear.clear [`color];
476 invalidate ();
477 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
480 let showtext c s =
481 GlDraw.color (0.0, 0.0, 0.0);
482 GlDraw.rect
483 (0.0, float (state.h - 18))
484 (float (state.w - conf.scrollw - 1), float state.h)
486 let font = Glut.BITMAP_8_BY_13 in
487 GlDraw.color (1.0, 1.0, 1.0);
488 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
489 Glut.bitmapCharacter ~font ~c:(Char.code c);
490 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
493 let enttext () =
494 let len = String.length state.text in
495 match state.textentry with
496 | None ->
497 if len > 0 then showtext ' ' state.text
499 | Some (c, text, _, _, _) ->
500 let s =
501 if len > 0
502 then
503 text ^ " [" ^ state.text ^ "]"
504 else
505 text
507 showtext c s;
510 let act cmd =
511 match cmd.[0] with
512 | 'c' ->
513 state.pages <- [];
514 state.outlines <- Olist []
516 | 'D' ->
517 state.rects <- state.rects1;
518 Glut.postRedisplay ()
520 | 'C' ->
521 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
522 state.pagecount <- n;
523 state.invalidated <- state.invalidated - 1;
524 if state.invalidated = 0
525 then (
526 let rely = yratio state.y in
527 state.maxy <- calcheight ();
528 gotoy (truncate (float state.maxy *. rely));
531 | 't' ->
532 let s = Scanf.sscanf cmd "t %n"
533 (fun n -> String.sub cmd n (String.length cmd - n))
535 Glut.setWindowTitle s
537 | 'T' ->
538 let s = Scanf.sscanf cmd "T %n"
539 (fun n -> String.sub cmd n (String.length cmd - n))
541 if state.textentry = None
542 then (
543 state.text <- s;
544 showtext ' ' s;
545 Glut.swapBuffers ();
547 else (
548 state.text <- s;
549 Glut.postRedisplay ();
552 | 'V' ->
553 if conf.verbose
554 then
555 let s = Scanf.sscanf cmd "V %n"
556 (fun n -> String.sub cmd n (String.length cmd - n))
558 state.text <- s;
559 showtext ' ' s;
560 Glut.swapBuffers ();
562 | 'F' ->
563 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
564 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
565 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
566 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
568 let y = (getpagey pageno) + truncate y0 in
569 addnav ();
570 gotoy y;
571 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
573 | 'R' ->
574 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
575 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
576 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
577 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
579 state.rects1 <-
580 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
582 | 'r' ->
583 let n, w, h, r, p =
584 Scanf.sscanf cmd "r %d %d %d %d %s"
585 (fun n w h r p -> (n, w, h, r, p))
587 Hashtbl.replace state.pagemap (n, w, r) p;
588 let opaque = cbpeekw state.pagecache in
589 if validopaque opaque
590 then (
591 let k =
592 Hashtbl.fold
593 (fun k v a -> if v = opaque then k else a)
594 state.pagemap (-1, -1, -1)
596 wcmd "free" [`s opaque];
597 Hashtbl.remove state.pagemap k
599 cbput state.pagecache p;
600 state.rendering <- false;
601 if conf.showall
602 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
603 else gotoy state.y
605 | 'l' ->
606 let (n, w, h) as pagelayout =
607 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
609 state.pages <- pagelayout :: state.pages
611 | 'o' ->
612 let (l, n, t, pos) =
613 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
615 let s = String.sub cmd pos (String.length cmd - pos) in
616 let outline = (s, l, n, t) in
617 let outlines =
618 match state.outlines with
619 | Olist outlines -> Olist (outline :: outlines)
620 | Oarray _ -> Olist [outline]
621 | Onarrow _ -> Olist [outline]
623 state.outlines <- outlines
625 | _ ->
626 log "unknown cmd `%S'" cmd
629 let now = Unix.gettimeofday;;
631 let idle () =
632 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
633 begin match r with
634 | [] ->
635 if conf.autoscroll then begin
636 let y = state.y + conf.scrollincr in
637 let y = if y >= state.maxy then 0 else y in
638 gotoy y;
639 state.text <- "";
640 end;
642 | _ ->
643 let cmd = readcmd state.csock in
644 act cmd;
645 end;
648 let onhist cb = function
649 | HCprev -> cbget cb ~-1
650 | HCnext -> cbget cb 1
651 | HCfirst -> cbget cb ~-(cb.rc)
652 | HClast -> cbget cb (cb.len - 1 - cb.rc)
655 let search pattern forward =
656 if String.length pattern > 0
657 then
658 let pn, py =
659 match state.layout with
660 | [] -> 0, 0
661 | l :: _ ->
662 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
664 let cmd =
665 let b = makecmd "search"
666 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
668 Buffer.add_char b ',';
669 Buffer.add_string b pattern;
670 Buffer.add_char b '\000';
671 Buffer.contents b;
673 writecmd state.csock cmd;
676 let intentry text key =
677 let c = Char.unsafe_chr key in
678 match c with
679 | '0' .. '9' ->
680 let s = "x" in s.[0] <- c;
681 let text = text ^ s in
682 TEcont text
684 | _ ->
685 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
686 TEcont text
689 let addchar s c =
690 let b = Buffer.create (String.length s + 1) in
691 Buffer.add_string b s;
692 Buffer.add_char b c;
693 Buffer.contents b;
696 let textentry text key =
697 let c = Char.unsafe_chr key in
698 match c with
699 | _ when key >= 32 && key < 127 ->
700 let text = addchar text c in
701 TEcont text
703 | _ ->
704 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
705 TEcont text
708 let rotate angle =
709 state.rotate <- angle;
710 invalidate ();
711 wcmd "rotate" [`i angle];
714 let optentry text key =
715 let btos b = if b then "on" else "off" in
716 let c = Char.unsafe_chr key in
717 match c with
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 "Document has no outline";
814 let enterbookmarkmode () =
815 let bookmarks = Array.of_list state.bookmarks in
816 enterselector true bookmarks "Document 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 state.colorscale <-
1049 max 0.0
1050 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1051 Glut.postRedisplay ()
1053 | _ ->
1054 vlog "huh? %d %c" key (Char.chr key);
1057 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1058 let len = String.length text in
1059 if len = 0
1060 then (
1061 state.textentry <- None;
1062 Glut.postRedisplay ();
1064 else (
1065 let s = String.sub text 0 (len - 1) in
1066 enttext (Some (c, s, onhist, onkey, ondone))
1069 | Some (c, text, onhist, onkey, ondone) ->
1070 begin match Char.unsafe_chr key with
1071 | '\r' | '\n' ->
1072 ondone text;
1073 state.textentry <- None;
1074 Glut.postRedisplay ()
1076 | '\027' ->
1077 state.textentry <- None;
1078 Glut.postRedisplay ()
1080 | _ ->
1081 begin match onkey text key with
1082 | TEdone text ->
1083 state.textentry <- None;
1084 ondone text;
1085 Glut.postRedisplay ()
1087 | TEcont text ->
1088 enttext (Some (c, text, onhist, onkey, ondone));
1090 | TEstop ->
1091 state.textentry <- None;
1092 Glut.postRedisplay ()
1094 | TEswitch te ->
1095 state.textentry <- Some te;
1096 Glut.postRedisplay ()
1097 end;
1098 end;
1101 let narrow outlines pattern =
1102 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1103 match reopt with
1104 | None -> None
1105 | Some re ->
1106 let rec fold accu n =
1107 if n = -1 then accu else
1108 let (s, _, _, _) as o = outlines.(n) in
1109 let accu =
1110 if (try ignore (Str.search_forward re s 0); true
1111 with Not_found -> false)
1112 then (o :: accu)
1113 else accu
1115 fold accu (n-1)
1117 let matched = fold [] (Array.length outlines - 1) in
1118 if matched = [] then None else Some (Array.of_list matched)
1121 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1122 let search active pattern incr =
1123 let dosearch re =
1124 let rec loop n =
1125 if n = Array.length outlines || n = -1 then None else
1126 let (s, _, _, _) = outlines.(n) in
1128 (try ignore (Str.search_forward re s 0); true
1129 with Not_found -> false)
1130 then Some n
1131 else loop (n + incr)
1133 loop active
1136 let re = Str.regexp_case_fold pattern in
1137 dosearch re
1138 with Failure s ->
1139 state.text <- s;
1140 None
1142 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1143 match key with
1144 | 27 ->
1145 if String.length qsearch = 0
1146 then (
1147 state.text <- "";
1148 state.outline <- None;
1149 Glut.postRedisplay ();
1151 else (
1152 state.text <- "";
1153 state.outline <- Some (allowdel, active, first, outlines, "");
1154 Glut.postRedisplay ();
1157 | 18 | 19 ->
1158 let incr = if key = 18 then -1 else 1 in
1159 let active, first =
1160 match search (active + incr) qsearch incr with
1161 | None ->
1162 state.text <- qsearch ^ " [not found]";
1163 active, first
1164 | Some active ->
1165 state.text <- qsearch;
1166 active, firstof active
1168 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1169 Glut.postRedisplay ();
1171 | 8 ->
1172 let len = String.length qsearch in
1173 if len = 0
1174 then ()
1175 else (
1176 if len = 1
1177 then (
1178 state.text <- "";
1179 state.outline <- Some (allowdel, active, first, outlines, "");
1181 else
1182 let qsearch = String.sub qsearch 0 (len - 1) in
1183 let active, first =
1184 match search active qsearch ~-1 with
1185 | None ->
1186 state.text <- qsearch ^ " [not found]";
1187 active, first
1188 | Some active ->
1189 state.text <- qsearch;
1190 active, firstof active
1192 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1194 Glut.postRedisplay ()
1196 | 13 ->
1197 if active < Array.length outlines
1198 then (
1199 let (_, _, n, t) = outlines.(active) in
1200 gotopage n t;
1202 state.text <- "";
1203 if allowdel then state.bookmarks <- Array.to_list outlines;
1204 state.outline <- None;
1205 Glut.postRedisplay ();
1207 | _ when key >= 32 && key < 127 ->
1208 let pattern = addchar qsearch (Char.chr key) in
1209 let active, first =
1210 match search active pattern 1 with
1211 | None ->
1212 state.text <- pattern ^ " [not found]";
1213 active, first
1214 | Some active ->
1215 state.text <- pattern;
1216 active, firstof active
1218 state.outline <- Some (allowdel, active, first, outlines, pattern);
1219 Glut.postRedisplay ()
1221 | 14 when not allowdel ->
1222 let optoutlines = narrow outlines qsearch in
1223 begin match optoutlines with
1224 | None -> state.text <- "can't narrow"
1225 | Some outlines ->
1226 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1227 match state.outlines with
1228 | Olist l -> ()
1229 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1230 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1231 end;
1232 Glut.postRedisplay ()
1234 | 21 when not allowdel ->
1235 let outline =
1236 match state.outlines with
1237 | Oarray a -> a
1238 | Olist l ->
1239 let a = Array.of_list (List.rev l) in
1240 state.outlines <- Oarray a;
1242 | Onarrow (a, b) ->
1243 state.outlines <- Oarray b;
1246 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1247 Glut.postRedisplay ()
1249 | 12 ->
1250 state.outline <-
1251 Some (allowdel, active, firstof active, outlines, qsearch);
1252 Glut.postRedisplay ()
1254 | 127 when allowdel ->
1255 let len = Array.length outlines - 1 in
1256 if len = 0
1257 then (
1258 state.outline <- None;
1259 state.bookmarks <- [];
1261 else (
1262 let bookmarks = Array.init len
1263 (fun i ->
1264 let i = if i >= active then i + 1 else i in
1265 outlines.(i)
1268 state.outline <-
1269 Some (allowdel,
1270 min active (len-1),
1271 min first (len-1),
1272 bookmarks, qsearch)
1275 Glut.postRedisplay ()
1277 | _ -> log "unknown key %d" key
1280 let keyboard ~key ~x ~y =
1281 if key = 7
1282 then
1283 wcmd "interrupt" []
1284 else
1285 match state.outline with
1286 | None -> viewkeyboard ~key ~x ~y
1287 | Some outline -> outlinekeyboard ~key ~x ~y outline
1290 let special ~key ~x ~y =
1291 match state.outline with
1292 | None ->
1293 begin match state.textentry with
1294 | None ->
1295 let y =
1296 match key with
1297 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1298 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1299 | Glut.KEY_DOWN -> clamp conf.scrollincr
1300 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1301 | Glut.KEY_PAGE_DOWN -> clamp state.h
1302 | Glut.KEY_HOME -> addnav (); 0
1303 | Glut.KEY_END ->
1304 addnav ();
1305 state.maxy - (if conf.maxhfit then state.h else 0)
1306 | _ -> state.y
1308 state.text <- "";
1309 gotoy y
1311 | Some (c, s, Some onhist, onkey, ondone) ->
1312 let s =
1313 match key with
1314 | Glut.KEY_UP -> onhist HCprev
1315 | Glut.KEY_DOWN -> onhist HCnext
1316 | Glut.KEY_HOME -> onhist HCfirst
1317 | Glut.KEY_END -> onhist HClast
1318 | _ -> state.text
1320 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1321 Glut.postRedisplay ()
1323 | _ -> ()
1326 | Some (allowdel, active, first, outlines, qsearch) ->
1327 let maxrows = maxoutlinerows () in
1328 let navigate incr =
1329 let active = active + incr in
1330 let active = max 0 (min active (Array.length outlines - 1)) in
1331 let first =
1332 if active > first
1333 then
1334 let rows = active - first in
1335 if rows > maxrows then active - maxrows else first
1336 else active
1338 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1339 Glut.postRedisplay ()
1341 match key with
1342 | Glut.KEY_UP -> navigate ~-1
1343 | Glut.KEY_DOWN -> navigate 1
1344 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1345 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1347 | Glut.KEY_HOME ->
1348 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1349 Glut.postRedisplay ()
1351 | Glut.KEY_END ->
1352 let active = Array.length outlines - 1 in
1353 let first = max 0 (active - maxrows) in
1354 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1355 Glut.postRedisplay ()
1357 | _ -> ()
1360 let drawplaceholder l =
1361 GlDraw.color (scalecolor 1.0);
1362 GlDraw.rect
1363 (0.0, float l.pagedispy)
1364 (float l.pagew, float (l.pagedispy + l.pagevh))
1366 let x = 0.0
1367 and y = float (l.pagedispy + 13) in
1368 let font = Glut.BITMAP_8_BY_13 in
1369 GlDraw.color (0.0, 0.0, 0.0);
1370 GlPix.raster_pos ~x ~y ();
1371 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1372 ("Loading " ^ string_of_int l.pageno);
1375 let now () = Unix.gettimeofday ();;
1377 let drawpage i l =
1378 begin match getopaque l.pageno with
1379 | Some opaque when validopaque opaque ->
1380 if state.textentry = None
1381 then GlDraw.color (scalecolor 1.0)
1382 else GlDraw.color (scalecolor 0.4);
1383 let a = now () in
1384 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1385 let b = now () in
1386 let d = b-.a in
1387 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1388 vlog "draw %f sec" d;
1390 | _ ->
1391 drawplaceholder l
1392 end;
1393 GlDraw.color (0.5, 0.5, 0.5);
1394 GlDraw.rect
1395 (0., float i)
1396 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1398 l.pagedispy + l.pagevh;
1401 let scrollindicator () =
1402 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1403 GlDraw.color (0.64 , 0.64, 0.64);
1404 GlDraw.rect
1405 (float (state.w - conf.scrollw), 0.)
1406 (float state.w, float state.h)
1408 GlDraw.color (0.0, 0.0, 0.0);
1409 let sh = (float (maxy + state.h) /. float state.h) in
1410 let sh = float state.h /. sh in
1411 let sh = max sh (float conf.scrollh) in
1413 let percent =
1414 if state.y = state.maxy
1415 then 1.0
1416 else float state.y /. float maxy
1418 let position = (float state.h -. sh) *. percent in
1420 let position =
1421 if position +. sh > float state.h
1422 then
1423 float state.h -. sh
1424 else
1425 position
1427 GlDraw.rect
1428 (float (state.w - conf.scrollw), position)
1429 (float state.w, position +. sh)
1433 let showsel () =
1434 match state.mstate with
1435 | Mnone ->
1438 | Msel ((x0, y0), (x1, y1)) ->
1439 let f l =
1440 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1441 || ((y1 >= l.pagedispy))
1442 then
1443 match getopaque l.pageno with
1444 | Some opaque when validopaque opaque ->
1445 let oy = -l.pagey + l.pagedispy in
1446 seltext opaque (x0, y0, x1, y1) oy
1447 | _ -> ()
1449 List.iter f state.layout
1452 let showrects () =
1453 Gl.enable `blend;
1454 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1455 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1456 List.iter
1457 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1458 List.iter (fun l ->
1459 if l.pageno = pageno
1460 then (
1461 let d = float (l.pagedispy - l.pagey) in
1462 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1463 GlDraw.begins `quads;
1465 GlDraw.vertex2 (x0, y0+.d);
1466 GlDraw.vertex2 (x1, y1+.d);
1467 GlDraw.vertex2 (x2, y2+.d);
1468 GlDraw.vertex2 (x3, y3+.d);
1470 GlDraw.ends ();
1472 ) state.layout
1473 ) state.rects
1475 Gl.disable `blend;
1478 let showoutline = function
1479 | None -> ()
1480 | Some (allowdel, active, first, outlines, qsearch) ->
1481 Gl.enable `blend;
1482 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1483 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1484 GlDraw.rect (0., 0.) (float state.w, float state.h);
1485 Gl.disable `blend;
1487 GlDraw.color (1., 1., 1.);
1488 let font = Glut.BITMAP_9_BY_15 in
1489 let draw_string x y s =
1490 GlPix.raster_pos ~x ~y ();
1491 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1493 let rec loop row =
1494 if row = Array.length outlines || (row - first) * 16 > state.h
1495 then ()
1496 else (
1497 let (s, l, _, _) = outlines.(row) in
1498 let y = (row - first) * 16 in
1499 let x = 5 + 15*l in
1500 if row = active
1501 then (
1502 Gl.enable `blend;
1503 GlDraw.polygon_mode `both `line;
1504 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1505 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1506 GlDraw.rect (0., float (y + 1))
1507 (float (state.w - conf.scrollw - 1), float (y + 18));
1508 GlDraw.polygon_mode `both `fill;
1509 Gl.disable `blend;
1510 GlDraw.color (1., 1., 1.);
1512 draw_string (float x) (float (y + 16)) s;
1513 loop (row+1)
1516 loop first
1519 let display () =
1520 let lasty = List.fold_left drawpage 0 (state.layout) in
1521 GlDraw.color (scalecolor 0.5);
1522 GlDraw.rect
1523 (0., float lasty)
1524 (float (state.w - conf.scrollw), float state.h)
1526 showrects ();
1527 scrollindicator ();
1528 showsel ();
1529 showoutline state.outline;
1530 enttext ();
1531 Glut.swapBuffers ();
1534 let getlink x y =
1535 let rec f = function
1536 | l :: rest ->
1537 begin match getopaque l.pageno with
1538 | Some opaque when validopaque opaque ->
1539 let y = y - l.pagedispy in
1540 if y > 0
1541 then
1542 let y = l.pagey + y in
1543 match getlink opaque x y with
1544 | LNone -> f rest
1545 | link -> link
1546 else
1547 f rest
1548 | _ ->
1549 f rest
1551 | [] -> LNone
1553 f state.layout
1556 let mouse ~button ~bstate ~x ~y =
1557 match button with
1558 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1559 let incr =
1560 if n = 3
1561 then
1562 -conf.scrollincr
1563 else
1564 conf.scrollincr
1566 let incr = incr * 2 in
1567 let y = clamp incr in
1568 gotoy y
1570 | Glut.LEFT_BUTTON when state.outline = None ->
1571 let dest = if bstate = Glut.DOWN then getlink x y else LNone in
1572 begin match dest with
1573 | LGoto (pageno, top) ->
1574 if pageno >= 0
1575 then
1576 gotopage pageno top
1578 | LUri s ->
1579 print_endline s
1581 | LNone ->
1582 if bstate = Glut.DOWN
1583 then (
1584 if state.rotate mod 360 = 0 then (
1585 Glut.setCursor Glut.CURSOR_TEXT;
1586 state.mstate <- Msel ((x, y), (x, y));
1587 Glut.postRedisplay ()
1590 else (
1591 match state.mstate with
1592 | Mnone -> ()
1593 | Msel ((x0, y0), (x1, y1)) ->
1594 let f l =
1595 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1596 || ((y1 >= l.pagedispy))
1597 then
1598 match getopaque l.pageno with
1599 | Some opaque when validopaque opaque ->
1600 copysel opaque
1601 | _ -> ()
1603 List.iter f state.layout;
1604 copysel ""; (* ugly *)
1605 Glut.setCursor Glut.CURSOR_INHERIT;
1606 state.mstate <- Mnone;
1610 | _ ->
1613 let mouse ~button ~state ~x ~y = mouse button state x y;;
1615 let motion ~x ~y =
1616 if state.outline = None
1617 then
1618 match state.mstate with
1619 | Mnone -> ()
1620 | Msel (a, _) ->
1621 state.mstate <- Msel (a, (x, y));
1622 Glut.postRedisplay ()
1625 let pmotion ~x ~y =
1626 if state.outline = None
1627 then
1628 match state.mstate with
1629 | Mnone ->
1630 if getlink x y != LNone
1631 then Glut.setCursor Glut.CURSOR_INFO
1632 else Glut.setCursor Glut.CURSOR_INHERIT
1634 | Msel (a, _) ->
1638 let () =
1639 let statepath =
1640 let home =
1641 if Sys.os_type = "Win32"
1642 then
1643 try Sys.getenv "HOMEPATH" with Not_found -> ""
1644 else
1645 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1647 Filename.concat home "llpp"
1649 let pstate =
1651 let ic = open_in_bin statepath in
1652 let hash = input_value ic in
1653 close_in ic;
1654 hash
1655 with exn ->
1656 if false
1657 then
1658 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1660 Hashtbl.create 1
1662 let savestate () =
1664 let w, h =
1665 match state.fullscreen with
1666 | None -> state.w, state.h
1667 | Some wh -> wh
1669 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1670 let oc = open_out_bin statepath in
1671 output_value oc pstate
1672 with exn ->
1673 if false
1674 then
1675 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1678 let setstate () =
1680 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1681 state.w <- statew;
1682 state.h <- stateh;
1683 state.bookmarks <- statebookmarks;
1684 with Not_found -> ()
1685 | exn ->
1686 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1689 Arg.parse [] (fun s -> state.path <- s) "options:";
1690 let name =
1691 if String.length state.path = 0
1692 then (prerr_endline "filename missing"; exit 1)
1693 else state.path
1696 setstate ();
1697 let _ = Glut.init Sys.argv in
1698 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1699 let () = Glut.initWindowSize state.w state.h in
1700 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1702 let csock, ssock =
1703 if Sys.os_type = "Unix"
1704 then
1705 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1706 else
1707 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1708 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1709 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1710 Unix.bind sock addr;
1711 Unix.listen sock 1;
1712 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1713 Unix.connect csock addr;
1714 let ssock, _ = Unix.accept sock in
1715 Unix.close sock;
1716 let opts sock =
1717 Unix.setsockopt sock Unix.TCP_NODELAY true;
1718 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1720 opts ssock;
1721 opts csock;
1722 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1723 ssock, csock
1726 let () = Glut.displayFunc display in
1727 let () = Glut.reshapeFunc reshape in
1728 let () = Glut.keyboardFunc keyboard in
1729 let () = Glut.specialFunc special in
1730 let () = Glut.idleFunc (Some idle) in
1731 let () = Glut.mouseFunc mouse in
1732 let () = Glut.motionFunc motion in
1733 let () = Glut.passiveMotionFunc pmotion in
1735 init ssock;
1736 state.csock <- csock;
1737 state.ssock <- ssock;
1738 state.text <- "Opening " ^ name;
1739 writecmd csock ("open " ^ name ^ "\000");
1741 at_exit savestate;
1743 let rec handlelablglutbug () =
1745 Glut.mainLoop ();
1746 with Glut.BadEnum "key in special_of_int" ->
1747 showtext '!' " LablGlut bug: special key not recognized";
1748 Glut.swapBuffers ();
1749 handlelablglutbug ()
1751 handlelablglutbug ();