Allow panning in outline/bookmark/info/help modes
[llpp.git] / main.ml
blobb7a98c38006a859a94ff51002f8343fc1bf9cb9d
1 type under =
2 | Unone
3 | Ulinkuri of string
4 | Ulinkgoto of (int * int)
5 | Utext of facename
6 and facename = string;;
8 let dolog fmt = Printf.kprintf prerr_endline fmt;;
10 type params = angle * proportional * texcount * sliceheight
11 and pageno = int
12 and width = int
13 and height = int
14 and leftx = int
15 and opaque = string
16 and recttype = int
17 and pixmapsize = int
18 and angle = int
19 and proportional = bool
20 and interpagespace = int
21 and texcount = int
22 and sliceheight = int
23 and gen = int
24 and top = float
27 external init : Unix.file_descr -> params -> unit = "ml_init";;
28 external draw : (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext : string -> (int * int * int * int) -> int -> unit =
30 "ml_seltext";;
31 external copysel : string -> unit = "ml_copysel";;
32 external getpdimrect : int -> float array = "ml_getpdimrect";;
33 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
34 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
36 type mpos = int * int
37 and mstate =
38 | Msel of (mpos * mpos)
39 | Mpan of mpos
40 | Mscroll
41 | Mzoom of (int * int)
42 | Mnone
45 type textentry = (char * string * onhist * onkey * ondone)
46 and onkey = string -> int -> te
47 and ondone = string -> unit
48 and histcancel = unit -> unit
49 and onhist = ((histcmd -> string) * histcancel) option
50 and histcmd = HCnext | HCprev | HCfirst | HClast
51 and te =
52 | TEstop
53 | TEdone of string
54 | TEcont of string
55 | TEswitch of textentry
58 type 'a circbuf =
59 { store : 'a array
60 ; mutable rc : int
61 ; mutable wc : int
62 ; mutable len : int
66 let cbnew n v =
67 { store = Array.create n v
68 ; rc = 0
69 ; wc = 0
70 ; len = 0
74 let cbcap b = Array.length b.store;;
76 let cbput b v =
77 let cap = cbcap b in
78 b.store.(b.wc) <- v;
79 b.wc <- (b.wc + 1) mod cap;
80 b.rc <- b.wc;
81 b.len <- min (b.len + 1) cap;
84 let cbempty b = b.len = 0;;
86 let cbgetg b circular dir =
87 if cbempty b
88 then b.store.(0)
89 else
90 let rc = b.rc + dir in
91 let rc =
92 if circular
93 then (
94 if rc = -1
95 then b.len-1
96 else (
97 if rc = b.len
98 then 0
99 else rc
102 else max 0 (min rc (b.len-1))
104 b.rc <- rc;
105 b.store.(rc);
108 let cbget b = cbgetg b false;;
109 let cbgetc b = cbgetg b true;;
111 let cbpeek b =
112 let rc = b.wc - b.len in
113 let rc = if rc < 0 then cbcap b + rc else rc in
114 b.store.(rc);
117 let cbdecr b = b.len <- b.len - 1;;
119 type layout =
120 { pageno : int
121 ; pagedimno : int
122 ; pagew : int
123 ; pageh : int
124 ; pagedispy : int
125 ; pagey : int
126 ; pagevh : int
127 ; pagex : int
131 type conf =
132 { mutable scrollw : int
133 ; mutable scrollh : int
134 ; mutable icase : bool
135 ; mutable preload : bool
136 ; mutable pagebias : int
137 ; mutable verbose : bool
138 ; mutable scrollstep : int
139 ; mutable maxhfit : bool
140 ; mutable crophack : bool
141 ; mutable autoscrollstep : int
142 ; mutable showall : bool
143 ; mutable hlinks : bool
144 ; mutable underinfo : bool
145 ; mutable interpagespace : interpagespace
146 ; mutable zoom : float
147 ; mutable presentation : bool
148 ; mutable angle : angle
149 ; mutable winw : int
150 ; mutable winh : int
151 ; mutable savebmarks : bool
152 ; mutable proportional : proportional
153 ; mutable memlimit : int
154 ; mutable texcount : texcount
155 ; mutable sliceheight : sliceheight
156 ; mutable thumbw : width
160 type outline = string * int * int * float;;
161 type outlines =
162 | Oarray of outline array
163 | Olist of outline list
164 | Onarrow of string * outline array * outline array
167 type rect = (float * float * float * float * float * float * float * float);;
169 type pagemapkey = (pageno * width * angle * proportional * gen);;
171 type anchor = pageno * top;;
173 type mode =
174 | Birdseye of (conf * leftx * pageno * pageno * anchor)
175 | Outline of (bool * int * int * outline array * string * int)
176 | Textentry of (textentry * mode)
177 | View
180 let isbirdseye = function Birdseye _ -> true | _ -> false;;
181 let istextentry = function Textentry _ -> true | _ -> false;;
183 type state =
184 { mutable csock : Unix.file_descr
185 ; mutable ssock : Unix.file_descr
186 ; mutable w : int
187 ; mutable x : int
188 ; mutable y : int
189 ; mutable anchor : anchor
190 ; mutable maxy : int
191 ; mutable layout : layout list
192 ; pagemap : (pagemapkey, (opaque * pixmapsize)) Hashtbl.t
193 ; mutable pdims : (pageno * width * height * leftx) list
194 ; mutable pagecount : int
195 ; pagecache : string circbuf
196 ; mutable rendering : bool
197 ; mutable mstate : mstate
198 ; mutable searchpattern : string
199 ; mutable rects : (pageno * recttype * rect) list
200 ; mutable rects1 : (pageno * recttype * rect) list
201 ; mutable text : string
202 ; mutable fullscreen : (width * height) option
203 ; mutable mode : mode
204 ; mutable outlines : outlines
205 ; mutable bookmarks : outline list
206 ; mutable path : string
207 ; mutable password : string
208 ; mutable invalidated : int
209 ; mutable colorscale : float
210 ; mutable memused : int
211 ; mutable gen : gen
212 ; mutable throttle : layout list option
213 ; mutable ascrollstep : int
214 ; mutable help : string list
215 ; mutable docinfo : (int * string) list
216 ; hists : hists
218 and hists =
219 { pat : string circbuf
220 ; pag : string circbuf
221 ; nav : anchor circbuf
225 let defconf =
226 { scrollw = 7
227 ; scrollh = 12
228 ; icase = true
229 ; preload = true
230 ; pagebias = 0
231 ; verbose = false
232 ; scrollstep = 24
233 ; maxhfit = true
234 ; crophack = false
235 ; autoscrollstep = 24
236 ; showall = false
237 ; hlinks = false
238 ; underinfo = false
239 ; interpagespace = 2
240 ; zoom = 1.0
241 ; presentation = false
242 ; angle = 0
243 ; winw = 900
244 ; winh = 900
245 ; savebmarks = true
246 ; proportional = true
247 ; memlimit = 32*1024*1024
248 ; texcount = 256
249 ; sliceheight = 24
250 ; thumbw = 76
254 let conf = { defconf with angle = defconf.angle };;
256 let state =
257 { csock = Unix.stdin
258 ; ssock = Unix.stdin
259 ; x = 0
260 ; y = 0
261 ; anchor = (0, 0.0)
262 ; w = 0
263 ; layout = []
264 ; maxy = max_int
265 ; pagemap = Hashtbl.create 10
266 ; pagecache = cbnew 100 ""
267 ; pdims = []
268 ; pagecount = 0
269 ; rendering = false
270 ; mstate = Mnone
271 ; rects = []
272 ; rects1 = []
273 ; text = ""
274 ; mode = View
275 ; fullscreen = None
276 ; searchpattern = ""
277 ; outlines = Olist []
278 ; bookmarks = []
279 ; path = ""
280 ; password = ""
281 ; invalidated = 0
282 ; hists =
283 { nav = cbnew 100 (0, 0.0)
284 ; pat = cbnew 20 ""
285 ; pag = cbnew 10 ""
287 ; colorscale = 1.0
288 ; memused = 0
289 ; gen = 0
290 ; throttle = None
291 ; ascrollstep = 0
292 ; help = Help.keys
293 ; docinfo = []
297 let vlog fmt =
298 if conf.verbose
299 then
300 Printf.kprintf prerr_endline fmt
301 else
302 Printf.kprintf ignore fmt
305 let writecmd fd s =
306 let len = String.length s in
307 let n = 4 + len in
308 let b = Buffer.create n in
309 Buffer.add_char b (Char.chr ((len lsr 24) land 0xff));
310 Buffer.add_char b (Char.chr ((len lsr 16) land 0xff));
311 Buffer.add_char b (Char.chr ((len lsr 8) land 0xff));
312 Buffer.add_char b (Char.chr ((len lsr 0) land 0xff));
313 Buffer.add_string b s;
314 let s' = Buffer.contents b in
315 let n' = Unix.write fd s' 0 n in
316 if n' != n then failwith "write failed";
319 let readcmd fd =
320 let s = "xxxx" in
321 let n = Unix.read fd s 0 4 in
322 if n != 4 then failwith "incomplete read(len)";
323 let len = 0
324 lor (Char.code s.[0] lsl 24)
325 lor (Char.code s.[1] lsl 16)
326 lor (Char.code s.[2] lsl 8)
327 lor (Char.code s.[3] lsl 0)
329 let s = String.create len in
330 let n = Unix.read fd s 0 len in
331 if n != len then failwith "incomplete read(data)";
335 let makecmd s l =
336 let b = Buffer.create 10 in
337 Buffer.add_string b s;
338 let rec combine = function
339 | [] -> b
340 | x :: xs ->
341 Buffer.add_char b ' ';
342 let s =
343 match x with
344 | `b b -> if b then "1" else "0"
345 | `s s -> s
346 | `i i -> string_of_int i
347 | `f f -> string_of_float f
348 | `I f -> string_of_int (truncate f)
350 Buffer.add_string b s;
351 combine xs;
353 combine l;
356 let wcmd s l =
357 let cmd = Buffer.contents (makecmd s l) in
358 writecmd state.csock cmd;
361 let calcips h =
362 if conf.presentation
363 then
364 let d = conf.winh - h in
365 max 0 ((d + 1) / 2)
366 else
367 conf.interpagespace
370 let calcheight () =
371 let rec f pn ph pi fh l =
372 match l with
373 | (n, _, h, _) :: rest ->
374 let ips = calcips h in
375 let fh =
376 if conf.presentation
377 then fh+ips
378 else (
379 if isbirdseye state.mode && pn = 0
380 then fh + ips
381 else fh
384 let fh = fh + ((n - pn) * (ph + pi)) in
385 f n h ips fh rest;
387 | [] ->
388 let inc =
389 if conf.presentation || (isbirdseye state.mode && pn = 0)
390 then 0
391 else -pi
393 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
394 max 0 fh
396 let fh = f 0 0 0 0 state.pdims in
400 let getpageyh pageno =
401 let rec f pn ph pi y l =
402 match l with
403 | (n, _, h, _) :: rest ->
404 let ips = calcips h in
405 if n >= pageno
406 then
407 let h = if n = pageno then h else ph in
408 if conf.presentation && n = pageno
409 then
410 y + (pageno - pn) * (ph + pi) + pi, h
411 else
412 y + (pageno - pn) * (ph + pi), h
413 else
414 let y = y + (if conf.presentation then pi else 0) in
415 let y = y + (n - pn) * (ph + pi) in
416 f n h ips y rest
418 | [] ->
419 y + (pageno - pn) * (ph + pi), ph
421 f 0 0 0 0 state.pdims
424 let getpagey pageno = fst (getpageyh pageno);;
426 let layout y sh =
427 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu =
428 let ((w, h, ips, x) as curr), rest, pdimno, yinc =
429 match pdims with
430 | (pageno', w, h, x) :: rest when pageno' = pageno ->
431 let ips = calcips h in
432 let yinc =
433 if conf.presentation || (isbirdseye state.mode && pageno = 0)
434 then ips
435 else 0
437 (w, h, ips, x), rest, pdimno + 1, yinc
438 | _ ->
439 prev, pdims, pdimno, 0
441 let dy = dy + yinc in
442 let py = py + yinc in
443 if pageno = state.pagecount || cacheleft = 0 || dy >= sh
444 then
445 accu
446 else
447 let vy = y + dy in
448 if py + h <= vy - yinc
449 then
450 let py = py + h + ips in
451 let dy = max 0 (py - y) in
452 f ~pageno:(pageno+1)
453 ~pdimno
454 ~prev:curr
457 ~pdims:rest
458 ~cacheleft
459 ~accu
460 else
461 let pagey = vy - py in
462 let pagevh = h - pagey in
463 let pagevh = min (sh - dy) pagevh in
464 let off = if yinc > 0 then py - vy else 0 in
465 let py = py + h + ips in
466 let e =
467 { pageno = pageno
468 ; pagedimno = pdimno
469 ; pagew = w
470 ; pageh = h
471 ; pagedispy = dy + off
472 ; pagey = pagey + off
473 ; pagevh = pagevh - off
474 ; pagex = x
477 let accu = e :: accu in
478 f ~pageno:(pageno+1)
479 ~pdimno
480 ~prev:curr
482 ~dy:(dy+pagevh+ips)
483 ~pdims:rest
484 ~cacheleft:(cacheleft-1)
485 ~accu
487 if state.invalidated = 0
488 then (
489 let accu =
491 ~pageno:0
492 ~pdimno:~-1
493 ~prev:(0,0,0,0)
494 ~py:0
495 ~dy:0
496 ~pdims:state.pdims
497 ~cacheleft:(cbcap state.pagecache)
498 ~accu:[]
500 List.rev accu
502 else
506 let clamp incr =
507 let y = state.y + incr in
508 let y = max 0 y in
509 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
513 let getopaque pageno =
514 try Some (Hashtbl.find state.pagemap
515 (pageno, state.w, conf.angle, conf.proportional, state.gen))
516 with Not_found -> None
519 let cache pageno opaque =
520 Hashtbl.replace state.pagemap
521 (pageno, state.w, conf.angle, conf.proportional, state.gen) opaque
524 let validopaque opaque = String.length opaque > 0;;
526 let render l =
527 match getopaque l.pageno with
528 | None when not state.rendering ->
529 state.rendering <- true;
530 cache l.pageno ("", -1);
531 wcmd "render" [`i (l.pageno + 1)
532 ;`i l.pagedimno
533 ;`i l.pagew
534 ;`i l.pageh];
535 | _ -> ()
538 let loadlayout layout =
539 let rec f all = function
540 | l :: ls ->
541 begin match getopaque l.pageno with
542 | None -> render l; f false ls
543 | Some (opaque, _) -> f (all && validopaque opaque) ls
545 | [] -> all
547 f (layout <> []) layout;
550 let findpageforopaque opaque =
551 Hashtbl.fold
552 (fun k (v, s) a -> if v = opaque then Some (k, s) else a)
553 state.pagemap None
556 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
558 let preload () =
559 let oktopreload =
560 if conf.preload
561 then
562 let memleft = conf.memlimit - state.memused in
563 if memleft < 0
564 then
565 let opaque = cbpeek state.pagecache in
566 match findpageforopaque opaque with
567 | Some ((n, _, _, _, _), size) ->
568 memleft + size >= 0 && not (pagevisible state.layout n)
569 | None -> false
570 else true
571 else false
573 if oktopreload
574 then
575 let presentation = conf.presentation in
576 let interpagespace = conf.interpagespace in
577 let maxy = state.maxy in
578 conf.presentation <- false;
579 conf.interpagespace <- 0;
580 state.maxy <- calcheight ();
581 let y =
582 match state.layout with
583 | [] -> 0
584 | l :: _ -> getpagey l.pageno + l.pagey
586 let y = if y < conf.winh then 0 else y - conf.winh in
587 let pages = layout y (conf.winh*3) in
588 List.iter render pages;
589 conf.presentation <- presentation;
590 conf.interpagespace <- interpagespace;
591 state.maxy <- maxy;
594 let gotoy y =
595 let y = max 0 y in
596 let y = min state.maxy y in
597 let pages = layout y conf.winh in
598 let ready = loadlayout pages in
599 if conf.showall
600 then (
601 if ready
602 then (
603 state.y <- y;
604 state.layout <- pages;
605 state.throttle <- None;
606 Glut.postRedisplay ();
608 else (
609 state.throttle <- Some pages;
612 else (
613 state.y <- y;
614 state.layout <- pages;
615 state.throttle <- None;
616 Glut.postRedisplay ();
618 begin match state.mode with
619 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
620 if not (pagevisible pages pageno)
621 then (
622 match state.layout with
623 | [] -> ()
624 | l :: _ ->
625 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
627 | _ -> ()
628 end;
629 preload ();
632 let gotoy_and_clear_text y =
633 gotoy y;
634 if not conf.verbose then state.text <- "";
637 let emptyanchor = (0, 0.0);;
639 let getanchor () =
640 match state.layout with
641 | [] -> emptyanchor
642 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
645 let getanchory (n, top) =
646 let y, h = getpageyh n in
647 y + (truncate (top *. float h));
650 let gotoanchor anchor =
651 gotoy (getanchory anchor);
654 let addnav () =
655 cbput state.hists.nav (getanchor ());
658 let getnav () =
659 let anchor = cbgetc state.hists.nav ~-1 in
660 getanchory anchor;
663 let gotopage n top =
664 let y, h = getpageyh n in
665 gotoy_and_clear_text (y + (truncate (top *. float h)));
668 let gotopage1 n top =
669 let y = getpagey n in
670 gotoy_and_clear_text (y + top);
673 let invalidate () =
674 state.layout <- [];
675 state.pdims <- [];
676 state.rects <- [];
677 state.rects1 <- [];
678 state.invalidated <- state.invalidated + 1;
681 let scalecolor c =
682 let c = c *. state.colorscale in
683 (c, c, c);
686 let represent () =
687 state.maxy <- calcheight ();
688 match state.mode with
689 | Birdseye (_, _, pageno, _, _) ->
690 let y, h = getpageyh pageno in
691 let top = (conf.winh - h) / 2 in
692 gotoy (max 0 (y - top))
693 | _ -> gotoanchor state.anchor
696 let pagematrix () =
697 GlMat.mode `projection;
698 GlMat.load_identity ();
699 GlMat.rotate ~x:1.0 ~angle:180.0 ();
700 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
701 GlMat.scale3 (2.0 /. float state.w, 2.0 /. float conf.winh, 1.0);
702 if state.x != 0
703 then (
704 GlMat.translate ~x:(float state.x) ();
708 let winmatrix () =
709 GlMat.mode `projection;
710 GlMat.load_identity ();
711 GlMat.rotate ~x:1.0 ~angle:180.0 ();
712 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
713 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
716 let reshape ~w ~h =
717 if state.invalidated = 0
718 then state.anchor <- getanchor ();
720 conf.winw <- w;
721 let w = truncate (float w *. conf.zoom) - conf.scrollw in
722 let w = max w 2 in
723 state.w <- w;
724 conf.winh <- h;
725 GlMat.mode `modelview;
726 GlMat.load_identity ();
727 GlClear.color (scalecolor 1.0);
728 GlClear.clear [`color];
730 invalidate ();
731 wcmd "geometry" [`i w; `i h];
734 let showtext c s =
735 GlDraw.color (0.0, 0.0, 0.0);
736 GlDraw.rect
737 (0.0, float (conf.winh - 18))
738 (float (conf.winw - conf.scrollw - 1), float conf.winh)
740 let font = Glut.BITMAP_8_BY_13 in
741 GlDraw.color (1.0, 1.0, 1.0);
742 GlPix.raster_pos ~x:0.0 ~y:(float (conf.winh - 5)) ();
743 Glut.bitmapCharacter ~font ~c:(Char.code c);
744 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s;
747 let enttext () =
748 let len = String.length state.text in
749 match state.mode with
750 | Textentry ((c, text, _, _, _), _) ->
751 let s =
752 if len > 0
753 then
754 text ^ " [" ^ state.text ^ "]"
755 else
756 text
758 showtext c s;
760 | _ ->
761 if len > 0 then showtext ' ' state.text
764 let showtext c s =
765 state.text <- Printf.sprintf "%c%s" c s;
766 Glut.postRedisplay ();
769 let act cmd =
770 match cmd.[0] with
771 | 'c' ->
772 state.pdims <- [];
774 | 'D' ->
775 state.rects <- state.rects1;
776 Glut.postRedisplay ()
778 | 'C' ->
779 let n = Scanf.sscanf cmd "C %u" (fun n -> n) in
780 state.pagecount <- n;
781 state.invalidated <- state.invalidated - 1;
782 if state.invalidated = 0
783 then represent ()
785 | 't' ->
786 let s = Scanf.sscanf cmd "t %n"
787 (fun n -> String.sub cmd n (String.length cmd - n))
789 Glut.setWindowTitle s
791 | 'T' ->
792 let s = Scanf.sscanf cmd "T %n"
793 (fun n -> String.sub cmd n (String.length cmd - n))
795 if istextentry state.mode
796 then (
797 state.text <- s;
798 showtext ' ' s;
800 else (
801 state.text <- s;
802 Glut.postRedisplay ();
805 | 'V' ->
806 if conf.verbose
807 then
808 let s = Scanf.sscanf cmd "V %n"
809 (fun n -> String.sub cmd n (String.length cmd - n))
811 state.text <- s;
812 showtext ' ' s;
814 | 'F' ->
815 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
816 Scanf.sscanf cmd "F %u %d %f %f %f %f %f %f %f %f"
817 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
818 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
820 let y = (getpagey pageno) + truncate y0 in
821 addnav ();
822 gotoy y;
823 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
825 | 'R' ->
826 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
827 Scanf.sscanf cmd "R %u %d %f %f %f %f %f %f %f %f"
828 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
829 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
831 state.rects1 <-
832 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
834 | 'r' ->
835 let n, w, h, r, l, s, p =
836 Scanf.sscanf cmd "r %u %u %u %u %d %u %s"
837 (fun n w h r l s p ->
838 (n-1, w, h, r, l != 0, s, p))
841 Hashtbl.replace state.pagemap (n, w, r, l, state.gen) (p, s);
842 state.memused <- state.memused + s;
844 let layout =
845 match state.throttle with
846 | None -> state.layout
847 | Some layout -> layout
850 let rec gc () =
851 if (state.memused <= conf.memlimit) || cbempty state.pagecache
852 then ()
853 else (
854 let evictedopaque = cbpeek state.pagecache in
855 match findpageforopaque evictedopaque with
856 | None -> failwith "bug in gc"
857 | Some ((evictedn, _, _, _, gen) as k, evictedsize) ->
858 if state.gen != gen || not (pagevisible layout evictedn)
859 then (
860 wcmd "free" [`s evictedopaque];
861 state.memused <- state.memused - evictedsize;
862 Hashtbl.remove state.pagemap k;
863 cbdecr state.pagecache;
864 gc ();
868 gc ();
870 cbput state.pagecache p;
871 state.rendering <- false;
873 begin match state.throttle with
874 | None ->
875 if pagevisible state.layout n
876 then gotoy state.y
877 else (
878 let allvisible = loadlayout state.layout in
879 if allvisible then preload ();
882 | Some layout ->
883 match layout with
884 | [] -> ()
885 | l :: _ ->
886 let y = getpagey l.pageno + l.pagey in
887 gotoy y
890 | 'l' ->
891 let (n, w, h, x) as pdim =
892 Scanf.sscanf cmd "l %u %u %u %u" (fun n w h x -> n, w, h, x)
894 state.pdims <- pdim :: state.pdims
896 | 'o' ->
897 let (l, n, t, h, pos) =
898 Scanf.sscanf cmd "o %u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
900 let s = String.sub cmd pos (String.length cmd - pos) in
901 let s =
902 let l = String.length s in
903 let b = Buffer.create (String.length s) in
904 let rec loop pc2 i =
905 if i = l
906 then ()
907 else
908 let pc2 =
909 match s.[i] with
910 | '\xa0' when pc2 -> Buffer.add_char b ' '; false
911 | '\xc2' -> true
912 | c ->
913 let c = if Char.code c land 0x80 = 0 then c else '?' in
914 Buffer.add_char b c;
915 false
917 loop pc2 (i+1)
919 loop false 0;
920 Buffer.contents b
922 let outline = (s, l, n, float t /. float h) in
923 let outlines =
924 match state.outlines with
925 | Olist outlines -> Olist (outline :: outlines)
926 | Oarray _ -> Olist [outline]
927 | Onarrow _ -> Olist [outline]
929 state.outlines <- outlines
932 | 'i' ->
933 let s = Scanf.sscanf cmd "i %n"
934 (fun n -> String.sub cmd n (String.length cmd - n))
936 let len = String.length s in
937 let rec fold accu pos =
938 let eolpos =
939 try String.index_from s pos '\n' with Not_found -> len
941 if eolpos = len
942 then List.rev accu
943 else
944 let line = String.sub s pos (eolpos - pos) in
945 fold ((1, line)::accu) (eolpos+1)
947 state.docinfo <- fold state.docinfo 0
949 | _ ->
950 dolog "unknown cmd `%S'" cmd
953 let now = Unix.gettimeofday;;
955 let idle () =
956 let rec loop delay =
957 let r, _, _ = Unix.select [state.csock] [] [] delay in
958 begin match r with
959 | [] ->
960 if state.ascrollstep > 0
961 then begin
962 let y = state.y + state.ascrollstep in
963 let y = if y >= state.maxy then 0 else y in
964 gotoy y;
965 state.text <- "";
966 end;
968 | _ ->
969 let cmd = readcmd state.csock in
970 act cmd;
971 loop 0.0
972 end;
973 in loop 0.001
976 let onhist cb =
977 let rc = cb.rc in
978 let action = function
979 | HCprev -> cbget cb ~-1
980 | HCnext -> cbget cb 1
981 | HCfirst -> cbget cb ~-(cb.rc)
982 | HClast -> cbget cb (cb.len - 1 - cb.rc)
983 and cancel () = cb.rc <- rc
984 in (action, cancel)
987 let search pattern forward =
988 if String.length pattern > 0
989 then
990 let pn, py =
991 match state.layout with
992 | [] -> 0, 0
993 | l :: _ ->
994 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
996 let cmd =
997 let b = makecmd "search"
998 [`b conf.icase; `i pn; `i py; `i (if forward then 1 else 0)]
1000 Buffer.add_char b ',';
1001 Buffer.add_string b pattern;
1002 Buffer.add_char b '\000';
1003 Buffer.contents b;
1005 writecmd state.csock cmd;
1008 let intentry text key =
1009 let c = Char.unsafe_chr key in
1010 match c with
1011 | '0' .. '9' ->
1012 let s = "x" in s.[0] <- c;
1013 let text = text ^ s in
1014 TEcont text
1016 | _ ->
1017 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1018 TEcont text
1021 let addchar s c =
1022 let b = Buffer.create (String.length s + 1) in
1023 Buffer.add_string b s;
1024 Buffer.add_char b c;
1025 Buffer.contents b;
1028 let textentry text key =
1029 let c = Char.unsafe_chr key in
1030 match c with
1031 | _ when key >= 32 && key < 127 ->
1032 let text = addchar text c in
1033 TEcont text
1035 | _ ->
1036 dolog "unhandled key %d char `%c'" key (Char.unsafe_chr key);
1037 TEcont text
1040 let reinit angle proportional =
1041 conf.angle <- angle;
1042 conf.proportional <- proportional;
1043 invalidate ();
1044 wcmd "reinit" [`i angle; `b proportional];
1047 let setzoom zoom =
1048 let zoom = max 0.01 (min 2.2 zoom) in
1049 if zoom <> conf.zoom
1050 then (
1051 if zoom <= 1.0
1052 then state.x <- 0;
1053 conf.zoom <- zoom;
1054 reshape conf.winw conf.winh;
1055 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
1059 let enterbirdseye () =
1060 let zoom = float conf.thumbw /. float conf.winw in
1061 let birdseyepageno =
1062 let rec fold candidate = function
1063 | [] -> candidate
1064 | l :: _ when l.pagey = 0 -> l.pageno
1065 | l :: rest -> fold l.pageno rest
1067 fold 0 state.layout
1069 state.mode <- Birdseye (
1070 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
1072 conf.zoom <- zoom;
1073 conf.presentation <- false;
1074 conf.interpagespace <- 10;
1075 conf.hlinks <- false;
1076 state.x <- 0;
1077 state.mstate <- Mnone;
1078 conf.showall <- false;
1079 Glut.setCursor Glut.CURSOR_INHERIT;
1080 if conf.verbose
1081 then
1082 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1083 (100.0*.zoom)
1084 else
1085 state.text <- ""
1087 reshape conf.winw conf.winh;
1090 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1091 state.mode <- View;
1092 conf.zoom <- c.zoom;
1093 conf.presentation <- c.presentation;
1094 conf.interpagespace <- c.interpagespace;
1095 conf.showall <- c.showall;
1096 conf.hlinks <- c.hlinks;
1097 state.x <- leftx;
1098 if conf.verbose
1099 then
1100 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1101 (100.0*.conf.zoom)
1103 reshape conf.winw conf.winh;
1104 state.anchor <- if goback then anchor else (pageno, 0.0);
1107 let togglebirdseye () =
1108 match state.mode with
1109 | Birdseye vals -> leavebirdseye vals true
1110 | View | Outline _ -> enterbirdseye ()
1111 | _ -> ()
1114 let optentry text key =
1115 let btos b = if b then "on" else "off" in
1116 let c = Char.unsafe_chr key in
1117 match c with
1118 | 's' ->
1119 let ondone s =
1120 try conf.scrollstep <- int_of_string s with exc ->
1121 state.text <- Printf.sprintf "bad integer `%s': %s"
1122 s (Printexc.to_string exc)
1124 TEswitch ('#', "", None, intentry, ondone)
1126 | 'A' ->
1127 let ondone s =
1129 conf.autoscrollstep <- int_of_string s;
1130 if state.ascrollstep > 0
1131 then state.ascrollstep <- conf.autoscrollstep;
1132 with exc ->
1133 state.text <- Printf.sprintf "bad integer `%s': %s"
1134 s (Printexc.to_string exc)
1136 TEswitch ('*', "", None, intentry, ondone)
1138 | 'Z' ->
1139 let ondone s =
1141 let zoom = float (int_of_string s) /. 100.0 in
1142 setzoom zoom
1143 with exc ->
1144 state.text <- Printf.sprintf "bad integer `%s': %s"
1145 s (Printexc.to_string exc)
1147 TEswitch ('@', "", None, intentry, ondone)
1149 | 't' ->
1150 let ondone s =
1152 conf.thumbw <- max 2 (min 1920 (int_of_string s));
1153 state.text <-
1154 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1155 begin match state.mode with
1156 | Textentry (_, Birdseye beye) ->
1157 leavebirdseye beye false;
1158 enterbirdseye ()
1159 | _ -> ()
1160 end;
1161 with exc ->
1162 state.text <- Printf.sprintf "bad integer `%s': %s"
1163 s (Printexc.to_string exc)
1165 TEswitch ('$', "", None, intentry, ondone)
1167 | 'R' ->
1168 let ondone s =
1169 match try
1170 Some (int_of_string s)
1171 with exc ->
1172 state.text <- Printf.sprintf "bad integer `%s': %s"
1173 s (Printexc.to_string exc);
1174 None
1175 with
1176 | Some angle -> reinit angle conf.proportional
1177 | None -> ()
1179 TEswitch ('^', "", None, intentry, ondone)
1181 | 'i' ->
1182 conf.icase <- not conf.icase;
1183 TEdone ("case insensitive search " ^ (btos conf.icase))
1185 | 'p' ->
1186 conf.preload <- not conf.preload;
1187 gotoy state.y;
1188 TEdone ("preload " ^ (btos conf.preload))
1190 | 'v' ->
1191 conf.verbose <- not conf.verbose;
1192 TEdone ("verbose " ^ (btos conf.verbose))
1194 | 'h' ->
1195 conf.maxhfit <- not conf.maxhfit;
1196 state.maxy <- state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
1197 TEdone ("maxhfit " ^ (btos conf.maxhfit))
1199 | 'c' ->
1200 conf.crophack <- not conf.crophack;
1201 TEdone ("crophack " ^ btos conf.crophack)
1203 | 'a' ->
1204 conf.showall <- not conf.showall;
1205 TEdone ("showall " ^ btos conf.showall)
1207 | 'f' ->
1208 conf.underinfo <- not conf.underinfo;
1209 TEdone ("underinfo " ^ btos conf.underinfo)
1211 | 'P' ->
1212 conf.savebmarks <- not conf.savebmarks;
1213 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
1215 | 'S' ->
1216 let ondone s =
1218 let pageno, py =
1219 match state.layout with
1220 | [] -> 0, 0
1221 | l :: _ ->
1222 l.pageno, l.pagey
1224 conf.interpagespace <- int_of_string s;
1225 state.maxy <- calcheight ();
1226 let y = getpagey pageno in
1227 gotoy (y + py)
1228 with exc ->
1229 state.text <- Printf.sprintf "bad integer `%s': %s"
1230 s (Printexc.to_string exc)
1232 TEswitch ('%', "", None, intentry, ondone)
1234 | 'l' ->
1235 reinit conf.angle (not conf.proportional);
1236 TEdone ("proprortional display " ^ btos conf.proportional)
1238 | _ ->
1239 state.text <- Printf.sprintf "bad option %d `%c'" key c;
1240 TEstop
1243 let maxoutlinerows () = (conf.winh - 31) / 16;;
1245 let enterselector allowdel outlines errmsg msg =
1246 if Array.length outlines = 0
1247 then (
1248 showtext ' ' errmsg;
1250 else (
1251 state.text <- msg;
1252 Glut.setCursor Glut.CURSOR_INHERIT;
1253 let pageno =
1254 match state.layout with
1255 | [] -> -1
1256 | {pageno=pageno} :: rest -> pageno
1258 let active =
1259 let rec loop n =
1260 if n = Array.length outlines
1261 then 0
1262 else
1263 let (_, _, outlinepageno, _) = outlines.(n) in
1264 if outlinepageno >= pageno then n else loop (n+1)
1266 loop 0
1268 state.mode <- Outline
1269 (allowdel, active, max 0 (active - maxoutlinerows () / 2), outlines, "", 0);
1270 Glut.postRedisplay ();
1274 let enteroutlinemode () =
1275 let outlines, msg =
1276 match state.outlines with
1277 | Oarray a -> a, ""
1278 | Olist l ->
1279 let a = Array.of_list (List.rev l) in
1280 state.outlines <- Oarray a;
1281 a, ""
1282 | Onarrow (pat, a, b) ->
1283 a, "Outline was narrowed to `" ^ pat ^ "' (Ctrl-u to restore)"
1285 enterselector false outlines "Document has no outline" msg;
1288 let enterbookmarkmode () =
1289 let bookmarks = Array.of_list state.bookmarks in
1290 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1293 let enterinfomode () =
1294 let btos = function true -> "on" | _ -> "off" in
1295 let pageno, top = getanchor () in
1296 let info =
1297 let autoscrollstep =
1298 if state.ascrollstep > 0
1299 then state.ascrollstep
1300 else conf.autoscrollstep
1302 (0, "Current parameters")
1303 :: (1, "version " ^ Help.version)
1304 :: (1, "presentation mode " ^ btos conf.presentation)
1305 :: (1, "case insensitive search " ^ btos conf.icase)
1306 :: (1, "preload " ^ btos conf.preload)
1307 :: (1, "page bias " ^ string_of_int conf.pagebias)
1308 :: (1, "verbose " ^ btos conf.verbose)
1309 :: (1, "scroll step " ^ string_of_int conf.scrollstep)
1310 :: (1, "max fit " ^ btos conf.maxhfit)
1311 :: (1, "crop hack " ^ btos conf.crophack)
1312 :: (1, "autoscroll step " ^ string_of_int autoscrollstep)
1313 :: (1, "throttle " ^ btos conf.showall)
1314 :: (1, "highlight links " ^ btos conf.hlinks)
1315 :: (1, "under info " ^ btos conf.underinfo)
1316 :: (1, "veritcal margin " ^ string_of_int conf.interpagespace)
1317 :: (1, "zoom " ^ Printf.sprintf "%-5.1f" (conf.zoom*.100.))
1318 :: (1, "rotation " ^ string_of_int conf.angle)
1319 :: (1, "persistent bookmarks " ^ btos conf.savebmarks)
1320 :: (1, "proportional display " ^ btos conf.proportional)
1321 :: (1, "pixmap cache size " ^ string_of_int conf.memlimit)
1322 :: (1, "pixmap cache used " ^ string_of_int state.memused)
1323 :: (1, "thumbnail width " ^ string_of_int conf.thumbw)
1324 :: (1, Printf.sprintf "window dimensions %dx%d " conf.winw conf.winh)
1325 :: (0, "Document information")
1326 :: (1, string_of_int state.pagecount ^ " pages")
1327 :: state.docinfo
1329 let o =
1330 let o = Array.create (List.length info) ("", 0, pageno, top) in
1331 let rec iteri i = function
1332 | [] -> ()
1333 | (l, s) :: rest ->
1334 o.(i) <- (s, l, pageno, top);
1335 iteri (i+1) rest
1337 iteri 0 info;
1340 enterselector false o "Info not available" "";
1343 let enterhelpmode () =
1344 let pageno, top = getanchor () in
1345 let o =
1346 let help = ("Keys for llpp " ^ Help.version) ::state.help in
1347 let o = Array.create (List.length help) ("", 0, pageno, top) in
1348 let rec iteri i = function
1349 | [] -> ()
1350 | s :: rest ->
1351 o.(i) <- (s, (if i = 0 then 0 else 1), pageno, top);
1352 iteri (i+1) rest
1354 iteri 0 help;
1357 enterselector false o "Help not available" "";
1360 let quickbookmark ?title () =
1361 match state.layout with
1362 | [] -> ()
1363 | l :: _ ->
1364 let title =
1365 match title with
1366 | None ->
1367 let sec = Unix.gettimeofday () in
1368 let tm = Unix.localtime sec in
1369 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1370 (l.pageno+1)
1371 tm.Unix.tm_mday
1372 tm.Unix.tm_mon
1373 (tm.Unix.tm_year + 1900)
1374 tm.Unix.tm_hour
1375 tm.Unix.tm_min
1376 | Some title -> title
1378 state.bookmarks <-
1379 (title, 0, l.pageno, float l.pagey /. float l.pageh) :: state.bookmarks
1382 let doreshape w h =
1383 state.fullscreen <- None;
1384 Glut.reshapeWindow w h;
1387 let writeopen path password =
1388 writecmd state.csock ("open " ^ path ^ "\000" ^ state.password ^ "\000");
1389 writecmd state.csock "info";
1392 let opendoc path password =
1393 invalidate ();
1394 state.path <- path;
1395 state.password <- password;
1396 state.gen <- state.gen + 1;
1398 writeopen path password;
1399 Glut.setWindowTitle ("llpp " ^ Filename.basename path);
1400 wcmd "geometry" [`i state.w; `i conf.winh];
1403 let viewkeyboard ~key ~x ~y =
1404 let enttext te =
1405 state.mode <- Textentry (te, state.mode);
1406 state.text <- "";
1407 enttext ();
1408 Glut.postRedisplay ()
1410 let c = Char.chr key in
1411 match c with
1412 | '\027' | 'q' ->
1413 exit 0
1415 | '\008' ->
1416 let y = getnav () in
1417 gotoy_and_clear_text y
1419 | 'o' ->
1420 enteroutlinemode ()
1422 | 'u' ->
1423 state.rects <- [];
1424 state.text <- "";
1425 Glut.postRedisplay ()
1427 | '/' | '?' ->
1428 let ondone isforw s =
1429 cbput state.hists.pat s;
1430 state.searchpattern <- s;
1431 search s isforw
1433 enttext (c, "", Some (onhist state.hists.pat),
1434 textentry, ondone (c ='/'))
1436 | '+' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1437 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1438 setzoom (min 2.2 (conf.zoom +. incr))
1440 | '+' ->
1441 let ondone s =
1442 let n =
1443 try int_of_string s with exc ->
1444 state.text <- Printf.sprintf "bad integer `%s': %s"
1445 s (Printexc.to_string exc);
1446 max_int
1448 if n != max_int
1449 then (
1450 conf.pagebias <- n;
1451 state.text <- "page bias is now " ^ string_of_int n;
1454 enttext ('+', "", None, intentry, ondone)
1456 | '-' when Glut.getModifiers () land Glut.active_ctrl != 0 ->
1457 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1458 setzoom (max 0.01 (conf.zoom -. decr))
1460 | '-' ->
1461 let ondone msg =
1462 state.text <- msg;
1464 enttext ('-', "", None, optentry, ondone)
1466 | '0' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1467 setzoom 1.0
1469 | '1' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1470 let zoom = zoomforh conf.winw conf.winh conf.scrollw in
1471 if zoom < 1.0
1472 then setzoom zoom
1474 | '9' when (Glut.getModifiers () land Glut.active_ctrl != 0) ->
1475 togglebirdseye ()
1477 | '0' .. '9' ->
1478 let ondone s =
1479 let n =
1480 try int_of_string s with exc ->
1481 state.text <- Printf.sprintf "bad integer `%s': %s"
1482 s (Printexc.to_string exc);
1485 if n >= 0
1486 then (
1487 addnav ();
1488 cbput state.hists.pag (string_of_int n);
1489 gotoy_and_clear_text (getpagey (n + conf.pagebias - 1))
1492 let pageentry text key =
1493 match Char.unsafe_chr key with
1494 | 'g' -> TEdone text
1495 | _ -> intentry text key
1497 let text = "x" in text.[0] <- c;
1498 enttext (':', text, Some (onhist state.hists.pag), pageentry, ondone)
1500 | 'b' ->
1501 conf.scrollw <- if conf.scrollw > 0 then 0 else defconf.scrollw;
1502 reshape conf.winw conf.winh;
1504 | 'l' ->
1505 conf.hlinks <- not conf.hlinks;
1506 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
1507 Glut.postRedisplay ()
1509 | 'a' ->
1510 if state.ascrollstep = 0
1511 then state.ascrollstep <- conf.autoscrollstep
1512 else (
1513 conf.autoscrollstep <- state.ascrollstep;
1514 state.ascrollstep <- 0;
1517 | 'P' ->
1518 conf.presentation <- not conf.presentation;
1519 showtext ' ' ("presentation mode " ^
1520 if conf.presentation then "on" else "off");
1521 state.anchor <- getanchor ();
1522 represent ()
1524 | 'f' ->
1525 begin match state.fullscreen with
1526 | None ->
1527 state.fullscreen <- Some (conf.winw, conf.winh);
1528 Glut.fullScreen ()
1529 | Some (w, h) ->
1530 state.fullscreen <- None;
1531 doreshape w h
1534 | 'g' ->
1535 gotoy_and_clear_text 0
1537 | 'n' ->
1538 search state.searchpattern true
1540 | 'p' | 'N' ->
1541 search state.searchpattern false
1543 | 't' ->
1544 begin match state.layout with
1545 | [] -> ()
1546 | l :: _ ->
1547 gotoy_and_clear_text (getpagey l.pageno)
1550 | ' ' ->
1551 begin match List.rev state.layout with
1552 | [] -> ()
1553 | l :: _ ->
1554 let pageno = min (l.pageno+1) (state.pagecount-1) in
1555 gotoy_and_clear_text (getpagey pageno)
1558 | '\127' ->
1559 begin match state.layout with
1560 | [] -> ()
1561 | l :: _ ->
1562 let pageno = max 0 (l.pageno-1) in
1563 gotoy_and_clear_text (getpagey pageno)
1566 | '=' ->
1567 let f (fn, ln) l =
1568 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1570 let fn, ln = List.fold_left f (-1, -1) state.layout in
1571 let s =
1572 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1573 let percent =
1574 if maxy <= 0
1575 then 100.
1576 else (100. *. (float state.y /. float maxy)) in
1577 if fn = ln
1578 then
1579 Printf.sprintf "Page %d of %d %.2f%%"
1580 (fn+1) state.pagecount percent
1581 else
1582 Printf.sprintf
1583 "Pages %d-%d of %d %.2f%%"
1584 (fn+1) (ln+1) state.pagecount percent
1586 showtext ' ' s;
1588 | 'w' ->
1589 begin match state.layout with
1590 | [] -> ()
1591 | l :: _ ->
1592 doreshape (l.pagew + conf.scrollw) l.pageh;
1593 Glut.postRedisplay ();
1596 | '\'' ->
1597 enterbookmarkmode ()
1599 | 'h' ->
1600 enterhelpmode ()
1602 | 'i' ->
1603 enterinfomode ()
1605 | 'm' ->
1606 let ondone s =
1607 match state.layout with
1608 | l :: _ ->
1609 state.bookmarks <-
1610 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1611 :: state.bookmarks
1612 | _ -> ()
1614 enttext ('~', "", None, textentry, ondone)
1616 | '~' ->
1617 quickbookmark ();
1618 showtext ' ' "Quick bookmark added";
1620 | 'z' ->
1621 begin match state.layout with
1622 | l :: _ ->
1623 let rect = getpdimrect l.pagedimno in
1624 let w, h =
1625 if conf.crophack
1626 then
1627 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1628 truncate (1.2 *. (rect.(3) -. rect.(0))))
1629 else
1630 (truncate (rect.(1) -. rect.(0)),
1631 truncate (rect.(3) -. rect.(0)))
1633 if w != 0 && h != 0
1634 then
1635 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1637 Glut.postRedisplay ();
1639 | [] -> ()
1642 | '<' | '>' ->
1643 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1645 | '[' | ']' ->
1646 state.colorscale <-
1647 max 0.0
1648 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1649 Glut.postRedisplay ()
1651 | 'k' -> gotoy (clamp (-conf.scrollstep))
1652 | 'j' -> gotoy (clamp conf.scrollstep)
1654 | 'r' -> opendoc state.path state.password
1656 | _ ->
1657 vlog "huh? %d %c" key (Char.chr key);
1660 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1661 let enttext te =
1662 state.mode <- Textentry (te, mode);
1663 state.text <- "";
1664 enttext ();
1665 Glut.postRedisplay ()
1667 match Char.unsafe_chr key with
1668 | '\008' ->
1669 let len = String.length text in
1670 if len = 0
1671 then (
1672 state.mode <- mode;
1673 Glut.postRedisplay ();
1675 else (
1676 let s = String.sub text 0 (len - 1) in
1677 enttext (c, s, opthist, onkey, ondone)
1680 | '\r' | '\n' ->
1681 ondone text;
1682 state.mode <- mode;
1683 Glut.postRedisplay ()
1685 | '\027' ->
1686 begin match opthist with
1687 | None -> ()
1688 | Some (_, onhistcancel) -> onhistcancel ()
1689 end;
1690 state.mode <- View;
1691 Glut.postRedisplay ()
1693 | _ ->
1694 begin match onkey text key with
1695 | TEdone text ->
1696 state.mode <- mode;
1697 ondone text;
1698 Glut.postRedisplay ()
1700 | TEcont text ->
1701 enttext (c, text, opthist, onkey, ondone);
1703 | TEstop ->
1704 state.mode <- mode;
1705 Glut.postRedisplay ()
1707 | TEswitch te ->
1708 state.mode <- Textentry (te, mode);
1709 Glut.postRedisplay ()
1710 end;
1713 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1714 match key with
1715 | 27 ->
1716 leavebirdseye beye true
1718 | 12 ->
1719 let y, h = getpageyh pageno in
1720 let top = (conf.winh - h) / 2 in
1721 gotoy (max 0 (y - top))
1723 | 13 ->
1724 leavebirdseye beye false
1726 | _ ->
1727 viewkeyboard ~key ~x ~y
1730 let outlinekeyboard ~key ~x ~y
1731 (allowdel, active, first, outlines, qsearch, pan) =
1732 let narrow outlines pattern =
1733 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1734 match reopt with
1735 | None -> None
1736 | Some re ->
1737 let rec fold accu n =
1738 if n = -1
1739 then accu
1740 else
1741 let (s, _, _, _) as o = outlines.(n) in
1742 let accu =
1743 if (try ignore (Str.search_forward re s 0); true
1744 with Not_found -> false)
1745 then (o :: accu)
1746 else accu
1748 fold accu (n-1)
1750 let matched = fold [] (Array.length outlines - 1) in
1751 if matched = [] then None else Some (Array.of_list matched)
1753 let search active pattern incr =
1754 let dosearch re =
1755 let rec loop n =
1756 if n = Array.length outlines || n = -1
1757 then None
1758 else
1759 let (s, _, _, _) = outlines.(n) in
1761 (try ignore (Str.search_forward re s 0); true
1762 with Not_found -> false)
1763 then Some n
1764 else loop (n + incr)
1766 loop active
1769 let re = Str.regexp_case_fold pattern in
1770 dosearch re
1771 with Failure s ->
1772 state.text <- s;
1773 None
1775 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1776 match key with
1777 | 27 ->
1778 if String.length qsearch = 0
1779 then (
1780 state.text <- "";
1781 state.mode <- View;
1782 Glut.postRedisplay ();
1784 else (
1785 state.text <- "";
1786 state.mode <- Outline (allowdel, active, first, outlines, "", pan);
1787 Glut.postRedisplay ();
1790 | 18 | 19 ->
1791 let incr = if key = 18 then -1 else 1 in
1792 let active, first =
1793 match search (active + incr) qsearch incr with
1794 | None ->
1795 state.text <- qsearch ^ " [not found]";
1796 active, first
1797 | Some active ->
1798 state.text <- qsearch;
1799 active, firstof active
1801 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
1802 Glut.postRedisplay ();
1804 | 8 ->
1805 let len = String.length qsearch in
1806 if len = 0
1807 then ()
1808 else (
1809 if len = 1
1810 then (
1811 state.text <- "";
1812 state.mode <- Outline (allowdel, active, first, outlines, "", pan);
1814 else
1815 let qsearch = String.sub qsearch 0 (len - 1) in
1816 let active, first =
1817 match search active qsearch ~-1 with
1818 | None ->
1819 state.text <- qsearch ^ " [not found]";
1820 active, first
1821 | Some active ->
1822 state.text <- qsearch;
1823 active, firstof active
1825 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
1827 Glut.postRedisplay ()
1829 | 13 ->
1830 if active < Array.length outlines
1831 then (
1832 let (_, _, n, t) = outlines.(active) in
1833 addnav ();
1834 gotopage n t;
1836 state.text <- "";
1837 if allowdel then state.bookmarks <- Array.to_list outlines;
1838 state.mode <- View;
1839 Glut.postRedisplay ();
1841 | _ when key >= 32 && key < 127 ->
1842 let pattern = addchar qsearch (Char.chr key) in
1843 let active, first =
1844 match search active pattern 1 with
1845 | None ->
1846 state.text <- pattern ^ " [not found]";
1847 active, first
1848 | Some active ->
1849 state.text <- pattern;
1850 active, firstof active
1852 state.mode <- Outline (allowdel, active, first, outlines, pattern, pan);
1853 Glut.postRedisplay ()
1855 | 14 when not allowdel -> (* ctrl-n *)
1856 if String.length qsearch > 0
1857 then (
1858 let optoutlines = narrow outlines qsearch in
1859 begin match optoutlines with
1860 | None -> state.text <- "can't narrow"
1861 | Some outlines ->
1862 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch, pan);
1863 match state.outlines with
1864 | Olist l -> ()
1865 | Oarray a ->
1866 state.outlines <- Onarrow (qsearch, outlines, a)
1867 | Onarrow (pat, a, b) ->
1868 state.outlines <- Onarrow (qsearch, outlines, b)
1869 end;
1871 Glut.postRedisplay ()
1873 | 21 when not allowdel -> (* ctrl-u *)
1874 let outline =
1875 match state.outlines with
1876 | Oarray a -> a
1877 | Olist l ->
1878 let a = Array.of_list (List.rev l) in
1879 state.outlines <- Oarray a;
1881 | Onarrow (pat, a, b) ->
1882 state.outlines <- Oarray b;
1883 state.text <- "";
1886 state.mode <- Outline (allowdel, 0, 0, outline, qsearch, pan);
1887 Glut.postRedisplay ()
1889 | 12 ->
1890 state.mode <- Outline
1891 (allowdel, active, firstof active, outlines, qsearch, pan);
1892 Glut.postRedisplay ()
1894 | 127 when allowdel ->
1895 let len = Array.length outlines - 1 in
1896 if len = 0
1897 then (
1898 state.mode <- View;
1899 state.bookmarks <- [];
1901 else (
1902 let bookmarks = Array.init len
1903 (fun i ->
1904 let i = if i >= active then i + 1 else i in
1905 outlines.(i)
1908 state.mode <-
1909 Outline (
1910 allowdel,
1911 min active (len-1),
1912 min first (len-1),
1913 bookmarks, qsearch,
1917 Glut.postRedisplay ()
1919 | _ -> dolog "unknown key %d" key
1922 let keyboard ~key ~x ~y =
1923 if key = 7
1924 then
1925 wcmd "interrupt" []
1926 else
1927 match state.mode with
1928 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1929 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1930 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1931 | View -> viewkeyboard ~key ~x ~y
1934 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1935 match key with
1936 | Glut.KEY_UP ->
1937 let pageno = max 0 (pageno - 1) in
1938 let rec loop = function
1939 | [] -> gotopage1 pageno 0
1940 | l :: _ when l.pageno = pageno ->
1941 if l.pagedispy >= 0 && l.pagey = 0
1942 then Glut.postRedisplay ()
1943 else gotopage1 pageno 0
1944 | _ :: rest -> loop rest
1946 loop state.layout;
1947 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1949 | Glut.KEY_DOWN ->
1950 let pageno = min (state.pagecount - 1) (pageno + 1) in
1951 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1952 let rec loop = function
1953 | [] ->
1954 let y, h = getpageyh pageno in
1955 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1956 gotoy (clamp dy)
1957 | l :: rest when l.pageno = pageno ->
1958 if l.pagevh != l.pageh
1959 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1960 else Glut.postRedisplay ()
1961 | l :: rest -> loop rest
1963 loop state.layout
1965 | Glut.KEY_PAGE_UP ->
1966 begin match state.layout with
1967 | l :: _ ->
1968 if l.pagey != 0
1969 then (
1970 state.mode <- Birdseye (
1971 conf, leftx, l.pageno, hooverpageno, anchor
1973 gotopage1 l.pageno 0;
1975 else (
1976 let layout = layout (state.y-conf.winh) conf.winh in
1977 match layout with
1978 | [] -> gotoy (clamp (-conf.winh))
1979 | l :: _ ->
1980 state.mode <- Birdseye (
1981 conf, leftx, l.pageno, hooverpageno, anchor
1983 gotopage1 l.pageno 0
1986 | [] -> gotoy (clamp (-conf.winh))
1987 end;
1989 | Glut.KEY_PAGE_DOWN ->
1990 begin match List.rev state.layout with
1991 | l :: _ ->
1992 let layout = layout (state.y + conf.winh) conf.winh in
1993 begin match layout with
1994 | [] ->
1995 let incr = l.pageh - l.pagevh in
1996 if incr = 0
1997 then (
1998 state.mode <-
1999 Birdseye (
2000 conf, leftx, state.pagecount - 1, hooverpageno, anchor
2002 Glut.postRedisplay ();
2004 else gotoy (clamp (incr + conf.interpagespace*2));
2006 | l :: _ ->
2007 state.mode <-
2008 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2009 gotopage1 l.pageno 0;
2012 | [] -> gotoy (clamp conf.winh)
2013 end;
2015 | Glut.KEY_HOME ->
2016 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2017 gotopage1 0 0
2019 | Glut.KEY_END ->
2020 let pageno = state.pagecount - 1 in
2021 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2022 if not (pagevisible state.layout pageno)
2023 then
2024 let h =
2025 match List.rev state.pdims with
2026 | [] -> conf.winh
2027 | (_, _, h, _) :: _ -> h
2029 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2030 else Glut.postRedisplay ();
2031 | _ -> ()
2034 let setautoscrollspeed goingdown =
2035 let incr = max 1 (state.ascrollstep / 2) in
2036 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2037 state.ascrollstep <- astep;
2040 let special ~key ~x ~y =
2041 match state.mode with
2042 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2043 togglebirdseye ()
2045 | Birdseye vals ->
2046 birdseyespecial key x y vals
2048 | View when key = Glut.KEY_F1 ->
2049 enterhelpmode ()
2051 | View ->
2052 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2053 then setautoscrollspeed (key = Glut.KEY_DOWN)
2054 else
2055 let y =
2056 match key with
2057 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2058 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2059 | Glut.KEY_DOWN -> clamp conf.scrollstep
2060 | Glut.KEY_PAGE_UP ->
2061 if Glut.getModifiers () land Glut.active_ctrl != 0
2062 then
2063 match state.layout with
2064 | [] -> state.y
2065 | l :: _ -> state.y - l.pagey
2066 else
2067 clamp (-conf.winh)
2068 | Glut.KEY_PAGE_DOWN ->
2069 if Glut.getModifiers () land Glut.active_ctrl != 0
2070 then
2071 match List.rev state.layout with
2072 | [] -> state.y
2073 | l :: _ -> getpagey l.pageno
2074 else
2075 clamp conf.winh
2076 | Glut.KEY_HOME -> addnav (); 0
2077 | Glut.KEY_END ->
2078 addnav ();
2079 state.maxy - (if conf.maxhfit then conf.winh else 0)
2081 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2082 state.x <- state.x - 10;
2083 state.y
2084 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2085 state.x <- state.x + 10;
2086 state.y
2088 | _ -> state.y
2090 gotoy_and_clear_text y
2092 | Textentry
2093 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
2094 let s =
2095 match key with
2096 | Glut.KEY_UP -> action HCprev
2097 | Glut.KEY_DOWN -> action HCnext
2098 | Glut.KEY_HOME -> action HCfirst
2099 | Glut.KEY_END -> action HClast
2100 | _ -> state.text
2102 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2103 Glut.postRedisplay ()
2105 | Textentry _ -> ()
2107 | Outline (allowdel, active, first, outlines, qsearch, pan) ->
2108 let maxrows = maxoutlinerows () in
2109 let calcfirst first active =
2110 if active > first
2111 then
2112 let rows = active - first in
2113 if rows > maxrows then active - maxrows else first
2114 else active
2116 let navigate incr =
2117 let active = active + incr in
2118 let active = max 0 (min active (Array.length outlines - 1)) in
2119 let first = calcfirst first active in
2120 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
2121 Glut.postRedisplay ()
2123 let updownlevel incr =
2124 let len = Array.length outlines in
2125 let (_, curlevel, _, _) = outlines.(active) in
2126 let rec flow i =
2127 if i = len then i-1 else if i = -1 then 0 else
2128 let (_, l, _, _) = outlines.(i) in
2129 if l != curlevel then i else flow (i+incr)
2131 let active = flow active in
2132 let first = calcfirst first active in
2133 state.mode <- Outline (allowdel, active, first, outlines, qsearch, pan);
2134 Glut.postRedisplay ()
2136 match key with
2137 | Glut.KEY_UP -> navigate ~-1
2138 | Glut.KEY_DOWN -> navigate 1
2139 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2140 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2142 | Glut.KEY_RIGHT ->
2143 if Glut.active_ctrl != 0
2144 then (
2145 state.mode <- Outline (
2146 allowdel, active, first, outlines, qsearch, pan + 1
2148 Glut.postRedisplay ();
2150 else (
2151 if not allowdel
2152 then updownlevel 1
2155 | Glut.KEY_LEFT ->
2156 if Glut.active_ctrl != 0
2157 then (
2158 state.mode <- Outline (
2159 allowdel, active, first, outlines, qsearch, pan - 1
2161 Glut.postRedisplay ();
2163 else (
2164 if not allowdel
2165 then updownlevel ~-1
2168 | Glut.KEY_HOME ->
2169 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch, pan);
2170 Glut.postRedisplay ()
2172 | Glut.KEY_END ->
2173 let active = Array.length outlines - 1 in
2174 let first = max 0 (active - maxrows) in
2175 state.mode <- Outline (
2176 allowdel, active, first, outlines, qsearch, pan
2178 Glut.postRedisplay ()
2180 | _ -> ()
2183 let drawplaceholder l =
2184 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2185 GlDraw.rect
2186 (float l.pagex, float l.pagedispy)
2187 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2189 let x = float (if margin < 0 then -margin else l.pagex)
2190 and y = float (l.pagedispy + 13) in
2191 let font = Glut.BITMAP_8_BY_13 in
2192 GlDraw.color (0.0, 0.0, 0.0);
2193 GlPix.raster_pos ~x ~y ();
2194 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2195 ("Loading " ^ string_of_int (l.pageno + 1));
2198 let now () = Unix.gettimeofday ();;
2200 let drawpage l =
2201 let color =
2202 match state.mode with
2203 | Textentry _ -> scalecolor 0.4
2204 | View | Outline _ -> scalecolor 1.0
2205 | Birdseye (_, _, pageno, hooverpageno, _) ->
2206 if l.pageno = hooverpageno
2207 then scalecolor 0.9
2208 else (
2209 if l.pageno = pageno
2210 then scalecolor 1.0
2211 else scalecolor 0.8
2214 GlDraw.color color;
2215 begin match getopaque l.pageno with
2216 | Some (opaque, _) when validopaque opaque ->
2217 let a = now () in
2218 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2219 opaque;
2220 let b = now () in
2221 let d = b-.a in
2222 vlog "draw %d %f sec" l.pageno d;
2224 | _ ->
2225 drawplaceholder l;
2226 end;
2229 let scrollph y =
2230 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2231 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2232 let sh = float conf.winh /. sh in
2233 let sh = max sh (float conf.scrollh) in
2235 let percent =
2236 if state.y = state.maxy
2237 then 1.0
2238 else float y /. float maxy
2240 let position = (float conf.winh -. sh) *. percent in
2242 let position =
2243 if position +. sh > float conf.winh
2244 then float conf.winh -. sh
2245 else position
2247 position, sh;
2250 let scrollindicator () =
2251 GlDraw.color (0.64 , 0.64, 0.64);
2252 GlDraw.rect
2253 (float (conf.winw - conf.scrollw), 0.)
2254 (float conf.winw, float conf.winh)
2256 GlDraw.color (0.0, 0.0, 0.0);
2258 let position, sh = scrollph state.y in
2259 GlDraw.rect
2260 (float (conf.winw - conf.scrollw), position)
2261 (float conf.winw, position +. sh)
2265 let showsel margin =
2266 match state.mstate with
2267 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2270 | Msel ((x0, y0), (x1, y1)) ->
2271 let rec loop = function
2272 | l :: ls ->
2273 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2274 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2275 then
2276 match getopaque l.pageno with
2277 | Some (opaque, _) when validopaque opaque ->
2278 let oy = -l.pagey + l.pagedispy in
2279 seltext opaque
2280 (x0 - margin - state.x, y0,
2281 x1 - margin - state.x, y1) oy;
2283 | _ -> ()
2284 else loop ls
2285 | [] -> ()
2287 loop state.layout
2290 let showrects () =
2291 let panx = float state.x in
2292 Gl.enable `blend;
2293 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2294 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2295 List.iter
2296 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2297 List.iter (fun l ->
2298 if l.pageno = pageno
2299 then (
2300 let d = float (l.pagedispy - l.pagey) in
2301 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2302 GlDraw.begins `quads;
2304 GlDraw.vertex2 (x0+.panx, y0+.d);
2305 GlDraw.vertex2 (x1+.panx, y1+.d);
2306 GlDraw.vertex2 (x2+.panx, y2+.d);
2307 GlDraw.vertex2 (x3+.panx, y3+.d);
2309 GlDraw.ends ();
2311 ) state.layout
2312 ) state.rects
2314 Gl.disable `blend;
2317 let showoutline () =
2318 match state.mode with
2319 | Outline (allowdel, active, first, outlines, qsearch, pan) ->
2320 Gl.enable `blend;
2321 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2322 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2323 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2324 Gl.disable `blend;
2326 GlDraw.color (1., 1., 1.);
2327 let font = Glut.BITMAP_9_BY_15 in
2328 let draw_string x y s =
2329 GlPix.raster_pos ~x ~y ();
2330 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2332 let rec loop row =
2333 if row = Array.length outlines || (row - first) * 16 > conf.winh
2334 then ()
2335 else (
2336 let (s, l, _, _) = outlines.(row) in
2337 let y = (row - first) * 16 in
2338 let x = 5 + 15*l in
2339 if row = active
2340 then (
2341 Gl.enable `blend;
2342 GlDraw.polygon_mode `both `line;
2343 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2344 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2345 GlDraw.rect (0., float (y + 1))
2346 (float (conf.winw - 1), float (y + 18));
2347 GlDraw.polygon_mode `both `fill;
2348 Gl.disable `blend;
2349 GlDraw.color (1., 1., 1.);
2351 let draw_string s =
2352 let l = String.length s in
2353 if pan < 0
2354 then (
2355 let pan = pan * 2 in
2356 let left = l + pan in
2357 if left > 0
2358 then
2359 let s = String.sub s (-pan) left in
2360 draw_string (float x) (float (y + 16)) s
2362 else
2363 draw_string (float (x + pan*15)) (float (y + 16)) s
2365 draw_string s;
2366 loop (row+1)
2369 loop first
2371 | _ -> ()
2374 let display () =
2375 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2376 GlDraw.viewport margin 0 state.w conf.winh;
2377 pagematrix ();
2378 GlClear.color (scalecolor 0.5);
2379 GlClear.clear [`color];
2380 if conf.zoom > 1.0
2381 then (
2382 Gl.enable `scissor_test;
2383 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2385 List.iter drawpage state.layout;
2386 if conf.zoom > 1.0
2387 then
2388 Gl.disable `scissor_test
2390 if state.x != 0
2391 then (
2392 let x = -.float state.x in
2393 GlMat.translate ~x ();
2395 showrects ();
2396 showsel margin;
2397 GlDraw.viewport 0 0 conf.winw conf.winh;
2398 winmatrix ();
2399 scrollindicator ();
2400 showoutline ();
2401 enttext ();
2402 Glut.swapBuffers ();
2405 let getunder x y =
2406 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2407 let x = x - margin - state.x in
2408 let rec f = function
2409 | l :: rest ->
2410 begin match getopaque l.pageno with
2411 | Some (opaque, _) when validopaque opaque ->
2412 let y = y - l.pagedispy in
2413 if y > 0
2414 then
2415 let y = l.pagey + y in
2416 let x = x - l.pagex in
2417 match whatsunder opaque x y with
2418 | Unone -> f rest
2419 | under -> under
2420 else
2421 f rest
2422 | _ ->
2423 f rest
2425 | [] -> Unone
2427 f state.layout
2430 let viewmouse button bstate x y =
2431 match button with
2432 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2433 if Glut.getModifiers () land Glut.active_ctrl != 0
2434 then (
2435 match state.mstate with
2436 | Mzoom (oldn, i) ->
2437 if oldn = n
2438 then (
2439 if i = 2
2440 then
2441 let incr =
2442 match n with
2443 | 4 ->
2444 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2445 | _ ->
2446 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2448 let zoom = conf.zoom +. incr in
2449 setzoom zoom;
2450 state.mstate <- Mzoom (n, 0);
2451 else
2452 state.mstate <- Mzoom (n, i+1);
2454 else state.mstate <- Mzoom (n, 0)
2456 | _ -> state.mstate <- Mzoom (n, 0)
2458 else (
2459 if state.ascrollstep > 0
2460 then
2461 setautoscrollspeed (n=4)
2462 else
2463 let incr =
2464 if n = 3
2465 then -conf.scrollstep
2466 else conf.scrollstep
2468 let incr = incr * 2 in
2469 let y = clamp incr in
2470 gotoy_and_clear_text y
2473 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2474 if bstate = Glut.DOWN
2475 then (
2476 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2477 state.mstate <- Mpan (x, y)
2479 else
2480 state.mstate <- Mnone
2482 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2483 if bstate = Glut.DOWN
2484 then
2485 let position, sh = scrollph state.y in
2486 if y > truncate position && y < truncate (position +. sh)
2487 then
2488 state.mstate <- Mscroll
2489 else
2490 let percent = float y /. float conf.winh in
2491 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2492 gotoy desty;
2493 state.mstate <- Mscroll
2494 else
2495 state.mstate <- Mnone
2497 | Glut.LEFT_BUTTON ->
2498 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2499 begin match dest with
2500 | Ulinkgoto (pageno, top) ->
2501 if pageno >= 0
2502 then (
2503 addnav ();
2504 gotopage1 pageno top;
2507 | Ulinkuri s ->
2508 print_endline s
2510 | Unone when bstate = Glut.DOWN ->
2511 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2512 state.mstate <- Mpan (x, y);
2514 | Unone | Utext _ ->
2515 if bstate = Glut.DOWN
2516 then (
2517 if conf.angle mod 360 = 0
2518 then (
2519 state.mstate <- Msel ((x, y), (x, y));
2520 Glut.postRedisplay ()
2523 else (
2524 match state.mstate with
2525 | Mnone -> ()
2527 | Mzoom _ | Mscroll ->
2528 state.mstate <- Mnone
2530 | Mpan _ ->
2531 Glut.setCursor Glut.CURSOR_INHERIT;
2532 state.mstate <- Mnone
2534 | Msel ((x0, y0), (x1, y1)) ->
2535 let f l =
2536 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2537 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2538 then
2539 match getopaque l.pageno with
2540 | Some (opaque, _) when validopaque opaque ->
2541 copysel opaque
2542 | _ -> ()
2544 List.iter f state.layout;
2545 copysel ""; (* ugly *)
2546 Glut.setCursor Glut.CURSOR_INHERIT;
2547 state.mstate <- Mnone;
2551 | _ -> ()
2554 let birdseyemouse button bstate x y
2555 (conf, leftx, pageno, hooverpageno, anchor) =
2556 match button with
2557 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2558 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2559 let rec loop = function
2560 | [] -> ()
2561 | l :: rest ->
2562 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2563 && x > margin && x < margin + l.pagew
2564 then (
2565 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
2567 else loop rest
2569 loop state.layout
2570 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2571 | _ -> ()
2574 let mouse bstate button x y =
2575 match state.mode with
2576 | View -> viewmouse button bstate x y
2577 | Birdseye beye -> birdseyemouse button bstate x y beye
2578 | Textentry _ -> ()
2579 | Outline _ -> ()
2582 let mouse ~button ~state ~x ~y = mouse state button x y;;
2584 let motion ~x ~y =
2585 match state.mode with
2586 | Outline _ -> ()
2587 | _ ->
2588 match state.mstate with
2589 | Mzoom _ | Mnone -> ()
2591 | Mpan (x0, y0) ->
2592 let dx = x - x0
2593 and dy = y0 - y in
2594 state.mstate <- Mpan (x, y);
2595 if conf.zoom > 1.0 then state.x <- state.x + dx;
2596 let y = clamp dy in
2597 gotoy_and_clear_text y
2599 | Msel (a, _) ->
2600 state.mstate <- Msel (a, (x, y));
2601 Glut.postRedisplay ()
2603 | Mscroll ->
2604 let y = min conf.winh (max 0 y) in
2605 let percent = float y /. float conf.winh in
2606 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2607 gotoy_and_clear_text y
2610 let pmotion ~x ~y =
2611 match state.mode with
2612 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2613 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2614 let rec loop = function
2615 | [] ->
2616 if hooverpageno != -1
2617 then (
2618 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2619 Glut.postRedisplay ();
2621 | l :: rest ->
2622 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2623 && x > margin && x < margin + l.pagew
2624 then (
2625 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2626 Glut.postRedisplay ();
2628 else loop rest
2630 loop state.layout
2632 | Outline _ -> ()
2633 | _ ->
2634 match state.mstate with
2635 | Mnone ->
2636 begin match getunder x y with
2637 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2638 | Ulinkuri uri ->
2639 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2640 Glut.setCursor Glut.CURSOR_INFO
2641 | Ulinkgoto (page, y) ->
2642 if conf.underinfo
2643 then showtext 'p' ("age: " ^ string_of_int page);
2644 Glut.setCursor Glut.CURSOR_INFO
2645 | Utext s ->
2646 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2647 Glut.setCursor Glut.CURSOR_TEXT
2650 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
2655 module State =
2656 struct
2657 open Parser
2659 let home =
2661 match Sys.os_type with
2662 | "Win32" -> Sys.getenv "HOMEPATH"
2663 | _ -> Sys.getenv "HOME"
2664 with exn ->
2665 prerr_endline
2666 ("Can not determine home directory location: " ^
2667 Printexc.to_string exn);
2671 let config_of c attrs =
2672 let apply c k v =
2674 match k with
2675 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2676 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2677 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2678 | "preload" -> { c with preload = bool_of_string v }
2679 | "page-bias" -> { c with pagebias = int_of_string v }
2680 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2681 | "auto-scroll-step" ->
2682 { c with autoscrollstep = max 0 (int_of_string v) }
2683 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2684 | "crop-hack" -> { c with crophack = bool_of_string v }
2685 | "throttle" -> { c with showall = bool_of_string v }
2686 | "highlight-links" -> { c with hlinks = bool_of_string v }
2687 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2688 | "vertical-margin" ->
2689 { c with interpagespace = max 0 (int_of_string v) }
2690 | "zoom" ->
2691 let zoom = float_of_string v /. 100. in
2692 let zoom = max 0.01 (min 2.2 zoom) in
2693 { c with zoom = zoom }
2694 | "presentation" -> { c with presentation = bool_of_string v }
2695 | "rotation-angle" -> { c with angle = int_of_string v }
2696 | "width" -> { c with winw = max 20 (int_of_string v) }
2697 | "height" -> { c with winh = max 20 (int_of_string v) }
2698 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2699 | "proportional-display" -> { c with proportional = bool_of_string v }
2700 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2701 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2702 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2703 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2704 | _ -> c
2705 with exn ->
2706 prerr_endline ("Error processing attribute (`" ^
2707 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2710 let rec fold c = function
2711 | [] -> c
2712 | (k, v) :: rest ->
2713 let c = apply c k v in
2714 fold c rest
2716 fold c attrs;
2719 let fromstring f pos n v d =
2720 try f v
2721 with exn ->
2722 dolog "Error processing attribute (%S=%S) at %d\n%s"
2723 n v pos (Printexc.to_string exn)
2728 let bookmark_of attrs =
2729 let rec fold title page rely = function
2730 | ("title", v) :: rest -> fold v page rely rest
2731 | ("page", v) :: rest -> fold title v rely rest
2732 | ("rely", v) :: rest -> fold title page v rest
2733 | _ :: rest -> fold title page rely rest
2734 | [] -> title, page, rely
2736 fold "invalid" "0" "0" attrs
2739 let doc_of attrs =
2740 let rec fold path page rely pan = function
2741 | ("path", v) :: rest -> fold v page rely pan rest
2742 | ("page", v) :: rest -> fold path v rely pan rest
2743 | ("rely", v) :: rest -> fold path page v pan rest
2744 | ("pan", v) :: rest -> fold path page rely v rest
2745 | _ :: rest -> fold path page rely pan rest
2746 | [] -> path, page, rely, pan
2748 fold "" "0" "0" "0" attrs
2751 let setconf dst src =
2752 dst.scrollw <- src.scrollw;
2753 dst.scrollh <- src.scrollh;
2754 dst.icase <- src.icase;
2755 dst.preload <- src.preload;
2756 dst.pagebias <- src.pagebias;
2757 dst.verbose <- src.verbose;
2758 dst.scrollstep <- src.scrollstep;
2759 dst.maxhfit <- src.maxhfit;
2760 dst.crophack <- src.crophack;
2761 dst.autoscrollstep <- src.autoscrollstep;
2762 dst.showall <- src.showall;
2763 dst.hlinks <- src.hlinks;
2764 dst.underinfo <- src.underinfo;
2765 dst.interpagespace <- src.interpagespace;
2766 dst.zoom <- src.zoom;
2767 dst.presentation <- src.presentation;
2768 dst.angle <- src.angle;
2769 dst.winw <- src.winw;
2770 dst.winh <- src.winh;
2771 dst.savebmarks <- src.savebmarks;
2772 dst.memlimit <- src.memlimit;
2773 dst.proportional <- src.proportional;
2774 dst.texcount <- src.texcount;
2775 dst.sliceheight <- src.sliceheight;
2776 dst.thumbw <- src.thumbw;
2779 let unent s =
2780 let l = String.length s in
2781 let b = Buffer.create l in
2782 unent b s 0 l;
2783 Buffer.contents b;
2786 let get s =
2787 let h = Hashtbl.create 10 in
2788 let dc = { defconf with angle = defconf.angle } in
2789 let rec toplevel v t spos epos =
2790 match t with
2791 | Vdata | Vcdata | Vend -> v
2792 | Vopen ("llppconfig", attrs, closed) ->
2793 if closed
2794 then v
2795 else { v with f = llppconfig }
2796 | Vopen _ ->
2797 error "unexpected subelement at top level" s spos
2798 | Vclose tag -> error "unexpected close at top level" s spos
2800 and llppconfig v t spos epos =
2801 match t with
2802 | Vdata | Vcdata | Vend -> v
2803 | Vopen ("defaults", attrs, closed) ->
2804 let c = config_of dc attrs in
2805 setconf dc c;
2806 if closed
2807 then v
2808 else { v with f = skip "defaults" (fun () -> v) }
2810 | Vopen ("doc", attrs, closed) ->
2811 let pathent, spage, srely, span = doc_of attrs in
2812 let path = unent pathent
2813 and pageno = fromstring int_of_string spos "page" spage 0
2814 and rely = fromstring float_of_string spos "rely" srely 0.0
2815 and pan = fromstring int_of_string spos "pan" span 0 in
2816 let c = config_of dc attrs in
2817 let anchor = (pageno, rely) in
2818 if closed
2819 then (Hashtbl.add h path (c, [], pan, anchor); v)
2820 else { v with f = doc path pan anchor c [] }
2822 | Vopen (tag, _, closed) ->
2823 error "unexpected subelement in llppconfig" s spos
2825 | Vclose "llppconfig" -> { v with f = toplevel }
2826 | Vclose tag -> error "unexpected close in llppconfig" s spos
2828 and doc path pan anchor c bookmarks v t spos epos =
2829 match t with
2830 | Vdata | Vcdata -> v
2831 | Vend -> error "unexpected end of input in doc" s spos
2832 | Vopen ("bookmarks", attrs, closed) ->
2833 { v with f = pbookmarks path pan anchor c bookmarks }
2835 | Vopen (tag, _, _) ->
2836 error "unexpected subelement in doc" s spos
2838 | Vclose "doc" ->
2839 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2840 { v with f = llppconfig }
2842 | Vclose tag -> error "unexpected close in doc" s spos
2844 and pbookmarks path pan anchor c bookmarks v t spos epos =
2845 match t with
2846 | Vdata | Vcdata -> v
2847 | Vend -> error "unexpected end of input in bookmarks" s spos
2848 | Vopen ("item", attrs, closed) ->
2849 let titleent, spage, srely = bookmark_of attrs in
2850 let page = fromstring int_of_string spos "page" spage 0
2851 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2852 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2853 if closed
2854 then { v with f = pbookmarks path pan anchor c bookmarks }
2855 else
2856 let f () = v in
2857 { v with f = skip "item" f }
2859 | Vopen _ ->
2860 error "unexpected subelement in bookmarks" s spos
2862 | Vclose "bookmarks" ->
2863 { v with f = doc path pan anchor c bookmarks }
2865 | Vclose tag -> error "unexpected close in bookmarks" s spos
2867 and skip tag f v t spos epos =
2868 match t with
2869 | Vdata | Vcdata -> v
2870 | Vend ->
2871 error ("unexpected end of input in skipped " ^ tag) s spos
2872 | Vopen (tag', _, closed) ->
2873 if closed
2874 then v
2875 else
2876 let f' () = { v with f = skip tag f } in
2877 { v with f = skip tag' f' }
2878 | Vclose ctag ->
2879 if tag = ctag
2880 then f ()
2881 else error ("unexpected close in skipped " ^ tag) s spos
2884 parse { f = toplevel; accu = () } s;
2885 h, dc;
2888 let do_load f ic =
2890 let len = in_channel_length ic in
2891 let s = String.create len in
2892 really_input ic s 0 len;
2893 f s;
2894 with
2895 | Parse_error (msg, s, pos) ->
2896 let subs = subs s pos in
2897 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2898 failwith ("parse error: " ^ s)
2900 | exn ->
2901 failwith ("config load error: " ^ Printexc.to_string exn)
2904 let path =
2905 let dir =
2907 let dir = Filename.concat home ".config" in
2908 if Sys.is_directory dir then dir else home
2909 with _ -> home
2911 Filename.concat dir "llpp.conf"
2914 let load1 f =
2915 if Sys.file_exists path
2916 then
2917 match
2918 (try Some (open_in_bin path)
2919 with exn ->
2920 prerr_endline
2921 ("Error opening configuation file `" ^ path ^ "': " ^
2922 Printexc.to_string exn);
2923 None
2925 with
2926 | Some ic ->
2927 begin try
2928 f (do_load get ic)
2929 with exn ->
2930 prerr_endline
2931 ("Error loading configuation from `" ^ path ^ "': " ^
2932 Printexc.to_string exn);
2933 end;
2934 close_in ic;
2936 | None -> ()
2937 else
2938 f (Hashtbl.create 0, defconf)
2941 let load () =
2942 let f (h, dc) =
2943 let pc, pb, px, pa =
2945 Hashtbl.find h (Filename.basename state.path)
2946 with Not_found -> dc, [], 0, (0, 0.0)
2948 setconf defconf dc;
2949 setconf conf pc;
2950 state.bookmarks <- pb;
2951 state.x <- px;
2952 cbput state.hists.nav pa;
2954 load1 f
2957 let add_attrs bb always dc c =
2958 let ob s a b =
2959 if always || a != b
2960 then Printf.bprintf bb "\n %s='%b'" s a
2961 and oi s a b =
2962 if always || a != b
2963 then Printf.bprintf bb "\n %s='%d'" s a
2964 and oz s a b =
2965 if always || a <> b
2966 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2968 let w, h =
2969 if always
2970 then dc.winw, dc.winh
2971 else
2972 match state.fullscreen with
2973 | Some wh -> wh
2974 | None -> c.winw, c.winh
2976 let zoom, presentation, interpagespace, showall=
2977 if always
2978 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2979 else
2980 match state.mode with
2981 | Birdseye (bc, _, _, _, _) ->
2982 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2983 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2985 oi "width" w dc.winw;
2986 oi "height" h dc.winh;
2987 oi "scroll-bar-width" c.scrollw dc.scrollw;
2988 oi "scroll-handle-height" c.scrollh dc.scrollh;
2989 ob "case-insensitive-search" c.icase dc.icase;
2990 ob "preload" c.preload dc.preload;
2991 oi "page-bias" c.pagebias dc.pagebias;
2992 oi "scroll-step" c.scrollstep dc.scrollstep;
2993 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
2994 ob "max-height-fit" c.maxhfit dc.maxhfit;
2995 ob "crop-hack" c.crophack dc.crophack;
2996 ob "throttle" showall dc.showall;
2997 ob "highlight-links" c.hlinks dc.hlinks;
2998 ob "under-cursor-info" c.underinfo dc.underinfo;
2999 oi "vertical-margin" interpagespace dc.interpagespace;
3000 oz "zoom" zoom dc.zoom;
3001 ob "presentation" presentation dc.presentation;
3002 oi "rotation-angle" c.angle dc.angle;
3003 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
3004 ob "proportional-display" c.proportional dc.proportional;
3005 oi "pixmap-cache-size" c.memlimit dc.memlimit;
3006 oi "texcount" c.texcount dc.texcount;
3007 oi "slice-height" c.sliceheight dc.sliceheight;
3008 oi "thumbnail-width" c.thumbw dc.thumbw;
3011 let save () =
3012 let bb = Buffer.create 32768 in
3013 let f (h, dc) =
3014 Buffer.add_string bb "<llppconfig>\n<defaults ";
3015 add_attrs bb true dc dc;
3016 Buffer.add_string bb "/>\n";
3018 let adddoc path pan anchor c bookmarks =
3019 if bookmarks == [] && c = dc && anchor = emptyanchor
3020 then ()
3021 else (
3022 Printf.bprintf bb "<doc path='%s'"
3023 (enent path 0 (String.length path));
3025 if anchor <> emptyanchor
3026 then (
3027 let n, y = anchor in
3028 Printf.bprintf bb " page='%d'" n;
3029 Printf.bprintf bb " rely='%f'" y;
3032 if pan != 0
3033 then Printf.bprintf bb " pan='%d'" pan;
3035 add_attrs bb false dc c;
3037 begin match bookmarks with
3038 | [] -> Buffer.add_string bb "/>\n"
3039 | _ ->
3040 Buffer.add_string bb ">\n<bookmarks>\n";
3041 List.iter (fun (title, _level, page, rely) ->
3042 Printf.bprintf bb
3043 "<item title='%s' page='%d' rely='%f'/>\n"
3044 (enent title 0 (String.length title))
3045 page
3046 rely
3047 ) bookmarks;
3048 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3049 end;
3053 let pan =
3054 match state.mode with
3055 | Birdseye (_, pan, _, _, _) -> pan
3056 | _ -> state.x
3058 let basename = Filename.basename state.path in
3059 adddoc basename pan (getanchor ())
3060 { conf with
3061 autoscrollstep =
3062 if state.ascrollstep > 0
3063 then state.ascrollstep
3064 else conf.autoscrollstep }
3065 (if conf.savebmarks then state.bookmarks else []);
3067 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3068 if basename <> path
3069 then adddoc path x y c bookmarks
3070 ) h;
3071 Buffer.add_string bb "</llppconfig>";
3073 load1 f;
3074 if Buffer.length bb > 0
3075 then
3077 let tmp = path ^ ".tmp" in
3078 let oc = open_out_bin tmp in
3079 Buffer.output_buffer oc bb;
3080 close_out oc;
3081 Sys.rename tmp path;
3082 with exn ->
3083 prerr_endline
3084 ("error while saving configuration: " ^ Printexc.to_string exn)
3086 end;;
3088 let () =
3089 Arg.parse
3090 ["-p", Arg.String (fun s -> state.password <- s) , "password"
3091 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3092 "print version")]
3093 (fun s -> state.path <- s)
3094 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
3096 if String.length state.path = 0
3097 then (prerr_endline "filename missing"; exit 1);
3099 State.load ();
3101 let _ = Glut.init Sys.argv in
3102 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3103 let () = Glut.initWindowSize conf.winw conf.winh in
3104 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3106 let csock, ssock =
3107 if Sys.os_type = "Unix"
3108 then
3109 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3110 else
3111 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3112 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3113 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3114 Unix.bind sock addr;
3115 Unix.listen sock 1;
3116 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3117 Unix.connect csock addr;
3118 let ssock, _ = Unix.accept sock in
3119 Unix.close sock;
3120 let opts sock =
3121 Unix.setsockopt sock Unix.TCP_NODELAY true;
3122 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3124 opts ssock;
3125 opts csock;
3126 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3127 ssock, csock
3130 let () = Glut.displayFunc display in
3131 let () = Glut.reshapeFunc reshape in
3132 let () = Glut.keyboardFunc keyboard in
3133 let () = Glut.specialFunc special in
3134 let () = Glut.idleFunc (Some idle) in
3135 let () = Glut.mouseFunc mouse in
3136 let () = Glut.motionFunc motion in
3137 let () = Glut.passiveMotionFunc pmotion in
3139 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3140 state.csock <- csock;
3141 state.ssock <- ssock;
3142 state.text <- "Opening " ^ state.path;
3143 writeopen state.path state.password;
3145 at_exit State.save;
3147 let rec handlelablglutbug () =
3149 Glut.mainLoop ();
3150 with Glut.BadEnum "key in special_of_int" ->
3151 showtext '!' " LablGlut bug: special key not recognized";
3152 handlelablglutbug ()
3154 handlelablglutbug ();