fz_fprint_obj does the line termination for us
[llpp.git] / main.ml
blob948a94072986980e89ff408ed0e6d3ebb9d06292
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)
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 = function
1063 | [] -> 0
1064 | l :: _ when l.pagey = 0 -> l.pageno
1065 | _ :: rest -> fold rest
1067 fold 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, "");
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 represent ()
1523 | 'f' ->
1524 begin match state.fullscreen with
1525 | None ->
1526 state.fullscreen <- Some (conf.winw, conf.winh);
1527 Glut.fullScreen ()
1528 | Some (w, h) ->
1529 state.fullscreen <- None;
1530 doreshape w h
1533 | 'g' ->
1534 gotoy_and_clear_text 0
1536 | 'n' ->
1537 search state.searchpattern true
1539 | 'p' | 'N' ->
1540 search state.searchpattern false
1542 | 't' ->
1543 begin match state.layout with
1544 | [] -> ()
1545 | l :: _ ->
1546 gotoy_and_clear_text (getpagey l.pageno)
1549 | ' ' ->
1550 begin match List.rev state.layout with
1551 | [] -> ()
1552 | l :: _ ->
1553 let pageno = min (l.pageno+1) (state.pagecount-1) in
1554 gotoy_and_clear_text (getpagey pageno)
1557 | '\127' ->
1558 begin match state.layout with
1559 | [] -> ()
1560 | l :: _ ->
1561 let pageno = max 0 (l.pageno-1) in
1562 gotoy_and_clear_text (getpagey pageno)
1565 | '=' ->
1566 let f (fn, ln) l =
1567 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
1569 let fn, ln = List.fold_left f (-1, -1) state.layout in
1570 let s =
1571 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
1572 let percent =
1573 if maxy <= 0
1574 then 100.
1575 else (100. *. (float state.y /. float maxy)) in
1576 if fn = ln
1577 then
1578 Printf.sprintf "Page %d of %d %.2f%%"
1579 (fn+1) state.pagecount percent
1580 else
1581 Printf.sprintf
1582 "Pages %d-%d of %d %.2f%%"
1583 (fn+1) (ln+1) state.pagecount percent
1585 showtext ' ' s;
1587 | 'w' ->
1588 begin match state.layout with
1589 | [] -> ()
1590 | l :: _ ->
1591 doreshape (l.pagew + conf.scrollw) l.pageh;
1592 Glut.postRedisplay ();
1595 | '\'' ->
1596 enterbookmarkmode ()
1598 | 'h' ->
1599 enterhelpmode ()
1601 | 'i' ->
1602 enterinfomode ()
1604 | 'm' ->
1605 let ondone s =
1606 match state.layout with
1607 | l :: _ ->
1608 state.bookmarks <-
1609 (s, 0, l.pageno, float l.pagey /. float l.pageh)
1610 :: state.bookmarks
1611 | _ -> ()
1613 enttext ('~', "", None, textentry, ondone)
1615 | '~' ->
1616 quickbookmark ();
1617 showtext ' ' "Quick bookmark added";
1619 | 'z' ->
1620 begin match state.layout with
1621 | l :: _ ->
1622 let rect = getpdimrect l.pagedimno in
1623 let w, h =
1624 if conf.crophack
1625 then
1626 (truncate (1.8 *. (rect.(1) -. rect.(0))),
1627 truncate (1.2 *. (rect.(3) -. rect.(0))))
1628 else
1629 (truncate (rect.(1) -. rect.(0)),
1630 truncate (rect.(3) -. rect.(0)))
1632 if w != 0 && h != 0
1633 then
1634 doreshape (w + conf.scrollw) (h + conf.interpagespace)
1636 Glut.postRedisplay ();
1638 | [] -> ()
1641 | '<' | '>' ->
1642 reinit (conf.angle + (if c = '>' then 30 else -30)) conf.proportional
1644 | '[' | ']' ->
1645 state.colorscale <-
1646 max 0.0
1647 (min (state.colorscale +. (if c = ']' then 0.1 else -0.1)) 1.0);
1648 Glut.postRedisplay ()
1650 | 'k' -> gotoy (clamp (-conf.scrollstep))
1651 | 'j' -> gotoy (clamp conf.scrollstep)
1653 | 'r' -> opendoc state.path state.password
1655 | _ ->
1656 vlog "huh? %d %c" key (Char.chr key);
1659 let textentrykeyboard ~key ~x ~y ((c, text, opthist, onkey, ondone), mode) =
1660 let enttext te =
1661 state.mode <- Textentry (te, mode);
1662 state.text <- "";
1663 enttext ();
1664 Glut.postRedisplay ()
1666 match Char.unsafe_chr key with
1667 | '\008' ->
1668 let len = String.length text in
1669 if len = 0
1670 then (
1671 state.mode <- mode;
1672 Glut.postRedisplay ();
1674 else (
1675 let s = String.sub text 0 (len - 1) in
1676 enttext (c, s, opthist, onkey, ondone)
1679 | '\r' | '\n' ->
1680 ondone text;
1681 state.mode <- mode;
1682 Glut.postRedisplay ()
1684 | '\027' ->
1685 begin match opthist with
1686 | None -> ()
1687 | Some (_, onhistcancel) -> onhistcancel ()
1688 end;
1689 state.mode <- View;
1690 Glut.postRedisplay ()
1692 | _ ->
1693 begin match onkey text key with
1694 | TEdone text ->
1695 state.mode <- mode;
1696 ondone text;
1697 Glut.postRedisplay ()
1699 | TEcont text ->
1700 enttext (c, text, opthist, onkey, ondone);
1702 | TEstop ->
1703 state.mode <- mode;
1704 Glut.postRedisplay ()
1706 | TEswitch te ->
1707 state.mode <- Textentry (te, mode);
1708 Glut.postRedisplay ()
1709 end;
1712 let birdseyekeyboard ~key ~x ~y ((_, _, pageno, _, anchor) as beye) =
1713 match key with
1714 | 27 ->
1715 leavebirdseye beye true
1717 | 12 ->
1718 let y, h = getpageyh pageno in
1719 let top = (conf.winh - h) / 2 in
1720 gotoy (max 0 (y - top))
1722 | 13 ->
1723 leavebirdseye beye false
1725 | _ ->
1726 viewkeyboard ~key ~x ~y
1729 let outlinekeyboard ~key ~x ~y (allowdel, active, first, outlines, qsearch) =
1730 let narrow outlines pattern =
1731 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
1732 match reopt with
1733 | None -> None
1734 | Some re ->
1735 let rec fold accu n =
1736 if n = -1
1737 then accu
1738 else
1739 let (s, _, _, _) as o = outlines.(n) in
1740 let accu =
1741 if (try ignore (Str.search_forward re s 0); true
1742 with Not_found -> false)
1743 then (o :: accu)
1744 else accu
1746 fold accu (n-1)
1748 let matched = fold [] (Array.length outlines - 1) in
1749 if matched = [] then None else Some (Array.of_list matched)
1751 let search active pattern incr =
1752 let dosearch re =
1753 let rec loop n =
1754 if n = Array.length outlines || n = -1
1755 then None
1756 else
1757 let (s, _, _, _) = outlines.(n) in
1759 (try ignore (Str.search_forward re s 0); true
1760 with Not_found -> false)
1761 then Some n
1762 else loop (n + incr)
1764 loop active
1767 let re = Str.regexp_case_fold pattern in
1768 dosearch re
1769 with Failure s ->
1770 state.text <- s;
1771 None
1773 let firstof active = max 0 (active - maxoutlinerows () / 2) in
1774 match key with
1775 | 27 ->
1776 if String.length qsearch = 0
1777 then (
1778 state.text <- "";
1779 state.mode <- View;
1780 Glut.postRedisplay ();
1782 else (
1783 state.text <- "";
1784 state.mode <- Outline (allowdel, active, first, outlines, "");
1785 Glut.postRedisplay ();
1788 | 18 | 19 ->
1789 let incr = if key = 18 then -1 else 1 in
1790 let active, first =
1791 match search (active + incr) qsearch incr with
1792 | None ->
1793 state.text <- qsearch ^ " [not found]";
1794 active, first
1795 | Some active ->
1796 state.text <- qsearch;
1797 active, firstof active
1799 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1800 Glut.postRedisplay ();
1802 | 8 ->
1803 let len = String.length qsearch in
1804 if len = 0
1805 then ()
1806 else (
1807 if len = 1
1808 then (
1809 state.text <- "";
1810 state.mode <- Outline (allowdel, active, first, outlines, "");
1812 else
1813 let qsearch = String.sub qsearch 0 (len - 1) in
1814 let active, first =
1815 match search active qsearch ~-1 with
1816 | None ->
1817 state.text <- qsearch ^ " [not found]";
1818 active, first
1819 | Some active ->
1820 state.text <- qsearch;
1821 active, firstof active
1823 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
1825 Glut.postRedisplay ()
1827 | 13 ->
1828 if active < Array.length outlines
1829 then (
1830 let (_, _, n, t) = outlines.(active) in
1831 addnav ();
1832 gotopage n t;
1834 state.text <- "";
1835 if allowdel then state.bookmarks <- Array.to_list outlines;
1836 state.mode <- View;
1837 Glut.postRedisplay ();
1839 | _ when key >= 32 && key < 127 ->
1840 let pattern = addchar qsearch (Char.chr key) in
1841 let active, first =
1842 match search active pattern 1 with
1843 | None ->
1844 state.text <- pattern ^ " [not found]";
1845 active, first
1846 | Some active ->
1847 state.text <- pattern;
1848 active, firstof active
1850 state.mode <- Outline (allowdel, active, first, outlines, pattern);
1851 Glut.postRedisplay ()
1853 | 14 when not allowdel -> (* ctrl-n *)
1854 if String.length qsearch > 0
1855 then (
1856 let optoutlines = narrow outlines qsearch in
1857 begin match optoutlines with
1858 | None -> state.text <- "can't narrow"
1859 | Some outlines ->
1860 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
1861 match state.outlines with
1862 | Olist l -> ()
1863 | Oarray a ->
1864 state.outlines <- Onarrow (qsearch, outlines, a)
1865 | Onarrow (pat, a, b) ->
1866 state.outlines <- Onarrow (qsearch, outlines, b)
1867 end;
1869 Glut.postRedisplay ()
1871 | 21 when not allowdel -> (* ctrl-u *)
1872 let outline =
1873 match state.outlines with
1874 | Oarray a -> a
1875 | Olist l ->
1876 let a = Array.of_list (List.rev l) in
1877 state.outlines <- Oarray a;
1879 | Onarrow (pat, a, b) ->
1880 state.outlines <- Oarray b;
1881 state.text <- "";
1884 state.mode <- Outline (allowdel, 0, 0, outline, qsearch);
1885 Glut.postRedisplay ()
1887 | 12 ->
1888 state.mode <- Outline
1889 (allowdel, active, firstof active, outlines, qsearch);
1890 Glut.postRedisplay ()
1892 | 127 when allowdel ->
1893 let len = Array.length outlines - 1 in
1894 if len = 0
1895 then (
1896 state.mode <- View;
1897 state.bookmarks <- [];
1899 else (
1900 let bookmarks = Array.init len
1901 (fun i ->
1902 let i = if i >= active then i + 1 else i in
1903 outlines.(i)
1906 state.mode <-
1907 Outline (
1908 allowdel,
1909 min active (len-1),
1910 min first (len-1),
1911 bookmarks, qsearch
1914 Glut.postRedisplay ()
1916 | _ -> dolog "unknown key %d" key
1919 let keyboard ~key ~x ~y =
1920 if key = 7
1921 then
1922 wcmd "interrupt" []
1923 else
1924 match state.mode with
1925 | Outline outline -> outlinekeyboard ~key ~x ~y outline
1926 | Textentry textentry -> textentrykeyboard ~key ~x ~y textentry
1927 | Birdseye birdseye -> birdseyekeyboard ~key ~x ~y birdseye
1928 | View -> viewkeyboard ~key ~x ~y
1931 let birdseyespecial key x y (conf, leftx, pageno, hooverpageno, anchor) =
1932 match key with
1933 | Glut.KEY_UP ->
1934 let pageno = max 0 (pageno - 1) in
1935 let rec loop = function
1936 | [] -> gotopage1 pageno 0
1937 | l :: _ when l.pageno = pageno ->
1938 if l.pagedispy >= 0 && l.pagey = 0
1939 then Glut.postRedisplay ()
1940 else gotopage1 pageno 0
1941 | _ :: rest -> loop rest
1943 loop state.layout;
1944 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1946 | Glut.KEY_DOWN ->
1947 let pageno = min (state.pagecount - 1) (pageno + 1) in
1948 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1949 let rec loop = function
1950 | [] ->
1951 let y, h = getpageyh pageno in
1952 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
1953 gotoy (clamp dy)
1954 | l :: rest when l.pageno = pageno ->
1955 if l.pagevh != l.pageh
1956 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
1957 else Glut.postRedisplay ()
1958 | l :: rest -> loop rest
1960 loop state.layout
1962 | Glut.KEY_PAGE_UP ->
1963 begin match state.layout with
1964 | l :: _ ->
1965 if l.pagey != 0
1966 then (
1967 state.mode <- Birdseye (
1968 conf, leftx, l.pageno, hooverpageno, anchor
1970 gotopage1 l.pageno 0;
1972 else (
1973 let layout = layout (state.y-conf.winh) conf.winh in
1974 match layout with
1975 | [] -> gotoy (clamp (-conf.winh))
1976 | l :: _ ->
1977 state.mode <- Birdseye (
1978 conf, leftx, l.pageno, hooverpageno, anchor
1980 gotopage1 l.pageno 0
1983 | [] -> gotoy (clamp (-conf.winh))
1984 end;
1986 | Glut.KEY_PAGE_DOWN ->
1987 begin match List.rev state.layout with
1988 | l :: _ ->
1989 let layout = layout (state.y + conf.winh) conf.winh in
1990 begin match layout with
1991 | [] ->
1992 let incr = l.pageh - l.pagevh in
1993 if incr = 0
1994 then (
1995 state.mode <-
1996 Birdseye (
1997 conf, leftx, state.pagecount - 1, hooverpageno, anchor
1999 Glut.postRedisplay ();
2001 else gotoy (clamp (incr + conf.interpagespace*2));
2003 | l :: _ ->
2004 state.mode <-
2005 Birdseye (conf, leftx, l.pageno, hooverpageno, anchor);
2006 gotopage1 l.pageno 0;
2009 | [] -> gotoy (clamp conf.winh)
2010 end;
2012 | Glut.KEY_HOME ->
2013 state.mode <- Birdseye (conf, leftx, 0, hooverpageno, anchor);
2014 gotopage1 0 0
2016 | Glut.KEY_END ->
2017 let pageno = state.pagecount - 1 in
2018 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2019 if not (pagevisible state.layout pageno)
2020 then
2021 let h =
2022 match List.rev state.pdims with
2023 | [] -> conf.winh
2024 | (_, _, h, _) :: _ -> h
2026 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
2027 else Glut.postRedisplay ();
2028 | _ -> ()
2031 let setautoscrollspeed goingdown =
2032 let incr = max 1 (state.ascrollstep / 2) in
2033 let astep = max 1 (state.ascrollstep + (if goingdown then incr else -incr)) in
2034 state.ascrollstep <- astep;
2037 let special ~key ~x ~y =
2038 match state.mode with
2039 | View | (Birdseye _) when key = Glut.KEY_F9 ->
2040 togglebirdseye ()
2042 | Birdseye vals ->
2043 birdseyespecial key x y vals
2045 | View when key = Glut.KEY_F1 ->
2046 enterhelpmode ()
2048 | View ->
2049 if state.ascrollstep > 0 && (key = Glut.KEY_DOWN || key = Glut.KEY_UP)
2050 then setautoscrollspeed (key = Glut.KEY_DOWN)
2051 else
2052 let y =
2053 match key with
2054 | Glut.KEY_F3 -> search state.searchpattern true; state.y
2055 | Glut.KEY_UP -> clamp (-conf.scrollstep)
2056 | Glut.KEY_DOWN -> clamp conf.scrollstep
2057 | Glut.KEY_PAGE_UP ->
2058 if Glut.getModifiers () land Glut.active_ctrl != 0
2059 then
2060 match state.layout with
2061 | [] -> state.y
2062 | l :: _ -> state.y - l.pagey
2063 else
2064 clamp (-conf.winh)
2065 | Glut.KEY_PAGE_DOWN ->
2066 if Glut.getModifiers () land Glut.active_ctrl != 0
2067 then
2068 match List.rev state.layout with
2069 | [] -> state.y
2070 | l :: _ -> getpagey l.pageno
2071 else
2072 clamp conf.winh
2073 | Glut.KEY_HOME -> addnav (); 0
2074 | Glut.KEY_END ->
2075 addnav ();
2076 state.maxy - (if conf.maxhfit then conf.winh else 0)
2078 | Glut.KEY_RIGHT when conf.zoom > 1.0 ->
2079 state.x <- state.x - 10;
2080 state.y
2081 | Glut.KEY_LEFT when conf.zoom > 1.0 ->
2082 state.x <- state.x + 10;
2083 state.y
2085 | _ -> state.y
2087 gotoy_and_clear_text y
2089 | Textentry
2090 ((c, s, (Some (action, _) as onhist), onkey, ondone), mode) ->
2091 let s =
2092 match key with
2093 | Glut.KEY_UP -> action HCprev
2094 | Glut.KEY_DOWN -> action HCnext
2095 | Glut.KEY_HOME -> action HCfirst
2096 | Glut.KEY_END -> action HClast
2097 | _ -> state.text
2099 state.mode <- Textentry ((c, s, onhist, onkey, ondone), mode);
2100 Glut.postRedisplay ()
2102 | Textentry _ -> ()
2104 | Outline (allowdel, active, first, outlines, qsearch) ->
2105 let maxrows = maxoutlinerows () in
2106 let calcfirst first active =
2107 if active > first
2108 then
2109 let rows = active - first in
2110 if rows > maxrows then active - maxrows else first
2111 else active
2113 let navigate incr =
2114 let active = active + incr in
2115 let active = max 0 (min active (Array.length outlines - 1)) in
2116 let first = calcfirst first active in
2117 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2118 Glut.postRedisplay ()
2120 let updownlevel incr =
2121 let len = Array.length outlines in
2122 let (_, curlevel, _, _) = outlines.(active) in
2123 let rec flow i =
2124 if i = len then i-1 else if i = -1 then 0 else
2125 let (_, l, _, _) = outlines.(i) in
2126 if l != curlevel then i else flow (i+incr)
2128 let active = flow active in
2129 let first = calcfirst first active in
2130 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2131 Glut.postRedisplay ()
2133 match key with
2134 | Glut.KEY_UP -> navigate ~-1
2135 | Glut.KEY_DOWN -> navigate 1
2136 | Glut.KEY_PAGE_UP -> navigate ~-maxrows
2137 | Glut.KEY_PAGE_DOWN -> navigate maxrows
2139 | Glut.KEY_RIGHT when not allowdel -> updownlevel 1
2140 | Glut.KEY_LEFT when not allowdel -> updownlevel ~-1
2142 | Glut.KEY_HOME ->
2143 state.mode <- Outline (allowdel, 0, 0, outlines, qsearch);
2144 Glut.postRedisplay ()
2146 | Glut.KEY_END ->
2147 let active = Array.length outlines - 1 in
2148 let first = max 0 (active - maxrows) in
2149 state.mode <- Outline (allowdel, active, first, outlines, qsearch);
2150 Glut.postRedisplay ()
2152 | _ -> ()
2155 let drawplaceholder l =
2156 let margin = state.x + (conf.winw - (state.w + conf.scrollw)) / 2 in
2157 GlDraw.rect
2158 (float l.pagex, float l.pagedispy)
2159 (float (l.pagew + l.pagex), float (l.pagedispy + l.pagevh))
2161 let x = float (if margin < 0 then -margin else l.pagex)
2162 and y = float (l.pagedispy + 13) in
2163 let font = Glut.BITMAP_8_BY_13 in
2164 GlDraw.color (0.0, 0.0, 0.0);
2165 GlPix.raster_pos ~x ~y ();
2166 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c))
2167 ("Loading " ^ string_of_int (l.pageno + 1));
2170 let now () = Unix.gettimeofday ();;
2172 let drawpage l =
2173 let color =
2174 match state.mode with
2175 | Textentry _ -> scalecolor 0.4
2176 | View | Outline _ -> scalecolor 1.0
2177 | Birdseye (_, _, pageno, hooverpageno, _) ->
2178 if l.pageno = pageno
2179 then scalecolor 1.0
2180 else (
2181 if l.pageno = hooverpageno
2182 then scalecolor 0.9
2183 else scalecolor 0.8
2186 GlDraw.color color;
2187 begin match getopaque l.pageno with
2188 | Some (opaque, _) when validopaque opaque ->
2189 let a = now () in
2190 draw (l.pagedispy, l.pagew, l.pagevh, l.pagey, conf.hlinks)
2191 opaque;
2192 let b = now () in
2193 let d = b-.a in
2194 vlog "draw %d %f sec" l.pageno d;
2196 | _ ->
2197 drawplaceholder l;
2198 end;
2201 let scrollph y =
2202 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
2203 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2204 let sh = float conf.winh /. sh in
2205 let sh = max sh (float conf.scrollh) in
2207 let percent =
2208 if state.y = state.maxy
2209 then 1.0
2210 else float y /. float maxy
2212 let position = (float conf.winh -. sh) *. percent in
2214 let position =
2215 if position +. sh > float conf.winh
2216 then float conf.winh -. sh
2217 else position
2219 position, sh;
2222 let scrollindicator () =
2223 GlDraw.color (0.64 , 0.64, 0.64);
2224 GlDraw.rect
2225 (float (conf.winw - conf.scrollw), 0.)
2226 (float conf.winw, float conf.winh)
2228 GlDraw.color (0.0, 0.0, 0.0);
2230 let position, sh = scrollph state.y in
2231 GlDraw.rect
2232 (float (conf.winw - conf.scrollw), position)
2233 (float conf.winw, position +. sh)
2237 let showsel margin =
2238 match state.mstate with
2239 | Mnone | Mscroll _ | Mpan _ | Mzoom _ ->
2242 | Msel ((x0, y0), (x1, y1)) ->
2243 let rec loop = function
2244 | l :: ls ->
2245 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2246 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2247 then
2248 match getopaque l.pageno with
2249 | Some (opaque, _) when validopaque opaque ->
2250 let oy = -l.pagey + l.pagedispy in
2251 seltext opaque
2252 (x0 - margin - state.x, y0,
2253 x1 - margin - state.x, y1) oy;
2255 | _ -> ()
2256 else loop ls
2257 | [] -> ()
2259 loop state.layout
2262 let showrects () =
2263 let panx = float state.x in
2264 Gl.enable `blend;
2265 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
2266 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2267 List.iter
2268 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
2269 List.iter (fun l ->
2270 if l.pageno = pageno
2271 then (
2272 let d = float (l.pagedispy - l.pagey) in
2273 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
2274 GlDraw.begins `quads;
2276 GlDraw.vertex2 (x0+.panx, y0+.d);
2277 GlDraw.vertex2 (x1+.panx, y1+.d);
2278 GlDraw.vertex2 (x2+.panx, y2+.d);
2279 GlDraw.vertex2 (x3+.panx, y3+.d);
2281 GlDraw.ends ();
2283 ) state.layout
2284 ) state.rects
2286 Gl.disable `blend;
2289 let showoutline () =
2290 match state.mode with
2291 | Outline (allowdel, active, first, outlines, qsearch) ->
2292 Gl.enable `blend;
2293 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2294 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2295 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2296 Gl.disable `blend;
2298 GlDraw.color (1., 1., 1.);
2299 let font = Glut.BITMAP_9_BY_15 in
2300 let draw_string x y s =
2301 GlPix.raster_pos ~x ~y ();
2302 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
2304 let rec loop row =
2305 if row = Array.length outlines || (row - first) * 16 > conf.winh
2306 then ()
2307 else (
2308 let (s, l, _, _) = outlines.(row) in
2309 let y = (row - first) * 16 in
2310 let x = 5 + 15*l in
2311 if row = active
2312 then (
2313 Gl.enable `blend;
2314 GlDraw.polygon_mode `both `line;
2315 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2316 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2317 GlDraw.rect (0., float (y + 1))
2318 (float (conf.winw - 1), float (y + 18));
2319 GlDraw.polygon_mode `both `fill;
2320 Gl.disable `blend;
2321 GlDraw.color (1., 1., 1.);
2323 draw_string (float x) (float (y + 16)) s;
2324 loop (row+1)
2327 loop first
2329 | _ -> ()
2332 let display () =
2333 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2334 GlDraw.viewport margin 0 state.w conf.winh;
2335 pagematrix ();
2336 GlClear.color (scalecolor 0.5);
2337 GlClear.clear [`color];
2338 if conf.zoom > 1.0
2339 then (
2340 Gl.enable `scissor_test;
2341 GlMisc.scissor 0 0 (conf.winw - conf.scrollw) conf.winh;
2343 List.iter drawpage state.layout;
2344 if conf.zoom > 1.0
2345 then
2346 Gl.disable `scissor_test
2348 if state.x != 0
2349 then (
2350 let x = -.float state.x in
2351 GlMat.translate ~x ();
2353 showrects ();
2354 showsel margin;
2355 GlDraw.viewport 0 0 conf.winw conf.winh;
2356 winmatrix ();
2357 scrollindicator ();
2358 showoutline ();
2359 enttext ();
2360 Glut.swapBuffers ();
2363 let getunder x y =
2364 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2365 let x = x - margin - state.x in
2366 let rec f = function
2367 | l :: rest ->
2368 begin match getopaque l.pageno with
2369 | Some (opaque, _) when validopaque opaque ->
2370 let y = y - l.pagedispy in
2371 if y > 0
2372 then
2373 let y = l.pagey + y in
2374 let x = x - l.pagex in
2375 match whatsunder opaque x y with
2376 | Unone -> f rest
2377 | under -> under
2378 else
2379 f rest
2380 | _ ->
2381 f rest
2383 | [] -> Unone
2385 f state.layout
2388 let viewmouse button bstate x y =
2389 match button with
2390 | Glut.OTHER_BUTTON n when (n == 3 || n == 4) && bstate = Glut.UP ->
2391 if Glut.getModifiers () land Glut.active_ctrl != 0
2392 then (
2393 match state.mstate with
2394 | Mzoom (oldn, i) ->
2395 if oldn = n
2396 then (
2397 if i = 2
2398 then
2399 let incr =
2400 match n with
2401 | 4 ->
2402 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2403 | _ ->
2404 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2406 let zoom = conf.zoom +. incr in
2407 setzoom zoom;
2408 state.mstate <- Mzoom (n, 0);
2409 else
2410 state.mstate <- Mzoom (n, i+1);
2412 else state.mstate <- Mzoom (n, 0)
2414 | _ -> state.mstate <- Mzoom (n, 0)
2416 else (
2417 if state.ascrollstep > 0
2418 then
2419 setautoscrollspeed (n=4)
2420 else
2421 let incr =
2422 if n = 3
2423 then -conf.scrollstep
2424 else conf.scrollstep
2426 let incr = incr * 2 in
2427 let y = clamp incr in
2428 gotoy_and_clear_text y
2431 | Glut.LEFT_BUTTON when Glut.getModifiers () land Glut.active_ctrl != 0 ->
2432 if bstate = Glut.DOWN
2433 then (
2434 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2435 state.mstate <- Mpan (x, y)
2437 else
2438 state.mstate <- Mnone
2440 | Glut.LEFT_BUTTON when x > conf.winw - conf.scrollw ->
2441 if bstate = Glut.DOWN
2442 then
2443 let position, sh = scrollph state.y in
2444 if y > truncate position && y < truncate (position +. sh)
2445 then
2446 state.mstate <- Mscroll
2447 else
2448 let percent = float y /. float conf.winh in
2449 let desty = truncate (float (state.maxy - conf.winh) *. percent) in
2450 gotoy desty;
2451 state.mstate <- Mscroll
2452 else
2453 state.mstate <- Mnone
2455 | Glut.LEFT_BUTTON ->
2456 let dest = if bstate = Glut.DOWN then getunder x y else Unone in
2457 begin match dest with
2458 | Ulinkgoto (pageno, top) ->
2459 if pageno >= 0
2460 then (
2461 addnav ();
2462 gotopage1 pageno top;
2465 | Ulinkuri s ->
2466 print_endline s
2468 | Unone when bstate = Glut.DOWN ->
2469 Glut.setCursor Glut.CURSOR_CROSSHAIR;
2470 state.mstate <- Mpan (x, y);
2472 | Unone | Utext _ ->
2473 if bstate = Glut.DOWN
2474 then (
2475 if conf.angle mod 360 = 0
2476 then (
2477 state.mstate <- Msel ((x, y), (x, y));
2478 Glut.postRedisplay ()
2481 else (
2482 match state.mstate with
2483 | Mnone -> ()
2485 | Mzoom _ | Mscroll ->
2486 state.mstate <- Mnone
2488 | Mpan _ ->
2489 Glut.setCursor Glut.CURSOR_INHERIT;
2490 state.mstate <- Mnone
2492 | Msel ((x0, y0), (x1, y1)) ->
2493 let f l =
2494 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
2495 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh)))
2496 then
2497 match getopaque l.pageno with
2498 | Some (opaque, _) when validopaque opaque ->
2499 copysel opaque
2500 | _ -> ()
2502 List.iter f state.layout;
2503 copysel ""; (* ugly *)
2504 Glut.setCursor Glut.CURSOR_INHERIT;
2505 state.mstate <- Mnone;
2509 | _ -> ()
2512 let birdseyemouse button bstate x y
2513 (conf, leftx, pageno, hooverpageno, anchor) =
2514 match button with
2515 | Glut.LEFT_BUTTON when bstate = Glut.UP ->
2516 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2517 let rec loop = function
2518 | [] -> ()
2519 | l :: rest ->
2520 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2521 && x > margin && x < margin + l.pagew
2522 then (
2523 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
2525 else loop rest
2527 loop state.layout
2528 | Glut.OTHER_BUTTON _ -> viewmouse button bstate x y
2529 | _ -> ()
2532 let mouse bstate button x y =
2533 match state.mode with
2534 | View -> viewmouse button bstate x y
2535 | Birdseye beye -> birdseyemouse button bstate x y beye
2536 | Textentry _ -> ()
2537 | Outline _ -> ()
2540 let mouse ~button ~state ~x ~y = mouse state button x y;;
2542 let motion ~x ~y =
2543 match state.mode with
2544 | Outline _ -> ()
2545 | _ ->
2546 match state.mstate with
2547 | Mzoom _ | Mnone -> ()
2549 | Mpan (x0, y0) ->
2550 let dx = x - x0
2551 and dy = y0 - y in
2552 state.mstate <- Mpan (x, y);
2553 if conf.zoom > 1.0 then state.x <- state.x + dx;
2554 let y = clamp dy in
2555 gotoy_and_clear_text y
2557 | Msel (a, _) ->
2558 state.mstate <- Msel (a, (x, y));
2559 Glut.postRedisplay ()
2561 | Mscroll ->
2562 let y = min conf.winh (max 0 y) in
2563 let percent = float y /. float conf.winh in
2564 let y = truncate (float (state.maxy - conf.winh) *. percent) in
2565 gotoy_and_clear_text y
2568 let pmotion ~x ~y =
2569 match state.mode with
2570 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
2571 let margin = (conf.winw - (state.w + conf.scrollw)) / 2 in
2572 let rec loop = function
2573 | [] ->
2574 if hooverpageno != -1
2575 then (
2576 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
2577 Glut.postRedisplay ();
2579 | l :: rest ->
2580 if y > l.pagedispy && y < l.pagedispy + l.pagevh
2581 && x > margin && x < margin + l.pagew
2582 then (
2583 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
2584 Glut.postRedisplay ();
2586 else loop rest
2588 loop state.layout
2590 | Outline _ -> ()
2591 | _ ->
2592 match state.mstate with
2593 | Mnone ->
2594 begin match getunder x y with
2595 | Unone -> Glut.setCursor Glut.CURSOR_INHERIT
2596 | Ulinkuri uri ->
2597 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
2598 Glut.setCursor Glut.CURSOR_INFO
2599 | Ulinkgoto (page, y) ->
2600 if conf.underinfo
2601 then showtext 'p' ("age: " ^ string_of_int page);
2602 Glut.setCursor Glut.CURSOR_INFO
2603 | Utext s ->
2604 if conf.underinfo then showtext 'f' ("ont: " ^ s);
2605 Glut.setCursor Glut.CURSOR_TEXT
2608 | Mpan _ | Msel _ | Mzoom _ | Mscroll ->
2613 module State =
2614 struct
2615 open Parser
2617 let home =
2619 match Sys.os_type with
2620 | "Win32" -> Sys.getenv "HOMEPATH"
2621 | _ -> Sys.getenv "HOME"
2622 with exn ->
2623 prerr_endline
2624 ("Can not determine home directory location: " ^
2625 Printexc.to_string exn);
2629 let config_of c attrs =
2630 let apply c k v =
2632 match k with
2633 | "scroll-bar-width" -> { c with scrollw = max 0 (int_of_string v) }
2634 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
2635 | "case-insensitive-search" -> { c with icase = bool_of_string v }
2636 | "preload" -> { c with preload = bool_of_string v }
2637 | "page-bias" -> { c with pagebias = int_of_string v }
2638 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
2639 | "auto-scroll-step" ->
2640 { c with autoscrollstep = max 0 (int_of_string v) }
2641 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
2642 | "crop-hack" -> { c with crophack = bool_of_string v }
2643 | "throttle" -> { c with showall = bool_of_string v }
2644 | "highlight-links" -> { c with hlinks = bool_of_string v }
2645 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
2646 | "vertical-margin" ->
2647 { c with interpagespace = max 0 (int_of_string v) }
2648 | "zoom" ->
2649 let zoom = float_of_string v /. 100. in
2650 let zoom = max 0.01 (min 2.2 zoom) in
2651 { c with zoom = zoom }
2652 | "presentation" -> { c with presentation = bool_of_string v }
2653 | "rotation-angle" -> { c with angle = int_of_string v }
2654 | "width" -> { c with winw = max 20 (int_of_string v) }
2655 | "height" -> { c with winh = max 20 (int_of_string v) }
2656 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
2657 | "proportional-display" -> { c with proportional = bool_of_string v }
2658 | "pixmap-cache-size" -> { c with memlimit = max 2 (int_of_string v) }
2659 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
2660 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
2661 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
2662 | _ -> c
2663 with exn ->
2664 prerr_endline ("Error processing attribute (`" ^
2665 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
2668 let rec fold c = function
2669 | [] -> c
2670 | (k, v) :: rest ->
2671 let c = apply c k v in
2672 fold c rest
2674 fold c attrs;
2677 let fromstring f pos n v d =
2678 try f v
2679 with exn ->
2680 dolog "Error processing attribute (%S=%S) at %d\n%s"
2681 n v pos (Printexc.to_string exn)
2686 let bookmark_of attrs =
2687 let rec fold title page rely = function
2688 | ("title", v) :: rest -> fold v page rely rest
2689 | ("page", v) :: rest -> fold title v rely rest
2690 | ("rely", v) :: rest -> fold title page v rest
2691 | _ :: rest -> fold title page rely rest
2692 | [] -> title, page, rely
2694 fold "invalid" "0" "0" attrs
2697 let doc_of attrs =
2698 let rec fold path page rely pan = function
2699 | ("path", v) :: rest -> fold v page rely pan rest
2700 | ("page", v) :: rest -> fold path v rely pan rest
2701 | ("rely", v) :: rest -> fold path page v pan rest
2702 | ("pan", v) :: rest -> fold path page rely v rest
2703 | _ :: rest -> fold path page rely pan rest
2704 | [] -> path, page, rely, pan
2706 fold "" "0" "0" "0" attrs
2709 let setconf dst src =
2710 dst.scrollw <- src.scrollw;
2711 dst.scrollh <- src.scrollh;
2712 dst.icase <- src.icase;
2713 dst.preload <- src.preload;
2714 dst.pagebias <- src.pagebias;
2715 dst.verbose <- src.verbose;
2716 dst.scrollstep <- src.scrollstep;
2717 dst.maxhfit <- src.maxhfit;
2718 dst.crophack <- src.crophack;
2719 dst.autoscrollstep <- src.autoscrollstep;
2720 dst.showall <- src.showall;
2721 dst.hlinks <- src.hlinks;
2722 dst.underinfo <- src.underinfo;
2723 dst.interpagespace <- src.interpagespace;
2724 dst.zoom <- src.zoom;
2725 dst.presentation <- src.presentation;
2726 dst.angle <- src.angle;
2727 dst.winw <- src.winw;
2728 dst.winh <- src.winh;
2729 dst.savebmarks <- src.savebmarks;
2730 dst.memlimit <- src.memlimit;
2731 dst.proportional <- src.proportional;
2732 dst.texcount <- src.texcount;
2733 dst.sliceheight <- src.sliceheight;
2734 dst.thumbw <- src.thumbw;
2737 let unent s =
2738 let l = String.length s in
2739 let b = Buffer.create l in
2740 unent b s 0 l;
2741 Buffer.contents b;
2744 let get s =
2745 let h = Hashtbl.create 10 in
2746 let dc = { defconf with angle = defconf.angle } in
2747 let rec toplevel v t spos epos =
2748 match t with
2749 | Vdata | Vcdata | Vend -> v
2750 | Vopen ("llppconfig", attrs, closed) ->
2751 if closed
2752 then v
2753 else { v with f = llppconfig }
2754 | Vopen _ ->
2755 error "unexpected subelement at top level" s spos
2756 | Vclose tag -> error "unexpected close at top level" s spos
2758 and llppconfig v t spos epos =
2759 match t with
2760 | Vdata | Vcdata | Vend -> v
2761 | Vopen ("defaults", attrs, closed) ->
2762 let c = config_of dc attrs in
2763 setconf dc c;
2764 if closed
2765 then v
2766 else { v with f = skip "defaults" (fun () -> v) }
2768 | Vopen ("doc", attrs, closed) ->
2769 let pathent, spage, srely, span = doc_of attrs in
2770 let path = unent pathent
2771 and pageno = fromstring int_of_string spos "page" spage 0
2772 and rely = fromstring float_of_string spos "rely" srely 0.0
2773 and pan = fromstring int_of_string spos "pan" span 0 in
2774 let c = config_of dc attrs in
2775 let anchor = (pageno, rely) in
2776 if closed
2777 then (Hashtbl.add h path (c, [], pan, anchor); v)
2778 else { v with f = doc path pan anchor c [] }
2780 | Vopen (tag, _, closed) ->
2781 error "unexpected subelement in llppconfig" s spos
2783 | Vclose "llppconfig" -> { v with f = toplevel }
2784 | Vclose tag -> error "unexpected close in llppconfig" s spos
2786 and doc path pan anchor c bookmarks v t spos epos =
2787 match t with
2788 | Vdata | Vcdata -> v
2789 | Vend -> error "unexpected end of input in doc" s spos
2790 | Vopen ("bookmarks", attrs, closed) ->
2791 { v with f = pbookmarks path pan anchor c bookmarks }
2793 | Vopen (tag, _, _) ->
2794 error "unexpected subelement in doc" s spos
2796 | Vclose "doc" ->
2797 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
2798 { v with f = llppconfig }
2800 | Vclose tag -> error "unexpected close in doc" s spos
2802 and pbookmarks path pan anchor c bookmarks v t spos epos =
2803 match t with
2804 | Vdata | Vcdata -> v
2805 | Vend -> error "unexpected end of input in bookmarks" s spos
2806 | Vopen ("item", attrs, closed) ->
2807 let titleent, spage, srely = bookmark_of attrs in
2808 let page = fromstring int_of_string spos "page" spage 0
2809 and rely = fromstring float_of_string spos "rely" srely 0.0 in
2810 let bookmarks = (unent titleent, 0, page, rely) :: bookmarks in
2811 if closed
2812 then { v with f = pbookmarks path pan anchor c bookmarks }
2813 else
2814 let f () = v in
2815 { v with f = skip "item" f }
2817 | Vopen _ ->
2818 error "unexpected subelement in bookmarks" s spos
2820 | Vclose "bookmarks" ->
2821 { v with f = doc path pan anchor c bookmarks }
2823 | Vclose tag -> error "unexpected close in bookmarks" s spos
2825 and skip tag f v t spos epos =
2826 match t with
2827 | Vdata | Vcdata -> v
2828 | Vend ->
2829 error ("unexpected end of input in skipped " ^ tag) s spos
2830 | Vopen (tag', _, closed) ->
2831 if closed
2832 then v
2833 else
2834 let f' () = { v with f = skip tag f } in
2835 { v with f = skip tag' f' }
2836 | Vclose ctag ->
2837 if tag = ctag
2838 then f ()
2839 else error ("unexpected close in skipped " ^ tag) s spos
2842 parse { f = toplevel; accu = () } s;
2843 h, dc;
2846 let do_load f ic =
2848 let len = in_channel_length ic in
2849 let s = String.create len in
2850 really_input ic s 0 len;
2851 f s;
2852 with
2853 | Parse_error (msg, s, pos) ->
2854 let subs = subs s pos in
2855 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
2856 failwith ("parse error: " ^ s)
2858 | exn ->
2859 failwith ("config load error: " ^ Printexc.to_string exn)
2862 let path =
2863 let dir =
2865 let dir = Filename.concat home ".config" in
2866 if Sys.is_directory dir then dir else home
2867 with _ -> home
2869 Filename.concat dir "llpp.conf"
2872 let load1 f =
2873 if Sys.file_exists path
2874 then
2875 match
2876 (try Some (open_in_bin path)
2877 with exn ->
2878 prerr_endline
2879 ("Error opening configuation file `" ^ path ^ "': " ^
2880 Printexc.to_string exn);
2881 None
2883 with
2884 | Some ic ->
2885 begin try
2886 f (do_load get ic)
2887 with exn ->
2888 prerr_endline
2889 ("Error loading configuation from `" ^ path ^ "': " ^
2890 Printexc.to_string exn);
2891 end;
2892 close_in ic;
2894 | None -> ()
2895 else
2896 f (Hashtbl.create 0, defconf)
2899 let load () =
2900 let f (h, dc) =
2901 let pc, pb, px, pa =
2903 Hashtbl.find h (Filename.basename state.path)
2904 with Not_found -> dc, [], 0, (0, 0.0)
2906 setconf defconf dc;
2907 setconf conf pc;
2908 state.bookmarks <- pb;
2909 state.x <- px;
2910 cbput state.hists.nav pa;
2912 load1 f
2915 let add_attrs bb always dc c =
2916 let ob s a b =
2917 if always || a != b
2918 then Printf.bprintf bb "\n %s='%b'" s a
2919 and oi s a b =
2920 if always || a != b
2921 then Printf.bprintf bb "\n %s='%d'" s a
2922 and oz s a b =
2923 if always || a <> b
2924 then Printf.bprintf bb "\n %s='%f'" s (a*.100.)
2926 let w, h =
2927 if always
2928 then dc.winw, dc.winh
2929 else
2930 match state.fullscreen with
2931 | Some wh -> wh
2932 | None -> c.winw, c.winh
2934 let zoom, presentation, interpagespace, showall=
2935 if always
2936 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
2937 else
2938 match state.mode with
2939 | Birdseye (bc, _, _, _, _) ->
2940 bc.zoom, bc.presentation, bc.interpagespace, bc.showall
2941 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
2943 oi "width" w dc.winw;
2944 oi "height" h dc.winh;
2945 oi "scroll-bar-width" c.scrollw dc.scrollw;
2946 oi "scroll-handle-height" c.scrollh dc.scrollh;
2947 ob "case-insensitive-search" c.icase dc.icase;
2948 ob "preload" c.preload dc.preload;
2949 oi "page-bias" c.pagebias dc.pagebias;
2950 oi "scroll-step" c.scrollstep dc.scrollstep;
2951 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
2952 ob "max-height-fit" c.maxhfit dc.maxhfit;
2953 ob "crop-hack" c.crophack dc.crophack;
2954 ob "throttle" showall dc.showall;
2955 ob "highlight-links" c.hlinks dc.hlinks;
2956 ob "under-cursor-info" c.underinfo dc.underinfo;
2957 oi "vertical-margin" interpagespace dc.interpagespace;
2958 oz "zoom" zoom dc.zoom;
2959 ob "presentation" presentation dc.presentation;
2960 oi "rotation-angle" c.angle dc.angle;
2961 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
2962 ob "proportional-display" c.proportional dc.proportional;
2963 oi "pixmap-cache-size" c.memlimit dc.memlimit;
2964 oi "texcount" c.texcount dc.texcount;
2965 oi "slice-height" c.sliceheight dc.sliceheight;
2966 oi "thumbnail-width" c.thumbw dc.thumbw;
2969 let save () =
2970 let bb = Buffer.create 32768 in
2971 let f (h, dc) =
2972 Buffer.add_string bb "<llppconfig>\n<defaults ";
2973 add_attrs bb true dc dc;
2974 Buffer.add_string bb "/>\n";
2976 let adddoc path pan anchor c bookmarks =
2977 if bookmarks == [] && c = dc && anchor = emptyanchor
2978 then ()
2979 else (
2980 Printf.bprintf bb "<doc path='%s'"
2981 (enent path 0 (String.length path));
2983 if anchor <> emptyanchor
2984 then (
2985 let n, y = anchor in
2986 Printf.bprintf bb " page='%d'" n;
2987 Printf.bprintf bb " rely='%f'" y;
2990 if pan != 0
2991 then Printf.bprintf bb " pan='%d'" pan;
2993 add_attrs bb false dc c;
2995 begin match bookmarks with
2996 | [] -> Buffer.add_string bb "/>\n"
2997 | _ ->
2998 Buffer.add_string bb ">\n<bookmarks>\n";
2999 List.iter (fun (title, _level, page, rely) ->
3000 Printf.bprintf bb
3001 "<item title='%s' page='%d' rely='%f'/>\n"
3002 (enent title 0 (String.length title))
3003 page
3004 rely
3005 ) bookmarks;
3006 Buffer.add_string bb "</bookmarks>\n</doc>\n";
3007 end;
3011 let pan =
3012 match state.mode with
3013 | Birdseye (_, pan, _, _, _) -> pan
3014 | _ -> state.x
3016 let basename = Filename.basename state.path in
3017 adddoc basename pan (getanchor ())
3018 { conf with
3019 autoscrollstep =
3020 if state.ascrollstep > 0
3021 then state.ascrollstep
3022 else conf.autoscrollstep }
3023 (if conf.savebmarks then state.bookmarks else []);
3025 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
3026 if basename <> path
3027 then adddoc path x y c bookmarks
3028 ) h;
3029 Buffer.add_string bb "</llppconfig>";
3031 load1 f;
3032 if Buffer.length bb > 0
3033 then
3035 let tmp = path ^ ".tmp" in
3036 let oc = open_out_bin tmp in
3037 Buffer.output_buffer oc bb;
3038 close_out oc;
3039 Sys.rename tmp path;
3040 with exn ->
3041 prerr_endline
3042 ("error while saving configuration: " ^ Printexc.to_string exn)
3044 end;;
3046 let () =
3047 Arg.parse
3048 ["-p", Arg.String (fun s -> state.password <- s) , "password"
3049 ;("-v", Arg.Unit (fun () -> print_endline Help.version; exit 0),
3050 "print version")]
3051 (fun s -> state.path <- s)
3052 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\noptions:")
3054 if String.length state.path = 0
3055 then (prerr_endline "filename missing"; exit 1);
3057 State.load ();
3059 let _ = Glut.init Sys.argv in
3060 let () = Glut.initDisplayMode ~depth:false ~double_buffer:true () in
3061 let () = Glut.initWindowSize conf.winw conf.winh in
3062 let _ = Glut.createWindow ("llpp " ^ Filename.basename state.path) in
3064 let csock, ssock =
3065 if Sys.os_type = "Unix"
3066 then
3067 Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0
3068 else
3069 let addr = Unix.ADDR_INET (Unix.inet_addr_loopback, 1337) in
3070 let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3071 Unix.setsockopt sock Unix.SO_REUSEADDR true;
3072 Unix.bind sock addr;
3073 Unix.listen sock 1;
3074 let csock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
3075 Unix.connect csock addr;
3076 let ssock, _ = Unix.accept sock in
3077 Unix.close sock;
3078 let opts sock =
3079 Unix.setsockopt sock Unix.TCP_NODELAY true;
3080 Unix.setsockopt_optint sock Unix.SO_LINGER None;
3082 opts ssock;
3083 opts csock;
3084 at_exit (fun () -> Unix.shutdown ssock Unix.SHUTDOWN_ALL);
3085 ssock, csock
3088 let () = Glut.displayFunc display in
3089 let () = Glut.reshapeFunc reshape in
3090 let () = Glut.keyboardFunc keyboard in
3091 let () = Glut.specialFunc special in
3092 let () = Glut.idleFunc (Some idle) in
3093 let () = Glut.mouseFunc mouse in
3094 let () = Glut.motionFunc motion in
3095 let () = Glut.passiveMotionFunc pmotion in
3097 init ssock (conf.angle, conf.proportional, conf.texcount, conf.sliceheight);
3098 state.csock <- csock;
3099 state.ssock <- ssock;
3100 state.text <- "Opening " ^ state.path;
3101 writeopen state.path state.password;
3103 at_exit State.save;
3105 let rec handlelablglutbug () =
3107 Glut.mainLoop ();
3108 with Glut.BadEnum "key in special_of_int" ->
3109 showtext '!' " LablGlut bug: special key not recognized";
3110 handlelablglutbug ()
3112 handlelablglutbug ();