pdf_devicergb is now fz_devicergb upstream
[llpp.git] / main.ml
blob9934561fdc825d45b46928163db5b781fff044d3
1 let log fmt = Printf.kprintf prerr_endline fmt;;
2 let dolog fmt = Printf.kprintf prerr_endline fmt;;
4 external init : Unix.file_descr -> unit = "ml_init";;
5 external draw : int -> int -> int -> int -> string -> unit = "ml_draw";;
6 external gettext : string -> (int * int * int * int) -> int -> bool -> unit =
7 "ml_gettext";;
8 external checklink : string -> int -> int -> bool = "ml_checklink";;
9 external getlink : string -> int -> int -> (int * int) option = "ml_getlink";;
10 external highlightlinks : string -> int -> unit = "ml_highlightlinks";;
11 external getpagewh : int -> float array = "ml_getpagewh";;
13 type mstate = Msel of ((int * int) * (int * int)) | Mnone;;
15 type 'a circbuf =
16 { store : 'a array
17 ; mutable rc : int
18 ; mutable wc : int
19 ; mutable len : int
23 type textentry = (char * string * onhist option * onkey * ondone)
24 and onkey = string -> int -> te
25 and ondone = string -> unit
26 and onhist = histcmd -> string
27 and histcmd = HCnext | HCprev | HCfirst | HClast
28 and te =
29 | TEstop
30 | TEdone of string
31 | TEcont of string
32 | TEswitch of textentry
35 let cbnew n v =
36 { store = Array.create n v
37 ; rc = 0
38 ; wc = 0
39 ; len = 0
43 let cblen b = Array.length b.store;;
45 let cbput b v =
46 let len = cblen b in
47 b.store.(b.wc) <- v;
48 b.wc <- (b.wc + 1) mod len;
49 b.len <- min (b.len + 1) len;
52 let cbpeekw b = b.store.(b.wc);;
54 let cbget b dir =
55 if b.len = 0 then b.store.(0) else
56 let rc = b.rc + dir in
57 let rc = if rc = -1 then b.len - 1 else rc in
58 let rc = if rc = b.len then 0 else rc in
59 b.rc <- rc;
60 b.store.(rc);
63 let cbrfollowlen b =
64 b.rc <- b.len;
67 type layout =
68 { pageno : int
69 ; pagedimno : int
70 ; pagew : int
71 ; pageh : int
72 ; pagedispy : int
73 ; pagey : int
74 ; pagevh : int
78 type conf =
79 { mutable scrollw : int
80 ; mutable scrollh : int
81 ; mutable rectsel : bool
82 ; mutable icase : bool
83 ; mutable preload : bool
84 ; mutable pagebias : int
85 ; mutable redispimm : bool
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 : int
113 ; mutable prevy : int
114 ; mutable maxy : int
115 ; mutable layout : layout list
116 ; pagemap : ((int * int * int), string) Hashtbl.t
117 ; mutable pages : (int * int * int) list
118 ; mutable pagecount : int
119 ; pagecache : string circbuf
120 ; mutable inflight : int
121 ; mutable mstate : mstate
122 ; mutable searchpattern : string
123 ; mutable rects : (int * int * rect) list
124 ; mutable rects1 : (int * int * rect) list
125 ; mutable text : string
126 ; mutable fullscreen : (int * int) option
127 ; mutable textentry : textentry option
128 ; mutable outlines : outlines
129 ; mutable outline : (bool * int * int * outline array * string) option
130 ; mutable bookmarks : outline list
131 ; mutable path : string
132 ; mutable invalidated : int
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 ; rectsel = true
147 ; preload = false
148 ; pagebias = 0
149 ; redispimm = false
150 ; verbose = false
151 ; scrollincr = 24
152 ; maxhfit = true
153 ; crophack = false
154 ; autoscroll = false
155 ; showall = false
156 ; hlinks = false
160 let state =
161 { csock = Unix.stdin
162 ; ssock = Unix.stdin
163 ; w = 900
164 ; h = 900
165 ; rotate = 0
166 ; y = 0
167 ; ty = 0
168 ; prevy = 0
169 ; layout = []
170 ; maxy = max_int
171 ; pagemap = Hashtbl.create 10
172 ; pagecache = cbnew 10 ""
173 ; pages = []
174 ; pagecount = 0
175 ; inflight = 0
176 ; mstate = Mnone
177 ; rects = []
178 ; rects1 = []
179 ; text = ""
180 ; fullscreen = None
181 ; textentry = None
182 ; searchpattern = ""
183 ; outlines = Olist []
184 ; outline = None
185 ; bookmarks = []
186 ; path = ""
187 ; invalidated = 0
188 ; hists =
189 { nav = cbnew 100 0.0
190 ; pat = cbnew 20 ""
191 ; pag = cbnew 10 ""
196 let vlog fmt =
197 if conf.verbose
198 then
199 Printf.kprintf prerr_endline fmt
200 else
201 Printf.kprintf ignore fmt
204 let writecmd fd s =
205 let len = String.length s in
206 let n = 4 + len in
207 let b = Buffer.create n in
208 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
209 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
210 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
211 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
212 Buffer.add_string b s;
213 let s' = Buffer.contents b in
214 let n' = Unix.write fd s' 0 n in
215 if n' != n then failwith "write failed";
218 let readcmd fd =
219 let s = "xxxx" in
220 let n = Unix.read fd s 0 4 in
221 if n != 4 then failwith "incomplete read(len)";
222 let len = 0
223 lor (Char.code s.[0] lsl 24)
224 lor (Char.code s.[1] lsl 16)
225 lor (Char.code s.[2] lsl 8)
226 lor (Char.code s.[3] lsl 0)
228 let s = String.create len in
229 let n = Unix.read fd s 0 len in
230 if n != len then failwith "incomplete read(data)";
234 let yratio y =
235 if y = state.maxy then 1.0
236 else float y /. float state.maxy
239 let makecmd s l =
240 let b = Buffer.create 10 in
241 Buffer.add_string b s;
242 let rec combine = function
243 | [] -> b
244 | x :: xs ->
245 Buffer.add_char b ' ';
246 let s =
247 match x with
248 | `b b -> if b then "1" else "0"
249 | `s s -> s
250 | `i i -> string_of_int i
251 | `f f -> string_of_float f
252 | `I f -> string_of_int (truncate f)
254 Buffer.add_string b s;
255 combine xs;
257 combine l;
260 let wcmd s l =
261 let cmd = Buffer.contents (makecmd s l) in
262 writecmd state.csock cmd;
265 let calcheight () =
266 let rec f pn ph fh l =
267 match l with
268 | (n, _, h) :: rest ->
269 let fh = fh + (n - pn) * ph in
270 f n h fh rest
272 | [] ->
273 let fh = fh + (ph * (state.pagecount - pn)) in
274 max 0 fh
276 let fh = f 0 0 0 state.pages in
280 let getpagey pageno =
281 let rec f pn ph y l =
282 match l with
283 | (n, _, h) :: rest ->
284 if n >= pageno
285 then
286 y + (pageno - pn) * ph
287 else
288 let y = y + (n - pn) * ph in
289 f n h y rest
291 | [] ->
292 y + (pageno - pn) * ph
294 f 0 0 0 state.pages;
297 let layout y sh =
298 let rec f pageno pdimno prev vy py dy l cacheleft accu =
299 if pageno = state.pagecount || cacheleft = 0
300 then accu
301 else
302 let ((_, w, h) as curr), rest, pdimno =
303 match l with
304 | ((pageno', _, _) as curr) :: rest when pageno' = pageno ->
305 curr, rest, pdimno + 1
306 | _ ->
307 prev, l, pdimno
309 let pageno' = pageno + 1 in
310 if py + h > vy
311 then
312 let py' = vy - py in
313 let vh = h - py' in
314 if dy + vh > sh
315 then
316 let vh = sh - dy in
317 if vh <= 0
318 then
319 accu
320 else
321 let e =
322 { pageno = pageno
323 ; pagedimno = pdimno
324 ; pagew = w
325 ; pageh = h
326 ; pagedispy = dy
327 ; pagey = py'
328 ; pagevh = vh
331 e :: accu
332 else
333 let e =
334 { pageno = pageno
335 ; pagedimno = pdimno
336 ; pagew = w
337 ; pageh = h
338 ; pagedispy = dy
339 ; pagey = py'
340 ; pagevh = vh
343 let accu = e :: accu in
344 f pageno' pdimno curr
345 (vy + vh) (py + h) (dy + vh + 2) rest
346 (pred cacheleft) accu
347 else
348 f pageno' pdimno curr vy (py + h) dy rest cacheleft accu
350 if state.invalidated = 0
351 then
352 let accu = f 0 ~-1 (0,0,0) y 0 0 state.pages (cblen state.pagecache) [] in
353 state.maxy <- calcheight ();
354 List.rev accu
355 else
359 let clamp incr =
360 let y = state.y + incr in
361 let y = max 0 y in
362 let y = min y (state.maxy - (if conf.maxhfit then state.h else 0)) in
366 let getopaque pageno =
367 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w - conf.scrollw,
368 state.rotate))
369 with Not_found -> None
372 let cache pageno opaque =
373 Hashtbl.replace state.pagemap (pageno + 1, state.w - conf.scrollw,
374 state.rotate) opaque
377 let validopaque opaque = String.length opaque > 0;;
379 let preload l =
380 match getopaque l.pageno with
381 | None when state.inflight < 2+0*(cblen state.pagecache) ->
382 state.inflight <- succ state.inflight;
383 cache l.pageno "";
384 wcmd "render" [`i (l.pageno + 1)
385 ;`i l.pagedimno
386 ;`i l.pagew
387 ;`i l.pageh];
389 | _ -> ()
392 let gotoy y =
393 let y = max 0 y in
394 let y = min state.maxy y in
395 let pages = layout y state.h in
396 let rec f all = function
397 | l :: ls ->
398 begin match getopaque l.pageno with
399 | None -> preload l; f false ls
400 | Some opaque -> f (all && validopaque opaque) ls
402 | [] -> all
404 if not conf.showall || f true pages
405 then (
406 state.y <- y;
407 state.layout <- pages;
409 state.ty <- y;
410 if conf.redispimm
411 then
412 Glut.postRedisplay ()
416 let addnav () =
417 cbput state.hists.nav (yratio state.y);
418 cbrfollowlen state.hists.nav;
421 let getnav () =
422 let y = cbget state.hists.nav ~-1 in
423 truncate (y *. float state.maxy)
426 let gotopage n top =
427 let y = getpagey n in
428 addnav ();
429 gotoy (y + top);
432 let invalidate () =
433 state.layout <- [];
434 state.pages <- [];
435 state.rects <- [];
436 state.rects1 <- [];
437 state.invalidated <- state.invalidated + 1;
440 let reshape ~w ~h =
441 let ratio = float w /. float state.w in
442 let fixbookmark (s, l, pageno, pagey) =
443 let pagey = truncate (float pagey *. ratio) in
444 (s, l, pageno, pagey)
446 state.bookmarks <- List.map fixbookmark state.bookmarks;
447 state.w <- w;
448 state.h <- h;
449 GlDraw.viewport 0 0 w h;
450 GlMat.mode `modelview;
451 GlMat.load_identity ();
452 GlMat.mode `projection;
453 GlMat.load_identity ();
454 GlMat.rotate ~x:1.0 ~angle:180.0 ();
455 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
456 GlMat.scale3 (2.0 /. float w, 2.0 /. float state.h, 1.0);
457 GlClear.color (1., 1., 1.);
458 GlClear.clear [`color];
459 invalidate ();
460 wcmd "geometry" [`i (state.w - conf.scrollw); `i h];
463 let showtext c s =
464 GlDraw.color (0.0, 0.0, 0.0);
465 GlDraw.rect
466 (0.0, float (state.h - 18))
467 (float (state.w - conf.scrollw - 1), float state.h)
469 let font = Glut.BITMAP_8_BY_13 in
470 GlDraw.color (1.0, 1.0, 1.0);
471 GlPix.raster_pos ~x:0.0 ~y:(float (state.h - 5)) ();
472 Glut.bitmapCharacter ~font ~c:(Char.code c);
473 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
476 let enttext () =
477 let len = String.length state.text in
478 match state.textentry with
479 | None ->
480 if len > 0 then showtext ' ' state.text
482 | Some (c, text, _, _, _) ->
483 let s =
484 if len > 0
485 then
486 text ^ " [" ^ state.text ^ "]"
487 else
488 text
490 showtext c s;
493 let act cmd =
494 match cmd.[0] with
495 | 'c' ->
496 state.pages <- [];
497 state.outlines <- Olist []
499 | 'D' ->
500 state.rects <- state.rects1;
501 Glut.postRedisplay ()
503 | 'd' ->
504 state.rects <- state.rects1;
505 Glut.postRedisplay ()
507 | 'C' ->
508 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
509 state.pagecount <- n;
510 state.invalidated <- state.invalidated - 1;
511 if state.invalidated = 0
512 then (
513 let rely = yratio state.y in
514 let maxy = calcheight () in
515 state.y <- truncate (float maxy *. rely);
516 state.ty <- state.y;
517 let pages = layout state.y state.h in
518 state.layout <- pages;
519 Glut.postRedisplay ();
522 | 't' ->
523 let s = Scanf.sscanf cmd "t %n"
524 (fun n -> String.sub cmd n (String.length cmd - n))
526 Glut.setWindowTitle s
528 | 'T' ->
529 let s = Scanf.sscanf cmd "T %n"
530 (fun n -> String.sub cmd n (String.length cmd - n))
532 if state.textentry = None
533 then (
534 state.text <- s;
535 showtext ' ' s;
536 Glut.swapBuffers ();
538 else (
539 state.text <- s;
540 Glut.postRedisplay ();
543 | 'V' ->
544 if conf.verbose
545 then
546 let s = Scanf.sscanf cmd "V %n"
547 (fun n -> String.sub cmd n (String.length cmd - n))
549 state.text <- s;
550 showtext ' ' s;
551 Glut.swapBuffers ();
553 | 'F' ->
554 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
555 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
556 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
557 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
559 let y = (getpagey pageno) + truncate y0 in
560 addnav ();
561 gotoy y;
562 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
564 | 'R' ->
565 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
566 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
567 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
568 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
570 state.rects1 <-
571 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
573 | 'r' ->
574 let n, w, h, r, p =
575 Scanf.sscanf cmd "r %d %d %d %d %s"
576 (fun n w h r p -> (n, w, h, r, p))
578 Hashtbl.replace state.pagemap (n, w, r) p;
579 let evicted = cbpeekw state.pagecache in
580 if String.length evicted > 0
581 then begin
582 wcmd "free" [`s evicted];
583 let l = Hashtbl.fold (fun k p a ->
584 if evicted = p then k :: a else a) state.pagemap []
586 List.iter (fun k -> Hashtbl.remove state.pagemap k) l;
587 end;
588 cbput state.pagecache p;
589 state.inflight <- pred state.inflight;
590 if conf.showall then gotoy state.ty;
591 Glut.postRedisplay ()
593 | 'l' ->
594 let (n, w, h) as pagelayout =
595 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
597 state.pages <- pagelayout :: state.pages
599 | 'o' ->
600 let (l, n, t, pos) =
601 Scanf.sscanf cmd "o %d %d %d %n" (fun l n t pos -> l, n, t, pos)
603 let s = String.sub cmd pos (String.length cmd - pos) in
604 let outline = (s, l, n, t) in
605 let outlines =
606 match state.outlines with
607 | Olist outlines -> Olist (outline :: outlines)
608 | Oarray _ -> Olist [outline]
609 | Onarrow _ -> Olist [outline]
611 state.outlines <- outlines
613 | _ ->
614 log "unknown cmd `%S'" cmd
617 let idle () =
618 if not conf.redispimm && state.y != state.prevy
619 then (
620 state.prevy <- state.y;
621 Glut.postRedisplay ();
623 else
624 let r, _, _ = Unix.select [state.csock] [] [] 0.001 in
626 begin match r with
627 | [] ->
628 if conf.preload then begin
629 let h = state.h in
630 let y = if state.y < state.h then 0 else state.y - state.h in
631 let pages = layout y (h*3) in
632 List.iter preload pages;
633 end;
634 if conf.autoscroll then begin
635 let y = state.y + conf.scrollincr in
636 let y = if y >= state.maxy then 0 else y in
637 gotoy y;
638 state.text <- "";
639 state.prevy <- state.y;
640 Glut.postRedisplay ();
641 end;
643 | _ ->
644 let cmd = readcmd state.csock in
645 act cmd;
646 end;
649 let onhist cb = function
650 | HCprev -> cbget cb ~-1
651 | HCnext -> cbget cb 1
652 | HCfirst -> cbget cb ~-(cb.rc)
653 | HClast -> cbget cb (cb.len - 1 - cb.rc)
656 let search pattern forward =
657 if String.length pattern > 0
658 then
659 let pn, py =
660 match state.layout with
661 | [] -> 0, 0
662 | l :: _ ->
663 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
665 let cmd =
666 let b = makecmd "search"
667 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
669 Buffer.add_char b ',';
670 Buffer.add_string b pattern;
671 Buffer.add_char b '\000';
672 Buffer.contents b;
674 writecmd state.csock cmd;
677 let intentry text key =
678 let c = Char.unsafe_chr key in
679 match c with
680 | '0' .. '9' ->
681 let s = "x" in s.[0] <- c;
682 let text = text ^ s in
683 TEcont text
685 | _ ->
686 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
687 TEcont text
690 let addchar s c =
691 let b = Buffer.create (String.length s + 1) in
692 Buffer.add_string b s;
693 Buffer.add_char b c;
694 Buffer.contents b;
697 let textentry text key =
698 let c = Char.unsafe_chr key in
699 match c with
700 | _ when key >= 32 && key < 127 ->
701 let text = addchar text c in
702 TEcont text
704 | _ ->
705 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
706 TEcont text
709 let rotate angle =
710 state.rotate <- angle;
711 invalidate ();
712 wcmd "rotate" [`i angle];
715 let optentry text key =
716 let btos b = if b then "on" else "off" in
717 let c = Char.unsafe_chr key in
718 match c with
719 | 'r' ->
720 conf.rectsel <- not conf.rectsel;
721 TEdone ("rectsel " ^ (btos conf.rectsel))
723 | 's' ->
724 let ondone s =
725 try conf.scrollincr <- int_of_string s with exc ->
726 state.text <- Printf.sprintf "bad integer `%s': %s"
727 s (Printexc.to_string exc)
729 TEswitch ('#', "", None, intentry, ondone)
731 | 'R' ->
732 let ondone s =
733 match try
734 Some (int_of_string s)
735 with exc ->
736 state.text <- Printf.sprintf "bad integer `%s': %s"
737 s (Printexc.to_string exc);
738 None
739 with
740 | Some angle -> rotate angle
741 | None -> ()
743 TEswitch ('^', "", None, intentry, ondone)
745 | 'i' ->
746 conf.icase <- not conf.icase;
747 TEdone ("case insensitive search " ^ (btos conf.icase))
749 | 'p' ->
750 conf.preload <- not conf.preload;
751 TEdone ("preload " ^ (btos conf.preload))
753 | 'd' ->
754 conf.redispimm <- not conf.redispimm;
755 TEdone ("immediate redisplay " ^ (btos conf.redispimm))
757 | 'v' ->
758 conf.verbose <- not conf.verbose;
759 TEdone ("verbose " ^ (btos conf.verbose))
761 | 'h' ->
762 conf.maxhfit <- not conf.maxhfit;
763 state.maxy <- state.maxy + (if conf.maxhfit then -state.h else state.h);
764 TEdone ("maxhfit " ^ (btos conf.maxhfit))
766 | 'c' ->
767 conf.crophack <- not conf.crophack;
768 TEdone ("crophack " ^ btos conf.crophack)
770 | 'a' ->
771 conf.showall <- not conf.showall;
772 TEdone ("showall " ^ btos conf.showall)
774 | _ ->
775 state.text <- Printf.sprintf "bad option %d `%c'" key c;
776 TEstop
779 let maxoutlinerows () = (state.h - 31) / 16;;
781 let enterselector allowdel outlines errmsg =
782 if Array.length outlines = 0
783 then (
784 showtext ' ' errmsg;
785 Glut.swapBuffers ()
787 else
788 let pageno =
789 match state.layout with
790 | [] -> -1
791 | {pageno=pageno} :: rest -> pageno
793 let active =
794 let rec loop n =
795 if n = Array.length outlines
796 then 0
797 else
798 let (_, _, outlinepageno, _) = outlines.(n) in
799 if outlinepageno >= pageno then n else loop (n+1)
801 loop 0
803 state.outline <-
804 Some (allowdel, active,
805 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
806 Glut.postRedisplay ();
809 let enteroutlinemode () =
810 let outlines =
811 match state.outlines with
812 | Oarray a -> a
813 | Olist l ->
814 let a = Array.of_list (List.rev l) in
815 state.outlines <- Oarray a;
817 | Onarrow (a, b) -> a
819 enterselector false outlines "Documents has no outline";
822 let enterbookmarkmode () =
823 let bookmarks = Array.of_list state.bookmarks in
824 enterselector true bookmarks "Documents has no bookmarks (yet)";
828 let quickbookmark ?title () =
829 match state.layout with
830 | [] -> ()
831 | l :: _ ->
832 let title =
833 match title with
834 | None ->
835 let sec = Unix.gettimeofday () in
836 let tm = Unix.localtime sec in
837 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
838 l.pageno
839 tm.Unix.tm_mday
840 tm.Unix.tm_mon
841 (tm.Unix.tm_year + 1900)
842 tm.Unix.tm_hour
843 tm.Unix.tm_min
844 | Some title -> title
846 state.bookmarks <-
847 (title, 0, l.pageno, l.pagey) :: state.bookmarks
850 let viewkeyboard ~key ~x ~y =
851 let enttext te =
852 state.textentry <- te;
853 state.text <- "";
854 enttext ();
855 Glut.postRedisplay ()
857 match state.textentry with
858 | None ->
859 let c = Char.chr key in
860 begin match c with
861 | '\027' | 'q' ->
862 exit 0
864 | '\008' ->
865 let y = getnav () in
866 gotoy y
868 | 'o' ->
869 enteroutlinemode ()
871 | 'u' ->
872 state.rects <- [];
873 state.text <- "";
874 Glut.postRedisplay ()
876 | '/' | '?' ->
877 let ondone isforw s =
878 cbput state.hists.pat s;
879 cbrfollowlen state.hists.pat;
880 state.searchpattern <- s;
881 search s isforw
883 enttext (Some (c, "", Some (onhist state.hists.pat),
884 textentry, ondone (c ='/')))
886 | '+' ->
887 let ondone s =
888 let n =
889 try int_of_string s with exc ->
890 state.text <- Printf.sprintf "bad integer `%s': %s"
891 s (Printexc.to_string exc);
892 max_int
894 if n != max_int
895 then (
896 conf.pagebias <- n;
897 state.text <- "page bias is now " ^ string_of_int n;
900 enttext (Some ('+', "", None, intentry, ondone))
902 | '-' ->
903 let ondone msg =
904 state.text <- msg;
906 enttext (Some ('-', "", None, optentry, ondone))
908 | '0' .. '9' ->
909 let ondone s =
910 let n =
911 try int_of_string s with exc ->
912 state.text <- Printf.sprintf "bad integer `%s': %s"
913 s (Printexc.to_string exc);
916 if n >= 0
917 then (
918 addnav ();
919 cbput state.hists.pag (string_of_int n);
920 cbrfollowlen state.hists.pag;
921 gotoy (getpagey (n + conf.pagebias - 1))
924 let pageentry text key =
925 match Char.unsafe_chr key with
926 | 'g' -> TEdone text
927 | _ -> intentry text key
929 let text = "x" in text.[0] <- c;
930 enttext (Some (':', text, Some (onhist state.hists.pag),
931 pageentry, ondone))
933 | 'b' ->
934 conf.scrollw <- if conf.scrollw > 0 then 0 else 5;
935 reshape state.w state.h;
937 | 'l' ->
938 conf.hlinks <- not conf.hlinks;
939 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
940 Glut.postRedisplay ()
942 | 'a' ->
943 conf.autoscroll <- not conf.autoscroll
945 | 'f' ->
946 begin match state.fullscreen with
947 | None ->
948 state.fullscreen <- Some (state.w, state.h);
949 Glut.fullScreen ()
950 | Some (w, h) ->
951 state.fullscreen <- None;
952 Glut.reshapeWindow ~w ~h
955 | 'g' ->
956 gotoy 0
958 | 'n' ->
959 search state.searchpattern true
961 | 'p' | 'N' ->
962 search state.searchpattern false
964 | 't' ->
965 begin match state.layout with
966 | [] -> ()
967 | l :: _ ->
968 gotoy (state.y - l.pagey);
971 | ' ' ->
972 begin match List.rev state.layout with
973 | [] -> ()
974 | l :: _ ->
975 gotoy (clamp (l.pageh - l.pagey))
978 | '\127' ->
979 begin match state.layout with
980 | [] -> ()
981 | l :: _ ->
982 gotoy (clamp (-l.pageh));
985 | '=' ->
986 let f (fn, ln) l =
987 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
989 let fn, ln = List.fold_left f (-1, -1) state.layout in
990 let s =
991 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
992 let percent =
993 if maxy <= 0
994 then 100.
995 else (100. *. (float state.y /. float maxy)) in
996 if fn = ln
997 then
998 Printf.sprintf "Page %d of %d %.2f%%"
999 (fn+1) state.pagecount percent
1000 else
1001 Printf.sprintf
1002 "Pages %d-%d of %d %.2f%%"
1003 (fn+1) (ln+1) state.pagecount percent
1005 showtext ' ' s;
1006 Glut.swapBuffers ()
1008 | 'w' ->
1009 begin match state.layout with
1010 | [] -> ()
1011 | l :: _ ->
1012 Glut.reshapeWindow (l.pagew + conf.scrollw) l.pageh;
1013 Glut.postRedisplay ();
1016 | '\'' ->
1017 enterbookmarkmode ()
1019 | 'm' ->
1020 let ondone s =
1021 match state.layout with
1022 | l :: _ ->
1023 state.bookmarks <- (s, 0, l.pageno, l.pagey) :: state.bookmarks
1024 | _ -> ()
1026 enttext (Some ('~', "", None, textentry, ondone))
1028 | '~' ->
1029 quickbookmark ();
1030 showtext ' ' "Quick bookmark added";
1031 Glut.swapBuffers ()
1033 | 'z' ->
1034 begin match state.layout with
1035 | l :: _ ->
1036 let a = getpagewh l.pagedimno in
1037 let w, h =
1038 if conf.crophack
1039 then
1040 (truncate (1.8 *. (a.(1) -. a.(0))),
1041 truncate (1.2 *. (a.(3) -. a.(0))))
1042 else
1043 (truncate (a.(1) -. a.(0)),
1044 truncate (a.(3) -. a.(0)))
1046 Glut.reshapeWindow (w + conf.scrollw) h;
1047 Glut.postRedisplay ();
1049 | [] -> ()
1052 | '<' | '>' ->
1053 rotate (state.rotate + (if c = '>' then 30 else -30));
1055 | _ ->
1056 vlog "huh? %d %c" key (Char.chr key);
1059 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1060 let len = String.length text in
1061 if len = 0
1062 then (
1063 state.textentry <- None;
1064 Glut.postRedisplay ();
1066 else (
1067 let s = String.sub text 0 (len - 1) in
1068 enttext (Some (c, s, onhist, onkey, ondone))
1071 | Some (c, text, onhist, onkey, ondone) ->
1072 begin match Char.unsafe_chr key with
1073 | '\r' | '\n' ->
1074 ondone text;
1075 state.textentry <- None;
1076 Glut.postRedisplay ()
1078 | '\027' ->
1079 state.textentry <- None;
1080 Glut.postRedisplay ()
1082 | _ ->
1083 begin match onkey text key with
1084 | TEdone text ->
1085 state.textentry <- None;
1086 ondone text;
1087 Glut.postRedisplay ()
1089 | TEcont text ->
1090 enttext (Some (c, text, onhist, onkey, ondone));
1092 | TEstop ->
1093 state.textentry <- None;
1094 Glut.postRedisplay ()
1096 | TEswitch te ->
1097 state.textentry <- Some te;
1098 Glut.postRedisplay ()
1099 end;
1100 end;
1103 let narrow outlines pattern =
1104 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1105 match reopt with
1106 | None -> None
1107 | Some re ->
1108 let rec fold accu n =
1109 if n = -1 then accu else
1110 let (s, _, _, _) as o = outlines.(n) in
1111 let accu =
1112 if (try ignore (Str.search_forward re s 0); true
1113 with Not_found -> false)
1114 then (o :: accu)
1115 else accu
1117 fold accu (n-1)
1119 let matched = fold [] (Array.length outlines - 1) in
1120 if matched = [] then None else Some (Array.of_list matched)
1123 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1124 let search active pattern incr =
1125 let dosearch re =
1126 let rec loop n =
1127 if n = Array.length outlines || n = -1 then None else
1128 let (s, _, _, _) = outlines.(n) in
1130 (try ignore (Str.search_forward re s 0); true
1131 with Not_found -> false)
1132 then Some n
1133 else loop (n + incr)
1135 loop active
1138 let re = Str.regexp_case_fold pattern in
1139 dosearch re
1140 with Failure s ->
1141 state.text <- s;
1142 None
1144 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1145 match key with
1146 | 27 ->
1147 if String.length qsearch = 0
1148 then (
1149 state.text <- "";
1150 state.outline <- None;
1151 Glut.postRedisplay ();
1153 else (
1154 state.text <- "";
1155 state.outline <- Some (allowdel, active, first, outlines, "");
1156 Glut.postRedisplay ();
1159 | 18 | 19 ->
1160 let incr = if key = 18 then -1 else 1 in
1161 let active, first =
1162 match search (active + incr) qsearch incr with
1163 | None ->
1164 state.text <- qsearch ^ " [not found]";
1165 active, first
1166 | Some active ->
1167 state.text <- qsearch;
1168 active, firstof active
1170 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1171 Glut.postRedisplay ();
1173 | 8 ->
1174 let len = String.length qsearch in
1175 if len = 0
1176 then ()
1177 else (
1178 if len = 1
1179 then (
1180 state.text <- "";
1181 state.outline <- Some (allowdel, active, first, outlines, "");
1183 else
1184 let qsearch = String.sub qsearch 0 (len - 1) in
1185 let active, first =
1186 match search active qsearch ~-1 with
1187 | None ->
1188 state.text <- qsearch ^ " [not found]";
1189 active, first
1190 | Some active ->
1191 state.text <- qsearch;
1192 active, firstof active
1194 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1196 Glut.postRedisplay ()
1198 | 13 ->
1199 if active < Array.length outlines
1200 then (
1201 let (_, _, n, t) = outlines.(active) in
1202 gotopage n t;
1204 state.text <- "";
1205 if allowdel then state.bookmarks <- Array.to_list outlines;
1206 state.outline <- None;
1207 Glut.postRedisplay ();
1209 | _ when key >= 32 && key < 127 ->
1210 let pattern = addchar qsearch (Char.chr key) in
1211 let active, first =
1212 match search active pattern 1 with
1213 | None ->
1214 state.text <- pattern ^ " [not found]";
1215 active, first
1216 | Some active ->
1217 state.text <- pattern;
1218 active, firstof active
1220 state.outline <- Some (allowdel, active, first, outlines, pattern);
1221 Glut.postRedisplay ()
1223 | 14 when not allowdel ->
1224 let optoutlines = narrow outlines qsearch in
1225 begin match optoutlines with
1226 | None -> state.text <- "can't narrow"
1227 | Some outlines ->
1228 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1229 match state.outlines with
1230 | Olist l -> ()
1231 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1232 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1233 end;
1234 Glut.postRedisplay ()
1236 | 21 when not allowdel ->
1237 let outline =
1238 match state.outlines with
1239 | Oarray a -> a
1240 | Olist l ->
1241 let a = Array.of_list (List.rev l) in
1242 state.outlines <- Oarray a;
1244 | Onarrow (a, b) ->
1245 state.outlines <- Oarray b;
1248 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1249 Glut.postRedisplay ()
1251 | 12 ->
1252 state.outline <-
1253 Some (allowdel, active, firstof active, outlines, qsearch);
1254 Glut.postRedisplay ()
1256 | 127 when allowdel ->
1257 let len = Array.length outlines - 1 in
1258 if len = 0
1259 then (
1260 state.outline <- None;
1261 state.bookmarks <- [];
1263 else (
1264 let bookmarks = Array.init len
1265 (fun i ->
1266 let i = if i >= active then i + 1 else i in
1267 outlines.(i)
1270 state.outline <-
1271 Some (allowdel,
1272 min active (len-1),
1273 min first (len-1),
1274 bookmarks, qsearch)
1277 Glut.postRedisplay ()
1279 | _ -> log "unknown key %d" key
1282 let keyboard ~key ~x ~y =
1283 if key = 7
1284 then
1285 wcmd "interrupt" []
1286 else
1287 match state.outline with
1288 | None -> viewkeyboard ~key ~x ~y
1289 | Some outline -> outlinekeyboard ~key ~x ~y outline
1292 let special ~key ~x ~y =
1293 match state.outline with
1294 | None ->
1295 begin match state.textentry with
1296 | None ->
1297 let y =
1298 match key with
1299 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1300 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1301 | Glut.KEY_DOWN -> clamp conf.scrollincr
1302 | Glut.KEY_PAGE_UP -> clamp (-state.h)
1303 | Glut.KEY_PAGE_DOWN -> clamp state.h
1304 | Glut.KEY_HOME -> addnav (); 0
1305 | Glut.KEY_END ->
1306 addnav ();
1307 state.maxy - (if conf.maxhfit then state.h else 0)
1308 | _ -> state.y
1310 state.text <- "";
1311 gotoy y
1313 | Some (c, s, Some onhist, onkey, ondone) ->
1314 let s =
1315 match key with
1316 | Glut.KEY_UP -> onhist HCprev
1317 | Glut.KEY_DOWN -> onhist HCnext
1318 | Glut.KEY_HOME -> onhist HCfirst
1319 | Glut.KEY_END -> onhist HClast
1320 | _ -> state.text
1322 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1323 Glut.postRedisplay ()
1325 | _ -> ()
1328 | Some (allowdel, active, first, outlines, qsearch) ->
1329 let maxrows = maxoutlinerows () in
1330 let navigate incr =
1331 let active = active + incr in
1332 let active = max 0 (min active (Array.length outlines - 1)) in
1333 let first =
1334 if active > first
1335 then
1336 let rows = active - first in
1337 if rows > maxrows then active - maxrows else first
1338 else active
1340 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1341 Glut.postRedisplay ()
1343 match key with
1344 | Glut.KEY_UP -> navigate ~-1
1345 | Glut.KEY_DOWN -> navigate 1
1346 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1347 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1349 | Glut.KEY_HOME ->
1350 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1351 Glut.postRedisplay ()
1353 | Glut.KEY_END ->
1354 let active = Array.length outlines - 1 in
1355 let first = max 0 (active - maxrows) in
1356 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1357 Glut.postRedisplay ()
1359 | _ -> ()
1362 let drawplaceholder l =
1363 GlDraw.color (1.0, 1.0, 1.0);
1364 GlDraw.rect
1365 (0.0, float l.pagedispy)
1366 (float l.pagew, float (l.pagedispy + l.pagevh))
1368 let x = 0.0
1369 and y = float (l.pagedispy + 13) in
1370 let font = Glut.BITMAP_8_BY_13 in
1371 GlDraw.color (0.0, 0.0, 0.0);
1372 GlPix.raster_pos ~x ~y ();
1373 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1374 ("Loading " ^ string_of_int l.pageno);
1377 let now () = Unix.gettimeofday ();;
1379 let drawpage i l =
1380 begin match getopaque l.pageno with
1381 | Some opaque when validopaque opaque ->
1382 if state.textentry = None
1383 then GlDraw.color (1.0, 1.0, 1.0)
1384 else GlDraw.color (0.4, 0.4, 0.4);
1385 let a = now () in
1386 draw l.pagedispy l.pagew l.pagevh l.pagey opaque;
1387 let b = now () in
1388 let d = b-.a in
1389 if conf.hlinks then highlightlinks opaque (l.pagedispy - l.pagey);
1390 vlog "draw %f sec" d;
1392 | Some _ ->
1393 drawplaceholder l
1395 | None ->
1396 drawplaceholder l;
1397 if state.inflight < cblen state.pagecache
1398 then (
1399 List.iter preload state.layout;
1401 else (
1402 vlog "inflight %d" state.inflight;
1404 end;
1405 GlDraw.color (0.5, 0.5, 0.5);
1406 GlDraw.rect
1407 (0., float i)
1408 (float (state.w - conf.scrollw), float (i + (l.pagedispy - i)))
1410 l.pagedispy + l.pagevh;
1413 let scrollindicator () =
1414 let maxy = state.maxy - (if conf.maxhfit then state.h else 0) in
1415 GlDraw.color (0.64 , 0.64, 0.64);
1416 GlDraw.rect
1417 (float (state.w - conf.scrollw), 0.)
1418 (float state.w, float state.h)
1420 GlDraw.color (0.0, 0.0, 0.0);
1421 let sh = (float (maxy + state.h) /. float state.h) in
1422 let sh = float state.h /. sh in
1423 let sh = max sh (float conf.scrollh) in
1425 let percent =
1426 if state.y = state.maxy
1427 then 1.0
1428 else float state.y /. float maxy
1430 let position = (float state.h -. sh) *. percent in
1432 let position =
1433 if position +. sh > float state.h
1434 then
1435 float state.h -. sh
1436 else
1437 position
1439 GlDraw.rect
1440 (float (state.w - conf.scrollw), position)
1441 (float state.w, position +. sh)
1445 let showsel () =
1446 match state.mstate with
1447 | Mnone ->
1450 | Msel ((x0, y0), (x1, y1)) ->
1451 let y0' = min y0 y1
1452 and y1 = max y0 y1 in
1453 let y0 = y0' in
1454 let f l =
1455 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1456 || ((y1 >= l.pagedispy)) (* && y1 <= (dy + vh))) *)
1457 then
1458 match getopaque l.pageno with
1459 | Some opaque when validopaque opaque ->
1460 let oy = -l.pagey + l.pagedispy in
1461 gettext opaque (min x0 x1, y0, max x1 x0, y1) oy conf.rectsel
1462 | _ -> ()
1464 List.iter f state.layout
1467 let showrects () =
1468 Gl.enable `blend;
1469 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1470 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1471 List.iter
1472 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1473 List.iter (fun l ->
1474 if l.pageno = pageno
1475 then (
1476 let d = float (l.pagedispy - l.pagey) in
1477 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1478 GlDraw.begins `quads;
1480 GlDraw.vertex2 (x0, y0+.d);
1481 GlDraw.vertex2 (x1, y1+.d);
1482 GlDraw.vertex2 (x2, y2+.d);
1483 GlDraw.vertex2 (x3, y3+.d);
1485 GlDraw.ends ();
1486 (* GlDraw.rect (x0, y0 +. d) (x1, y1 +. d) *)
1488 ) state.layout
1489 ) state.rects
1491 Gl.disable `blend;
1494 let showoutline = function
1495 | None -> ()
1496 | Some (allowdel, active, first, outlines, qsearch) ->
1497 Gl.enable `blend;
1498 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1499 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1500 GlDraw.rect (0., 0.) (float state.w, float state.h);
1501 Gl.disable `blend;
1503 GlDraw.color (1., 1., 1.);
1504 let font = Glut.BITMAP_9_BY_15 in
1505 let draw_string x y s =
1506 GlPix.raster_pos ~x ~y ();
1507 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1509 let rec loop row =
1510 if row = Array.length outlines || (row - first) * 16 > state.h
1511 then ()
1512 else (
1513 let (s, l, _, _) = outlines.(row) in
1514 let y = (row - first) * 16 in
1515 let x = 5 + 15*l in
1516 if row = active
1517 then (
1518 Gl.enable `blend;
1519 GlDraw.polygon_mode `both `line;
1520 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1521 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1522 GlDraw.rect (0., float (y + 1))
1523 (float (state.w - conf.scrollw - 1), float (y + 18));
1524 GlDraw.polygon_mode `both `fill;
1525 Gl.disable `blend;
1526 GlDraw.color (1., 1., 1.);
1528 draw_string (float x) (float (y + 16)) s;
1529 loop (row+1)
1532 loop first
1535 let display () =
1536 let lasty = List.fold_left drawpage 0 (state.layout) in
1537 GlDraw.color (0.5, 0.5, 0.5);
1538 GlDraw.rect
1539 (0., float lasty)
1540 (float (state.w - conf.scrollw), float state.h)
1542 showrects ();
1543 scrollindicator ();
1544 showsel ();
1545 showoutline state.outline;
1546 enttext ();
1547 Glut.swapBuffers ();
1550 let getlink x y =
1551 let rec f = function
1552 | l :: rest ->
1553 begin match getopaque l.pageno with
1554 | Some opaque when validopaque opaque ->
1555 let y = y - l.pagedispy in
1556 if y > 0
1557 then
1558 let y = l.pagey + y in
1559 match getlink opaque x y with
1560 | None -> f rest
1561 | some -> some
1562 else
1563 f rest
1564 | _ ->
1565 f rest
1567 | [] -> None
1569 f state.layout
1572 let checklink x y =
1573 let rec f = function
1574 | l :: rest ->
1575 begin match getopaque l.pageno with
1576 | Some opaque when validopaque opaque ->
1577 let y = y - l.pagedispy in
1578 if y > 0
1579 then
1580 let y = l.pagey + y in
1581 if checklink opaque x y then true else f rest
1582 else
1583 f rest
1584 | _ ->
1585 f rest
1587 | [] -> false
1589 f state.layout
1592 let mouse ~button ~bstate ~x ~y =
1593 match button with
1594 | Glut.OTHER_BUTTON n when n == 3 || n == 4 && bstate = Glut.UP ->
1595 let incr =
1596 if n = 3
1597 then
1598 -conf.scrollincr
1599 else
1600 conf.scrollincr
1602 let incr = incr * 2 in
1603 let y = clamp incr in
1604 gotoy y
1606 | Glut.LEFT_BUTTON when state.outline = None ->
1607 let dest = if bstate = Glut.DOWN then getlink x y else None in
1608 begin match dest with
1609 | Some (pageno, top) ->
1610 if pageno >= 0
1611 then
1612 gotopage pageno top
1614 | None ->
1615 if bstate = Glut.DOWN
1616 then (
1617 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1618 state.mstate <- Msel ((x, y), (x, y));
1619 Glut.postRedisplay ()
1621 else (
1622 Glut.setCursor Glut.CURSOR_INHERIT;
1623 state.mstate <- Mnone;
1627 | _ ->
1630 let mouse ~button ~state ~x ~y = mouse button state x y;;
1632 let motion ~x ~y =
1633 if state.outline = None
1634 then
1635 match state.mstate with
1636 | Mnone -> ()
1637 | Msel (a, _) ->
1638 state.mstate <- Msel (a, (x, y));
1639 Glut.postRedisplay ()
1642 let pmotion ~x ~y =
1643 if state.outline = None
1644 then
1645 match state.mstate with
1646 | Mnone when (checklink x y) ->
1647 Glut.setCursor Glut.CURSOR_INFO
1649 | Mnone ->
1650 Glut.setCursor Glut.CURSOR_INHERIT
1652 | Msel (a, _) ->
1656 let () =
1657 let statepath =
1658 let home =
1659 if Sys.os_type = "Win32"
1660 then
1661 try Sys.getenv "HOMEPATH" with Not_found -> ""
1662 else
1663 try Filename.concat (Sys.getenv "HOME") ".config" with Not_found -> ""
1665 Filename.concat home "llpp"
1667 let pstate =
1669 let ic = open_in_bin statepath in
1670 let hash = input_value ic in
1671 close_in ic;
1672 hash
1673 with exn ->
1674 if false
1675 then
1676 prerr_endline ("Error loading state " ^ Printexc.to_string exn)
1678 Hashtbl.create 1
1680 let savestate () =
1682 let w, h =
1683 match state.fullscreen with
1684 | None -> state.w, state.h
1685 | Some wh -> wh
1687 Hashtbl.replace pstate state.path (state.bookmarks, w, h);
1688 let oc = open_out_bin statepath in
1689 output_value oc pstate
1690 with exn ->
1691 if false
1692 then
1693 prerr_endline ("Error saving state " ^ Printexc.to_string exn)
1696 let setstate () =
1698 let statebookmarks, statew, stateh = Hashtbl.find pstate state.path in
1699 state.w <- statew;
1700 state.h <- stateh;
1701 state.bookmarks <- statebookmarks;
1702 with Not_found -> ()
1703 | exn ->
1704 prerr_endline ("Error setting state " ^ Printexc.to_string exn)
1707 Arg.parse [] (fun s -> state.path <- s) "options:";
1708 let name =
1709 if String.length state.path = 0
1710 then (prerr_endline "filename missing"; exit 1)
1711 else state.path
1714 setstate ();
1715 let _ = Glut.init Sys.argv in
1716 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
1717 let () = Glut.initWindowSize state.w state.h in
1718 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
1720 let csock, ssock =
1721 if Sys.os_type = "Unix"
1722 then
1723 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
1724 else
1725 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
1726 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1727 Unix.setsockopt sock Unix.SO_REUSEADDR true;
1728 Unix.bind sock addr;
1729 Unix.listen sock 1;
1730 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
1731 Unix.connect csock addr;
1732 let ssock, _ = Unix.accept sock in
1733 Unix.close sock;
1734 let opts sock =
1735 Unix.setsockopt sock Unix.TCP_NODELAY true;
1736 Unix.setsockopt_optint sock Unix.SO_LINGER None;
1738 opts ssock;
1739 opts csock;
1740 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
1741 ssock, csock
1744 let () = Glut.displayFunc display in
1745 let () = Glut.reshapeFunc reshape in
1746 let () = Glut.keyboardFunc keyboard in
1747 let () = Glut.specialFunc special in
1748 let () = Glut.idleFunc (Some idle) in
1749 let () = Glut.mouseFunc mouse in
1750 let () = Glut.motionFunc motion in
1751 let () = Glut.passiveMotionFunc pmotion in
1753 init ssock;
1754 state.csock <- csock;
1755 state.ssock <- ssock;
1756 writecmd csock ("open " ^ name ^ "\000");
1758 at_exit savestate;
1760 let rec handlelablglutbug () =
1762 Glut.mainLoop ();
1763 with Glut.BadEnum "key in special_of_int" ->
1764 showtext '!' " LablGlut bug: special key not recognized";
1765 Glut.swapBuffers ();
1766 handlelablglutbug ()
1768 handlelablglutbug ();