Make sure preloading takes effect immediately
[llpp.git] / main.ml
blobc3433986c834be7801387d2b45b5011f358d673e
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 highlightlinks : string -> int -> unit = "ml_highlightlinks";;
11 external getpagewh : int -> float array = "ml_getpagewh";;
13 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
15 type 'a circbuf =
16 { store : 'a array
17 ; mutable rc : int
18 ; mutable wc : int
19 ; mutable len : int
23 type textentry = (char * string * onhist option * onkey * ondone)
24 and onkey = string -> int -> te
25 and ondone = string -> unit
26 and onhist = histcmd -> string
27 and histcmd = HCnext | HCprev | HCfirst | HClast
28 and te =
29 | TEstop
30 | TEdone of string
31 | TEcont of string
32 | TEswitch of textentry
35 let cbnew n v =
36 { store = Array.create n v
37 ; rc = 0
38 ; wc = 0
39 ; len = 0
43 let cblen b = Array.length b.store;;
45 let cbput b v =
46 let len = cblen b in
47 b.store.(b.wc) <- v;
48 b.wc <- (b.wc + 1) mod len;
49 b.len <- min (b.len + 1) len;
52 let cbpeekw b = b.store.(b.wc);;
54 let cbget b dir =
55 if b.len = 0 then b.store.(0) else
56 let rc = b.rc + dir in
57 let rc = if rc = -1 then b.len - 1 else rc in
58 let rc = if rc = b.len then 0 else rc in
59 b.rc <- rc;
60 b.store.(rc);
63 let cbrfollowlen b =
64 b.rc <- b.len;
67 type layout =
68 { pageno : int
69 ; pagedimno : int
70 ; pagew : int
71 ; pageh : int
72 ; pagedispy : int
73 ; pagey : int
74 ; pagevh : int
78 type conf =
79 { mutable scrollw : int
80 ; mutable scrollh : int
81 ; mutable rectsel : bool
82 ; mutable icase : bool
83 ; mutable preload : bool
84 ; mutable pagebias : int
85 ; mutable verbose : bool
86 ; mutable scrollincr : int
87 ; mutable maxhfit : bool
88 ; mutable crophack : bool
89 ; mutable autoscroll : bool
90 ; mutable showall : bool
91 ; mutable hlinks : bool
95 type outline = string * int * int * int;;
96 type outlines =
97 | Oarray of outline array
98 | Olist of outline list
99 | Onarrow of outline array * outline array
102 type rect = (float * float * float * float * float * float * float * float);;
104 type state =
105 { mutable csock : Unix.file_descr
106 ; mutable ssock : Unix.file_descr
107 ; mutable w : int
108 ; mutable h : int
109 ; mutable rotate : int
110 ; mutable y : int
111 ; mutable ty : int
112 ; mutable prevy : int
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 inflight : int
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 ; hists : hists
134 and hists =
135 { pat : string circbuf
136 ; pag : string circbuf
137 ; nav : float circbuf
141 let conf =
142 { scrollw = 5
143 ; scrollh = 12
144 ; icase = true
145 ; rectsel = 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
166 ; prevy = 0
167 ; layout = []
168 ; maxy = max_int
169 ; pagemap = Hashtbl.create 10
170 ; pagecache = cbnew 10 ""
171 ; pages = []
172 ; pagecount = 0
173 ; inflight = 0
174 ; mstate = Mnone
175 ; rects = []
176 ; rects1 = []
177 ; text = ""
178 ; fullscreen = None
179 ; textentry = None
180 ; searchpattern = ""
181 ; outlines = Olist []
182 ; outline = None
183 ; bookmarks = []
184 ; path = ""
185 ; invalidated = 0
186 ; hists =
187 { nav = cbnew 100 0.0
188 ; pat = cbnew 20 ""
189 ; pag = cbnew 10 ""
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 preload l =
378 match getopaque l.pageno with
379 | None when state.inflight < 2+0*(cblen state.pagecache) ->
380 state.inflight <- succ state.inflight;
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 gotoy y =
391 let y = max 0 y in
392 let y = min state.maxy y in
393 let pages = layout y state.h in
394 let rec f all = function
395 | l :: ls ->
396 begin match getopaque l.pageno with
397 | None -> preload l; f false ls
398 | Some opaque -> f (all && validopaque opaque) ls
400 | [] -> all
402 if not conf.showall || f true pages
403 then (
404 state.y <- y;
405 state.layout <- pages;
407 state.ty <- y;
408 if conf.preload then begin
409 let h = state.h in
410 let y = if state.y < state.h then 0 else state.y - state.h in
411 let pages = layout y (h*3) in
412 List.iter preload pages;
413 end;
416 let addnav () =
417 cbput state.hists.nav (yratio state.y);
418 cbrfollowlen state.hists.nav;
421 let getnav () =
422 let y = cbget state.hists.nav ~-1 in
423 truncate (y *. float state.maxy)
426 let gotopage n top =
427 let y = getpagey n in
428 addnav ();
429 gotoy (y + top);
432 let invalidate () =
433 state.layout <- [];
434 state.pages <- [];
435 state.rects <- [];
436 state.rects1 <- [];
437 state.invalidated <- state.invalidated + 1;
440 let reshape ~w ~h =
441 let ratio = float w /. float state.w in
442 let fixbookmark (s, l, pageno, pagey) =
443 let pagey = truncate (float pagey *. ratio) in
444 (s, l, pageno, pagey)
446 state.bookmarks <- List.map fixbookmark state.bookmarks;
447 state.w <- w;
448 state.h <- h;
449 GlDraw.viewport 0 0 w h;
450 GlMat.mode `modelview;
451 GlMat.load_identity ();
452 GlMat.mode `projection;
453 GlMat.load_identity ();
454 GlMat.rotate ~x:1.0 ~angle:180.0 ();
455 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
456 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
457 GlClear.color (1., 1., 1.);
458 GlClear.clear [`color];
459 invalidate ();
460 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
463 let showtext c s =
464 GlDraw.color (0.0, 0.0, 0.0);
465 GlDraw.rect
466 (0.0, float (state.h - 18))
467 (float (state.w - conf.scrollw - 1), float state.h)
469 let font = Glut.BITMAP_8_BY_13 in
470 GlDraw.color (1.0, 1.0, 1.0);
471 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
472 Glut.bitmapCharacter ~font ~c:(Char.code c);
473 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
476 let enttext () =
477 let len = String.length state.text in
478 match state.textentry with
479 | None ->
480 if len > 0 then showtext ' ' state.text
482 | Some (c, text, _, _, _) ->
483 let s =
484 if len > 0
485 then
486 text ^ " [" ^ state.text ^ "]"
487 else
488 text
490 showtext c s;
493 let act cmd =
494 match cmd.[0] with
495 | 'c' ->
496 state.pages <- [];
497 state.outlines <- Olist []
499 | 'D' ->
500 state.rects <- state.rects1;
501 Glut.postRedisplay ()
503 | 'd' ->
504 state.rects <- state.rects1;
505 Glut.postRedisplay ()
507 | 'C' ->
508 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
509 state.pagecount <- n;
510 state.invalidated <- state.invalidated - 1;
511 if state.invalidated = 0
512 then (
513 let rely = yratio state.y in
514 let maxy = calcheight () in
515 state.y <- truncate (float maxy *. rely);
516 state.ty <- state.y;
517 let pages = layout state.y state.h in
518 state.layout <- pages;
519 Glut.postRedisplay ();
522 | 't' ->
523 let s = Scanf.sscanf cmd "t %n"
524 (fun n -> String.sub cmd n (String.length cmd - n))
526 Glut.setWindowTitle s
528 | 'T' ->
529 let s = Scanf.sscanf cmd "T %n"
530 (fun n -> String.sub cmd n (String.length cmd - n))
532 if state.textentry = None
533 then (
534 state.text <- s;
535 showtext ' ' s;
536 Glut.swapBuffers ();
538 else (
539 state.text <- s;
540 Glut.postRedisplay ();
543 | 'V' ->
544 if conf.verbose
545 then
546 let s = Scanf.sscanf cmd "V %n"
547 (fun n -> String.sub cmd n (String.length cmd - n))
549 state.text <- s;
550 showtext ' ' s;
551 Glut.swapBuffers ();
553 | 'F' ->
554 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
555 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
556 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
557 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
559 let y = (getpagey pageno) + truncate y0 in
560 addnav ();
561 gotoy y;
562 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
564 | 'R' ->
565 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
566 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
567 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
568 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
570 state.rects1 <-
571 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
573 | 'r' ->
574 let n, w, h, r, p =
575 Scanf.sscanf cmd "r %d %d %d %d %s"
576 (fun n w h r p -> (n, w, h, r, p))
578 Hashtbl.replace state.pagemap (n, w, r) p;
579 let evicted = cbpeekw state.pagecache in
580 if String.length evicted > 0
581 then begin
582 wcmd "free" [`s evicted];
583 let l = Hashtbl.fold (fun k p a ->
584 if evicted = p then k :: a else a) state.pagemap []
586 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
587 end;
588 cbput state.pagecache p;
589 state.inflight <- pred state.inflight;
590 if conf.showall then gotoy state.ty;
591 Glut.postRedisplay ()
593 | 'l' ->
594 let (n, w, h) as pagelayout =
595 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
597 state.pages <- pagelayout :: state.pages
599 | 'o' ->
600 let (l, n, t, pos) =
601 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
603 let s = String.sub cmd pos (String.length cmd - pos) in
604 let outline = (s, l, n, t) in
605 let outlines =
606 match state.outlines with
607 | Olist outlines -> Olist (outline :: outlines)
608 | Oarray _ -> Olist [outline]
609 | Onarrow _ -> Olist [outline]
611 state.outlines <- outlines
613 | _ ->
614 log "unknown cmd `%S'" cmd
617 let idle () =
618 if state.y != state.prevy
619 then (
620 state.prevy <- state.y;
621 Glut.postRedisplay ();
623 else
624 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
626 begin match r with
627 | [] ->
628 if conf.autoscroll then begin
629 let y = state.y + conf.scrollincr in
630 let y = if y >= state.maxy then 0 else y in
631 gotoy y;
632 state.text <- "";
633 state.prevy <- state.y;
634 Glut.postRedisplay ();
635 end;
637 | _ ->
638 let cmd = readcmd state.csock in
639 act cmd;
640 end;
643 let onhist cb = function
644 | HCprev -> cbget cb ~-1
645 | HCnext -> cbget cb 1
646 | HCfirst -> cbget cb ~-(cb.rc)
647 | HClast -> cbget cb (cb.len - 1 - cb.rc)
650 let search pattern forward =
651 if String.length pattern > 0
652 then
653 let pn, py =
654 match state.layout with
655 | [] -> 0, 0
656 | l :: _ ->
657 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
659 let cmd =
660 let b = makecmd "search"
661 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
663 Buffer.add_char b ',';
664 Buffer.add_string b pattern;
665 Buffer.add_char b '\000';
666 Buffer.contents b;
668 writecmd state.csock cmd;
671 let intentry text key =
672 let c = Char.unsafe_chr key in
673 match c with
674 | '0' .. '9' ->
675 let s = "x" in s.[0] <- c;
676 let text = text ^ s in
677 TEcont text
679 | _ ->
680 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
681 TEcont text
684 let addchar s c =
685 let b = Buffer.create (String.length s + 1) in
686 Buffer.add_string b s;
687 Buffer.add_char b c;
688 Buffer.contents b;
691 let textentry text key =
692 let c = Char.unsafe_chr key in
693 match c with
694 | _ when key >= 32 && key < 127 ->
695 let text = addchar text c in
696 TEcont text
698 | _ ->
699 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
700 TEcont text
703 let rotate angle =
704 state.rotate <- angle;
705 invalidate ();
706 wcmd "rotate" [`i angle];
709 let optentry text key =
710 let btos b = if b then "on" else "off" in
711 let c = Char.unsafe_chr key in
712 match c with
713 | 'r' ->
714 conf.rectsel <- not conf.rectsel;
715 TEdone ("rectsel " ^ (btos conf.rectsel))
717 | 's' ->
718 let ondone s =
719 try conf.scrollincr <- int_of_string s with exc ->
720 state.text <- Printf.sprintf "bad integer `%s': %s"
721 s (Printexc.to_string exc)
723 TEswitch ('#', "", None, intentry, ondone)
725 | 'R' ->
726 let ondone s =
727 match try
728 Some (int_of_string s)
729 with exc ->
730 state.text <- Printf.sprintf "bad integer `%s': %s"
731 s (Printexc.to_string exc);
732 None
733 with
734 | Some angle -> rotate angle
735 | None -> ()
737 TEswitch ('^', "", None, intentry, ondone)
739 | 'i' ->
740 conf.icase <- not conf.icase;
741 TEdone ("case insensitive search " ^ (btos conf.icase))
743 | 'p' ->
744 conf.preload <- not conf.preload;
745 gotoy state.y;
746 TEdone ("preload " ^ (btos conf.preload))
748 | 'v' ->
749 conf.verbose <- not conf.verbose;
750 TEdone ("verbose " ^ (btos conf.verbose))
752 | 'h' ->
753 conf.maxhfit <- not conf.maxhfit;
754 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
755 TEdone ("maxhfit " ^ (btos conf.maxhfit))
757 | 'c' ->
758 conf.crophack <- not conf.crophack;
759 TEdone ("crophack " ^ btos conf.crophack)
761 | 'a' ->
762 conf.showall <- not conf.showall;
763 TEdone ("showall " ^ btos conf.showall)
765 | _ ->
766 state.text <- Printf.sprintf "bad option %d `%c'" key c;
767 TEstop
770 let maxoutlinerows () = (state.h - 31) / 16;;
772 let enterselector allowdel outlines errmsg =
773 if Array.length outlines = 0
774 then (
775 showtext ' ' errmsg;
776 Glut.swapBuffers ()
778 else
779 let pageno =
780 match state.layout with
781 | [] -> -1
782 | {pageno=pageno} :: rest -> pageno
784 let active =
785 let rec loop n =
786 if n = Array.length outlines
787 then 0
788 else
789 let (_, _, outlinepageno, _) = outlines.(n) in
790 if outlinepageno >= pageno then n else loop (n+1)
792 loop 0
794 state.outline <-
795 Some (allowdel, active,
796 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
797 Glut.postRedisplay ();
800 let enteroutlinemode () =
801 let outlines =
802 match state.outlines with
803 | Oarray a -> a
804 | Olist l ->
805 let a = Array.of_list (List.rev l) in
806 state.outlines <- Oarray a;
808 | Onarrow (a, b) -> a
810 enterselector false outlines "Documents has no outline";
813 let enterbookmarkmode () =
814 let bookmarks = Array.of_list state.bookmarks in
815 enterselector true bookmarks "Documents has no bookmarks (yet)";
819 let quickbookmark ?title () =
820 match state.layout with
821 | [] -> ()
822 | l :: _ ->
823 let title =
824 match title with
825 | None ->
826 let sec = Unix.gettimeofday () in
827 let tm = Unix.localtime sec in
828 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
829 l.pageno
830 tm.Unix.tm_mday
831 tm.Unix.tm_mon
832 (tm.Unix.tm_year + 1900)
833 tm.Unix.tm_hour
834 tm.Unix.tm_min
835 | Some title -> title
837 state.bookmarks <-
838 (title, 0, l.pageno, l.pagey) :: state.bookmarks
841 let viewkeyboard ~key ~x ~y =
842 let enttext te =
843 state.textentry <- te;
844 state.text <- "";
845 enttext ();
846 Glut.postRedisplay ()
848 match state.textentry with
849 | None ->
850 let c = Char.chr key in
851 begin match c with
852 | '\027' | 'q' ->
853 exit 0
855 | '\008' ->
856 let y = getnav () in
857 gotoy y
859 | 'o' ->
860 enteroutlinemode ()
862 | 'u' ->
863 state.rects <- [];
864 state.text <- "";
865 Glut.postRedisplay ()
867 | '/' | '?' ->
868 let ondone isforw s =
869 cbput state.hists.pat s;
870 cbrfollowlen state.hists.pat;
871 state.searchpattern <- s;
872 search s isforw
874 enttext (Some (c, "", Some (onhist state.hists.pat),
875 textentry, ondone (c ='/')))
877 | '+' ->
878 let ondone s =
879 let n =
880 try int_of_string s with exc ->
881 state.text <- Printf.sprintf "bad integer `%s': %s"
882 s (Printexc.to_string exc);
883 max_int
885 if n != max_int
886 then (
887 conf.pagebias <- n;
888 state.text <- "page bias is now " ^ string_of_int n;
891 enttext (Some ('+', "", None, intentry, ondone))
893 | '-' ->
894 let ondone msg =
895 state.text <- msg;
897 enttext (Some ('-', "", None, optentry, ondone))
899 | '0' .. '9' ->
900 let ondone s =
901 let n =
902 try int_of_string s with exc ->
903 state.text <- Printf.sprintf "bad integer `%s': %s"
904 s (Printexc.to_string exc);
907 if n >= 0
908 then (
909 addnav ();
910 cbput state.hists.pag (string_of_int n);
911 cbrfollowlen state.hists.pag;
912 gotoy (getpagey (n + conf.pagebias - 1))
915 let pageentry text key =
916 match Char.unsafe_chr key with
917 | 'g' -> TEdone text
918 | _ -> intentry text key
920 let text = "x" in text.[0] <- c;
921 enttext (Some (':', text, Some (onhist state.hists.pag),
922 pageentry, ondone))
924 | 'b' ->
925 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
926 reshape state.w state.h;
928 | 'l' ->
929 conf.hlinks <- not conf.hlinks;
930 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
931 Glut.postRedisplay ()
933 | 'a' ->
934 conf.autoscroll <- not conf.autoscroll
936 | 'f' ->
937 begin match state.fullscreen with
938 | None ->
939 state.fullscreen <- Some (state.w, state.h);
940 Glut.fullScreen ()
941 | Some (w, h) ->
942 state.fullscreen <- None;
943 Glut.reshapeWindow ~w ~h
946 | 'g' ->
947 gotoy 0
949 | 'n' ->
950 search state.searchpattern true
952 | 'p' | 'N' ->
953 search state.searchpattern false
955 | 't' ->
956 begin match state.layout with
957 | [] -> ()
958 | l :: _ ->
959 gotoy (state.y - l.pagey);
962 | ' ' ->
963 begin match List.rev state.layout with
964 | [] -> ()
965 | l :: _ ->
966 gotoy (clamp (l.pageh - l.pagey))
969 | '\127' ->
970 begin match state.layout with
971 | [] -> ()
972 | l :: _ ->
973 gotoy (clamp (-l.pageh));
976 | '=' ->
977 let f (fn, ln) l =
978 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
980 let fn, ln = List.fold_left f (-1, -1) state.layout in
981 let s =
982 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
983 let percent =
984 if maxy <= 0
985 then 100.
986 else (100. *. (float state.y /. float maxy)) in
987 if fn = ln
988 then
989 Printf.sprintf "Page %d of %d %.2f%%"
990 (fn+1) state.pagecount percent
991 else
992 Printf.sprintf
993 "Pages %d-%d of %d %.2f%%"
994 (fn+1) (ln+1) state.pagecount percent
996 showtext ' ' s;
997 Glut.swapBuffers ()
999 | 'w' ->
1000 begin match state.layout with
1001 | [] -> ()
1002 | l :: _ ->
1003 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1004 Glut.postRedisplay ();
1007 | '\'' ->
1008 enterbookmarkmode ()
1010 | 'm' ->
1011 let ondone s =
1012 match state.layout with
1013 | l :: _ ->
1014 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1015 | _ -> ()
1017 enttext (Some ('~', "", None, textentry, ondone))
1019 | '~' ->
1020 quickbookmark ();
1021 showtext ' ' "Quick bookmark added";
1022 Glut.swapBuffers ()
1024 | 'z' ->
1025 begin match state.layout with
1026 | l :: _ ->
1027 let a = getpagewh l.pagedimno in
1028 let w, h =
1029 if conf.crophack
1030 then
1031 (truncate (1.8 *. (a.(1) -. a.(0))),
1032 truncate (1.2 *. (a.(3) -. a.(0))))
1033 else
1034 (truncate (a.(1) -. a.(0)),
1035 truncate (a.(3) -. a.(0)))
1037 Glut.reshapeWindow (w + conf.scrollw) h;
1038 Glut.postRedisplay ();
1040 | [] -> ()
1043 | '<' | '>' ->
1044 rotate (state.rotate + (if c = '>' then 30 else -30));
1046 | _ ->
1047 vlog "huh? %d %c" key (Char.chr key);
1050 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1051 let len = String.length text in
1052 if len = 0
1053 then (
1054 state.textentry <- None;
1055 Glut.postRedisplay ();
1057 else (
1058 let s = String.sub text 0 (len - 1) in
1059 enttext (Some (c, s, onhist, onkey, ondone))
1062 | Some (c, text, onhist, onkey, ondone) ->
1063 begin match Char.unsafe_chr key with
1064 | '\r' | '\n' ->
1065 ondone text;
1066 state.textentry <- None;
1067 Glut.postRedisplay ()
1069 | '\027' ->
1070 state.textentry <- None;
1071 Glut.postRedisplay ()
1073 | _ ->
1074 begin match onkey text key with
1075 | TEdone text ->
1076 state.textentry <- None;
1077 ondone text;
1078 Glut.postRedisplay ()
1080 | TEcont text ->
1081 enttext (Some (c, text, onhist, onkey, ondone));
1083 | TEstop ->
1084 state.textentry <- None;
1085 Glut.postRedisplay ()
1087 | TEswitch te ->
1088 state.textentry <- Some te;
1089 Glut.postRedisplay ()
1090 end;
1091 end;
1094 let narrow outlines pattern =
1095 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1096 match reopt with
1097 | None -> None
1098 | Some re ->
1099 let rec fold accu n =
1100 if n = -1 then accu else
1101 let (s, _, _, _) as o = outlines.(n) in
1102 let accu =
1103 if (try ignore (Str.search_forward re s 0); true
1104 with Not_found -> false)
1105 then (o :: accu)
1106 else accu
1108 fold accu (n-1)
1110 let matched = fold [] (Array.length outlines - 1) in
1111 if matched = [] then None else Some (Array.of_list matched)
1114 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1115 let search active pattern incr =
1116 let dosearch re =
1117 let rec loop n =
1118 if n = Array.length outlines || n = -1 then None else
1119 let (s, _, _, _) = outlines.(n) in
1121 (try ignore (Str.search_forward re s 0); true
1122 with Not_found -> false)
1123 then Some n
1124 else loop (n + incr)
1126 loop active
1129 let re = Str.regexp_case_fold pattern in
1130 dosearch re
1131 with Failure s ->
1132 state.text <- s;
1133 None
1135 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1136 match key with
1137 | 27 ->
1138 if String.length qsearch = 0
1139 then (
1140 state.text <- "";
1141 state.outline <- None;
1142 Glut.postRedisplay ();
1144 else (
1145 state.text <- "";
1146 state.outline <- Some (allowdel, active, first, outlines, "");
1147 Glut.postRedisplay ();
1150 | 18 | 19 ->
1151 let incr = if key = 18 then -1 else 1 in
1152 let active, first =
1153 match search (active + incr) qsearch incr with
1154 | None ->
1155 state.text <- qsearch ^ " [not found]";
1156 active, first
1157 | Some active ->
1158 state.text <- qsearch;
1159 active, firstof active
1161 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1162 Glut.postRedisplay ();
1164 | 8 ->
1165 let len = String.length qsearch in
1166 if len = 0
1167 then ()
1168 else (
1169 if len = 1
1170 then (
1171 state.text <- "";
1172 state.outline <- Some (allowdel, active, first, outlines, "");
1174 else
1175 let qsearch = String.sub qsearch 0 (len - 1) in
1176 let active, first =
1177 match search active qsearch ~-1 with
1178 | None ->
1179 state.text <- qsearch ^ " [not found]";
1180 active, first
1181 | Some active ->
1182 state.text <- qsearch;
1183 active, firstof active
1185 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1187 Glut.postRedisplay ()
1189 | 13 ->
1190 if active < Array.length outlines
1191 then (
1192 let (_, _, n, t) = outlines.(active) in
1193 gotopage n t;
1195 state.text <- "";
1196 if allowdel then state.bookmarks <- Array.to_list outlines;
1197 state.outline <- None;
1198 Glut.postRedisplay ();
1200 | _ when key >= 32 && key < 127 ->
1201 let pattern = addchar qsearch (Char.chr key) in
1202 let active, first =
1203 match search active pattern 1 with
1204 | None ->
1205 state.text <- pattern ^ " [not found]";
1206 active, first
1207 | Some active ->
1208 state.text <- pattern;
1209 active, firstof active
1211 state.outline <- Some (allowdel, active, first, outlines, pattern);
1212 Glut.postRedisplay ()
1214 | 14 when not allowdel ->
1215 let optoutlines = narrow outlines qsearch in
1216 begin match optoutlines with
1217 | None -> state.text <- "can't narrow"
1218 | Some outlines ->
1219 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1220 match state.outlines with
1221 | Olist l -> ()
1222 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1223 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1224 end;
1225 Glut.postRedisplay ()
1227 | 21 when not allowdel ->
1228 let outline =
1229 match state.outlines with
1230 | Oarray a -> a
1231 | Olist l ->
1232 let a = Array.of_list (List.rev l) in
1233 state.outlines <- Oarray a;
1235 | Onarrow (a, b) ->
1236 state.outlines <- Oarray b;
1239 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1240 Glut.postRedisplay ()
1242 | 12 ->
1243 state.outline <-
1244 Some (allowdel, active, firstof active, outlines, qsearch);
1245 Glut.postRedisplay ()
1247 | 127 when allowdel ->
1248 let len = Array.length outlines - 1 in
1249 if len = 0
1250 then (
1251 state.outline <- None;
1252 state.bookmarks <- [];
1254 else (
1255 let bookmarks = Array.init len
1256 (fun i ->
1257 let i = if i >= active then i + 1 else i in
1258 outlines.(i)
1261 state.outline <-
1262 Some (allowdel,
1263 min active (len-1),
1264 min first (len-1),
1265 bookmarks, qsearch)
1268 Glut.postRedisplay ()
1270 | _ -> log "unknown key %d" key
1273 let keyboard ~key ~x ~y =
1274 if key = 7
1275 then
1276 wcmd "interrupt" []
1277 else
1278 match state.outline with
1279 | None -> viewkeyboard ~key ~x ~y
1280 | Some outline -> outlinekeyboard ~key ~x ~y outline
1283 let special ~key ~x ~y =
1284 match state.outline with
1285 | None ->
1286 begin match state.textentry with
1287 | None ->
1288 let y =
1289 match key with
1290 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1291 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1292 | Glut.KEY_DOWN -> clamp conf.scrollincr
1293 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1294 | Glut.KEY_PAGE_DOWN -> clamp state.h
1295 | Glut.KEY_HOME -> addnav (); 0
1296 | Glut.KEY_END ->
1297 addnav ();
1298 state.maxy - (if conf.maxhfit then state.h else 0)
1299 | _ -> state.y
1301 state.text <- "";
1302 gotoy y
1304 | Some (c, s, Some onhist, onkey, ondone) ->
1305 let s =
1306 match key with
1307 | Glut.KEY_UP -> onhist HCprev
1308 | Glut.KEY_DOWN -> onhist HCnext
1309 | Glut.KEY_HOME -> onhist HCfirst
1310 | Glut.KEY_END -> onhist HClast
1311 | _ -> state.text
1313 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1314 Glut.postRedisplay ()
1316 | _ -> ()
1319 | Some (allowdel, active, first, outlines, qsearch) ->
1320 let maxrows = maxoutlinerows () in
1321 let navigate incr =
1322 let active = active + incr in
1323 let active = max 0 (min active (Array.length outlines - 1)) in
1324 let first =
1325 if active > first
1326 then
1327 let rows = active - first in
1328 if rows > maxrows then active - maxrows else first
1329 else active
1331 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1332 Glut.postRedisplay ()
1334 match key with
1335 | Glut.KEY_UP -> navigate ~-1
1336 | Glut.KEY_DOWN -> navigate 1
1337 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1338 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1340 | Glut.KEY_HOME ->
1341 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1342 Glut.postRedisplay ()
1344 | Glut.KEY_END ->
1345 let active = Array.length outlines - 1 in
1346 let first = max 0 (active - maxrows) in
1347 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1348 Glut.postRedisplay ()
1350 | _ -> ()
1353 let drawplaceholder l =
1354 GlDraw.color (1.0, 1.0, 1.0);
1355 GlDraw.rect
1356 (0.0, float l.pagedispy)
1357 (float l.pagew, float (l.pagedispy + l.pagevh))
1359 let x = 0.0
1360 and y = float (l.pagedispy + 13) in
1361 let font = Glut.BITMAP_8_BY_13 in
1362 GlDraw.color (0.0, 0.0, 0.0);
1363 GlPix.raster_pos ~x ~y ();
1364 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1365 ("Loading " ^ string_of_int l.pageno);
1368 let now () = Unix.gettimeofday ();;
1370 let drawpage i l =
1371 begin match getopaque l.pageno with
1372 | Some opaque when validopaque opaque ->
1373 if state.textentry = None
1374 then GlDraw.color (1.0, 1.0, 1.0)
1375 else GlDraw.color (0.4, 0.4, 0.4);
1376 let a = now () in
1377 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1378 let b = now () in
1379 let d = b-.a in
1380 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1381 vlog "draw %f sec" d;
1383 | Some _ ->
1384 drawplaceholder l
1386 | None ->
1387 drawplaceholder l;
1388 if state.inflight < cblen state.pagecache
1389 then (
1390 List.iter preload state.layout;
1392 else (
1393 vlog "inflight %d" state.inflight;
1395 end;
1396 GlDraw.color (0.5, 0.5, 0.5);
1397 GlDraw.rect
1398 (0., float i)
1399 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1401 l.pagedispy + l.pagevh;
1404 let scrollindicator () =
1405 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1406 GlDraw.color (0.64 , 0.64, 0.64);
1407 GlDraw.rect
1408 (float (state.w - conf.scrollw), 0.)
1409 (float state.w, float state.h)
1411 GlDraw.color (0.0, 0.0, 0.0);
1412 let sh = (float (maxy + state.h) /. float state.h) in
1413 let sh = float state.h /. sh in
1414 let sh = max sh (float conf.scrollh) in
1416 let percent =
1417 if state.y = state.maxy
1418 then 1.0
1419 else float state.y /. float maxy
1421 let position = (float state.h -. sh) *. percent in
1423 let position =
1424 if position +. sh > float state.h
1425 then
1426 float state.h -. sh
1427 else
1428 position
1430 GlDraw.rect
1431 (float (state.w - conf.scrollw), position)
1432 (float state.w, position +. sh)
1436 let showsel () =
1437 match state.mstate with
1438 | Mnone ->
1441 | Msel ((x0, y0), (x1, y1)) ->
1442 let y0' = min y0 y1
1443 and y1 = max y0 y1 in
1444 let y0 = y0' in
1445 let f l =
1446 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1447 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1448 then
1449 match getopaque l.pageno with
1450 | Some opaque when validopaque opaque ->
1451 let oy = -l.pagey + l.pagedispy in
1452 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1453 | _ -> ()
1455 List.iter f state.layout
1458 let showrects () =
1459 Gl.enable `blend;
1460 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1461 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1462 List.iter
1463 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1464 List.iter (fun l ->
1465 if l.pageno = pageno
1466 then (
1467 let d = float (l.pagedispy - l.pagey) in
1468 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1469 GlDraw.begins `quads;
1471 GlDraw.vertex2 (x0, y0+.d);
1472 GlDraw.vertex2 (x1, y1+.d);
1473 GlDraw.vertex2 (x2, y2+.d);
1474 GlDraw.vertex2 (x3, y3+.d);
1476 GlDraw.ends ();
1477 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1479 ) state.layout
1480 ) state.rects
1482 Gl.disable `blend;
1485 let showoutline = function
1486 | None -> ()
1487 | Some (allowdel, active, first, outlines, qsearch) ->
1488 Gl.enable `blend;
1489 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1490 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1491 GlDraw.rect (0., 0.) (float state.w, float state.h);
1492 Gl.disable `blend;
1494 GlDraw.color (1., 1., 1.);
1495 let font = Glut.BITMAP_9_BY_15 in
1496 let draw_string x y s =
1497 GlPix.raster_pos ~x ~y ();
1498 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1500 let rec loop row =
1501 if row = Array.length outlines || (row - first) * 16 > state.h
1502 then ()
1503 else (
1504 let (s, l, _, _) = outlines.(row) in
1505 let y = (row - first) * 16 in
1506 let x = 5 + 15*l in
1507 if row = active
1508 then (
1509 Gl.enable `blend;
1510 GlDraw.polygon_mode `both `line;
1511 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1512 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1513 GlDraw.rect (0., float (y + 1))
1514 (float (state.w - conf.scrollw - 1), float (y + 18));
1515 GlDraw.polygon_mode `both `fill;
1516 Gl.disable `blend;
1517 GlDraw.color (1., 1., 1.);
1519 draw_string (float x) (float (y + 16)) s;
1520 loop (row+1)
1523 loop first
1526 let display () =
1527 let lasty = List.fold_left drawpage 0 (state.layout) in
1528 GlDraw.color (0.5, 0.5, 0.5);
1529 GlDraw.rect
1530 (0., float lasty)
1531 (float (state.w - conf.scrollw), float state.h)
1533 showrects ();
1534 scrollindicator ();
1535 showsel ();
1536 showoutline state.outline;
1537 enttext ();
1538 Glut.swapBuffers ();
1541 let getlink x y =
1542 let rec f = function
1543 | l :: rest ->
1544 begin match getopaque l.pageno with
1545 | Some opaque when validopaque opaque ->
1546 let y = y - l.pagedispy in
1547 if y > 0
1548 then
1549 let y = l.pagey + y in
1550 match getlink opaque x y with
1551 | None -> f rest
1552 | some -> some
1553 else
1554 f rest
1555 | _ ->
1556 f rest
1558 | [] -> None
1560 f state.layout
1563 let checklink x y =
1564 let rec f = function
1565 | l :: rest ->
1566 begin match getopaque l.pageno with
1567 | Some opaque when validopaque opaque ->
1568 let y = y - l.pagedispy in
1569 if y > 0
1570 then
1571 let y = l.pagey + y in
1572 if checklink opaque x y then true else f rest
1573 else
1574 f rest
1575 | _ ->
1576 f rest
1578 | [] -> false
1580 f state.layout
1583 let mouse ~button ~bstate ~x ~y =
1584 match button with
1585 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1586 let incr =
1587 if n = 3
1588 then
1589 -conf.scrollincr
1590 else
1591 conf.scrollincr
1593 let incr = incr * 2 in
1594 let y = clamp incr in
1595 gotoy y
1597 | Glut.LEFT_BUTTON when state.outline = None ->
1598 let dest = if bstate = Glut.DOWN then getlink x y else None in
1599 begin match dest with
1600 | Some (pageno, top) ->
1601 if pageno >= 0
1602 then
1603 gotopage pageno top
1605 | None ->
1606 if bstate = Glut.DOWN
1607 then (
1608 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1609 state.mstate <- Msel ((x, y), (x, y));
1610 Glut.postRedisplay ()
1612 else (
1613 Glut.setCursor Glut.CURSOR_INHERIT;
1614 state.mstate <- Mnone;
1618 | _ ->
1621 let mouse ~button ~state ~x ~y = mouse button state x y;;
1623 let motion ~x ~y =
1624 if state.outline = None
1625 then
1626 match state.mstate with
1627 | Mnone -> ()
1628 | Msel (a, _) ->
1629 state.mstate <- Msel (a, (x, y));
1630 Glut.postRedisplay ()
1633 let pmotion ~x ~y =
1634 if state.outline = None
1635 then
1636 match state.mstate with
1637 | Mnone when (checklink x y) ->
1638 Glut.setCursor Glut.CURSOR_INFO
1640 | Mnone ->
1641 Glut.setCursor Glut.CURSOR_INHERIT
1643 | Msel (a, _) ->
1647 let () =
1648 let statepath =
1649 let home =
1650 if Sys.os_type = "Win32"
1651 then
1652 try Sys.getenv "HOMEPATH" with Not_found -> ""
1653 else
1654 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1656 Filename.concat home "llpp"
1658 let pstate =
1660 let ic = open_in_bin statepath in
1661 let hash = input_value ic in
1662 close_in ic;
1663 hash
1664 with exn ->
1665 if false
1666 then
1667 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1669 Hashtbl.create 1
1671 let savestate () =
1673 let w, h =
1674 match state.fullscreen with
1675 | None -> state.w, state.h
1676 | Some wh -> wh
1678 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1679 let oc = open_out_bin statepath in
1680 output_value oc pstate
1681 with exn ->
1682 if false
1683 then
1684 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1687 let setstate () =
1689 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1690 state.w <- statew;
1691 state.h <- stateh;
1692 state.bookmarks <- statebookmarks;
1693 with Not_found -> ()
1694 | exn ->
1695 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1698 Arg.parse [] (fun s -> state.path <- s) "options:";
1699 let name =
1700 if String.length state.path = 0
1701 then (prerr_endline "filename missing"; exit 1)
1702 else state.path
1705 setstate ();
1706 let _ = Glut.init Sys.argv in
1707 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1708 let () = Glut.initWindowSize state.w state.h in
1709 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1711 let csock, ssock =
1712 if Sys.os_type = "Unix"
1713 then
1714 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1715 else
1716 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1717 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1718 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1719 Unix.bind sock addr;
1720 Unix.listen sock 1;
1721 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1722 Unix.connect csock addr;
1723 let ssock, _ = Unix.accept sock in
1724 Unix.close sock;
1725 let opts sock =
1726 Unix.setsockopt sock Unix.TCP_NODELAY true;
1727 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1729 opts ssock;
1730 opts csock;
1731 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1732 ssock, csock
1735 let () = Glut.displayFunc display in
1736 let () = Glut.reshapeFunc reshape in
1737 let () = Glut.keyboardFunc keyboard in
1738 let () = Glut.specialFunc special in
1739 let () = Glut.idleFunc (Some idle) in
1740 let () = Glut.mouseFunc mouse in
1741 let () = Glut.motionFunc motion in
1742 let () = Glut.passiveMotionFunc pmotion in
1744 init ssock;
1745 state.csock <- csock;
1746 state.ssock <- ssock;
1747 writecmd csock ("open " ^ name ^ "\000");
1749 at_exit savestate;
1751 let rec handlelablglutbug () =
1753 Glut.mainLoop ();
1754 with Glut.BadEnum "key in special_of_int" ->
1755 showtext '!' " LablGlut bug: special key not recognized";
1756 Glut.swapBuffers ();
1757 handlelablglutbug ()
1759 handlelablglutbug ();