Fix navigation corner case
[llpp.git] / main.ml
blob260754695e585e9b4b324bca5f36d77fcfe52efd
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 redispimm : bool
86 ; mutable verbose : bool
87 ; mutable scrollincr : int
88 ; mutable maxhfit : bool
89 ; mutable crophack : bool
90 ; mutable autoscroll : bool
91 ; mutable showall : bool
92 ; mutable hlinks : bool
96 type outline = string * int * int * int;;
97 type outlines =
98 | Oarray of outline array
99 | Olist of outline list
100 | Onarrow of outline array * outline array
103 type rect = (float * float * float * float * float * float * float * float);;
105 type state =
106 { mutable csock : Unix.file_descr
107 ; mutable ssock : Unix.file_descr
108 ; mutable w : int
109 ; mutable h : int
110 ; mutable rotate : int
111 ; mutable y : int
112 ; mutable ty : int
113 ; mutable prevy : int
114 ; mutable maxy : int
115 ; mutable layout : layout list
116 ; pagemap : ((int * int * int), string) Hashtbl.t
117 ; mutable pages : (int * int * int) list
118 ; mutable pagecount : int
119 ; pagecache : string circbuf
120 ; mutable inflight : int
121 ; mutable mstate : mstate
122 ; mutable searchpattern : string
123 ; mutable rects : (int * int * rect) list
124 ; mutable rects1 : (int * int * rect) list
125 ; mutable text : string
126 ; mutable fullscreen : (int * int) option
127 ; mutable textentry : textentry option
128 ; mutable outlines : outlines
129 ; mutable outline : (bool * int * int * outline array * string) option
130 ; mutable bookmarks : outline list
131 ; mutable path : string
132 ; 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 ; redispimm = false
149 ; verbose = false
150 ; scrollincr = 24
151 ; maxhfit = true
152 ; crophack = false
153 ; autoscroll = false
154 ; showall = false
155 ; hlinks = false
159 let state =
160 { csock = Unix.stdin
161 ; ssock = Unix.stdin
162 ; w = 900
163 ; h = 900
164 ; rotate = 0
165 ; y = 0
166 ; ty = 0
167 ; prevy = 0
168 ; layout = []
169 ; maxy = max_int
170 ; pagemap = Hashtbl.create 10
171 ; pagecache = cbnew 10 ""
172 ; pages = []
173 ; pagecount = 0
174 ; inflight = 0
175 ; mstate = Mnone
176 ; rects = []
177 ; rects1 = []
178 ; text = ""
179 ; fullscreen = None
180 ; textentry = None
181 ; searchpattern = ""
182 ; outlines = Olist []
183 ; outline = None
184 ; bookmarks = []
185 ; path = ""
186 ; 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 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
349 state.maxy <- calcheight ();
350 List.rev accu
353 let clamp incr =
354 let y = state.y + incr in
355 let y = max 0 y in
356 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
360 let getopaque pageno =
361 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
362 state.rotate))
363 with Not_found -> None
366 let cache pageno opaque =
367 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
368 state.rotate) opaque
371 let validopaque opaque = String.length opaque > 0;;
373 let preload l =
374 match getopaque l.pageno with
375 | None when state.inflight < 2+0*(cblen state.pagecache) ->
376 state.inflight <- succ state.inflight;
377 cache l.pageno "";
378 wcmd "render" [`i (l.pageno + 1)
379 ;`i l.pagedimno
380 ;`i l.pagew
381 ;`i l.pageh];
383 | _ -> ()
386 let gotoy y =
387 let y = max 0 y in
388 let y = min state.maxy y in
389 let pages = layout y state.h in
390 let rec f all = function
391 | l :: ls ->
392 begin match getopaque l.pageno with
393 | None -> preload l; f false ls
394 | Some opaque -> f (all && validopaque opaque) ls
396 | [] -> all
398 if not conf.showall || f true pages
399 then (
400 state.y <- y;
401 state.layout <- pages;
403 state.ty <- y;
404 if conf.redispimm
405 then
406 Glut.postRedisplay ()
410 let addnav () =
411 cbput state.hists.nav (yratio state.y);
412 cbrfollowlen state.hists.nav;
415 let getnav () =
416 let y = cbget state.hists.nav ~-1 in
417 truncate (y *. float state.maxy)
420 let gotopage n top =
421 let y = getpagey n in
422 addnav ();
423 gotoy (y + top);
426 let reshape ~w ~h =
427 let ratio = float w /. float state.w in
428 let fixbookmark (s, l, pageno, pagey) =
429 let pagey = truncate (float pagey *. ratio) in
430 (s, l, pageno, pagey)
432 state.bookmarks <- List.map fixbookmark state.bookmarks;
433 state.w <- w;
434 state.h <- h;
435 GlDraw.viewport 0 0 w h;
436 GlMat.mode `modelview;
437 GlMat.load_identity ();
438 GlMat.mode `projection;
439 GlMat.load_identity ();
440 GlMat.rotate ~x:1.0 ~angle:180.0 ();
441 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
442 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
443 GlClear.color (1., 1., 1.);
444 GlClear.clear [`color];
445 state.layout <- [];
446 state.pages <- [];
447 state.rects <- [];
448 state.text <- "";
449 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
452 let showtext c s =
453 GlDraw.color (0.0, 0.0, 0.0);
454 GlDraw.rect
455 (0.0, float (state.h - 18))
456 (float (state.w - conf.scrollw - 1), float state.h)
458 let font = Glut.BITMAP_8_BY_13 in
459 GlDraw.color (1.0, 1.0, 1.0);
460 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
461 Glut.bitmapCharacter ~font ~c:(Char.code c);
462 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
465 let enttext () =
466 let len = String.length state.text in
467 match state.textentry with
468 | None ->
469 if len > 0 then showtext ' ' state.text
471 | Some (c, text, _, _, _) ->
472 let s =
473 if len > 0
474 then
475 text ^ " [" ^ state.text ^ "]"
476 else
477 text
479 showtext c s;
482 let act cmd =
483 match cmd.[0] with
484 | 'c' ->
485 state.pages <- [];
486 state.outlines <- Olist []
488 | 'D' ->
489 state.rects <- state.rects1;
490 Glut.postRedisplay ()
492 | 'd' ->
493 state.rects <- state.rects1;
494 Glut.postRedisplay ()
496 | 'C' ->
497 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
498 state.pagecount <- n;
499 let rely = yratio state.y in
500 let maxy = calcheight () in
501 state.y <- truncate (float maxy *. rely);
502 state.ty <- state.y;
503 let pages = layout state.y state.h in
504 state.layout <- pages;
505 Glut.postRedisplay ();
507 | 't' ->
508 let s = Scanf.sscanf cmd "t %n"
509 (fun n -> String.sub cmd n (String.length cmd - n))
511 Glut.setWindowTitle s
513 | 'T' ->
514 let s = Scanf.sscanf cmd "T %n"
515 (fun n -> String.sub cmd n (String.length cmd - n))
517 if state.textentry = None
518 then (
519 state.text <- s;
520 showtext ' ' s;
521 Glut.swapBuffers ();
523 else (
524 state.text <- s;
525 Glut.postRedisplay ();
528 | 'V' ->
529 if conf.verbose
530 then
531 let s = Scanf.sscanf cmd "V %n"
532 (fun n -> String.sub cmd n (String.length cmd - n))
534 state.text <- s;
535 showtext ' ' s;
536 Glut.swapBuffers ();
538 | 'F' ->
539 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
540 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
541 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
542 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
544 let y = (getpagey pageno) + truncate y0 in
545 addnav ();
546 gotoy y;
547 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
549 | 'R' ->
550 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
551 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
552 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
553 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
555 state.rects1 <-
556 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
558 | 'r' ->
559 let n, w, h, r, p =
560 Scanf.sscanf cmd "r %d %d %d %d %s"
561 (fun n w h r p -> (n, w, h, r, p))
563 Hashtbl.replace state.pagemap (n, w, r) p;
564 let evicted = cbpeekw state.pagecache in
565 if String.length evicted > 0
566 then begin
567 wcmd "free" [`s evicted];
568 let l = Hashtbl.fold (fun k p a ->
569 if evicted = p then k :: a else a) state.pagemap []
571 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
572 end;
573 cbput state.pagecache p;
574 state.inflight <- pred state.inflight;
575 if conf.showall then gotoy state.ty;
576 Glut.postRedisplay ()
578 | 'l' ->
579 let (n, w, h) as pagelayout =
580 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
582 state.pages <- pagelayout :: state.pages
584 | 'o' ->
585 let (l, n, t, pos) =
586 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
588 let s = String.sub cmd pos (String.length cmd - pos) in
589 let outline = (s, l, n, t) in
590 let outlines =
591 match state.outlines with
592 | Olist outlines -> Olist (outline :: outlines)
593 | Oarray _ -> Olist [outline]
594 | Onarrow _ -> Olist [outline]
596 state.outlines <- outlines
598 | _ ->
599 log "unknown cmd `%S'" cmd
602 let idle () =
603 if not conf.redispimm && state.y != state.prevy
604 then (
605 state.prevy <- state.y;
606 Glut.postRedisplay ();
608 else
609 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
611 begin match r with
612 | [] ->
613 if conf.preload then begin
614 let h = state.h in
615 let y = if state.y < state.h then 0 else state.y - state.h in
616 let pages = layout y (h*3) in
617 List.iter preload pages;
618 end;
619 if conf.autoscroll then begin
620 let y = state.y + conf.scrollincr in
621 let y = if y >= state.maxy then 0 else y in
622 gotoy y;
623 state.text <- "";
624 state.prevy <- state.y;
625 Glut.postRedisplay ();
626 end;
628 | _ ->
629 let cmd = readcmd state.csock in
630 act cmd;
631 end;
634 let onhist cb = function
635 | HCprev -> cbget cb ~-1
636 | HCnext -> cbget cb 1
637 | HCfirst -> cbget cb ~-(cb.rc)
638 | HClast -> cbget cb (cb.len - 1 - cb.rc)
641 let search pattern forward =
642 if String.length pattern > 0
643 then
644 let pn, py =
645 match state.layout with
646 | [] -> 0, 0
647 | l :: _ ->
648 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
650 let cmd =
651 let b = makecmd "search"
652 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
654 Buffer.add_char b ',';
655 Buffer.add_string b pattern;
656 Buffer.add_char b '\000';
657 Buffer.contents b;
659 writecmd state.csock cmd;
662 let intentry text key =
663 let c = Char.unsafe_chr key in
664 match c with
665 | '0' .. '9' ->
666 let s = "x" in s.[0] <- c;
667 let text = text ^ s in
668 TEcont text
670 | _ ->
671 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
672 TEcont text
675 let addchar s c =
676 let b = Buffer.create (String.length s + 1) in
677 Buffer.add_string b s;
678 Buffer.add_char b c;
679 Buffer.contents b;
682 let textentry text key =
683 let c = Char.unsafe_chr key in
684 match c with
685 | _ when key >= 32 && key < 127 ->
686 let text = addchar text c in
687 TEcont text
689 | _ ->
690 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
691 TEcont text
694 let rotate angle =
695 state.rects <- [];
696 state.rects1 <- [];
697 state.rotate <- angle;
698 wcmd "rotate" [`i angle];
701 let optentry text key =
702 let btos b = if b then "on" else "off" in
703 let c = Char.unsafe_chr key in
704 match c with
705 | 'r' ->
706 conf.rectsel <- not conf.rectsel;
707 TEdone ("rectsel " ^ (btos conf.rectsel))
709 | 's' ->
710 let ondone s =
711 try conf.scrollincr <- int_of_string s with exc ->
712 state.text <- Printf.sprintf "bad integer `%s': %s"
713 s (Printexc.to_string exc)
715 TEswitch ('#', "", None, intentry, ondone)
717 | 'R' ->
718 let ondone s =
719 match try
720 Some (int_of_string s)
721 with exc ->
722 state.text <- Printf.sprintf "bad integer `%s': %s"
723 s (Printexc.to_string exc);
724 None
725 with
726 | Some angle -> rotate angle
727 | None -> ()
729 TEswitch ('^', "", None, intentry, ondone)
731 | 'i' ->
732 conf.icase <- not conf.icase;
733 TEdone ("case insensitive search " ^ (btos conf.icase))
735 | 'p' ->
736 conf.preload <- not conf.preload;
737 TEdone ("preload " ^ (btos conf.preload))
739 | 'd' ->
740 conf.redispimm <- not conf.redispimm;
741 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
743 | 'v' ->
744 conf.verbose <- not conf.verbose;
745 TEdone ("verbose " ^ (btos conf.verbose))
747 | 'h' ->
748 conf.maxhfit <- not conf.maxhfit;
749 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
750 TEdone ("maxhfit " ^ (btos conf.maxhfit))
752 | 'c' ->
753 conf.crophack <- not conf.crophack;
754 TEdone ("crophack " ^ btos conf.crophack)
756 | 'a' ->
757 conf.showall <- not conf.showall;
758 TEdone ("showall " ^ btos conf.showall)
760 | _ ->
761 state.text <- Printf.sprintf "bad option %d `%c'" key c;
762 TEstop
765 let maxoutlinerows () = (state.h - 31) / 16;;
767 let enterselector allowdel outlines errmsg =
768 if Array.length outlines = 0
769 then (
770 showtext ' ' errmsg;
771 Glut.swapBuffers ()
773 else
774 let pageno =
775 match state.layout with
776 | [] -> -1
777 | {pageno=pageno} :: rest -> pageno
779 let active =
780 let rec loop n =
781 if n = Array.length outlines
782 then 0
783 else
784 let (_, _, outlinepageno, _) = outlines.(n) in
785 if outlinepageno >= pageno then n else loop (n+1)
787 loop 0
789 state.outline <-
790 Some (allowdel, active,
791 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
792 Glut.postRedisplay ();
795 let enteroutlinemode () =
796 let outlines =
797 match state.outlines with
798 | Oarray a -> a
799 | Olist l ->
800 let a = Array.of_list (List.rev l) in
801 state.outlines <- Oarray a;
803 | Onarrow (a, b) -> a
805 enterselector false outlines "Documents has no outline";
808 let enterbookmarkmode () =
809 let bookmarks = Array.of_list state.bookmarks in
810 enterselector true bookmarks "Documents has no bookmarks (yet)";
814 let quickbookmark ?title () =
815 match state.layout with
816 | [] -> ()
817 | l :: _ ->
818 let title =
819 match title with
820 | None ->
821 let sec = Unix.gettimeofday () in
822 let tm = Unix.localtime sec in
823 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
824 l.pageno
825 tm.Unix.tm_mday
826 tm.Unix.tm_mon
827 (tm.Unix.tm_year + 1900)
828 tm.Unix.tm_hour
829 tm.Unix.tm_min
830 | Some title -> title
832 state.bookmarks <-
833 (title, 0, l.pageno, l.pagey) :: state.bookmarks
836 let viewkeyboard ~key ~x ~y =
837 let enttext te =
838 state.textentry <- te;
839 state.text <- "";
840 enttext ();
841 Glut.postRedisplay ()
843 match state.textentry with
844 | None ->
845 let c = Char.chr key in
846 begin match c with
847 | '\027' | 'q' ->
848 exit 0
850 | '\008' ->
851 let y = getnav () in
852 gotoy y
854 | 'o' ->
855 enteroutlinemode ()
857 | 'u' ->
858 state.rects <- [];
859 state.text <- "";
860 Glut.postRedisplay ()
862 | '/' | '?' ->
863 let ondone isforw s =
864 cbput state.hists.pat s;
865 cbrfollowlen state.hists.pat;
866 state.searchpattern <- s;
867 search s isforw
869 enttext (Some (c, "", Some (onhist state.hists.pat),
870 textentry, ondone (c ='/')))
872 | '+' ->
873 let ondone s =
874 let n =
875 try int_of_string s with exc ->
876 state.text <- Printf.sprintf "bad integer `%s': %s"
877 s (Printexc.to_string exc);
878 max_int
880 if n != max_int
881 then (
882 conf.pagebias <- n;
883 state.text <- "page bias is now " ^ string_of_int n;
886 enttext (Some ('+', "", None, intentry, ondone))
888 | '-' ->
889 let ondone msg =
890 state.text <- msg;
892 enttext (Some ('-', "", None, optentry, ondone))
894 | '0' .. '9' ->
895 let ondone s =
896 let n =
897 try int_of_string s with exc ->
898 state.text <- Printf.sprintf "bad integer `%s': %s"
899 s (Printexc.to_string exc);
902 if n >= 0
903 then (
904 addnav ();
905 cbput state.hists.pag (string_of_int n);
906 cbrfollowlen state.hists.pag;
907 gotoy (getpagey (n + conf.pagebias - 1))
910 let pageentry text key =
911 match Char.unsafe_chr key with
912 | 'g' -> TEdone text
913 | _ -> intentry text key
915 let text = "x" in text.[0] <- c;
916 enttext (Some (':', text, Some (onhist state.hists.pag),
917 pageentry, ondone))
919 | 'b' ->
920 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
921 reshape state.w state.h;
923 | 'l' ->
924 conf.hlinks <- not conf.hlinks;
925 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
926 Glut.postRedisplay ()
928 | 'a' ->
929 conf.autoscroll <- not conf.autoscroll
931 | 'f' ->
932 begin match state.fullscreen with
933 | None ->
934 state.fullscreen <- Some (state.w, state.h);
935 Glut.fullScreen ()
936 | Some (w, h) ->
937 state.fullscreen <- None;
938 Glut.reshapeWindow ~w ~h
941 | 'g' ->
942 gotoy 0
944 | 'n' ->
945 search state.searchpattern true
947 | 'p' | 'N' ->
948 search state.searchpattern false
950 | 't' ->
951 begin match state.layout with
952 | [] -> ()
953 | l :: _ ->
954 gotoy (state.y - l.pagey);
957 | ' ' ->
958 begin match List.rev state.layout with
959 | [] -> ()
960 | l :: _ ->
961 gotoy (clamp (l.pageh - l.pagey))
964 | '\127' ->
965 begin match state.layout with
966 | [] -> ()
967 | l :: _ ->
968 gotoy (clamp (-l.pageh));
971 | '=' ->
972 let f (fn, ln) l =
973 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
975 let fn, ln = List.fold_left f (-1, -1) state.layout in
976 let s =
977 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
978 let percent =
979 if maxy <= 0
980 then 100.
981 else (100. *. (float state.y /. float maxy)) in
982 if fn = ln
983 then
984 Printf.sprintf "Page %d of %d %.2f%%"
985 (fn+1) state.pagecount percent
986 else
987 Printf.sprintf
988 "Pages %d-%d of %d %.2f%%"
989 (fn+1) (ln+1) state.pagecount percent
991 showtext ' ' s;
992 Glut.swapBuffers ()
994 | 'w' ->
995 begin match state.layout with
996 | [] -> ()
997 | l :: _ ->
998 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
999 Glut.postRedisplay ();
1002 | '\'' ->
1003 enterbookmarkmode ()
1005 | 'm' ->
1006 let ondone s =
1007 match state.layout with
1008 | l :: _ ->
1009 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1010 | _ -> ()
1012 enttext (Some ('~', "", None, textentry, ondone))
1014 | '~' ->
1015 quickbookmark ();
1016 showtext ' ' "Quick bookmark added";
1017 Glut.swapBuffers ()
1019 | 'z' ->
1020 begin match state.layout with
1021 | l :: _ ->
1022 let a = getpagewh l.pagedimno in
1023 let w, h =
1024 if conf.crophack
1025 then
1026 (truncate (1.8 *. (a.(1) -. a.(0))),
1027 truncate (1.2 *. (a.(3) -. a.(0))))
1028 else
1029 (truncate (a.(1) -. a.(0)),
1030 truncate (a.(3) -. a.(0)))
1032 Glut.reshapeWindow (w + conf.scrollw) h;
1033 Glut.postRedisplay ();
1035 | [] -> ()
1038 | '<' | '>' ->
1039 rotate (state.rotate + (if c = '>' then 30 else -30));
1041 | _ ->
1042 vlog "huh? %d %c" key (Char.chr key);
1045 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1046 let len = String.length text in
1047 if len = 0
1048 then (
1049 state.textentry <- None;
1050 Glut.postRedisplay ();
1052 else (
1053 let s = String.sub text 0 (len - 1) in
1054 enttext (Some (c, s, onhist, onkey, ondone))
1057 | Some (c, text, onhist, onkey, ondone) ->
1058 begin match Char.unsafe_chr key with
1059 | '\r' | '\n' ->
1060 ondone text;
1061 state.textentry <- None;
1062 Glut.postRedisplay ()
1064 | '\027' ->
1065 state.textentry <- None;
1066 Glut.postRedisplay ()
1068 | _ ->
1069 begin match onkey text key with
1070 | TEdone text ->
1071 state.textentry <- None;
1072 ondone text;
1073 Glut.postRedisplay ()
1075 | TEcont text ->
1076 enttext (Some (c, text, onhist, onkey, ondone));
1078 | TEstop ->
1079 state.textentry <- None;
1080 Glut.postRedisplay ()
1082 | TEswitch te ->
1083 state.textentry <- Some te;
1084 Glut.postRedisplay ()
1085 end;
1086 end;
1089 let narrow outlines pattern =
1090 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1091 match reopt with
1092 | None -> None
1093 | Some re ->
1094 let rec fold accu n =
1095 if n = -1 then accu else
1096 let (s, _, _, _) as o = outlines.(n) in
1097 let accu =
1098 if (try ignore (Str.search_forward re s 0); true
1099 with Not_found -> false)
1100 then (o :: accu)
1101 else accu
1103 fold accu (n-1)
1105 let matched = fold [] (Array.length outlines - 1) in
1106 if matched = [] then None else Some (Array.of_list matched)
1109 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1110 let search active pattern incr =
1111 let dosearch re =
1112 let rec loop n =
1113 if n = Array.length outlines || n = -1 then None else
1114 let (s, _, _, _) = outlines.(n) in
1116 (try ignore (Str.search_forward re s 0); true
1117 with Not_found -> false)
1118 then Some n
1119 else loop (n + incr)
1121 loop active
1124 let re = Str.regexp_case_fold pattern in
1125 dosearch re
1126 with Failure s ->
1127 state.text <- s;
1128 None
1130 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1131 match key with
1132 | 27 ->
1133 if String.length qsearch = 0
1134 then (
1135 state.text <- "";
1136 state.outline <- None;
1137 Glut.postRedisplay ();
1139 else (
1140 state.text <- "";
1141 state.outline <- Some (allowdel, active, first, outlines, "");
1142 Glut.postRedisplay ();
1145 | 18 | 19 ->
1146 let incr = if key = 18 then -1 else 1 in
1147 let active, first =
1148 match search (active + incr) qsearch incr with
1149 | None ->
1150 state.text <- qsearch ^ " [not found]";
1151 active, first
1152 | Some active ->
1153 state.text <- qsearch;
1154 active, firstof active
1156 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1157 Glut.postRedisplay ();
1159 | 8 ->
1160 let len = String.length qsearch in
1161 if len = 0
1162 then ()
1163 else (
1164 if len = 1
1165 then (
1166 state.text <- "";
1167 state.outline <- Some (allowdel, active, first, outlines, "");
1169 else
1170 let qsearch = String.sub qsearch 0 (len - 1) in
1171 let active, first =
1172 match search active qsearch ~-1 with
1173 | None ->
1174 state.text <- qsearch ^ " [not found]";
1175 active, first
1176 | Some active ->
1177 state.text <- qsearch;
1178 active, firstof active
1180 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1182 Glut.postRedisplay ()
1184 | 13 ->
1185 if active < Array.length outlines
1186 then (
1187 let (_, _, n, t) = outlines.(active) in
1188 gotopage n t;
1190 state.text <- "";
1191 if allowdel then state.bookmarks <- Array.to_list outlines;
1192 state.outline <- None;
1193 Glut.postRedisplay ();
1195 | _ when key >= 32 && key < 127 ->
1196 let pattern = addchar qsearch (Char.chr key) in
1197 let active, first =
1198 match search active pattern 1 with
1199 | None ->
1200 state.text <- pattern ^ " [not found]";
1201 active, first
1202 | Some active ->
1203 state.text <- pattern;
1204 active, firstof active
1206 state.outline <- Some (allowdel, active, first, outlines, pattern);
1207 Glut.postRedisplay ()
1209 | 14 when not allowdel ->
1210 let optoutlines = narrow outlines qsearch in
1211 begin match optoutlines with
1212 | None -> state.text <- "can't narrow"
1213 | Some outlines ->
1214 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1215 match state.outlines with
1216 | Olist l -> ()
1217 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1218 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1219 end;
1220 Glut.postRedisplay ()
1222 | 21 when not allowdel ->
1223 let outline =
1224 match state.outlines with
1225 | Oarray a -> a
1226 | Olist l ->
1227 let a = Array.of_list (List.rev l) in
1228 state.outlines <- Oarray a;
1230 | Onarrow (a, b) ->
1231 state.outlines <- Oarray b;
1234 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1235 Glut.postRedisplay ()
1237 | 12 ->
1238 state.outline <-
1239 Some (allowdel, active, firstof active, outlines, qsearch);
1240 Glut.postRedisplay ()
1242 | 127 when allowdel ->
1243 let len = Array.length outlines - 1 in
1244 if len = 0
1245 then (
1246 state.outline <- None;
1247 state.bookmarks <- [];
1249 else (
1250 let bookmarks = Array.init len
1251 (fun i ->
1252 let i = if i >= active then i + 1 else i in
1253 outlines.(i)
1256 state.outline <-
1257 Some (allowdel,
1258 min active (len-1),
1259 min first (len-1),
1260 bookmarks, qsearch)
1263 Glut.postRedisplay ()
1265 | _ -> log "unknown key %d" key
1268 let keyboard ~key ~x ~y =
1269 if key = 7
1270 then
1271 wcmd "interrupt" []
1272 else
1273 match state.outline with
1274 | None -> viewkeyboard ~key ~x ~y
1275 | Some outline -> outlinekeyboard ~key ~x ~y outline
1278 let special ~key ~x ~y =
1279 match state.outline with
1280 | None ->
1281 begin match state.textentry with
1282 | None ->
1283 let y =
1284 match key with
1285 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1286 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1287 | Glut.KEY_DOWN -> clamp conf.scrollincr
1288 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1289 | Glut.KEY_PAGE_DOWN -> clamp state.h
1290 | Glut.KEY_HOME -> addnav (); 0
1291 | Glut.KEY_END ->
1292 addnav ();
1293 state.maxy - (if conf.maxhfit then state.h else 0)
1294 | _ -> state.y
1296 state.text <- "";
1297 gotoy y
1299 | Some (c, s, Some onhist, onkey, ondone) ->
1300 let s =
1301 match key with
1302 | Glut.KEY_UP -> onhist HCprev
1303 | Glut.KEY_DOWN -> onhist HCnext
1304 | Glut.KEY_HOME -> onhist HCfirst
1305 | Glut.KEY_END -> onhist HClast
1306 | _ -> state.text
1308 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1309 Glut.postRedisplay ()
1311 | _ -> ()
1314 | Some (allowdel, active, first, outlines, qsearch) ->
1315 let maxrows = maxoutlinerows () in
1316 let navigate incr =
1317 let active = active + incr in
1318 let active = max 0 (min active (Array.length outlines - 1)) in
1319 let first =
1320 if active > first
1321 then
1322 let rows = active - first in
1323 if rows > maxrows then active - maxrows else first
1324 else active
1326 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1327 Glut.postRedisplay ()
1329 match key with
1330 | Glut.KEY_UP -> navigate ~-1
1331 | Glut.KEY_DOWN -> navigate 1
1332 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1333 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1335 | Glut.KEY_HOME ->
1336 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1337 Glut.postRedisplay ()
1339 | Glut.KEY_END ->
1340 let active = Array.length outlines - 1 in
1341 let first = max 0 (active - maxrows) in
1342 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1343 Glut.postRedisplay ()
1345 | _ -> ()
1348 let drawplaceholder l =
1349 GlDraw.color (1.0, 1.0, 1.0);
1350 GlDraw.rect
1351 (0.0, float l.pagedispy)
1352 (float l.pagew, float (l.pagedispy + l.pagevh))
1354 let x = 0.0
1355 and y = float (l.pagedispy + 13) in
1356 let font = Glut.BITMAP_8_BY_13 in
1357 GlDraw.color (0.0, 0.0, 0.0);
1358 GlPix.raster_pos ~x ~y ();
1359 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1360 ("Loading " ^ string_of_int l.pageno);
1363 let now () = Unix.gettimeofday ();;
1365 let drawpage i l =
1366 begin match getopaque l.pageno with
1367 | Some opaque when validopaque opaque ->
1368 if state.textentry = None
1369 then GlDraw.color (1.0, 1.0, 1.0)
1370 else GlDraw.color (0.4, 0.4, 0.4);
1371 let a = now () in
1372 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1373 let b = now () in
1374 let d = b-.a in
1375 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1376 vlog "draw %f sec" d;
1378 | Some _ ->
1379 drawplaceholder l
1381 | None ->
1382 drawplaceholder l;
1383 if state.inflight < cblen state.pagecache
1384 then (
1385 List.iter preload state.layout;
1387 else (
1388 vlog "inflight %d" state.inflight;
1390 end;
1391 GlDraw.color (0.5, 0.5, 0.5);
1392 GlDraw.rect
1393 (0., float i)
1394 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1396 l.pagedispy + l.pagevh;
1399 let scrollindicator () =
1400 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1401 GlDraw.color (0.64 , 0.64, 0.64);
1402 GlDraw.rect
1403 (float (state.w - conf.scrollw), 0.)
1404 (float state.w, float state.h)
1406 GlDraw.color (0.0, 0.0, 0.0);
1407 let sh = (float (maxy + state.h) /. float state.h) in
1408 let sh = float state.h /. sh in
1409 let sh = max sh (float conf.scrollh) in
1411 let percent =
1412 if state.y = state.maxy
1413 then 1.0
1414 else float state.y /. float maxy
1416 let position = (float state.h -. sh) *. percent in
1418 let position =
1419 if position +. sh > float state.h
1420 then
1421 float state.h -. sh
1422 else
1423 position
1425 GlDraw.rect
1426 (float (state.w - conf.scrollw), position)
1427 (float state.w, position +. sh)
1431 let showsel () =
1432 match state.mstate with
1433 | Mnone ->
1436 | Msel ((x0, y0), (x1, y1)) ->
1437 let y0' = min y0 y1
1438 and y1 = max y0 y1 in
1439 let y0 = y0' in
1440 let f l =
1441 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1442 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1443 then
1444 match getopaque l.pageno with
1445 | Some opaque when validopaque opaque ->
1446 let oy = -l.pagey + l.pagedispy in
1447 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1448 | _ -> ()
1450 List.iter f state.layout
1453 let showrects () =
1454 Gl.enable `blend;
1455 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1456 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1457 List.iter
1458 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1459 List.iter (fun l ->
1460 if l.pageno = pageno
1461 then (
1462 let d = float (l.pagedispy - l.pagey) in
1463 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1464 GlDraw.begins `quads;
1466 GlDraw.vertex2 (x0, y0+.d);
1467 GlDraw.vertex2 (x1, y1+.d);
1468 GlDraw.vertex2 (x2, y2+.d);
1469 GlDraw.vertex2 (x3, y3+.d);
1471 GlDraw.ends ();
1472 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1474 ) state.layout
1475 ) state.rects
1477 Gl.disable `blend;
1480 let showoutline = function
1481 | None -> ()
1482 | Some (allowdel, active, first, outlines, qsearch) ->
1483 Gl.enable `blend;
1484 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1485 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1486 GlDraw.rect (0., 0.) (float state.w, float state.h);
1487 Gl.disable `blend;
1489 GlDraw.color (1., 1., 1.);
1490 let font = Glut.BITMAP_9_BY_15 in
1491 let draw_string x y s =
1492 GlPix.raster_pos ~x ~y ();
1493 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1495 let rec loop row =
1496 if row = Array.length outlines || (row - first) * 16 > state.h
1497 then ()
1498 else (
1499 let (s, l, _, _) = outlines.(row) in
1500 let y = (row - first) * 16 in
1501 let x = 5 + 15*l in
1502 if row = active
1503 then (
1504 Gl.enable `blend;
1505 GlDraw.polygon_mode `both `line;
1506 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1507 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1508 GlDraw.rect (0., float (y + 1))
1509 (float (state.w - conf.scrollw - 1), float (y + 18));
1510 GlDraw.polygon_mode `both `fill;
1511 Gl.disable `blend;
1512 GlDraw.color (1., 1., 1.);
1514 draw_string (float x) (float (y + 16)) s;
1515 loop (row+1)
1518 loop first
1521 let display () =
1522 let lasty = List.fold_left drawpage 0 (state.layout) in
1523 GlDraw.color (0.5, 0.5, 0.5);
1524 GlDraw.rect
1525 (0., float lasty)
1526 (float (state.w - conf.scrollw), float state.h)
1528 showrects ();
1529 scrollindicator ();
1530 showsel ();
1531 showoutline state.outline;
1532 enttext ();
1533 Glut.swapBuffers ();
1536 let getlink x y =
1537 let rec f = function
1538 | l :: rest ->
1539 begin match getopaque l.pageno with
1540 | Some opaque when validopaque opaque ->
1541 let y = y - l.pagedispy in
1542 if y > 0
1543 then
1544 let y = l.pagey + y in
1545 match getlink opaque x y with
1546 | None -> f rest
1547 | some -> some
1548 else
1549 f rest
1550 | _ ->
1551 f rest
1553 | [] -> None
1555 f state.layout
1558 let checklink x y =
1559 let rec f = function
1560 | l :: rest ->
1561 begin match getopaque l.pageno with
1562 | Some opaque when validopaque opaque ->
1563 let y = y - l.pagedispy in
1564 if y > 0
1565 then
1566 let y = l.pagey + y in
1567 if checklink opaque x y then true else f rest
1568 else
1569 f rest
1570 | _ ->
1571 f rest
1573 | [] -> false
1575 f state.layout
1578 let mouse ~button ~bstate ~x ~y =
1579 match button with
1580 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1581 let incr =
1582 if n = 3
1583 then
1584 -conf.scrollincr
1585 else
1586 conf.scrollincr
1588 let incr = incr * 2 in
1589 let y = clamp incr in
1590 gotoy y
1592 | Glut.LEFT_BUTTON when state.outline = None ->
1593 let dest = if bstate = Glut.DOWN then getlink x y else None in
1594 begin match dest with
1595 | Some (pageno, top) ->
1596 if pageno >= 0
1597 then
1598 gotopage pageno top
1600 | None ->
1601 if bstate = Glut.DOWN
1602 then (
1603 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1604 state.mstate <- Msel ((x, y), (x, y));
1605 Glut.postRedisplay ()
1607 else (
1608 Glut.setCursor Glut.CURSOR_INHERIT;
1609 state.mstate <- Mnone;
1613 | _ ->
1616 let mouse ~button ~state ~x ~y = mouse button state x y;;
1618 let motion ~x ~y =
1619 if state.outline = None
1620 then
1621 match state.mstate with
1622 | Mnone -> ()
1623 | Msel (a, _) ->
1624 state.mstate <- Msel (a, (x, y));
1625 Glut.postRedisplay ()
1628 let pmotion ~x ~y =
1629 if state.outline = None
1630 then
1631 match state.mstate with
1632 | Mnone when (checklink x y) ->
1633 Glut.setCursor Glut.CURSOR_INFO
1635 | Mnone ->
1636 Glut.setCursor Glut.CURSOR_INHERIT
1638 | Msel (a, _) ->
1642 let () =
1643 let statepath =
1644 let home =
1645 if Sys.os_type = "Win32"
1646 then
1647 try Sys.getenv "HOMEPATH" with Not_found -> ""
1648 else
1649 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1651 Filename.concat home "llpp"
1653 let pstate =
1655 let ic = open_in_bin statepath in
1656 let hash = input_value ic in
1657 close_in ic;
1658 hash
1659 with exn ->
1660 if false
1661 then
1662 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1664 Hashtbl.create 1
1666 let savestate () =
1668 let w, h =
1669 match state.fullscreen with
1670 | None -> state.w, state.h
1671 | Some wh -> wh
1673 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1674 let oc = open_out_bin statepath in
1675 output_value oc pstate
1676 with exn ->
1677 if false
1678 then
1679 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1682 let setstate () =
1684 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1685 state.w <- statew;
1686 state.h <- stateh;
1687 state.bookmarks <- statebookmarks;
1688 with Not_found -> ()
1689 | exn ->
1690 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1693 Arg.parse [] (fun s -> state.path <- s) "options:";
1694 let name =
1695 if String.length state.path = 0
1696 then (prerr_endline "filename missing"; exit 1)
1697 else state.path
1700 setstate ();
1701 let _ = Glut.init Sys.argv in
1702 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1703 let () = Glut.initWindowSize state.w state.h in
1704 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1706 let csock, ssock =
1707 if Sys.os_type = "Unix"
1708 then
1709 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1710 else
1711 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1712 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1713 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1714 Unix.bind sock addr;
1715 Unix.listen sock 1;
1716 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1717 Unix.connect csock addr;
1718 let ssock, _ = Unix.accept sock in
1719 Unix.close sock;
1720 let opts sock =
1721 Unix.setsockopt sock Unix.TCP_NODELAY true;
1722 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1724 opts ssock;
1725 opts csock;
1726 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1727 ssock, csock
1730 let () = Glut.displayFunc display in
1731 let () = Glut.reshapeFunc reshape in
1732 let () = Glut.keyboardFunc keyboard in
1733 let () = Glut.specialFunc special in
1734 let () = Glut.idleFunc (Some idle) in
1735 let () = Glut.mouseFunc mouse in
1736 let () = Glut.motionFunc motion in
1737 let () = Glut.passiveMotionFunc pmotion in
1739 init ssock;
1740 state.csock <- csock;
1741 state.ssock <- ssock;
1742 writecmd csock ("open " ^ name ^ "\000");
1744 at_exit savestate;
1746 let rec handlelablglutbug () =
1748 Glut.mainLoop ();
1749 with Glut.BadEnum "key in special_of_int" ->
1750 showtext '!' " LablGlut bug: special key not recognized";
1751 Glut.swapBuffers ();
1752 handlelablglutbug ()
1754 handlelablglutbug ();