Avoid doing redundant memory operations
[llpp.git] / main.ml
blob33570c6b09e319fb94c702207c192466c50423cc
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 preload () =
410 if conf.preload then begin
411 let y = if state.y < state.h then 0 else state.y - state.h in
412 let pages = layout y (state.h*3) in
413 List.iter render pages;
414 end;
417 let gotoy y =
418 let y = max 0 y in
419 let y = min state.maxy y in
420 let pages = layout y state.h in
421 let ready = loadlayout pages in
422 state.ty <- yratio y;
423 if conf.showall then (
424 if ready then (
425 state.layout <- pages;
426 state.y <- y;
427 Glut.postRedisplay ();
430 else (
431 state.layout <- pages;
432 state.y <- y;
433 Glut.postRedisplay ();
435 preload ();
438 let addnav () =
439 cbput state.hists.nav (yratio state.y);
440 cbrfollowlen state.hists.nav;
443 let getnav () =
444 let y = cbget state.hists.nav ~-1 in
445 truncate (y *. float state.maxy)
448 let gotopage n top =
449 let y = getpagey n in
450 addnav ();
451 gotoy (y + top);
454 let invalidate () =
455 state.layout <- [];
456 state.pages <- [];
457 state.rects <- [];
458 state.rects1 <- [];
459 state.invalidated <- state.invalidated + 1;
462 let scalecolor c =
463 let c = c *. state.colorscale in
464 (c, c, c);
467 let reshape ~w ~h =
468 let ratio = float w /. float state.w in
469 let fixbookmark (s, l, pageno, pagey) =
470 let pagey = truncate (float pagey *. ratio) in
471 (s, l, pageno, pagey)
473 state.bookmarks <- List.map fixbookmark state.bookmarks;
474 state.w <- w;
475 state.h <- h;
476 GlDraw.viewport 0 0 w h;
477 GlMat.mode `modelview;
478 GlMat.load_identity ();
479 GlMat.mode `projection;
480 GlMat.load_identity ();
481 GlMat.rotate ~x:1.0 ~angle:180.0 ();
482 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
483 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
484 GlClear.color (scalecolor 1.0);
485 GlClear.clear [`color];
487 invalidate ();
488 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
491 let showtext c s =
492 GlDraw.color (0.0, 0.0, 0.0);
493 GlDraw.rect
494 (0.0, float (state.h - 18))
495 (float (state.w - conf.scrollw - 1), float state.h)
497 let font = Glut.BITMAP_8_BY_13 in
498 GlDraw.color (1.0, 1.0, 1.0);
499 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
500 Glut.bitmapCharacter ~font ~c:(Char.code c);
501 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
504 let enttext () =
505 let len = String.length state.text in
506 match state.textentry with
507 | None ->
508 if len > 0 then showtext ' ' state.text
510 | Some (c, text, _, _, _) ->
511 let s =
512 if len > 0
513 then
514 text ^ " [" ^ state.text ^ "]"
515 else
516 text
518 showtext c s;
521 let showtext c s =
522 if true
523 then (
524 state.text <- Printf.sprintf "%c%s" c s;
525 Glut.postRedisplay ();
527 else (
528 showtext c s;
529 Glut.swapBuffers ();
534 let act cmd =
535 match cmd.[0] with
536 | 'c' ->
537 state.pages <- [];
538 state.outlines <- Olist []
540 | 'D' ->
541 state.rects <- state.rects1;
542 Glut.postRedisplay ()
544 | 'C' ->
545 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
546 state.pagecount <- n;
547 state.invalidated <- state.invalidated - 1;
548 if state.invalidated = 0
549 then (
550 let rely = yratio state.y in
551 state.maxy <- calcheight ();
552 gotoy (truncate (float state.maxy *. rely));
555 | 't' ->
556 let s = Scanf.sscanf cmd "t %n"
557 (fun n -> String.sub cmd n (String.length cmd - n))
559 Glut.setWindowTitle s
561 | 'T' ->
562 let s = Scanf.sscanf cmd "T %n"
563 (fun n -> String.sub cmd n (String.length cmd - n))
565 if state.textentry = None
566 then (
567 state.text <- s;
568 showtext ' ' s;
570 else (
571 state.text <- s;
572 Glut.postRedisplay ();
575 | 'V' ->
576 if conf.verbose
577 then
578 let s = Scanf.sscanf cmd "V %n"
579 (fun n -> String.sub cmd n (String.length cmd - n))
581 state.text <- s;
582 showtext ' ' s;
584 | 'F' ->
585 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
586 Scanf.sscanf cmd "F %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 let y = (getpagey pageno) + truncate y0 in
591 addnav ();
592 gotoy y;
593 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
595 | 'R' ->
596 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
597 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
598 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
599 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
601 state.rects1 <-
602 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
604 | 'r' ->
605 let n, w, h, r, p =
606 Scanf.sscanf cmd "r %d %d %d %d %s"
607 (fun n w h r p -> (n, w, h, r, p))
609 Hashtbl.replace state.pagemap (n, w, r) p;
610 let opaque = cbpeekw state.pagecache in
611 if validopaque opaque
612 then (
613 let k =
614 Hashtbl.fold
615 (fun k v a -> if v = opaque then k else a)
616 state.pagemap (-1, -1, -1)
618 wcmd "free" [`s opaque];
619 Hashtbl.remove state.pagemap k
621 cbput state.pagecache p;
622 state.rendering <- false;
623 if conf.showall
624 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
625 else (
626 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
627 if visible then gotoy state.y
628 else (ignore (loadlayout state.layout); preload ())
631 | 'l' ->
632 let (n, w, h) as pagelayout =
633 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
635 state.pages <- pagelayout :: state.pages
637 | 'o' ->
638 let (l, n, t, pos) =
639 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
641 let s = String.sub cmd pos (String.length cmd - pos) in
642 let outline = (s, l, n, t) in
643 let outlines =
644 match state.outlines with
645 | Olist outlines -> Olist (outline :: outlines)
646 | Oarray _ -> Olist [outline]
647 | Onarrow _ -> Olist [outline]
649 state.outlines <- outlines
651 | _ ->
652 log "unknown cmd `%S'" cmd
655 let now = Unix.gettimeofday;;
657 let idle () =
658 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
659 begin match r with
660 | [] ->
661 if conf.autoscroll then begin
662 let y = state.y + conf.scrollincr in
663 let y = if y >= state.maxy then 0 else y in
664 gotoy y;
665 state.text <- "";
666 end;
668 | _ ->
669 let cmd = readcmd state.csock in
670 act cmd;
671 end;
674 let onhist cb = function
675 | HCprev -> cbget cb ~-1
676 | HCnext -> cbget cb 1
677 | HCfirst -> cbget cb ~-(cb.rc)
678 | HClast -> cbget cb (cb.len - 1 - cb.rc)
681 let search pattern forward =
682 if String.length pattern > 0
683 then
684 let pn, py =
685 match state.layout with
686 | [] -> 0, 0
687 | l :: _ ->
688 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
690 let cmd =
691 let b = makecmd "search"
692 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
694 Buffer.add_char b ',';
695 Buffer.add_string b pattern;
696 Buffer.add_char b '\000';
697 Buffer.contents b;
699 writecmd state.csock cmd;
702 let intentry text key =
703 let c = Char.unsafe_chr key in
704 match c with
705 | '0' .. '9' ->
706 let s = "x" in s.[0] <- c;
707 let text = text ^ s in
708 TEcont text
710 | _ ->
711 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
712 TEcont text
715 let addchar s c =
716 let b = Buffer.create (String.length s + 1) in
717 Buffer.add_string b s;
718 Buffer.add_char b c;
719 Buffer.contents b;
722 let textentry text key =
723 let c = Char.unsafe_chr key in
724 match c with
725 | _ when key >= 32 && key < 127 ->
726 let text = addchar text c in
727 TEcont text
729 | _ ->
730 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
731 TEcont text
734 let rotate angle =
735 state.rotate <- angle;
736 invalidate ();
737 wcmd "rotate" [`i angle];
740 let optentry text key =
741 let btos b = if b then "on" else "off" in
742 let c = Char.unsafe_chr key in
743 match c with
744 | 's' ->
745 let ondone s =
746 try conf.scrollincr <- int_of_string s with exc ->
747 state.text <- Printf.sprintf "bad integer `%s': %s"
748 s (Printexc.to_string exc)
750 TEswitch ('#', "", None, intentry, ondone)
752 | 'R' ->
753 let ondone s =
754 match try
755 Some (int_of_string s)
756 with exc ->
757 state.text <- Printf.sprintf "bad integer `%s': %s"
758 s (Printexc.to_string exc);
759 None
760 with
761 | Some angle -> rotate angle
762 | None -> ()
764 TEswitch ('^', "", None, intentry, ondone)
766 | 'i' ->
767 conf.icase <- not conf.icase;
768 TEdone ("case insensitive search " ^ (btos conf.icase))
770 | 'p' ->
771 conf.preload <- not conf.preload;
772 gotoy state.y;
773 TEdone ("preload " ^ (btos conf.preload))
775 | 'v' ->
776 conf.verbose <- not conf.verbose;
777 TEdone ("verbose " ^ (btos conf.verbose))
779 | 'h' ->
780 conf.maxhfit <- not conf.maxhfit;
781 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
782 TEdone ("maxhfit " ^ (btos conf.maxhfit))
784 | 'c' ->
785 conf.crophack <- not conf.crophack;
786 TEdone ("crophack " ^ btos conf.crophack)
788 | 'a' ->
789 conf.showall <- not conf.showall;
790 TEdone ("showall " ^ btos conf.showall)
792 | 'f' ->
793 conf.underinfo <- not conf.underinfo;
794 TEdone ("underinfo " ^ btos conf.underinfo)
796 | _ ->
797 state.text <- Printf.sprintf "bad option %d `%c'" key c;
798 TEstop
801 let maxoutlinerows () = (state.h - 31) / 16;;
803 let enterselector allowdel outlines errmsg =
804 if Array.length outlines = 0
805 then (
806 showtext ' ' errmsg;
808 else
809 let pageno =
810 match state.layout with
811 | [] -> -1
812 | {pageno=pageno} :: rest -> pageno
814 let active =
815 let rec loop n =
816 if n = Array.length outlines
817 then 0
818 else
819 let (_, _, outlinepageno, _) = outlines.(n) in
820 if outlinepageno >= pageno then n else loop (n+1)
822 loop 0
824 state.outline <-
825 Some (allowdel, active,
826 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
827 Glut.postRedisplay ();
830 let enteroutlinemode () =
831 let outlines =
832 match state.outlines with
833 | Oarray a -> a
834 | Olist l ->
835 let a = Array.of_list (List.rev l) in
836 state.outlines <- Oarray a;
838 | Onarrow (a, b) -> a
840 enterselector false outlines "Document has no outline";
843 let enterbookmarkmode () =
844 let bookmarks = Array.of_list state.bookmarks in
845 enterselector true bookmarks "Document has no bookmarks (yet)";
849 let quickbookmark ?title () =
850 match state.layout with
851 | [] -> ()
852 | l :: _ ->
853 let title =
854 match title with
855 | None ->
856 let sec = Unix.gettimeofday () in
857 let tm = Unix.localtime sec in
858 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
859 l.pageno
860 tm.Unix.tm_mday
861 tm.Unix.tm_mon
862 (tm.Unix.tm_year + 1900)
863 tm.Unix.tm_hour
864 tm.Unix.tm_min
865 | Some title -> title
867 state.bookmarks <-
868 (title, 0, l.pageno, l.pagey) :: state.bookmarks
871 let viewkeyboard ~key ~x ~y =
872 let enttext te =
873 state.textentry <- te;
874 state.text <- "";
875 enttext ();
876 Glut.postRedisplay ()
878 match state.textentry with
879 | None ->
880 let c = Char.chr key in
881 begin match c with
882 | '\027' | 'q' ->
883 exit 0
885 | '\008' ->
886 let y = getnav () in
887 gotoy y
889 | 'o' ->
890 enteroutlinemode ()
892 | 'u' ->
893 state.rects <- [];
894 state.text <- "";
895 Glut.postRedisplay ()
897 | '/' | '?' ->
898 let ondone isforw s =
899 cbput state.hists.pat s;
900 cbrfollowlen state.hists.pat;
901 state.searchpattern <- s;
902 search s isforw
904 enttext (Some (c, "", Some (onhist state.hists.pat),
905 textentry, ondone (c ='/')))
907 | '+' ->
908 let ondone s =
909 let n =
910 try int_of_string s with exc ->
911 state.text <- Printf.sprintf "bad integer `%s': %s"
912 s (Printexc.to_string exc);
913 max_int
915 if n != max_int
916 then (
917 conf.pagebias <- n;
918 state.text <- "page bias is now " ^ string_of_int n;
921 enttext (Some ('+', "", None, intentry, ondone))
923 | '-' ->
924 let ondone msg =
925 state.text <- msg;
927 enttext (Some ('-', "", None, optentry, ondone))
929 | '0' .. '9' ->
930 let ondone s =
931 let n =
932 try int_of_string s with exc ->
933 state.text <- Printf.sprintf "bad integer `%s': %s"
934 s (Printexc.to_string exc);
937 if n >= 0
938 then (
939 addnav ();
940 cbput state.hists.pag (string_of_int n);
941 cbrfollowlen state.hists.pag;
942 gotoy (getpagey (n + conf.pagebias - 1))
945 let pageentry text key =
946 match Char.unsafe_chr key with
947 | 'g' -> TEdone text
948 | _ -> intentry text key
950 let text = "x" in text.[0] <- c;
951 enttext (Some (':', text, Some (onhist state.hists.pag),
952 pageentry, ondone))
954 | 'b' ->
955 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
956 reshape state.w state.h;
958 | 'l' ->
959 conf.hlinks <- not conf.hlinks;
960 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
961 Glut.postRedisplay ()
963 | 'a' ->
964 conf.autoscroll <- not conf.autoscroll
966 | 'f' ->
967 begin match state.fullscreen with
968 | None ->
969 state.fullscreen <- Some (state.w, state.h);
970 Glut.fullScreen ()
971 | Some (w, h) ->
972 state.fullscreen <- None;
973 Glut.reshapeWindow ~w ~h
976 | 'g' ->
977 gotoy 0
979 | 'n' ->
980 search state.searchpattern true
982 | 'p' | 'N' ->
983 search state.searchpattern false
985 | 't' ->
986 begin match state.layout with
987 | [] -> ()
988 | l :: _ ->
989 gotoy (state.y - l.pagey);
992 | ' ' ->
993 begin match List.rev state.layout with
994 | [] -> ()
995 | l :: _ ->
996 gotoy (clamp (l.pageh - l.pagey))
999 | '\127' ->
1000 begin match state.layout with
1001 | [] -> ()
1002 | l :: _ ->
1003 gotoy (clamp (-l.pageh));
1006 | '=' ->
1007 let f (fn, ln) l =
1008 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1010 let fn, ln = List.fold_left f (-1, -1) state.layout in
1011 let s =
1012 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1013 let percent =
1014 if maxy <= 0
1015 then 100.
1016 else (100. *. (float state.y /. float maxy)) in
1017 if fn = ln
1018 then
1019 Printf.sprintf "Page %d of %d %.2f%%"
1020 (fn+1) state.pagecount percent
1021 else
1022 Printf.sprintf
1023 "Pages %d-%d of %d %.2f%%"
1024 (fn+1) (ln+1) state.pagecount percent
1026 showtext ' ' s;
1028 | 'w' ->
1029 begin match state.layout with
1030 | [] -> ()
1031 | l :: _ ->
1032 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1033 Glut.postRedisplay ();
1036 | '\'' ->
1037 enterbookmarkmode ()
1039 | 'm' ->
1040 let ondone s =
1041 match state.layout with
1042 | l :: _ ->
1043 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1044 | _ -> ()
1046 enttext (Some ('~', "", None, textentry, ondone))
1048 | '~' ->
1049 quickbookmark ();
1050 showtext ' ' "Quick bookmark added";
1052 | 'z' ->
1053 begin match state.layout with
1054 | l :: _ ->
1055 let a = getpagewh l.pagedimno in
1056 let w, h =
1057 if conf.crophack
1058 then
1059 (truncate (1.8 *. (a.(1) -. a.(0))),
1060 truncate (1.2 *. (a.(3) -. a.(0))))
1061 else
1062 (truncate (a.(1) -. a.(0)),
1063 truncate (a.(3) -. a.(0)))
1065 Glut.reshapeWindow (w + conf.scrollw) h;
1066 Glut.postRedisplay ();
1068 | [] -> ()
1071 | '<' | '>' ->
1072 rotate (state.rotate + (if c = '>' then 30 else -30));
1074 | '[' | ']' ->
1075 state.colorscale <-
1076 max 0.0
1077 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1078 Glut.postRedisplay ()
1080 | _ ->
1081 vlog "huh? %d %c" key (Char.chr key);
1084 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1085 let len = String.length text in
1086 if len = 0
1087 then (
1088 state.textentry <- None;
1089 Glut.postRedisplay ();
1091 else (
1092 let s = String.sub text 0 (len - 1) in
1093 enttext (Some (c, s, onhist, onkey, ondone))
1096 | Some (c, text, onhist, onkey, ondone) ->
1097 begin match Char.unsafe_chr key with
1098 | '\r' | '\n' ->
1099 ondone text;
1100 state.textentry <- None;
1101 Glut.postRedisplay ()
1103 | '\027' ->
1104 state.textentry <- None;
1105 Glut.postRedisplay ()
1107 | _ ->
1108 begin match onkey text key with
1109 | TEdone text ->
1110 state.textentry <- None;
1111 ondone text;
1112 Glut.postRedisplay ()
1114 | TEcont text ->
1115 enttext (Some (c, text, onhist, onkey, ondone));
1117 | TEstop ->
1118 state.textentry <- None;
1119 Glut.postRedisplay ()
1121 | TEswitch te ->
1122 state.textentry <- Some te;
1123 Glut.postRedisplay ()
1124 end;
1125 end;
1128 let narrow outlines pattern =
1129 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1130 match reopt with
1131 | None -> None
1132 | Some re ->
1133 let rec fold accu n =
1134 if n = -1 then accu else
1135 let (s, _, _, _) as o = outlines.(n) in
1136 let accu =
1137 if (try ignore (Str.search_forward re s 0); true
1138 with Not_found -> false)
1139 then (o :: accu)
1140 else accu
1142 fold accu (n-1)
1144 let matched = fold [] (Array.length outlines - 1) in
1145 if matched = [] then None else Some (Array.of_list matched)
1148 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1149 let search active pattern incr =
1150 let dosearch re =
1151 let rec loop n =
1152 if n = Array.length outlines || n = -1 then None else
1153 let (s, _, _, _) = outlines.(n) in
1155 (try ignore (Str.search_forward re s 0); true
1156 with Not_found -> false)
1157 then Some n
1158 else loop (n + incr)
1160 loop active
1163 let re = Str.regexp_case_fold pattern in
1164 dosearch re
1165 with Failure s ->
1166 state.text <- s;
1167 None
1169 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1170 match key with
1171 | 27 ->
1172 if String.length qsearch = 0
1173 then (
1174 state.text <- "";
1175 state.outline <- None;
1176 Glut.postRedisplay ();
1178 else (
1179 state.text <- "";
1180 state.outline <- Some (allowdel, active, first, outlines, "");
1181 Glut.postRedisplay ();
1184 | 18 | 19 ->
1185 let incr = if key = 18 then -1 else 1 in
1186 let active, first =
1187 match search (active + incr) qsearch incr with
1188 | None ->
1189 state.text <- qsearch ^ " [not found]";
1190 active, first
1191 | Some active ->
1192 state.text <- qsearch;
1193 active, firstof active
1195 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1196 Glut.postRedisplay ();
1198 | 8 ->
1199 let len = String.length qsearch in
1200 if len = 0
1201 then ()
1202 else (
1203 if len = 1
1204 then (
1205 state.text <- "";
1206 state.outline <- Some (allowdel, active, first, outlines, "");
1208 else
1209 let qsearch = String.sub qsearch 0 (len - 1) in
1210 let active, first =
1211 match search active qsearch ~-1 with
1212 | None ->
1213 state.text <- qsearch ^ " [not found]";
1214 active, first
1215 | Some active ->
1216 state.text <- qsearch;
1217 active, firstof active
1219 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1221 Glut.postRedisplay ()
1223 | 13 ->
1224 if active < Array.length outlines
1225 then (
1226 let (_, _, n, t) = outlines.(active) in
1227 gotopage n t;
1229 state.text <- "";
1230 if allowdel then state.bookmarks <- Array.to_list outlines;
1231 state.outline <- None;
1232 Glut.postRedisplay ();
1234 | _ when key >= 32 && key < 127 ->
1235 let pattern = addchar qsearch (Char.chr key) in
1236 let active, first =
1237 match search active pattern 1 with
1238 | None ->
1239 state.text <- pattern ^ " [not found]";
1240 active, first
1241 | Some active ->
1242 state.text <- pattern;
1243 active, firstof active
1245 state.outline <- Some (allowdel, active, first, outlines, pattern);
1246 Glut.postRedisplay ()
1248 | 14 when not allowdel ->
1249 let optoutlines = narrow outlines qsearch in
1250 begin match optoutlines with
1251 | None -> state.text <- "can't narrow"
1252 | Some outlines ->
1253 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1254 match state.outlines with
1255 | Olist l -> ()
1256 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1257 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1258 end;
1259 Glut.postRedisplay ()
1261 | 21 when not allowdel ->
1262 let outline =
1263 match state.outlines with
1264 | Oarray a -> a
1265 | Olist l ->
1266 let a = Array.of_list (List.rev l) in
1267 state.outlines <- Oarray a;
1269 | Onarrow (a, b) ->
1270 state.outlines <- Oarray b;
1273 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1274 Glut.postRedisplay ()
1276 | 12 ->
1277 state.outline <-
1278 Some (allowdel, active, firstof active, outlines, qsearch);
1279 Glut.postRedisplay ()
1281 | 127 when allowdel ->
1282 let len = Array.length outlines - 1 in
1283 if len = 0
1284 then (
1285 state.outline <- None;
1286 state.bookmarks <- [];
1288 else (
1289 let bookmarks = Array.init len
1290 (fun i ->
1291 let i = if i >= active then i + 1 else i in
1292 outlines.(i)
1295 state.outline <-
1296 Some (allowdel,
1297 min active (len-1),
1298 min first (len-1),
1299 bookmarks, qsearch)
1302 Glut.postRedisplay ()
1304 | _ -> log "unknown key %d" key
1307 let keyboard ~key ~x ~y =
1308 if key = 7
1309 then
1310 wcmd "interrupt" []
1311 else
1312 match state.outline with
1313 | None -> viewkeyboard ~key ~x ~y
1314 | Some outline -> outlinekeyboard ~key ~x ~y outline
1317 let special ~key ~x ~y =
1318 match state.outline with
1319 | None ->
1320 begin match state.textentry with
1321 | None ->
1322 let y =
1323 match key with
1324 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1325 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1326 | Glut.KEY_DOWN -> clamp conf.scrollincr
1327 | Glut.KEY_PAGE_UP ->
1328 if Glut.getModifiers () land Glut.active_ctrl != 0
1329 then
1330 match state.layout with
1331 | [] -> state.y
1332 | l :: _ -> state.y - l.pagey
1333 else
1334 clamp (-state.h)
1335 | Glut.KEY_PAGE_DOWN ->
1336 if Glut.getModifiers () land Glut.active_ctrl != 0
1337 then
1338 match List.rev state.layout with
1339 | [] -> state.y
1340 | l :: _ -> getpagey l.pageno
1341 else
1342 clamp state.h
1343 | Glut.KEY_HOME -> addnav (); 0
1344 | Glut.KEY_END ->
1345 addnav ();
1346 state.maxy - (if conf.maxhfit then state.h else 0)
1347 | _ -> state.y
1349 state.text <- "";
1350 gotoy y
1352 | Some (c, s, Some onhist, onkey, ondone) ->
1353 let s =
1354 match key with
1355 | Glut.KEY_UP -> onhist HCprev
1356 | Glut.KEY_DOWN -> onhist HCnext
1357 | Glut.KEY_HOME -> onhist HCfirst
1358 | Glut.KEY_END -> onhist HClast
1359 | _ -> state.text
1361 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1362 Glut.postRedisplay ()
1364 | _ -> ()
1367 | Some (allowdel, active, first, outlines, qsearch) ->
1368 let maxrows = maxoutlinerows () in
1369 let navigate incr =
1370 let active = active + incr in
1371 let active = max 0 (min active (Array.length outlines - 1)) in
1372 let first =
1373 if active > first
1374 then
1375 let rows = active - first in
1376 if rows > maxrows then active - maxrows else first
1377 else active
1379 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1380 Glut.postRedisplay ()
1382 match key with
1383 | Glut.KEY_UP -> navigate ~-1
1384 | Glut.KEY_DOWN -> navigate 1
1385 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1386 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1388 | Glut.KEY_HOME ->
1389 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1390 Glut.postRedisplay ()
1392 | Glut.KEY_END ->
1393 let active = Array.length outlines - 1 in
1394 let first = max 0 (active - maxrows) in
1395 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1396 Glut.postRedisplay ()
1398 | _ -> ()
1401 let drawplaceholder l =
1402 GlDraw.color (scalecolor 1.0);
1403 GlDraw.rect
1404 (0.0, float l.pagedispy)
1405 (float l.pagew, float (l.pagedispy + l.pagevh))
1407 let x = 0.0
1408 and y = float (l.pagedispy + 13) in
1409 let font = Glut.BITMAP_8_BY_13 in
1410 GlDraw.color (0.0, 0.0, 0.0);
1411 GlPix.raster_pos ~x ~y ();
1412 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1413 ("Loading " ^ string_of_int l.pageno);
1416 let now () = Unix.gettimeofday ();;
1418 let drawpage i l =
1419 begin match getopaque l.pageno with
1420 | Some opaque when validopaque opaque ->
1421 if state.textentry = None
1422 then GlDraw.color (scalecolor 1.0)
1423 else GlDraw.color (scalecolor 0.4);
1424 let a = now () in
1425 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1426 let b = now () in
1427 let d = b-.a in
1428 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1429 vlog "draw %f sec" d;
1431 | _ ->
1432 drawplaceholder l;
1433 end;
1434 GlDraw.color (0.5, 0.5, 0.5);
1435 GlDraw.rect
1436 (0., float i)
1437 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1439 l.pagedispy + l.pagevh;
1442 let scrollindicator () =
1443 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1444 GlDraw.color (0.64 , 0.64, 0.64);
1445 GlDraw.rect
1446 (float (state.w - conf.scrollw), 0.)
1447 (float state.w, float state.h)
1449 GlDraw.color (0.0, 0.0, 0.0);
1450 let sh = (float (maxy + state.h) /. float state.h) in
1451 let sh = float state.h /. sh in
1452 let sh = max sh (float conf.scrollh) in
1454 let percent =
1455 if state.y = state.maxy
1456 then 1.0
1457 else float state.y /. float maxy
1459 let position = (float state.h -. sh) *. percent in
1461 let position =
1462 if position +. sh > float state.h
1463 then
1464 float state.h -. sh
1465 else
1466 position
1468 GlDraw.rect
1469 (float (state.w - conf.scrollw), position)
1470 (float state.w, position +. sh)
1474 let showsel () =
1475 match state.mstate with
1476 | Mnone ->
1479 | Msel ((x0, y0), (x1, y1)) ->
1480 let rec loop = function
1481 | l :: ls ->
1482 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1483 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1484 then
1485 match getopaque l.pageno with
1486 | Some opaque when validopaque opaque ->
1487 let oy = -l.pagey + l.pagedispy in
1488 seltext opaque (x0, y0, x1, y1) oy;
1490 | _ -> ()
1491 else loop ls
1492 | [] -> ()
1494 loop state.layout
1497 let showrects () =
1498 Gl.enable `blend;
1499 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1500 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1501 List.iter
1502 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1503 List.iter (fun l ->
1504 if l.pageno = pageno
1505 then (
1506 let d = float (l.pagedispy - l.pagey) in
1507 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1508 GlDraw.begins `quads;
1510 GlDraw.vertex2 (x0, y0+.d);
1511 GlDraw.vertex2 (x1, y1+.d);
1512 GlDraw.vertex2 (x2, y2+.d);
1513 GlDraw.vertex2 (x3, y3+.d);
1515 GlDraw.ends ();
1517 ) state.layout
1518 ) state.rects
1520 Gl.disable `blend;
1523 let showoutline = function
1524 | None -> ()
1525 | Some (allowdel, active, first, outlines, qsearch) ->
1526 Gl.enable `blend;
1527 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1528 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1529 GlDraw.rect (0., 0.) (float state.w, float state.h);
1530 Gl.disable `blend;
1532 GlDraw.color (1., 1., 1.);
1533 let font = Glut.BITMAP_9_BY_15 in
1534 let draw_string x y s =
1535 GlPix.raster_pos ~x ~y ();
1536 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1538 let rec loop row =
1539 if row = Array.length outlines || (row - first) * 16 > state.h
1540 then ()
1541 else (
1542 let (s, l, _, _) = outlines.(row) in
1543 let y = (row - first) * 16 in
1544 let x = 5 + 15*l in
1545 if row = active
1546 then (
1547 Gl.enable `blend;
1548 GlDraw.polygon_mode `both `line;
1549 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1550 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1551 GlDraw.rect (0., float (y + 1))
1552 (float (state.w - conf.scrollw - 1), float (y + 18));
1553 GlDraw.polygon_mode `both `fill;
1554 Gl.disable `blend;
1555 GlDraw.color (1., 1., 1.);
1557 draw_string (float x) (float (y + 16)) s;
1558 loop (row+1)
1561 loop first
1564 let display () =
1565 let lasty = List.fold_left drawpage 0 (state.layout) in
1566 GlDraw.color (scalecolor 0.5);
1567 GlDraw.rect
1568 (0., float lasty)
1569 (float (state.w - conf.scrollw), float state.h)
1571 showrects ();
1572 scrollindicator ();
1573 showsel ();
1574 showoutline state.outline;
1575 enttext ();
1576 Glut.swapBuffers ();
1579 let getunder x y =
1580 let rec f = function
1581 | l :: rest ->
1582 begin match getopaque l.pageno with
1583 | Some opaque when validopaque opaque ->
1584 let y = y - l.pagedispy in
1585 if y > 0
1586 then
1587 let y = l.pagey + y in
1588 match whatsunder opaque x y with
1589 | Unone -> f rest
1590 | under -> under
1591 else
1592 f rest
1593 | _ ->
1594 f rest
1596 | [] -> Unone
1598 f state.layout
1601 let mouse ~button ~bstate ~x ~y =
1602 match button with
1603 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1604 let incr =
1605 if n = 3
1606 then
1607 -conf.scrollincr
1608 else
1609 conf.scrollincr
1611 let incr = incr * 2 in
1612 let y = clamp incr in
1613 gotoy y
1615 | Glut.LEFT_BUTTON when state.outline = None ->
1616 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1617 begin match dest with
1618 | Ulinkgoto (pageno, top) ->
1619 if pageno >= 0
1620 then
1621 gotopage pageno top
1623 | Ulinkuri s ->
1624 print_endline s
1626 | Unone when bstate = Glut.DOWN ->
1627 Glut.setCursor Glut.CURSOR_INHERIT;
1628 state.mstate <- Mnone
1630 | Unone | Utext _ ->
1631 if bstate = Glut.DOWN
1632 then (
1633 if state.rotate mod 360 = 0 then (
1634 state.mstate <- Msel ((x, y), (x, y));
1635 Glut.postRedisplay ()
1638 else (
1639 match state.mstate with
1640 | Mnone -> ()
1641 | Msel ((x0, y0), (x1, y1)) ->
1642 let f l =
1643 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1644 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1645 then
1646 match getopaque l.pageno with
1647 | Some opaque when validopaque opaque ->
1648 copysel opaque
1649 | _ -> ()
1651 List.iter f state.layout;
1652 copysel ""; (* ugly *)
1653 Glut.setCursor Glut.CURSOR_INHERIT;
1654 state.mstate <- Mnone;
1658 | _ ->
1661 let mouse ~button ~state ~x ~y = mouse button state x y;;
1663 let motion ~x ~y =
1664 if state.outline = None
1665 then
1666 match state.mstate with
1667 | Mnone -> ()
1668 | Msel (a, _) ->
1669 state.mstate <- Msel (a, (x, y));
1670 Glut.postRedisplay ()
1673 let pmotion ~x ~y =
1674 if state.outline = None
1675 then
1676 match state.mstate with
1677 | Mnone ->
1678 begin match getunder x y with
1679 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
1680 | Ulinkuri uri ->
1681 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1682 Glut.setCursor Glut.CURSOR_INFO
1683 | Ulinkgoto (page, y) ->
1684 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
1685 Glut.setCursor Glut.CURSOR_INFO
1686 | Utext s ->
1687 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1688 Glut.setCursor Glut.CURSOR_TEXT
1691 | Msel (a, _) ->
1695 let () =
1696 let statepath =
1697 let home =
1698 if Sys.os_type = "Win32"
1699 then
1700 try Sys.getenv "HOMEPATH" with Not_found -> ""
1701 else
1702 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1704 Filename.concat home "llpp"
1706 let pstate =
1708 let ic = open_in_bin statepath in
1709 let hash = input_value ic in
1710 close_in ic;
1711 hash
1712 with exn ->
1713 if false
1714 then
1715 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1717 Hashtbl.create 1
1719 let savestate () =
1721 let w, h =
1722 match state.fullscreen with
1723 | None -> state.w, state.h
1724 | Some wh -> wh
1726 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1727 let oc = open_out_bin statepath in
1728 output_value oc pstate
1729 with exn ->
1730 if false
1731 then
1732 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1735 let setstate () =
1737 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1738 state.w <- statew;
1739 state.h <- stateh;
1740 state.bookmarks <- statebookmarks;
1741 with Not_found -> ()
1742 | exn ->
1743 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1746 Arg.parse [] (fun s -> state.path <- s) "options:";
1747 let name =
1748 if String.length state.path = 0
1749 then (prerr_endline "filename missing"; exit 1)
1750 else state.path
1753 setstate ();
1754 let _ = Glut.init Sys.argv in
1755 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1756 let () = Glut.initWindowSize state.w state.h in
1757 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1759 let csock, ssock =
1760 if Sys.os_type = "Unix"
1761 then
1762 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1763 else
1764 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1765 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1766 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1767 Unix.bind sock addr;
1768 Unix.listen sock 1;
1769 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1770 Unix.connect csock addr;
1771 let ssock, _ = Unix.accept sock in
1772 Unix.close sock;
1773 let opts sock =
1774 Unix.setsockopt sock Unix.TCP_NODELAY true;
1775 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1777 opts ssock;
1778 opts csock;
1779 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1780 ssock, csock
1783 let () = Glut.displayFunc display in
1784 let () = Glut.reshapeFunc reshape in
1785 let () = Glut.keyboardFunc keyboard in
1786 let () = Glut.specialFunc special in
1787 let () = Glut.idleFunc (Some idle) in
1788 let () = Glut.mouseFunc mouse in
1789 let () = Glut.motionFunc motion in
1790 let () = Glut.passiveMotionFunc pmotion in
1792 init ssock;
1793 state.csock <- csock;
1794 state.ssock <- ssock;
1795 state.text <- "Opening " ^ name;
1796 writecmd csock ("open " ^ name ^ "\000");
1798 at_exit savestate;
1800 let rec handlelablglutbug () =
1802 Glut.mainLoop ();
1803 with Glut.BadEnum "key in special_of_int" ->
1804 showtext '!' " LablGlut bug: special key not recognized";
1805 handlelablglutbug ()
1807 handlelablglutbug ();