Allow changing page brightness
[llpp.git] / main.ml
blob61330a9313f26bf79d4cdac193f0ea5aeb6bdea1
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 ; 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 ; inflight = 0
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 preload l =
378 match getopaque l.pageno with
379 | None when state.inflight < 2+0*(cblen state.pagecache) ->
380 state.inflight <- succ state.inflight;
381 cache l.pageno "";
382 wcmd "render" [`i (l.pageno + 1)
383 ;`i l.pagedimno
384 ;`i l.pagew
385 ;`i l.pageh];
387 | _ -> ()
390 let preloadlayout layout =
391 let rec f all = function
392 | l :: ls ->
393 begin match getopaque l.pageno with
394 | None -> preload 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 = preloadlayout 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 preload 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 act cmd =
511 match cmd.[0] with
512 | 'c' ->
513 state.pages <- [];
514 state.outlines <- Olist []
516 | 'D' ->
517 state.rects <- state.rects1;
518 Glut.postRedisplay ()
520 | 'C' ->
521 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
522 state.pagecount <- n;
523 state.invalidated <- state.invalidated - 1;
524 if state.invalidated = 0
525 then (
526 let rely = yratio state.y in
527 let maxy = calcheight () in
528 state.y <- truncate (float maxy *. rely);
529 let pages = layout state.y state.h in
530 state.layout <- pages;
531 Glut.postRedisplay ();
534 | 't' ->
535 let s = Scanf.sscanf cmd "t %n"
536 (fun n -> String.sub cmd n (String.length cmd - n))
538 Glut.setWindowTitle s
540 | 'T' ->
541 let s = Scanf.sscanf cmd "T %n"
542 (fun n -> String.sub cmd n (String.length cmd - n))
544 if state.textentry = None
545 then (
546 state.text <- s;
547 showtext ' ' s;
548 Glut.swapBuffers ();
550 else (
551 state.text <- s;
552 Glut.postRedisplay ();
555 | 'V' ->
556 if conf.verbose
557 then
558 let s = Scanf.sscanf cmd "V %n"
559 (fun n -> String.sub cmd n (String.length cmd - n))
561 state.text <- s;
562 showtext ' ' s;
563 Glut.swapBuffers ();
565 | 'F' ->
566 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
567 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
568 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
569 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
571 let y = (getpagey pageno) + truncate y0 in
572 addnav ();
573 gotoy y;
574 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
576 | 'R' ->
577 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
578 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
579 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
580 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
582 state.rects1 <-
583 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
585 | 'r' ->
586 let n, w, h, r, p =
587 Scanf.sscanf cmd "r %d %d %d %d %s"
588 (fun n w h r p -> (n, w, h, r, p))
590 Hashtbl.replace state.pagemap (n, w, r) p;
591 let evicted = cbpeekw state.pagecache in
592 if String.length evicted > 0
593 then begin
594 wcmd "free" [`s evicted];
595 let l = Hashtbl.fold (fun k p a ->
596 if evicted = p then k :: a else a) state.pagemap []
598 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
599 end;
600 cbput state.pagecache p;
601 state.inflight <- pred state.inflight;
602 if conf.showall then (
603 let y = truncate (ceil (state.ty *. float state.maxy)) in
604 let layout = layout y state.h in
605 if preloadlayout layout
606 then (
607 state.y <- y;
608 state.layout <- layout;
609 Glut.postRedisplay ();
612 else (
613 Glut.postRedisplay ();
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;
788 Glut.swapBuffers ()
790 else
791 let pageno =
792 match state.layout with
793 | [] -> -1
794 | {pageno=pageno} :: rest -> pageno
796 let active =
797 let rec loop n =
798 if n = Array.length outlines
799 then 0
800 else
801 let (_, _, outlinepageno, _) = outlines.(n) in
802 if outlinepageno >= pageno then n else loop (n+1)
804 loop 0
806 state.outline <-
807 Some (allowdel, active,
808 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
809 Glut.postRedisplay ();
812 let enteroutlinemode () =
813 let outlines =
814 match state.outlines with
815 | Oarray a -> a
816 | Olist l ->
817 let a = Array.of_list (List.rev l) in
818 state.outlines <- Oarray a;
820 | Onarrow (a, b) -> a
822 enterselector false outlines "Document has no outline";
825 let enterbookmarkmode () =
826 let bookmarks = Array.of_list state.bookmarks in
827 enterselector true bookmarks "Document has no bookmarks (yet)";
831 let quickbookmark ?title () =
832 match state.layout with
833 | [] -> ()
834 | l :: _ ->
835 let title =
836 match title with
837 | None ->
838 let sec = Unix.gettimeofday () in
839 let tm = Unix.localtime sec in
840 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
841 l.pageno
842 tm.Unix.tm_mday
843 tm.Unix.tm_mon
844 (tm.Unix.tm_year + 1900)
845 tm.Unix.tm_hour
846 tm.Unix.tm_min
847 | Some title -> title
849 state.bookmarks <-
850 (title, 0, l.pageno, l.pagey) :: state.bookmarks
853 let viewkeyboard ~key ~x ~y =
854 let enttext te =
855 state.textentry <- te;
856 state.text <- "";
857 enttext ();
858 Glut.postRedisplay ()
860 match state.textentry with
861 | None ->
862 let c = Char.chr key in
863 begin match c with
864 | '\027' | 'q' ->
865 exit 0
867 | '\008' ->
868 let y = getnav () in
869 gotoy y
871 | 'o' ->
872 enteroutlinemode ()
874 | 'u' ->
875 state.rects <- [];
876 state.text <- "";
877 Glut.postRedisplay ()
879 | '/' | '?' ->
880 let ondone isforw s =
881 cbput state.hists.pat s;
882 cbrfollowlen state.hists.pat;
883 state.searchpattern <- s;
884 search s isforw
886 enttext (Some (c, "", Some (onhist state.hists.pat),
887 textentry, ondone (c ='/')))
889 | '+' ->
890 let ondone s =
891 let n =
892 try int_of_string s with exc ->
893 state.text <- Printf.sprintf "bad integer `%s': %s"
894 s (Printexc.to_string exc);
895 max_int
897 if n != max_int
898 then (
899 conf.pagebias <- n;
900 state.text <- "page bias is now " ^ string_of_int n;
903 enttext (Some ('+', "", None, intentry, ondone))
905 | '-' ->
906 let ondone msg =
907 state.text <- msg;
909 enttext (Some ('-', "", None, optentry, ondone))
911 | '0' .. '9' ->
912 let ondone s =
913 let n =
914 try int_of_string s with exc ->
915 state.text <- Printf.sprintf "bad integer `%s': %s"
916 s (Printexc.to_string exc);
919 if n >= 0
920 then (
921 addnav ();
922 cbput state.hists.pag (string_of_int n);
923 cbrfollowlen state.hists.pag;
924 gotoy (getpagey (n + conf.pagebias - 1))
927 let pageentry text key =
928 match Char.unsafe_chr key with
929 | 'g' -> TEdone text
930 | _ -> intentry text key
932 let text = "x" in text.[0] <- c;
933 enttext (Some (':', text, Some (onhist state.hists.pag),
934 pageentry, ondone))
936 | 'b' ->
937 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
938 reshape state.w state.h;
940 | 'l' ->
941 conf.hlinks <- not conf.hlinks;
942 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
943 Glut.postRedisplay ()
945 | 'a' ->
946 conf.autoscroll <- not conf.autoscroll
948 | 'f' ->
949 begin match state.fullscreen with
950 | None ->
951 state.fullscreen <- Some (state.w, state.h);
952 Glut.fullScreen ()
953 | Some (w, h) ->
954 state.fullscreen <- None;
955 Glut.reshapeWindow ~w ~h
958 | 'g' ->
959 gotoy 0
961 | 'n' ->
962 search state.searchpattern true
964 | 'p' | 'N' ->
965 search state.searchpattern false
967 | 't' ->
968 begin match state.layout with
969 | [] -> ()
970 | l :: _ ->
971 gotoy (state.y - l.pagey);
974 | ' ' ->
975 begin match List.rev state.layout with
976 | [] -> ()
977 | l :: _ ->
978 gotoy (clamp (l.pageh - l.pagey))
981 | '\127' ->
982 begin match state.layout with
983 | [] -> ()
984 | l :: _ ->
985 gotoy (clamp (-l.pageh));
988 | '=' ->
989 let f (fn, ln) l =
990 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
992 let fn, ln = List.fold_left f (-1, -1) state.layout in
993 let s =
994 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
995 let percent =
996 if maxy <= 0
997 then 100.
998 else (100. *. (float state.y /. float maxy)) in
999 if fn = ln
1000 then
1001 Printf.sprintf "Page %d of %d %.2f%%"
1002 (fn+1) state.pagecount percent
1003 else
1004 Printf.sprintf
1005 "Pages %d-%d of %d %.2f%%"
1006 (fn+1) (ln+1) state.pagecount percent
1008 showtext ' ' s;
1009 Glut.swapBuffers ()
1011 | 'w' ->
1012 begin match state.layout with
1013 | [] -> ()
1014 | l :: _ ->
1015 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1016 Glut.postRedisplay ();
1019 | '\'' ->
1020 enterbookmarkmode ()
1022 | 'm' ->
1023 let ondone s =
1024 match state.layout with
1025 | l :: _ ->
1026 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1027 | _ -> ()
1029 enttext (Some ('~', "", None, textentry, ondone))
1031 | '~' ->
1032 quickbookmark ();
1033 showtext ' ' "Quick bookmark added";
1034 Glut.swapBuffers ()
1036 | 'z' ->
1037 begin match state.layout with
1038 | l :: _ ->
1039 let a = getpagewh l.pagedimno in
1040 let w, h =
1041 if conf.crophack
1042 then
1043 (truncate (1.8 *. (a.(1) -. a.(0))),
1044 truncate (1.2 *. (a.(3) -. a.(0))))
1045 else
1046 (truncate (a.(1) -. a.(0)),
1047 truncate (a.(3) -. a.(0)))
1049 Glut.reshapeWindow (w + conf.scrollw) h;
1050 Glut.postRedisplay ();
1052 | [] -> ()
1055 | '<' | '>' ->
1056 rotate (state.rotate + (if c = '>' then 30 else -30));
1058 | '[' | ']' ->
1059 state.colorscale <-
1060 max 0.0
1061 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1062 Glut.postRedisplay ()
1064 | _ ->
1065 vlog "huh? %d %c" key (Char.chr key);
1068 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1069 let len = String.length text in
1070 if len = 0
1071 then (
1072 state.textentry <- None;
1073 Glut.postRedisplay ();
1075 else (
1076 let s = String.sub text 0 (len - 1) in
1077 enttext (Some (c, s, onhist, onkey, ondone))
1080 | Some (c, text, onhist, onkey, ondone) ->
1081 begin match Char.unsafe_chr key with
1082 | '\r' | '\n' ->
1083 ondone text;
1084 state.textentry <- None;
1085 Glut.postRedisplay ()
1087 | '\027' ->
1088 state.textentry <- None;
1089 Glut.postRedisplay ()
1091 | _ ->
1092 begin match onkey text key with
1093 | TEdone text ->
1094 state.textentry <- None;
1095 ondone text;
1096 Glut.postRedisplay ()
1098 | TEcont text ->
1099 enttext (Some (c, text, onhist, onkey, ondone));
1101 | TEstop ->
1102 state.textentry <- None;
1103 Glut.postRedisplay ()
1105 | TEswitch te ->
1106 state.textentry <- Some te;
1107 Glut.postRedisplay ()
1108 end;
1109 end;
1112 let narrow outlines pattern =
1113 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1114 match reopt with
1115 | None -> None
1116 | Some re ->
1117 let rec fold accu n =
1118 if n = -1 then accu else
1119 let (s, _, _, _) as o = outlines.(n) in
1120 let accu =
1121 if (try ignore (Str.search_forward re s 0); true
1122 with Not_found -> false)
1123 then (o :: accu)
1124 else accu
1126 fold accu (n-1)
1128 let matched = fold [] (Array.length outlines - 1) in
1129 if matched = [] then None else Some (Array.of_list matched)
1132 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1133 let search active pattern incr =
1134 let dosearch re =
1135 let rec loop n =
1136 if n = Array.length outlines || n = -1 then None else
1137 let (s, _, _, _) = outlines.(n) in
1139 (try ignore (Str.search_forward re s 0); true
1140 with Not_found -> false)
1141 then Some n
1142 else loop (n + incr)
1144 loop active
1147 let re = Str.regexp_case_fold pattern in
1148 dosearch re
1149 with Failure s ->
1150 state.text <- s;
1151 None
1153 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1154 match key with
1155 | 27 ->
1156 if String.length qsearch = 0
1157 then (
1158 state.text <- "";
1159 state.outline <- None;
1160 Glut.postRedisplay ();
1162 else (
1163 state.text <- "";
1164 state.outline <- Some (allowdel, active, first, outlines, "");
1165 Glut.postRedisplay ();
1168 | 18 | 19 ->
1169 let incr = if key = 18 then -1 else 1 in
1170 let active, first =
1171 match search (active + incr) qsearch incr with
1172 | None ->
1173 state.text <- qsearch ^ " [not found]";
1174 active, first
1175 | Some active ->
1176 state.text <- qsearch;
1177 active, firstof active
1179 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1180 Glut.postRedisplay ();
1182 | 8 ->
1183 let len = String.length qsearch in
1184 if len = 0
1185 then ()
1186 else (
1187 if len = 1
1188 then (
1189 state.text <- "";
1190 state.outline <- Some (allowdel, active, first, outlines, "");
1192 else
1193 let qsearch = String.sub qsearch 0 (len - 1) in
1194 let active, first =
1195 match search active qsearch ~-1 with
1196 | None ->
1197 state.text <- qsearch ^ " [not found]";
1198 active, first
1199 | Some active ->
1200 state.text <- qsearch;
1201 active, firstof active
1203 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1205 Glut.postRedisplay ()
1207 | 13 ->
1208 if active < Array.length outlines
1209 then (
1210 let (_, _, n, t) = outlines.(active) in
1211 gotopage n t;
1213 state.text <- "";
1214 if allowdel then state.bookmarks <- Array.to_list outlines;
1215 state.outline <- None;
1216 Glut.postRedisplay ();
1218 | _ when key >= 32 && key < 127 ->
1219 let pattern = addchar qsearch (Char.chr key) in
1220 let active, first =
1221 match search active pattern 1 with
1222 | None ->
1223 state.text <- pattern ^ " [not found]";
1224 active, first
1225 | Some active ->
1226 state.text <- pattern;
1227 active, firstof active
1229 state.outline <- Some (allowdel, active, first, outlines, pattern);
1230 Glut.postRedisplay ()
1232 | 14 when not allowdel ->
1233 let optoutlines = narrow outlines qsearch in
1234 begin match optoutlines with
1235 | None -> state.text <- "can't narrow"
1236 | Some outlines ->
1237 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1238 match state.outlines with
1239 | Olist l -> ()
1240 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1241 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1242 end;
1243 Glut.postRedisplay ()
1245 | 21 when not allowdel ->
1246 let outline =
1247 match state.outlines with
1248 | Oarray a -> a
1249 | Olist l ->
1250 let a = Array.of_list (List.rev l) in
1251 state.outlines <- Oarray a;
1253 | Onarrow (a, b) ->
1254 state.outlines <- Oarray b;
1257 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1258 Glut.postRedisplay ()
1260 | 12 ->
1261 state.outline <-
1262 Some (allowdel, active, firstof active, outlines, qsearch);
1263 Glut.postRedisplay ()
1265 | 127 when allowdel ->
1266 let len = Array.length outlines - 1 in
1267 if len = 0
1268 then (
1269 state.outline <- None;
1270 state.bookmarks <- [];
1272 else (
1273 let bookmarks = Array.init len
1274 (fun i ->
1275 let i = if i >= active then i + 1 else i in
1276 outlines.(i)
1279 state.outline <-
1280 Some (allowdel,
1281 min active (len-1),
1282 min first (len-1),
1283 bookmarks, qsearch)
1286 Glut.postRedisplay ()
1288 | _ -> log "unknown key %d" key
1291 let keyboard ~key ~x ~y =
1292 if key = 7
1293 then
1294 wcmd "interrupt" []
1295 else
1296 match state.outline with
1297 | None -> viewkeyboard ~key ~x ~y
1298 | Some outline -> outlinekeyboard ~key ~x ~y outline
1301 let special ~key ~x ~y =
1302 match state.outline with
1303 | None ->
1304 begin match state.textentry with
1305 | None ->
1306 let y =
1307 match key with
1308 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1309 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1310 | Glut.KEY_DOWN -> clamp conf.scrollincr
1311 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1312 | Glut.KEY_PAGE_DOWN -> clamp state.h
1313 | Glut.KEY_HOME -> addnav (); 0
1314 | Glut.KEY_END ->
1315 addnav ();
1316 state.maxy - (if conf.maxhfit then state.h else 0)
1317 | _ -> state.y
1319 state.text <- "";
1320 gotoy y
1322 | Some (c, s, Some onhist, onkey, ondone) ->
1323 let s =
1324 match key with
1325 | Glut.KEY_UP -> onhist HCprev
1326 | Glut.KEY_DOWN -> onhist HCnext
1327 | Glut.KEY_HOME -> onhist HCfirst
1328 | Glut.KEY_END -> onhist HClast
1329 | _ -> state.text
1331 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1332 Glut.postRedisplay ()
1334 | _ -> ()
1337 | Some (allowdel, active, first, outlines, qsearch) ->
1338 let maxrows = maxoutlinerows () in
1339 let navigate incr =
1340 let active = active + incr in
1341 let active = max 0 (min active (Array.length outlines - 1)) in
1342 let first =
1343 if active > first
1344 then
1345 let rows = active - first in
1346 if rows > maxrows then active - maxrows else first
1347 else active
1349 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1350 Glut.postRedisplay ()
1352 match key with
1353 | Glut.KEY_UP -> navigate ~-1
1354 | Glut.KEY_DOWN -> navigate 1
1355 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1356 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1358 | Glut.KEY_HOME ->
1359 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1360 Glut.postRedisplay ()
1362 | Glut.KEY_END ->
1363 let active = Array.length outlines - 1 in
1364 let first = max 0 (active - maxrows) in
1365 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1366 Glut.postRedisplay ()
1368 | _ -> ()
1371 let drawplaceholder l =
1372 GlDraw.color (scalecolor 1.0);
1373 GlDraw.rect
1374 (0.0, float l.pagedispy)
1375 (float l.pagew, float (l.pagedispy + l.pagevh))
1377 let x = 0.0
1378 and y = float (l.pagedispy + 13) in
1379 let font = Glut.BITMAP_8_BY_13 in
1380 GlDraw.color (0.0, 0.0, 0.0);
1381 GlPix.raster_pos ~x ~y ();
1382 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1383 ("Loading " ^ string_of_int l.pageno);
1386 let now () = Unix.gettimeofday ();;
1388 let drawpage i l =
1389 begin match getopaque l.pageno with
1390 | Some opaque when validopaque opaque ->
1391 if state.textentry = None
1392 then GlDraw.color (scalecolor 1.0)
1393 else GlDraw.color (scalecolor 0.4);
1394 let a = now () in
1395 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1396 let b = now () in
1397 let d = b-.a in
1398 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1399 vlog "draw %f sec" d;
1401 | Some _ ->
1402 drawplaceholder l
1404 | None ->
1405 drawplaceholder l;
1406 if state.inflight < cblen state.pagecache
1407 then (
1408 List.iter preload state.layout;
1410 else (
1411 vlog "inflight %d" state.inflight;
1413 end;
1414 GlDraw.color (0.5, 0.5, 0.5);
1415 GlDraw.rect
1416 (0., float i)
1417 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1419 l.pagedispy + l.pagevh;
1422 let scrollindicator () =
1423 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1424 GlDraw.color (0.64 , 0.64, 0.64);
1425 GlDraw.rect
1426 (float (state.w - conf.scrollw), 0.)
1427 (float state.w, float state.h)
1429 GlDraw.color (0.0, 0.0, 0.0);
1430 let sh = (float (maxy + state.h) /. float state.h) in
1431 let sh = float state.h /. sh in
1432 let sh = max sh (float conf.scrollh) in
1434 let percent =
1435 if state.y = state.maxy
1436 then 1.0
1437 else float state.y /. float maxy
1439 let position = (float state.h -. sh) *. percent in
1441 let position =
1442 if position +. sh > float state.h
1443 then
1444 float state.h -. sh
1445 else
1446 position
1448 GlDraw.rect
1449 (float (state.w - conf.scrollw), position)
1450 (float state.w, position +. sh)
1454 let showsel () =
1455 match state.mstate with
1456 | Mnone ->
1459 | Msel ((x0, y0), (x1, y1)) ->
1460 let f l =
1461 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1462 || ((y1 >= l.pagedispy))
1463 then
1464 match getopaque l.pageno with
1465 | Some opaque when validopaque opaque ->
1466 let oy = -l.pagey + l.pagedispy in
1467 seltext opaque (x0, y0, x1, y1) oy
1468 | _ -> ()
1470 List.iter f state.layout
1473 let showrects () =
1474 Gl.enable `blend;
1475 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1476 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1477 List.iter
1478 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1479 List.iter (fun l ->
1480 if l.pageno = pageno
1481 then (
1482 let d = float (l.pagedispy - l.pagey) in
1483 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1484 GlDraw.begins `quads;
1486 GlDraw.vertex2 (x0, y0+.d);
1487 GlDraw.vertex2 (x1, y1+.d);
1488 GlDraw.vertex2 (x2, y2+.d);
1489 GlDraw.vertex2 (x3, y3+.d);
1491 GlDraw.ends ();
1493 ) state.layout
1494 ) state.rects
1496 Gl.disable `blend;
1499 let showoutline = function
1500 | None -> ()
1501 | Some (allowdel, active, first, outlines, qsearch) ->
1502 Gl.enable `blend;
1503 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1504 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1505 GlDraw.rect (0., 0.) (float state.w, float state.h);
1506 Gl.disable `blend;
1508 GlDraw.color (1., 1., 1.);
1509 let font = Glut.BITMAP_9_BY_15 in
1510 let draw_string x y s =
1511 GlPix.raster_pos ~x ~y ();
1512 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1514 let rec loop row =
1515 if row = Array.length outlines || (row - first) * 16 > state.h
1516 then ()
1517 else (
1518 let (s, l, _, _) = outlines.(row) in
1519 let y = (row - first) * 16 in
1520 let x = 5 + 15*l in
1521 if row = active
1522 then (
1523 Gl.enable `blend;
1524 GlDraw.polygon_mode `both `line;
1525 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1526 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1527 GlDraw.rect (0., float (y + 1))
1528 (float (state.w - conf.scrollw - 1), float (y + 18));
1529 GlDraw.polygon_mode `both `fill;
1530 Gl.disable `blend;
1531 GlDraw.color (1., 1., 1.);
1533 draw_string (float x) (float (y + 16)) s;
1534 loop (row+1)
1537 loop first
1540 let display () =
1541 let lasty = List.fold_left drawpage 0 (state.layout) in
1542 GlDraw.color (scalecolor 0.5);
1543 GlDraw.rect
1544 (0., float lasty)
1545 (float (state.w - conf.scrollw), float state.h)
1547 showrects ();
1548 scrollindicator ();
1549 showsel ();
1550 showoutline state.outline;
1551 enttext ();
1552 Glut.swapBuffers ();
1555 let getlink x y =
1556 let rec f = function
1557 | l :: rest ->
1558 begin match getopaque l.pageno with
1559 | Some opaque when validopaque opaque ->
1560 let y = y - l.pagedispy in
1561 if y > 0
1562 then
1563 let y = l.pagey + y in
1564 match getlink opaque x y with
1565 | LNone -> f rest
1566 | link -> link
1567 else
1568 f rest
1569 | _ ->
1570 f rest
1572 | [] -> LNone
1574 f state.layout
1577 let mouse ~button ~bstate ~x ~y =
1578 match button with
1579 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1580 let incr =
1581 if n = 3
1582 then
1583 -conf.scrollincr
1584 else
1585 conf.scrollincr
1587 let incr = incr * 2 in
1588 let y = clamp incr in
1589 gotoy y
1591 | Glut.LEFT_BUTTON when state.outline = None ->
1592 let dest = if bstate = Glut.DOWN then getlink x y else LNone in
1593 begin match dest with
1594 | LGoto (pageno, top) ->
1595 if pageno >= 0
1596 then
1597 gotopage pageno top
1599 | LUri s ->
1600 print_endline s
1602 | LNone ->
1603 if bstate = Glut.DOWN
1604 then (
1605 if state.rotate mod 360 = 0 then (
1606 Glut.setCursor Glut.CURSOR_TEXT;
1607 state.mstate <- Msel ((x, y), (x, y));
1608 Glut.postRedisplay ()
1611 else (
1612 match state.mstate with
1613 | Mnone -> ()
1614 | Msel ((x0, y0), (x1, y1)) ->
1615 let f l =
1616 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1617 || ((y1 >= l.pagedispy))
1618 then
1619 match getopaque l.pageno with
1620 | Some opaque when validopaque opaque ->
1621 copysel opaque
1622 | _ -> ()
1624 List.iter f state.layout;
1625 copysel ""; (* ugly *)
1626 Glut.setCursor Glut.CURSOR_INHERIT;
1627 state.mstate <- Mnone;
1631 | _ ->
1634 let mouse ~button ~state ~x ~y = mouse button state x y;;
1636 let motion ~x ~y =
1637 if state.outline = None
1638 then
1639 match state.mstate with
1640 | Mnone -> ()
1641 | Msel (a, _) ->
1642 state.mstate <- Msel (a, (x, y));
1643 Glut.postRedisplay ()
1646 let pmotion ~x ~y =
1647 if state.outline = None
1648 then
1649 match state.mstate with
1650 | Mnone ->
1651 if getlink x y != LNone
1652 then Glut.setCursor Glut.CURSOR_INFO
1653 else Glut.setCursor Glut.CURSOR_INHERIT
1655 | Msel (a, _) ->
1659 let () =
1660 let statepath =
1661 let home =
1662 if Sys.os_type = "Win32"
1663 then
1664 try Sys.getenv "HOMEPATH" with Not_found -> ""
1665 else
1666 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1668 Filename.concat home "llpp"
1670 let pstate =
1672 let ic = open_in_bin statepath in
1673 let hash = input_value ic in
1674 close_in ic;
1675 hash
1676 with exn ->
1677 if false
1678 then
1679 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1681 Hashtbl.create 1
1683 let savestate () =
1685 let w, h =
1686 match state.fullscreen with
1687 | None -> state.w, state.h
1688 | Some wh -> wh
1690 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1691 let oc = open_out_bin statepath in
1692 output_value oc pstate
1693 with exn ->
1694 if false
1695 then
1696 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1699 let setstate () =
1701 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1702 state.w <- statew;
1703 state.h <- stateh;
1704 state.bookmarks <- statebookmarks;
1705 with Not_found -> ()
1706 | exn ->
1707 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1710 Arg.parse [] (fun s -> state.path <- s) "options:";
1711 let name =
1712 if String.length state.path = 0
1713 then (prerr_endline "filename missing"; exit 1)
1714 else state.path
1717 setstate ();
1718 let _ = Glut.init Sys.argv in
1719 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1720 let () = Glut.initWindowSize state.w state.h in
1721 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1723 let csock, ssock =
1724 if Sys.os_type = "Unix"
1725 then
1726 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1727 else
1728 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1729 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1730 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1731 Unix.bind sock addr;
1732 Unix.listen sock 1;
1733 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1734 Unix.connect csock addr;
1735 let ssock, _ = Unix.accept sock in
1736 Unix.close sock;
1737 let opts sock =
1738 Unix.setsockopt sock Unix.TCP_NODELAY true;
1739 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1741 opts ssock;
1742 opts csock;
1743 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1744 ssock, csock
1747 let () = Glut.displayFunc display in
1748 let () = Glut.reshapeFunc reshape in
1749 let () = Glut.keyboardFunc keyboard in
1750 let () = Glut.specialFunc special in
1751 let () = Glut.idleFunc (Some idle) in
1752 let () = Glut.mouseFunc mouse in
1753 let () = Glut.motionFunc motion in
1754 let () = Glut.passiveMotionFunc pmotion in
1756 init ssock;
1757 state.csock <- csock;
1758 state.ssock <- ssock;
1759 state.text <- "Opening " ^ name;
1760 writecmd csock ("open " ^ name ^ "\000");
1762 at_exit savestate;
1764 let rec handlelablglutbug () =
1766 Glut.mainLoop ();
1767 with Glut.BadEnum "key in special_of_int" ->
1768 showtext '!' " LablGlut bug: special key not recognized";
1769 Glut.swapBuffers ();
1770 handlelablglutbug ()
1772 handlelablglutbug ();