Add "What's under the cursor" information
[llpp.git] / main.ml
blob22cff04d73925dfd75a9c8948c7b0eb6d05333bf
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let log fmt = Printf.kprintf prerr_endline fmt;;
9 let dolog fmt = Printf.kprintf prerr_endline fmt;;
11 external init : Unix.file_descr -> unit = "ml_init";;
12 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
13 external seltext : string -> (int * int * int * int) -> int -> unit =
14 "ml_seltext";;
15 external copysel : string -> unit = "ml_copysel";;
16 external highlightlinks : string -> int -> unit = "ml_highlightlinks";;
17 external getpagewh : int -> float array = "ml_getpagewh";;
18 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
20 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
22 type 'a circbuf =
23 { store : 'a array
24 ; mutable rc : int
25 ; mutable wc : int
26 ; mutable len : int
30 type textentry = (char * string * onhist option * onkey * ondone)
31 and onkey = string -> int -> te
32 and ondone = string -> unit
33 and onhist = histcmd -> string
34 and histcmd = HCnext | HCprev | HCfirst | HClast
35 and te =
36 | TEstop
37 | TEdone of string
38 | TEcont of string
39 | TEswitch of textentry
42 let cbnew n v =
43 { store = Array.create n v
44 ; rc = 0
45 ; wc = 0
46 ; len = 0
50 let cblen b = Array.length b.store;;
52 let cbput b v =
53 let len = cblen b in
54 b.store.(b.wc) <- v;
55 b.wc <- (b.wc + 1) mod len;
56 b.len <- min (b.len + 1) len;
59 let cbpeekw b = b.store.(b.wc);;
61 let cbget b dir =
62 if b.len = 0 then b.store.(0) else
63 let rc = b.rc + dir in
64 let rc = if rc = -1 then b.len - 1 else rc in
65 let rc = if rc = b.len then 0 else rc in
66 b.rc <- rc;
67 b.store.(rc);
70 let cbrfollowlen b =
71 b.rc <- b.len;
74 type layout =
75 { pageno : int
76 ; pagedimno : int
77 ; pagew : int
78 ; pageh : int
79 ; pagedispy : int
80 ; pagey : int
81 ; pagevh : int
85 type conf =
86 { mutable scrollw : int
87 ; mutable scrollh : int
88 ; mutable icase : bool
89 ; mutable preload : bool
90 ; mutable pagebias : int
91 ; mutable verbose : bool
92 ; mutable scrollincr : int
93 ; mutable maxhfit : bool
94 ; mutable crophack : bool
95 ; mutable autoscroll : bool
96 ; mutable showall : bool
97 ; mutable hlinks : bool
98 ; mutable underinfo : bool
102 type outline = string * int * int * int;;
103 type outlines =
104 | Oarray of outline array
105 | Olist of outline list
106 | Onarrow of outline array * outline array
109 type rect = (float * float * float * float * float * float * float * float);;
111 type state =
112 { mutable csock : Unix.file_descr
113 ; mutable ssock : Unix.file_descr
114 ; mutable w : int
115 ; mutable h : int
116 ; mutable rotate : int
117 ; mutable y : int
118 ; mutable ty : float
119 ; mutable maxy : int
120 ; mutable layout : layout list
121 ; pagemap : ((int * int * int), string) Hashtbl.t
122 ; mutable pages : (int * int * int) list
123 ; mutable pagecount : int
124 ; pagecache : string circbuf
125 ; mutable rendering : bool
126 ; mutable mstate : mstate
127 ; mutable searchpattern : string
128 ; mutable rects : (int * int * rect) list
129 ; mutable rects1 : (int * int * rect) list
130 ; mutable text : string
131 ; mutable fullscreen : (int * int) option
132 ; mutable textentry : textentry option
133 ; mutable outlines : outlines
134 ; mutable outline : (bool * int * int * outline array * string) option
135 ; mutable bookmarks : outline list
136 ; mutable path : string
137 ; mutable invalidated : int
138 ; mutable colorscale : float
139 ; hists : hists
141 and hists =
142 { pat : string circbuf
143 ; pag : string circbuf
144 ; nav : float circbuf
148 let conf =
149 { scrollw = 5
150 ; scrollh = 12
151 ; icase = true
152 ; preload = false
153 ; pagebias = 0
154 ; verbose = false
155 ; scrollincr = 24
156 ; maxhfit = true
157 ; crophack = false
158 ; autoscroll = false
159 ; showall = false
160 ; hlinks = false
161 ; underinfo = false
165 let state =
166 { csock = Unix.stdin
167 ; ssock = Unix.stdin
168 ; w = 900
169 ; h = 900
170 ; rotate = 0
171 ; y = 0
172 ; ty = 0.0
173 ; layout = []
174 ; maxy = max_int
175 ; pagemap = Hashtbl.create 10
176 ; pagecache = cbnew 10 ""
177 ; pages = []
178 ; pagecount = 0
179 ; rendering = false
180 ; mstate = Mnone
181 ; rects = []
182 ; rects1 = []
183 ; text = ""
184 ; fullscreen = None
185 ; textentry = None
186 ; searchpattern = ""
187 ; outlines = Olist []
188 ; outline = None
189 ; bookmarks = []
190 ; path = ""
191 ; invalidated = 0
192 ; hists =
193 { nav = cbnew 100 0.0
194 ; pat = cbnew 20 ""
195 ; pag = cbnew 10 ""
197 ; colorscale = 1.0
201 let vlog fmt =
202 if conf.verbose
203 then
204 Printf.kprintf prerr_endline fmt
205 else
206 Printf.kprintf ignore fmt
209 let writecmd fd s =
210 let len = String.length s in
211 let n = 4 + len in
212 let b = Buffer.create n in
213 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
214 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
215 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
216 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
217 Buffer.add_string b s;
218 let s' = Buffer.contents b in
219 let n' = Unix.write fd s' 0 n in
220 if n' != n then failwith "write failed";
223 let readcmd fd =
224 let s = "xxxx" in
225 let n = Unix.read fd s 0 4 in
226 if n != 4 then failwith "incomplete read(len)";
227 let len = 0
228 lor (Char.code s.[0] lsl 24)
229 lor (Char.code s.[1] lsl 16)
230 lor (Char.code s.[2] lsl 8)
231 lor (Char.code s.[3] lsl 0)
233 let s = String.create len in
234 let n = Unix.read fd s 0 len in
235 if n != len then failwith "incomplete read(data)";
239 let yratio y =
240 if y = state.maxy then 1.0
241 else float y /. float state.maxy
244 let makecmd s l =
245 let b = Buffer.create 10 in
246 Buffer.add_string b s;
247 let rec combine = function
248 | [] -> b
249 | x :: xs ->
250 Buffer.add_char b ' ';
251 let s =
252 match x with
253 | `b b -> if b then "1" else "0"
254 | `s s -> s
255 | `i i -> string_of_int i
256 | `f f -> string_of_float f
257 | `I f -> string_of_int (truncate f)
259 Buffer.add_string b s;
260 combine xs;
262 combine l;
265 let wcmd s l =
266 let cmd = Buffer.contents (makecmd s l) in
267 writecmd state.csock cmd;
270 let calcheight () =
271 let rec f pn ph fh l =
272 match l with
273 | (n, _, h) :: rest ->
274 let fh = fh + (n - pn) * ph in
275 f n h fh rest
277 | [] ->
278 let fh = fh + (ph * (state.pagecount - pn)) in
279 max 0 fh
281 let fh = f 0 0 0 state.pages in
285 let getpagey pageno =
286 let rec f pn ph y l =
287 match l with
288 | (n, _, h) :: rest ->
289 if n >= pageno
290 then
291 y + (pageno - pn) * ph
292 else
293 let y = y + (n - pn) * ph in
294 f n h y rest
296 | [] ->
297 y + (pageno - pn) * ph
299 f 0 0 0 state.pages;
302 let layout y sh =
303 let rec f pageno pdimno prev vy py dy l cacheleft accu =
304 if pageno = state.pagecount || cacheleft = 0
305 then accu
306 else
307 let ((_, w, h) as curr), rest, pdimno =
308 match l with
309 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
310 curr, rest, pdimno + 1
311 | _ ->
312 prev, l, pdimno
314 let pageno' = pageno + 1 in
315 if py + h > vy
316 then
317 let py' = vy - py in
318 let vh = h - py' in
319 if dy + vh > sh
320 then
321 let vh = sh - dy in
322 if vh <= 0
323 then
324 accu
325 else
326 let e =
327 { pageno = pageno
328 ; pagedimno = pdimno
329 ; pagew = w
330 ; pageh = h
331 ; pagedispy = dy
332 ; pagey = py'
333 ; pagevh = vh
336 e :: accu
337 else
338 let e =
339 { pageno = pageno
340 ; pagedimno = pdimno
341 ; pagew = w
342 ; pageh = h
343 ; pagedispy = dy
344 ; pagey = py'
345 ; pagevh = vh
348 let accu = e :: accu in
349 f pageno' pdimno curr
350 (vy + vh) (py + h) (dy + vh + 2) rest
351 (pred cacheleft) accu
352 else
353 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
355 if state.invalidated = 0
356 then
357 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
358 state.maxy <- calcheight ();
359 List.rev accu
360 else
364 let clamp incr =
365 let y = state.y + incr in
366 let y = max 0 y in
367 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
371 let getopaque pageno =
372 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
373 state.rotate))
374 with Not_found -> None
377 let cache pageno opaque =
378 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
379 state.rotate) opaque
382 let validopaque opaque = String.length opaque > 0;;
384 let render l =
385 match getopaque l.pageno with
386 | None when not state.rendering ->
387 state.rendering <- true;
388 cache l.pageno "";
389 wcmd "render" [`i (l.pageno + 1)
390 ;`i l.pagedimno
391 ;`i l.pagew
392 ;`i l.pageh];
394 | _ -> ()
397 let loadlayout layout =
398 let rec f all = function
399 | l :: ls ->
400 begin match getopaque l.pageno with
401 | None -> render l; f false ls
402 | Some opaque -> f (all && validopaque opaque) ls
404 | [] -> all
406 f (layout <> []) layout;
409 let gotoy y =
410 let y = max 0 y in
411 let y = min state.maxy y in
412 let pages = layout y state.h in
413 let ready = loadlayout pages in
414 state.ty <- yratio y;
415 if conf.showall then (
416 if ready then (
417 state.layout <- pages;
418 state.y <- y;
419 Glut.postRedisplay ();
422 else (
423 state.layout <- pages;
424 state.y <- y;
425 Glut.postRedisplay ();
427 if conf.preload then begin
428 let y = if state.y < state.h then 0 else state.y - state.h in
429 let pages = layout y (state.h*3) in
430 List.iter render pages;
431 end;
434 let addnav () =
435 cbput state.hists.nav (yratio state.y);
436 cbrfollowlen state.hists.nav;
439 let getnav () =
440 let y = cbget state.hists.nav ~-1 in
441 truncate (y *. float state.maxy)
444 let gotopage n top =
445 let y = getpagey n in
446 addnav ();
447 gotoy (y + top);
450 let invalidate () =
451 state.layout <- [];
452 state.pages <- [];
453 state.rects <- [];
454 state.rects1 <- [];
455 state.invalidated <- state.invalidated + 1;
458 let scalecolor c =
459 let c = c *. state.colorscale in
460 (c, c, c);
463 let reshape ~w ~h =
464 let ratio = float w /. float state.w in
465 let fixbookmark (s, l, pageno, pagey) =
466 let pagey = truncate (float pagey *. ratio) in
467 (s, l, pageno, pagey)
469 state.bookmarks <- List.map fixbookmark state.bookmarks;
470 state.w <- w;
471 state.h <- h;
472 GlDraw.viewport 0 0 w h;
473 GlMat.mode `modelview;
474 GlMat.load_identity ();
475 GlMat.mode `projection;
476 GlMat.load_identity ();
477 GlMat.rotate ~x:1.0 ~angle:180.0 ();
478 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
479 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
480 GlClear.color (scalecolor 1.0);
481 GlClear.clear [`color];
483 invalidate ();
484 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
487 let showtext c s =
488 GlDraw.color (0.0, 0.0, 0.0);
489 GlDraw.rect
490 (0.0, float (state.h - 18))
491 (float (state.w - conf.scrollw - 1), float state.h)
493 let font = Glut.BITMAP_8_BY_13 in
494 GlDraw.color (1.0, 1.0, 1.0);
495 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
496 Glut.bitmapCharacter ~font ~c:(Char.code c);
497 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
500 let enttext () =
501 let len = String.length state.text in
502 match state.textentry with
503 | None ->
504 if len > 0 then showtext ' ' state.text
506 | Some (c, text, _, _, _) ->
507 let s =
508 if len > 0
509 then
510 text ^ " [" ^ state.text ^ "]"
511 else
512 text
514 showtext c s;
517 let showtext c s =
518 if true
519 then (
520 state.text <- Printf.sprintf "%c%s" c s;
521 Glut.postRedisplay ();
523 else (
524 showtext c s;
525 Glut.swapBuffers ();
530 let act cmd =
531 match cmd.[0] with
532 | 'c' ->
533 state.pages <- [];
534 state.outlines <- Olist []
536 | 'D' ->
537 state.rects <- state.rects1;
538 Glut.postRedisplay ()
540 | 'C' ->
541 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
542 state.pagecount <- n;
543 state.invalidated <- state.invalidated - 1;
544 if state.invalidated = 0
545 then (
546 let rely = yratio state.y in
547 state.maxy <- calcheight ();
548 gotoy (truncate (float state.maxy *. rely));
551 | 't' ->
552 let s = Scanf.sscanf cmd "t %n"
553 (fun n -> String.sub cmd n (String.length cmd - n))
555 Glut.setWindowTitle s
557 | 'T' ->
558 let s = Scanf.sscanf cmd "T %n"
559 (fun n -> String.sub cmd n (String.length cmd - n))
561 if state.textentry = None
562 then (
563 state.text <- s;
564 showtext ' ' s;
566 else (
567 state.text <- s;
568 Glut.postRedisplay ();
571 | 'V' ->
572 if conf.verbose
573 then
574 let s = Scanf.sscanf cmd "V %n"
575 (fun n -> String.sub cmd n (String.length cmd - n))
577 state.text <- s;
578 showtext ' ' s;
580 | 'F' ->
581 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
582 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
583 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
584 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
586 let y = (getpagey pageno) + truncate y0 in
587 addnav ();
588 gotoy y;
589 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
591 | 'R' ->
592 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
593 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
594 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
595 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
597 state.rects1 <-
598 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
600 | 'r' ->
601 let n, w, h, r, p =
602 Scanf.sscanf cmd "r %d %d %d %d %s"
603 (fun n w h r p -> (n, w, h, r, p))
605 Hashtbl.replace state.pagemap (n, w, r) p;
606 let opaque = cbpeekw state.pagecache in
607 if validopaque opaque
608 then (
609 let k =
610 Hashtbl.fold
611 (fun k v a -> if v = opaque then k else a)
612 state.pagemap (-1, -1, -1)
614 wcmd "free" [`s opaque];
615 Hashtbl.remove state.pagemap k
617 cbput state.pagecache p;
618 state.rendering <- false;
619 if conf.showall
620 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
621 else gotoy state.y
623 | 'l' ->
624 let (n, w, h) as pagelayout =
625 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
627 state.pages <- pagelayout :: state.pages
629 | 'o' ->
630 let (l, n, t, pos) =
631 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
633 let s = String.sub cmd pos (String.length cmd - pos) in
634 let outline = (s, l, n, t) in
635 let outlines =
636 match state.outlines with
637 | Olist outlines -> Olist (outline :: outlines)
638 | Oarray _ -> Olist [outline]
639 | Onarrow _ -> Olist [outline]
641 state.outlines <- outlines
643 | _ ->
644 log "unknown cmd `%S'" cmd
647 let now = Unix.gettimeofday;;
649 let idle () =
650 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
651 begin match r with
652 | [] ->
653 if conf.autoscroll then begin
654 let y = state.y + conf.scrollincr in
655 let y = if y >= state.maxy then 0 else y in
656 gotoy y;
657 state.text <- "";
658 end;
660 | _ ->
661 let cmd = readcmd state.csock in
662 act cmd;
663 end;
666 let onhist cb = function
667 | HCprev -> cbget cb ~-1
668 | HCnext -> cbget cb 1
669 | HCfirst -> cbget cb ~-(cb.rc)
670 | HClast -> cbget cb (cb.len - 1 - cb.rc)
673 let search pattern forward =
674 if String.length pattern > 0
675 then
676 let pn, py =
677 match state.layout with
678 | [] -> 0, 0
679 | l :: _ ->
680 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
682 let cmd =
683 let b = makecmd "search"
684 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
686 Buffer.add_char b ',';
687 Buffer.add_string b pattern;
688 Buffer.add_char b '\000';
689 Buffer.contents b;
691 writecmd state.csock cmd;
694 let intentry text key =
695 let c = Char.unsafe_chr key in
696 match c with
697 | '0' .. '9' ->
698 let s = "x" in s.[0] <- c;
699 let text = text ^ s in
700 TEcont text
702 | _ ->
703 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
704 TEcont text
707 let addchar s c =
708 let b = Buffer.create (String.length s + 1) in
709 Buffer.add_string b s;
710 Buffer.add_char b c;
711 Buffer.contents b;
714 let textentry text key =
715 let c = Char.unsafe_chr key in
716 match c with
717 | _ when key >= 32 && key < 127 ->
718 let text = addchar text c in
719 TEcont text
721 | _ ->
722 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
723 TEcont text
726 let rotate angle =
727 state.rotate <- angle;
728 invalidate ();
729 wcmd "rotate" [`i angle];
732 let optentry text key =
733 let btos b = if b then "on" else "off" in
734 let c = Char.unsafe_chr key in
735 match c with
736 | 's' ->
737 let ondone s =
738 try conf.scrollincr <- int_of_string s with exc ->
739 state.text <- Printf.sprintf "bad integer `%s': %s"
740 s (Printexc.to_string exc)
742 TEswitch ('#', "", None, intentry, ondone)
744 | 'R' ->
745 let ondone s =
746 match try
747 Some (int_of_string s)
748 with exc ->
749 state.text <- Printf.sprintf "bad integer `%s': %s"
750 s (Printexc.to_string exc);
751 None
752 with
753 | Some angle -> rotate angle
754 | None -> ()
756 TEswitch ('^', "", None, intentry, ondone)
758 | 'i' ->
759 conf.icase <- not conf.icase;
760 TEdone ("case insensitive search " ^ (btos conf.icase))
762 | 'p' ->
763 conf.preload <- not conf.preload;
764 gotoy state.y;
765 TEdone ("preload " ^ (btos conf.preload))
767 | 'v' ->
768 conf.verbose <- not conf.verbose;
769 TEdone ("verbose " ^ (btos conf.verbose))
771 | 'h' ->
772 conf.maxhfit <- not conf.maxhfit;
773 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
774 TEdone ("maxhfit " ^ (btos conf.maxhfit))
776 | 'c' ->
777 conf.crophack <- not conf.crophack;
778 TEdone ("crophack " ^ btos conf.crophack)
780 | 'a' ->
781 conf.showall <- not conf.showall;
782 TEdone ("showall " ^ btos conf.showall)
784 | 'f' ->
785 conf.underinfo <- not conf.underinfo;
786 TEdone ("underinfo " ^ btos conf.underinfo)
788 | _ ->
789 state.text <- Printf.sprintf "bad option %d `%c'" key c;
790 TEstop
793 let maxoutlinerows () = (state.h - 31) / 16;;
795 let enterselector allowdel outlines errmsg =
796 if Array.length outlines = 0
797 then (
798 showtext ' ' errmsg;
800 else
801 let pageno =
802 match state.layout with
803 | [] -> -1
804 | {pageno=pageno} :: rest -> pageno
806 let active =
807 let rec loop n =
808 if n = Array.length outlines
809 then 0
810 else
811 let (_, _, outlinepageno, _) = outlines.(n) in
812 if outlinepageno >= pageno then n else loop (n+1)
814 loop 0
816 state.outline <-
817 Some (allowdel, active,
818 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
819 Glut.postRedisplay ();
822 let enteroutlinemode () =
823 let outlines =
824 match state.outlines with
825 | Oarray a -> a
826 | Olist l ->
827 let a = Array.of_list (List.rev l) in
828 state.outlines <- Oarray a;
830 | Onarrow (a, b) -> a
832 enterselector false outlines "Document has no outline";
835 let enterbookmarkmode () =
836 let bookmarks = Array.of_list state.bookmarks in
837 enterselector true bookmarks "Document has no bookmarks (yet)";
841 let quickbookmark ?title () =
842 match state.layout with
843 | [] -> ()
844 | l :: _ ->
845 let title =
846 match title with
847 | None ->
848 let sec = Unix.gettimeofday () in
849 let tm = Unix.localtime sec in
850 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
851 l.pageno
852 tm.Unix.tm_mday
853 tm.Unix.tm_mon
854 (tm.Unix.tm_year + 1900)
855 tm.Unix.tm_hour
856 tm.Unix.tm_min
857 | Some title -> title
859 state.bookmarks <-
860 (title, 0, l.pageno, l.pagey) :: state.bookmarks
863 let viewkeyboard ~key ~x ~y =
864 let enttext te =
865 state.textentry <- te;
866 state.text <- "";
867 enttext ();
868 Glut.postRedisplay ()
870 match state.textentry with
871 | None ->
872 let c = Char.chr key in
873 begin match c with
874 | '\027' | 'q' ->
875 exit 0
877 | '\008' ->
878 let y = getnav () in
879 gotoy y
881 | 'o' ->
882 enteroutlinemode ()
884 | 'u' ->
885 state.rects <- [];
886 state.text <- "";
887 Glut.postRedisplay ()
889 | '/' | '?' ->
890 let ondone isforw s =
891 cbput state.hists.pat s;
892 cbrfollowlen state.hists.pat;
893 state.searchpattern <- s;
894 search s isforw
896 enttext (Some (c, "", Some (onhist state.hists.pat),
897 textentry, ondone (c ='/')))
899 | '+' ->
900 let ondone s =
901 let n =
902 try int_of_string s with exc ->
903 state.text <- Printf.sprintf "bad integer `%s': %s"
904 s (Printexc.to_string exc);
905 max_int
907 if n != max_int
908 then (
909 conf.pagebias <- n;
910 state.text <- "page bias is now " ^ string_of_int n;
913 enttext (Some ('+', "", None, intentry, ondone))
915 | '-' ->
916 let ondone msg =
917 state.text <- msg;
919 enttext (Some ('-', "", None, optentry, ondone))
921 | '0' .. '9' ->
922 let ondone s =
923 let n =
924 try int_of_string s with exc ->
925 state.text <- Printf.sprintf "bad integer `%s': %s"
926 s (Printexc.to_string exc);
929 if n >= 0
930 then (
931 addnav ();
932 cbput state.hists.pag (string_of_int n);
933 cbrfollowlen state.hists.pag;
934 gotoy (getpagey (n + conf.pagebias - 1))
937 let pageentry text key =
938 match Char.unsafe_chr key with
939 | 'g' -> TEdone text
940 | _ -> intentry text key
942 let text = "x" in text.[0] <- c;
943 enttext (Some (':', text, Some (onhist state.hists.pag),
944 pageentry, ondone))
946 | 'b' ->
947 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
948 reshape state.w state.h;
950 | 'l' ->
951 conf.hlinks <- not conf.hlinks;
952 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
953 Glut.postRedisplay ()
955 | 'a' ->
956 conf.autoscroll <- not conf.autoscroll
958 | 'f' ->
959 begin match state.fullscreen with
960 | None ->
961 state.fullscreen <- Some (state.w, state.h);
962 Glut.fullScreen ()
963 | Some (w, h) ->
964 state.fullscreen <- None;
965 Glut.reshapeWindow ~w ~h
968 | 'g' ->
969 gotoy 0
971 | 'n' ->
972 search state.searchpattern true
974 | 'p' | 'N' ->
975 search state.searchpattern false
977 | 't' ->
978 begin match state.layout with
979 | [] -> ()
980 | l :: _ ->
981 gotoy (state.y - l.pagey);
984 | ' ' ->
985 begin match List.rev state.layout with
986 | [] -> ()
987 | l :: _ ->
988 gotoy (clamp (l.pageh - l.pagey))
991 | '\127' ->
992 begin match state.layout with
993 | [] -> ()
994 | l :: _ ->
995 gotoy (clamp (-l.pageh));
998 | '=' ->
999 let f (fn, ln) l =
1000 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1002 let fn, ln = List.fold_left f (-1, -1) state.layout in
1003 let s =
1004 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1005 let percent =
1006 if maxy <= 0
1007 then 100.
1008 else (100. *. (float state.y /. float maxy)) in
1009 if fn = ln
1010 then
1011 Printf.sprintf "Page %d of %d %.2f%%"
1012 (fn+1) state.pagecount percent
1013 else
1014 Printf.sprintf
1015 "Pages %d-%d of %d %.2f%%"
1016 (fn+1) (ln+1) state.pagecount percent
1018 showtext ' ' s;
1020 | 'w' ->
1021 begin match state.layout with
1022 | [] -> ()
1023 | l :: _ ->
1024 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1025 Glut.postRedisplay ();
1028 | '\'' ->
1029 enterbookmarkmode ()
1031 | 'm' ->
1032 let ondone s =
1033 match state.layout with
1034 | l :: _ ->
1035 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1036 | _ -> ()
1038 enttext (Some ('~', "", None, textentry, ondone))
1040 | '~' ->
1041 quickbookmark ();
1042 showtext ' ' "Quick bookmark added";
1044 | 'z' ->
1045 begin match state.layout with
1046 | l :: _ ->
1047 let a = getpagewh l.pagedimno in
1048 let w, h =
1049 if conf.crophack
1050 then
1051 (truncate (1.8 *. (a.(1) -. a.(0))),
1052 truncate (1.2 *. (a.(3) -. a.(0))))
1053 else
1054 (truncate (a.(1) -. a.(0)),
1055 truncate (a.(3) -. a.(0)))
1057 Glut.reshapeWindow (w + conf.scrollw) h;
1058 Glut.postRedisplay ();
1060 | [] -> ()
1063 | '<' | '>' ->
1064 rotate (state.rotate + (if c = '>' then 30 else -30));
1066 | '[' | ']' ->
1067 state.colorscale <-
1068 max 0.0
1069 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1070 Glut.postRedisplay ()
1072 | _ ->
1073 vlog "huh? %d %c" key (Char.chr key);
1076 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1077 let len = String.length text in
1078 if len = 0
1079 then (
1080 state.textentry <- None;
1081 Glut.postRedisplay ();
1083 else (
1084 let s = String.sub text 0 (len - 1) in
1085 enttext (Some (c, s, onhist, onkey, ondone))
1088 | Some (c, text, onhist, onkey, ondone) ->
1089 begin match Char.unsafe_chr key with
1090 | '\r' | '\n' ->
1091 ondone text;
1092 state.textentry <- None;
1093 Glut.postRedisplay ()
1095 | '\027' ->
1096 state.textentry <- None;
1097 Glut.postRedisplay ()
1099 | _ ->
1100 begin match onkey text key with
1101 | TEdone text ->
1102 state.textentry <- None;
1103 ondone text;
1104 Glut.postRedisplay ()
1106 | TEcont text ->
1107 enttext (Some (c, text, onhist, onkey, ondone));
1109 | TEstop ->
1110 state.textentry <- None;
1111 Glut.postRedisplay ()
1113 | TEswitch te ->
1114 state.textentry <- Some te;
1115 Glut.postRedisplay ()
1116 end;
1117 end;
1120 let narrow outlines pattern =
1121 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1122 match reopt with
1123 | None -> None
1124 | Some re ->
1125 let rec fold accu n =
1126 if n = -1 then accu else
1127 let (s, _, _, _) as o = outlines.(n) in
1128 let accu =
1129 if (try ignore (Str.search_forward re s 0); true
1130 with Not_found -> false)
1131 then (o :: accu)
1132 else accu
1134 fold accu (n-1)
1136 let matched = fold [] (Array.length outlines - 1) in
1137 if matched = [] then None else Some (Array.of_list matched)
1140 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1141 let search active pattern incr =
1142 let dosearch re =
1143 let rec loop n =
1144 if n = Array.length outlines || n = -1 then None else
1145 let (s, _, _, _) = outlines.(n) in
1147 (try ignore (Str.search_forward re s 0); true
1148 with Not_found -> false)
1149 then Some n
1150 else loop (n + incr)
1152 loop active
1155 let re = Str.regexp_case_fold pattern in
1156 dosearch re
1157 with Failure s ->
1158 state.text <- s;
1159 None
1161 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1162 match key with
1163 | 27 ->
1164 if String.length qsearch = 0
1165 then (
1166 state.text <- "";
1167 state.outline <- None;
1168 Glut.postRedisplay ();
1170 else (
1171 state.text <- "";
1172 state.outline <- Some (allowdel, active, first, outlines, "");
1173 Glut.postRedisplay ();
1176 | 18 | 19 ->
1177 let incr = if key = 18 then -1 else 1 in
1178 let active, first =
1179 match search (active + incr) qsearch incr with
1180 | None ->
1181 state.text <- qsearch ^ " [not found]";
1182 active, first
1183 | Some active ->
1184 state.text <- qsearch;
1185 active, firstof active
1187 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1188 Glut.postRedisplay ();
1190 | 8 ->
1191 let len = String.length qsearch in
1192 if len = 0
1193 then ()
1194 else (
1195 if len = 1
1196 then (
1197 state.text <- "";
1198 state.outline <- Some (allowdel, active, first, outlines, "");
1200 else
1201 let qsearch = String.sub qsearch 0 (len - 1) in
1202 let active, first =
1203 match search active qsearch ~-1 with
1204 | None ->
1205 state.text <- qsearch ^ " [not found]";
1206 active, first
1207 | Some active ->
1208 state.text <- qsearch;
1209 active, firstof active
1211 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1213 Glut.postRedisplay ()
1215 | 13 ->
1216 if active < Array.length outlines
1217 then (
1218 let (_, _, n, t) = outlines.(active) in
1219 gotopage n t;
1221 state.text <- "";
1222 if allowdel then state.bookmarks <- Array.to_list outlines;
1223 state.outline <- None;
1224 Glut.postRedisplay ();
1226 | _ when key >= 32 && key < 127 ->
1227 let pattern = addchar qsearch (Char.chr key) in
1228 let active, first =
1229 match search active pattern 1 with
1230 | None ->
1231 state.text <- pattern ^ " [not found]";
1232 active, first
1233 | Some active ->
1234 state.text <- pattern;
1235 active, firstof active
1237 state.outline <- Some (allowdel, active, first, outlines, pattern);
1238 Glut.postRedisplay ()
1240 | 14 when not allowdel ->
1241 let optoutlines = narrow outlines qsearch in
1242 begin match optoutlines with
1243 | None -> state.text <- "can't narrow"
1244 | Some outlines ->
1245 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1246 match state.outlines with
1247 | Olist l -> ()
1248 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1249 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1250 end;
1251 Glut.postRedisplay ()
1253 | 21 when not allowdel ->
1254 let outline =
1255 match state.outlines with
1256 | Oarray a -> a
1257 | Olist l ->
1258 let a = Array.of_list (List.rev l) in
1259 state.outlines <- Oarray a;
1261 | Onarrow (a, b) ->
1262 state.outlines <- Oarray b;
1265 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1266 Glut.postRedisplay ()
1268 | 12 ->
1269 state.outline <-
1270 Some (allowdel, active, firstof active, outlines, qsearch);
1271 Glut.postRedisplay ()
1273 | 127 when allowdel ->
1274 let len = Array.length outlines - 1 in
1275 if len = 0
1276 then (
1277 state.outline <- None;
1278 state.bookmarks <- [];
1280 else (
1281 let bookmarks = Array.init len
1282 (fun i ->
1283 let i = if i >= active then i + 1 else i in
1284 outlines.(i)
1287 state.outline <-
1288 Some (allowdel,
1289 min active (len-1),
1290 min first (len-1),
1291 bookmarks, qsearch)
1294 Glut.postRedisplay ()
1296 | _ -> log "unknown key %d" key
1299 let keyboard ~key ~x ~y =
1300 if key = 7
1301 then
1302 wcmd "interrupt" []
1303 else
1304 match state.outline with
1305 | None -> viewkeyboard ~key ~x ~y
1306 | Some outline -> outlinekeyboard ~key ~x ~y outline
1309 let special ~key ~x ~y =
1310 match state.outline with
1311 | None ->
1312 begin match state.textentry with
1313 | None ->
1314 let y =
1315 match key with
1316 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1317 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1318 | Glut.KEY_DOWN -> clamp conf.scrollincr
1319 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1320 | Glut.KEY_PAGE_DOWN -> clamp state.h
1321 | Glut.KEY_HOME -> addnav (); 0
1322 | Glut.KEY_END ->
1323 addnav ();
1324 state.maxy - (if conf.maxhfit then state.h else 0)
1325 | _ -> state.y
1327 state.text <- "";
1328 gotoy y
1330 | Some (c, s, Some onhist, onkey, ondone) ->
1331 let s =
1332 match key with
1333 | Glut.KEY_UP -> onhist HCprev
1334 | Glut.KEY_DOWN -> onhist HCnext
1335 | Glut.KEY_HOME -> onhist HCfirst
1336 | Glut.KEY_END -> onhist HClast
1337 | _ -> state.text
1339 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1340 Glut.postRedisplay ()
1342 | _ -> ()
1345 | Some (allowdel, active, first, outlines, qsearch) ->
1346 let maxrows = maxoutlinerows () in
1347 let navigate incr =
1348 let active = active + incr in
1349 let active = max 0 (min active (Array.length outlines - 1)) in
1350 let first =
1351 if active > first
1352 then
1353 let rows = active - first in
1354 if rows > maxrows then active - maxrows else first
1355 else active
1357 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1358 Glut.postRedisplay ()
1360 match key with
1361 | Glut.KEY_UP -> navigate ~-1
1362 | Glut.KEY_DOWN -> navigate 1
1363 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1364 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1366 | Glut.KEY_HOME ->
1367 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1368 Glut.postRedisplay ()
1370 | Glut.KEY_END ->
1371 let active = Array.length outlines - 1 in
1372 let first = max 0 (active - maxrows) in
1373 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1374 Glut.postRedisplay ()
1376 | _ -> ()
1379 let drawplaceholder l =
1380 GlDraw.color (scalecolor 1.0);
1381 GlDraw.rect
1382 (0.0, float l.pagedispy)
1383 (float l.pagew, float (l.pagedispy + l.pagevh))
1385 let x = 0.0
1386 and y = float (l.pagedispy + 13) in
1387 let font = Glut.BITMAP_8_BY_13 in
1388 GlDraw.color (0.0, 0.0, 0.0);
1389 GlPix.raster_pos ~x ~y ();
1390 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1391 ("Loading " ^ string_of_int l.pageno);
1394 let now () = Unix.gettimeofday ();;
1396 let drawpage i l =
1397 begin match getopaque l.pageno with
1398 | Some opaque when validopaque opaque ->
1399 if state.textentry = None
1400 then GlDraw.color (scalecolor 1.0)
1401 else GlDraw.color (scalecolor 0.4);
1402 let a = now () in
1403 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1404 let b = now () in
1405 let d = b-.a in
1406 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1407 vlog "draw %f sec" d;
1409 | _ ->
1410 drawplaceholder l;
1411 end;
1412 GlDraw.color (0.5, 0.5, 0.5);
1413 GlDraw.rect
1414 (0., float i)
1415 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1417 l.pagedispy + l.pagevh;
1420 let scrollindicator () =
1421 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1422 GlDraw.color (0.64 , 0.64, 0.64);
1423 GlDraw.rect
1424 (float (state.w - conf.scrollw), 0.)
1425 (float state.w, float state.h)
1427 GlDraw.color (0.0, 0.0, 0.0);
1428 let sh = (float (maxy + state.h) /. float state.h) in
1429 let sh = float state.h /. sh in
1430 let sh = max sh (float conf.scrollh) in
1432 let percent =
1433 if state.y = state.maxy
1434 then 1.0
1435 else float state.y /. float maxy
1437 let position = (float state.h -. sh) *. percent in
1439 let position =
1440 if position +. sh > float state.h
1441 then
1442 float state.h -. sh
1443 else
1444 position
1446 GlDraw.rect
1447 (float (state.w - conf.scrollw), position)
1448 (float state.w, position +. sh)
1452 let showsel () =
1453 match state.mstate with
1454 | Mnone ->
1457 | Msel ((x0, y0), (x1, y1)) ->
1458 let rec loop = function
1459 | l :: ls ->
1460 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1461 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1462 then
1463 match getopaque l.pageno with
1464 | Some opaque when validopaque opaque ->
1465 let oy = -l.pagey + l.pagedispy in
1466 seltext opaque (x0, y0, x1, y1) oy;
1468 | _ -> ()
1469 else loop ls
1470 | [] -> ()
1472 loop state.layout
1475 let showrects () =
1476 Gl.enable `blend;
1477 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1478 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1479 List.iter
1480 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1481 List.iter (fun l ->
1482 if l.pageno = pageno
1483 then (
1484 let d = float (l.pagedispy - l.pagey) in
1485 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1486 GlDraw.begins `quads;
1488 GlDraw.vertex2 (x0, y0+.d);
1489 GlDraw.vertex2 (x1, y1+.d);
1490 GlDraw.vertex2 (x2, y2+.d);
1491 GlDraw.vertex2 (x3, y3+.d);
1493 GlDraw.ends ();
1495 ) state.layout
1496 ) state.rects
1498 Gl.disable `blend;
1501 let showoutline = function
1502 | None -> ()
1503 | Some (allowdel, active, first, outlines, qsearch) ->
1504 Gl.enable `blend;
1505 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1506 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1507 GlDraw.rect (0., 0.) (float state.w, float state.h);
1508 Gl.disable `blend;
1510 GlDraw.color (1., 1., 1.);
1511 let font = Glut.BITMAP_9_BY_15 in
1512 let draw_string x y s =
1513 GlPix.raster_pos ~x ~y ();
1514 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1516 let rec loop row =
1517 if row = Array.length outlines || (row - first) * 16 > state.h
1518 then ()
1519 else (
1520 let (s, l, _, _) = outlines.(row) in
1521 let y = (row - first) * 16 in
1522 let x = 5 + 15*l in
1523 if row = active
1524 then (
1525 Gl.enable `blend;
1526 GlDraw.polygon_mode `both `line;
1527 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1528 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1529 GlDraw.rect (0., float (y + 1))
1530 (float (state.w - conf.scrollw - 1), float (y + 18));
1531 GlDraw.polygon_mode `both `fill;
1532 Gl.disable `blend;
1533 GlDraw.color (1., 1., 1.);
1535 draw_string (float x) (float (y + 16)) s;
1536 loop (row+1)
1539 loop first
1542 let display () =
1543 let lasty = List.fold_left drawpage 0 (state.layout) in
1544 GlDraw.color (scalecolor 0.5);
1545 GlDraw.rect
1546 (0., float lasty)
1547 (float (state.w - conf.scrollw), float state.h)
1549 showrects ();
1550 scrollindicator ();
1551 showsel ();
1552 showoutline state.outline;
1553 enttext ();
1554 Glut.swapBuffers ();
1557 let getunder x y =
1558 let rec f = function
1559 | l :: rest ->
1560 begin match getopaque l.pageno with
1561 | Some opaque when validopaque opaque ->
1562 let y = y - l.pagedispy in
1563 if y > 0
1564 then
1565 let y = l.pagey + y in
1566 match whatsunder opaque x y with
1567 | Unone -> f rest
1568 | under -> under
1569 else
1570 f rest
1571 | _ ->
1572 f rest
1574 | [] -> Unone
1576 f state.layout
1579 let mouse ~button ~bstate ~x ~y =
1580 match button with
1581 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1582 let incr =
1583 if n = 3
1584 then
1585 -conf.scrollincr
1586 else
1587 conf.scrollincr
1589 let incr = incr * 2 in
1590 let y = clamp incr in
1591 gotoy y
1593 | Glut.LEFT_BUTTON when state.outline = None ->
1594 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1595 begin match dest with
1596 | Ulinkgoto (pageno, top) ->
1597 if pageno >= 0
1598 then
1599 gotopage pageno top
1601 | Ulinkuri s ->
1602 print_endline s
1604 | Unone when bstate = Glut.DOWN ->
1605 Glut.setCursor Glut.CURSOR_INHERIT;
1606 state.mstate <- Mnone
1608 | Unone | Utext _ ->
1609 if bstate = Glut.DOWN
1610 then (
1611 if state.rotate mod 360 = 0 then (
1612 state.mstate <- Msel ((x, y), (x, y));
1613 Glut.postRedisplay ()
1616 else (
1617 match state.mstate with
1618 | Mnone -> ()
1619 | Msel ((x0, y0), (x1, y1)) ->
1620 let f l =
1621 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1622 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1623 then
1624 match getopaque l.pageno with
1625 | Some opaque when validopaque opaque ->
1626 copysel opaque
1627 | _ -> ()
1629 List.iter f state.layout;
1630 copysel ""; (* ugly *)
1631 Glut.setCursor Glut.CURSOR_INHERIT;
1632 state.mstate <- Mnone;
1636 | _ ->
1639 let mouse ~button ~state ~x ~y = mouse button state x y;;
1641 let motion ~x ~y =
1642 if state.outline = None
1643 then
1644 match state.mstate with
1645 | Mnone -> ()
1646 | Msel (a, _) ->
1647 state.mstate <- Msel (a, (x, y));
1648 Glut.postRedisplay ()
1651 let pmotion ~x ~y =
1652 if state.outline = None
1653 then
1654 match state.mstate with
1655 | Mnone ->
1656 begin match getunder x y with
1657 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1658 | Ulinkuri _ | Ulinkgoto _ -> Glut.setCursor Glut.CURSOR_INFO
1659 | Utext s ->
1660 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1661 Glut.setCursor Glut.CURSOR_TEXT
1664 | Msel (a, _) ->
1668 let () =
1669 let statepath =
1670 let home =
1671 if Sys.os_type = "Win32"
1672 then
1673 try Sys.getenv "HOMEPATH" with Not_found -> ""
1674 else
1675 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1677 Filename.concat home "llpp"
1679 let pstate =
1681 let ic = open_in_bin statepath in
1682 let hash = input_value ic in
1683 close_in ic;
1684 hash
1685 with exn ->
1686 if false
1687 then
1688 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1690 Hashtbl.create 1
1692 let savestate () =
1694 let w, h =
1695 match state.fullscreen with
1696 | None -> state.w, state.h
1697 | Some wh -> wh
1699 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1700 let oc = open_out_bin statepath in
1701 output_value oc pstate
1702 with exn ->
1703 if false
1704 then
1705 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1708 let setstate () =
1710 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1711 state.w <- statew;
1712 state.h <- stateh;
1713 state.bookmarks <- statebookmarks;
1714 with Not_found -> ()
1715 | exn ->
1716 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1719 Arg.parse [] (fun s -> state.path <- s) "options:";
1720 let name =
1721 if String.length state.path = 0
1722 then (prerr_endline "filename missing"; exit 1)
1723 else state.path
1726 setstate ();
1727 let _ = Glut.init Sys.argv in
1728 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1729 let () = Glut.initWindowSize state.w state.h in
1730 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1732 let csock, ssock =
1733 if Sys.os_type = "Unix"
1734 then
1735 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1736 else
1737 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1738 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1739 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1740 Unix.bind sock addr;
1741 Unix.listen sock 1;
1742 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1743 Unix.connect csock addr;
1744 let ssock, _ = Unix.accept sock in
1745 Unix.close sock;
1746 let opts sock =
1747 Unix.setsockopt sock Unix.TCP_NODELAY true;
1748 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1750 opts ssock;
1751 opts csock;
1752 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1753 ssock, csock
1756 let () = Glut.displayFunc display in
1757 let () = Glut.reshapeFunc reshape in
1758 let () = Glut.keyboardFunc keyboard in
1759 let () = Glut.specialFunc special in
1760 let () = Glut.idleFunc (Some idle) in
1761 let () = Glut.mouseFunc mouse in
1762 let () = Glut.motionFunc motion in
1763 let () = Glut.passiveMotionFunc pmotion in
1765 init ssock;
1766 state.csock <- csock;
1767 state.ssock <- ssock;
1768 state.text <- "Opening " ^ name;
1769 writecmd csock ("open " ^ name ^ "\000");
1771 at_exit savestate;
1773 let rec handlelablglutbug () =
1775 Glut.mainLoop ();
1776 with Glut.BadEnum "key in special_of_int" ->
1777 showtext '!' " LablGlut bug: special key not recognized";
1778 handlelablglutbug ()
1780 handlelablglutbug ();