Partial and flawed solution to rotation problem
[llpp.git] / main.ml
blobbdee6a4a3c55fd8373b98d5c25620eb3016c29fd
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 ; mutable sconty : 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 ; rectsel = true
147 ; preload = false
148 ; pagebias = 0
149 ; redispimm = false
150 ; verbose = false
151 ; scrollincr = 24
152 ; maxhfit = true
153 ; crophack = false
154 ; autoscroll = false
155 ; showall = false
156 ; hlinks = false
160 let state =
161 { csock = Unix.stdin
162 ; ssock = Unix.stdin
163 ; w = 900
164 ; h = 900
165 ; rotate = 0
166 ; y = 0
167 ; ty = 0
168 ; prevy = 0
169 ; layout = []
170 ; maxy = max_int
171 ; pagemap = Hashtbl.create 10
172 ; pagecache = cbnew 10 ""
173 ; pages = []
174 ; pagecount = 0
175 ; inflight = 0
176 ; mstate = Mnone
177 ; rects = []
178 ; rects1 = []
179 ; text = ""
180 ; fullscreen = None
181 ; textentry = None
182 ; searchpattern = ""
183 ; outlines = Olist []
184 ; outline = None
185 ; bookmarks = []
186 ; path = ""
187 ; sconty = 0.0
188 ; hists =
189 { nav = cbnew 100 0.0
190 ; pat = cbnew 20 ""
191 ; pag = cbnew 10 ""
196 let vlog fmt =
197 if conf.verbose
198 then
199 Printf.kprintf prerr_endline fmt
200 else
201 Printf.kprintf ignore fmt
204 let writecmd fd s =
205 let len = String.length s in
206 let n = 4 + len in
207 let b = Buffer.create n in
208 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
209 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
210 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
211 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
212 Buffer.add_string b s;
213 let s' = Buffer.contents b in
214 let n' = Unix.write fd s' 0 n in
215 if n' != n then failwith "write failed";
218 let readcmd fd =
219 let s = "xxxx" in
220 let n = Unix.read fd s 0 4 in
221 if n != 4 then failwith "incomplete read(len)";
222 let len = 0
223 lor (Char.code s.[0] lsl 24)
224 lor (Char.code s.[1] lsl 16)
225 lor (Char.code s.[2] lsl 8)
226 lor (Char.code s.[3] lsl 0)
228 let s = String.create len in
229 let n = Unix.read fd s 0 len in
230 if n != len then failwith "incomplete read(data)";
234 let yratio y =
235 if y = state.maxy then 1.0
236 else float y /. float state.maxy
239 let makecmd s l =
240 let b = Buffer.create 10 in
241 Buffer.add_string b s;
242 let rec combine = function
243 | [] -> b
244 | x :: xs ->
245 Buffer.add_char b ' ';
246 let s =
247 match x with
248 | `b b -> if b then "1" else "0"
249 | `s s -> s
250 | `i i -> string_of_int i
251 | `f f -> string_of_float f
252 | `I f -> string_of_int (truncate f)
254 Buffer.add_string b s;
255 combine xs;
257 combine l;
260 let wcmd s l =
261 let cmd = Buffer.contents (makecmd s l) in
262 writecmd state.csock cmd;
265 let calcheight () =
266 let rec f pn ph fh l =
267 match l with
268 | (n, _, h) :: rest ->
269 let fh = fh + (n - pn) * ph in
270 f n h fh rest
272 | [] ->
273 let fh = fh + (ph * (state.pagecount - pn)) in
274 max 0 fh
276 let fh = f 0 0 0 state.pages in
280 let getpagey pageno =
281 let rec f pn ph y l =
282 match l with
283 | (n, _, h) :: rest ->
284 if n >= pageno
285 then
286 y + (pageno - pn) * ph
287 else
288 let y = y + (n - pn) * ph in
289 f n h y rest
291 | [] ->
292 y + (pageno - pn) * ph
294 f 0 0 0 state.pages;
297 let layout y sh =
298 let rec f pageno pdimno prev vy py dy l cacheleft accu =
299 if pageno = state.pagecount || cacheleft = 0
300 then accu
301 else
302 let ((_, w, h) as curr), rest, pdimno =
303 match l with
304 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
305 curr, rest, pdimno + 1
306 | _ ->
307 prev, l, pdimno
309 let pageno' = pageno + 1 in
310 if py + h > vy
311 then
312 let py' = vy - py in
313 let vh = h - py' in
314 if dy + vh > sh
315 then
316 let vh = sh - dy in
317 if vh <= 0
318 then
319 accu
320 else
321 let e =
322 { pageno = pageno
323 ; pagedimno = pdimno
324 ; pagew = w
325 ; pageh = h
326 ; pagedispy = dy
327 ; pagey = py'
328 ; pagevh = vh
331 e :: accu
332 else
333 let e =
334 { pageno = pageno
335 ; pagedimno = pdimno
336 ; pagew = w
337 ; pageh = h
338 ; pagedispy = dy
339 ; pagey = py'
340 ; pagevh = vh
343 let accu = e :: accu in
344 f pageno' pdimno curr
345 (vy + vh) (py + h) (dy + vh + 2) rest
346 (pred cacheleft) accu
347 else
348 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
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
355 let clamp incr =
356 let y = state.y + incr in
357 let y = max 0 y in
358 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
362 let getopaque pageno =
363 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
364 state.rotate))
365 with Not_found -> None
368 let cache pageno opaque =
369 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
370 state.rotate) opaque
373 let validopaque opaque = String.length opaque > 0;;
375 let preload l =
376 match getopaque l.pageno with
377 | None when state.inflight < 2+0*(cblen state.pagecache) ->
378 state.inflight <- succ state.inflight;
379 cache l.pageno "";
380 wcmd "render" [`i (l.pageno + 1)
381 ;`i l.pagedimno
382 ;`i l.pagew
383 ;`i l.pageh];
385 | _ -> ()
388 let gotoy y =
389 let y = max 0 y in
390 let y = min state.maxy y in
391 let pages = layout y state.h in
392 let rec f all = function
393 | l :: ls ->
394 begin match getopaque l.pageno with
395 | None -> preload l; f false ls
396 | Some opaque -> f (all && validopaque opaque) ls
398 | [] -> all
400 if not conf.showall || f true pages
401 then (
402 state.y <- y;
403 state.layout <- pages;
405 state.ty <- y;
406 if conf.redispimm
407 then
408 Glut.postRedisplay ()
412 let addnav () =
413 cbput state.hists.nav (yratio state.y);
414 cbrfollowlen state.hists.nav;
417 let getnav () =
418 let y = cbget state.hists.nav ~-1 in
419 truncate (y *. float state.maxy)
422 let gotopage n top =
423 let y = getpagey n in
424 addnav ();
425 gotoy (y + top);
428 let reshape ~w ~h =
429 let ratio = float w /. float state.w in
430 let fixbookmark (s, l, pageno, pagey) =
431 let pagey = truncate (float pagey *. ratio) in
432 (s, l, pageno, pagey)
434 state.bookmarks <- List.map fixbookmark state.bookmarks;
435 state.w <- w;
436 state.h <- h;
437 GlDraw.viewport 0 0 w h;
438 GlMat.mode `modelview;
439 GlMat.load_identity ();
440 GlMat.mode `projection;
441 GlMat.load_identity ();
442 GlMat.rotate ~x:1.0 ~angle:180.0 ();
443 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
444 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
445 GlClear.color (1., 1., 1.);
446 GlClear.clear [`color];
447 state.layout <- [];
448 state.pages <- [];
449 state.rects <- [];
450 state.text <- "";
451 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
454 let showtext c s =
455 GlDraw.color (0.0, 0.0, 0.0);
456 GlDraw.rect
457 (0.0, float (state.h - 18))
458 (float (state.w - conf.scrollw - 1), float state.h)
460 let font = Glut.BITMAP_8_BY_13 in
461 GlDraw.color (1.0, 1.0, 1.0);
462 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
463 Glut.bitmapCharacter ~font ~c:(Char.code c);
464 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
467 let enttext () =
468 let len = String.length state.text in
469 match state.textentry with
470 | None ->
471 if len > 0 then showtext ' ' state.text
473 | Some (c, text, _, _, _) ->
474 let s =
475 if len > 0
476 then
477 text ^ " [" ^ state.text ^ "]"
478 else
479 text
481 showtext c s;
484 let act cmd =
485 match cmd.[0] with
486 | 'c' ->
487 state.pages <- [];
488 state.outlines <- Olist []
490 | 'D' ->
491 state.rects <- state.rects1;
492 Glut.postRedisplay ()
494 | 'd' ->
495 state.rects <- state.rects1;
496 Glut.postRedisplay ()
498 | 'C' ->
499 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
500 state.pagecount <- n;
501 let rely = yratio state.y in
502 let maxy = calcheight () in
503 state.y <- truncate (float maxy *. rely);
504 state.ty <- state.y;
505 let pages = layout state.y state.h in
506 state.layout <- pages;
507 Glut.postRedisplay ();
509 | 't' ->
510 let s = Scanf.sscanf cmd "t %n"
511 (fun n -> String.sub cmd n (String.length cmd - n))
513 Glut.setWindowTitle s
515 | 'T' ->
516 let s = Scanf.sscanf cmd "T %n"
517 (fun n -> String.sub cmd n (String.length cmd - n))
519 if state.textentry = None
520 then (
521 state.text <- s;
522 showtext ' ' s;
523 Glut.swapBuffers ();
525 else (
526 state.text <- s;
527 Glut.postRedisplay ();
530 | 'V' ->
531 if conf.verbose
532 then
533 let s = Scanf.sscanf cmd "V %n"
534 (fun n -> String.sub cmd n (String.length cmd - n))
536 state.text <- s;
537 showtext ' ' s;
538 Glut.swapBuffers ();
540 | 'F' ->
541 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
542 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
543 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
544 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
546 let y = (getpagey pageno) + truncate y0 in
547 addnav ();
548 gotoy y;
549 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
551 | 'R' ->
552 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
553 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
554 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
555 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
557 state.rects1 <-
558 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
560 | 'r' ->
561 let n, w, h, r, p =
562 Scanf.sscanf cmd "r %d %d %d %d %s"
563 (fun n w h r p -> (n, w, h, r, p))
565 Hashtbl.replace state.pagemap (n, w, r) p;
566 let evicted = cbpeekw state.pagecache in
567 if String.length evicted > 0
568 then begin
569 wcmd "free" [`s evicted];
570 let l = Hashtbl.fold (fun k p a ->
571 if evicted = p then k :: a else a) state.pagemap []
573 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
574 end;
575 cbput state.pagecache p;
576 state.inflight <- pred state.inflight;
577 if conf.showall then gotoy state.ty;
578 Glut.postRedisplay ()
580 | 'l' ->
581 let (n, w, h) as pagelayout =
582 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
584 state.pages <- pagelayout :: state.pages
586 | 'o' ->
587 let (l, n, t, pos) =
588 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
590 let s = String.sub cmd pos (String.length cmd - pos) in
591 let outline = (s, l, n, t) in
592 let outlines =
593 match state.outlines with
594 | Olist outlines -> Olist (outline :: outlines)
595 | Oarray _ -> Olist [outline]
596 | Onarrow _ -> Olist [outline]
598 state.outlines <- outlines
600 | _ ->
601 log "unknown cmd `%S'" cmd
604 let idle () =
605 if not conf.redispimm && state.y != state.prevy
606 then (
607 state.prevy <- state.y;
608 Glut.postRedisplay ();
610 else
611 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
613 begin match r with
614 | [] ->
615 if conf.preload then begin
616 let h = state.h in
617 let y = if state.y < state.h then 0 else state.y - state.h in
618 let pages = layout y (h*3) in
619 List.iter preload pages;
620 end;
621 if conf.autoscroll then begin
622 let y = state.y + conf.scrollincr in
623 let y = if y >= state.maxy then 0 else y in
624 gotoy y;
625 state.text <- "";
626 state.prevy <- state.y;
627 Glut.postRedisplay ();
628 end;
630 | _ ->
631 let cmd = readcmd state.csock in
632 act cmd;
633 end;
636 let onhist cb = function
637 | HCprev -> cbget cb ~-1
638 | HCnext -> cbget cb 1
639 | HCfirst -> cbget cb ~-(cb.rc)
640 | HClast -> cbget cb (cb.len - 1 - cb.rc)
643 let search pattern forward =
644 if String.length pattern > 0
645 then
646 let pn, py =
647 match state.layout with
648 | [] -> 0, 0
649 | l :: _ ->
650 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
652 let cmd =
653 let b = makecmd "search"
654 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
656 Buffer.add_char b ',';
657 Buffer.add_string b pattern;
658 Buffer.add_char b '\000';
659 Buffer.contents b;
661 writecmd state.csock cmd;
664 let intentry text key =
665 let c = Char.unsafe_chr key in
666 match c with
667 | '0' .. '9' ->
668 let s = "x" in s.[0] <- c;
669 let text = text ^ s in
670 TEcont text
672 | _ ->
673 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
674 TEcont text
677 let addchar s c =
678 let b = Buffer.create (String.length s + 1) in
679 Buffer.add_string b s;
680 Buffer.add_char b c;
681 Buffer.contents b;
684 let textentry text key =
685 let c = Char.unsafe_chr key in
686 match c with
687 | _ when key >= 32 && key < 127 ->
688 let text = addchar text c in
689 TEcont text
691 | _ ->
692 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
693 TEcont text
696 let rotate angle =
697 state.rects <- [];
698 state.rects1 <- [];
699 state.rotate <- angle;
700 wcmd "rotate" [`i angle];
703 let optentry text key =
704 let btos b = if b then "on" else "off" in
705 let c = Char.unsafe_chr key in
706 match c with
707 | 'r' ->
708 conf.rectsel <- not conf.rectsel;
709 TEdone ("rectsel " ^ (btos conf.rectsel))
711 | 's' ->
712 let ondone s =
713 try conf.scrollincr <- int_of_string s with exc ->
714 state.text <- Printf.sprintf "bad integer `%s': %s"
715 s (Printexc.to_string exc)
717 TEswitch ('#', "", None, intentry, ondone)
719 | 'R' ->
720 let ondone s =
721 match try
722 Some (int_of_string s)
723 with exc ->
724 state.text <- Printf.sprintf "bad integer `%s': %s"
725 s (Printexc.to_string exc);
726 None
727 with
728 | Some angle -> rotate angle
729 | None -> ()
731 TEswitch ('^', "", None, intentry, ondone)
733 | 'i' ->
734 conf.icase <- not conf.icase;
735 TEdone ("case insensitive search " ^ (btos conf.icase))
737 | 'p' ->
738 conf.preload <- not conf.preload;
739 TEdone ("preload " ^ (btos conf.preload))
741 | 'd' ->
742 conf.redispimm <- not conf.redispimm;
743 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
745 | 'v' ->
746 conf.verbose <- not conf.verbose;
747 TEdone ("verbose " ^ (btos conf.verbose))
749 | 'h' ->
750 conf.maxhfit <- not conf.maxhfit;
751 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
752 TEdone ("maxhfit " ^ (btos conf.maxhfit))
754 | 'c' ->
755 conf.crophack <- not conf.crophack;
756 TEdone ("crophack " ^ btos conf.crophack)
758 | 'a' ->
759 conf.showall <- not conf.showall;
760 TEdone ("showall " ^ btos conf.showall)
762 | _ ->
763 state.text <- Printf.sprintf "bad option %d `%c'" key c;
764 TEstop
767 let maxoutlinerows () = (state.h - 31) / 16;;
769 let enterselector allowdel outlines errmsg =
770 if Array.length outlines = 0
771 then (
772 showtext ' ' errmsg;
773 Glut.swapBuffers ()
775 else
776 let pageno =
777 match state.layout with
778 | [] -> -1
779 | {pageno=pageno} :: rest -> pageno
781 let active =
782 let rec loop n =
783 if n = Array.length outlines
784 then 0
785 else
786 let (_, _, outlinepageno, _) = outlines.(n) in
787 if outlinepageno >= pageno then n else loop (n+1)
789 loop 0
791 state.outline <-
792 Some (allowdel, active, max 0 (active - maxoutlinerows ()), outlines, "");
793 Glut.postRedisplay ();
796 let enteroutlinemode () =
797 let outlines =
798 match state.outlines with
799 | Oarray a -> a
800 | Olist l ->
801 let a = Array.of_list (List.rev l) in
802 state.outlines <- Oarray a;
804 | Onarrow (a, b) -> a
806 enterselector false outlines "Documents has no outline";
809 let enterbookmarkmode () =
810 let bookmarks = Array.of_list state.bookmarks in
811 enterselector true bookmarks "Documents has no bookmarks (yet)";
815 let quickbookmark ?title () =
816 match state.layout with
817 | [] -> ()
818 | l :: _ ->
819 let title =
820 match title with
821 | None ->
822 let sec = Unix.gettimeofday () in
823 let tm = Unix.localtime sec in
824 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
825 l.pageno
826 tm.Unix.tm_mday
827 tm.Unix.tm_mon
828 (tm.Unix.tm_year + 1900)
829 tm.Unix.tm_hour
830 tm.Unix.tm_min
831 | Some title -> title
833 state.bookmarks <-
834 (title, 0, l.pageno, l.pagey) :: state.bookmarks
837 let viewkeyboard ~key ~x ~y =
838 let enttext te =
839 state.textentry <- te;
840 state.text <- "";
841 enttext ();
842 Glut.postRedisplay ()
844 match state.textentry with
845 | None ->
846 let c = Char.chr key in
847 begin match c with
848 | '\027' | 'q' ->
849 exit 0
851 | '\008' ->
852 let y = getnav () in
853 gotoy y
855 | 'o' ->
856 enteroutlinemode ()
858 | 'u' ->
859 state.rects <- [];
860 state.text <- "";
861 Glut.postRedisplay ()
863 | '/' | '?' ->
864 let ondone isforw s =
865 cbput state.hists.pat s;
866 cbrfollowlen state.hists.pat;
867 state.searchpattern <- s;
868 search s isforw
870 enttext (Some (c, "", Some (onhist state.hists.pat),
871 textentry, ondone (c ='/')))
873 | '+' ->
874 let ondone s =
875 let n =
876 try int_of_string s with exc ->
877 state.text <- Printf.sprintf "bad integer `%s': %s"
878 s (Printexc.to_string exc);
879 max_int
881 if n != max_int
882 then (
883 conf.pagebias <- n;
884 state.text <- "page bias is now " ^ string_of_int n;
887 enttext (Some ('+', "", None, intentry, ondone))
889 | '-' ->
890 let ondone msg =
891 state.text <- msg;
893 enttext (Some ('-', "", None, optentry, ondone))
895 | '0' .. '9' ->
896 let ondone s =
897 let n =
898 try int_of_string s with exc ->
899 state.text <- Printf.sprintf "bad integer `%s': %s"
900 s (Printexc.to_string exc);
903 if n >= 0
904 then (
905 addnav ();
906 cbput state.hists.pag (string_of_int n);
907 cbrfollowlen state.hists.pag;
908 gotoy (getpagey (n + conf.pagebias - 1))
911 let pageentry text key =
912 match Char.unsafe_chr key with
913 | 'g' -> TEdone text
914 | _ -> intentry text key
916 let text = "x" in text.[0] <- c;
917 enttext (Some (':', text, Some (onhist state.hists.pag),
918 pageentry, ondone))
920 | 'b' ->
921 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
922 reshape state.w state.h;
924 | 'l' ->
925 conf.hlinks <- not conf.hlinks;
926 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
927 Glut.postRedisplay ()
929 | 'a' ->
930 conf.autoscroll <- not conf.autoscroll
932 | 'f' ->
933 begin match state.fullscreen with
934 | None ->
935 state.fullscreen <- Some (state.w, state.h);
936 Glut.fullScreen ()
937 | Some (w, h) ->
938 state.fullscreen <- None;
939 Glut.reshapeWindow ~w ~h
942 | 'g' ->
943 gotoy 0
945 | 'n' ->
946 search state.searchpattern true
948 | 'p' | 'N' ->
949 search state.searchpattern false
951 | 't' ->
952 begin match state.layout with
953 | [] -> ()
954 | l :: _ ->
955 gotoy (state.y - l.pagey);
958 | ' ' ->
959 begin match List.rev state.layout with
960 | [] -> ()
961 | l :: _ ->
962 gotoy (clamp (l.pageh - l.pagey))
965 | '\127' ->
966 begin match state.layout with
967 | [] -> ()
968 | l :: _ ->
969 gotoy (clamp (-l.pageh));
972 | '=' ->
973 let f (fn, ln) l =
974 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
976 let fn, ln = List.fold_left f (-1, -1) state.layout in
977 let s =
978 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
979 let percent =
980 if maxy <= 0
981 then 100.
982 else (100. *. (float state.y /. float maxy)) in
983 if fn = ln
984 then
985 Printf.sprintf "Page %d of %d %.2f%%"
986 (fn+1) state.pagecount percent
987 else
988 Printf.sprintf
989 "Pages %d-%d of %d %.2f%%"
990 (fn+1) (ln+1) state.pagecount percent
992 showtext ' ' s;
993 Glut.swapBuffers ()
995 | 'w' ->
996 begin match state.layout with
997 | [] -> ()
998 | l :: _ ->
999 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1000 Glut.postRedisplay ();
1003 | '\'' ->
1004 enterbookmarkmode ()
1006 | 'm' ->
1007 let ondone s =
1008 match state.layout with
1009 | l :: _ ->
1010 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1011 | _ -> ()
1013 enttext (Some ('~', "", None, textentry, ondone))
1015 | '~' ->
1016 quickbookmark ();
1017 showtext ' ' "Quick bookmark added";
1018 Glut.swapBuffers ()
1020 | 'z' ->
1021 begin match state.layout with
1022 | l :: _ ->
1023 let a = getpagewh l.pagedimno in
1024 let w, h =
1025 if conf.crophack
1026 then
1027 (truncate (1.8 *. (a.(1) -. a.(0))),
1028 truncate (1.4 *. (a.(3) -. a.(0))))
1029 else
1030 (truncate (a.(1) -. a.(0)),
1031 truncate (a.(3) -. a.(0)))
1033 Glut.reshapeWindow (w + conf.scrollw) h;
1034 Glut.postRedisplay ();
1036 | [] -> ()
1039 | '<' | '>' ->
1040 rotate (state.rotate + (if c = '>' then 30 else -30));
1042 | _ ->
1043 vlog "huh? %d %c" key (Char.chr key);
1046 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1047 let len = String.length text in
1048 if len = 0
1049 then (
1050 state.textentry <- None;
1051 Glut.postRedisplay ();
1053 else (
1054 let s = String.sub text 0 (len - 1) in
1055 enttext (Some (c, s, onhist, onkey, ondone))
1058 | Some (c, text, onhist, onkey, ondone) ->
1059 begin match Char.unsafe_chr key with
1060 | '\r' | '\n' ->
1061 ondone text;
1062 state.textentry <- None;
1063 Glut.postRedisplay ()
1065 | '\027' ->
1066 state.textentry <- None;
1067 Glut.postRedisplay ()
1069 | _ ->
1070 begin match onkey text key with
1071 | TEdone text ->
1072 state.textentry <- None;
1073 ondone text;
1074 Glut.postRedisplay ()
1076 | TEcont text ->
1077 enttext (Some (c, text, onhist, onkey, ondone));
1079 | TEstop ->
1080 state.textentry <- None;
1081 Glut.postRedisplay ()
1083 | TEswitch te ->
1084 state.textentry <- Some te;
1085 Glut.postRedisplay ()
1086 end;
1087 end;
1090 let narrow outlines pattern =
1091 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1092 match reopt with
1093 | None -> None
1094 | Some re ->
1095 let rec fold accu n =
1096 if n = -1 then accu else
1097 let (s, _, _, _) as o = outlines.(n) in
1098 let accu =
1099 if (try ignore (Str.search_forward re s 0); true
1100 with Not_found -> false)
1101 then (o :: accu)
1102 else accu
1104 fold accu (n-1)
1106 let matched = fold [] (Array.length outlines - 1) in
1107 if matched = [] then None else Some (Array.of_list matched)
1110 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1111 let search active pattern incr =
1112 let dosearch re =
1113 let rec loop n =
1114 if n = Array.length outlines || n = -1 then None else
1115 let (s, _, _, _) = outlines.(n) in
1117 (try ignore (Str.search_forward re s 0); true
1118 with Not_found -> false)
1119 then Some n
1120 else loop (n + incr)
1122 loop active
1125 let re = Str.regexp_case_fold pattern in
1126 dosearch re
1127 with Failure s ->
1128 state.text <- s;
1129 None
1131 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1132 match key with
1133 | 27 ->
1134 if String.length qsearch = 0
1135 then (
1136 state.text <- "";
1137 state.outline <- None;
1138 Glut.postRedisplay ();
1140 else (
1141 state.text <- "";
1142 state.outline <- Some (allowdel, active, first, outlines, "");
1143 Glut.postRedisplay ();
1146 | 18 | 19 ->
1147 let incr = if key = 18 then -1 else 1 in
1148 let active, first =
1149 match search (active + incr) qsearch incr with
1150 | None ->
1151 state.text <- qsearch ^ " [not found]";
1152 active, first
1153 | Some active ->
1154 state.text <- qsearch;
1155 active, firstof active
1157 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1158 Glut.postRedisplay ();
1160 | 8 ->
1161 let len = String.length qsearch in
1162 if len = 0
1163 then ()
1164 else (
1165 if len = 1
1166 then (
1167 state.text <- "";
1168 state.outline <- Some (allowdel, active, first, outlines, "");
1170 else
1171 let qsearch = String.sub qsearch 0 (len - 1) in
1172 let active, first =
1173 match search active qsearch ~-1 with
1174 | None ->
1175 state.text <- qsearch ^ " [not found]";
1176 active, first
1177 | Some active ->
1178 state.text <- qsearch;
1179 active, firstof active
1181 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1183 Glut.postRedisplay ()
1185 | 13 ->
1186 if active < Array.length outlines
1187 then (
1188 let (_, _, n, t) = outlines.(active) in
1189 gotopage n t;
1191 state.text <- "";
1192 if allowdel then state.bookmarks <- Array.to_list outlines;
1193 state.outline <- None;
1194 Glut.postRedisplay ();
1196 | _ when key >= 32 && key < 127 ->
1197 let pattern = addchar qsearch (Char.chr key) in
1198 let active, first =
1199 match search active pattern 1 with
1200 | None ->
1201 state.text <- pattern ^ " [not found]";
1202 active, first
1203 | Some active ->
1204 state.text <- pattern;
1205 active, firstof active
1207 state.outline <- Some (allowdel, active, first, outlines, pattern);
1208 Glut.postRedisplay ()
1210 | 14 when not allowdel ->
1211 let optoutlines = narrow outlines qsearch in
1212 begin match optoutlines with
1213 | None -> state.text <- "can't narrow"
1214 | Some outlines ->
1215 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1216 match state.outlines with
1217 | Olist l -> ()
1218 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1219 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1220 end;
1221 Glut.postRedisplay ()
1223 | 21 when not allowdel ->
1224 let outline =
1225 match state.outlines with
1226 | Oarray a -> a
1227 | Olist l ->
1228 let a = Array.of_list (List.rev l) in
1229 state.outlines <- Oarray a;
1231 | Onarrow (a, b) ->
1232 state.outlines <- Oarray b;
1235 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1236 Glut.postRedisplay ()
1238 | 12 ->
1239 state.outline <-
1240 Some (allowdel, active, firstof active, outlines, qsearch);
1241 Glut.postRedisplay ()
1243 | 127 when allowdel ->
1244 let len = Array.length outlines - 1 in
1245 if len = 0
1246 then (
1247 state.outline <- None;
1248 state.bookmarks <- [];
1250 else (
1251 let bookmarks = Array.init len
1252 (fun i ->
1253 let i = if i >= active then i + 1 else i in
1254 outlines.(i)
1257 state.outline <-
1258 Some (allowdel,
1259 min active (len-1),
1260 min first (len-1),
1261 bookmarks, qsearch)
1264 Glut.postRedisplay ()
1266 | _ -> log "unknown key %d" key
1269 let keyboard ~key ~x ~y =
1270 if key = 7
1271 then
1272 wcmd "interrupt" []
1273 else
1274 match state.outline with
1275 | None -> viewkeyboard ~key ~x ~y
1276 | Some outline -> outlinekeyboard ~key ~x ~y outline
1279 let special ~key ~x ~y =
1280 match state.outline with
1281 | None ->
1282 begin match state.textentry with
1283 | None ->
1284 let y =
1285 match key with
1286 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1287 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1288 | Glut.KEY_DOWN -> clamp conf.scrollincr
1289 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1290 | Glut.KEY_PAGE_DOWN -> clamp state.h
1291 | Glut.KEY_HOME -> addnav (); 0
1292 | Glut.KEY_END ->
1293 addnav ();
1294 state.maxy - (if conf.maxhfit then state.h else 0)
1295 | _ -> state.y
1297 state.text <- "";
1298 gotoy y
1300 | Some (c, s, Some onhist, onkey, ondone) ->
1301 let s =
1302 match key with
1303 | Glut.KEY_UP -> onhist HCprev
1304 | Glut.KEY_DOWN -> onhist HCnext
1305 | Glut.KEY_HOME -> onhist HCfirst
1306 | Glut.KEY_END -> onhist HClast
1307 | _ -> state.text
1309 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1310 Glut.postRedisplay ()
1312 | _ -> ()
1315 | Some (allowdel, active, first, outlines, qsearch) ->
1316 let maxrows = maxoutlinerows () in
1317 let navigate incr =
1318 let active = active + incr in
1319 let active = max 0 (min active (Array.length outlines - 1)) in
1320 let first =
1321 if active > first
1322 then
1323 let rows = active - first in
1324 if rows > maxrows then first + incr else first
1325 else active
1327 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1328 Glut.postRedisplay ()
1330 match key with
1331 | Glut.KEY_UP -> navigate ~-1
1332 | Glut.KEY_DOWN -> navigate 1
1333 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1334 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1336 | Glut.KEY_HOME ->
1337 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1338 Glut.postRedisplay ()
1340 | Glut.KEY_END ->
1341 let active = Array.length outlines - 1 in
1342 let first = max 0 (active - maxrows) in
1343 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1344 Glut.postRedisplay ()
1346 | _ -> ()
1349 let drawplaceholder l =
1350 GlDraw.color (1.0, 1.0, 1.0);
1351 GlDraw.rect
1352 (0.0, float l.pagedispy)
1353 (float l.pagew, float (l.pagedispy + l.pagevh))
1355 let x = 0.0
1356 and y = float (l.pagedispy + 13) in
1357 let font = Glut.BITMAP_8_BY_13 in
1358 GlDraw.color (0.0, 0.0, 0.0);
1359 GlPix.raster_pos ~x ~y ();
1360 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1361 ("Loading " ^ string_of_int l.pageno);
1364 let now () = Unix.gettimeofday ();;
1366 let drawpage i l =
1367 begin match getopaque l.pageno with
1368 | Some opaque when validopaque opaque ->
1369 if state.textentry = None
1370 then GlDraw.color (1.0, 1.0, 1.0)
1371 else GlDraw.color (0.4, 0.4, 0.4);
1372 let a = now () in
1373 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1374 let b = now () in
1375 let d = b-.a in
1376 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1377 vlog "draw %f sec" d;
1379 | Some _ ->
1380 drawplaceholder l
1382 | None ->
1383 drawplaceholder l;
1384 if state.inflight < cblen state.pagecache
1385 then (
1386 List.iter preload state.layout;
1388 else (
1389 vlog "inflight %d" state.inflight;
1391 end;
1392 GlDraw.color (0.5, 0.5, 0.5);
1393 GlDraw.rect
1394 (0., float i)
1395 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1397 l.pagedispy + l.pagevh;
1400 let scrollindicator () =
1401 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1402 GlDraw.color (0.64 , 0.64, 0.64);
1403 GlDraw.rect
1404 (float (state.w - conf.scrollw), 0.)
1405 (float state.w, float state.h)
1407 GlDraw.color (0.0, 0.0, 0.0);
1408 let sh = (float (maxy + state.h) /. float state.h) in
1409 let sh = float state.h /. sh in
1410 let sh = max sh (float conf.scrollh) in
1412 let percent =
1413 if state.y = state.maxy
1414 then 1.0
1415 else float state.y /. float maxy
1417 let position = (float state.h -. sh) *. percent in
1419 let position =
1420 if position +. sh > float state.h
1421 then
1422 float state.h -. sh
1423 else
1424 position
1426 GlDraw.rect
1427 (float (state.w - conf.scrollw), position)
1428 (float state.w, position +. sh)
1432 let showsel () =
1433 match state.mstate with
1434 | Mnone ->
1437 | Msel ((x0, y0), (x1, y1)) ->
1438 let y0' = min y0 y1
1439 and y1 = max y0 y1 in
1440 let y0 = y0' in
1441 let f l =
1442 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1443 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1444 then
1445 match getopaque l.pageno with
1446 | Some opaque when validopaque opaque ->
1447 let oy = -l.pagey + l.pagedispy in
1448 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1449 | _ -> ()
1451 List.iter f state.layout
1454 let showrects () =
1455 Gl.enable `blend;
1456 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1457 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1458 List.iter
1459 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1460 List.iter (fun l ->
1461 if l.pageno = pageno
1462 then (
1463 let d = float (l.pagedispy - l.pagey) in
1464 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1465 GlDraw.begins `quads;
1467 GlDraw.vertex2 (x0, y0+.d);
1468 GlDraw.vertex2 (x1, y1+.d);
1469 GlDraw.vertex2 (x2, y2+.d);
1470 GlDraw.vertex2 (x3, y3+.d);
1472 GlDraw.ends ();
1473 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1475 ) state.layout
1476 ) state.rects
1478 Gl.disable `blend;
1481 let showoutline = function
1482 | None -> ()
1483 | Some (allowdel, active, first, outlines, qsearch) ->
1484 Gl.enable `blend;
1485 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1486 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1487 GlDraw.rect (0., 0.) (float state.w, float state.h);
1488 Gl.disable `blend;
1490 GlDraw.color (1., 1., 1.);
1491 let font = Glut.BITMAP_9_BY_15 in
1492 let draw_string x y s =
1493 GlPix.raster_pos ~x ~y ();
1494 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1496 let rec loop row =
1497 if row = Array.length outlines || (row - first) * 16 > state.h
1498 then ()
1499 else (
1500 let (s, l, _, _) = outlines.(row) in
1501 let y = (row - first) * 16 in
1502 let x = 5 + 5*l in
1503 if row = active
1504 then (
1505 Gl.enable `blend;
1506 GlDraw.polygon_mode `both `line;
1507 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1508 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1509 GlDraw.rect (0., float (y + 1))
1510 (float (state.w - conf.scrollw - 1), float (y + 18));
1511 GlDraw.polygon_mode `both `fill;
1512 Gl.disable `blend;
1513 GlDraw.color (1., 1., 1.);
1515 draw_string (float x) (float (y + 16)) s;
1516 loop (row+1)
1519 loop first
1522 let display () =
1523 let lasty = List.fold_left drawpage 0 (state.layout) in
1524 GlDraw.color (0.5, 0.5, 0.5);
1525 GlDraw.rect
1526 (0., float lasty)
1527 (float (state.w - conf.scrollw), float state.h)
1529 showrects ();
1530 scrollindicator ();
1531 showsel ();
1532 showoutline state.outline;
1533 enttext ();
1534 Glut.swapBuffers ();
1537 let getlink x y =
1538 let rec f = function
1539 | l :: rest ->
1540 begin match getopaque l.pageno with
1541 | Some opaque when validopaque opaque ->
1542 let y = y - l.pagedispy in
1543 if y > 0
1544 then
1545 let y = l.pagey + y in
1546 match getlink opaque x y with
1547 | None -> f rest
1548 | some -> some
1549 else
1550 f rest
1551 | _ ->
1552 f rest
1554 | [] -> None
1556 f state.layout
1559 let checklink x y =
1560 let rec f = function
1561 | l :: rest ->
1562 begin match getopaque l.pageno with
1563 | Some opaque when validopaque opaque ->
1564 let y = y - l.pagedispy in
1565 if y > 0
1566 then
1567 let y = l.pagey + y in
1568 if checklink opaque x y then true else f rest
1569 else
1570 f rest
1571 | _ ->
1572 f rest
1574 | [] -> false
1576 f state.layout
1579 let mouse ~button ~bstate ~x ~y =
1580 match button with
1581 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1582 let incr =
1583 if n = 3
1584 then
1585 -conf.scrollincr
1586 else
1587 conf.scrollincr
1589 let incr = incr * 2 in
1590 let y = clamp incr in
1591 gotoy y
1593 | Glut.LEFT_BUTTON when state.outline = None ->
1594 let dest = if bstate = Glut.DOWN then getlink x y else None in
1595 begin match dest with
1596 | Some (pageno, top) ->
1597 gotopage pageno top
1599 | None ->
1600 if bstate = Glut.DOWN
1601 then (
1602 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1603 state.mstate <- Msel ((x, y), (x, y));
1604 Glut.postRedisplay ()
1606 else (
1607 Glut.setCursor Glut.CURSOR_INHERIT;
1608 state.mstate <- Mnone;
1612 | _ ->
1615 let mouse ~button ~state ~x ~y = mouse button state x y;;
1617 let motion ~x ~y =
1618 if state.outline = None
1619 then
1620 match state.mstate with
1621 | Mnone -> ()
1622 | Msel (a, _) ->
1623 state.mstate <- Msel (a, (x, y));
1624 Glut.postRedisplay ()
1627 let pmotion ~x ~y =
1628 if state.outline = None
1629 then
1630 match state.mstate with
1631 | Mnone when (checklink x y) ->
1632 Glut.setCursor Glut.CURSOR_INFO
1634 | Mnone ->
1635 Glut.setCursor Glut.CURSOR_INHERIT
1637 | Msel (a, _) ->
1641 let () =
1642 let statepath =
1643 let home =
1644 if Sys.os_type = "Win32"
1645 then
1646 try Sys.getenv "HOMEPATH" with Not_found -> ""
1647 else
1648 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1650 Filename.concat home "llpp"
1652 let pstate =
1654 let ic = open_in_bin statepath in
1655 let hash = input_value ic in
1656 close_in ic;
1657 hash
1658 with exn ->
1659 if false
1660 then
1661 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1663 Hashtbl.create 1
1665 let savestate () =
1667 let w, h =
1668 match state.fullscreen with
1669 | None -> state.w, state.h
1670 | Some wh -> wh
1672 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1673 let oc = open_out_bin statepath in
1674 output_value oc pstate
1675 with exn ->
1676 if false
1677 then
1678 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1681 let setstate () =
1683 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1684 state.w <- statew;
1685 state.h <- stateh;
1686 state.bookmarks <- statebookmarks;
1687 with Not_found -> ()
1688 | exn ->
1689 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1692 Arg.parse [] (fun s -> state.path <- s) "options:";
1693 let name =
1694 if String.length state.path = 0
1695 then (prerr_endline "filename missing"; exit 1)
1696 else state.path
1699 setstate ();
1700 let _ = Glut.init Sys.argv in
1701 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1702 let () = Glut.initWindowSize state.w state.h in
1703 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1705 let csock, ssock =
1706 if Sys.os_type = "Unix"
1707 then
1708 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1709 else
1710 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1711 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1712 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1713 Unix.bind sock addr;
1714 Unix.listen sock 1;
1715 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1716 Unix.connect csock addr;
1717 let ssock, _ = Unix.accept sock in
1718 Unix.close sock;
1719 let opts sock =
1720 Unix.setsockopt sock Unix.TCP_NODELAY true;
1721 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1723 opts ssock;
1724 opts csock;
1725 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1726 ssock, csock
1729 let () = Glut.displayFunc display in
1730 let () = Glut.reshapeFunc reshape in
1731 let () = Glut.keyboardFunc keyboard in
1732 let () = Glut.specialFunc special in
1733 let () = Glut.idleFunc (Some idle) in
1734 let () = Glut.mouseFunc mouse in
1735 let () = Glut.motionFunc motion in
1736 let () = Glut.passiveMotionFunc pmotion in
1738 init ssock;
1739 state.csock <- csock;
1740 state.ssock <- ssock;
1741 writecmd csock ("open " ^ name ^ "\000");
1743 at_exit savestate;
1745 let rec handlelablglutbug () =
1747 Glut.mainLoop ();
1748 with Glut.BadEnum "key in special_of_int" ->
1749 showtext '!' " LablGlut bug: special key not recognized";
1750 Glut.swapBuffers ();
1751 handlelablglutbug ()
1753 handlelablglutbug ();