Update
[llpp.git] / main.ml
blobdb16d83bab41d05bb95c81d3a4de4030e90e1c1c
1 type link = | LNone | LUri of string | LGoto of (int * int);;
3 let log fmt = Printf.kprintf prerr_endline fmt;;
4 let dolog fmt = Printf.kprintf prerr_endline fmt;;
6 external init : Unix.file_descr -> unit = "ml_init";;
7 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
8 external seltext : string -> (int * int * int * int) -> int -> unit =
9 "ml_seltext";;
10 external copysel : string -> unit = "ml_copysel";;
11 external getlink : string -> int -> int -> link = "ml_getlink";;
12 external highlightlinks : string -> int -> unit = "ml_highlightlinks";;
13 external getpagewh : int -> float array = "ml_getpagewh";;
15 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
17 type 'a circbuf =
18 { store : 'a array
19 ; mutable rc : int
20 ; mutable wc : int
21 ; mutable len : int
25 type textentry = (char * string * onhist option * onkey * ondone)
26 and onkey = string -> int -> te
27 and ondone = string -> unit
28 and onhist = histcmd -> string
29 and histcmd = HCnext | HCprev | HCfirst | HClast
30 and te =
31 | TEstop
32 | TEdone of string
33 | TEcont of string
34 | TEswitch of textentry
37 let cbnew n v =
38 { store = Array.create n v
39 ; rc = 0
40 ; wc = 0
41 ; len = 0
45 let cblen b = Array.length b.store;;
47 let cbput b v =
48 let len = cblen b in
49 b.store.(b.wc) <- v;
50 b.wc <- (b.wc + 1) mod len;
51 b.len <- min (b.len + 1) len;
54 let cbpeekw b = b.store.(b.wc);;
56 let cbget b dir =
57 if b.len = 0 then b.store.(0) else
58 let rc = b.rc + dir in
59 let rc = if rc = -1 then b.len - 1 else rc in
60 let rc = if rc = b.len then 0 else rc in
61 b.rc <- rc;
62 b.store.(rc);
65 let cbrfollowlen b =
66 b.rc <- b.len;
69 type layout =
70 { pageno : int
71 ; pagedimno : int
72 ; pagew : int
73 ; pageh : int
74 ; pagedispy : int
75 ; pagey : int
76 ; pagevh : int
80 type conf =
81 { mutable scrollw : int
82 ; mutable scrollh : int
83 ; mutable icase : bool
84 ; mutable preload : bool
85 ; mutable pagebias : int
86 ; mutable verbose : bool
87 ; mutable scrollincr : int
88 ; mutable maxhfit : bool
89 ; mutable crophack : bool
90 ; mutable autoscroll : bool
91 ; mutable showall : bool
92 ; mutable hlinks : bool
96 type outline = string * int * int * int;;
97 type outlines =
98 | Oarray of outline array
99 | Olist of outline list
100 | Onarrow of outline array * outline array
103 type rect = (float * float * float * float * float * float * float * float);;
105 type state =
106 { mutable csock : Unix.file_descr
107 ; mutable ssock : Unix.file_descr
108 ; mutable w : int
109 ; mutable h : int
110 ; mutable rotate : int
111 ; mutable y : int
112 ; mutable ty : float
113 ; mutable maxy : int
114 ; mutable layout : layout list
115 ; pagemap : ((int * int * int), string) Hashtbl.t
116 ; mutable pages : (int * int * int) list
117 ; mutable pagecount : int
118 ; pagecache : string circbuf
119 ; mutable 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 ; preload = false
146 ; pagebias = 0
147 ; verbose = false
148 ; scrollincr = 24
149 ; maxhfit = true
150 ; crophack = false
151 ; autoscroll = false
152 ; showall = false
153 ; hlinks = false
157 let state =
158 { csock = Unix.stdin
159 ; ssock = Unix.stdin
160 ; w = 900
161 ; h = 900
162 ; rotate = 0
163 ; y = 0
164 ; ty = 0.0
165 ; layout = []
166 ; maxy = max_int
167 ; pagemap = Hashtbl.create 10
168 ; pagecache = cbnew 10 ""
169 ; pages = []
170 ; pagecount = 0
171 ; inflight = 0
172 ; mstate = Mnone
173 ; rects = []
174 ; rects1 = []
175 ; text = ""
176 ; fullscreen = None
177 ; textentry = None
178 ; searchpattern = ""
179 ; outlines = Olist []
180 ; outline = None
181 ; bookmarks = []
182 ; path = ""
183 ; invalidated = 0
184 ; hists =
185 { nav = cbnew 100 0.0
186 ; pat = cbnew 20 ""
187 ; pag = cbnew 10 ""
192 let vlog fmt =
193 if conf.verbose
194 then
195 Printf.kprintf prerr_endline fmt
196 else
197 Printf.kprintf ignore fmt
200 let writecmd fd s =
201 let len = String.length s in
202 let n = 4 + len in
203 let b = Buffer.create n in
204 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
205 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
206 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
207 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
208 Buffer.add_string b s;
209 let s' = Buffer.contents b in
210 let n' = Unix.write fd s' 0 n in
211 if n' != n then failwith "write failed";
214 let readcmd fd =
215 let s = "xxxx" in
216 let n = Unix.read fd s 0 4 in
217 if n != 4 then failwith "incomplete read(len)";
218 let len = 0
219 lor (Char.code s.[0] lsl 24)
220 lor (Char.code s.[1] lsl 16)
221 lor (Char.code s.[2] lsl 8)
222 lor (Char.code s.[3] lsl 0)
224 let s = String.create len in
225 let n = Unix.read fd s 0 len in
226 if n != len then failwith "incomplete read(data)";
230 let yratio y =
231 if y = state.maxy then 1.0
232 else float y /. float state.maxy
235 let makecmd s l =
236 let b = Buffer.create 10 in
237 Buffer.add_string b s;
238 let rec combine = function
239 | [] -> b
240 | x :: xs ->
241 Buffer.add_char b ' ';
242 let s =
243 match x with
244 | `b b -> if b then "1" else "0"
245 | `s s -> s
246 | `i i -> string_of_int i
247 | `f f -> string_of_float f
248 | `I f -> string_of_int (truncate f)
250 Buffer.add_string b s;
251 combine xs;
253 combine l;
256 let wcmd s l =
257 let cmd = Buffer.contents (makecmd s l) in
258 writecmd state.csock cmd;
261 let calcheight () =
262 let rec f pn ph fh l =
263 match l with
264 | (n, _, h) :: rest ->
265 let fh = fh + (n - pn) * ph in
266 f n h fh rest
268 | [] ->
269 let fh = fh + (ph * (state.pagecount - pn)) in
270 max 0 fh
272 let fh = f 0 0 0 state.pages in
276 let getpagey pageno =
277 let rec f pn ph y l =
278 match l with
279 | (n, _, h) :: rest ->
280 if n >= pageno
281 then
282 y + (pageno - pn) * ph
283 else
284 let y = y + (n - pn) * ph in
285 f n h y rest
287 | [] ->
288 y + (pageno - pn) * ph
290 f 0 0 0 state.pages;
293 let layout y sh =
294 let rec f pageno pdimno prev vy py dy l cacheleft accu =
295 if pageno = state.pagecount || cacheleft = 0
296 then accu
297 else
298 let ((_, w, h) as curr), rest, pdimno =
299 match l with
300 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
301 curr, rest, pdimno + 1
302 | _ ->
303 prev, l, pdimno
305 let pageno' = pageno + 1 in
306 if py + h > vy
307 then
308 let py' = vy - py in
309 let vh = h - py' in
310 if dy + vh > sh
311 then
312 let vh = sh - dy in
313 if vh <= 0
314 then
315 accu
316 else
317 let e =
318 { pageno = pageno
319 ; pagedimno = pdimno
320 ; pagew = w
321 ; pageh = h
322 ; pagedispy = dy
323 ; pagey = py'
324 ; pagevh = vh
327 e :: accu
328 else
329 let e =
330 { pageno = pageno
331 ; pagedimno = pdimno
332 ; pagew = w
333 ; pageh = h
334 ; pagedispy = dy
335 ; pagey = py'
336 ; pagevh = vh
339 let accu = e :: accu in
340 f pageno' pdimno curr
341 (vy + vh) (py + h) (dy + vh + 2) rest
342 (pred cacheleft) accu
343 else
344 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
346 if state.invalidated = 0
347 then
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
351 else
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 preloadlayout layout =
389 let rec f all = function
390 | l :: ls ->
391 begin match getopaque l.pageno with
392 | None -> preload l; f false ls
393 | Some opaque -> f (all && validopaque opaque) ls
395 | [] -> all
397 f (layout <> []) layout;
400 let gotoy y =
401 let y = max 0 y in
402 let y = min state.maxy y in
403 let pages = layout y state.h in
404 let ready = preloadlayout pages in
405 state.ty <- yratio y;
406 if conf.showall then (
407 if ready then (
408 state.layout <- pages;
409 state.y <- y;
410 Glut.postRedisplay ();
413 else (
414 state.layout <- pages;
415 state.y <- y;
416 Glut.postRedisplay ();
418 if conf.preload then begin
419 let y = if state.y < state.h then 0 else state.y - state.h in
420 let pages = layout y (state.h*3) in
421 List.iter preload pages;
422 end;
425 let addnav () =
426 cbput state.hists.nav (yratio state.y);
427 cbrfollowlen state.hists.nav;
430 let getnav () =
431 let y = cbget state.hists.nav ~-1 in
432 truncate (y *. float state.maxy)
435 let gotopage n top =
436 let y = getpagey n in
437 addnav ();
438 gotoy (y + top);
441 let invalidate () =
442 state.layout <- [];
443 state.pages <- [];
444 state.rects <- [];
445 state.rects1 <- [];
446 state.invalidated <- state.invalidated + 1;
449 let reshape ~w ~h =
450 let ratio = float w /. float state.w in
451 let fixbookmark (s, l, pageno, pagey) =
452 let pagey = truncate (float pagey *. ratio) in
453 (s, l, pageno, pagey)
455 state.bookmarks <- List.map fixbookmark state.bookmarks;
456 state.w <- w;
457 state.h <- h;
458 GlDraw.viewport 0 0 w h;
459 GlMat.mode `modelview;
460 GlMat.load_identity ();
461 GlMat.mode `projection;
462 GlMat.load_identity ();
463 GlMat.rotate ~x:1.0 ~angle:180.0 ();
464 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
465 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
466 GlClear.color (1., 1., 1.);
467 GlClear.clear [`color];
468 invalidate ();
469 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
472 let showtext c s =
473 GlDraw.color (0.0, 0.0, 0.0);
474 GlDraw.rect
475 (0.0, float (state.h - 18))
476 (float (state.w - conf.scrollw - 1), float state.h)
478 let font = Glut.BITMAP_8_BY_13 in
479 GlDraw.color (1.0, 1.0, 1.0);
480 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
481 Glut.bitmapCharacter ~font ~c:(Char.code c);
482 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
485 let enttext () =
486 let len = String.length state.text in
487 match state.textentry with
488 | None ->
489 if len > 0 then showtext ' ' state.text
491 | Some (c, text, _, _, _) ->
492 let s =
493 if len > 0
494 then
495 text ^ " [" ^ state.text ^ "]"
496 else
497 text
499 showtext c s;
502 let act cmd =
503 match cmd.[0] with
504 | 'c' ->
505 state.pages <- [];
506 state.outlines <- Olist []
508 | 'D' ->
509 state.rects <- state.rects1;
510 Glut.postRedisplay ()
512 | 'C' ->
513 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
514 state.pagecount <- n;
515 state.invalidated <- state.invalidated - 1;
516 if state.invalidated = 0
517 then (
518 let rely = yratio state.y in
519 let maxy = calcheight () in
520 state.y <- truncate (float maxy *. rely);
521 let pages = layout state.y state.h in
522 state.layout <- pages;
523 Glut.postRedisplay ();
526 | 't' ->
527 let s = Scanf.sscanf cmd "t %n"
528 (fun n -> String.sub cmd n (String.length cmd - n))
530 Glut.setWindowTitle s
532 | 'T' ->
533 let s = Scanf.sscanf cmd "T %n"
534 (fun n -> String.sub cmd n (String.length cmd - n))
536 if state.textentry = None
537 then (
538 state.text <- s;
539 showtext ' ' s;
540 Glut.swapBuffers ();
542 else (
543 state.text <- s;
544 Glut.postRedisplay ();
547 | 'V' ->
548 if conf.verbose
549 then
550 let s = Scanf.sscanf cmd "V %n"
551 (fun n -> String.sub cmd n (String.length cmd - n))
553 state.text <- s;
554 showtext ' ' s;
555 Glut.swapBuffers ();
557 | 'F' ->
558 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
559 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
560 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
561 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
563 let y = (getpagey pageno) + truncate y0 in
564 addnav ();
565 gotoy y;
566 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
568 | 'R' ->
569 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
570 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
571 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
572 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
574 state.rects1 <-
575 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
577 | 'r' ->
578 let n, w, h, r, p =
579 Scanf.sscanf cmd "r %d %d %d %d %s"
580 (fun n w h r p -> (n, w, h, r, p))
582 Hashtbl.replace state.pagemap (n, w, r) p;
583 let evicted = cbpeekw state.pagecache in
584 if String.length evicted > 0
585 then begin
586 wcmd "free" [`s evicted];
587 let l = Hashtbl.fold (fun k p a ->
588 if evicted = p then k :: a else a) state.pagemap []
590 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
591 end;
592 cbput state.pagecache p;
593 state.inflight <- pred state.inflight;
594 if conf.showall then (
595 let y = truncate (ceil (state.ty *. float state.maxy)) in
596 let layout = layout y state.h in
597 if preloadlayout layout
598 then (
599 state.y <- y;
600 state.layout <- layout;
601 Glut.postRedisplay ();
604 else (
605 Glut.postRedisplay ();
608 | 'l' ->
609 let (n, w, h) as pagelayout =
610 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
612 state.pages <- pagelayout :: state.pages
614 | 'o' ->
615 let (l, n, t, pos) =
616 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
618 let s = String.sub cmd pos (String.length cmd - pos) in
619 let outline = (s, l, n, t) in
620 let outlines =
621 match state.outlines with
622 | Olist outlines -> Olist (outline :: outlines)
623 | Oarray _ -> Olist [outline]
624 | Onarrow _ -> Olist [outline]
626 state.outlines <- outlines
628 | _ ->
629 log "unknown cmd `%S'" cmd
632 let now = Unix.gettimeofday;;
634 let idle () =
635 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
636 begin match r with
637 | [] ->
638 if conf.autoscroll then begin
639 let y = state.y + conf.scrollincr in
640 let y = if y >= state.maxy then 0 else y in
641 gotoy y;
642 state.text <- "";
643 end;
645 | _ ->
646 let cmd = readcmd state.csock in
647 act cmd;
648 end;
651 let onhist cb = function
652 | HCprev -> cbget cb ~-1
653 | HCnext -> cbget cb 1
654 | HCfirst -> cbget cb ~-(cb.rc)
655 | HClast -> cbget cb (cb.len - 1 - cb.rc)
658 let search pattern forward =
659 if String.length pattern > 0
660 then
661 let pn, py =
662 match state.layout with
663 | [] -> 0, 0
664 | l :: _ ->
665 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
667 let cmd =
668 let b = makecmd "search"
669 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
671 Buffer.add_char b ',';
672 Buffer.add_string b pattern;
673 Buffer.add_char b '\000';
674 Buffer.contents b;
676 writecmd state.csock cmd;
679 let intentry text key =
680 let c = Char.unsafe_chr key in
681 match c with
682 | '0' .. '9' ->
683 let s = "x" in s.[0] <- c;
684 let text = text ^ s in
685 TEcont text
687 | _ ->
688 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
689 TEcont text
692 let addchar s c =
693 let b = Buffer.create (String.length s + 1) in
694 Buffer.add_string b s;
695 Buffer.add_char b c;
696 Buffer.contents b;
699 let textentry text key =
700 let c = Char.unsafe_chr key in
701 match c with
702 | _ when key >= 32 && key < 127 ->
703 let text = addchar text c in
704 TEcont text
706 | _ ->
707 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
708 TEcont text
711 let rotate angle =
712 state.rotate <- angle;
713 invalidate ();
714 wcmd "rotate" [`i angle];
717 let optentry text key =
718 let btos b = if b then "on" else "off" in
719 let c = Char.unsafe_chr key in
720 match c with
721 | 's' ->
722 let ondone s =
723 try conf.scrollincr <- int_of_string s with exc ->
724 state.text <- Printf.sprintf "bad integer `%s': %s"
725 s (Printexc.to_string exc)
727 TEswitch ('#', "", None, intentry, ondone)
729 | 'R' ->
730 let ondone s =
731 match try
732 Some (int_of_string s)
733 with exc ->
734 state.text <- Printf.sprintf "bad integer `%s': %s"
735 s (Printexc.to_string exc);
736 None
737 with
738 | Some angle -> rotate angle
739 | None -> ()
741 TEswitch ('^', "", None, intentry, ondone)
743 | 'i' ->
744 conf.icase <- not conf.icase;
745 TEdone ("case insensitive search " ^ (btos conf.icase))
747 | 'p' ->
748 conf.preload <- not conf.preload;
749 gotoy state.y;
750 TEdone ("preload " ^ (btos conf.preload))
752 | 'v' ->
753 conf.verbose <- not conf.verbose;
754 TEdone ("verbose " ^ (btos conf.verbose))
756 | 'h' ->
757 conf.maxhfit <- not conf.maxhfit;
758 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
759 TEdone ("maxhfit " ^ (btos conf.maxhfit))
761 | 'c' ->
762 conf.crophack <- not conf.crophack;
763 TEdone ("crophack " ^ btos conf.crophack)
765 | 'a' ->
766 conf.showall <- not conf.showall;
767 TEdone ("showall " ^ btos conf.showall)
769 | _ ->
770 state.text <- Printf.sprintf "bad option %d `%c'" key c;
771 TEstop
774 let maxoutlinerows () = (state.h - 31) / 16;;
776 let enterselector allowdel outlines errmsg =
777 if Array.length outlines = 0
778 then (
779 showtext ' ' errmsg;
780 Glut.swapBuffers ()
782 else
783 let pageno =
784 match state.layout with
785 | [] -> -1
786 | {pageno=pageno} :: rest -> pageno
788 let active =
789 let rec loop n =
790 if n = Array.length outlines
791 then 0
792 else
793 let (_, _, outlinepageno, _) = outlines.(n) in
794 if outlinepageno >= pageno then n else loop (n+1)
796 loop 0
798 state.outline <-
799 Some (allowdel, active,
800 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
801 Glut.postRedisplay ();
804 let enteroutlinemode () =
805 let outlines =
806 match state.outlines with
807 | Oarray a -> a
808 | Olist l ->
809 let a = Array.of_list (List.rev l) in
810 state.outlines <- Oarray a;
812 | Onarrow (a, b) -> a
814 enterselector false outlines "Document has no outline";
817 let enterbookmarkmode () =
818 let bookmarks = Array.of_list state.bookmarks in
819 enterselector true bookmarks "Document has no bookmarks (yet)";
823 let quickbookmark ?title () =
824 match state.layout with
825 | [] -> ()
826 | l :: _ ->
827 let title =
828 match title with
829 | None ->
830 let sec = Unix.gettimeofday () in
831 let tm = Unix.localtime sec in
832 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
833 l.pageno
834 tm.Unix.tm_mday
835 tm.Unix.tm_mon
836 (tm.Unix.tm_year + 1900)
837 tm.Unix.tm_hour
838 tm.Unix.tm_min
839 | Some title -> title
841 state.bookmarks <-
842 (title, 0, l.pageno, l.pagey) :: state.bookmarks
845 let viewkeyboard ~key ~x ~y =
846 let enttext te =
847 state.textentry <- te;
848 state.text <- "";
849 enttext ();
850 Glut.postRedisplay ()
852 match state.textentry with
853 | None ->
854 let c = Char.chr key in
855 begin match c with
856 | '\027' | 'q' ->
857 exit 0
859 | '\008' ->
860 let y = getnav () in
861 gotoy y
863 | 'o' ->
864 enteroutlinemode ()
866 | 'u' ->
867 state.rects <- [];
868 state.text <- "";
869 Glut.postRedisplay ()
871 | '/' | '?' ->
872 let ondone isforw s =
873 cbput state.hists.pat s;
874 cbrfollowlen state.hists.pat;
875 state.searchpattern <- s;
876 search s isforw
878 enttext (Some (c, "", Some (onhist state.hists.pat),
879 textentry, ondone (c ='/')))
881 | '+' ->
882 let ondone s =
883 let n =
884 try int_of_string s with exc ->
885 state.text <- Printf.sprintf "bad integer `%s': %s"
886 s (Printexc.to_string exc);
887 max_int
889 if n != max_int
890 then (
891 conf.pagebias <- n;
892 state.text <- "page bias is now " ^ string_of_int n;
895 enttext (Some ('+', "", None, intentry, ondone))
897 | '-' ->
898 let ondone msg =
899 state.text <- msg;
901 enttext (Some ('-', "", None, optentry, ondone))
903 | '0' .. '9' ->
904 let ondone s =
905 let n =
906 try int_of_string s with exc ->
907 state.text <- Printf.sprintf "bad integer `%s': %s"
908 s (Printexc.to_string exc);
911 if n >= 0
912 then (
913 addnav ();
914 cbput state.hists.pag (string_of_int n);
915 cbrfollowlen state.hists.pag;
916 gotoy (getpagey (n + conf.pagebias - 1))
919 let pageentry text key =
920 match Char.unsafe_chr key with
921 | 'g' -> TEdone text
922 | _ -> intentry text key
924 let text = "x" in text.[0] <- c;
925 enttext (Some (':', text, Some (onhist state.hists.pag),
926 pageentry, ondone))
928 | 'b' ->
929 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
930 reshape state.w state.h;
932 | 'l' ->
933 conf.hlinks <- not conf.hlinks;
934 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
935 Glut.postRedisplay ()
937 | 'a' ->
938 conf.autoscroll <- not conf.autoscroll
940 | 'f' ->
941 begin match state.fullscreen with
942 | None ->
943 state.fullscreen <- Some (state.w, state.h);
944 Glut.fullScreen ()
945 | Some (w, h) ->
946 state.fullscreen <- None;
947 Glut.reshapeWindow ~w ~h
950 | 'g' ->
951 gotoy 0
953 | 'n' ->
954 search state.searchpattern true
956 | 'p' | 'N' ->
957 search state.searchpattern false
959 | 't' ->
960 begin match state.layout with
961 | [] -> ()
962 | l :: _ ->
963 gotoy (state.y - l.pagey);
966 | ' ' ->
967 begin match List.rev state.layout with
968 | [] -> ()
969 | l :: _ ->
970 gotoy (clamp (l.pageh - l.pagey))
973 | '\127' ->
974 begin match state.layout with
975 | [] -> ()
976 | l :: _ ->
977 gotoy (clamp (-l.pageh));
980 | '=' ->
981 let f (fn, ln) l =
982 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
984 let fn, ln = List.fold_left f (-1, -1) state.layout in
985 let s =
986 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
987 let percent =
988 if maxy <= 0
989 then 100.
990 else (100. *. (float state.y /. float maxy)) in
991 if fn = ln
992 then
993 Printf.sprintf "Page %d of %d %.2f%%"
994 (fn+1) state.pagecount percent
995 else
996 Printf.sprintf
997 "Pages %d-%d of %d %.2f%%"
998 (fn+1) (ln+1) state.pagecount percent
1000 showtext ' ' s;
1001 Glut.swapBuffers ()
1003 | 'w' ->
1004 begin match state.layout with
1005 | [] -> ()
1006 | l :: _ ->
1007 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1008 Glut.postRedisplay ();
1011 | '\'' ->
1012 enterbookmarkmode ()
1014 | 'm' ->
1015 let ondone s =
1016 match state.layout with
1017 | l :: _ ->
1018 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1019 | _ -> ()
1021 enttext (Some ('~', "", None, textentry, ondone))
1023 | '~' ->
1024 quickbookmark ();
1025 showtext ' ' "Quick bookmark added";
1026 Glut.swapBuffers ()
1028 | 'z' ->
1029 begin match state.layout with
1030 | l :: _ ->
1031 let a = getpagewh l.pagedimno in
1032 let w, h =
1033 if conf.crophack
1034 then
1035 (truncate (1.8 *. (a.(1) -. a.(0))),
1036 truncate (1.2 *. (a.(3) -. a.(0))))
1037 else
1038 (truncate (a.(1) -. a.(0)),
1039 truncate (a.(3) -. a.(0)))
1041 Glut.reshapeWindow (w + conf.scrollw) h;
1042 Glut.postRedisplay ();
1044 | [] -> ()
1047 | '<' | '>' ->
1048 rotate (state.rotate + (if c = '>' then 30 else -30));
1050 | _ ->
1051 vlog "huh? %d %c" key (Char.chr key);
1054 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1055 let len = String.length text in
1056 if len = 0
1057 then (
1058 state.textentry <- None;
1059 Glut.postRedisplay ();
1061 else (
1062 let s = String.sub text 0 (len - 1) in
1063 enttext (Some (c, s, onhist, onkey, ondone))
1066 | Some (c, text, onhist, onkey, ondone) ->
1067 begin match Char.unsafe_chr key with
1068 | '\r' | '\n' ->
1069 ondone text;
1070 state.textentry <- None;
1071 Glut.postRedisplay ()
1073 | '\027' ->
1074 state.textentry <- None;
1075 Glut.postRedisplay ()
1077 | _ ->
1078 begin match onkey text key with
1079 | TEdone text ->
1080 state.textentry <- None;
1081 ondone text;
1082 Glut.postRedisplay ()
1084 | TEcont text ->
1085 enttext (Some (c, text, onhist, onkey, ondone));
1087 | TEstop ->
1088 state.textentry <- None;
1089 Glut.postRedisplay ()
1091 | TEswitch te ->
1092 state.textentry <- Some te;
1093 Glut.postRedisplay ()
1094 end;
1095 end;
1098 let narrow outlines pattern =
1099 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1100 match reopt with
1101 | None -> None
1102 | Some re ->
1103 let rec fold accu n =
1104 if n = -1 then accu else
1105 let (s, _, _, _) as o = outlines.(n) in
1106 let accu =
1107 if (try ignore (Str.search_forward re s 0); true
1108 with Not_found -> false)
1109 then (o :: accu)
1110 else accu
1112 fold accu (n-1)
1114 let matched = fold [] (Array.length outlines - 1) in
1115 if matched = [] then None else Some (Array.of_list matched)
1118 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1119 let search active pattern incr =
1120 let dosearch re =
1121 let rec loop n =
1122 if n = Array.length outlines || n = -1 then None else
1123 let (s, _, _, _) = outlines.(n) in
1125 (try ignore (Str.search_forward re s 0); true
1126 with Not_found -> false)
1127 then Some n
1128 else loop (n + incr)
1130 loop active
1133 let re = Str.regexp_case_fold pattern in
1134 dosearch re
1135 with Failure s ->
1136 state.text <- s;
1137 None
1139 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1140 match key with
1141 | 27 ->
1142 if String.length qsearch = 0
1143 then (
1144 state.text <- "";
1145 state.outline <- None;
1146 Glut.postRedisplay ();
1148 else (
1149 state.text <- "";
1150 state.outline <- Some (allowdel, active, first, outlines, "");
1151 Glut.postRedisplay ();
1154 | 18 | 19 ->
1155 let incr = if key = 18 then -1 else 1 in
1156 let active, first =
1157 match search (active + incr) qsearch incr with
1158 | None ->
1159 state.text <- qsearch ^ " [not found]";
1160 active, first
1161 | Some active ->
1162 state.text <- qsearch;
1163 active, firstof active
1165 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1166 Glut.postRedisplay ();
1168 | 8 ->
1169 let len = String.length qsearch in
1170 if len = 0
1171 then ()
1172 else (
1173 if len = 1
1174 then (
1175 state.text <- "";
1176 state.outline <- Some (allowdel, active, first, outlines, "");
1178 else
1179 let qsearch = String.sub qsearch 0 (len - 1) in
1180 let active, first =
1181 match search active qsearch ~-1 with
1182 | None ->
1183 state.text <- qsearch ^ " [not found]";
1184 active, first
1185 | Some active ->
1186 state.text <- qsearch;
1187 active, firstof active
1189 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1191 Glut.postRedisplay ()
1193 | 13 ->
1194 if active < Array.length outlines
1195 then (
1196 let (_, _, n, t) = outlines.(active) in
1197 gotopage n t;
1199 state.text <- "";
1200 if allowdel then state.bookmarks <- Array.to_list outlines;
1201 state.outline <- None;
1202 Glut.postRedisplay ();
1204 | _ when key >= 32 && key < 127 ->
1205 let pattern = addchar qsearch (Char.chr key) in
1206 let active, first =
1207 match search active pattern 1 with
1208 | None ->
1209 state.text <- pattern ^ " [not found]";
1210 active, first
1211 | Some active ->
1212 state.text <- pattern;
1213 active, firstof active
1215 state.outline <- Some (allowdel, active, first, outlines, pattern);
1216 Glut.postRedisplay ()
1218 | 14 when not allowdel ->
1219 let optoutlines = narrow outlines qsearch in
1220 begin match optoutlines with
1221 | None -> state.text <- "can't narrow"
1222 | Some outlines ->
1223 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1224 match state.outlines with
1225 | Olist l -> ()
1226 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1227 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1228 end;
1229 Glut.postRedisplay ()
1231 | 21 when not allowdel ->
1232 let outline =
1233 match state.outlines with
1234 | Oarray a -> a
1235 | Olist l ->
1236 let a = Array.of_list (List.rev l) in
1237 state.outlines <- Oarray a;
1239 | Onarrow (a, b) ->
1240 state.outlines <- Oarray b;
1243 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1244 Glut.postRedisplay ()
1246 | 12 ->
1247 state.outline <-
1248 Some (allowdel, active, firstof active, outlines, qsearch);
1249 Glut.postRedisplay ()
1251 | 127 when allowdel ->
1252 let len = Array.length outlines - 1 in
1253 if len = 0
1254 then (
1255 state.outline <- None;
1256 state.bookmarks <- [];
1258 else (
1259 let bookmarks = Array.init len
1260 (fun i ->
1261 let i = if i >= active then i + 1 else i in
1262 outlines.(i)
1265 state.outline <-
1266 Some (allowdel,
1267 min active (len-1),
1268 min first (len-1),
1269 bookmarks, qsearch)
1272 Glut.postRedisplay ()
1274 | _ -> log "unknown key %d" key
1277 let keyboard ~key ~x ~y =
1278 if key = 7
1279 then
1280 wcmd "interrupt" []
1281 else
1282 match state.outline with
1283 | None -> viewkeyboard ~key ~x ~y
1284 | Some outline -> outlinekeyboard ~key ~x ~y outline
1287 let special ~key ~x ~y =
1288 match state.outline with
1289 | None ->
1290 begin match state.textentry with
1291 | None ->
1292 let y =
1293 match key with
1294 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1295 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1296 | Glut.KEY_DOWN -> clamp conf.scrollincr
1297 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1298 | Glut.KEY_PAGE_DOWN -> clamp state.h
1299 | Glut.KEY_HOME -> addnav (); 0
1300 | Glut.KEY_END ->
1301 addnav ();
1302 state.maxy - (if conf.maxhfit then state.h else 0)
1303 | _ -> state.y
1305 state.text <- "";
1306 gotoy y
1308 | Some (c, s, Some onhist, onkey, ondone) ->
1309 let s =
1310 match key with
1311 | Glut.KEY_UP -> onhist HCprev
1312 | Glut.KEY_DOWN -> onhist HCnext
1313 | Glut.KEY_HOME -> onhist HCfirst
1314 | Glut.KEY_END -> onhist HClast
1315 | _ -> state.text
1317 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1318 Glut.postRedisplay ()
1320 | _ -> ()
1323 | Some (allowdel, active, first, outlines, qsearch) ->
1324 let maxrows = maxoutlinerows () in
1325 let navigate incr =
1326 let active = active + incr in
1327 let active = max 0 (min active (Array.length outlines - 1)) in
1328 let first =
1329 if active > first
1330 then
1331 let rows = active - first in
1332 if rows > maxrows then active - maxrows else first
1333 else active
1335 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1336 Glut.postRedisplay ()
1338 match key with
1339 | Glut.KEY_UP -> navigate ~-1
1340 | Glut.KEY_DOWN -> navigate 1
1341 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1342 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1344 | Glut.KEY_HOME ->
1345 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1346 Glut.postRedisplay ()
1348 | Glut.KEY_END ->
1349 let active = Array.length outlines - 1 in
1350 let first = max 0 (active - maxrows) in
1351 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1352 Glut.postRedisplay ()
1354 | _ -> ()
1357 let drawplaceholder l =
1358 GlDraw.color (1.0, 1.0, 1.0);
1359 GlDraw.rect
1360 (0.0, float l.pagedispy)
1361 (float l.pagew, float (l.pagedispy + l.pagevh))
1363 let x = 0.0
1364 and y = float (l.pagedispy + 13) in
1365 let font = Glut.BITMAP_8_BY_13 in
1366 GlDraw.color (0.0, 0.0, 0.0);
1367 GlPix.raster_pos ~x ~y ();
1368 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1369 ("Loading " ^ string_of_int l.pageno);
1372 let now () = Unix.gettimeofday ();;
1374 let drawpage i l =
1375 begin match getopaque l.pageno with
1376 | Some opaque when validopaque opaque ->
1377 if state.textentry = None
1378 then GlDraw.color (1.0, 1.0, 1.0)
1379 else GlDraw.color (0.4, 0.4, 0.4);
1380 let a = now () in
1381 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1382 let b = now () in
1383 let d = b-.a in
1384 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1385 vlog "draw %f sec" d;
1387 | Some _ ->
1388 drawplaceholder l
1390 | None ->
1391 drawplaceholder l;
1392 if state.inflight < cblen state.pagecache
1393 then (
1394 List.iter preload state.layout;
1396 else (
1397 vlog "inflight %d" state.inflight;
1399 end;
1400 GlDraw.color (0.5, 0.5, 0.5);
1401 GlDraw.rect
1402 (0., float i)
1403 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1405 l.pagedispy + l.pagevh;
1408 let scrollindicator () =
1409 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1410 GlDraw.color (0.64 , 0.64, 0.64);
1411 GlDraw.rect
1412 (float (state.w - conf.scrollw), 0.)
1413 (float state.w, float state.h)
1415 GlDraw.color (0.0, 0.0, 0.0);
1416 let sh = (float (maxy + state.h) /. float state.h) in
1417 let sh = float state.h /. sh in
1418 let sh = max sh (float conf.scrollh) in
1420 let percent =
1421 if state.y = state.maxy
1422 then 1.0
1423 else float state.y /. float maxy
1425 let position = (float state.h -. sh) *. percent in
1427 let position =
1428 if position +. sh > float state.h
1429 then
1430 float state.h -. sh
1431 else
1432 position
1434 GlDraw.rect
1435 (float (state.w - conf.scrollw), position)
1436 (float state.w, position +. sh)
1440 let showsel () =
1441 match state.mstate with
1442 | Mnone ->
1445 | Msel ((x0, y0), (x1, y1)) ->
1446 let f l =
1447 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1448 || ((y1 >= l.pagedispy))
1449 then
1450 match getopaque l.pageno with
1451 | Some opaque when validopaque opaque ->
1452 let oy = -l.pagey + l.pagedispy in
1453 seltext opaque (x0, y0, x1, y1) oy
1454 | _ -> ()
1456 List.iter f state.layout
1459 let showrects () =
1460 Gl.enable `blend;
1461 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1462 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1463 List.iter
1464 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1465 List.iter (fun l ->
1466 if l.pageno = pageno
1467 then (
1468 let d = float (l.pagedispy - l.pagey) in
1469 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1470 GlDraw.begins `quads;
1472 GlDraw.vertex2 (x0, y0+.d);
1473 GlDraw.vertex2 (x1, y1+.d);
1474 GlDraw.vertex2 (x2, y2+.d);
1475 GlDraw.vertex2 (x3, y3+.d);
1477 GlDraw.ends ();
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 | LNone -> f rest
1552 | link -> link
1553 else
1554 f rest
1555 | _ ->
1556 f rest
1558 | [] -> LNone
1560 f state.layout
1563 let mouse ~button ~bstate ~x ~y =
1564 match button with
1565 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1566 let incr =
1567 if n = 3
1568 then
1569 -conf.scrollincr
1570 else
1571 conf.scrollincr
1573 let incr = incr * 2 in
1574 let y = clamp incr in
1575 gotoy y
1577 | Glut.LEFT_BUTTON when state.outline = None ->
1578 let dest = if bstate = Glut.DOWN then getlink x y else LNone in
1579 begin match dest with
1580 | LGoto (pageno, top) ->
1581 if pageno >= 0
1582 then
1583 gotopage pageno top
1585 | LUri s ->
1586 print_endline s
1588 | LNone ->
1589 if bstate = Glut.DOWN
1590 then (
1591 if state.rotate mod 360 = 0 then (
1592 Glut.setCursor Glut.CURSOR_TEXT;
1593 state.mstate <- Msel ((x, y), (x, y));
1594 Glut.postRedisplay ()
1597 else (
1598 match state.mstate with
1599 | Mnone -> ()
1600 | Msel ((x0, y0), (x1, y1)) ->
1601 let f l =
1602 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1603 || ((y1 >= l.pagedispy))
1604 then
1605 match getopaque l.pageno with
1606 | Some opaque when validopaque opaque ->
1607 copysel opaque
1608 | _ -> ()
1610 List.iter f state.layout;
1611 copysel ""; (* ugly *)
1612 Glut.setCursor Glut.CURSOR_INHERIT;
1613 state.mstate <- Mnone;
1617 | _ ->
1620 let mouse ~button ~state ~x ~y = mouse button state x y;;
1622 let motion ~x ~y =
1623 if state.outline = None
1624 then
1625 match state.mstate with
1626 | Mnone -> ()
1627 | Msel (a, _) ->
1628 state.mstate <- Msel (a, (x, y));
1629 Glut.postRedisplay ()
1632 let pmotion ~x ~y =
1633 if state.outline = None
1634 then
1635 match state.mstate with
1636 | Mnone ->
1637 if getlink x y != LNone
1638 then Glut.setCursor Glut.CURSOR_INFO
1639 else Glut.setCursor Glut.CURSOR_INHERIT
1641 | Msel (a, _) ->
1645 let () =
1646 let statepath =
1647 let home =
1648 if Sys.os_type = "Win32"
1649 then
1650 try Sys.getenv "HOMEPATH" with Not_found -> ""
1651 else
1652 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1654 Filename.concat home "llpp"
1656 let pstate =
1658 let ic = open_in_bin statepath in
1659 let hash = input_value ic in
1660 close_in ic;
1661 hash
1662 with exn ->
1663 if false
1664 then
1665 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1667 Hashtbl.create 1
1669 let savestate () =
1671 let w, h =
1672 match state.fullscreen with
1673 | None -> state.w, state.h
1674 | Some wh -> wh
1676 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1677 let oc = open_out_bin statepath in
1678 output_value oc pstate
1679 with exn ->
1680 if false
1681 then
1682 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1685 let setstate () =
1687 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1688 state.w <- statew;
1689 state.h <- stateh;
1690 state.bookmarks <- statebookmarks;
1691 with Not_found -> ()
1692 | exn ->
1693 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1696 Arg.parse [] (fun s -> state.path <- s) "options:";
1697 let name =
1698 if String.length state.path = 0
1699 then (prerr_endline "filename missing"; exit 1)
1700 else state.path
1703 setstate ();
1704 let _ = Glut.init Sys.argv in
1705 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1706 let () = Glut.initWindowSize state.w state.h in
1707 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1709 let csock, ssock =
1710 if Sys.os_type = "Unix"
1711 then
1712 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1713 else
1714 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1715 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1716 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1717 Unix.bind sock addr;
1718 Unix.listen sock 1;
1719 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1720 Unix.connect csock addr;
1721 let ssock, _ = Unix.accept sock in
1722 Unix.close sock;
1723 let opts sock =
1724 Unix.setsockopt sock Unix.TCP_NODELAY true;
1725 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1727 opts ssock;
1728 opts csock;
1729 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1730 ssock, csock
1733 let () = Glut.displayFunc display in
1734 let () = Glut.reshapeFunc reshape in
1735 let () = Glut.keyboardFunc keyboard in
1736 let () = Glut.specialFunc special in
1737 let () = Glut.idleFunc (Some idle) in
1738 let () = Glut.mouseFunc mouse in
1739 let () = Glut.motionFunc motion in
1740 let () = Glut.passiveMotionFunc pmotion in
1742 init ssock;
1743 state.csock <- csock;
1744 state.ssock <- ssock;
1745 writecmd csock ("open " ^ name ^ "\000");
1747 at_exit savestate;
1749 let rec handlelablglutbug () =
1751 Glut.mainLoop ();
1752 with Glut.BadEnum "key in special_of_int" ->
1753 showtext '!' " LablGlut bug: special key not recognized";
1754 Glut.swapBuffers ();
1755 handlelablglutbug ()
1757 handlelablglutbug ();