Do not ignore rotation angle from config
[llpp.git] / main.ml
blob3bf54a67e8180fd37554a2c90d363d74d48aaa41
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 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 =
966 if Array.length outlines = 0
967 then (
968 showtext ' ' errmsg;
970 else (
971 state.text <- "";
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 =
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;
1003 | Onarrow (a, b) -> a
1005 enterselector false outlines "Document has no outline";
1008 let enterbookmarkmode () =
1009 let bookmarks = Array.of_list state.bookmarks in
1010 enterselector true bookmarks "Document has no bookmarks (yet)";
1013 let quickbookmark ?title () =
1014 match state.layout with
1015 | [] -> ()
1016 | l :: _ ->
1017 let title =
1018 match title with
1019 | None ->
1020 let sec = Unix.gettimeofday () in
1021 let tm = Unix.localtime sec in
1022 Printf.sprintf "Quick %d visited (%d/%d/%d %d:%d)"
1023 l.pageno
1024 tm.Unix.tm_mday
1025 tm.Unix.tm_mon
1026 (tm.Unix.tm_year + 1900)
1027 tm.Unix.tm_hour
1028 tm.Unix.tm_min
1029 | Some title -> title
1031 state.bookmarks <-
1032 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1035 let doreshape w h =
1036 state.fullscreen <- None;
1037 Glut.reshapeWindow w h;
1040 let opendoc path password =
1041 invalidate ();
1042 state.path <- path;
1043 state.password <- password;
1044 Hashtbl.clear state.pagemap;
1046 writecmd state.csock ("open " ^ path ^ "\000" ^ password ^ "\000");
1047 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1048 wcmd "geometry" [`i state.w; `i conf.winh];
1051 let viewkeyboard ~key ~x ~y =
1052 let enttext te =
1053 state.textentry <- te;
1054 state.text <- "";
1055 enttext ();
1056 Glut.postRedisplay ()
1058 match state.textentry with
1059 | None ->
1060 let c = Char.chr key in
1061 begin match c with
1062 | '\027' | 'q' ->
1063 exit 0
1065 | '\008' ->
1066 let y = getnav () in
1067 gotoy_and_clear_text y
1069 | 'o' ->
1070 enteroutlinemode ()
1072 | 'u' ->
1073 state.rects <- [];
1074 state.text <- "";
1075 Glut.postRedisplay ()
1077 | '/' | '?' ->
1078 let ondone isforw s =
1079 cbput state.hists.pat s;
1080 cbrfollowlen state.hists.pat;
1081 state.searchpattern <- s;
1082 search s isforw
1084 enttext (Some (c, "", Some (onhist state.hists.pat),
1085 textentry, ondone (c ='/')))
1087 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1088 conf.zoom <- min 2.2 (conf.zoom +. 0.1);
1089 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1090 reshape conf.winw conf.winh
1092 | '+' ->
1093 let ondone s =
1094 let n =
1095 try int_of_string s with exc ->
1096 state.text <- Printf.sprintf "bad integer `%s': %s"
1097 s (Printexc.to_string exc);
1098 max_int
1100 if n != max_int
1101 then (
1102 conf.pagebias <- n;
1103 state.text <- "page bias is now " ^ string_of_int n;
1106 enttext (Some ('+', "", None, intentry, ondone))
1108 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1109 conf.zoom <- max 0.1 (conf.zoom -. 0.1);
1110 if conf.zoom <= 1.0 then state.x <- 0;
1111 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1112 reshape conf.winw conf.winh;
1114 | '-' ->
1115 let ondone msg =
1116 state.text <- msg;
1118 enttext (Some ('-', "", None, optentry, ondone))
1120 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1121 state.x <- 0;
1122 conf.zoom <- 1.0;
1123 state.text <- "zoom is 100%";
1124 reshape conf.winw conf.winh
1126 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1127 let n =
1128 let rec find n maxh nformaxh = function
1129 | (_, _, h) :: rest ->
1130 if h > maxh
1131 then find (n+1) h n rest
1132 else find (n+1) maxh nformaxh rest
1133 | [] -> nformaxh
1135 find 0 0 0 state.pdims
1138 let rect = getpdimrect n in
1139 let pw = rect.(1) -. rect.(0) in
1140 let ph = rect.(3) -. rect.(2) in
1142 let num = (float conf.winh *. pw) +. (ph *. float conf.scrollw) in
1143 let den = ph *. float conf.winw in
1144 let zoom = num /. den in
1146 if zoom < 1.0
1147 then (
1148 conf.zoom <- zoom;
1149 state.x <- 0;
1150 state.text <- Printf.sprintf "zoom is %3.1f%%" (100.0*.conf.zoom);
1151 reshape conf.winw conf.winh;
1154 | '0' .. '9' ->
1155 let ondone s =
1156 let n =
1157 try int_of_string s with exc ->
1158 state.text <- Printf.sprintf "bad integer `%s': %s"
1159 s (Printexc.to_string exc);
1162 if n >= 0
1163 then (
1164 addnav ();
1165 cbput state.hists.pag (string_of_int n);
1166 cbrfollowlen state.hists.pag;
1167 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1170 let pageentry text key =
1171 match Char.unsafe_chr key with
1172 | 'g' -> TEdone text
1173 | _ -> intentry text key
1175 let text = "x" in text.[0] <- c;
1176 enttext (Some (':', text, Some (onhist state.hists.pag),
1177 pageentry, ondone))
1179 | 'b' ->
1180 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1181 reshape conf.winw conf.winh;
1183 | 'l' ->
1184 conf.hlinks <- not conf.hlinks;
1185 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1186 Glut.postRedisplay ()
1188 | 'a' ->
1189 conf.autoscroll <- not conf.autoscroll
1191 | 'P' ->
1192 conf.presentation <- not conf.presentation;
1193 showtext ' ' ("presentation mode " ^
1194 if conf.presentation then "on" else "off");
1195 represent ()
1197 | 'f' ->
1198 begin match state.fullscreen with
1199 | None ->
1200 state.fullscreen <- Some (conf.winw, conf.winh);
1201 Glut.fullScreen ()
1202 | Some (w, h) ->
1203 state.fullscreen <- None;
1204 doreshape w h
1207 | 'g' ->
1208 gotoy_and_clear_text 0
1210 | 'n' ->
1211 search state.searchpattern true
1213 | 'p' | 'N' ->
1214 search state.searchpattern false
1216 | 't' ->
1217 begin match state.layout with
1218 | [] -> ()
1219 | l :: _ ->
1220 gotoy_and_clear_text (getpagey l.pageno)
1223 | ' ' ->
1224 begin match List.rev state.layout with
1225 | [] -> ()
1226 | l :: _ ->
1227 let pageno = min (l.pageno+1) (state.pagecount-1) in
1228 gotoy_and_clear_text (getpagey pageno)
1231 | '\127' ->
1232 begin match state.layout with
1233 | [] -> ()
1234 | l :: _ ->
1235 let pageno = max 0 (l.pageno-1) in
1236 gotoy_and_clear_text (getpagey pageno)
1239 | '=' ->
1240 let f (fn, ln) l =
1241 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1243 let fn, ln = List.fold_left f (-1, -1) state.layout in
1244 let s =
1245 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1246 let percent =
1247 if maxy <= 0
1248 then 100.
1249 else (100. *. (float state.y /. float maxy)) in
1250 if fn = ln
1251 then
1252 Printf.sprintf "Page %d of %d %.2f%%"
1253 (fn+1) state.pagecount percent
1254 else
1255 Printf.sprintf
1256 "Pages %d-%d of %d %.2f%%"
1257 (fn+1) (ln+1) state.pagecount percent
1259 showtext ' ' s;
1261 | 'w' ->
1262 begin match state.layout with
1263 | [] -> ()
1264 | l :: _ ->
1265 doreshape (l.pagew + conf.scrollw) l.pageh;
1266 Glut.postRedisplay ();
1269 | '\'' ->
1270 enterbookmarkmode ()
1272 | 'm' ->
1273 let ondone s =
1274 match state.layout with
1275 | l :: _ ->
1276 state.bookmarks <-
1277 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1278 :: state.bookmarks
1279 | _ -> ()
1281 enttext (Some ('~', "", None, textentry, ondone))
1283 | '~' ->
1284 quickbookmark ();
1285 showtext ' ' "Quick bookmark added";
1287 | 'z' ->
1288 begin match state.layout with
1289 | l :: _ ->
1290 let rect = getpdimrect l.pagedimno in
1291 let w, h =
1292 if conf.crophack
1293 then
1294 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1295 truncate (1.2 *. (rect.(3) -. rect.(0))))
1296 else
1297 (truncate (rect.(1) -. rect.(0)),
1298 truncate (rect.(3) -. rect.(0)))
1300 doreshape (w + conf.scrollw) (h + conf.interpagespace);
1301 Glut.postRedisplay ();
1303 | [] -> ()
1306 | '<' | '>' ->
1307 rotate (conf.angle + (if c = '>' then 30 else -30));
1309 | '[' | ']' ->
1310 state.colorscale <-
1311 max 0.0
1312 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1313 Glut.postRedisplay ()
1315 | 'k' -> gotoy (clamp (-conf.scrollincr))
1316 | 'j' -> gotoy (clamp conf.scrollincr)
1318 | 'r' -> opendoc state.path state.password
1320 | _ ->
1321 vlog "huh? %d %c" key (Char.chr key);
1324 | Some (c, text, onhist, onkey, ondone) when key = 8 ->
1325 let len = String.length text in
1326 if len = 0
1327 then (
1328 state.textentry <- None;
1329 Glut.postRedisplay ();
1331 else (
1332 let s = String.sub text 0 (len - 1) in
1333 enttext (Some (c, s, onhist, onkey, ondone))
1336 | Some (c, text, onhist, onkey, ondone) ->
1337 begin match Char.unsafe_chr key with
1338 | '\r' | '\n' ->
1339 ondone text;
1340 state.textentry <- None;
1341 Glut.postRedisplay ()
1343 | '\027' ->
1344 state.textentry <- None;
1345 Glut.postRedisplay ()
1347 | _ ->
1348 begin match onkey text key with
1349 | TEdone text ->
1350 state.textentry <- None;
1351 ondone text;
1352 Glut.postRedisplay ()
1354 | TEcont text ->
1355 enttext (Some (c, text, onhist, onkey, ondone));
1357 | TEstop ->
1358 state.textentry <- None;
1359 Glut.postRedisplay ()
1361 | TEswitch te ->
1362 state.textentry <- Some te;
1363 Glut.postRedisplay ()
1364 end;
1365 end;
1368 let narrow outlines pattern =
1369 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1370 match reopt with
1371 | None -> None
1372 | Some re ->
1373 let rec fold accu n =
1374 if n = -1
1375 then accu
1376 else
1377 let (s, _, _, _) as o = outlines.(n) in
1378 let accu =
1379 if (try ignore (Str.search_forward re s 0); true
1380 with Not_found -> false)
1381 then (o :: accu)
1382 else accu
1384 fold accu (n-1)
1386 let matched = fold [] (Array.length outlines - 1) in
1387 if matched = [] then None else Some (Array.of_list matched)
1390 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1391 let search active pattern incr =
1392 let dosearch re =
1393 let rec loop n =
1394 if n = Array.length outlines || n = -1
1395 then None
1396 else
1397 let (s, _, _, _) = outlines.(n) in
1399 (try ignore (Str.search_forward re s 0); true
1400 with Not_found -> false)
1401 then Some n
1402 else loop (n + incr)
1404 loop active
1407 let re = Str.regexp_case_fold pattern in
1408 dosearch re
1409 with Failure s ->
1410 state.text <- s;
1411 None
1413 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1414 match key with
1415 | 27 ->
1416 if String.length qsearch = 0
1417 then (
1418 state.text <- "";
1419 state.outline <- None;
1420 Glut.postRedisplay ();
1422 else (
1423 state.text <- "";
1424 state.outline <- Some (allowdel, active, first, outlines, "");
1425 Glut.postRedisplay ();
1428 | 18 | 19 ->
1429 let incr = if key = 18 then -1 else 1 in
1430 let active, first =
1431 match search (active + incr) qsearch incr with
1432 | None ->
1433 state.text <- qsearch ^ " [not found]";
1434 active, first
1435 | Some active ->
1436 state.text <- qsearch;
1437 active, firstof active
1439 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1440 Glut.postRedisplay ();
1442 | 8 ->
1443 let len = String.length qsearch in
1444 if len = 0
1445 then ()
1446 else (
1447 if len = 1
1448 then (
1449 state.text <- "";
1450 state.outline <- Some (allowdel, active, first, outlines, "");
1452 else
1453 let qsearch = String.sub qsearch 0 (len - 1) in
1454 let active, first =
1455 match search active qsearch ~-1 with
1456 | None ->
1457 state.text <- qsearch ^ " [not found]";
1458 active, first
1459 | Some active ->
1460 state.text <- qsearch;
1461 active, firstof active
1463 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1465 Glut.postRedisplay ()
1467 | 13 ->
1468 if active < Array.length outlines
1469 then (
1470 let (_, _, n, t) = outlines.(active) in
1471 gotopage n t;
1473 state.text <- "";
1474 if allowdel then state.bookmarks <- Array.to_list outlines;
1475 state.outline <- None;
1476 Glut.postRedisplay ();
1478 | _ when key >= 32 && key < 127 ->
1479 let pattern = addchar qsearch (Char.chr key) in
1480 let active, first =
1481 match search active pattern 1 with
1482 | None ->
1483 state.text <- pattern ^ " [not found]";
1484 active, first
1485 | Some active ->
1486 state.text <- pattern;
1487 active, firstof active
1489 state.outline <- Some (allowdel, active, first, outlines, pattern);
1490 Glut.postRedisplay ()
1492 | 14 when not allowdel ->
1493 let optoutlines = narrow outlines qsearch in
1494 begin match optoutlines with
1495 | None -> state.text <- "can't narrow"
1496 | Some outlines ->
1497 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1498 match state.outlines with
1499 | Olist l -> ()
1500 | Oarray a -> state.outlines <- Onarrow (outlines, a)
1501 | Onarrow (a, b) -> state.outlines <- Onarrow (outlines, b)
1502 end;
1503 Glut.postRedisplay ()
1505 | 21 when not allowdel ->
1506 let outline =
1507 match state.outlines with
1508 | Oarray a -> a
1509 | Olist l ->
1510 let a = Array.of_list (List.rev l) in
1511 state.outlines <- Oarray a;
1513 | Onarrow (a, b) ->
1514 state.outlines <- Oarray b;
1517 state.outline <- Some (allowdel, 0, 0, outline, qsearch);
1518 Glut.postRedisplay ()
1520 | 12 ->
1521 state.outline <-
1522 Some (allowdel, active, firstof active, outlines, qsearch);
1523 Glut.postRedisplay ()
1525 | 127 when allowdel ->
1526 let len = Array.length outlines - 1 in
1527 if len = 0
1528 then (
1529 state.outline <- None;
1530 state.bookmarks <- [];
1532 else (
1533 let bookmarks = Array.init len
1534 (fun i ->
1535 let i = if i >= active then i + 1 else i in
1536 outlines.(i)
1539 state.outline <-
1540 Some (allowdel,
1541 min active (len-1),
1542 min first (len-1),
1543 bookmarks, qsearch)
1546 Glut.postRedisplay ()
1548 | _ -> log "unknown key %d" key
1551 let keyboard ~key ~x ~y =
1552 if key = 7
1553 then
1554 wcmd "interrupt" []
1555 else
1556 match state.outline with
1557 | None -> viewkeyboard ~key ~x ~y
1558 | Some outline -> outlinekeyboard ~key ~x ~y outline
1561 let special ~key ~x ~y =
1562 match state.outline with
1563 | None ->
1564 begin match state.textentry with
1565 | None ->
1566 let y =
1567 match key with
1568 | Glut.KEY_F3 -> search state.searchpattern true; state.y
1569 | Glut.KEY_UP -> clamp (-conf.scrollincr)
1570 | Glut.KEY_DOWN -> clamp conf.scrollincr
1571 | Glut.KEY_PAGE_UP ->
1572 if Glut.getModifiers () land Glut.active_ctrl != 0
1573 then
1574 match state.layout with
1575 | [] -> state.y
1576 | l :: _ -> state.y - l.pagey
1577 else
1578 clamp (-conf.winh)
1579 | Glut.KEY_PAGE_DOWN ->
1580 if Glut.getModifiers () land Glut.active_ctrl != 0
1581 then
1582 match List.rev state.layout with
1583 | [] -> state.y
1584 | l :: _ -> getpagey l.pageno
1585 else
1586 clamp conf.winh
1587 | Glut.KEY_HOME -> addnav (); 0
1588 | Glut.KEY_END ->
1589 addnav ();
1590 state.maxy - (if conf.maxhfit then conf.winh else 0)
1592 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
1593 state.x <- state.x - 10;
1594 state.y
1595 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
1596 state.x <- state.x + 10;
1597 state.y
1599 | _ -> state.y
1601 gotoy_and_clear_text y
1603 | Some (c, s, Some onhist, onkey, ondone) ->
1604 let s =
1605 match key with
1606 | Glut.KEY_UP -> onhist HCprev
1607 | Glut.KEY_DOWN -> onhist HCnext
1608 | Glut.KEY_HOME -> onhist HCfirst
1609 | Glut.KEY_END -> onhist HClast
1610 | _ -> state.text
1612 state.textentry <- Some (c, s, Some onhist, onkey, ondone);
1613 Glut.postRedisplay ()
1615 | _ -> ()
1618 | Some (allowdel, active, first, outlines, qsearch) ->
1619 let maxrows = maxoutlinerows () in
1620 let navigate incr =
1621 let active = active + incr in
1622 let active = max 0 (min active (Array.length outlines - 1)) in
1623 let first =
1624 if active > first
1625 then
1626 let rows = active - first in
1627 if rows > maxrows then active - maxrows else first
1628 else active
1630 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1631 Glut.postRedisplay ()
1633 match key with
1634 | Glut.KEY_UP -> navigate ~-1
1635 | Glut.KEY_DOWN -> navigate 1
1636 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
1637 | Glut.KEY_PAGE_DOWN -> navigate maxrows
1639 | Glut.KEY_HOME ->
1640 state.outline <- Some (allowdel, 0, 0, outlines, qsearch);
1641 Glut.postRedisplay ()
1643 | Glut.KEY_END ->
1644 let active = Array.length outlines - 1 in
1645 let first = max 0 (active - maxrows) in
1646 state.outline <- Some (allowdel, active, first, outlines, qsearch);
1647 Glut.postRedisplay ()
1649 | _ -> ()
1652 let drawplaceholder l =
1653 GlDraw.color (scalecolor 1.0);
1654 GlDraw.rect
1655 (0.0, float l.pagedispy)
1656 (float l.pagew, float (l.pagedispy + l.pagevh))
1658 let x = 0.0
1659 and y = float (l.pagedispy + 13) in
1660 let font = Glut.BITMAP_8_BY_13 in
1661 GlDraw.color (0.0, 0.0, 0.0);
1662 GlPix.raster_pos ~x ~y ();
1663 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
1664 ("Loading " ^ string_of_int (l.pageno + 1));
1667 let now () = Unix.gettimeofday ();;
1669 let drawpage i l =
1670 begin match getopaque l.pageno with
1671 | Some opaque when validopaque opaque ->
1672 if state.textentry = None
1673 then GlDraw.color (scalecolor 1.0)
1674 else GlDraw.color (scalecolor 0.4);
1675 let a = now () in
1676 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
1677 opaque;
1678 let b = now () in
1679 let d = b-.a in
1680 vlog "draw %d %f sec" l.pageno d;
1682 | _ ->
1683 drawplaceholder l;
1684 end;
1685 l.pagedispy + l.pagevh;
1688 let scrollph y =
1689 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1690 let sh = (float (maxy + conf.winh) /. float conf.winh) in
1691 let sh = float conf.winh /. sh in
1692 let sh = max sh (float conf.scrollh) in
1694 let percent =
1695 if state.y = state.maxy
1696 then 1.0
1697 else float y /. float maxy
1699 let position = (float conf.winh -. sh) *. percent in
1701 let position =
1702 if position +. sh > float conf.winh
1703 then float conf.winh -. sh
1704 else position
1706 position, sh;
1709 let scrollindicator () =
1710 GlDraw.color (0.64 , 0.64, 0.64);
1711 GlDraw.rect
1712 (float (conf.winw - conf.scrollw), 0.)
1713 (float conf.winw, float conf.winh)
1715 GlDraw.color (0.0, 0.0, 0.0);
1717 let position, sh = scrollph state.y in
1718 GlDraw.rect
1719 (float (conf.winw - conf.scrollw), position)
1720 (float conf.winw, position +. sh)
1724 let showsel margin =
1725 match state.mstate with
1726 | Mnone | Mscroll _ | Mpan _ ->
1729 | Msel ((x0, y0), (x1, y1)) ->
1730 let rec loop = function
1731 | l :: ls ->
1732 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1733 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1734 then
1735 match getopaque l.pageno with
1736 | Some opaque when validopaque opaque ->
1737 let oy = -l.pagey + l.pagedispy in
1738 seltext opaque
1739 (x0 - margin - state.x, y0,
1740 x1 - margin - state.x, y1) oy;
1742 | _ -> ()
1743 else loop ls
1744 | [] -> ()
1746 loop state.layout
1749 let showrects () =
1750 let panx = float state.x in
1751 Gl.enable `blend;
1752 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
1753 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1754 List.iter
1755 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
1756 List.iter (fun l ->
1757 if l.pageno = pageno
1758 then (
1759 let d = float (l.pagedispy - l.pagey) in
1760 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
1761 GlDraw.begins `quads;
1763 GlDraw.vertex2 (x0+.panx, y0+.d);
1764 GlDraw.vertex2 (x1+.panx, y1+.d);
1765 GlDraw.vertex2 (x2+.panx, y2+.d);
1766 GlDraw.vertex2 (x3+.panx, y3+.d);
1768 GlDraw.ends ();
1770 ) state.layout
1771 ) state.rects
1773 Gl.disable `blend;
1776 let showoutline = function
1777 | None -> ()
1778 | Some (allowdel, active, first, outlines, qsearch) ->
1779 Gl.enable `blend;
1780 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1781 GlDraw.color (0., 0., 0.) ~alpha:0.85;
1782 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
1783 Gl.disable `blend;
1785 GlDraw.color (1., 1., 1.);
1786 let font = Glut.BITMAP_9_BY_15 in
1787 let draw_string x y s =
1788 GlPix.raster_pos ~x ~y ();
1789 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
1791 let rec loop row =
1792 if row = Array.length outlines || (row - first) * 16 > conf.winh
1793 then ()
1794 else (
1795 let (s, l, _, _) = outlines.(row) in
1796 let y = (row - first) * 16 in
1797 let x = 5 + 15*l in
1798 if row = active
1799 then (
1800 Gl.enable `blend;
1801 GlDraw.polygon_mode `both `line;
1802 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
1803 GlDraw.color (1., 1., 1.) ~alpha:0.9;
1804 GlDraw.rect (0., float (y + 1))
1805 (float (conf.winw - 1), float (y + 18));
1806 GlDraw.polygon_mode `both `fill;
1807 Gl.disable `blend;
1808 GlDraw.color (1., 1., 1.);
1810 draw_string (float x) (float (y + 16)) s;
1811 loop (row+1)
1814 loop first
1817 let display () =
1818 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1819 GlDraw.viewport margin 0 state.w conf.winh;
1820 pagematrix ();
1821 GlClear.color (scalecolor 0.5);
1822 GlClear.clear [`color];
1823 if state.x != 0
1824 then (
1825 let x = float state.x in
1826 GlMat.translate ~x ();
1828 if conf.zoom > 1.0
1829 then (
1830 Gl.enable `scissor_test;
1831 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
1833 let _lasty = List.fold_left drawpage 0 (state.layout) in
1834 if conf.zoom > 1.0
1835 then
1836 Gl.disable `scissor_test
1838 if state.x != 0
1839 then (
1840 let x = -.float state.x in
1841 GlMat.translate ~x ();
1843 showrects ();
1844 showsel margin;
1845 GlDraw.viewport 0 0 conf.winw conf.winh;
1846 winmatrix ();
1847 scrollindicator ();
1848 showoutline state.outline;
1849 enttext ();
1850 Glut.swapBuffers ();
1853 let getunder x y =
1854 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
1855 let x = x - margin - state.x in
1856 let rec f = function
1857 | l :: rest ->
1858 begin match getopaque l.pageno with
1859 | Some opaque when validopaque opaque ->
1860 let y = y - l.pagedispy in
1861 if y > 0
1862 then
1863 let y = l.pagey + y in
1864 match whatsunder opaque x y with
1865 | Unone -> f rest
1866 | under -> under
1867 else
1868 f rest
1869 | _ ->
1870 f rest
1872 | [] -> Unone
1874 f state.layout
1877 let mouse ~button ~bstate ~x ~y =
1878 match button with
1879 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
1880 let incr =
1881 if n = 3
1882 then
1883 -conf.scrollincr
1884 else
1885 conf.scrollincr
1887 let incr = incr * 2 in
1888 let y = clamp incr in
1889 gotoy_and_clear_text y
1891 | Glut.LEFT_BUTTON when state.outline = None
1892 && Glut.getModifiers () land Glut.active_ctrl != 0 ->
1893 if bstate = Glut.DOWN
1894 then (
1895 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1896 state.mstate <- Mpan (x, y)
1898 else
1899 state.mstate <- Mnone
1901 | Glut.LEFT_BUTTON
1902 when state.outline = None && x > conf.winw - conf.scrollw ->
1903 if bstate = Glut.DOWN
1904 then
1905 let position, sh = scrollph state.y in
1906 if y > truncate position && y < truncate (position +. sh)
1907 then
1908 state.mstate <- Mscroll
1909 else
1910 let percent = float y /. float conf.winh in
1911 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
1912 gotoy desty;
1913 state.mstate <- Mscroll
1914 else
1915 state.mstate <- Mnone
1917 | Glut.LEFT_BUTTON when state.outline = None ->
1918 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
1919 begin match dest with
1920 | Ulinkgoto (pageno, top) ->
1921 if pageno >= 0
1922 then
1923 gotopage1 pageno top
1925 | Ulinkuri s ->
1926 print_endline s
1928 | Unone when bstate = Glut.DOWN ->
1929 Glut.setCursor Glut.CURSOR_CROSSHAIR;
1930 state.mstate <- Mpan (x, y);
1932 | Unone | Utext _ ->
1933 if bstate = Glut.DOWN
1934 then (
1935 if conf.angle mod 360 = 0
1936 then (
1937 state.mstate <- Msel ((x, y), (x, y));
1938 Glut.postRedisplay ()
1941 else (
1942 match state.mstate with
1943 | Mnone -> ()
1945 | Mscroll ->
1946 state.mstate <- Mnone
1948 | Mpan _ ->
1949 Glut.setCursor Glut.CURSOR_INHERIT;
1950 state.mstate <- Mnone
1952 | Msel ((x0, y0), (x1, y1)) ->
1953 let f l =
1954 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
1955 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
1956 then
1957 match getopaque l.pageno with
1958 | Some opaque when validopaque opaque ->
1959 copysel opaque
1960 | _ -> ()
1962 List.iter f state.layout;
1963 copysel ""; (* ugly *)
1964 Glut.setCursor Glut.CURSOR_INHERIT;
1965 state.mstate <- Mnone;
1969 | _ ->
1972 let mouse ~button ~state ~x ~y = mouse button state x y;;
1974 let motion ~x ~y =
1975 if state.outline = None
1976 then
1977 match state.mstate with
1978 | Mnone -> ()
1980 | Mpan (x0, y0) ->
1981 let dx = x - x0
1982 and dy = y0 - y in
1983 state.mstate <- Mpan (x, y);
1984 if conf.zoom > 1.0 then state.x <- state.x + dx;
1985 let y = clamp dy in
1986 gotoy_and_clear_text y
1988 | Msel (a, _) ->
1989 state.mstate <- Msel (a, (x, y));
1990 Glut.postRedisplay ()
1992 | Mscroll ->
1993 let y = min conf.winh (max 0 y) in
1994 let percent = float y /. float conf.winh in
1995 let y = truncate (float (state.maxy - conf.winh) *. percent) in
1996 gotoy_and_clear_text y
1999 let pmotion ~x ~y =
2000 if state.outline = None
2001 then
2002 match state.mstate with
2003 | Mnone ->
2004 begin match getunder x y with
2005 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2006 | Ulinkuri uri ->
2007 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2008 Glut.setCursor Glut.CURSOR_INFO
2009 | Ulinkgoto (page, y) ->
2010 if conf.underinfo then showtext 'p' ("age: " ^ string_of_int page);
2011 Glut.setCursor Glut.CURSOR_INFO
2012 | Utext s ->
2013 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2014 Glut.setCursor Glut.CURSOR_TEXT
2017 | Mpan _ | Msel _ | Mscroll ->
2021 module State =
2022 struct
2023 open Parser
2025 let home =
2027 match Sys.os_type with
2028 | "Win32" -> Sys.getenv "HOMEPATH"
2029 | _ -> Sys.getenv "HOME"
2030 with exn ->
2031 prerr_endline
2032 ("Can not determine home directory location: " ^
2033 Printexc.to_string exn);
2037 let config_of c attrs =
2038 let apply c k v =
2040 match k with
2041 | "scroll-bar-width" -> { c with scrollw = int_of_string v }
2042 | "scroll-handle-height" -> { c with scrollh = int_of_string v }
2043 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2044 | "preload" -> { c with preload = bool_of_string v }
2045 | "page-bias" -> { c with pagebias = int_of_string v }
2046 | "scroll-step" -> { c with scrollincr = int_of_string v }
2047 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2048 | "crop-hack" -> { c with crophack = bool_of_string v }
2049 | "throttle" -> { c with showall = bool_of_string v }
2050 | "highlight-links" -> { c with hlinks = bool_of_string v }
2051 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2052 | "vertical-margin" -> { c with interpagespace = int_of_string v }
2053 | "zoom" -> { c with zoom = float_of_string v /. 100. }
2054 | "presentation" -> { c with presentation = bool_of_string v }
2055 | "rotation-angle" -> { c with angle = int_of_string v }
2056 | "width" -> { c with winw = int_of_string v }
2057 | "height" -> { c with winh = int_of_string v }
2058 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2059 | _ -> c
2060 with exn ->
2061 prerr_endline ("Error processing attribute (`" ^
2062 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2065 let rec fold c = function
2066 | [] -> c
2067 | (k, v) :: rest ->
2068 let c = apply c k v in
2069 fold c rest
2071 fold c attrs;
2074 let bookmark_of attrs =
2075 let rec fold title page rely = function
2076 | ("title", v) :: rest -> fold v page rely rest
2077 | ("page", v) :: rest -> fold title v rely rest
2078 | ("rely", v) :: rest -> fold title page v rest
2079 | _ :: rest -> fold title page rely rest
2080 | [] -> title, page, rely
2082 fold "invalid" "0" "0" attrs
2085 let setconf dst src =
2086 dst.scrollw <- src.scrollw;
2087 dst.scrollh <- src.scrollh;
2088 dst.icase <- src.icase;
2089 dst.preload <- src.preload;
2090 dst.pagebias <- src.pagebias;
2091 dst.verbose <- src.verbose;
2092 dst.scrollincr <- src.scrollincr;
2093 dst.maxhfit <- src.maxhfit;
2094 dst.crophack <- src.crophack;
2095 dst.autoscroll <- src.autoscroll;
2096 dst.showall <- src.showall;
2097 dst.hlinks <- src.hlinks;
2098 dst.underinfo <- src.underinfo;
2099 dst.interpagespace <- src.interpagespace;
2100 dst.zoom <- src.zoom;
2101 dst.presentation <- src.presentation;
2102 dst.angle <- src.angle;
2103 dst.winw <- src.winw;
2104 dst.winh <- src.winh;
2105 dst.savebmarks <- src.savebmarks;
2108 let unent s =
2109 let l = String.length s in
2110 let b = Buffer.create l in
2111 unent b s 0 l;
2112 Buffer.contents b;
2115 let get s =
2116 let h = Hashtbl.create 10 in
2117 let dc = { defconf with angle = defconf.angle } in
2118 let rec toplevel v t spos epos =
2119 match t with
2120 | Vdata | Vcdata | Vend -> v
2121 | Vopen ("llppconfig", attrs, closed) ->
2122 if closed
2123 then v
2124 else { v with f = llppconfig }
2125 | Vopen _ ->
2126 error "unexpected subelement at top level" s spos
2127 | Vclose tag -> error "unexpected close at toplevel" s spos
2129 and llppconfig v t spos epos =
2130 match t with
2131 | Vdata | Vcdata | Vend -> v
2132 | Vopen ("defaults", attrs, closed) ->
2133 let c = config_of dc attrs in
2134 setconf dc c;
2135 if closed
2136 then v
2137 else { v with f = skip "defaults" (fun () -> v) }
2139 | Vopen ("doc", attrs, closed) ->
2140 let pathent =
2142 List.assoc "path" attrs
2143 with Not_found -> error "doc is missing path attribute" s spos
2145 let path = unent pathent in
2146 let c = config_of dc attrs in
2147 let y =
2149 float_of_string (List.assoc "rely" attrs)
2150 with
2151 | Not_found -> 0.0
2152 | exn ->
2153 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2156 let x =
2158 int_of_string (List.assoc "pan" attrs)
2159 with
2160 | Not_found -> 0
2161 | exn ->
2162 dolog "error while accesing rely: %s" (Printexc.to_string exn);
2165 if closed
2166 then (Hashtbl.add h path (c, [], x, y); v)
2167 else { v with f = doc path x y c [] }
2169 | Vopen (tag, _, closed) ->
2170 error "unexpected subelement in llppconfig" s spos
2172 | Vclose "llppconfig" -> { v with f = toplevel }
2173 | Vclose tag -> error "unexpected close in llppconfig" s spos
2175 and doc path x y c bookmarks v t spos epos =
2176 match t with
2177 | Vdata | Vcdata -> v
2178 | Vend -> error "unexpected end of input in doc" s spos
2179 | Vopen ("bookmarks", attrs, closed) ->
2180 { v with f = pbookmarks path x y c [] }
2182 | Vopen (tag, _, _) ->
2183 error "unexpected subelement in doc" s spos
2185 | Vclose "doc" ->
2186 Hashtbl.add h path (c, bookmarks, x, y);
2187 { v with f = llppconfig }
2189 | Vclose tag -> error "unexpected close in doc" s spos
2191 and pbookmarks path x y c bookmarks v t spos epos =
2192 match t with
2193 | Vdata | Vcdata -> v
2194 | Vend -> error "unexpected end of input in bookmarks" s spos
2195 | Vopen ("item", attrs, closed) ->
2196 let titleent, spage, srely = bookmark_of attrs in
2197 let page =
2199 int_of_string spage
2200 with exn ->
2201 dolog "Failed to convert page %S to integer: %s"
2202 spage (Printexc.to_string exn);
2205 let rely =
2207 float_of_string srely
2208 with exn ->
2209 dolog "Failed to convert rely %S to real: %s"
2210 srely (Printexc.to_string exn);
2213 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2214 if closed
2215 then { v with f = pbookmarks path x y c bookmarks }
2216 else
2217 let f () = v in
2218 { v with f = skip "item" f }
2220 | Vopen _ ->
2221 error "unexpected subelement in bookmarks" s spos
2223 | Vclose "bookmarks" ->
2224 { v with f = doc path x y c bookmarks }
2226 | Vclose tag -> error "unexpected close in bookmarks" s spos
2228 and skip tag f v t spos epos =
2229 match t with
2230 | Vdata | Vcdata -> v
2231 | Vend ->
2232 error ("unexpected end of input in skipped " ^ tag) s spos
2233 | Vopen (tag', _, closed) ->
2234 if closed
2235 then v
2236 else
2237 let f' () = { v with f = skip tag f } in
2238 { v with f = skip tag' f' }
2239 | Vclose ctag ->
2240 if tag = ctag
2241 then f ()
2242 else error ("unexpected close in skipped " ^ tag) s spos
2245 parse { f = toplevel; accu = () } s;
2246 h, dc;
2249 let do_load f ic =
2251 let len = in_channel_length ic in
2252 let s = String.create len in
2253 really_input ic s 0 len;
2254 f s;
2255 with
2256 | Parse_error (msg, s, pos) ->
2257 let subs = subs s pos in
2258 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2259 failwith ("parse error: " ^ s)
2261 | exn ->
2262 failwith ("config load error: " ^ Printexc.to_string exn)
2265 let path =
2266 let dir =
2268 let dir = Filename.concat home ".config" in
2269 if Sys.is_directory dir then dir else home
2270 with _ -> home
2272 Filename.concat dir "llpp.conf"
2275 let load1 f =
2276 if Sys.file_exists path
2277 then
2278 match
2279 (try Some (open_in_bin path)
2280 with exn ->
2281 prerr_endline
2282 ("Error opening configuation file `" ^ path ^ "': " ^
2283 Printexc.to_string exn);
2284 None
2286 with
2287 | Some ic ->
2288 begin try
2289 f (do_load get ic)
2290 with exn ->
2291 prerr_endline
2292 ("Error loading configuation from `" ^ path ^ "': " ^
2293 Printexc.to_string exn);
2294 end;
2295 close_in ic;
2297 | None -> ()
2298 else
2299 f (Hashtbl.create 0, defconf)
2302 let load () =
2303 let f (h, dc) =
2304 let pc, pb, px, py =
2306 Hashtbl.find h state.path
2307 with Not_found -> dc, [], 0, 0.0
2309 setconf defconf dc;
2310 setconf conf pc;
2311 state.bookmarks <- pb;
2312 state.x <- px;
2313 cbput state.hists.nav py;
2314 cbrfollowlen state.hists.nav;
2316 load1 f
2319 let add_attrs bb always dc c =
2320 let ob s a b =
2321 if always || a != b
2322 then Printf.bprintf bb "\n %s='%b'" s a
2323 and oi s a b =
2324 if always || a != b
2325 then Printf.bprintf bb "\n %s='%d'" s a
2326 and oz s a b =
2327 if always || a <> b
2328 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2330 let w, h =
2331 if always
2332 then dc.winw, dc.winh
2333 else
2334 match state.fullscreen with
2335 | Some wh -> wh
2336 | None -> c.winw, c.winh
2338 oi "width" w dc.winw;
2339 oi "height" h dc.winh;
2340 oi "scroll-bar-width" c.scrollw dc.scrollw;
2341 oi "scroll-handle-height" c.scrollh dc.scrollh;
2342 ob "case-insensitive-search" c.icase dc.icase;
2343 ob "preload" c.preload dc.preload;
2344 oi "page-bias" c.pagebias dc.pagebias;
2345 oi "scroll-step" c.scrollincr dc.scrollincr;
2346 ob "max-height-fit" c.maxhfit dc.maxhfit;
2347 ob "crop-hack" c.crophack dc.crophack;
2348 ob "throttle" c.showall dc.showall;
2349 ob "highlight-links" c.hlinks dc.hlinks;
2350 ob "under-cursor-info" c.underinfo dc.underinfo;
2351 oi "vertical-margin" c.interpagespace dc.interpagespace;
2352 oz "zoom" c.zoom dc.zoom;
2353 ob "presentation" c.presentation dc.presentation;
2354 oi "rotation-angle" c.angle dc.angle;
2355 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2358 let save () =
2359 let bb = Buffer.create 32768 in
2360 let f (h, dc) =
2361 Buffer.add_string bb "<llppconfig>\n<defaults ";
2362 add_attrs bb true dc dc;
2363 Buffer.add_string bb "/>\n";
2365 let adddoc path x y c bookmarks =
2366 if bookmarks == [] && c = dc && y = 0.0
2367 then ()
2368 else (
2369 Printf.bprintf bb "<doc path='%s'"
2370 (enent path 0 (String.length path));
2372 if y <> 0.0
2373 then Printf.bprintf bb " rely='%f'" y;
2375 if x != 0
2376 then Printf.bprintf bb " pan='%d'" x;
2378 add_attrs bb false dc c;
2380 begin match bookmarks with
2381 | [] -> Buffer.add_string bb "/>\n"
2382 | _ ->
2383 Buffer.add_string bb ">\n<bookmarks>\n";
2384 List.iter (fun (title, _level, page, rely) ->
2385 Printf.bprintf bb
2386 "<item title='%s' page='%d' rely='%f'/>\n"
2387 (enent title 0 (String.length title))
2388 page
2389 rely
2390 ) bookmarks;
2391 Buffer.add_string bb "</bookmarks>\n</doc>\n";
2392 end;
2396 adddoc state.path state.x (yratio state.y) conf
2397 (if conf.savebmarks then state.bookmarks else []);
2399 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
2400 if path <> state.path
2401 then
2402 adddoc path x y c bookmarks
2403 ) h;
2404 Buffer.add_string bb "</llppconfig>";
2406 load1 f;
2408 let tmp = path ^ ".tmp" in
2409 let oc = open_out_bin tmp in
2410 Buffer.output_buffer oc bb;
2411 close_out oc;
2412 Sys.rename tmp path;
2413 with exn ->
2414 prerr_endline
2415 ("error while saving configuration: " ^ Printexc.to_string exn)
2417 end;;
2419 let () =
2420 Arg.parse
2421 ["-p", Arg.String (fun s -> state.password <- s) , "password"]
2422 (fun s -> state.path <- s)
2423 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
2425 let name =
2426 if String.length state.path = 0
2427 then (prerr_endline "filename missing"; exit 1)
2428 else state.path
2431 State.load ();
2433 let _ = Glut.init Sys.argv in
2434 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
2435 let () = Glut.initWindowSize conf.winw conf.winh in
2436 let _ = Glut.createWindow ("llpp " ^ Filename.basename name) in
2438 let csock, ssock =
2439 if Sys.os_type = "Unix"
2440 then
2441 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
2442 else
2443 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
2444 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2445 Unix.setsockopt sock Unix.SO_REUSEADDR true;
2446 Unix.bind sock addr;
2447 Unix.listen sock 1;
2448 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
2449 Unix.connect csock addr;
2450 let ssock, _ = Unix.accept sock in
2451 Unix.close sock;
2452 let opts sock =
2453 Unix.setsockopt sock Unix.TCP_NODELAY true;
2454 Unix.setsockopt_optint sock Unix.SO_LINGER None;
2456 opts ssock;
2457 opts csock;
2458 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
2459 ssock, csock
2462 let () = Glut.displayFunc display in
2463 let () = Glut.reshapeFunc reshape in
2464 let () = Glut.keyboardFunc keyboard in
2465 let () = Glut.specialFunc special in
2466 let () = Glut.idleFunc (Some idle) in
2467 let () = Glut.mouseFunc mouse in
2468 let () = Glut.motionFunc motion in
2469 let () = Glut.passiveMotionFunc pmotion in
2471 init ssock;
2472 state.csock <- csock;
2473 state.ssock <- ssock;
2474 state.text <- "Opening " ^ name;
2475 writecmd state.csock
2476 ("open " ^ state.path ^ "\000" ^ state.password ^ "\000"
2477 ^ string_of_int conf.angle ^ "\000");
2479 at_exit State.save;
2481 let rec handlelablglutbug () =
2483 Glut.mainLoop ();
2484 with Glut.BadEnum "key in special_of_int" ->
2485 showtext '!' " LablGlut bug: special key not recognized";
2486 handlelablglutbug ()
2488 handlelablglutbug ();