Reject text selection which spans multiple pages
[llpp.git] / main.ml
blobec38ae0456d800c7d374654d3c6569a21d047d2f
1 type link = | LNone | LUri of string | LGoto of (int * int);;
3 let log fmt = Printf.kprintf prerr_endline fmt;;
4 let dolog fmt = Printf.kprintf prerr_endline fmt;;
6 external init : Unix.file_descr -> unit = "ml_init";;
7 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
8 external seltext : string -> (int * int * int * int) -> int -> unit =
9 "ml_seltext";;
10 external copysel : string -> unit = "ml_copysel";;
11 external getlink : string -> int -> int -> link = "ml_getlink";;
12 external highlightlinks : string -> int -> unit = "ml_highlightlinks";;
13 external getpagewh : int -> float array = "ml_getpagewh";;
15 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
17 type 'a circbuf =
18 { store : 'a array
19 ; mutable rc : int
20 ; mutable wc : int
21 ; mutable len : int
25 type textentry = (char * string * onhist option * onkey * ondone)
26 and onkey = string -> int -> te
27 and ondone = string -> unit
28 and onhist = histcmd -> string
29 and histcmd = HCnext | HCprev | HCfirst | HClast
30 and te =
31 | TEstop
32 | TEdone of string
33 | TEcont of string
34 | TEswitch of textentry
37 let cbnew n v =
38 { store = Array.create n v
39 ; rc = 0
40 ; wc = 0
41 ; len = 0
45 let cblen b = Array.length b.store;;
47 let cbput b v =
48 let len = cblen b in
49 b.store.(b.wc) <- v;
50 b.wc <- (b.wc + 1) mod len;
51 b.len <- min (b.len + 1) len;
54 let cbpeekw b = b.store.(b.wc);;
56 let cbget b dir =
57 if b.len = 0 then b.store.(0) else
58 let rc = b.rc + dir in
59 let rc = if rc = -1 then b.len - 1 else rc in
60 let rc = if rc = b.len then 0 else rc in
61 b.rc <- rc;
62 b.store.(rc);
65 let cbrfollowlen b =
66 b.rc <- b.len;
69 type layout =
70 { pageno : int
71 ; pagedimno : int
72 ; pagew : int
73 ; pageh : int
74 ; pagedispy : int
75 ; pagey : int
76 ; pagevh : int
80 type conf =
81 { mutable scrollw : int
82 ; mutable scrollh : int
83 ; mutable icase : bool
84 ; mutable preload : bool
85 ; mutable pagebias : int
86 ; mutable verbose : bool
87 ; mutable scrollincr : int
88 ; mutable maxhfit : bool
89 ; mutable crophack : bool
90 ; mutable autoscroll : bool
91 ; mutable showall : bool
92 ; mutable hlinks : bool
96 type outline = string * int * int * int;;
97 type outlines =
98 | Oarray of outline array
99 | Olist of outline list
100 | Onarrow of outline array * outline array
103 type rect = (float * float * float * float * float * float * float * float);;
105 type state =
106 { mutable csock : Unix.file_descr
107 ; mutable ssock : Unix.file_descr
108 ; mutable w : int
109 ; mutable h : int
110 ; mutable rotate : int
111 ; mutable y : int
112 ; mutable ty : float
113 ; mutable maxy : int
114 ; mutable layout : layout list
115 ; pagemap : ((int * int * int), string) Hashtbl.t
116 ; mutable pages : (int * int * int) list
117 ; mutable pagecount : int
118 ; pagecache : string circbuf
119 ; mutable rendering : bool
120 ; mutable mstate : mstate
121 ; mutable searchpattern : string
122 ; mutable rects : (int * int * rect) list
123 ; mutable rects1 : (int * int * rect) list
124 ; mutable text : string
125 ; mutable fullscreen : (int * int) option
126 ; mutable textentry : textentry option
127 ; mutable outlines : outlines
128 ; mutable outline : (bool * int * int * outline array * string) option
129 ; mutable bookmarks : outline list
130 ; mutable path : string
131 ; mutable invalidated : int
132 ; mutable colorscale : float
133 ; hists : hists
135 and hists =
136 { pat : string circbuf
137 ; pag : string circbuf
138 ; nav : float circbuf
142 let conf =
143 { scrollw = 5
144 ; scrollh = 12
145 ; icase = true
146 ; preload = false
147 ; pagebias = 0
148 ; verbose = false
149 ; scrollincr = 24
150 ; maxhfit = true
151 ; crophack = false
152 ; autoscroll = false
153 ; showall = false
154 ; hlinks = false
158 let state =
159 { csock = Unix.stdin
160 ; ssock = Unix.stdin
161 ; w = 900
162 ; h = 900
163 ; rotate = 0
164 ; y = 0
165 ; ty = 0.0
166 ; layout = []
167 ; maxy = max_int
168 ; pagemap = Hashtbl.create 10
169 ; pagecache = cbnew 10 ""
170 ; pages = []
171 ; pagecount = 0
172 ; rendering = false
173 ; mstate = Mnone
174 ; rects = []
175 ; rects1 = []
176 ; text = ""
177 ; fullscreen = None
178 ; textentry = None
179 ; searchpattern = ""
180 ; outlines = Olist []
181 ; outline = None
182 ; bookmarks = []
183 ; path = ""
184 ; invalidated = 0
185 ; hists =
186 { nav = cbnew 100 0.0
187 ; pat = cbnew 20 ""
188 ; pag = cbnew 10 ""
190 ; colorscale = 1.0
194 let vlog fmt =
195 if conf.verbose
196 then
197 Printf.kprintf prerr_endline fmt
198 else
199 Printf.kprintf ignore fmt
202 let writecmd fd s =
203 let len = String.length s in
204 let n = 4 + len in
205 let b = Buffer.create n in
206 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
207 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
208 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
209 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
210 Buffer.add_string b s;
211 let s' = Buffer.contents b in
212 let n' = Unix.write fd s' 0 n in
213 if n' != n then failwith "write failed";
216 let readcmd fd =
217 let s = "xxxx" in
218 let n = Unix.read fd s 0 4 in
219 if n != 4 then failwith "incomplete read(len)";
220 let len = 0
221 lor (Char.code s.[0] lsl 24)
222 lor (Char.code s.[1] lsl 16)
223 lor (Char.code s.[2] lsl 8)
224 lor (Char.code s.[3] lsl 0)
226 let s = String.create len in
227 let n = Unix.read fd s 0 len in
228 if n != len then failwith "incomplete read(data)";
232 let yratio y =
233 if y = state.maxy then 1.0
234 else float y /. float state.maxy
237 let makecmd s l =
238 let b = Buffer.create 10 in
239 Buffer.add_string b s;
240 let rec combine = function
241 | [] -> b
242 | x :: xs ->
243 Buffer.add_char b ' ';
244 let s =
245 match x with
246 | `b b -> if b then "1" else "0"
247 | `s s -> s
248 | `i i -> string_of_int i
249 | `f f -> string_of_float f
250 | `I f -> string_of_int (truncate f)
252 Buffer.add_string b s;
253 combine xs;
255 combine l;
258 let wcmd s l =
259 let cmd = Buffer.contents (makecmd s l) in
260 writecmd state.csock cmd;
263 let calcheight () =
264 let rec f pn ph fh l =
265 match l with
266 | (n, _, h) :: rest ->
267 let fh = fh + (n - pn) * ph in
268 f n h fh rest
270 | [] ->
271 let fh = fh + (ph * (state.pagecount - pn)) in
272 max 0 fh
274 let fh = f 0 0 0 state.pages in
278 let getpagey pageno =
279 let rec f pn ph y l =
280 match l with
281 | (n, _, h) :: rest ->
282 if n >= pageno
283 then
284 y + (pageno - pn) * ph
285 else
286 let y = y + (n - pn) * ph in
287 f n h y rest
289 | [] ->
290 y + (pageno - pn) * ph
292 f 0 0 0 state.pages;
295 let layout y sh =
296 let rec f pageno pdimno prev vy py dy l cacheleft accu =
297 if pageno = state.pagecount || cacheleft = 0
298 then accu
299 else
300 let ((_, w, h) as curr), rest, pdimno =
301 match l with
302 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
303 curr, rest, pdimno + 1
304 | _ ->
305 prev, l, pdimno
307 let pageno' = pageno + 1 in
308 if py + h > vy
309 then
310 let py' = vy - py in
311 let vh = h - py' in
312 if dy + vh > sh
313 then
314 let vh = sh - dy in
315 if vh <= 0
316 then
317 accu
318 else
319 let e =
320 { pageno = pageno
321 ; pagedimno = pdimno
322 ; pagew = w
323 ; pageh = h
324 ; pagedispy = dy
325 ; pagey = py'
326 ; pagevh = vh
329 e :: accu
330 else
331 let e =
332 { pageno = pageno
333 ; pagedimno = pdimno
334 ; pagew = w
335 ; pageh = h
336 ; pagedispy = dy
337 ; pagey = py'
338 ; pagevh = vh
341 let accu = e :: accu in
342 f pageno' pdimno curr
343 (vy + vh) (py + h) (dy + vh + 2) rest
344 (pred cacheleft) accu
345 else
346 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
348 if state.invalidated = 0
349 then
350 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
351 state.maxy <- calcheight ();
352 List.rev accu
353 else
357 let clamp incr =
358 let y = state.y + incr in
359 let y = max 0 y in
360 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
364 let getopaque pageno =
365 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
366 state.rotate))
367 with Not_found -> None
370 let cache pageno opaque =
371 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
372 state.rotate) opaque
375 let validopaque opaque = String.length opaque > 0;;
377 let render l =
378 match getopaque l.pageno with
379 | None when not state.rendering ->
380 state.rendering <- true;
381 cache l.pageno "";
382 wcmd "render" [`i (l.pageno + 1)
383 ;`i l.pagedimno
384 ;`i l.pagew
385 ;`i l.pageh];
387 | _ -> ()
390 let loadlayout layout =
391 let rec f all = function
392 | l :: ls ->
393 begin match getopaque l.pageno with
394 | None -> render l; f false ls
395 | Some opaque -> f (all && validopaque opaque) ls
397 | [] -> all
399 f (layout <> []) layout;
402 let gotoy y =
403 let y = max 0 y in
404 let y = min state.maxy y in
405 let pages = layout y state.h in
406 let ready = loadlayout pages in
407 state.ty <- yratio y;
408 if conf.showall then (
409 if ready then (
410 state.layout <- pages;
411 state.y <- y;
412 Glut.postRedisplay ();
415 else (
416 state.layout <- pages;
417 state.y <- y;
418 Glut.postRedisplay ();
420 if conf.preload then begin
421 let y = if state.y < state.h then 0 else state.y - state.h in
422 let pages = layout y (state.h*3) in
423 List.iter render pages;
424 end;
427 let addnav () =
428 cbput state.hists.nav (yratio state.y);
429 cbrfollowlen state.hists.nav;
432 let getnav () =
433 let y = cbget state.hists.nav ~-1 in
434 truncate (y *. float state.maxy)
437 let gotopage n top =
438 let y = getpagey n in
439 addnav ();
440 gotoy (y + top);
443 let invalidate () =
444 state.layout <- [];
445 state.pages <- [];
446 state.rects <- [];
447 state.rects1 <- [];
448 state.invalidated <- state.invalidated + 1;
451 let scalecolor c =
452 let c = c *. state.colorscale in
453 (c, c, c);
456 let reshape ~w ~h =
457 let ratio = float w /. float state.w in
458 let fixbookmark (s, l, pageno, pagey) =
459 let pagey = truncate (float pagey *. ratio) in
460 (s, l, pageno, pagey)
462 state.bookmarks <- List.map fixbookmark state.bookmarks;
463 state.w <- w;
464 state.h <- h;
465 GlDraw.viewport 0 0 w h;
466 GlMat.mode `modelview;
467 GlMat.load_identity ();
468 GlMat.mode `projection;
469 GlMat.load_identity ();
470 GlMat.rotate ~x:1.0 ~angle:180.0 ();
471 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
472 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
473 GlClear.color (scalecolor 1.0);
474 GlClear.clear [`color];
476 invalidate ();
477 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
480 let showtext c s =
481 GlDraw.color (0.0, 0.0, 0.0);
482 GlDraw.rect
483 (0.0, float (state.h - 18))
484 (float (state.w - conf.scrollw - 1), float state.h)
486 let font = Glut.BITMAP_8_BY_13 in
487 GlDraw.color (1.0, 1.0, 1.0);
488 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
489 Glut.bitmapCharacter ~font ~c:(Char.code c);
490 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
493 let enttext () =
494 let len = String.length state.text in
495 match state.textentry with
496 | None ->
497 if len > 0 then showtext ' ' state.text
499 | Some (c, text, _, _, _) ->
500 let s =
501 if len > 0
502 then
503 text ^ " [" ^ state.text ^ "]"
504 else
505 text
507 showtext c s;
510 let showtext c s =
511 if true
512 then (
513 state.text <- Printf.sprintf "%c%s" c s;
514 Glut.postRedisplay ();
516 else (
517 showtext c s;
518 Glut.swapBuffers ();
523 let act cmd =
524 match cmd.[0] with
525 | 'c' ->
526 state.pages <- [];
527 state.outlines <- Olist []
529 | 'D' ->
530 state.rects <- state.rects1;
531 Glut.postRedisplay ()
533 | 'C' ->
534 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
535 state.pagecount <- n;
536 state.invalidated <- state.invalidated - 1;
537 if state.invalidated = 0
538 then (
539 let rely = yratio state.y in
540 state.maxy <- calcheight ();
541 gotoy (truncate (float state.maxy *. rely));
544 | 't' ->
545 let s = Scanf.sscanf cmd "t %n"
546 (fun n -> String.sub cmd n (String.length cmd - n))
548 Glut.setWindowTitle s
550 | 'T' ->
551 let s = Scanf.sscanf cmd "T %n"
552 (fun n -> String.sub cmd n (String.length cmd - n))
554 if state.textentry = None
555 then (
556 state.text <- s;
557 showtext ' ' s;
559 else (
560 state.text <- s;
561 Glut.postRedisplay ();
564 | 'V' ->
565 if conf.verbose
566 then
567 let s = Scanf.sscanf cmd "V %n"
568 (fun n -> String.sub cmd n (String.length cmd - n))
570 state.text <- s;
571 showtext ' ' s;
573 | 'F' ->
574 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
575 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
576 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
577 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
579 let y = (getpagey pageno) + truncate y0 in
580 addnav ();
581 gotoy y;
582 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
584 | 'R' ->
585 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
586 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
587 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
588 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
590 state.rects1 <-
591 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
593 | 'r' ->
594 let n, w, h, r, p =
595 Scanf.sscanf cmd "r %d %d %d %d %s"
596 (fun n w h r p -> (n, w, h, r, p))
598 Hashtbl.replace state.pagemap (n, w, r) p;
599 let opaque = cbpeekw state.pagecache in
600 if validopaque opaque
601 then (
602 let k =
603 Hashtbl.fold
604 (fun k v a -> if v = opaque then k else a)
605 state.pagemap (-1, -1, -1)
607 wcmd "free" [`s opaque];
608 Hashtbl.remove state.pagemap k
610 cbput state.pagecache p;
611 state.rendering <- false;
612 if conf.showall
613 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
614 else gotoy state.y
616 | 'l' ->
617 let (n, w, h) as pagelayout =
618 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
620 state.pages <- pagelayout :: state.pages
622 | 'o' ->
623 let (l, n, t, pos) =
624 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
626 let s = String.sub cmd pos (String.length cmd - pos) in
627 let outline = (s, l, n, t) in
628 let outlines =
629 match state.outlines with
630 | Olist outlines -> Olist (outline :: outlines)
631 | Oarray _ -> Olist [outline]
632 | Onarrow _ -> Olist [outline]
634 state.outlines <- outlines
636 | _ ->
637 log "unknown cmd `%S'" cmd
640 let now = Unix.gettimeofday;;
642 let idle () =
643 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
644 begin match r with
645 | [] ->
646 if conf.autoscroll then begin
647 let y = state.y + conf.scrollincr in
648 let y = if y >= state.maxy then 0 else y in
649 gotoy y;
650 state.text <- "";
651 end;
653 | _ ->
654 let cmd = readcmd state.csock in
655 act cmd;
656 end;
659 let onhist cb = function
660 | HCprev -> cbget cb ~-1
661 | HCnext -> cbget cb 1
662 | HCfirst -> cbget cb ~-(cb.rc)
663 | HClast -> cbget cb (cb.len - 1 - cb.rc)
666 let search pattern forward =
667 if String.length pattern > 0
668 then
669 let pn, py =
670 match state.layout with
671 | [] -> 0, 0
672 | l :: _ ->
673 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
675 let cmd =
676 let b = makecmd "search"
677 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
679 Buffer.add_char b ',';
680 Buffer.add_string b pattern;
681 Buffer.add_char b '\000';
682 Buffer.contents b;
684 writecmd state.csock cmd;
687 let intentry text key =
688 let c = Char.unsafe_chr key in
689 match c with
690 | '0' .. '9' ->
691 let s = "x" in s.[0] <- c;
692 let text = text ^ s in
693 TEcont text
695 | _ ->
696 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
697 TEcont text
700 let addchar s c =
701 let b = Buffer.create (String.length s + 1) in
702 Buffer.add_string b s;
703 Buffer.add_char b c;
704 Buffer.contents b;
707 let textentry text key =
708 let c = Char.unsafe_chr key in
709 match c with
710 | _ when key >= 32 && key < 127 ->
711 let text = addchar text c in
712 TEcont text
714 | _ ->
715 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
716 TEcont text
719 let rotate angle =
720 state.rotate <- angle;
721 invalidate ();
722 wcmd "rotate" [`i angle];
725 let optentry text key =
726 let btos b = if b then "on" else "off" in
727 let c = Char.unsafe_chr key in
728 match c with
729 | 's' ->
730 let ondone s =
731 try conf.scrollincr <- int_of_string s with exc ->
732 state.text <- Printf.sprintf "bad integer `%s': %s"
733 s (Printexc.to_string exc)
735 TEswitch ('#', "", None, intentry, ondone)
737 | 'R' ->
738 let ondone s =
739 match try
740 Some (int_of_string s)
741 with exc ->
742 state.text <- Printf.sprintf "bad integer `%s': %s"
743 s (Printexc.to_string exc);
744 None
745 with
746 | Some angle -> rotate angle
747 | None -> ()
749 TEswitch ('^', "", None, intentry, ondone)
751 | 'i' ->
752 conf.icase <- not conf.icase;
753 TEdone ("case insensitive search " ^ (btos conf.icase))
755 | 'p' ->
756 conf.preload <- not conf.preload;
757 gotoy state.y;
758 TEdone ("preload " ^ (btos conf.preload))
760 | 'v' ->
761 conf.verbose <- not conf.verbose;
762 TEdone ("verbose " ^ (btos conf.verbose))
764 | 'h' ->
765 conf.maxhfit <- not conf.maxhfit;
766 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
767 TEdone ("maxhfit " ^ (btos conf.maxhfit))
769 | 'c' ->
770 conf.crophack <- not conf.crophack;
771 TEdone ("crophack " ^ btos conf.crophack)
773 | 'a' ->
774 conf.showall <- not conf.showall;
775 TEdone ("showall " ^ btos conf.showall)
777 | _ ->
778 state.text <- Printf.sprintf "bad option %d `%c'" key c;
779 TEstop
782 let maxoutlinerows () = (state.h - 31) / 16;;
784 let enterselector allowdel outlines errmsg =
785 if Array.length outlines = 0
786 then (
787 showtext ' ' errmsg;
789 else
790 let pageno =
791 match state.layout with
792 | [] -> -1
793 | {pageno=pageno} :: rest -> pageno
795 let active =
796 let rec loop n =
797 if n = Array.length outlines
798 then 0
799 else
800 let (_, _, outlinepageno, _) = outlines.(n) in
801 if outlinepageno >= pageno then n else loop (n+1)
803 loop 0
805 state.outline <-
806 Some (allowdel, active,
807 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
808 Glut.postRedisplay ();
811 let enteroutlinemode () =
812 let outlines =
813 match state.outlines with
814 | Oarray a -> a
815 | Olist l ->
816 let a = Array.of_list (List.rev l) in
817 state.outlines <- Oarray a;
819 | Onarrow (a, b) -> a
821 enterselector false outlines "Document has no outline";
824 let enterbookmarkmode () =
825 let bookmarks = Array.of_list state.bookmarks in
826 enterselector true bookmarks "Document has no bookmarks (yet)";
830 let quickbookmark ?title () =
831 match state.layout with
832 | [] -> ()
833 | l :: _ ->
834 let title =
835 match title with
836 | None ->
837 let sec = Unix.gettimeofday () in
838 let tm = Unix.localtime sec in
839 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
840 l.pageno
841 tm.Unix.tm_mday
842 tm.Unix.tm_mon
843 (tm.Unix.tm_year + 1900)
844 tm.Unix.tm_hour
845 tm.Unix.tm_min
846 | Some title -> title
848 state.bookmarks <-
849 (title, 0, l.pageno, l.pagey) :: state.bookmarks
852 let viewkeyboard ~key ~x ~y =
853 let enttext te =
854 state.textentry <- te;
855 state.text <- "";
856 enttext ();
857 Glut.postRedisplay ()
859 match state.textentry with
860 | None ->
861 let c = Char.chr key in
862 begin match c with
863 | '\027' | 'q' ->
864 exit 0
866 | '\008' ->
867 let y = getnav () in
868 gotoy y
870 | 'o' ->
871 enteroutlinemode ()
873 | 'u' ->
874 state.rects <- [];
875 state.text <- "";
876 Glut.postRedisplay ()
878 | '/' | '?' ->
879 let ondone isforw s =
880 cbput state.hists.pat s;
881 cbrfollowlen state.hists.pat;
882 state.searchpattern <- s;
883 search s isforw
885 enttext (Some (c, "", Some (onhist state.hists.pat),
886 textentry, ondone (c ='/')))
888 | '+' ->
889 let ondone s =
890 let n =
891 try int_of_string s with exc ->
892 state.text <- Printf.sprintf "bad integer `%s': %s"
893 s (Printexc.to_string exc);
894 max_int
896 if n != max_int
897 then (
898 conf.pagebias <- n;
899 state.text <- "page bias is now " ^ string_of_int n;
902 enttext (Some ('+', "", None, intentry, ondone))
904 | '-' ->
905 let ondone msg =
906 state.text <- msg;
908 enttext (Some ('-', "", None, optentry, ondone))
910 | '0' .. '9' ->
911 let ondone s =
912 let n =
913 try int_of_string s with exc ->
914 state.text <- Printf.sprintf "bad integer `%s': %s"
915 s (Printexc.to_string exc);
918 if n >= 0
919 then (
920 addnav ();
921 cbput state.hists.pag (string_of_int n);
922 cbrfollowlen state.hists.pag;
923 gotoy (getpagey (n + conf.pagebias - 1))
926 let pageentry text key =
927 match Char.unsafe_chr key with
928 | 'g' -> TEdone text
929 | _ -> intentry text key
931 let text = "x" in text.[0] <- c;
932 enttext (Some (':', text, Some (onhist state.hists.pag),
933 pageentry, ondone))
935 | 'b' ->
936 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
937 reshape state.w state.h;
939 | 'l' ->
940 conf.hlinks <- not conf.hlinks;
941 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
942 Glut.postRedisplay ()
944 | 'a' ->
945 conf.autoscroll <- not conf.autoscroll
947 | 'f' ->
948 begin match state.fullscreen with
949 | None ->
950 state.fullscreen <- Some (state.w, state.h);
951 Glut.fullScreen ()
952 | Some (w, h) ->
953 state.fullscreen <- None;
954 Glut.reshapeWindow ~w ~h
957 | 'g' ->
958 gotoy 0
960 | 'n' ->
961 search state.searchpattern true
963 | 'p' | 'N' ->
964 search state.searchpattern false
966 | 't' ->
967 begin match state.layout with
968 | [] -> ()
969 | l :: _ ->
970 gotoy (state.y - l.pagey);
973 | ' ' ->
974 begin match List.rev state.layout with
975 | [] -> ()
976 | l :: _ ->
977 gotoy (clamp (l.pageh - l.pagey))
980 | '\127' ->
981 begin match state.layout with
982 | [] -> ()
983 | l :: _ ->
984 gotoy (clamp (-l.pageh));
987 | '=' ->
988 let f (fn, ln) l =
989 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
991 let fn, ln = List.fold_left f (-1, -1) state.layout in
992 let s =
993 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
994 let percent =
995 if maxy <= 0
996 then 100.
997 else (100. *. (float state.y /. float maxy)) in
998 if fn = ln
999 then
1000 Printf.sprintf "Page %d of %d %.2f%%"
1001 (fn+1) state.pagecount percent
1002 else
1003 Printf.sprintf
1004 "Pages %d-%d of %d %.2f%%"
1005 (fn+1) (ln+1) state.pagecount percent
1007 showtext ' ' s;
1009 | 'w' ->
1010 begin match state.layout with
1011 | [] -> ()
1012 | l :: _ ->
1013 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1014 Glut.postRedisplay ();
1017 | '\'' ->
1018 enterbookmarkmode ()
1020 | 'm' ->
1021 let ondone s =
1022 match state.layout with
1023 | l :: _ ->
1024 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1025 | _ -> ()
1027 enttext (Some ('~', "", None, textentry, ondone))
1029 | '~' ->
1030 quickbookmark ();
1031 showtext ' ' "Quick bookmark added";
1033 | 'z' ->
1034 begin match state.layout with
1035 | l :: _ ->
1036 let a = getpagewh l.pagedimno in
1037 let w, h =
1038 if conf.crophack
1039 then
1040 (truncate (1.8 *. (a.(1) -. a.(0))),
1041 truncate (1.2 *. (a.(3) -. a.(0))))
1042 else
1043 (truncate (a.(1) -. a.(0)),
1044 truncate (a.(3) -. a.(0)))
1046 Glut.reshapeWindow (w + conf.scrollw) h;
1047 Glut.postRedisplay ();
1049 | [] -> ()
1052 | '<' | '>' ->
1053 rotate (state.rotate + (if c = '>' then 30 else -30));
1055 | '[' | ']' ->
1056 state.colorscale <-
1057 max 0.0
1058 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1059 Glut.postRedisplay ()
1061 | _ ->
1062 vlog "huh? %d %c" key (Char.chr key);
1065 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1066 let len = String.length text in
1067 if len = 0
1068 then (
1069 state.textentry <- None;
1070 Glut.postRedisplay ();
1072 else (
1073 let s = String.sub text 0 (len - 1) in
1074 enttext (Some (c, s, onhist, onkey, ondone))
1077 | Some (c, text, onhist, onkey, ondone) ->
1078 begin match Char.unsafe_chr key with
1079 | '\r' | '\n' ->
1080 ondone text;
1081 state.textentry <- None;
1082 Glut.postRedisplay ()
1084 | '\027' ->
1085 state.textentry <- None;
1086 Glut.postRedisplay ()
1088 | _ ->
1089 begin match onkey text key with
1090 | TEdone text ->
1091 state.textentry <- None;
1092 ondone text;
1093 Glut.postRedisplay ()
1095 | TEcont text ->
1096 enttext (Some (c, text, onhist, onkey, ondone));
1098 | TEstop ->
1099 state.textentry <- None;
1100 Glut.postRedisplay ()
1102 | TEswitch te ->
1103 state.textentry <- Some te;
1104 Glut.postRedisplay ()
1105 end;
1106 end;
1109 let narrow outlines pattern =
1110 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1111 match reopt with
1112 | None -> None
1113 | Some re ->
1114 let rec fold accu n =
1115 if n = -1 then accu else
1116 let (s, _, _, _) as o = outlines.(n) in
1117 let accu =
1118 if (try ignore (Str.search_forward re s 0); true
1119 with Not_found -> false)
1120 then (o :: accu)
1121 else accu
1123 fold accu (n-1)
1125 let matched = fold [] (Array.length outlines - 1) in
1126 if matched = [] then None else Some (Array.of_list matched)
1129 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1130 let search active pattern incr =
1131 let dosearch re =
1132 let rec loop n =
1133 if n = Array.length outlines || n = -1 then None else
1134 let (s, _, _, _) = outlines.(n) in
1136 (try ignore (Str.search_forward re s 0); true
1137 with Not_found -> false)
1138 then Some n
1139 else loop (n + incr)
1141 loop active
1144 let re = Str.regexp_case_fold pattern in
1145 dosearch re
1146 with Failure s ->
1147 state.text <- s;
1148 None
1150 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1151 match key with
1152 | 27 ->
1153 if String.length qsearch = 0
1154 then (
1155 state.text <- "";
1156 state.outline <- None;
1157 Glut.postRedisplay ();
1159 else (
1160 state.text <- "";
1161 state.outline <- Some (allowdel, active, first, outlines, "");
1162 Glut.postRedisplay ();
1165 | 18 | 19 ->
1166 let incr = if key = 18 then -1 else 1 in
1167 let active, first =
1168 match search (active + incr) qsearch incr with
1169 | None ->
1170 state.text <- qsearch ^ " [not found]";
1171 active, first
1172 | Some active ->
1173 state.text <- qsearch;
1174 active, firstof active
1176 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1177 Glut.postRedisplay ();
1179 | 8 ->
1180 let len = String.length qsearch in
1181 if len = 0
1182 then ()
1183 else (
1184 if len = 1
1185 then (
1186 state.text <- "";
1187 state.outline <- Some (allowdel, active, first, outlines, "");
1189 else
1190 let qsearch = String.sub qsearch 0 (len - 1) in
1191 let active, first =
1192 match search active qsearch ~-1 with
1193 | None ->
1194 state.text <- qsearch ^ " [not found]";
1195 active, first
1196 | Some active ->
1197 state.text <- qsearch;
1198 active, firstof active
1200 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1202 Glut.postRedisplay ()
1204 | 13 ->
1205 if active < Array.length outlines
1206 then (
1207 let (_, _, n, t) = outlines.(active) in
1208 gotopage n t;
1210 state.text <- "";
1211 if allowdel then state.bookmarks <- Array.to_list outlines;
1212 state.outline <- None;
1213 Glut.postRedisplay ();
1215 | _ when key >= 32 && key < 127 ->
1216 let pattern = addchar qsearch (Char.chr key) in
1217 let active, first =
1218 match search active pattern 1 with
1219 | None ->
1220 state.text <- pattern ^ " [not found]";
1221 active, first
1222 | Some active ->
1223 state.text <- pattern;
1224 active, firstof active
1226 state.outline <- Some (allowdel, active, first, outlines, pattern);
1227 Glut.postRedisplay ()
1229 | 14 when not allowdel ->
1230 let optoutlines = narrow outlines qsearch in
1231 begin match optoutlines with
1232 | None -> state.text <- "can't narrow"
1233 | Some outlines ->
1234 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1235 match state.outlines with
1236 | Olist l -> ()
1237 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1238 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1239 end;
1240 Glut.postRedisplay ()
1242 | 21 when not allowdel ->
1243 let outline =
1244 match state.outlines with
1245 | Oarray a -> a
1246 | Olist l ->
1247 let a = Array.of_list (List.rev l) in
1248 state.outlines <- Oarray a;
1250 | Onarrow (a, b) ->
1251 state.outlines <- Oarray b;
1254 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1255 Glut.postRedisplay ()
1257 | 12 ->
1258 state.outline <-
1259 Some (allowdel, active, firstof active, outlines, qsearch);
1260 Glut.postRedisplay ()
1262 | 127 when allowdel ->
1263 let len = Array.length outlines - 1 in
1264 if len = 0
1265 then (
1266 state.outline <- None;
1267 state.bookmarks <- [];
1269 else (
1270 let bookmarks = Array.init len
1271 (fun i ->
1272 let i = if i >= active then i + 1 else i in
1273 outlines.(i)
1276 state.outline <-
1277 Some (allowdel,
1278 min active (len-1),
1279 min first (len-1),
1280 bookmarks, qsearch)
1283 Glut.postRedisplay ()
1285 | _ -> log "unknown key %d" key
1288 let keyboard ~key ~x ~y =
1289 if key = 7
1290 then
1291 wcmd "interrupt" []
1292 else
1293 match state.outline with
1294 | None -> viewkeyboard ~key ~x ~y
1295 | Some outline -> outlinekeyboard ~key ~x ~y outline
1298 let special ~key ~x ~y =
1299 match state.outline with
1300 | None ->
1301 begin match state.textentry with
1302 | None ->
1303 let y =
1304 match key with
1305 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1306 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1307 | Glut.KEY_DOWN -> clamp conf.scrollincr
1308 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1309 | Glut.KEY_PAGE_DOWN -> clamp state.h
1310 | Glut.KEY_HOME -> addnav (); 0
1311 | Glut.KEY_END ->
1312 addnav ();
1313 state.maxy - (if conf.maxhfit then state.h else 0)
1314 | _ -> state.y
1316 state.text <- "";
1317 gotoy y
1319 | Some (c, s, Some onhist, onkey, ondone) ->
1320 let s =
1321 match key with
1322 | Glut.KEY_UP -> onhist HCprev
1323 | Glut.KEY_DOWN -> onhist HCnext
1324 | Glut.KEY_HOME -> onhist HCfirst
1325 | Glut.KEY_END -> onhist HClast
1326 | _ -> state.text
1328 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1329 Glut.postRedisplay ()
1331 | _ -> ()
1334 | Some (allowdel, active, first, outlines, qsearch) ->
1335 let maxrows = maxoutlinerows () in
1336 let navigate incr =
1337 let active = active + incr in
1338 let active = max 0 (min active (Array.length outlines - 1)) in
1339 let first =
1340 if active > first
1341 then
1342 let rows = active - first in
1343 if rows > maxrows then active - maxrows else first
1344 else active
1346 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1347 Glut.postRedisplay ()
1349 match key with
1350 | Glut.KEY_UP -> navigate ~-1
1351 | Glut.KEY_DOWN -> navigate 1
1352 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1353 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1355 | Glut.KEY_HOME ->
1356 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1357 Glut.postRedisplay ()
1359 | Glut.KEY_END ->
1360 let active = Array.length outlines - 1 in
1361 let first = max 0 (active - maxrows) in
1362 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1363 Glut.postRedisplay ()
1365 | _ -> ()
1368 let drawplaceholder l =
1369 GlDraw.color (scalecolor 1.0);
1370 GlDraw.rect
1371 (0.0, float l.pagedispy)
1372 (float l.pagew, float (l.pagedispy + l.pagevh))
1374 let x = 0.0
1375 and y = float (l.pagedispy + 13) in
1376 let font = Glut.BITMAP_8_BY_13 in
1377 GlDraw.color (0.0, 0.0, 0.0);
1378 GlPix.raster_pos ~x ~y ();
1379 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1380 ("Loading " ^ string_of_int l.pageno);
1383 let now () = Unix.gettimeofday ();;
1385 let drawpage i l =
1386 begin match getopaque l.pageno with
1387 | Some opaque when validopaque opaque ->
1388 if state.textentry = None
1389 then GlDraw.color (scalecolor 1.0)
1390 else GlDraw.color (scalecolor 0.4);
1391 let a = now () in
1392 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1393 let b = now () in
1394 let d = b-.a in
1395 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1396 vlog "draw %f sec" d;
1398 | _ ->
1399 drawplaceholder l;
1400 end;
1401 GlDraw.color (0.5, 0.5, 0.5);
1402 GlDraw.rect
1403 (0., float i)
1404 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1406 l.pagedispy + l.pagevh;
1409 let scrollindicator () =
1410 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1411 GlDraw.color (0.64 , 0.64, 0.64);
1412 GlDraw.rect
1413 (float (state.w - conf.scrollw), 0.)
1414 (float state.w, float state.h)
1416 GlDraw.color (0.0, 0.0, 0.0);
1417 let sh = (float (maxy + state.h) /. float state.h) in
1418 let sh = float state.h /. sh in
1419 let sh = max sh (float conf.scrollh) in
1421 let percent =
1422 if state.y = state.maxy
1423 then 1.0
1424 else float state.y /. float maxy
1426 let position = (float state.h -. sh) *. percent in
1428 let position =
1429 if position +. sh > float state.h
1430 then
1431 float state.h -. sh
1432 else
1433 position
1435 GlDraw.rect
1436 (float (state.w - conf.scrollw), position)
1437 (float state.w, position +. sh)
1441 let showsel () =
1442 match state.mstate with
1443 | Mnone ->
1446 | Msel ((x0, y0), (x1, y1)) ->
1447 let rec loop = function
1448 | l :: ls ->
1449 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1450 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1451 then
1452 match getopaque l.pageno with
1453 | Some opaque when validopaque opaque ->
1454 let oy = -l.pagey + l.pagedispy in
1455 seltext opaque (x0, y0, x1, y1) oy;
1457 | _ -> ()
1458 else loop ls
1459 | [] -> ()
1461 loop state.layout
1464 let showrects () =
1465 Gl.enable `blend;
1466 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1467 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1468 List.iter
1469 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1470 List.iter (fun l ->
1471 if l.pageno = pageno
1472 then (
1473 let d = float (l.pagedispy - l.pagey) in
1474 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1475 GlDraw.begins `quads;
1477 GlDraw.vertex2 (x0, y0+.d);
1478 GlDraw.vertex2 (x1, y1+.d);
1479 GlDraw.vertex2 (x2, y2+.d);
1480 GlDraw.vertex2 (x3, y3+.d);
1482 GlDraw.ends ();
1484 ) state.layout
1485 ) state.rects
1487 Gl.disable `blend;
1490 let showoutline = function
1491 | None -> ()
1492 | Some (allowdel, active, first, outlines, qsearch) ->
1493 Gl.enable `blend;
1494 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1495 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1496 GlDraw.rect (0., 0.) (float state.w, float state.h);
1497 Gl.disable `blend;
1499 GlDraw.color (1., 1., 1.);
1500 let font = Glut.BITMAP_9_BY_15 in
1501 let draw_string x y s =
1502 GlPix.raster_pos ~x ~y ();
1503 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1505 let rec loop row =
1506 if row = Array.length outlines || (row - first) * 16 > state.h
1507 then ()
1508 else (
1509 let (s, l, _, _) = outlines.(row) in
1510 let y = (row - first) * 16 in
1511 let x = 5 + 15*l in
1512 if row = active
1513 then (
1514 Gl.enable `blend;
1515 GlDraw.polygon_mode `both `line;
1516 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1517 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1518 GlDraw.rect (0., float (y + 1))
1519 (float (state.w - conf.scrollw - 1), float (y + 18));
1520 GlDraw.polygon_mode `both `fill;
1521 Gl.disable `blend;
1522 GlDraw.color (1., 1., 1.);
1524 draw_string (float x) (float (y + 16)) s;
1525 loop (row+1)
1528 loop first
1531 let display () =
1532 let lasty = List.fold_left drawpage 0 (state.layout) in
1533 GlDraw.color (scalecolor 0.5);
1534 GlDraw.rect
1535 (0., float lasty)
1536 (float (state.w - conf.scrollw), float state.h)
1538 showrects ();
1539 scrollindicator ();
1540 showsel ();
1541 showoutline state.outline;
1542 enttext ();
1543 Glut.swapBuffers ();
1546 let getlink x y =
1547 let rec f = function
1548 | l :: rest ->
1549 begin match getopaque l.pageno with
1550 | Some opaque when validopaque opaque ->
1551 let y = y - l.pagedispy in
1552 if y > 0
1553 then
1554 let y = l.pagey + y in
1555 match getlink opaque x y with
1556 | LNone -> f rest
1557 | link -> link
1558 else
1559 f rest
1560 | _ ->
1561 f rest
1563 | [] -> LNone
1565 f state.layout
1568 let mouse ~button ~bstate ~x ~y =
1569 match button with
1570 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1571 let incr =
1572 if n = 3
1573 then
1574 -conf.scrollincr
1575 else
1576 conf.scrollincr
1578 let incr = incr * 2 in
1579 let y = clamp incr in
1580 gotoy y
1582 | Glut.LEFT_BUTTON when state.outline = None ->
1583 let dest = if bstate = Glut.DOWN then getlink x y else LNone in
1584 begin match dest with
1585 | LGoto (pageno, top) ->
1586 if pageno >= 0
1587 then
1588 gotopage pageno top
1590 | LUri s ->
1591 print_endline s
1593 | LNone ->
1594 if bstate = Glut.DOWN
1595 then (
1596 if state.rotate mod 360 = 0 then (
1597 Glut.setCursor Glut.CURSOR_TEXT;
1598 state.mstate <- Msel ((x, y), (x, y));
1599 Glut.postRedisplay ()
1602 else (
1603 match state.mstate with
1604 | Mnone -> ()
1605 | Msel ((x0, y0), (x1, y1)) ->
1606 let f l =
1607 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1608 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1609 then
1610 match getopaque l.pageno with
1611 | Some opaque when validopaque opaque ->
1612 copysel opaque
1613 | _ -> ()
1615 List.iter f state.layout;
1616 copysel ""; (* ugly *)
1617 Glut.setCursor Glut.CURSOR_INHERIT;
1618 state.mstate <- Mnone;
1622 | _ ->
1625 let mouse ~button ~state ~x ~y = mouse button state x y;;
1627 let motion ~x ~y =
1628 if state.outline = None
1629 then
1630 match state.mstate with
1631 | Mnone -> ()
1632 | Msel (a, _) ->
1633 state.mstate <- Msel (a, (x, y));
1634 Glut.postRedisplay ()
1637 let pmotion ~x ~y =
1638 if state.outline = None
1639 then
1640 match state.mstate with
1641 | Mnone ->
1642 if getlink x y != LNone
1643 then Glut.setCursor Glut.CURSOR_INFO
1644 else Glut.setCursor Glut.CURSOR_INHERIT
1646 | Msel (a, _) ->
1650 let () =
1651 let statepath =
1652 let home =
1653 if Sys.os_type = "Win32"
1654 then
1655 try Sys.getenv "HOMEPATH" with Not_found -> ""
1656 else
1657 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1659 Filename.concat home "llpp"
1661 let pstate =
1663 let ic = open_in_bin statepath in
1664 let hash = input_value ic in
1665 close_in ic;
1666 hash
1667 with exn ->
1668 if false
1669 then
1670 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1672 Hashtbl.create 1
1674 let savestate () =
1676 let w, h =
1677 match state.fullscreen with
1678 | None -> state.w, state.h
1679 | Some wh -> wh
1681 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1682 let oc = open_out_bin statepath in
1683 output_value oc pstate
1684 with exn ->
1685 if false
1686 then
1687 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1690 let setstate () =
1692 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1693 state.w <- statew;
1694 state.h <- stateh;
1695 state.bookmarks <- statebookmarks;
1696 with Not_found -> ()
1697 | exn ->
1698 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1701 Arg.parse [] (fun s -> state.path <- s) "options:";
1702 let name =
1703 if String.length state.path = 0
1704 then (prerr_endline "filename missing"; exit 1)
1705 else state.path
1708 setstate ();
1709 let _ = Glut.init Sys.argv in
1710 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1711 let () = Glut.initWindowSize state.w state.h in
1712 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1714 let csock, ssock =
1715 if Sys.os_type = "Unix"
1716 then
1717 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1718 else
1719 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1720 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1721 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1722 Unix.bind sock addr;
1723 Unix.listen sock 1;
1724 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1725 Unix.connect csock addr;
1726 let ssock, _ = Unix.accept sock in
1727 Unix.close sock;
1728 let opts sock =
1729 Unix.setsockopt sock Unix.TCP_NODELAY true;
1730 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1732 opts ssock;
1733 opts csock;
1734 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1735 ssock, csock
1738 let () = Glut.displayFunc display in
1739 let () = Glut.reshapeFunc reshape in
1740 let () = Glut.keyboardFunc keyboard in
1741 let () = Glut.specialFunc special in
1742 let () = Glut.idleFunc (Some idle) in
1743 let () = Glut.mouseFunc mouse in
1744 let () = Glut.motionFunc motion in
1745 let () = Glut.passiveMotionFunc pmotion in
1747 init ssock;
1748 state.csock <- csock;
1749 state.ssock <- ssock;
1750 state.text <- "Opening " ^ name;
1751 writecmd csock ("open " ^ name ^ "\000");
1753 at_exit savestate;
1755 let rec handlelablglutbug () =
1757 Glut.mainLoop ();
1758 with Glut.BadEnum "key in special_of_int" ->
1759 showtext '!' " LablGlut bug: special key not recognized";
1760 handlelablglutbug ()
1762 handlelablglutbug ();