Do not narrow to empty string
[llpp.git] / main.ml
blob2bf6d63ed72bfcdfd99b4c7c10770c16e16a0201
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 * bool) -> 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 getpdimrect : int -> float array = "ml_getpdimrect";;
17 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
19 type mpos = int * int
20 and mstate =
21 | Msel of (mpos * mpos)
22 | Mpan of mpos
23 | Mscroll
24 | Mnone
27 type 'a circbuf =
28 { store : 'a array
29 ; mutable rc : int
30 ; mutable wc : int
31 ; mutable len : int
35 type textentry = (char * string * onhist option * onkey * ondone)
36 and onkey = string -> int -> te
37 and ondone = string -> unit
38 and onhist = histcmd -> string
39 and histcmd = HCnext | HCprev | HCfirst | HClast
40 and te =
41 | TEstop
42 | TEdone of string
43 | TEcont of string
44 | TEswitch of textentry
47 let cbnew n v =
48 { store = Array.create n v
49 ; rc = 0
50 ; wc = 0
51 ; len = 0
55 let cblen b = Array.length b.store;;
57 let cbput b v =
58 let len = cblen b in
59 b.store.(b.wc) <- v;
60 b.wc <- (b.wc + 1) mod len;
61 b.len <- min (b.len + 1) len;
64 let cbpeekw b = b.store.(b.wc);;
66 let cbget b dir =
67 if b.len = 0
68 then b.store.(0)
69 else
70 let rc = b.rc + dir in
71 let rc = if rc = -1 then b.len - 1 else rc in
72 let rc = if rc = b.len then 0 else rc in
73 b.rc <- rc;
74 b.store.(rc);
77 let cbrfollowlen b =
78 b.rc <- b.len;
81 let cbclear b v =
82 b.len <- 0;
83 Array.fill b.store 0 (Array.length b.store) v;
86 type layout =
87 { pageno : int
88 ; pagedimno : int
89 ; pagew : int
90 ; pageh : int
91 ; pagedispy : int
92 ; pagey : int
93 ; pagevh : int
97 type conf =
98 { mutable scrollw : int
99 ; mutable scrollh : int
100 ; mutable icase : bool
101 ; mutable preload : bool
102 ; mutable pagebias : int
103 ; mutable verbose : bool
104 ; mutable scrollincr : int
105 ; mutable maxhfit : bool
106 ; mutable crophack : bool
107 ; mutable autoscroll : bool
108 ; mutable showall : bool
109 ; mutable hlinks : bool
110 ; mutable underinfo : bool
111 ; mutable interpagespace : int
112 ; mutable zoom : float
113 ; mutable presentation : bool
114 ; mutable angle : int
115 ; mutable winw : int
116 ; mutable winh : int
117 ; mutable savebmarks : bool
121 type outline = string * int * int * float;;
122 type outlines =
123 | Oarray of outline array
124 | Olist of outline list
125 | Onarrow of string * outline array * outline array
128 type rect = (float * float * float * float * float * float * float * float);;
130 type state =
131 { mutable csock : Unix.file_descr
132 ; mutable ssock : Unix.file_descr
133 ; mutable w : int
134 ; mutable x : int
135 ; mutable y : int
136 ; mutable ty : float
137 ; mutable maxy : int
138 ; mutable layout : layout list
139 ; pagemap : ((int * int * int), string) Hashtbl.t
140 ; mutable pdims : (int * int * int) list
141 ; mutable pagecount : int
142 ; pagecache : string circbuf
143 ; mutable rendering : bool
144 ; mutable mstate : mstate
145 ; mutable searchpattern : string
146 ; mutable rects : (int * int * rect) list
147 ; mutable rects1 : (int * int * rect) list
148 ; mutable text : string
149 ; mutable fullscreen : (int * int) option
150 ; mutable textentry : textentry option
151 ; mutable outlines : outlines
152 ; mutable outline : (bool * int * int * outline array * string) option
153 ; mutable bookmarks : outline list
154 ; mutable path : string
155 ; mutable password : string
156 ; mutable invalidated : int
157 ; mutable colorscale : float
158 ; hists : hists
160 and hists =
161 { pat : string circbuf
162 ; pag : string circbuf
163 ; nav : float circbuf
167 let conf =
168 { scrollw = 7
169 ; scrollh = 12
170 ; icase = true
171 ; preload = true
172 ; pagebias = 0
173 ; verbose = false
174 ; scrollincr = 24
175 ; maxhfit = true
176 ; crophack = false
177 ; autoscroll = false
178 ; showall = false
179 ; hlinks = false
180 ; underinfo = false
181 ; interpagespace = 2
182 ; zoom = 1.0
183 ; presentation = false
184 ; angle = 0
185 ; winw = 900
186 ; winh = 900
187 ; savebmarks = true
191 let defconf = { conf with angle=conf.angle };;
193 let state =
194 { csock = Unix.stdin
195 ; ssock = Unix.stdin
196 ; w = 0
197 ; y = 0
198 ; x = 0
199 ; ty = 0.0
200 ; layout = []
201 ; maxy = max_int
202 ; pagemap = Hashtbl.create 10
203 ; pagecache = cbnew 10 ""
204 ; pdims = []
205 ; pagecount = 0
206 ; rendering = false
207 ; mstate = Mnone
208 ; rects = []
209 ; rects1 = []
210 ; text = ""
211 ; fullscreen = None
212 ; textentry = None
213 ; searchpattern = ""
214 ; outlines = Olist []
215 ; outline = None
216 ; bookmarks = []
217 ; path = ""
218 ; password = ""
219 ; invalidated = 0
220 ; hists =
221 { nav = cbnew 100 0.0
222 ; pat = cbnew 20 ""
223 ; pag = cbnew 10 ""
225 ; colorscale = 1.0
229 let vlog fmt =
230 if conf.verbose
231 then
232 Printf.kprintf prerr_endline fmt
233 else
234 Printf.kprintf ignore fmt
237 let writecmd fd s =
238 let len = String.length s in
239 let n = 4 + len in
240 let b = Buffer.create n in
241 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
242 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
243 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
244 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
245 Buffer.add_string b s;
246 let s' = Buffer.contents b in
247 let n' = Unix.write fd s' 0 n in
248 if n' != n then failwith "write failed";
251 let readcmd fd =
252 let s = "xxxx" in
253 let n = Unix.read fd s 0 4 in
254 if n != 4 then failwith "incomplete read(len)";
255 let len = 0
256 lor (Char.code s.[0] lsl 24)
257 lor (Char.code s.[1] lsl 16)
258 lor (Char.code s.[2] lsl 8)
259 lor (Char.code s.[3] lsl 0)
261 let s = String.create len in
262 let n = Unix.read fd s 0 len in
263 if n != len then failwith "incomplete read(data)";
267 let yratio y =
268 if y = state.maxy
269 then 1.0
270 else float y /. float state.maxy
273 let makecmd s l =
274 let b = Buffer.create 10 in
275 Buffer.add_string b s;
276 let rec combine = function
277 | [] -> b
278 | x :: xs ->
279 Buffer.add_char b ' ';
280 let s =
281 match x with
282 | `b b -> if b then "1" else "0"
283 | `s s -> s
284 | `i i -> string_of_int i
285 | `f f -> string_of_float f
286 | `I f -> string_of_int (truncate f)
288 Buffer.add_string b s;
289 combine xs;
291 combine l;
294 let wcmd s l =
295 let cmd = Buffer.contents (makecmd s l) in
296 writecmd state.csock cmd;
299 let calcips h =
300 if conf.presentation
301 then
302 let d = conf.winh - h in
303 max 0 ((d + 1) / 2)
304 else
305 conf.interpagespace
308 let calcheight () =
309 let rec f pn ph pi fh l =
310 match l with
311 | (n, _, h) :: rest ->
312 let ips = calcips h in
313 let fh =
314 if conf.presentation
315 then fh+ips
316 else fh
318 let fh = fh + ((n - pn) * (ph + pi)) in
319 f n h ips fh rest
321 | [] ->
322 let inc =
323 if conf.presentation
324 then 0
325 else -pi
327 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
328 max 0 fh
330 let fh = f 0 0 0 0 state.pdims in
334 let getpageyh pageno =
335 let rec f pn ph pi y l =
336 match l with
337 | (n, _, h) :: rest ->
338 let ips = calcips h in
339 if n >= pageno
340 then
341 if conf.presentation && n = pageno
342 then
343 y + (pageno - pn) * (ph + pi) + pi, h
344 else
345 y + (pageno - pn) * (ph + pi), h
346 else
347 let y = y + (if conf.presentation then pi else 0) in
348 let y = y + (n - pn) * (ph + pi) in
349 f n h ips y rest
351 | [] ->
352 y + (pageno - pn) * (ph + pi), ph
354 f 0 0 0 0 state.pdims
357 let getpagey pageno = fst (getpageyh pageno);;
359 let layout y sh =
360 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
361 let ((w, h, ips) as curr), rest, pdimno, yinc =
362 match pdims with
363 | (pageno', w, h) :: rest when pageno' = pageno ->
364 let ips = calcips h in
365 let yinc = if conf.presentation then ips else 0 in
366 (w, h, ips), rest, pdimno + 1, yinc
367 | _ ->
368 prev, pdims, pdimno, 0
370 let dy = dy + yinc in
371 let py = py + yinc in
372 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
373 then
374 accu
375 else
376 let vy = y + dy in
377 if py + h <= vy - yinc
378 then
379 let py = py + h + ips in
380 let dy = max 0 (py - y) in
381 f ~pageno:(pageno+1)
382 ~pdimno
383 ~prev:curr
386 ~pdims:rest
387 ~cacheleft
388 ~accu
389 else
390 let pagey = vy - py in
391 let pagevh = h - pagey in
392 let pagevh = min (sh - dy) pagevh in
393 let off = if yinc > 0 then py - vy else 0
395 let py = py + h + ips in
396 let e =
397 { pageno = pageno
398 ; pagedimno = pdimno
399 ; pagew = w
400 ; pageh = h
401 ; pagedispy = dy + off
402 ; pagey = pagey + off
403 ; pagevh = pagevh - off
406 let accu = e :: accu in
407 f ~pageno:(pageno+1)
408 ~pdimno
409 ~prev:curr
411 ~dy:(dy+pagevh+ips)
412 ~pdims:rest
413 ~cacheleft:(cacheleft-1)
414 ~accu
416 if state.invalidated = 0
417 then (
418 let accu =
420 ~pageno:0
421 ~pdimno:~-1
422 ~prev:(0,0,0)
423 ~py:0
424 ~dy:0
425 ~pdims:state.pdims
426 ~cacheleft:(cblen state.pagecache)
427 ~accu:[]
429 List.rev accu
431 else
435 let clamp incr =
436 let y = state.y + incr in
437 let y = max 0 y in
438 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
442 let getopaque pageno =
443 try Some (Hashtbl.find state.pagemap (pageno + 1, state.w, conf.angle))
444 with Not_found -> None
447 let cache pageno opaque =
448 Hashtbl.replace state.pagemap (pageno + 1, state.w, conf.angle) opaque
451 let validopaque opaque = String.length opaque > 0;;
453 let render l =
454 match getopaque l.pageno with
455 | None when not state.rendering ->
456 state.rendering <- true;
457 cache l.pageno "";
458 wcmd "render" [`i (l.pageno + 1)
459 ;`i l.pagedimno
460 ;`i l.pagew
461 ;`i l.pageh];
463 | _ -> ()
466 let loadlayout layout =
467 let rec f all = function
468 | l :: ls ->
469 begin match getopaque l.pageno with
470 | None -> render l; f false ls
471 | Some opaque -> f (all && validopaque opaque) ls
473 | [] -> all
475 f (layout <> []) layout;
478 let preload () =
479 if conf.preload
480 then
481 let evictedvisible =
482 let evictedopaque = cbpeekw state.pagecache in
483 List.exists (fun l ->
484 match getopaque l.pageno with
485 | Some opaque when validopaque opaque ->
486 evictedopaque = opaque
487 | otherwise -> false
488 ) state.layout
490 if not evictedvisible
491 then
492 let rely = yratio state.y in
493 let presentation = conf.presentation in
494 let interpagespace = conf.interpagespace in
495 let maxy = state.maxy in
496 conf.presentation <- false;
497 conf.interpagespace <- 0;
498 state.maxy <- calcheight ();
499 let y = truncate (float state.maxy *. rely) in
500 let y = if y < conf.winh then 0 else y - conf.winh in
501 let pages = layout y (conf.winh*3) in
502 List.iter render pages;
503 conf.presentation <- presentation;
504 conf.interpagespace <- interpagespace;
505 state.maxy <- maxy;
508 let gotoy y =
509 let y = max 0 y in
510 let y = min state.maxy y in
511 let pages = layout y conf.winh in
512 let ready = loadlayout pages in
513 state.ty <- yratio y;
514 if conf.showall
515 then (
516 if ready
517 then (
518 state.layout <- pages;
519 state.y <- y;
520 Glut.postRedisplay ();
523 else (
524 state.layout <- pages;
525 state.y <- y;
526 Glut.postRedisplay ();
528 preload ();
531 let gotoy_and_clear_text y =
532 gotoy y;
533 if not conf.verbose then state.text <- "";
536 let addnav () =
537 cbput state.hists.nav (yratio state.y);
538 cbrfollowlen state.hists.nav;
541 let getnav () =
542 let y = cbget state.hists.nav ~-1 in
543 truncate (y *. float state.maxy)
546 let gotopage n top =
547 let y, h = getpageyh n in
548 addnav ();
549 gotoy_and_clear_text (y + (truncate (top *. float h)));
552 let gotopage1 n top =
553 let y = getpagey n in
554 addnav ();
555 gotoy_and_clear_text (y + top);
558 let invalidate () =
559 state.layout <- [];
560 state.pdims <- [];
561 state.rects <- [];
562 state.rects1 <- [];
563 state.invalidated <- state.invalidated + 1;
566 let scalecolor c =
567 let c = c *. state.colorscale in
568 (c, c, c);
571 let represent () =
572 let y =
573 match state.layout with
574 | [] ->
575 let rely = yratio state.y in
576 state.maxy <- calcheight ();
577 truncate (float state.maxy *. rely)
579 | l :: _ ->
580 state.maxy <- calcheight ();
581 getpagey l.pageno
583 gotoy y
586 let pagematrix () =
587 GlMat.mode `projection;
588 GlMat.load_identity ();
589 GlMat.rotate ~x:1.0 ~angle:180.0 ();
590 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
591 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
594 let winmatrix () =
595 GlMat.mode `projection;
596 GlMat.load_identity ();
597 GlMat.rotate ~x:1.0 ~angle:180.0 ();
598 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
599 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
602 let reshape ~w ~h =
603 conf.winw <- w;
604 let w = truncate (float w *. conf.zoom) - conf.scrollw in
605 state.w <- w;
606 conf.winh <- h;
607 GlMat.mode `modelview;
608 GlMat.load_identity ();
609 GlClear.color (scalecolor 1.0);
610 GlClear.clear [`color];
612 invalidate ();
613 wcmd "geometry" [`i w; `i h];
616 let showtext c s =
617 GlDraw.color (0.0, 0.0, 0.0);
618 GlDraw.rect
619 (0.0, float (conf.winh - 18))
620 (float (conf.winw - conf.scrollw - 1), float conf.winh)
622 let font = Glut.BITMAP_8_BY_13 in
623 GlDraw.color (1.0, 1.0, 1.0);
624 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
625 Glut.bitmapCharacter ~font ~c:(Char.code c);
626 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
629 let enttext () =
630 let len = String.length state.text in
631 match state.textentry with
632 | None ->
633 if len > 0 then showtext ' ' state.text
635 | Some (c, text, _, _, _) ->
636 let s =
637 if len > 0
638 then
639 text ^ " [" ^ state.text ^ "]"
640 else
641 text
643 showtext c s;
646 let showtext c s =
647 if true
648 then (
649 state.text <- Printf.sprintf "%c%s" c s;
650 Glut.postRedisplay ();
652 else (
653 showtext c s;
654 Glut.swapBuffers ();
658 let act cmd =
659 match cmd.[0] with
660 | 'c' ->
661 state.pdims <- [];
663 | 'D' ->
664 state.rects <- state.rects1;
665 Glut.postRedisplay ()
667 | 'C' ->
668 let n = Scanf.sscanf cmd "C %d" (fun n -> n) in
669 state.pagecount <- n;
670 state.invalidated <- state.invalidated - 1;
671 if state.invalidated = 0
672 then represent ()
674 | 't' ->
675 let s = Scanf.sscanf cmd "t %n"
676 (fun n -> String.sub cmd n (String.length cmd - n))
678 Glut.setWindowTitle s
680 | 'T' ->
681 let s = Scanf.sscanf cmd "T %n"
682 (fun n -> String.sub cmd n (String.length cmd - n))
684 if state.textentry = None
685 then (
686 state.text <- s;
687 showtext ' ' s;
689 else (
690 state.text <- s;
691 Glut.postRedisplay ();
694 | 'V' ->
695 if conf.verbose
696 then
697 let s = Scanf.sscanf cmd "V %n"
698 (fun n -> String.sub cmd n (String.length cmd - n))
700 state.text <- s;
701 showtext ' ' s;
703 | 'F' ->
704 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
705 Scanf.sscanf cmd "F %d %d %f %f %f %f %f %f %f %f"
706 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
707 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
709 let y = (getpagey pageno) + truncate y0 in
710 addnav ();
711 gotoy y;
712 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
714 | 'R' ->
715 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
716 Scanf.sscanf cmd "R %d %d %f %f %f %f %f %f %f %f"
717 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
718 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
720 state.rects1 <-
721 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
723 | 'r' ->
724 let n, w, h, r, p =
725 Scanf.sscanf cmd "r %d %d %d %d %s"
726 (fun n w h r p -> (n, w, h, r, p))
728 Hashtbl.replace state.pagemap (n, w, r) p;
729 let opaque = cbpeekw state.pagecache in
730 if validopaque opaque
731 then (
732 let k =
733 Hashtbl.fold
734 (fun k v a -> if v = opaque then k else a)
735 state.pagemap (-1, -1, -1)
737 wcmd "free" [`s opaque];
738 Hashtbl.remove state.pagemap k
740 cbput state.pagecache p;
741 state.rendering <- false;
742 if conf.showall
743 then gotoy (truncate (ceil (state.ty *. float state.maxy)))
744 else (
745 let visible = List.exists (fun l -> l.pageno + 1 = n) state.layout in
746 if visible
747 then gotoy state.y
748 else (ignore (loadlayout state.layout); preload ())
751 | 'l' ->
752 let (n, w, h) as pdim =
753 Scanf.sscanf cmd "l %d %d %d" (fun n w h -> n, w, h)
755 state.pdims <- pdim :: state.pdims
757 | 'o' ->
758 let (l, n, t, h, pos) =
759 Scanf.sscanf cmd "o %d %d %d %d %n" (fun l n t h pos -> l, n, t, h, pos)
761 let s = String.sub cmd pos (String.length cmd - pos) in
762 let s =
763 let l = String.length s in
764 let b = Buffer.create (String.length s) in
765 let rec loop pc2 i =
766 if i = l
767 then ()
768 else
769 let pc2 =
770 match s.[i] with
771 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
772 | '\xc2' -> true
773 | c ->
774 let c = if Char.code c land 0x80 = 0 then c else '?' in
775 Buffer.add_char b c;
776 false
778 loop pc2 (i+1)
780 loop false 0;
781 Buffer.contents b
783 let outline = (s, l, n, float t /. float h) in
784 let outlines =
785 match state.outlines with
786 | Olist outlines -> Olist (outline :: outlines)
787 | Oarray _ -> Olist [outline]
788 | Onarrow _ -> Olist [outline]
790 state.outlines <- outlines
792 | _ ->
793 log "unknown cmd `%S'" cmd
796 let now = Unix.gettimeofday;;
798 let idle () =
799 let rec loop delay =
800 let r, _, _ = Unix.select [state.csock] [] [] delay in
801 begin match r with
802 | [] ->
803 if conf.autoscroll
804 then begin
805 let y = state.y + conf.scrollincr in
806 let y = if y >= state.maxy then 0 else y in
807 gotoy y;
808 state.text <- "";
809 end;
811 | _ ->
812 let cmd = readcmd state.csock in
813 act cmd;
814 loop 0.0
815 end;
816 in loop 0.001
819 let onhist cb = function
820 | HCprev -> cbget cb ~-1
821 | HCnext -> cbget cb 1
822 | HCfirst -> cbget cb ~-(cb.rc)
823 | HClast -> cbget cb (cb.len - 1 - cb.rc)
826 let search pattern forward =
827 if String.length pattern > 0
828 then
829 let pn, py =
830 match state.layout with
831 | [] -> 0, 0
832 | l :: _ ->
833 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
835 let cmd =
836 let b = makecmd "search"
837 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
839 Buffer.add_char b ',';
840 Buffer.add_string b pattern;
841 Buffer.add_char b '\000';
842 Buffer.contents b;
844 writecmd state.csock cmd;
847 let intentry text key =
848 let c = Char.unsafe_chr key in
849 match c with
850 | '0' .. '9' ->
851 let s = "x" in s.[0] <- c;
852 let text = text ^ s in
853 TEcont text
855 | _ ->
856 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
857 TEcont text
860 let addchar s c =
861 let b = Buffer.create (String.length s + 1) in
862 Buffer.add_string b s;
863 Buffer.add_char b c;
864 Buffer.contents b;
867 let textentry text key =
868 let c = Char.unsafe_chr key in
869 match c with
870 | _ when key >= 32 && key < 127 ->
871 let text = addchar text c in
872 TEcont text
874 | _ ->
875 log "unhandled key %d char `%c'" key (Char.unsafe_chr key);
876 TEcont text
879 let rotate angle =
880 conf.angle <- angle;
881 invalidate ();
882 wcmd "rotate" [`i angle];
885 let optentry text key =
886 let btos b = if b then "on" else "off" in
887 let c = Char.unsafe_chr key in
888 match c with
889 | 's' ->
890 let ondone s =
891 try conf.scrollincr <- int_of_string s with exc ->
892 state.text <- Printf.sprintf "bad integer `%s': %s"
893 s (Printexc.to_string exc)
895 TEswitch ('#', "", None, intentry, ondone)
897 | 'R' ->
898 let ondone s =
899 match try
900 Some (int_of_string s)
901 with exc ->
902 state.text <- Printf.sprintf "bad integer `%s': %s"
903 s (Printexc.to_string exc);
904 None
905 with
906 | Some angle -> rotate angle
907 | None -> ()
909 TEswitch ('^', "", None, intentry, ondone)
911 | 'i' ->
912 conf.icase <- not conf.icase;
913 TEdone ("case insensitive search " ^ (btos conf.icase))
915 | 'p' ->
916 conf.preload <- not conf.preload;
917 gotoy state.y;
918 TEdone ("preload " ^ (btos conf.preload))
920 | 'v' ->
921 conf.verbose <- not conf.verbose;
922 TEdone ("verbose " ^ (btos conf.verbose))
924 | 'h' ->
925 conf.maxhfit <- not conf.maxhfit;
926 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
927 TEdone ("maxhfit " ^ (btos conf.maxhfit))
929 | 'c' ->
930 conf.crophack <- not conf.crophack;
931 TEdone ("crophack " ^ btos conf.crophack)
933 | 'a' ->
934 conf.showall <- not conf.showall;
935 TEdone ("showall " ^ btos conf.showall)
937 | 'f' ->
938 conf.underinfo <- not conf.underinfo;
939 TEdone ("underinfo " ^ btos conf.underinfo)
941 | 'P' ->
942 conf.savebmarks <- not conf.savebmarks;
943 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
945 | 'S' ->
946 let ondone s =
948 conf.interpagespace <- int_of_string s;
949 let rely = yratio state.y in
950 state.maxy <- calcheight ();
951 gotoy (truncate (float state.maxy *. rely));
952 with exc ->
953 state.text <- Printf.sprintf "bad integer `%s': %s"
954 s (Printexc.to_string exc)
956 TEswitch ('%', "", None, intentry, ondone)
958 | _ ->
959 state.text <- Printf.sprintf "bad option %d `%c'" key c;
960 TEstop
963 let maxoutlinerows () = (conf.winh - 31) / 16;;
965 let enterselector allowdel outlines errmsg msg =
966 if Array.length outlines = 0
967 then (
968 showtext ' ' errmsg;
970 else (
971 state.text <- msg;
972 Glut.setCursor Glut.CURSOR_INHERIT;
973 let pageno =
974 match state.layout with
975 | [] -> -1
976 | {pageno=pageno} :: rest -> pageno
978 let active =
979 let rec loop n =
980 if n = Array.length outlines
981 then 0
982 else
983 let (_, _, outlinepageno, _) = outlines.(n) in
984 if outlinepageno >= pageno then n else loop (n+1)
986 loop 0
988 state.outline <-
989 Some (allowdel, active,
990 max 0 ((active - maxoutlinerows () / 2)), outlines, "");
991 Glut.postRedisplay ();
995 let enteroutlinemode () =
996 let outlines, msg =
997 match state.outlines with
998 | Oarray a -> a, ""
999 | Olist l ->
1000 let a = Array.of_list (List.rev l) in
1001 state.outlines <- Oarray a;
1002 a, ""
1003 | Onarrow (pat, a, b) ->
1004 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1006 enterselector false outlines "Document has no outline" msg;
1009 let enterbookmarkmode () =
1010 let bookmarks = Array.of_list state.bookmarks in
1011 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1014 let quickbookmark ?title () =
1015 match state.layout with
1016 | [] -> ()
1017 | l :: _ ->
1018 let title =
1019 match title with
1020 | None ->
1021 let sec = Unix.gettimeofday () in
1022 let tm = Unix.localtime sec in
1023 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
1024 l.pageno
1025 tm.Unix.tm_mday
1026 tm.Unix.tm_mon
1027 (tm.Unix.tm_year + 1900)
1028 tm.Unix.tm_hour
1029 tm.Unix.tm_min
1030 | Some title -> title
1032 state.bookmarks <-
1033 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1036 let doreshape w h =
1037 state.fullscreen <- None;
1038 Glut.reshapeWindow w h;
1041 let opendoc path password =
1042 invalidate ();
1043 state.path <- path;
1044 state.password <- password;
1045 Hashtbl.clear state.pagemap;
1047 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1048 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1049 wcmd "geometry" [`i state.w; `i conf.winh];
1052 let viewkeyboard ~key ~x ~y =
1053 let enttext te =
1054 state.textentry <- te;
1055 state.text <- "";
1056 enttext ();
1057 Glut.postRedisplay ()
1059 match state.textentry with
1060 | None ->
1061 let c = Char.chr key in
1062 begin match c with
1063 | '\027' | 'q' ->
1064 exit 0
1066 | '\008' ->
1067 let y = getnav () in
1068 gotoy_and_clear_text y
1070 | 'o' ->
1071 enteroutlinemode ()
1073 | 'u' ->
1074 state.rects <- [];
1075 state.text <- "";
1076 Glut.postRedisplay ()
1078 | '/' | '?' ->
1079 let ondone isforw s =
1080 cbput state.hists.pat s;
1081 cbrfollowlen state.hists.pat;
1082 state.searchpattern <- s;
1083 search s isforw
1085 enttext (Some (c, "", Some (onhist state.hists.pat),
1086 textentry, ondone (c ='/')))
1088 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1089 conf.zoom <- min 2.2 (conf.zoom +. 0.1);
1090 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1091 reshape conf.winw conf.winh
1093 | '+' ->
1094 let ondone s =
1095 let n =
1096 try int_of_string s with exc ->
1097 state.text <- Printf.sprintf "bad integer `%s': %s"
1098 s (Printexc.to_string exc);
1099 max_int
1101 if n != max_int
1102 then (
1103 conf.pagebias <- n;
1104 state.text <- "page bias is now " ^ string_of_int n;
1107 enttext (Some ('+', "", None, intentry, ondone))
1109 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1110 conf.zoom <- max 0.1 (conf.zoom -. 0.1);
1111 if conf.zoom <= 1.0 then state.x <- 0;
1112 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1113 reshape conf.winw conf.winh;
1115 | '-' ->
1116 let ondone msg =
1117 state.text <- msg;
1119 enttext (Some ('-', "", None, optentry, ondone))
1121 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1122 state.x <- 0;
1123 conf.zoom <- 1.0;
1124 state.text <- "zoom is 100%";
1125 reshape conf.winw conf.winh
1127 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1128 let n =
1129 let rec find n maxh nformaxh = function
1130 | (_, _, h) :: rest ->
1131 if h > maxh
1132 then find (n+1) h n rest
1133 else find (n+1) maxh nformaxh rest
1134 | [] -> nformaxh
1136 find 0 0 0 state.pdims
1139 let rect = getpdimrect n in
1140 let pw = rect.(1) -. rect.(0) in
1141 let ph = rect.(3) -. rect.(2) in
1143 let num = (float conf.winh *. pw) +. (ph *. float conf.scrollw) in
1144 let den = ph *. float conf.winw in
1145 let zoom = num /. den in
1147 if zoom < 1.0
1148 then (
1149 conf.zoom <- zoom;
1150 state.x <- 0;
1151 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1152 reshape conf.winw conf.winh;
1155 | '0' .. '9' ->
1156 let ondone s =
1157 let n =
1158 try int_of_string s with exc ->
1159 state.text <- Printf.sprintf "bad integer `%s': %s"
1160 s (Printexc.to_string exc);
1163 if n >= 0
1164 then (
1165 addnav ();
1166 cbput state.hists.pag (string_of_int n);
1167 cbrfollowlen state.hists.pag;
1168 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1171 let pageentry text key =
1172 match Char.unsafe_chr key with
1173 | 'g' -> TEdone text
1174 | _ -> intentry text key
1176 let text = "x" in text.[0] <- c;
1177 enttext (Some (':', text, Some (onhist state.hists.pag),
1178 pageentry, ondone))
1180 | 'b' ->
1181 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1182 reshape conf.winw conf.winh;
1184 | 'l' ->
1185 conf.hlinks <- not conf.hlinks;
1186 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1187 Glut.postRedisplay ()
1189 | 'a' ->
1190 conf.autoscroll <- not conf.autoscroll
1192 | 'P' ->
1193 conf.presentation <- not conf.presentation;
1194 showtext ' ' ("presentation mode " ^
1195 if conf.presentation then "on" else "off");
1196 represent ()
1198 | 'f' ->
1199 begin match state.fullscreen with
1200 | None ->
1201 state.fullscreen <- Some (conf.winw, conf.winh);
1202 Glut.fullScreen ()
1203 | Some (w, h) ->
1204 state.fullscreen <- None;
1205 doreshape w h
1208 | 'g' ->
1209 gotoy_and_clear_text 0
1211 | 'n' ->
1212 search state.searchpattern true
1214 | 'p' | 'N' ->
1215 search state.searchpattern false
1217 | 't' ->
1218 begin match state.layout with
1219 | [] -> ()
1220 | l :: _ ->
1221 gotoy_and_clear_text (getpagey l.pageno)
1224 | ' ' ->
1225 begin match List.rev state.layout with
1226 | [] -> ()
1227 | l :: _ ->
1228 let pageno = min (l.pageno+1) (state.pagecount-1) in
1229 gotoy_and_clear_text (getpagey pageno)
1232 | '\127' ->
1233 begin match state.layout with
1234 | [] -> ()
1235 | l :: _ ->
1236 let pageno = max 0 (l.pageno-1) in
1237 gotoy_and_clear_text (getpagey pageno)
1240 | '=' ->
1241 let f (fn, ln) l =
1242 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1244 let fn, ln = List.fold_left f (-1, -1) state.layout in
1245 let s =
1246 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1247 let percent =
1248 if maxy <= 0
1249 then 100.
1250 else (100. *. (float state.y /. float maxy)) in
1251 if fn = ln
1252 then
1253 Printf.sprintf "Page %d of %d %.2f%%"
1254 (fn+1) state.pagecount percent
1255 else
1256 Printf.sprintf
1257 "Pages %d-%d of %d %.2f%%"
1258 (fn+1) (ln+1) state.pagecount percent
1260 showtext ' ' s;
1262 | 'w' ->
1263 begin match state.layout with
1264 | [] -> ()
1265 | l :: _ ->
1266 doreshape (l.pagew + conf.scrollw) l.pageh;
1267 Glut.postRedisplay ();
1270 | '\'' ->
1271 enterbookmarkmode ()
1273 | 'm' ->
1274 let ondone s =
1275 match state.layout with
1276 | l :: _ ->
1277 state.bookmarks <-
1278 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1279 :: state.bookmarks
1280 | _ -> ()
1282 enttext (Some ('~', "", None, textentry, ondone))
1284 | '~' ->
1285 quickbookmark ();
1286 showtext ' ' "Quick bookmark added";
1288 | 'z' ->
1289 begin match state.layout with
1290 | l :: _ ->
1291 let rect = getpdimrect l.pagedimno in
1292 let w, h =
1293 if conf.crophack
1294 then
1295 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1296 truncate (1.2 *. (rect.(3) -. rect.(0))))
1297 else
1298 (truncate (rect.(1) -. rect.(0)),
1299 truncate (rect.(3) -. rect.(0)))
1301 doreshape (w + conf.scrollw) (h + conf.interpagespace);
1302 Glut.postRedisplay ();
1304 | [] -> ()
1307 | '<' | '>' ->
1308 rotate (conf.angle + (if c = '>' then 30 else -30));
1310 | '[' | ']' ->
1311 state.colorscale <-
1312 max 0.0
1313 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1314 Glut.postRedisplay ()
1316 | 'k' -> gotoy (clamp (-conf.scrollincr))
1317 | 'j' -> gotoy (clamp conf.scrollincr)
1319 | 'r' -> opendoc state.path state.password
1321 | _ ->
1322 vlog "huh? %d %c" key (Char.chr key);
1325 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1326 let len = String.length text in
1327 if len = 0
1328 then (
1329 state.textentry <- None;
1330 Glut.postRedisplay ();
1332 else (
1333 let s = String.sub text 0 (len - 1) in
1334 enttext (Some (c, s, onhist, onkey, ondone))
1337 | Some (c, text, onhist, onkey, ondone) ->
1338 begin match Char.unsafe_chr key with
1339 | '\r' | '\n' ->
1340 ondone text;
1341 state.textentry <- None;
1342 Glut.postRedisplay ()
1344 | '\027' ->
1345 state.textentry <- None;
1346 Glut.postRedisplay ()
1348 | _ ->
1349 begin match onkey text key with
1350 | TEdone text ->
1351 state.textentry <- None;
1352 ondone text;
1353 Glut.postRedisplay ()
1355 | TEcont text ->
1356 enttext (Some (c, text, onhist, onkey, ondone));
1358 | TEstop ->
1359 state.textentry <- None;
1360 Glut.postRedisplay ()
1362 | TEswitch te ->
1363 state.textentry <- Some te;
1364 Glut.postRedisplay ()
1365 end;
1366 end;
1369 let narrow outlines pattern =
1370 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1371 match reopt with
1372 | None -> None
1373 | Some re ->
1374 let rec fold accu n =
1375 if n = -1
1376 then accu
1377 else
1378 let (s, _, _, _) as o = outlines.(n) in
1379 let accu =
1380 if (try ignore (Str.search_forward re s 0); true
1381 with Not_found -> false)
1382 then (o :: accu)
1383 else accu
1385 fold accu (n-1)
1387 let matched = fold [] (Array.length outlines - 1) in
1388 if matched = [] then None else Some (Array.of_list matched)
1391 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1392 let search active pattern incr =
1393 let dosearch re =
1394 let rec loop n =
1395 if n = Array.length outlines || n = -1
1396 then None
1397 else
1398 let (s, _, _, _) = outlines.(n) in
1400 (try ignore (Str.search_forward re s 0); true
1401 with Not_found -> false)
1402 then Some n
1403 else loop (n + incr)
1405 loop active
1408 let re = Str.regexp_case_fold pattern in
1409 dosearch re
1410 with Failure s ->
1411 state.text <- s;
1412 None
1414 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1415 match key with
1416 | 27 ->
1417 if String.length qsearch = 0
1418 then (
1419 state.text <- "";
1420 state.outline <- None;
1421 Glut.postRedisplay ();
1423 else (
1424 state.text <- "";
1425 state.outline <- Some (allowdel, active, first, outlines, "");
1426 Glut.postRedisplay ();
1429 | 18 | 19 ->
1430 let incr = if key = 18 then -1 else 1 in
1431 let active, first =
1432 match search (active + incr) qsearch incr with
1433 | None ->
1434 state.text <- qsearch ^ " [not found]";
1435 active, first
1436 | Some active ->
1437 state.text <- qsearch;
1438 active, firstof active
1440 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1441 Glut.postRedisplay ();
1443 | 8 ->
1444 let len = String.length qsearch in
1445 if len = 0
1446 then ()
1447 else (
1448 if len = 1
1449 then (
1450 state.text <- "";
1451 state.outline <- Some (allowdel, active, first, outlines, "");
1453 else
1454 let qsearch = String.sub qsearch 0 (len - 1) in
1455 let active, first =
1456 match search active qsearch ~-1 with
1457 | None ->
1458 state.text <- qsearch ^ " [not found]";
1459 active, first
1460 | Some active ->
1461 state.text <- qsearch;
1462 active, firstof active
1464 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1466 Glut.postRedisplay ()
1468 | 13 ->
1469 if active < Array.length outlines
1470 then (
1471 let (_, _, n, t) = outlines.(active) in
1472 gotopage n t;
1474 state.text <- "";
1475 if allowdel then state.bookmarks <- Array.to_list outlines;
1476 state.outline <- None;
1477 Glut.postRedisplay ();
1479 | _ when key >= 32 && key < 127 ->
1480 let pattern = addchar qsearch (Char.chr key) in
1481 let active, first =
1482 match search active pattern 1 with
1483 | None ->
1484 state.text <- pattern ^ " [not found]";
1485 active, first
1486 | Some active ->
1487 state.text <- pattern;
1488 active, firstof active
1490 state.outline <- Some (allowdel, active, first, outlines, pattern);
1491 Glut.postRedisplay ()
1493 | 14 when not allowdel -> (* ctrl-n *)
1494 if String.length qsearch > 0
1495 then (
1496 let optoutlines = narrow outlines qsearch in
1497 begin match optoutlines with
1498 | None -> state.text <- "can't narrow"
1499 | Some outlines ->
1500 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1501 match state.outlines with
1502 | Olist l -> ()
1503 | Oarray a ->
1504 state.outlines <- Onarrow (qsearch, outlines, a)
1505 | Onarrow (pat, a, b) ->
1506 state.outlines <- Onarrow (qsearch, outlines, b)
1507 end;
1509 Glut.postRedisplay ()
1511 | 21 when not allowdel -> (* ctrl-u *)
1512 let outline =
1513 match state.outlines with
1514 | Oarray a -> a
1515 | Olist l ->
1516 let a = Array.of_list (List.rev l) in
1517 state.outlines <- Oarray a;
1519 | Onarrow (pat, a, b) ->
1520 state.outlines <- Oarray b;
1521 state.text <- "";
1524 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1525 Glut.postRedisplay ()
1527 | 12 ->
1528 state.outline <-
1529 Some (allowdel, active, firstof active, outlines, qsearch);
1530 Glut.postRedisplay ()
1532 | 127 when allowdel ->
1533 let len = Array.length outlines - 1 in
1534 if len = 0
1535 then (
1536 state.outline <- None;
1537 state.bookmarks <- [];
1539 else (
1540 let bookmarks = Array.init len
1541 (fun i ->
1542 let i = if i >= active then i + 1 else i in
1543 outlines.(i)
1546 state.outline <-
1547 Some (allowdel,
1548 min active (len-1),
1549 min first (len-1),
1550 bookmarks, qsearch)
1553 Glut.postRedisplay ()
1555 | _ -> log "unknown key %d" key
1558 let keyboard ~key ~x ~y =
1559 if key = 7
1560 then
1561 wcmd "interrupt" []
1562 else
1563 match state.outline with
1564 | None -> viewkeyboard ~key ~x ~y
1565 | Some outline -> outlinekeyboard ~key ~x ~y outline
1568 let special ~key ~x ~y =
1569 match state.outline with
1570 | None ->
1571 begin match state.textentry with
1572 | None ->
1573 let y =
1574 match key with
1575 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1576 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1577 | Glut.KEY_DOWN -> clamp conf.scrollincr
1578 | Glut.KEY_PAGE_UP ->
1579 if Glut.getModifiers () land Glut.active_ctrl != 0
1580 then
1581 match state.layout with
1582 | [] -> state.y
1583 | l :: _ -> state.y - l.pagey
1584 else
1585 clamp (-conf.winh)
1586 | Glut.KEY_PAGE_DOWN ->
1587 if Glut.getModifiers () land Glut.active_ctrl != 0
1588 then
1589 match List.rev state.layout with
1590 | [] -> state.y
1591 | l :: _ -> getpagey l.pageno
1592 else
1593 clamp conf.winh
1594 | Glut.KEY_HOME -> addnav (); 0
1595 | Glut.KEY_END ->
1596 addnav ();
1597 state.maxy - (if conf.maxhfit then conf.winh else 0)
1599 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1600 state.x <- state.x - 10;
1601 state.y
1602 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1603 state.x <- state.x + 10;
1604 state.y
1606 | _ -> state.y
1608 gotoy_and_clear_text y
1610 | Some (c, s, Some onhist, onkey, ondone) ->
1611 let s =
1612 match key with
1613 | Glut.KEY_UP -> onhist HCprev
1614 | Glut.KEY_DOWN -> onhist HCnext
1615 | Glut.KEY_HOME -> onhist HCfirst
1616 | Glut.KEY_END -> onhist HClast
1617 | _ -> state.text
1619 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1620 Glut.postRedisplay ()
1622 | _ -> ()
1625 | Some (allowdel, active, first, outlines, qsearch) ->
1626 let maxrows = maxoutlinerows () in
1627 let navigate incr =
1628 let active = active + incr in
1629 let active = max 0 (min active (Array.length outlines - 1)) in
1630 let first =
1631 if active > first
1632 then
1633 let rows = active - first in
1634 if rows > maxrows then active - maxrows else first
1635 else active
1637 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1638 Glut.postRedisplay ()
1640 match key with
1641 | Glut.KEY_UP -> navigate ~-1
1642 | Glut.KEY_DOWN -> navigate 1
1643 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1644 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1646 | Glut.KEY_HOME ->
1647 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1648 Glut.postRedisplay ()
1650 | Glut.KEY_END ->
1651 let active = Array.length outlines - 1 in
1652 let first = max 0 (active - maxrows) in
1653 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1654 Glut.postRedisplay ()
1656 | _ -> ()
1659 let drawplaceholder l =
1660 GlDraw.color (scalecolor 1.0);
1661 GlDraw.rect
1662 (0.0, float l.pagedispy)
1663 (float l.pagew, float (l.pagedispy + l.pagevh))
1665 let x = 0.0
1666 and y = float (l.pagedispy + 13) in
1667 let font = Glut.BITMAP_8_BY_13 in
1668 GlDraw.color (0.0, 0.0, 0.0);
1669 GlPix.raster_pos ~x ~y ();
1670 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1671 ("Loading " ^ string_of_int (l.pageno + 1));
1674 let now () = Unix.gettimeofday ();;
1676 let drawpage i l =
1677 begin match getopaque l.pageno with
1678 | Some opaque when validopaque opaque ->
1679 if state.textentry = None
1680 then GlDraw.color (scalecolor 1.0)
1681 else GlDraw.color (scalecolor 0.4);
1682 let a = now () in
1683 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1684 opaque;
1685 let b = now () in
1686 let d = b-.a in
1687 vlog "draw %d %f sec" l.pageno d;
1689 | _ ->
1690 drawplaceholder l;
1691 end;
1692 l.pagedispy + l.pagevh;
1695 let scrollph y =
1696 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1697 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1698 let sh = float conf.winh /. sh in
1699 let sh = max sh (float conf.scrollh) in
1701 let percent =
1702 if state.y = state.maxy
1703 then 1.0
1704 else float y /. float maxy
1706 let position = (float conf.winh -. sh) *. percent in
1708 let position =
1709 if position +. sh > float conf.winh
1710 then float conf.winh -. sh
1711 else position
1713 position, sh;
1716 let scrollindicator () =
1717 GlDraw.color (0.64 , 0.64, 0.64);
1718 GlDraw.rect
1719 (float (conf.winw - conf.scrollw), 0.)
1720 (float conf.winw, float conf.winh)
1722 GlDraw.color (0.0, 0.0, 0.0);
1724 let position, sh = scrollph state.y in
1725 GlDraw.rect
1726 (float (conf.winw - conf.scrollw), position)
1727 (float conf.winw, position +. sh)
1731 let showsel margin =
1732 match state.mstate with
1733 | Mnone | Mscroll _ | Mpan _ ->
1736 | Msel ((x0, y0), (x1, y1)) ->
1737 let rec loop = function
1738 | l :: ls ->
1739 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1740 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1741 then
1742 match getopaque l.pageno with
1743 | Some opaque when validopaque opaque ->
1744 let oy = -l.pagey + l.pagedispy in
1745 seltext opaque
1746 (x0 - margin - state.x, y0,
1747 x1 - margin - state.x, y1) oy;
1749 | _ -> ()
1750 else loop ls
1751 | [] -> ()
1753 loop state.layout
1756 let showrects () =
1757 let panx = float state.x in
1758 Gl.enable `blend;
1759 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1760 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1761 List.iter
1762 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1763 List.iter (fun l ->
1764 if l.pageno = pageno
1765 then (
1766 let d = float (l.pagedispy - l.pagey) in
1767 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1768 GlDraw.begins `quads;
1770 GlDraw.vertex2 (x0+.panx, y0+.d);
1771 GlDraw.vertex2 (x1+.panx, y1+.d);
1772 GlDraw.vertex2 (x2+.panx, y2+.d);
1773 GlDraw.vertex2 (x3+.panx, y3+.d);
1775 GlDraw.ends ();
1777 ) state.layout
1778 ) state.rects
1780 Gl.disable `blend;
1783 let showoutline = function
1784 | None -> ()
1785 | Some (allowdel, active, first, outlines, qsearch) ->
1786 Gl.enable `blend;
1787 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1788 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1789 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
1790 Gl.disable `blend;
1792 GlDraw.color (1., 1., 1.);
1793 let font = Glut.BITMAP_9_BY_15 in
1794 let draw_string x y s =
1795 GlPix.raster_pos ~x ~y ();
1796 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1798 let rec loop row =
1799 if row = Array.length outlines || (row - first) * 16 > conf.winh
1800 then ()
1801 else (
1802 let (s, l, _, _) = outlines.(row) in
1803 let y = (row - first) * 16 in
1804 let x = 5 + 15*l in
1805 if row = active
1806 then (
1807 Gl.enable `blend;
1808 GlDraw.polygon_mode `both `line;
1809 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1810 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1811 GlDraw.rect (0., float (y + 1))
1812 (float (conf.winw - 1), float (y + 18));
1813 GlDraw.polygon_mode `both `fill;
1814 Gl.disable `blend;
1815 GlDraw.color (1., 1., 1.);
1817 draw_string (float x) (float (y + 16)) s;
1818 loop (row+1)
1821 loop first
1824 let display () =
1825 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1826 GlDraw.viewport margin 0 state.w conf.winh;
1827 pagematrix ();
1828 GlClear.color (scalecolor 0.5);
1829 GlClear.clear [`color];
1830 if state.x != 0
1831 then (
1832 let x = float state.x in
1833 GlMat.translate ~x ();
1835 if conf.zoom > 1.0
1836 then (
1837 Gl.enable `scissor_test;
1838 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
1840 let _lasty = List.fold_left drawpage 0 (state.layout) in
1841 if conf.zoom > 1.0
1842 then
1843 Gl.disable `scissor_test
1845 if state.x != 0
1846 then (
1847 let x = -.float state.x in
1848 GlMat.translate ~x ();
1850 showrects ();
1851 showsel margin;
1852 GlDraw.viewport 0 0 conf.winw conf.winh;
1853 winmatrix ();
1854 scrollindicator ();
1855 showoutline state.outline;
1856 enttext ();
1857 Glut.swapBuffers ();
1860 let getunder x y =
1861 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1862 let x = x - margin - state.x in
1863 let rec f = function
1864 | l :: rest ->
1865 begin match getopaque l.pageno with
1866 | Some opaque when validopaque opaque ->
1867 let y = y - l.pagedispy in
1868 if y > 0
1869 then
1870 let y = l.pagey + y in
1871 match whatsunder opaque x y with
1872 | Unone -> f rest
1873 | under -> under
1874 else
1875 f rest
1876 | _ ->
1877 f rest
1879 | [] -> Unone
1881 f state.layout
1884 let mouse ~button ~bstate ~x ~y =
1885 match button with
1886 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1887 let incr =
1888 if n = 3
1889 then
1890 -conf.scrollincr
1891 else
1892 conf.scrollincr
1894 let incr = incr * 2 in
1895 let y = clamp incr in
1896 gotoy_and_clear_text y
1898 | Glut.LEFT_BUTTON when state.outline = None
1899 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1900 if bstate = Glut.DOWN
1901 then (
1902 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1903 state.mstate <- Mpan (x, y)
1905 else
1906 state.mstate <- Mnone
1908 | Glut.LEFT_BUTTON
1909 when state.outline = None && x > conf.winw - conf.scrollw ->
1910 if bstate = Glut.DOWN
1911 then
1912 let position, sh = scrollph state.y in
1913 if y > truncate position && y < truncate (position +. sh)
1914 then
1915 state.mstate <- Mscroll
1916 else
1917 let percent = float y /. float conf.winh in
1918 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
1919 gotoy desty;
1920 state.mstate <- Mscroll
1921 else
1922 state.mstate <- Mnone
1924 | Glut.LEFT_BUTTON when state.outline = None ->
1925 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1926 begin match dest with
1927 | Ulinkgoto (pageno, top) ->
1928 if pageno >= 0
1929 then
1930 gotopage1 pageno top
1932 | Ulinkuri s ->
1933 print_endline s
1935 | Unone when bstate = Glut.DOWN ->
1936 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1937 state.mstate <- Mpan (x, y);
1939 | Unone | Utext _ ->
1940 if bstate = Glut.DOWN
1941 then (
1942 if conf.angle mod 360 = 0
1943 then (
1944 state.mstate <- Msel ((x, y), (x, y));
1945 Glut.postRedisplay ()
1948 else (
1949 match state.mstate with
1950 | Mnone -> ()
1952 | Mscroll ->
1953 state.mstate <- Mnone
1955 | Mpan _ ->
1956 Glut.setCursor Glut.CURSOR_INHERIT;
1957 state.mstate <- Mnone
1959 | Msel ((x0, y0), (x1, y1)) ->
1960 let f l =
1961 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1962 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1963 then
1964 match getopaque l.pageno with
1965 | Some opaque when validopaque opaque ->
1966 copysel opaque
1967 | _ -> ()
1969 List.iter f state.layout;
1970 copysel ""; (* ugly *)
1971 Glut.setCursor Glut.CURSOR_INHERIT;
1972 state.mstate <- Mnone;
1976 | _ ->
1979 let mouse ~button ~state ~x ~y = mouse button state x y;;
1981 let motion ~x ~y =
1982 if state.outline = None
1983 then
1984 match state.mstate with
1985 | Mnone -> ()
1987 | Mpan (x0, y0) ->
1988 let dx = x - x0
1989 and dy = y0 - y in
1990 state.mstate <- Mpan (x, y);
1991 if conf.zoom > 1.0 then state.x <- state.x + dx;
1992 let y = clamp dy in
1993 gotoy_and_clear_text y
1995 | Msel (a, _) ->
1996 state.mstate <- Msel (a, (x, y));
1997 Glut.postRedisplay ()
1999 | Mscroll ->
2000 let y = min conf.winh (max 0 y) in
2001 let percent = float y /. float conf.winh in
2002 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2003 gotoy_and_clear_text y
2006 let pmotion ~x ~y =
2007 if state.outline = None
2008 then
2009 match state.mstate with
2010 | Mnone ->
2011 begin match getunder x y with
2012 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2013 | Ulinkuri uri ->
2014 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2015 Glut.setCursor Glut.CURSOR_INFO
2016 | Ulinkgoto (page, y) ->
2017 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2018 Glut.setCursor Glut.CURSOR_INFO
2019 | Utext s ->
2020 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2021 Glut.setCursor Glut.CURSOR_TEXT
2024 | Mpan _ | Msel _ | Mscroll ->
2028 module State =
2029 struct
2030 open Parser
2032 let home =
2034 match Sys.os_type with
2035 | "Win32" -> Sys.getenv "HOMEPATH"
2036 | _ -> Sys.getenv "HOME"
2037 with exn ->
2038 prerr_endline
2039 ("Can not determine home directory location: " ^
2040 Printexc.to_string exn);
2044 let config_of c attrs =
2045 let apply c k v =
2047 match k with
2048 | "scroll-bar-width" -> { c with scrollw = int_of_string v }
2049 | "scroll-handle-height" -> { c with scrollh = int_of_string v }
2050 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2051 | "preload" -> { c with preload = bool_of_string v }
2052 | "page-bias" -> { c with pagebias = int_of_string v }
2053 | "scroll-step" -> { c with scrollincr = int_of_string v }
2054 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2055 | "crop-hack" -> { c with crophack = bool_of_string v }
2056 | "throttle" -> { c with showall = bool_of_string v }
2057 | "highlight-links" -> { c with hlinks = bool_of_string v }
2058 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2059 | "vertical-margin" -> { c with interpagespace = int_of_string v }
2060 | "zoom" -> { c with zoom = float_of_string v /. 100. }
2061 | "presentation" -> { c with presentation = bool_of_string v }
2062 | "rotation-angle" -> { c with angle = int_of_string v }
2063 | "width" -> { c with winw = int_of_string v }
2064 | "height" -> { c with winh = int_of_string v }
2065 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2066 | _ -> c
2067 with exn ->
2068 prerr_endline ("Error processing attribute (`" ^
2069 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2072 let rec fold c = function
2073 | [] -> c
2074 | (k, v) :: rest ->
2075 let c = apply c k v in
2076 fold c rest
2078 fold c attrs;
2081 let bookmark_of attrs =
2082 let rec fold title page rely = function
2083 | ("title", v) :: rest -> fold v page rely rest
2084 | ("page", v) :: rest -> fold title v rely rest
2085 | ("rely", v) :: rest -> fold title page v rest
2086 | _ :: rest -> fold title page rely rest
2087 | [] -> title, page, rely
2089 fold "invalid" "0" "0" attrs
2092 let setconf dst src =
2093 dst.scrollw <- src.scrollw;
2094 dst.scrollh <- src.scrollh;
2095 dst.icase <- src.icase;
2096 dst.preload <- src.preload;
2097 dst.pagebias <- src.pagebias;
2098 dst.verbose <- src.verbose;
2099 dst.scrollincr <- src.scrollincr;
2100 dst.maxhfit <- src.maxhfit;
2101 dst.crophack <- src.crophack;
2102 dst.autoscroll <- src.autoscroll;
2103 dst.showall <- src.showall;
2104 dst.hlinks <- src.hlinks;
2105 dst.underinfo <- src.underinfo;
2106 dst.interpagespace <- src.interpagespace;
2107 dst.zoom <- src.zoom;
2108 dst.presentation <- src.presentation;
2109 dst.angle <- src.angle;
2110 dst.winw <- src.winw;
2111 dst.winh <- src.winh;
2112 dst.savebmarks <- src.savebmarks;
2115 let unent s =
2116 let l = String.length s in
2117 let b = Buffer.create l in
2118 unent b s 0 l;
2119 Buffer.contents b;
2122 let get s =
2123 let h = Hashtbl.create 10 in
2124 let dc = { defconf with angle = defconf.angle } in
2125 let rec toplevel v t spos epos =
2126 match t with
2127 | Vdata | Vcdata | Vend -> v
2128 | Vopen ("llppconfig", attrs, closed) ->
2129 if closed
2130 then v
2131 else { v with f = llppconfig }
2132 | Vopen _ ->
2133 error "unexpected subelement at top level" s spos
2134 | Vclose tag -> error "unexpected close at toplevel" s spos
2136 and llppconfig v t spos epos =
2137 match t with
2138 | Vdata | Vcdata | Vend -> v
2139 | Vopen ("defaults", attrs, closed) ->
2140 let c = config_of dc attrs in
2141 setconf dc c;
2142 if closed
2143 then v
2144 else { v with f = skip "defaults" (fun () -> v) }
2146 | Vopen ("doc", attrs, closed) ->
2147 let pathent =
2149 List.assoc "path" attrs
2150 with Not_found -> error "doc is missing path attribute" s spos
2152 let path = unent pathent in
2153 let c = config_of dc attrs in
2154 let y =
2156 float_of_string (List.assoc "rely" attrs)
2157 with
2158 | Not_found -> 0.0
2159 | exn ->
2160 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2163 let x =
2165 int_of_string (List.assoc "pan" attrs)
2166 with
2167 | Not_found -> 0
2168 | exn ->
2169 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2172 if closed
2173 then (Hashtbl.add h path (c, [], x, y); v)
2174 else { v with f = doc path x y c [] }
2176 | Vopen (tag, _, closed) ->
2177 error "unexpected subelement in llppconfig" s spos
2179 | Vclose "llppconfig" -> { v with f = toplevel }
2180 | Vclose tag -> error "unexpected close in llppconfig" s spos
2182 and doc path x y c bookmarks v t spos epos =
2183 match t with
2184 | Vdata | Vcdata -> v
2185 | Vend -> error "unexpected end of input in doc" s spos
2186 | Vopen ("bookmarks", attrs, closed) ->
2187 { v with f = pbookmarks path x y c [] }
2189 | Vopen (tag, _, _) ->
2190 error "unexpected subelement in doc" s spos
2192 | Vclose "doc" ->
2193 Hashtbl.add h path (c, bookmarks, x, y);
2194 { v with f = llppconfig }
2196 | Vclose tag -> error "unexpected close in doc" s spos
2198 and pbookmarks path x y c bookmarks v t spos epos =
2199 match t with
2200 | Vdata | Vcdata -> v
2201 | Vend -> error "unexpected end of input in bookmarks" s spos
2202 | Vopen ("item", attrs, closed) ->
2203 let titleent, spage, srely = bookmark_of attrs in
2204 let page =
2206 int_of_string spage
2207 with exn ->
2208 dolog "Failed to convert page %S to integer: %s"
2209 spage (Printexc.to_string exn);
2212 let rely =
2214 float_of_string srely
2215 with exn ->
2216 dolog "Failed to convert rely %S to real: %s"
2217 srely (Printexc.to_string exn);
2220 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2221 if closed
2222 then { v with f = pbookmarks path x y c bookmarks }
2223 else
2224 let f () = v in
2225 { v with f = skip "item" f }
2227 | Vopen _ ->
2228 error "unexpected subelement in bookmarks" s spos
2230 | Vclose "bookmarks" ->
2231 { v with f = doc path x y c bookmarks }
2233 | Vclose tag -> error "unexpected close in bookmarks" s spos
2235 and skip tag f v t spos epos =
2236 match t with
2237 | Vdata | Vcdata -> v
2238 | Vend ->
2239 error ("unexpected end of input in skipped " ^ tag) s spos
2240 | Vopen (tag', _, closed) ->
2241 if closed
2242 then v
2243 else
2244 let f' () = { v with f = skip tag f } in
2245 { v with f = skip tag' f' }
2246 | Vclose ctag ->
2247 if tag = ctag
2248 then f ()
2249 else error ("unexpected close in skipped " ^ tag) s spos
2252 parse { f = toplevel; accu = () } s;
2253 h, dc;
2256 let do_load f ic =
2258 let len = in_channel_length ic in
2259 let s = String.create len in
2260 really_input ic s 0 len;
2261 f s;
2262 with
2263 | Parse_error (msg, s, pos) ->
2264 let subs = subs s pos in
2265 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2266 failwith ("parse error: " ^ s)
2268 | exn ->
2269 failwith ("config load error: " ^ Printexc.to_string exn)
2272 let path =
2273 let dir =
2275 let dir = Filename.concat home ".config" in
2276 if Sys.is_directory dir then dir else home
2277 with _ -> home
2279 Filename.concat dir "llpp.conf"
2282 let load1 f =
2283 if Sys.file_exists path
2284 then
2285 match
2286 (try Some (open_in_bin path)
2287 with exn ->
2288 prerr_endline
2289 ("Error opening configuation file `" ^ path ^ "': " ^
2290 Printexc.to_string exn);
2291 None
2293 with
2294 | Some ic ->
2295 begin try
2296 f (do_load get ic)
2297 with exn ->
2298 prerr_endline
2299 ("Error loading configuation from `" ^ path ^ "': " ^
2300 Printexc.to_string exn);
2301 end;
2302 close_in ic;
2304 | None -> ()
2305 else
2306 f (Hashtbl.create 0, defconf)
2309 let load () =
2310 let f (h, dc) =
2311 let pc, pb, px, py =
2313 Hashtbl.find h state.path
2314 with Not_found -> dc, [], 0, 0.0
2316 setconf defconf dc;
2317 setconf conf pc;
2318 state.bookmarks <- pb;
2319 state.x <- px;
2320 cbput state.hists.nav py;
2321 cbrfollowlen state.hists.nav;
2323 load1 f
2326 let add_attrs bb always dc c =
2327 let ob s a b =
2328 if always || a != b
2329 then Printf.bprintf bb "\n %s='%b'" s a
2330 and oi s a b =
2331 if always || a != b
2332 then Printf.bprintf bb "\n %s='%d'" s a
2333 and oz s a b =
2334 if always || a <> b
2335 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2337 let w, h =
2338 if always
2339 then dc.winw, dc.winh
2340 else
2341 match state.fullscreen with
2342 | Some wh -> wh
2343 | None -> c.winw, c.winh
2345 oi "width" w dc.winw;
2346 oi "height" h dc.winh;
2347 oi "scroll-bar-width" c.scrollw dc.scrollw;
2348 oi "scroll-handle-height" c.scrollh dc.scrollh;
2349 ob "case-insensitive-search" c.icase dc.icase;
2350 ob "preload" c.preload dc.preload;
2351 oi "page-bias" c.pagebias dc.pagebias;
2352 oi "scroll-step" c.scrollincr dc.scrollincr;
2353 ob "max-height-fit" c.maxhfit dc.maxhfit;
2354 ob "crop-hack" c.crophack dc.crophack;
2355 ob "throttle" c.showall dc.showall;
2356 ob "highlight-links" c.hlinks dc.hlinks;
2357 ob "under-cursor-info" c.underinfo dc.underinfo;
2358 oi "vertical-margin" c.interpagespace dc.interpagespace;
2359 oz "zoom" c.zoom dc.zoom;
2360 ob "presentation" c.presentation dc.presentation;
2361 oi "rotation-angle" c.angle dc.angle;
2362 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2365 let save () =
2366 let bb = Buffer.create 32768 in
2367 let f (h, dc) =
2368 Buffer.add_string bb "<llppconfig>\n<defaults ";
2369 add_attrs bb true dc dc;
2370 Buffer.add_string bb "/>\n";
2372 let adddoc path x y c bookmarks =
2373 if bookmarks == [] && c = dc && y = 0.0
2374 then ()
2375 else (
2376 Printf.bprintf bb "<doc path='%s'"
2377 (enent path 0 (String.length path));
2379 if y <> 0.0
2380 then Printf.bprintf bb " rely='%f'" y;
2382 if x != 0
2383 then Printf.bprintf bb " pan='%d'" x;
2385 add_attrs bb false dc c;
2387 begin match bookmarks with
2388 | [] -> Buffer.add_string bb "/>\n"
2389 | _ ->
2390 Buffer.add_string bb ">\n<bookmarks>\n";
2391 List.iter (fun (title, _level, page, rely) ->
2392 Printf.bprintf bb
2393 "<item title='%s' page='%d' rely='%f'/>\n"
2394 (enent title 0 (String.length title))
2395 page
2396 rely
2397 ) bookmarks;
2398 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2399 end;
2403 adddoc state.path state.x (yratio state.y) conf
2404 (if conf.savebmarks then state.bookmarks else []);
2406 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2407 if path <> state.path
2408 then
2409 adddoc path x y c bookmarks
2410 ) h;
2411 Buffer.add_string bb "</llppconfig>";
2413 load1 f;
2415 let tmp = path ^ ".tmp" in
2416 let oc = open_out_bin tmp in
2417 Buffer.output_buffer oc bb;
2418 close_out oc;
2419 Sys.rename tmp path;
2420 with exn ->
2421 prerr_endline
2422 ("error while saving configuration: " ^ Printexc.to_string exn)
2424 end;;
2426 let () =
2427 Arg.parse
2428 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2429 (fun s -> state.path <- s)
2430 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2432 let name =
2433 if String.length state.path = 0
2434 then (prerr_endline "filename missing"; exit 1)
2435 else state.path
2438 State.load ();
2440 let _ = Glut.init Sys.argv in
2441 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2442 let () = Glut.initWindowSize conf.winw conf.winh in
2443 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
2445 let csock, ssock =
2446 if Sys.os_type = "Unix"
2447 then
2448 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2449 else
2450 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2451 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2452 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2453 Unix.bind sock addr;
2454 Unix.listen sock 1;
2455 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2456 Unix.connect csock addr;
2457 let ssock, _ = Unix.accept sock in
2458 Unix.close sock;
2459 let opts sock =
2460 Unix.setsockopt sock Unix.TCP_NODELAY true;
2461 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2463 opts ssock;
2464 opts csock;
2465 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2466 ssock, csock
2469 let () = Glut.displayFunc display in
2470 let () = Glut.reshapeFunc reshape in
2471 let () = Glut.keyboardFunc keyboard in
2472 let () = Glut.specialFunc special in
2473 let () = Glut.idleFunc (Some idle) in
2474 let () = Glut.mouseFunc mouse in
2475 let () = Glut.motionFunc motion in
2476 let () = Glut.passiveMotionFunc pmotion in
2478 init ssock;
2479 state.csock <- csock;
2480 state.ssock <- ssock;
2481 state.text <- "Opening " ^ name;
2482 writecmd state.csock
2483 ("open " ^ state.path ^ "\000" ^ state.password ^ "\000"
2484 ^ string_of_int conf.angle ^ "\000");
2486 at_exit State.save;
2488 let rec handlelablglutbug () =
2490 Glut.mainLoop ();
2491 with Glut.BadEnum "key in special_of_int" ->
2492 showtext '!' " LablGlut bug: special key not recognized";
2493 handlelablglutbug ()
2495 handlelablglutbug ();